linux驅動之LED驅動_1,linuxled_1

來源:互聯網
上載者:User

linux驅動之LED驅動_1,linuxled_1

步驟:

1、架構

2、完善硬體的操作:

    a.看原理圖、引腳

    b.看2440手冊

    c.寫代碼: IO口需要用ioremap映射

我的板子電路如下所示


1、配置GPBCON 寄存器,配置輸出   在open函數中去配置

2、配置GPBDAT 寄存器                        在write函數中去配置

#########################################################################

手冊資料:

GPBCON 寄存器的物理地址為:0x56000010

GPBDAT  寄存器的物理地址為:0x56000014

#########################################################################

1、首先聲明全域變數:

volatile unsigned long *gpbcon = NULL;volatile unsigned long *gpbdat = NULL;

2、在入口函數中對引腳進行映射

/*映射物理地址*/gpbcon = (volatile unsigned long *) ioremap(0x56000010,16);gpbdat = gpbcon + 1;
3、在出口函數中取消映射

iounmap(gpbcon);

4、在open函數中將引腳設定為輸出

/*配置 GPB 5 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)));// 配置為1 (輸出)

5、在寫函數中對使用者空間資料進行簡單處理

int val;copy_from_user(&val,buf,count);//從使用者空間向核心空間拷貝資料if(val == 1){//點燈*gpbdat &=~((1<<5)|(1<<6)|(1<<7)|(1<<8));}else{//滅燈*gpbdat|=(1<<5)|(1<<6)|(1<<7)|(1<<8);}

完整的驅動代碼如下:

#include <linux/init.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/fs.h>#include <asm/io.h>#include <linux/cdev.h>#include <linux/device.h>#include <asm/uaccess.h>static struct class *firstdrv_class;static struct class_devices *firstdrv_class_dev;volatile unsigned long *gpbcon = NULL;volatile unsigned long *gpbdat = NULL;MODULE_LICENSE("Dual BSD/GPL");static int first_dev_open(struct inode *inode,struct file *file){/*配置 GPB 5 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)));// 配置為1 (輸出)//printk("first dev open\n");return 0;}static ssize_t first_dev_write(struct file *file,const char __user *buf,size_t count,loff_t *ppos){int val;copy_from_user(&val,buf,count);//從使用者空間向核心空間拷貝資料if(val == 1){//點燈*gpbdat &=~((1<<5)|(1<<6)|(1<<7)|(1<<8));}else{//滅燈*gpbdat|=(1<<5)|(1<<6)|(1<<7)|(1<<8);}//printk("first dev write\n");return 0;}static struct file_operations first_sdv_fops ={.owner = THIS_MODULE,.open  = first_dev_open,.write = first_dev_write,};int major;int first_drv_init(void){major = register_chrdev(0,"first_drv",&first_sdv_fops);//註冊firstdrv_class = class_create(THIS_MODULE,"first_drv");if(IS_ERR(firstdrv_class))return PTR_ERR(firstdrv_class);firstdrv_class_dev = device_create(firstdrv_class,NULL,MKDEV(major,0),NULL,"wq_device");if(unlikely(IS_ERR(firstdrv_class_dev)))return PTR_ERR(firstdrv_class_dev);/*映射物理地址*/gpbcon = (volatile unsigned long *) ioremap(0x56000010,16);gpbdat = gpbcon + 1;//printk("init   major= %d\n",major);return 0;}void first_dev_exit(void){//printk("exit\n");unregister_chrdev(major,"first_drv");//卸載device_unregister(firstdrv_class_dev);class_destroy(firstdrv_class);iounmap(gpbcon);}module_init(first_drv_init);module_exit(first_dev_exit);

完整的測試代碼如下:

#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>/*wq_device on  開啟 *wq_device off 關閉 */int main(int argc, char **argv){int fd;int val = 1;fd = open("/dev/wq_device",O_RDWR);if(fd < 0){printf("can't open \n");}if(argc != 2){printf("Usage :\n");printf("%s <on|off>\n",argv[0]);return 0;}if(strcmp(argv[1],"on") == 0){val =1;}else{val =0;}write(fd,&val,4);return 0;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.