This commit is contained in:
moyuhai
2026-06-11 13:00:46 +08:00
commit e8074162f3
2017 changed files with 507042 additions and 0 deletions
@@ -0,0 +1,154 @@
/*!
* \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 "xc_drv_i2c.h"
/*------------------------------------------------------------------------------------
Macros
-------------------------------------------------------------------------------------*/
#define I2C_SLAVE_ADDR 0x50
/*------------------------------------------------------------------------------------
Global Variables
-------------------------------------------------------------------------------------*/
typedef struct
{
uint8_t r_buff[90];
uint8_t r_len;
uint8_t w_buff[90];
uint8_t w_len;
bool rx_done;
bool tx_done;
} i2c_user_t;
i2c_user_t i2c_user_ctl;
/*------------------------------------------------------------------------------------
Functions
-------------------------------------------------------------------------------------*/
void i2c_slave_read(uint8_t *buff, uint16_t len)
{
volatile static uint8_t rx_cnt = 0;
for (int i = 0; i < len; i++) {
i2c_user_ctl.r_buff[rx_cnt++] = buff[i];
i2c_user_ctl.r_len++;
}
if (rx_cnt == sizeof(i2c_user_ctl.r_buff)) {
i2c_user_ctl.rx_done = true;
rx_cnt = 0;
}
}
void i2c_slave_write()
{
volatile static uint8_t tx_cnt = 0;
i2c_user_ctl.w_buff[tx_cnt] = tx_cnt;
i2c_user_ctl.w_len++;
i2c_data_cmd_set(tx_cnt++);
if (tx_cnt == sizeof(i2c_user_ctl.r_buff) / 3) {
i2c_user_ctl.tx_done = true;
tx_cnt = 0;
}
}
/**
* @brief i2c_slave_demo
* @details
* @param void
* @retval void
*/
void i2c_slave_demo(void)
{
DEBUG("i2c_slave_test \n");
I2C_InitCfg_t i2c_cfg = {0};
GPIO_InitCfg_t gpio_cfg = {0};
gpio_cfg.Mux = GPIO_Mux0;
gpio_cfg.Pull = GPIO_PULLUP;
gpio_cfg.Int = NOT_INT;
gpio_cfg.Pin = GPIO_3;
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
gpio_cfg.FunSel = I2C_SCL;
xc_gpio_init(&gpio_cfg);
gpio_cfg.Pin = GPIO_4;
gpio_cfg.Dir = GPIO_DIR_INPUT;
gpio_cfg.FunSel = I2C_SDA;
xc_gpio_init(&gpio_cfg);
i2c_cfg.I2C_ClockSpeed = I2C_CLOCK_SPEED_400K;
i2c_cfg.I2C_Mode = I2C_MODE_SLAVE;
i2c_cfg.I2C_SlaveAddr = I2C_SLAVE_ADDR;
xc_i2c_init(&i2c_cfg);
gpio_cfg.Pin = GPIO_14;
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
gpio_cfg.FunSel = GPIO_Dx;
xc_gpio_init(&gpio_cfg);
gpio_cfg.Pin = GPIO_15;
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
gpio_cfg.FunSel = GPIO_Dx;
xc_gpio_init(&gpio_cfg);
xc_i2c_register_read_cb(i2c_slave_read);
xc_i2c_register_write_cb(i2c_slave_write);
xc_i2c_enable_it(I2C_INTR_STAT_RX_DONE_SET | I2C_INTR_STAT_STOP_DET_SET | I2C_INTR_STAT_RD_REQ_SET |
I2C_INTR_STAT_RX_FULL_SET);
xc_i2c_fifo_rxtl_set(I2C_RX_TL_FIFO_1);
xc_i2c_fifo_txtl_set(I2C_TX_TL_FIFO_0);
NVIC_EnableIRQ(I2C_IRQn);
while (1) {
if (i2c_user_ctl.rx_done == true) {
i2c_user_ctl.rx_done = false;
DEBUG("rx_data:\n");
for (int i = 0; i < i2c_user_ctl.r_len; i++) {
DEBUG("%d-", i2c_user_ctl.r_buff[i]);
}
DEBUG("\n");
i2c_user_ctl.r_len = 0;
}
if (i2c_user_ctl.tx_done == true) {
i2c_user_ctl.tx_done = false;
DEBUG("tx_data:\n");
for (int i = 0; i < i2c_user_ctl.w_len; i++) {
DEBUG("%d-", i2c_user_ctl.w_buff[i]);
}
DEBUG("\n");
i2c_user_ctl.w_len = 0;
}
}
}
@@ -0,0 +1,114 @@
/*!
* \file main.c
*
* \brief Target main 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 "main.h"
/*------------------------------------------------------------------------------------
Macros
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Global Variables
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Func Prototype
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Functions
-------------------------------------------------------------------------------------*/
void clock_init(void)
{
CLOCK_InitCfg_t clock_cfg;
clock_cfg.hfclk_src = CLOCK_HFCLK_SRC_XTAL;
clock_cfg.hfclk_in = CLOCK_HFCLK_IN_32M;
clock_cfg.lfclk_src = CLOCK_LFCLK_SRC_RC;
if(clock_cfg.lfclk_src == CLOCK_LFCLK_SRC_XTAL) {
clock_cfg.lfclk_in = CLOCK_LFCLK_IN_32768;
} else if(clock_cfg.lfclk_src == CLOCK_LFCLK_SRC_RC) {
clock_cfg.lfclk_in = CLOCK_LFCLK_IN_32K;
}
xc_clock_init_cfg(&clock_cfg);
SysTick_Config(xc_clock_hfclk_in_get( ) / 100);
SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
}
void app_uart_init(void)
{
GPIO_InitCfg_t gpio_cfg = {0};
gpio_cfg.Mux = GPIO_Mux0;
gpio_cfg.Pull = GPIO_PULLUP;
gpio_cfg.Int = NOT_INT;
gpio_cfg.FunSel = UART0_TX;
gpio_cfg.Pin = GPIO_18;
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
xc_gpio_init(&gpio_cfg);
gpio_cfg.FunSel = UART0_RX;
gpio_cfg.Pin = GPIO_19;
gpio_cfg.Dir = GPIO_DIR_INPUT;
xc_gpio_init(&gpio_cfg);
UART_InitCfg_t uart_cfg = {0};
uart_cfg.Parity = UART_PARITY_DISABLE;
uart_cfg.StopBits = UART_STOP_1_BITS;
uart_cfg.WordLength = UART_DATA_8_BITS;
uart_cfg.BaudRate = UART_BAUDRATE_115200;
uart_cfg.HardwareFlowControl = UART_HWFC_DISABLE;
xc_uart_init(UART0_IDX, &uart_cfg);
}
int main(void)
{
// Clock Initialization
clock_init();
app_uart_init();
DEBUG("__START__\n");
/* iic slave demo */
i2c_slave_demo( );
while(1)
{
/* main loop */
;
}
}