历史上的今天
今天是:2024年11月14日(星期四)
2019年11月14日 | TQ2440开发板 Linux第一个驱动--点灯
2019-11-14 来源:51hei
我用的是TQ2440开发板,这个程序是参考韦东山的.
4盏LED灯
以下是驱动程序
#include #include #include #include #include #include #include #include #include #include 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_openn"); /* 配置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_writen"); 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, }; 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 = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */ gpbcon = (volatile unsigned long *)ioremap(0x56000010, 16); //0x56000010是的GPIOB的 gpbdat = gpbcon + 1; printk("first_drv_init...n"); return 0; } static void first_drv_exit(void) { unregister_chrdev(major, "first_drv"); // 卸载 class_device_unregister(firstdrv_class_dev); class_destroy(firstdrv_class); iounmap(gpbcon); printk("first_drv_exit...n"); } module_init(first_drv_init); module_exit(first_drv_exit); MODULE_LICENSE("GPL"); 以下是测试程序 #include #include #include #include /* 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 return 0; } if (strcmp(argv[1], "on") == 0) { val = 1; } else { val = 0; } write(fd, &val, 4); return 0; }
上一篇:u-boot 第一阶段启动流程
下一篇:为madplay编写应用程序
史海拾趣
|
我们大厦被投诉最多的是空调,几乎都是VAV。反应慢,可控性差,该出风时不出风,不该出风时狂出...... 最后害得技工不得不在电脑上控制几乎每个VAV,快把人累死。 其实最好的方式是在温控器上能直接控制风阀大小,不要什么VAV,什么PID.....其实 ...… 查看全部问答> |
|
中心议题: 城市轨道交通控制系统和电源系统需要加装滤波器介绍电源滤波器的基本概念、参数选取以及安装原则等几 个方面分析电源滤波器得出相关结论 解决方案: 安装无源EMI滤波器,减少干扰和衰减采用横截面积较大的磁芯绕制成多匝线圈,得到 ...… 查看全部问答> |
|
能读进去,写却怎么也写不进去 使用的芯片是S3c2440+k9f1208 哪位帮我好好看看吧! 代码如下 /* NAND Flash registers 2440*/ #define NFCONF (*(volatile unsigned int *)0x4e000000) #define NFCONT (*(volatile u ...… 查看全部问答> |
|
我在做一个HID USB设备的minidriver,USB设备总是循环的发送相同的数据,在以中断传输方式读取数据的时候,我不知道该采用怎么样的方式: 1. 根据Walter Oney,HID USB设备的hidMinidriverRegistration.DevicesArePolled ...… 查看全部问答> |
|
接触过RT-Thread的都知道RT-Thread的许可证是很宽松的: RT-Thread 0.3.0遵循GPLv2许可证,后期会更改更为宽松的许可证:可能是BSD,也可能是Apache v2。不管是哪个,这两个都是商业化友好的许可证,即在使用的时候的声明有使用相关代码 ...… 查看全部问答> |
|
大家可以通过各种渠道拿到工具了,不知道有没有必要搞一个全国范围的技术讲座,围绕着软件,还有芯片的外围功能和调试技术,做一个入门级的培训。可以送一些书,,, 上海,深圳,北京 拿到工具的人希望讲 ...… 查看全部问答> |




