3
3
* rtmouse.c
4
4
* Raspberry Pi Mouse device driver
5
5
*
6
- * Version: 2.0 .0
6
+ * Version: 2.1 .0
7
7
*
8
8
* Copyright (C) 2015-2020 RT Corporation <shop@rt-net.jp>
9
9
*
55
55
56
56
MODULE_AUTHOR ("RT Corporation" );
57
57
MODULE_LICENSE ("GPL" );
58
- MODULE_VERSION ("2.0 .0" );
58
+ MODULE_VERSION ("2.1 .0" );
59
59
MODULE_DESCRIPTION ("Raspberry Pi Mouse device driver" );
60
60
61
61
/* --- Device Numbers --- */
@@ -67,7 +67,7 @@ MODULE_DESCRIPTION("Raspberry Pi Mouse device driver");
67
67
#define NUM_DEV_MOTORRAWL 1
68
68
#define NUM_DEV_MOTOREN 1
69
69
#define NUM_DEV_MOTOR 1
70
- #define NUM_DEV_CNT 1
70
+ #define NUM_DEV_CNT 2
71
71
72
72
#define NUM_DEV_TOTAL \
73
73
(NUM_DEV_LED + NUM_DEV_SWITCH + NUM_DEV_BUZZER + NUM_DEV_MOTORRAWR + \
@@ -267,8 +267,8 @@ static struct mutex lock;
267
267
#define MCP3204_CHANNELS 4
268
268
269
269
/* I2C Parameter */
270
- #define DEV_ADDR_CNTR 0x10
271
- #define DEV_ADDR_CNTL 0x11
270
+ #define DEV_ADDR_CNTL 0x10
271
+ #define DEV_ADDR_CNTR 0x11
272
272
#define CNT_ADDR_MSB 0x10
273
273
#define CNT_ADDR_LSB 0x11
274
274
@@ -323,13 +323,20 @@ static struct spi_driver mcp3204_driver = {
323
323
struct rtcnt_device_info {
324
324
struct cdev cdev ;
325
325
unsigned int device_major ;
326
+ unsigned int device_minor ;
326
327
struct class * device_class ;
327
328
struct i2c_client * client ;
328
329
struct mutex lock ;
330
+ int signed_pulse_count ;
331
+ int raw_pulse_count ;
329
332
};
330
333
331
334
static struct i2c_client * i2c_client_r = NULL ;
332
335
static struct i2c_client * i2c_client_l = NULL ;
336
+ static unsigned int motor_l_freq_is_positive = 1 ;
337
+ static unsigned int motor_r_freq_is_positive = 1 ;
338
+ #define SIGNED_COUNT_SIZE 32767
339
+ #define MAX_PULSE_COUNT 65535
333
340
334
341
/* I2C Device ID */
335
342
static struct i2c_device_id i2c_counter_id [] = {
@@ -468,8 +475,10 @@ static void set_motor_l_freq(int freq)
468
475
}
469
476
470
477
if (freq > 0 ) {
478
+ motor_l_freq_is_positive = 1 ;
471
479
rpi_gpio_clear32 (RPI_GPIO_P2MASK , 1 << MOTDIR_L_BASE );
472
480
} else {
481
+ motor_l_freq_is_positive = 0 ;
473
482
rpi_gpio_set32 (RPI_GPIO_P2MASK , 1 << MOTDIR_L_BASE );
474
483
freq = - freq ;
475
484
}
@@ -497,8 +506,10 @@ static void set_motor_r_freq(int freq)
497
506
}
498
507
499
508
if (freq > 0 ) {
509
+ motor_r_freq_is_positive = 1 ;
500
510
rpi_gpio_set32 (RPI_GPIO_P2MASK , 1 << MOTDIR_R_BASE );
501
511
} else {
512
+ motor_r_freq_is_positive = 0 ;
502
513
rpi_gpio_clear32 (RPI_GPIO_P2MASK , 1 << MOTDIR_R_BASE );
503
514
freq = - freq ;
504
515
}
@@ -813,6 +824,7 @@ static int i2c_dev_open(struct inode *inode, struct file *filep)
813
824
if (dev_info == NULL || dev_info -> client == NULL ) {
814
825
printk (KERN_ERR "%s: i2c dev_open failed.\n" , DRIVER_NAME );
815
826
}
827
+ dev_info -> device_minor = MINOR (inode -> i_rdev );
816
828
filep -> private_data = dev_info ;
817
829
return 0 ;
818
830
}
@@ -1160,6 +1172,62 @@ static int i2c_counter_read(struct rtcnt_device_info *dev_info, int *ret)
1160
1172
return 0 ;
1161
1173
}
1162
1174
1175
+ /*
1176
+ * update_signed_count - update signed pulse count of dev_info
1177
+ * called by rtcnt_read()
1178
+ */
1179
+ void update_signed_count (struct rtcnt_device_info * dev_info ,
1180
+ int rtcnt_count )
1181
+ {
1182
+ int diff_count = rtcnt_count - dev_info -> raw_pulse_count ;
1183
+
1184
+ // カウントがMAX_PULSE_COUNTから0に変わる場合の処理
1185
+ // ただし、それ以外でもdiffが負の値になることがある
1186
+ // そのため、diffが十分に大きな負の値の場合に処理する
1187
+ // if(diff_count < 0) では正常に動作しない
1188
+ if (diff_count < - SIGNED_COUNT_SIZE ){
1189
+ diff_count += MAX_PULSE_COUNT ;
1190
+ }
1191
+
1192
+ if (dev_info -> client -> addr == DEV_ADDR_CNTL ){
1193
+ if (motor_l_freq_is_positive ){
1194
+ dev_info -> signed_pulse_count += diff_count ;
1195
+ }else {
1196
+ dev_info -> signed_pulse_count -= diff_count ;
1197
+ }
1198
+ }else {
1199
+ if (motor_r_freq_is_positive ){
1200
+ dev_info -> signed_pulse_count += diff_count ;
1201
+ }else {
1202
+ dev_info -> signed_pulse_count -= diff_count ;
1203
+ }
1204
+ }
1205
+
1206
+ if (dev_info -> signed_pulse_count > SIGNED_COUNT_SIZE ||
1207
+ dev_info -> signed_pulse_count < - SIGNED_COUNT_SIZE ){
1208
+ dev_info -> signed_pulse_count = 0 ;
1209
+ }
1210
+ }
1211
+
1212
+ /*
1213
+ * reset_signed_count - reset signed pulse count of dev_info
1214
+ * called by rtcnt_write()
1215
+ */
1216
+ void reset_signed_count (struct rtcnt_device_info * dev_info ,
1217
+ int rtcnt_count )
1218
+ {
1219
+ int raw_count ;
1220
+
1221
+ if (rtcnt_count > SIGNED_COUNT_SIZE ){
1222
+ rtcnt_count = SIGNED_COUNT_SIZE ;
1223
+ }else if (rtcnt_count < - SIGNED_COUNT_SIZE ){
1224
+ rtcnt_count = - SIGNED_COUNT_SIZE ;
1225
+ }
1226
+ dev_info -> signed_pulse_count = rtcnt_count ;
1227
+ i2c_counter_read (dev_info , & raw_count );
1228
+ dev_info -> raw_pulse_count = raw_count ;
1229
+ }
1230
+
1163
1231
/*
1164
1232
* rtcnt_read - Read value from right/left pulse counter
1165
1233
* Read function of /dev/rtcounter_*
@@ -1177,6 +1245,14 @@ static ssize_t rtcnt_read(struct file *filep, char __user *buf, size_t count,
1177
1245
return 0 ; /* close device */
1178
1246
i2c_counter_read (dev_info , & rtcnt_count );
1179
1247
1248
+ if (dev_info -> device_minor == 1 ){
1249
+ update_signed_count (dev_info , rtcnt_count );
1250
+ dev_info -> raw_pulse_count = rtcnt_count ;
1251
+ rtcnt_count = dev_info -> signed_pulse_count ;
1252
+ }else {
1253
+ dev_info -> raw_pulse_count = rtcnt_count ;
1254
+ }
1255
+
1180
1256
/* set sensor data to rw_buf(static buffer) */
1181
1257
sprintf (rw_buf , "%d\n" , rtcnt_count );
1182
1258
buflen = strlen (rw_buf );
@@ -1212,6 +1288,10 @@ static ssize_t rtcnt_write(struct file *filep, const char __user *buf,
1212
1288
1213
1289
i2c_counter_set (dev_info , rtcnt_count );
1214
1290
1291
+ if (dev_info -> device_minor == 1 ){
1292
+ reset_signed_count (dev_info , rtcnt_count );
1293
+ }
1294
+
1215
1295
printk (KERN_INFO "%s: set pulse counter value %d\n" , DRIVER_NAME ,
1216
1296
rtcnt_count );
1217
1297
return bufcnt ;
@@ -1963,9 +2043,9 @@ static int i2c_counter_init(void)
1963
2043
struct i2c_adapter * i2c_adap_l ;
1964
2044
struct i2c_adapter * i2c_adap_r ;
1965
2045
struct i2c_board_info i2c_board_info_l = {
1966
- I2C_BOARD_INFO (DEVNAME_CNTL , 0x10 )};
2046
+ I2C_BOARD_INFO (DEVNAME_CNTL , DEV_ADDR_CNTL )};
1967
2047
struct i2c_board_info i2c_board_info_r = {
1968
- I2C_BOARD_INFO (DEVNAME_CNTR , 0x11 )};
2048
+ I2C_BOARD_INFO (DEVNAME_CNTR , DEV_ADDR_CNTR )};
1969
2049
1970
2050
// printk(KERN_DEBUG "%s: initializing i2c device", __func__);
1971
2051
retval = i2c_add_driver (& i2c_counter_driver );
@@ -2059,7 +2139,7 @@ int dev_init_module(void)
2059
2139
2060
2140
retval = i2c_counter_init ();
2061
2141
if (retval == 0 ) {
2062
- registered_devices += 2 ;
2142
+ registered_devices += 2 * NUM_DEV_CNT ;
2063
2143
}
2064
2144
else {
2065
2145
printk (KERN_ALERT
0 commit comments