parent
5927d24bc2
commit
795c7fdd0d
@ -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
|
@ -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
|
Loading…
Reference in new issue