/*
* Copyright (c) 2021 Valentin Milea <valentin.milea@gmail.com>
* Copyright (c) 2023 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/#include<hardware/i2c.h>#include<pico/i2c_slave.h>#include<pico/stdlib.h>#include<stdio.h>#include<string.h>staticconst uint I2C_SLAVE_ADDRESS =0x17;staticconst uint I2C_BAUDRATE =100000;// 100 kHz#ifdefi2c_default// For this example, we run both the master and slave from the same board.// You'll need to wire pin GP4 to GP6 (SDA), and pin GP5 to GP7 (SCL).staticconst uint I2C_SLAVE_SDA_PIN = PICO_DEFAULT_I2C_SDA_PIN;// 4staticconst uint I2C_SLAVE_SCL_PIN = PICO_DEFAULT_I2C_SCL_PIN;// 5staticconst uint I2C_MASTER_SDA_PIN =6;staticconst uint I2C_MASTER_SCL_PIN =7;// The slave implements a 256 byte memory. To write a series of bytes, the master first// writes the memory address, followed by the data. The address is automatically incremented// for each byte transferred, looping back to 0 upon reaching the end. Reading is done// sequentially from the current memory address.staticstruct{uint8_t mem[256];uint8_t mem_address;
bool mem_address_written;} context;// Our handler is called from the I2C ISR, so it must complete quickly. Blocking calls /// printing to stdio may interfere with interrupt handling.staticvoidi2c_slave_handler(i2c_inst_t*i2c,i2c_slave_event_t event){switch(event){case I2C_SLAVE_RECEIVE:// master has written some dataif(!context.mem_address_written){// writes always start with the memory address
context.mem_address =i2c_read_byte_raw(i2c);
context.mem_address_written = true;}else{// save into memory
context.mem[context.mem_address]=i2c_read_byte_raw(i2c);
context.mem_address++;}break;case I2C_SLAVE_REQUEST:// master is requesting data// load from memoryi2c_write_byte_raw(i2c, context.mem[context.mem_address]);
context.mem_address++;break;case I2C_SLAVE_FINISH:// master has signalled Stop / Restart
context.mem_address_written = false;break;default:break;}}staticvoidsetup_slave(){gpio_init(I2C_SLAVE_SDA_PIN);gpio_set_function(I2C_SLAVE_SDA_PIN, GPIO_FUNC_I2C);gpio_pull_up(I2C_SLAVE_SDA_PIN);gpio_init(I2C_SLAVE_SCL_PIN);gpio_set_function(I2C_SLAVE_SCL_PIN, GPIO_FUNC_I2C);gpio_pull_up(I2C_SLAVE_SCL_PIN);i2c_init(i2c0, I2C_BAUDRATE);// configure I2C0 for slave modei2c_slave_init(i2c0, I2C_SLAVE_ADDRESS,&i2c_slave_handler);}staticvoidrun_master(){gpio_init(I2C_MASTER_SDA_PIN);gpio_set_function(I2C_MASTER_SDA_PIN, GPIO_FUNC_I2C);// pull-ups are already active on slave side, this is just a fail-safe in case the wiring is faultygpio_pull_up(I2C_MASTER_SDA_PIN);gpio_init(I2C_MASTER_SCL_PIN);gpio_set_function(I2C_MASTER_SCL_PIN, GPIO_FUNC_I2C);gpio_pull_up(I2C_MASTER_SCL_PIN);i2c_init(i2c1, I2C_BAUDRATE);for(uint8_t mem_address =0;; mem_address =(mem_address +32)%256){char msg[32];snprintf(msg,sizeof(msg),"Hello, I2C slave! - 0x%02X", mem_address);uint8_t msg_len =strlen(msg);uint8_t buf[32];
buf[0]= mem_address;memcpy(buf +1, msg, msg_len);// write message at mem_addressprintf("Write at 0x%02X: '%s'\n", mem_address, msg);int count =i2c_write_blocking(i2c1, I2C_SLAVE_ADDRESS, buf,1+ msg_len, false);if(count <0){puts("Couldn't write to slave, please check your wiring!");return;}hard_assert(count ==1+ msg_len);// seek to mem_address
count =i2c_write_blocking(i2c1, I2C_SLAVE_ADDRESS, buf,1, true);hard_assert(count ==1);// partial readuint8_t split =5;
count =i2c_read_blocking(i2c1, I2C_SLAVE_ADDRESS, buf, split, true);hard_assert(count == split);
buf[count]='\0';printf("Read at 0x%02X: '%s'\n", mem_address, buf);hard_assert(memcmp(buf, msg, split)==0);// read the remaining bytes, continuing from last address
count =i2c_read_blocking(i2c1, I2C_SLAVE_ADDRESS, buf, msg_len - split, false);hard_assert(count == msg_len - split);
buf[count]='\0';printf("Read at 0x%02X: '%s'\n", mem_address + split, buf);hard_assert(memcmp(buf, msg + split, msg_len - split)==0);puts("");sleep_ms(2000);}}#endifintmain(){stdio_init_all();#if!defined(i2c_default)||!defined(PICO_DEFAULT_I2C_SDA_PIN)||!defined(PICO_DEFAULT_I2C_SCL_PIN)#warningi2c / slave_mem_i2c example requires a board with I2C pinsputs("Default I2C pins were not defined");return0;#elseputs("\nI2C slave example");setup_slave();run_master();#endif}