You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
zavlaha-kzk/src/ee.c

53 lines
1.2 KiB

/**
* 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;
//}