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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3973|回復: 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精品欧美久久久久久久 | 国内自拍偷拍一区 | 国产精品成人一区二区三区 | 自拍偷拍精品 | 综合色影院 | 亚洲网址在线观看 | 可以看黄的视频 | 国产欧美日韩一区二区三区 | 国产精品乱码一二三区的特点 | 亚洲三区视频 | 成人网在线观看 | 欧美精品一区二区蜜桃 | 97超碰成人| 日韩欧美国产一区二区 | 久久一区二区三区免费 | 狼色网| 国产精品视频久久久 | 欧美 中文字幕 | 国产精品久久久久久久久久 | 精品视频成人 | 亚洲欧美一区二区三区在线 | 成人高清在线 | 99精品视频免费观看 | 欧美久久精品 | 翔田千里一区二区 | 理论片免费在线观看 | 欧美高清dvd| 欧美福利| 国产高清一区二区三区 | 精品久久久久久久 | 午夜精品久久久久久久 | 欧美猛交 | 亚洲午夜视频 | 中文字幕日韩一区 | 免费看一区二区三区 | 狠狠草视频| 欧美精品91| 日本中文字幕在线视频 | 亚洲欧美日本在线 | 亚洲精品久久久久久宅男 | 中文字幕日韩在线 |