Paparazzi UAS  v4.0.4_stable-3-gf39211a
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
humid_pcap01.c
Go to the documentation of this file.
1 /*
2  * $Id: humid_pcap01.c $
3  *
4  * Copyright (C) 2011 Norman Wildmann, 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 #include "led.h"
33 #include "mcu_periph/sys_time.h"
34 #include "mcu_periph/i2c.h"
35 #include "mcu_periph/uart.h"
36 #include "messages.h"
39 #ifdef PCAP01_LOAD_FIRMWARE
40 #include "modules/meteo/humid_pcap01_firmware.h"
41 #endif
42 
43 #ifndef DOWNLINK_DEVICE
44 #define DOWNLINK_DEVICE DOWNLINK_AP_DEVICE
45 #endif
46 
50 
51 #ifndef PCAP01_I2C_DEV
52 #define PCAP01_I2C_DEV i2c0
53 #endif
54 
56 {
58 
59  pcap01_trans.buf[0] = 0x90+(unsigned char)(s_add>>8);
60  pcap01_trans.buf[1] = (unsigned char)(s_add);
61  pcap01_trans.buf[2] = data;
63 }
64 
66 {
68 
69  pcap01_trans.buf[0] = 0x10+(unsigned char)(s_add>>8);
70  pcap01_trans.buf[1] = (unsigned char)(s_add);
73 
74  return pcap01_trans.buf[0];
75 }
89  void PCAP01_Control(uint8_t control)
90 {
92 
93  pcap01_trans.buf[0] = control;
94  pcap01_trans.buf[1] = 0;
95  pcap01_trans.buf[2] = 0;
96  pcap01_trans.buf[3] = 0;
98 }
99 
101  {
103 
104  pcap01_trans.buf[0] = PCAP01_WRITE_REG + reg;
105  pcap01_trans.buf[1] = (unsigned char) (value>>16);
106  pcap01_trans.buf[2] = (unsigned char) (value>>8);
107  pcap01_trans.buf[3] = (unsigned char) (value);
109  }
110 
111 #ifdef PCAP01_LOAD_FIRMWARE
112 void writePCAP01_firmware(void)
113 {
114  int i = 0;
115  char testbyte = 44;
116  uint16_t testaddress = 71;
117  // Hard reset
119  sys_time_usleep(50000);
120  //write testbyte
121  writePCAP01_SRAM(testbyte, testaddress);
122 
123  //check testbyte
124  if (readPCAP01_SRAM(testaddress) != testbyte)
125  return;
126  else
127  {
128 LED_ON(3);
129  //Hard reset
131  //write firmware
132  for (i = 0;i< sizeof(firmware); i++)
133  {
134  writePCAP01_SRAM(firmware[i],i);
135  }
136  //fill with ffs
137  for (;i< 4029; i++)
138  {
139  writePCAP01_SRAM(0xff,i);
140  }
141  i++;
142 #ifdef PCAP01_200HZ
143  //write end bytes of sram
144  writePCAP01_SRAM(0x04,i++);
145  writePCAP01_SRAM(0x01,i++);
146  writePCAP01_SRAM(0x01,i++);
147 #endif
148 #ifdef PCAP01_STANDARD
149  //write end bytes of sram
150  writePCAP01_SRAM(0x02,i++);
151  writePCAP01_SRAM(0x01,i++);
152  writePCAP01_SRAM(0x03,i++);
153 #endif
154  }
155 }
156 #endif // PCAP01_LOAD_FIRMWARE
157 
158 void pcap01_init(void) {
160  pcap01Value.status = PCAP01_IDLE;
161 #ifdef PCAP01_LOAD_FIRMWARE
163 #endif
186  sys_time_usleep(500000);
188 }
189 
191  {
192  uint16_t byte1 = 0x40 | reg;
193  pcap01_trans.buf[0] = byte1;
195  }
196 
211 void pcap01_periodic(void)
212 {
213  pcap01Value.status = PCAP01_GET_HUMID;
214 #ifdef PCAP01_STANDARD
216 #endif
217 #ifdef PCAP01_200HZ
219 #endif
220 }
221 
222 void pcap01_event(void)
223 {
224  float humidity;
225  float temperature;
226 
228  switch (pcap01Value.status) {
229 
230  case PCAP01_GET_HUMID:
231  pcap01Value.C_ratio = pcap01_trans.buf[0] << 16;
232  pcap01Value.C_ratio |= (pcap01_trans.buf[1] << 8);
233  pcap01Value.C_ratio |= pcap01_trans.buf[2];
234  pcap01Value.status = PCAP01_GET_TEMP;
235 #ifdef PCAP01_STANDARD
237 #endif
238 #ifdef PCAP01_200HZ
240 #endif
241  break;
242 
243  case PCAP01_GET_TEMP:
244  pcap01Value.R_ratio = pcap01_trans.buf[0] << 16;
245  pcap01Value.R_ratio |= (pcap01_trans.buf[1] << 8);
246  pcap01Value.R_ratio |= pcap01_trans.buf[2];
247  humidity = pcap01Value.C_ratio * (-0.0023959245437) + 516.4124438673063;
248  temperature = pcap01Value.R_ratio * 61.927 - 259.74;
249  DOWNLINK_SEND_PCAP01_STATUS(DefaultChannel, DefaultDevice,
250  &pcap01Value.C_ratio,
251  &pcap01Value.R_ratio,
252  &humidity,
253  &temperature);
255  pcap01Value.status = PCAP01_IDLE;
256  break;
257 
258  default:
260  break;
261  }
262  }
263 }
#define PCAP01_GET_TEMP
Definition: humid_pcap01.h:24
unsigned short uint16_t
Definition: types.h:16
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
#define PCAP01_REG0_VALUE
Definition: humid_pcap01.h:63
void writePCAP01_SRAM(uint8_t data, uint16_t s_add)
Definition: humid_pcap01.c:55
#define PCAP01_REG15_VALUE
Definition: humid_pcap01.h:78
#define PCAP01_REG11
Definition: humid_pcap01.h:50
void PCAP01_Control(uint8_t control)
PCAP01_Control.
Definition: humid_pcap01.c:89
#define I2CTransmit(_p, _t, _s_addr, _len)
Definition: i2c.h:148
#define PCAP01_REG13_VALUE
Definition: humid_pcap01.h:76
#define PCAP01_REG19
Definition: humid_pcap01.h:58
static void sys_time_usleep(uint32_t us)
Busy wait, in microseconds.
Definition: sys_time_arch.h:79
#define PCAP01_REG8
Definition: humid_pcap01.h:47
#define PCAP01_REG11_VALUE
Definition: humid_pcap01.h:74
#define LED_ON(i)
Definition: led_hw.h:28
void pcap01readRegister(uint8_t reg)
Definition: humid_pcap01.c:190
#define PCAP01_START
Definition: humid_pcap01.h:29
#define PCAP01_REG9_VALUE
Definition: humid_pcap01.h:72
#define PCAP01_REG4
Definition: humid_pcap01.h:43
#define PCAP01_REG17_VALUE
Definition: humid_pcap01.h:80
struct i2c_transaction pcap01_trans
Definition: humid_pcap01.c:48
void pcap01_event(void)
Definition: humid_pcap01.c:222
#define PCAP01_REG6
Definition: humid_pcap01.h:45
#define PCAP01_REG7_VALUE
Definition: humid_pcap01.h:70
#define PCAP01_REG5_VALUE
Definition: humid_pcap01.h:68
#define PCAP01_IN_RESET
Definition: humid_pcap01.h:28
#define PCAP01_REG7
Definition: humid_pcap01.h:46
#define PCAP01_REG1
Definition: humid_pcap01.h:40
#define PCAP01_REG18_VALUE
Definition: humid_pcap01.h:81
Architecture independent timing functions.
PCAP01VALUE pcap01Value
Definition: humid_pcap01.c:49
#define PCAP01_I2C_DEV
Definition: humid_pcap01.c:52
unsigned long uint32_t
Definition: types.h:18
void writePCAP01_firmware(void)
#define PCAP01_REG5
Definition: humid_pcap01.h:44
void pcap01writeRegister(uint8_t reg, uint32_t value)
Definition: humid_pcap01.c:100
uint8_t pcap01_meas_started
Definition: humid_pcap01.c:47
#define PCAP01_REG8_VALUE
Definition: humid_pcap01.h:71
#define PCAP01_REG19_VALUE
Definition: humid_pcap01.h:82
#define PCAP01_PU_RESET
Definition: humid_pcap01.h:27
uint32_t C_ratio
Definition: humid_pcap01.h:14
enum I2CTransactionStatus status
Definition: i2c.h:47
#define PCAP01_ADDR
Definition: humid_pcap01.h:20
#define PCAP01_REG3_VALUE
Definition: humid_pcap01.h:66
#define PCAP01_REG18
Definition: humid_pcap01.h:57
#define PCAP01_REG16_VALUE
Definition: humid_pcap01.h:79
#define PCAP01_REG12_VALUE
Definition: humid_pcap01.h:75
uint32_t status
Definition: humid_pcap01.h:17
volatile uint8_t buf[I2C_BUF_LEN]
Definition: i2c.h:46
#define PCAP01_REG10
Definition: humid_pcap01.h:49
uint8_t readPCAP01_SRAM(uint16_t s_add)
Definition: humid_pcap01.c:65
unsigned char uint8_t
Definition: types.h:14
#define I2CTransceive(_p, _t, _s_addr, _len_w, _len_r)
Definition: i2c.h:156
#define PCAP01_REG15
Definition: humid_pcap01.h:54
#define PCAP01_GET_HUMID
Definition: humid_pcap01.h:23
#define PCAP01_WRITE_REG
Definition: humid_pcap01.h:32
#define PCAP01_REG2_VALUE
Definition: humid_pcap01.h:65
uint32_t R_ratio
Definition: humid_pcap01.h:13
#define PCAP01_REG14
Definition: humid_pcap01.h:53
#define PCAP01_REG0
Definition: humid_pcap01.h:39
#define PCAP01_REG12
Definition: humid_pcap01.h:51
arch independent LED (Light Emitting Diodes) API
#define PCAP01_REG16
Definition: humid_pcap01.h:55
void pcap01_periodic(void)
pcap01_readData
Definition: humid_pcap01.c:211
#define PCAP01_REG13
Definition: humid_pcap01.h:52
#define PCAP01_REG20
Definition: humid_pcap01.h:59
#define PCAP01_REG6_VALUE
Definition: humid_pcap01.h:69
#define PCAP01_REG20_VALUE
Definition: humid_pcap01.h:83
void pcap01_init(void)
Definition: humid_pcap01.c:158
#define PCAP01_REG10_VALUE
Definition: humid_pcap01.h:73
#define PCAP01_REG14_VALUE
Definition: humid_pcap01.h:77
#define PCAP01_REG4_VALUE
Definition: humid_pcap01.h:67
#define PCAP01_REG17
Definition: humid_pcap01.h:56
#define PCAP01_REG3
Definition: humid_pcap01.h:42
#define PCAP01_REG2
Definition: humid_pcap01.h:41
#define PCAP01_IDLE
Definition: humid_pcap01.h:22
#define PCAP01_REG9
Definition: humid_pcap01.h:48
#define PCAP01_REG1_VALUE
Definition: humid_pcap01.h:64