47 #include "lib/sensors.h" 51 #define PRINTF(...) printf(__VA_ARGS__) 56 static uint8_t enabled;
57 static uint8_t user_reg;
71 sht25_read_reg(uint8_t reg, uint8_t *buf, uint8_t num)
73 if((buf == NULL) || (num <= 0)) {
87 sht25_convert(uint8_t variable, uint16_t value)
93 buff = (uint32_t)(value & ~SHT25_STATUS_BITS_MASK);
95 if(variable == SHT25_VAL_TEMP) {
98 rd = (int16_t)buff - 4685;
102 rd = (int16_t)buff - 600;
103 rd = (rd > 10000) ? 10000 : rd;
109 sht25_read(uint8_t variable, uint16_t *rd)
114 if((variable != SHT25_VAL_TEMP) && (variable != SHT25_VAL_HUM)) {
115 PRINTF(
"SHT25: invalid sensor requested\n");
119 if(sht25_read_reg(variable, buf, 2) == SHT25_SUCCESS) {
120 raw = (buf[0] << 8) + buf[1];
121 *rd = sht25_convert(variable, raw);
122 return SHT25_SUCCESS;
125 PRINTF(
"SHT25: failed to read sensor\n");
130 sht25_write_reg(uint8_t *buf, uint8_t num)
132 if((buf == NULL) || (num <= 0)) {
133 PRINTF(
"SHT25: invalid write values\n");
139 return SHT25_SUCCESS;
145 sht25_read_user_register(
void)
147 if(sht25_read_reg(SHT2X_UREG_READ, &user_reg, 1) == SHT25_SUCCESS) {
148 PRINTF(
"SHT25: user register 0x%02X\n", user_reg);
149 return SHT25_SUCCESS;
151 PRINTF(
"SHT25: failed to read user register\n");
161 PRINTF(
"SHT25: sensor not started\n");
165 if((type != SHT25_VAL_TEMP) && (type != SHT25_VAL_HUM) &&
166 (type != SHT25_VOLTAGE_ALARM)) {
167 PRINTF(
"SHT25: invalid value requested\n");
171 if(type == SHT25_VOLTAGE_ALARM) {
172 if(sht25_read_user_register() == SHT25_SUCCESS) {
173 return (user_reg & SHT2x_LOW_VOLTAGE_MASK) >> SHT2x_LOW_VOLTAGE_SHIFT;
176 if(sht25_read(type, &val) == SHT25_SUCCESS) {
184 configure(
int type,
int value)
188 if((type != SHT25_ACTIVE) && (type != SHT25_SOFT_RESET) &&
189 (type != SHT25_RESOLUTION)) {
190 PRINTF(
"SHT25: option not supported\n");
197 i2c_init(I2C_SDA_PORT, I2C_SDA_PIN, I2C_SCL_PORT, I2C_SCL_PIN,
198 I2C_SCL_NORMAL_BUS_SPEED);
201 if(sht25_read_user_register() == SHT25_SUCCESS) {
203 return SHT25_SUCCESS;
207 case SHT25_SOFT_RESET:
208 buf[0] = SHT2X_SOFT_RESET;
209 if(sht25_write_reg(&buf[0], 1) != SHT25_SUCCESS) {
210 PRINTF(
"SHT25: failed to reset the sensor\n");
214 return SHT25_SUCCESS;
216 case SHT25_RESOLUTION:
217 if((value != SHT2X_RES_14T_12RH) && (value != SHT2X_RES_12T_08RH) &&
218 (value != SHT2X_RES_13T_10RH) && (value != SHT2X_RES_11T_11RH)) {
219 PRINTF(
"SHT25: invalid resolution value\n");
223 user_reg &= ~SHT2X_RES_11T_11RH;
225 buf[0] = SHT2X_UREG_WRITE;
228 if(sht25_write_reg(buf, 2) == SHT25_SUCCESS) {
229 PRINTF(
"SHT25: new user register value 0x%02X\n", user_reg);
230 return SHT25_SUCCESS;
240 SENSORS_SENSOR(sht25, SHT25_SENSOR, value, configure, status);
uint8_t i2c_burst_send(uint8_t slave_addr, uint8_t *data, uint8_t len)
Perform all operations to send multiple bytes to a slave.
void clock_delay_usec(uint16_t dt)
Delay a given number of microseconds.
SHT25 temperature and humidity sensor driver header file.
void i2c_init(uint8_t port_sda, uint8_t pin_sda, uint8_t port_scl, uint8_t pin_scl, uint32_t bus_speed)
Initialize the I2C peripheral and pins.
uint8_t i2c_burst_receive(uint8_t slave_addr, uint8_t *data, uint8_t len)
Perform all operations to receive multiple bytes from a slave.
uint8_t i2c_single_send(uint8_t slave_addr, uint8_t data)
Perform all operations to send a byte to a slave.
void i2c_master_enable(void)
Enable master I2C module.