Skip to content

Commit dd03434

Browse files
Shota AokiTiryoh
andauthored
Add signed counter (rtcounter_l1, rtcounter_r1) (#35)
* Use DEV_ADDR_CNT params * Add minor devices to rtcounter * Add global variables : motor_freq_is_positive * read device_minor value in i2c_counter_read function * Add set_signed_count function * Add reset_signed_count function * Rename set_signed_count to update_signed_count * Bump up version Co-authored-by: Daisuke Sato <daisuke.sato@rt-net.jp>
1 parent cae8fde commit dd03434

File tree

1 file changed

+88
-8
lines changed

1 file changed

+88
-8
lines changed

src/drivers/rtmouse.c

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* rtmouse.c
44
* Raspberry Pi Mouse device driver
55
*
6-
* Version: 2.0.0
6+
* Version: 2.1.0
77
*
88
* Copyright (C) 2015-2020 RT Corporation <shop@rt-net.jp>
99
*
@@ -55,7 +55,7 @@
5555

5656
MODULE_AUTHOR("RT Corporation");
5757
MODULE_LICENSE("GPL");
58-
MODULE_VERSION("2.0.0");
58+
MODULE_VERSION("2.1.0");
5959
MODULE_DESCRIPTION("Raspberry Pi Mouse device driver");
6060

6161
/* --- Device Numbers --- */
@@ -67,7 +67,7 @@ MODULE_DESCRIPTION("Raspberry Pi Mouse device driver");
6767
#define NUM_DEV_MOTORRAWL 1
6868
#define NUM_DEV_MOTOREN 1
6969
#define NUM_DEV_MOTOR 1
70-
#define NUM_DEV_CNT 1
70+
#define NUM_DEV_CNT 2
7171

7272
#define NUM_DEV_TOTAL \
7373
(NUM_DEV_LED + NUM_DEV_SWITCH + NUM_DEV_BUZZER + NUM_DEV_MOTORRAWR + \
@@ -267,8 +267,8 @@ static struct mutex lock;
267267
#define MCP3204_CHANNELS 4
268268

269269
/* 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
272272
#define CNT_ADDR_MSB 0x10
273273
#define CNT_ADDR_LSB 0x11
274274

@@ -323,13 +323,20 @@ static struct spi_driver mcp3204_driver = {
323323
struct rtcnt_device_info {
324324
struct cdev cdev;
325325
unsigned int device_major;
326+
unsigned int device_minor;
326327
struct class *device_class;
327328
struct i2c_client *client;
328329
struct mutex lock;
330+
int signed_pulse_count;
331+
int raw_pulse_count;
329332
};
330333

331334
static struct i2c_client *i2c_client_r = NULL;
332335
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
333340

334341
/* I2C Device ID */
335342
static struct i2c_device_id i2c_counter_id[] = {
@@ -468,8 +475,10 @@ static void set_motor_l_freq(int freq)
468475
}
469476

470477
if (freq > 0) {
478+
motor_l_freq_is_positive = 1;
471479
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << MOTDIR_L_BASE);
472480
} else {
481+
motor_l_freq_is_positive = 0;
473482
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << MOTDIR_L_BASE);
474483
freq = -freq;
475484
}
@@ -497,8 +506,10 @@ static void set_motor_r_freq(int freq)
497506
}
498507

499508
if (freq > 0) {
509+
motor_r_freq_is_positive = 1;
500510
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << MOTDIR_R_BASE);
501511
} else {
512+
motor_r_freq_is_positive = 0;
502513
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << MOTDIR_R_BASE);
503514
freq = -freq;
504515
}
@@ -813,6 +824,7 @@ static int i2c_dev_open(struct inode *inode, struct file *filep)
813824
if (dev_info == NULL || dev_info->client == NULL) {
814825
printk(KERN_ERR "%s: i2c dev_open failed.\n", DRIVER_NAME);
815826
}
827+
dev_info->device_minor = MINOR(inode->i_rdev);
816828
filep->private_data = dev_info;
817829
return 0;
818830
}
@@ -1160,6 +1172,62 @@ static int i2c_counter_read(struct rtcnt_device_info *dev_info, int *ret)
11601172
return 0;
11611173
}
11621174

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+
11631231
/*
11641232
* rtcnt_read - Read value from right/left pulse counter
11651233
* Read function of /dev/rtcounter_*
@@ -1177,6 +1245,14 @@ static ssize_t rtcnt_read(struct file *filep, char __user *buf, size_t count,
11771245
return 0; /* close device */
11781246
i2c_counter_read(dev_info, &rtcnt_count);
11791247

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+
11801256
/* set sensor data to rw_buf(static buffer) */
11811257
sprintf(rw_buf, "%d\n", rtcnt_count);
11821258
buflen = strlen(rw_buf);
@@ -1212,6 +1288,10 @@ static ssize_t rtcnt_write(struct file *filep, const char __user *buf,
12121288

12131289
i2c_counter_set(dev_info, rtcnt_count);
12141290

1291+
if(dev_info->device_minor == 1){
1292+
reset_signed_count(dev_info, rtcnt_count);
1293+
}
1294+
12151295
printk(KERN_INFO "%s: set pulse counter value %d\n", DRIVER_NAME,
12161296
rtcnt_count);
12171297
return bufcnt;
@@ -1963,9 +2043,9 @@ static int i2c_counter_init(void)
19632043
struct i2c_adapter *i2c_adap_l;
19642044
struct i2c_adapter *i2c_adap_r;
19652045
struct i2c_board_info i2c_board_info_l = {
1966-
I2C_BOARD_INFO(DEVNAME_CNTL, 0x10)};
2046+
I2C_BOARD_INFO(DEVNAME_CNTL, DEV_ADDR_CNTL)};
19672047
struct i2c_board_info i2c_board_info_r = {
1968-
I2C_BOARD_INFO(DEVNAME_CNTR, 0x11)};
2048+
I2C_BOARD_INFO(DEVNAME_CNTR, DEV_ADDR_CNTR)};
19692049

19702050
// printk(KERN_DEBUG "%s: initializing i2c device", __func__);
19712051
retval = i2c_add_driver(&i2c_counter_driver);
@@ -2059,7 +2139,7 @@ int dev_init_module(void)
20592139

20602140
retval = i2c_counter_init();
20612141
if (retval == 0) {
2062-
registered_devices += 2;
2142+
registered_devices += 2 * NUM_DEV_CNT;
20632143
}
20642144
else{
20652145
printk(KERN_ALERT

0 commit comments

Comments
 (0)