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:
Rhyme
Date:
Tue Apr 03 08:30:29 2018 +0000
Revision:
0:d895cd1cd897
Initial I2C Pin force reset function added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 0:d895cd1cd897 1 #include "mbed.h"
Rhyme 0:d895cd1cd897 2 #include "vt100.h"
Rhyme 0:d895cd1cd897 3 #include "afLib.h"
Rhyme 0:d895cd1cd897 4 #include "af_mgr.h"
Rhyme 0:d895cd1cd897 5 #include "edge_mgr.h"
Rhyme 0:d895cd1cd897 6 #include "edge_time.h"
Rhyme 0:d895cd1cd897 7 #include "edge_reset_mgr.h"
Rhyme 0:d895cd1cd897 8 /**
Rhyme 0:d895cd1cd897 9 * afero poc1.5 25-Dec-2017 version
Rhyme 0:d895cd1cd897 10 * from this version, watch dog timer joined again.
Rhyme 0:d895cd1cd897 11 */
Rhyme 0:d895cd1cd897 12
Rhyme 0:d895cd1cd897 13 vt100 *tty = 0 ;
Rhyme 0:d895cd1cd897 14 uint32_t wait_tolerance = 500 ; /* 5sec */
Rhyme 0:d895cd1cd897 15 uint32_t connect_tolerance = 60 ; /* after 60 trials, reboot */
Rhyme 0:d895cd1cd897 16 uint32_t wait_count = 0 ;
Rhyme 0:d895cd1cd897 17 uint32_t connect_trial_count = 0 ;
Rhyme 0:d895cd1cd897 18
Rhyme 0:d895cd1cd897 19 /**
Rhyme 0:d895cd1cd897 20 * wait_connection
Rhyme 0:d895cd1cd897 21 * When gConnected == false, which is connection is lost.
Rhyme 0:d895cd1cd897 22 * Each 5sec check attribute ATTR_WIFI_STDY_STATE to see
Rhyme 0:d895cd1cd897 23 * if the connection has recovered.
Rhyme 0:d895cd1cd897 24 * Meantime even if connection is established communicated
Rhyme 0:d895cd1cd897 25 * data is invalid, so AF_SYSTEM_ASR_STATE is also
Rhyme 0:d895cd1cd897 26 * checked for gLinked ;
Rhyme 0:d895cd1cd897 27 * And in case connect_tolerance trials failed
Rhyme 0:d895cd1cd897 28 * try to reboot the system if it can improve the situation.
Rhyme 0:d895cd1cd897 29 */
Rhyme 0:d895cd1cd897 30 void wait_connection(void)
Rhyme 0:d895cd1cd897 31 {
Rhyme 0:d895cd1cd897 32 int result ;
Rhyme 0:d895cd1cd897 33 wait_count++ ;
Rhyme 0:d895cd1cd897 34 if (wait_count > wait_tolerance) {
Rhyme 0:d895cd1cd897 35 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 36 if (gConnected == false) {
Rhyme 0:d895cd1cd897 37 result = afero->getAttribute(ATTR_WIFI_STDY_STATE) ;
Rhyme 0:d895cd1cd897 38 if (result != afSUCCESS) {
Rhyme 0:d895cd1cd897 39 print_af_error(result) ;
Rhyme 0:d895cd1cd897 40 }
Rhyme 0:d895cd1cd897 41 }
Rhyme 0:d895cd1cd897 42 if (gLinked == false) {
Rhyme 0:d895cd1cd897 43 result = afero->getAttribute(AF_SYSTEM_ASR_STATE) ;
Rhyme 0:d895cd1cd897 44 if (result != afSUCCESS) {
Rhyme 0:d895cd1cd897 45 print_af_error(result) ;
Rhyme 0:d895cd1cd897 46 }
Rhyme 0:d895cd1cd897 47 }
Rhyme 0:d895cd1cd897 48 connect_trial_count++ ;
Rhyme 0:d895cd1cd897 49 if (connect_trial_count > connect_tolerance) {
Rhyme 0:d895cd1cd897 50 reboot_edge() ;
Rhyme 0:d895cd1cd897 51 }
Rhyme 0:d895cd1cd897 52 wait_count = 0 ;
Rhyme 0:d895cd1cd897 53 }
Rhyme 0:d895cd1cd897 54 }
Rhyme 0:d895cd1cd897 55
Rhyme 0:d895cd1cd897 56 void init_hardware(void)
Rhyme 0:d895cd1cd897 57 {
Rhyme 0:d895cd1cd897 58 int i ;
Rhyme 0:d895cd1cd897 59 int result ;
Rhyme 0:d895cd1cd897 60
Rhyme 0:d895cd1cd897 61 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 62 init_display() ;
Rhyme 0:d895cd1cd897 63 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 64 init_aflib() ;
Rhyme 0:d895cd1cd897 65 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 66 init_sensors() ;
Rhyme 0:d895cd1cd897 67 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 68 init_timer() ;
Rhyme 0:d895cd1cd897 69
Rhyme 0:d895cd1cd897 70 while(true) {
Rhyme 0:d895cd1cd897 71 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 72 for (i = 0 ; i < 10 ; i++ ) {
Rhyme 0:d895cd1cd897 73 afero->loop() ;
Rhyme 0:d895cd1cd897 74 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 75 }
Rhyme 0:d895cd1cd897 76 if ((gLinked == true)&&(gConnected == true)) {
Rhyme 0:d895cd1cd897 77 wait_count = 0 ;
Rhyme 0:d895cd1cd897 78 connect_trial_count = 0 ;
Rhyme 0:d895cd1cd897 79 if (afero->isIdle()) {
Rhyme 0:d895cd1cd897 80 result = init_edge_attribute() ;
Rhyme 0:d895cd1cd897 81 if (result == 0) {
Rhyme 0:d895cd1cd897 82 break ;
Rhyme 0:d895cd1cd897 83 }
Rhyme 0:d895cd1cd897 84 }
Rhyme 0:d895cd1cd897 85 } else { /* gLinked == false */
Rhyme 0:d895cd1cd897 86 wait_connection() ;
Rhyme 0:d895cd1cd897 87 }
Rhyme 0:d895cd1cd897 88 wait_ms(10) ;
Rhyme 0:d895cd1cd897 89 }
Rhyme 0:d895cd1cd897 90 do {
Rhyme 0:d895cd1cd897 91 // while(!afero->isIdle()) {
Rhyme 0:d895cd1cd897 92 reset_watch_dog() ;
Rhyme 0:d895cd1cd897 93 for (i = 0 ; i < 10 ; i++ ) {
Rhyme 0:d895cd1cd897 94 afero->loop() ;
Rhyme 0:d895cd1cd897 95 wait_ms(100) ;
Rhyme 0:d895cd1cd897 96 }
Rhyme 0:d895cd1cd897 97 } while(!afero->isIdle()) ;
Rhyme 0:d895cd1cd897 98 edge_mgr_status = EDGE_MGR_RUNNING ;
Rhyme 0:d895cd1cd897 99 }
Rhyme 0:d895cd1cd897 100
Rhyme 0:d895cd1cd897 101 // main() runs in its own thread in the OS
Rhyme 0:d895cd1cd897 102 int main() {
Rhyme 0:d895cd1cd897 103 static uint32_t count_robin = 0 ;
Rhyme 0:d895cd1cd897 104
Rhyme 0:d895cd1cd897 105 tty = new vt100() ;
Rhyme 0:d895cd1cd897 106 // tty->cls() ;
Rhyme 0:d895cd1cd897 107 printf("Afero test program (ver. %s) started\n", __DATE__) ;
Rhyme 0:d895cd1cd897 108 printf("=== Reset Reason ===\n") ;
Rhyme 0:d895cd1cd897 109 print_reset_reason() ;
Rhyme 0:d895cd1cd897 110 printf("====================\n") ;
Rhyme 0:d895cd1cd897 111
Rhyme 0:d895cd1cd897 112 init_hardware() ;
Rhyme 0:d895cd1cd897 113
Rhyme 0:d895cd1cd897 114 edge_splash() ;
Rhyme 0:d895cd1cd897 115
Rhyme 0:d895cd1cd897 116 while (true) {
Rhyme 0:d895cd1cd897 117 count_robin++ ;
Rhyme 0:d895cd1cd897 118 afero->loop() ;
Rhyme 0:d895cd1cd897 119 if ((gLinked == true)&&(gConnected == true)) {
Rhyme 0:d895cd1cd897 120 wait_count = 0 ;
Rhyme 0:d895cd1cd897 121 connect_trial_count = 0 ;
Rhyme 0:d895cd1cd897 122 if (afero->isIdle()) {
Rhyme 0:d895cd1cd897 123 edge_loop(count_robin) ;
Rhyme 0:d895cd1cd897 124 }
Rhyme 0:d895cd1cd897 125 } else { /* gLinked == false */
Rhyme 0:d895cd1cd897 126 wait_connection() ;
Rhyme 0:d895cd1cd897 127 }
Rhyme 0:d895cd1cd897 128 wait_ms(10) ;
Rhyme 0:d895cd1cd897 129 }
Rhyme 0:d895cd1cd897 130 }