/** * @file i2cs.h * @brief Header file for I2C Slave module. * * This file contains the declarations and definitions for the I2C Slave module. * It provides functions to initialize and configure the I2C peripheral as a slave, * as well as functions to send and receive data over the I2C bus. * * @author xxx * @date 2023-12-27 14:44:03 * @version 1.0 * @copyright Copyright (c) 2024 by xxx, All Rights Reserved. */ #ifndef __I2CS_H__ #define __I2CS_H__ #include "lib.h" #include "gpios.h" /** * @file i2cs.h * @brief Header file containing the definition of the I2C slave (I2CS) structure and related functions. */ typedef struct I2CS i2c_t; typedef void i2cs_dma_callback(i2c_t *handle); typedef struct { void (*start)(i2c_t *handle); // Function pointer to start the I2C communication. void (*stop)(i2c_t *handle); // Function pointer to stop the I2C communication. BOOL(*wait_ack) (i2c_t *handle); // Function pointer to wait for the acknowledgment from the I2C bus. void (*write_byte)(i2c_t *handle, uint8_t data); // Function pointer to write a byte of data to the I2C bus. uint8_t (*read_byte)(i2c_t *handle, BOOL ack); // Function pointer to read a byte of data from the I2C bus. void (*write_word)(i2c_t *handle, uint16_t data); // Function pointer to write two bytes of data to the I2C bus. BOOL(*write_mem_dma) (i2c_t *handle, uint16_t mem_address, uint16_t mem_addsize, uint8_t *data, uint16_t size); // Function pointer to write multiple bytes of data to a memory address using DMA. BOOL(*read_mem_dma) (i2c_t *handle, uint16_t mem_address, uint16_t mem_addsize, uint8_t *data, uint16_t size); // Function pointer to read multiple bytes of data from a memory address using DMA. } i2c_interface_t; typedef struct { struct GPIO *scl; // Pointer to the GPIO pin used for the I2C clock (SCL). struct GPIO *sda; // Pointer to the GPIO pin used for the I2C data (SDA). } i2c_gpio_group_t; struct I2CS { // Analog part definition i2c_gpio_group_t gpios; // Structure containing the GPIO pins used for the I2C communication. uint16_t delay_ticks; // Number of NOP instructions to delay the I2C communication. // Hardware part definition I2C_TypeDef *i2c; // Pointer to the I2C peripheral. DMA_TypeDef *dma; // Pointer to the DMA peripheral. uint32_t dma_rx_channel; // DMA channel used for receiving data. uint32_t dma_tx_channel; // DMA channel used for transmitting data. uint8_t *rxbuf; // Pointer to the receive buffer. uint16_t rxsize; // Size of the receive buffer. uint8_t *txbuf; // Pointer to the transmit buffer. uint16_t txsize; // Size of the transmit buffer. uint8_t w_address; // 7-bit write address. uint8_t r_address; // 7-bit read address. __IO BOOL rx_dma_ok; // Flag indicating the completion of receive DMA. __IO BOOL tx_dma_ok; // Flag indicating the completion of transmit DMA. i2cs_dma_callback *dma_rx_cb; // Callback function called when receive DMA is completed. i2cs_dma_callback *dma_tx_cb; // Callback function called when transmit DMA is completed. i2c_interface_t interface; // Structure containing the function pointers for the I2C interface. }; /** * @brief Creates an I2C slave instance with GPIO pins for clock and data. * @param gpios The GPIO pins used for the I2C communication. * @param delay_ticks The number of NOP instructions to delay the I2C communication. * @return A pointer to the created I2C slave instance. */ extern i2c_t *i2c_create(i2c_gpio_group_t gpios, uint16_t delay_ticks); /** * @brief Creates an I2C slave instance with DMA support. * @param i2c Pointer to the I2C peripheral. * @param dma Pointer to the DMA peripheral. * @param rxsize Size of the receive buffer. * @param dma_rx_channel DMA channel used for receiving data. * @param dma_rx_cb Callback function called when receive DMA is completed. * @param txsize Size of the transmit buffer. * @param dma_tx_channel DMA channel used for transmitting data. * @param dma_tx_cb Callback function called when transmit DMA is completed. * @return A pointer to the created I2C slave instance. */ extern i2c_t *i2c_create_dma(I2C_TypeDef *i2c, DMA_TypeDef *dma, uint16_t rxsize, uint32_t dma_rx_channel, i2cs_dma_callback *dma_rx_cb, uint16_t txsize, uint32_t dma_tx_channel, i2cs_dma_callback *dma_tx_cb); /** * @brief Sets the write and read addresses for the I2C slave instance with DMA support. * @param handle Pointer to the I2C slave instance. * @param w_address 7-bit write address. * @param r_address 7-bit read address. */ extern void i2c_dma_set_address(i2c_t *handle, uint8_t w_address, uint8_t r_address); /** * @brief Frees the resources used by the I2C slave instance. * @param handle Pointer to the I2C slave instance. */ extern void i2c_free(i2c_t *handle); /** * @brief Callback function called when an I2C event occurs. * @param handle Pointer to the I2C slave instance. */ extern void i2c_ev_callback(i2c_t *handle); /** * @brief Callback function called when an I2C DMA event occurs. * @param handle Pointer to the I2C slave instance. */ extern void i2c_dma_callback(i2c_t *handle); #endif // __I2CS_H__