added eeprom read/write code

Only single bytes can be read or written, page read/write will follow.
Added testcode in main.c to write a value to the EEPROM and read+verify
it.
This commit is contained in:
bsx 2011-05-19 14:05:28 +02:00
parent 96d6cbd616
commit 2a58e88fd2
4 changed files with 117 additions and 3 deletions

View File

@ -21,6 +21,7 @@ OBJS = main.o
VPATH +=
OBJS +=
OBJS += eeprom/eeprom.o
LIBS += core/libcore.a lcd/libfont.a
##########################################################################

68
eeprom/eeprom.c Normal file
View File

@ -0,0 +1,68 @@
#include "i2c/i2c.h"
#include "eeprom.h"
uint8_t eeprom_ready() {
uint32_t state;
I2CMasterBuffer[0] = EEPROM_BASE_ADDR; // Slave address, page 0, write
I2CWriteLength = 1; // just write the slave address and stop
I2CReadLength = 0;
state = i2cEngine();
if (state == I2CSTATE_ACK) {
return 1; // ready for next operation
} else {
return 0;
}
}
uint8_t eeprom_write_byte(uint8_t page_addr, uint8_t byte_addr, uint8_t value) {
uint8_t i2c_addr;
uint8_t intern_byte_addr;
uint32_t state;
// calculate our addresses with evil bitshift magic
i2c_addr = EEPROM_BASE_ADDR | (page_addr >> 3) & 0xFE; // bits 7-4 = base address, 3-1 = bits 6-4 of page address, 0 = R/W
intern_byte_addr = (page_addr << 4) | (byte_addr & 0x0F); // bits 7-4 = bits 3-0 of page address, 3-0 = lower 4 bits of byte address
I2CMasterBuffer[0] = i2c_addr;
I2CMasterBuffer[1] = intern_byte_addr;
I2CMasterBuffer[2] = value;
I2CWriteLength = 3;
I2CReadLength = 0;
state = i2cEngine();
if (state == I2CSTATE_ACK) {
return 1; // ready for next operation
} else {
return 0;
}
}
uint8_t eeprom_read_byte(uint8_t page_addr, uint8_t byte_addr, uint8_t *value) {
uint8_t i2c_addr;
uint8_t intern_byte_addr;
uint32_t state;
// calculate our addresses with evil bitshift magic
i2c_addr = EEPROM_BASE_ADDR | (page_addr >> 3) & 0xFE; // bits 7-4 = base address, 3-1 = bits 6-4 of page address, 0 = R/W
intern_byte_addr = (page_addr << 4) | (byte_addr & 0x0F); // bits 7-4 = bits 3-0 of page address, 3-0 = lower 4 bits of byte address
I2CMasterBuffer[0] = i2c_addr;
I2CMasterBuffer[1] = intern_byte_addr;
I2CMasterBuffer[2] = i2c_addr | READ_WRITE;
I2CWriteLength = 2;
I2CReadLength = 1;
state = i2cEngine();
if (state == I2CSTATE_ACK) {
*value = I2CSlaveBuffer[0];
return 1; // ready for next operation
} else {
return 0;
}
}

10
eeprom/eeprom.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef _EEPROM_H
#define _EEPROM_H 1
#define EEPROM_BASE_ADDR 0xA0
uint8_t eeprom_ready();
uint8_t eeprom_write_byte(uint8_t page_addr, uint8_t byte_addr, uint8_t value);
uint8_t eeprom_read_byte(uint8_t page_addr, uint8_t byte_addr, uint8_t *value);
#endif /* _EEPROM_H */

41
main.c
View File

@ -22,6 +22,8 @@ void ReinvokeISP(void);
#include <lcd/smallfonts.h>
#include <lcd/ubuntu18.h>
#include "core/i2c/i2c.h"
#include "eeprom/eeprom.h"
/**************************************************************************/
@ -33,6 +35,9 @@ int main(void)
//enable clocks to adc and watchdog
pmuInit();
//enable I2C
i2cInit(I2CMASTER);
uint32_t currentSecond, lastSecond;
currentSecond = lastSecond = 0;
@ -115,13 +120,16 @@ int main(void)
static FONT fonts[]=
{
& Font_8x8,
& Font_Ubuntu18pt, // 3 byte-font
& Font_7x8,
& Font_Ubuntu18pt, // 3 byte-font
& Font_8x8,
};
int fontctr=0;
yctr=18;
uint8_t written = 0;
uint8_t eeprom_val = 0;
while (1)
{
display(j);
@ -136,7 +144,34 @@ int main(void)
*/
font=fonts[fontctr];
DoString(1,yctr,"Hey Y!");
//DoString(1,yctr,"Hey Y!");
if (!written) {
if (eeprom_ready()) {
if (eeprom_write_byte(127,15,42)) {
DoString(1, yctr, "Write OK!");
written++;
} else {
DoString(1, yctr, "Write NOK!");
}
} else {
DoString(1, yctr, "NOT READY!");
}
} else {
if (eeprom_ready()) {
if (eeprom_read_byte(127,15,&eeprom_val)) {
if (eeprom_val == 42) {
DoString(1, yctr, "verified!");
} else {
DoString(1, yctr, "failed!");
}
} else {
DoString(1, yctr, "Read NOK!");
}
} else {
DoString(1, yctr, "NOT READY!!");
}
}
if(1 && gpioGetValue(3,3)==0){
gpioSetValue (CFG_LED_PORT, CFG_LED_PIN, CFG_LED_ON);