树莓派驱动学习-字符设备驱动(LED)

上传人:cn****1 文档编号:466175152 上传时间:2022-08-12 格式:DOC 页数:17 大小:484.50KB
返回 下载 相关 举报
树莓派驱动学习-字符设备驱动(LED)_第1页
第1页 / 共17页
树莓派驱动学习-字符设备驱动(LED)_第2页
第2页 / 共17页
树莓派驱动学习-字符设备驱动(LED)_第3页
第3页 / 共17页
树莓派驱动学习-字符设备驱动(LED)_第4页
第4页 / 共17页
树莓派驱动学习-字符设备驱动(LED)_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《树莓派驱动学习-字符设备驱动(LED)》由会员分享,可在线阅读,更多相关《树莓派驱动学习-字符设备驱动(LED)(17页珍藏版)》请在金锄头文库上搜索。

1、从上次hello world程序中,我们已经搭建好了驱动学习相关的环境搭建,为接下来的设备驱动做好了准备。同时通过最简单的hello world程序,学习了模块的初始化和退出,知道了如何编写 *_init和*_exit函数,知道了如何通过内核 打印函数printk输出相关信息。Linux中的设备驱动分三大类:字符设备、块设备、网络设备。本篇文章讨论字符型设备程序如何编写,通过简单的LED 驱动程序介绍相关知识。下篇文章介绍杂项设备(mis c)驱动程序的编写,这在实际项目中很常用,相当于字符设备的简化版。首先附上完整程序:plainview plaincopy28.30.从上次hello wo

2、rld程序中,我们已经搭建好了驱动学习相关的环境搭建,为接下来的设备驱动做好了准备。同时通过最28.30.从上次hello world程序中,我们已经搭建好了驱动学习相关的环境搭建,为接下来的设备驱动做好了准备。同时通过最1.2.* Fileled driver.c3.* Author:Fawe n Xu4.* Desc :led driver code5.* History:May 9th 20156.*/7.#in clude 8.#in clude 9.#in clude 10.#in clude 11.#in clude 12.#in clude 13.#in clude 14.#in

3、 clude 15.#in clude 16.#in clude 17.#in clude 18.#in clude 19.#in clude 20.#in clude 21.#in clude 22.23.MODULE_AUTHOR(Fawen Xu);24.MODULE_LICENSE(Dual BSD/GPL);25.26.static un sig ned int led_major=0;27.static un sig ned int bcm2835_gpio_baseaddr;29.module_param(led_major,i nt,0);31.#define LED MAGI

4、C k/*28.30.从上次hello world程序中,我们已经搭建好了驱动学习相关的环境搭建,为接下来的设备驱动做好了准备。同时通过最32.#defi ne LED_ON_CMD _IO(LED_MAGIC,1)28.30.#defi ne LED_OFF_CMD _IO(LED_MAGIC,2)#defi ne LED_OFF_CMD _IO(LED_MAGIC,2)#define BCM2835 PERI BASE0x20000000/#define BCM2835 PERI BASE0x7e000000#define BCM2835 GPIO BASE0x20200000#defin

5、e BCM2835_GPIOReg_GPFSEL00x00000000#define BCM2835_GPIOReg_GPSET00x0000001c#defi ne BCM2835_GPIOReg_GPCLR00x00000028#define BCM2835 GPIO FSEL INP0x00000000#define BCM2835 GPIO FSEL OUTP0x00000001#defi neRPI BPLUS GPIO J8 1218int bcm2835_gpio_fsel(int pin,int mode)volatile int shift,value;volatile in

6、t *bcm2835_gpio_fsel_addrp = bcm2835_gpio_baseaddr + (pi n/10)*4;/prin tk(In bcm2835_gpio_fsel fun ctio n:n);/prin tk( pin = %d, (pi n/10)*4二 %d,pi n,(pi n/10)*4);/prin tk( bcm2835_gpio_baseaddr = 0x%xn, bcm2835_gpio_baseaddr);/prin tk( bcm2835_gpio_fsel_addrp = 0x%pn, bcm2835_gpio_fsel_addrp);shift

7、 = (pin % 10) * 3;value = mode shift;/prin tk( shift=%d,value=0x%xn,shift,value);/prin tk( bcm2835_gpio_fsel addr 0x%p: 0x%xn, bcm2835_gpio_fsel_addrp, *bcm2835_gpio_fsel_addrp);(*bcm2835_gpio_fsel_addrp) = (*bcm2835_gpio_fsel_addrp) | value;/prin tk( bcm2835_gpio_fsel addr 0x%p: 0x%xn, bcm2835_gpio

8、_fsel_addrp, *bcm2835_gpio_fsel_addrp);return 0;int bcm2835_gpio_set(int pin)volatile int shift,value;volatile int *bcm2835_gpio_set_addrp = bcm2835_gpio_baseaddr + BCM2835_GPIOReg_GPSET0 + (pi n/32)*4;/prin tk(In bcm2835_gpio_set fun ctio n:n);/prin tk( bcm2835_gpio_baseaddr = 0x%xn, bcm2835_gpio_b

9、aseaddr);/prin tk( bcm2835_gpio_set_addrp = 0x%pn, bcm2835_gpio_set_addrp);shift = pin % 32;value = 1 shift;/prin tk( shift=%d,value=0x%xn,shift,value);33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73./prin tk( bcm2835_gpio_se

10、t addr Ox%p: Ox%xn, bcm2835_gpio_set_addrp, *bcm2835_gpio_set_addrp);74.(*bcm2835_gpio_set_addrp) = (*bcm2835_gpio_set_addrp) | value;75./prin tk( bcm2835_gpio_set addr 0x%p: 0x%xn, bcm2835_gpio_set_addrp, *bcm2835_gpio_set_addrp);76.return 0;77.78.int bcm2835_gpio_set_release(i nt pin)79.80.volatil

11、e int shift,value;81.volatile int *bcm2835_gpio_set_release_addrp = bcm2835_gpio_baseaddr + BCM2835_GPIOReg_GPSET0+ (pin/32)*4;82./printk(In bcm2835_gpio_set_release function:n);83./prin tk( bcm2835_gpio_baseaddr = 0x%xn, bcm2835_gpio_baseaddr);84./prin tk( bcm2835_gpio_set_release_addrp = 0x%pn, bc

12、m2835_gpio_set_release_addrp);85.86.shift = pin % 32;87.88.value =(1 shift);89./prin tk( shift=%d,value=0x%xn,shift,value);90./prin tk( bcm2835_gpio_set_release addr 0x%p: 0x%xn, bcm2835_gpio_set_release_addrp, *bcm2835_gpio_set_release_addrp);91.(*bcm2835_gpio_set_release_addrp) = (*bcm2835_gpio_set_release_addrp) & value;92./prin tk( bcm2835_gpio_set_release addr 0x%p: 0x%xn, bcm2835_gpio_set_release_addrp, *bcm2835_gpio_set_release_addrp);93.return 0;94.95.int bcm2835_gpio_clr(i nt pin)96.97.volatile

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号