Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
BSP
RTT nrf24l01 设备驱动程序
发布于 2015-05-20 22:06:14 浏览:7970
订阅该版
第一次写os底层驱动,写的不好请多多谅解 今天写的nrf24l01 设备驱动程序 请大神给点意见 ```#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_dev; #define nrf24l01_lock() rt_mutex_take(&nrf24l01_dev.lock, RT_WAITING_FOREVER) #define nrf24l01_unlock() rt_mutex_release(&nrf24l01_dev.lock) /* 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_dev.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_dev.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_dev.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_dev.rt_spi_device,®,1,&date,1); return date; } extern void spi_nrf_isr(int vector) { rt_uint8_t sta; nrf24l01_lock(); sta=NRF24L01_Read_Reg(STATUS); //????×????????÷???? NRF_TRACE("nrf isr %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?????÷ } if(sta&TX_OK)//·????ê?? { NRF_TRACE("send ok "); } if(sta&RX_OK)//?????????? { NRF_TRACE("recv ok "); NRF24L01_Read_Buf(RD_RX_PLOAD,nrf24l01_dev.date.buff,20);//???????? nrf24l01_dev.date.Wrt=20; NRF24L01_Write_Reg(FLUSH_RX,0xff);//????RX FIFO?????÷ } nrf24l01_unlock(); } /* 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); SetCEN(0); 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); break; /* EN CH */ case SET_EN_CH: send_buffer[0]=NRF_WRITE_REG+EN_RXADDR; rt_spi_send_then_send(nrf24l01_dev.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_dev.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_dev.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_dev.rt_spi_device,send_buffer,1,args,5); send_buffer[0]=NRF_WRITE_REG+RX_ADDR_P0; rt_spi_send_then_send(nrf24l01_dev.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_dev.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_dev.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_dev.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_dev.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_dev.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_dev.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_dev.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_dev.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_dev.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_dev.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_dev.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_dev.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_dev.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_dev.rt_spi_device,send_buffer,1,args,1); break; } SetCEN(1); return RT_EOK; } static rt_size_t nrf24lxx_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) { size=nrf24l01_dev.date.Wrt; if(nrf24l01_dev.date.Wrt>0) { rt_memcpy(buffer, buffer, size); nrf24l01_dev.date.Wrt=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) { SetCEN(0); NRF_TRACE("start sengd date "); NRF24L01_Write_Buf(WR_TX_PLOAD,buffer,size); SetCEN(1); return size; } rt_err_t nrf24l01_init(const char * nrf_device_name, const char * spi_device_name) { struct rt_spi_device * rt_spi_device; /* initialize mutex */ if (rt_mutex_init(&nrf24l01_dev.lock, spi_device_name, RT_IPC_FLAG_FIFO) != RT_EOK) { rt_kprintf("init sd lock mutex failed "); return -RT_ENOSYS; } rt_spi_device = (struct rt_spi_device *)rt_device_find(spi_device_name); if(rt_spi_device == RT_NULL) { NRF_TRACE("spi device %s not found! ", spi_device_name); return -RT_ENOSYS; } nrf24l01_dev.rt_spi_device = rt_spi_device; /* 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(rt_spi_device, &cfg); } /* 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_dev.device,SET_TX_ADDR,"FYYYY"); /* set nrf en ack */ sendbuffer[0]=0x00; nrf24lxx_control(&nrf24l01_dev.device,SET_EN_ACK,sendbuffer); /* set nrf en ch */ sendbuffer[0]=0x01; nrf24lxx_control(&nrf24l01_dev.device,SET_EN_CH,sendbuffer); /* reset send time :500us + 86us; send max count 10 */ sendbuffer[0]=0x1a; nrf24lxx_control(&nrf24l01_dev.device,SET_RF_RETR,sendbuffer); /* set nrf rf ch */ sendbuffer[0]=40; nrf24lxx_control(&nrf24l01_dev.device,SET_RF_CH,sendbuffer); sendbuffer[0]=0x0f; nrf24lxx_control(&nrf24l01_dev.device,SET_RF_SETUP,sendbuffer); } /* register device */ nrf24l01_dev.device.type = RT_Device_Class_Char; nrf24l01_dev.device.init = nrf24lxx_init; nrf24l01_dev.device.open = nrf24lxx_open; nrf24l01_dev.device.close = nrf24lxx_close; nrf24l01_dev.device.read = nrf24lxx_read; nrf24l01_dev.device.write = nrf24lxx_write; nrf24l01_dev.device.control = nrf24lxx_control; /* no private */ nrf24l01_dev.device.user_data = RT_NULL; rt_device_register(&nrf24l01_dev.device, nrf_device_name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE); return RT_EOK; } #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%2x ",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```
查看更多
19
个回答
默认排序
按发布时间排序
zhuwenqiang
2015-05-20
这家伙很懒,什么也没写!
``` 请大神帮忙看看有什么地方需要注意,或者怎样修改 ,代码测试能用, 以下NRF_device是结构 struct nrf_date { rt_uint8_t buff[30]; rt_uint8_t Wrt; }; struct spi_nrf_device { struct rt_device device; struct rt_spi_device * rt_spi_device; struct nrf_date date; struct rt_mutex lock; }; ```
aozima
2015-05-20
调网络不抓包,调I2C等时序不上逻辑分析仪,就像电工不用万用表!多用整理的好的文字,比截图更省流量,还能在整理过程中思考。
ISR中不要去做读写。 有读写就会去试图持有Mutex,这是错误的用法。 即使没有mutex,ISR中也不适合作这类耗费的操作。
zhuwenqiang
2015-05-21
这家伙很懒,什么也没写!
有中断的设备,底层os驱动需要怎样处理,比如有数据接收,发生中断, 怎样去读数据, 读函数可以写成 ``` static rt_size_t nrf24lxx_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) { rt_uint8_t sta; sta=NRF24L01_Read_Reg(STATUS); //读取状态寄存器的值 if(sta&RX_OK)//接收到数据 { NRF_TRACE("recv data "); NRF24L01_Write_Reg(NRF_WRITE_REG+STATUS,sta); //清除TX_DS或MAX_RT中断标志 NRF24L01_Read_Buf(RD_RX_PLOAD,buffer,20);//读取数据 return 20; } return 0; ``` }
aozima
2015-05-21
调网络不抓包,调I2C等时序不上逻辑分析仪,就像电工不用万用表!多用整理的好的文字,比截图更省流量,还能在整理过程中思考。
中断里面通知线程去处理。
zhuwenqiang
2015-05-21
这家伙很懒,什么也没写!
在底层中创建一个线程 处理中断信息吗
ralfak
2015-05-22
这家伙很懒,什么也没写!
很好,支持楼主继续完善驱动,以及使用该驱动的例程
zhuwenqiang
2015-05-22
这家伙很懒,什么也没写!
更新下驱动 在底层创建一个线程接收数据,大家给点意见, ``` #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[i]!=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; }; ```
zhuwenqiang
2015-05-25
这家伙很懒,什么也没写!
没有人吗
huang7094748
2015-07-09
这家伙很懒,什么也没写!
楼主代码写得不错
huang7094748
2015-07-10
这家伙很懒,什么也没写!
您好 你的代码,nrf24l01_init函数中倒数第四行的spi_nrf_hw_init()来自哪里呢?在哪里定义的,能详细说说吗
撰写答案
登录
注册新账号
关注者
0
被浏览
8k
关于作者
zhuwenqiang
这家伙很懒,什么也没写!
提问
9
回答
14
被采纳
0
关注TA
发私信
相关问题
1
STM32 407 串口接收数据 系统卡死
2
stm32f10x串口只能发送数据,无法接收
3
第一次尝试移植rt-thread 到stm32F103系列问题
4
有人把stm32L07xx的bsp移到rtt上来了吗?求一个
5
rt-thread线程调度异常在stm32f103芯片上
6
RTT是否支持STM32F429
7
请问谁有 STM32F40x HAL + RT-THREAD 模板
8
rt-thread在stm32f411下的移植问题
9
针对STM32F7系列平台的MPU,Cache特性,需要注意哪些问题?
10
以后会把RT-thread移植到STM32的cube库上面么?我自己移植了一下STM32F373
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
【RT-Thread】【ci】【scons】将ci.attachconfig.yml和scons结合使用
2
Rt-thread中OTA下载后,bootloader不搬程序
3
ulog 日志 LOG_HEX 输出时间改为本地日期时间
4
在RT-Thread Studio中构建前执行python命令
5
研究一了一段时间RTT,直接标准版上手太难,想用nano,但又舍不得组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
I2C_IIC
ESP8266
UART
WIZnet_W5500
ota在线升级
cubemx
PWM
flash
freemodbus
BSP
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
flashDB
GD32
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
SFUD
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
xusiwei1236
8
个答案
2
次被采纳
踩姑娘的小蘑菇
1
个答案
2
次被采纳
用户名由3_15位
9
个答案
1
次被采纳
bernard
4
个答案
1
次被采纳
RTT_逍遥
3
个答案
1
次被采纳
本月文章贡献
聚散无由
2
篇文章
15
次点赞
catcatbing
2
篇文章
5
次点赞
Wade
2
篇文章
4
次点赞
Ghost_Girls
1
篇文章
7
次点赞
xiaorui
1
篇文章
2
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部