hpw422移植新的sdk
This commit is contained in:
@@ -0,0 +1,277 @@
|
||||
/*!
|
||||
* \file i2c_eeprom.c
|
||||
*
|
||||
* \brief Target i2c eeprom 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_eeprom.h"
|
||||
#include "xc_drv_i2c.h"
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
#define AT24C01 128
|
||||
#define AT24C02 256
|
||||
#define AT24C04 512
|
||||
#define AT24C08 1024
|
||||
#define AT24C16 2048
|
||||
#define AT24C32 4096
|
||||
#define AT24C64 8192
|
||||
#define AT24C128 16384
|
||||
#define AT24C256 32768
|
||||
|
||||
#define EE_PAGE_ADDR_UNIT 256
|
||||
#define EE_PAGE_BYTE 8
|
||||
#define EE_SIZE AT24C08
|
||||
#define I2C_SLAVE_ADDR (0x50)
|
||||
|
||||
#define AT24XX_SR1_ADDR (0x50)
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
void at24xx_wait_standby(uint8_t dev_addr)
|
||||
{
|
||||
|
||||
uint32_t abort_reason;
|
||||
|
||||
uint16_t timeout = 0xFFFF;
|
||||
|
||||
do {
|
||||
i2c_data_cmd_set(I2C_DATA_CMD_CMD_READ | I2C_DATA_CMD_STOP_SET | I2C_DATA_CMD_RESTART_RESET);
|
||||
|
||||
do {
|
||||
|
||||
} while (timeout-- && !(i2c_raw_intr_stat_get() & I2C_RAW_INTR_STAT_TX_EMPTY_SET));
|
||||
|
||||
delay_us(100);
|
||||
|
||||
abort_reason = i2c_tx_abrt_source_get();
|
||||
if (abort_reason) {
|
||||
i2c_clr_tx_abrt_get();
|
||||
}
|
||||
|
||||
} while (abort_reason != 0);
|
||||
|
||||
if (abort_reason != 0) {
|
||||
printf("abort_reason=0x%x\n", abort_reason);
|
||||
}
|
||||
}
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void at24cxx_read(uint16_t addr, uint8_t *buff, uint16_t num)
|
||||
{
|
||||
uint8_t val;
|
||||
|
||||
while (i2c_flag_status_get(I2C_STATUS_ACTIVITY_RESET))
|
||||
;
|
||||
|
||||
while (num) {
|
||||
if (EE_SIZE <= AT24C16) {
|
||||
i2c_enable_set(I2C_ENABLE_EN_DISABLE);
|
||||
|
||||
if (addr > (EE_PAGE_ADDR_UNIT - 1)) {
|
||||
|
||||
i2c_tar__i2c_tar__setf(I2C_SLAVE_ADDR + (addr / EE_PAGE_ADDR_UNIT));
|
||||
} else {
|
||||
i2c_tar__i2c_tar__setf(I2C_SLAVE_ADDR);
|
||||
}
|
||||
i2c_enable_set(I2C_ENABLE_EN_ENABLE);
|
||||
} else if (EE_SIZE >= AT24C32) {
|
||||
i2c_data_cmd_set(addr / EE_PAGE_ADDR_UNIT);
|
||||
}
|
||||
|
||||
i2c_data_cmd_set(I2C_DATA_CMD_CMD_WRITE | I2C_DATA_CMD_STOP_RESET | I2C_DATA_CMD_RESTART_RESET |
|
||||
(addr % EE_PAGE_ADDR_UNIT));
|
||||
|
||||
i2c_data_cmd_set(I2C_DATA_CMD_CMD_READ | I2C_DATA_CMD_STOP_SET | I2C_DATA_CMD_RESTART_RESET);
|
||||
|
||||
while (!i2c_flag_status_get(I2C_STATUS_RFNE_SET))
|
||||
;
|
||||
|
||||
val = i2c_data_cmd__dat__getf();
|
||||
|
||||
*buff++ = val;
|
||||
|
||||
num--;
|
||||
|
||||
addr++;
|
||||
}
|
||||
}
|
||||
|
||||
void at24cxx_write(uint16_t addr, uint8_t *buff, uint16_t num)
|
||||
{
|
||||
uint16_t cnt = 0;
|
||||
uint16_t data_addr = addr;
|
||||
|
||||
uint8_t dev_addr = 0;
|
||||
while (i2c_flag_status_get(I2C_STATUS_ACTIVITY_SET))
|
||||
;
|
||||
|
||||
if (EE_SIZE <= AT24C16) {
|
||||
|
||||
if (data_addr > (EE_PAGE_ADDR_UNIT - 1)) {
|
||||
dev_addr = I2C_SLAVE_ADDR + (data_addr / EE_PAGE_ADDR_UNIT);
|
||||
} else {
|
||||
dev_addr = I2C_SLAVE_ADDR;
|
||||
}
|
||||
|
||||
xc_i2c_write_block(dev_addr, (uint8_t *)(&data_addr), 1, I2C_DATA_CMD_RESTART_RESET, I2C_DATA_CMD_STOP_RESET);
|
||||
|
||||
data_addr = data_addr % EE_SIZE;
|
||||
|
||||
} else if (EE_SIZE >= AT24C32) {
|
||||
|
||||
data_addr = data_addr / EE_PAGE_ADDR_UNIT;
|
||||
xc_i2c_write_block(dev_addr, (uint8_t *)(&data_addr), 1, I2C_DATA_CMD_RESTART_RESET, I2C_DATA_CMD_STOP_RESET);
|
||||
|
||||
data_addr = data_addr % EE_PAGE_ADDR_UNIT;
|
||||
xc_i2c_write_block(dev_addr, (uint8_t *)(&data_addr), 1, I2C_DATA_CMD_RESTART_RESET, I2C_DATA_CMD_STOP_RESET);
|
||||
}
|
||||
|
||||
while (num--) {
|
||||
cnt++;
|
||||
|
||||
if (num == 0) {
|
||||
xc_i2c_write_block(dev_addr, buff, 1, I2C_DATA_CMD_RESTART_RESET, I2C_DATA_CMD_STOP_SET);
|
||||
|
||||
at24xx_wait_standby(dev_addr);
|
||||
|
||||
} else if (cnt % EE_PAGE_BYTE != 0) { // cnt =7
|
||||
|
||||
xc_i2c_write_block(dev_addr, buff, 1, I2C_DATA_CMD_RESTART_RESET, I2C_DATA_CMD_STOP_RESET);
|
||||
buff++;
|
||||
} else if (cnt % EE_PAGE_BYTE == 0) { // page over, next start
|
||||
|
||||
xc_i2c_write_block(dev_addr, buff, 1, I2C_DATA_CMD_RESTART_RESET, I2C_DATA_CMD_STOP_SET);
|
||||
|
||||
at24xx_wait_standby(dev_addr);
|
||||
|
||||
data_addr += EE_PAGE_BYTE;
|
||||
buff++;
|
||||
|
||||
if (data_addr > (EE_PAGE_ADDR_UNIT - 1)) {
|
||||
dev_addr = I2C_SLAVE_ADDR + (data_addr / EE_PAGE_ADDR_UNIT);
|
||||
}
|
||||
|
||||
data_addr = data_addr % EE_PAGE_ADDR_UNIT;
|
||||
|
||||
xc_i2c_write_block(dev_addr, (uint8_t *)(&data_addr), 1, I2C_DATA_CMD_RESTART_RESET,
|
||||
I2C_DATA_CMD_STOP_RESET);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief i2c_eeprom_test_demo
|
||||
* @details
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void i2c_eeprom_demo(void)
|
||||
{
|
||||
uint8_t txbuff[512] = {"XC66xx I2C TEST \n"};
|
||||
uint8_t rxbuff[512] = {0};
|
||||
uint16_t len;
|
||||
|
||||
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_SDA;
|
||||
xc_gpio_init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.Pin = GPIO_2;
|
||||
gpio_cfg.Dir = GPIO_DIR_INPUT;
|
||||
gpio_cfg.FunSel = I2C_SCL;
|
||||
xc_gpio_init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.Mux = GPIO_Mux0;
|
||||
gpio_cfg.Pull = GPIO_PULLUP;
|
||||
gpio_cfg.Int = NOT_INT;
|
||||
gpio_cfg.FunSel = GPIO_Dx;
|
||||
|
||||
gpio_cfg.Pin = GPIO_8;
|
||||
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
|
||||
xc_gpio_init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = GPIO_Dx;
|
||||
gpio_cfg.Pin = GPIO_9;
|
||||
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
|
||||
xc_gpio_init(&gpio_cfg);
|
||||
|
||||
gpio_cfg.FunSel = GPIO_Dx;
|
||||
gpio_cfg.Pin = GPIO_10;
|
||||
gpio_cfg.Dir = GPIO_DIR_OUTPUT;
|
||||
xc_gpio_init(&gpio_cfg);
|
||||
|
||||
i2c_cfg.I2C_ClockSpeed = I2C_CLOCK_SPEED_400K;
|
||||
i2c_cfg.I2C_Mode = I2C_MODE_MASTER;
|
||||
i2c_cfg.I2C_SlaveAddr = I2C_SLAVE_ADDR;
|
||||
|
||||
xc_i2c_init(&i2c_cfg);
|
||||
|
||||
len = 512;
|
||||
|
||||
for (int i = 0; i < sizeof(txbuff); i++) {
|
||||
txbuff[i] = 0xFF;
|
||||
}
|
||||
|
||||
at24cxx_write(0, (uint8_t *)txbuff, len);
|
||||
|
||||
for (int i = 0; i < sizeof(txbuff) / 2; i++) {
|
||||
txbuff[i] = i;
|
||||
}
|
||||
|
||||
for (int i = sizeof(txbuff) / 2; i < sizeof(txbuff); i++) {
|
||||
txbuff[i] = sizeof(txbuff) - i - 1;
|
||||
}
|
||||
|
||||
DEBUG("Start Write 24Cxx....\r\n");
|
||||
at24cxx_write(0, (uint8_t *)txbuff, len);
|
||||
DEBUG("24Cxx Write Sucess!\r\n");
|
||||
|
||||
DEBUG("Start Read 24Cxx....\r\n");
|
||||
|
||||
at24cxx_read(0, rxbuff, len);
|
||||
DEBUG("The Data Readed Is: \r\n");
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
DEBUG("%d[%d-%d]\n", i, txbuff[i], rxbuff[i]);
|
||||
if (rxbuff[i] != txbuff[i]) {
|
||||
DEBUG("i2c test error\n");
|
||||
break;
|
||||
}
|
||||
if (i == len - 1) {
|
||||
DEBUG("i2c test ok\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
/*!
|
||||
* \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 eeprom demo */
|
||||
i2c_eeprom_demo( );
|
||||
|
||||
|
||||
while(1)
|
||||
{
|
||||
/* main loop */
|
||||
;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user