• 驱动程序中调用的内核函数,在 4.x 版本的内核里都是一样的
  • LED驱动编写详细步骤如下:

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

  • 需要注意的是:
    • 在芯片手册中确定的寄存器地址被称为物理地址,在 Linux 内核中无法直接使用。
    • 需要使用内核提供的 ioremap 把物理地址映射为虚拟地址,使用虚拟地址。
    • image.png
    • image.png

实际上,它是按页(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

  • 即使传入的是4byte大小也将会映射成4096一页的大小
  • 不再使用该段虚拟地址时,要 iounmap(virt_addr):image.png
    • 为什么需要ioremap?
  • image.png
  • 运行程序两次,那么在内存中必定会由两份代码
  • 同一个程序,运行时打印的全局变量的地址应该是一样的才对;但是运行两次实际内存保存的是两份代码,即两者全局变量在内存中的物理地址是不同的;显然,程序中打印的不是物理地址,而是CPU看到的虚拟地址
  • CPU使用同一个地址来访问同时运行的两个程序;根据不同的pid进程号进而访问不同的物理地址
  • 使用MMU来实现虚拟地址到物理地址的转换
  • 内核发出一个地址,能不能访问到硬件也是由MMU来决定的;mmu需要把物理地址映射成虚拟地址,内核才能使用虚拟地址来访问硬件
    • volatile的使用
  • 为了避免编译器自动优化,需要加上 volatile,告诉它“这是容易出错的,别乱优化”
  • volatile int *p = ioremap(xxxx, 4); // GPIO 寄存器的地址
    • 每个单板所使用的Linux内核、头文件可能不一样,在编写makefile时需指定对应board内核的目录,在对应的内核中编译
    • 修改board结构体
      1. struct led_operations {
      2. int num; /* LED灯的数量 */
      3. int (*init) (int which); /* 初始化LED, which-哪个LED */
      4. int (*ctl) (int which, char status); /* 控制LED, which-哪个LED, status:1-亮,0-灭 */
      5. int (*exit) (int which); /* 退出LED */
      6. };
  • 修改board的LED操作代码 ```c

    include

include “led_opr.h”

static volatile unsigned int CCM_CCGR1 ; static volatile unsigned int IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3; static volatile unsigned int GPIO5_GDIR ; static volatile unsigned int GPIO5_DR ;

static int board_demo_led_init (int which) / 初始化LED, which-哪个LED /
{ unsigned int val;

  1. //printk("%s %s line %d, led %d\n", __FILE__, __FUNCTION__, __LINE__, which);
  2. if (which == 0)
  3. {
  4. if (!CCM_CCGR1)
  5. {
  6. CCM_CCGR1 = ioremap(0x20C406C, 4);
  7. IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = ioremap(0x2290014, 4);
  8. GPIO5_GDIR = ioremap(0x020AC000 + 0x4, 4);
  9. GPIO5_DR = ioremap(0x020AC000 + 0, 4);
  10. }
  11. /* GPIO5_IO03 */
  12. /* a. 使能GPIO5
  13. * set CCM to enable GPIO5
  14. * CCM_CCGR1[CG15] 0x20C406C
  15. * bit[31:30] = 0b11
  16. */
  17. *CCM_CCGR1 |= (3<<30);
  18. /* b. 设置GPIO5_IO03用于GPIO
  19. * set IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3
  20. * to configure GPIO5_IO03 as GPIO
  21. * IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 0x2290014
  22. * bit[3:0] = 0b0101 alt5
  23. */
  24. val = *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3;
  25. val &= ~(0xf);
  26. val |= (5);
  27. *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = val;
  28. /* b. 设置GPIO5_IO03作为output引脚
  29. * set GPIO5_GDIR to configure GPIO5_IO03 as output
  30. * GPIO5_GDIR 0x020AC000 + 0x4
  31. * bit[3] = 0b1
  32. */
  33. *GPIO5_GDIR |= (1<<3);
  34. }
  35. return 0;

}

static int boarddemoledctl (int which, char status) / 控制LED, which-哪个LED, status:1-亮,0-灭 / { //printk(“%s %s line %d, led %d, %s\n”, FILE, _FUNCTION, __LINE, which, status ? “on” : “off”); if (which == 0) { if (status) / on: output 0/ { /* d. 设置GPIO5_DR输出低电平

  1. * set GPIO5_DR to configure GPIO5_IO03 output 0
  2. * GPIO5_DR 0x020AC000 + 0
  3. * bit[3] = 0b0
  4. */
  5. *GPIO5_DR &= ~(1<<3);
  6. }
  7. else /* off: output 1*/
  8. {
  9. /* e. 设置GPIO5_IO3输出高电平
  10. * set GPIO5_DR to configure GPIO5_IO03 output 1
  11. * GPIO5_DR 0x020AC000 + 0
  12. * bit[3] = 0b1
  13. */
  14. *GPIO5_DR |= (1<<3);
  15. }
  16. }
  17. return 0;

}

/ 自己添加的 / static int board_demo_led_exit(int which) { if (which == 0) { board_demo_led_ctl(which, 0); / 将相关寄存器的值恢复为默认 / //不去设置 b[31:30],GPIO5 也是默认使能的

  1. /* 取消映射 */
  2. iounmap( CCM_CCGR1 );
  3. iounmap( IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 );
  4. iounmap( GPIO5_GDIR );
  5. iounmap( GPIO5_DR );
  6. }
  7. return 0;

} static struct led_operations board_demo_led_opr = { .num = 1, .init = board_demo_led_init, .ctl = board_demo_led_ctl, .exit = board_demo_led_exit, };

struct led_operations *get_board_led_opr(void) { return &board_demo_led_opr; }

  1. - leddrv.copen函数中调用led_operationsinitclose函数中调用led_operationsexit
  2. ```c
  3. static int led_drv_open (struct inode *node, struct file *file)
  4. {
  5. int minor = iminor(node);
  6. printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
  7. /* 根据次设备号初始化LED */
  8. p_led_opr->init(minor);
  9. return 0;
  10. }
  11. static int led_drv_close (struct inode *node, struct file *file)
  12. {
  13. int minor = iminor(node);
  14. printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
  15. p_led_opr->exit(minor);
  16. return 0;
  17. }
  • 使用 QEMU 模拟的硬件,它的硬件资源可以随意扩展。
  • IMX6ULL-QEMU代码 ```c struct iomux { volatile unsigned int unnames[23]; volatile unsigned int IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO00; / offset 0x5c/ volatile unsigned int IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO01; volatile unsigned int IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO02; volatile unsigned int IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO03; volatile unsigned int IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO04; volatile unsigned int IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO05; volatile unsigned int IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO06; volatile unsigned int IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO07; volatile unsigned int IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO08; volatile unsigned int IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO09; };

struct imx6ull_gpio { volatile unsigned int dr; volatile unsigned int gdir; volatile unsigned int psr; volatile unsigned int icr1; volatile unsigned int icr2; volatile unsigned int imr; volatile unsigned int isr; volatile unsigned int edge_sel; };

/ enable GPIO1,GPIO5 / static volatile unsigned int *CCM_CCGR1;

/ set GPIO5_IO03 as GPIO / static volatile unsigned int *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3;

static struct iomux *iomux;

static struct imx6ull_gpio gpio1; static struct imx6ull_gpio gpio5;

static int board_demo_led_init (int which) / 初始化LED, which-哪个LED /
{ if (!CCM_CCGR1) { CCM_CCGR1 = ioremap(0x20C406C, 4); IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = ioremap(0x2290014, 4);

  1. iomux = ioremap(0x20e0000, sizeof(struct iomux));
  2. gpio1 = ioremap(0x209C000, sizeof(struct imx6ull_gpio));
  3. gpio5 = ioremap(0x20AC000, sizeof(struct imx6ull_gpio));
  4. }
  5. if (which == 0)
  6. {
  7. /* 1. enable GPIO5
  8. * CG15, b[31:30] = 0b11
  9. */
  10. *CCM_CCGR1 |= (3<<30);
  11. /* 2. set GPIO5_IO03 as GPIO
  12. * MUX_MODE, b[3:0] = 0b101
  13. */
  14. *IOMUXC_SNVS_SW_MUX_CTL_PAD_SNVS_TAMPER3 = 5;
  15. /* 3. set GPIO5_IO03 as output
  16. * GPIO5 GDIR, b[3] = 0b1
  17. */
  18. gpio5->gdir |= (1<<3);
  19. }
  20. else if(which == 1)
  21. {
  22. /* 1. enable GPIO1
  23. * CG13, b[27:26] = 0b11
  24. */
  25. *CCM_CCGR1 |= (3<<26);
  26. /* 2. set GPIO1_IO03 as GPIO
  27. * MUX_MODE, b[3:0] = 0b101
  28. */
  29. iomux->IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO03 = 5;
  30. /* 3. set GPIO1_IO03 as output
  31. * GPIO1 GDIR, b[3] = 0b1
  32. */
  33. gpio1->gdir |= (1<<3);
  34. }
  35. else if(which == 2)
  36. {
  37. /* 1. enable GPIO1
  38. * CG13, b[27:26] = 0b11
  39. */
  40. *CCM_CCGR1 |= (3<<26);
  41. /* 2. set GPIO1_IO05 as GPIO
  42. * MUX_MODE, b[3:0] = 0b101
  43. */
  44. iomux->IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO05 = 5;
  45. /* 3. set GPIO1_IO05 as output
  46. * GPIO1 GDIR, b[5] = 0b1
  47. */
  48. gpio1->gdir |= (1<<5);
  49. }
  50. else if(which == 3)
  51. {
  52. /* 1. enable GPIO1
  53. * CG13, b[27:26] = 0b11
  54. */
  55. *CCM_CCGR1 |= (3<<26);
  56. /* 2. set GPIO1_IO06 as GPIO
  57. * MUX_MODE, b[3:0] = 0b101
  58. */
  59. iomux->IOMUXC_SW_MUX_CTL_PAD_GPIO1_IO06 = 5;
  60. /* 3. set GPIO1_IO06 as output
  61. * GPIO1 GDIR, b[6] = 0b1
  62. */
  63. gpio1->gdir |= (1<<6);
  64. }
  65. //printk("%s %s line %d, led %d\n", __FILE__, __FUNCTION__, __LINE__, which);
  66. return 0;

}

static int boarddemoledctl (int which, char status) / 控制LED, which-哪个LED, status:1-亮,0-灭 / { //printk(“%s %s line %d, led %d, %s\n”, FILE, _FUNCTION, __LINE, which, status ? “on” : “off”); if (which == 0) { if (status) / on : output 0 / gpio5->dr &= ~(1<<3); else /* on : output 1 */ gpio5->dr |= (1<<3); } else if (which == 1) { if (status) /* on : output 0 */ gpio1->dr &= ~(1<<3); else /* on : output 1 */ gpio1->dr |= (1<<3); } else if (which == 2) { if (status) /* on : output 0 */ gpio1->dr &= ~(1<<5); else /* on : output 1 */ gpio1->dr |= (1<<5); } else if (which == 3) { if (status) /* on : output 0 */ gpio1->dr &= ~(1<<6); else /* on : output 1 */ gpio1->dr |= (1<<6); } return 0; }

static struct led_operations board_demo_led_opr = { .num = 4, .init = board_demo_led_init, .ctl = board_demo_led_ctl, };

struct led_operations *get_board_led_opr(void) { return &board_demo_led_opr; } ```