105 lines
3.2 KiB
C
105 lines
3.2 KiB
C
/*
|
|
* 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>
|
|
|
|
static const uint I2C_SLAVE_ADDRESS = 0x09;
|
|
static const uint I2C_BAUDRATE = 100000; // 100 kHz
|
|
|
|
uint reception;
|
|
|
|
#ifdef i2c_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).
|
|
static const uint I2C_SLAVE_SDA_PIN = 17; // 17
|
|
static const uint I2C_SLAVE_SCL_PIN = 16; // 16
|
|
static const uint I2C_MASTER_SDA_PIN = 6;
|
|
static const 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.
|
|
static struct
|
|
{
|
|
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.
|
|
static void i2c_slave_handler(i2c_inst_t *i2c, i2c_slave_event_t event) {
|
|
switch (event) {
|
|
case I2C_SLAVE_RECEIVE: // master has written some data
|
|
if (!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 memory
|
|
i2c_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;
|
|
reception++;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
static void setup_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 I2C1 for slave mode
|
|
i2c_slave_init(i2c0, I2C_SLAVE_ADDRESS, &i2c_slave_handler);
|
|
}
|
|
|
|
#endif
|
|
|
|
int main() {
|
|
stdio_init_all();
|
|
sleep_ms(5000);
|
|
#if !defined(i2c_default) || !defined(PICO_DEFAULT_I2C_SDA_PIN) || !defined(PICO_DEFAULT_I2C_SCL_PIN)
|
|
#warning i2c / slave_mem_i2c example requires a board with I2C pins
|
|
puts("Default I2C pins were not defined");
|
|
return 0;
|
|
#else
|
|
puts("\nI2C slave example");
|
|
|
|
setup_slave();
|
|
//run_master();
|
|
while(1){
|
|
if(reception != 0){
|
|
puts("reception != 0\n");
|
|
reception = 0;
|
|
printf(">a:%d\n", context.mem[0]);
|
|
}
|
|
puts("I2C slave example\n");
|
|
sleep_ms(10);
|
|
}
|
|
#endif
|
|
} |