Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures 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 #ifdef BARO_LED
76  LED_OFF(BARO_LED);
77 #endif
80 }
81 
82 
83 void baro_periodic(void) {
84  // check i2c_done
85  if (!i2c_idle(&i2c2)) return;
86 
87  switch (baro_board.status) {
88  case LBS_UNINITIALIZED:
91  break;
92  case LBS_RESETED:
95  break;
99  break;
103  break;
107  // baro_board.status = LBS_UNINITIALIZED;
108  break;
111  case LBS_READ_DIFF:
114  break;
115  case LBS_READ_ABS:
118  break;
119  default:
120  break;
121  }
122 
123 #ifdef BARO_LED
124  if (baro_board.running == TRUE) {
125  LED_ON(BARO_LED);
126  }
127  else {
128  LED_TOGGLE(BARO_LED);
129  }
130 #endif
131 }
132 
133 void lisa_l_baro_event(void) {
138  int16_t tmp = baro_trans.buf[0]<<8 | baro_trans.buf[1];
139  float pressure = LISA_L_BARO_SENS*(float)tmp;
140  AbiSendMsgBARO_ABS(BARO_BOARD_SENDER_ID, &pressure);
141  }
142  }
143  else if (baro_board.status == LBS_READING_DIFF &&
147  int16_t tmp = baro_trans.buf[0]<<8 | baro_trans.buf[1];
148  float diff = LISA_L_DIFF_SENS*(float)tmp;
149  AbiSendMsgBARO_DIFF(BARO_BOARD_SENDER_ID, &diff);
150  }
151  }
152 }
153 
154 static inline void baro_board_send_config_abs(void) {
155 #ifndef BARO_LOW_GAIN
156 INFO("Using High LisaL Baro Gain: Do not use below 1000hPa")
157  baro_board_write_to_register(BARO_ABS_ADDR, 0x01, 0x86, 0x83);
158 #else
159 INFO("Using Low LisaL Baro Gain, capable of measuring below 1000hPa or more")
160  //config register should be 0x84 in low countries, or 0x86 in normal countries
161  baro_board_write_to_register(BARO_ABS_ADDR, 0x01, 0x84, 0x83);
162 #endif
163 }
164 
165 static inline void baro_board_send_config_diff(void) {
166  baro_board_write_to_register(BARO_DIFF_ADDR, 0x01, 0x84, 0x83);
167 }
168 
169 static inline void baro_board_send_reset(void) {
171  baro_trans.slave_addr = 0x00;
172  baro_trans.len_w = 1;
173  baro_trans.buf[0] = 0x06;
174  i2c_submit(&i2c2,&baro_trans);
175 }
176 
177 static inline void baro_board_write_to_register(uint8_t baro_addr, uint8_t reg_addr, uint8_t val_msb, uint8_t val_lsb) {
179  baro_trans.slave_addr = baro_addr;
180  baro_trans.len_w = 3;
181  baro_trans.buf[0] = reg_addr;
182  baro_trans.buf[1] = val_msb;
183  baro_trans.buf[2] = val_lsb;
184  i2c_submit(&i2c2,&baro_trans);
185 }
186 
187 static inline void baro_board_read_from_register(uint8_t baro_addr, uint8_t reg_addr) {
189  baro_trans.slave_addr = baro_addr;
190  baro_trans.len_w = 1;
191  baro_trans.len_r = 2;
192  baro_trans.buf[0] = reg_addr;
193  i2c_submit(&i2c2,&baro_trans);
194  // i2c2.buf[0] = reg_addr;
195  // i2c2_transceive(baro_addr, 1, 2, &baro_board.i2c_done);
196 }
197 
198 static inline void baro_board_set_current_register(uint8_t baro_addr, uint8_t reg_addr) {
200  baro_trans.slave_addr = baro_addr;
201  baro_trans.len_w = 1;
202  baro_trans.buf[0] = reg_addr;
203  i2c_submit(&i2c2,&baro_trans);
204  // i2c2.buf[0] = reg_addr;
205  // i2c2_transmit(baro_addr, 1, &baro_board.i2c_done);
206 }
207 
208 static inline void baro_board_read_from_current_register(uint8_t baro_addr) {
210  baro_trans.slave_addr = baro_addr;
211  baro_trans.len_r = 2;
212  i2c_submit(&i2c2,&baro_trans);
213  // i2c2_receive(baro_addr, 2, &baro_board.i2c_done);
214 }
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:208
bool_t i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition: i2c_arch.c:335
#define BARO_ABS_ADDR
Definition: baro_board.c:61
Common barometric sensor implementation.
transaction successfully finished by I2C driver
Definition: i2c.h:57
I2C transaction structure.
Definition: i2c.h:93
#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:177
#define LED_ON(i)
Definition: led_hw.h:28
void baro_init(void)
Definition: baro_board.c:68
LisaBaroStatus
Definition: baro_board.c:29
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
#define FALSE
Definition: imu_chimu.h:141
void baro_periodic(void)
Definition: baro_board.c:76
bool_t running
Definition: baro_board.c:45
void lisa_l_baro_event(void)
Definition: baro_board.c:133
static void baro_board_read_from_register(uint8_t baro_addr, uint8_t reg_addr)
Definition: baro_board.c:187
signed short int16_t
Definition: types.h:17
#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
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
#define LED_TOGGLE(i)
Definition: led_hw.h:30
#define TRUE
Definition: imu_chimu.h:144
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
uint8_t len_w
Number of bytes to write/transmit.
Definition: i2c.h:116
uint16_t len_r
Number of bytes to read/receive.
Definition: i2c.h:110
unsigned char uint8_t
Definition: types.h:14
static void baro_board_send_config_abs(void)
Definition: baro_board.c:154
struct i2c_transaction baro_trans
Definition: baro_board.c:50
transmit only transaction
Definition: i2c.h:47
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
#define LED_OFF(i)
Definition: led_hw.h:29
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:198
static void baro_board_send_config_diff(void)
Definition: baro_board.c:165
receive only transaction
Definition: i2c.h:48
static void baro_board_send_reset(void)
Definition: baro_board.c:169
bool_t i2c_idle(struct i2c_periph *p)
Check if I2C bus is idle.
Definition: i2c_arch.c:331
transaction is pending in queue
Definition: i2c.h:55
enum BaroBoardStatus status
Definition: baro_board.h:43
Architecture independent I2C (Inter-Integrated Circuit Bus) API.