Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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
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
63
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 */
84 sys_time_usleep(100000);
85 }
86
87 /* read serial number */
89
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 */
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;
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;
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 */
140 return -1;
141 }
142 /* needs 85ms delay from temp trigger measurement */
143 sys_time_usleep(85000);
144 /* read temperature */
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 */
162 return -1;
163 }
164 /* needs 29ms delay from humid trigger measurement */
165 sys_time_usleep(29000);
166 /* read humidity */
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
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition i2c.h:123
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
pprz_thread_t thd_handle
enum sht_stat_i2c sht_status
float ftempsht_i2c
pprz_bsem_t bsem_sht_status
uint32_t sht_serial1
@ SHT2_UNINIT
@ SHT2_READING
uint16_t humidsht_i2c
uint16_t tempsht_i2c
float fhumidsht_i2c
uint32_t sht_serial2
struct i2c_transaction sht_trans
uint16_t foo
Definition main_demo5.c:58
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
signed char int8_t
Typedef defining 8 bit char type.