• 当湿度达到70蜂鸣器警报


    1.编写设备树,添加蜂鸣器等设备

     驱动代码:

    1. #include <linux/init.h>
    2. #include <linux/module.h>
    3. #include <linux/i2c.h>
    4. #include <linux/fs.h>
    5. #include <linux/uaccess.h>
    6. #include <linux/device.h>
    7. #include "si7006.h"
    8. #include <linux/of_gpio.h>
    9. #include <linux/gpio.h>
    10. int major;
    11. struct class *cls;
    12. struct device *dev;
    13. //tclient指向的结构体,存储设备驱动和总线驱动匹配成功的信息
    14. struct i2c_client *tclient;
    15. //指向gpio节点
    16. struct device_node *node;
    17. //定义结构体指针指向gpio编号
    18. struct gpio_desc *gpiono;
    19. int rcv;
    20. //获取温湿度的函数
    21. int i2c_read_hum_tem(unsigned char reg)
    22. {
    23. int ret;
    24. char r_buf[]={reg}; //地址放到数组中,读取消息
    25. unsigned short val; //读的数据
    26. //设备驱动将要收发的消息放到结构体中
    27. struct i2c_msg r_msg[]={
    28. [0]={
    29. .addr=tclient->addr,
    30. .flags=0,
    31. .len=1, //发送的地址长度是8bit一字节
    32. .buf=r_buf,
    33. },
    34. [1]={
    35. .addr=tclient->addr,
    36. .flags=1,
    37. .len=2,
    38. .buf=(char*)&val,
    39. }
    40. };
    41. //消息传输
    42. ret=i2c_transfer(tclient->adapter,r_msg,ARRAY_SIZE(r_msg));
    43. if(ret!=ARRAY_SIZE(r_msg))
    44. {
    45. printk("获取温湿度数据失败\n");
    46. return -EAGAIN;
    47. }
    48. return val;
    49. }
    50. int si7006_open(struct inode *inode, struct file *file)
    51. {
    52. printk("open\n");
    53. return 0;
    54. }
    55. long si7006_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
    56. {
    57. int tem,hum;
    58. int ret;
    59. switch(cmd)
    60. {
    61. case GET_HUM:
    62. hum=i2c_read_hum_tem(0xe5);
    63. ret=copy_to_user((void*)arg,(void*)&hum,sizeof(int));
    64. if(ret)
    65. {
    66. printk("copy to user failed\n");
    67. return -EINVAL;
    68. }
    69. break;
    70. case GET_TEM:
    71. tem=i2c_read_hum_tem(0xe3);
    72. ret=copy_to_user((void*)arg,(void*)&tem,sizeof(int));
    73. if(ret)
    74. {
    75. printk("copy to user failed\n");
    76. return -EINVAL;
    77. }
    78. break;
    79. case FENGM:
    80. gpiod_set_value(gpiono,1);
    81. break;
    82. }
    83. return 0;
    84. }
    85. int si7006_close(struct inode *inode, struct file *file)
    86. {
    87. printk("close\n");
    88. return 0;
    89. }
    90. struct file_operations fops={
    91. .open=si7006_open,
    92. .unlocked_ioctl=si7006_ioctl,
    93. .release=si7006_close,
    94. };
    95. //注册字符设备驱动
    96. int chrdev(void){
    97. major=register_chrdev(0,"si7006",&fops);
    98. if(major<0)
    99. {
    100. printk("字符设备驱动注册失败\n");
    101. return major;
    102. }
    103. printk("字符设备驱动注册成功\n");
    104. //自动创建设备节点
    105. cls=class_create(THIS_MODULE,"si7006");
    106. if(IS_ERR(cls))
    107. {
    108. printk("向上提交目录失败\n");
    109. return PTR_ERR(cls);
    110. }
    111. printk("向上提交目录成功\n");
    112. dev=device_create(cls,NULL,MKDEV(major,0),NULL,"si7006");
    113. if(IS_ERR(dev))
    114. {
    115. printk("向上提交设备信息失败\n");
    116. return PTR_ERR(dev);
    117. }
    118. printk("向上提交设备信息成功\n");
    119. return 0;
    120. }
    121. //定义一个probe函数
    122. int si7006_probe(struct i2c_client *client,const struct i2c_device_id *id)
    123. {
    124. tclient=client;//将总线驱动指针编程全局的
    125. //注册字符设备驱动
    126. rcv=chrdev();
    127. //获取节点信息
    128. node=of_find_node_by_name(NULL,"extend_dev");
    129. if(node==NULL)
    130. {
    131. printk("通过名字解析设备树节点失败\n");
    132. return -EFAULT;
    133. }
    134. printk("设备树解析成功\n");
    135. //获取并申请gpio编号,键值fengm
    136. gpiono=gpiod_get_from_of_node(node,"fengm",0,GPIOD_OUT_LOW,NULL);
    137. if(IS_ERR(gpiono))
    138. {
    139. printk("获取gpiono 失败\n");
    140. return PTR_ERR(gpiono);
    141. }
    142. printk("获取gpiono 成功\n");
    143. return 0;
    144. }
    145. //设备分离后执行remove
    146. int si7006_remove(struct i2c_client *client)
    147. {
    148. gpiod_set_value(gpiono,0);
    149. //销毁设备节点
    150. device_destroy(cls,MKDEV(major,0));
    151. //销毁目录
    152. class_destroy(cls);
    153. //驱动注销
    154. unregister_chrdev(major,"si7006");
    155. printk("%s:%d",__func__,__LINE__);
    156. return 0;
    157. }
    158. struct of_device_id oftable[]={
    159. {
    160. .compatible="hqyj,si7006",
    161. },
    162. {},
    163. };
    164. //热插拔宏,可以在插入硬件时,自动安装驱动
    165. MODULE_DEVICE_TABLE(of,oftable);
    166. //对象初始化
    167. struct i2c_driver si7006={
    168. .probe=si7006_probe,
    169. .remove=si7006_remove,
    170. .driver={
    171. .name="tem_hum_driver", //按名字匹配,只能匹配一个设备
    172. .of_match_table=oftable,//设备树匹配
    173. },
    174. };
    175. module_i2c_driver(si7006);
    176. MODULE_LICENSE("GPL");

     应用层代码:

    1. #include <stdio.h>
    2. #include <sys/types.h>
    3. #include <sys/stat.h>
    4. #include <fcntl.h>
    5. #include <unistd.h>
    6. #include <sys/ioctl.h>
    7. #include <stdlib.h>
    8. #include <arpa/inet.h>
    9. #include "si7006.h"
    10. int main(int argc, char const *argv[])
    11. {
    12. int hum,tem;
    13. float hum1,tem1;
    14. int ret;
    15. int ubuf[20];
    16. int fd=open("/dev/si7006",O_RDWR);
    17. if(fd<0)
    18. {
    19. printf("打开设备文件失败\n");
    20. exit(-1);
    21. }
    22. while(1)
    23. {
    24. //通过ioctl获取温湿度,放到tem,hum所在的地址
    25. ioctl(fd,GET_TEM,&tem);
    26. ioctl(fd,GET_HUM,&hum);
    27. //大端转小端
    28. hum=ntohs(hum);
    29. tem=ntohs(tem);
    30. //温湿度转换
    31. hum1=125.0*hum/65536-6;
    32. tem1=175.72*tem/65536-46.85;
    33. printf("tem:%f hum=%f\n",tem1,hum1);
    34. //当湿度达到70触发警报---PB6蜂鸣器
    35. if(hum1>=70)
    36. {
    37. ioctl(fd,FENGM);
    38. printf("警报\n");
    39. }
    40. sleep(1);
    41. }
    42. return 0;
    43. }

    头文件:

    1. #ifndef __SI7006_H__
    2. #define __SI7006_H__
    3. //命令码封装,方向,大小,类型m,功能0
    4. #define GET_TEM _IOR('m',0,int)
    5. #define GET_HUM _IOR('m',1,int)
    6. #define FENGM _IOW('u',3,int)
    7. #endif

    报错:

     [root@fsmp1a /]#rmmod driver.ko
    rmmod: can't unload module 'driver': Device or resource busy

    解决办法:

    vi  /proc/devices

    删除驱动信息,重启开发板

    实验现象: 

  • 相关阅读:
    vulnhub靶场之THE PLANETS: EARTH
    数据库课程设计——学籍管理系统
    C语言——内存函数
    element-ui组件table去除下方滚动条,实现鼠标左右拖拽移动表格
    运行时数据区域
    机械臂速成小指南(七):机械臂位姿的描述方法
    Kafka系列之:Kafka Connect错误报告设置
    ASEMI整流桥SKBPC3516,SKBPC3516参数,SKBPC3516应用
    map 详细说明
    【UE5】 ListView使用DataTable数据的蓝图方法
  • 原文地址:https://blog.csdn.net/m0_68004652/article/details/128122645