I2C hang recover function added

Dependencies:   UniGraphic mbed vt100

In this version, check_i2c_pins function was added in edge_mgr.cpp.

プログラムの起動時、I2Cモジュールを初期化する前に、I2Cに使用するピンの電位を確認し
もし一方でも Low に張り付いていた場合、SCL を GPIO 出力に設定して 
所定回数 (I2C_UNLOCK_TRIAL_CYCLE) 反転させることにより、疑似リセットクロックを生成します。

その後は、通常の起動手順に復帰し、以降はこれまでと同様の動作をします。

Committer:
gaku_miyagawa
Date:
Mon Jun 18 02:55:38 2018 +0000
Revision:
2:de22987be9ba
Parent:
1:1af1fd910340
SBU SPECIAL

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 0:d895cd1cd897 1 #include "mbed.h"
Rhyme 0:d895cd1cd897 2 #include "edge_mgr.h"
Rhyme 0:d895cd1cd897 3 #include "af_attributes.h"
Rhyme 0:d895cd1cd897 4
Rhyme 0:d895cd1cd897 5 #include "edge_time.h"
Rhyme 0:d895cd1cd897 6 #include "edge_pin.h"
Rhyme 0:d895cd1cd897 7 #include "MMA8451Q.h"
Rhyme 0:d895cd1cd897 8 #include "VEML6040.h"
Rhyme 0:d895cd1cd897 9 #include "LM75B.h"
Rhyme 0:d895cd1cd897 10 #include "SMTC502AT.h"
Rhyme 0:d895cd1cd897 11 #include "PSE530.h"
Rhyme 0:d895cd1cd897 12 #include <ILI9341.h>
Rhyme 0:d895cd1cd897 13 #include "Arial12x12.h"
Rhyme 0:d895cd1cd897 14 #include "Arial24x23.h"
Rhyme 0:d895cd1cd897 15 #include "Arial28x28.h"
Rhyme 0:d895cd1cd897 16
Rhyme 0:d895cd1cd897 17 #include "edge_sensor.h"
Rhyme 0:d895cd1cd897 18 #include "edge_accel.h"
Rhyme 0:d895cd1cd897 19 #include "edge_color.h"
Rhyme 0:d895cd1cd897 20 #include "edge_temp.h"
Rhyme 0:d895cd1cd897 21 #include "edge_pressure.h"
Rhyme 0:d895cd1cd897 22 #include "edge_reset_mgr.h"
Rhyme 0:d895cd1cd897 23 #include "edge_chart.h"
Rhyme 0:d895cd1cd897 24
Rhyme 0:d895cd1cd897 25 #define MMA8451Q_I2C_ADDRESS 0x1C
Rhyme 0:d895cd1cd897 26 #define VEML6040_I2C_ADDRESS 0x10
Rhyme 0:d895cd1cd897 27 #define LM75B_I2C_ADDRESS 0x48
Rhyme 0:d895cd1cd897 28 #define SO1602A_I2C_ADDRESS 0x3C
Rhyme 0:d895cd1cd897 29
Rhyme 0:d895cd1cd897 30 #define NUM_MAX_SENSOR 5
Rhyme 0:d895cd1cd897 31
Rhyme 0:d895cd1cd897 32 uint16_t attr_to_set[] = {
Rhyme 0:d895cd1cd897 33 ATTR_ACCEL_PRESENT,
Rhyme 0:d895cd1cd897 34 ATTR_COLOR0_PRESENT,
Rhyme 0:d895cd1cd897 35 ATTR_COLOR1_PRESENT,
Rhyme 0:d895cd1cd897 36 ATTR_TEMP0_PRESENT,
Rhyme 0:d895cd1cd897 37 ATTR_GAS_PRESENT,
Rhyme 0:d895cd1cd897 38 } ;
Rhyme 0:d895cd1cd897 39
Rhyme 0:d895cd1cd897 40 uint16_t attr_to_get[] = {
Rhyme 0:d895cd1cd897 41 // accel
Rhyme 0:d895cd1cd897 42 ATTR_ACCEL_ENABLE,
Rhyme 0:d895cd1cd897 43 ATTR_ACCEL_INTERVAL,
Rhyme 0:d895cd1cd897 44 // Color0
Rhyme 0:d895cd1cd897 45 ATTR_COLOR0_ENABLE,
Rhyme 0:d895cd1cd897 46 ATTR_COLOR0_INTERVAL,
Rhyme 0:d895cd1cd897 47 ATTR_COLOR0_ITIME,
Rhyme 0:d895cd1cd897 48 ATTR_COLOR0_PWM_PERIOD,
Rhyme 0:d895cd1cd897 49 ATTR_COLOR0_PWM_TARGET,
Rhyme 0:d895cd1cd897 50 ATTR_COLOR0_PWM_R,
Rhyme 0:d895cd1cd897 51 ATTR_COLOR0_PWM_G,
Rhyme 0:d895cd1cd897 52 ATTR_COLOR0_PWM_B,
Rhyme 0:d895cd1cd897 53 // Color1
Rhyme 0:d895cd1cd897 54 ATTR_COLOR1_ENABLE,
Rhyme 0:d895cd1cd897 55 ATTR_COLOR1_INTERVAL,
Rhyme 0:d895cd1cd897 56 ATTR_COLOR1_ITIME,
Rhyme 0:d895cd1cd897 57 ATTR_COLOR1_PWM_PERIOD,
Rhyme 0:d895cd1cd897 58 ATTR_COLOR1_PWM_TARGET,
Rhyme 0:d895cd1cd897 59 ATTR_COLOR1_PWM_R,
Rhyme 0:d895cd1cd897 60 ATTR_COLOR1_PWM_G,
Rhyme 0:d895cd1cd897 61 ATTR_COLOR1_PWM_B,
Rhyme 0:d895cd1cd897 62 // Temp
Rhyme 0:d895cd1cd897 63 ATTR_TEMP0_INTERVAL,
Rhyme 0:d895cd1cd897 64 ATTR_TEMP0_ENABLE,
Rhyme 0:d895cd1cd897 65 // Gas Pressure
Rhyme 0:d895cd1cd897 66 ATTR_GAS_ENABLE,
Rhyme 0:d895cd1cd897 67 ATTR_GAS_INTERVAL,
Rhyme 0:d895cd1cd897 68 ATTR_GAS_THR_MODE,
Rhyme 0:d895cd1cd897 69 ATTR_GAS_THR_HIGH,
Rhyme 0:d895cd1cd897 70 ATTR_GAS_THR_LOW,
Rhyme 0:d895cd1cd897 71 0 } ;
Rhyme 0:d895cd1cd897 72
Rhyme 0:d895cd1cd897 73 bool verbos = true ;
Rhyme 0:d895cd1cd897 74 edge_sensor *sensor[NUM_MAX_SENSOR] ;
Rhyme 0:d895cd1cd897 75 int num_sensor = 0 ;
Rhyme 0:d895cd1cd897 76
Rhyme 0:d895cd1cd897 77 edge_accel *accel = 0 ;
Rhyme 0:d895cd1cd897 78 edge_color *color[2] = {0, 0} ;
Rhyme 0:d895cd1cd897 79 edge_temp *temp = 0 ;
Rhyme 0:d895cd1cd897 80 edge_pressure *pressure = 0 ;
Rhyme 0:d895cd1cd897 81
Rhyme 0:d895cd1cd897 82 PwmOut *led[3] = {0, 0, 0} ;
Rhyme 0:d895cd1cd897 83 uint16_t pwm[3] = { 0x5FA2, 0xB09B, 0x83DF } ;
Rhyme 0:d895cd1cd897 84 I2C *edge_i2c0 = 0 ;
Rhyme 0:d895cd1cd897 85 I2C *edge_i2c1 = 0 ;
Rhyme 0:d895cd1cd897 86 ILI9341 *display = 0 ;
Rhyme 0:d895cd1cd897 87 MMA8451Q *mma8451q = 0 ;
Rhyme 0:d895cd1cd897 88 VEML6040 *veml6040[2] = { 0, 0 } ;
Rhyme 0:d895cd1cd897 89 LM75B *lm75b0 = 0 ; /* for temp1 */
Rhyme 0:d895cd1cd897 90 AnalogIn *an0 = 0 ; /* for temp2 */
Rhyme 0:d895cd1cd897 91 SMTC502AT *smtc502at0 = 0 ;
Rhyme 0:d895cd1cd897 92 AnalogIn *an1 = 0 ; /* for temp3 */
Rhyme 0:d895cd1cd897 93 SMTC502AT *smtc502at1 = 0 ;
Rhyme 0:d895cd1cd897 94 LM75B *lm75b1 = 0 ; /* for temp4 */
Rhyme 0:d895cd1cd897 95 AnalogIn *an2 = 0 ; /* for gas pressure */
Rhyme 0:d895cd1cd897 96 PSE530 *pse530 = 0 ; /* gas pressure sensor */
Rhyme 0:d895cd1cd897 97
Rhyme 0:d895cd1cd897 98 DigitalOut *tft_reset = 0 ;
Rhyme 0:d895cd1cd897 99 DigitalOut *tft_backlight = 0 ;
Rhyme 0:d895cd1cd897 100 DigitalOut *tft_cs = 0 ;
Rhyme 0:d895cd1cd897 101 DigitalOut *pse530_en = 0 ;
Rhyme 0:d895cd1cd897 102
Rhyme 0:d895cd1cd897 103 static int error_tolerance = 100 ;
Rhyme 0:d895cd1cd897 104 static int loop_interval = 100 ; // 1000 ;
Rhyme 0:d895cd1cd897 105 static int accel_interval = 10 ;
Rhyme 0:d895cd1cd897 106 int edge_mgr_status = EDGE_MGR_INIT ;
Rhyme 0:d895cd1cd897 107 char *reset_reason_str = 0 ;
Rhyme 0:d895cd1cd897 108 int display_mode = 1 ;
Rhyme 0:d895cd1cd897 109 bool reboot_requested = false ;
Rhyme 0:d895cd1cd897 110
Rhyme 0:d895cd1cd897 111 void init_display(void)
Rhyme 0:d895cd1cd897 112 {
Rhyme 0:d895cd1cd897 113 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 114 printf("TFT Initializing\n") ;
Rhyme 0:d895cd1cd897 115 tft_reset = new DigitalOut(PIN_RESET_TFT, 1) ;
Rhyme 0:d895cd1cd897 116 tft_backlight = new DigitalOut(PIN_BL_TFT, 0) ;
Rhyme 0:d895cd1cd897 117 tft_cs = new DigitalOut(PIN_CS_TFT, 1) ;
Rhyme 0:d895cd1cd897 118
Rhyme 0:d895cd1cd897 119 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 120 display = new ILI9341(SPI_8, 10000000,
Rhyme 0:d895cd1cd897 121 PIN_MOSI, PIN_MISO, PIN_SCK,
Rhyme 0:d895cd1cd897 122 PIN_CS_TFT, PIN_RESET_TFT, PIN_DC_TFT, "LaSuno") ;
Rhyme 0:d895cd1cd897 123
Rhyme 0:d895cd1cd897 124 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 125 display->BusEnable(true) ;
Rhyme 0:d895cd1cd897 126 display->set_orientation(1) ;
Rhyme 0:d895cd1cd897 127
Rhyme 0:d895cd1cd897 128 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 129 display->cls() ;
Rhyme 0:d895cd1cd897 130 *tft_backlight = 1 ;
Rhyme 0:d895cd1cd897 131 display->BusEnable(false) ;
Rhyme 0:d895cd1cd897 132 printf("TFT Initialized\n") ;
Rhyme 0:d895cd1cd897 133 }
Rhyme 0:d895cd1cd897 134
Rhyme 0:d895cd1cd897 135 void edge_splash(void)
Rhyme 0:d895cd1cd897 136 {
Rhyme 0:d895cd1cd897 137 printf("Sensor loop started!\n") ;
Rhyme 0:d895cd1cd897 138 if (display) {
Rhyme 0:d895cd1cd897 139 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 140 display->BusEnable(true) ;
Rhyme 0:d895cd1cd897 141 display->cls() ;
Rhyme 0:d895cd1cd897 142 display->foreground(Green) ;
Rhyme 0:d895cd1cd897 143 display->locate(40, 20) ;
Rhyme 0:d895cd1cd897 144 display->printf("Sensor Loop") ;
Rhyme 0:d895cd1cd897 145 display->locate(40, 60) ;
Rhyme 0:d895cd1cd897 146 display->printf(" Started!") ;
Rhyme 0:d895cd1cd897 147 display->BusEnable(false) ;
Rhyme 0:d895cd1cd897 148 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 149 }
Rhyme 0:d895cd1cd897 150 }
Rhyme 0:d895cd1cd897 151
Rhyme 0:d895cd1cd897 152 int init_edge_attribute(void)
Rhyme 0:d895cd1cd897 153 {
Rhyme 0:d895cd1cd897 154 static int sensor_index = 0 ;
Rhyme 0:d895cd1cd897 155 static int attr_index = 0 ;
Rhyme 0:d895cd1cd897 156 static int error_count = 0 ;
Rhyme 0:d895cd1cd897 157 int return_value = 1 ;
Rhyme 0:d895cd1cd897 158 int result ;
Rhyme 0:d895cd1cd897 159
Rhyme 0:d895cd1cd897 160 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 161
Rhyme 0:d895cd1cd897 162 if (reset_reason_str) {
Rhyme 0:d895cd1cd897 163 result = afero->setAttribute(ATTR_MCU_RESET_REASON, reset_reason_str) ;
Rhyme 0:d895cd1cd897 164 if (result == afSUCCESS) {
Rhyme 0:d895cd1cd897 165 error_count = 0 ;
Rhyme 0:d895cd1cd897 166 reset_reason_str = 0 ;
Rhyme 0:d895cd1cd897 167 } else {
Rhyme 0:d895cd1cd897 168 error_count++ ;
Rhyme 0:d895cd1cd897 169 }
Rhyme 0:d895cd1cd897 170 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 171 }
Rhyme 0:d895cd1cd897 172 if (sensor_index < NUM_MAX_SENSOR) {// for each sensor send presence
Rhyme 0:d895cd1cd897 173 // printf("Setting sensor[%d] presence\n", sensor_index) ;
Rhyme 0:d895cd1cd897 174 if (sensor_index == 3) { /* for temp lm75b0 is used */
Rhyme 0:d895cd1cd897 175 result = afero->setAttributeBool(attr_to_set[sensor_index], lm75b0) ;
Rhyme 0:d895cd1cd897 176 } else {
Rhyme 0:d895cd1cd897 177 result = afero->setAttributeBool(attr_to_set[sensor_index], sensor[sensor_index]) ;
Rhyme 0:d895cd1cd897 178 }
Rhyme 0:d895cd1cd897 179 if (result == afSUCCESS) {
Rhyme 0:d895cd1cd897 180 error_count = 0 ;
Rhyme 0:d895cd1cd897 181 sensor_index++ ;
Rhyme 0:d895cd1cd897 182 } else {
Rhyme 0:d895cd1cd897 183 error_count++ ;
Rhyme 0:d895cd1cd897 184 }
Rhyme 0:d895cd1cd897 185 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 186 } else { // all sensor presence sent, now get attributes
Rhyme 0:d895cd1cd897 187 if (attr_to_get[attr_index] != 0) {
Rhyme 0:d895cd1cd897 188 // printf("getting attribute [%d]\n", attr_index) ;
Rhyme 0:d895cd1cd897 189 result = afero->getAttribute(attr_to_get[attr_index]) ;
Rhyme 0:d895cd1cd897 190 if (result == afSUCCESS) {
Rhyme 0:d895cd1cd897 191 error_count = 0 ;
Rhyme 0:d895cd1cd897 192 attr_index++ ;
Rhyme 0:d895cd1cd897 193 } else {
Rhyme 0:d895cd1cd897 194 error_count++ ;
Rhyme 0:d895cd1cd897 195 }
Rhyme 0:d895cd1cd897 196 }
Rhyme 0:d895cd1cd897 197 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 198 }
Rhyme 0:d895cd1cd897 199
Rhyme 0:d895cd1cd897 200 if (error_count > error_tolerance) { // too many fails, trying reset
Rhyme 0:d895cd1cd897 201 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 202 reboot_edge() ;
Rhyme 0:d895cd1cd897 203 }
Rhyme 0:d895cd1cd897 204
Rhyme 0:d895cd1cd897 205 if ((sensor_index >= NUM_MAX_SENSOR)&&(attr_to_get[attr_index] == 0)) { /* all sensors attributes done */
Rhyme 0:d895cd1cd897 206 sensor_index = 0 ;
Rhyme 0:d895cd1cd897 207 attr_index = 0 ;
Rhyme 0:d895cd1cd897 208 return_value = 0 ;
Rhyme 0:d895cd1cd897 209 }
Rhyme 0:d895cd1cd897 210 return(return_value) ;
Rhyme 0:d895cd1cd897 211 }
Rhyme 0:d895cd1cd897 212
Rhyme 0:d895cd1cd897 213 void edge_loop(uint32_t count_robin)
Rhyme 0:d895cd1cd897 214 {
Rhyme 0:d895cd1cd897 215 static int sensor_index = 0 ;
Rhyme 0:d895cd1cd897 216 int result ;
Rhyme 0:d895cd1cd897 217
Rhyme 0:d895cd1cd897 218 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 219
Rhyme 0:d895cd1cd897 220 if ((count_robin % accel_interval) == 0) {
Rhyme 0:d895cd1cd897 221 if (accel) {
Rhyme 0:d895cd1cd897 222 accel->accum() ; /* get and accum accel data */
Rhyme 0:d895cd1cd897 223 }
Rhyme 0:d895cd1cd897 224 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 225 }
Rhyme 0:d895cd1cd897 226
Rhyme 0:d895cd1cd897 227 if ((count_robin % loop_interval) == 0) {
Rhyme 0:d895cd1cd897 228 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 229 loop_interval = 10 ;
Rhyme 0:d895cd1cd897 230 if ((sensor[sensor_index])&&(sensor[sensor_index]->isEnabled())) {
Rhyme 0:d895cd1cd897 231 switch(sensor_index) {
Rhyme 0:d895cd1cd897 232 case SENSOR_ID_COLOR1: /* color0 */
Rhyme 0:d895cd1cd897 233 if (((edge_color*)sensor[sensor_index])->calibration_requested()) {
Rhyme 0:d895cd1cd897 234 ((edge_color*)sensor[sensor_index])->calibrate(color0_target, color0_pwm, 10) ;
Rhyme 0:d895cd1cd897 235 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 236 while((result = afero->setAttribute32(ATTR_COLOR0_PWM_R, color0_pwm[0])) != afSUCCESS) {
Rhyme 0:d895cd1cd897 237 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 238 print_af_error(result) ;
Rhyme 0:d895cd1cd897 239 wait_ms(10) ;
Rhyme 0:d895cd1cd897 240 }
Rhyme 0:d895cd1cd897 241 while((result = afero->setAttribute32(ATTR_COLOR0_PWM_G, color0_pwm[1])) != afSUCCESS) {
Rhyme 0:d895cd1cd897 242 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 243 print_af_error(result) ;
Rhyme 0:d895cd1cd897 244 wait_ms(10) ;
Rhyme 0:d895cd1cd897 245 }
Rhyme 0:d895cd1cd897 246 while((result = afero->setAttribute32(ATTR_COLOR0_PWM_B, color0_pwm[2])) != afSUCCESS) {
Rhyme 0:d895cd1cd897 247 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 248 print_af_error(result) ;
Rhyme 0:d895cd1cd897 249 wait_ms(10) ;
Rhyme 0:d895cd1cd897 250 }
Rhyme 0:d895cd1cd897 251 while((afero->setAttributeBool(ATTR_COLOR0_CALIBRATE, false)) != afSUCCESS) {
Rhyme 0:d895cd1cd897 252 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 253 print_af_error(result) ;
Rhyme 0:d895cd1cd897 254 wait_ms(10) ;
Rhyme 0:d895cd1cd897 255 }
Rhyme 0:d895cd1cd897 256 } else {
Rhyme 0:d895cd1cd897 257 sensor[sensor_index]->runStateMachine() ;
Rhyme 0:d895cd1cd897 258 }
Rhyme 0:d895cd1cd897 259 break ;
Rhyme 0:d895cd1cd897 260 case SENSOR_ID_COLOR2: /* color1 */
Rhyme 0:d895cd1cd897 261 if (((edge_color*)sensor[sensor_index])->calibration_requested()) {
Rhyme 0:d895cd1cd897 262 ((edge_color*)sensor[sensor_index])->calibrate(color1_target, color1_pwm, 10) ;
Rhyme 0:d895cd1cd897 263 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 264 if ((result = afero->setAttribute32(ATTR_COLOR1_PWM_R, color1_pwm[0])) != afSUCCESS) {
Rhyme 0:d895cd1cd897 265 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 266 print_af_error(result) ;
Rhyme 0:d895cd1cd897 267 wait_ms(10) ;
Rhyme 0:d895cd1cd897 268 }
Rhyme 0:d895cd1cd897 269 if ((result = afero->setAttribute32(ATTR_COLOR1_PWM_G, color1_pwm[1])) != afSUCCESS) {
Rhyme 0:d895cd1cd897 270 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 271 print_af_error(result) ;
Rhyme 0:d895cd1cd897 272 wait_ms(10) ;
Rhyme 0:d895cd1cd897 273 }
Rhyme 0:d895cd1cd897 274 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 275 if ((result = afero->setAttribute32(ATTR_COLOR1_PWM_B, color1_pwm[2])) != afSUCCESS) {
Rhyme 0:d895cd1cd897 276 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 277 print_af_error(result) ;
Rhyme 0:d895cd1cd897 278 wait_ms(10) ;
Rhyme 0:d895cd1cd897 279 }
Rhyme 0:d895cd1cd897 280 while((afero->setAttributeBool(ATTR_COLOR1_CALIBRATE, false)) != afSUCCESS) {
Rhyme 0:d895cd1cd897 281 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 282 print_af_error(result) ;
Rhyme 0:d895cd1cd897 283 wait_ms(10) ;
Rhyme 0:d895cd1cd897 284 }
Rhyme 0:d895cd1cd897 285 } else {
Rhyme 0:d895cd1cd897 286 sensor[sensor_index]->runStateMachine() ;
Rhyme 0:d895cd1cd897 287 }
Rhyme 0:d895cd1cd897 288 break ;
Rhyme 0:d895cd1cd897 289 default:
Rhyme 0:d895cd1cd897 290 sensor[sensor_index]->runStateMachine() ;
Rhyme 0:d895cd1cd897 291 break ;
Rhyme 0:d895cd1cd897 292 }
Rhyme 0:d895cd1cd897 293 }
Rhyme 0:d895cd1cd897 294 sensor_index = (sensor_index + 1) % NUM_MAX_SENSOR ;
Rhyme 0:d895cd1cd897 295 }
Rhyme 0:d895cd1cd897 296 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 297 }
Rhyme 0:d895cd1cd897 298
Rhyme 0:d895cd1cd897 299 int is_present(I2C *i2c, int address)
Rhyme 0:d895cd1cd897 300 {
Rhyme 0:d895cd1cd897 301 char t[1] = { 0 } ;
Rhyme 0:d895cd1cd897 302 char data[2] = { 0, 0 } ;
Rhyme 0:d895cd1cd897 303 int result ;
Rhyme 0:d895cd1cd897 304 address <<= 1 ;
Rhyme 0:d895cd1cd897 305 result = i2c->write(address, t, 1, true) ;
Rhyme 0:d895cd1cd897 306 if (result == 0) {
Rhyme 0:d895cd1cd897 307 result = i2c->read(address, data, 2) ;
Rhyme 0:d895cd1cd897 308 }
Rhyme 0:d895cd1cd897 309 return((result == 0)) ;
Rhyme 0:d895cd1cd897 310 }
Rhyme 0:d895cd1cd897 311
Rhyme 0:d895cd1cd897 312 /**
Rhyme 0:d895cd1cd897 313 * check_i2c_pins
Rhyme 0:d895cd1cd897 314 * To avoid I2C dead-lock condition,
Rhyme 0:d895cd1cd897 315 * check status of SDA and SCL.
Rhyme 0:d895cd1cd897 316 * As they are supposed to be HIGH
Rhyme 0:d895cd1cd897 317 * in case one of them is/are LOW,
Rhyme 0:d895cd1cd897 318 * change SCL pin to a digital out pin and
Rhyme 0:d895cd1cd897 319 * generate forced clock for a several cycles.
Rhyme 0:d895cd1cd897 320 * and when SDA come back to High returns
Rhyme 0:d895cd1cd897 321 * or I2C_UNLOCK_TRIAL_CYCLE exceeds, give up.
Rhyme 0:d895cd1cd897 322 */
Rhyme 0:d895cd1cd897 323 #define I2C_UNLOCK_TRIAL_CYCLE 50
Rhyme 0:d895cd1cd897 324
Rhyme 1:1af1fd910340 325 void check_i2c_pins(PinName sda_pin, PinName scl_pin, int number)
Rhyme 0:d895cd1cd897 326 {
Rhyme 0:d895cd1cd897 327 DigitalIn *sda_in = 0 ;
Rhyme 0:d895cd1cd897 328 DigitalIn *scl_in = 0 ;
Rhyme 0:d895cd1cd897 329 DigitalOut *scl_out = 0 ;
Rhyme 0:d895cd1cd897 330 int count = 0 ;
Rhyme 0:d895cd1cd897 331 sda_in = new DigitalIn(sda_pin, PullUp) ;
Rhyme 0:d895cd1cd897 332 scl_in = new DigitalIn(scl_pin, PullUp) ;
Rhyme 1:1af1fd910340 333 printf("I2C%d pin ", number) ;
Rhyme 0:d895cd1cd897 334 if ((*sda_in == 0) || (*scl_in == 0)) { /* bus hang! */
Rhyme 1:1af1fd910340 335 printf("hang detected, trying to clear ... ") ;
Rhyme 0:d895cd1cd897 336 delete scl_in ;
Rhyme 0:d895cd1cd897 337 scl_in = 0 ;
Rhyme 0:d895cd1cd897 338 scl_out = new DigitalOut(scl_pin) ;
Rhyme 0:d895cd1cd897 339 while((*sda_in == 0)&&(count++ > I2C_UNLOCK_TRIAL_CYCLE)) {
Rhyme 0:d895cd1cd897 340 *scl_out = 0 ;
Rhyme 0:d895cd1cd897 341 wait(0.01) ;
Rhyme 0:d895cd1cd897 342 *scl_out = 1 ;
Rhyme 0:d895cd1cd897 343 wait(0.01) ;
Rhyme 0:d895cd1cd897 344 }
Rhyme 1:1af1fd910340 345 if (*sda_in != 0) {
Rhyme 1:1af1fd910340 346 printf("Cleared!\n") ;
Rhyme 1:1af1fd910340 347 } else {
Rhyme 1:1af1fd910340 348 printf("Failed to Clear, proceeding\n") ;
Rhyme 1:1af1fd910340 349 }
Rhyme 0:d895cd1cd897 350 } else {
Rhyme 1:1af1fd910340 351 printf("condition OK\n") ;
Rhyme 0:d895cd1cd897 352 }
Rhyme 0:d895cd1cd897 353 if (sda_in) { delete sda_in ; }
Rhyme 0:d895cd1cd897 354 if (scl_in) { delete scl_in ; }
Rhyme 0:d895cd1cd897 355 if (scl_out) { delete scl_out ; }
Rhyme 0:d895cd1cd897 356 }
Rhyme 0:d895cd1cd897 357
Rhyme 0:d895cd1cd897 358 void init_sensors(void)
Rhyme 0:d895cd1cd897 359 {
Rhyme 0:d895cd1cd897 360 printf("=== Initializing Sensor(s) ===\n") ;
Rhyme 1:1af1fd910340 361 check_i2c_pins(PIN_I2C0_SDA, PIN_I2C0_SCL, 0) ;
Rhyme 0:d895cd1cd897 362 edge_i2c0 = new I2C(PIN_I2C0_SDA, PIN_I2C0_SCL) ;
Rhyme 0:d895cd1cd897 363
Rhyme 1:1af1fd910340 364 check_i2c_pins(PIN_I2C1_SDA, PIN_I2C1_SCL, 1) ;
Rhyme 0:d895cd1cd897 365 edge_i2c1 = new I2C(PIN_I2C1_SDA, PIN_I2C1_SCL) ;
Rhyme 0:d895cd1cd897 366
Rhyme 0:d895cd1cd897 367 if (display) {
Rhyme 0:d895cd1cd897 368 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 369 printf("printing inital string to TFT\n") ;
Rhyme 0:d895cd1cd897 370 display->BusEnable(true) ;
Rhyme 0:d895cd1cd897 371
Rhyme 0:d895cd1cd897 372
Rhyme 0:d895cd1cd897 373 display->background(Black) ;
Rhyme 0:d895cd1cd897 374 display->foreground(White) ;
Rhyme 0:d895cd1cd897 375 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 376 display->cls() ;
Rhyme 0:d895cd1cd897 377 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 378 display->set_font((unsigned char*) Arial24x23);
Rhyme 0:d895cd1cd897 379 display->foreground(Green) ;
Rhyme 0:d895cd1cd897 380 display->locate(70, 5) ;
Rhyme 0:d895cd1cd897 381 display->printf("Suntory") ;
Rhyme 0:d895cd1cd897 382 display->locate(30, 30) ;
Rhyme 0:d895cd1cd897 383 display->printf("Server Monitor") ;
Rhyme 0:d895cd1cd897 384 display->set_font((unsigned char*) Arial28x28);
Rhyme 0:d895cd1cd897 385 display->foreground(White) ;
Rhyme 0:d895cd1cd897 386 display->locate(30, 60) ;
Rhyme 0:d895cd1cd897 387 display->printf("La Suno") ;
Rhyme 0:d895cd1cd897 388 display->locate(30, 100) ;
Rhyme 0:d895cd1cd897 389 display->foreground(Red) ;
Rhyme 0:d895cd1cd897 390 display->printf("Preparing...") ;
Rhyme 0:d895cd1cd897 391 display->BusEnable(true) ;
Rhyme 0:d895cd1cd897 392 printf("Done\n") ;
Rhyme 0:d895cd1cd897 393 wait(0.1) ;
Rhyme 0:d895cd1cd897 394 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 395 display->cls() ;
Rhyme 0:d895cd1cd897 396 display->foreground(Yellow) ;
Rhyme 0:d895cd1cd897 397 display->locate(40, 5) ;
Rhyme 0:d895cd1cd897 398 display->printf("Probing sensors...") ;
Rhyme 0:d895cd1cd897 399 display->foreground(Green) ;
Rhyme 0:d895cd1cd897 400 display->BusEnable(false) ;
Rhyme 0:d895cd1cd897 401 }
Rhyme 0:d895cd1cd897 402 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 403 if (is_present(edge_i2c1, MMA8451Q_I2C_ADDRESS)) {
Rhyme 0:d895cd1cd897 404 printf("MMA8451Q on I2C1 is present\n") ;
Rhyme 0:d895cd1cd897 405 if (display) {
Rhyme 0:d895cd1cd897 406 display->BusEnable(true) ;
Rhyme 0:d895cd1cd897 407 display->locate(30, num_sensor * 30 + 40) ;
Rhyme 0:d895cd1cd897 408 display->printf("ACCEL is present") ;
Rhyme 0:d895cd1cd897 409 display->BusEnable(false) ;
Rhyme 0:d895cd1cd897 410 }
Rhyme 0:d895cd1cd897 411 mma8451q = new MMA8451Q(edge_i2c1, MMA8451Q_I2C_ADDRESS) ;
Rhyme 0:d895cd1cd897 412 accel = new edge_accel(mma8451q) ;
Rhyme 0:d895cd1cd897 413 sensor[SENSOR_ID_ACCEL] = accel ;
Rhyme 0:d895cd1cd897 414 sensor[SENSOR_ID_ACCEL]->setId(SENSOR_ID_ACCEL) ;
Rhyme 0:d895cd1cd897 415 num_sensor++ ;
Rhyme 0:d895cd1cd897 416 } else {
Rhyme 0:d895cd1cd897 417 sensor[SENSOR_ID_ACCEL] = 0 ;
Rhyme 0:d895cd1cd897 418 printf("MMA8451Q is absent\n") ;
Rhyme 0:d895cd1cd897 419 }
Rhyme 0:d895cd1cd897 420 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 421 if (is_present(edge_i2c1, VEML6040_I2C_ADDRESS)) {
Rhyme 0:d895cd1cd897 422 printf("VEML6040 on I2C1 is present\n") ;
Rhyme 0:d895cd1cd897 423 if (display) {
Rhyme 0:d895cd1cd897 424 display->BusEnable(true) ;
Rhyme 0:d895cd1cd897 425 display->locate(30, num_sensor * 30 + 40) ;
Rhyme 0:d895cd1cd897 426 display->printf("COLOR1 is present") ;
Rhyme 0:d895cd1cd897 427 display->BusEnable(false) ;
Rhyme 0:d895cd1cd897 428 }
Rhyme 0:d895cd1cd897 429 veml6040[0] = new VEML6040(edge_i2c1, VEML6040_I2C_ADDRESS) ;
Rhyme 0:d895cd1cd897 430 led[0] = new PwmOut(PIN_LED_R) ;
Rhyme 0:d895cd1cd897 431 led[1] = new PwmOut(PIN_LED_G) ;
Rhyme 0:d895cd1cd897 432 led[2] = new PwmOut(PIN_LED_B) ;
Rhyme 0:d895cd1cd897 433 color[0] = new edge_color(veml6040[0], led, pwm) ;
Rhyme 0:d895cd1cd897 434 sensor[SENSOR_ID_COLOR1] = color[0] ;
Rhyme 0:d895cd1cd897 435 sensor[SENSOR_ID_COLOR1]->setId(SENSOR_ID_COLOR1) ;
Rhyme 0:d895cd1cd897 436 num_sensor++ ;
Rhyme 0:d895cd1cd897 437 } else {
Rhyme 0:d895cd1cd897 438 sensor[SENSOR_ID_COLOR1] = 0 ;
Rhyme 0:d895cd1cd897 439 printf("VEML6040 on I2C1 is absent\n") ;
Rhyme 0:d895cd1cd897 440 }
Rhyme 0:d895cd1cd897 441 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 442 if (is_present(edge_i2c0, VEML6040_I2C_ADDRESS)) {
Rhyme 0:d895cd1cd897 443 printf("VEML6040 on I2C0 is present\n") ;
Rhyme 0:d895cd1cd897 444 if (display) {
Rhyme 0:d895cd1cd897 445 display->BusEnable(true) ;
Rhyme 0:d895cd1cd897 446 display->locate(30, num_sensor * 30 + 40) ;
Rhyme 0:d895cd1cd897 447 display->printf("COLOR2 is present") ;
Rhyme 0:d895cd1cd897 448 display->BusEnable(false) ;
Rhyme 0:d895cd1cd897 449 }
Rhyme 0:d895cd1cd897 450 veml6040[1] = new VEML6040(edge_i2c0, VEML6040_I2C_ADDRESS) ;
Rhyme 0:d895cd1cd897 451 if (led[0] == 0) {
Rhyme 0:d895cd1cd897 452 led[0] = new PwmOut(PIN_LED_R) ;
Rhyme 0:d895cd1cd897 453 led[1] = new PwmOut(PIN_LED_G) ;
Rhyme 0:d895cd1cd897 454 led[2] = new PwmOut(PIN_LED_B) ;
Rhyme 0:d895cd1cd897 455 }
Rhyme 0:d895cd1cd897 456 color[1] = new edge_color(veml6040[1], led, pwm) ;
Rhyme 0:d895cd1cd897 457 sensor[SENSOR_ID_COLOR2] = color[1] ;
Rhyme 0:d895cd1cd897 458 sensor[SENSOR_ID_COLOR2]->setId(SENSOR_ID_COLOR2) ;
Rhyme 0:d895cd1cd897 459 num_sensor++ ;
Rhyme 0:d895cd1cd897 460 } else {
Rhyme 0:d895cd1cd897 461 sensor[SENSOR_ID_COLOR2] = 0 ;
Rhyme 0:d895cd1cd897 462 printf("VEML6040 on I2C0 is absent\n") ;
Rhyme 0:d895cd1cd897 463 }
Rhyme 0:d895cd1cd897 464 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 465 if (is_present(edge_i2c1, LM75B_I2C_ADDRESS)) {
Rhyme 0:d895cd1cd897 466 printf("LM75B on I2C1 is present\n") ;
Rhyme 0:d895cd1cd897 467 if (display) {
Rhyme 0:d895cd1cd897 468 display->BusEnable(true) ;
Rhyme 0:d895cd1cd897 469 display->locate(30, num_sensor * 30 + 40) ;
Rhyme 0:d895cd1cd897 470 display->printf("TEMP1 is present") ;
Rhyme 0:d895cd1cd897 471 display->BusEnable(false) ;
Rhyme 0:d895cd1cd897 472 }
Rhyme 0:d895cd1cd897 473 lm75b0 = new LM75B(edge_i2c1, LM75B_I2C_ADDRESS) ;
Rhyme 0:d895cd1cd897 474 } else {
Rhyme 0:d895cd1cd897 475 printf("LM75B on I2C1 is absent\n") ;
Rhyme 0:d895cd1cd897 476 }
Rhyme 0:d895cd1cd897 477 #if 0
Rhyme 0:d895cd1cd897 478 if (is_present(edge_i2c0, LM75B_I2C_ADDRESS)) {
Rhyme 0:d895cd1cd897 479 printf("LM75B on I2C0 is present\n") ;
Rhyme 0:d895cd1cd897 480 lm75b1 = new LM75B(edge_i2c0, LM75B_I2C_ADDRESS) ;
Rhyme 0:d895cd1cd897 481 } else {
Rhyme 0:d895cd1cd897 482 printf("LM75B on I2C0 is absent\n") ;
Rhyme 0:d895cd1cd897 483 }
Rhyme 0:d895cd1cd897 484 #endif
Rhyme 0:d895cd1cd897 485 if (display) { /* press is present anyway */
Rhyme 0:d895cd1cd897 486 display->BusEnable(true) ;
Rhyme 0:d895cd1cd897 487 if (lm75b0) {
Rhyme 0:d895cd1cd897 488 display->locate(30, (num_sensor+1) * 30 + 40) ;
Rhyme 0:d895cd1cd897 489 } else {
Rhyme 0:d895cd1cd897 490 display->locate(30, num_sensor * 30 + 40) ;
Rhyme 0:d895cd1cd897 491 }
Rhyme 0:d895cd1cd897 492 display->printf("PRESS is present") ;
Rhyme 0:d895cd1cd897 493 display->BusEnable(false) ;
Rhyme 0:d895cd1cd897 494 }
Rhyme 0:d895cd1cd897 495 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 496 an0 = new AnalogIn(PIN_AN0) ;
Rhyme 0:d895cd1cd897 497 smtc502at0 = new SMTC502AT(an0) ;
Rhyme 0:d895cd1cd897 498 an1 = new AnalogIn(PIN_AN1) ;
Rhyme 0:d895cd1cd897 499 smtc502at1 = new SMTC502AT(an1) ;
Rhyme 0:d895cd1cd897 500 temp = new edge_temp(lm75b0, smtc502at0, smtc502at1, lm75b1) ;
Rhyme 0:d895cd1cd897 501 sensor[SENSOR_ID_TEMP] = temp ;
Rhyme 0:d895cd1cd897 502 sensor[SENSOR_ID_TEMP]->setId(SENSOR_ID_TEMP) ;
Rhyme 0:d895cd1cd897 503 num_sensor++ ;
Rhyme 0:d895cd1cd897 504
Rhyme 0:d895cd1cd897 505
Rhyme 0:d895cd1cd897 506 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 507 an2 = new AnalogIn(PIN_AN2) ;
Rhyme 0:d895cd1cd897 508 pse530_en = new DigitalOut(PIN_PRESS_EN, 0) ;
Rhyme 0:d895cd1cd897 509 pse530 = new PSE530(an2) ;
Rhyme 0:d895cd1cd897 510 pressure = new edge_pressure(pse530, pse530_en) ;
Rhyme 0:d895cd1cd897 511 sensor[SENSOR_ID_PRESS] = pressure ;
Rhyme 0:d895cd1cd897 512 sensor[SENSOR_ID_PRESS]->setId(SENSOR_ID_PRESS) ;
Rhyme 0:d895cd1cd897 513 num_sensor++ ;
Rhyme 0:d895cd1cd897 514
Rhyme 0:d895cd1cd897 515 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 516 if (num_sensor > 0) {
Rhyme 0:d895cd1cd897 517 printf("%d edge_sensor(s) registered\n", num_sensor) ;
Rhyme 0:d895cd1cd897 518 printf("Edge is waiting for ASR to link\n") ;
Rhyme 0:d895cd1cd897 519 if (display) {
Rhyme 0:d895cd1cd897 520 display->BusEnable(true) ;
Rhyme 0:d895cd1cd897 521 display->foreground(White) ;
Rhyme 0:d895cd1cd897 522 display->locate(40, 200) ;
Rhyme 0:d895cd1cd897 523 display->printf("Waiting for ASR") ;
Rhyme 0:d895cd1cd897 524 display->BusEnable(false) ;
Rhyme 0:d895cd1cd897 525 }
Rhyme 0:d895cd1cd897 526 }
Rhyme 0:d895cd1cd897 527 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 528 }
Rhyme 0:d895cd1cd897 529
Rhyme 0:d895cd1cd897 530 void enable_sensors(void)
Rhyme 0:d895cd1cd897 531 {
Rhyme 0:d895cd1cd897 532 int i ;
Rhyme 0:d895cd1cd897 533 for (i = 0 ; i < NUM_MAX_SENSOR ; i++ ) {
Rhyme 0:d895cd1cd897 534 if (sensor[i]) {
Rhyme 0:d895cd1cd897 535 sensor[i]->enable() ;
Rhyme 0:d895cd1cd897 536 }
Rhyme 0:d895cd1cd897 537 }
Rhyme 0:d895cd1cd897 538 }
Rhyme 0:d895cd1cd897 539
Rhyme 0:d895cd1cd897 540 void disable_sensors(void)
Rhyme 0:d895cd1cd897 541 {
Rhyme 0:d895cd1cd897 542 int i ;
Rhyme 0:d895cd1cd897 543 for (i = 0 ; i < NUM_MAX_SENSOR ; i++ ) {
Rhyme 0:d895cd1cd897 544 if (sensor[i]) {
Rhyme 0:d895cd1cd897 545 sensor[i]->disable() ;
Rhyme 0:d895cd1cd897 546 }
Rhyme 0:d895cd1cd897 547 }
Rhyme 0:d895cd1cd897 548 }
Rhyme 0:d895cd1cd897 549
Rhyme 0:d895cd1cd897 550 void reboot_edge(void)
Rhyme 0:d895cd1cd897 551 {
Rhyme 0:d895cd1cd897 552 int i ;
Rhyme 0:d895cd1cd897 553 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 554 disable_sensors() ;
Rhyme 0:d895cd1cd897 555 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 556 if (display) {
Rhyme 0:d895cd1cd897 557 delete display ;
Rhyme 0:d895cd1cd897 558 display = 0 ;
Rhyme 0:d895cd1cd897 559 }
Rhyme 0:d895cd1cd897 560 for (i = 0 ; i < NUM_MAX_SENSOR ; i++ ) {
Rhyme 0:d895cd1cd897 561 if (sensor[i]) {
Rhyme 0:d895cd1cd897 562 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 563 delete sensor[i] ;
Rhyme 0:d895cd1cd897 564 sensor[i] = 0 ;
Rhyme 0:d895cd1cd897 565 }
Rhyme 0:d895cd1cd897 566 }
Rhyme 0:d895cd1cd897 567 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 568 software_reset() ;
Rhyme 0:d895cd1cd897 569 }