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
31#include "mcu_periph/i2c.h"
32#include "mcu_periph/uart.h"
33#include "pprzlink/messages.h"
35
36
37#ifndef SHT_I2C_DEV
38#define SHT_I2C_DEV i2c0
39#endif
40
41#define SHT_SLAVE_ADDR 0x80
42
49
51{
52 uint8_t i, bit, crc = 0;
53
54 for (i = 0; i < 2; i++) {
55 crc ^= (data[i]);
56 for (bit = 8; bit > 0; bit--) {
57 if (crc & 0x80) {
58 crc = (crc << 1) ^ 0x131;
59 } else {
60 crc = (crc << 1);
61 }
62 }
63 }
64 if (crc != data[2]) {
65 return -1;
66 } else {
67 return 0;
68 }
69}
70
72{
74}
75
77{
78 switch (sht_status) {
79
80 case SHT2_UNINIT:
81 /* do soft reset, then wait at least 15ms */
85 break;
86
87 case SHT2_SERIAL:
88 /* get serial number part 1 */
90 sht_trans.buf[0] = 0xFA;
91 sht_trans.buf[1] = 0x0F;
93 break;
94
95 case SHT2_SERIAL1:
96 case SHT2_SERIAL2:
97 break;
98
99 default:
100 /* trigger temp measurement, no master hold */
104 /* send serial number every 30 seconds */
106 break;
107 }
108}
109
110/* needs 85ms delay from temp trigger measurement */
112{
113 if (sht_status == SHT2_GET_TEMP) {
114 /* get temp */
117 }
118}
119
120/* needs 29ms delay from humid trigger measurement */
122{
123 if (sht_status == SHT2_GET_HUMID) {
124 /* read humid */
127 }
128}
129
131{
133 switch (sht_status) {
134
135 case SHT2_TRIG_TEMP:
138 break;
139
140 case SHT2_READ_TEMP:
141 /* read temperature */
142 tempsht_i2c = (sht_trans.buf[0] << 8) | sht_trans.buf[1];
143 tempsht_i2c &= 0xFFFC;
144 if (humid_sht_crc(sht_trans.buf) == 0) {
145 /* trigger humid measurement, no master hold */
149 } else {
150 /* checksum error, restart */
153 }
154 break;
155
156 case SHT2_TRIG_HUMID:
159 break;
160
161 case SHT2_READ_HUMID:
162 /* read humidity */
163 humidsht_i2c = (sht_trans.buf[0] << 8) | sht_trans.buf[1];
164 humidsht_i2c &= 0xFFFC;
165 fhumidsht_i2c = -6. + 125. / 65536. * humidsht_i2c;
166 ftempsht_i2c = -46.85 + 175.72 / 65536. * tempsht_i2c;
167
170
171 if (humid_sht_crc(sht_trans.buf) == 0) {
173 }
174 break;
175
176 case SHT2_RESET:
179 break;
180
181 case SHT2_SERIAL1:
182 /* read serial number part 1 */
183 sht_serial[5] = sht_trans.buf[0];
184 sht_serial[4] = sht_trans.buf[2];
185 sht_serial[3] = sht_trans.buf[4];
186 sht_serial[2] = sht_trans.buf[6];
187 /* get serial number part 2 */
189 sht_trans.buf[0] = 0xFC;
190 sht_trans.buf[1] = 0xC9;
192 break;
193
194 case SHT2_SERIAL2:
195 /* read serial number part 2 */
196 sht_serial[1] = sht_trans.buf[0];
197 sht_serial[0] = sht_trans.buf[1];
198 sht_serial[7] = sht_trans.buf[3];
199 sht_serial[6] = sht_trans.buf[4];
200 sht_serial1 = sht_serial[7] << 24 | sht_serial[6] << 16 | sht_serial[5] << 8 | sht_serial[4];
201 sht_serial2 = sht_serial[3] << 24 | sht_serial[2] << 16 | sht_serial[1] << 8 | sht_serial[0];
205 break;
206
207 default:
209 break;
210 }
211 }
212}
213
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition i2c.h:122
enum I2CTransactionStatus status
Transaction status.
Definition i2c.h:126
bool i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Submit a write only transaction.
Definition i2c.c:202
bool i2c_receive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint16_t len)
Submit a read only transaction.
Definition i2c.c:212
bool i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r)
Submit a write/read transaction.
Definition i2c.c:222
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition i2c.h:57
@ I2CTransDone
transaction set to done by user level
Definition i2c.h:59
I2C transaction structure.
Definition i2c.h:93
struct i2c_transaction sht_trans
uint16_t tempsht_i2c
#define SHT_SLAVE_ADDR
void humid_sht_periodic_i2c(void)
uint32_t sht_serial1
uint8_t sht_serial[8]
void humid_sht_p_temp(void)
int8_t humid_sht_crc(volatile uint8_t *data)
void humid_sht_p_humid(void)
uint32_t sht_serial2
void humid_sht_event_i2c(void)
void humid_sht_init_i2c(void)
#define SHT_I2C_DEV
uint8_t sht_status
float ftempsht_i2c
uint16_t humidsht_i2c
float fhumidsht_i2c
#define SHT2_TRIGGER_TEMP
#define SHT2_SOFT_RESET
@ SHT2_SERIAL2
@ SHT2_READ_TEMP
@ SHT2_RESET
@ SHT2_SERIAL
@ SHT2_GET_TEMP
@ SHT2_TRIG_TEMP
@ SHT2_UNINIT
@ SHT2_READ_HUMID
@ SHT2_GET_HUMID
@ SHT2_IDLE
@ SHT2_TRIG_HUMID
@ SHT2_SERIAL1
#define SHT2_TRIGGER_HUMID
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
uint16_t foo
Definition main_demo5.c:58
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
signed char int8_t
Typedef defining 8 bit char type.