Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
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 #ifndef DOWNLINK_DEVICE
40 #define DOWNLINK_DEVICE DOWNLINK_AP_DEVICE
41 #endif
42 
43 #ifndef MLX_I2C_DEV
44 #define MLX_I2C_DEV i2c0
45 #endif
46 
48 
56 
57 /* I2C address is set to 6 (8 bit) */
58 #ifndef MLX90614_ADDR
59 #define MLX90614_ADDR 0x06
60 #endif
61 
62 #define MLX90614_GENERAL_ADDR 0
63 
64 // printf("Ta = %2.2f°C (0x%04X)\n", (tp*0.02)-273.15, tp);
65 
66 void ir_mlx_crc(unsigned char addr, volatile unsigned char* data) {
67  unsigned char i, bit, crc = 0;
68 
69  for (i = 0; i < 4; i++) {
70  if (i != 0) crc ^= (data[i-1]);
71  else crc ^= addr;
72  for (bit = 8; bit > 0; bit--) {
73  if (crc & 0x80)
74  /* SMBus x^8 + x^2 + x + 1 */
75  crc = (crc << 1) ^ 0x107;
76  else
77  crc = (crc << 1);
78  }
79  }
80  data[3] = crc;
81 }
82 
83 void ir_mlx_init( void ) {
84 #ifdef IR_MLX_ONE_TIME_CONFIG
85 #warning Starting MLX90614 in CONFIGURATION MODE, do this only once for
86 #warning setup, then turn this MODE off again and recompile/flash
88 #else
90 #endif
91 }
92 
93 void ir_mlx_periodic( void ) {
94 #ifdef IR_MLX_ONE_TIME_CONFIG
95  if (sys_time.nb_sec > 4) {
96 #else
97  if (sys_time.nb_sec > 1) {
98 #endif
99  if (ir_mlx_status >= IR_MLX_IDLE) {
100  /* start two byte case temperature */
104  /* send serial number every 30 seconds */
105  RunOnceEvery((8*30), DOWNLINK_SEND_MLX_SERIAL(DefaultChannel, DefaultDevice, &ir_mlx_id_01, &ir_mlx_id_23));
106  } else if (ir_mlx_status == IR_MLX_UNINIT) {
107  /* start two byte ID 0 */
111  }
112  }
113 #ifdef IR_MLX_ONE_TIME_CONFIG
114  else if ((sys_time.nb_sec > 1) && (ir_mlx_status == IR_MLX_ADDR_CHANGE)) {
115  /* erase address by writing zero to SMBus address register */
118  mlx_trans.buf[1] = 0;
119  mlx_trans.buf[2] = 0;
122  } else
123  if ((sys_time.nb_sec > 2) && (ir_mlx_status == IR_MLX_ADDR_ERASE)) {
124  /* set address by writing it to AMBus address register */
127  mlx_trans.buf[1] = MLX90614_ADDR >> 1;
128  mlx_trans.buf[2] = 0;
131  } else
132  if ((sys_time.nb_sec > 3) && (ir_mlx_status == IR_MLX_ADDR_SET)) {
134  }
135 #endif
136 }
137 
138 void ir_mlx_event( void ) {
139  if ((mlx_trans.status == I2CTransSuccess)) {
140  switch (ir_mlx_status) {
141 
142  case IR_MLX_RD_ID_0:
143  /* read two byte ID 0 */
145  ir_mlx_id_01 |= mlx_trans.buf[1] << 8;
146  /* start two byte ID 1 */
150  break;
151 
152  case IR_MLX_RD_ID_1:
153  /* read two byte ID 1 */
154  ir_mlx_id_01 |= mlx_trans.buf[0] << 16;
155  ir_mlx_id_01 |= mlx_trans.buf[1] << 24;
156  /* start two byte ID 2 */
160  break;
161 
162  case IR_MLX_RD_ID_2:
163  /* read two byte ID 2 */
165  ir_mlx_id_23 |= mlx_trans.buf[1] << 8;
166  /* start two byte ID 3 */
170  break;
171 
172  case IR_MLX_RD_ID_3:
173  /* read two byte ID 3 */
174  ir_mlx_id_23 |= mlx_trans.buf[0] << 16;
175  ir_mlx_id_23 |= mlx_trans.buf[1] << 24;
178  DOWNLINK_SEND_MLX_SERIAL(DefaultChannel, DefaultDevice, &ir_mlx_id_01, &ir_mlx_id_23);
179  break;
180 
181  case IR_MLX_RD_CASE_TEMP:
182  /* read two byte case temperature */
183  ir_mlx_itemp_case = mlx_trans.buf[1] << 8;
185  ir_mlx_temp_case = ir_mlx_itemp_case*0.02 - 273.15;
186 
187  /* start two byte obj temperature */
191  break;
192 
193  case IR_MLX_RD_OBJ_TEMP:
194  /* read two byte obj temperature */
195  ir_mlx_itemp_obj = mlx_trans.buf[1] << 8;
197  ir_mlx_temp_obj = ir_mlx_itemp_obj*0.02 - 273.15;
199 
200  DOWNLINK_SEND_MLX_STATUS(DefaultChannel, DefaultDevice,
204  &ir_mlx_temp_obj);
205  break;
206  default:
208  break;
209  }
210  }
211 }
#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:44
#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:54
#define MLX90614_TA
Definition: ir_mlx.h:6
void ir_mlx_event(void)
Definition: ir_mlx.c:138
#define MLX90614_TOBJ
Definition: ir_mlx.h:7
uint16_t ir_mlx_itemp_obj
Definition: ir_mlx.c:52
bool_t i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r)
Definition: i2c.c:105
void ir_mlx_crc(unsigned char addr, volatile unsigned char *data)
Definition: ir_mlx.c:66
#define MLX90614_SADR
Definition: ir_mlx.h:8
#define MLX90614_ADDR
Definition: ir_mlx.c:59
float ir_mlx_temp_case
Definition: ir_mlx.c:51
uint16_t ir_mlx_itemp_case
Definition: ir_mlx.c:50
Architecture independent timing functions.
bool_t i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Definition: i2c.c:85
unsigned long uint32_t
Definition: types.h:18
float ir_mlx_temp_obj
Definition: ir_mlx.c:53
struct i2c_transaction mlx_trans
Definition: ir_mlx.c:47
void ir_mlx_periodic(void)
Definition: ir_mlx.c:93
void ir_mlx_init(void)
Definition: ir_mlx.c:83
enum I2CTransactionStatus status
Definition: i2c.h:83
#define MLX90614_ID_2
Definition: ir_mlx.h:11
volatile uint8_t buf[I2C_BUF_LEN]
Definition: i2c.h:82
#define MLX90614_GENERAL_ADDR
Definition: ir_mlx.c:62
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:49
arch independent LED (Light Emitting Diodes) API
uint32_t ir_mlx_id_23
Definition: ir_mlx.c:55
Architecture independent I2C (Inter-Integrated Circuit Bus) API.