Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces 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 
46 
47 #ifndef PCAP01_I2C_DEV
48 #define PCAP01_I2C_DEV i2c0
49 #endif
50 
52 {
54 
55  pcap01_trans.buf[0] = 0x90 + (unsigned char)(s_add >> 8);
56  pcap01_trans.buf[1] = (unsigned char)(s_add);
57  pcap01_trans.buf[2] = data;
59 }
60 
62 {
64 
65  pcap01_trans.buf[0] = 0x10 + (unsigned char)(s_add >> 8);
66  pcap01_trans.buf[1] = (unsigned char)(s_add);
69 
70  return pcap01_trans.buf[0];
71 }
85 void PCAP01_Control(uint8_t control)
86 {
88 
89  pcap01_trans.buf[0] = control;
90  pcap01_trans.buf[1] = 0;
91  pcap01_trans.buf[2] = 0;
92  pcap01_trans.buf[3] = 0;
94 }
95 
97 {
99 
100  pcap01_trans.buf[0] = PCAP01_WRITE_REG + reg;
101  pcap01_trans.buf[1] = (unsigned char)(value >> 16);
102  pcap01_trans.buf[2] = (unsigned char)(value >> 8);
103  pcap01_trans.buf[3] = (unsigned char)(value);
105 }
106 
107 #ifdef PCAP01_LOAD_FIRMWARE
108 void writePCAP01_firmware(void)
109 {
110  int i = 0;
111  char testbyte = 44;
112  uint16_t testaddress = 71;
113  // Hard reset
115  sys_time_usleep(50000);
116  //write testbyte
117  writePCAP01_SRAM(testbyte, testaddress);
118 
119  //check testbyte
120  if (readPCAP01_SRAM(testaddress) != testbyte) {
121  return;
122  } else {
123  LED_ON(3);
124  //Hard reset
126  //write firmware
127  for (i = 0; i < sizeof(firmware); i++) {
128  writePCAP01_SRAM(firmware[i], i);
129  }
130  //fill with ffs
131  for (; i < 4029; i++) {
132  writePCAP01_SRAM(0xff, i);
133  }
134  i++;
135 #ifdef PCAP01_200HZ
136  //write end bytes of sram
137  writePCAP01_SRAM(0x04, i++);
138  writePCAP01_SRAM(0x01, i++);
139  writePCAP01_SRAM(0x01, i++);
140 #endif
141 #ifdef PCAP01_STANDARD
142  //write end bytes of sram
143  writePCAP01_SRAM(0x02, i++);
144  writePCAP01_SRAM(0x01, i++);
145  writePCAP01_SRAM(0x03, i++);
146 #endif
147  }
148 }
149 #endif // PCAP01_LOAD_FIRMWARE
150 
151 void pcap01_init(void)
152 {
154  pcap01Value.status = PCAP01_IDLE;
155 #ifdef PCAP01_LOAD_FIRMWARE
157 #endif
180  sys_time_usleep(500000);
182 }
183 
185 {
186  uint16_t byte1 = 0x40 | reg;
187  pcap01_trans.buf[0] = byte1;
189 }
190 
197 void pcap01_periodic(void)
198 {
199  pcap01Value.status = PCAP01_GET_HUMID;
200 #ifdef PCAP01_STANDARD
202 #endif
203 #ifdef PCAP01_200HZ
205 #endif
206 }
207 
208 void pcap01_event(void)
209 {
210  float humidity;
211  float temperature;
212 
214  switch (pcap01Value.status) {
215 
216  case PCAP01_GET_HUMID:
217  pcap01Value.C_ratio = pcap01_trans.buf[0] << 16;
218  pcap01Value.C_ratio |= (pcap01_trans.buf[1] << 8);
219  pcap01Value.C_ratio |= pcap01_trans.buf[2];
220  pcap01Value.status = PCAP01_GET_TEMP;
221 #ifdef PCAP01_STANDARD
223 #endif
224 #ifdef PCAP01_200HZ
226 #endif
227  break;
228 
229  case PCAP01_GET_TEMP:
230  pcap01Value.R_ratio = pcap01_trans.buf[0] << 16;
231  pcap01Value.R_ratio |= (pcap01_trans.buf[1] << 8);
232  pcap01Value.R_ratio |= pcap01_trans.buf[2];
233  humidity = pcap01Value.C_ratio * (-0.0023959245437) + 516.4124438673063;
234  temperature = pcap01Value.R_ratio * 61.927 - 259.74;
235  DOWNLINK_SEND_PCAP01_STATUS(DefaultChannel, DefaultDevice,
236  &pcap01Value.C_ratio,
237  &pcap01Value.R_ratio,
238  &humidity,
239  &temperature);
241  pcap01Value.status = PCAP01_IDLE;
242  break;
243 
244  default:
246  break;
247  }
248  }
249 }
#define PCAP01_GET_TEMP
Definition: humid_pcap01.h:54
unsigned short uint16_t
Definition: types.h:16
uint32_t status
Definition: humid_pcap01.h:47
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:51
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
transaction successfully finished by I2C driver
Definition: i2c.h:57
#define PCAP01_REG15_VALUE
Definition: humid_pcap01.h:108
uint16_t value
Definition: adc_arch.c:586
#define PCAP01_REG11
Definition: humid_pcap01.h:80
void PCAP01_Control(uint8_t control)
PCAP01_Control.
Definition: humid_pcap01.c:85
#define PCAP01_REG13_VALUE
Definition: humid_pcap01.h:106
#define PCAP01_REG19
Definition: humid_pcap01.h:88
#define PCAP01_REG8
Definition: humid_pcap01.h:77
#define PCAP01_REG11_VALUE
Definition: humid_pcap01.h:104
void pcap01readRegister(uint8_t reg)
Definition: humid_pcap01.c:184
#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)
Submit a write/read transaction.
Definition: i2c.c:278
struct i2c_transaction pcap01_trans
Definition: humid_pcap01.c:44
void pcap01_event(void)
Definition: humid_pcap01.c:208
#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
transaction set to done by user level
Definition: i2c.h:59
Architecture independent timing functions.
PCAP01VALUE pcap01Value
Definition: humid_pcap01.c:45
#define PCAP01_I2C_DEV
Definition: humid_pcap01.c:48
uint32_t C_ratio
Definition: humid_pcap01.h:44
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:258
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:96
uint8_t pcap01_meas_started
Definition: humid_pcap01.c:43
#define PCAP01_REG8_VALUE
Definition: humid_pcap01.h:101
uint32_t R_ratio
Definition: humid_pcap01.h:43
#define PCAP01_REG19_VALUE
Definition: humid_pcap01.h:112
#define PCAP01_PU_RESET
Definition: humid_pcap01.h:57
#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
I2C transaction structure.
Definition: i2c.h:93
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
#define PCAP01_REG12_VALUE
Definition: humid_pcap01.h:105
#define PCAP01_REG10
Definition: humid_pcap01.h:79
#define LED_ON(i)
Definition: led_hw.h:42
static void sys_time_usleep(uint32_t us)
Definition: sys_time_arch.h:55
uint8_t readPCAP01_SRAM(uint16_t s_add)
Definition: humid_pcap01.c:61
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
#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:197
#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:151
#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
transaction is pending in queue
Definition: i2c.h:55
#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.