| | |
| | | /********************************************************************************* |
| | | * Copyright: (C) 2018 LingYun IoT System Studio |
| | | * All rights reserved. |
| | | * |
| | | * Filename: sht20.c |
| | | * Description: This file is temperature and relative humidity sensor SHT20 code |
| | | * |
| | | * Version: 1.0.0(2018/10/14) |
| | | * Author: Guo Wenxue <guowenxue@gmail.com> |
| | | * ChangeLog: 1, Release initial version on "2018/10/14 12:13:26" |
| | | * |
| | | ********************************************************************************/ |
| | | |
| | | #include <stdio.h> |
| | | #include <fcntl.h> |
| | |
| | | static inline void msleep(unsigned long ms); |
| | | static inline void dump_buf(const char *prompt, uint8_t *buf, int size); |
| | | int sht2x_init(void); |
| | | int sht2x_softreset(int fd); |
| | | int sht2x_get_serialnumber(int fd, uint8_t *serialnumber, int size); |
| | | int sht2x_get_temp_humidity(int fd, float *temp, float *rh); |
| | | |
| | |
| | | { |
| | | printf("SHT2x initialize failure\n"); |
| | | return 1; |
| | | } |
| | | |
| | | if( sht2x_softreset(fd) < 0 ) |
| | | { |
| | | printf("SHT2x softreset failure\n"); |
| | | return 2; |
| | | } |
| | | |
| | | if( sht2x_get_serialnumber(fd, serialnumber, 8) < 0) |
| | |
| | | |
| | | #ifdef I2C_API_RDWR /* Use I2C userspace driver read/write API */ |
| | | |
| | | int sht2x_init(void) |
| | | { |
| | | int fd; |
| | | |
| | | if( (fd=open("/dev/i2c-1", O_RDWR)) < 0) |
| | | { |
| | | printf("i2c device open failed: %s\n", strerror(errno)); |
| | | return -1; |
| | | } |
| | | |
| | | /* set I2C mode and SHT2x slave address */ |
| | | ioctl(fd, I2C_TENBIT, 0); /* Not 10-bit but 7-bit mode */ |
| | | ioctl(fd, I2C_SLAVE, 0x40); /* set SHT2x slava address 0x40*/ |
| | | |
| | | return fd; |
| | | } |
| | | |
| | | int sht2x_softreset(int fd) |
| | | { |
| | | uint8_t buf[4]; |
| | |
| | | msleep(50); |
| | | |
| | | return 0; |
| | | } |
| | | |
| | | int sht2x_init(void) |
| | | { |
| | | int fd; |
| | | |
| | | if( (fd=open("/dev/i2c-1", O_RDWR)) < 0) |
| | | { |
| | | printf("i2c device open failed: %s\n", strerror(errno)); |
| | | return -1; |
| | | } |
| | | |
| | | /* set I2C mode and SHT2x slave address */ |
| | | ioctl(fd, I2C_TENBIT, 0); /* Not 10-bit but 7-bit mode */ |
| | | ioctl(fd, I2C_SLAVE, 0x40); /* set SHT2x slava address 0x40*/ |
| | | |
| | | if( sht2x_softreset(fd) < 0 ) |
| | | { |
| | | printf("SHT2x softreset failure\n"); |
| | | return -2; |
| | | } |
| | | |
| | | return fd; |
| | | } |
| | | |
| | | int sht2x_get_temp_humidity(int fd, float *temp, float *rh) |
| | |
| | | |
| | | #elif (defined I2C_API_IOCTL) /* Use I2C userspace driver read/write API */ |
| | | |
| | | int sht2x_init(void) |
| | | { |
| | | int fd; |
| | | |
| | | if( (fd=open("/dev/i2c-1", O_RDWR)) < 0) |
| | | { |
| | | printf("i2c device open failed: %s\n", strerror(errno)); |
| | | return -1; |
| | | } |
| | | |
| | | return fd; |
| | | } |
| | | |
| | | |
| | | int sht2x_softreset(int fd) |
| | | { |
| | | struct i2c_msg msg; |
| | |
| | | return 0; |
| | | } |
| | | |
| | | |
| | | int sht2x_init(void) |
| | | { |
| | | int fd; |
| | | |
| | | if( (fd=open("/dev/i2c-1", O_RDWR)) < 0) |
| | | { |
| | | printf("i2c device open failed: %s\n", strerror(errno)); |
| | | return -1; |
| | | } |
| | | |
| | | if( sht2x_softreset(fd) < 0 ) |
| | | { |
| | | printf("SHT2x softreset failure\n"); |
| | | return -2; |
| | | } |
| | | |
| | | return fd; |
| | | } |
| | | |
| | | |
| | | int sht2x_get_serialnumber(int fd, uint8_t *serialnumber, int size) |
| | |
| | | |
| | | return 0; |
| | | } |
| | | |
| | | |
| | | |
| | | int sht2x_get_temp_humidity(int fd, float *temp, float *rh) |