#include <stdio.h> //provides printf function
#include <string.h> //provides memset etc
#include "pico/stdlib.h" //provides standard library for the pico
#include "hardware/gpio.h" //configure gpio
#include "hardware/i2c.h" //configure i2c
#define I2C_INSTANCE i2c0
#define PICO_I2C_SDA_PIN 16
#define PICO_I2C_SCL_PIN 17
// I2C peripheral adapted from https://github.com/GoodAlex223/rule_indicators
#define EEPROM_ADDRESS 0x50
#define EEPROM_REG 0x0000
//avoid dumping all 32k
#define EEPROM_DUMP_LIMIT 0x40
//#define EEPROM_DUMP_LIMIT 0x8000
int main()
{
stdio_init_all();
// This example will use I2C0 on the selected SDA and SCL pins
i2c_init(I2C_INSTANCE, 100 * 1000);
gpio_set_function(PICO_I2C_SDA_PIN, GPIO_FUNC_I2C);
gpio_set_function(PICO_I2C_SCL_PIN, GPIO_FUNC_I2C);
//the pico has a board with hardware pullups attached
gpio_pull_up(PICO_I2C_SDA_PIN);
gpio_pull_up(PICO_I2C_SCL_PIN);
printf("I2C EEPROM Example\n");
const char msg[]="Hello World!";
char write_command[sizeof(msg)+2]={0};
memcpy(write_command+2,msg,sizeof(msg));
write_command[0]=(EEPROM_REG>>8)&0xFF;
write_command[1]=EEPROM_REG&0xFF;
printf("Writing: %s\n",msg);
i2c_write_blocking(i2c0, EEPROM_ADDRESS, (uint8_t *)write_command, sizeof(write_command), false);
sleep_ms(100);
char read_command[2]={0};
char read_buffer[sizeof(msg)+1]={0};
read_command[0]=EEPROM_REG&0xFF;
read_command[1]=(EEPROM_REG>>8)&0xFF;
i2c_write_blocking(i2c0, EEPROM_ADDRESS, (uint8_t *)read_command, sizeof(read_command), true);
i2c_read_blocking(i2c0, EEPROM_ADDRESS, (uint8_t *)read_buffer, sizeof(read_buffer)-1, false);
printf("Read %s\n",read_buffer);
// use converter to check online https://www.rapidtables.com/convert/number/hex-to-ascii.html
printf(" 0 1 2 3 4 5 6 7 8 9 A B C D E F\n");
for (int addr = 0; addr<EEPROM_DUMP_LIMIT; ++addr) {
if (addr % 16 == 0) {
printf("%08x ", addr);
}
char dump_command[2]={0};
char dump_buffer[1]={0};
dump_command[0]=(addr>>8)&0xFF;
dump_command[1]=addr&0xFF;
i2c_write_blocking(i2c0, EEPROM_ADDRESS, (uint8_t *)&dump_command, sizeof(dump_command), true);
i2c_read_blocking(i2c0, EEPROM_ADDRESS, (uint8_t *)dump_buffer, sizeof(dump_buffer), false);
printf("%2X",dump_buffer[0]);
printf(addr % 16 == 15 ? "\n" : " ");
}
printf("\n");
while (true) {
sleep_ms(1000);
}
}