Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
humid_sht_i2c.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010 Martin Mueller
3 *
4 * This file is part of paparazzi.
5 *
6 * paparazzi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * paparazzi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with paparazzi; see the file COPYING. If not, write to
18 * the Free Software Foundation, 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 *
21 */
22
27
28
30#include "pprzlink/messages.h"
32
33
34#ifndef SHT_I2C_DEV
35#define SHT_I2C_DEV i2c0
36#endif
37
38#define SHT_SLAVE_ADDR 0x80
39
40#define SHT2_WRITE_USER 0xE6
41#define SHT2_READ_USER 0xE7
42#define SHT2_TRIGGER_TEMP 0xF3
43#define SHT2_TRIGGER_HUMID 0xF5
44#define SHT2_SOFT_RESET 0xFE
45
46static void humid_sht_thd(void* arg);
47static void sht_read_serial(struct sht_humid_t* sht);
48static int sht_read_temp(struct sht_humid_t* sht);
49static int sht_read_humid(struct sht_humid_t* sht);
50static int8_t humid_sht_crc(volatile uint8_t *data);
51
52
53
54static struct sht_humid_t sht;
55
56
58{
59 sht.sht_status = SHT2_UNINIT;
60 pprz_bsem_init(&sht.bsem_sht_status, true);
61 pprz_thread_create(&sht.thd_handle, 512, "humid_sht25", PPRZ_NORMAL_PRIO+1, humid_sht_thd, &sht);
62}
63
65{
66 if(sht.sht_status == SHT2_READING) {
67 /* send serial number every 30 seconds */
68 RunOnceEvery((4 * 30), DOWNLINK_SEND_SHT_I2C_SERIAL(DefaultChannel, DefaultDevice, &sht.sht_serial1, &sht.sht_serial2));
69 }
70
71 if(pprz_bsem_wait_timeout(&sht.bsem_sht_status, 0) == 0) {
72 DOWNLINK_SEND_SHT_I2C_STATUS(DefaultChannel, DefaultDevice, &sht.humidsht_i2c, &sht.tempsht_i2c, &sht.fhumidsht_i2c, &sht.ftempsht_i2c);
73 }
74}
75
76
77static void humid_sht_thd(void* arg) {
78 sys_time_usleep(100000);
79 struct sht_humid_t* sht = (struct sht_humid_t*)arg;
80
81 /* soft reset sensor */
82 sht->sht_trans.buf[0] = SHT2_SOFT_RESET;
83 while(i2c_blocking_transmit(&SHT_I2C_DEV, &sht->sht_trans, SHT_SLAVE_ADDR, 1, 0.5) != I2CTransSuccess) {
84 sys_time_usleep(100000);
85 }
86
87 /* read serial number */
89
90 sht->sht_status = SHT2_READING;
91
92 while(true) {
93 /* read temperature */
94 if(sht_read_temp(sht)) {continue;}
95 /* read humidity */
96 if(sht_read_humid(sht)) {continue;}
97 /* signal semaphore to handle data */
98 pprz_bsem_signal(&sht->bsem_sht_status);
99
100 sys_time_usleep(500000);
101 }
102}
103
104
105static void sht_read_serial(struct sht_humid_t* sht) {
106 uint8_t sht_serial[8] = {0};
107
108 /* request serial number part 1 */
109 sht->sht_trans.buf[0] = 0xFA;
110 sht->sht_trans.buf[1] = 0x0F;
111 while(i2c_blocking_transceive(&SHT_I2C_DEV, &sht->sht_trans, SHT_SLAVE_ADDR, 2, 8, 0.5) != I2CTransSuccess) {
112 sys_time_usleep(10000);
113 }
114 /* read serial number part 1 */
115 sht_serial[5] = sht->sht_trans.buf[0];
116 sht_serial[4] = sht->sht_trans.buf[2];
117 sht_serial[3] = sht->sht_trans.buf[4];
118 sht_serial[2] = sht->sht_trans.buf[6];
119
120 /* request serial number part 2 */
121 sht->sht_trans.buf[0] = 0xFC;
122 sht->sht_trans.buf[1] = 0xC9;
123 while(i2c_blocking_transceive(&SHT_I2C_DEV, &sht->sht_trans, SHT_SLAVE_ADDR, 2, 6, 0.5) != I2CTransSuccess) {
124 sys_time_usleep(10000);
125 }
126 /* read serial number part 2 */
127 sht_serial[1] = sht->sht_trans.buf[0];
128 sht_serial[0] = sht->sht_trans.buf[1];
129 sht_serial[7] = sht->sht_trans.buf[3];
130 sht_serial[6] = sht->sht_trans.buf[4];
131
132 sht->sht_serial1 = sht_serial[7] << 24 | sht_serial[6] << 16 | sht_serial[5] << 8 | sht_serial[4];
133 sht->sht_serial2 = sht_serial[3] << 24 | sht_serial[2] << 16 | sht_serial[1] << 8 | sht_serial[0];
134}
135
136static int sht_read_temp(struct sht_humid_t* sht) {
137 /* trigger temp measurement, no master hold */
138 sht->sht_trans.buf[0] = SHT2_TRIGGER_TEMP;
139 if(i2c_blocking_transmit(&SHT_I2C_DEV, &sht->sht_trans, SHT_SLAVE_ADDR, 1, 0.5) != I2CTransSuccess ) {
140 return -1;
141 }
142 /* needs 85ms delay from temp trigger measurement */
143 sys_time_usleep(85000);
144 /* read temperature */
145 if(i2c_blocking_receive(&SHT_I2C_DEV, &sht->sht_trans, SHT_SLAVE_ADDR, 3, 0.5) != I2CTransSuccess) {
146 return -1;
147 }
148 if (humid_sht_crc(sht->sht_trans.buf) != 0) {
149 /* checksum error*/
150 return -1;
151 }
152
153 sht->tempsht_i2c = ((sht->sht_trans.buf[0] << 8) | sht->sht_trans.buf[1]) & 0xFFFC;
154 sht->ftempsht_i2c = -46.85 + 175.72 / 65536. * sht->tempsht_i2c;
155 return 0;
156}
157
158static int sht_read_humid(struct sht_humid_t* sht) {
159 /* trigger humid measurement, no master hold */
160 sht->sht_trans.buf[0] = SHT2_TRIGGER_HUMID;
162 return -1;
163 }
164 /* needs 29ms delay from humid trigger measurement */
165 sys_time_usleep(29000);
166 /* read humidity */
167 if(i2c_blocking_receive(&SHT_I2C_DEV, &sht->sht_trans, SHT_SLAVE_ADDR, 3, 0.5) != I2CTransSuccess) {
168 return -1;
169 }
170 if (humid_sht_crc(sht->sht_trans.buf) != 0) {
171 /* checksum error */
172 return -1;
173 }
174 sht->humidsht_i2c = ((sht->sht_trans.buf[0] << 8) | sht->sht_trans.buf[1]) & 0xFFFC;
175 sht->fhumidsht_i2c = -6. + 125. / 65536. * sht->humidsht_i2c;
176 return 0;
177}
178
179static int8_t humid_sht_crc(volatile uint8_t *data)
180{
181 uint8_t i, bit, crc = 0;
182
183 for (i = 0; i < 2; i++) {
184 crc ^= (data[i]);
185 for (bit = 8; bit > 0; bit--) {
186 if (crc & 0x80) {
187 crc = (crc << 1) ^ 0x131;
188 } else {
189 crc = (crc << 1);
190 }
191 }
192 }
193 if (crc != data[2]) {
194 return -1;
195 } else {
196 return 0;
197 }
198}
void sys_time_usleep(uint32_t us)
sys_time_usleep(uint32_t us)
int pprz_bsem_wait_timeout(pprz_bsem_t *bsem, float timeout)
Wait on semaphore no more than timeout.
void pprz_bsem_init(pprz_bsem_t *bsem, bool taken)
int pprz_thread_create(pprz_thread_t *thread, size_t size, const char *name, uint8_t prio, void(*pf)(void *), void *arg)
Creates a new thread whose stack is dynamically allocated.
void pprz_bsem_signal(pprz_bsem_t *bsem)
#define PPRZ_NORMAL_PRIO
enum I2CTransactionStatus i2c_blocking_receive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint16_t len, float timeout)
Submit a read only transaction and wait for it to complete.
Definition i2c.c:255
enum I2CTransactionStatus i2c_blocking_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len, float timeout)
Submit a write only transaction and wait for it to complete.
Definition i2c.c:245
enum I2CTransactionStatus i2c_blocking_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r, float timeout)
Submit a write/read transaction and wait for it to complete.
Definition i2c.c:265
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition i2c.h:58
static struct sht_humid_t sht
#define SHT2_TRIGGER_TEMP
#define SHT_SLAVE_ADDR
static void sht_read_serial(struct sht_humid_t *sht)
void humid_sht_periodic_i2c(void)
static int sht_read_temp(struct sht_humid_t *sht)
static void humid_sht_thd(void *arg)
static int sht_read_humid(struct sht_humid_t *sht)
#define SHT2_SOFT_RESET
static int8_t humid_sht_crc(volatile uint8_t *data)
void humid_sht_init_i2c(void)
#define SHT_I2C_DEV
#define SHT2_TRIGGER_HUMID
@ SHT2_UNINIT
@ SHT2_READING
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
signed char int8_t
Typedef defining 8 bit char type.