├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── dht11_drv.c ├── dht11_drv.ko ├── dht11_test └── dht11_test.c /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ROOTFS_DIR = /home/pi/ 2 | 3 | ifeq ($(KERNELRELEASE), ) 4 | 5 | KERNEL_DIR = /home/pi/t4_develop/t4_kernel 6 | CUR_DIR = $(shell pwd) 7 | APP_NAME = dht11_test 8 | CC = aarch64-linux-gnu-gcc 9 | all : 10 | make -C $(KERNEL_DIR) M=$(CUR_DIR) modules 11 | $(CC) $(APP_NAME).c -o $(APP_NAME) 12 | 13 | clean : 14 | make -C $(KERNEL_DIR) M=$(CUR_DIR) clean 15 | rm -rf $(APP_NAME) 16 | 17 | install: 18 | cp -raf *.ko $(ROOTFS_DIR) 19 | cp -raf $(APP_NAME) /usr/local/bin 20 | 21 | else 22 | 23 | obj-m += dht11_drv.o 24 | #obj-m += math.o 25 | endif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DHT11-for-RK3399-driver 2 | 手把手教你从单片机移植驱动到ARM Linux上,以STM32 DHT11为例实现GPIO操作的快速移植至RK3399 3 | 4 | [TOC] 5 | 6 | ***** 7 | ## **1. 写在前面** 8 | 经过前面的章节,可能我们可以看到从一个普通外设到驱动的编写,应用层的测试,其实对于同是ARM 的芯片,但却是不同系列,又或者操作系统不一致,如RTOS与Linux之前,但是对于一个硬件外设来说,它的寄存器、操作时序等,不会因为主机的不同也发生变化。那么,这篇文章也是帮助一些具有一定的硬件开发能力的童鞋,将之前的成果快速地转化到ARM Linux开发环境上来,帮助其更好的理解ARM Linux环境下的开发;又或者给只有ARM Linux驱动开发的童鞋提供多一条思路,对于一些比较成熟的外设模块,在没有相同平台的Demo的情况下,快速地参考已经有的其他平台的例子,快速完成功能开发。 9 | 本章就以为**STM32**平台驱动温湿度**DHT11**为例,实现将单 片机驱动移植到ARM Linux下。 10 | 11 | ## **2. 硬件环境** 12 | ### **2.1 主控环境** 13 | > 开发板:友善电子Nano Pi T4 14 | > 主控:RK3399(ARM V8) 15 | > OS:Linux version 4.4.143 16 | > 编译器:aarch64-linux-gnu-gcc (gcc version 7.3.0) 17 | ### **2.2 外设环境** 18 | DHT11 数字温湿度传感器是一款含有已校准数字信号输出的温湿度复合传感器。 19 | > 1. 供电电压:3-5.5V 20 | > 21 | > 2. 供电电流:最大2.5mA 22 | > 23 | > 3. 温度范围:0-50℃ 误差±2℃ 24 | > 25 | > 4. 湿度范围:20-90%RH 误差±5%RH 26 | > 27 | > 5. 响应时间: 1/e(63%) 6-30s 28 | > 29 | > 6. 测量分辨率分别为 8bit(温度)、8bit(湿度) 30 | > 31 | > 7. 采样周期间隔不得低于1 秒钟 32 | 33 | ## **3.硬件接线** 34 | 由于DHT11采用了单总线的方式,所以输入输出都是在DATA线上完成了,典型的接线图如下,一般将DATA线接至CPU的某个IO口上。 35 | ![](images/dht11.jpg) 36 | 37 | ### **3.1 STM32接线** 38 | 39 | :-: PE4---->DATA线 40 | 41 | ### **3.2 RK3399接线** 42 | 43 | :-: GPIO1_A0---->DATA线 44 | 45 | ## **4. 移植原理** 46 | 对于DHT11这种外设来说,无非就是IO口上设为输入输出间切换,进而进行数据的读写操作。对IO的操作来说,STM32有提供一系列方便的库函数,如: 47 | 48 | ``` 49 | void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct); 50 | void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct); 51 | uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); 52 | uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx); 53 | uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); 54 | uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx); 55 | void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); 56 | void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); 57 | void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal); 58 | void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal); 59 | ``` 60 | 对于linux来说,同样,有一系列方便的接口,与STM32库函数不同的是,这些接口是经过高度抽象化的,不直接操作硬件。 61 | 62 | ``` 63 | enum of_gpio_flags { 64 | OF_GPIO_ACTIVE_LOW = 0x1, 65 | }; 66 | int of_get_named_gpio_flags(struct device_node *np, const char *propname, 67 | int index, enum of_gpio_flags *flags); 68 | int gpio_is_valid(int gpio); 69 | int gpio_request(unsigned gpio, const char *label); 70 | void gpio_free(unsigned gpio); 71 | int gpio_direction_input(int gpio); 72 | int gpio_direction_output(int gpio, int v); 73 | ``` 74 | 75 | 除此之外,对于外设驱动来说,除了IO控制之外,还必须对时序有严格的要求,比如说延时,那么,对于一定MCU来说有类似STM32 滴答时钟实现的延时,其实,在linux驱动来说,也有提供这些类似的接口。 76 | 所以,综上,我们从单片机移植一个驱动到Linux环境下,无非就是将接口进行替换。 77 | 78 | ## **5. 移植操作** 79 | ### **5.1 从设备树中传入GPIO口定义** 80 | 从上文,我们知道,其实操作Linux下的GPIO无非就是通过GPIO的API进行操作,那么,我们大可直接使用`gpio_direction_output(12,1)` 来实现IO直接操作,但是在ARM Linux上呢,我们可以通过设备树的方式,灵活的传入 `12` 这个编号,对于本次例程,我使用了之前的GPIO-LED方式的GPIO,设备树大概如下: 81 | ``` 82 | myleds: mygpio-leds { 83 | compatible = "mygpio-leds"; 84 | pinctrl-names = "default"; 85 | pinctrl-0 =<&myleds_gpio>; 86 | 87 | led@1 { 88 | gpios = <&gpio1 0 GPIO_ACTIVE_HIGH>; 89 | label = "my_led"; 90 | linux,default-trigger = "heartbeat"; 91 | linux,default-trigger-delay-ms = <0>; 92 | }; 93 | }; 94 | mygpio-leds { 95 | myleds_gpio: myleds-gpio { 96 | rockchip,pins = <1 0 RK_FUNC_GPIO &pcfg_pull_none>; 97 | }; 98 | }; 99 | ``` 100 | 101 | 所以我们需要在驱动添加获取到这个GPIO的定义:` gpios = <&gpio1 0 GPIO_ACTIVE_HIGH>;` 102 | 103 | 首先,按照前面的章节所介绍的,我们只需要构建好我们的设备树结构体,然后,将名字与`compatible = "mygpio-leds"` 中的 "mygpio-leds" 一致即可,看下面示例: 104 | ``` 105 | static const struct of_device_id of_dht11_match[] = { 106 | { .compatible = "mygpio-leds", }, 107 | {}, 108 | }; 109 | 110 | MODULE_DEVICE_TABLE(of, of_dht11_match); 111 | ``` 112 | 最后使用的`MODULE_DEVICE_TABLE` 将我们的结构体添加到内核的设备树的链表中去。 113 | 114 | 最后,我们需要构建一个drivers将我们的驱动注册到内核中去,让内核去匹配,这里使用一个platform 驱动为例: 115 | ``` 116 | static struct platform_driver dht11_driver = { 117 | .probe = dht11_probe, 118 | .remove = dht11_remove, 119 | .shutdown = dht11_shutdown, 120 | .driver = { 121 | .name = "dht11_driver", 122 | .of_match_table = of_dht11_match, 123 | }, 124 | }; 125 | 126 | module_platform_driver(dht11_driver); 127 | ``` 128 | 注意,也要把刚才说的设备树的结构体附给这个driver,如上代码片段所示的: 129 | 130 | ``` 131 | .driver = { 132 | .name = "dht11_driver", 133 | .of_match_table = of_dht11_match, 134 | ``` 135 | 136 | 按照之前的方法,当设备树中的节点信息与driver匹配 后,会调用probe函数,那我们就可以在probe函数中,将` gpios = <&gpio1 0 GPIO_ACTIVE_HIGH>;` 取出来,利用一些设备树的API,代码如下: 137 | ``` 138 | struct device_node *dht11_gpio_node = pdev->dev.of_node; 139 | ``` 140 | 此时,dht11_gpio_node 就取出`myleds: mygpio-leds` 这个主节点,然后需要找出这个子节点:` led@1` 所以,真正的节点的是`dht11_gpio_node->child` 所以使用起来的效果是这样的: 141 | ``` 142 | dht11_gpio = of_get_named_gpio_flags(dht11_gpio_node->child, "gpios", 0, &flag); 143 | ``` 144 | 其中,`dht11_gpio` 就是我们要使用的GPIO的定义,按设备树的定义来说,就是` gpios = <&gpio1 0 GPIO_ACTIVE_HIGH>;` 如果不出问题的话,`dht11_gpio=32` 145 | 146 | 一旦得到GPIO号之后,我们就可以使之前提及的GPIO输出输入控制接口,所以最终,在probe函数中可以初步写成如下: 147 | 148 | ``` 149 | static int dht11_probe(struct platform_device *pdev) 150 | { 151 | int ret; 152 | enum of_gpio_flags flag;//(flag == OF_GPIO_ACTIVE_LOW) ? 153 | 154 | printk("-------%s-------------\n", __FUNCTION__); 155 | 156 | struct device_node *dht11_gpio_node = pdev->dev.of_node; 157 | 158 | dht11_gpio = of_get_named_gpio_flags(dht11_gpio_node->child, "gpios", 0, &flag); 159 | 160 | if (!gpio_is_valid(dht11_gpio)) 161 | { 162 | printk("dht11-gpio: %d is invalid\n", dht11_gpio); 163 | return -ENODEV; 164 | } 165 | else 166 | printk("dht11-gpio: %d is valid!\n", dht11_gpio); 167 | if (gpio_request(dht11_gpio, "dht11-gpio")) 168 | { 169 | printk("gpio %d request failed!\n", dht11_gpio); 170 | gpio_free(dht11_gpio); 171 | return -ENODEV; 172 | } 173 | else 174 | printk("gpio %d request success!\n", dht11_gpio); 175 | } 176 | ``` 177 | 178 | ### **5.2 替换STM32中的接口** 179 | 按前文所述,就是将STM32中关于IO口操作的接口换成Linux中的,下面开始逐步分解 180 | 181 | #### **5.2.1 替换宏定义接口** 182 | 我们先来看一下STM32中关于GPIO中操作的宏定义相关的: 183 | ``` 184 | #define IO_DHT11 GPIO_Pin_4 //引入中间变量,方便移植 185 | #define GPIO_DHT11 GPIOE //引入中间变量,方便移植 186 | #define DHT11_DQ_High GPIO_SetBits(GPIO_DHT11,IO_DHT11) 187 | #define DHT11_DQ_Low GPIO_ResetBits(GPIO_DHT11,IO_DHT11) 188 | ``` 189 | 替换成这样: 190 | ``` 191 | static int dht11_gpio; //data线宏定义,替换STM32中GPIOXX 192 | #define DHT11_DQ_High gpio_direction_output(dht11_gpio, 1) 193 | #define DHT11_DQ_Low gpio_direction_output(dht11_gpio, 0) 194 | ``` 195 | 这里的参数:`dht11_gpio` 就是前面通过设备树API从设备树中读到的。所以前面,为了方便,直接将其做了全局变量了。 196 | 197 | 198 | #### **5.2.2 替换延时接口** 199 | 熟悉STM32单片机的童鞋一般都知道在STM32上使用的延时函数,一般是通过滴答(systick)实现的,为了移植过程中,替换代码量少,我直接将原代码中的接口宏定义了: 200 | ``` 201 | #define delay_us(x) udelay(x) 202 | #define delay_ms(x) msleep(x) 203 | ``` 204 | #### **5.2.3 操作函数接口替换** 205 | 主要有以下的接口: 206 | ``` 207 | void DHT11_IO_OUT(void);//温湿度模块输出函数 208 | void DHT11_IO_IN(void); //温湿度模块输入函数 209 | void DHT11_Init(void); //初始化DHT11 210 | u8 DHT11_Read_DQ(void); //读取Data线电平 211 | u8 DHT11_Read_Data(u8 *temp,u8 *humi);//读取温湿度 212 | u8 DHT11_Read_Byte(void); //读出一个字节 213 | u8 DHT11_Read_Bit(void); //读出一个位 214 | u8 DHT11_Check(void); //检测是否存在DHT11 215 | void DHT11_Rst(void); //复位DHT11 216 | ``` 217 | 下面来看一个个函数修改移植的效果 218 | 219 | ##### **5.2.3.1 void DHT11_IO_OUT(void) ;** 220 | ``` 221 | void DHT11_IO_OUT(void)//温湿度模块输出函数 222 | { 223 | GPIO_InitTypeDef GPIO_InitStructure; 224 | GPIO_InitStructure.GPIO_Pin=IO_DHT11; 225 | GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; 226 | GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; 227 | GPIO_Init(GPIO_DHT11,&GPIO_InitStructure); 228 | } 229 | ``` 230 | 替换:其实这个函数的作用无非就是将IO设为输出模式,然后使用`DHT11_DQ_High` 或者 `DHT11_DQ_Low` 输出高低电平,而我们移植的宏定义,已经实现了先设置为输出再输出高低电平,一步到位! 231 | 232 | ##### **5.2.3.2 void DHT11_IO_IN(void) ;** 233 | 234 | STM32上原始的样子: 235 | ``` 236 | void DHT11_IO_IN(void)//温湿度模块输入函数 237 | { 238 | GPIO_InitTypeDef GPIO_InitStructure; 239 | 240 | GPIO_InitStructure.GPIO_Pin=IO_DHT11; 241 | GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; 242 | GPIO_Init(GPIO_DHT11,&GPIO_InitStructure); 243 | } 244 | 245 | ``` 246 | 替换成这样: 247 | ``` 248 | void DHT11_IO_IN(void)//温湿度模块输入函数 249 | { 250 | gpio_direction_input(dht11_gpio); 251 | }; 252 | ``` 253 | 254 | ##### **5.2.3.3 u8 DHT11_Read_DQ(void);** 255 | STM32上原始的样子: 256 | ``` 257 | u8 DHT11_Read_DQ(void) 258 | { 259 | return GPIO_ReadInputDataBit(GPIO_DHT11,IO_DHT11); 260 | } 261 | 262 | ``` 263 | 替换成: 264 | ``` 265 | static u8 DHT11_Read_DQ(void) 266 | { 267 | return gpio_get_value(dht11_gpio); 268 | } 269 | ``` 270 | 271 | ##### **5.2.3.4 总结对比** 272 | 到此时,我们可以发现其实对于比较上层一点的函数,其实函数基本上可以直接用了,因为我们完成了底层的接口的替换之后,代码基本上一致。下面就不再一一赘述这些函数了,我们可以直接看整个代码移植前后的对比图: 273 | ![](images/对比图.jpg) 274 | 275 | 276 | ##### **5.2.3.5 其他直接无缝使用的的代码** 277 | ``` 278 | //复位DHT11 279 | void DHT11_Rst(void) 280 | { 281 | DHT11_IO_OUT(); //SET OUTPUT 282 | DHT11_DQ_Low; //DQ=0 283 | delay_ms(20); //拉低至少18ms 284 | DHT11_DQ_High; //DQ=1 285 | delay_us(30); //主机拉高20~40us 286 | } 287 | 288 | //等待DHT11的回应 289 | //返回1:未检测到DHT11的存在 290 | //返回0:存在 291 | u8 DHT11_Check(void) 292 | { 293 | u8 retry=0;//定义临时变量 294 | DHT11_IO_IN();//SET INPUT 295 | while ((DHT11_Read_DQ()==1)&&retry<100)//DHT11会拉低40~80us 296 | { 297 | retry++; 298 | delay_us(1); 299 | }; 300 | if(retry>=100)return 1; 301 | else retry=0; 302 | while ((DHT11_Read_DQ())==0)&&retry<100)//DHT11拉低后会再次拉高40~80us 303 | { 304 | retry++; 305 | delay_us(1); 306 | }; 307 | if(retry>=100)return 1; 308 | return 0; 309 | } 310 | //从DHT11读取一个位 311 | //返回值:1/0 312 | u8 DHT11_Read_Bit(void) 313 | { 314 | u8 retry=0; 315 | while((DHT11_Read_DQ()==1)&&retry<100)//等待变为低电平 316 | { 317 | retry++; 318 | delay_us(1); 319 | } 320 | retry=0; 321 | while((DHT11_Read_DQ()==0)&&retry<100)//等待变高电平 322 | { 323 | retry++; 324 | delay_us(1); 325 | } 326 | delay_us(40);//等待40us 327 | if(DHT11_Read_DQ()==1) 328 | return 1; 329 | else 330 | return 0; 331 | } 332 | //从DHT11读取一个字节 333 | //返回值:读到的数据 334 | u8 DHT11_Read_Byte(void) 335 | { 336 | u8 i,dat; 337 | dat=0; 338 | for (i=0;i<8;i++) 339 | { 340 | dat<<=1; 341 | dat|=DHT11_Read_Bit(); 342 | } 343 | return dat; 344 | } 345 | 346 | //从DHT11读取一次数据 347 | //temp:温度值(范围:0~50°) 348 | //humi:湿度值(范围:20%~90%) 349 | //返回值:0,正常;1,读取失败 350 | u8 DHT11_Read_Data(u8 *temp,u8 *humi) 351 | { 352 | u8 buf[5]; 353 | u8 i; 354 | DHT11_Rst(); 355 | if(DHT11_Check()==0) 356 | { 357 | for(i=0;i<5;i++)//读取40位数据 358 | { 359 | buf[i]=DHT11_Read_Byte(); 360 | } 361 | if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4]) 362 | { 363 | *humi=buf[0]; 364 | *temp=buf[2]; 365 | } 366 | }else return 1; 367 | return 0; 368 | } 369 | //初始化DHT11的IO口 DQ 同时检测DHT11的存在 370 | //返回1:不存在 371 | //返回0:存在 372 | void DHT11_Init(void) 373 | { 374 | DHT11_Rst(); //复位DHT11 375 | DHT11_Check();//等待DHT11的回应 376 | } 377 | ``` 378 | 379 | ## **6. 加入到字符设备驱动框架** 380 | 381 | 按照之前的教程,编写字符驱动框架,最终创建一个字符设备`/dev/dht11`供上层应用读写,主要在read函数中,调用`DHT11_Read_Data`,将读到的温湿度传给上层。 382 | read函数效果大致如下: 383 | ``` 384 | static ssize_t DHT11_read(struct file *file, char __user *buf, 385 | size_t nbytes, loff_t *ppos) 386 | { 387 | printk("--------------%s--------------\n",__FUNCTION__); 388 | 389 | dht11_data Last_dht11_data; 390 | if(DHT11_Read_Data(&Last_dht11_data.temp,&Last_dht11_data.hum) == 0);//读取温湿度值 391 | { 392 | if ( copy_to_user(buf,&Last_dht11_data,sizeof(Last_dht11_data)) ) 393 | { 394 | return EFAULT ; 395 | } 396 | } 397 | 398 | } 399 | .... 400 | static struct file_operations dht11_fops = { 401 | .owner = THIS_MODULE, 402 | .read = DHT11_read, 403 | .open = DHT11_open, 404 | .release = DHT11_close, 405 | }; 406 | ...... 407 | ``` 408 | 409 | 410 | ## **7. 测试代码** 411 | 412 | ``` 413 | #include 414 | #include 415 | #include 416 | #include 417 | #include 418 | #include 419 | #include 420 | 421 | 422 | typedef unsigned char u8; 423 | typedef unsigned short u16; 424 | 425 | typedef struct DHT11_SENSOR_DATA 426 | { 427 | u16 temp;//温度 428 | u16 hum;//湿度 429 | }dht11_data; 430 | 431 | int main ( void ) 432 | { 433 | int fd ; 434 | int retval ; 435 | dht11_data Curdht11_data; 436 | fd = open ( "/dev/dht11" , O_RDONLY) ; 437 | if ( fd == -1 ) 438 | { 439 | perror ( "open dht11 error\n" ) ; 440 | exit ( -1 ) ; 441 | } 442 | printf ( "open /dev/dht11 successfully\n" ) ; 443 | sleep ( 2 ) ; 444 | while ( 1 ) 445 | { 446 | sleep ( 1 ) ; 447 | retval = read ( fd , &Curdht11_data , sizeof(Curdht11_data) ); 448 | if ( retval == -1 ) 449 | { 450 | perror ( "read dht11 error" ) ; 451 | printf ( "read dht11 error" ) ; 452 | exit ( -1 ) ; 453 | } 454 | if(Curdht11_data.temp != 0xffff) 455 | printf ( "Temperature:%d.%d C,Humidity:%d.%d %%RH\n",Curdht11_data.temp>>8,Curdht11_data.temp&0xff,\ 456 | Curdht11_data.hum>>8,Curdht11_data.hum&0xff ) ; 457 | } 458 | close ( fd ) ; 459 | } 460 | ``` 461 | ## **8. 测试效果** 462 | ![](images/QQ图片20181025114724.png) 463 | 464 | ## **9. 代码开源地址** 465 | 466 | [Github](https://github.com/jackeyt/DHT11-for-RK3399-driver) 467 | 468 | 469 | 470 | 471 | 472 | -------------------------------------------------------------------------------- /dht11_drv.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | typedef unsigned char u8; 18 | typedef unsigned short u16; 19 | 20 | static int dht11_major = 0; 21 | static int dht11_gpio; 22 | static struct class *dht11_class;//类 23 | static struct device *dht11_dev;//设备 24 | static const char* dht11_name = "dht11"; 25 | 26 | 27 | typedef struct DHT11_SENSOR_DATA 28 | { 29 | u16 temp;//温度 30 | u16 hum;//湿度 31 | }dht11_data; 32 | 33 | #define DHT11_DQ_High gpio_direction_output(dht11_gpio, 1) 34 | #define DHT11_DQ_Low gpio_direction_output(dht11_gpio, 0) 35 | #define DHT11_IO_IN gpio_direction_input(dht11_gpio) 36 | 37 | 38 | #define delay_us(x) udelay(x) 39 | #define delay_ms(x) msleep(x) 40 | 41 | static u8 DHT11_Read_DQ(void) 42 | { 43 | DHT11_IO_IN; 44 | return gpio_get_value(dht11_gpio); 45 | } 46 | 47 | //复位DHT11 48 | static void DHT11_Rst(void) 49 | { 50 | DHT11_DQ_Low; 51 | msleep (20); //拉低至少18ms 52 | DHT11_DQ_High; //DQ=1 53 | delay_us (30); //主机拉高20~40us 54 | } 55 | 56 | //等待DHT11的回应 57 | //返回1:未检测到DHT11的存在 58 | //返回0:存在 59 | static u8 DHT11_Check(void) 60 | { 61 | u8 retry=0;//定义临时变量 62 | DHT11_IO_IN;//SET INPUT 63 | while ((DHT11_Read_DQ()==1)&&retry<100)//DHT11会拉低40~80us 64 | { 65 | retry++; 66 | delay_us(1); 67 | }; 68 | if(retry>=100)return 1; 69 | else retry=0; 70 | while ((DHT11_Read_DQ()==0)&&retry<100)//DHT11拉低后会再次拉高40~80us 71 | { 72 | retry++; 73 | delay_us(1); 74 | }; 75 | if(retry>=100)return 1; 76 | return 0; 77 | } 78 | 79 | //从DHT11读取一个位 80 | //返回值:1/0 81 | static u8 DHT11_Read_Bit(void) 82 | { 83 | u8 retry=0; 84 | while((DHT11_Read_DQ()==1)&&retry<100)//等待变为低电平 85 | { 86 | retry++; 87 | delay_us(1); 88 | } 89 | retry=0; 90 | while((DHT11_Read_DQ()==0)&&retry<100)//等待变高电平 91 | { 92 | retry++; 93 | delay_us(1); 94 | } 95 | delay_us(40);//等待40us 96 | if(DHT11_Read_DQ()==1) 97 | return 1; 98 | else 99 | return 0; 100 | } 101 | //从DHT11读取一个字节 102 | //返回值:读到的数据 103 | static u8 DHT11_Read_Byte(void) 104 | { 105 | u8 i,dat; 106 | dat=0; 107 | for (i=0;i<8;i++) 108 | { 109 | dat<<=1; 110 | dat|=DHT11_Read_Bit(); 111 | } 112 | return dat; 113 | } 114 | 115 | //从DHT11读取一次数据 116 | //temp:温度值(范围:0~50°) 117 | //humi:湿度值(范围:20%~90%) 118 | //返回值:0,正常;1,读取失败 119 | static u8 DHT11_Read_Data(u16 *temp,u16 *humi) 120 | { 121 | u8 buf[5]; 122 | u8 i; 123 | DHT11_Rst(); 124 | if(DHT11_Check()==0) 125 | { 126 | for(i=0;i<5;i++)//读取40位数据 127 | { 128 | buf[i]=DHT11_Read_Byte(); 129 | } 130 | if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4]) 131 | { 132 | *humi=buf[0]<<8|buf[1]; 133 | *temp=buf[2]<<8|buf[3]; 134 | printk("buf=%d,%d,%d,%d,%d\n",buf[0],buf[1],buf[2],buf[3],buf[4]); 135 | } 136 | }else return 1; 137 | return 0; 138 | } 139 | //初始化DHT11的IO口 DQ 同时检测DHT11的存在 140 | //返回1:不存在 141 | //返回0:存在 142 | static void DHT11_Init(void) 143 | { 144 | DHT11_Rst(); //复位DHT11 145 | DHT11_Check();//等待DHT11的回应 146 | } 147 | 148 | int DHT11_open(struct inode *inode, struct file *flips) 149 | { 150 | printk("--------------%s--------------\n",__FUNCTION__); 151 | return 0; 152 | } 153 | 154 | static ssize_t DHT11_read(struct file *file, char __user *buf, 155 | size_t nbytes, loff_t *ppos) 156 | { 157 | printk("--------------%s--------------\n",__FUNCTION__); 158 | 159 | dht11_data Last_dht11_data; 160 | if(DHT11_Read_Data(&Last_dht11_data.temp,&Last_dht11_data.hum) == 0);//读取温湿度值 161 | { 162 | if ( copy_to_user(buf,&Last_dht11_data,sizeof(Last_dht11_data)) ) 163 | { 164 | return EFAULT ; 165 | } 166 | } 167 | 168 | } 169 | 170 | static int DHT11_close(struct inode *inode, struct file *flip) 171 | { 172 | printk("--------------%s--------------\n",__FUNCTION__); 173 | return 0; 174 | } 175 | 176 | static struct file_operations dht11_fops = { 177 | .owner = THIS_MODULE, 178 | .read = DHT11_read, 179 | .open = DHT11_open, 180 | .release = DHT11_close, 181 | }; 182 | 183 | static const struct of_device_id of_dht11_match[] = { 184 | { .compatible = "mygpio-leds", }, 185 | {}, 186 | }; 187 | 188 | MODULE_DEVICE_TABLE(of, of_dht11_match); 189 | 190 | 191 | 192 | static int dht11_probe(struct platform_device *pdev) 193 | { 194 | int ret; 195 | enum of_gpio_flags flag;//(flag == OF_GPIO_ACTIVE_LOW) ? 196 | 197 | printk("-------%s-------------\n", __FUNCTION__); 198 | 199 | struct device_node *dht11_gpio_node = pdev->dev.of_node; 200 | 201 | dht11_gpio = of_get_named_gpio_flags(dht11_gpio_node->child, "gpios", 0, &flag); 202 | 203 | if (!gpio_is_valid(dht11_gpio)) 204 | { 205 | printk("dht11-gpio: %d is invalid\n", dht11_gpio); 206 | return -ENODEV; 207 | } 208 | else 209 | printk("dht11-gpio: %d is valid!\n", dht11_gpio); 210 | if (gpio_request(dht11_gpio, "dht11-gpio")) 211 | { 212 | printk("gpio %d request failed!\n", dht11_gpio); 213 | gpio_free(dht11_gpio); 214 | return -ENODEV; 215 | } 216 | else 217 | printk("gpio %d request success!\n", dht11_gpio); 218 | //能够读到配置信息之后就可以开始创建设备节点 219 | dht11_major = register_chrdev(0, "dht11",&dht11_fops); 220 | if(dht11_major <0) 221 | { 222 | printk(KERN_ERR "reg error!\n"); 223 | goto err_0; 224 | } 225 | else 226 | printk("dht11_major =%d\n",dht11_major); 227 | 228 | dht11_class = class_create(THIS_MODULE,"dht11_class");//creat class 229 | if( IS_ERR(dht11_class)) 230 | { 231 | printk(KERN_ERR "fail create class\n"); 232 | ret = PTR_ERR(dht11_class); 233 | goto err_1; 234 | } 235 | 236 | //creat dht11_dev--->>/dev/dht11_dev 237 | dht11_dev = device_create(dht11_class, NULL,MKDEV(dht11_major,0), NULL, dht11_name); 238 | if(IS_ERR(dht11_dev)) 239 | { 240 | printk(KERN_ERR "fail create device!\n"); 241 | ret = PTR_ERR(dht11_dev); 242 | goto err_2; 243 | } 244 | 245 | //init dht11 246 | DHT11_Init(); 247 | printk("dht11 Initing...\n"); 248 | return 0; 249 | 250 | err_2: 251 | device_destroy(dht11_class,MKDEV(dht11_major,0)); 252 | err_1: 253 | class_destroy(dht11_class); 254 | err_0: 255 | unregister_chrdev(dht11_major,dht11_name); 256 | return -1; 257 | } 258 | 259 | static int dht11_remove(struct platform_device *pdev) 260 | { 261 | printk("-------%s-------------\n", __FUNCTION__); 262 | device_destroy(dht11_class,MKDEV(dht11_major,0)); 263 | class_destroy(dht11_class); 264 | unregister_chrdev(dht11_major,dht11_name); 265 | return 0; 266 | } 267 | 268 | static void dht11_shutdown(struct platform_device *pdev) 269 | { 270 | printk("-------%s-------------\n", __FUNCTION__); 271 | } 272 | 273 | static struct platform_driver dht11_driver = { 274 | .probe = dht11_probe, 275 | .remove = dht11_remove, 276 | .shutdown = dht11_shutdown, 277 | .driver = { 278 | .name = "dht11_driver", 279 | .of_match_table = of_dht11_match, 280 | }, 281 | }; 282 | 283 | module_platform_driver(dht11_driver); 284 | 285 | MODULE_AUTHOR("jackeyt,bbs.elecfans.com"); 286 | MODULE_DESCRIPTION("DHT11 Sensor driver"); 287 | MODULE_LICENSE("GPL"); 288 | 289 | -------------------------------------------------------------------------------- /dht11_drv.ko: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jackeyt/DHT11-for-RK3399-driver/7959f28ee9586e858364c929535c92ad371ecbb4/dht11_drv.ko -------------------------------------------------------------------------------- /dht11_test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jackeyt/DHT11-for-RK3399-driver/7959f28ee9586e858364c929535c92ad371ecbb4/dht11_test -------------------------------------------------------------------------------- /dht11_test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | 10 | typedef unsigned char u8; 11 | typedef unsigned short u16; 12 | 13 | typedef struct DHT11_SENSOR_DATA 14 | { 15 | u16 temp;//温度 16 | u16 hum;//湿度 17 | }dht11_data; 18 | 19 | int main ( void ) 20 | { 21 | int fd ; 22 | int retval ; 23 | dht11_data Curdht11_data; 24 | fd = open ( "/dev/dht11" , O_RDONLY) ; 25 | if ( fd == -1 ) 26 | { 27 | perror ( "open dht11 error\n" ) ; 28 | exit ( -1 ) ; 29 | } 30 | printf ( "open /dev/dht11 successfully\n" ) ; 31 | sleep ( 2 ) ; 32 | while ( 1 ) 33 | { 34 | sleep ( 1 ) ; 35 | retval = read ( fd , &Curdht11_data , sizeof(Curdht11_data) ); 36 | if ( retval == -1 ) 37 | { 38 | perror ( "read dht11 error" ) ; 39 | printf ( "read dht11 error" ) ; 40 | exit ( -1 ) ; 41 | } 42 | if(Curdht11_data.temp != 0xffff) 43 | printf ( "Temperature:%d.%d C,Humidity:%d.%d %%RH\n",Curdht11_data.temp>>8,Curdht11_data.temp&0xff,\ 44 | Curdht11_data.hum>>8,Curdht11_data.hum&0xff ) ; 45 | } 46 | close ( fd ) ; 47 | } --------------------------------------------------------------------------------