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

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 3902|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

TQ2440開(kāi)發(fā)板 Linux第一個(gè)驅(qū)動(dòng)--點(diǎn)燈

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:72519 發(fā)表于 2015-1-20 02:33 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式

我用的是TQ2440開(kāi)發(fā)板,這個(gè)程序是參考韋東山的.

4盞LED燈

以下是驅(qū)動(dòng)程序

  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.                 // 點(diǎn)燈
  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,    /* 這是一個(gè)宏,推向編譯模塊時(shí)自動(dòng)創(chuàng)建的__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); // 注冊(cè), 告訴內(nèi)核

  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");
復(fù)制代碼


以下是測(cè)試程序

  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. }
復(fù)制代碼



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

使用道具 舉報(bào)

本版積分規(guī)則

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

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 国产中文字幕网 | 超碰在线观看97 | 国产精品国产精品国产专区不卡 | 91在线电影 | 久久久久国产一区二区三区不卡 | 亚洲国产黄 | 国产高清在线 | 欧美精品乱码久久久久久按摩 | 国产在线一区二区三区 | 亚洲高清av在线 | 91精品国产色综合久久 | 色综合久久久久 | 欧美日韩专区 | a欧美| 91成人免费看片 | 看亚洲a级一级毛片 | 在线观看国产 | 亚洲女人天堂成人av在线 | 成人欧美一区二区三区黑人孕妇 | 久久精品成人 | 免费天天干 | 日本视频在线播放 | 日韩在线中文字幕 | 狠狠视频 | www.一区二区三区 | 2018国产精品| 亚洲精品大片 | 男人的天堂一级片 | 天堂中文av| 91色视频在线观看 | 色欧美片视频在线观看 | 麻豆视频在线免费看 | 91久久国产综合久久91精品网站 | 一区二区福利视频 | 一区二区三区四区电影视频在线观看 | 日本网站免费观看 | 2020亚洲天堂 | 黄色毛片在线播放 | www免费视频 | 欧美综合一区二区三区 | 超碰在线人人 |