字元裝置驅動程式之LED驅動程式__韋老師linux視頻源碼

來源:互聯網
上載者:User

虛擬機器ubuntu 11.04

linux-2.6.32.2

mini2440 

題外話:我是個初學者,一直想學linux編程, 但是平時工作的平台是單片機,沒有使用上,但是自己心真的不甘,希望能夠征服它。平台是大四時候購買的mini2440,4年了,現在把它拿出來學習。再不學習就老了。廢話不說了。直接上原始碼。

first_drv.c

#include <linux/module.h>#include <linux/kernel.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/delay.h>#include <linux/device.h>#include <mach/irqs.h>#include <mach/regs-gpio.h>#include <mach/hardware.h>#include <asm/uaccess.h>#include <asm/io.h>static struct class *firstdrv_class;static struct class_device*firstdrv_class_dev;volatile unsigned long *gpbcon = NULL;volatile unsigned long *gpbdat = NULL;static int first_drv_open(struct inode *inode, struct file *file){printk("first_drv_open\n");// gpb5,6,7,8 output*gpbcon &= ~((0x03<<(5*2)) | (0x03<<(6*2)) | (0x03<<(7*2)) |(0x03<<(8*2)) );*gpbcon |= ((0x01<<(5*2)) | (0x01<<(6*2)) | (0x01<<(7*2)) |(0x01<<(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){     //led on*gpbdat &= ~((1<<5) | (1<<6) | (1<<7) | (1<<8));}else{        //led off*gpbdat |= (1<<5) | (1<<6) | (1<<7) | (1<<8);}return 0;}static struct file_operations first_drv_fops = {    .owner  =   THIS_MODULE,        .open   =   first_drv_open,     .write=first_drv_write,   };int major;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_destroy(firstdrv_class,firstdrv_class_dev);}module_init(first_drv_init);module_exit(first_drv_exit);MODULE_LICENSE("GPL");

firstdrvtest.c

#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>/* firstdrvtest on  * firstdrvtest off  */int main(int argc, char **argv){int fd;int val = 1;fd = open("/dev/xyz", 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.