-
Notifications
You must be signed in to change notification settings - Fork 100
/
shtc3.c
104 lines (79 loc) · 2.55 KB
/
shtc3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
Copyright 2022 Benjamin Vedder [email protected]
This file is part of the VESC BMS firmware.
The VESC BMS firmware is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The VESC BMS firmware is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "i2c_bb.h"
#include "shtc3.h"
#include "string.h"
// Private variables
static i2c_bb_state m_i2c;
static float m_last_temp = 0.0;
static float m_last_hum = 0.0;
// Threads
static THD_WORKING_AREA(sample_thread_wa, 512);
static THD_FUNCTION(sample_thread, arg);
// Private functions
static uint8_t crc8(const uint8_t *data, uint8_t len);
void shtc3_init(
stm32_gpio_t *sda_gpio, int sda_pin,
stm32_gpio_t *scl_gpio, int scl_pin) {
memset(&m_i2c, 0, sizeof(i2c_bb_state));
m_i2c.sda_gpio = sda_gpio;
m_i2c.sda_pin = sda_pin;
m_i2c.scl_gpio = scl_gpio;
m_i2c.scl_pin = scl_pin;
i2c_bb_init(&m_i2c);
chThdCreateStatic(sample_thread_wa, sizeof(sample_thread_wa), LOWPRIO, sample_thread, NULL);
}
float shtc3_get_hum(void) {
return m_last_hum;
}
float shtc3_get_temp(void) {
return m_last_temp;
}
static THD_FUNCTION(sample_thread, arg) {
(void)arg;
chRegSetThreadName("SHTC3");
for(;;) {
m_i2c.has_error = 0;
uint8_t rxbuf[6];
(void)i2c_bb_tx_rx(&m_i2c, 0x70, 0, 0, rxbuf, 6);
if (rxbuf[2] == crc8(rxbuf, 2) && rxbuf[5] == crc8(rxbuf + 3, 2)) {
uint16_t temp = (uint16_t)rxbuf[0] << 8 | (uint16_t)rxbuf[1];
uint16_t hum = (uint16_t)rxbuf[3] << 8 | (uint16_t)rxbuf[4];
m_last_temp = (float)temp / 65535.0 * 175.0 - 45.0;
m_last_hum = (float)hum / 65535.0 * 100.0;
} else {
m_last_temp = 0.0;
m_last_hum = 0.0;
}
// Start next measurement
m_i2c.has_error = 0;
uint8_t txbuf[2];
txbuf[0] = 0x78;
txbuf[1] = 0x66;
i2c_bb_tx_rx(&m_i2c, 0x70, txbuf, 2, 0, 0);
chThdSleepMilliseconds(1000);
}
}
static uint8_t crc8(const uint8_t *data, uint8_t len) {
const uint8_t poly = 0x31;
uint8_t crc = 0xFF;
for (uint8_t j = len; j; --j) {
crc ^= *data++;
for (uint8_t i = 8; i; --i) {
crc = (crc & 0x80) ? (crc << 1) ^ poly : (crc << 1);
}
}
return crc;
}