Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
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)
71 {
73 }
74 
75 void infrared_update(void)
76 {
78 }
79 
80 void infrared_event(void)
81 {
83 }
84 
88 {
92  ir_i2c_conf_word = IR_I2C_DEFAULT_CONF;
93  ir_i2c_conf_hor_done = false;
94  ir_i2c_conf_ver_done = false;
95  irh_trans.status = I2CTransDone;
97 
99 }
100 
102 {
103 #if ! (defined SITL || defined HITL)
104  // IR horizontal
105  if (irh_trans.status == I2CTransDone && ir_i2c_hor_status == IR_I2C_IDLE) {
107  irh_trans.buf[0] = ir_i2c_conf_word | IR_HOR_OC_BIT | IR_START_CONV ;
108  i2c_transmit(&i2c0, &irh_trans, IR_HOR_I2C_ADDR, 1);
110  } else {
111  // Read next values
112  i2c_receive(&i2c0, &irh_trans, IR_HOR_I2C_ADDR, 3);
115  }
116  }
117  // IR vertical
118  if (irv_trans.status == I2CTransDone) {
122  } else {
123  // Read next values
126  }
127  }
128 #endif /* SITL || HITL */
129 }
130 
131 #define FilterIR(_ir_prev, _ir_next) (((1<<ir_i2c_conf_word)*_ir_prev + _ir_next) / ((1<<ir_i2c_conf_word) + 1))
132 
134 {
135 #if ! (defined SITL || defined HITL)
136  irh_trans.status = I2CTransDone;
137  switch (ir_i2c_hor_status) {
138  case IR_I2C_IDLE :
139  break;
140  case IR_I2C_READ_IR1 :
141  if (bit_is_set(irh_trans.buf[2], 7)) {
142  i2c_receive(&i2c0, &irh_trans, IR_HOR_I2C_ADDR, 3);
143  break;
144  }
145  // Read IR1 value
146  int16_t ir1 = (irh_trans.buf[0] << 8) | irh_trans.buf[1];
147  ir1 = ir1 - (IR_I2C_IR1_NEUTRAL << ir_i2c_conf_word);
148  ir_i2c.ir1 = FilterIR(ir_i2c.ir1, ir1);
149  // Select IR2 channel
151  i2c_transmit(&i2c0, &irh_trans, IR_HOR_I2C_ADDR, 1);
153  break;
154  case IR_I2C_IR2_SELECTED :
155  // IR2 selected, asking for IR2 value
156  i2c_receive(&i2c0, &irh_trans, IR_HOR_I2C_ADDR, 3);
158  break;
159  case IR_I2C_READ_IR2 :
160  // Read IR2 value
161  if (bit_is_set(irh_trans.buf[2], 7)) {
162  i2c_receive(&i2c0, &irh_trans, IR_HOR_I2C_ADDR, 3);
163  break;
164  }
165  int16_t ir2 = (irh_trans.buf[0] << 8) | irh_trans.buf[1];
166  ir2 = ir2 - (IR_I2C_IR2_NEUTRAL << ir_i2c_conf_word);
167  ir_i2c.ir2 = FilterIR(ir_i2c.ir2, ir2);
168  // Update estimator
170 #ifndef IR_I2C_READ_ONLY
175  }
176 #endif
177  // Select IR1 channel
179  i2c_transmit(&i2c0, &irh_trans, IR_HOR_I2C_ADDR, 1);
181  break;
182  case IR_I2C_IR1_SELECTED :
183  // End reading cycle
185  break;
186  case IR_I2C_CONFIGURE_HOR :
187  // End conf cycle
188  ir_i2c_conf_hor_done = true;
190  break;
191  }
192 #endif /* !SITL && !HITL */
193 }
194 
196 {
197 #if ! (defined SITL || defined HITL)
199  // Read TOP value
200  if (irv_trans.type == I2CTransRx) {
201  int16_t ir3 = (irv_trans.buf[0] << 8) | irv_trans.buf[1];
202  ir3 = ir3 - (IR_I2C_TOP_NEUTRAL << ir_i2c_conf_word);
203  ir_i2c.ir3 = FilterIR(ir_i2c.ir3, ir3);
205 #ifndef IR_I2C_READ_ONLY
210  }
211 #endif
212  }
213  if (irv_trans.type == I2CTransTx) {
214  ir_i2c_conf_ver_done = true;
215  }
216 #endif /* !SITL && !HITL */
217 }
218 
IR_HOR_OC_BIT
#define IR_HOR_OC_BIT
Definition: infrared_i2c.c:29
i2c_transaction::buf
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
Infrared_raw::ir1
int16_t ir1
Definition: infrared.h:112
UpdateIRValue
#define UpdateIRValue(_v)
Definition: infrared.h:149
I2CTransTx
@ I2CTransTx
transmit only transaction
Definition: i2c.h:47
infrared_event
void infrared_event(void)
Definition: infrared_i2c.c:80
ir_i2c
struct Infrared_raw ir_i2c
Definition: infrared_i2c.c:48
irv_trans
struct i2c_transaction irh_trans irv_trans
Definition: infrared_i2c.c:67
IR_HOR_I2C_SELECT_IR1
#define IR_HOR_I2C_SELECT_IR1
Definition: infrared_i2c.c:31
infrared_init
void infrared_init(void)
Definition: infrared_i2c.c:70
IR_I2C_IR1_SELECTED
#define IR_I2C_IR1_SELECTED
Definition: infrared_i2c.c:58
IR_HOR_I2C_SELECT_IR2
#define IR_HOR_I2C_SELECT_IR2
Definition: infrared_i2c.c:32
IR_START_CONV
#define IR_START_CONV
Definition: infrared_i2c.c:33
infrared_update
void infrared_update(void)
Definition: infrared_i2c.c:75
ir_i2c_hor_status
static uint8_t ir_i2c_hor_status
Definition: infrared_i2c.c:61
FilterIR
#define FilterIR(_ir_prev, _ir_next)
Definition: infrared_i2c.c:131
ir_i2c_conf_word
uint8_t ir_i2c_conf_word
Definition: infrared_i2c.c:50
Infrared_raw::ir2
int16_t ir2
Definition: infrared.h:113
i2c_transmit
bool 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:324
IR_I2C_READ_IR2
#define IR_I2C_READ_IR2
Definition: infrared_i2c.c:57
IR_I2C_READ_IR1
#define IR_I2C_READ_IR1
Definition: infrared_i2c.c:55
I2CTransRx
@ I2CTransRx
receive only transaction
Definition: i2c.h:48
Infrared_raw
Definition: infrared.h:109
infrared_i2c_update
void infrared_i2c_update(void)
Definition: infrared_i2c.c:101
Infrared_raw::ir3
int16_t ir3
Definition: infrared.h:114
int16_t
signed short int16_t
Definition: types.h:17
IR_I2C_IR1_NEUTRAL
#define IR_I2C_IR1_NEUTRAL
Definition: infrared_i2c.c:37
uint8_t
unsigned char uint8_t
Definition: types.h:14
infrared_struct_init
void infrared_struct_init(void)
Initialisation of ir structure.
Definition: infrared.c:34
i2c_transaction::status
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
infrared_i2c_ver_event
void infrared_i2c_ver_event(void)
Definition: infrared_i2c.c:195
infrared_i2cEvent
#define infrared_i2cEvent()
Definition: infrared_i2c.h:47
IR_I2C_CONFIGURE_HOR
#define IR_I2C_CONFIGURE_HOR
Definition: infrared_i2c.c:59
ir_i2c_conf_hor_done
bool ir_i2c_conf_hor_done
Definition: infrared_i2c.c:51
i2c_transaction
I2C transaction structure.
Definition: i2c.h:93
IR_HOR_I2C_ADDR
#define IR_HOR_I2C_ADDR
Definition: infrared_i2c.c:26
IR_I2C_IDLE
#define IR_I2C_IDLE
Definition: infrared_i2c.c:54
IR_I2C_TOP_NEUTRAL
#define IR_I2C_TOP_NEUTRAL
Definition: infrared_i2c.c:45
IR_VER_I2C_ADDR
#define IR_VER_I2C_ADDR
Definition: infrared_i2c.c:27
IR_I2C_IR2_SELECTED
#define IR_I2C_IR2_SELECTED
Definition: infrared_i2c.c:56
I2CTransDone
@ I2CTransDone
transaction set to done by user level
Definition: i2c.h:59
infrared_i2c_hor_event
void infrared_i2c_hor_event(void)
Definition: infrared_i2c.c:133
ir_i2c_conf_ver_done
bool ir_i2c_conf_ver_done
Definition: infrared_i2c.c:51
ir_i2c_data_ver_available
bool ir_i2c_data_ver_available
Definition: infrared_i2c.c:49
ir_i2c_data_hor_available
bool ir_i2c_data_hor_available
Definition: infrared_i2c.c:49
infrared_i2c_init
void infrared_i2c_init(void)
Initialisation.
Definition: infrared_i2c.c:87
IR_I2C_IR2_NEUTRAL
#define IR_I2C_IR2_NEUTRAL
Definition: infrared_i2c.c:41
i2c_transaction::type
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
IR_VER_OC_BIT
#define IR_VER_OC_BIT
Definition: infrared_i2c.c:30
i2c_receive
bool 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:334
ValidConfWord
#define ValidConfWord(_x)
Definition: infrared_i2c.c:64