diff --git a/Makefile b/Makefile index 5a1f375..b12ddc4 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,7 @@ OBJS = main.o VPATH += OBJS += +OBJS += eeprom/eeprom.o LIBS += core/libcore.a lcd/libfont.a ########################################################################## diff --git a/eeprom/eeprom.c b/eeprom/eeprom.c new file mode 100644 index 0000000..69c108a --- /dev/null +++ b/eeprom/eeprom.c @@ -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; + } +} + diff --git a/eeprom/eeprom.h b/eeprom/eeprom.h new file mode 100644 index 0000000..8f508d5 --- /dev/null +++ b/eeprom/eeprom.h @@ -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 */ diff --git a/main.c b/main.c index d002cec..5630ccd 100644 --- a/main.c +++ b/main.c @@ -22,6 +22,8 @@ void ReinvokeISP(void); #include #include +#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);