HardwareDriver/nuvoton/m451/roboticarm_controller/24l01.c

268 lines
5.5 KiB
C
Raw Blame History

#include "M451Series.h"
#include "24l01.h"
#include "spi_hal.h"
uchar Recv_Buf[32] = {0};
uchar Send_Buf[32] = {0};
char rfch = 45;
unsigned short RxCnt = 0;
unsigned char TxAddr[]={0x11,0x22,0x33,0x44,0x55};//????
unsigned char RxAddr[]={0x11,0x22,0x33,0x44,0x55};//????
enum NRF_Mode Curr_Mode = NRF_RX_Mode;
void delay_s()
{
int z=0;
for(z=0;z<250;z++)
{}
}
void delay_mss()
{
int z = 0;
for(z=0;z<5;z++)
{
delay_s();
}
}
void NRF_Init(void)
{
}
void NRF_SetUpInterrupt(void)
{
GPIO_SetMode(PB,BIT15,GPIO_MODE_INPUT);
GPIO_EnableInt(PB,15,GPIO_INT_FALLING);
NVIC->ISER[0]|=(0X01<<2);
}
int gRecvPkg = 0;
//接收或者发送中断
void EINT0_IRQHandler()
{
char trycnt = 5;
gRecvPkg ++;
delay_s();
switch(Curr_Mode){
//if now in rx mode mean data been receieved
case NRF_RX_Mode:
//PB9 = 0;
while(trycnt > 0)
{
if(NRF24L01_RxPacket(Recv_Buf) == 0)
{
NRFSetTxMode();
break;
}
trycnt --;
}
//nrf_write(FLUSH_RX,0xff);//清除RX FIFO寄存器
//PB9 = 1;
//read the buffer
break;
//if now in tx mode mean data been sent
case NRF_TX_Mode:
break;
}
PB->INTSRC |= BIT14;
}
void RX_Mode(void)
{
char sta;
sta = nrf_read(STATUS);
nrf_write(W_REGISTER + STATUS,sta);
nrf_write(FLUSH_TX,0xff);
nrf_write(FLUSH_RX,0xff);
PB13=0;
Curr_Mode = NRF_RX_Mode;
delay_s();
nrf_writebuf(W_REGISTER + TX_ADDR,(uchar*)TxAddr,5);//写TX节点地址
nrf_writebuf(W_REGISTER + RX_ADDR_P0,(uchar*)RxAddr,5); //设置TX节点地址,主要为了使能ACK
nrf_write(W_REGISTER + SETUP_RETR,0x11);//设置自动重发间隔时间:500us + 86us;最大自动重发次数:10次
nrf_write(W_REGISTER + EN_AA, 0x01); // Enable Auto.Ack:Pipe0
nrf_write(W_REGISTER + EN_RXADDR, 0x01); // Enable Pipe0
nrf_write(W_REGISTER + RF_CH, rfch); // Select RF channel 40
nrf_write(W_REGISTER + RX_PW_P0,RX_DATA_WITDH);
//nrf_write(W_REGISTER+FEATURE, 0x20);
nrf_write(W_REGISTER + RF_SETUP, 0x0f);
nrf_write(W_REGISTER + CONFIG, 0x2f); // Set PWR_UP bit, enable CRC(2 bytes)
delay_s();
PB13=1;
}
void NRFSwitchMode(char mode)
{
static char ifinit = 0;
//TX Mode
if(mode == 1)
{
if (0 == ifinit)
{
NRFSetTxMode();
ifinit = 1;
return;
}
PB9 = 0;
nrf_write(W_REGISTER + STATUS,0xff); //使能通道0的自动应答
nrf_write(FLUSH_TX,0); //使能通道0的自动应答
nrf_write(W_REGISTER + CONFIG,0xfe); //使能通道0的自动应答
delay_s();
PB9 = 1;
}
else
{
PB9 = 0;
nrf_write(W_REGISTER+STATUS,0xff); //使能通道0的自动应答
nrf_write(FLUSH_TX,0); //使能通道0的自动应答
nrf_write(W_REGISTER + CONFIG,0xff); //使能通道0的自动应答
delay_s();
PB9 = 1;
}
}
void NRFSetTxMode()
{
char sta;
sta = nrf_read(STATUS);
nrf_write(W_REGISTER + STATUS,sta);
nrf_write(FLUSH_TX,0xff);
nrf_write(FLUSH_RX,0xff);
PB14 = 0;
Curr_Mode = NRF_TX_Mode;
delay_s();
nrf_writebuf(W_REGISTER+TX_ADDR,(uchar*)TxAddr,5);//写TX节点地址
nrf_writebuf(W_REGISTER+RX_ADDR_P0,(uchar*)RxAddr,5); //设置TX节点地址,主要为了使能ACK
nrf_write(W_REGISTER+EN_AA,0x01); //使能通道0的自动应答
nrf_write(W_REGISTER+EN_RXADDR,0x01); //使能通道0的接收地址
nrf_write(W_REGISTER+SETUP_RETR,0x1a);//设置自动重发间隔时间:500us + 86us;最大自动重发次数:10次
nrf_write(W_REGISTER+RF_CH,rfch); //设置RF通道为40
nrf_write(W_REGISTER+RF_SETUP,0x0f); //设置TX发射参数,0db增益,2Mbps,低噪声增益开启
nrf_write(W_REGISTER+CONFIG,0x0e); //配置基本工作模式的参数;PWR_UP,EN_CRC,16BIT_CRC,接收模式,开启所有中断
delay_s();
PB14 = 1;
}
//启动NRF24L01发送一次数据
//txbuf:待发送数据首地址
//返回值:发送完成状况
unsigned char NRF24L01_TxPacket(unsigned char *txbuf)
{
unsigned char sta;
PB9=0;
delay_s();
nrf_writebuf(W_TX_PAYLOAD,txbuf,TX_DATA_WITDH);//写数据到TX BUF 32个字节
PB9=1;
sta=nrf_read(STATUS); //读取状态寄存器的值
nrf_write(W_REGISTER+STATUS,sta); //清除TX_DS或MAX_RT中断标志
if(sta&0x10)//达到最大重发次数
{
nrf_wf(FLUSH_TX);//清除TX FIFO寄存器
nrf_write(W_REGISTER+STATUS,sta); //清除TX_DS或MAX_RT中断标志
sta=nrf_read(STATUS); //读取状态寄存器的值
return 0x10;
}
if(sta&0x20)//发送完成
{
return 0;
}
return 0xff;//其他原因发送失败
}
unsigned char NrfDump()
{
char ret = 0;
ret=nrf_read(EN_AA);
delay_s();
ret=nrf_read(EN_RXADDR);
delay_s();
ret=nrf_read(RF_CH);
delay_s();
ret=nrf_read(RX_PW_P0);
delay_s();
ret=nrf_read(RF_SETUP);
delay_s();
ret=nrf_read(CONFIG);
delay_s();
ret=nrf_read(CD);
delay_s();
ret=nrf_read(STATUS);
delay_s();
return ret;
}
unsigned char NRF24L01_Read_Buf(unsigned char reg,unsigned char *pBuf,unsigned char len)
{
unsigned char status,u8_ctr;
spi_enable();
status=spi_send(reg);
for(u8_ctr=0;u8_ctr<len;u8_ctr++)
pBuf[u8_ctr]= spi_read();
spi_disable();
return status;
}
//载波监听
unsigned char NRF24L01_CD_Detect()
{
short ret = 0;
int x = 255;
int i = 0;
unsigned char sum = 0;
for(i=0;i<x;i++){
ret=nrf_read(CD);
sum+=ret;
}
return sum;
}
void NRF24L01_Monitor()
{
char ret = nrf_read(STATUS);
ret = nrf_read(CONFIG);
ret = nrf_read(RF_SETUP);
if(ret & 0x10)
{
nrf_write(W_REGISTER+STATUS,ret); //清除TX_DS或MAX_RT中断标志
}
}
short NRF24L01_RxPacket(unsigned char *rxbuf)
{
unsigned char sta;
sta = nrf_read(STATUS); //读取状态寄存器的<E599A8>
if(sta&0x40)//接收到数据
{
NRF24L01_Read_Buf(0x61,rxbuf,RX_DATA_WITDH);//读取数据
RxCnt ++;
//nrf_write(W_REGISTER+STATUS,sta); //清除TX_DS或MAX_RT中断标志
return 0;
}
if(sta & 0x20) //如果该中断已经有数据发送了
{
nrf_write(W_REGISTER+STATUS,sta); //清除TX_DS或MAX_RT中断标志
return -1;
}
return -1;//没收到任何数据
}