-
Notifications
You must be signed in to change notification settings - Fork 8
/
nrf.c
executable file
·262 lines (211 loc) · 4.93 KB
/
nrf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/spi/spi.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <mach/platform.h>
#include "nRF24L01.h"
#include "gpio.h"
#include "radio.h"
#define NRF_CE 25 //module chip enable pin
/* NRF registers*/
static const uint8_t nrf_reg[]={
CONFIG,
EN_AA,
EN_RXADDR,
SETUP_AW,
SETUP_RETR,
RF_CH,
RF_SETUP,
STATUS,
OBSERVE_TX,
RPD,
FIFO_STATUS
};
static char *nrf_reg_name[]={
"CONFIG",
"EN_AA",
"EN_RXADDR",
"SETUP_AW",
"SETUP_RETR",
"RF_CH",
"RF_SETUP",
"STATUS",
"OBSERVE_TX",
"RPD",
"FIFO_STATUS"
};
unsigned int address[5]={0x00,0x00,0x00,0x00,0x00};
static dev_t dev;
static struct cdev *nrf_cdev;
static struct class* nrf_class = NULL;
static struct device* nrf_device = NULL;
struct spi_device *spi_device;
/******************************
nrf module file operations
******************************/
static int nrf_open(struct inode *inode, struct file *fp)
{
return 0;
}
static int nrf_release(struct inode *inode,struct file *fp)
{
printk(KERN_INFO "device successfully closed\n");
return 0;
}
static ssize_t nrf_write(struct file *fp, const char *data, size_t len, loff_t *offset)
{
printk(KERN_INFO "%d bytes written!!\n",len);
return len;
}
/* Displays NRF module register info*/
static ssize_t nrf_read(struct file *fp, char *buf, size_t len, loff_t *offset){
uint8_t *ret;
char s[64];
int loop;
int count=0;
/*avoid repeated read*/
if(*offset > 0)
goto out;
for(loop = 0; loop < 11; loop++){
//ret = spi_w8r8(spi_device,nrf_reg[loop]);
ret = nrf_xfer(nrf_reg[loop],1,NULL,R);
mdelay(10);
sprintf(s,"%s = 0x%02x\n",nrf_reg_name[loop],ret[0]);
if(copy_to_user(buf+count,s,strlen(s))){
return -EFAULT;
}
*offset += strlen(s);
count+= strlen(s);
}
return count;
out:
return 0;
}
struct file_operations fops = {
.owner = THIS_MODULE,
.open = nrf_open,
.release = nrf_release,
.read = nrf_read,
.write = nrf_write,
};
/******************************
creates device class and device node
******************************/
static int reg_dev(void)
{
int retval=0;
retval = alloc_chrdev_region(&dev,0,1,"nrf");
if(retval != 0){
printk(KERN_ALERT "device registration failed!!\n");
goto out;
}else{
printk(KERN_INFO "device registration is successful!!\n");
}
nrf_cdev = cdev_alloc();
cdev_init(nrf_cdev,&fops);
nrf_cdev -> owner = THIS_MODULE;
retval = cdev_add(nrf_cdev,MKDEV(MAJOR(dev),0),1);
if(retval < 0){
printk(KERN_ALERT "adding to cdev is failed!!\n");
goto out;
}
nrf_class = class_create(THIS_MODULE,"radio");
if(IS_ERR(nrf_class)){
printk(KERN_ALERT "adding to class is failed!!\n");
retval = -1;
goto out;
}
printk(KERN_INFO "device class is registerd!!\n");
nrf_device = device_create(nrf_class,NULL,MKDEV(MAJOR(dev),0),NULL,"nrf");
if(IS_ERR(nrf_device)){
printk(KERN_ALERT "registering device is failed!!\n");
retval = -ENODEV;
goto out;
}
printk(KERN_INFO "device is registerd!!\n");
return 0;
out:
return retval;
}
/******************************
register spi device and attach it to Master driver
******************************/
static int reg_spi_device(void)
{
int retval = 0;
struct spi_board_info spi_device_info = {
.modalias = "nrf24l01+",
.max_speed_hz = 5000000,
.bus_num = 0,
.chip_select = 1,
.mode = 0,
};
struct spi_master *master;
master = spi_busnum_to_master(spi_device_info.bus_num);
if(!master){
printk(KERN_ALERT "getting master device is failed!!\n");
retval = -ENODEV;
goto out;
}
spi_device = spi_new_device(master,&spi_device_info);
if(!spi_device){
printk(KERN_ALERT "registering spi device is failed!!\n");
retval = -ENODEV;
goto out;
}
spi_device->bits_per_word = 8;
retval = spi_setup(spi_device);
if(retval){
spi_unregister_device(spi_device);
goto out;
}
return 0;
out:
return retval;
}
/******************************
Module initialization function
******************************/
static int __init nrf_init(void)
{
int retval = 0;
printk(KERN_INFO "hello from module!!\n");
retval = reg_dev();
if(retval != 0)
goto out;
retval = reg_spi_device();
if(retval != 0)
goto out;
radio_init();
return 0;
out:
return retval;
}
/******************************
Module exit function
******************************/
static void __exit nrf_exit(void)
{
printk(KERN_ALERT "Bye from module!!\n");
nrf_power_down();
spi_unregister_device(spi_device);
device_destroy(nrf_class,MKDEV(MAJOR(dev),0));
class_unregister(nrf_class);
class_destroy(nrf_class);
cdev_del(nrf_cdev);
unregister_chrdev_region(MKDEV(MAJOR(dev),0),1);
}
module_init(nrf_init);
module_exit(nrf_exit);
module_param_array(address,uint,NULL,0);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Nisarg Patel");
MODULE_VERSION("v0.1");
MODULE_DESCRIPTION("Driver for 2.4 Ghz RF module nrf24L01+");