做网站与做游戏那个好,网站域名怎么起,制作视频的网站软件,河南郑州网站关键词排名助手开发板#xff1a;STM32MP157A
温湿度传感器#xff1a;si7006
显示器#xff08;数码管#xff09;#xff1a;m74hc595
遇到的问题#xff1a;循环采集温湿度传感器数值#xff0c;并将数值发送给数码管的时候两者存在竞态关系#xff0c;导致数码管显示亮度很暗 …开发板STM32MP157A
温湿度传感器si7006
显示器数码管m74hc595
遇到的问题循环采集温湿度传感器数值并将数值发送给数码管的时候两者存在竞态关系导致数码管显示亮度很暗
解决办法采用多线程或者多进程解决内核竞态问题
驱动代码
#include linux/init.h
#include linux/module.h
#include linux/i2c.h
#include linux/fs.h
#include linux/device.h
#include head.hunsigned int major;
struct class *cls;
struct device *dev;
struct i2c_client *client1;//读取温湿度的函数
int i2c_read_hum_tem(char reg)
{short value;char r_buf[]{reg};int ret;//封装消息struct i2c_msg r_msg[]{[0]{.addrclient1-addr,.flags0,//先写.lensizeof(r_buf),.bufr_buf,},[1]{.addrclient1-addr,.flags1,.len2,.buf(char *)value,},};//将消息传送reti2c_transfer(client1-adapter,r_msg,2);if(ret!2){printk(消息传输失败\n);return -EIO;}return value;//将读取到的温湿度返回
}int si7006_open(struct inode *inode,struct file *file)
{printk(%s:%s:%d\n,__FILE__,__func__,__LINE__);return 0;
}long si7006_ioctl(struct file *file,unsigned int cmd,unsigned long arg)
{int tem,hum,ret;switch(cmd){case GET_HUM://读取湿度//读取湿度的逻辑humi2c_read_hum_tem(0xE5);retcopy_to_user((void *)arg,hum,4);//int类型4个字节if(ret){printk(copy_to_user error\n);return ret;}break;case GET_TEM://读取温度//读取温度的逻辑temi2c_read_hum_tem(0xE3);retcopy_to_user((void *)arg,tem,4);if(ret){printk(copy_to_user error\n);return ret;}break;}return 0;
}int si7006_close(struct inode *inode,struct file *file)
{printk(%s:%s:%d\n,__FILE__,__func__,__LINE__); return 0;
}//操作方法结构体
struct file_operations fops{.opensi7006_open,.unlocked_ioctlsi7006_ioctl,.releasesi7006_close,
};//给对象分配空间并且初始化
int i2c_probe(struct i2c_client *client,const struct i2c_device_id *id)
{client1client;//字符设备驱动的注册majorregister_chrdev(0,si7006,fops);if(major0){printk(register_chrdev failed\n); return major;}printk(register_chrdev success\n);//设备节点的创建//向上提交目录clsclass_create(THIS_MODULE,si7006);if(IS_ERR(cls)){printk(向上提交目录失败\n);return -PTR_ERR(cls);}printk(向上提交目录成功\n);//向上提交设备节点devdevice_create(cls,NULL,MKDEV(major,0),NULL,si7006);if(IS_ERR(dev)){printk(向上提交设备节点失败\n);return -PTR_ERR(dev); }printk(向上提交设备节点成功\n);return 0;
}int i2c_remove(struct i2c_client *client)
{//销毁节点device_destroy(cls,MKDEV(major,0));//销毁目录class_destroy(cls);//注销字符设备驱动unregister_chrdev(major,si7006);return 0;
}
//定义设备树匹配的表
struct of_device_id oftable[]{{.compatiblehqyj,si7006,},{},
};//分配IIC驱动信息对象
struct i2c_driver i2c_drv{.probei2c_probe,.removei2c_remove,.driver{.namesi7006,.of_match_tableoftable,},
};module_i2c_driver(i2c_drv);
MODULE_LICENSE(GPL);
应用层代码
#include stdio.h
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include unistd.h
#include stdlib.h
#include string.h
#include sys/ioctl.h
#include arpa/inet.h
#include head.h
#include pthread.hint number1000;
int *pnumber;
int flag 0;void *shuma_callback(void *arg)
{while(1){//将温湿度传给数码管ioctl显示,发送数据ioctl(*(int *)arg,GET_SHUMA,p);} pthread_exit(NULL);
}int main(int argc,char const *argv[])
{int tem,hum;float tem1,hum1;int fd_i2copen(/dev/si7006,O_RDWR);if(fd_i2c0){printf(设备文件打开失败\n);exit(-1);}//温湿度值采用数码管显示int fd_spiopen(/dev/m74hc595,O_RDWR);if(fd_spi0){printf(设备文件打开失败\n);exit(-1);}pthread_t tid_1;if(pthread_create(tid_1,NULL,shuma_callback,(void *)fd_spi)!0){fprintf(stderr,pthread_create failed__%d__\n,__LINE__);return -1;}pthread_detach(tid_1);//分离线程while(1){//获取数据ioctl(fd_i2c,GET_HUM,hum);ioctl(fd_i2c,GET_TEM,tem);printf(hum%d,tem%d\n,hum,tem);//大小端转换humntohs(hum);temntohs(tem);//计算数据hum1125.0*hum/65536-6;tem1175.72*tem/65536-46.85;printf(hum1%d,tem1%d\n,hum1,tem1);number(int)hum1 * 100 (int)tem1;sleep(1);}pthread_join(tid_1,NULL);//阻塞等待tid_1线程退出return 0;
}
头文件
#ifndef __HEAD_H__
#define __HEAD_H__#define GET_SHUMA _IOR(m,2,int)//获取数码管的功能码
#define GET_HUM _IOR(m,1,int)//获取湿度的功能码
#define GET_TEM _IOR(m,0,int)//获取温度的功能码#endif