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/StatusCommand.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 | #include "StatusCommand.h" |
Rhyme | 0:d895cd1cd897 | 18 | |
Rhyme | 0:d895cd1cd897 | 19 | #define SERIAL_PRINT_DBG_ASR_ON 0 |
Rhyme | 0:d895cd1cd897 | 20 | |
Rhyme | 0:d895cd1cd897 | 21 | StatusCommand::StatusCommand(uint16_t bytesToSend) { |
Rhyme | 0:d895cd1cd897 | 22 | _cmd = 0x30; |
Rhyme | 0:d895cd1cd897 | 23 | _bytesToSend = bytesToSend; |
Rhyme | 0:d895cd1cd897 | 24 | _bytesToRecv = 0; |
Rhyme | 0:d895cd1cd897 | 25 | } |
Rhyme | 0:d895cd1cd897 | 26 | |
Rhyme | 0:d895cd1cd897 | 27 | StatusCommand::StatusCommand() { |
Rhyme | 0:d895cd1cd897 | 28 | _cmd = 0x30; |
Rhyme | 0:d895cd1cd897 | 29 | _bytesToSend = 0; |
Rhyme | 0:d895cd1cd897 | 30 | _bytesToRecv = 0; |
Rhyme | 0:d895cd1cd897 | 31 | } |
Rhyme | 0:d895cd1cd897 | 32 | |
Rhyme | 0:d895cd1cd897 | 33 | StatusCommand::~StatusCommand() { |
Rhyme | 0:d895cd1cd897 | 34 | } |
Rhyme | 0:d895cd1cd897 | 35 | |
Rhyme | 0:d895cd1cd897 | 36 | uint16_t StatusCommand::getSize() { |
Rhyme | 0:d895cd1cd897 | 37 | return sizeof(_cmd) + sizeof(_bytesToSend) + sizeof(_bytesToRecv); |
Rhyme | 0:d895cd1cd897 | 38 | } |
Rhyme | 0:d895cd1cd897 | 39 | |
Rhyme | 0:d895cd1cd897 | 40 | uint16_t StatusCommand::getBytes(int *bytes) { |
Rhyme | 0:d895cd1cd897 | 41 | int index = 0; |
Rhyme | 0:d895cd1cd897 | 42 | |
Rhyme | 0:d895cd1cd897 | 43 | bytes[index++] = (_cmd); |
Rhyme | 0:d895cd1cd897 | 44 | bytes[index++] = (_bytesToSend & 0xff); |
Rhyme | 0:d895cd1cd897 | 45 | bytes[index++] = ((_bytesToSend >> 8) & 0xff); |
Rhyme | 0:d895cd1cd897 | 46 | bytes[index++] = (_bytesToRecv & 0xff); |
Rhyme | 0:d895cd1cd897 | 47 | bytes[index++] = ((_bytesToRecv >> 8) & 0xff); |
Rhyme | 0:d895cd1cd897 | 48 | |
Rhyme | 0:d895cd1cd897 | 49 | return index; |
Rhyme | 0:d895cd1cd897 | 50 | } |
Rhyme | 0:d895cd1cd897 | 51 | |
Rhyme | 0:d895cd1cd897 | 52 | uint8_t StatusCommand::calcChecksum() { |
Rhyme | 0:d895cd1cd897 | 53 | uint8_t result = 0; |
Rhyme | 0:d895cd1cd897 | 54 | |
Rhyme | 0:d895cd1cd897 | 55 | result += (_cmd); |
Rhyme | 0:d895cd1cd897 | 56 | result += (_bytesToSend & 0xff); |
Rhyme | 0:d895cd1cd897 | 57 | result += ((_bytesToSend >> 8) & 0xff); |
Rhyme | 0:d895cd1cd897 | 58 | result += (_bytesToRecv & 0xff); |
Rhyme | 0:d895cd1cd897 | 59 | result += ((_bytesToRecv >> 8) & 0xff); |
Rhyme | 0:d895cd1cd897 | 60 | |
Rhyme | 0:d895cd1cd897 | 61 | return result; |
Rhyme | 0:d895cd1cd897 | 62 | } |
Rhyme | 0:d895cd1cd897 | 63 | |
Rhyme | 0:d895cd1cd897 | 64 | void StatusCommand::setChecksum(uint8_t checksum) { |
Rhyme | 0:d895cd1cd897 | 65 | _checksum = checksum; |
Rhyme | 0:d895cd1cd897 | 66 | } |
Rhyme | 0:d895cd1cd897 | 67 | |
Rhyme | 0:d895cd1cd897 | 68 | uint8_t StatusCommand::getChecksum() { |
Rhyme | 0:d895cd1cd897 | 69 | uint8_t result = 0; |
Rhyme | 0:d895cd1cd897 | 70 | |
Rhyme | 0:d895cd1cd897 | 71 | result += (_cmd); |
Rhyme | 0:d895cd1cd897 | 72 | result += (_bytesToSend & 0xff); |
Rhyme | 0:d895cd1cd897 | 73 | result += ((_bytesToSend >> 8) & 0xff); |
Rhyme | 0:d895cd1cd897 | 74 | result += (_bytesToRecv & 0xff); |
Rhyme | 0:d895cd1cd897 | 75 | result += ((_bytesToRecv >> 8) & 0xff); |
Rhyme | 0:d895cd1cd897 | 76 | |
Rhyme | 0:d895cd1cd897 | 77 | return result; |
Rhyme | 0:d895cd1cd897 | 78 | } |
Rhyme | 0:d895cd1cd897 | 79 | |
Rhyme | 0:d895cd1cd897 | 80 | void StatusCommand::setAck(bool ack) { |
Rhyme | 0:d895cd1cd897 | 81 | _cmd = ack ? 0x31 : 0x30; |
Rhyme | 0:d895cd1cd897 | 82 | } |
Rhyme | 0:d895cd1cd897 | 83 | |
Rhyme | 0:d895cd1cd897 | 84 | void StatusCommand::setBytesToSend(uint16_t bytesToSend) { |
Rhyme | 0:d895cd1cd897 | 85 | _bytesToSend = bytesToSend; |
Rhyme | 0:d895cd1cd897 | 86 | } |
Rhyme | 0:d895cd1cd897 | 87 | |
Rhyme | 0:d895cd1cd897 | 88 | uint16_t StatusCommand::getBytesToSend() { |
Rhyme | 0:d895cd1cd897 | 89 | return _bytesToSend; |
Rhyme | 0:d895cd1cd897 | 90 | } |
Rhyme | 0:d895cd1cd897 | 91 | |
Rhyme | 0:d895cd1cd897 | 92 | void StatusCommand::setBytesToRecv(uint16_t bytesToRecv) { |
Rhyme | 0:d895cd1cd897 | 93 | _bytesToRecv = bytesToRecv; |
Rhyme | 0:d895cd1cd897 | 94 | } |
Rhyme | 0:d895cd1cd897 | 95 | |
Rhyme | 0:d895cd1cd897 | 96 | uint16_t StatusCommand::getBytesToRecv() { |
Rhyme | 0:d895cd1cd897 | 97 | return _bytesToRecv; |
Rhyme | 0:d895cd1cd897 | 98 | } |
Rhyme | 0:d895cd1cd897 | 99 | |
Rhyme | 0:d895cd1cd897 | 100 | bool StatusCommand::equals(StatusCommand *statusCommand) { |
Rhyme | 0:d895cd1cd897 | 101 | return (_cmd == statusCommand->_cmd && _bytesToSend == statusCommand->_bytesToSend && |
Rhyme | 0:d895cd1cd897 | 102 | _bytesToRecv == statusCommand->_bytesToRecv); |
Rhyme | 0:d895cd1cd897 | 103 | } |
Rhyme | 0:d895cd1cd897 | 104 | |
Rhyme | 0:d895cd1cd897 | 105 | bool StatusCommand::isValid() { |
Rhyme | 0:d895cd1cd897 | 106 | return (_checksum == calcChecksum()) && (_cmd == 0x30 || _cmd == 0x31); |
Rhyme | 0:d895cd1cd897 | 107 | } |
Rhyme | 0:d895cd1cd897 | 108 | |
Rhyme | 0:d895cd1cd897 | 109 | void StatusCommand::dumpBytes() { |
Rhyme | 0:d895cd1cd897 | 110 | #if SERIAL_PRINT_DBG_ASR_ON |
Rhyme | 0:d895cd1cd897 | 111 | int len = getSize(); |
Rhyme | 0:d895cd1cd897 | 112 | int bytes[len]; |
Rhyme | 0:d895cd1cd897 | 113 | getBytes(bytes); |
Rhyme | 0:d895cd1cd897 | 114 | |
Rhyme | 0:d895cd1cd897 | 115 | printf("len : %d\n",len); |
Rhyme | 0:d895cd1cd897 | 116 | printf("data : "); |
Rhyme | 0:d895cd1cd897 | 117 | for (int i = 0; i < len; i++) { |
Rhyme | 0:d895cd1cd897 | 118 | if (i > 0) { |
Rhyme | 0:d895cd1cd897 | 119 | printf(", "); |
Rhyme | 0:d895cd1cd897 | 120 | } |
Rhyme | 0:d895cd1cd897 | 121 | int b = bytes[i] & 0xff; |
Rhyme | 0:d895cd1cd897 | 122 | printf("0x%02x", b) ; |
Rhyme | 0:d895cd1cd897 | 123 | } |
Rhyme | 0:d895cd1cd897 | 124 | printf("\n") ; |
Rhyme | 0:d895cd1cd897 | 125 | #endif |
Rhyme | 0:d895cd1cd897 | 126 | } |
Rhyme | 0:d895cd1cd897 | 127 | |
Rhyme | 0:d895cd1cd897 | 128 | void StatusCommand::dump() { |
Rhyme | 0:d895cd1cd897 | 129 | #if SERIAL_PRINT_DBG_ASR_ON |
Rhyme | 0:d895cd1cd897 | 130 | printf("cmd : %s\n",_cmd == 0x30 ? "STATUS" : "STATUS_ACK"); |
Rhyme | 0:d895cd1cd897 | 131 | printf("bytes to send : %d\n",_bytesToSend); |
Rhyme | 0:d895cd1cd897 | 132 | printf("bytes to receive : %d\n",_bytesToRecv); |
Rhyme | 0:d895cd1cd897 | 133 | #endif |
Rhyme | 0:d895cd1cd897 | 134 | } |
Rhyme | 0:d895cd1cd897 | 135 |