久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3909|回復: 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. }
復制代碼



分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 久久蜜桃资源一区二区老牛 | 日韩精品一 | 亚洲第一区国产精品 | 91精品久久久久久久久 | 91精品国产色综合久久 | 国产国语精品 | 亚洲精品国产精品国自产在线 | 韩日一区二区三区 | 亚洲区在线 | 91在线观看免费 | 天天操天天射综合 | 成人黄色av网址 | 欧美精品91爱爱 | 亚洲欧美国产精品一区二区 | 伊人网站视频 | 国精产品一品二品国精在线观看 | av网址在线播放 | av中文字幕在线观看 | 99久久精品免费看国产小宝寻花 | 91文字幕巨乱亚洲香蕉 | 亚洲精品一二三区 | 91精品国产欧美一区二区 | 久久久精品一区 | 久久久99国产精品免费 | 成人在线免费电影 | 亚洲视频精品在线 | 欧美精品在线观看 | 91.xxx.高清在线 | 亚洲毛片在线观看 | 久久精品国产免费看久久精品 | 精品久久久久久久久久久久 | 久久国产精品一区二区三区 | 久久久91精品国产一区二区三区 | 天天色图 | 日本中文字幕一区 | 91精品一区 | 国产精品久久精品 | 日韩欧美国产一区二区 | 日韩欧美一区二区三区免费观看 | 欧美日韩精品影院 | 精品国产一区二区国模嫣然 |