背景
所有驅動開發都是基于全志T507(Android 10)進行開發,用于記錄驅動開發過程。
簡介
定時器是比較常用的一個功能,用來執行周期性任務。一般不太精確的定時可以用系統提供的延時函數進行。如果需要進行較為精確的延時,就要考慮使用系統內核提供的定時器中斷。
在linux中ktime可以實現ms級定時,如果需要us、ns級就沒法使用這個了(這個我們后續在討論)。
驅動實現
首先在Linux驅動開發1 - Platform設備中已經實現了Platform設備驅動的編寫,本文是基于這個驅動進行的定時器功能開發。
步驟1、配置時鐘頻率
首先在內核中可以配置定時器中斷周期,T507中默認配置為300HZ,即最大精度為3.33ms。如上圖所示,timer最大支持的精度為1ms,可以通過啟用CONFIG_HZ_1000進行設置。本文就以默認的300HZ頻率為例。
也可以用make menuconfig在圖形化界面進行配置。
配置文件路徑為/kernel/linux-4.9/arch/arm64/configs/xxx
步驟2、實現platform_driver 結構體。
在longan/kernel/linux-4.9/drivers目錄下創建自己的文件夾,定義一個timer_led_drv.c文件。
這里可以不用修改,只修改了驅動名稱
/* platform驅動結構體 */
static struct platform_driver led_timer_driver = {.driver = {.name = "devledtimer", // 無設備樹時,用于設備和驅動間的匹配.of_match_table = led_timer_of_match, // 有設備樹后,利用設備樹匹配表},.probe = led_timer_probe,.remove = led_timer_remove,
};
在設備數創建設備節點。這里以PI0為例
led_timer {compatible = "devledtimer";pinctrl-names = "default";status = "okay";//pinctrl-0 = <&timer_led_pin_a>;ledtimer {gpios = <&pio PI 0 1 2 0 1>;state = "on";};};
在PIO節點下定義pinctrl_led_test節點,主要用于配置IO口。
soc@03000000 {pio: pinctrl@0300b000 {...timer_led_pin_a: led_timer_grp@0 {allwinner,pins = "PI0";allwinner,function = "led_timer_grp";allwinner,muxsel = <0x01>;allwinner,drive = <0x00>;allwinner,pull = <0x01>;allwinner,data = <0x01>;};...}}
步驟3、定義設備節點
主要定義一下led_timer_priv,用來存放節點信息,用于生成/dev/xxx節點。
這里用到了定時器,因此增加了timer_list和timeperiod用來啟用定時器及控制定時器周期。
另外還用到了自旋鎖,作用自己查資料,這里就不詳述了。
/* 存放led信息的結構體 */
struct led_timer_data
{char name[16]; // 設備名字int pin; // gpio編號int active; // 控制亮滅的標志int enable; // 定時器使能標志位
};/* 存放led的私有屬性 */
struct led_timer_priv
{struct cdev cdev; // cdev結構體struct class *dev_class; // 自動創建設備節點的類int num_leds; // led的數量struct led_timer_data led; // 存放led信息的結構體數組int timeperiod; // 定時周期struct timer_list timer; // 定義一個定時器spinlock_t lock; // 定義一個自旋鎖};
步驟4、初始化設備
定義一個初始化方法gpio_led_probe,并綁定到步驟2中的.probe
static int led_timer_probe(struct platform_device *pdev)
這個函數和?Linux驅動開發1 - Platform設備中gpio_led_probe一樣。
parser_init_led_timer用于獲取設備樹中的節點信息,并進行初始化。
步驟5、移除設備
常規操作,將所有設備依次注銷回收。
增加del_timer_sync(&priv->timer);用于在移除設備時刪除定時器。
static int led_timer_remove(struct platform_device *pdev)
{struct led_timer_priv *priv = platform_get_drvdata(pdev);int i;dev_t devno = MKDEV(dev_major, 0);/* 注銷設備結構體,class結構體和cdev結構體 */for(i=0; i<priv->num_leds; i++){devno = MKDEV(dev_major, i);device_destroy(priv->dev_class, devno);}class_destroy(priv->dev_class);cdev_del(&priv->cdev); unregister_chrdev_region(MKDEV(dev_major, 0), priv->num_leds);/* 將led的狀態設置為滅 */for (i = 0; i < priv->num_leds; i++) {gpio_set_value(priv->led.pin, 0);} del_timer_sync(&priv->timer);printk("led_timer_ioctl success to remove driver[major=%d]!\n", dev_major);return 0;
}
步驟6、字符設備描述file_operations
static struct file_operations led_fops =
{.owner = THIS_MODULE,.open = led_open,.release = led_release,.unlocked_ioctl = led_ioctl,.compat_ioctl = led_timer_ioctl,
};
實現led_open、led_release、led_ioctl。
在led_open中初始化自旋鎖及初始化定時器timer。修改ioctl邏輯,增加定時器控制命令。
static int led_open(struct inode *inode, struct file *file)
{struct led_timer_priv *priv;priv = container_of(inode->i_cdev, struct led_timer_priv, cdev);file->private_data = priv;priv->timeperiod = 1000;/* 初始化自旋鎖 */spin_lock_init(&priv->lock);/* 初始化timer */setup_timer(&priv->timer, timer_do_in_time, &priv->timer);return 0;
}static int led_release(struct inode *inode, struct file *file)
{return 0;
}static long led_timer_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{struct led_timer_priv *priv;priv = file->private_data;unsigned int flags;printk(">>>>> led_timer_ioctl command=%d arg=%ld\n", cmd, arg);switch (cmd){case 10:/* 關閉定時器 */del_timer_sync(&priv->timer);priv->led.enable = 0;gpio_set_value(priv->led.pin, 0);priv->led.active = 0;printk(">>>>> led_timer_ioctl del_timer_sync\n");break;case 11:/* 打開定時器 */del_timer_sync(&priv->timer);mod_timer(&priv->timer, jiffies + msecs_to_jiffies(priv->timeperiod));priv->led.enable = 1;printk(">>>>> led_timer_ioctl mod_timer = %ld\n", jiffies);break;case 12:/* 設置定時器周期 */spin_lock_irqsave(&priv->lock, flags);priv->timeperiod = arg;spin_unlock_irqrestore(&priv->lock, flags);if (priv->led.enable == 0){del_timer_sync(&priv->timer);mod_timer(&priv->timer, jiffies + msecs_to_jiffies(priv->timeperiod));priv->led.enable = 1;}printk(">>>>> led_timer_ioctl period = %d\n", priv->timeperiod);break;default:printk("Ioctl command=%d can't be supported\n", cmd);break;}return 0;}
實現定時器中斷處理函數
void timer_do_in_time(unsigned long arg)
{struct timer_list *mytimer = (struct timer_list *)arg;struct led_timer_priv *priv = container_of(mytimer, struct led_timer_priv, timer);// TODO 在這里處理需要做的事情// 根據標志位處理是否重啟定時器if (priv->led.enable == 1){mod_timer(&priv->timer, jiffies + msecs_to_jiffies(priv->timeperiod)); }
}
步驟7、編譯環境MakeFile
在自定義目錄下創建Makefile文件。這里默認加載設備。
obj-y += timer_led_drv.o
遇到的問題
問題1、中斷函數中獲取led_timer_priv結構體信息。
主要用container_of獲取到內存中struct led_timer_priv *priv類型指針,然后使用priv中的led_timer_data或者timer進行操作。
源碼
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/timer.h>
#include <linux/jiffies.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of_gpio.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/of_device.h>
#include <linux/slab.h>#define LED_OFF 0
#define LED_ON 1static int dev_major = 0;/* 存放led信息的結構體 */
struct led_timer_data
{char name[16]; // 設備名字int pin; // gpio編號int active; // 控制亮滅的標志int enable; // 定時器使能標志位
};/* 存放led的私有屬性 */
struct led_timer_priv
{struct cdev cdev; // cdev結構體struct class *dev_class; // 自動創建設備節點的類int num_leds; // led的數量struct led_timer_data led; // 存放led信息的結構體數組int timeperiod; // 定時周期struct timer_list timer; // 定義一個定時器spinlock_t lock; // 定義一個自旋鎖};void timer_do_in_time(unsigned long arg)
{struct timer_list *mytimer = (struct timer_list *)arg;struct led_timer_priv *priv = container_of(mytimer, struct led_timer_priv, timer);/* 每次都取反,實現LED燈反轉 */priv->led.active = !priv->led.active; gpio_set_value(priv->led.pin, priv->led.active);/* 重啟定時器 */if (priv->led.enable == 1){mod_timer(&priv->timer, jiffies + msecs_to_jiffies(priv->timeperiod)); }
}static inline int sizeof_led_timers_priv(int num_leds)
{return sizeof(struct led_timer_priv) + (sizeof(struct led_timer_priv) * num_leds);
}int parser_init_led_timer(struct platform_device *pdev)
{struct device_node *np = pdev->dev.of_node; // 當前設備節點struct device_node *child; // 當前設備節點的子節點struct led_timer_priv *priv; // 存放私有屬性int num_leds, gpio; // led數量和gpio編號 num_leds = 1;if(num_leds <= 0) {dev_err(&pdev->dev, "fail to find child node\n");return -EINVAL;}priv = devm_kzalloc(&pdev->dev, sizeof_led_timers_priv(num_leds), GFP_KERNEL);if (!priv){return -ENOMEM;}priv->num_leds = 1;/* 找到子節點并傳給child */child = of_get_child_by_name(np, "ledtimer");/* 解析dts并且獲取gpio口,函數返回值就得到gpio號,并且讀取gpio現在的標志 */gpio = of_get_named_gpio(child, "gpios", 0);/* 將子節點的名字,傳給私有屬性結構體中的led信息結構體中的name屬性 */strncpy(priv->led.name, child->name, sizeof(priv->led.name)); printk(">>>>> led_timer_ioctl gpio=%d name=%s\n", gpio, priv->led.name);/* 將gpio編號和控制亮滅的標志傳給結構體* active屬性,1代表亮,0代表滅,初始屬性為亮*/priv->led.active = 1; priv->led.pin = gpio; /* 申請gpio口,相較于gpio_request增加了gpio資源獲取與釋放功能 */gpio_request(priv->led.pin, "ledtimer");/* 設置gpio為輸出模式,并設置初始狀態 */gpio_direction_output(priv->led.pin, 1);/* 將led的私有屬性放入platform_device結構體的device結構體中的私有數據中 */platform_set_drvdata(pdev, priv);return 0;}static int led_open(struct inode *inode, struct file *file)
{struct led_timer_priv *priv;priv = container_of(inode->i_cdev, struct led_timer_priv, cdev);file->private_data = priv;priv->timeperiod = 1000;/* 初始化自旋鎖 */spin_lock_init(&priv->lock);/* 初始化timer */setup_timer(&priv->timer, timer_do_in_time, &priv->timer);return 0;
}static int led_release(struct inode *inode, struct file *file)
{return 0;
}static long led_timer_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{struct led_timer_priv *priv;priv = file->private_data;unsigned int flags;switch (cmd){case 10:/* 關閉定時器 */del_timer_sync(&priv->timer);priv->led.enable = 0;gpio_set_value(priv->led.pin, 0);priv->led.active = 0;break;case 11:/* 打開定時器 */del_timer_sync(&priv->timer);mod_timer(&priv->timer, jiffies + msecs_to_jiffies(priv->timeperiod));priv->led.enable = 1;break;case 12:/* 設置定時器周期 */spin_lock_irqsave(&priv->lock, flags);priv->timeperiod = arg;spin_unlock_irqrestore(&priv->lock, flags);if (priv->led.enable == 0){del_timer_sync(&priv->timer);mod_timer(&priv->timer, jiffies + msecs_to_jiffies(priv->timeperiod));priv->led.enable = 1;}break;default:printk("Ioctl command=%d can't be supported\n", cmd);break;}return 0;}static struct file_operations led_fops =
{.owner = THIS_MODULE,.open = led_open,.release = led_release,.unlocked_ioctl = led_timer_ioctl,.compat_ioctl = led_timer_ioctl,
};static int led_timer_probe(struct platform_device *pdev)
{struct led_timer_priv *priv; // 臨時存放私有屬性的結構體struct device *dev; // 設備結構體dev_t devno; // 設備的主次設備號int i, rv = 0; /* 1)解析設備樹并初始化led狀態 */rv = parser_init_led_timer(pdev);if( rv < 0 )return rv;/* 將之前存入的私有屬性,放入臨時的結構體中 */priv = platform_get_drvdata(pdev);/* 2)分配主次設備號 */if (0 != dev_major) { /* 靜態分配主次設備號 */devno = MKDEV(dev_major, 0); rv = register_chrdev_region(devno, priv->num_leds, "devledtimer"); } else { /* 動態分配主次設備號 */rv = alloc_chrdev_region(&devno, 0, priv->num_leds, "devledtimer"); dev_major = MAJOR(devno); } if (rv < 0) { dev_err(&pdev->dev, "major can't be allocated\n"); return rv; } /* 3)分配cdev結構體 */cdev_init(&priv->cdev, &led_fops);priv->cdev.owner = THIS_MODULE;rv = cdev_add (&priv->cdev, devno , priv->num_leds); if( rv < 0) {dev_err(&pdev->dev, "struture cdev can't be allocated\n");goto undo_major;}/* 4)創建類,實現自動創建設備節點 */priv->dev_class = class_create(THIS_MODULE, "ledtimer");if( IS_ERR(priv->dev_class) ) {dev_err(&pdev->dev, "fail to create class\n");rv = -ENOMEM;goto undo_cdev;}/* 5)創建設備 */for(i=0; i<priv->num_leds; i++){devno = MKDEV(dev_major, i);dev = device_create(priv->dev_class, NULL, devno, NULL, "ledtimer");if( IS_ERR(dev) ) {dev_err(&pdev->dev, "fail to create device\n");rv = -ENOMEM;goto undo_class;}}printk("led_timer_ioctl success to install driver[major=%d]!\n", dev_major);return 0;undo_class:class_destroy(priv->dev_class);undo_cdev:cdev_del(&priv->cdev);undo_major:unregister_chrdev_region(devno, priv->num_leds);return rv;}static int led_timer_remove(struct platform_device *pdev)
{struct led_timer_priv *priv = platform_get_drvdata(pdev);int i;dev_t devno = MKDEV(dev_major, 0);/* 注銷設備結構體,class結構體和cdev結構體 */for(i=0; i<priv->num_leds; i++){devno = MKDEV(dev_major, i);device_destroy(priv->dev_class, devno);}class_destroy(priv->dev_class);cdev_del(&priv->cdev); unregister_chrdev_region(MKDEV(dev_major, 0), priv->num_leds);/* 將led的狀態設置為滅 */for (i = 0; i < priv->num_leds; i++) {gpio_set_value(priv->led.pin, 0);} del_timer_sync(&priv->timer);printk("led_timer_ioctl success to remove driver[major=%d]!\n", dev_major);return 0;
} /* 匹配列表 */
static const struct of_device_id led_timer_of_match[] = {{ .compatible = "devledtimer" },{}
};MODULE_DEVICE_TABLE(of, led_timer_of_match);/* platform驅動結構體 */
static struct platform_driver led_timer_driver = {.driver = {.name = "devledtimer", // 無設備樹時,用于設備和驅動間的匹配.of_match_table = led_timer_of_match, // 有設備樹后,利用設備樹匹配表},.probe = led_timer_probe,.remove = led_timer_remove,
};module_platform_driver(led_timer_driver);MODULE_LICENSE("GPL");