From 795c7fdd0d8f03fc206458c831c9c9b461f6529f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sun, 11 Jun 2023 00:49:38 +0200 Subject: [PATCH] make eeprom work --- CMakeLists.txt | 1 + src/ds_rtc.c | 9 +++------ src/ee.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/ee.h | 14 ++++++++++++++ src/main.c | 15 +++++++++++++-- src/pinout.h | 3 --- src/rv_try.h | 13 +++++++++++++ 7 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 src/ee.c create mode 100644 src/ee.h create mode 100644 src/rv_try.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 06671f8..3f4cb41 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ add_executable(zavlaha src/main.c src/lcd.c src/ds_rtc.c + src/ee.c ) # Add pico_stdlib library which aggregates commonly used features diff --git a/src/ds_rtc.c b/src/ds_rtc.c index 9a7f56f..81bf2d9 100644 --- a/src/ds_rtc.c +++ b/src/ds_rtc.c @@ -9,15 +9,11 @@ #include "ds_rtc.h" #include "pinout.h" +#include "rv_try.h" #define I2C_ADDR_RTC 0b1101000 #define TIMEOUT_US 10000 -#define TRY(what) do { \ - rv = (what); \ - if (rv < 0) { return rv; } \ -} while (0) - static int rtc_read(uint8_t start, uint8_t *dest, size_t len) { int rv; @@ -26,10 +22,11 @@ static int rtc_read(uint8_t start, uint8_t *dest, size_t len) { return 0; } +static uint8_t scratch[20]; + static int rtc_write(uint8_t start, const uint8_t *data, size_t len) { int rv; if (len > 19) return -1; - uint8_t scratch[20]; scratch[0] = start; memcpy(&scratch[1], data, len); TRY(i2c_write_timeout_us(i2c0, I2C_ADDR_RTC, scratch, len + 1, true, TIMEOUT_US)); diff --git a/src/ee.c b/src/ee.c new file mode 100644 index 0000000..6281037 --- /dev/null +++ b/src/ee.c @@ -0,0 +1,52 @@ +/** + * TODO file description + */ + +#include +#include +#include +#include + +#include "ee.h" +#include "pinout.h" +#include "rv_try.h" + +#define I2C_ADDR_EEPROM 0b1010111 +#define TIMEOUT_US 20000 + +static uint8_t scratch[64 + 2]; + +int ee_read(uint16_t start, uint8_t *dest, size_t len) { + int rv; + uint8_t req[2] = { + (start & 0xFF00) >> 8, + start & 0xFF + }; + TRY(i2c_write_timeout_us(i2c0, I2C_ADDR_EEPROM, req, 2, true, TIMEOUT_US)); + TRY(i2c_read_timeout_us(i2c0, I2C_ADDR_EEPROM, dest, len, false, TIMEOUT_US)); + return rv; +} + +int ee_write(uint16_t start, const uint8_t *data, size_t len) { + int rv; + scratch[0] = (start & 0xFF00) >> 8; + scratch[1] = (start & 0xFF); + memcpy(&scratch[2], data, len); + + TRY(i2c_write_timeout_us(i2c0, I2C_ADDR_EEPROM, scratch, 2 + len, false, TIMEOUT_US)); + + sleep_ms(5 * (1 + len / 8)); + + return rv; +} + +// +//int ee_write(uint8_t start, const uint8_t *data, size_t len) { +// int rv; +// if (len > 19) return -1; +// uint8_t scratch[20]; +// scratch[0] = start; +// memcpy(&scratch[1], data, len); +// TRY(i2c_write_timeout_us(i2c0, I2C_ADDR_RTC, scratch, len + 1, true, TIMEOUT_US)); +// return 0; +//} diff --git a/src/ee.h b/src/ee.h new file mode 100644 index 0000000..acd2440 --- /dev/null +++ b/src/ee.h @@ -0,0 +1,14 @@ +/** + * TODO file description + */ + +#ifndef ZAVLAHA_EE_H +#define ZAVLAHA_EE_H + +/** Write and wait for completion; max 64 bytes in one go and should be aligned or at least not cross the boundary! */ +int ee_write(uint16_t start, const uint8_t *data, size_t len); + +/** Read data */ +int ee_read(uint16_t start, uint8_t *dest, size_t len); + +#endif //ZAVLAHA_EE_H diff --git a/src/main.c b/src/main.c index 1fced6c..6389c82 100644 --- a/src/main.c +++ b/src/main.c @@ -7,6 +7,7 @@ #include "pinout.h" #include "lcd.h" #include "ds_rtc.h" +#include "ee.h" /* @@ -133,12 +134,22 @@ int main() lcd_init(); lcd_puts("HELLO WORLD!"); + uint8_t ee_data[1] = {}; + int ee_rv = ee_read(0, ee_data, 1); + printf("ee rd rv = %d, val %02x", ee_rv, ee_data[0]); + + ee_data[0]++; + ee_rv = ee_write(0, ee_data, 1); + printf("ee wr rv = %d", ee_rv); + + int boot_count = ee_data[0]; uint cnt = 0; - char buf[16]; + char buf[20]; while (1) { lcd_clear(); - lcd_puts("HELLO WORLD!"); + sprintf(buf, "ZAVLAHA-Boot=%d", boot_count); + lcd_puts(buf); // Try to read RTC struct rtc_time time; diff --git a/src/pinout.h b/src/pinout.h index a970cd7..370bd84 100644 --- a/src/pinout.h +++ b/src/pinout.h @@ -22,7 +22,4 @@ #define PIN_LCD_D5 19 #define PIN_LCD_D4 18 -// must be shifted left before adding the RW bit! -#define I2C_ADDR_EEPROM 0b1010000 - #endif //ZAVLAHA_PINOUT_H diff --git a/src/rv_try.h b/src/rv_try.h new file mode 100644 index 0000000..d5556f8 --- /dev/null +++ b/src/rv_try.h @@ -0,0 +1,13 @@ +/** + * TODO file description + */ + +#ifndef ZAVLAHA_RV_TRY_H +#define ZAVLAHA_RV_TRY_H + +#define TRY(what) do { \ + rv = (what); \ + if (rv < 0) { return rv; } \ +} while (0) + +#endif //ZAVLAHA_RV_TRY_H