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) 反転させることにより、疑似リセットクロックを生成します。
その後は、通常の起動手順に復帰し、以降はこれまでと同様の動作をします。
edge_sensor/edge_accel.cpp@2:de22987be9ba, 2018-06-18 (annotated)
- Committer:
- gaku_miyagawa
- Date:
- Mon Jun 18 02:55:38 2018 +0000
- Revision:
- 2:de22987be9ba
- Parent:
- 0:d895cd1cd897
SBU SPECIAL
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rhyme | 0:d895cd1cd897 | 1 | #include "mbed.h" |
Rhyme | 0:d895cd1cd897 | 2 | #include "afLib.h" |
Rhyme | 0:d895cd1cd897 | 3 | #include "edge_reset_mgr.h" |
Rhyme | 0:d895cd1cd897 | 4 | #include "edge_sensor.h" |
Rhyme | 0:d895cd1cd897 | 5 | #include "edge_accel.h" |
Rhyme | 0:d895cd1cd897 | 6 | #include "MMA8451Q.h" |
Rhyme | 0:d895cd1cd897 | 7 | |
Rhyme | 0:d895cd1cd897 | 8 | edge_accel::edge_accel(MMA8451Q *accel) : edge_sensor() |
Rhyme | 0:d895cd1cd897 | 9 | { |
Rhyme | 0:d895cd1cd897 | 10 | _accel = accel ; |
Rhyme | 0:d895cd1cd897 | 11 | _sample_count = 0 ; |
Rhyme | 0:d895cd1cd897 | 12 | _accumulation = 0 ; |
Rhyme | 0:d895cd1cd897 | 13 | _prev_x = 0 ; |
Rhyme | 0:d895cd1cd897 | 14 | _prev_y = 0 ; |
Rhyme | 0:d895cd1cd897 | 15 | _prev_z = 0 ; |
Rhyme | 0:d895cd1cd897 | 16 | |
Rhyme | 0:d895cd1cd897 | 17 | _interval = 30 ; |
Rhyme | 0:d895cd1cd897 | 18 | } |
Rhyme | 0:d895cd1cd897 | 19 | |
Rhyme | 0:d895cd1cd897 | 20 | edge_accel::~edge_accel(void) |
Rhyme | 0:d895cd1cd897 | 21 | { |
Rhyme | 0:d895cd1cd897 | 22 | delete _accel ; |
Rhyme | 0:d895cd1cd897 | 23 | } |
Rhyme | 0:d895cd1cd897 | 24 | |
Rhyme | 0:d895cd1cd897 | 25 | void edge_accel::reset(void) |
Rhyme | 0:d895cd1cd897 | 26 | { |
Rhyme | 0:d895cd1cd897 | 27 | clear_value() ; |
Rhyme | 0:d895cd1cd897 | 28 | edge_sensor::reset() ; |
Rhyme | 0:d895cd1cd897 | 29 | } |
Rhyme | 0:d895cd1cd897 | 30 | |
Rhyme | 0:d895cd1cd897 | 31 | #if 0 |
Rhyme | 0:d895cd1cd897 | 32 | void edge_accel::prepare(void) |
Rhyme | 0:d895cd1cd897 | 33 | { |
Rhyme | 0:d895cd1cd897 | 34 | // printf("accel prepare\n") ; |
Rhyme | 0:d895cd1cd897 | 35 | } |
Rhyme | 0:d895cd1cd897 | 36 | #endif |
Rhyme | 0:d895cd1cd897 | 37 | |
Rhyme | 0:d895cd1cd897 | 38 | int edge_accel::sample(void) |
Rhyme | 0:d895cd1cd897 | 39 | { |
Rhyme | 0:d895cd1cd897 | 40 | int result ; |
Rhyme | 0:d895cd1cd897 | 41 | float theValue = 0.0 ; |
Rhyme | 0:d895cd1cd897 | 42 | if (_sample_count > 1) { /* if sample is 1 or less, no data */ |
Rhyme | 0:d895cd1cd897 | 43 | _num_sampled = _sample_count - 1 ; |
Rhyme | 0:d895cd1cd897 | 44 | theValue = (float)_accumulation / (float)(_num_sampled) ; |
Rhyme | 0:d895cd1cd897 | 45 | result = 0 ; /* success */ |
Rhyme | 0:d895cd1cd897 | 46 | } else { |
Rhyme | 0:d895cd1cd897 | 47 | result = 1 ; /* fail! */ |
Rhyme | 0:d895cd1cd897 | 48 | } |
Rhyme | 0:d895cd1cd897 | 49 | _value = theValue / 4096.0 ; |
Rhyme | 0:d895cd1cd897 | 50 | _sampled_time = edge_time ; |
Rhyme | 0:d895cd1cd897 | 51 | _sample_count = 0 ; |
Rhyme | 0:d895cd1cd897 | 52 | _accumulation = 0 ; |
Rhyme | 0:d895cd1cd897 | 53 | return( result ) ; |
Rhyme | 0:d895cd1cd897 | 54 | } |
Rhyme | 0:d895cd1cd897 | 55 | |
Rhyme | 0:d895cd1cd897 | 56 | int edge_accel::deliver(void) |
Rhyme | 0:d895cd1cd897 | 57 | { |
Rhyme | 0:d895cd1cd897 | 58 | int result ; |
Rhyme | 0:d895cd1cd897 | 59 | char timestr[16] ; |
Rhyme | 0:d895cd1cd897 | 60 | |
Rhyme | 0:d895cd1cd897 | 61 | print_time(_sampled_time) ; |
Rhyme | 0:d895cd1cd897 | 62 | // _value = get_value() ; |
Rhyme | 0:d895cd1cd897 | 63 | printf(" accel: %.3f [%d samples]\n", _value, _num_sampled) ; |
Rhyme | 0:d895cd1cd897 | 64 | time2seq(_sampled_time, timestr) ; |
Rhyme | 0:d895cd1cd897 | 65 | sprintf(_str_buf, |
Rhyme | 0:d895cd1cd897 | 66 | "{\"DEVICE\":\"ACCEL\",\"PN\":\"MMA8451Q\",\"VAL_X\":\"%.3f\",\"VAL_Y\":\"0\",\"VAL_Z\":\"0\",\"UNIT\":\"g\",\"T\":\"%s\",\"E\":\"%d\"}", |
Rhyme | 0:d895cd1cd897 | 67 | _value, timestr, _error_count) ; |
Rhyme | 0:d895cd1cd897 | 68 | result = afero->setAttribute(1, _str_buf) ; |
Rhyme | 0:d895cd1cd897 | 69 | |
Rhyme | 0:d895cd1cd897 | 70 | return( result == afSUCCESS ) ; |
Rhyme | 0:d895cd1cd897 | 71 | } |
Rhyme | 0:d895cd1cd897 | 72 | |
Rhyme | 0:d895cd1cd897 | 73 | int accel_v2y(float value, edge_chart_type *p) |
Rhyme | 0:d895cd1cd897 | 74 | { |
Rhyme | 0:d895cd1cd897 | 75 | int y ; |
Rhyme | 0:d895cd1cd897 | 76 | if (value < p->min) { |
Rhyme | 0:d895cd1cd897 | 77 | value = p->min ; |
Rhyme | 0:d895cd1cd897 | 78 | } else if (value > p->max) { |
Rhyme | 0:d895cd1cd897 | 79 | value = p->max ; |
Rhyme | 0:d895cd1cd897 | 80 | } |
Rhyme | 0:d895cd1cd897 | 81 | y = p->top + p->height/2 - 1 |
Rhyme | 0:d895cd1cd897 | 82 | - (int)((p->height - 2) * value /(p->max - p->min)) ; |
Rhyme | 0:d895cd1cd897 | 83 | return( y ) ; |
Rhyme | 0:d895cd1cd897 | 84 | } |
Rhyme | 0:d895cd1cd897 | 85 | |
Rhyme | 0:d895cd1cd897 | 86 | void edge_accel::show(void) |
Rhyme | 0:d895cd1cd897 | 87 | { |
Rhyme | 0:d895cd1cd897 | 88 | int x, y ; |
Rhyme | 0:d895cd1cd897 | 89 | edge_chart_type *p = &edge_chart[0] ; /* edge_chart for accel */ |
Rhyme | 0:d895cd1cd897 | 90 | reset_watch_dog() ; |
Rhyme | 0:d895cd1cd897 | 91 | if (display) { |
Rhyme | 0:d895cd1cd897 | 92 | switch(display_mode) { |
Rhyme | 0:d895cd1cd897 | 93 | case DISPLAY_MODE_SUMMARY: |
Rhyme | 0:d895cd1cd897 | 94 | display->BusEnable(true) ; |
Rhyme | 0:d895cd1cd897 | 95 | display->set_font((unsigned char*) Arial12x12); |
Rhyme | 0:d895cd1cd897 | 96 | display->set_font_zoom(2, 2) ; |
Rhyme | 0:d895cd1cd897 | 97 | display->foreground(White) ; |
Rhyme | 0:d895cd1cd897 | 98 | display->locate(EDGE_SUMMARY_X, EDGE_SUMMARY_TIME_Y) ; |
Rhyme | 0:d895cd1cd897 | 99 | displayTime(_sampled_time) ; |
Rhyme | 0:d895cd1cd897 | 100 | // display->locate(10, 5) ; |
Rhyme | 0:d895cd1cd897 | 101 | // display->printf(timestr) ; |
Rhyme | 0:d895cd1cd897 | 102 | display->locate(EDGE_SUMMARY_X, EDGE_SUMMARY_ACCEL_Y) ; |
Rhyme | 0:d895cd1cd897 | 103 | display->printf("Accel: %.3f [%4d]", _value, _num_sampled) ; |
Rhyme | 0:d895cd1cd897 | 104 | display->BusEnable(false) ; |
Rhyme | 0:d895cd1cd897 | 105 | reset_watch_dog() ; |
Rhyme | 0:d895cd1cd897 | 106 | break ; |
Rhyme | 0:d895cd1cd897 | 107 | case DISPLAY_MODE_CHART: |
Rhyme | 0:d895cd1cd897 | 108 | x = p->left + p->index + 1; |
Rhyme | 0:d895cd1cd897 | 109 | y = accel_v2y(_value, p) ; |
Rhyme | 0:d895cd1cd897 | 110 | display->BusEnable(true) ; |
Rhyme | 0:d895cd1cd897 | 111 | if (p->index == 0) { |
Rhyme | 0:d895cd1cd897 | 112 | draw_chart_frame(p) ; |
Rhyme | 0:d895cd1cd897 | 113 | } |
Rhyme | 0:d895cd1cd897 | 114 | display->pixel(x, y, White) ; |
Rhyme | 0:d895cd1cd897 | 115 | display->BusEnable(false) ; |
Rhyme | 0:d895cd1cd897 | 116 | p->index = (p->index + 1) % (p->width - 2) ; |
Rhyme | 0:d895cd1cd897 | 117 | break ; |
Rhyme | 0:d895cd1cd897 | 118 | default: |
Rhyme | 0:d895cd1cd897 | 119 | break ; |
Rhyme | 0:d895cd1cd897 | 120 | } |
Rhyme | 0:d895cd1cd897 | 121 | } |
Rhyme | 0:d895cd1cd897 | 122 | clear_value() ; |
Rhyme | 0:d895cd1cd897 | 123 | reset_watch_dog() ; |
Rhyme | 0:d895cd1cd897 | 124 | } |
Rhyme | 0:d895cd1cd897 | 125 | |
Rhyme | 0:d895cd1cd897 | 126 | int edge_accel::accum(void) |
Rhyme | 0:d895cd1cd897 | 127 | { |
Rhyme | 0:d895cd1cd897 | 128 | int result ; |
Rhyme | 0:d895cd1cd897 | 129 | int16_t value[3] ; |
Rhyme | 0:d895cd1cd897 | 130 | |
Rhyme | 0:d895cd1cd897 | 131 | if (_enable) { |
Rhyme | 0:d895cd1cd897 | 132 | result = _accel->getAllRawData(value) ; |
Rhyme | 0:d895cd1cd897 | 133 | |
Rhyme | 0:d895cd1cd897 | 134 | if (result == 0) { /* success */ |
Rhyme | 0:d895cd1cd897 | 135 | if (_sample_count != 0) { /* first data does not have prev_data */ |
Rhyme | 0:d895cd1cd897 | 136 | _accumulation += |
Rhyme | 0:d895cd1cd897 | 137 | abs(_prev_x - value[0]) |
Rhyme | 0:d895cd1cd897 | 138 | + abs(_prev_y - value[1]) |
Rhyme | 0:d895cd1cd897 | 139 | + abs(_prev_z - value[2]) ; |
Rhyme | 0:d895cd1cd897 | 140 | } |
Rhyme | 0:d895cd1cd897 | 141 | |
Rhyme | 0:d895cd1cd897 | 142 | _sample_count++ ; |
Rhyme | 0:d895cd1cd897 | 143 | |
Rhyme | 0:d895cd1cd897 | 144 | _prev_x = value[0] ; |
Rhyme | 0:d895cd1cd897 | 145 | _prev_y = value[1] ; |
Rhyme | 0:d895cd1cd897 | 146 | _prev_z = value[2] ; |
Rhyme | 0:d895cd1cd897 | 147 | } |
Rhyme | 0:d895cd1cd897 | 148 | } |
Rhyme | 0:d895cd1cd897 | 149 | |
Rhyme | 0:d895cd1cd897 | 150 | return( result ) ; |
Rhyme | 0:d895cd1cd897 | 151 | } |
Rhyme | 0:d895cd1cd897 | 152 | |
Rhyme | 0:d895cd1cd897 | 153 | void edge_accel::clear_value(void) |
Rhyme | 0:d895cd1cd897 | 154 | { |
Rhyme | 0:d895cd1cd897 | 155 | _sample_count = 0 ; |
Rhyme | 0:d895cd1cd897 | 156 | _accumulation = 0 ; |
Rhyme | 0:d895cd1cd897 | 157 | _prev_x = 0 ; |
Rhyme | 0:d895cd1cd897 | 158 | _prev_y = 0 ; |
Rhyme | 0:d895cd1cd897 | 159 | _prev_z = 0 ; |
Rhyme | 0:d895cd1cd897 | 160 | } |
Rhyme | 0:d895cd1cd897 | 161 | |
Rhyme | 0:d895cd1cd897 | 162 | |
Rhyme | 0:d895cd1cd897 | 163 |