Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

右と左でそれぞれ用意されている関数を一つにまとめる(リファクタリング) #93

Merged
merged 6 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/drivers/rtmouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
// Raspberry Pi 4 B : 4
#define RASPBERRYPI 2

#define DEV_RIGHT 0
#define DEV_LEFT 1

/* --- Device ID --- */
#define ID_DEV_LED 0
#define ID_DEV_SWITCH 1
Expand Down
95 changes: 40 additions & 55 deletions src/drivers/rtmouse_dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,10 @@ static int getPWMCount(int freq)
}

/*
* left motor function
* motor function
* called by parseMotorCmd() and rawmotor_l_write()
*/
static void set_motor_l_freq(int freq)
static void set_motor_freq(int freq, const int dev_side)
{
int dat;

Expand All @@ -244,65 +244,50 @@ static void set_motor_l_freq(int freq)
}

if (freq == 0) {
rpi_gpio_function_set(MOTCLK_L_BASE, RPI_GPF_OUTPUT);
if (dev_side == DEV_LEFT) {
rpi_gpio_function_set(MOTCLK_L_BASE, RPI_GPF_OUTPUT);
} else if (dev_side == DEV_RIGHT) {
rpi_gpio_function_set(MOTCLK_R_BASE, RPI_GPF_OUTPUT);
}
return;
} else {
rpi_gpio_function_set(MOTCLK_L_BASE, RPI_GPF_ALT0);
if (dev_side == DEV_LEFT) {
rpi_gpio_function_set(MOTCLK_L_BASE, RPI_GPF_ALT0);
} else if (dev_side == DEV_RIGHT) {
rpi_gpio_function_set(MOTCLK_R_BASE, RPI_GPF_ALT0);
}
}

if (freq > 0) {
motor_l_freq_is_positive = 1;
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << MOTDIR_L_BASE);
if (dev_side == DEV_LEFT) {
motor_l_freq_is_positive = 1;
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << MOTDIR_L_BASE);
} else if (dev_side == DEV_RIGHT) {
motor_r_freq_is_positive = 1;
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << MOTDIR_R_BASE);
}
} else {
motor_l_freq_is_positive = 0;
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << MOTDIR_L_BASE);
freq = -freq;
if (dev_side == DEV_LEFT) {
motor_l_freq_is_positive = 0;
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << MOTDIR_L_BASE);
freq = -freq;
} else if (dev_side == DEV_RIGHT) {
motor_r_freq_is_positive = 0;
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << MOTDIR_R_BASE);
freq = -freq;
}
}

dat = getPWMCount(freq);

rpi_pwm_write32(RPI_PWM_RNG1, dat);
rpi_pwm_write32(RPI_PWM_DAT1, dat >> 1);

return;
}

/*
* right motor function
* called by parseMotorCmd() and rawmotor_r_write()
*/
static void set_motor_r_freq(int freq)
{
int dat;

rpi_gpio_function_set(BUZZER_BASE, RPI_GPF_OUTPUT);

// Reset uncontrollable frequency to zero.
if (abs(freq) < MOTOR_UNCONTROLLABLE_FREQ) {
freq = 0;
}

if (freq == 0) {
rpi_gpio_function_set(MOTCLK_R_BASE, RPI_GPF_OUTPUT);
return;
} else {
rpi_gpio_function_set(MOTCLK_R_BASE, RPI_GPF_ALT0);
}

if (freq > 0) {
motor_r_freq_is_positive = 1;
rpi_gpio_set32(RPI_GPIO_P2MASK, 1 << MOTDIR_R_BASE);
} else {
motor_r_freq_is_positive = 0;
rpi_gpio_clear32(RPI_GPIO_P2MASK, 1 << MOTDIR_R_BASE);
freq = -freq;
if (dev_side == DEV_LEFT) {
rpi_pwm_write32(RPI_PWM_RNG1, dat);
rpi_pwm_write32(RPI_PWM_DAT1, dat >> 1);
} else if (dev_side == DEV_RIGHT) {
rpi_pwm_write32(RPI_PWM_RNG2, dat);
rpi_pwm_write32(RPI_PWM_DAT2, dat >> 1);
}

dat = getPWMCount(freq);

rpi_pwm_write32(RPI_PWM_RNG2, dat);
rpi_pwm_write32(RPI_PWM_DAT2, dat >> 1);

return;
}

Expand All @@ -326,13 +311,13 @@ static int parseMotorCmd(const char __user *buf, size_t count, int *ret)

mutex_lock(&lock);

set_motor_l_freq(l_motor_val);
set_motor_r_freq(r_motor_val);
set_motor_freq(l_motor_val, DEV_LEFT);
set_motor_freq(r_motor_val, DEV_RIGHT);

msleep_interruptible(time_val);

set_motor_l_freq(0);
set_motor_r_freq(0);
set_motor_freq(0, DEV_LEFT);
set_motor_freq(0, DEV_RIGHT);

mutex_unlock(&lock);

Expand Down Expand Up @@ -751,7 +736,7 @@ static ssize_t rawmotor_l_write(struct file *filep, const char __user *buf,
DRIVER_NAME, __func__);
return ret;
}
set_motor_l_freq(freq);
set_motor_freq(freq, DEV_LEFT);

return count;
}
Expand All @@ -772,7 +757,7 @@ static ssize_t rawmotor_r_write(struct file *filep, const char __user *buf,
return ret;
}

set_motor_r_freq(freq);
set_motor_freq(freq, DEV_RIGHT);

return count;
}
Expand Down
117 changes: 36 additions & 81 deletions src/drivers/rtmouse_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,86 +68,23 @@ static struct i2c_driver i2c_counter_driver = {
/* -- Device Addition -- */
MODULE_DEVICE_TABLE(i2c, i2c_counter_id);

static int rtcntr_i2c_create_cdev(struct rtcnt_device_info *dev_info)
{
int minor;
int alloc_ret = 0;
int cdev_err = 0;
dev_t dev;

/* 空いているメジャー番号を確保する */
alloc_ret = alloc_chrdev_region(&dev, DEV_MINOR, NUM_DEV[ID_DEV_CNT],
DEVNAME_CNTR);
if (alloc_ret != 0) {
printk(KERN_ERR "alloc_chrdev_region = %d\n", alloc_ret);
return -1;
}

/* 取得したdev( = メジャー番号 + マイナー番号)
* からメジャー番号を取得して保持しておく */
dev_info->device_major = MAJOR(dev);
dev = MKDEV(dev_info->device_major, DEV_MINOR);

/* cdev構造体の初期化とシステムコールハンドラテーブルの登録 */
cdev_init(&dev_info->cdev, &dev_fops[ID_DEV_CNT]);
dev_info->cdev.owner = THIS_MODULE;

/* このデバイスドライバ(cdev)をカーネルに登録する */
cdev_err = cdev_add(&dev_info->cdev, dev, NUM_DEV[ID_DEV_CNT]);
if (cdev_err != 0) {
printk(KERN_ERR "cdev_add = %d\n", alloc_ret);
unregister_chrdev_region(dev, NUM_DEV[ID_DEV_CNT]);
return -1;
}

/* このデバイスのクラス登録をする(/sys/class/mydevice/ を作る) */
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0)
dev_info->device_class = class_create(THIS_MODULE, DEVNAME_CNTR);
#else
dev_info->device_class = class_create(DEVNAME_CNTR);
#endif

if (IS_ERR(dev_info->device_class)) {
printk(KERN_ERR "class_create\n");
cdev_del(&dev_info->cdev);
unregister_chrdev_region(dev, NUM_DEV[ID_DEV_CNT]);
return -1;
}

for (minor = DEV_MINOR; minor < DEV_MINOR + NUM_DEV[ID_DEV_CNT];
minor++) {

struct device *dev_ret;
dev_ret = device_create(dev_info->device_class, NULL,
MKDEV(dev_info->device_major, minor),
NULL, "rtcounter_r%d", minor);

/* デバイスファイル作成の可否を判定 */
if (IS_ERR(dev_ret)) {
/* デバイスファイルの作成に失敗した */
printk(KERN_ERR "device_create failed minor = %d\n",
minor);
/* リソースリークを避けるために登録された状態cdevを削除する
*/
cdev_del(&(cdev_array[cdev_index]));
return PTR_ERR(dev_ret);
}
}

return 0;
}

// called by rtcnt_i2c_probe()
static int rtcntl_i2c_create_cdev(struct rtcnt_device_info *dev_info)
static int rtcnt_i2c_create_cdev(struct rtcnt_device_info *dev_info,
const int dev_side)
{
int minor;
int alloc_ret = 0;
int cdev_err = 0;
dev_t dev;

/* 空いているメジャー番号を確保する */
alloc_ret = alloc_chrdev_region(&dev, DEV_MINOR, NUM_DEV[ID_DEV_CNT],
DEVNAME_CNTL);
if (dev_side == DEV_LEFT) {
alloc_ret = alloc_chrdev_region(
&dev, DEV_MINOR, NUM_DEV[ID_DEV_CNT], DEVNAME_CNTL);
} else if (dev_side == DEV_RIGHT) {
alloc_ret = alloc_chrdev_region(
&dev, DEV_MINOR, NUM_DEV[ID_DEV_CNT], DEVNAME_CNTR);
}
if (alloc_ret != 0) {
printk(KERN_ERR "alloc_chrdev_region = %d\n", alloc_ret);
return -1;
Expand All @@ -172,9 +109,19 @@ static int rtcntl_i2c_create_cdev(struct rtcnt_device_info *dev_info)

/* このデバイスのクラス登録をする(/sys/class/mydevice/ を作る) */
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0)
dev_info->device_class = class_create(THIS_MODULE, DEVNAME_CNTL);
if (dev_side == DEV_LEFT) {
dev_info->device_class =
class_create(THIS_MODULE, DEVNAME_CNTL);
} else if (dev_side == DEV_RIGHT) {
dev_info->device_class =
class_create(THIS_MODULE, DEVNAME_CNTR);
}
#else
dev_info->device_class = class_create(DEVNAME_CNTL);
if (dev_side == DEV_LEFT) {
dev_info->device_class = class_create(DEVNAME_CNTL);
} else if (dev_side == DEV_RIGHT) {
dev_info->device_class = class_create(DEVNAME_CNTR);
}
#endif

if (IS_ERR(dev_info->device_class)) {
Expand All @@ -189,9 +136,17 @@ static int rtcntl_i2c_create_cdev(struct rtcnt_device_info *dev_info)
minor++) {

struct device *dev_ret;
dev_ret = device_create(dev_info->device_class, NULL,
MKDEV(dev_info->device_major, minor),
NULL, "rtcounter_l%d", minor);
if (dev_side == DEV_LEFT) {
dev_ret =
device_create(dev_info->device_class, NULL,
MKDEV(dev_info->device_major, minor),
NULL, "rtcounter_l%d", minor);
} else if (dev_side == DEV_RIGHT) {
dev_ret =
device_create(dev_info->device_class, NULL,
MKDEV(dev_info->device_major, minor),
NULL, "rtcounter_r%d", minor);
}

/* デバイスファイル作成の可否を判定 */
if (IS_ERR(dev_ret)) {
Expand Down Expand Up @@ -260,10 +215,10 @@ static int rtcnt_i2c_probe(struct i2c_client *client,

/* create character device */
if ((int)(id->driver_data) == 0) {
if (rtcntl_i2c_create_cdev(dev_info))
if (rtcnt_i2c_create_cdev(dev_info, DEV_LEFT))
return -ENOMEM;
} else if ((int)(id->driver_data) == 1) {
if (rtcntr_i2c_create_cdev(dev_info))
if (rtcnt_i2c_create_cdev(dev_info, DEV_RIGHT))
return -ENOMEM;
}

Expand Down Expand Up @@ -301,10 +256,10 @@ static int rtcnt_i2c_probe(struct i2c_client *client)

/* create character device */
if ((int)(id->driver_data) == 0) {
if (rtcntl_i2c_create_cdev(dev_info))
if (rtcnt_i2c_create_cdev(dev_info, DEV_LEFT))
return -ENOMEM;
} else if ((int)(id->driver_data) == 1) {
if (rtcntr_i2c_create_cdev(dev_info))
if (rtcnt_i2c_create_cdev(dev_info, DEV_RIGHT))
return -ENOMEM;
}

Expand Down
4 changes: 2 additions & 2 deletions src/drivers/rtmouse_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* rtmouse_main.c
* Raspberry Pi Mouse device driver
*
* Version: 3.3.3
* Version: 3.3.4
*
* Copyright (C) 2015-2024 RT Corporation <[email protected]>
*
Expand All @@ -26,7 +26,7 @@

MODULE_AUTHOR("RT Corporation");
MODULE_LICENSE("GPL");
MODULE_VERSION("3.3.3");
MODULE_VERSION("3.3.4");
MODULE_DESCRIPTION("Raspberry Pi Mouse device driver");

/*
Expand Down