Skip to content

rtmouse.cの分割(リファクタリング) #92

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

Merged
merged 26 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6e53c59
renamed rtmouse.c to rtmouse_main.c
YusukeKato Nov 5, 2024
a4b6b0e
lintの対象ファイルを修正
YusukeKato Nov 5, 2024
4f0ef43
Cファイル分割の準備
YusukeKato Nov 5, 2024
93ad739
外部変数をヘッダファイルで宣言
YusukeKato Nov 5, 2024
bd7640b
デバイス関連の関数をrtmouse_dev.cへ移動
YusukeKato Nov 5, 2024
f2e86ce
lint対象を修正&インデント修正
YusukeKato Nov 5, 2024
f260f46
lintに対応して修正
YusukeKato Nov 5, 2024
152d057
lintに対応して修正
YusukeKato Nov 5, 2024
560bd67
rtmouse_dev.cをrtmouse_dev_fops.cへ名称変更
YusukeKato Nov 5, 2024
d530af8
ライセンスとバージョンを更新
YusukeKato Nov 5, 2024
71aa661
関数の並びを整理
YusukeKato Nov 5, 2024
b2d7b7d
外部変数を整理
YusukeKato Nov 5, 2024
c680610
lintに対応して修正
YusukeKato Nov 5, 2024
2948238
外部からも使用する関数を整理
YusukeKato Nov 5, 2024
92ec244
lintに対応して修正
YusukeKato Nov 5, 2024
cead9f2
rtmouse_main.cで使用する関数はrtmouse_main.cに残す
YusukeKato Nov 7, 2024
5b87a72
rtmouse_spi.cを作成してSPI関連の関数を移す
YusukeKato Nov 7, 2024
c1f774a
rtmouse_spi.cにコメントを追加
YusukeKato Nov 7, 2024
f6cd192
rtmouse_i2c.cを作成してI2C関連の関数を移す
YusukeKato Nov 7, 2024
b175fbc
register_dev()をrtmouse_dev_fops.cへ移す
YusukeKato Nov 7, 2024
77bb477
rtmouse_dev_fops.cをrtmouse_dev.cへ名称変更
YusukeKato Nov 7, 2024
a556d62
lintに対応して修正
YusukeKato Nov 7, 2024
0cc93d9
lint対象のファイルを修正
YusukeKato Nov 7, 2024
6e95fbf
rtmouse_gpio.cを作成してGPIO関連の関数を移す
YusukeKato Nov 7, 2024
2b50070
コメントなど微修正
YusukeKato Nov 7, 2024
b3c3c79
lintに対応して修正
YusukeKato Nov 7, 2024
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
Prev Previous commit
Next Next commit
rtmouse_spi.cを作成してSPI関連の関数を移す
  • Loading branch information
YusukeKato committed Nov 7, 2024
commit 5b87a720fbf08e9e5f8b03f1c69d6c828db71630
4 changes: 2 additions & 2 deletions src/drivers/Makefile.header_from_apt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
MODULE:= rtmouse
obj-m:= $(MODULE).o
$(MODULE)-y:= $(MODULE)_main.o $(MODULE)_dev_fops.o
$(MODULE)-y:= $(MODULE)_main.o $(MODULE)_dev_fops.o $(MODULE)_spi.o
clean-files:= *.o *.ko *.mod.[co] *~

LINUX_SRC_DIR:=/usr/src/linux-headers-$(shell uname -r)
VERBOSE:=0

$(MODULE).ko: $(MODULE)_dev_fops.c $(MODULE)_main.c $(MODULE).h
$(MODULE).ko: $(MODULE)_main.c $(MODULE)_dev_fops.c $(MODULE)_spi.c $(MODULE).h
make -C $(LINUX_SRC_DIR) M=$(shell pwd) V=$(VERBOSE) modules

clean:
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/Makefile.header_from_source
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
MODULE:= rtmouse
obj-m:= $(MODULE).o
$(MODULE)-y:= $(MODULE)_main.o $(MODULE)_dev_fops.o
$(MODULE)-y:= $(MODULE)_main.o $(MODULE)_dev_fops.o $(MODULE)_spi.o
clean-files:= *.o *.ko *.mod.[co] *~

LINUX_SRC_DIR:=/usr/src/linux
VERBOSE:=0

$(MODULE).ko: $(MODULE)_dev_fops.c $(MODULE)_main.c $(MODULE).h
$(MODULE).ko: $(MODULE)_main.c $(MODULE)_dev_fops.c $(MODULE)_spi.c $(MODULE).h
make -C $(LINUX_SRC_DIR) M=$(shell pwd) V=$(VERBOSE) modules

clean:
Expand Down
5 changes: 4 additions & 1 deletion src/drivers/rtmouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,13 @@ extern struct file_operations dev_fops[ID_DEV_SIZE];
extern struct device *mcp320x_dev;
#endif

/* --- used in rtmouse_dev_fops.c --- */
int rpi_gpio_function_set(int pin, uint32_t func);
void rpi_gpio_set32(uint32_t mask, uint32_t val);
void rpi_gpio_clear32(uint32_t mask, uint32_t val);
void rpi_pwm_write32(uint32_t offset, uint32_t val);

/* --- used in rtmouse_spi.c --- */
int mcp3204_init(void);
void mcp3204_exit(void);

#endif // RTMOUSE_H
209 changes: 5 additions & 204 deletions src/drivers/rtmouse_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,15 @@ static struct class *class_dev[ID_DEV_SIZE] = {
[ID_DEV_MOTORRAWR] = NULL, [ID_DEV_MOTORRAWL] = NULL,
[ID_DEV_MOTOREN] = NULL, [ID_DEV_MOTOR] = NULL};

volatile void __iomem *pwm_base;
static volatile void __iomem *clk_base;
volatile uint32_t *gpio_base;
static volatile int cdev_index = 0;

// used in rtmouse_dev_fops.c
volatile void __iomem *pwm_base;
volatile uint32_t *gpio_base;
struct mutex lock;

/* --- Function Declarations --- */
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 18, 0)
static int mcp3204_remove(struct spi_device *spi);
#else
static void mcp3204_remove(struct spi_device *spi);
#endif

static int mcp3204_probe(struct spi_device *spi);

#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 2, 0)
static int rtcnt_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id);
Expand All @@ -107,37 +101,11 @@ static void rtcnt_i2c_remove(struct i2c_client *client);
#endif

/* --- Static variables --- */
/* SPI device ID */
static struct spi_device_id mcp3204_id[] = {
{"mcp3204", 0},
{},
};

/* SPI Info */
struct spi_board_info mcp3204_info = {
.modalias = "mcp3204",
.max_speed_hz = 100000,
.bus_num = 0,
.chip_select = 0,
.mode = SPI_MODE_3,
};

// used in rtmouse_dev_fops.c
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 16, 0)
struct device *mcp320x_dev;
#endif

/* SPI Dirver Info */
static struct spi_driver mcp3204_driver = {
.driver =
{
.name = DEVNAME_SENSOR,
.owner = THIS_MODULE,
},
.id_table = mcp3204_id,
.probe = mcp3204_probe,
.remove = mcp3204_remove,
};

static struct i2c_client *i2c_client_r = NULL;
static struct i2c_client *i2c_client_l = NULL;

Expand All @@ -161,7 +129,6 @@ static struct i2c_driver i2c_counter_driver = {
};

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

/*
Expand Down Expand Up @@ -365,172 +332,6 @@ static int register_dev(int id_dev)
return 0;
}

/* mcp3204_remove - remove function lined with spi_dirver */
#if LINUX_VERSION_CODE < KERNEL_VERSION(5, 18, 0)
static int mcp3204_remove(struct spi_device *spi)
{
struct mcp3204_drvdata *data;
/* get drvdata */
data = (struct mcp3204_drvdata *)spi_get_drvdata(spi);
/* free kernel memory */
kfree(data);
printk(KERN_INFO "%s: mcp3204 removed\n", DRIVER_NAME);
return 0;
}
#else
static void mcp3204_remove(struct spi_device *spi)
{
struct mcp3204_drvdata *data;
/* get drvdata */
data = (struct mcp3204_drvdata *)spi_get_drvdata(spi);
/* free kernel memory */
kfree(data);
printk(KERN_INFO "%s: mcp3204 removed\n", DRIVER_NAME);
}
#endif

/* mcp3204_probe - probe function lined with spi_dirver */
static int mcp3204_probe(struct spi_device *spi)
{
struct mcp3204_drvdata *data;

spi->max_speed_hz = mcp3204_info.max_speed_hz;
spi->mode = mcp3204_info.mode;
spi->bits_per_word = 8;

if (spi_setup(spi)) {
printk(KERN_ERR "%s:spi_setup failed!\n", __func__);
return -ENODEV;
}

/* alloc kernel memory */
data = kzalloc(sizeof(struct mcp3204_drvdata), GFP_KERNEL);
if (data == NULL) {
printk(KERN_ERR "%s:kzalloc() failed!\n", __func__);
return -ENODEV;
}

data->spi = spi;

mutex_init(&data->lock);

// memset(data->tx, 0, MCP320X_PACKET_SIZE);
// memset(data->rx, 0, MCP320X_PACKET_SIZE);

data->xfer.tx_buf = data->tx;
data->xfer.rx_buf = data->rx;
data->xfer.bits_per_word = 8;
data->xfer.len = MCP320X_PACKET_SIZE;
data->xfer.cs_change = 0;
data->xfer.speed_hz = 100000;

spi_message_init_with_transfers(&data->msg, &data->xfer, 1);

/* set drvdata */
spi_set_drvdata(spi, data);

printk(KERN_INFO "%s: mcp3204 probed", DRIVER_NAME);

return 0;
}

/*
* spi_remove_device - remove SPI device
* called by mcp3204_init and mcp3204_exit
*/
static void spi_remove_device(struct spi_master *master, unsigned int cs)
{
struct device *dev;
char str[128];

snprintf(str, sizeof(str), "%s.%u", dev_name(&master->dev), cs);

dev = bus_find_device_by_name(&spi_bus_type, NULL, str);
// ここを参考にspi_deviceを取得するプログラムを作成する
if (dev) {
device_del(dev);
}
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 16, 0)
/* spiをサーチする関数 */
static int __callback_find_mcp3204(struct device *dev, void *data)
{
printk(KERN_INFO " device_name: %s\n", dev->driver->name);
if (mcp320x_dev == NULL && strcmp(dev->driver->name, "mcp320x") == 0) {
mcp320x_dev = dev;
mcp3204_probe(to_spi_device(dev));
}
return 0;
}
#endif

/*
* mcp3204_init - initialize MCP3204
* called by 'dev_init_module'
*/
static int mcp3204_init(void)
{

#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 16, 0)
bus_for_each_dev(&spi_bus_type, NULL, NULL, __callback_find_mcp3204);
#else
struct spi_master *master;
struct spi_device *spi_device;

spi_register_driver(&mcp3204_driver);

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

master = spi_busnum_to_master(mcp3204_info.bus_num);

if (!master) {
printk(KERN_ERR "%s: spi_busnum_to_master returned NULL\n",
__func__);
spi_unregister_driver(&mcp3204_driver);
return -ENODEV;
}

spi_remove_device(master, mcp3204_info.chip_select);

spi_device = spi_new_device(master, &mcp3204_info);
if (!spi_device) {
printk(KERN_ERR "%s: spi_new_device returned NULL\n", __func__);
spi_unregister_driver(&mcp3204_driver);
return -ENODEV;
}
#endif

return 0;
}

/*
* mcp3204_exit - cleanup MCP3204
* called by dev_cleanup_module()
*/
static void mcp3204_exit(void)
{

#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 16, 0)
printk(KERN_INFO " mcp3204_exit\n");
if (mcp320x_dev) {
mcp3204_remove(to_spi_device(mcp320x_dev));
}
#else
struct spi_master *master;
master = spi_busnum_to_master(mcp3204_info.bus_num);

if (master) {
spi_remove_device(master, mcp3204_info.chip_select);
} else {
printk(KERN_ERR "mcp3204 remove error\n");
}

spi_unregister_driver(&mcp3204_driver);
#endif
}

static int rtcntr_i2c_create_cdev(struct rtcnt_device_info *dev_info)
{
int minor;
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy