• 韦东山嵌入式linux系列-具体单板的 LED 驱动程序


    笔者使用的是STM32MP157的板子

    1 怎么写 LED 驱动程序?

    详细步骤如下:
    ① 看原理图确定引脚,确定引脚输出什么电平才能点亮/熄灭 LED
    ② 看主芯片手册,确定寄存器操作方法:哪些寄存器?哪些位?地址是?
    ③ 编写驱动:先写框架,再写硬件操作的代码

    注意:在芯片手册中确定的寄存器地址被称为物理地址,在 Linux 内核中无法直接使用。

    需要使用内核提供的 ioremap 把物理地址映射为虚拟地址,使用虚拟地址。

    ioremap 函数的使用:

    1. #include
    2. void __iomem *ioremap(resource_size_t res_cookie, size_t size);

    把物理地址 phys_addr 开始的一段空间(大小为 size),映射为虚拟地址;返回值是该段虚拟地址的首地址。

    实际上,它是按页(4096 字节)进行映射的,是整页整页地映射的。
    假设 phys_addr = 0x10002, size=4, ioremap 的内部实现是:
    a) phys_addr 按页取整,得到地址 0x10000
    b) size 按页取整,得到 4096
    c) 把起始地址 0x10000,大小为 4096 的这一块物理地址空间,映射到虚拟地址空间,
    假设得到的虚拟空间起始地址为 0xf0010000
    d) 那么 phys_addr = 0x10002 对应的 virt_addr = 0xf0010002
    ③ 不再使用该段虚拟地址时,要 iounmap(virt_addr):

    void iounmap(volatile void __iomem *cookie);

    为什么有ioremap,这里解释的很清楚了。

    同一个程序,同时运行2次,在内存中有两份代码,他们地址是不同的,但是打印出来的结果是一样的(虚拟地址),主要是MMU(内存管理单元)在起作用,完成物理地址到虚拟地址的转换。

    感怪怪的,图中代码是全局变量

    根据进程号转换成不同的物理地址。MMU将物理地址映射成虚拟地址,内核通过虚拟地址访问uart等硬件。

    2 修改

    修改之期的led_operations结构体,由它控制点灯的个数

    led_operations.h

    1. #ifndef LED_OPERATIONS_H
    2. #define LED_OPERATIONS_H
    3. struct led_operations {
    4. int num; // 灯的数量
    5. int (*init) (int which); // 初始化LED,which是哪一个LED
    6. int (*ctl) (int which, char status); // 控制LED,which-哪一个LED,status-1亮,0灭
    7. };
    8. // 返回结构体指针
    9. struct led_operations* get_board_led_operations(void);
    10. #endif

    stmp32mp157.c

    (主要框架还是board_demo.c的,结合了之前的 韦东山嵌入式linux系列-LED驱动程序-CSDN博客

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include "led_operations.h"
    14. // 不能使用物理地址,需要映射
    15. // 1寄存器
    16. // RCC_PLL4CR地址:0x50000000 + 0x894,提供时钟的
    17. static volatile unsigned int* RCC_PLL4CR;
    18. // 2使能GPIOA本身
    19. // RCC_MP_AHB4ENSETR地址:0x50000000 + 0xA28
    20. static volatile unsigned int* RCC_MP_AHB4ENSETR;
    21. // 3设置引脚为输出模式
    22. // GPIOA_MODER地址:0x50002000 + 0x00,设置bit[21:20]=0b01,用于输出模式
    23. static volatile unsigned int* GPIOA_MODER;
    24. // 4设置输出电平
    25. // 方法2:直接写寄存器,一次操作即可,高效
    26. // GPIOA_BSRR地址: 0x50002000 + 0x18
    27. static volatile unsigned int* GPIOA_BSRR;
    28. // init函数-配置引脚,把引脚配置成GPIO输出功能
    29. static int board_demo_led_init(int which)
    30. {
    31. printk("%s %s line %d, led %d\n", __FILE__, __FUNCTION__, __LINE__, which);
    32. // 之前没有映射,就映射
    33. if (!RCC_PLL4CR)
    34. {
    35. // 驱动程序访问硬件,必须先ioremap,在这里映射,映射的是一页4k的地址,参考
    36. // ioremap(base_phy, size);
    37. // 1寄存器
    38. // RCC_PLL4CR地址:0x50000000 + 0x894,提供时钟的
    39. // static volatile unsigned int* RCC_PLL4CR;
    40. RCC_PLL4CR = ioremap(0x50000000 + 0x894, 4);
    41. // 2使能GPIOA本身
    42. // RCC_MP_AHB4ENSETR地址:0x50000000 + 0xA28
    43. // static volatile unsigned int* RCC_MP_AHB4ENSETR;
    44. RCC_MP_AHB4ENSETR = ioremap(0x50000000 + 0xA28, 4);
    45. // 3设置引脚为输出模式
    46. // GPIOA_MODER地址:0x50002000 + 0x00,设置bit[21:20]=0b01,用于输出模式
    47. // static volatile unsigned int* GPIOA_MODER;
    48. GPIOA_MODER = ioremap(0x50002000 + 0x00, 4);
    49. // 4设置输出电平
    50. // 方法2:直接写寄存器,一次操作即可,高效
    51. // GPIOA_BSRR地址: 0x50002000 + 0x18
    52. // static volatile unsigned int* GPIOA_BSRR;
    53. GPIOA_BSRR = ioremap(0x50002000 + 0x18, 4);
    54. }
    55. // 初始化引脚
    56. if (which == 0)
    57. {
    58. // 使能PLL4,是所有GPIO的时钟
    59. *RCC_PLL4CR |= (1 << 0); // 设置bit0为1
    60. while ((*RCC_PLL4CR & (1 << 1)) == 0); // 如果bit1一直为0的话,就等待
    61. // 使能GPIOA
    62. *RCC_MP_AHB4ENSETR |= (1 << 0); // 1左移0位
    63. // 将GPIOA的第十个引脚配置成GPIO
    64. // 配置GPIO是输出模式,只有用户程序open的时候,才表示要使用这个引脚,这个时候再配置引脚
    65. *GPIOA_MODER &= ~(3 << 20); // 清零 11左移20位,取反,
    66. *GPIOA_MODER |= (1 << 20); // 20位设置成1,配置成01,输出模式
    67. }
    68. return 0;
    69. }
    70. // ctl函数-通过参数把引脚设置成高/低电平
    71. static int board_demo_led_ctl(int which, char status)
    72. {
    73. printk("%s %s line %d, led %d, %s\n",
    74. __FILE__, __FUNCTION__, __LINE__, which, status ? "on" : "off");
    75. // 设置高/低电平
    76. if (which == 0)
    77. {
    78. // 设置GPIOA10寄存器1/0
    79. if (status)
    80. {
    81. // 设置led on,让引脚输出低电平
    82. *GPIOA_BSRR = (1 << 26); // 1左移26
    83. }
    84. else
    85. {
    86. // 设置led off,让引脚输出高电平
    87. *GPIOA_BSRR = (1 << 10); // 1左移10
    88. }
    89. }
    90. return 0;
    91. }
    92. // 加一个num属性
    93. static struct led_operations board_demo_led_operations = {
    94. .num = 1,
    95. .init = board_demo_led_init,
    96. .ctl = board_demo_led_ctl,
    97. };
    98. // 返回结构体
    99. struct led_operations* get_board_led_operations(void)
    100. {
    101. return &board_demo_led_operations;
    102. }

    led_drv.c

    1. /*************************************************************************
    2. > File Name: led.drv.c
    3. > Author: Winter
    4. > Created Time: Sun 07 Jul 2024 12:35:19 AM EDT
    5. ************************************************************************/
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include
    14. #include
    15. #include
    16. #include
    17. #include
    18. #include
    19. #include
    20. #include
    21. #include "led_operations.h"
    22. // #define LED_NUM 2
    23. // 1确定主设备号,也可以让内核分配
    24. static int major = 0; // 让内核分配
    25. static struct class *led_class;
    26. struct led_operations* p_led_operations;
    27. #define MIN(a, b) (a < b ? a : b)
    28. // 3 实现对应的 drv_open/drv_read/drv_write 等函数,填入 file_operations 结构体
    29. static ssize_t led_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
    30. {
    31. printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    32. return 0;
    33. }
    34. // write(fd, &val, 1);
    35. static ssize_t led_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
    36. {
    37. int err;
    38. char status;
    39. struct inode* node;
    40. int minor;
    41. printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    42. // 把用户区的数据buf拷贝到内核区status,即向写到内核status中写数据
    43. err = copy_from_user(&status, buf, 1);
    44. // 根据次设备号和status控制LED
    45. node = file_inode(file);
    46. minor = iminor(node);
    47. p_led_operations->ctl(minor, status);
    48. return 1;
    49. }
    50. static int led_drv_open (struct inode *node, struct file *file)
    51. {
    52. int minor;
    53. printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    54. // 得到次设备号
    55. minor = iminor(node);
    56. // 根据次设备号初始化LED
    57. p_led_operations->init(minor);
    58. return 0;
    59. }
    60. static int led_drv_close (struct inode *node, struct file *file)
    61. {
    62. printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    63. return 0;
    64. }
    65. // 2定义自己的 file_operations 结构体
    66. static struct file_operations led_drv = {
    67. .owner = THIS_MODULE,
    68. .open = led_drv_open,
    69. .read = led_drv_read,
    70. .write = led_drv_write,
    71. .release = led_drv_close,
    72. };
    73. // 4把 file_operations 结构体告诉内核: register_chrdev
    74. // 5谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数
    75. static int __init led_init(void)
    76. {
    77. int err, i;
    78. printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    79. // 注册led_drv,返回主设备号
    80. major = register_chrdev(0, "winter_led", &led_drv); /* /dev/led */
    81. // 创建class
    82. led_class = class_create(THIS_MODULE, "led_class");
    83. err = PTR_ERR(led_class);
    84. if (IS_ERR(led_class)) {
    85. printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    86. unregister_chrdev(major, "led_class");
    87. return -1;
    88. }
    89. // 入口函数获得结构体指针
    90. p_led_operations = get_board_led_operations();
    91. // 创建device
    92. // 根据次设备号访问多个LED
    93. // device_create(led_class, NULL, MKDEV(major, 0), NULL, "winter_led0"); /* /dev/winter_led0 */
    94. // device_create(led_class, NULL, MKDEV(major, 1), NULL, "winter_led1"); /* /dev/winter_led1 */
    95. for (i = 0; i < p_led_operations->num; i++)
    96. {
    97. device_create(led_class, NULL, MKDEV(major, i), NULL, "winter_led%d", i);
    98. }
    99. return 0;
    100. }
    101. // 6有入口函数就应该有出口函数:卸载驱动程序时,出口函数调用unregister_chrdev
    102. static void __exit led_exit(void)
    103. {
    104. int i;
    105. printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    106. for (i = 0; i < p_led_operations->num; i++)
    107. {
    108. device_destroy(led_class, MKDEV(major, i));
    109. }
    110. class_destroy(led_class);
    111. // 卸载
    112. unregister_chrdev(major, "winter_led");
    113. }
    114. // 7其他完善:提供设备信息,自动创建设备节点: class_create,device_create
    115. module_init(led_init);
    116. module_exit(led_exit);
    117. MODULE_LICENSE("GPL");

    led_drv_test.c

    1. /*************************************************************************
    2. > File Name: hello_test.c
    3. > Author: Winter
    4. > Created Time: Sun 07 Jul 2024 01:39:39 AM EDT
    5. ************************************************************************/
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. /*
    13. * ./led_drv /dev/winter_led0 on
    14. * ./led_drv /dev/winter_led0 off
    15. */
    16. int main(int argc, char **argv)
    17. {
    18. int fd;
    19. char status;
    20. /* 1. 判断参数 */
    21. if (argc < 2)
    22. {
    23. printf("Usage: %s \n", argv[0]);
    24. return -1;
    25. }
    26. /* 2. 打开文件 */
    27. fd = open(argv[1], O_RDWR);
    28. if (fd == -1)
    29. {
    30. printf("can not open file %s\n", argv[1]);
    31. return -1;
    32. }
    33. /* 3. 写文件 */
    34. if (0 == strcmp(argv[2], "on"))
    35. {
    36. status = 1;
    37. }
    38. else
    39. {
    40. status = 0;
    41. }
    42. write(fd, &status, 1);
    43. close(fd);
    44. return 0;
    45. }

    Makefile

    1. # 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
    2. # 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
    3. # 2.1 ARCH, 比如: export ARCH=arm64
    4. # 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
    5. # 2.3 PATH, 比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
    6. # 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
    7. # 请参考各开发板的高级用户使用手册
    8. KERN_DIR = /home/book/100ask_stm32mp157_pro-sdk/Linux-5.4
    9. all:
    10. make -C $(KERN_DIR) M=`pwd` modules
    11. $(CROSS_COMPILE)gcc -o led_drv_test led_drv_test.c
    12. clean:
    13. make -C $(KERN_DIR) M=`pwd` modules clean
    14. rm -rf modules.order
    15. rm -f led_drv_test
    16. # 参考内核源码drivers/char/ipmi/Makefile
    17. # 要想把a.c, b.c编译成ab.ko, 可以这样指定:
    18. # ab-y := a.o b.o
    19. # obj-m += ab.o
    20. # leddrv.c board_demo.c 编译成 100ask.ko
    21. winter_led-y := led_drv.o stm32mp157.o
    22. obj-m += winter_led.o

    编译

    make

    3 测试

    在开发板挂载 Ubuntu 的NFS目录

    mount -t nfs -o nolock,vers=3 192.168.5.11:/home/book/nfs_rootfs/ /mnt

    将ko文件和测试代码拷贝到挂载目录,安装驱动

    insmod winter_led.ko

    执行测试程序

    1. ./led_drv_test /dev/winter_led0 on
    2. ./led_drv_test /dev/winter_led0 off

    板子上只有log,关掉【心跳灯】

    1. ls /sys/class/leds/
    2. echo none > /sys/class/leds/heartbeat/trigger

    再执行on/off就可以看到灯的亮灭了

    4 思考

    a.在驱动里有 ioremap,什么时候执行 iounmap?请完善程序

    答:需要在led_operations.h和stmp32mp157.c中加一个close函数,在close函数中执行iounmap操作,在led_drv.c的close函数中调用。

    似乎不太行

    b.视频里我们只实现了点一个 LED,修改代码支持两个 LED。

    答:需要看原理图和手册,在stmp32mp157.c的init函数中配置引脚和输出高低电平。

  • 相关阅读:
    SpringMVC文件上传
    请求被中止: 未能创建 SSL/TLS 安全通道
    【遍历二叉树的非递归算法,二叉树的层次遍历】
    数据结构与算法-队列
    【CSS】5分钟带你彻底搞懂 W3C & IE 盒模型
    SSM框架全套笔记
    手机 IOS 软件 IPA 签名下载安装详情图文教程
    微信小程序python+nodejs+php+springboot+vue 学习资料销售平台
    Qt之读写文件
    opengl 学习着色器
  • 原文地址:https://blog.csdn.net/Zhouzi_heng/article/details/140397160