自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

HDF驅(qū)動(dòng)框架探路:Linux總線機(jī)制imx6ull驅(qū)動(dòng)sr501紅外傳感器

系統(tǒng) Linux
總線是處理器與一個(gè)或者多個(gè)設(shè)備之間的通道。在設(shè)備模型中,所有的設(shè)備都通過(guò)總線相連。甚至是那些內(nèi)部的虛擬“平臺(tái)”總線??偩€可以相互插入,比如一個(gè)USB控制器通常是一個(gè)PCI設(shè)備。

[[441647]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

老規(guī)矩還是將最終希望跑出來(lái)的效果放出來(lái)。如下:

HDF驅(qū)動(dòng)框架探路(六):linux總線機(jī)制imx6ull驅(qū)動(dòng)sr501紅外傳感器-鴻蒙HarmonyOS技術(shù)社區(qū)

HDF驅(qū)動(dòng)框架探路6:

前言

上一篇文章中最后在操作led燈的硬件時(shí)候,我們是直接讀的原理圖,去操作的寄存器,這種情況是我們絕大多數(shù)情況下會(huì)這樣子進(jìn)行操作,而本章我們的核心重點(diǎn)是使用總線機(jī)制,也就是通過(guò)修改設(shè)備樹(shù)的方法來(lái)操作硬件。

本章框架圖

HDF驅(qū)動(dòng)框架探路(六):linux總線機(jī)制imx6ull驅(qū)動(dòng)sr501紅外傳感器-鴻蒙HarmonyOS技術(shù)社區(qū)

總線框架圖

HDF驅(qū)動(dòng)框架探路(六):linux總線機(jī)制imx6ull驅(qū)動(dòng)sr501紅外傳感器-鴻蒙HarmonyOS技術(shù)社區(qū)

涉及到的概念介紹

1.總線的概念

總線是處理器與一個(gè)或者多個(gè)設(shè)備之間的通道。在設(shè)備模型中,所有的設(shè)備都通過(guò)總線相連。甚至是那些內(nèi)部的虛擬“平臺(tái)”總線??偩€可以相互插入,比如一個(gè)USB控制器通常是一個(gè)PCI設(shè)備。設(shè)備模型展示了總線和它們所控制的設(shè)備之間的連接。

驅(qū)動(dòng)程序

1.1 總線的使用邏輯首先調(diào)用

為了正確地注冊(cè)到內(nèi)核,所有的platform驅(qū)動(dòng)程序都必須創(chuàng)建的主要結(jié)構(gòu)體是struct platform_driver結(jié)構(gòu)體。該結(jié)構(gòu)體由許多回調(diào)函數(shù)和變量組成,向platform描述了platform驅(qū)動(dòng)程序。所以我們需要寫(xiě)一個(gè)struct platform_driver結(jié)構(gòu)體。然后在其中有兩個(gè)比較重要的回調(diào)函數(shù):probe和remove

  • int (*probe)(…)指向platform驅(qū)動(dòng)程序中的探測(cè)函數(shù)的指針。當(dāng)platform核心有一個(gè)它認(rèn)為驅(qū)動(dòng)程序需要控制的struct platform_device時(shí),就會(huì)調(diào)用該函數(shù)。所以我們會(huì)在這個(gè)函數(shù)中寫(xiě)驅(qū)動(dòng)程序的邏輯。
  • int (*remove)(…)指向一個(gè)移除函數(shù)的指針,當(dāng)struct platform_device被從系統(tǒng)中移除,或者platform驅(qū)動(dòng)程序正在從內(nèi)核中卸載時(shí),platform核心調(diào)用該函數(shù)。

為了把struct platform_driver注冊(cè)到platform核心中,需要調(diào)用以struct platform_driver為參數(shù)的platform_driver_register函數(shù)。通常在platform驅(qū)動(dòng)程序的模塊化代碼中完成該工程。

當(dāng)platform驅(qū)動(dòng)程序?qū)⒁恍遁d的時(shí)候,需要把struct platform_driver從內(nèi)核注銷。這是通過(guò)調(diào)用platform_driver_unregister完成的。

1.2 完整實(shí)現(xiàn)代碼

  1. #include <linux/module.h> 
  2. #include <linux/poll.h> 
  3. #include <linux/fs.h> 
  4. #include <linux/errno.h> 
  5. #include <linux/miscdevice.h> 
  6. #include <linux/kernel.h> 
  7. #include <linux/major.h> 
  8. #include <linux/mutex.h> 
  9. #include <linux/proc_fs.h> 
  10. #include <linux/seq_file.h> 
  11. #include <linux/stat.h> 
  12. #include <linux/init.h> 
  13. #include <linux/device.h> 
  14. #include <linux/tty.h> 
  15. #include <linux/kmod.h> 
  16. #include <linux/gfp.h> 
  17. #include <linux/gpio/consumer.h> 
  18. #include <linux/platform_device.h> 
  19. #include <linux/of_gpio.h> 
  20. #include <linux/of_irq.h> 
  21. #include <linux/interrupt.h> 
  22. #include <linux/irq.h> 
  23. #include <linux/slab.h> 
  24. #include <linux/fcntl.h> 
  25. #include <linux/timer.h> 
  26. #include <linux/workqueue.h> 
  27. #include <asm/current.h> 
  28. static int major; 
  29. static struct class *sr501_class; 
  30. static struct gpio_desc *sr501_gpio; 
  31. static int irq; 
  32. static int sr501_data = 0;   
  33. static wait_queue_head_t sr501_wq; 
  34. /* 實(shí)現(xiàn)對(duì)應(yīng)的open/read/write等函數(shù),填入file_operations結(jié)構(gòu)體                   */ 
  35. static ssize_t sr501_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset) 
  36. #if 0 
  37.     int val; 
  38.     int len = (size < 4)? size : 4; 
  39.      
  40.     val = gpiod_get_value(sr501_gpio);  
  41.     copy_to_user(buf, &val, len); 
  42.     return len; 
  43. #else 
  44.     int val; 
  45.     int len = (size < 4)? size : 4; 
  46.     /* 1. 有數(shù)據(jù)就copy_to_uesr */ 
  47.     /* 2. 無(wú)數(shù)據(jù)就休眠:  放入某個(gè)鏈表 */ 
  48.     wait_event_interruptible(sr501_wq, sr501_data);  
  49.     copy_to_user(buf, &sr501_data, len); 
  50.     sr501_data = 0; 
  51.     return len; 
  52. #endif 
  53.  
  54. static unsigned int sr501_drv_poll(struct file *fp, poll_table * wait) 
  55.     return 0; 
  56. /* 定義自己的file_operations結(jié)構(gòu)體                                              */ 
  57. static struct file_operations sr501_fops = { 
  58.     .owner   = THIS_MODULE, 
  59.     .read    = sr501_drv_read, 
  60.     .poll    = sr501_drv_poll, 
  61. }; 
  62. static irqreturn_t sr501_isr(int irq, void *dev_id) 
  63.     printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__); 
  64.     /* 1. 記錄數(shù)據(jù) */ 
  65.     sr501_data = 1; 
  66.     /* 2. 喚醒APP:去同一個(gè)鏈表把APP喚醒 */ 
  67.     wake_up(&sr501_wq);  
  68.     return IRQ_HANDLED; 
  69. /* 1. 從platform_device獲得GPIO 
  70.  * 2. gpio=>irq 
  71.  * 3. request_irq 
  72.  */ 
  73. static int sr501_probe(struct platform_device *pdev) 
  74.     /* 1. 獲得硬件信息 */ 
  75.     sr501_gpio = gpiod_get(&pdev->dev, NULL, 0); 
  76.     gpiod_direction_input(sr501_gpio); 
  77.     irq = gpiod_to_irq(sr501_gpio); 
  78.     request_irq(irq, sr501_isr, IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING, "sr501"NULL); 
  79.     /* 注冊(cè)file_operations    */ 
  80.     major = register_chrdev(0, "sr501", &sr501_fops);   
  81.  
  82.     sr501_class = class_create(THIS_MODULE, "sr501_class"); 
  83.     if (IS_ERR(sr501_class)) { 
  84.         printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__); 
  85.         unregister_chrdev(major, "sr501"); 
  86.         return PTR_ERR(sr501_class); 
  87.     } 
  88.     /* 2. device_create */ 
  89.     device_create(sr501_class, NULL, MKDEV(major, 0), NULL"sr501"); 
  90.  
  91.     return 0; 
  92. static int sr501_remove(struct platform_device *pdev) 
  93.     device_destroy(sr501_class, MKDEV(major, 0)); 
  94.     class_destroy(sr501_class); 
  95.     unregister_chrdev(major, "sr501"); 
  96. static const struct of_device_id qzk_sr501[] = { 
  97.     { .compatible = "qzk,sr501" }, 
  98.     { }, 
  99. }; 
  100. /* 1. 定義platform_driver */ 
  101. static struct platform_driver sr501s_driver = { 
  102.     .probe      = sr501_probe, 
  103.     .remove     = sr501_remove, 
  104.     .driver     = { 
  105.         .name   = "qzk_sr501"
  106.         .of_match_table = qzk_sr501, 
  107.     }, 
  108. }; 
  109. /* 2. 在入口函數(shù)注冊(cè)platform_driver */ 
  110. static int __init sr501_init(void) 
  111.     int err; 
  112.     printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__); 
  113.     init_waitqueue_head(&sr501_wq); 
  114.     err = platform_driver_register(&sr501s_driver);  
  115.     return err; 
  116. /* 3. 有入口函數(shù)就應(yīng)該有出口函數(shù):卸載驅(qū)動(dòng)程序時(shí),就會(huì)去調(diào)用這個(gè)出口函數(shù) 
  117.  *     卸載platform_driver 
  118.  */ 
  119. static void __exit sr501_exit(void) 
  120.     printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__); 
  121.  
  122.     platform_driver_unregister(&sr501s_driver); 
  123. /* 7. 其他完善:提供設(shè)備信息,自動(dòng)創(chuàng)建設(shè)備節(jié)點(diǎn)                                     */ 
  124.  
  125. module_init(sr501_init); 
  126. module_exit(sr501_exit); 
  127. MODULE_LICENSE("GPL"); 

2.修改設(shè)備樹(shù)

2.1 修改思路

在上述驅(qū)動(dòng)中of_device_id結(jié)構(gòu)體中的.compatible = “qzk,sr501”,所以我們需要將設(shè)備樹(shù)中添加一個(gè)這樣的設(shè)備節(jié)點(diǎn)

2.2 修改代碼如下:

Linux-4.9.88/arch/arm/boot/dts/100ask_imx6ull-14x14.dts

HDF驅(qū)動(dòng)框架探路(六):linux總線機(jī)制imx6ull驅(qū)動(dòng)sr501紅外傳感器-鴻蒙HarmonyOS技術(shù)社區(qū)

3.應(yīng)用側(cè)測(cè)試程序

3.1 測(cè)試程序邏輯

測(cè)試程序只需要不斷的去read,等到中斷收到返回時(shí),就會(huì)獲得返回。

3.2 測(cè)試程序完成實(shí)現(xiàn)代碼

  1. #include <sys/types.h> 
  2. #include <sys/stat.h> 
  3. #include <fcntl.h> 
  4. #include <unistd.h> 
  5. #include <stdio.h> 
  6. #include <string.h> 
  7.  
  8. int main(int argc, char **argv) 
  9.     int fd; 
  10.     int val; 
  11.     fd = open("/dev/sr501", O_RDWR); 
  12.     if (fd == -1) 
  13.     { 
  14.         printf("can not open file %s\n", argv[1]); 
  15.         return -1; 
  16.     } 
  17.  
  18.     while (1) 
  19.     { 
  20.         read(fd, &val, 4); 
  21.         if (val == 0x1) { 
  22.             printf("detect people\n"); 
  23.         } 
  24.     } 
  25.      
  26.     close(fd); 
  27.      
  28.     return 0; 

4.編譯

4.1 配置交叉編譯工具鏈

回到源碼根目錄執(zhí)行source env.sh配置編譯工具鏈

4.2 重新生成內(nèi)核設(shè)備樹(shù)

進(jìn)入源碼目錄下的Linux-4.9.88文件夾后執(zhí)行make dtbs重新生成設(shè)備樹(shù)

4.3 將重新生成的設(shè)備樹(shù)放入開(kāi)發(fā)板的boot目錄

cp 100ask_imx6ull-14x14.dtb /boot

4.4 make編譯驅(qū)動(dòng)程序

  1. KERN_DIR =  ~/imx6ullpro/Linux-4.9.88 # 板子所用內(nèi)核源碼的目錄 
  2. all
  3.     make -C $(KERN_DIR) M=`pwd` modules 
  4.     $(CROSS_COMPILE)gcc -o sr501_test sr501_test.c 
  5. clean: 
  6.     make -C $(KERN_DIR) M=`pwd` modules clean 
  7.     rm -rf modules.order 
  8. obj-m += sr501_drv.o 

 直接執(zhí)行make腳本執(zhí)行上述Makefile生成測(cè)試程序和驅(qū)動(dòng)ko文件

4.5 加載驅(qū)動(dòng)程序

  1. insmod sr501_drv.ko 

4.6 執(zhí)行測(cè)試程序

  1. ./sr501_test 

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來(lái)源: 鴻蒙社區(qū)
相關(guān)推薦

2021-12-15 15:30:38

鴻蒙HarmonyOS應(yīng)用

2023-03-16 15:18:16

2021-11-22 16:46:59

鴻蒙HarmonyOS應(yīng)用

2023-03-20 16:05:49

HDF傳感器驅(qū)動(dòng)開(kāi)發(fā)

2021-11-30 14:52:41

鴻蒙HarmonyOS應(yīng)用

2021-05-24 14:28:34

鴻蒙HarmonyOS應(yīng)用

2021-09-07 15:48:28

鴻蒙HarmonyOS應(yīng)用

2023-03-22 09:23:53

I2C總線溫度傳感器

2021-11-26 15:34:27

鴻蒙HarmonyOS應(yīng)用

2021-05-20 11:13:22

Linux紅外文件

2021-12-15 10:02:25

鴻蒙HarmonyOS應(yīng)用

2022-09-30 13:50:07

設(shè)備開(kāi)發(fā)鴻蒙

2017-11-16 14:46:58

Linuxplatform總線驅(qū)動(dòng)設(shè)備

2022-08-08 19:35:37

HDF驅(qū)動(dòng)開(kāi)發(fā)鴻蒙

2023-05-30 14:58:05

智能開(kāi)發(fā)鴻蒙

2022-04-20 20:28:40

HDF 驅(qū)動(dòng)框架鴻蒙操作系統(tǒng)

2017-02-10 15:32:47

2020-10-16 09:47:34

鴻蒙Liteos-a移植

2023-05-06 15:41:00

人體傳感器數(shù)據(jù)鴻蒙

2009-08-04 10:46:04

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)