Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures 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 "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  uint8_t i, bit, crc = 0;
52 
53  for (i = 0; i < 2; i++) {
54  crc ^= (data[i]);
55  for (bit = 8; bit > 0; bit--) {
56  if (crc & 0x80)
57  crc = (crc << 1) ^ 0x131;
58  else
59  crc = (crc << 1);
60  }
61  }
62  if (crc != data[2])
63  return -1;
64  else
65  return 0;
66 }
67 
68 void humid_sht_init_i2c(void) {
70 }
71 
72 void humid_sht_periodic_i2c( void ) {
73  switch (sht_status) {
74 
75  case SHT2_UNINIT:
76  /* do soft reset, then wait at least 15ms */
80  break;
81 
82  case SHT2_SERIAL:
83  /* get serial number part 1 */
85  sht_trans.buf[0] = 0xFA;
86  sht_trans.buf[1] = 0x0F;
88  break;
89 
90  case SHT2_SERIAL1:
91  case SHT2_SERIAL2:
92  break;
93 
94  default:
95  /* trigger temp measurement, no master hold */
99  /* send serial number every 30 seconds */
100  RunOnceEvery((4*30), DOWNLINK_SEND_SHT_I2C_SERIAL(DefaultChannel, DefaultDevice, &sht_serial1, &sht_serial2));
101  break;
102  }
103 }
104 
105 /* needs 85ms delay from temp trigger measurement */
106 void humid_sht_p_temp( void ) {
107  if (sht_status == SHT2_GET_TEMP) {
108  /* get temp */
111  }
112 }
113 
114 /* needs 29ms delay from humid trigger measurement */
115 void humid_sht_p_humid( void ) {
116  if (sht_status == SHT2_GET_HUMID) {
117  /* read humid */
120  }
121 }
122 
123 void humid_sht_event_i2c( void ) {
125  switch (sht_status) {
126 
127  case SHT2_TRIG_TEMP:
130  break;
131 
132  case SHT2_READ_TEMP:
133  /* read temperature */
134  tempsht_i2c = (sht_trans.buf[0] << 8) | sht_trans.buf[1];
135  tempsht_i2c &= 0xFFFC;
136  if (humid_sht_crc(sht_trans.buf) == 0) {
137  /* trigger humid measurement, no master hold */
141  }
142  else {
143  /* checksum error, restart */
146  }
147  break;
148 
149  case SHT2_TRIG_HUMID:
152  break;
153 
154  case SHT2_READ_HUMID:
155  /* read humidity */
156  humidsht_i2c = (sht_trans.buf[0] << 8) | sht_trans.buf[1];
157  humidsht_i2c &= 0xFFFC;
158  fhumidsht_i2c = -6. + 125. / 65536. * humidsht_i2c;
159  ftempsht_i2c = -46.85 + 175.72 / 65536. * tempsht_i2c;
160 
163 
164  if (humid_sht_crc(sht_trans.buf) == 0) {
165  DOWNLINK_SEND_SHT_I2C_STATUS(DefaultChannel, DefaultDevice, &humidsht_i2c, &tempsht_i2c, &fhumidsht_i2c, &ftempsht_i2c);
166  }
167  break;
168 
169  case SHT2_RESET:
172  break;
173 
174  case SHT2_SERIAL1:
175  /* read serial number part 1 */
176  sht_serial[5] = sht_trans.buf[0];
177  sht_serial[4] = sht_trans.buf[2];
178  sht_serial[3] = sht_trans.buf[4];
179  sht_serial[2] = sht_trans.buf[6];
180  /* get serial number part 2 */
182  sht_trans.buf[0] = 0xFC;
183  sht_trans.buf[1] = 0xC9;
185  break;
186 
187  case SHT2_SERIAL2:
188  /* read serial number part 2 */
189  sht_serial[1] = sht_trans.buf[0];
190  sht_serial[0] = sht_trans.buf[1];
191  sht_serial[7] = sht_trans.buf[3];
192  sht_serial[6] = sht_trans.buf[4];
193  sht_serial1=sht_serial[7]<<24|sht_serial[6]<<16|sht_serial[5]<<8|sht_serial[4];
194  sht_serial2=sht_serial[3]<<24|sht_serial[2]<<16|sht_serial[1]<<8|sht_serial[0];
195  DOWNLINK_SEND_SHT_I2C_SERIAL(DefaultChannel, DefaultDevice, &sht_serial1, &sht_serial2);
198  break;
199 
200  default:
202  break;
203  }
204  }
205 }
206 
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)
#define SHT_SLAVE_ADDR
Definition: humid_sht_i2c.c:41
transaction successfully finished by I2C driver
Definition: i2c.h:57
I2C transaction structure.
Definition: i2c.h:93
bool_t 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:258
void humid_sht_p_humid(void)
#define SHT2_TRIGGER_HUMID
Definition: humid_sht_i2c.h:9
if(PrimarySpektrumState.SpektrumTimer)--PrimarySpektrumState.SpektrumTimer
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:68
transaction set to done by user level
Definition: i2c.h:59
bool_t 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:238
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
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
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
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:72
signed char int8_t
Definition: types.h:15
bool_t 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:248
Architecture independent I2C (Inter-Integrated Circuit Bus) API.