Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
baro_board.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010-2013 The Paparazzi Team
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 
23 #include "std.h"
24 #include "modules/sensors/baro.h"
25 #include "mcu_periph/i2c.h"
26 #include "modules/core/abi.h"
27 #include "led.h"
28 
41 };
42 
43 struct BaroBoard {
45  bool running;
46 };
47 
48 
49 struct BaroBoard baro_board;
51 
52 static inline void baro_board_send_reset(void);
53 static inline void baro_board_send_config_abs(void);
54 static inline void baro_board_send_config_diff(void);
55 static inline void baro_board_write_to_register(uint8_t baro_addr, uint8_t reg_addr, uint8_t val_msb, uint8_t val_lsb);
56 static inline void baro_board_read_from_register(uint8_t baro_addr, uint8_t reg_addr);
57 static inline void baro_board_set_current_register(uint8_t baro_addr, uint8_t reg_addr);
58 static inline void baro_board_read_from_current_register(uint8_t baro_addr);
59 
60 // absolute
61 #define BARO_ABS_ADDR 0x90
62 // differential
63 #define BARO_DIFF_ADDR 0x92
64 
65 // FIXME
66 #ifndef LISA_L_BARO_SENS
67 #define LISA_L_BARO_SENS 1.0
68 #endif
69 
70 #ifndef LISA_L_DIFF_SENS
71 #define LISA_L_DIFF_SENS 1.0
72 #endif
73 
74 void baro_init(void)
75 {
76 #ifdef BARO_LED
77  LED_OFF(BARO_LED);
78 #endif
80  baro_board.running = false;
81 }
82 
83 
84 void baro_periodic(void)
85 {
86  // check i2c_done
87  if (!i2c_idle(&i2c2)) { return; }
88 
89  switch (baro_board.status) {
90  case LBS_UNINITIALIZED:
93  break;
94  case LBS_RESETED:
97  break;
101  break;
105  break;
109  // baro_board.status = LBS_UNINITIALIZED;
110  break;
112  baro_board.running = true;
113  /* Falls through. */
114  case LBS_READ_DIFF:
117  break;
118  case LBS_READ_ABS:
121  break;
122  default:
123  break;
124  }
125 
126 #ifdef BARO_LED
127  if (baro_board.running == TRUE) {
128  LED_ON(BARO_LED);
129  } else {
130  LED_TOGGLE(BARO_LED);
131  }
132 #endif
133 }
134 
136 {
141  uint32_t now_ts = get_sys_time_usec();
142  int16_t tmp = baro_trans.buf[0] << 8 | baro_trans.buf[1];
143  float pressure = LISA_L_BARO_SENS * (float)tmp;
144  AbiSendMsgBARO_ABS(BARO_BOARD_SENDER_ID, now_ts, pressure);
145  }
146  } else if (baro_board.status == LBS_READING_DIFF &&
150  int16_t tmp = baro_trans.buf[0] << 8 | baro_trans.buf[1];
151  float diff = LISA_L_DIFF_SENS * (float)tmp;
152  AbiSendMsgBARO_DIFF(BARO_BOARD_SENDER_ID, diff);
153  }
154  }
155 }
156 
157 static inline void baro_board_send_config_abs(void)
158 {
159 #ifndef BARO_LOW_GAIN
160  INFO("Using High LisaL Baro Gain: Do not use below 1000hPa")
161  baro_board_write_to_register(BARO_ABS_ADDR, 0x01, 0x86, 0x83);
162 #else
163  INFO("Using Low LisaL Baro Gain, capable of measuring below 1000hPa or more")
164  //config register should be 0x84 in low countries, or 0x86 in normal countries
165  baro_board_write_to_register(BARO_ABS_ADDR, 0x01, 0x84, 0x83);
166 #endif
167 }
168 
169 static inline void baro_board_send_config_diff(void)
170 {
171  baro_board_write_to_register(BARO_DIFF_ADDR, 0x01, 0x84, 0x83);
172 }
173 
174 static inline void baro_board_send_reset(void)
175 {
177  baro_trans.slave_addr = 0x00;
178  baro_trans.len_w = 1;
179  baro_trans.buf[0] = 0x06;
180  i2c_submit(&i2c2, &baro_trans);
181 }
182 
183 static inline void baro_board_write_to_register(uint8_t baro_addr, uint8_t reg_addr, uint8_t val_msb, uint8_t val_lsb)
184 {
186  baro_trans.slave_addr = baro_addr;
187  baro_trans.len_w = 3;
188  baro_trans.buf[0] = reg_addr;
189  baro_trans.buf[1] = val_msb;
190  baro_trans.buf[2] = val_lsb;
191  i2c_submit(&i2c2, &baro_trans);
192 }
193 
194 static inline void baro_board_read_from_register(uint8_t baro_addr, uint8_t reg_addr)
195 {
197  baro_trans.slave_addr = baro_addr;
198  baro_trans.len_w = 1;
199  baro_trans.len_r = 2;
200  baro_trans.buf[0] = reg_addr;
201  i2c_submit(&i2c2, &baro_trans);
202  // i2c2.buf[0] = reg_addr;
203  // i2c2_transceive(baro_addr, 1, 2, &baro_board.i2c_done);
204 }
205 
206 static inline void baro_board_set_current_register(uint8_t baro_addr, uint8_t reg_addr)
207 {
209  baro_trans.slave_addr = baro_addr;
210  baro_trans.len_w = 1;
211  baro_trans.buf[0] = reg_addr;
212  i2c_submit(&i2c2, &baro_trans);
213  // i2c2.buf[0] = reg_addr;
214  // i2c2_transmit(baro_addr, 1, &baro_board.i2c_done);
215 }
216 
217 static inline void baro_board_read_from_current_register(uint8_t baro_addr)
218 {
220  baro_trans.slave_addr = baro_addr;
221  baro_trans.len_r = 2;
222  i2c_submit(&i2c2, &baro_trans);
223  // i2c2_receive(baro_addr, 2, &baro_board.i2c_done);
224 }
Main include for ABI (AirBorneInterface).
#define BARO_BOARD_SENDER_ID
default onboard baro
void baro_periodic(void)
Definition: baro_board.c:90
void baro_init(void)
Definition: baro_board.c:76
Common barometric sensor implementation.
#define LED_ON(i)
Definition: led_hw.h:51
#define LED_OFF(i)
Definition: led_hw.h:52
#define LED_TOGGLE(i)
Definition: led_hw.h:53
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:71
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
uint16_t len_r
Number of bytes to read/receive.
Definition: i2c.h:110
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
uint8_t len_w
Number of bytes to write/transmit.
Definition: i2c.h:116
static bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition: i2c.h:266
static bool i2c_idle(struct i2c_periph *p)
Check if I2C bus is idle.
Definition: i2c.h:256
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition: i2c.h:57
@ I2CTransPending
transaction is pending in queue
Definition: i2c.h:55
@ I2CTransRx
receive only transaction
Definition: i2c.h:48
@ I2CTransTx
transmit only transaction
Definition: i2c.h:47
@ I2CTransTxRx
transmit and receive transaction
Definition: i2c.h:49
I2C transaction structure.
Definition: i2c.h:93
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
arch independent LED (Light Emitting Diodes) API
static void baro_board_send_reset(void)
Definition: baro_board.c:174
#define LISA_L_DIFF_SENS
Definition: baro_board.c:71
static void baro_board_read_from_register(uint8_t baro_addr, uint8_t reg_addr)
Definition: baro_board.c:194
enum LisaBaroStatus status
Definition: baro_board.c:44
static void baro_board_send_config_diff(void)
Definition: baro_board.c:169
static void baro_board_send_config_abs(void)
Definition: baro_board.c:157
static void baro_board_write_to_register(uint8_t baro_addr, uint8_t reg_addr, uint8_t val_msb, uint8_t val_lsb)
Definition: baro_board.c:183
#define LISA_L_BARO_SENS
Definition: baro_board.c:67
#define BARO_DIFF_ADDR
Definition: baro_board.c:63
void lisa_l_baro_event(void)
Definition: baro_board.c:135
static void baro_board_read_from_current_register(uint8_t baro_addr)
Definition: baro_board.c:217
bool running
Definition: baro_board.c:45
struct BaroBoard baro_board
Definition: baro_board.c:49
#define BARO_ABS_ADDR
Definition: baro_board.c:61
struct i2c_transaction baro_trans
Definition: baro_board.c:50
static void baro_board_set_current_register(uint8_t baro_addr, uint8_t reg_addr)
Definition: baro_board.c:206
LisaBaroStatus
Definition: baro_board.c:29
@ LBS_READ_DIFF
Definition: baro_board.c:40
@ LBS_IDLE
Definition: baro_board.c:36
@ LBS_INITIALIZING_DIFF
Definition: baro_board.c:34
@ LBS_READING_ABS
Definition: baro_board.c:37
@ LBS_INITIALIZING_ABS_1
Definition: baro_board.c:33
@ LBS_INITIALIZING_DIFF_1
Definition: baro_board.c:35
@ LBS_RESETED
Definition: baro_board.c:31
@ LBS_READ_ABS
Definition: baro_board.c:38
@ LBS_UNINITIALIZED
Definition: baro_board.c:30
@ LBS_READING_DIFF
Definition: baro_board.c:39
@ LBS_INITIALIZING_ABS
Definition: baro_board.c:32
#define TRUE
Definition: std.h:4
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
short int16_t
Typedef defining 16 bit short type.
Definition: vl53l1_types.h:93
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98