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_pcap01.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 Norman Wildmann, 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 
31 #include "led.h"
32 #include "mcu_periph/sys_time.h"
33 #include "mcu_periph/i2c.h"
34 #include "mcu_periph/uart.h"
35 #include "messages.h"
38 #ifdef PCAP01_LOAD_FIRMWARE
39 #include "modules/meteo/humid_pcap01_firmware.h"
40 #endif
41 
42 #ifndef DOWNLINK_DEVICE
43 #define DOWNLINK_DEVICE DOWNLINK_AP_DEVICE
44 #endif
45 
49 
50 #ifndef PCAP01_I2C_DEV
51 #define PCAP01_I2C_DEV i2c0
52 #endif
53 
55 {
57 
58  pcap01_trans.buf[0] = 0x90+(unsigned char)(s_add>>8);
59  pcap01_trans.buf[1] = (unsigned char)(s_add);
60  pcap01_trans.buf[2] = data;
62 }
63 
65 {
67 
68  pcap01_trans.buf[0] = 0x10+(unsigned char)(s_add>>8);
69  pcap01_trans.buf[1] = (unsigned char)(s_add);
72 
73  return pcap01_trans.buf[0];
74 }
88  void PCAP01_Control(uint8_t control)
89 {
91 
92  pcap01_trans.buf[0] = control;
93  pcap01_trans.buf[1] = 0;
94  pcap01_trans.buf[2] = 0;
95  pcap01_trans.buf[3] = 0;
97 }
98 
100  {
102 
103  pcap01_trans.buf[0] = PCAP01_WRITE_REG + reg;
104  pcap01_trans.buf[1] = (unsigned char) (value>>16);
105  pcap01_trans.buf[2] = (unsigned char) (value>>8);
106  pcap01_trans.buf[3] = (unsigned char) (value);
108  }
109 
110 #ifdef PCAP01_LOAD_FIRMWARE
111 void writePCAP01_firmware(void)
112 {
113  int i = 0;
114  char testbyte = 44;
115  uint16_t testaddress = 71;
116  // Hard reset
118  sys_time_usleep(50000);
119  //write testbyte
120  writePCAP01_SRAM(testbyte, testaddress);
121 
122  //check testbyte
123  if (readPCAP01_SRAM(testaddress) != testbyte)
124  return;
125  else
126  {
127 LED_ON(3);
128  //Hard reset
130  //write firmware
131  for (i = 0;i< sizeof(firmware); i++)
132  {
133  writePCAP01_SRAM(firmware[i],i);
134  }
135  //fill with ffs
136  for (;i< 4029; i++)
137  {
138  writePCAP01_SRAM(0xff,i);
139  }
140  i++;
141 #ifdef PCAP01_200HZ
142  //write end bytes of sram
143  writePCAP01_SRAM(0x04,i++);
144  writePCAP01_SRAM(0x01,i++);
145  writePCAP01_SRAM(0x01,i++);
146 #endif
147 #ifdef PCAP01_STANDARD
148  //write end bytes of sram
149  writePCAP01_SRAM(0x02,i++);
150  writePCAP01_SRAM(0x01,i++);
151  writePCAP01_SRAM(0x03,i++);
152 #endif
153  }
154 }
155 #endif // PCAP01_LOAD_FIRMWARE
156 
157 void pcap01_init(void) {
159  pcap01Value.status = PCAP01_IDLE;
160 #ifdef PCAP01_LOAD_FIRMWARE
162 #endif
185  sys_time_usleep(500000);
187 }
188 
190  {
191  uint16_t byte1 = 0x40 | reg;
192  pcap01_trans.buf[0] = byte1;
194  }
195 
202 void pcap01_periodic(void)
203 {
204  pcap01Value.status = PCAP01_GET_HUMID;
205 #ifdef PCAP01_STANDARD
207 #endif
208 #ifdef PCAP01_200HZ
210 #endif
211 }
212 
213 void pcap01_event(void)
214 {
215  float humidity;
216  float temperature;
217 
219  switch (pcap01Value.status) {
220 
221  case PCAP01_GET_HUMID:
222  pcap01Value.C_ratio = pcap01_trans.buf[0] << 16;
223  pcap01Value.C_ratio |= (pcap01_trans.buf[1] << 8);
224  pcap01Value.C_ratio |= pcap01_trans.buf[2];
225  pcap01Value.status = PCAP01_GET_TEMP;
226 #ifdef PCAP01_STANDARD
228 #endif
229 #ifdef PCAP01_200HZ
231 #endif
232  break;
233 
234  case PCAP01_GET_TEMP:
235  pcap01Value.R_ratio = pcap01_trans.buf[0] << 16;
236  pcap01Value.R_ratio |= (pcap01_trans.buf[1] << 8);
237  pcap01Value.R_ratio |= pcap01_trans.buf[2];
238  humidity = pcap01Value.C_ratio * (-0.0023959245437) + 516.4124438673063;
239  temperature = pcap01Value.R_ratio * 61.927 - 259.74;
240  DOWNLINK_SEND_PCAP01_STATUS(DefaultChannel, DefaultDevice,
241  &pcap01Value.C_ratio,
242  &pcap01Value.R_ratio,
243  &humidity,
244  &temperature);
246  pcap01Value.status = PCAP01_IDLE;
247  break;
248 
249  default:
251  break;
252  }
253  }
254 }
#define PCAP01_GET_TEMP
Definition: humid_pcap01.h:54
unsigned short uint16_t
Definition: types.h:16
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
#define PCAP01_REG0_VALUE
Definition: humid_pcap01.h:93
void writePCAP01_SRAM(uint8_t data, uint16_t s_add)
Definition: humid_pcap01.c:54
#define PCAP01_REG15_VALUE
Definition: humid_pcap01.h:108
uint16_t value
ADC1+2 interrupt hander.
Definition: adc_arch.c:577
#define PCAP01_REG11
Definition: humid_pcap01.h:80
void PCAP01_Control(uint8_t control)
PCAP01_Control.
Definition: humid_pcap01.c:88
#define PCAP01_REG13_VALUE
Definition: humid_pcap01.h:106
#define PCAP01_REG19
Definition: humid_pcap01.h:88
static void sys_time_usleep(uint32_t us)
Busy wait, in microseconds.
Definition: sys_time_arch.h:73
#define PCAP01_REG8
Definition: humid_pcap01.h:77
#define PCAP01_REG11_VALUE
Definition: humid_pcap01.h:104
#define LED_ON(i)
Definition: led_hw.h:28
void pcap01readRegister(uint8_t reg)
Definition: humid_pcap01.c:189
#define PCAP01_START
Definition: humid_pcap01.h:59
#define PCAP01_REG9_VALUE
Definition: humid_pcap01.h:102
#define PCAP01_REG4
Definition: humid_pcap01.h:73
#define PCAP01_REG17_VALUE
Definition: humid_pcap01.h:110
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
struct i2c_transaction pcap01_trans
Definition: humid_pcap01.c:47
void pcap01_event(void)
Definition: humid_pcap01.c:213
#define PCAP01_REG6
Definition: humid_pcap01.h:75
#define PCAP01_REG7_VALUE
Definition: humid_pcap01.h:100
#define PCAP01_REG5_VALUE
Definition: humid_pcap01.h:98
#define PCAP01_IN_RESET
Definition: humid_pcap01.h:58
#define PCAP01_REG7
Definition: humid_pcap01.h:76
#define PCAP01_REG1
Definition: humid_pcap01.h:70
ACAM Picocap Single-chip Solution for Capacitance Measurement.
#define PCAP01_REG18_VALUE
Definition: humid_pcap01.h:111
Architecture independent timing functions.
PCAP01VALUE pcap01Value
Definition: humid_pcap01.c:48
#define PCAP01_I2C_DEV
Definition: humid_pcap01.c:51
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
void writePCAP01_firmware(void)
#define PCAP01_REG5
Definition: humid_pcap01.h:74
void pcap01writeRegister(uint8_t reg, uint32_t value)
Definition: humid_pcap01.c:99
uint8_t pcap01_meas_started
Definition: humid_pcap01.c:46
#define PCAP01_REG8_VALUE
Definition: humid_pcap01.h:101
#define PCAP01_REG19_VALUE
Definition: humid_pcap01.h:112
#define PCAP01_PU_RESET
Definition: humid_pcap01.h:57
uint32_t C_ratio
Definition: humid_pcap01.h:44
enum I2CTransactionStatus status
Definition: i2c.h:83
#define PCAP01_ADDR
Definition: humid_pcap01.h:50
#define PCAP01_REG3_VALUE
Definition: humid_pcap01.h:96
#define PCAP01_REG18
Definition: humid_pcap01.h:87
#define PCAP01_REG16_VALUE
Definition: humid_pcap01.h:109
#define PCAP01_REG12_VALUE
Definition: humid_pcap01.h:105
uint32_t status
Definition: humid_pcap01.h:47
volatile uint8_t buf[I2C_BUF_LEN]
Definition: i2c.h:82
#define PCAP01_REG10
Definition: humid_pcap01.h:79
uint8_t readPCAP01_SRAM(uint16_t s_add)
Definition: humid_pcap01.c:64
unsigned char uint8_t
Definition: types.h:14
#define PCAP01_REG15
Definition: humid_pcap01.h:84
#define PCAP01_GET_HUMID
Definition: humid_pcap01.h:53
#define PCAP01_WRITE_REG
Definition: humid_pcap01.h:62
#define PCAP01_REG2_VALUE
Definition: humid_pcap01.h:95
uint32_t R_ratio
Definition: humid_pcap01.h:43
#define PCAP01_REG14
Definition: humid_pcap01.h:83
#define PCAP01_REG0
Definition: humid_pcap01.h:69
#define PCAP01_REG12
Definition: humid_pcap01.h:81
arch independent LED (Light Emitting Diodes) API
#define PCAP01_REG16
Definition: humid_pcap01.h:85
void pcap01_periodic(void)
pcap01_readData
Definition: humid_pcap01.c:202
#define PCAP01_REG13
Definition: humid_pcap01.h:82
#define PCAP01_REG20
Definition: humid_pcap01.h:89
#define PCAP01_REG6_VALUE
Definition: humid_pcap01.h:99
#define PCAP01_REG20_VALUE
Definition: humid_pcap01.h:113
void pcap01_init(void)
Definition: humid_pcap01.c:157
#define PCAP01_REG10_VALUE
Definition: humid_pcap01.h:103
#define PCAP01_REG14_VALUE
Definition: humid_pcap01.h:107
#define PCAP01_REG4_VALUE
Definition: humid_pcap01.h:97
#define PCAP01_REG17
Definition: humid_pcap01.h:86
#define PCAP01_REG3
Definition: humid_pcap01.h:72
#define PCAP01_REG2
Definition: humid_pcap01.h:71
#define PCAP01_IDLE
Definition: humid_pcap01.h:52
#define PCAP01_REG9
Definition: humid_pcap01.h:78
#define PCAP01_REG1_VALUE
Definition: humid_pcap01.h:94
Architecture independent I2C (Inter-Integrated Circuit Bus) API.