1 /* 2 * Copyright (c) 2021 Valentin Milea <valentin.milea@gmail.com> 3 * Copyright (c) 2023 Raspberry Pi (Trading) Ltd. 4 * 5 * SPDX-License-Identifier: BSD-3-Clause 6 */ 7 8 #ifndef _PICO_I2C_SLAVE_H 9 #define _PICO_I2C_SLAVE_H 10 11 #include "hardware/i2c.h" 12 13 #ifdef __cplusplus 14 extern "C" { 15 #endif 16 17 /** \file pico/i2c_slave.h 18 * \defgroup pico_i2c_slave pico_i2c_slave 19 * 20 * \brief Functions providing an interrupt driven I2C slave interface 21 * 22 * This I2C slave helper library configures slave mode and hooks the relevant I2C IRQ 23 * so that a user supplied handler is called with enumerated I2C events. 24 * 25 * An example application \c slave_mem_i2c, which makes use of this library, can be found in 26 * <a href="https://github.com/raspberrypi/pico-examples/blob/master/i2c/slave_mem_i2c/slave_mem_i2c.c">pico_examples</a>. 27 */ 28 29 /** 30 * \brief I2C slave event types. 31 * \ingroup pico_i2c_slave 32 */ 33 typedef enum i2c_slave_event_t 34 { 35 I2C_SLAVE_RECEIVE, ///< Data from master is available for reading. Slave must read from Rx FIFO. 36 I2C_SLAVE_REQUEST, ///< Master is requesting data. Slave must write into Tx FIFO. 37 I2C_SLAVE_FINISH, ///< Master has sent a Stop or Restart signal. Slave may prepare for the next transfer. 38 } i2c_slave_event_t; 39 40 /** 41 * \brief I2C slave event handler 42 * \ingroup pico_i2c_slave 43 * 44 * The event handler will run from the I2C ISR, so it should return quickly (under 25 us at 400 kb/s). 45 * Avoid blocking inside the handler and split large data transfers across multiple calls for best results. 46 * When sending data to master, up to \ref i2c_get_write_available() bytes can be written without blocking. 47 * When receiving data from master, up to \ref i2c_get_read_available() bytes can be read without blocking. 48 * 49 * \param i2c Either \ref i2c0 or \ref i2c1 50 * \param event Event type. 51 */ 52 typedef void (*i2c_slave_handler_t)(i2c_inst_t *i2c, i2c_slave_event_t event); 53 54 /** 55 * \brief Configure an I2C instance for slave mode. 56 * \ingroup pico_i2c_slave 57 * \param i2c I2C instance. 58 * \param address 7-bit slave address. 59 * \param handler Callback for events from I2C master. It will run from the I2C ISR, on the CPU core 60 * where the slave was initialised. 61 */ 62 void i2c_slave_init(i2c_inst_t *i2c, uint8_t address, i2c_slave_handler_t handler); 63 64 /** 65 * \brief Restore an I2C instance to master mode. 66 * \ingroup pico_i2c_slave 67 * \param i2c Either \ref i2c0 or \ref i2c1 68 */ 69 void i2c_slave_deinit(i2c_inst_t *i2c); 70 71 #ifdef __cplusplus 72 } 73 #endif 74 75 #endif // _PICO_I2C_SLAVE_H_ 76