2 Star 5 Fork 3

liguang13579 / Linux NRF24L01SPI Network Card Drivers

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
nrf2401_test.c 8.82 KB
一键复制 编辑 原始数据 按行查看 历史
/*
* /linux-3.0.8/drivers/spi/nrf2401_test.c
*
*/
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <time.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include "nrf2401_test.h"
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
static const __u8 dev0_addr[5] = {0x00, 0x00, 0x00, 0x01, 0x24};
static const __u8 dev1_addr[5] = {0x00, 0x11, 0x11, 0x01, 0x24};
static char *device = "/dev/nrf2401-0.0";
static char is_dev0 = 0;
//static const char *device = "/dev/nrf2401-1.0";
static uint8_t mode = SPI_MODE_0;
static uint8_t bits = 8;
static uint32_t speed = 500000;
static uint16_t delay;
static void pabort(const char *s){perror(s);abort();}
static void print_usage(const char *prog)
{
printf("Usage: %s [-DsbdlHOLC3]\n", prog);
puts(" -D --device device to use (default /dev/spidev1.1)\n"
" -s --speed max speed (Hz)\n"
" -d --delay delay (usec)\n"
" -b --bpw bits per word \n"
" -l --loop loopback\n"
" -H --cpha clock phase\n"
" -O --cpol clock polarity\n"
" -L --lsb least significant bit first\n"
" -C --cs-high chip select active high\n"
" -3 --3wire SI/SO signals shared\n");
exit(1);
}
static void parse_opts(int argc, char *argv[])
{
while (1) {
static const struct option lopts[] = {
{ "device", 1, 0, 'D' },
{ "speed", 1, 0, 's' },
{ "delay", 1, 0, 'd' },
{ "bpw", 1, 0, 'b' },
{ "loop", 0, 0, 'l' },
{ "cpha", 0, 0, 'H' },
{ "cpol", 0, 0, 'O' },
{ "lsb", 0, 0, 'L' },
{ "cs-high", 0, 0, 'C' },
{ "3wire", 0, 0, '3' },
{ "no-cs", 0, 0, 'N' },
{ "ready", 0, 0, 'R' },
{ NULL, 0, 0, 0 },
};
int c;
c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR", lopts, NULL);
if (c == -1)
break;
switch (c) {
case 'D':
device = optarg;
break;
case 's':
speed = atoi(optarg);
break;
case 'd':
delay = atoi(optarg);
break;
case 'b':
bits = atoi(optarg);
break;
case 'l':
mode |= SPI_LOOP;
break;
case 'H':
mode |= SPI_CPHA;
break;
case 'O':
mode |= SPI_CPOL;
break;
case 'L':
mode |= SPI_LSB_FIRST;
break;
case 'C':
mode |= SPI_CS_HIGH;
break;
case '3':
mode |= SPI_3WIRE;
break;
case 'N':
mode |= SPI_NO_CS;
break;
case 'R':
mode |= SPI_READY;
break;
default:
print_usage(argv[0]);
break;
}
}
}
/***** nrf2401 card ops *****/
static int nrf2401_operate_chip(int fd,
struct nrf2401_ioc_transfer *xfer)
{
int ret = 0;
ret = ioctl(fd, SPI_IOC_MESSAGE(1), xfer);
if (ret == -1)
printf(" Y@_@Y (ioctl error) Y@_@Y\n");
return ret;
}
/***** nrf2401 read, write blk ops *****/
static int nrf2401_read_pipe(int fd,
struct nrf2401_ioc_transfer *xfer, int select_index)
{
int i =0;
int ret = 0;
//xfer->delay_ms = 10000;
//xfer->rw_mode = NRF2401_IOC_RW_NOBLOCK;
xfer->ioc_type = NRF2401_IOC_READ_PIPE;
xfer->addr_num = (__u8)select_index - 0 ;
printf("/***** nrf2401 read pipe test: start(pipe = %d) *****/\n", xfer->addr_num );
memset(xfer->txrx_buf, 0x00,
NRF2401_PIPE_LEN);
ret = nrf2401_operate_chip(fd, xfer);
if(ret < 0){
return ret;
}
for(i=0; i<NRF2401_PIPE_LEN; i++){
if(i%4 == 0) printf("\n");
printf("0x%02x ", xfer->txrx_buf[i]);
}
printf("\n/***** nrf2401 read pipe test: end *****/\n");
}
static int nrf2401_write_pipe(int fd,
struct nrf2401_ioc_transfer *xfer, int select_index)
{
printf("/***** nrf2401 write pipe test: start(pipe = %d) *****/\n", (__u8)select_index - 6);
int i =0;
int ret =0;
//xfer->delay_ms = 10000;
xfer->ioc_type = NRF2401_IOC_SET_TX_PIPE;
xfer->addr_num = 0;
if(is_dev0){
memcpy(xfer->txrx_buf, dev1_addr, NRF2401_ADDR_LEN);
xfer->txrx_buf[0] = (__u8)select_index - 6;
}else{
memcpy(xfer->txrx_buf, dev0_addr, NRF2401_ADDR_LEN);
xfer->txrx_buf[0] = (__u8)select_index - 6;
}
ret = nrf2401_operate_chip(fd, xfer);
if(ret < 0){
return ret;
}
//xfer->rw_mode = NRF2401_IOC_RW_NOBLOCK;
xfer->ioc_type = NRF2401_IOC_WRITE_PIPE,
memset(xfer->txrx_buf, (__u8)(rand()%10),
NRF2401_PIPE_LEN);
ret = nrf2401_operate_chip(fd, xfer);
if(ret < 0)
return 0;
for(i=0; i<NRF2401_PIPE_LEN; i++){
if(i%4 == 0) printf("\n");
printf("0x%02x ", xfer->txrx_buf[i]);
}
printf("\n/***** nrf2401 write pipe test: end *****/\n");
}
static int nrf2401_rw_mode(int fd,
struct nrf2401_ioc_transfer *xfer, int select_index)
{
int ret = 0;
if(select_index == 12){
printf("\n/***** nrf2401 read write mode: block *****/\n");
xfer->rw_mode = NRF2401_IOC_RW_BLOCK;
}
else if(select_index == 13){
printf("\n/***** nrf2401 read write mode: no block *****/\n");
xfer->rw_mode = NRF2401_IOC_RW_NOBLOCK;
}
else
;
return ret;
}
static int nrf2401_goto_menu(int fd,
struct nrf2401_ioc_transfer *xfer, int select_index)
{
return 0;
}
struct {
void (*function)(int fd, struct nrf2401_ioc_transfer *xfer, int select_index);
char *msg;
}ioc_type_menu[] = {
//{ nrf2401_goto_menu, "nrf2401 goto before menu: \n" } ,
{ nrf2401_read_pipe, "nrf2401 read pipe 0 : \n" } ,
{ nrf2401_read_pipe, "nrf2401 read pipe 1 : \n" } ,
{ nrf2401_read_pipe, "nrf2401 read pipe 2 : \n" } ,
{ nrf2401_read_pipe, "nrf2401 read pipe 3 : \n" } ,
{ nrf2401_read_pipe, "nrf2401 read pipe 4 : \n" } ,
{ nrf2401_read_pipe, "nrf2401 read pipe 5 : \n\n" } ,
{ nrf2401_write_pipe, "nrf2401 write pipe 0 : \n" } ,
{ nrf2401_write_pipe, "nrf2401 write pipe 1 : \n" } ,
{ nrf2401_write_pipe, "nrf2401 write pipe 2 : \n" } ,
{ nrf2401_write_pipe, "nrf2401 write pipe 3 : \n" } ,
{ nrf2401_write_pipe, "nrf2401 write pipe 4 : \n" } ,
{ nrf2401_write_pipe, "nrf2401 write pipe 5 : \n\n" } ,
{ nrf2401_rw_mode, "nrf2401 read write mode : block\n" } ,
{ nrf2401_rw_mode, "nrf2401 read write mode : noblock\n" } ,
{ 0, 0} ,
};
static int nrf2401_menu_card(int fd, struct nrf2401_ioc_transfer *xfer)
{
int i =0;
int select_index =0;
while(1){
while(1){
printf("\nPlease select function: \n");
for(i=0; ioc_type_menu[i].function!=0; i++)
printf("%d : %s", i, ioc_type_menu[i].msg);
printf("\nPlease input the select number:");
scanf("%d", &select_index);
printf("You select %d function: %s\n",
select_index, ioc_type_menu[select_index].msg);
//getchar();
if(0 <= select_index && select_index < i)
{
(*ioc_type_menu[select_index].function)(fd, xfer, select_index);
sleep(1);
}else{
break;
}
//printf("\nPlease enter key to contine:\n");
//getchar();
}
}
printf("Do not goto here\n");
return i;
}
int main(int argc, char *argv[])
{
int ret = 0;
int fd = -1;
parse_opts(argc, argv);
fd = open(device, O_RDWR);
if (fd < 0)
pabort("failed to open device");
struct nrf2401_ioc_transfer xfer;
xfer.rw_mode = NRF2401_IOC_RW_NOBLOCK;
if( strcmp(device, "/dev/nrf2401-0.0" , strlen("/dev/nrf2401-0.0")) == 0){
printf("good to open device: %s, rw mode = %d,( %d,%d)\n",
device, xfer.rw_mode,NRF2401_IOC_RW_NOBLOCK,NRF2401_IOC_RW_BLOCK );
is_dev0 = 1;
__u8 i = 0;
__u8 j = 0;
memcpy(xfer.txrx_buf, dev0_addr, NRF2401_ADDR_LEN);
for(i = 0; i<=5; i++){
xfer.ioc_type = NRF2401_IOC_SET_RX_PIPE;
xfer.addr_num = i;
xfer.txrx_buf[0] = i;
nrf2401_operate_chip(fd, &xfer);
}
nrf2401_menu_card(fd, &xfer);
}else if( strcmp(device, "/dev/nrf2401-1.0" , strlen("/dev/nrf2401-1.0")) == 0){
printf("good to open device: %s, rw mode = %d,( %d,%d)\n",
device, xfer.rw_mode,NRF2401_IOC_RW_NOBLOCK,NRF2401_IOC_RW_BLOCK );
is_dev0 = 0;
__u8 i = 0;
__u8 j = 0;
memcpy(xfer.txrx_buf, dev1_addr, NRF2401_ADDR_LEN);
for(i = 0; i<=5; i++){
xfer.ioc_type = NRF2401_IOC_SET_RX_PIPE;
xfer.addr_num = i;
xfer.txrx_buf[0] = i;
nrf2401_operate_chip(fd, &xfer);
}
nrf2401_menu_card(fd, &xfer);
}
/*
printf("SPI interface mode: \n");
ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
if (ret == -1)
pabort("can't set spi mode");
ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
if (ret == -1)
pabort("can't get spi mode");
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -1)
pabort("can't set bits per word");
ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
if (ret == -1)
pabort("can't get bits per word");
ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
if (ret == -1)
pabort("can't set max speed hz");
ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
if (ret == -1)
pabort("can't get max speed hz");
printf("spi mode: %d\n", mode);
printf("bits per word: %d\n", bits);
printf("max speed: %d Hz (%d KHz)\n\n\n", speed, speed/1000);
*/
close(fd);
return ret;
}
C
1
https://gitee.com/liguang13579/Linux-NRF24L01SPI-Network-Card-Drivers.git
git@gitee.com:liguang13579/Linux-NRF24L01SPI-Network-Card-Drivers.git
liguang13579
Linux-NRF24L01SPI-Network-Card-Drivers
Linux NRF24L01SPI Network Card Drivers
master

搜索帮助