《驅動學習 - LED驅動程序》

1.編寫LED驅動程序步驟node

  1.1 框架linux

  1.2 完善硬件操做框架

    1.2.1 查看原理圖函數

    1.2.2 看2440手冊this

    1.2.3 寫代碼spa

其中須要注意的是:單片機一般直接用物理地址去操做寄存器。而這裏要將物理地址映射爲虛擬地址,用ioremap函數。3d

 

2.查看原理圖和2440手冊指針

  

   

  從mini2440的原理圖能夠看到,2440是低電平點亮LED。而後繼續查看nLED引腳  code

  

 

  因此對應的是:blog

  

 

  查看上s3c2440芯片手冊:

  能夠看到GPBCON和GPBDAT的物理地址,這個是後面點亮LED的須要操做的兩個寄存器。

   須要把GPB4.5.6.7設置爲Output模式。

 

 3.編寫LED驅動代碼

volatile unsigned long *gpbcon = NULL; volatile unsigned long *gpbdat = NULL; static int first_drv_init(void) { gpbcon = (volatile unsigned long *)ioremap(0x56000010, 16); gpbdat = gpbcon + 1; }

  經過ioremap講CPBCON的物理地址映射爲虛擬地址。其中第一個參數0x56000010是GPBCON的物理地址,第二個參數是映射的大小。好比一個寄存器是32位的,那麼寫4就能夠了。

  gpbdat = gpbcon + 1是由於GPBDAT和GPBCON的物理地址相差0x4,也就是32位。GPBCON就是32位的指針類型。因此指針+1操做就是偏移32位。

static void first_drv_exit(void) { iounmap(gpbcon); }

  而後在卸載函數中把映射取消。

 

 

static int first_drv_open(struct inode *inode, struct file *file) { //printk("first_drv_open\n");
    /* * LED1,LED2,LED4對應GPB五、GPB六、GPB七、GPB8 */
    /* 配置GPB5,6,7,8爲輸出 */
    *gpbcon &= ~((0x3<<(5*2)) | (0x3<<(6*2)) | (0x3<<(7*2)) | (0x3<<(8*2))); *gpbcon |= ((0x1<<(5*2)) | (0x1<<(6*2)) | (0x1<<(7*2)) | (0x1<<(8*2))); return 0; }

 

  先將GPBCON的5.6.7.8清零,而後再或上1。這樣就將GPBCON的5.6.7.8引腳設置爲了Output模式。

  

static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos) { int val; //printk("first_drv_write\n"); copy_from_user(&val, buf, count); // copy_to_user();
if (val == 1) { // 點燈 *gpbdat &= ~((1<<5) | (1<<6) | (1<<7) | (1<<8)); } else { // 滅燈 *gpbdat |= (1<<5) | (1<<6) | (1<<7) | (1<<8); } return 0; }

  copy_from_user(&val, buf, count)將用戶空間的數據拷貝到內核空間。copy_to_user將內核空間中的數據拷貝到用戶空間。

  其中在應用程序中會調用write函數。

int main(int argc, char **argv) { int fd; int val = 1; 中間略 write(fd, &val, 4); return 0; }

 

  &val對應*buf,4對應count。因此就數據就這樣拷貝到內核空間。(write的第三個參數是字節數,val是init類型的變量,因此是4個字節)

 

以上一個簡單的LED驅動程序就寫完了。

還有一種是讀取此設備號,能夠經過此設備號去開光每一個單獨的燈。後面能夠作一下試驗。

 

4.編譯驅動程序

Makefile

KERN_DIR = /work/system/linux-2.6.22.6 all: make -C $(KERN_DIR) M=`pwd` modules clean: make -C $(KERN_DIR) M=`pwd` modules clean rm -rf modules.order obj-m    += first_drv.o

  內核路徑根據自身修改。

  make

  

 

 

   提示錯誤找不到regs-gpio.h。由於我用的是2.6.32.2的內核,而本來驅動程序用的是2.6.22.6。因此有些文件的位置不同。

  find -name regs-gpio.h和find -name hardware.h

  

 

 

   

 

 

   因此修改驅動程序:

#include <asm/arch/regs-gpio.h> #include <asm/hardware.h> 修改爲 #include <mach/regs-gpio.h> #include <mach/hardware.h>

  class_device_create 和 class_device_destroy 是最期版本的API。

  如今已經改爲device_create 和 device_destroy。在包含#include <linux/device.h>中。

  static struct class_device *firstdrv_class_dev要修改爲static struct device *firstdrv_class_dev;

  

 完整代碼:

#include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/delay.h> #include <asm/uaccess.h> #include <asm/irq.h> #include <asm/io.h> #include <mach/regs-gpio.h> #include <mach/hardware.h> #include <linux/device.h>

static struct class *firstdrv_class; static struct device    *firstdrv_class_dev; volatile unsigned long *gpbcon = NULL; volatile unsigned long *gpbdat = NULL; int major; static int first_drv_open(struct inode *inode, struct file *file) { //printk("first_drv_open\n");
    /* * LED1,LED2,LED4對應GPB五、GPB六、GPB七、GPB8 */
    /* 配置GPB5,6,7,8爲輸出 */
    *gpbcon &= ~((0x3<<(5*2)) | (0x3<<(6*2)) | (0x3<<(7*2)) | (0x3<<(8*2))); *gpbcon |= ((0x1<<(5*2)) | (0x1<<(6*2)) | (0x1<<(7*2)) | (0x1<<(8*2))); return 0; } static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos) { int val; //printk("first_drv_write\n");
 copy_from_user(&val, buf, count); // copy_to_user();

    if (val == 1) { // 點燈
        *gpbdat &= ~((1<<5) | (1<<6) | (1<<7) | (1<<8)); } else { // 滅燈
        *gpbdat |= (1<<5) | (1<<6) | (1<<7) | (1<<8); } return 0; } static struct file_operations first_drv_fops = { .owner =   THIS_MODULE,    /* 這是一個宏,推向編譯模塊時自動建立的__this_module變量 */ .open = first_drv_open, .write = first_drv_write, }; static int first_drv_init(void) { major = register_chrdev(0, "first_drv", &first_drv_fops); // 註冊, 告訴內核
 firstdrv_class = class_create(THIS_MODULE, "firstdrv"); firstdrv_class_dev = device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */ gpbcon = (volatile unsigned long *)ioremap(0x56000010, 16); gpbdat = gpbcon + 1; return 0; } static void first_drv_exit(void) { unregister_chrdev(major, "first_drv"); // 卸載
 device_unregister(firstdrv_class_dev); class_destroy(firstdrv_class); iounmap(gpbcon); } module_init(first_drv_init); module_exit(first_drv_exit); MODULE_LICENSE("GPL");
相關文章
相關標籤/搜索