|
| 1 | +#include <stdio.h> |
| 2 | +#include "pico/stdlib.h" |
| 3 | +#include "hardware/i2c.h" |
| 4 | + |
| 5 | +// This is the max number of bytes which can be written at once during a page write |
| 6 | +#define MAX_PAGE_WRITE 32 |
| 7 | + |
| 8 | +// Acknowledge polling can be used to see when a write cycle is complete |
| 9 | +// Wait for the i2c slave to acknowledge our empty write |
| 10 | +bool ack_poll(uint8_t i2c_addr) { |
| 11 | + int timeout = 100; |
| 12 | + for (int i = 0; i < timeout; i++) { |
| 13 | + if (i2c_write_blocking(i2c_default, i2c_addr, NULL, 1, false) == 1) { |
| 14 | + return true; |
| 15 | + } |
| 16 | + sleep_ms(1); |
| 17 | + } |
| 18 | + printf("ack timeout\n"); |
| 19 | + return false; |
| 20 | +} |
| 21 | + |
| 22 | +// Read num bytes of memory starting at given address into result |
| 23 | +void read_eeprom(uint16_t address, uint8_t *result, uint16_t num, uint8_t i2c_addr) { |
| 24 | + for (int i = 0; i < num; i++) { |
| 25 | + uint8_t byte1 = (uint8_t)(address >> 8); |
| 26 | + uint8_t byte2 = (uint8_t)(address); |
| 27 | + |
| 28 | + uint8_t buf[2] = {byte1, byte2}; |
| 29 | + |
| 30 | + i2c_write_blocking(i2c_default, i2c_addr, buf, 2, true); |
| 31 | + i2c_read_blocking(i2c_default, i2c_addr, (result + i), 1, false); |
| 32 | + |
| 33 | + address++; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// Write a single byte of data at a specified address in memory |
| 38 | +void byte_write_eeprom(uint16_t address, uint8_t data, uint8_t i2c_addr) { |
| 39 | + uint8_t byte1 = (uint8_t)(address >> 8); |
| 40 | + uint8_t byte2 = (uint8_t)(address); |
| 41 | + |
| 42 | + uint8_t buf[3] = {byte1, byte2, data}; |
| 43 | + |
| 44 | + i2c_write_blocking(i2c_default, i2c_addr, buf, 3, false); |
| 45 | + |
| 46 | + // ack_poll to wait for write to be complete |
| 47 | + ack_poll(i2c_addr); |
| 48 | +} |
| 49 | + |
| 50 | +// Write a block of data to eeprom |
| 51 | +// Returns false if more than MAX_PAGE_WRITE bytes passed in, which would lead to overwriting of data |
| 52 | +bool page_write_eeprom(uint16_t address, uint8_t *data, uint8_t num, uint8_t i2c_addr) { |
| 53 | + if (num > MAX_PAGE_WRITE) { |
| 54 | + printf("Tried to write more than 32 bytes.\n"); |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + uint8_t byte1 = (uint8_t)(address >> 8); |
| 59 | + uint8_t byte2 = (uint8_t)(address); |
| 60 | + uint8_t buf[2 + num]; |
| 61 | + buf[0] = byte1; |
| 62 | + buf[1] = byte2; |
| 63 | + |
| 64 | + // transfer data into buffer |
| 65 | + for (int i = 0; i < num; i++) { |
| 66 | + buf[i + 2] = data[i]; |
| 67 | + } |
| 68 | + |
| 69 | + i2c_write_blocking(i2c_default, i2c_addr, buf, 2 + num, false); |
| 70 | + |
| 71 | + // ack_poll to wait for write to be complete |
| 72 | + ack_poll(i2c_addr); |
| 73 | + return true; |
| 74 | +} |
| 75 | + |
| 76 | +// Initialise i2c for given gpio at given frequency |
| 77 | +void eeprom_init(int sda_pin, int scl_pin, int freq, i2c_inst_t *i2c_instance) { |
| 78 | + i2c_init(i2c_default, freq); |
| 79 | + |
| 80 | + gpio_set_function(sda_pin, GPIO_FUNC_I2C); |
| 81 | + gpio_set_function(scl_pin, GPIO_FUNC_I2C); |
| 82 | + gpio_pull_up(sda_pin); |
| 83 | + gpio_pull_up(scl_pin); |
| 84 | +} |
| 85 | + |
0 commit comments