/* i2cmemory.ino - 256 byte ram memory emulation over i2c at address 0x55 running on a Rasprberry Pi Pico When writing data to the pico the first data byte updates the current address to be used when writing or reading from the RAM Subsequent data bytes contain data that is written to the ram at the current address and following locations (current address auto increments) When reading data from the pico the first data byte returned will be from the ram storage located at current address Subsequent bytes will be returned from the following ram locations (again current address auto increments) N.B. if the current address reaches 255, it will autoincrement to 0 after next read / write */ #include const byte PICO_I2C_ADDRESS = 0x55; const byte PICO_I2C_SDA = 20; const byte PICO_I2C_SCL = 21; byte volatile rx_flag = 0; byte volatile tx_flag = 0; uint8_t volatile ram_addr = 0; uint8_t volatile ram[256]; void setup() { Wire.setSDA(PICO_I2C_SDA); Wire.setSCL(PICO_I2C_SCL); Wire.begin(PICO_I2C_ADDRESS); // join i2c bus as slave Wire.onReceive(i2c_receive); // i2c interrupt receive Wire.onRequest(i2c_transmit); // i2c interrupt send } void loop() { if (rx_flag) { rx_flag = 0; } if (tx_flag) { tx_flag = 0; } delay(1); } void i2c_receive(int bytes_count) { // bytes_count gives number of bytes in rx buffer ram_addr = (uint8_t)Wire.read(); // first byte is ram offset address (0-255) for (byte i=1; i