ChipTest/PAN159/PAN159-Template/Library/StdDriver/driver/lib_driver_xn297l.c

290 lines
11 KiB
C
Raw Normal View History

2021-09-26 09:18:47 +00:00
/*******************************************************************************
* @note Copyright (C) 2017 Shanghai Panchip Microelectronics Co., Ltd.
* All rights reserved.
*
* @file drv_xn297l.c
* @brief XN297L RF<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*
* @history - V1.0, 2017-03-06, xiaoguolin, first implementation.
*******************************************************************************/
#include "lib_driver_xn297l.h"
/******************************************************************************/
//void rf_write_reg(uint8_t reg,uint8_t data);
//rfд<66>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//input:uint8_t reg<65><67><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7>uint8_t data:<3A><>Ҫд<D2AA><D0B4><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ֵ
//output:<3A><>
/******************************************************************************/
void xn297l_write_reg(uint8_t reg,uint8_t data)
{
rfspi_cs(0); //ʹ<><CAB9>Ƭѡ
rfspi_rwc(reg); //д<><D0B4><EFBFBD>ļĴ<C4BC><C4B4><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ
rfspi_rwc(data); //д<><D0B4><EFBFBD>ļĴ<C4BC><C4B4><EFBFBD><EFBFBD><EFBFBD>ֵ
rfspi_cs(1); //<2F><><EFBFBD><EFBFBD>Ƭѡ
}
/******************************************************************************/
//uint8_t rf_read_reg(uint8_t reg);
//rf<72><66><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//input:uint8_t reg<65><67><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>ȡ<EFBFBD>ļĴ<C4BC><C4B4><EFBFBD>
//output:<3A>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ֵ
/******************************************************************************/
uint8_t xn297l_read_reg(uint8_t reg)
{
uint8_t tmp;
rfspi_cs(0); //ʹ<><CAB9>Ƭѡ
rfspi_rwc(reg); //д<><D0B4><EFBFBD>ļĴ<C4BC><C4B4><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ
tmp = rfspi_rwc(0x00); //<2F><>ȡ<EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ֵ
rfspi_cs(1); //<2F><><EFBFBD><EFBFBD>Ƭѡ
return tmp;
}
/******************************************************************************/
//void rf_write_buf(uint8_t reg,uint8_t *pbuf,uint8_t length);
//rfд<66><D0B4><EFBFBD><EFBFBD><E5BAAF>
//input:uint8_t reg<65><67>д<EFBFBD><D0B4><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ;uint8_t *pbuf<75><66>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׵<EFBFBD>ַ;uint8_t length<74><68>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD>ݵij<DDB5><C4B3><EFBFBD>
//output:<3A><>
/******************************************************************************/
void xn297l_write_buf(uint8_t reg,uint8_t *pbuf,uint8_t length)
{
uint8_t i;
rfspi_cs(0); //ʹ<><CAB9>Ƭѡ
rfspi_rwc(reg);
for(i = 0;i < length; i++)
{
rfspi_rwc(pbuf[i]); //д<><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
rfspi_cs(1); //<2F><><EFBFBD><EFBFBD>Ƭѡ
}
/******************************************************************************/
//void rf_read_buf(uint8_t reg,uint8_t *pbuf,uint8_t length);
//rf<72><66><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E5BAAF>
//input:uint8_t reg<65><67><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD>ĵ<EFBFBD>ַ;uint8_t *pbuf<75><66><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׵<EFBFBD>ַ;uint8_t length<74><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵij<DDB5><C4B3><EFBFBD>
//output:<3A><>
/******************************************************************************/
void xn297l_read_buf(uint8_t reg, uint8_t *pbuf, uint8_t length)
{
uint8_t i;
rfspi_cs(0); //ʹ<><CAB9>Ƭѡ
rfspi_rwc(reg);
for(i = 0; i < length; i++)
{
pbuf[i] = rfspi_rwc(0x00); //<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
}
rfspi_cs(1); //<2F><><EFBFBD><EFBFBD>Ƭѡ
}
/******************************************************************************/
//void rf_tx_mode(void);
//XN297L<37><4C><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>ģʽ
//input:<3A><>
//output:<3A><>
/******************************************************************************/
void xn297l_tx_mode(void)
{
// uint8_t rf_cal_data[] = {0xF6,0x37,0x5D};
// rf_write_buf(W_REGISTER + RF_CAL,rf_cal_data, sizeof(rf_cal_data));//<2F><><EFBFBD><EFBFBD>RF_CAL
xn297l_write_reg(W_REGISTER + CONFIG,0x8E); // rf<72><66><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>ģʽ
clear_CE();
delay_us(20);
}
/******************************************************************************/
//void rf_rx_mode(void);
//XN297L<37><4C><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>ģʽ
//input:<3A><>
//output:<3A><>
/******************************************************************************/
void xn297l_rx_mode(void)
{
// uint8_t rf_cal_data[] = {0x06,0x37,0x5D};
// rf_write_buf(W_REGISTER + RF_CAL,rf_cal_data, sizeof(rf_cal_data));//<2F><><EFBFBD><EFBFBD>RF_CAL
xn297l_write_reg(W_REGISTER + CONFIG,0X8F); // rf<72><66><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>ģʽ
set_CE();
//delay_ms(1);
}
/******************************************************************************/
//void rf_get_status(void);
//XN297L<37><4C>ȡ״̬<D7B4><CCAC>Ϣ
//input:<3A><>
//output:״̬<D7B4>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD>ֵ
/******************************************************************************/
uint8_t xn297l_get_status(void)
{
return xn297l_read_reg(RF_STATUS)&0x70; //<2F><>ȡ״ֵ̬
}
/******************************************************************************/
//void rf_clear_status(void);
//XN297L<37><4C><EFBFBD><EFBFBD>״̬<D7B4><CCAC>Ϣ
//input:<3A><>
//output:<3A><>
/******************************************************************************/
void xn297l_clear_status(void)
{
xn297l_write_reg(W_REGISTER + RF_STATUS,0x70);
}
/******************************************************************************/
//void rf_clear_FIFO(void);
//XN297L<37><4C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//input:<3A><>
//output:<3A><>
/******************************************************************************/
void xn297l_clear_fifo(void)
{
xn297l_write_reg(FLUSH_TX, 0); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD>
xn297l_write_reg(FLUSH_RX, 0); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB><EFBFBD>
}
/******************************************************************************/
//void rf_set_channel(uint8_t channel);
//XN297L<37><4C><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD>ŵ<EFBFBD>
//input:uint8_t channel<65><6C><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD>ŵ<EFBFBD>
//output:<3A><>
/******************************************************************************/
void xn297l_set_channel(uint8_t channel)
{
// current_channel = channel;
xn297l_write_reg(W_REGISTER + RF_CH, channel);
}
/******************************************************************************/
//void rf_set_addr(uint8_t reg,uint8_t *rf_addr_array);
//XN297L<37><4C><EFBFBD><EFBFBD>ͨ<EFBFBD>ŵ<EFBFBD>ַ
//input:uint8_t reg,<2C><>Ҫ<EFBFBD><D2AA><EFBFBD>õļĴ<C4BC><C4B4><EFBFBD><EFBFBD><EFBFBD>uint8_t *rf_addr_array<61><79><EFBFBD><EFBFBD><EFBFBD>õĵ<C3B5>ַ
//output:<3A><>
/******************************************************************************/
void xn297l_set_addr(uint8_t reg,uint8_t *rf_addr_array,uint8_t addr_len)
{
xn297l_write_buf(W_REGISTER + reg,rf_addr_array, addr_len);//<2F><><EFBFBD>õ<EFBFBD>ַ
}
/******************************************************************************/
//uint8_t rf_tx_data(uint8_t *data, uint8_t length);
//XN297L<37><4C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//input:uint8_t *data<74><61><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׵<EFBFBD>ַ<EFBFBD><D6B7>uint8_t length<74><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵij<DDB5><C4B3><EFBFBD>
//output:<3A><><EFBFBD><EFBFBD>״ֵ̬
/******************************************************************************/
void xn297l_tx_data(uint8_t *data, uint8_t length)
{
set_CE();
delay_us(100);
xn297l_write_buf(W_TX_PAYLOAD,data,length); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
while(IRQ_STATUS);
xn297l_clear_status(); //<2F><><EFBFBD><EFBFBD>״̬
xn297l_write_reg(FLUSH_TX,0); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD>
clear_CE();
}
/******************************************************************************/
//uint8_t rf_rx_data(uint8_t *data,uint8_t length);
//rf<72><66><EFBFBD>պ<EFBFBD><D5BA><EFBFBD>
//input:uint8_t *data,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׵<EFBFBD>ַ<EFBFBD><D6B7>uint8_t length:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵij<DDB5><C4B3><EFBFBD>
//output:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־
/******************************************************************************/
uint8_t xn297l_rx_data(uint8_t *data,uint8_t length)
{
if(IRQ_STATUS)
{
return 0; //<2F>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>
}
xn297l_read_buf(R_RX_PAYLOAD,data,length); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
xn297l_write_reg(FLUSH_RX,0); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB><EFBFBD>
xn297l_clear_status(); //<2F><><EFBFBD><EFBFBD>״̬
return 1;
}
/******************************************************************************/
//void rf_init(void);
//rf<72><66>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4>һЩ<D2BB><D0A9>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//input:<3A><>
//output:<3A><>
/******************************************************************************/
void rf_init(void)
{
uint8_t addr[] = {0x23,0x23,0x23};
rfspi_init();
xn297l_init(addr,3,20,13,XN297L_RF_POWER_P_5|XN297L_RF_DATA_RATE_1M);
xn297l_rx_mode();
}
void xn297l_init(uint8_t* addr,uint8_t addr_len,uint8_t chn,uint8_t payload,uint8_t rate_power)
{
#if ((DATA_RATE==DR_1M) || (DATA_RATE==DR_2M))
uint8_t BB_cal_data[] = {0x12,0xED,0x67,0x9C,0x46};
uint8_t rf_cal_data[] = {0xF6,0x3F,0x5D};
uint8_t rf_cal2_data[] = {0x45,0x21,0xEF,0x2C,0x5A,0x40};
uint8_t Dem_cal_data[] = {0x01};
uint8_t Dem_cal2_data[] = {0x0B,0xDF,0x02};
#elif (DATA_RATE==DR_250K)
uint8_t BB_cal_data[] = {0x12,0xEC,0x6F,0xA1,0x46};
uint8_t rf_cal_data[] = {0xF6,0x3F,0x5D};
uint8_t rf_cal2_data[] = {0xD5,0x21,0xEB,0x2C,0x5A,0x40};
uint8_t Dem_cal_data[] = {0x1F};
uint8_t Dem_cal2_data[] = {0x0B,0xDF,0x02};
#endif
xn297l_write_reg(RST_FSPI,0x5A); //оƬ<D0BE><C6AC><EFBFBD><EFBFBD>λ
xn297l_write_reg(RST_FSPI,0XA5);
delay_ms(1);
xn297l_clear_fifo();//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
xn297l_clear_status(); //<2F><><EFBFBD><EFBFBD>״̬<D7B4>Ĵ<EFBFBD><C4B4><EFBFBD>
if(payload < 33)
{
#if(CE_TYPE==CE_HARDWARE_MODE)
xn297l_write_reg(W_REGISTER +FEATURE, 0x00); //32<33>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD>ݣ<EFBFBD>ʹ<EFBFBD><CAB9>Ӳ<EFBFBD><D3B2>CE
#else
xn297l_write_reg(W_REGISTER +FEATURE, 0x20); //32<33>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD>ݣ<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>CE
#endif
}
else
{
#if(CE_TYPE==CE_HARDWARE_MODE)
xn297l_write_reg(W_REGISTER +FEATURE, 0x18); //64<36>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD>ݣ<EFBFBD>ʹ<EFBFBD><CAB9>Ӳ<EFBFBD><D3B2>CE
#else
xn297l_write_reg(W_REGISTER +FEATURE, 0x38); //64<36>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD>ݣ<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>CE
#endif
}
clear_CE();
xn297l_write_reg(W_REGISTER + EN_RXADDR,0x03); //ʹ<>ܽ<EFBFBD><DCBD><EFBFBD>ͨ<EFBFBD><CDA8><30><CDA8>1
xn297l_write_reg(W_REGISTER + SETUP_AW,addr_len-2); //[1-3] = [3-5]
xn297l_write_reg(W_REGISTER + RF_CH,chn); //<2F><><EFBFBD>ý<EFBFBD><C3BD><EFBFBD><EFBFBD>ŵ<EFBFBD>
xn297l_write_reg(W_REGISTER + RX_PW_P0,payload); //<2F><><EFBFBD>ý<EFBFBD><C3BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF>ȣ<EFBFBD><C8A3><EFBFBD>Payload<61><64><EFBFBD><EFBFBD>
xn297l_write_reg(W_REGISTER + RX_PW_P1,payload); //<2F><><EFBFBD>ý<EFBFBD><C3BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF>ȣ<EFBFBD><C8A3><EFBFBD>Payload<61><64><EFBFBD><EFBFBD>
xn297l_write_buf(W_REGISTER + TX_ADDR,addr, addr_len); //<2F><><EFBFBD>÷<EFBFBD><C3B7>͵<EFBFBD>ַ
xn297l_write_buf(W_REGISTER + RX_ADDR_P0,addr, addr_len); //<2F><><EFBFBD>ý<EFBFBD><C3BD>յ<EFBFBD>ַ
xn297l_write_buf(W_REGISTER + BB_CAL,BB_cal_data, sizeof(BB_cal_data)); //<2F><><EFBFBD><EFBFBD>BB_CAL
xn297l_write_buf(W_REGISTER + RF_CAL2,rf_cal2_data, sizeof(rf_cal2_data)); //<2F><><EFBFBD><EFBFBD>RF_CAL2
xn297l_write_buf(W_REGISTER + DEM_CAL,Dem_cal_data, sizeof(Dem_cal_data)); //<2F><><EFBFBD><EFBFBD>DEM_CAL
xn297l_write_buf(W_REGISTER + RF_CAL,rf_cal_data, sizeof(rf_cal_data)); //<2F><><EFBFBD><EFBFBD>RF_CAL
xn297l_write_buf(W_REGISTER + DEM_CAL2,Dem_cal2_data,sizeof(Dem_cal2_data));//<2F><><EFBFBD><EFBFBD>DEM_CAL2
xn297l_write_reg(W_REGISTER + DYNPD, 0x00); //<2F><>ֹ<EFBFBD><D6B9>̬Payload
xn297l_write_reg(W_REGISTER + RF_SETUP,rate_power); //<2F><><EFBFBD>÷<EFBFBD><C3B7><EFBFBD>ʺ<EFBFBD><CABA><EFBFBD><EFBFBD><EFBFBD>
#if(TRANSMIT_TYPE == TRANS_ENHANCE_MODE) //<2F><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ǿģʽ
xn297l_write_reg(W_REGISTER + SETUP_RETR,0x03); //ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ط<EFBFBD>
xn297l_write_reg(W_REGISTER + EN_AA, 0x01); //ʹ<><CAB9><EFBFBD>Զ<EFBFBD>Ӧ<EFBFBD><D3A6>
#elif(TRANSMIT_TYPE == TRANS_NORMAL_MODE) //<2F><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ͨģʽ
xn297l_write_reg(W_REGISTER + SETUP_RETR,0x00); //<2F><>ֹ<EFBFBD>ط<EFBFBD>
xn297l_write_reg(W_REGISTER + EN_AA, 0x00); //<2F><>ֹ<EFBFBD>Զ<EFBFBD>Ӧ<EFBFBD><D3A6>
#endif
}
/******************************************************************************/
//<2F><><EFBFBD>ز<EFBFBD>ģʽ
/******************************************************************************/
void RF_Carrier( uint8_t ucChannel_Set)
{
uint8_t BB_cal_data[] = {0x0A,0x6D,0x67,0x9C,0x46};
uint8_t RF_cal_data[] = {0xF6,0x37,0x5D};
uint8_t RF_cal2_data[] = {0x45,0x21,0xEF,0xAC,0x5A,0x50};
uint8_t Dem_cal_data[] = {0xE1};
uint8_t Dem_cal2_data[] = {0x0B,0xDF,0x02};
xn297l_write_reg(W_REGISTER + RF_CH,ucChannel_Set);
xn297l_write_reg(W_REGISTER + RF_SETUP, 0x3F); //13dbm
xn297l_write_buf(W_REGISTER + BB_CAL, BB_cal_data, sizeof(BB_cal_data));
xn297l_write_buf(W_REGISTER + RF_CAL2, RF_cal2_data, sizeof(RF_cal2_data));
xn297l_write_buf(W_REGISTER + DEM_CAL, Dem_cal_data, sizeof(Dem_cal_data));
xn297l_write_buf(W_REGISTER + RF_CAL, RF_cal_data, sizeof(RF_cal_data));
xn297l_write_buf(W_REGISTER + DEM_CAL2, Dem_cal2_data,sizeof(Dem_cal2_data));
}