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
ir_mlx.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 Martin Mueller
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 
30 #include "modules/meteo/ir_mlx.h"
31 
32 #include "mcu_periph/sys_time.h"
33 #include "mcu_periph/i2c.h"
34 #include "led.h"
35 #include "mcu_periph/uart.h"
36 #include "messages.h"
38 
39 
40 #ifndef MLX_I2C_DEV
41 #define MLX_I2C_DEV i2c0
42 #endif
43 
45 
53 
54 /* I2C address is set to 6 (8 bit) */
55 #ifndef MLX90614_ADDR
56 #define MLX90614_ADDR 0x06
57 #endif
58 
59 #define MLX90614_GENERAL_ADDR 0
60 
61 // printf("Ta = %2.2f°C (0x%04X)\n", (tp*0.02)-273.15, tp);
62 
63 void ir_mlx_crc(unsigned char addr, volatile unsigned char* data) {
64  unsigned char i, bit, crc = 0;
65 
66  for (i = 0; i < 4; i++) {
67  if (i != 0) crc ^= (data[i-1]);
68  else crc ^= addr;
69  for (bit = 8; bit > 0; bit--) {
70  if (crc & 0x80)
71  /* SMBus x^8 + x^2 + x + 1 */
72  crc = (crc << 1) ^ 0x107;
73  else
74  crc = (crc << 1);
75  }
76  }
77  data[3] = crc;
78 }
79 
80 void ir_mlx_init( void ) {
81 #ifdef IR_MLX_ONE_TIME_CONFIG
82 #warning Starting MLX90614 in CONFIGURATION MODE, do this only once for
83 #warning setup, then turn this MODE off again and recompile/flash
85 #else
87 #endif
88 }
89 
90 void ir_mlx_periodic( void ) {
91 #ifdef IR_MLX_ONE_TIME_CONFIG
92  if (sys_time.nb_sec > 4) {
93 #else
94  if (sys_time.nb_sec > 1) {
95 #endif
96  if (ir_mlx_status >= IR_MLX_IDLE) {
97  /* start two byte case temperature */
101  /* send serial number every 30 seconds */
102  RunOnceEvery((8*30), DOWNLINK_SEND_MLX_SERIAL(DefaultChannel, DefaultDevice, &ir_mlx_id_01, &ir_mlx_id_23));
103  } else if (ir_mlx_status == IR_MLX_UNINIT) {
104  /* start two byte ID 0 */
108  }
109  }
110 #ifdef IR_MLX_ONE_TIME_CONFIG
111  else if ((sys_time.nb_sec > 1) && (ir_mlx_status == IR_MLX_ADDR_CHANGE)) {
112  /* erase address by writing zero to SMBus address register */
115  mlx_trans.buf[1] = 0;
116  mlx_trans.buf[2] = 0;
119  } else
120  if ((sys_time.nb_sec > 2) && (ir_mlx_status == IR_MLX_ADDR_ERASE)) {
121  /* set address by writing it to AMBus address register */
124  mlx_trans.buf[1] = MLX90614_ADDR >> 1;
125  mlx_trans.buf[2] = 0;
128  } else
129  if ((sys_time.nb_sec > 3) && (ir_mlx_status == IR_MLX_ADDR_SET)) {
131  }
132 #endif
133 }
134 
135 void ir_mlx_event( void ) {
136  if ((mlx_trans.status == I2CTransSuccess)) {
137  switch (ir_mlx_status) {
138 
139  case IR_MLX_RD_ID_0:
140  /* read two byte ID 0 */
142  ir_mlx_id_01 |= mlx_trans.buf[1] << 8;
143  /* start two byte ID 1 */
147  break;
148 
149  case IR_MLX_RD_ID_1:
150  /* read two byte ID 1 */
151  ir_mlx_id_01 |= mlx_trans.buf[0] << 16;
152  ir_mlx_id_01 |= mlx_trans.buf[1] << 24;
153  /* start two byte ID 2 */
157  break;
158 
159  case IR_MLX_RD_ID_2:
160  /* read two byte ID 2 */
162  ir_mlx_id_23 |= mlx_trans.buf[1] << 8;
163  /* start two byte ID 3 */
167  break;
168 
169  case IR_MLX_RD_ID_3:
170  /* read two byte ID 3 */
171  ir_mlx_id_23 |= mlx_trans.buf[0] << 16;
172  ir_mlx_id_23 |= mlx_trans.buf[1] << 24;
175  DOWNLINK_SEND_MLX_SERIAL(DefaultChannel, DefaultDevice, &ir_mlx_id_01, &ir_mlx_id_23);
176  break;
177 
178  case IR_MLX_RD_CASE_TEMP:
179  /* read two byte case temperature */
180  ir_mlx_itemp_case = mlx_trans.buf[1] << 8;
182  ir_mlx_temp_case = ir_mlx_itemp_case*0.02 - 273.15;
183 
184  /* start two byte obj temperature */
188  break;
189 
190  case IR_MLX_RD_OBJ_TEMP:
191  /* read two byte obj temperature */
192  ir_mlx_itemp_obj = mlx_trans.buf[1] << 8;
194  ir_mlx_temp_obj = ir_mlx_itemp_obj*0.02 - 273.15;
196 
197  DOWNLINK_SEND_MLX_STATUS(DefaultChannel, DefaultDevice,
201  &ir_mlx_temp_obj);
202  break;
203  default:
205  break;
206  }
207  }
208 }
#define MLX90614_ID_0
Definition: ir_mlx.h:9
unsigned short uint16_t
Definition: types.h:16
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
#define MLX_I2C_DEV
Definition: ir_mlx.c:41
#define MLX90614_ID_3
Definition: ir_mlx.h:12
#define MLX90614_ID_1
Definition: ir_mlx.h:10
uint32_t ir_mlx_id_01
Definition: ir_mlx.c:51
transaction successfully finished by I2C driver
Definition: i2c.h:57
#define MLX90614_TA
Definition: ir_mlx.h:6
I2C transaction structure.
Definition: i2c.h:93
void ir_mlx_event(void)
Definition: ir_mlx.c:135
#define MLX90614_TOBJ
Definition: ir_mlx.h:7
uint16_t ir_mlx_itemp_obj
Definition: ir_mlx.c:49
bool_t i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r)
Submit a write/read transaction.
Definition: i2c.c:258
void ir_mlx_crc(unsigned char addr, volatile unsigned char *data)
Definition: ir_mlx.c:63
#define MLX90614_SADR
Definition: ir_mlx.h:8
#define MLX90614_ADDR
Definition: ir_mlx.c:56
float ir_mlx_temp_case
Definition: ir_mlx.c:48
uint16_t ir_mlx_itemp_case
Definition: ir_mlx.c:47
transaction set to done by user level
Definition: i2c.h:59
Architecture independent timing functions.
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
unsigned long uint32_t
Definition: types.h:18
float ir_mlx_temp_obj
Definition: ir_mlx.c:50
struct i2c_transaction mlx_trans
Definition: ir_mlx.c:44
void ir_mlx_periodic(void)
Definition: ir_mlx.c:90
void ir_mlx_init(void)
Definition: ir_mlx.c:80
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
#define MLX90614_ID_2
Definition: ir_mlx.h:11
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
#define MLX90614_GENERAL_ADDR
Definition: ir_mlx.c:59
unsigned char uint8_t
Definition: types.h:14
volatile uint32_t nb_sec
full seconds since startup
Definition: sys_time.h:69
uint8_t ir_mlx_status
Definition: ir_mlx.c:46
arch independent LED (Light Emitting Diodes) API
uint32_t ir_mlx_id_23
Definition: ir_mlx.c:52
Architecture independent I2C (Inter-Integrated Circuit Bus) API.