Paparazzi UAS  v5.15_devel-230-gc96ce27
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 
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 
unsigned short uint16_t
Definition: types.h:16
#define SHT_I2C_DEV
Definition: humid_sht_i2c.c:38
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
uint32_t sht_serial1
Definition: humid_sht_i2c.c:46
void humid_sht_event_i2c(void)
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
#define SHT_SLAVE_ADDR
Definition: humid_sht_i2c.c:41
transaction successfully finished by I2C driver
Definition: i2c.h:57
void humid_sht_p_humid(void)
#define SHT2_TRIGGER_HUMID
Definition: humid_sht_i2c.h:9
float ftempsht_i2c
Definition: humid_sht_i2c.c:48
float fhumidsht_i2c
Definition: humid_sht_i2c.c:48
void humid_sht_init_i2c(void)
Definition: humid_sht_i2c.c:71
transaction set to done by user level
Definition: i2c.h:59
unsigned long uint32_t
Definition: types.h:18
uint16_t humidsht_i2c
Definition: humid_sht_i2c.c:47
int8_t humid_sht_crc(volatile uint8_t *data)
Definition: humid_sht_i2c.c:50
uint8_t sht_serial[8]
Definition: humid_sht_i2c.c:45
#define SHT2_TRIGGER_TEMP
Definition: humid_sht_i2c.h:8
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
I2C transaction structure.
Definition: i2c.h:93
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:324
uint32_t sht_serial2
Definition: humid_sht_i2c.c:46
unsigned char uint8_t
Definition: types.h:14
uint8_t sht_status
Definition: humid_sht_i2c.c:44
struct i2c_transaction sht_trans
Definition: humid_sht_i2c.c:43
#define SHT2_SOFT_RESET
Definition: humid_sht_i2c.h:10
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
void humid_sht_p_temp(void)
uint16_t tempsht_i2c
Definition: humid_sht_i2c.c:47
void humid_sht_periodic_i2c(void)
Definition: humid_sht_i2c.c:76
signed char int8_t
Definition: types.h:15
Architecture independent I2C (Inter-Integrated Circuit Bus) API.