关于arm+nrf24l01通信,请高手指点
时间:10-02
整理:3721RD
点击:
情部况是这样的,我想做linux arm + nrf24l01通信, 在网上找了一些资料,调试了很久才把驱动编译完成,本以为可以用了,测试一下发不出数据,和厂家送的资料对比了一下,也没发出问题,现在很困惑,
- #include <linux/config.h>
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/init.h>
- #include <linux/devfs_fs_kernel.h>
- #include <linux/miscdevice.h>
- #include <linux/delay.h>
- #include <asm/irq.h>
- #include <asm/arch/regs-gpio.h>
- #include <asm/hardware.h>
- #include <asm/uaccess.h>
- MODULE_LICENSE("GPL");
- typedef unsigned int uint16;
- typedef unsigned char uint8;
- #define CSN S3C2410_GPF3
- #define CSN_OUTP S3C2410_GPIO_OUTPUT
- #define MOSI S3C2410_GPF4
- #define MOSI_OUTP S3C2410_GPIO_OUTPUT
- #define IRQ S3C2410_GPG3
- #define IRQ_INP S3C2410_GPIO_INPUT
- #define MISO S3C2410_GPG0
- #define MISO_INP S3C2410_GPIO_INPUT
- #define SCK S3C2410_GPG5
- #define SCK_OUTP S3C2410_GPIO_OUTPUT
- #define CE S3C2410_GPG6
- #define CE_OUTP S3C2410_GPIO_OUTPUT
- #define DEVICE_NAME "NRF24L01"
- #define NRF24L01_MAJOR 255
- #define TxBufSize 32
- uint8 TxBuf[TxBufSize] = {
- 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
- 0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,
- 0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x24,
- 0x25,0x26,0x27,0x28,0x29,0x30,0x31,0x32,
- };
- #define CE_OUT s3c2410_gpio_cfgpin(CE,CE_OUTP)
- #define CE_UP s3c2410_gpio_pullup(CE,1)
- #define CE_L s3c2410_gpio_setpin(CE,0)
- #define CE_H s3c2410_gpio_setpin(CE,1)
- #define SCK_OUT s3c2410_gpio_cfgpin(SCK,SCK_OUTP)
- #define SCK_UP s3c2410_gpio_pullup(SCK,1)
- #define SCK_L s3c2410_gpio_setpin(SCK,0)
- #define SCK_H s3c2410_gpio_setpin(SCK,1)
- #define MISO_IN s3c2410_gpio_cfgpin(MISO,MISO_INP)
- #define MISO_UP s3c2410_gpio_pullup(MISO,1)
- #define MISO_STU s3c2410_gpio_getpin(MISO)
- #define IRQ_IN s3c2410_gpio_cfgpin(IRQ,IRQ_INP)
- #define IRQ_UP s3c2410_gpio_pullup(IRQ,1)
- #define IRQ_L s3c2410_gpio_setpin(IRQ,0)
- #define IRQ_H s3c2410_gpio_setpin(IRQ,1)
- #define MOSI_OUT s3c2410_gpio_cfgpin(MOSI,MOSI_OUTP)
- #define MOSI_UP s3c2410_gpio_pullup(MOSI,1)
- #define MOSI_L s3c2410_gpio_setpin(MOSI,0)
- #define MOSI_H s3c2410_gpio_setpin(MOSI,1)
- #define CSN_OUT s3c2410_gpio_cfgpin(CSN,CSN_OUTP)
- #define CSN_UP s3c2410_gpio_pullup(CSN,1)
- #define CSN_L s3c2410_gpio_setpin(CSN,0)
- #define CSN_H s3c2410_gpio_setpin(CSN,1)
- #define TX_ADR_WIDTH 5
- #define RX_ADR_WIDTH 5
- #define TX_PLOAD_WIDTH 32
- #define RX_PLOAD_WIDTH 32
- uint8 const TX_ADDRESS[TX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x01};
- uint8 const RX_ADDRESS[RX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x01};
- #define READ_REG 0x00
- #define WRITE_REG 0x20
- #define RD_RX_PLOAD 0x61
- #define WR_TX_PLOAD 0xA0
- #define FLUSH_TX 0xE1
- #define FLUSH_RX 0xE2
- #define REUSE_TX_PL 0xE3
- #define NOP 0xFF
- #define CONFIG 0x00
- #define EN_AA 0x01
- #define EN_RXADDR 0x02
- #define SETUP_AW 0x03
- #define SETUP_RETR 0x04
- #define RF_CH 0x05
- #define RF_SETUP 0x06
- #define STATUS 0x07
- #define OBSERVE_TX 0x08
- #define CD 0x09
- #define RX_ADDR_P0 0x0A
- #define RX_ADDR_p1 0x0B
- #define RX_ADDR_P2 0x0C
- #define RX_ADDR_P3 0x0D
- #define RX_ADDR_P4 0x0E
- #define RX_ADDR_P5 0x0F
- #define TX_ADDR 0x10
- #define RX_PW_P0 0x11
- #define RX_PW_P1 0x12
- #define RX_PW_P2 0x13
- #define RX_PW_P3 0x14
- #define RX_PW_P4 0x15
- #define RX_PW_P5 0x16
- #define FIFO_STATUS 0x17
- uint8 init_NRF24L01(void);
- uint8 SPI_RW(uint8 tmp);
- uint8 SPI_Read(uint8 reg);
- void SetRX_Mode(void);
- void SetTX_Mode(void);
- uint8 SPI_RW_Reg(uint8 reg,uint8 value);
- uint8 SPI_Read_Buf(uint8 reg,uint8 *pBuf,uint8 uchars);
- uint8 SPI_Write_Buf(uint8 reg,uint8 *pBuf,uint8 uchars);
- unsigned char nRF24L01_RxPacket(unsigned char* rx_buf);
- void nRF24L01_TxPacket(unsigned char *tx_buf);
- uint8 opencount = 0;
- uint8 sta;
- #define RX_DR 6
- #define TX_DS 5
- #define MAX_RT 4
- uint8 init_NRF24L01(void)
- {
- MISO_UP;
- CE_OUT;
- CSN_OUT;
- SCK_OUT;
- MOSI_OUT;
- MISO_IN;
- IRQ_IN;
- udelay(500);
- CE_L;
- ndelay(60);
- CSN_H;
- ndelay(60);
- SCK_L;
- ndelay(60);
- SPI_Write_Buf(WRITE_REG + TX_ADDR,TX_ADDRESS,TX_ADR_WIDTH);
- SPI_Write_Buf(WRITE_REG + RX_ADDR_P0,RX_ADDRESS,RX_ADR_WIDTH);
- SPI_RW_Reg(WRITE_REG + EN_AA,0x00);
- SPI_RW_Reg(WRITE_REG + EN_RXADDR,0x01);
- SPI_RW_Reg(WRITE_REG + RF_CH,0);
- SPI_RW_Reg(WRITE_REG + RX_PW_P0,RX_PLOAD_WIDTH);
- SPI_RW_Reg(WRITE_REG + RF_SETUP,0x07);
- SPI_RW_Reg(WRITE_REG + CONFIG,0x0f);
- mdelay(1000);
- // nRF24L01_TxPacket(TxBuf);
- // SPI_RW_Reg(WRITE_REG + STATUS,0xff);
- // printk("test 1\n");
- mdelay(1000);
- return 1;
- }
- uint8 SPI_RW(uint8 tmp)
- {
- uint8 bit_ctr;
- for(bit_ctr = 0;bit_ctr < 8;bit_ctr++)
- {
- if(tmp&0x80)
- MOSI_H;
- else
- MOSI_L;
- tmp<<=1;
- SCK_H;
- ndelay(60);
- tmp |= MISO_STU;
- SCK_L;
- ndelay(60);
- }
- return(tmp);
- }
- uint8 SPI_Read(uint8 reg)
- {
- uint8 reg_val;
- CSN_L;
- ndelay(60);
- SPI_RW(reg);
- reg_val = SPI_RW(0);
- CSN_H;
- ndelay(60);
- return reg_val;
- }
- uint8 SPI_RW_Reg(uint8 reg,uint8 value)
- {
- uint8 status;
- CSN_L;
- ndelay(60);
- status = SPI_RW(reg);
- SPI_RW(value);
- CSN_H;
- ndelay(60);
- return(status);
- }
- uint8 SPI_Read_Buf(uint8 reg,uint8 *pBuf,uint8 uchars)
- {
- uint8 status ,uint8_ctr;
- CSN_L;
- ndelay(60);
- status = SPI_RW(reg);
-
- for(uint8_ctr = 0;uint8_ctr < uchars;uint8_ctr++)
- {
- pBuf[uint8_ctr] = SPI_RW(0);
- ndelay(20);
- }
-
- CSN_H;
- ndelay(60);;
- return (status);
- }
- uint8 SPI_Write_Buf(uint8 reg,uint8 *pBuf,uint8 uchars)
- {
- uint8 status,uint8_ctr;
-
- CSN_L;
- ndelay(60);
- status = SPI_RW(reg);
- ndelay(60);
- for(uint8_ctr = 0;uint8_ctr < uchars;uint8_ctr++)
- {
- SPI_RW(*pBuf++);
- ndelay(20);
- }
- CSN_H;
- ndelay(60);
- return status;
- }
- void SetRX_Mode(void)
- {
- CE_L;
- ndelay(60);
- // SPI_Write_Buf(WRITE_REG + RX_ADDR_P0,TX_ADDRESS,TX_ADR_WIDTH);
- // SPI_RW_Reg(WRITE_REG + EN_AA,0x01);
- // SPI_RW_Reg(WRITE_REG + EN_RXADDR,0x01);
- // SPI_RW_Reg(WRITE_REG + RF_CH,40);
- // SPI_RW_Reg(WRITE_REG + RX_PW_P0,TX_PLOAD_WIDTH);
- // SPI_RW_Reg(WRITE_REG + RF_SETUP,0x07);
- // SPI_RW_Reg(WRITE_REG + CONFIG,0x0f);
- CE_H;
- udelay(131);
- }
- void SetTX_Mode(void)
- {
- udelay(100);
- CE_L;
- CSN_H;
- SCK_L;
- SPI_Write_Buf(WRITE_REG + TX_ADDR,TX_ADDRESS,TX_ADR_WIDTH);
- SPI_Write_Buf(WRITE_REG + RX_ADDR_P0,TX_ADDRESS,TX_ADR_WIDTH);
- SPI_Write_Buf(WR_TX_PLOAD,TxBuf,TX_PLOAD_WIDTH);
- SPI_RW_Reg(WRITE_REG + EN_AA,0x00);//0x01
- SPI_RW_Reg(WRITE_REG + EN_RXADDR,0x01);//0x01
- SPI_RW_Reg(WRITE_REG + SETUP_RETR,0x00);//0x1a
- SPI_RW_Reg(WRITE_REG + RF_CH,0);
- SPI_RW_Reg(WRITE_REG + RX_PW_P0,RX_PLOAD_WIDTH);
- SPI_RW_Reg(WRITE_REG + RF_SETUP,0x07);
- SPI_RW_Reg(WRITE_REG + CONFIG,0x0e);
- // CE_H;
- udelay(1000);
- }
- unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
- {
- unsigned char revale = 0;
- sta = SPI_Read(STATUS);
- if(sta&(1<<RX_DR))
- // if(RX_DR == 1)
- {
- CE_L;
- udelay(50);
- SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);
- revale = 1;
- }
- SPI_RW_Reg(WRITE_REG + STATUS,sta);
- return revale;
- }
- void nRF24L01_TxPacket(unsigned char* tx_buf)
- {
- CE_L;
- ndelay(60);
- SPI_Write_Buf(WRITE_REG + RX_ADDR_P0,TX_ADDRESS,TX_ADR_WIDTH);
- SPI_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);
- SPI_RW_Reg(WRITE_REG + CONFIG,0x0e);
- CE_H;
- printk("==%x=%x== ",SPI_RW(STATUS),SPI_RW(FIFO_STATUS));
- udelay(10);
- }
- static ssize_t nrf24l01_write(struct file *filp,const char *buffer,size_t count,loff_t* ppos)
- {
- int n1 = 0;
- int n2 = 0;
- if(copy_from_user(&TxBuf,buffer,count))
- {
- printk("Can't Send Data! --%d\n",count);
- return -EFAULT;
- }
- nRF24L01_TxPacket(TxBuf);
- n1 = SPI_RW( STATUS);
- n2 = SPI_RW(FIFO_STATUS);
- printk(" %x- %x ",n1,n2);
- // SPI_RW_Reg(FLUSH_TX,0x00);
- SPI_RW_Reg(WRITE_REG + STATUS,0xff);
- return 0;
- }
- static ssize_t nrf24l01_read(struct file* filp,char* buffer,size_t count,loff_t *ppos)
- {
- int ret = -1;
- SetRX_Mode();
- mdelay(100);
- nRF24L01_RxPacket(TxBuf);
- // ret = copy_to_user(buffer,TxBuf,count);
- SPI_RW_Reg(WRITE_REG + STATUS,0xff);
- // if(ret > 0) return -EFAULT;
- // else return 0;
- return 10;
- }
- static int nrf24l01_open(struct inode* node,struct file *file)
- {
- uint8 flag = 0;
- if(opencount == 1)
- return -EBUSY;
- flag = init_NRF24L01();
- mdelay(100);
- if(flag == 0)
- {
- printk("unable to open device \n");
- return -1;
- }
- else
- {
- opencount++;
- printk("device opened !\n");
- return 0;
- }
- }
- static int nrf24l01_release(struct inode* node,struct file* file)
- {
- opencount--;
- printk(DEVICE_NAME"release!\n");
- return 0;
- }
-
- static struct file_operations nrf24l01_fops = {
- .owner = THIS_MODULE,
- .open = nrf24l01_open,
- .write = nrf24l01_write,
- .read = nrf24l01_read,
- .release = nrf24l01_release,
- };
- static int __init nrf24l01_init(void)
- {
- int ret;
-
- printk("Initial driver for NRF24L01 .....\n");
- ret = register_chrdev(NRF24L01_MAJOR,DEVICE_NAME,&nrf24l01_fops);
- mdelay(10);
- if(ret < 0)
- {
- printk(DEVICE_NAME" can't register major number \n");
- return ret;
- }
- else
- {
- printk(DEVICE_NAME "register success \n");
- devfs_mk_cdev(MKDEV(NRF24L01_MAJOR, 0), S_IFCHR | S_IRUSR | S_IWUSR |S_IRGRP, DEVICE_NAME);
- return 0;
- }
- }
- static void __exit nrf24l01_exit(void)
- {
- devfs_remove(DEVICE_NAME);
- unregister_chrdev(NRF24L01_MAJOR,DEVICE_NAME);
- printk("NRF24L01 unregister success \n");
- }
- module_init(nrf24l01_init);
- module_exit(nrf24l01_exit);
本人小白,刚开始学习嵌入式,硬件功底不好,现在这里也米有仪器,也不能检测,都困惑了好几天了,请高手来指点一下吧
要要示波器看相关的引脚才能调试呢 网上的代码需要根据实际的情况进行移植才能使用
不错,下载下来看看。
看一下是否真的驱动起来了,还是你发送的数据格式有错,还是时序上有问题,你可以用示波器看一下波形