Skip to content

Commit

Permalink
ヘッダファイルrtmouse.hを追加(リファクタリング) (#91)
Browse files Browse the repository at this point in the history
Co-authored-by: Kazushi Kurasawa <[email protected]>
  • Loading branch information
YusukeKato and KuraZuzu authored Oct 29, 2024
1 parent 8e920c2 commit 43532e9
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 298 deletions.
3 changes: 2 additions & 1 deletion .test/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SRC_DIR=$(cd $(dirname ${BASH_SOURCE:-$0}); cd ../; pwd)
lint_driver () {
pushd $SRC_DIR/src/drivers
python3 $SRC_DIR/.test/bin/run-clang-format.py rtmouse.c
python3 $SRC_DIR/.test/bin/run-clang-format.py rtmouse.h
popd
}

Expand All @@ -25,4 +26,4 @@ check_driver_version () {


lint_driver
check_driver_version
check_driver_version
4 changes: 2 additions & 2 deletions src/drivers/Makefile.header_from_apt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ clean-files:= *.o *.ko *.mod.[co] *~
LINUX_SRC_DIR:=/usr/src/linux-headers-$(shell uname -r)
VERBOSE:=0

rtmouse.ko: rtmouse.c
rtmouse.ko: rtmouse.c rtmouse.h
make -C $(LINUX_SRC_DIR) M=$(shell pwd) V=$(VERBOSE) modules

clean:
Expand All @@ -17,4 +17,4 @@ install: rtmouse.ko
uninstall:
rm /etc/udev/rules.d/50-rtmouse.rules

#Reference: http://www.devdrv.co.jp/linux/kernel26-makefile.htm
#Reference: http://www.devdrv.co.jp/linux/kernel26-makefile.htm
4 changes: 2 additions & 2 deletions src/drivers/Makefile.header_from_source
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ clean-files:= *.o *.ko *.mod.[co] *~
LINUX_SRC_DIR:=/usr/src/linux
VERBOSE:=0

rtmouse.ko: rtmouse.c
rtmouse.ko: rtmouse.c rtmouse.h
make -C $(LINUX_SRC_DIR) M=$(shell pwd) V=$(VERBOSE) modules

clean:
make -C $(LINUX_SRC_DIR) M=$(shell pwd) V=$(VERBOSE) clean
make -C $(LINUX_SRC_DIR) M=$(shell pwd) V=$(VERBOSE) clean
297 changes: 6 additions & 291 deletions src/drivers/rtmouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* rtmouse.c
* Raspberry Pi Mouse device driver
*
* Version: 3.3.1
* Version: 3.3.2
*
* Copyright (C) 2015-2021 RT Corporation <[email protected]>
* Copyright (C) 2015-2024 RT Corporation <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -22,100 +22,13 @@
* MA 02110-1301, USA.
*/

#include <linux/cdev.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/i2c.h>
#include <linux/iio/iio.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/ioport.h>
#include <linux/kdev_t.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/spi/spi.h>
#include <linux/stat.h>
#include <linux/timer.h>
#include <linux/types.h>
#include <linux/uaccess.h>
#include <linux/version.h>

// define the Raspberry Pi version here
// Raspberry Pi 1 B/A/B+/A+: 1
// Raspberry Pi 2 B : 2
// Raspberry Pi 3 B/A+/B+ : 2
// Raspberry Pi 4 B : 4
#define RASPBERRYPI 2
#include "rtmouse.h"

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

/* --- Device ID --- */
#define ID_DEV_LED 0
#define ID_DEV_SWITCH 1
#define ID_DEV_SENSOR 2
#define ID_DEV_BUZZER 3
#define ID_DEV_MOTORRAWR 4
#define ID_DEV_MOTORRAWL 5
#define ID_DEV_MOTOREN 6
#define ID_DEV_MOTOR 7
#define ID_DEV_CNT 8
#define ID_DEV_SIZE 9

/* --- Device Numbers --- */
static const unsigned int NUM_DEV[ID_DEV_SIZE] = {
[ID_DEV_LED] = 4, [ID_DEV_SWITCH] = 3, [ID_DEV_SENSOR] = 1,
[ID_DEV_BUZZER] = 1, [ID_DEV_MOTORRAWR] = 1, [ID_DEV_MOTORRAWL] = 1,
[ID_DEV_MOTOREN] = 1, [ID_DEV_MOTOR] = 1, [ID_DEV_CNT] = 2};

#define NUM_DEV_TOTAL \
(NUM_DEV[ID_DEV_LED] + NUM_DEV[ID_DEV_SWITCH] + \
NUM_DEV[ID_DEV_SENSOR] + NUM_DEV[ID_DEV_BUZZER] + \
NUM_DEV[ID_DEV_MOTORRAWR] + NUM_DEV[ID_DEV_MOTORRAWL] + \
NUM_DEV[ID_DEV_MOTOREN] + NUM_DEV[ID_DEV_MOTOR])

/* --- Device Names --- */
static const char *NAME_DEV[ID_DEV_SIZE] = {
[ID_DEV_LED] = "rtled",
[ID_DEV_SWITCH] = "rtswitch",
[ID_DEV_SENSOR] = "rtlightsensor",
[ID_DEV_BUZZER] = "rtbuzzer",
[ID_DEV_MOTORRAWR] = "rtmotor_raw_r",
[ID_DEV_MOTORRAWL] = "rtmotor_raw_l",
[ID_DEV_MOTOREN] = "rtmotoren",
[ID_DEV_MOTOR] = "rtmotor"};

static const char *NAME_DEV_U[ID_DEV_SIZE] = {
[ID_DEV_LED] = "rtled%u",
[ID_DEV_SWITCH] = "rtswitch%u",
[ID_DEV_SENSOR] = "rtlightsensor%u",
[ID_DEV_BUZZER] = "rtbuzzer%u",
[ID_DEV_MOTORRAWR] = "rtmotor_raw_r%u",
[ID_DEV_MOTORRAWL] = "rtmotor_raw_l%u",
[ID_DEV_MOTOREN] = "rtmotoren%u",
[ID_DEV_MOTOR] = "rtmotor%u"};

#define DEVNAME_SENSOR "rtlightsensor"
#define DEVNAME_CNTR "rtcounter_r"
#define DEVNAME_CNTL "rtcounter_l"

#define DRIVER_NAME "rtmouse"

/* --- Device Major and Minor Numbers --- */
#define DEV_MAJOR 0
#define DEV_MINOR 0

static int spi_bus_num = 0;
static int spi_chip_select = 0;

static int _major_dev[ID_DEV_SIZE] = {
[ID_DEV_LED] = DEV_MAJOR, [ID_DEV_SWITCH] = DEV_MAJOR,
[ID_DEV_SENSOR] = DEV_MAJOR, [ID_DEV_BUZZER] = DEV_MAJOR,
Expand Down Expand Up @@ -144,143 +57,6 @@ static volatile int cdev_index = 0;

static struct mutex lock;

/* --- GPIO Pin Definitions --- */
#define R_AD_CH 3
#define L_AD_CH 0
#define RF_AD_CH 2
#define LF_AD_CH 1

#define R_LED_BASE 22
#define L_LED_BASE 4
#define RF_LED_BASE 27
#define LF_LED_BASE 17

#define LED0_BASE 25
#define LED1_BASE 24
#define LED2_BASE 23
#define LED3_BASE 18

#define SW1_PIN 20
#define SW2_PIN 26
#define SW3_PIN 21

#define BUZZER_BASE 19

#define MOTCLK_L_BASE 12
#define MOTDIR_L_BASE 16

#define MOTCLK_R_BASE 13
#define MOTDIR_R_BASE 6

#define MOTEN_BASE 5

#define PWM_ORG0_BASE 40
#define PWM_ORG1_BASE 45

/* --- Register Address --- */
/* Base Addr */
#if RASPBERRYPI == 1
#define RPI_REG_BASE 0x20000000
#elif RASPBERRYPI == 2
#define RPI_REG_BASE 0x3f000000
#elif RASPBERRYPI == 4
#define RPI_REG_BASE 0xfe000000
/* 2711 has a different mechanism for pin pull-up/down/enable */
#define GPPUPPDN0 57 /* Pin pull-up/down for pins 15:0 */
#define GPPUPPDN1 58 /* Pin pull-up/down for pins 31:16 */
#define GPPUPPDN2 59 /* Pin pull-up/down for pins 47:32 */
#define GPPUPPDN3 60 /* Pin pull-up/down for pins 57:48 */
#endif

/* GPIO Addr */
#define RPI_GPIO_OFFSET 0x200000
#define RPI_GPIO_SIZE 0xC0
#define RPI_GPIO_BASE (RPI_REG_BASE + RPI_GPIO_OFFSET)
#define REG_GPIO_NAME "RPi mouse GPIO"

/* Pwm Addr */
#define RPI_PWM_OFFSET 0x20C000
#define RPI_PWM_SIZE 0xC0
#define RPI_PWM_BASE (RPI_REG_BASE + RPI_PWM_OFFSET)
#define REG_PWM_NAME "RPi mouse PWM"

/* Clock Addr */
#define RPI_CLK_OFFSET 0x101000
#define RPI_CLK_SIZE 0x100
#define RPI_CLK_BASE (RPI_REG_BASE + RPI_CLK_OFFSET)
#define REG_CLK_NAME "RPi mouse CLK"

/* --- General Options --- */
/* Clock Offset */
#define CLK_PWM_INDEX 0xa0
#define CLK_PWMDIV_INDEX 0xa4

/* GPIO PUPD select */
#if RASPBERRYPI == 4
#define GPIO_PULLNONE 0x0
#define GPIO_PULLUP 0x1
#define GPIO_PULLDOWN 0x2
#else
#define GPIO_PULLNONE 0x0
#define GPIO_PULLDOWN 0x1
#define GPIO_PULLUP 0x2
#endif

/* GPIO Function */
#define RPI_GPF_INPUT 0x00
#define RPI_GPF_OUTPUT 0x01
#define RPI_GPF_ALT0 0x04
#define RPI_GPF_ALT5 0x02

/* GPIO Register Index */
#define RPI_GPFSEL0_INDEX 0
#define RPI_GPFSEL1_INDEX 1
#define RPI_GPFSEL2_INDEX 2
#define RPI_GPFSEL3_INDEX 3

#define RPI_GPSET0_INDEX 7
#define RPI_GPCLR0_INDEX 10

/* GPIO Mask */
#define RPI_GPIO_P1MASK \
(uint32_t)((0x01 << 2) | (0x01 << 3) | (0x01 << 4) | (0x01 << 7) | \
(0x01 << 8) | (0x01 << 9) | (0x01 << 10) | (0x01 << 11) | \
(0x01 << 14) | (0x01 << 15) | (0x01 << 17) | (0x01 << 18) | \
(0x01 << 22) | (0x01 << 23) | (0x01 << 24) | (0x01 << 25) | \
(0x01 << 27))
#define RPI_GPIO_P2MASK (uint32_t)0xffffffff

/* PWM Index */
#define RPI_PWM_CTRL 0x0
#define RPI_PWM_STA 0x4
#define RPI_PWM_DMAC 0x8
#define RPI_PWM_RNG1 0x10
#define RPI_PWM_DAT1 0x14
#define RPI_PWM_FIF1 0x18
#define RPI_PWM_RNG2 0x20
#define RPI_PWM_DAT2 0x24

#if RASPBERRYPI == 4
#define PWM_BASECLK 27000000
#else
#define PWM_BASECLK 9600000
#endif

/* A/D Parameter */
#define MCP320X_PACKET_SIZE 3
#define MCP320X_DIFF 0
#define MCP320X_SINGLE 1
#define MCP3204_CHANNELS 4

/* I2C Parameter */
#define DEV_ADDR_CNTL 0x10
#define DEV_ADDR_CNTR 0x11
#define CNT_ADDR_MSB 0x10
#define CNT_ADDR_LSB 0x11

/* Motor Parameter */
#define MOTOR_UNCONTROLLABLE_FREQ 5

/* --- Function Declarations --- */
static void set_motor_r_freq(int freq);
static void set_motor_l_freq(int freq);
Expand Down Expand Up @@ -366,8 +142,6 @@ static struct i2c_client *i2c_client_r = NULL;
static struct i2c_client *i2c_client_l = NULL;
static unsigned int motor_l_freq_is_positive = 1;
static unsigned int motor_r_freq_is_positive = 1;
#define SIGNED_COUNT_SIZE 32767
#define MAX_PULSE_COUNT 65535

/* I2C Device ID */
static struct i2c_device_id i2c_counter_id[] = {
Expand All @@ -390,67 +164,8 @@ static struct i2c_driver i2c_counter_driver = {

/* -- Device Addition -- */
MODULE_DEVICE_TABLE(spi, mcp3204_id);

MODULE_DEVICE_TABLE(i2c, i2c_counter_id);

/* -- Buffer -- */
#define MAX_BUFLEN 64
// static int buflen = 0;

#define MOTOR_MOTION 0
#if MOTOR_MOTION
/* Variable Type Definition for motor motion */
typedef struct {
signed int r_hz;
signed int l_hz;
unsigned int time;
} t_motor_motion;

#define MAX_MOTORBUFLEN 16
static t_motor_motion motor_motion[MAX_MOTORBUFLEN];
static unsigned int motor_motion_head = 0, motor_motion_tail = 0;

static int motor_motion_push(int r_hz, int l_hz, int time)
{
unsigned int next_tail = motor_motion_tail + 1;

if (next_tail >= MAX_MOTORBUFLEN) {
next_tail = 0;
}

if (next_tail == motor_motion_head) {
return -1;
}

motor_motion[motor_motion_tail].r_hz = r_hz;
motor_motion[motor_motion_tail].l_hz = l_hz;
motor_motion[motor_motion_tail].time = time;

motor_motion_tail = next_tail;

return 0;
}

static int motor_motion_pop(t_motor_motion **ret)
{
unsigned int next_head = motor_motion_head + 1;

if (motor_motion_tail == motor_motion_head) {
return -1;
}

if (next_head >= MAX_MOTORBUFLEN) {
next_head = 0;
}

*ret = (motor_motion + motor_motion_head);

motor_motion_head = next_head;

return 0;
}
#endif

/* --- GPIO Operation --- */
/* getPWMCount function for GPIO Operation */
static int getPWMCount(int freq)
Expand Down Expand Up @@ -1531,8 +1246,8 @@ static int mcp3204_init(void)

spi_register_driver(&mcp3204_driver);

mcp3204_info.bus_num = spi_bus_num;
mcp3204_info.chip_select = spi_chip_select;
mcp3204_info.bus_num = SPI_BUS_NUM;
mcp3204_info.chip_select = SPI_CHIP_SELECT;

master = spi_busnum_to_master(mcp3204_info.bus_num);

Expand Down
Loading

0 comments on commit 43532e9

Please sign in to comment.