NRF24L01使用的SPI协议通信,这里并没有用到s3c2440自带的SPI功能,而是直接用IO口模拟SPI。而根据Datasheet所示,NRF24L01的SPI接速率为0~8Mbps,因此需要在读写时序上加上适当的延时。
驱动程序:
/************************************************************/
//文件名:nrf24l01.c
//功能:linux下的nrf24l01驱动程序
//使用说明: (1)
// (2)
// (3)
// (4)
//作者:jammy-lee
//日期:2010-01-11
/************************************************************/
#include <linux/config.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/miscdevice.h>
#include <linux/devfs_fs_kernel.h>
#include <asm/uaccess.h>
#include <asm/hardware.h>
#include <asm/arch/regs-gpio.h>
typedef unsigned int uint16 ;
typedef unsigned char uint8 ;
/*
//和引脚相关的宏定义
#define CE S3C2410_GPF3
#define CE_OUTP S3C2410_GPF3_OUTP
#define SCK S3C2410_GPF4
#define SCK_OUTP S3C2410_GPF4_OUTP
#define MISO S3C2410_GPG3
#define MISO_INP S3C2410_GPG3_INP
#define IRQ S3C2410_GPG0
#define IRQ_OUTP S3C2410_GPG0_OUTP
#define MOSI S3C2410_GPG5
#define MOSI_OUTP S3C2410_GPG5_OUTP
#define CSN S3C2410_GPG6
#define CSN_OUTP S3C2410_GPG6_OUTP
*/
//和引脚相关的宏定义
#define CSN S3C2410_GPF3
#define CSN_OUTP S3C2410_GPF3_OUTP
#define MOSI S3C2410_GPF4
#define MOSI_OUTP S3C2410_GPF4_OUTP
#define IRQ S3C2410_GPG3
#define IRQ_INP S3C2410_GPG3_INP
#define MISO S3C2410_GPG0
#define MISO_INP S3C2410_GPG0_INP
#define SCK S3C2410_GPG5
#define SCK_OUTP S3C2410_GPG5_OUTP
#define CE S3C2410_GPG6
#define CE_OUTP S3C2410_GPG6_OUTP
#define DEVICE_NAME "NRF24L01" //设备名称,在可以 /proc/devices 查看
#define NRF24L01_MAJOR 241 //主设备号
#define TxBufSize 32
uint8 TxBuf[TxBufSize]={
0x01,0x02,0x03,0x4,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,
};
//NRF24L01端口定义
#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) //拉高数据线电平