/*! * \file i2c_slave.c * * \brief Target i2c slave implementation * * \copyright Revised BSD License, see section \ref LICENSE. * * \code * * _ __ _ ________ _ * | |/ /(_)___ / ____/ /_ (_)___ * | // / __ \/ / / __ \/ / __ \ * / |/ / / / / /___/ / / / / /_/ / * /_/|_/_/_/ /_/\____/_/ /_/_/ .___/ * /_/ * (C) 2022-2025 XinChip * * \endcode * * \author ( XinChip ) Alex-J * * \author ( XinChip ) */ /*----------------------------------------------------------------------------------- INCLUDE HEADE FILES ------------------------------------------------------------------------------------*/ #include "i2c_slave.h" #include "xc6xxx_hal_i2c.h" /*------------------------------------------------------------------------------------ Macros -------------------------------------------------------------------------------------*/ /*------------------------------------------------------------------------------------ Global Variables -------------------------------------------------------------------------------------*/ /*------------------------------------------------------------------------------------ Functions -------------------------------------------------------------------------------------*/ /** * @brief i2c_slave_demo * @details * @param void * @retval void */ void i2c_slave_demo(void) { DEBUG("i2c_slave_test \n"); I2C_InitTypeDef I2C_InitStruct = {0}; GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitStruct.Mux = GPIO_Mux0; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Int = NOT_INT; GPIO_InitStruct.Pin = GPIO_5; GPIO_InitStruct.Dir = GPIO_DIR_OUTPUT; GPIO_InitStruct.FunSel = I2C_SCL; GPIO_Init(&GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_4; GPIO_InitStruct.Dir = GPIO_DIR_INPUT; GPIO_InitStruct.FunSel = I2C_SDA; GPIO_Init(&GPIO_InitStruct); I2C_InitStruct.I2C_ClockSpeed = I2C_ClockSpeed_400K; I2C_InitStruct.I2C_Mode = I2C_Mode_SLAVE; I2C_InitStruct.I2C_OwnAddress1 = 0xA0 >> 1; I2C_Init(XC_I2C, &I2C_InitStruct); I2C_Enable_IT(XC_I2C, I2C_INTR_STAT_RX_DONE_GENERATED | I2C_INTR_STAT_STOP_DET_GENERATED | I2C_INTR_STAT_RD_REQ_GENERATED | I2C_INTR_STAT_RX_FULL_GENERATED); I2C_FIFO_IT_RXTL_Set(XC_I2C, I2C_RX_TL_FIFO_1); I2C_FIFO_IT_TXTL_Set(XC_I2C, I2C_TX_TL_FIFO_0); NVIC_EnableIRQ(I2C_IRQn); while (1) { if (i2c_slave_rx_flag == 1) { for (int i = 0; i < 32; i++) { DEBUG("test_buff[%d]=%d\n", i, i2c_slave_rx_buff[i]); } i2c_slave_rx_flag = 0; } } }