Paparazzi UAS  v4.2.2_stable-4-gcc32f65
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
humid_sht_i2c.c
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Copyright (C) 2010 Martin Mueller
5  *
6  * This file is part of paparazzi.
7  *
8  * paparazzi is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * paparazzi is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with paparazzi; see the file COPYING. If not, write to
20  * the Free Software Foundation, 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *
23  */
24 
32 
33 #include "mcu_periph/i2c.h"
34 #include "mcu_periph/uart.h"
35 #include "messages.h"
37 
38 #ifndef DOWNLINK_DEVICE
39 #define DOWNLINK_DEVICE DOWNLINK_AP_DEVICE
40 #endif
41 
42 #ifndef SHT_I2C_DEV
43 #define SHT_I2C_DEV i2c0
44 #endif
45 
46 #define SHT_SLAVE_ADDR 0x80
47 
54 
55 int8_t humid_sht_crc(volatile uint8_t* data) {
56  uint8_t i, bit, crc = 0;
57 
58  for (i = 0; i < 2; i++) {
59  crc ^= (data[i]);
60  for (bit = 8; bit > 0; bit--) {
61  if (crc & 0x80)
62  crc = (crc << 1) ^ 0x131;
63  else
64  crc = (crc << 1);
65  }
66  }
67  if (crc != data[2])
68  return -1;
69  else
70  return 0;
71 }
72 
73 void humid_sht_init_i2c(void) {
75 }
76 
77 void humid_sht_periodic_i2c( void ) {
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 */
111 void humid_sht_p_temp( void ) {
112  if (sht_status == SHT2_GET_TEMP) {
113  /* get temp */
116  }
117 }
118 
119 /* needs 29ms delay from humid trigger measurement */
120 void humid_sht_p_humid( void ) {
121  if (sht_status == SHT2_GET_HUMID) {
122  /* read humid */
125  }
126 }
127 
128 void humid_sht_event_i2c( void ) {
130  switch (sht_status) {
131 
132  case SHT2_TRIG_TEMP:
135  break;
136 
137  case SHT2_READ_TEMP:
138  /* read temperature */
139  tempsht_i2c = (sht_trans.buf[0] << 8) | sht_trans.buf[1];
140  tempsht_i2c &= 0xFFFC;
141  if (humid_sht_crc(sht_trans.buf) == 0) {
142  /* trigger humid measurement, no master hold */
146  }
147  else {
148  /* checksum error, restart */
151  }
152  break;
153 
154  case SHT2_TRIG_HUMID:
157  break;
158 
159  case SHT2_READ_HUMID:
160  /* read humidity */
161  humidsht_i2c = (sht_trans.buf[0] << 8) | sht_trans.buf[1];
162  humidsht_i2c &= 0xFFFC;
163  fhumidsht_i2c = -6. + 125. / 65536. * humidsht_i2c;
164  ftempsht_i2c = -46.85 + 175.72 / 65536. * tempsht_i2c;
165 
168 
169  if (humid_sht_crc(sht_trans.buf) == 0) {
170  DOWNLINK_SEND_SHT_I2C_STATUS(DefaultChannel, DefaultDevice, &humidsht_i2c, &tempsht_i2c, &fhumidsht_i2c, &ftempsht_i2c);
171  }
172  break;
173 
174  case SHT2_RESET:
177  break;
178 
179  case SHT2_SERIAL1:
180  /* read serial number part 1 */
181  sht_serial[5] = sht_trans.buf[0];
182  sht_serial[4] = sht_trans.buf[2];
183  sht_serial[3] = sht_trans.buf[4];
184  sht_serial[2] = sht_trans.buf[6];
185  /* get serial number part 2 */
187  sht_trans.buf[0] = 0xFC;
188  sht_trans.buf[1] = 0xC9;
190  break;
191 
192  case SHT2_SERIAL2:
193  /* read serial number part 2 */
194  sht_serial[1] = sht_trans.buf[0];
195  sht_serial[0] = sht_trans.buf[1];
196  sht_serial[7] = sht_trans.buf[3];
197  sht_serial[6] = sht_trans.buf[4];
198  sht_serial1=sht_serial[7]<<24|sht_serial[6]<<16|sht_serial[5]<<8|sht_serial[4];
199  sht_serial2=sht_serial[3]<<24|sht_serial[2]<<16|sht_serial[1]<<8|sht_serial[0];
200  DOWNLINK_SEND_SHT_I2C_SERIAL(DefaultChannel, DefaultDevice, &sht_serial1, &sht_serial2);
203  break;
204 
205  default:
207  break;
208  }
209  }
210 }
211 
unsigned short uint16_t
Definition: types.h:16
#define SHT_I2C_DEV
Definition: humid_sht_i2c.c:43
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
#define I2CReceive(_p, _t, _s_addr, _len)
Definition: i2c.h:140
uint32_t sht_serial1
Definition: humid_sht_i2c.c:51
void humid_sht_event_i2c(void)
#define SHT_SLAVE_ADDR
Definition: humid_sht_i2c.c:46
#define I2CTransmit(_p, _t, _s_addr, _len)
Definition: i2c.h:148
void humid_sht_p_humid(void)
#define SHT2_TRIGGER_HUMID
Definition: humid_sht_i2c.h:9
float ftempsht_i2c
Definition: humid_sht_i2c.c:53
float fhumidsht_i2c
Definition: humid_sht_i2c.c:53
void humid_sht_init_i2c(void)
Definition: humid_sht_i2c.c:73
unsigned long uint32_t
Definition: types.h:18
uint16_t humidsht_i2c
Definition: humid_sht_i2c.c:52
int8_t humid_sht_crc(volatile uint8_t *data)
Definition: humid_sht_i2c.c:55
uint8_t sht_serial[8]
Definition: humid_sht_i2c.c:50
#define SHT2_TRIGGER_TEMP
Definition: humid_sht_i2c.h:8
enum I2CTransactionStatus status
Definition: i2c.h:47
volatile uint8_t buf[I2C_BUF_LEN]
Definition: i2c.h:46
uint32_t sht_serial2
Definition: humid_sht_i2c.c:51
unsigned char uint8_t
Definition: types.h:14
uint8_t sht_status
Definition: humid_sht_i2c.c:49
#define I2CTransceive(_p, _t, _s_addr, _len_w, _len_r)
Definition: i2c.h:156
struct i2c_transaction sht_trans
Definition: humid_sht_i2c.c:48
#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:52
void humid_sht_periodic_i2c(void)
Definition: humid_sht_i2c.c:77
signed char int8_t
Definition: types.h:15