完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
第一次写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 请大神帮忙看看有什么地方需要注意,或者怎样修改 ,代码测试能用, 以下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; }; |
|
相关推荐
6个回答
|
|
|
|
|
|
有中断的设备,底层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; } |
|
|
|
中断里面通知线程去处理。
|
|
|
|
在底层中创建一个线程 处理中断信息吗
|
|
|
|
更新下驱动 在底层创建一个线程接收数据,大家给点意见,
#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; }; |
|
|
|
这个代码在ISR中可以读取中断寄存器,然后根据超时,发送成功,接收成功发出不同的event,同时接收还需要读出接收到的数据。
用户线程根据不同的event去处理,例如三个event 0x01 0x02 0x04 ,分别代表前3种事件,0x08代表有数据需要发送事件,这样就用户线程只需要永远等待事件,然后根据不同事件去处理即可。 |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
792 浏览 0 评论
4507 浏览 0 评论
如何使用python调起UDE STK5.2进行下载自动化下载呢?
2564 浏览 0 评论
开启全新AI时代 智能嵌入式系统快速发展——“第六届国产嵌入式操作系统技术与产业发展论坛”圆满结束
2939 浏览 0 评论
获奖公布!2024 RT-Thread全球巡回线下培训火热来袭!报名提问有奖!
31587 浏览 11 评论
73020 浏览 21 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-24 08:15 , Processed in 0.762824 second(s), Total 82, Slave 65 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号