Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
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 
50 int8_t humid_sht_crc(volatile uint8_t *data)
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 */
105  RunOnceEvery((4 * 30), DOWNLINK_SEND_SHT_I2C_SERIAL(DefaultChannel, DefaultDevice, &sht_serial1, &sht_serial2));
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) {
172  DOWNLINK_SEND_SHT_I2C_STATUS(DefaultChannel, DefaultDevice, &humidsht_i2c, &tempsht_i2c, &fhumidsht_i2c, &ftempsht_i2c);
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];
202  DOWNLINK_SEND_SHT_I2C_SERIAL(DefaultChannel, DefaultDevice, &sht_serial1, &sht_serial2);
205  break;
206 
207  default:
209  break;
210  }
211  }
212 }
213 
SHT2_TRIG_TEMP
@ SHT2_TRIG_TEMP
Definition: humid_sht_i2c.h:21
uint16_t
unsigned short uint16_t
Definition: types.h:16
humidsht_i2c
uint16_t humidsht_i2c
Definition: humid_sht_i2c.c:47
i2c_transaction::buf
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
SHT2_UNINIT
@ SHT2_UNINIT
Definition: humid_sht_i2c.h:13
sht_serial2
uint32_t sht_serial2
Definition: humid_sht_i2c.c:46
SHT2_SOFT_RESET
#define SHT2_SOFT_RESET
Definition: humid_sht_i2c.h:10
SHT2_GET_TEMP
@ SHT2_GET_TEMP
Definition: humid_sht_i2c.h:22
uint32_t
unsigned long uint32_t
Definition: types.h:18
sht_trans
struct i2c_transaction sht_trans
Definition: humid_sht_i2c.c:43
ftempsht_i2c
float ftempsht_i2c
Definition: humid_sht_i2c.c:48
tempsht_i2c
uint16_t tempsht_i2c
Definition: humid_sht_i2c.c:47
I2CTransSuccess
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition: i2c.h:57
SHT2_READ_HUMID
@ SHT2_READ_HUMID
Definition: humid_sht_i2c.h:26
uart.h
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
humid_sht_periodic_i2c
void humid_sht_periodic_i2c(void)
Definition: humid_sht_i2c.c:76
i2c_transmit
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:324
humid_sht_i2c.h
SHT2_SERIAL1
@ SHT2_SERIAL1
Definition: humid_sht_i2c.h:17
SHT2_SERIAL
@ SHT2_SERIAL
Definition: humid_sht_i2c.h:16
SHT2_TRIGGER_HUMID
#define SHT2_TRIGGER_HUMID
Definition: humid_sht_i2c.h:9
humid_sht_crc
int8_t humid_sht_crc(volatile uint8_t *data)
Definition: humid_sht_i2c.c:50
humid_sht_p_temp
void humid_sht_p_temp(void)
Definition: humid_sht_i2c.c:111
SHT2_GET_HUMID
@ SHT2_GET_HUMID
Definition: humid_sht_i2c.h:25
i2c_transceive
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:344
uint8_t
unsigned char uint8_t
Definition: types.h:14
i2c_transaction::status
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
SHT2_READ_TEMP
@ SHT2_READ_TEMP
Definition: humid_sht_i2c.h:23
if
if(GpsFixValid() &&e_identification_started)
Definition: e_identification_fr.c:159
i2c_transaction
I2C transaction structure.
Definition: i2c.h:93
humid_sht_init_i2c
void humid_sht_init_i2c(void)
Definition: humid_sht_i2c.c:71
SHT2_TRIG_HUMID
@ SHT2_TRIG_HUMID
Definition: humid_sht_i2c.h:24
sht_serial
uint8_t sht_serial[8]
Definition: humid_sht_i2c.c:45
SHT2_TRIGGER_TEMP
#define SHT2_TRIGGER_TEMP
Definition: humid_sht_i2c.h:8
int8_t
signed char int8_t
Definition: types.h:15
SHT_SLAVE_ADDR
#define SHT_SLAVE_ADDR
Definition: humid_sht_i2c.c:41
fhumidsht_i2c
float fhumidsht_i2c
Definition: humid_sht_i2c.c:48
SHT2_IDLE
@ SHT2_IDLE
Definition: humid_sht_i2c.h:14
SHT2_SERIAL2
@ SHT2_SERIAL2
Definition: humid_sht_i2c.h:18
I2CTransDone
@ I2CTransDone
transaction set to done by user level
Definition: i2c.h:59
SHT2_RESET
@ SHT2_RESET
Definition: humid_sht_i2c.h:15
i2c.h
humid_sht_p_humid
void humid_sht_p_humid(void)
Definition: humid_sht_i2c.c:121
sht_status
uint8_t sht_status
Definition: humid_sht_i2c.c:44
i2c_receive
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:334
SHT_I2C_DEV
#define SHT_I2C_DEV
Definition: humid_sht_i2c.c:38
sht_serial1
uint32_t sht_serial1
Definition: humid_sht_i2c.c:46
humid_sht_event_i2c
void humid_sht_event_i2c(void)
Definition: humid_sht_i2c.c:130