make eeprom work
This commit is contained in:
@@ -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
|
||||
|
||||
+3
-6
@@ -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));
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* TODO file description
|
||||
*/
|
||||
|
||||
#include <pico/stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <hardware/i2c.h>
|
||||
#include <string.h>
|
||||
|
||||
#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;
|
||||
//}
|
||||
@@ -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
|
||||
+13
-2
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user