更新下驱动 在底层创建一个线程接收数据,大家给点意见,
#include
#include "nrf24l01.h"
#define NRF_DEBUG
#ifdef NRF_DEBUG
#define NRF_TRACE rt_kprintf
#else
#define NRF_TRACE(...)
#endif /* #ifdef FLASH_DEBUG */
static struct spi_nrf_device nrf24l01_device;
/* write nrf buf */
static rt_uint8_t NRF24L01_Write_Buf(rt_uint8_t reg, const void *buffer, rt_size_t size)
{
return rt_spi_send_then_send(nrf24l01_device.rt_spi_device,®,1,buffer,size);
}
/* read nrf buf */
static rt_uint8_t NRF24L01_Read_Buf(rt_uint8_t reg,void *buffer,rt_size_t size)
{
return rt_spi_send_then_recv(nrf24l01_device.rt_spi_device,®,1,buffer,size);
}
/* cheeek nef24l01 */
/* 0: ok 1:error*/
rt_uint8_t NRF24L01_Check(void)
{
rt_uint8_t buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5};
rt_uint8_t i;
NRF24L01_Write_Buf(NRF_WRITE_REG+TX_ADDR,buf,5);
NRF24L01_Read_Buf(TX_ADDR,buf,5);
for(i=0;i<5;i++)
if(buf
!=0XA5)break;
if(i!=5)return 1;
return 0;
}
void NRF24L01_Write_Reg(rt_uint8_t reg,rt_uint8_t date)
{
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,®,1,&date,1);
}
rt_uint8_t NRF24L01_Read_Reg(rt_uint8_t reg)
{
rt_uint8_t date;
rt_spi_send_then_recv(nrf24l01_device.rt_spi_device,®,1,&date,1);
return date;
}
static rt_err_t spi_nrf_transfer(struct spi_nrf_device *dev)
{
rt_uint8_t sta;
if(nrf24l01_device.tx_packet.data_len>0)
{
rt_uint8_t size=nrf24l01_device.tx_packet.data_len;
NRF_TRACE("send data
");
SetCEN(0);
NRF24L01_Write_Buf(WR_TX_PLOAD,nrf24l01_device.tx_packet.buffer,size);//????????
nrf24l01_device.tx_packet.data_len=0;
SetCEN(1);
return RT_EOK;
}
sta=NRF24L01_Read_Reg(STATUS); //????×????????÷????
NRF_TRACE("status=%2x
",sta);
NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,sta); //????TX_DS?òMAX_RT????±ê??
if(sta&MAX_TX)//????×??ó??·?????
{
NRF_TRACE("send max data
");
NRF24L01_Write_Reg(FLUSH_TX,0xff);//????TX FIFO?????÷
}
else if(sta&TX_OK)//·????ê??
{
NRF_TRACE("send ok
");
nrf24l01_device.rx_packet.data_len=0;
NRF24L01_Write_Reg(FLUSH_TX,0xff);//????TX FIFO?????÷
}
else if(sta&RX_OK)//??????????
{
NRF_TRACE("recv ok
");
NRF24L01_Read_Buf(RD_RX_PLOAD,nrf24l01_device.rx_packet.buffer,20);//????????
nrf24l01_device.rx_packet.data_len=20;
NRF24L01_Write_Reg(FLUSH_RX,0xff);//????RX FIFO?????÷
}
return RT_EOK;
}
/* RT-Thread device interface */
static rt_err_t nrf24lxx_init(rt_device_t dev)
{
return RT_EOK;
}
static rt_err_t nrf24lxx_open(rt_device_t dev, rt_uint16_t oflag)
{
return RT_EOK;
}
static rt_err_t nrf24lxx_close(rt_device_t dev)
{
return RT_EOK;
}
static rt_err_t nrf24lxx_control(rt_device_t dev, rt_uint8_t cmd, void *args)
{
uint8_t send_buffer[4];
RT_ASSERT(dev != RT_NULL);
switch(cmd)
{
/* modue tx */
case SET_MODE_TX:
NRF_TRACE("set mode tx
");
NRF24L01_Write_Reg(NRF_WRITE_REG+CONFIG,0x0e);
break;
/* modue rx */
case SET_MODE_RX:
NRF24L01_Write_Reg(NRF_WRITE_REG+CONFIG,0x0f);
SetCEN(1);
break;
/* EN CH */
case SET_EN_CH:
send_buffer[0]=NRF_WRITE_REG+EN_RXADDR;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* rf ch */
case SET_RF_CH:
send_buffer[0]=NRF_WRITE_REG+RF_CH;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
case SET_RF_RETR:
send_buffer[0]=NRF_WRITE_REG+SETUP_RETR;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set tx addr */
case SET_TX_ADDR:
send_buffer[0]=NRF_WRITE_REG+TX_ADDR;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,5);
send_buffer[0]=NRF_WRITE_REG+RX_ADDR_P0;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,5);
break;
/* set tx ack */
case SET_EN_ACK:
send_buffer[0]=NRF_WRITE_REG+EN_AA;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set SET_RF_SETUP */
case SET_RF_SETUP:
send_buffer[0]=NRF_WRITE_REG+RF_SETUP;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set rx add0 */
case SET_RX_ADDR0:
send_buffer[0]=NRF_WRITE_REG+RX_ADDR_P0;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,5);
break;
/* set rx add1 */
case SET_RX_ADDR1:
send_buffer[0]=NRF_WRITE_REG+RX_ADDR_P1;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,5);
break;
/* set rx add2 */
case SET_RX_ADDR2:
send_buffer[0]=NRF_WRITE_REG+RX_ADDR_P2;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set rx add3 */
case SET_RX_ADDR3:
send_buffer[0]=NRF_WRITE_REG+RX_ADDR_P3;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set rx add4 */
case SET_RX_ADDR4:
send_buffer[0]=NRF_WRITE_REG+RX_ADDR_P4;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set rx add5 */
case SET_RX_ADDR5:
send_buffer[0]=NRF_WRITE_REG+RX_ADDR_P5;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set SET_RX_SIZE0 */
case SET_RX_SIZE0:
send_buffer[0]=NRF_WRITE_REG+RX_PW_P0;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set SET_RX_SIZE0 */
case SET_RX_SIZE1:
send_buffer[0]=NRF_WRITE_REG+RX_PW_P1;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set SET_RX_SIZE0 */
case SET_RX_SIZE2:
send_buffer[0]=NRF_WRITE_REG+RX_PW_P2;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set SET_RX_SIZE0 */
case SET_RX_SIZE3:
send_buffer[0]=NRF_WRITE_REG+RX_PW_P3;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set SET_RX_SIZE0 */
case SET_RX_SIZE4:
send_buffer[0]=NRF_WRITE_REG+RX_PW_P4;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set SET_RX_SIZE0 */
case SET_RX_SIZE5:
send_buffer[0]=NRF_WRITE_REG+RX_PW_P5;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
}
return RT_EOK;
}
static rt_size_t nrf24lxx_read(rt_device_t dev,
rt_off_t pos,
void* buffer,
rt_size_t size)
{
if (nrf24l01_device.rx_packet.data_len>0)
{
size=nrf24l01_device.rx_packet.data_len;
rt_memcpy(buffer,nrf24l01_device.rx_packet.buffer,size);
nrf24l01_device.rx_packet.data_len=0;
return size;
}
else
return 0;
}
static rt_size_t nrf24lxx_write(rt_device_t dev,
rt_off_t pos,
const void* buffer,
rt_size_t size)
{
NRF_TRACE("write nrf size %d
",size);
if (nrf24l01_device.tx_packet.data_len==0)
{
nrf24l01_device.tx_packet.data_len =size;
rt_memcpy(nrf24l01_device.tx_packet.buffer,buffer,size);
rt_event_send(&nrf24l01_device.data_event, 1);
return size;
}
else
return 0;
}
/********************************* RT-Thread rt_device interface end **************************************/
static void nrf_data_thread_entry(void *parameter)
{
rt_uint32_t e;
rt_err_t result;
NRF_TRACE("spi thread is ok
");
while (1)
{
/* receive first event */
if (rt_event_recv(&nrf24l01_device.data_event,
1,
RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
RT_WAITING_FOREVER,
&e) != RT_EOK)
{
continue;
}
spi_nrf_transfer(&nrf24l01_device);
}
}
rt_err_t nrf24l01_init(const char * nrf_device_name, const char * spi_device_name)
{
rt_memset(&nrf24l01_device, 0, sizeof(struct spi_nrf_device));
nrf24l01_device.rt_spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name);
if(nrf24l01_device.rt_spi_device == RT_NULL)
{
NRF_TRACE("spi device %s not found!
", spi_device_name);
return -RT_ENOSYS;
}
/* config spi */
{
struct rt_spi_configuration cfg;
cfg.data_width = 8;
cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible: Mode 0 and Mode 3 */
cfg.max_hz = 8 * 1000 * 1000; /* 8M */
rt_spi_configure(nrf24l01_device.rt_spi_device, &cfg);
}
rt_event_init(&nrf24l01_device.data_event, "nrf", RT_IPC_FLAG_FIFO);
/* nrf24lxx_init */
{
rt_uint8_t sendbuffer[5];
if(NRF24L01_Check())
{
rt_kprintf("nrf device not found!
");
return -RT_ENOSYS;
}
/* set nrf tx addr */
nrf24lxx_control(&nrf24l01_device.device,SET_RX_ADDR0,"FYYYY");
sendbuffer[0]=20;
nrf24lxx_control(&nrf24l01_device.device,SET_RX_SIZE0,sendbuffer);
/* set nrf en ack */
sendbuffer[0]=0x00;
nrf24lxx_control(&nrf24l01_device.device,SET_EN_ACK,sendbuffer);
/* set nrf en ch */
sendbuffer[0]=0x01;
nrf24lxx_control(&nrf24l01_device.device,SET_EN_CH,sendbuffer);
/* reset send time :500us + 86us; send max count 10 */
sendbuffer[0]=0x1a;
nrf24lxx_control(&nrf24l01_device.device,SET_RF_RETR,sendbuffer);
/* set nrf rf ch */
sendbuffer[0]=40;
nrf24lxx_control(&nrf24l01_device.device,SET_RF_CH,sendbuffer);
sendbuffer[0]=0x0f;
nrf24lxx_control(&nrf24l01_device.device,SET_RF_SETUP,sendbuffer);
}
/* register device */
nrf24l01_device.device.type = RT_Device_Class_Char;
nrf24l01_device.device.init = nrf24lxx_init;
nrf24l01_device.device.open = nrf24lxx_open;
nrf24l01_device.device.close = nrf24lxx_close;
nrf24l01_device.device.read = nrf24lxx_read;
nrf24l01_device.device.write = nrf24lxx_write;
nrf24l01_device.device.control = nrf24lxx_control;
/* no private */
nrf24l01_device.device.user_data = RT_NULL;
/* nrf thread init */
{
rt_thread_t tid;
tid = rt_thread_create("nrf",
nrf_data_thread_entry,
RT_NULL,
256,
RT_THREAD_PRIORITY_MAX - 2,
20);
if (tid != RT_NULL)
rt_thread_startup(tid);
}
spi_nrf_hw_init();
rt_device_register(&nrf24l01_device.device, nrf_device_name,
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
return RT_EOK;
}
void spi_nrf_isr(int vector)
{
/* enter interrupt */
rt_interrupt_enter();
NRF_TRACE("spi_nrf_isr
");
rt_event_send(&nrf24l01_device.data_event, 1);
/* leave interrupt */
rt_interrupt_leave();
}
#ifdef RT_USING_FINSH
#include
void nrf_info(void)
{
rt_uint8_t sendbuffer[20];
NRF24L01_Read_Buf(TX_ADDR,sendbuffer,5);
rt_kprintf("tx_addr: 0x%2x-0x%2x-0x%2x-0x%2x-0x%2x
",sendbuffer[0],
sendbuffer[1],
sendbuffer[2],
sendbuffer[3],
sendbuffer[4]);
NRF24L01_Read_Buf(RX_ADDR_P0,sendbuffer,5);
rt_kprintf("rx_addr0: 0x%2x-0x%2x-0x%2x-0x%2x-0x%2x
",sendbuffer[0],
sendbuffer[1],
sendbuffer[2],
sendbuffer[3],
sendbuffer[4]);
rt_kprintf("RF_CH = 0x%2d
",NRF24L01_Read_Reg(RF_CH));
rt_kprintf("EN_AA = 0x%2x
",NRF24L01_Read_Reg(EN_AA));
rt_kprintf("EN_RXADDR = 0x%2x
",NRF24L01_Read_Reg(EN_RXADDR));
rt_kprintf("RF_SETUP = 0x%2x
",NRF24L01_Read_Reg(RF_SETUP));
rt_kprintf("SETUP_RETR = 0x%2x
",NRF24L01_Read_Reg(SETUP_RETR));
rt_kprintf("CONFIG = 0x%2x
",NRF24L01_Read_Reg(CONFIG));
}
FINSH_FUNCTION_EXPORT(nrf_info, printf nrf_info.)
#endif
struct nrf_data_packet
{
rt_uint8_t data_len;
rt_uint8_t buffer[NRF_SIZE];
};
struct spi_nrf_device
{
struct rt_device device;
struct rt_spi_device * rt_spi_device;
struct rt_event data_event;
struct nrf_data_packet tx_packet;
struct nrf_data_packet rx_packet;
};
更新下驱动 在底层创建一个线程接收数据,大家给点意见,
#include
#include "nrf24l01.h"
#define NRF_DEBUG
#ifdef NRF_DEBUG
#define NRF_TRACE rt_kprintf
#else
#define NRF_TRACE(...)
#endif /* #ifdef FLASH_DEBUG */
static struct spi_nrf_device nrf24l01_device;
/* write nrf buf */
static rt_uint8_t NRF24L01_Write_Buf(rt_uint8_t reg, const void *buffer, rt_size_t size)
{
return rt_spi_send_then_send(nrf24l01_device.rt_spi_device,®,1,buffer,size);
}
/* read nrf buf */
static rt_uint8_t NRF24L01_Read_Buf(rt_uint8_t reg,void *buffer,rt_size_t size)
{
return rt_spi_send_then_recv(nrf24l01_device.rt_spi_device,®,1,buffer,size);
}
/* cheeek nef24l01 */
/* 0: ok 1:error*/
rt_uint8_t NRF24L01_Check(void)
{
rt_uint8_t buf[5]={0XA5,0XA5,0XA5,0XA5,0XA5};
rt_uint8_t i;
NRF24L01_Write_Buf(NRF_WRITE_REG+TX_ADDR,buf,5);
NRF24L01_Read_Buf(TX_ADDR,buf,5);
for(i=0;i<5;i++)
if(buf
!=0XA5)break;
if(i!=5)return 1;
return 0;
}
void NRF24L01_Write_Reg(rt_uint8_t reg,rt_uint8_t date)
{
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,®,1,&date,1);
}
rt_uint8_t NRF24L01_Read_Reg(rt_uint8_t reg)
{
rt_uint8_t date;
rt_spi_send_then_recv(nrf24l01_device.rt_spi_device,®,1,&date,1);
return date;
}
static rt_err_t spi_nrf_transfer(struct spi_nrf_device *dev)
{
rt_uint8_t sta;
if(nrf24l01_device.tx_packet.data_len>0)
{
rt_uint8_t size=nrf24l01_device.tx_packet.data_len;
NRF_TRACE("send data
");
SetCEN(0);
NRF24L01_Write_Buf(WR_TX_PLOAD,nrf24l01_device.tx_packet.buffer,size);//????????
nrf24l01_device.tx_packet.data_len=0;
SetCEN(1);
return RT_EOK;
}
sta=NRF24L01_Read_Reg(STATUS); //????×????????÷????
NRF_TRACE("status=%2x
",sta);
NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,sta); //????TX_DS?òMAX_RT????±ê??
if(sta&MAX_TX)//????×??ó??·?????
{
NRF_TRACE("send max data
");
NRF24L01_Write_Reg(FLUSH_TX,0xff);//????TX FIFO?????÷
}
else if(sta&TX_OK)//·????ê??
{
NRF_TRACE("send ok
");
nrf24l01_device.rx_packet.data_len=0;
NRF24L01_Write_Reg(FLUSH_TX,0xff);//????TX FIFO?????÷
}
else if(sta&RX_OK)//??????????
{
NRF_TRACE("recv ok
");
NRF24L01_Read_Buf(RD_RX_PLOAD,nrf24l01_device.rx_packet.buffer,20);//????????
nrf24l01_device.rx_packet.data_len=20;
NRF24L01_Write_Reg(FLUSH_RX,0xff);//????RX FIFO?????÷
}
return RT_EOK;
}
/* RT-Thread device interface */
static rt_err_t nrf24lxx_init(rt_device_t dev)
{
return RT_EOK;
}
static rt_err_t nrf24lxx_open(rt_device_t dev, rt_uint16_t oflag)
{
return RT_EOK;
}
static rt_err_t nrf24lxx_close(rt_device_t dev)
{
return RT_EOK;
}
static rt_err_t nrf24lxx_control(rt_device_t dev, rt_uint8_t cmd, void *args)
{
uint8_t send_buffer[4];
RT_ASSERT(dev != RT_NULL);
switch(cmd)
{
/* modue tx */
case SET_MODE_TX:
NRF_TRACE("set mode tx
");
NRF24L01_Write_Reg(NRF_WRITE_REG+CONFIG,0x0e);
break;
/* modue rx */
case SET_MODE_RX:
NRF24L01_Write_Reg(NRF_WRITE_REG+CONFIG,0x0f);
SetCEN(1);
break;
/* EN CH */
case SET_EN_CH:
send_buffer[0]=NRF_WRITE_REG+EN_RXADDR;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* rf ch */
case SET_RF_CH:
send_buffer[0]=NRF_WRITE_REG+RF_CH;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
case SET_RF_RETR:
send_buffer[0]=NRF_WRITE_REG+SETUP_RETR;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set tx addr */
case SET_TX_ADDR:
send_buffer[0]=NRF_WRITE_REG+TX_ADDR;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,5);
send_buffer[0]=NRF_WRITE_REG+RX_ADDR_P0;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,5);
break;
/* set tx ack */
case SET_EN_ACK:
send_buffer[0]=NRF_WRITE_REG+EN_AA;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set SET_RF_SETUP */
case SET_RF_SETUP:
send_buffer[0]=NRF_WRITE_REG+RF_SETUP;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set rx add0 */
case SET_RX_ADDR0:
send_buffer[0]=NRF_WRITE_REG+RX_ADDR_P0;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,5);
break;
/* set rx add1 */
case SET_RX_ADDR1:
send_buffer[0]=NRF_WRITE_REG+RX_ADDR_P1;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,5);
break;
/* set rx add2 */
case SET_RX_ADDR2:
send_buffer[0]=NRF_WRITE_REG+RX_ADDR_P2;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set rx add3 */
case SET_RX_ADDR3:
send_buffer[0]=NRF_WRITE_REG+RX_ADDR_P3;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set rx add4 */
case SET_RX_ADDR4:
send_buffer[0]=NRF_WRITE_REG+RX_ADDR_P4;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set rx add5 */
case SET_RX_ADDR5:
send_buffer[0]=NRF_WRITE_REG+RX_ADDR_P5;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set SET_RX_SIZE0 */
case SET_RX_SIZE0:
send_buffer[0]=NRF_WRITE_REG+RX_PW_P0;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set SET_RX_SIZE0 */
case SET_RX_SIZE1:
send_buffer[0]=NRF_WRITE_REG+RX_PW_P1;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set SET_RX_SIZE0 */
case SET_RX_SIZE2:
send_buffer[0]=NRF_WRITE_REG+RX_PW_P2;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set SET_RX_SIZE0 */
case SET_RX_SIZE3:
send_buffer[0]=NRF_WRITE_REG+RX_PW_P3;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set SET_RX_SIZE0 */
case SET_RX_SIZE4:
send_buffer[0]=NRF_WRITE_REG+RX_PW_P4;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
/* set SET_RX_SIZE0 */
case SET_RX_SIZE5:
send_buffer[0]=NRF_WRITE_REG+RX_PW_P5;
rt_spi_send_then_send(nrf24l01_device.rt_spi_device,send_buffer,1,args,1);
break;
}
return RT_EOK;
}
static rt_size_t nrf24lxx_read(rt_device_t dev,
rt_off_t pos,
void* buffer,
rt_size_t size)
{
if (nrf24l01_device.rx_packet.data_len>0)
{
size=nrf24l01_device.rx_packet.data_len;
rt_memcpy(buffer,nrf24l01_device.rx_packet.buffer,size);
nrf24l01_device.rx_packet.data_len=0;
return size;
}
else
return 0;
}
static rt_size_t nrf24lxx_write(rt_device_t dev,
rt_off_t pos,
const void* buffer,
rt_size_t size)
{
NRF_TRACE("write nrf size %d
",size);
if (nrf24l01_device.tx_packet.data_len==0)
{
nrf24l01_device.tx_packet.data_len =size;
rt_memcpy(nrf24l01_device.tx_packet.buffer,buffer,size);
rt_event_send(&nrf24l01_device.data_event, 1);
return size;
}
else
return 0;
}
/********************************* RT-Thread rt_device interface end **************************************/
static void nrf_data_thread_entry(void *parameter)
{
rt_uint32_t e;
rt_err_t result;
NRF_TRACE("spi thread is ok
");
while (1)
{
/* receive first event */
if (rt_event_recv(&nrf24l01_device.data_event,
1,
RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
RT_WAITING_FOREVER,
&e) != RT_EOK)
{
continue;
}
spi_nrf_transfer(&nrf24l01_device);
}
}
rt_err_t nrf24l01_init(const char * nrf_device_name, const char * spi_device_name)
{
rt_memset(&nrf24l01_device, 0, sizeof(struct spi_nrf_device));
nrf24l01_device.rt_spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name);
if(nrf24l01_device.rt_spi_device == RT_NULL)
{
NRF_TRACE("spi device %s not found!
", spi_device_name);
return -RT_ENOSYS;
}
/* config spi */
{
struct rt_spi_configuration cfg;
cfg.data_width = 8;
cfg.mode = RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible: Mode 0 and Mode 3 */
cfg.max_hz = 8 * 1000 * 1000; /* 8M */
rt_spi_configure(nrf24l01_device.rt_spi_device, &cfg);
}
rt_event_init(&nrf24l01_device.data_event, "nrf", RT_IPC_FLAG_FIFO);
/* nrf24lxx_init */
{
rt_uint8_t sendbuffer[5];
if(NRF24L01_Check())
{
rt_kprintf("nrf device not found!
");
return -RT_ENOSYS;
}
/* set nrf tx addr */
nrf24lxx_control(&nrf24l01_device.device,SET_RX_ADDR0,"FYYYY");
sendbuffer[0]=20;
nrf24lxx_control(&nrf24l01_device.device,SET_RX_SIZE0,sendbuffer);
/* set nrf en ack */
sendbuffer[0]=0x00;
nrf24lxx_control(&nrf24l01_device.device,SET_EN_ACK,sendbuffer);
/* set nrf en ch */
sendbuffer[0]=0x01;
nrf24lxx_control(&nrf24l01_device.device,SET_EN_CH,sendbuffer);
/* reset send time :500us + 86us; send max count 10 */
sendbuffer[0]=0x1a;
nrf24lxx_control(&nrf24l01_device.device,SET_RF_RETR,sendbuffer);
/* set nrf rf ch */
sendbuffer[0]=40;
nrf24lxx_control(&nrf24l01_device.device,SET_RF_CH,sendbuffer);
sendbuffer[0]=0x0f;
nrf24lxx_control(&nrf24l01_device.device,SET_RF_SETUP,sendbuffer);
}
/* register device */
nrf24l01_device.device.type = RT_Device_Class_Char;
nrf24l01_device.device.init = nrf24lxx_init;
nrf24l01_device.device.open = nrf24lxx_open;
nrf24l01_device.device.close = nrf24lxx_close;
nrf24l01_device.device.read = nrf24lxx_read;
nrf24l01_device.device.write = nrf24lxx_write;
nrf24l01_device.device.control = nrf24lxx_control;
/* no private */
nrf24l01_device.device.user_data = RT_NULL;
/* nrf thread init */
{
rt_thread_t tid;
tid = rt_thread_create("nrf",
nrf_data_thread_entry,
RT_NULL,
256,
RT_THREAD_PRIORITY_MAX - 2,
20);
if (tid != RT_NULL)
rt_thread_startup(tid);
}
spi_nrf_hw_init();
rt_device_register(&nrf24l01_device.device, nrf_device_name,
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
return RT_EOK;
}
void spi_nrf_isr(int vector)
{
/* enter interrupt */
rt_interrupt_enter();
NRF_TRACE("spi_nrf_isr
");
rt_event_send(&nrf24l01_device.data_event, 1);
/* leave interrupt */
rt_interrupt_leave();
}
#ifdef RT_USING_FINSH
#include
void nrf_info(void)
{
rt_uint8_t sendbuffer[20];
NRF24L01_Read_Buf(TX_ADDR,sendbuffer,5);
rt_kprintf("tx_addr: 0x%2x-0x%2x-0x%2x-0x%2x-0x%2x
",sendbuffer[0],
sendbuffer[1],
sendbuffer[2],
sendbuffer[3],
sendbuffer[4]);
NRF24L01_Read_Buf(RX_ADDR_P0,sendbuffer,5);
rt_kprintf("rx_addr0: 0x%2x-0x%2x-0x%2x-0x%2x-0x%2x
",sendbuffer[0],
sendbuffer[1],
sendbuffer[2],
sendbuffer[3],
sendbuffer[4]);
rt_kprintf("RF_CH = 0x%2d
",NRF24L01_Read_Reg(RF_CH));
rt_kprintf("EN_AA = 0x%2x
",NRF24L01_Read_Reg(EN_AA));
rt_kprintf("EN_RXADDR = 0x%2x
",NRF24L01_Read_Reg(EN_RXADDR));
rt_kprintf("RF_SETUP = 0x%2x
",NRF24L01_Read_Reg(RF_SETUP));
rt_kprintf("SETUP_RETR = 0x%2x
",NRF24L01_Read_Reg(SETUP_RETR));
rt_kprintf("CONFIG = 0x%2x
",NRF24L01_Read_Reg(CONFIG));
}
FINSH_FUNCTION_EXPORT(nrf_info, printf nrf_info.)
#endif
struct nrf_data_packet
{
rt_uint8_t data_len;
rt_uint8_t buffer[NRF_SIZE];
};
struct spi_nrf_device
{
struct rt_device device;
struct rt_spi_device * rt_spi_device;
struct rt_event data_event;
struct nrf_data_packet tx_packet;
struct nrf_data_packet rx_packet;
};
举报