hpw422移植新的sdk
This commit is contained in:
@@ -0,0 +1,514 @@
|
||||
/*!
|
||||
* \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
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/* the number of channels used to hop */
|
||||
#define XC_HOPPING_NUM 4
|
||||
|
||||
/* retry times, if send times is more than this value, then hop */
|
||||
#define XC_RETRY_NUM 5
|
||||
|
||||
/* */
|
||||
#define SEND_TIMER_MS 10 /* ms */
|
||||
|
||||
#define RECV_TIMER_MS_OUT SEND_TIMER_MS//(XC_HOPPING_NUM + XC_RETRY_NUM + 1) * SEND_TIMERMS_CNT//hopping_num * SEND_TIMERMS_CNT +interval
|
||||
|
||||
#define XC_SEND_NUM 1000U
|
||||
|
||||
#define RF_TX_TIMERx TIMER0_IDX
|
||||
#define RF_RX_TIMERx TIMER0_IDX
|
||||
|
||||
#define RF_CONFIRMED_VALUE 5
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
volatile bool rf24g_rx_irq_flag = false;
|
||||
volatile bool rf24g_tx_irq_flag = false;
|
||||
volatile bool rf24g_rx_to_irq_flag = false;
|
||||
|
||||
bool rf_send_timer_flag = false;
|
||||
bool rf_recv_timer_flag = false;
|
||||
|
||||
/* channels to hop */
|
||||
uint16_t rf_channels[XC_HOPPING_NUM] = {2402, 2424, 2451, 2478}; //{02,13,24,34,46,51,56,61,78};
|
||||
|
||||
/* current channel's index */
|
||||
uint8_t rf_channel_index = 0;
|
||||
|
||||
/* used to calc when to hop */
|
||||
uint32_t recv_hop_num = 0;
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Func Prototype
|
||||
-------------------------------------------------------------------------------------*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
****************************************************************************************
|
||||
* @brief
|
||||
*
|
||||
* @param[in]
|
||||
* @param[in]
|
||||
****************************************************************************************
|
||||
*/
|
||||
void timer_init(uint8_t timer_id, uint32_t timer_cnt)
|
||||
{
|
||||
uint32_t timerx_us = timer_cnt * 1000;
|
||||
Timer_InitCfg_t timer_cfg;
|
||||
|
||||
timer_cfg.timer_src_clk = TIMER_CLK_SRC_32M_DIV;
|
||||
timer_cfg.timer_div_clk = TIMER_DIV_CLK_1MHzOr1K;
|
||||
timer_cfg.timer_mode = TIMER_MODE_CYCLE;
|
||||
|
||||
xc_timer_init(timer_id, &timer_cfg);
|
||||
xc_timer_set_value(timer_id, timerx_us);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief timer0_Callback
|
||||
* @param void * - context
|
||||
*
|
||||
* @retval void
|
||||
*/
|
||||
void timer0_callback(void *context)
|
||||
{
|
||||
#if XINCX_RF_MODE == TX_MODE
|
||||
rf_send_timer_flag = true;
|
||||
#elif XINCX_RF_MODE == RX_MODE
|
||||
rf_recv_timer_flag = true;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
****************************************************************************************
|
||||
* @brief Clock Initialization
|
||||
*
|
||||
* @param[in]
|
||||
* @param[in]
|
||||
****************************************************************************************
|
||||
*/
|
||||
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;
|
||||
#if (RC_32K)
|
||||
clock_cb.lfclk_src = CLOCK_LFCLK_SRC_RC;
|
||||
#endif //(RC_32K)
|
||||
|
||||
#if (XTAL_32K)
|
||||
clock_cb.lfclk_src = CLOCK_LFCLK_SRC_XTAL;
|
||||
#endif // (XTAL_32K)
|
||||
|
||||
#if (XTAL_32768K)
|
||||
clock_cb.lfclk_src = CLOCK_LFCLK_SRC_XTAL;
|
||||
#endif // (XTAL_32768K)
|
||||
#ifdef XC62XX
|
||||
clock_cb.lfclk_src = CLOCK_LFCLK_SRC_RC;
|
||||
clock_cfg.lfclk_in = CLOCK_LFCLK_IN_32K;
|
||||
#else
|
||||
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;
|
||||
}
|
||||
#endif
|
||||
xc_clock_init_cfg(&clock_cfg);
|
||||
|
||||
SysTick_Config(xc_clock_hfclk_in_get() / 100);
|
||||
SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_LOG
|
||||
/**
|
||||
* @brief RF 2.4g register information printing
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void rf_2M_reg_info(void)
|
||||
{
|
||||
/* Register information printing */
|
||||
LOGI("CFG_TOP: 0x%08x\r\n", XC_RF_2_4G->CFG_TOP);
|
||||
LOGI("TXPROC_CFG: 0x%08x\r\n", XC_RF_2_4G->TXPROC_CFG);
|
||||
LOGI("RXPROC_CFG_L: 0x%08x\r\n", XC_RF_2_4G->RXPROC_CFG_L);
|
||||
LOGI("RF_CH: 0x%08x\r\n", XC_RF_2_4G->RF_CH);
|
||||
LOGI("rccal_reg_l: 0x%08x\r\n", XC_BT_RF->rccal_reg_l);
|
||||
LOGI("ana2_reg_l: 0x%08x\r\n", XC_BT_RF->ana2_reg_l);
|
||||
LOGI("ana3_reg_l: 0x%08x\r\n", XC_BT_RF->ana3_reg_l);
|
||||
LOGI("ana4_reg_l: 0x%08x\r\n", XC_BT_RF->ana4_reg_l);
|
||||
LOGI("ana5_reg_l: 0x%08x\r\n", XC_BT_RF->ana5_reg_l);
|
||||
LOGI("ana14_reg_l: 0x%08x\r\n", XC_BT_RF->ana14_reg_l);
|
||||
LOGI("ana15_reg_l: 0x%08x\r\n", XC_BT_RF->ana15_reg_l);
|
||||
LOGI("ana16_reg_l: 0x%08x\r\n", XC_BT_RF->ana16_reg_l);
|
||||
LOGI("ana17_reg_l: 0x%08x\r\n", XC_BT_RF->ana17_reg_l);
|
||||
LOGI("ana18_reg_l: 0x%08x\r\n", XC_BT_RF->ana18_reg_l);
|
||||
LOGI("ana21_reg_l: 0x%08x\r\n", XC_BT_RF->ana21_reg_l);
|
||||
LOGI("AON_RF_AONREG: 0x%08x\r\n", cprao_aon_rf_aonreg_get()); // 0x36
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Rf 2.4g dump log
|
||||
* @param void
|
||||
*
|
||||
* @retval void
|
||||
*/
|
||||
void rf_dump_log(void)
|
||||
{
|
||||
LOGI("***************** 2.4G Param ******************\n");
|
||||
LOGI("CFG_TOP: 0x%08x\r\n", XC_RF_2_4G->CFG_TOP);
|
||||
LOGI("EN_AA: 0x%08x\r\n", XC_RF_2_4G->EN_AA);
|
||||
LOGI("EN_RXADDR: 0x%08x\r\n", XC_RF_2_4G->EN_RXADDR);
|
||||
LOGI("SETUP_AW: 0x%08x\r\n", XC_RF_2_4G->SETUP_AW);
|
||||
LOGI("SETUP_RETR: 0x%08x\r\n", XC_RF_2_4G->SETUP_RETR);
|
||||
LOGI("RF_CH: 0x%08x\r\n", XC_RF_2_4G->RF_CH);
|
||||
LOGI("SETUP_RF: 0x%08x\r\n", XC_RF_2_4G->SETUP_RF);
|
||||
LOGI("STATUS: 0x%08x\r\n", XC_RF_2_4G->STATUS);
|
||||
LOGI("OBSERVE_TX: 0x%08x\r\n", XC_RF_2_4G->OBSERVE_TX);
|
||||
LOGI("RSSI: 0x%08x\r\n", XC_RF_2_4G->RSSI);
|
||||
LOGI("RX_ADDR_P0: 0x%02x %08x\r\n", 0xFF & (XC_RF_2_4G->RX_ADDR_P0_H), XC_RF_2_4G->RX_ADDR_P0_L);
|
||||
LOGI("RX_ADDR_P1: 0x%02x %08x\r\n", 0xFF & (XC_RF_2_4G->RX_ADDR_P1_H), XC_RF_2_4G->RX_ADDR_P1_L);
|
||||
LOGI("RX_ADDR_P2TOP5:0x%08x\r\n", XC_RF_2_4G->RX_ADDR_P2TOP5);
|
||||
LOGI("BER_RESULT: 0x%08x%08x\r\n", XC_RF_2_4G->BER_ERR_CNT, XC_RF_2_4G->BER_RECV_CNT);
|
||||
LOGI("AGC_SETTING: 0x%08x\r\n", XC_RF_2_4G->AGC_SETTING);
|
||||
LOGI("PGA_SETTING: 0x%02x %08x\r\n", ((XC_RF_2_4G->TX_ADDR_H) & 0xFF00) >> 8, XC_RF_2_4G->PGA_SETTING);
|
||||
LOGI("TX_ADDR: 0x%02x %08x\r\n", ((XC_RF_2_4G->TX_ADDR_H) & 0xFF), XC_RF_2_4G->TX_ADDR_L);
|
||||
LOGI("RX_PW_PX: 0x%04x %08x\r\n", 0xFFFF & (XC_RF_2_4G->RX_PW_Px_H), XC_RF_2_4G->RX_PW_Px_L);
|
||||
LOGI("STATUS_FIFO: 0x%08x\r\n", XC_RF_2_4G->STATUS_FIFO);
|
||||
LOGI("RSSIREC: 0x%08x\r\n", XC_RF_2_4G->RSSIREC);
|
||||
LOGI("TXPROC_CFG: 0x%08x\r\n", XC_RF_2_4G->TXPROC_CFG);
|
||||
LOGI("RXPROC_CFG: 0x%02x %08x\r\n", 0xFF & (XC_RF_2_4G->RXPROC_CFG_H), XC_RF_2_4G->RXPROC_CFG_L);
|
||||
LOGI("DYNPD: 0x%08x\r\n", XC_RF_2_4G->DYNPD);
|
||||
LOGI("FEATURE: 0x%08x\r\n", XC_RF_2_4G->FEATURE);
|
||||
LOGI("PGA_SETTING_H: 0x%08x\r\n", XC_RF_2_4G->TX_ADDR_H);
|
||||
LOGI("TX_POWER 0x%08x\r\n", (XC_BT_RF->ana21_reg_l >> 6) & 0x3f);
|
||||
LOGI("ana11_reg_l: 0x%08x\r\n", XC_BT_RF->ana11_reg_l);
|
||||
LOGI("***********************************************\n");
|
||||
LOGI("\r\n");
|
||||
}
|
||||
#endif // DEBUG_LOG
|
||||
|
||||
/**
|
||||
* @brief RF 2.4g buff info
|
||||
* @param uint8_t * - buff
|
||||
* uint8_t len
|
||||
* @retval void
|
||||
*/
|
||||
void rf_buff_info(uint8_t *buff, uint8_t len)
|
||||
{
|
||||
for (uint8_t i = 0; i < len; i++)
|
||||
LOGI("%02x ", buff[i]);
|
||||
LOGI("\r\n");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
void rf_channel_hop(void)
|
||||
{
|
||||
LOGI("rf_hop_channel:%d\r\n",XC_RF_2_4G->RF_CH & 0x3fff);
|
||||
|
||||
rf_channel_index = (rf_channel_index+1)%XC_HOPPING_NUM;
|
||||
|
||||
rf_set_channel(rf_channels[rf_channel_index]);
|
||||
}
|
||||
|
||||
/* RF接收完成回调 */
|
||||
void rf_24g_rx_dr_cb(void)
|
||||
{
|
||||
/* set flag */
|
||||
rf24g_rx_irq_flag = true;
|
||||
}
|
||||
|
||||
|
||||
/* RF发送完成回调 */
|
||||
void rf_24g_tx_ds_cb(void)
|
||||
{
|
||||
rf24g_tx_irq_flag = true;
|
||||
}
|
||||
|
||||
|
||||
/* RF接收超时回调 */
|
||||
void rf_24g_max_rt_cb(void)
|
||||
{
|
||||
rf24g_rx_to_irq_flag = true;
|
||||
}
|
||||
|
||||
/* RF 发送数据(自动应答) */
|
||||
__RAM_CODE eRF_Tx_Status rf_tx_autoack(uint8_t *buff, uint16_t len)
|
||||
{
|
||||
eRF_Tx_Status ret = TX_DATA_INVALID;
|
||||
|
||||
/* len can't be 0 */
|
||||
if (len == 0)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* rf_off_and_on */
|
||||
rf_off_and_on();
|
||||
|
||||
rf24g_tx_irq_flag = false;
|
||||
rf24g_rx_to_irq_flag = false;
|
||||
|
||||
/* send data */
|
||||
rf_tx_packet(buff, len);
|
||||
|
||||
/* wait for ack */
|
||||
while(1)
|
||||
{
|
||||
/* recv ack */
|
||||
if(rf24g_tx_irq_flag)
|
||||
{
|
||||
ret = TX_DATA_OK;
|
||||
break;
|
||||
}
|
||||
/* time out */
|
||||
else if(rf24g_rx_to_irq_flag)
|
||||
{
|
||||
ret = TX_RETRANS_MAX;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void rf24g_hop_tx_demo()
|
||||
{
|
||||
uint8_t ack_check = 0;
|
||||
uint16_t ack_cnt = 0;
|
||||
int ack_status;
|
||||
uint32_t tx_cnt = 0;
|
||||
uint8_t tx_buff[BUFF_LEN] = {0};
|
||||
|
||||
/* Set channel */
|
||||
rf_set_channel(rf_channels[rf_channel_index]);
|
||||
|
||||
/* Init Timer */
|
||||
timer_init(RF_TX_TIMERx, SEND_TIMER_MS);
|
||||
xc_timer_start(RF_TX_TIMERx);
|
||||
|
||||
/* fill data */
|
||||
for (uint8_t i = 0; i < BUFF_LEN; i++)
|
||||
{
|
||||
tx_buff[i] = i;
|
||||
}
|
||||
|
||||
LOGI("%s start!\r\n", __func__);
|
||||
|
||||
/* main loop */
|
||||
while(true)
|
||||
{
|
||||
/* Send pack periodically using Timer */
|
||||
if ((rf_send_timer_flag))
|
||||
{
|
||||
rf_send_timer_flag = false;
|
||||
|
||||
/* Filling in software frame number */
|
||||
tx_cnt++;
|
||||
tx_buff[0] = tx_cnt;
|
||||
|
||||
/* data transmission */
|
||||
ack_status = rf_tx_autoack(tx_buff, BUFF_LEN);
|
||||
|
||||
/* The sent data was received. */
|
||||
if (ack_status == TX_DATA_OK)
|
||||
{
|
||||
ack_cnt += 1;
|
||||
LOGI("ack ok :%d\r\n", ack_cnt);
|
||||
ack_check = 0;
|
||||
}
|
||||
/* The sent data was not received. */
|
||||
else // if(status == TX_RETRANS_MAX) )
|
||||
{
|
||||
LOGI("failed\r\n");
|
||||
ack_check++;
|
||||
}
|
||||
|
||||
/* If the transmission fails more than XC_RETRY_NUM times, switch next frequency and continue retransmission. */
|
||||
if (ack_check > XC_RETRY_NUM)
|
||||
{
|
||||
/* Channel hopping */
|
||||
rf_channel_hop();
|
||||
ack_check = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void rf24g_hop_rx_demo()
|
||||
{
|
||||
static uint32_t conut = 0;
|
||||
uint8_t rx_buff[BUFF_LEN+1] = {0};
|
||||
uint8_t rx_len = 0;
|
||||
|
||||
/* Set frequency points */
|
||||
rf_set_channel(rf_channels[rf_channel_index]);
|
||||
|
||||
/* Set receiving channel hopping interval */
|
||||
timer_init(RF_RX_TIMERx, RECV_TIMER_MS_OUT);
|
||||
xc_timer_start(RF_RX_TIMERx);
|
||||
|
||||
LOGI("%s start!\r\n", __func__);
|
||||
|
||||
/* RX enters receiver */
|
||||
CE_CTL_HIGH;
|
||||
|
||||
while(true)
|
||||
{
|
||||
/* 1.Data received */
|
||||
if (rf24g_rx_irq_flag)
|
||||
{
|
||||
/* Stop channel hopping timer */
|
||||
xc_timer_stop(RF_RX_TIMERx);
|
||||
|
||||
rf24g_rx_irq_flag = false;
|
||||
|
||||
/* Entering standby mode */
|
||||
CE_CTL_LOW;
|
||||
|
||||
/* Read rf data */
|
||||
rx_len = rf_rx_packet(rx_buff);
|
||||
|
||||
/* Receive Count */
|
||||
conut += 1;
|
||||
|
||||
LOGI("recv:%d\r\n", conut);
|
||||
|
||||
/* Print the received data. */
|
||||
//rf_buff_info(rx_buff, rx_len);
|
||||
|
||||
delay_us(200);
|
||||
|
||||
/* rf_off_and_on */
|
||||
rf_off_and_on();
|
||||
|
||||
/* Channel hopping counter reset */
|
||||
recv_hop_num = 0;
|
||||
|
||||
/* Start channel hopping timer */
|
||||
xc_timer_start(RF_RX_TIMERx);
|
||||
|
||||
/* Entering the receiver */
|
||||
CE_CTL_HIGH;
|
||||
}
|
||||
|
||||
/* 2.No data received */
|
||||
if (rf_recv_timer_flag)
|
||||
{
|
||||
rf_recv_timer_flag = false;
|
||||
|
||||
recv_hop_num++;
|
||||
|
||||
/* Each channel RX retry XC_RETRY_NUM times in first hop cycle */
|
||||
if ((recv_hop_num % (XC_RETRY_NUM) == 0) && (recv_hop_num < XC_HOPPING_NUM * XC_RETRY_NUM))
|
||||
{
|
||||
/* Channel hopping */
|
||||
rf_channel_hop();
|
||||
}
|
||||
/* Each channel RX retry XC_RETRY_NUM times */
|
||||
else if (recv_hop_num % (XC_HOPPING_NUM * XC_RETRY_NUM) == 0)
|
||||
{
|
||||
/* Channel hopping */
|
||||
rf_channel_hop();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
/* Clock initialization */
|
||||
clock_init();
|
||||
|
||||
/* Serial port initialization */
|
||||
app_uart_init();
|
||||
|
||||
/* rf 2.4g initialization configuration */
|
||||
rf24g_init();
|
||||
|
||||
#if (XINCX_RF_TRANS_RATE == DR_2M)
|
||||
/* Set 2M mode register configuration */
|
||||
/* Don't move this function interface */
|
||||
rf_2M_config();
|
||||
#endif //(XINCX_RF_TRANS_RATE == DR_2M)
|
||||
|
||||
#if (XINCX_RF_NVIC_MODE)
|
||||
/* Enable RF2.4G interrupts */
|
||||
NVIC_EnableIRQ(RF24G_IRQn);
|
||||
/* Configure the RF2.4G interrupt priority */
|
||||
NVIC_SetPriority(RF24G_IRQn, 0);
|
||||
#endif // XINCX_RF_NVIC_MODE
|
||||
|
||||
xc_system_param_check_rf24g();
|
||||
|
||||
#if XINCX_RF_MODE == TX_MODE
|
||||
rf24g_hop_tx_demo();
|
||||
#elif XINCX_RF_MODE == RX_MODE
|
||||
rf24g_hop_rx_demo();
|
||||
#endif
|
||||
|
||||
while(true);
|
||||
}
|
||||
Reference in New Issue
Block a user