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) 反転させることにより、疑似リセットクロックを生成します。
その後は、通常の起動手順に復帰し、以降はこれまでと同様の動作をします。
afLib/Command.cpp@0:d895cd1cd897, 2018-04-03 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rhyme | 0:d895cd1cd897 | 1 | /** |
Rhyme | 0:d895cd1cd897 | 2 | * Copyright 2015 Afero, Inc. |
Rhyme | 0:d895cd1cd897 | 3 | * |
Rhyme | 0:d895cd1cd897 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Rhyme | 0:d895cd1cd897 | 5 | * you may not use this file except in compliance with the License. |
Rhyme | 0:d895cd1cd897 | 6 | * You may obtain a copy of the License at |
Rhyme | 0:d895cd1cd897 | 7 | * |
Rhyme | 0:d895cd1cd897 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Rhyme | 0:d895cd1cd897 | 9 | * |
Rhyme | 0:d895cd1cd897 | 10 | * Unless required by applicable law or agreed to in writing, software |
Rhyme | 0:d895cd1cd897 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Rhyme | 0:d895cd1cd897 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Rhyme | 0:d895cd1cd897 | 13 | * See the License for the specific language governing permissions and |
Rhyme | 0:d895cd1cd897 | 14 | * limitations under the License. |
Rhyme | 0:d895cd1cd897 | 15 | */ |
Rhyme | 0:d895cd1cd897 | 16 | |
Rhyme | 0:d895cd1cd897 | 17 | //wsugi #include "Arduino.h" |
Rhyme | 0:d895cd1cd897 | 18 | #include "mbed.h" |
Rhyme | 0:d895cd1cd897 | 19 | #include <stdio.h> |
Rhyme | 0:d895cd1cd897 | 20 | #include "Command.h" |
Rhyme | 0:d895cd1cd897 | 21 | #include "msg_types.h" |
Rhyme | 0:d895cd1cd897 | 22 | |
Rhyme | 0:d895cd1cd897 | 23 | #define SERIAL_PRINT_DBG_ASR_ON 0 |
Rhyme | 0:d895cd1cd897 | 24 | |
Rhyme | 0:d895cd1cd897 | 25 | #define CMD_HDR_LEN 4 // 4 byte header on all commands |
Rhyme | 0:d895cd1cd897 | 26 | #define CMD_VAL_LEN 2 // 2 byte value length for commands that have a value |
Rhyme | 0:d895cd1cd897 | 27 | |
Rhyme | 0:d895cd1cd897 | 28 | const char *CMD_NAMES[] = {"SET ", "GET ", "UPDATE"}; |
Rhyme | 0:d895cd1cd897 | 29 | |
Rhyme | 0:d895cd1cd897 | 30 | |
Rhyme | 0:d895cd1cd897 | 31 | Command::Command(uint16_t len, uint8_t *bytes) { |
Rhyme | 0:d895cd1cd897 | 32 | int index = 0; |
Rhyme | 0:d895cd1cd897 | 33 | |
Rhyme | 0:d895cd1cd897 | 34 | _cmd = bytes[index++]; |
Rhyme | 0:d895cd1cd897 | 35 | _requestId = bytes[index++]; |
Rhyme | 0:d895cd1cd897 | 36 | _attrId = bytes[index + 0] | bytes[index + 1] << 8; |
Rhyme | 0:d895cd1cd897 | 37 | index += 2; |
Rhyme | 0:d895cd1cd897 | 38 | |
Rhyme | 0:d895cd1cd897 | 39 | if (_cmd == MSG_TYPE_GET) { |
Rhyme | 0:d895cd1cd897 | 40 | return; |
Rhyme | 0:d895cd1cd897 | 41 | } |
Rhyme | 0:d895cd1cd897 | 42 | if (_cmd == MSG_TYPE_UPDATE) { |
Rhyme | 0:d895cd1cd897 | 43 | _status = bytes[index++]; |
Rhyme | 0:d895cd1cd897 | 44 | _reason = bytes[index++]; |
Rhyme | 0:d895cd1cd897 | 45 | } |
Rhyme | 0:d895cd1cd897 | 46 | |
Rhyme | 0:d895cd1cd897 | 47 | _valueLen = bytes[index + 0] | bytes[index + 1] << 8; |
Rhyme | 0:d895cd1cd897 | 48 | index += 2; |
Rhyme | 0:d895cd1cd897 | 49 | _value = new uint8_t[_valueLen]; |
Rhyme | 0:d895cd1cd897 | 50 | for (int i = 0; i < _valueLen; i++) { |
Rhyme | 0:d895cd1cd897 | 51 | _value[i] = bytes[index + i]; |
Rhyme | 0:d895cd1cd897 | 52 | } |
Rhyme | 0:d895cd1cd897 | 53 | } |
Rhyme | 0:d895cd1cd897 | 54 | |
Rhyme | 0:d895cd1cd897 | 55 | Command::Command(uint8_t requestId, const char *str) { |
Rhyme | 0:d895cd1cd897 | 56 | _requestId = requestId & 0xff; |
Rhyme | 0:d895cd1cd897 | 57 | |
Rhyme | 0:d895cd1cd897 | 58 | char *cp; //wsugi = strdup(str); |
Rhyme | 0:d895cd1cd897 | 59 | //wsugi 00 start |
Rhyme | 0:d895cd1cd897 | 60 | { |
Rhyme | 0:d895cd1cd897 | 61 | int length = strlen(str)+1; |
Rhyme | 0:d895cd1cd897 | 62 | cp = (char*)malloc(length); |
Rhyme | 0:d895cd1cd897 | 63 | strcpy(cp,str); |
Rhyme | 0:d895cd1cd897 | 64 | } |
Rhyme | 0:d895cd1cd897 | 65 | //wsugi 00 end |
Rhyme | 0:d895cd1cd897 | 66 | char *tok = strtok(cp, " "); |
Rhyme | 0:d895cd1cd897 | 67 | _cmd = strToCmd(tok); |
Rhyme | 0:d895cd1cd897 | 68 | |
Rhyme | 0:d895cd1cd897 | 69 | tok = strtok(NULL, " "); |
Rhyme | 0:d895cd1cd897 | 70 | _attrId = strToAttrId(tok); |
Rhyme | 0:d895cd1cd897 | 71 | |
Rhyme | 0:d895cd1cd897 | 72 | if (_cmd == MSG_TYPE_GET) { |
Rhyme | 0:d895cd1cd897 | 73 | _valueLen = 0; |
Rhyme | 0:d895cd1cd897 | 74 | _value = NULL; |
Rhyme | 0:d895cd1cd897 | 75 | } else { |
Rhyme | 0:d895cd1cd897 | 76 | tok = strtok(NULL, " "); |
Rhyme | 0:d895cd1cd897 | 77 | _valueLen = strlen(tok) / 2; |
Rhyme | 0:d895cd1cd897 | 78 | _value = new uint8_t[_valueLen]; |
Rhyme | 0:d895cd1cd897 | 79 | strToValue(tok, _value); |
Rhyme | 0:d895cd1cd897 | 80 | } |
Rhyme | 0:d895cd1cd897 | 81 | |
Rhyme | 0:d895cd1cd897 | 82 | free(cp); |
Rhyme | 0:d895cd1cd897 | 83 | } |
Rhyme | 0:d895cd1cd897 | 84 | |
Rhyme | 0:d895cd1cd897 | 85 | Command::Command(uint8_t requestId, uint8_t cmd, uint16_t attrId) { |
Rhyme | 0:d895cd1cd897 | 86 | _requestId = requestId; |
Rhyme | 0:d895cd1cd897 | 87 | _cmd = cmd; |
Rhyme | 0:d895cd1cd897 | 88 | _attrId = attrId; |
Rhyme | 0:d895cd1cd897 | 89 | _valueLen = 0; |
Rhyme | 0:d895cd1cd897 | 90 | _value = NULL; |
Rhyme | 0:d895cd1cd897 | 91 | } |
Rhyme | 0:d895cd1cd897 | 92 | |
Rhyme | 0:d895cd1cd897 | 93 | Command::Command(uint8_t requestId, uint8_t cmd, uint16_t attrId, uint16_t valueLen, uint8_t *value) { |
Rhyme | 0:d895cd1cd897 | 94 | _requestId = requestId; |
Rhyme | 0:d895cd1cd897 | 95 | _cmd = cmd; |
Rhyme | 0:d895cd1cd897 | 96 | _attrId = attrId; |
Rhyme | 0:d895cd1cd897 | 97 | _valueLen = valueLen; |
Rhyme | 0:d895cd1cd897 | 98 | _value = new uint8_t[_valueLen]; |
Rhyme | 0:d895cd1cd897 | 99 | memcpy(_value, value, valueLen); |
Rhyme | 0:d895cd1cd897 | 100 | } |
Rhyme | 0:d895cd1cd897 | 101 | |
Rhyme | 0:d895cd1cd897 | 102 | Command::Command(uint8_t requestId, uint8_t cmd, uint16_t attrId, uint8_t status, uint8_t reason, uint16_t valueLen, |
Rhyme | 0:d895cd1cd897 | 103 | uint8_t *value) { |
Rhyme | 0:d895cd1cd897 | 104 | _requestId = requestId; |
Rhyme | 0:d895cd1cd897 | 105 | _cmd = cmd; |
Rhyme | 0:d895cd1cd897 | 106 | _attrId = attrId; |
Rhyme | 0:d895cd1cd897 | 107 | _status = status; |
Rhyme | 0:d895cd1cd897 | 108 | _reason = reason; |
Rhyme | 0:d895cd1cd897 | 109 | _valueLen = valueLen; |
Rhyme | 0:d895cd1cd897 | 110 | _value = new uint8_t[_valueLen]; |
Rhyme | 0:d895cd1cd897 | 111 | memcpy(_value, value, valueLen); |
Rhyme | 0:d895cd1cd897 | 112 | } |
Rhyme | 0:d895cd1cd897 | 113 | |
Rhyme | 0:d895cd1cd897 | 114 | Command::Command() { |
Rhyme | 0:d895cd1cd897 | 115 | } |
Rhyme | 0:d895cd1cd897 | 116 | |
Rhyme | 0:d895cd1cd897 | 117 | Command::~Command() { |
Rhyme | 0:d895cd1cd897 | 118 | if (_value != NULL) { |
Rhyme | 0:d895cd1cd897 | 119 | delete[] _value; //wsugi delete (_value); |
Rhyme | 0:d895cd1cd897 | 120 | } |
Rhyme | 0:d895cd1cd897 | 121 | } |
Rhyme | 0:d895cd1cd897 | 122 | |
Rhyme | 0:d895cd1cd897 | 123 | int Command::strToValue(char *valueStr, uint8_t *value) { |
Rhyme | 0:d895cd1cd897 | 124 | for (int i = 0; i < (int) (strlen(valueStr) / 2); i++) { |
Rhyme | 0:d895cd1cd897 | 125 | int j = i * 2; |
Rhyme | 0:d895cd1cd897 | 126 | value[i] = getVal(valueStr[j + 1]) + (getVal(valueStr[j]) << 4); |
Rhyme | 0:d895cd1cd897 | 127 | } |
Rhyme | 0:d895cd1cd897 | 128 | |
Rhyme | 0:d895cd1cd897 | 129 | return 0; |
Rhyme | 0:d895cd1cd897 | 130 | } |
Rhyme | 0:d895cd1cd897 | 131 | |
Rhyme | 0:d895cd1cd897 | 132 | uint16_t Command::strToAttrId(char *attrIdStr) { |
Rhyme | 0:d895cd1cd897 | 133 | return atoi(attrIdStr); |
Rhyme | 0:d895cd1cd897 | 134 | //return String(attrIdStr).toInt(); |
Rhyme | 0:d895cd1cd897 | 135 | } |
Rhyme | 0:d895cd1cd897 | 136 | |
Rhyme | 0:d895cd1cd897 | 137 | uint8_t Command::strToCmd(char *cmdStr) { |
Rhyme | 0:d895cd1cd897 | 138 | char c = cmdStr[0]; |
Rhyme | 0:d895cd1cd897 | 139 | if (c == 'g' || c == 'G') { |
Rhyme | 0:d895cd1cd897 | 140 | return MSG_TYPE_GET; |
Rhyme | 0:d895cd1cd897 | 141 | } else if (c == 's' || c == 'S') { |
Rhyme | 0:d895cd1cd897 | 142 | return MSG_TYPE_SET; |
Rhyme | 0:d895cd1cd897 | 143 | } else if (c == 'u' || c == 'U') { |
Rhyme | 0:d895cd1cd897 | 144 | return MSG_TYPE_UPDATE; |
Rhyme | 0:d895cd1cd897 | 145 | } |
Rhyme | 0:d895cd1cd897 | 146 | |
Rhyme | 0:d895cd1cd897 | 147 | return 0xFF ; |
Rhyme | 0:d895cd1cd897 | 148 | } |
Rhyme | 0:d895cd1cd897 | 149 | |
Rhyme | 0:d895cd1cd897 | 150 | uint8_t Command::getCommand() { |
Rhyme | 0:d895cd1cd897 | 151 | return _cmd; |
Rhyme | 0:d895cd1cd897 | 152 | } |
Rhyme | 0:d895cd1cd897 | 153 | |
Rhyme | 0:d895cd1cd897 | 154 | uint8_t Command::getReqId() { |
Rhyme | 0:d895cd1cd897 | 155 | return _requestId; |
Rhyme | 0:d895cd1cd897 | 156 | } |
Rhyme | 0:d895cd1cd897 | 157 | |
Rhyme | 0:d895cd1cd897 | 158 | uint16_t Command::getAttrId() { |
Rhyme | 0:d895cd1cd897 | 159 | return _attrId; |
Rhyme | 0:d895cd1cd897 | 160 | } |
Rhyme | 0:d895cd1cd897 | 161 | |
Rhyme | 0:d895cd1cd897 | 162 | uint16_t Command::getValueLen() { |
Rhyme | 0:d895cd1cd897 | 163 | return _valueLen; |
Rhyme | 0:d895cd1cd897 | 164 | } |
Rhyme | 0:d895cd1cd897 | 165 | |
Rhyme | 0:d895cd1cd897 | 166 | void Command::getValue(uint8_t *value) { |
Rhyme | 0:d895cd1cd897 | 167 | for (int i = 0; i < _valueLen; i++) { |
Rhyme | 0:d895cd1cd897 | 168 | value[i] = _value[i]; |
Rhyme | 0:d895cd1cd897 | 169 | } |
Rhyme | 0:d895cd1cd897 | 170 | } |
Rhyme | 0:d895cd1cd897 | 171 | |
Rhyme | 0:d895cd1cd897 | 172 | uint8_t *Command::getValueP() { |
Rhyme | 0:d895cd1cd897 | 173 | return _value; |
Rhyme | 0:d895cd1cd897 | 174 | } |
Rhyme | 0:d895cd1cd897 | 175 | |
Rhyme | 0:d895cd1cd897 | 176 | uint16_t Command::getSize() { |
Rhyme | 0:d895cd1cd897 | 177 | uint16_t len = CMD_HDR_LEN; |
Rhyme | 0:d895cd1cd897 | 178 | |
Rhyme | 0:d895cd1cd897 | 179 | if (_cmd != MSG_TYPE_GET) { |
Rhyme | 0:d895cd1cd897 | 180 | len += CMD_VAL_LEN + _valueLen; |
Rhyme | 0:d895cd1cd897 | 181 | } |
Rhyme | 0:d895cd1cd897 | 182 | |
Rhyme | 0:d895cd1cd897 | 183 | if (_cmd == MSG_TYPE_UPDATE) { |
Rhyme | 0:d895cd1cd897 | 184 | len += 2; // status byte + reason byte |
Rhyme | 0:d895cd1cd897 | 185 | } |
Rhyme | 0:d895cd1cd897 | 186 | |
Rhyme | 0:d895cd1cd897 | 187 | return len; |
Rhyme | 0:d895cd1cd897 | 188 | } |
Rhyme | 0:d895cd1cd897 | 189 | |
Rhyme | 0:d895cd1cd897 | 190 | uint16_t Command::getBytes(uint8_t *bytes) { |
Rhyme | 0:d895cd1cd897 | 191 | uint16_t len = getSize(); |
Rhyme | 0:d895cd1cd897 | 192 | |
Rhyme | 0:d895cd1cd897 | 193 | int index = 0; |
Rhyme | 0:d895cd1cd897 | 194 | |
Rhyme | 0:d895cd1cd897 | 195 | bytes[index++] = (_cmd); |
Rhyme | 0:d895cd1cd897 | 196 | |
Rhyme | 0:d895cd1cd897 | 197 | bytes[index++] = (_requestId); |
Rhyme | 0:d895cd1cd897 | 198 | |
Rhyme | 0:d895cd1cd897 | 199 | bytes[index++] = (_attrId & 0xff); |
Rhyme | 0:d895cd1cd897 | 200 | bytes[index++] = ((_attrId >> 8) & 0xff); |
Rhyme | 0:d895cd1cd897 | 201 | |
Rhyme | 0:d895cd1cd897 | 202 | if (_cmd == MSG_TYPE_GET) { |
Rhyme | 0:d895cd1cd897 | 203 | return len; |
Rhyme | 0:d895cd1cd897 | 204 | } |
Rhyme | 0:d895cd1cd897 | 205 | |
Rhyme | 0:d895cd1cd897 | 206 | if (_cmd == MSG_TYPE_UPDATE) { |
Rhyme | 0:d895cd1cd897 | 207 | bytes[index++] = (_status); |
Rhyme | 0:d895cd1cd897 | 208 | bytes[index++] = (_reason); |
Rhyme | 0:d895cd1cd897 | 209 | } |
Rhyme | 0:d895cd1cd897 | 210 | |
Rhyme | 0:d895cd1cd897 | 211 | bytes[index++] = (_valueLen & 0xff); |
Rhyme | 0:d895cd1cd897 | 212 | bytes[index++] = ((_valueLen >> 8) & 0xff); |
Rhyme | 0:d895cd1cd897 | 213 | |
Rhyme | 0:d895cd1cd897 | 214 | for (int i = 0; i < _valueLen; i++) { |
Rhyme | 0:d895cd1cd897 | 215 | bytes[index++] = (_value[i]); |
Rhyme | 0:d895cd1cd897 | 216 | } |
Rhyme | 0:d895cd1cd897 | 217 | |
Rhyme | 0:d895cd1cd897 | 218 | return len; |
Rhyme | 0:d895cd1cd897 | 219 | } |
Rhyme | 0:d895cd1cd897 | 220 | |
Rhyme | 0:d895cd1cd897 | 221 | bool Command::isValid() { |
Rhyme | 0:d895cd1cd897 | 222 | return (_cmd == MSG_TYPE_SET) || (_cmd == MSG_TYPE_GET) || (_cmd == MSG_TYPE_UPDATE); |
Rhyme | 0:d895cd1cd897 | 223 | } |
Rhyme | 0:d895cd1cd897 | 224 | |
Rhyme | 0:d895cd1cd897 | 225 | void Command::dumpBytes() { |
Rhyme | 0:d895cd1cd897 | 226 | #if SERIAL_PRINT_DBG_ASR_ON |
Rhyme | 0:d895cd1cd897 | 227 | uint16_t len = getSize(); |
Rhyme | 0:d895cd1cd897 | 228 | uint8_t bytes[len]; |
Rhyme | 0:d895cd1cd897 | 229 | getBytes(bytes); |
Rhyme | 0:d895cd1cd897 | 230 | |
Rhyme | 0:d895cd1cd897 | 231 | _printBuf[0] = 0; |
Rhyme | 0:d895cd1cd897 | 232 | sprintf(_printBuf, "len: %d value: ", len); |
Rhyme | 0:d895cd1cd897 | 233 | for (int i = 0; i < len; i++) { |
Rhyme | 0:d895cd1cd897 | 234 | int b = bytes[i] & 0xff; |
Rhyme | 0:d895cd1cd897 | 235 | sprintf(&_printBuf[strlen(_printBuf)], "%02x", b); |
Rhyme | 0:d895cd1cd897 | 236 | } |
Rhyme | 0:d895cd1cd897 | 237 | printf("%s\n",_printBuf); |
Rhyme | 0:d895cd1cd897 | 238 | #endif |
Rhyme | 0:d895cd1cd897 | 239 | } |
Rhyme | 0:d895cd1cd897 | 240 | |
Rhyme | 0:d895cd1cd897 | 241 | void Command::dump() { |
Rhyme | 0:d895cd1cd897 | 242 | #if SERIAL_PRINT_DBG_ASR_ON |
Rhyme | 0:d895cd1cd897 | 243 | _printBuf[0] = 0; |
Rhyme | 0:d895cd1cd897 | 244 | sprintf(_printBuf, "cmd: %s attr: %d value: ", |
Rhyme | 0:d895cd1cd897 | 245 | CMD_NAMES[_cmd - MESSAGE_CHANNEL_BASE - 1], |
Rhyme | 0:d895cd1cd897 | 246 | _attrId |
Rhyme | 0:d895cd1cd897 | 247 | ); |
Rhyme | 0:d895cd1cd897 | 248 | if (_cmd != MSG_TYPE_GET) { |
Rhyme | 0:d895cd1cd897 | 249 | for (int i = 0; i < _valueLen; i++) { |
Rhyme | 0:d895cd1cd897 | 250 | int b = _value[i] & 0xff; |
Rhyme | 0:d895cd1cd897 | 251 | sprintf(&_printBuf[strlen(_printBuf)], "%02x", b); |
Rhyme | 0:d895cd1cd897 | 252 | } |
Rhyme | 0:d895cd1cd897 | 253 | } |
Rhyme | 0:d895cd1cd897 | 254 | printf("%s\n",_printBuf); |
Rhyme | 0:d895cd1cd897 | 255 | #endif |
Rhyme | 0:d895cd1cd897 | 256 | } |
Rhyme | 0:d895cd1cd897 | 257 | |
Rhyme | 0:d895cd1cd897 | 258 | uint8_t Command::getVal(char c) { |
Rhyme | 0:d895cd1cd897 | 259 | if (c >= '0' && c <= '9') |
Rhyme | 0:d895cd1cd897 | 260 | return (uint8_t)(c - '0'); |
Rhyme | 0:d895cd1cd897 | 261 | else if (c >= 'A' && c <= 'F') |
Rhyme | 0:d895cd1cd897 | 262 | return (uint8_t)(c - 'A' + 10); |
Rhyme | 0:d895cd1cd897 | 263 | else if (c >= 'a' && c <= 'f') |
Rhyme | 0:d895cd1cd897 | 264 | return (uint8_t)(c - 'a' + 10); |
Rhyme | 0:d895cd1cd897 | 265 | |
Rhyme | 0:d895cd1cd897 | 266 | printf("bad hex char: %c\n",c); |
Rhyme | 0:d895cd1cd897 | 267 | |
Rhyme | 0:d895cd1cd897 | 268 | return 0; |
Rhyme | 0:d895cd1cd897 | 269 | } |