Revision 1496
Reverted "libdragonfly" folder back to version before Init Checking was implemented and did "make dist" to recompile the library. BOM LEDs now shine
correctly.
trunk/code/lib/include/libdragonfly/spi.h | ||
---|---|---|
23 | 23 |
#define SS _BV(PB0) |
24 | 24 |
#define SCLK _BV(PB1) |
25 | 25 |
|
26 |
typedef int (*spi_fun_recv_t)(char);
|
|
26 |
typedef void (*spi_fun_recv_t)(char);
|
|
27 | 27 |
typedef void (*spi_fun_recv_complete_t)(void); |
28 | 28 |
|
29 | 29 |
/** |
... | ... | |
32 | 32 |
* @param spi_fun_recv_t The function that handles SPI data, byte for byte. |
33 | 33 |
* @param spi_fun_recv_complete_t Called on a completed transmission - typically for cleaning up. |
34 | 34 |
*/ |
35 |
int spi_init (spi_fun_recv_t, spi_fun_recv_complete_t);
|
|
35 |
void spi_init (spi_fun_recv_t, spi_fun_recv_complete_t);
|
|
36 | 36 |
|
37 | 37 |
/** |
38 | 38 |
* @brief Initialize SPI transfer. |
39 | 39 |
* |
40 | 40 |
* @param char The number of bytes to transfer. |
41 | 41 |
*/ |
42 |
int spi_transfer (char);
|
|
42 |
void spi_transfer (char);
|
|
43 | 43 |
|
44 | 44 |
/**@}**/ //end group |
45 | 45 |
|
trunk/code/lib/include/libdragonfly/time.h | ||
---|---|---|
37 | 37 |
#ifndef _TIME_H_ |
38 | 38 |
#define _TIME_H_ |
39 | 39 |
|
40 |
|
|
41 | 40 |
/* Predefined times for prescale_opt in time.c. |
42 | 41 |
To make you own, know that a pulse is 1/16th of a second. You cannot get less than this. To get more, you need |
43 | 42 |
to know how many 16ths of a second are in the time you want. (Time_desired * 16 = prescaler_opt) |
... | ... | |
64 | 63 |
/** @brief Delay execution for the specified time **/ |
65 | 64 |
void delay_ms(int ms) ; |
66 | 65 |
/** @brief Enable the realtime clock **/ |
67 |
int rtc_init(int prescale_opt, void (*rtc_func)(void));
|
|
66 |
void rtc_init(int prescale_opt, void (*rtc_func)(void));
|
|
68 | 67 |
/** @brief Reset the counter of the realtime clock **/ |
69 | 68 |
void rtc_reset(void); |
70 | 69 |
/** @brief Get the value of the realtime clock. **/ |
trunk/code/lib/include/libdragonfly/motor.h | ||
---|---|---|
39 | 39 |
#define _MOTOR_H |
40 | 40 |
|
41 | 41 |
#include <avr/io.h> |
42 |
|
|
43 | 42 |
/** |
44 | 43 |
* @addtogroup motors |
45 | 44 |
* @{ |
... | ... | |
51 | 50 |
#define BACKWARD 0 |
52 | 51 |
|
53 | 52 |
/** @brief Initialize the motors **/ |
54 |
int motors_init(void);
|
|
53 |
void motors_init(void);
|
|
55 | 54 |
/** @brief Set speed and direction of motor1 |
56 | 55 |
* @deprecated use the left motor function instead. it's more intuitive and easier to read.**/ |
57 |
int motor1_set(int direction, int speed);
|
|
56 |
void motor1_set(int direction, int speed);
|
|
58 | 57 |
/** @brief Set speed and direction of motor2 |
59 | 58 |
* @deprecated use the right motor function instead. it's more intuitive and easier to read.**/ |
60 |
int motor2_set(int direction, int speed);
|
|
59 |
void motor2_set(int direction, int speed);
|
|
61 | 60 |
/** @brief Set speed and direction of left motor **/ |
62 |
int motor_l_set(int direction, int speed);
|
|
61 |
void motor_l_set(int direction, int speed);
|
|
63 | 62 |
/** @brief Set speed and direction of right motor **/ |
64 |
int motor_r_set(int direction, int speed);
|
|
63 |
void motor_r_set(int direction, int speed);
|
|
65 | 64 |
/** @brief Turn the motors off **/ |
66 |
int motors_off(void);
|
|
65 |
void motors_off(void);
|
|
67 | 66 |
|
68 | 67 |
/**@}**/ // end addtogroup |
69 | 68 |
|
trunk/code/lib/include/libdragonfly/analog.h | ||
---|---|---|
113 | 113 |
|
114 | 114 |
/** @brief Initialize analog ports. Will start running a loop |
115 | 115 |
if start_conversion is ADC_START.**/ |
116 |
int analog_init(int start_conversion);
|
|
116 |
void analog_init(int start_conversion);
|
|
117 | 117 |
/** @brief starts the analog loop. Doesn't do anything if the loop is already running. **/ |
118 |
int analog_start_loop(void);
|
|
118 |
void analog_start_loop(void);
|
|
119 | 119 |
/** @brief Stops the analog loop. Doesn't do anything if the loop is already stopped. **/ |
120 |
int analog_stop_loop(void);
|
|
120 |
void analog_stop_loop(void);
|
|
121 | 121 |
/** @brief Returns the status of the analog loop. **/ |
122 | 122 |
int analog_loop_status(void); |
123 | 123 |
/** @brief Returns an 8-bit analog value from the look up table. Use this instead of analog_get8. **/ |
trunk/code/lib/include/libdragonfly/encoders.h | ||
---|---|---|
1 | 1 |
/** |
2 |
* Copyright (c) 2007 Colony Project |
|
3 | 2 |
* |
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 | 3 |
* @file encoders.h |
29 | 4 |
* @brief Contains functions for reading encoder values. |
30 | 5 |
* |
... | ... | |
76 | 51 |
#define MagINCn _BV(1) |
77 | 52 |
#define MagDECn _BV(0) |
78 | 53 |
|
79 |
#ifdef BUFFER_SIZE |
|
80 |
#error BUFFER_SIZE already defined! |
|
81 |
#endif |
|
82 |
|
|
83 | 54 |
/** @brief Buffer size **/ |
84 | 55 |
#define BUFFER_SIZE 46 |
85 | 56 |
|
86 | 57 |
#define ERR_VEL 1024 |
87 | 58 |
|
88 | 59 |
/** @brief Initialize encoders. **/ |
89 |
int encoders_init(void);
|
|
60 |
void encoders_init(void);
|
|
90 | 61 |
/** @brief Read instantaneous encoder value. **/ |
91 | 62 |
int encoder_read(char encoder); |
92 | 63 |
|
... | ... | |
101 | 72 |
/** @brief Get total distance traveled. **/ |
102 | 73 |
int encoder_get_dx(char encoder); |
103 | 74 |
/** @brief Reset distance counter. **/ |
104 |
int encoder_rst_dx(char encoder);
|
|
75 |
void encoder_rst_dx(char encoder);
|
|
105 | 76 |
/** @brief Get time count: The number of encoder reads that have occurred. **/ |
106 | 77 |
int encoder_get_tc(void); |
107 | 78 |
/** @brief Reset the time count. **/ |
108 |
int encoder_rst_tc(void);
|
|
79 |
void encoder_rst_tc(void);
|
|
109 | 80 |
|
110 | 81 |
/** @brief Waits for the next n encoder reading, then returns. **/ |
111 |
int encoder_wait( int nReadings );
|
|
82 |
void encoder_wait( int nReadings );
|
|
112 | 83 |
|
113 | 84 |
/**@}**/ //end group |
114 | 85 |
|
trunk/code/lib/include/libdragonfly/dragonfly_lib.h | ||
---|---|---|
41 | 41 |
* @{ |
42 | 42 |
**/ |
43 | 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 |
|
|
44 | 72 |
/** @brief Initialize the board **/ |
45 | 73 |
void dragonfly_init(int config); |
46 | 74 |
|
... | ... | |
56 | 84 |
// missing from the AVR libc distribution. |
57 | 85 |
#include "atomic.h" |
58 | 86 |
|
59 |
#include "dragonfly_defs.h" |
|
60 | 87 |
#include "analog.h" |
61 | 88 |
#include "dio.h" |
62 | 89 |
#include "time.h" |
... | ... | |
76 | 103 |
#include <stddef.h> |
77 | 104 |
#include <stdbool.h> |
78 | 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 |
|
|
79 | 117 |
#endif |
80 | 118 |
|
trunk/code/lib/include/libdragonfly/bom.h | ||
---|---|---|
57 | 57 |
|
58 | 58 |
|
59 | 59 |
/** @brief Initialize the bom according to bom type **/ |
60 |
int bom_init(char type);
|
|
60 |
void bom_init(char type);
|
|
61 | 61 |
|
62 | 62 |
/** @brief Refresh bom_val[] with new values from analog8. analog_init and bom_init must be called for this to work. **/ |
63 |
int bom_refresh(int bit_field);
|
|
63 |
void bom_refresh(int bit_field);
|
|
64 | 64 |
|
65 | 65 |
/** @brief Gets the bom reading from bom_val[which]. Call bom_refresh beforehand to read new bom values. **/ |
66 | 66 |
int bom_get(int which); |
... | ... | |
72 | 72 |
int bom_get_max10(int *dist); |
73 | 73 |
|
74 | 74 |
/** @brief Enables the selected bom leds on a BOM1.5 **/ |
75 |
int bom_set_leds(int bit_field);
|
|
75 |
void bom_set_leds(int bit_field);
|
|
76 | 76 |
|
77 | 77 |
/** @brief (DEPRECATED) Gets and compares all bom values. Returns the index to the highest value element since last refresh. **/ |
78 | 78 |
int get_max_bom(void); |
79 | 79 |
|
80 | 80 |
/** @brief Turns on all BOM leds, or turns on enabled leds on a BOM1.5. **/ |
81 |
int bom_on(void);
|
|
81 |
void bom_on(void);
|
|
82 | 82 |
|
83 | 83 |
/** @brief Turns off all bom leds. **/ |
84 |
int bom_off(void);
|
|
84 |
void bom_off(void);
|
|
85 | 85 |
|
86 | 86 |
/** @} **/ |
87 | 87 |
|
trunk/code/lib/include/libdragonfly/serial.h | ||
---|---|---|
87 | 87 |
#endif |
88 | 88 |
|
89 | 89 |
/** @brief Initialize the USB **/ |
90 |
int usb_init(void);
|
|
90 |
void usb_init(void);
|
|
91 | 91 |
/** @brief Print a character to USB **/ |
92 | 92 |
int usb_putc(char c); |
93 | 93 |
/** @brief Read a character from USB **/ |
... | ... | |
97 | 97 |
/** @brief Print a string to USB **/ |
98 | 98 |
int usb_puts(char *s); |
99 | 99 |
/** @brief Print a string from program space to USB **/ |
100 |
int usb_puts_P (PGM_P s);
|
|
100 |
void usb_puts_P (PGM_P s);
|
|
101 | 101 |
/** @brief Print an integer to USB **/ |
102 | 102 |
int usb_puti(int value); |
103 | 103 |
/** @brief Determine a hexadecimal digit **/ |
104 | 104 |
uint8_t hex_digit (uint8_t value); |
105 | 105 |
/** @brief Print a fixed width hexadecimal representation to USB **/ |
106 |
int usb_puth16 (uint16_t value);
|
|
106 |
void usb_puth16 (uint16_t value);
|
|
107 | 107 |
/** @brief Print a fixed width hexadecimal representation to USB **/ |
108 |
int usb_puth8(uint8_t value);
|
|
108 |
void usb_puth8(uint8_t value);
|
|
109 | 109 |
/** @brief Alias for usb_puth16 **/ |
110 | 110 |
static inline void usb_puth (uint16_t value) { usb_puth16 (value); }; |
111 | 111 |
|
... | ... | |
130 | 130 |
#endif |
131 | 131 |
|
132 | 132 |
/** @brief Initialize the XBee **/ |
133 |
int xbee_init(void);
|
|
133 |
void xbee_init(void);
|
|
134 | 134 |
/** @brief Print a character to the XBee **/ |
135 | 135 |
int xbee_putc(char c); |
136 | 136 |
/** @brief Read a character from the XBee **/ |
trunk/code/lib/include/libdragonfly/rangefinder.h | ||
---|---|---|
58 | 58 |
#define MAX_IR_ADC8 133 |
59 | 59 |
|
60 | 60 |
/** @brief Initialize the rangefinders **/ |
61 |
int range_init(void);
|
|
61 |
void range_init(void);
|
|
62 | 62 |
/** @brief Read the distance from a rangefinder **/ |
63 | 63 |
int range_read_distance(int range_id); |
64 | 64 |
/** @brief Convert logarithmic-scale distance readings to a linear scale **/ |
trunk/code/lib/include/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 |
|
|
1 | 10 |
#ifndef _EEPROM_H_ |
2 | 11 |
#define _EEPROM_H_ |
3 | 12 |
|
4 | 13 |
#define EEPROM_ROBOT_ID_ADDR 0x10 |
5 | 14 |
#define EEPROM_BOM_TYPE_ADDR 0x14 |
6 | 15 |
|
16 |
/** @brief store a byte to eeproem |
|
17 |
* @return 0 if success, nonzero on failure |
|
18 |
*/ |
|
7 | 19 |
int eeprom_put_byte(unsigned int addr, unsigned char byte); |
8 | 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 |
*/ |
|
9 | 29 |
int eeprom_get_byte(unsigned int addr, unsigned char *byte); |
10 | 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 |
*/ |
|
11 | 37 |
unsigned char get_robotid(void); |
12 | 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 |
*/ |
|
13 | 45 |
unsigned char get_bom_type(void); |
14 | 46 |
|
15 | 47 |
#endif |
trunk/code/projects/libdragonfly/dragonfly_defs.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_def.h |
|
29 |
* @brief Contains definitions for dragonfly library |
|
30 |
* |
|
31 |
* Should be included in all dragonfly library source files. |
|
32 |
* Does not need to be included by user programs. |
|
33 |
* |
|
34 |
* @author Colony Project, CMU Robotics Club |
|
35 |
**/ |
|
36 |
|
|
37 |
#ifndef _DRAGONFLY_DEFS_H_ |
|
38 |
#define _DRAGONFLY_DEFS_H_ |
|
39 |
|
|
40 |
/** |
|
41 |
* @addtogroup dragonfly |
|
42 |
* @{ |
|
43 |
**/ |
|
44 |
|
|
45 |
//return value definitions |
|
46 |
/** @brief Error code for init failure **/ |
|
47 |
#define ERROR_INIT_FAILED 1 |
|
48 |
/** @brief Error code for duplicate init calls **/ |
|
49 |
#define ERROR_INIT_ALREADY_INITD 2 |
|
50 |
/** @brief Error code for not calling init **/ |
|
51 |
#define ERROR_LIBRARY_NOT_INITD 3 |
|
52 |
|
|
53 |
// Configuration definitions |
|
54 |
/** @brief Initialize analog **/ |
|
55 |
#define ANALOG 0x01 |
|
56 |
/** @brief Initialize serial communications **/ |
|
57 |
#define SERIAL 0x02 |
|
58 |
/** @brief Initialize USB communications **/ |
|
59 |
#define USB 0x02 |
|
60 |
/** @brief Initialize communications **/ |
|
61 |
#define COMM 0x02 |
|
62 |
/** @brief Initialize the orb **/ |
|
63 |
#define ORB 0x04 |
|
64 |
/** @brief Initialize the motors **/ |
|
65 |
#define MOTORS 0x08 |
|
66 |
/** @brief Initialize I2C **/ |
|
67 |
#define I2C 0x20 |
|
68 |
/** @brief Initialize the buzzer **/ |
|
69 |
#define BUZZER 0x40 |
|
70 |
/** @brief Initialize the LCD screen **/ |
|
71 |
#define LCD 0x80 |
|
72 |
/** @brief Initialize the rangefinders **/ |
|
73 |
#define RANGE 0x0100 |
|
74 |
/** @brief Initialize the BOM **/ |
|
75 |
#define BOM 0x0200 |
|
76 |
/** @brief Initilize encoders **/ |
|
77 |
#define ENCODERS 0x400 |
|
78 |
/** @brief Initialize everything **/ |
|
79 |
#define ALL_ON 0x07FF |
|
80 |
|
|
81 |
/** @} **/ //end addtogroup |
|
82 |
|
|
83 |
/** @brief shortcut for ATOMIC_BLOCK(ATOMIC_RESTORESTATE) **/ |
|
84 |
#define SYNC ATOMIC_BLOCK(ATOMIC_RESTORESTATE) |
|
85 |
|
|
86 |
/** @brief atomically grab a lock if it is free, return otherwise **/ |
|
87 |
#define REQUIRE_LOCK_OR_RETURN(LOCK) do { SYNC { if (LOCK) return; LOCK=1; } } while (0) |
|
88 |
|
|
89 |
/** @brief atomically release a lock **/ |
|
90 |
#define RELEASE_LOCK(LOCK) do { LOCK=0; } while (0) |
|
91 |
|
|
92 |
|
|
93 |
#endif |
trunk/code/projects/libdragonfly/motor.c | ||
---|---|---|
34 | 34 |
* Much of this is taken from FWR's library, author: Tom Lauwers |
35 | 35 |
**/ |
36 | 36 |
|
37 |
#include "dragonfly_defs.h" |
|
38 | 37 |
#include "motor.h" |
39 | 38 |
|
40 | 39 |
/** |
... | ... | |
44 | 43 |
* @{ |
45 | 44 |
**/ |
46 | 45 |
|
47 |
unsigned char motors_initd=0; |
|
48 |
|
|
49 | 46 |
/** |
50 | 47 |
* Initializes both motors so that they can be used with future |
51 | 48 |
* calls to motor1_set and motor2_set. |
52 | 49 |
* |
53 |
* @return 0 if init succesfull, an error code otherwise |
|
54 |
* |
|
55 | 50 |
* @see motors_off, motor1_set, motor2_set |
56 | 51 |
**/ |
57 |
int motors_init( void ) { |
|
58 |
|
|
59 |
if(motors_initd) |
|
60 |
return ERROR_INIT_ALREADY_INITD; |
|
61 |
|
|
62 |
|
|
52 |
void motors_init( void ) { |
|
63 | 53 |
// Configure counter such that we use phase correct |
64 | 54 |
// PWM with 8-bit resolution |
65 | 55 |
PORTA &= 0x0F; |
... | ... | |
75 | 65 |
OCR1AL=0; |
76 | 66 |
OCR1BH=0; |
77 | 67 |
OCR1BL=0; |
78 |
|
|
79 |
motors_initd=1; |
|
80 |
return 0; |
|
81 | 68 |
} |
82 | 69 |
|
83 | 70 |
/** |
... | ... | |
87 | 74 |
* @param direction Either FORWARD or BACKWARD to set the direction of rotation. |
88 | 75 |
* @param speed The speed the motor will run at, in the range 0-255. |
89 | 76 |
* |
90 |
* @return 0 if init succesfull, an error code otherwise |
|
91 |
* |
|
92 | 77 |
* @see motor_r_set, motors_init |
93 | 78 |
**/ |
94 |
int motor_l_set(int direction, int speed) { |
|
95 |
if(!motors_initd) |
|
96 |
return ERROR_LIBRARY_NOT_INITD; |
|
79 |
void motor_l_set(int direction, int speed) { |
|
97 | 80 |
|
98 | 81 |
if(direction == 0) { |
99 | 82 |
// turn off PWM first if switching directions |
... | ... | |
115 | 98 |
|
116 | 99 |
// Set the timer to count up to speed, an 8-bit value |
117 | 100 |
OCR1AL = speed; |
118 |
|
|
119 |
return 0; |
|
120 | 101 |
} |
121 | 102 |
|
122 | 103 |
/** |
... | ... | |
126 | 107 |
* @param direction Either FORWARD or BACKWARD to set the direction of rotation. |
127 | 108 |
* @param speed The speed the motor will run at, in the range 0-255. |
128 | 109 |
* |
129 |
* @return 0 if init succesfull, an error code otherwise |
|
130 |
* |
|
131 | 110 |
* @see motor_l_set, motors_init |
132 | 111 |
**/ |
133 |
int motor_r_set(int direction, int speed) { |
|
134 |
if(!motors_initd) |
|
135 |
return ERROR_LIBRARY_NOT_INITD; |
|
136 |
|
|
112 |
void motor_r_set(int direction, int speed) { |
|
137 | 113 |
if(direction == 0) { |
138 | 114 |
// PORTD |= 0x20; |
139 | 115 |
// PORTD &= 0x7F; |
... | ... | |
155 | 131 |
PORTA = (PORTA & 0x3F) | 0x40; |
156 | 132 |
} |
157 | 133 |
OCR1BL = speed; |
158 |
|
|
159 |
return 0; |
|
160 | 134 |
} |
161 | 135 |
|
162 | 136 |
/** |
... | ... | |
166 | 140 |
* @param direction Either FORWARD or BACKWARD to set the direction of rotation. |
167 | 141 |
* @param speed The speed the motor will run at, in the range 0-255. |
168 | 142 |
* |
169 |
* @return 0 if init succesfull, an error code otherwise |
|
170 |
* |
|
171 | 143 |
* @see motor2_set, motors_init |
172 | 144 |
**/ |
173 |
int motor1_set(int direction, int speed) {
|
|
174 |
return motor_l_set(direction, speed);
|
|
145 |
void motor1_set(int direction, int speed) {
|
|
146 |
motor_l_set(direction, speed);
|
|
175 | 147 |
} |
176 | 148 |
|
177 | 149 |
/** |
... | ... | |
181 | 153 |
* @param direction Either FORWARD or BACKWARD to set the direction of rotation. |
182 | 154 |
* @param speed The speed the motor will run at, in the range 0-255. |
183 | 155 |
* |
184 |
* @return 0 if init succesfull, an error code otherwise |
|
185 |
* |
|
186 | 156 |
* @see motor2_set, motors_init |
187 | 157 |
**/ |
188 |
int motor2_set(int direction, int speed) {
|
|
189 |
return motor_r_set(direction, speed);
|
|
158 |
void motor2_set(int direction, int speed) {
|
|
159 |
motor_r_set(direction, speed);
|
|
190 | 160 |
} |
191 | 161 |
|
192 | 162 |
|
193 | 163 |
/** |
194 | 164 |
* Turns off both motors. |
195 | 165 |
* |
196 |
* @return 0 if init succesfull, an error code otherwise |
|
197 |
* |
|
198 | 166 |
* @see motors_init |
199 | 167 |
**/ |
200 |
int motors_off( void ) { |
|
201 |
if(!motors_initd) |
|
202 |
return ERROR_LIBRARY_NOT_INITD; |
|
203 |
|
|
168 |
void motors_off( void ) { |
|
204 | 169 |
OCR1AL = 0x0; |
205 | 170 |
OCR1BL = 0x0; |
206 |
|
|
207 |
return 0; |
|
208 | 171 |
} |
209 | 172 |
|
210 | 173 |
/**@}**///end defgroup |
trunk/code/projects/libdragonfly/motor.h | ||
---|---|---|
39 | 39 |
#define _MOTOR_H |
40 | 40 |
|
41 | 41 |
#include <avr/io.h> |
42 |
|
|
43 | 42 |
/** |
44 | 43 |
* @addtogroup motors |
45 | 44 |
* @{ |
... | ... | |
51 | 50 |
#define BACKWARD 0 |
52 | 51 |
|
53 | 52 |
/** @brief Initialize the motors **/ |
54 |
int motors_init(void);
|
|
53 |
void motors_init(void);
|
|
55 | 54 |
/** @brief Set speed and direction of motor1 |
56 | 55 |
* @deprecated use the left motor function instead. it's more intuitive and easier to read.**/ |
57 |
int motor1_set(int direction, int speed);
|
|
56 |
void motor1_set(int direction, int speed);
|
|
58 | 57 |
/** @brief Set speed and direction of motor2 |
59 | 58 |
* @deprecated use the right motor function instead. it's more intuitive and easier to read.**/ |
60 |
int motor2_set(int direction, int speed);
|
|
59 |
void motor2_set(int direction, int speed);
|
|
61 | 60 |
/** @brief Set speed and direction of left motor **/ |
62 |
int motor_l_set(int direction, int speed);
|
|
61 |
void motor_l_set(int direction, int speed);
|
|
63 | 62 |
/** @brief Set speed and direction of right motor **/ |
64 |
int motor_r_set(int direction, int speed);
|
|
63 |
void motor_r_set(int direction, int speed);
|
|
65 | 64 |
/** @brief Turn the motors off **/ |
66 |
int motors_off(void);
|
|
65 |
void motors_off(void);
|
|
67 | 66 |
|
68 | 67 |
/**@}**/ // end addtogroup |
69 | 68 |
|
trunk/code/projects/libdragonfly/i2c.c | ||
---|---|---|
43 | 43 |
#include "i2c.h" |
44 | 44 |
#include "ring_buffer.h" |
45 | 45 |
|
46 |
//NOTE: this is here just to provide colony error codes, it can be safely removed |
|
47 |
// for use in another project |
|
48 |
#include <dragonfly_lib.h> |
|
49 |
|
|
50 | 46 |
/** |
51 | 47 |
* @defgroup i2c I2C |
52 | 48 |
* |
... | ... | |
82 | 78 |
*/ |
83 | 79 |
#define I2C_BIT_RATE_DIVIDER 0x0C |
84 | 80 |
|
85 |
unsigned char i2c_initd=0; |
|
86 |
|
|
87 | 81 |
static int start_flag; |
88 | 82 |
|
89 | 83 |
static fun_mrecv_t master_recv_function; |
... | ... | |
111 | 105 |
* @return 0 for success, nonzero for failure |
112 | 106 |
**/ |
113 | 107 |
int i2c_init(char addr, fun_mrecv_t master_recv, fun_srecv_t slave_recv, fun_send_t slave_send) { |
114 |
|
|
115 |
if(i2c_initd) |
|
116 |
return ERROR_INIT_ALREADY_INITD; |
|
117 |
|
|
118 |
|
|
119 | 108 |
master_recv_function = master_recv; |
120 | 109 |
slave_recv_function = slave_recv; |
121 | 110 |
slave_send_function = slave_send; |
... | ... | |
133 | 122 |
* global messages to be accepted */ |
134 | 123 |
TWAR = (addr << 1) | 1; |
135 | 124 |
|
136 |
i2c_initd = 1; |
|
137 | 125 |
return 0; |
138 | 126 |
} |
139 | 127 |
|
... | ... | |
153 | 141 |
int i2c_send(char dest, char *data, unsigned int bytes) { |
154 | 142 |
int i; |
155 | 143 |
|
156 |
if(!i2c_initd) |
|
157 |
return ERROR_LIBRARY_NOT_INITD; |
|
158 |
|
|
159 |
|
|
160 | 144 |
/* adding data to be sent to ring buffers is not atomic, |
161 | 145 |
* so disable interrupts */ |
162 | 146 |
cli(); |
... | ... | |
196 | 180 |
* @return 0 for success, nonzero for failure |
197 | 181 |
**/ |
198 | 182 |
int i2c_request(char dest) { |
199 |
if(!i2c_initd) |
|
200 |
return ERROR_LIBRARY_NOT_INITD; |
|
201 |
|
|
202 | 183 |
if(RING_BUFFER_FULL(i2c_write_buff)) |
203 | 184 |
return -1; |
204 | 185 |
|
trunk/code/projects/libdragonfly/serial.c | ||
---|---|---|
34 | 34 |
**/ |
35 | 35 |
|
36 | 36 |
#include <avr/io.h> |
37 |
|
|
38 |
#include "dragonfly_defs.h" |
|
39 | 37 |
#include "serial.h" |
40 | 38 |
|
41 |
unsigned char usb_initd=0; |
|
42 |
unsigned char xbee_initd=0; |
|
43 |
|
|
44 | 39 |
#ifdef USE_STDIO |
45 | 40 |
|
46 | 41 |
#include <stdio.h> |
... | ... | |
62 | 57 |
* This must be called before any other usb function |
63 | 58 |
* may be used. |
64 | 59 |
**/ |
65 |
int usb_init() {
|
|
60 |
void usb_init() {
|
|
66 | 61 |
//Set baud rate |
67 | 62 |
// - 115200 (both wired and wireless) is UBRR=8, U2X=1 |
68 | 63 |
// - 9600 is U2X =1, UBRR = 107. |
69 |
|
|
70 |
if(usb_initd) { |
|
71 |
return ERROR_INIT_ALREADY_INITD; |
|
72 |
} |
|
73 |
|
|
74 | 64 |
#if (USB_BAUD == 115200) |
75 | 65 |
UBRR0H = 0x00; |
76 | 66 |
UBRR0L = 8; |
... | ... | |
80 | 70 |
UBRR0L = 103; |
81 | 71 |
UCSR0A |= _BV(U2X0); |
82 | 72 |
#else //Baud rate is defined in the header file, we should not get here |
83 |
return 0;
|
|
73 |
return; |
|
84 | 74 |
#endif |
85 | 75 |
|
86 | 76 |
/*Enable receiver and transmitter */ |
... | ... | |
94 | 84 |
/* Open the stdio stream corresponding to this port */ |
95 | 85 |
usb_fd = fdevopen(usb_putc, usb_getc); |
96 | 86 |
#endif |
97 |
|
|
98 |
usb_initd = 1; |
|
99 |
return 0; |
|
100 |
|
|
101 | 87 |
} |
102 | 88 |
|
103 | 89 |
/** |
... | ... | |
105 | 91 |
* This must be called before any other xbee function |
106 | 92 |
* may be used. |
107 | 93 |
**/ |
108 |
int xbee_init() { |
|
109 |
|
|
110 |
if(xbee_initd) { |
|
111 |
return ERROR_INIT_ALREADY_INITD; |
|
112 |
} |
|
113 |
|
|
94 |
void xbee_init() { |
|
114 | 95 |
//Set baud rate |
115 | 96 |
// - 115200 (both wired and wireless) is UBRR=8, U2X=1 |
116 | 97 |
// - 9600 is U2X =1, UBRR = 107. |
... | ... | |
123 | 104 |
UBRR1L = 103; |
124 | 105 |
UCSR1A |= _BV(U2X1); |
125 | 106 |
#else //Baud rate is defined in the header file, we should not get here |
126 |
return 0;
|
|
107 |
return; |
|
127 | 108 |
#endif |
128 | 109 |
|
129 | 110 |
//Enable receiver and transmitter |
... | ... | |
137 | 118 |
/* Open the stdio stream corresponding to this port */ |
138 | 119 |
xbee_fd = fdevopen(xbee_putc, xbee_getc); |
139 | 120 |
#endif |
140 |
|
|
141 |
xbee_initd = 1; |
|
142 |
return 0; |
|
143 |
|
|
144 | 121 |
} |
145 | 122 |
|
146 | 123 |
/** |
... | ... | |
150 | 127 |
* @return 0 for success, nonzero for failure |
151 | 128 |
**/ |
152 | 129 |
int usb_putc(char c) { |
153 |
|
|
154 |
if(!usb_initd) |
|
155 |
return ERROR_LIBRARY_NOT_INITD; |
|
156 |
|
|
157 | 130 |
// Wait until buffer is clear for sending |
158 | 131 |
loop_until_bit_is_set(UCSR0A, UDRE0); |
159 | 132 |
|
... | ... | |
169 | 142 |
* @return 0 for success, nonzero for failure |
170 | 143 |
**/ |
171 | 144 |
int xbee_putc(char c) { |
172 |
|
|
173 |
if(!xbee_initd) |
|
174 |
return ERROR_LIBRARY_NOT_INITD; |
|
175 |
|
|
176 | 145 |
// Wait until buffer is clear for sending |
177 | 146 |
loop_until_bit_is_set(UCSR1A, UDRE1); |
178 | 147 |
|
... | ... | |
189 | 158 |
**/ |
190 | 159 |
int usb_puts(char *s) { |
191 | 160 |
char *t = s; |
192 |
|
|
193 |
if(!usb_initd) |
|
194 |
return ERROR_LIBRARY_NOT_INITD; |
|
195 |
|
|
196 | 161 |
while (*t != 0) { |
197 | 162 |
usb_putc(*t); |
198 | 163 |
t++; |
... | ... | |
204 | 169 |
* Sends a sequence of characters from program space over USB. |
205 | 170 |
* |
206 | 171 |
* @param s the string to send |
207 |
* |
|
208 |
* @return 0 if init succesfull, an error code otherwise |
|
209 | 172 |
**/ |
210 |
int usb_puts_P (PGM_P s) {
|
|
173 |
void usb_puts_P (PGM_P s) {
|
|
211 | 174 |
char buf; |
212 |
|
|
213 |
if(!usb_initd) |
|
214 |
return ERROR_LIBRARY_NOT_INITD; |
|
215 | 175 |
|
216 | 176 |
while (memcpy_P (&buf, s, sizeof (char)), buf!=0) { |
217 | 177 |
usb_putc (buf); |
218 | 178 |
s++; |
219 | 179 |
} |
220 |
|
|
221 |
return 0; |
|
222 | 180 |
} |
223 | 181 |
|
224 | 182 |
|
... | ... | |
228 | 186 |
* This function blocks execution until a character has been received. |
229 | 187 |
* xbee_init must be called before this function may be used. |
230 | 188 |
* |
231 |
* @return the first character in the usb buffer, -1 on error
|
|
189 |
* @return the first character in the usb buffer |
|
232 | 190 |
* |
233 | 191 |
* @see usb_init, usb_getc_nb |
234 | 192 |
**/ |
235 | 193 |
int usb_getc(void) { |
236 |
|
|
237 |
if(!usb_initd) |
|
238 |
return -1; |
|
239 |
|
|
240 | 194 |
// Wait for the receive buffer to be filled |
241 | 195 |
loop_until_bit_is_set(UCSR0A, RXC0); |
242 | 196 |
|
... | ... | |
250 | 204 |
* received. xbee_init must be called before this function |
251 | 205 |
* may be used. |
252 | 206 |
* |
253 |
* @return the first character in the xbee buffer, -1 on error
|
|
207 |
* @return the first character in the xbee buffer |
|
254 | 208 |
* |
255 | 209 |
* @see xbee_init, xbee_getc_nb |
256 | 210 |
**/ |
257 | 211 |
int xbee_getc(void) { |
258 |
|
|
259 |
if(!usb_initd) |
|
260 |
return -1; |
|
261 |
|
|
262 | 212 |
// Wait for the receive buffer to be filled |
263 | 213 |
loop_until_bit_is_set(UCSR1A, RXC1); |
264 | 214 |
|
... | ... | |
274 | 224 |
* @param c the received character. This will be set if a character has |
275 | 225 |
* been received. |
276 | 226 |
* |
277 |
* @return -1 if no character is available, 0 otherwise, positive for error
|
|
227 |
* @return -1 if no character is available, 0 otherwise |
|
278 | 228 |
* |
279 | 229 |
* @see usb_init, usb_getc |
280 | 230 |
**/ |
281 | 231 |
int usb_getc_nb(char *c) { |
282 |
|
|
283 |
if(!usb_initd) |
|
284 |
return ERROR_LIBRARY_NOT_INITD; |
|
285 |
|
|
286 | 232 |
// check if the receive buffer is filled |
287 | 233 |
if (UCSR0A & _BV(RXC0)) { |
288 | 234 |
// Read the receive buffer |
... | ... | |
302 | 248 |
* @param c the received character. This will be set if a character has |
303 | 249 |
* been received. |
304 | 250 |
* |
305 |
* @return -1 if no character is available, 0 otherwise, positive for error
|
|
251 |
* @return -1 if no character is available, 0 otherwise |
|
306 | 252 |
* |
307 | 253 |
* @see xbee_init, xbee_getc |
308 | 254 |
**/ |
309 | 255 |
int xbee_getc_nb(char *c) { |
310 |
if(!xbee_initd) |
|
311 |
return ERROR_LIBRARY_NOT_INITD; |
|
312 |
|
|
313 | 256 |
// check if the receive buffer is filled |
314 | 257 |
if (UCSR1A & _BV(RXC1)) { |
315 | 258 |
// Read the receive buffer |
... | ... | |
341 | 284 |
int usb_puti(int value ) { |
342 | 285 |
unsigned char usb_data[6]={'0','0','0','0','0','0' }, position=sizeof(usb_data), radix=10; |
343 | 286 |
|
344 |
if(!usb_initd) |
|
345 |
return ERROR_LIBRARY_NOT_INITD; |
|
346 |
|
|
347 |
|
|
348 | 287 |
/* convert int to ascii */ |
349 | 288 |
if(value<0) { |
350 | 289 |
usb_putc('-'); |
... | ... | |
390 | 329 |
* @param value the value to print |
391 | 330 |
* |
392 | 331 |
* @see usb_init, usb_puti, usb_puts, usb_puth8, hex_digit |
393 |
* |
|
394 |
* @return 0 if init succesfull, an error code otherwise |
|
395 | 332 |
**/ |
396 |
int usb_puth16 (uint16_t value)
|
|
333 |
void usb_puth16 (uint16_t value)
|
|
397 | 334 |
{ |
398 |
if(!usb_initd) |
|
399 |
return ERROR_LIBRARY_NOT_INITD; |
|
400 |
|
|
401 | 335 |
usb_putc (hex_digit((value >>12)&0xF)); |
402 | 336 |
usb_putc (hex_digit((value >>8 )&0xF)); |
403 | 337 |
usb_putc (hex_digit((value >>4 )&0xF)); |
404 | 338 |
usb_putc (hex_digit( value &0xF)); |
405 |
|
|
406 |
return 0; |
|
407 | 339 |
} |
408 | 340 |
|
409 | 341 |
/** |
... | ... | |
414 | 346 |
* @param value the value to print |
415 | 347 |
* |
416 | 348 |
* @see usb_init, usb_puti, usb_puts, usb_puth16, hex_digit |
417 |
* |
|
418 |
* @return 0 if init succesfull, an error code otherwise |
|
419 | 349 |
**/ |
420 |
int usb_puth8(uint8_t value)
|
|
350 |
void usb_puth8(uint8_t value)
|
|
421 | 351 |
{ |
422 |
if(!usb_initd) |
|
423 |
return ERROR_LIBRARY_NOT_INITD; |
|
424 |
|
|
425 | 352 |
usb_putc (hex_digit ((value)>>4 &0xF)); |
426 | 353 |
usb_putc (hex_digit ( value &0xF)); |
427 |
|
|
428 |
return 0; |
|
429 | 354 |
} |
430 | 355 |
|
trunk/code/projects/libdragonfly/dragonfly_lib.h | ||
---|---|---|
41 | 41 |
* @{ |
42 | 42 |
**/ |
43 | 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 |
|
|
44 | 72 |
/** @brief Initialize the board **/ |
45 | 73 |
void dragonfly_init(int config); |
46 | 74 |
|
... | ... | |
56 | 84 |
// missing from the AVR libc distribution. |
57 | 85 |
#include "atomic.h" |
58 | 86 |
|
59 |
#include "dragonfly_defs.h" |
|
60 | 87 |
#include "analog.h" |
61 | 88 |
#include "dio.h" |
62 | 89 |
#include "time.h" |
... | ... | |
76 | 103 |
#include <stddef.h> |
77 | 104 |
#include <stdbool.h> |
78 | 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 |
|
|
79 | 117 |
#endif |
80 | 118 |
|
trunk/code/projects/libdragonfly/serial.h | ||
---|---|---|
87 | 87 |
#endif |
88 | 88 |
|
89 | 89 |
/** @brief Initialize the USB **/ |
90 |
int usb_init(void);
|
|
90 |
void usb_init(void);
|
|
91 | 91 |
/** @brief Print a character to USB **/ |
92 | 92 |
int usb_putc(char c); |
93 | 93 |
/** @brief Read a character from USB **/ |
... | ... | |
97 | 97 |
/** @brief Print a string to USB **/ |
98 | 98 |
int usb_puts(char *s); |
99 | 99 |
/** @brief Print a string from program space to USB **/ |
100 |
int usb_puts_P (PGM_P s);
|
|
100 |
void usb_puts_P (PGM_P s);
|
|
101 | 101 |
/** @brief Print an integer to USB **/ |
102 | 102 |
int usb_puti(int value); |
103 | 103 |
/** @brief Determine a hexadecimal digit **/ |
104 | 104 |
uint8_t hex_digit (uint8_t value); |
105 | 105 |
/** @brief Print a fixed width hexadecimal representation to USB **/ |
106 |
int usb_puth16 (uint16_t value);
|
|
106 |
void usb_puth16 (uint16_t value);
|
|
107 | 107 |
/** @brief Print a fixed width hexadecimal representation to USB **/ |
108 |
int usb_puth8(uint8_t value);
|
|
108 |
void usb_puth8(uint8_t value);
|
|
109 | 109 |
/** @brief Alias for usb_puth16 **/ |
110 | 110 |
static inline void usb_puth (uint16_t value) { usb_puth16 (value); }; |
111 | 111 |
|
... | ... | |
130 | 130 |
#endif |
131 | 131 |
|
132 | 132 |
/** @brief Initialize the XBee **/ |
133 |
int xbee_init(void);
|
|
133 |
void xbee_init(void);
|
|
134 | 134 |
/** @brief Print a character to the XBee **/ |
135 | 135 |
int xbee_putc(char c); |
136 | 136 |
/** @brief Read a character from the XBee **/ |
trunk/code/projects/libdragonfly/eeprom.c | ||
---|---|---|
1 |
/** |
|
2 |
* @file eeprom.c |
|
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 | 1 |
#include <avr/io.h> |
11 | 2 |
#include "eeprom.h" |
12 | 3 |
|
13 |
/** @brief store a byte to eeproem |
|
14 |
* @return 0 if success, nonzero on failure |
|
15 |
*/ |
|
16 | 4 |
int eeprom_put_byte(unsigned int uiAddress, unsigned char ucData) { |
17 | 5 |
/* Wait for completion of previous write */ |
18 | 6 |
while(EECR & (1<<EEWE)); |
... | ... | |
27 | 15 |
return 0; |
28 | 16 |
} |
29 | 17 |
|
30 |
/** @brief reads a byte from eeprom |
|
31 |
* |
|
32 |
* Pass it thge address and a pointer to a byte where the byte at the |
|
33 |
* address will be stored |
|
34 |
* |
|
35 |
* @return 0 if successful (byte is set to the eeprom value at addr), |
|
36 |
* nonzero if there was a problem |
|
37 |
*/ |
|
38 | 18 |
int eeprom_get_byte(unsigned int uiAddress, unsigned char *byte) { |
39 | 19 |
/* Wait for completion of previous write */ |
40 | 20 |
while(EECR & (1<<EEWE)); |
... | ... | |
48 | 28 |
return 0; |
49 | 29 |
} |
50 | 30 |
|
51 |
/** @brief get stored robot ID |
|
52 |
* |
|
53 |
* checks that EEPROM has been programed with an ID and returns it |
|
54 |
* |
|
55 |
* @return the robot id, if it is stored. If it returns 0xFF it is probably invalid |
|
56 |
*/ |
|
31 |
|
|
57 | 32 |
unsigned char get_robotid(void) { |
58 | 33 |
unsigned char c0, c1, c2; |
59 | 34 |
|
... | ... | |
66 | 41 |
return 0xFF; |
67 | 42 |
} |
68 | 43 |
|
69 |
/** @brief get stored robot ID |
|
70 |
* |
|
71 |
* checks that EEPROM has been programed with an BOM type and returns it |
|
72 |
* |
|
73 |
* @return the robot bom type as defined in bom.h, if it is stored. If it returns 0xFF it is probably invalid |
|
74 |
*/ |
|
75 | 44 |
unsigned char get_bom_type(void) { |
76 | 45 |
unsigned char c0, c1, c2, c3; |
77 | 46 |
|
trunk/code/projects/libdragonfly/spi.c | ||
---|---|---|
6 | 6 |
**/ |
7 | 7 |
|
8 | 8 |
#include <avr/interrupt.h> |
9 |
|
|
10 |
#include "dragonfly_defs.h" |
|
11 | 9 |
#include "spi.h" |
12 | 10 |
|
13 |
unsigned char spi_initd=0; |
|
14 | 11 |
|
15 | 12 |
static volatile char spi_bytes; /* number of bytes to read */ |
16 | 13 |
static spi_fun_recv_t spi_recv_func; /* byte handler */ |
... | ... | |
21 | 18 |
* |
22 | 19 |
* @param recv_func The function to be called for each byte of data received. |
23 | 20 |
* @param recv_complete_func The function to be called at the end of a complete transmission. |
24 |
* |
|
25 |
* @return 0 if init succesfull, an error code otherwise |
|
26 | 21 |
*/ |
27 |
int spi_init (spi_fun_recv_t recv_func, spi_fun_recv_complete_t recv_complete_func)
|
|
22 |
void spi_init (spi_fun_recv_t recv_func, spi_fun_recv_complete_t recv_complete_func)
|
|
28 | 23 |
{ |
29 |
if(spi_initd) { |
|
30 |
return ERROR_INIT_ALREADY_INITD; |
|
31 |
} |
|
32 |
|
|
33 | 24 |
/* Enable Interrupt, Enable SPI Module, MSB First, Master Mode, Clock div = 64 */ |
34 | 25 |
SPCR = _BV(SPE) | _BV(SPIE) /*| _BV(DORD)*/ | _BV(MSTR) | _BV(SPR1) | _BV(SPR0); |
35 | 26 |
SPSR = _BV(SPI2X); |
... | ... | |
46 | 37 |
spi_recv_complete_func = recv_complete_func; |
47 | 38 |
spi_bytes = 0; |
48 | 39 |
//usb_puts("\tspi.c Debug: SPI INITIALIZED\n"); |
49 |
|
|
50 |
spi_initd=1; |
|
51 |
return 0; |
|
52 | 40 |
} |
53 | 41 |
|
54 | 42 |
/** |
... | ... | |
56 | 44 |
* |
57 | 45 |
* @param bytes The number of bytes to be transferred. |
58 | 46 |
**/ |
59 |
int spi_transfer(char bytes)
|
|
47 |
void spi_transfer(char bytes)
|
|
60 | 48 |
{ |
61 |
if(!spi_initd) |
|
62 |
return ERROR_LIBRARY_NOT_INITD; |
|
63 |
|
|
64 | 49 |
spi_bytes = bytes; |
65 | 50 |
PORTB &= ~SS; /* Set SS low to initiate transmission */ |
66 | 51 |
SPDR = 0xff; /* Initiate data transmision */ |
67 |
|
|
68 |
return 0; |
|
69 | 52 |
} |
70 | 53 |
|
71 | 54 |
ISR(SIG_SPI) |
trunk/code/projects/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 |
|
|
1 | 10 |
#ifndef _EEPROM_H_ |
2 | 11 |
#define _EEPROM_H_ |
3 | 12 |
|
4 | 13 |
#define EEPROM_ROBOT_ID_ADDR 0x10 |
5 | 14 |
#define EEPROM_BOM_TYPE_ADDR 0x14 |
6 | 15 |
|
16 |
/** @brief store a byte to eeproem |
|
17 |
* @return 0 if success, nonzero on failure |
|
18 |
*/ |
|
7 | 19 |
int eeprom_put_byte(unsigned int addr, unsigned char byte); |
8 | 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 |
*/ |
|
9 | 29 |
int eeprom_get_byte(unsigned int addr, unsigned char *byte); |
10 | 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 |
*/ |
|
11 | 37 |
unsigned char get_robotid(void); |
12 | 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 |
*/ |
|
13 | 45 |
unsigned char get_bom_type(void); |
14 | 46 |
|
15 | 47 |
#endif |
trunk/code/projects/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 |
**/ |
|
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 |
|
|
25 | 63 |
|
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 |
|
|
62 |
#include "dragonfly_defs.h" |
|
63 |
#include "time.h" |
|
64 |
|
|
65 |
|
|
66 | 64 |
/* Calculate how many cycles to delay for to get 1 ms. Based on F_CPU which should be defined by the makefile */ |
67 | 65 |
#ifdef F_CPU |
68 | 66 |
#define WAIT_CYCLES ((F_CPU / 1000) / 10) |
69 | 67 |
#else |
70 | 68 |
#define WAIT_CYCLES (8000 / 10) |
71 | 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; |
|
72 | 75 |
|
73 | 76 |
|
74 |
unsigned char time_initd = 0; |
|
75 |
|
|
76 |
static volatile int _rtc_val = 0; |
|
77 |
static volatile int _rtc_pulse = 0; |
|
78 |
static volatile int _rtc_scale = 32; //Defaults to 1 Second per pulse |
|
79 |
static void (*_rtc_f)(void) = 0; |
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
/** |
|
84 |
* @defgroup time Time |
|
85 |
* @brief Time functions |
|
86 |
* |
|
87 |
* Functions dealing with time. |
|
88 |
* |
|
89 |
* @{ |
|
90 |
**/ |
|
91 |
|
|
92 |
/** |
|
93 |
* Delays for the specified number of milliseconds. |
|
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. |
|
94 | 89 |
* It depends on F_CPU to be defined in order to calculate how many cycles |
95 | 90 |
* it should delay. If it is not defined, a default clock of 8MHz is assumed. |
96 | 91 |
* |
97 | 92 |
* We use _delay_loop_2 which will run assembly instructions that should be |
98 | 93 |
* 4 cycles long. Optimizations must be enabled for this to be true. |
99 | 94 |
* That function is called to ensure around 1ms per execution. To generate |
100 |
* multiple ms we run a for loop of how many milliseconds are desired. |
|
95 |
* multiple ms we run a for loop of how many milliseconds are desired.
|
|
101 | 96 |
* |
102 | 97 |
* The error should be just the skew on the oscillator as the formula to |
103 | 98 |
* calculate delay cycles should always be a whole number. The is some skew |
104 | 99 |
* in practice though it is unavoidable. Delaying for less than 1s should make |
105 | 100 |
* the error negligable. |
106 |
* |
|
107 |
* @param ms the number of milliseconds to delay for |
|
108 |
**/ |
|
101 |
*
|
|
102 |
* @param ms the number of milliseconds to delay for
|
|
103 |
**/
|
|
109 | 104 |
void delay_ms(int ms) { |
110 | 105 |
for (; ms > 0; ms--) { |
111 | 106 |
_delay_loop_2(WAIT_CYCLES); |
112 | 107 |
} |
113 | 108 |
} |
114 | 109 |
|
115 |
|
|
116 |
/* Prescales defined in time.h. SECOND will give you 1 second. |
|
117 |
More scales are defined in the time.h file. |
|
118 |
rtc_func is the address to a function that you want called every clock tick. */ |
|
119 |
/** |
|
120 |
* Initializes the real time clock. Prescales are defined in time.h. |
|
121 |
* For example, SECOND will give 1 second. The specified function is |
Also available in: Unified diff