forked from BeaglePilot/BeagleBone-Black-MPU6000-Driver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpu_6000.c
390 lines (291 loc) · 8.95 KB
/
mpu_6000.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/sched.h>
#include <linux/mod_devicetable.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/spi/spi.h>
#include <linux/of.h>
#include <linux/kthread.h>
#include <linux/time.h>
#include <linux/kfifo.h>
#include <linux/mutex.h>
#include "mpu_6000.h"
#define CLASS_NAME "mpu6000"
#define swap_uint16_t(x)((x & 0xff) << 8) | ((x & 0xff00) >> 8)
typedef struct MPUReport {
struct timeval timestamp;
unsigned long error_count;
int16_t gyro_x_raw;
int16_t gyro_y_raw;
int16_t gyro_z_raw;
int16_t accel_x_raw;
int16_t accel_y_raw;
int16_t accel_z_raw;
int16_t temperature_raw;
}MPUReport;
MODULE_LICENSE("Dual BSD/GPL");
static DEFINE_MUTEX(read_lock);
#define FIFO_SIZE 32
static DECLARE_KFIFO(mpu_reports, MPUReport, FIFO_SIZE);
static struct task_struct *mpu_sample_thread;
static struct class* mpu_class = NULL;
static struct device* mpu_device = NULL;
static int write_reg(struct spi_device *spi, uint8_t reg, uint8_t val);
static ssize_t read_reg(struct spi_device *spi, uint8_t reg);
static void set_sample_rate(struct spi_device *spi, uint16_t desired_sample_rate_hz);
static void set_dlpf_filter(struct spi_device *spi, uint16_t frequency_hz);
static ssize_t read_multiple_regs(struct spi_device *spi, uint8_t reg, uint8_t * rx_buf, size_t length);
static int sample_mpu(void * data);
struct {
unsigned int sample_rate;
uint8_t product;
} MPU6000_data = { .sample_rate = 1000, .product = 0 };
static struct of_device_id imu_of_match[] = {
{ .compatible = "ti,imu", },
{ }
};
MODULE_DEVICE_TABLE(of, imu_of_match);
static int imu_remove(struct spi_device *spi) {
printk("Module removed\n");
return 0;
}
static ssize_t sys_store_sample_rate(struct device* dev,
struct device_attribute* attr,
const char* buf,
size_t count) {
unsigned int sample_rate;
int status;
status = kstrtouint(buf, 10, &sample_rate);
if(status < 0) {
return status;
}
set_sample_rate((struct spi_device *) dev->platform_data, sample_rate);
printk(KERN_INFO "MPU 6000 Sample rate set to %d\n", sample_rate);
return count;
}
static DEVICE_ATTR(sample_rate, S_IWUSR, NULL, sys_store_sample_rate);
static int imu_probe(struct spi_device *spi) {
int status = 0;
// Save spi pointer info
spi->dev.platform_data = spi;
//reset
status = write_reg(spi, MPUREG_PWR_MGMT_1, BIT_H_RESET);
mdelay(100);
//sleep mode off
status = write_reg(spi, MPUREG_PWR_MGMT_1, 0x00); //clear SLEEP bit
mdelay(100);
//disable I2C
status = write_reg(spi, MPUREG_USER_CTRL, BIT_I2C_IF_DIS); //set bit I2C_IF_DIS to 1
mdelay(100);
// SAMPLE RATE
set_sample_rate(spi, MPU6000_data.sample_rate);
mdelay(100);
// FS & DLPF FS=2000 deg/s, DLPF = 20Hz (low pass filter)
set_dlpf_filter(spi, MPU6000_DEFAULT_ONCHIP_FILTER_FREQ);
mdelay(100);
// Gyro scale 2000 deg/s ()
write_reg(spi, MPUREG_GYRO_CONFIG, BITS_FS_2000DPS);
mdelay(100);
MPU6000_data.product = read_reg(spi, MPUREG_PRODUCT_ID);
// product-specific scaling
switch (MPU6000_data.product) {
case MPU6000ES_REV_C4:
case MPU6000ES_REV_C5:
case MPU6000_REV_C4:
case MPU6000_REV_C5:
// Accel scale 8g (4096 LSB/g)
// Rev C has different scaling than rev D
write_reg(spi, MPUREG_ACCEL_CONFIG, 1 << 3);
break;
case MPU6000ES_REV_D6:
case MPU6000ES_REV_D7:
case MPU6000ES_REV_D8:
case MPU6000_REV_D6:
case MPU6000_REV_D7:
case MPU6000_REV_D8:
case MPU6000_REV_D9:
case MPU6000_REV_D10:
// default case to cope with new chip revisions, which
// presumably won't have the accel scaling bug
default:
// Accel scale 8g (4096 LSB/g)
write_reg(spi, MPUREG_ACCEL_CONFIG, 2 << 3);
break;
}
mdelay(200);
// INT CFG => Interrupt on Data Ready
write_reg(spi, MPUREG_INT_ENABLE, BIT_RAW_RDY_EN); // INT: Raw data ready
mdelay(100);
write_reg(spi, MPUREG_INT_PIN_CFG, BIT_INT_ANYRD_2CLEAR); // INT: Clear on any read
mdelay(300);
mpu_sample_thread = kthread_create(sample_mpu, spi, "mpu_sampling");
if(mpu_sample_thread) {
printk(KERN_INFO "New thread created");
wake_up_process(mpu_sample_thread);
}
status = device_create_file(&spi->dev, &dev_attr_sample_rate);
return 0;
}
int write_reg(struct spi_device *spi, uint8_t reg, uint8_t val) {
uint8_t tx[2];
tx[0] = reg;
tx[1] = val;
return spi_write(spi, tx, sizeof(tx));
}
/*
set the DLPF filter frequency. This affects both accel and gyro.
*/
void set_dlpf_filter(struct spi_device *spi, uint16_t frequency_hz) {
uint8_t filter;
/*
choose next highest filter frequency available
*/
if (frequency_hz <= 5) {
filter = BITS_DLPF_CFG_5HZ;
} else if (frequency_hz <= 10) {
filter = BITS_DLPF_CFG_10HZ;
} else if (frequency_hz <= 20) {
filter = BITS_DLPF_CFG_20HZ;
} else if (frequency_hz <= 42) {
filter = BITS_DLPF_CFG_42HZ;
} else if (frequency_hz <= 98) {
filter = BITS_DLPF_CFG_98HZ;
} else if (frequency_hz <= 188) {
filter = BITS_DLPF_CFG_188HZ;
} else if (frequency_hz <= 256) {
filter = BITS_DLPF_CFG_256HZ_NOLPF2;
} else {
filter = BITS_DLPF_CFG_2100HZ_NOLPF;
}
write_reg(spi, MPUREG_CONFIG, filter);
}
ssize_t read_reg(struct spi_device *spi, uint8_t reg) {
return spi_w8r8(spi, reg | 0x80);
}
static ssize_t read_multiple_regs(struct spi_device *spi, uint8_t reg, uint8_t * rx_buf, size_t length) {
reg = reg | 0x80;
return spi_write_then_read(spi, ®, sizeof(uint8_t), rx_buf, length);
}
static ssize_t mpu_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) {
int ret;
unsigned int copied;
if (mutex_lock_interruptible(&read_lock))
return -ERESTARTSYS;
ret = kfifo_to_user(&mpu_reports, buf, count, &copied);
mutex_unlock(&read_lock);
return ret ? ret : copied;
}
static ssize_t mpu_open(struct inode * inode, struct file * file_ptr) {
printk("Opened mpu 6000\n");
return 0;
}
static ssize_t mpu_close(struct inode * inode, struct file * file_ptr) {
printk("Closed mpu 6000\n");
return 0;
}
static struct file_operations fops = {
.read = mpu_read,
.open = mpu_open,
.release = mpu_close
};
static struct spi_driver imu_driver = {
.driver = {
.name = "imu",
.owner = THIS_MODULE,
.of_match_table = imu_of_match,
},
.probe = imu_probe,
.remove = imu_remove,
};
void set_sample_rate(struct spi_device *spi, uint16_t desired_sample_rate_hz) {
uint8_t div = 1000 / desired_sample_rate_hz;
if (div > 200)
div = 200;
if (div < 1)
div = 1;
write_reg(spi, MPUREG_SMPLRT_DIV, div - 1);
MPU6000_data.sample_rate = 1000 / div;
}
static int mpu_init(void) {
int mpu_major;
int retval = 0;
INIT_KFIFO(mpu_reports);
retval = spi_register_driver(&imu_driver);
if (retval < 0) {
printk(KERN_ERR "failed to register driver: error %d\n", retval);
return retval;
}
mpu_major = register_chrdev(0, "imu", &fops);
if (mpu_major < 0) {
printk(KERN_ERR "failed to register device: error %d\n", mpu_major);
retval = mpu_major;
goto init_failed;
}
mpu_class = class_create(THIS_MODULE, CLASS_NAME);
if (IS_ERR(mpu_class)) {
printk(KERN_ERR "failed to register device class '%s'\n", CLASS_NAME);
retval = PTR_ERR(mpu_class);
goto init_failed;
}
mpu_device = device_create(mpu_class, NULL, MKDEV(mpu_major, 0), NULL, CLASS_NAME);
if (IS_ERR(mpu_device)) {
printk(KERN_ERR "failed to create device '%s'\n", CLASS_NAME);
retval = PTR_ERR(mpu_device);
goto init_failed;
}
return retval;
init_failed:
//spi_unregister_device(spi);
return retval;
}
static int sample_mpu(void * data) {
struct spi_device *spi = (struct spi_device *) data;
MPUReport mpu_report;
int result;
#pragma pack(push, 1)
struct mpu_report {
uint8_t status;
int16_t gyro_x;
int16_t gyro_y;
int16_t gyro_z;
int16_t temp;
int16_t accel_x;
int16_t accel_y;
int16_t accel_z;
} mpu_raw_report = {0};
#pragma pack(pop)
while(1) {
set_current_state(TASK_INTERRUPTIBLE);
// change this to be controlled by mpu interrupt
schedule_timeout (HZ/100);
// pull out old data to make room for new read
// if needed and throw it out
if(kfifo_is_full(&mpu_reports)) {
result = kfifo_get(&mpu_reports, &mpu_report);
}
// change this to be a dma transfer
read_multiple_regs(spi, MPUREG_INT_STATUS, ((uint8_t *)&mpu_raw_report), sizeof(mpu_raw_report));
do_gettimeofday(&mpu_report.timestamp);
mpu_report.accel_x_raw = swap_uint16_t(mpu_raw_report.accel_x);
mpu_report.accel_y_raw = swap_uint16_t(mpu_raw_report.accel_y);
mpu_report.accel_z_raw = swap_uint16_t(mpu_raw_report.accel_z);
mpu_report.temperature_raw = swap_uint16_t(mpu_raw_report.temp);
mpu_report.gyro_x_raw = swap_uint16_t(mpu_raw_report.gyro_x);
mpu_report.gyro_y_raw = swap_uint16_t(mpu_raw_report.gyro_y);
mpu_report.gyro_z_raw = swap_uint16_t(mpu_raw_report.gyro_z);
kfifo_put(&mpu_reports, &mpu_report);
}
return 0;
}
module_init(mpu_init);
static void exit_mpu(void) {
spi_unregister_driver(&imu_driver);
}
module_exit(exit_mpu);
MODULE_DESCRIPTION("Driver for MPU 6000");
MODULE_AUTHOR("Jimmy Johnson");
MODULE_LICENSE("GPL");
MODULE_ALIAS("imu");