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
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"
25 #include "mcu_periph/i2c.h"
26 #include "subsystems/abi.h"
27 #include "led.h"
28 
41 };
42 
43 struct BaroBoard {
45  bool_t running;
46 };
47 
48 
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
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;
113  case LBS_READ_DIFF:
116  break;
117  case LBS_READ_ABS:
120  break;
121  default:
122  break;
123  }
124 
125 #ifdef BARO_LED
126  if (baro_board.running == TRUE) {
127  LED_ON(BARO_LED);
128  } else {
129  LED_TOGGLE(BARO_LED);
130  }
131 #endif
132 }
133 
135 {
140  int16_t tmp = baro_trans.buf[0] << 8 | baro_trans.buf[1];
141  float pressure = LISA_L_BARO_SENS * (float)tmp;
142  AbiSendMsgBARO_ABS(BARO_BOARD_SENDER_ID, pressure);
143  }
144  } else if (baro_board.status == LBS_READING_DIFF &&
148  int16_t tmp = baro_trans.buf[0] << 8 | baro_trans.buf[1];
149  float diff = LISA_L_DIFF_SENS * (float)tmp;
150  AbiSendMsgBARO_DIFF(BARO_BOARD_SENDER_ID, diff);
151  }
152  }
153 }
154 
155 static inline void baro_board_send_config_abs(void)
156 {
157 #ifndef BARO_LOW_GAIN
158  INFO("Using High LisaL Baro Gain: Do not use below 1000hPa")
159  baro_board_write_to_register(BARO_ABS_ADDR, 0x01, 0x86, 0x83);
160 #else
161  INFO("Using Low LisaL Baro Gain, capable of measuring below 1000hPa or more")
162  //config register should be 0x84 in low countries, or 0x86 in normal countries
163  baro_board_write_to_register(BARO_ABS_ADDR, 0x01, 0x84, 0x83);
164 #endif
165 }
166 
167 static inline void baro_board_send_config_diff(void)
168 {
169  baro_board_write_to_register(BARO_DIFF_ADDR, 0x01, 0x84, 0x83);
170 }
171 
172 static inline void baro_board_send_reset(void)
173 {
175  baro_trans.slave_addr = 0x00;
176  baro_trans.len_w = 1;
177  baro_trans.buf[0] = 0x06;
178  i2c_submit(&i2c2, &baro_trans);
179 }
180 
181 static inline void baro_board_write_to_register(uint8_t baro_addr, uint8_t reg_addr, uint8_t val_msb, uint8_t val_lsb)
182 {
184  baro_trans.slave_addr = baro_addr;
185  baro_trans.len_w = 3;
186  baro_trans.buf[0] = reg_addr;
187  baro_trans.buf[1] = val_msb;
188  baro_trans.buf[2] = val_lsb;
189  i2c_submit(&i2c2, &baro_trans);
190 }
191 
192 static inline void baro_board_read_from_register(uint8_t baro_addr, uint8_t reg_addr)
193 {
195  baro_trans.slave_addr = baro_addr;
196  baro_trans.len_w = 1;
197  baro_trans.len_r = 2;
198  baro_trans.buf[0] = reg_addr;
199  i2c_submit(&i2c2, &baro_trans);
200  // i2c2.buf[0] = reg_addr;
201  // i2c2_transceive(baro_addr, 1, 2, &baro_board.i2c_done);
202 }
203 
204 static inline void baro_board_set_current_register(uint8_t baro_addr, uint8_t reg_addr)
205 {
207  baro_trans.slave_addr = baro_addr;
208  baro_trans.len_w = 1;
209  baro_trans.buf[0] = reg_addr;
210  i2c_submit(&i2c2, &baro_trans);
211  // i2c2.buf[0] = reg_addr;
212  // i2c2_transmit(baro_addr, 1, &baro_board.i2c_done);
213 }
214 
215 static inline void baro_board_read_from_current_register(uint8_t baro_addr)
216 {
218  baro_trans.slave_addr = baro_addr;
219  baro_trans.len_r = 2;
220  i2c_submit(&i2c2, &baro_trans);
221  // i2c2_receive(baro_addr, 2, &baro_board.i2c_done);
222 }
struct BaroBoard baro_board
Definition: baro_board.c:63
static void baro_board_read_from_current_register(uint8_t baro_addr)
Definition: baro_board.c:215
bool_t i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition: i2c_arch.c:52
#define BARO_ABS_ADDR
Definition: baro_board.c:61
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
Common barometric sensor implementation.
uint16_t len_r
Number of bytes to read/receive.
Definition: i2c.h:110
transaction successfully finished by I2C driver
Definition: i2c.h:57
#define BARO_DIFF_ADDR
Definition: baro_board.c:63
transmit and receive transaction
Definition: i2c.h:49
Main include for ABI (AirBorneInterface).
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:181
void baro_init(void)
Definition: baro_board.c:68
LisaBaroStatus
Definition: baro_board.c:29
#define FALSE
Definition: std.h:5
void baro_periodic(void)
Definition: baro_board.c:77
#define LED_TOGGLE(i)
Definition: led_hw.h:44
void lisa_l_baro_event(void)
Definition: baro_board.c:134
uint8_t len_w
Number of bytes to write/transmit.
Definition: i2c.h:116
#define TRUE
Definition: std.h:4
static void baro_board_read_from_register(uint8_t baro_addr, uint8_t reg_addr)
Definition: baro_board.c:192
signed short int16_t
Definition: types.h:17
enum BaroBoardStatus status
Definition: baro_board.h:43
#define LISA_L_DIFF_SENS
Definition: baro_board.c:71
#define BARO_BOARD_SENDER_ID
default onboard baro
#define LISA_L_BARO_SENS
Definition: baro_board.c:67
I2C transaction structure.
Definition: i2c.h:93
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
#define LED_ON(i)
Definition: led_hw.h:42
unsigned char uint8_t
Definition: types.h:14
bool_t running
Definition: baro_board.c:45
static void baro_board_send_config_abs(void)
Definition: baro_board.c:155
struct i2c_transaction baro_trans
Definition: baro_board.c:50
transmit only transaction
Definition: i2c.h:47
arch independent LED (Light Emitting Diodes) API
static void baro_board_set_current_register(uint8_t baro_addr, uint8_t reg_addr)
Definition: baro_board.c:204
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
static void baro_board_send_config_diff(void)
Definition: baro_board.c:167
#define LED_OFF(i)
Definition: led_hw.h:43
receive only transaction
Definition: i2c.h:48
static void baro_board_send_reset(void)
Definition: baro_board.c:172
bool_t i2c_idle(struct i2c_periph *p)
Check if I2C bus is idle.
Definition: i2c_arch.c:45
transaction is pending in queue
Definition: i2c.h:55
Architecture independent I2C (Inter-Integrated Circuit Bus) API.