root / trunk / code / projects / libdragonfly / eeprom.c @ 1461
History | View | Annotate | Download (2.41 KB)
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 |
#include <avr/io.h> |
11 |
#include "eeprom.h" |
12 |
|
13 |
/** @brief store a byte to eeproem
|
14 |
* @return 0 if success, nonzero on failure
|
15 |
*/
|
16 |
int eeprom_put_byte(unsigned int uiAddress, unsigned char ucData) { |
17 |
/* Wait for completion of previous write */
|
18 |
while(EECR & (1<<EEWE)); |
19 |
/* Set up address and data registers */
|
20 |
EEAR = uiAddress; |
21 |
EEDR = ucData; |
22 |
/* Write logical one to EEMWE */
|
23 |
EECR |= (1<<EEMWE);
|
24 |
/* Start eeprom write by setting EEWE */
|
25 |
EECR |= (1<<EEWE);
|
26 |
|
27 |
return 0; |
28 |
} |
29 |
|
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 |
int eeprom_get_byte(unsigned int uiAddress, unsigned char *byte) { |
39 |
/* Wait for completion of previous write */
|
40 |
while(EECR & (1<<EEWE)); |
41 |
/* Set up address register */
|
42 |
EEAR = uiAddress; |
43 |
/* Start eeprom read by writing EERE */
|
44 |
EECR |= (1<<EERE);
|
45 |
/* get data from data register */
|
46 |
*byte=EEDR; |
47 |
|
48 |
return 0; |
49 |
} |
50 |
|
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 |
*/
|
57 |
unsigned char get_robotid(void) { |
58 |
unsigned char c0, c1, c2; |
59 |
|
60 |
eeprom_get_byte(EEPROM_ROBOT_ID_ADDR, &c0); |
61 |
eeprom_get_byte(EEPROM_ROBOT_ID_ADDR+1, &c1);
|
62 |
eeprom_get_byte(EEPROM_ROBOT_ID_ADDR+2, &c2);
|
63 |
if(c0 == 'I' && c1 == 'D') |
64 |
return c2;
|
65 |
else
|
66 |
return 0xFF; |
67 |
} |
68 |
|
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 |
unsigned char get_bom_type(void) { |
76 |
unsigned char c0, c1, c2, c3; |
77 |
|
78 |
eeprom_get_byte(EEPROM_BOM_TYPE_ADDR, &c0); |
79 |
eeprom_get_byte(EEPROM_BOM_TYPE_ADDR+1, &c1);
|
80 |
eeprom_get_byte(EEPROM_BOM_TYPE_ADDR+2, &c2);
|
81 |
eeprom_get_byte(EEPROM_BOM_TYPE_ADDR+3, &c3);
|
82 |
if(c0 == 'B' && c1 == 'O' && c2 == 'M') |
83 |
return c3;
|
84 |
else
|
85 |
return 0xFF; |
86 |
} |