Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
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 #ifndef DOWNLINK_DEVICE
37 #define DOWNLINK_DEVICE DOWNLINK_AP_DEVICE
38 #endif
39 
40 #ifndef SHT_I2C_DEV
41 #define SHT_I2C_DEV i2c0
42 #endif
43 
44 #define SHT_SLAVE_ADDR 0x80
45 
52 
53 int8_t humid_sht_crc(volatile uint8_t* data) {
54  uint8_t i, bit, crc = 0;
55 
56  for (i = 0; i < 2; i++) {
57  crc ^= (data[i]);
58  for (bit = 8; bit > 0; bit--) {
59  if (crc & 0x80)
60  crc = (crc << 1) ^ 0x131;
61  else
62  crc = (crc << 1);
63  }
64  }
65  if (crc != data[2])
66  return -1;
67  else
68  return 0;
69 }
70 
71 void humid_sht_init_i2c(void) {
73 }
74 
75 void humid_sht_periodic_i2c( void ) {
76  switch (sht_status) {
77 
78  case SHT2_UNINIT:
79  /* do soft reset, then wait at least 15ms */
83  break;
84 
85  case SHT2_SERIAL:
86  /* get serial number part 1 */
88  sht_trans.buf[0] = 0xFA;
89  sht_trans.buf[1] = 0x0F;
91  break;
92 
93  case SHT2_SERIAL1:
94  case SHT2_SERIAL2:
95  break;
96 
97  default:
98  /* trigger temp measurement, no master hold */
102  /* send serial number every 30 seconds */
103  RunOnceEvery((4*30), DOWNLINK_SEND_SHT_I2C_SERIAL(DefaultChannel, DefaultDevice, &sht_serial1, &sht_serial2));
104  break;
105  }
106 }
107 
108 /* needs 85ms delay from temp trigger measurement */
109 void humid_sht_p_temp( void ) {
110  if (sht_status == SHT2_GET_TEMP) {
111  /* get temp */
114  }
115 }
116 
117 /* needs 29ms delay from humid trigger measurement */
118 void humid_sht_p_humid( void ) {
119  if (sht_status == SHT2_GET_HUMID) {
120  /* read humid */
123  }
124 }
125 
126 void humid_sht_event_i2c( void ) {
128  switch (sht_status) {
129 
130  case SHT2_TRIG_TEMP:
133  break;
134 
135  case SHT2_READ_TEMP:
136  /* read temperature */
137  tempsht_i2c = (sht_trans.buf[0] << 8) | sht_trans.buf[1];
138  tempsht_i2c &= 0xFFFC;
139  if (humid_sht_crc(sht_trans.buf) == 0) {
140  /* trigger humid measurement, no master hold */
144  }
145  else {
146  /* checksum error, restart */
149  }
150  break;
151 
152  case SHT2_TRIG_HUMID:
155  break;
156 
157  case SHT2_READ_HUMID:
158  /* read humidity */
159  humidsht_i2c = (sht_trans.buf[0] << 8) | sht_trans.buf[1];
160  humidsht_i2c &= 0xFFFC;
161  fhumidsht_i2c = -6. + 125. / 65536. * humidsht_i2c;
162  ftempsht_i2c = -46.85 + 175.72 / 65536. * tempsht_i2c;
163 
166 
167  if (humid_sht_crc(sht_trans.buf) == 0) {
168  DOWNLINK_SEND_SHT_I2C_STATUS(DefaultChannel, DefaultDevice, &humidsht_i2c, &tempsht_i2c, &fhumidsht_i2c, &ftempsht_i2c);
169  }
170  break;
171 
172  case SHT2_RESET:
175  break;
176 
177  case SHT2_SERIAL1:
178  /* read serial number part 1 */
179  sht_serial[5] = sht_trans.buf[0];
180  sht_serial[4] = sht_trans.buf[2];
181  sht_serial[3] = sht_trans.buf[4];
182  sht_serial[2] = sht_trans.buf[6];
183  /* get serial number part 2 */
185  sht_trans.buf[0] = 0xFC;
186  sht_trans.buf[1] = 0xC9;
188  break;
189 
190  case SHT2_SERIAL2:
191  /* read serial number part 2 */
192  sht_serial[1] = sht_trans.buf[0];
193  sht_serial[0] = sht_trans.buf[1];
194  sht_serial[7] = sht_trans.buf[3];
195  sht_serial[6] = sht_trans.buf[4];
196  sht_serial1=sht_serial[7]<<24|sht_serial[6]<<16|sht_serial[5]<<8|sht_serial[4];
197  sht_serial2=sht_serial[3]<<24|sht_serial[2]<<16|sht_serial[1]<<8|sht_serial[0];
198  DOWNLINK_SEND_SHT_I2C_SERIAL(DefaultChannel, DefaultDevice, &sht_serial1, &sht_serial2);
201  break;
202 
203  default:
205  break;
206  }
207  }
208 }
209 
unsigned short uint16_t
Definition: types.h:16
#define SHT_I2C_DEV
Definition: humid_sht_i2c.c:41
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
uint32_t sht_serial1
Definition: humid_sht_i2c.c:49
void humid_sht_event_i2c(void)
#define SHT_SLAVE_ADDR
Definition: humid_sht_i2c.c:44
bool_t i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r)
Definition: i2c.c:105
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:51
float fhumidsht_i2c
Definition: humid_sht_i2c.c:51
void humid_sht_init_i2c(void)
Definition: humid_sht_i2c.c:71
bool_t i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Definition: i2c.c:85
unsigned long uint32_t
Definition: types.h:18
uint16_t humidsht_i2c
Definition: humid_sht_i2c.c:50
int8_t humid_sht_crc(volatile uint8_t *data)
Definition: humid_sht_i2c.c:53
uint8_t sht_serial[8]
Definition: humid_sht_i2c.c:48
#define SHT2_TRIGGER_TEMP
Definition: humid_sht_i2c.h:8
enum I2CTransactionStatus status
Definition: i2c.h:83
volatile uint8_t buf[I2C_BUF_LEN]
Definition: i2c.h:82
uint32_t sht_serial2
Definition: humid_sht_i2c.c:49
unsigned char uint8_t
Definition: types.h:14
uint8_t sht_status
Definition: humid_sht_i2c.c:47
struct i2c_transaction sht_trans
Definition: humid_sht_i2c.c:46
#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:50
void humid_sht_periodic_i2c(void)
Definition: humid_sht_i2c.c:75
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)
Definition: i2c.c:95
Architecture independent I2C (Inter-Integrated Circuit Bus) API.