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
infrared_i2c.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 ENAC
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 "sensors/infrared_i2c.h"
24 
25 // IR I2C definitions
26 #define IR_HOR_I2C_ADDR (0x6C << 1)
27 #define IR_VER_I2C_ADDR (0x68 << 1)
28 #define IR_SAMPLE_RATE_SELECT (0 << 2)
29 #define IR_HOR_OC_BIT (0 << 4)
30 #define IR_VER_OC_BIT (1 << 4)
31 #define IR_HOR_I2C_SELECT_IR1 (0 << 5)
32 #define IR_HOR_I2C_SELECT_IR2 (1 << 5)
33 #define IR_START_CONV (1 << 7)
34 
35 
36 #ifndef IR_I2C_IR1_NEUTRAL
37 #define IR_I2C_IR1_NEUTRAL 0
38 #endif
39 
40 #ifndef IR_I2C_IR2_NEUTRAL
41 #define IR_I2C_IR2_NEUTRAL 0
42 #endif
43 
44 #ifndef IR_I2C_TOP_NEUTRAL
45 #define IR_I2C_TOP_NEUTRAL 0
46 #endif
47 
52 
53 // Local variables
54 #define IR_I2C_IDLE 0
55 #define IR_I2C_READ_IR1 1
56 #define IR_I2C_IR2_SELECTED 2
57 #define IR_I2C_READ_IR2 3
58 #define IR_I2C_IR1_SELECTED 4
59 #define IR_I2C_CONFIGURE_HOR 5
60 
62 
63 #define NO_CONF_WORD 0xff
64 #define ValidConfWord(_x) (_x < 0x4)
65 
66 // I2C structure
67 struct i2c_transaction irh_trans, irv_trans;
68 
69 // Standard infrared implementation
70 void infrared_init(void) {
72 }
73 
74 void infrared_update(void) {
76 }
77 
78 void infrared_event(void) {
80 }
81 
84 void infrared_i2c_init( void ) {
88  ir_i2c_conf_word = IR_I2C_DEFAULT_CONF;
91  irh_trans.status = I2CTransDone;
93 
95 }
96 
97 void infrared_i2c_update( void ) {
98 #if ! (defined SITL || defined HITL)
99  // IR horizontal
100  if (irh_trans.status == I2CTransDone && ir_i2c_hor_status == IR_I2C_IDLE) {
102  irh_trans.buf[0] = ir_i2c_conf_word | IR_HOR_OC_BIT | IR_START_CONV ;
103  i2c_transmit(&i2c0, &irh_trans, IR_HOR_I2C_ADDR, 1);
105  } else {
106  // Read next values
107  i2c_receive(&i2c0, &irh_trans, IR_HOR_I2C_ADDR, 3);
110  }
111  }
112  // IR vertical
113  if (irv_trans.status == I2CTransDone) {
117  } else {
118  // Read next values
121  }
122  }
123 #endif /* SITL || HITL */
124 }
125 
126 #define FilterIR(_ir_prev, _ir_next) (((1<<ir_i2c_conf_word)*_ir_prev + _ir_next) / ((1<<ir_i2c_conf_word) + 1))
127 
129 #if ! (defined SITL || defined HITL)
130  irh_trans.status = I2CTransDone;
131  switch (ir_i2c_hor_status) {
132  case IR_I2C_IDLE :
133  break;
134  case IR_I2C_READ_IR1 :
135  if (bit_is_set(irh_trans.buf[2],7)) {
136  i2c_receive(&i2c0, &irh_trans, IR_HOR_I2C_ADDR, 3);
137  break;
138  }
139  // Read IR1 value
140  int16_t ir1 = (irh_trans.buf[0]<<8) | irh_trans.buf[1];
141  ir1 = ir1 - (IR_I2C_IR1_NEUTRAL << ir_i2c_conf_word);
142  ir_i2c.ir1 = FilterIR(ir_i2c.ir1, ir1);
143  // Select IR2 channel
145  i2c_transmit(&i2c0, &irh_trans, IR_HOR_I2C_ADDR, 1);
147  break;
148  case IR_I2C_IR2_SELECTED :
149  // IR2 selected, asking for IR2 value
150  i2c_receive(&i2c0, &irh_trans, IR_HOR_I2C_ADDR, 3);
152  break;
153  case IR_I2C_READ_IR2 :
154  // Read IR2 value
155  if (bit_is_set(irh_trans.buf[2],7)) {
156  i2c_receive(&i2c0, &irh_trans, IR_HOR_I2C_ADDR, 3);
157  break;
158  }
159  int16_t ir2 = (irh_trans.buf[0]<<8) | irh_trans.buf[1];
160  ir2 = ir2 - (IR_I2C_IR2_NEUTRAL << ir_i2c_conf_word);
161  ir_i2c.ir2 = FilterIR(ir_i2c.ir2, ir2);
162  // Update estimator
164 #ifndef IR_I2C_READ_ONLY
169  }
170 #endif
171  // Select IR1 channel
173  i2c_transmit(&i2c0, &irh_trans, IR_HOR_I2C_ADDR, 1);
175  break;
176  case IR_I2C_IR1_SELECTED :
177  // End reading cycle
179  break;
180  case IR_I2C_CONFIGURE_HOR :
181  // End conf cycle
184  break;
185  }
186 #endif /* !SITL && !HITL */
187 }
188 
190 #if ! (defined SITL || defined HITL)
192  // Read TOP value
193  if (irv_trans.type == I2CTransRx) {
194  int16_t ir3 = (irv_trans.buf[0]<<8) | irv_trans.buf[1];
195  ir3 = ir3 - (IR_I2C_TOP_NEUTRAL << ir_i2c_conf_word);
196  ir_i2c.ir3 = FilterIR(ir_i2c.ir3, ir3);
198 #ifndef IR_I2C_READ_ONLY
203  }
204 #endif
205  }
206  if (irv_trans.type == I2CTransTx) {
208  }
209 #endif /* !SITL && !HITL */
210 }
211 
#define infrared_i2cEvent()
Definition: infrared_i2c.h:47
struct i2c_transaction irh_trans irv_trans
Definition: infrared_i2c.c:67
#define ValidConfWord(_x)
Definition: infrared_i2c.c:64
void infrared_i2c_init(void)
Initialisation.
Definition: infrared_i2c.c:84
#define IR_I2C_IR2_NEUTRAL
Definition: infrared_i2c.c:41
#define IR_HOR_I2C_SELECT_IR2
Definition: infrared_i2c.c:32
#define IR_VER_OC_BIT
Definition: infrared_i2c.c:30
I2C transaction structure.
Definition: i2c.h:93
int16_t ir2
Definition: infrared.h:113
static uint8_t ir_i2c_hor_status
Definition: infrared_i2c.c:61
#define IR_I2C_IR1_SELECTED
Definition: infrared_i2c.c:58
void infrared_init(void)
Definition: infrared_i2c.c:70
struct Infrared_raw ir_i2c
Definition: infrared_i2c.c:48
#define IR_HOR_I2C_SELECT_IR1
Definition: infrared_i2c.c:31
#define FALSE
Definition: imu_chimu.h:141
#define IR_I2C_IR1_NEUTRAL
Definition: infrared_i2c.c:37
#define IR_START_CONV
Definition: infrared_i2c.c:33
void infrared_update(void)
Definition: infrared_i2c.c:74
bool_t ir_i2c_conf_hor_done
Definition: infrared_i2c.c:51
bool_t ir_i2c_data_hor_available
Definition: infrared_i2c.c:49
#define FilterIR(_ir_prev, _ir_next)
Definition: infrared_i2c.c:126
transaction set to done by user level
Definition: i2c.h:59
uint8_t ir_i2c_conf_word
Definition: infrared_i2c.c:50
void infrared_struct_init(void)
Initialisation of ir structure.
Definition: infrared.c:38
bool_t ir_i2c_data_ver_available
Definition: infrared_i2c.c:49
bool_t ir_i2c_conf_ver_done
Definition: infrared_i2c.c:51
#define IR_I2C_READ_IR1
Definition: infrared_i2c.c:55
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:238
#define IR_I2C_IDLE
Definition: infrared_i2c.c:54
signed short int16_t
Definition: types.h:17
#define UpdateIRValue(_v)
Definition: infrared.h:149
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
#define IR_I2C_READ_IR2
Definition: infrared_i2c.c:57
#define TRUE
Definition: imu_chimu.h:144
void infrared_i2c_update(void)
Definition: infrared_i2c.c:97
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
unsigned char uint8_t
Definition: types.h:14
void infrared_i2c_ver_event(void)
Definition: infrared_i2c.c:189
#define IR_I2C_CONFIGURE_HOR
Definition: infrared_i2c.c:59
transmit only transaction
Definition: i2c.h:47
int16_t ir1
Definition: infrared.h:112
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
#define IR_HOR_I2C_ADDR
Definition: infrared_i2c.c:26
#define IR_I2C_TOP_NEUTRAL
Definition: infrared_i2c.c:45
#define IR_HOR_OC_BIT
Definition: infrared_i2c.c:29
receive only transaction
Definition: i2c.h:48
#define IR_VER_I2C_ADDR
Definition: infrared_i2c.c:27
bool_t i2c_receive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint16_t len)
Submit a read only transaction.
Definition: i2c.c:248
#define IR_I2C_IR2_SELECTED
Definition: infrared_i2c.c:56
int16_t ir3
Definition: infrared.h:114
void infrared_i2c_hor_event(void)
Definition: infrared_i2c.c:128
void infrared_event(void)
Definition: infrared_i2c.c:78