Revision 1736

View differences:

demos/joystick/Makefile
1
all:
2
	make all -C src
3

  
4
program:
5
	make program -C src
6

  
7
clean:
8
	make clean -C src
9
	make clean -C lib/src/libdragonfly
10
	make clean -C lib/src/libwireless
11

  
12
.PHONY: all program clean
demos/joystick/lib/src/libdragonfly/dragonfly_lib.h
1
/**
2
 * Copyright (c) 2007 Colony Project
3
 * 
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use,
8
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following
11
 * conditions:
12
 * 
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 * 
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
 * OTHER DEALINGS IN THE SOFTWARE.
24
 **/
25

  
26

  
27
/**
28
 * @file dragonfly_lib.h
29
 * @brief Contains other include files
30
 * 
31
 * Include this file for all the functionality of libdragonfly.
32
 *
33
 * @author Colony Project, CMU Robotics Club
34
 **/
35

  
36
#ifndef _DRAGONFLY_LIB_H_
37
#define _DRAGONFLY_LIB_H_
38

  
39
/**
40
 * @addtogroup dragonfly
41
 * @{
42
 **/
43

  
44
// Configuration definitions
45
/** @brief Initialize analog **/
46
#define ANALOG 0x01
47
/** @brief Initialize serial communications **/
48
#define SERIAL 0x02
49
/** @brief Initialize USB communications **/
50
#define USB    0x02
51
/** @brief Initialize communications **/
52
#define COMM   0x02
53
/** @brief Initialize the orb **/
54
#define ORB    0x04
55
/** @brief Initialize the motors **/
56
#define MOTORS 0x08
57
/** @brief Initialize I2C **/
58
#define I2C    0x20
59
/** @brief Initialize the buzzer **/
60
#define BUZZER 0x40
61
/** @brief Initialize the LCD screen **/
62
#define LCD    0x80
63
/** @brief Initialize the rangefinders **/
64
#define RANGE  0x0100
65
/** @brief Initialize the BOM **/
66
#define BOM  0x0200
67
/** @brief Initilize encoders **/
68
#define ENCODERS 0x400
69
/** @brief Initialize everything  **/
70
#define ALL_ON 0x07FF
71

  
72
/** @brief Initialize the board **/
73
void dragonfly_init(int config);
74

  
75
/** @} **/ //end addtogroup
76

  
77
#include <inttypes.h>
78
#include <avr/io.h>
79
#include <avr/interrupt.h>
80
#include <util/delay.h>
81
#include <util/twi.h>
82

  
83
// This file is included from the libdragonfly directory because it seems to be
84
// missing from the AVR libc distribution.
85
#include "atomic.h"
86

  
87
#include "analog.h"
88
#include "dio.h"
89
#include "time.h"
90
#include "lcd.h"
91
#include "lights.h"
92
#include "motor.h"
93
#include "serial.h"
94
#include "buzzer.h"
95
#include "rangefinder.h"
96
#include "bom.h"
97
#include "encoders.h"
98
#include "move.h"
99
#include "reset.h"
100
#include "math.h"
101
#include "eeprom.h"
102

  
103
#include <stddef.h>
104
#include <stdbool.h>
105

  
106
/** @brief shortcut for ATOMIC_BLOCK(ATOMIC_RESTORESTATE) **/
107
#define SYNC ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
108

  
109
/** @brief atomically grab a lock if it is free, return otherwise **/
110
#define REQUIRE_LOCK_OR_RETURN(LOCK) do { SYNC { if (LOCK) return; LOCK=1; } } while (0)
111

  
112
/** @brief atomically release a lock **/
113
#define RELEASE_LOCK(LOCK) do { LOCK=0; } while (0)
114

  
115

  
116

  
117
#endif
118

  
demos/joystick/lib/src/libdragonfly/buzzer.c
1
/**
2
 * Copyright (c) 2007 Colony Project
3
 * 
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use,
8
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following
11
 * conditions:
12
 * 
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 * 
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
 * OTHER DEALINGS IN THE SOFTWARE.
24
 **/
25

  
26

  
27
/**
28
 * @file buzzer
29
 * @brief Functions for using the buzzer
30
 *
31
 * Contains functions for using the buzzer.
32
 *
33
 * @author Colony Project, CMU Robotics Club
34
 *
35
 * much taken from FWR's firefly library (author: Tom Lauwers)
36
 **/
37

  
38
#include <avr/io.h>
39
#include "buzzer.h"
40
#include "time.h"
41

  
42
/**
43
 * @defgroup buzzer Buzzer
44
 * @brief Functions for controlling the buzzer.
45
 * Functions for controlling the buzzer. Include
46
 * buzzer.h to access these functions.
47
 * @{
48
 **/
49

  
50
/**
51
 * Initializes the buzzer. Must be called before any other buzzer
52
 * function may be used.
53
 **/
54
void buzzer_init( void )
55
{
56
	//NOTE: This is mostly handled by buzzer_Set_val for now
57
	// Set pin B7 to output - B7 is buzzer pin
58
	//DDRB |= _BV(DDB7);
59
	//Fast PWM Mode, Clear OCR0 on compare match and set at bottom, clock/64
60
	//TCCR2 = _BV(COM20) | _BV(WGM21)  | _BV(CS22);
61
}
62

  
63
/**
64
 * Sets the value of the buzzer's pitch.
65
 * Higher values are lower frequencies.
66
 *
67
 * @param buzz_value the value to set the buzzer's frequency too,
68
 * in the range 0-255
69
 *
70
 * @see buzzer_init, buzzer_set_freq, buzzer_off
71
 **/
72
void buzzer_set_val(unsigned int buzz_value)
73
{ 
74
	TCCR2 = _BV(COM20) | _BV(WGM21)  | _BV(CS22);
75
	DDRB |= _BV(DDB7);
76
	OCR2 = buzz_value;
77
}
78

  
79
/**
80
 * Sets the buzzer frequency. Usage of constants such as C4
81
 * is highly recommended as input to this function.
82
 * buzzer_init must be called before this function may
83
 * be used.
84
 *
85
 * @param buzz_freq the frequency to set the buzzer to
86
 *
87
 * @see buzzer_init, buzzer_set_val, buzzer_off
88
 **/
89
void buzzer_set_freq(unsigned int buzz_freq)
90
{
91
  int buzz_value;
92
  
93
  buzz_value = 62500/buzz_freq - 1;
94

  
95
  if(buzz_value > 255){
96
    buzz_value = 255;
97
  }else if(buzz_value < 0){
98
    buzz_value = 0;
99
  }
100
  
101
  buzzer_set_val(buzz_value);
102
}
103

  
104
/**
105
 * Plays the specified frequency for the specified
106
 * amount of time. This function blocks execution
107
 * until the time is completed. buzzer_init must be
108
 * called before this function can be used.
109
 *
110
 * @param ms the time in milliseconds to play the frequency
111
 * @param buzz_freq the frequency to play
112
 *
113
 * @see buzzer_init, buzzer_set_freq
114
 **/
115
void buzzer_chirp(unsigned int ms, unsigned int buzz_freq) 
116
{ 
117
  buzzer_set_freq(buzz_freq);
118
  delay_ms(ms);
119
  buzzer_off();
120
}
121

  
122
/**
123
 * Turns off the buzzer by disabling the timer0 clock.
124
 *
125
 * @see buzzer_init
126
 **/
127
void buzzer_off()
128
{
129
  // Disable clock, to halt counter
130
  TCCR2 &= 0xF8;//0b11111000;
131
  
132
  // Set buzzer pin low in case it's high
133
  PORTB &= 0xBF;//0b10111111;
134
}
135

  
136
/** @} **/ // end buzzer group
137

  
demos/joystick/lib/src/libdragonfly/i2c.h
1
/**
2
 * Copyright (c) 2007 Colony Project
3
 * 
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use,
8
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following
11
 * conditions:
12
 * 
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 * 
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
 * OTHER DEALINGS IN THE SOFTWARE.
24
 **/
25

  
26

  
27
/** @file i2c.h
28
 *  @brief Header file for I2C
29
 *
30
 *  Contains functions for I2C.
31
 *
32
 *  @author Kevin Woo and Suresh Nidhiry, Colony Project, CMU Robotics Club
33
 **/
34

  
35

  
36
#ifndef _I2C_H_
37
#define _I2C_H_
38

  
39
/** @brief Address of slave receive handler function **/
40
typedef void (*fun_srecv_t)(char);
41

  
42
/** @brief Address of master receive handler function**/
43
typedef int (*fun_mrecv_t)(char);
44

  
45
/** @brief Address of slave send handler function**/
46
typedef char (*fun_send_t)(void);
47

  
48
int i2c_init(char addr, fun_mrecv_t master_recv, fun_srecv_t slave_recv, fun_send_t slave_send);
49
int i2c_send(char dest, char* data, unsigned int bytes);
50
int i2c_request(char dest);
51

  
52
void i2c_packet_rec (char i2c_byte);
53
void i2c_packet_sniff(char data);
54
#endif
55

  
demos/joystick/lib/src/libdragonfly/serial.h
1
/**
2
 * Copyright (c) 2007 Colony Project
3
 * 
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use,
8
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following
11
 * conditions:
12
 * 
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 * 
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
 * OTHER DEALINGS IN THE SOFTWARE.
24
 **/
25

  
26

  
27
/**
28
 * @file serial.h
29
 * @brief Contains declarations for serial input and output
30
 *
31
 * Contains definitions for serial input and output.
32
 *
33
 * @author Colony Project, CMU Robotics Club
34
 * Based on Tom Lauwer's Firefly Library
35
 *
36
 **/
37

  
38
/*
39
  serial.h - Contains definitions and function prototypes for the RS232 serial port
40
  author(s): pkv
41
  
42
  Directions:
43
  Call the initialization function for the serial port you wish to use.  Then, use
44
  either the provided functions or the stdio functions (fprintf, etc) to read and
45
  write characters to the serial ports.
46
  
47
  UART Mapping:
48
  usb_*() -> UART0
49
  xbee_*() -> UART1
50
  
51
  Options: (Add the following defines to your code to configure this library)
52
  #define USB_BAUD { 115200 | 9600 } <= pick ONE value from in here
53
  #define XBEE_BAUD { 115200 | 9600 } <= pick ONE value from in here
54
  #define USE_STDIO
55
  
56
  Note: If you enable USE_STDIO, the first init function that is called will 
57
  automatically be linked to stdin, stdout, and stderr.  To use the baud rate 
58
  commands, add something like the following to your code:
59
  
60
  #define FOO_BAUD 9600
61
  
62
  **UNLESS YOU KNOW WHAT YOU ARE DOING, PLEASE DO NOT CHANGE THIS FILE**
63
  Many, many other people use this file in their code.  If you change it, you will
64
  probably break all of their nice code.  You should not need to change anything in
65
  here, except to accomodate new hardware.
66
*/
67

  
68
#ifndef _SERIAL_H
69
#define _SERIAL_H
70

  
71
#include <inttypes.h>
72
#include <avr/pgmspace.h>
73

  
74
/**
75
 * @defgroup usb USB Input / Output
76
 * @brief Functions for USB input / output
77
 *
78
 * Low level functions for USB input and output.
79
 *
80
 * @{
81
 **/
82

  
83
// if no baud rate is defined for usb, default is set here
84
#ifndef USB_BAUD
85
/** @brief the USB baud rate **/
86
#define USB_BAUD 115200
87
#endif
88

  
89
/** @brief Initialize the USB **/
90
void usb_init(void);
91
/** @brief Print a character to USB **/
92
int usb_putc(char c);
93
/** @brief Read a character from USB **/
94
int usb_getc(void);
95
/** @brief Read a character from USB without blocking **/
96
int usb_getc_nb(char *c);
97
/** @brief Print a string to USB **/
98
int usb_puts(char *s);
99
/** @brief Print a string from program space to USB **/
100
void usb_puts_P (PGM_P s);
101
/** @brief Print an integer to USB **/
102
int usb_puti(int value);
103
/** @brief Determine a hexadecimal digit **/
104
uint8_t hex_digit (uint8_t value);
105
/** @brief Print a fixed width hexadecimal representation to USB **/
106
void usb_puth16 (uint16_t value);
107
/** @brief Print a fixed width hexadecimal representation to USB **/
108
void usb_puth8(uint8_t value);
109
/** @brief Alias for usb_puth16 **/
110
static inline void usb_puth (uint16_t value) { usb_puth16 (value); };
111

  
112

  
113
/** @} **/ //end addtogroup
114

  
115
/**
116
 * @defgroup xbee XBee Input / Output
117
 * @brief Functions for XBee input / output
118
 *
119
 * Low level functions for XBee input and output.
120
 *
121
 * @{
122
 **/
123

  
124
// if no baud rate is defined for usb, default is set here
125

  
126
// if no baud rate is defined for xbee, default is set here
127
#ifndef XBEE_BAUD
128
/** @brief the XBee baud rate **/
129
#define XBEE_BAUD 9600
130
#endif
131

  
132
/** @brief Initialize the XBee **/
133
void xbee_init(void);
134
/** @brief Print a character to the XBee **/
135
int xbee_putc(char c);
136
/** @brief Read a character from the XBee **/
137
int xbee_getc(void);
138
/** @brief Read a character from the XBee without blocking **/
139
int xbee_getc_nb(char *c);
140

  
141

  
142

  
143
/** @} **/ //end addtogroup
144

  
145
#endif
146

  
demos/joystick/lib/src/libdragonfly/buzzer.h
1
/**
2
 * Copyright (c) 2007 Colony Project
3
 * 
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use,
8
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following
11
 * conditions:
12
 * 
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 * 
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
 * OTHER DEALINGS IN THE SOFTWARE.
24
 **/
25

  
26

  
27
/**
28
 * @file buzzer.h
29
 * @brief Contains definitions for using the buzzer.
30
 *
31
 * Contains definitions for using the buzzer built into
32
 * the colony robots.
33
 *
34
 * @author Colony Project, CMU Robotics Club
35
 **/
36

  
37
#ifndef _BUZZER_H_
38
#define _BUZZER_H_
39

  
40
/**
41
 * @addtogroup buzzer
42
 * @{
43
 **/
44

  
45
//Musical note definitions
46
//Source: http://www.answers.com/topic/piano-key-frequencies
47
/** @brief Middle C **/
48
#define	C4	260 //Middle C
49
/** @brief C# **/
50
#define	C4s	277 //C sharp
51
/** @brief D **/
52
#define	D4	294
53
/** @brief D# **/
54
#define	D4s	311
55
/** @brief E **/
56
#define	E4	330
57
/** @brief F **/
58
#define	F4	349
59
/** @brief F# **/
60
#define	F4s	370
61
/** @brief G **/
62
#define	G4	392
63
/** @brief G# **/
64
#define	G4s	415
65
/** @brief A **/
66
#define	A4	440
67
/** @brief A# **/
68
#define	A4s	466
69
/** @brief B **/
70
#define	B4	494
71
/** @brief C **/
72
#define	C5	523
73

  
74
/** @brief Initialize the buzzer. **/
75
void buzzer_init(void);
76
/** @brief Set the value the buzzer plays. **/
77
void buzzer_set_val(unsigned int buzz_value);
78
/** @brief Set the frequency the buzzer plays. **/
79
void buzzer_set_freq(unsigned int buzz_freq);
80
/** @brief Play a frequency for a specified time. **/
81
void buzzer_chirp(unsigned int ms, unsigned int buzz_freq);
82
/** @brief Turn the buzzer off. **/
83
void buzzer_off(void); 
84

  
85
/** @} **/ //end addtogroup
86

  
87
#endif
88

  
demos/joystick/lib/src/libdragonfly/eeprom.c
1
#include <avr/io.h>
2
#include "eeprom.h"
3

  
4
int eeprom_put_byte(unsigned int uiAddress, unsigned char ucData) {
5
    /* Wait for completion of previous write */
6
    while(EECR & (1<<EEWE));
7
    /* Set up address and data registers */
8
    EEAR = uiAddress;
9
    EEDR = ucData;
10
    /* Write logical one to EEMWE */
11
    EECR |= (1<<EEMWE);
12
    /* Start eeprom write by setting EEWE */
13
    EECR |= (1<<EEWE);
14
    
15
    return 0;
16
}
17

  
18
int eeprom_get_byte(unsigned int uiAddress, unsigned char *byte) {
19
    /* Wait for completion of previous write */
20
    while(EECR & (1<<EEWE));
21
    /* Set up address register */
22
    EEAR = uiAddress;
23
    /* Start eeprom read by writing EERE */
24
    EECR |= (1<<EERE);
25
    /* get data from data register */
26
    *byte=EEDR;
27
    
28
    return 0;
29
}
30

  
31

  
32
unsigned char get_robotid(void) {
33
    unsigned char c0, c1, c2;
34
    
35
    eeprom_get_byte(EEPROM_ROBOT_ID_ADDR, &c0);
36
    eeprom_get_byte(EEPROM_ROBOT_ID_ADDR+1, &c1);
37
    eeprom_get_byte(EEPROM_ROBOT_ID_ADDR+2, &c2);
38
    if(c0 == 'I' && c1 == 'D')
39
        return c2;
40
    else
41
        return 0xFF;
42
}
43

  
44
unsigned char get_bom_type(void) {
45
    unsigned char c0, c1, c2, c3;
46
    
47
    eeprom_get_byte(EEPROM_BOM_TYPE_ADDR, &c0);
48
    eeprom_get_byte(EEPROM_BOM_TYPE_ADDR+1, &c1);
49
    eeprom_get_byte(EEPROM_BOM_TYPE_ADDR+2, &c2);
50
    eeprom_get_byte(EEPROM_BOM_TYPE_ADDR+3, &c3);
51
    if(c0 == 'B' && c1 == 'O' && c2 == 'M')
52
        return c3;
53
    else
54
        return 0xFF;
55
}
demos/joystick/lib/src/libdragonfly/spi.c
1
/**
2
 * @file spi.c
3
 * @brief Basic SPI module to handle encoders
4
 * @author Colony Project, CMU Robotics Club
5
 *	Need to move spi.h include into dragonfly_lib.h when stable
6
 **/
7

  
8
#include <avr/interrupt.h>
9
#include "spi.h"
10

  
11

  
12
static volatile char spi_bytes; /* number of bytes to read */
13
static spi_fun_recv_t spi_recv_func; /* byte handler */
14
static spi_fun_recv_complete_t spi_recv_complete_func; /*transmission completion handler */
15

  
16
/** 
17
* @brief Initialize SPI hardware for communication.
18
* 
19
* @param recv_func The function to be called for each byte of data received.
20
* @param recv_complete_func  The function to be called at the end of a complete transmission.
21
*/
22
void spi_init (spi_fun_recv_t recv_func, spi_fun_recv_complete_t recv_complete_func)
23
{
24
    /*  Enable Interrupt, Enable SPI Module, MSB First, Master Mode, Clock div = 64 */
25
    SPCR = _BV(SPE) | _BV(SPIE) /*| _BV(DORD)*/ | _BV(MSTR) | _BV(SPR1) | _BV(SPR0);
26
    SPSR = _BV(SPI2X); 
27

  
28
    /* Set SCLK, SS, MOSI as outputs. MISO as input */
29
    DDRB |= MOSI | SCLK | SS;
30
    DDRB &= ~MISO;
31
    
32
    /* Keep SS high until transmit */
33
    PORTB |= SS;
34

  
35
    /* set function to be executed when we receive a byte */
36
    spi_recv_func = recv_func;
37
    spi_recv_complete_func = recv_complete_func;
38
    spi_bytes = 0;
39
    //usb_puts("\tspi.c Debug: SPI INITIALIZED\n");
40
}
41

  
42
/** 
43
* @brief Transfer a given byte to slave and receive a byte 
44
* 
45
* @param bytes The number of bytes to be transferred.
46
**/
47
void spi_transfer(char bytes)
48
{
49
    spi_bytes = bytes;
50
    PORTB &= ~SS; /* Set SS low to initiate transmission */
51
    SPDR = 0xff; /* Initiate data transmision */
52
}
53

  
54
ISR(SIG_SPI) 
55
{
56
	//usb_puts("Interrupt");
57
    /* only handle intterupt when we are expecting data */
58
    if(spi_bytes > 0){
59
	/* process byte */
60
	spi_recv_func(SPDR);
61
	/* if we've read all the bytes, set SS high to end transmission,
62
	 * otherwise get the next byte  */
63
	if(--spi_bytes == 0){
64
		//usb_puts("Read all bytes\r\n");
65
		PORTB |= SS;
66
		if(spi_recv_complete_func)
67
			spi_recv_complete_func();
68
	}else {
69
		//usb_puts("There are this many bytes left: "); usb_puti(spi_bytes);usb_puts("\r\n");
70
		SPDR = 0xff;
71
	}
72
    }
73
}		
demos/joystick/lib/src/libdragonfly/Makefile
1
############################
2
### Update this Section ####
3
############################
4

  
5
COLONYROOT = ../../..
6

  
7
# Target file name (without extension).
8
TARGET = libdragonfly
9

  
10
############################
11

  
12
CDEFS = 
13

  
14
# MCU name
15
MCU = atmega128
16

  
17

  
18
# Processor frequency.
19
#     This will define a symbol, F_CPU, in all source code files equal to the 
20
#     processor frequency. You can then use this symbol in your source code to 
21
#     calculate timings. Do NOT tack on a 'UL' at the end, this will be done
22
#     automatically to create a 32-bit value in your source code.
23
F_CPU = 16000000
24

  
25

  
26
# Output format. (can be srec, ihex, binary)
27
FORMAT = ihex
28

  
29

  
30
# List C source files here. (C dependencies are automatically generated.)
31
SRC = $(wildcard *.c)
32

  
33
# List Assembler source files here.
34
#     Make them always end in a capital .S.  Files ending in a lowercase .s
35
#     will not be considered source files but generated files (assembler
36
#     output from the compiler), and will be deleted upon "make clean"!
37
#     Even though the DOS/Win* filesystem matches both .s and .S the same,
38
#     it will preserve the spelling of the filenames, and gcc itself does
39
#     care about how the name is spelled on its command-line.
40
ASRC = 
41

  
42
# Optimization level, can be [0, 1, 2, 3, s]. 
43
#     0 = turn off optimization. s = optimize for size.
44
#     (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
45
OPT = s
46

  
47
# Debugging format.
48
#     Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
49
#     AVR Studio 4.10 requires dwarf-2.
50
#     AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
51
DEBUG = 
52
# dwarf-2
53

  
54
# List any extra directories to look for include files here.
55
#     Each directory must be seperated by a space.
56
#     Use forward slashes for directory separators.
57
#     For a directory that has spaces, enclose it in quotes.
58
EXTRAINCDIRS = 
59

  
60
# Compiler flag to set the C Standard level.
61
#     c89   = "ANSI" C
62
#     gnu89 = c89 plus GCC extensions
63
#     c99   = ISO C99 standard (not yet fully implemented)
64
#     gnu99 = c99 plus GCC extensions
65
CSTANDARD = -std=gnu99
66

  
67

  
68
# Place -D or -U options here
69
CDEFS += -DF_CPU=$(F_CPU)UL 
70

  
71
# Place -I options here
72

  
73

  
74
#---------------- Compiler Options ----------------
75
#  -g*:          generate debugging information
76
#  -O*:          optimization level
77
#  -f...:        tuning, see GCC manual and avr-libc documentation
78
#  -Wall...:     warning level
79
#  -Wa,...:      tell GCC to pass this to the assembler.
80
#    -adhlns...: create assembler listing
81
CFLAGS = -g$(DEBUG)
82
CFLAGS += $(CDEFS) $(CINCS)
83
CFLAGS += -O$(OPT)
84
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
85
CFLAGS += -Wall -Wstrict-prototypes
86
CFLAGS += -Wa,-adhlns=$(<:.c=.lst)
87
CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
88
CFLAGS += $(CSTANDARD)
89

  
90

  
91
#---------------- Library Options ----------------
92
# Minimalistic printf version
93
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
94

  
95
# Floating point printf version (requires MATH_LIB = -lm below)
96
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
97

  
98
# If this is left blank, then it will use the Standard printf version.
99
PRINTF_LIB = 
100
#PRINTF_LIB = $(PRINTF_LIB_MIN)
101
#PRINTF_LIB = $(PRINTF_LIB_FLOAT)
102

  
103
# Minimalistic scanf version
104
SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
105

  
106
# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
107
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
108

  
109
# If this is left blank, then it will use the Standard scanf version.
110
SCANF_LIB = 
111
#SCANF_LIB = $(SCANF_LIB_MIN)
112
#SCANF_LIB = $(SCANF_LIB_FLOAT)
113

  
114
MATH_LIB = -lm
115

  
116
#============================================================================
117

  
118

  
119
# Define programs and commands.
120
SHELL = sh
121
CC = avr-gcc
122
AR = avr-ar
123
OBJCOPY = avr-objcopy
124
OBJDUMP = avr-objdump
125
SIZE = avr-size
126
NM = avr-nm
127
AVRDUDE = avrdude
128
REMOVE = rm -f
129
REMOVEDIR = rm -rf
130
COPY = cp
131
DOXYGEN = doxygen
132
WINSHELL = cmd
133

  
134

  
135
# Define Messages
136
# English
137
MSG_ERRORS_NONE = Errors: none
138
MSG_BEGIN = -------- begin --------
139
MSG_END = --------  end  --------
140
MSG_COMPILING = Compiling:
141
MSG_CLEANING = Cleaning project:
142

  
143
# Define all object files.
144
OBJ = $(SRC:.c=.o) $(ASRC:.S=.o) 
145

  
146
# Define all listing files.
147
LST = $(SRC:.c=.lst) $(ASRC:.S=.lst) 
148

  
149
# Compiler flags to generate dependency files.
150
GENDEPFLAGS = -MD -MP -MF .dep/$(@F).d
151

  
152
# Combine all necessary flags and optional flags.
153
# Add target processor to flags.
154
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
155
ALL_ASFLAGS = rcs
156

  
157
# Default target.
158
all: begin build end
159

  
160
dist: all publish
161

  
162
publish:
163
	$(COPY) $(TARGET).a $(COLONYROOT)/lib/bin
164
	$(COPY) *.h $(COLONYROOT)/lib/include/$(TARGET)
165

  
166
.PHONY: docs
167
docs:
168
	$(DOXYGEN)
169
	$(COPY) docs/html/* $(COLONYROOT)/docs/$(TARGET)
170

  
171

  
172
build: $(OBJ)
173
	$(AR) $(ALL_ASFLAGS) $(TARGET).a $(OBJ)
174

  
175
# Eye candy.
176
# AVR Studio 3.x does not check make's exit code but relies on
177
# the following magic strings to be generated by the compile job.
178
begin:
179
	@echo
180
	@echo $(MSG_BEGIN)
181

  
182
end:
183
	@echo $(MSG_END)
184
	@echo
185

  
186
# Compile: create object files from C source files.
187
%.o : %.c
188
	@echo
189
	@echo $(MSG_COMPILING) $<
190
	$(CC) -c $(ALL_CFLAGS) $< -o $@ 
191

  
192

  
193
# Target: clean project.
194
clean: begin clean_list end
195

  
196
clean_list :
197
	@echo
198
	@echo $(MSG_CLEANING)
199
	$(REMOVE) $(TARGET).a
200
	$(REMOVE) $(OBJ)
201
	$(REMOVE) $(LST)
202
	$(REMOVE) $(SRC:.c=.s)
203
	$(REMOVE) $(SRC:.c=.d)
204
	$(REMOVEDIR) .dep
205

  
206

  
207

  
208
# Include the dependency files.
209
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
210

  
demos/joystick/lib/src/libdragonfly/eeprom.h
1
/**
2
 * @file eeprom.h
3
 * @brief handles eeprom storage for persistent data
4
 *
5
 * Contains functions and definitions for reading and writing to eeprom
6
 *
7
 * @author Colony Project, Brad Neuman
8
 */
9
 
10
 #ifndef _EEPROM_H_
11
 #define _EEPROM_H_
12
 
13
 #define EEPROM_ROBOT_ID_ADDR 0x10
14
 #define EEPROM_BOM_TYPE_ADDR 0x14
15
 
16
 /** @brief store a byte to eeproem
17
  *  @return 0 if success, nonzero on failure
18
  */
19
 int eeprom_put_byte(unsigned int addr, unsigned char byte);
20
 
21
 /** @brief reads a byte from eeprom
22
  *
23
  *  Pass it thge address and a pointer to a byte where the byte at the
24
  *  address will be stored
25
  *
26
  *  @return 0 if successful (byte is set to the eeprom value at addr), 
27
  *  nonzero if there was a problem
28
   */
29
 int eeprom_get_byte(unsigned int addr, unsigned char *byte);
30
 
31
 /** @brief get stored robot ID
32
  *
33
  *  checks that EEPROM has been programed with an ID and returns it
34
  *
35
  *  @return the robot id, if it is stored. If it returns 0xFF it is probably invalid
36
  */
37
 unsigned char get_robotid(void);
38
  
39
   /** @brief get stored robot ID
40
  *
41
  * checks that EEPROM has been programed with an BOM type and returns it
42
  *
43
  *  @return the robot bom type as defined in bom.h, if it is stored. If it returns 0xFF it is probably invalid
44
  */
45
 unsigned char get_bom_type(void);
46
 
47
 #endif
demos/joystick/lib/src/libdragonfly/time.c
1
/**
2
 * Copyright (c) 2007 Colony Project
3
 * 
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use,
8
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following
11
 * conditions:
12
 * 
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 * 
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
 * OTHER DEALINGS IN THE SOFTWARE.
24
 **/
25

  
26

  
27
/**
28
 * @file time.c
29
 * @brief Timer code
30
 *
31
 * Implementation of functions for timers.
32
 *
33
 * @author Colony Project, CMU Robotics Club
34
 **/
35

  
36
/*
37
  time.c
38
  anything that requires a delay
39
  mostly delay_ms
40

  
41
  author: Robotics Club, Colony Project
42

  
43
  Change Log:
44
  2.5.07 - Kevin
45
  Aaron fixed the orb/servo code and made them use timer3 but compare registers B and C. He hard set the prescaler
46
  to 8 so the RTC broke. Changed it so that we use a 8 prescaler which sets the compare match at 1/16th of a second.
47
  You now count how many 16ths of a second you want until you trigger your actual interrupt. Works. Changed defines
48
  for time so you can still call rtc_init with a scale but now it is defined in terms of actual time like second, quarter_second
49
  etc. Read that section in the time.h file for more information. Tested and works, though the clock drifts more than
50
  it used to
51
  1.30.07 - Kevin
52
  Modified the clock to run on timer3 on the Dragonfly. Works with decent accuracy. Using a prescaler of 256
53
  the timer counts up to a precomputer value which will trigger an interrupt and reset the timer. Multiples of
54
  256 change it by that multiple. Refer to the time.h file for all possible prescalers.
55
  The interrupt will call a specified function _rtc_func every pulse.
56
  All of it has been tested and it works.
57

  
58
*/
59
#include <avr/interrupt.h>
60
#include <util/delay.h>
61
#include "time.h"
62

  
63

  
64
/* Calculate how many cycles to delay for to get 1 ms. Based on F_CPU which should be defined by the makefile */
65
#ifdef F_CPU
66
#define WAIT_CYCLES ((F_CPU / 1000) / 10)
67
#else
68
#define WAIT_CYCLES (8000 / 10)
69
#endif
70

  
71
static volatile int _rtc_val = 0;
72
static volatile int _rtc_pulse = 0;
73
static volatile int _rtc_scale = 32;	//Defaults to 1 Second per pulse
74
static void (*_rtc_f)(void) = 0;
75

  
76

  
77

  
78
/**
79
 * @defgroup time Time
80
 * @brief Time functions
81
 * 
82
 * Functions dealing with time.
83
 * 
84
 * @{
85
 **/
86

  
87
/**
88
 * Delays for the specified number of milliseconds.
89
 * It depends on F_CPU to be defined in order to calculate how many cycles
90
 * it should delay. If it is not defined, a default clock of 8MHz is assumed.
91
 * 
92
 * We use _delay_loop_2 which will run assembly instructions that should be
93
 * 4 cycles long. Optimizations must be enabled for this to be true.
94
 * That function is called to ensure around 1ms per execution. To generate
95
 * multiple ms we run a for loop of how many milliseconds are desired.
96
 *
97
 * The error should be just the skew on the oscillator as the formula to 
98
 * calculate delay cycles should always be a whole number. The is some skew
99
 * in practice though it is unavoidable. Delaying for less than 1s should make
100
 * the error negligable.
101
 *
102
 * @param ms the number of milliseconds to delay for
103
 **/
104
void delay_ms(int ms) {
105
    for (; ms > 0; ms--) {
106
        _delay_loop_2(WAIT_CYCLES);
107
    }
108
}
109

  
110

  
111
/* 	Prescales defined in time.h. SECOND will give you 1 second.
112
	More scales are defined in the time.h file.
113
	rtc_func is the address to a function that you want called every clock tick. */
114
/**
115
 * Initializes the real time clock. Prescales are defined in time.h.
116
 * For example, SECOND will give 1 second. The specified function is
117
 * called every clock tick. For the real time clock to activate,
118
 * interrupts must be enabled. (through sei() )
119
 *
120
 * @param prescale_opt the period with which the timer is triggered
121
 * @param rtc_func the function called when the timer is triggered
122
 *
123
 * @see rtc_get, rtc_reset
124
 *
125
 **/
126
void rtc_init(int prescale_opt, void (*rtc_func)(void)) {
127
	
128
  //Clear timer register for Timer 3
129
  TCNT3 = 0;
130
	
131
  /* 	This sets the Waveform Generation Module to CTC (Clear Timer on Compare) Mode (100)
132
	See page135 in Atmega128 Docs for more modes and explanations */
133
  TCCR3B |= _BV(WGM32);
134
	
135
  /* 	This sets the prescaler for the system clock (8MHz) ie: divides the clock by some number.
136
	Currently set to a prescaler of 8 because that is what the orb and servos use (they are on this timer as well)
137
	See page137 in Atemga128 Docs for all the available prescalers */
138
  TCCR3B |= _BV(CS31);
139
	
140
  /* 	Sets the two regsiters that we compare against. So the timer counts up to this number and
141
	then resets back to 0 and calls the compare match interrupt.
142
	8x10^6 / 8 = 1/16 Second. All values are based off of this number. Do not change it unless you
143
	are l337*/
144
		
145
  OCR3A = 0xF424;	
146

  
147
  /* 	Enable Output Compare A Interrupt. When OCR3A is met by the timer TCNT3 this interrupt will be
148
	triggerd. (See page140 in Atmega128 Docs for more information */
149
  ETIMSK |= _BV(OCIE3A);
150
	
151
  /*	Store the pointer to the function to be used in the interrupt */
152
  _rtc_f = rtc_func;
153
	
154
  /*	Store how many 1/16ths of a second you want to let by before triggering an interrupt */
155
  _rtc_scale = prescale_opt;
156
}
157

  
158
/**
159
 * Returns the time elapsed in seconds since the last call to
160
 * rtc_init or rtc_reset.
161
 *
162
 * @return the number of seconds since the last call to rtc_init or rtc_reset
163
 *
164
 * @see rtc_init, rtc_reset
165
 **/
166
int rtc_get(void) {
167
  return _rtc_val;
168
}
169

  
170
/**
171
 * Resets the real time clock counter to 0.
172
 *
173
 * @see rtc_init, rtc_get
174
 **/
175
void rtc_reset(void) {
176
  _rtc_val = 0;
177
}
178

  
179
/** @} **/ //end defgroup
180

  
181
/*	Called every pulse. Function in _rtc_f is called every _rtc_scale and also the counter is updated.
182
	Bascially, since the pulse is hard set at 1/16s  you want to count how many 16ths of a second have passed
183
	and when it reaches the amount of time you want, execute the code. */
184
SIGNAL(TIMER3_COMPA_vect) {
185

  
186
  if (_rtc_pulse ==  _rtc_scale) {
187
    //Increment the real time clock counter
188
    _rtc_val++;
189
		
190
    //Calls the function tied to the real time clock if defined
191
    if(_rtc_f != 0)
192
      _rtc_f();
193
		
194
    //Resets the pulse until the next scale is matched
195
    _rtc_pulse = 0;
196
  }	
197
	
198
  //Updates the amount of pulses seen since the last scale match
199
  _rtc_pulse++;
200
	
201
}
demos/joystick/lib/src/libdragonfly/spi.h
1
/**
2
 * @file spi.h
3
 * @brief Definitions for SPI
4
 * @author Colony Project, CMU Robotics Club
5
 **/
6

  
7
/**
8
 * @addtogroup spi
9
 * @{
10
 **/
11

  
12
#ifndef __SPI_H__
13
#define __SPI_H__
14

  
15
#define DOUBLE_SCK 1
16
#define SPR0_BIT 1
17

  
18
#define MASTER 1
19
#define SLAVE 0
20

  
21
#define MOSI _BV(PB2)
22
#define MISO _BV(PB3)
23
#define SS   _BV(PB0)
24
#define SCLK _BV(PB1)
25

  
26
typedef void (*spi_fun_recv_t)(char);
27
typedef void (*spi_fun_recv_complete_t)(void);
28

  
29
/** 
30
* @brief Initialize SPI
31
* 
32
* @param spi_fun_recv_t The function that handles SPI data, byte for byte.
33
* @param spi_fun_recv_complete_t  Called on a completed transmission - typically for cleaning up.
34
*/
35
void spi_init (spi_fun_recv_t, spi_fun_recv_complete_t);
36

  
37
/** 
38
* @brief Initialize SPI transfer.
39
* 
40
* @param char The number of bytes to transfer.
41
*/
42
void spi_transfer (char);
43

  
44
/**@}**/ //end group
45

  
46
#endif
demos/joystick/lib/src/libdragonfly/analog.c
1
/**
2
 * Copyright (c) 2007 Colony Project
3
 * 
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use,
8
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following
11
 * conditions:
12
 * 
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 * 
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
 * OTHER DEALINGS IN THE SOFTWARE.
24
 **/
25

  
26
/**
27
 * @file analog.c
28
 * @brief Analog input and output
29
 *
30
 * Contains functions for manipulating the ADC on the Dragonfly board.
31
 * 
32
 * @author Colony Project, CMU Robotics Club
33
 * originally taken from fwr analog file (author: Tom Lauwers)
34
 * loop code written by Kevin Woo and James Kong
35
 **/
36

  
37
#include <util/delay.h>
38
#include <avr/interrupt.h>
39
#include "analog.h"
40
#include "serial.h"
41
// Internal Function Prototypes
42
void set_adc_mux(int which);
43

  
44
/**
45
 * @defgroup analog Analog
46
 * Functions for manipulation the ADC on the dragonfly board.
47
 * All definitions may be found in analog.h.
48
 *
49
 * @{
50
 **/
51

  
52
volatile int adc_loop_status = ADC_LOOP_STOPPED;
53
volatile int adc_sig_stop_loop = 0;
54
volatile int adc_current_port = 0;
55
volatile adc_t an_val[11];
56

  
57
/**
58
 * Initializes the ADC.
59
 * Call analog_init before reading from the analog ports.
60
 *
61
 * @see analog8, analog10, analog_get8, analog_get10
62
 *
63
 * @bug First conversion takes a performance penalty of
64
 * 25 vs. 13 ADC clock cycles of successive conversions.
65
 * Analog_init should run a dummy conversion to pre-empt
66
 * this.
67
 *
68
 * For good 10-bit precision, ACD clock must be between
69
 * 50kHz and 200kHz. Currently, ADC clock is fixed at
70
 * 125kHz using 1/64prescalar. However, most code uses
71
 * 8-bit precision which can work at ADC clock speeds
72
 * higher than 200kHz. Experimental tests needed to
73
 * determine highest clock speed for accurate 8-bit ADC.
74
 *
75
 **/
76
void analog_init(int start_conversion) {
77
	for (int i = 0; i < 11; i++) {
78
		an_val[i].adc10 = 0;
79
		an_val[i].adc8 = 0;
80
	}
81

  
82
	// ADMUX register
83
	// Bit 7,6 - Set voltage reference to AVcc (0b01)
84
	// Bit 5 - ADLAR set to simplify moving from register
85
	// Bit 4 - X
86
	// Bit 3:0 - Sets the current channel
87
	// Initializes to read from AN1 first (AN0 is reservered for the BOM)
88
	ADMUX = 0;
89
	ADMUX |= ADMUX_OPT | _BV(MUX0);
90

  
91
	// ADC Status Register A
92
	// Bit 7 - ADEN is set (enables analog)
93
	// Bit 6 - Start conversion bit is set (must be done once for free-running mode)
94
	// Bit 5 - Enable Auto Trigger (for free running mode) NOT DOING THIS RIGHT NOW
95
	// Bit 4 - ADC interrupt flag, 0
96
	// Bit 3 - Enable ADC Interrupt (required to run free-running mode)
97
	// Bits 2-0 - Set to create a clock divisor of 128, to make ADC clock = 8,000,000/64 = 125kHz EDIT: changed to 8MHz
98
	ADCSRA = 0;
99
	ADCSRA |= _BV(ADEN) | _BV(ADPS0);
100
	
101
	// Set external mux lines to outputs
102
	DDRG |= 0x1C;
103
	
104
	// Set up first port for conversions
105
	set_adc_mux(0x00);
106
	adc_current_port = AN1;
107

  
108
	//Start the conversion loop if requested
109
	//if (start_conversion)
110
		//analog_start_loop();
111
		
112
	//Conversion loop disabled by default
113
}	
114

  
115
/**
116
 * Returns the 8-bit analog conversion of which from
117
 * the lookup table. If the requested port is the BOM_PORT
118
 * you will get an automatic 0 since the BOM_PORT is not
119
 * read in the loop and not stored. If you need that port
120
 * you should use the functions in bom.c. There is an analog_get8
121
 * function which for instant lookups but should be avoided unless
122
 * you know what you're doing.
123
 *
124
 * @param which the port that you want to read
125
 *
126
 * @bug may cause a seg fault if which is a larger value
127
 * than exists in an_val table. Not sure if we should fix
128
 * this or not since it would add overhead.
129
 *
130
 * @return 8-bit analog value for the which port requested
131
 *
132
 * @see analog10, analog_get8, analog_get10
133
 **/
134
unsigned int analog8(int which) {
135
	if (which == BOM_PORT) {
136
		return 0;
137
	} else {
138
		return an_val[which - 1].adc8;
139
	}
140
}
141

  
142
/**
143
 * Returns the 10-bit analog conversion of which from
144
 * the lookup table. If the requested port is the BOM_PORT
145
 * you will get an automatic 0 since the BOM_PORT is not
146
 * read in the loop and not stored. If you need that port
147
 * you should use the functions in bom.c. There is an analog_get10
148
 * function which for instant lookups but should be avoided unless
149
 * you know what you are doing.
150
 *
151
 * @param which the port that you want to read
152
 *
153
 * @bug may cause a seg fault if which is a larger value
154
 * than exists in an_val table. Not sure if we should fix
155
 * this or not since it would add overhead.
156
 *
157
 * @return 10-bit analog value for the which port requested
158
 *
159
 * @see analog8, analog_get8, analog_get10
160
 **/
161
unsigned int analog10(int which) {
162
	if (which == BOM_PORT) {
163
		return 0;
164
	} else {
165
		return an_val[which - 1].adc10;
166
	}
167
}
168

  
169
/**
170
 * Starts the analog update loop. Will continue to run
171
 * until analog_stop_loop is called.
172
 *
173
 * @see analog_stop_loop, analog_loop_status
174
 **/
175
void analog_start_loop(void) {
176
	if(adc_loop_status != ADC_LOOP_RUNNING){
177
		//Start the conversion, enable ADC interrupt
178
		ADCSRA |= _BV(ADIE);
179
		ADCSRA |= _BV(ADSC);
180
		adc_loop_status = ADC_LOOP_RUNNING;
181
	}
182
}
183

  
184
/**
185
 * Stops the analog update loop. If there is a current
186
 * read, it will finish up and be stored before the loop
187
 * is interrupted. No further updates will be made until
188
 * the loop is started again.
189
 *
190
 * @see analog_start_loop, analog_loop_status
191
 **/
192
void analog_stop_loop() {
193
	//Signal to stop after the next conversion
194
	adc_sig_stop_loop = 1;
195
}
196

  
197
/**
198
 * Returns the status of loop. 0 for stopped.
199
 * 1 for running. 2 for paused.
200
 *
201
 * @see analog_start_loop, analog_stop_loop
202
 **/
203
int analog_loop_status(void) {
204
	return adc_loop_status;
205
}
206

  
207
/**
208
 * Reads an 8-bit number from an analog port.
209
 * analog_init must be called before using this function.
210
 * The analog loop must also be stopped before using this
211
 * function or you will mess up the lookup table. You
212
 * must also reenabled the loop when you are done unless
213
 * you are doing more instant reads. See analog_stop_loop
214
 * and analog_start_loop for more information about the loop.
215
 * 
216
 * @param which the analog port to read from. One of
217
 * the constants AN0 - AN7.
218
 *
219
 * @return the 8-bit input to the specified port
220
 *
221
 * @see analog_init, analog_get10, analog8, analog_stop_loop,
222
 * analog_start_loop
223
 **/
224
unsigned int analog_get8(int which) {	
225
	// Let any previous conversion finish
226
	while (ADCSRA & _BV(ADSC));
227
	
228
	if(which < EXT_MUX) {
229
		ADMUX = ADMUX_OPT + which;
230
	} else {
231
		ADMUX = ADMUX_OPT + EXT_MUX;
232
		set_adc_mux(which - 8);
233
	}
234
	
235
	// Start the conversion
236
	ADCSRA |= _BV(ADSC);
237

  
238
	// Wait for the conversion to finish
239
	while (ADCSRA & _BV(ADSC));
240

  
241
	return ADCH; //since we left aligned the data, ADCH is the 8 MSB.
242
}
243

  
244
/**
245
 * Reads an 10-bit number from an analog port.
246
 * analog_init must be called before using this function.
247
 * The analog loop must also be stopped before using this
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff