2023年7月11日 星期二 Linux驱动作业

8 篇文章 0 订阅
6 篇文章 0 订阅

1.在内核模块中启用定时器,定时1s,让led1 一秒亮、一面灭

驱动程序


#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/timer.h>

struct device_node *dnode;
unsigned int gpiono;
struct timer_list mytimer;
// 定义定时器处理函数
void mytimer_func(struct timer_list *timer)
{
    // 一秒亮一秒灭
    gpio_set_value(gpiono, !gpio_get_value(gpiono));
    // 再次启用定时器
    mod_timer(timer, jiffies + HZ);
}
static int __init mycdev_init(void)
{
    int ret;
    // 解析led灯的设备树节点
    dnode = of_find_node_by_name(NULL, "myleds");
    if (dnode == NULL)
    {
        printk("解析设备树节点失败\n");
        return -ENXIO;
    }
    printk("解析设备树节点成功\n");
    // 根据设备树节点解析led1gpio编号
    gpiono = of_get_named_gpio(dnode, "led1", 0);
    if (gpiono < 0)
    {
        printk("gpio编号解析失败\n");
    }
    printk("解析gpio编号成功gpiono=%d\n", gpiono);
    // 申请gpio编号
    ret = gpio_request(gpiono, NULL);
    if (ret < 0)
    {
        printk("申请gpio编号失败\n");
        return ret;
    }
    // led1对应的gpio的管脚为输出
    gpio_direction_output(gpiono, 0);
    // 亮灯
    gpio_set_value(gpiono, 1);
    // 初始化定时器对象
    mytimer.expires = jiffies + HZ;
    timer_setup(&mytimer, mytimer_func, 0);
    // 将定时器对象注册进内核,并启用
    add_timer(&mytimer);
    return 0;
}
static void __exit mycdev_exit(void)
{
    // 灭灯
    gpio_set_value(gpiono, 0);
    // 释放gpio编号
    gpio_free(gpiono);
    del_timer(&mytimer);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

2.基于gpio子系统完成LED灯驱动的注册,应用程序测试

应用程序


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>

#define LED_ON _IOW('l', 1, int)
#define LED_OFF _IOW('l', 0, int)

int main(int argc, char const *argv[])
{
    int a, b;
    char buf[128] = {0};
    int fd_led = open("/dev/myled0", O_RDWR);
    if (fd_led < 0)
    {
        printf("打开设备文件失败\n");
        exit(-1);
    }
    while (1)
    {
        // 从终端读取
        printf("请选择灯功能\n");
        printf("0(关灯) 1(开灯)>");
        scanf("%d", &a);
        printf("请输入要控制的灯\n");
        printf("1(LED1) 2(LED2) 3(LED3)>");
        scanf("%d", &b);
        if (a == 1) // 开灯
        {
            ioctl(fd_led, LED_ON, &b);
        }
        else if (a == 0) // 关灯
        {
            ioctl(fd_led, LED_OFF, &b);
        }
    }
    close(fd_led);
    return 0;
}

驱动程序


#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/io.h>
#include <linux/of.h>

#define LED_ON _IOW('l', 1, int)
#define LED_OFF _IOW('l', 0, int)

struct cdev *cdev;
unsigned int major = 0;
unsigned int minor = 0; // 次设备号的起始值
dev_t devno;
struct class *cls;
struct device *dev;
char kbuf[128] = {0};
struct device_node *dnode;
unsigned int gpiono1;
unsigned int gpiono2;
unsigned int gpiono3;
int mycdev_open(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int which;
    int ret = copy_from_user(&which, (void *)arg, 4);
    if (ret)
    {
        printk("拷贝用户空间数据失败\n");
        return -EIO;
    }
    switch (cmd)
    {
    case LED_ON:
        switch (which)
        {
        case 1:
            // 亮灯
            gpio_set_value(gpiono1, 1);
            break;
        case 2:
            // 亮灯
            gpio_set_value(gpiono2, 1);
            break;
        case 3:
            // 亮灯
            gpio_set_value(gpiono3, 1);
            break;
        }
        break;
    case LED_OFF:
        switch (which)
        {
        case 1:
            // 闭灯
            gpio_set_value(gpiono1, 0);
            break;
        case 2:
            // 闭灯
            gpio_set_value(gpiono2, 0);
            break;
        case 3:
            // 闭灯
            gpio_set_value(gpiono3, 0);
            break;
        }
        break;
    }
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

// 定义操作方法结构体变量并赋值
struct file_operations fops = {

    .open = mycdev_open,
    .unlocked_ioctl = mycdev_ioctl,
    .release = mycdev_close,
};

static int __init mycdev_init(void)
{
    int ret, i;
    // 1.分配字符设备驱动对象空间  cdev_alloc
    cdev = cdev_alloc();
    if (cdev == NULL)
    {
        printk("申请字符设备驱动对象空间失败\n");
        ret = -EFAULT;
        goto out1;
    }
    printk("字符设备驱动对象申请成功\n");
    // 2.字符设备驱动对象部分初始化  cdev_init
    cdev_init(cdev, &fops);
    // 3.申请设备号  register_chrdev_region/alloc_chrdev_region
    if (major > 0) // 静态申请设备号
    {
        ret = register_chrdev_region(MKDEV(major, minor), 3, "myled");
        if (ret)
        {
            printk("静态指定设备号失败\n");
            goto out2;
        }
    }
    else // 动态申请设备号
    {
        ret = alloc_chrdev_region(&devno, minor, 3, "myled");
        if (ret)
        {
            printk("动态申请设备号失败\n");
            goto out2;
        }
        major = MAJOR(devno); // 根据设备号得到主设备号
        minor = MINOR(devno); // 根据设备号得到次设备号
    }
    printk("申请设备号成功\n");
    // 4.注册字符设备驱动对象  cdev_add()
    ret = cdev_add(cdev, MKDEV(major, minor), 3);
    if (ret)
    {
        printk("注册字符设备驱动对象失败\n");
        goto out3;
    }
    printk("注册字符设备驱动对象成功\n");
    // 5.向上提交目录
    cls = class_create(THIS_MODULE, "myled");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        ret = -PTR_ERR(cls);
        goto out4;
    }
    printk("向上提交目录成功\n");
    // 6.向上提交设备节点
    for (i = 0; i < 3; i++)
    {
        dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
        if (IS_ERR(dev))
        {
            printk("向上提交节点信息失败\n");
            ret = -PTR_ERR(dev);
            goto out5;
        }
    }
    printk("向上提交设备节点信息成功\n");
    // 解析led灯的设备树节点
    dnode = of_find_node_by_name(NULL, "myleds");
    if (dnode == NULL)
    {
        printk("解析设备树节点失败\n");
    }
    printk("解析设备树节点成功\n");
    // 根据设备树节点解析led1gpio编号
    gpiono1 = of_get_named_gpio(dnode, "led1", 0);
    if (gpiono1 < 0)
    {
        printk("gpio编号解析失败\n");
    }
    printk("解析gpio1编号成功gpiono1=%d\n", gpiono1);
    // 根据设备树节点解析led2gpio编号
    gpiono2 = of_get_named_gpio(dnode, "led2", 0);
    if (gpiono2 < 0)
    {
        printk("gpio编号解析失败\n");
    }
    printk("解析gpio2编号成功gpiono2=%d\n", gpiono2);
    // 根据设备树节点解析led3gpio编号
    gpiono3 = of_get_named_gpio(dnode, "led3", 0);
    if (gpiono3 < 0)
    {
        printk("gpio编号解析失败\n");
    }
    printk("解析gpio3编号成功gpiono3=%d\n", gpiono3);
    // 申请gpio编号
    ret = gpio_request(gpiono1, NULL);
    if (ret < 0)
    {
        printk("申请gpio编号失败\n");
        goto out6;
    }
    ret = gpio_request(gpiono2, NULL);
    if (ret < 0)
    {
        printk("申请gpio编号失败\n");
        goto out7;
    }
    ret = gpio_request(gpiono3, NULL);
    if (ret < 0)
    {
        printk("申请gpio编号失败\n");
        goto out8;
    }
    // led1对应的gpio的管脚为输出
    gpio_direction_output(gpiono1, 0);
    // led2对应的gpio的管脚为输出
    gpio_direction_output(gpiono2, 0);
    // led3对应的gpio的管脚为输出
    gpio_direction_output(gpiono3, 0);
    // 亮灯
    gpio_set_value(gpiono1, 1);
    // 亮灯
    gpio_set_value(gpiono2, 1);
    // 亮灯
    gpio_set_value(gpiono3, 1);
    return 0;
out8:
    // 释放gpio编号
    gpio_free(gpiono3);
out7:
    // 释放gpio编号
    gpio_free(gpiono2);
out6:
    // 释放gpio编号
    gpio_free(gpiono1);
out5:
    for (--i; i >= 0; i--)
    {
        // 销毁上面提交的设备信息
        device_destroy(cls, MKDEV(major, i));
    }
    class_destroy(cls);
out4:
    cdev_del(cdev);
out3:
    unregister_chrdev_region(MKDEV(major, minor), 3);
out2:
    kfree(cdev);
out1:
    return ret;
}
static void __exit mycdev_exit(void)
{

    // 1.销毁设备信息  device_destroy
    int i;
    for (i = 0; i < 3; i++)
    {
        device_destroy(cls, MKDEV(major, i));
    }
    // 2.销毁目录  class_destroy
    class_destroy(cls);
    // 3.注销对象  cdev_del()
    cdev_del(cdev);
    // 4.释放设备号   unregister_chrdev_region()
    unregister_chrdev_region(MKDEV(major, minor), 3);
    // 5.释放对象空间  kfree()
    kfree(cdev);
    // 6.释放gpio编号
    gpio_free(gpiono1);
    gpio_free(gpiono2);
    gpio_free(gpiono3);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用第三方库LunarCalendar.js来实现,具体步骤如下: 1. 引入LunarCalendar.js库 ```html <script src="https://unpkg.com/lunar-calendar/dist/lunar-calendar.js"></script> ``` 2. 获取当前时间 ```javascript var now = new Date(); ``` 3. 转换为农历时间 ```javascript var lunar = calendar.solar2lunar(now.getFullYear(), now.getMonth() + 1, now.getDate()); ``` 4. 格式化输出 ```javascript var weekDays = ['', '一', '二', '三', '四', '五', '六']; var yearName = calendar.toChinaYear(lunar.lYear); var monthName = calendar.toChinaMonth(lunar.lMonth); var dayName = calendar.toChinaDay(lunar.lDay); var result = now.getFullYear() + '年' + (now.getMonth() + 1) + '月' + now.getDate() + ' ' + '星期' + weekDays[now.getDay()] + ' 农历 ' + yearName + monthName + dayName; console.log(result); ``` 完整代码如下: ```javascript <script src="https://unpkg.com/lunar-calendar/dist/lunar-calendar.js"></script> <script> var now = new Date(); var lunar = calendar.solar2lunar(now.getFullYear(), now.getMonth() + 1, now.getDate()); var weekDays = ['', '一', '二', '三', '四', '五', '六']; var yearName = calendar.toChinaYear(lunar.lYear); var monthName = calendar.toChinaMonth(lunar.lMonth); var dayName = calendar.toChinaDay(lunar.lDay); var result = now.getFullYear() + '年' + (now.getMonth() + 1) + '月' + now.getDate() + ' ' + '星期' + weekDays[now.getDay()] + ' 农历 ' + yearName + monthName + dayName; console.log(result); </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值