找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3442|回复: 0
收起左侧

TQ2440开发板 Linux第一个驱动--点灯

[复制链接]
ID:72519 发表于 2015-1-20 02:33 | 显示全部楼层 |阅读模式

我用的是TQ2440开发板,这个程序是参考韦东山的.

4盏LED灯

以下是驱动程序

  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/fs.h>
  4. #include <linux/init.h>
  5. #include <linux/delay.h>
  6. #include <asm/uaccess.h>
  7. #include <asm/irq.h>
  8. #include <asm/io.h>
  9. #include <asm/arch/regs-gpio.h>
  10. #include <asm/hardware.h>

  11. static struct class *firstdrv_class;
  12. static struct class_device        *firstdrv_class_dev;

  13. volatile unsigned long *gpbcon = NULL;
  14. volatile unsigned long *gpbdat = NULL;


  15. static int first_drv_open(struct inode *inode, struct file *file)
  16. {
  17.         //printk("first_drv_open\n");
  18.         /* 配置gpb5,6,7,8为输出 */
  19.         *gpbcon &= ~((0x3<<(5*2)) | (0x3<<(6*2)) | (0x3<<(7*2)) | (0x3<<(8*2)));
  20.         *gpbcon |= ((0x1<<(5*2)) |(0x1<<(6*2)) | (0x1<<(7*2)) | (0x1<<(8*2)));
  21.         return 0;
  22. }

  23. static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
  24. {
  25.         int val;

  26.         //printk("first_drv_write\n");

  27.         copy_from_user(&val, buf, count); //        copy_to_user();

  28.         if (val == 1)
  29.         {
  30.                 // 点灯
  31.                 *gpbdat &= ~((1<<5) | (1<<6) | (1<<7) | (1<<8));
  32.         }
  33.         else
  34.         {
  35.                 // 灭灯
  36.                 *gpbdat |= (1<<5) | (1<<6) | (1<<7) | (1<<8);
  37.         }
  38.        
  39.         return 0;
  40. }

  41. static struct file_operations first_drv_fops = {
  42.     .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
  43.     .open   =   first_drv_open,     
  44.         .write        =        first_drv_write,          
  45. };


  46. int major;
  47. static int first_drv_init(void)
  48. {
  49.         major = register_chrdev(0, "first_drv", &first_drv_fops); // 注册, 告诉内核

  50.         firstdrv_class = class_create(THIS_MODULE, "firstdrv");

  51.         firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */

  52.         gpbcon = (volatile unsigned long *)ioremap(0x56000010, 16); //0x56000010是的GPIOB的
  53.         gpbdat = gpbcon + 1;
  54.         printk("first_drv_init...\n");

  55.         return 0;
  56. }

  57. static void first_drv_exit(void)
  58. {
  59.         unregister_chrdev(major, "first_drv"); // 卸载

  60.         class_device_unregister(firstdrv_class_dev);
  61.         class_destroy(firstdrv_class);
  62.         iounmap(gpbcon);
  63.         printk("first_drv_exit...\n");
  64. }

  65. module_init(first_drv_init);
  66. module_exit(first_drv_exit);


  67. MODULE_LICENSE("GPL");
复制代码


以下是测试程序

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>

  5. /* firstdrvtest on
  6.   * firstdrvtest off
  7.   */
  8. int main(int argc, char **argv)
  9. {
  10.         int fd;
  11.         int val = 1;
  12.         fd = open("/dev/xyz", O_RDWR);
  13.         if (fd < 0)
  14.         {
  15.                 printf("can't open!\n");
  16.         }
  17.         if (argc != 2)
  18.         {
  19.                 printf("Usage :\n");
  20.                 printf("%s <on|off>\n", argv[0]);
  21.                 return 0;
  22.         }

  23.         if (strcmp(argv[1], "on") == 0)
  24.         {
  25.                 val  = 1;
  26.         }
  27.         else
  28.         {
  29.                 val = 0;
  30.         }
  31.        
  32.         write(fd, &val, 4);
  33.         return 0;
  34. }
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表