RS232 control for TVOne products
spk_tvone_mbed.cpp@19:350d1191e466, 2012-12-11 (annotated)
- Committer:
- tobyspark
- Date:
- Tue Dec 11 13:14:17 2012 +0000
- Revision:
- 19:350d1191e466
- Parent:
- 18:d765b0d9271c
- Child:
- 20:ebddf3ddb1f6
Read tweak + HDCP status logic
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tobyspark | 0:533cfae24a1b | 1 | // *spark audio-visual |
tobyspark | 0:533cfae24a1b | 2 | // RS232 Control for TV-One products |
tobyspark | 0:533cfae24a1b | 3 | // Good for 1T-C2-750, others will need some extra work |
tobyspark | 1:349d6da461df | 4 | |
tobyspark | 1:349d6da461df | 5 | /* Copyright (c) 2011 Toby Harris, MIT License |
tobyspark | 1:349d6da461df | 6 | * |
tobyspark | 1:349d6da461df | 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
tobyspark | 1:349d6da461df | 8 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
tobyspark | 1:349d6da461df | 9 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
tobyspark | 1:349d6da461df | 10 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
tobyspark | 1:349d6da461df | 11 | * furnished to do so, subject to the following conditions: |
tobyspark | 1:349d6da461df | 12 | * |
tobyspark | 1:349d6da461df | 13 | * The above copyright notice and this permission notice shall be included in all copies or |
tobyspark | 1:349d6da461df | 14 | * substantial portions of the Software. |
tobyspark | 1:349d6da461df | 15 | * |
tobyspark | 1:349d6da461df | 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
tobyspark | 1:349d6da461df | 17 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
tobyspark | 1:349d6da461df | 18 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
tobyspark | 1:349d6da461df | 19 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
tobyspark | 1:349d6da461df | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
tobyspark | 1:349d6da461df | 21 | */ |
tobyspark | 0:533cfae24a1b | 22 | |
tobyspark | 0:533cfae24a1b | 23 | #include "spk_tvone_mbed.h" |
tobyspark | 0:533cfae24a1b | 24 | #include "mbed.h" |
tobyspark | 0:533cfae24a1b | 25 | |
tobyspark | 0:533cfae24a1b | 26 | SPKTVOne::SPKTVOne(PinName txPin, PinName rxPin, PinName signWritePin, PinName signErrorPin, Serial *debugSerial) |
tobyspark | 0:533cfae24a1b | 27 | { |
tobyspark | 0:533cfae24a1b | 28 | // Create Serial connection for TVOne unit comms |
tobyspark | 0:533cfae24a1b | 29 | // Creating our own as this is exclusively for TVOne comms |
tobyspark | 9:42c83cac2f6d | 30 | serial = new Serial(txPin, rxPin); |
tobyspark | 0:533cfae24a1b | 31 | serial->baud(57600); |
tobyspark | 0:533cfae24a1b | 32 | |
tobyspark | 0:533cfae24a1b | 33 | if (signWritePin != NC) writeDO = new DigitalOut(signWritePin); |
tobyspark | 0:533cfae24a1b | 34 | else writeDO = NULL; |
tobyspark | 0:533cfae24a1b | 35 | |
tobyspark | 0:533cfae24a1b | 36 | if (signErrorPin != NC) errorDO = new DigitalOut(signErrorPin); |
tobyspark | 0:533cfae24a1b | 37 | else errorDO = NULL; |
tobyspark | 0:533cfae24a1b | 38 | |
tobyspark | 18:d765b0d9271c | 39 | processor.version = -1; |
tobyspark | 18:d765b0d9271c | 40 | processor.productType = -1; |
tobyspark | 18:d765b0d9271c | 41 | processor.boardType = -1; |
tobyspark | 18:d765b0d9271c | 42 | |
tobyspark | 18:d765b0d9271c | 43 | resetCommandPeriods(); |
tobyspark | 14:da403a01f9ef | 44 | |
tobyspark | 14:da403a01f9ef | 45 | timer.start(); |
tobyspark | 17:68b9bb89da2b | 46 | timerCheckTicker.attach(this, &SPKTVOne::timerCheck, 60); |
tobyspark | 14:da403a01f9ef | 47 | |
tobyspark | 0:533cfae24a1b | 48 | // Link up debug Serial object |
tobyspark | 0:533cfae24a1b | 49 | // Passing in shared object as debugging is shared between all DVI mixer functions |
tobyspark | 0:533cfae24a1b | 50 | debug = debugSerial; |
tobyspark | 0:533cfae24a1b | 51 | } |
tobyspark | 0:533cfae24a1b | 52 | |
tobyspark | 10:5f398fc9b5c1 | 53 | bool SPKTVOne::command(uint8_t channel, uint8_t window, int32_t func, int32_t payload) |
tobyspark | 10:5f398fc9b5c1 | 54 | { |
tobyspark | 14:da403a01f9ef | 55 | int ackBuff[standardAckLength] = {0}; |
tobyspark | 10:5f398fc9b5c1 | 56 | |
tobyspark | 11:90e5a72a0034 | 57 | bool success = command(writeCommandType, ackBuff, standardAckLength, channel, window, func, payload); |
tobyspark | 11:90e5a72a0034 | 58 | |
tobyspark | 11:90e5a72a0034 | 59 | // TASK: Check return payload is what we tried to set it to |
tobyspark | 11:90e5a72a0034 | 60 | char payloadStr[7]; |
tobyspark | 11:90e5a72a0034 | 61 | for (int i = 0; i < 6; i++) |
tobyspark | 11:90e5a72a0034 | 62 | { |
tobyspark | 11:90e5a72a0034 | 63 | payloadStr[i] = ackBuff[11+i]; |
tobyspark | 11:90e5a72a0034 | 64 | } |
tobyspark | 11:90e5a72a0034 | 65 | payloadStr[6] = NULL; |
tobyspark | 11:90e5a72a0034 | 66 | |
tobyspark | 11:90e5a72a0034 | 67 | int payloadBack = strtol (payloadStr, NULL, 16); |
tobyspark | 11:90e5a72a0034 | 68 | |
tobyspark | 11:90e5a72a0034 | 69 | if (payload != payloadBack) |
tobyspark | 11:90e5a72a0034 | 70 | { |
tobyspark | 11:90e5a72a0034 | 71 | success = false; |
tobyspark | 13:4d659b89a457 | 72 | if (debug) debug->printf("TVOne return value (%d) is not what was set (%d). Channel: %#x, Window: %#x, Function: %#x \r\n", payloadBack, payload, channel, window, func); |
tobyspark | 11:90e5a72a0034 | 73 | } |
tobyspark | 11:90e5a72a0034 | 74 | return success; |
tobyspark | 11:90e5a72a0034 | 75 | } |
tobyspark | 11:90e5a72a0034 | 76 | |
tobyspark | 11:90e5a72a0034 | 77 | bool SPKTVOne::readCommand(uint8_t channel, uint8_t window, int32_t func, int32_t &payload) |
tobyspark | 11:90e5a72a0034 | 78 | { |
tobyspark | 14:da403a01f9ef | 79 | int ackBuff[standardAckLength] = {0}; |
tobyspark | 11:90e5a72a0034 | 80 | |
tobyspark | 11:90e5a72a0034 | 81 | bool success = command(readCommandType, ackBuff, standardAckLength, channel, window, func, payload); |
tobyspark | 11:90e5a72a0034 | 82 | |
tobyspark | 19:350d1191e466 | 83 | if (success) |
tobyspark | 19:350d1191e466 | 84 | { |
tobyspark | 19:350d1191e466 | 85 | char payloadStr[7]; |
tobyspark | 19:350d1191e466 | 86 | for (int i = 0; i < 6; i++) |
tobyspark | 19:350d1191e466 | 87 | { |
tobyspark | 19:350d1191e466 | 88 | payloadStr[i] = ackBuff[11+i]; |
tobyspark | 19:350d1191e466 | 89 | } |
tobyspark | 19:350d1191e466 | 90 | payloadStr[6] = NULL; |
tobyspark | 19:350d1191e466 | 91 | |
tobyspark | 19:350d1191e466 | 92 | payload = strtol (payloadStr, NULL, 16); |
tobyspark | 11:90e5a72a0034 | 93 | } |
tobyspark | 10:5f398fc9b5c1 | 94 | |
tobyspark | 10:5f398fc9b5c1 | 95 | return success; |
tobyspark | 10:5f398fc9b5c1 | 96 | } |
tobyspark | 10:5f398fc9b5c1 | 97 | |
tobyspark | 10:5f398fc9b5c1 | 98 | bool SPKTVOne::command(commandType readWrite, int* ackBuffer, int ackLength, uint8_t channel, uint8_t window, int32_t func, int32_t payload) |
tobyspark | 14:da403a01f9ef | 99 | { |
tobyspark | 14:da403a01f9ef | 100 | if (debug) debug->printf("TVOne %s Channel: %#x, Window: %#x, Function: %#x Payload: %i \r\n", (readWrite == writeCommandType) ? "Write" : "Read", channel, window, func, payload); |
tobyspark | 14:da403a01f9ef | 101 | |
tobyspark | 0:533cfae24a1b | 102 | // TASK: Sign start of serial command write |
tobyspark | 0:533cfae24a1b | 103 | if (writeDO) *writeDO = 1; |
tobyspark | 0:533cfae24a1b | 104 | |
tobyspark | 14:da403a01f9ef | 105 | // TASK: Prepare to issue command to the TVOne unit |
tobyspark | 14:da403a01f9ef | 106 | // - discard anything waiting to be read in the return serial buffer |
tobyspark | 14:da403a01f9ef | 107 | // - make sure we're past the minimum time between command sends as the unit can get overloaded |
tobyspark | 18:d765b0d9271c | 108 | while (serial->readable() || timer.read_ms() < commandMinimumPeriod) { |
tobyspark | 14:da403a01f9ef | 109 | if (serial->readable()) serial->getc(); |
tobyspark | 0:533cfae24a1b | 110 | } |
tobyspark | 0:533cfae24a1b | 111 | |
tobyspark | 0:533cfae24a1b | 112 | // TASK: Create the bytes of command |
tobyspark | 0:533cfae24a1b | 113 | |
tobyspark | 0:533cfae24a1b | 114 | uint8_t cmd[8]; |
tobyspark | 0:533cfae24a1b | 115 | uint8_t checksum = 0; |
tobyspark | 0:533cfae24a1b | 116 | |
tobyspark | 0:533cfae24a1b | 117 | // CMD |
tobyspark | 10:5f398fc9b5c1 | 118 | cmd[0] = readWrite<<7 | 1<<2; |
tobyspark | 0:533cfae24a1b | 119 | // CHA |
tobyspark | 0:533cfae24a1b | 120 | cmd[1] = channel; |
tobyspark | 0:533cfae24a1b | 121 | // WINDOW |
tobyspark | 0:533cfae24a1b | 122 | cmd[2] = window; |
tobyspark | 0:533cfae24a1b | 123 | // OUTPUT & FUNCTION |
tobyspark | 0:533cfae24a1b | 124 | // cmd[3] cmd[4] |
tobyspark | 0:533cfae24a1b | 125 | // output 0 = 0000xxx xxxxxxx |
tobyspark | 0:533cfae24a1b | 126 | // function = xxxXXXX XXXXXXX |
tobyspark | 0:533cfae24a1b | 127 | cmd[3] = func >> 8; |
tobyspark | 0:533cfae24a1b | 128 | cmd[4] = func & 0xFF; |
tobyspark | 0:533cfae24a1b | 129 | // PAYLOAD |
tobyspark | 0:533cfae24a1b | 130 | cmd[5] = (payload >> 16) & 0xFF; |
tobyspark | 0:533cfae24a1b | 131 | cmd[6] = (payload >> 8) & 0xFF; |
tobyspark | 0:533cfae24a1b | 132 | cmd[7] = payload & 0xFF; |
tobyspark | 0:533cfae24a1b | 133 | |
tobyspark | 0:533cfae24a1b | 134 | // TASK: Write the bytes of command to RS232 as correctly packaged 20 characters of ASCII |
tobyspark | 10:5f398fc9b5c1 | 135 | |
tobyspark | 11:90e5a72a0034 | 136 | if (readWrite == writeCommandType) |
tobyspark | 0:533cfae24a1b | 137 | { |
tobyspark | 14:da403a01f9ef | 138 | for (int i=0; i<8; i++) checksum += cmd[i]; |
tobyspark | 10:5f398fc9b5c1 | 139 | serial->printf("F%02X%02X%02X%02X%02X%02X%02X%02X%02X\r", cmd[0], cmd[1], cmd[2], cmd[3], cmd[4], cmd[5], cmd[6], cmd[7], checksum); |
tobyspark | 0:533cfae24a1b | 140 | } |
tobyspark | 11:90e5a72a0034 | 141 | if (readWrite == readCommandType) |
tobyspark | 10:5f398fc9b5c1 | 142 | { |
tobyspark | 14:da403a01f9ef | 143 | for (int i=0; i<5; i++) checksum += cmd[i]; |
tobyspark | 10:5f398fc9b5c1 | 144 | serial->printf("F%02X%02X%02X%02X%02X%02X\r", cmd[0], cmd[1], cmd[2], cmd[3], cmd[4], checksum); |
tobyspark | 10:5f398fc9b5c1 | 145 | } |
tobyspark | 10:5f398fc9b5c1 | 146 | |
tobyspark | 0:533cfae24a1b | 147 | // TASK: Check the unit's return string, to enable return to main program as soon as unit is ready |
tobyspark | 0:533cfae24a1b | 148 | |
tobyspark | 0:533cfae24a1b | 149 | // Handling the timing of this return is critical to effective control. |
tobyspark | 0:533cfae24a1b | 150 | // Returning the instant something is received back overloads the processor, as does anything until the full 20 char acknowledgement. |
tobyspark | 0:533cfae24a1b | 151 | // TVOne turn out to say that receipt of the ack doesn't guarantee the unit is ready for the next command. |
tobyspark | 0:533cfae24a1b | 152 | // According to the manual, operations typically take 30ms, and to simplify programming you can throttle commands to every 100ms. |
tobyspark | 0:533cfae24a1b | 153 | // 100ms is too slow for us. Going with returning after 30ms if we've received an acknowledgement, returning after 100ms otherwise. |
tobyspark | 10:5f398fc9b5c1 | 154 | |
tobyspark | 14:da403a01f9ef | 155 | bool success = false; |
tobyspark | 14:da403a01f9ef | 156 | int ackPos = 0; |
tobyspark | 14:da403a01f9ef | 157 | timer.reset(); |
tobyspark | 0:533cfae24a1b | 158 | |
tobyspark | 18:d765b0d9271c | 159 | while (timer.read_ms() < commandTimeoutPeriod) |
tobyspark | 10:5f398fc9b5c1 | 160 | { |
tobyspark | 14:da403a01f9ef | 161 | if (serial->readable()) |
tobyspark | 10:5f398fc9b5c1 | 162 | { |
tobyspark | 14:da403a01f9ef | 163 | if (ackPos == 0) |
tobyspark | 9:42c83cac2f6d | 164 | { |
tobyspark | 14:da403a01f9ef | 165 | ackBuffer[0] = serial->getc(); |
tobyspark | 14:da403a01f9ef | 166 | if (ackBuffer[0] == 'F') ackPos = 1; |
tobyspark | 14:da403a01f9ef | 167 | } |
tobyspark | 14:da403a01f9ef | 168 | else |
tobyspark | 14:da403a01f9ef | 169 | { |
tobyspark | 14:da403a01f9ef | 170 | ackBuffer[ackPos] = serial->getc(); |
tobyspark | 14:da403a01f9ef | 171 | ackPos++; |
tobyspark | 14:da403a01f9ef | 172 | if (ackPos == ackLength) break; |
tobyspark | 0:533cfae24a1b | 173 | } |
tobyspark | 10:5f398fc9b5c1 | 174 | } |
tobyspark | 9:42c83cac2f6d | 175 | } |
tobyspark | 14:da403a01f9ef | 176 | |
tobyspark | 14:da403a01f9ef | 177 | // Return true if we got the no error acknowledgement from the unit. The rest of the ack will be verified elsewhere if needed. |
tobyspark | 19:350d1191e466 | 178 | if (ackPos == ackLength && ackBuffer[1] == '4') |
tobyspark | 14:da403a01f9ef | 179 | { |
tobyspark | 14:da403a01f9ef | 180 | success = true; |
tobyspark | 14:da403a01f9ef | 181 | } |
tobyspark | 0:533cfae24a1b | 182 | |
tobyspark | 0:533cfae24a1b | 183 | // TASK: Sign end of write |
tobyspark | 0:533cfae24a1b | 184 | |
tobyspark | 0:533cfae24a1b | 185 | if (writeDO) *writeDO = 0; |
tobyspark | 0:533cfae24a1b | 186 | |
tobyspark | 0:533cfae24a1b | 187 | if (!success) { |
tobyspark | 0:533cfae24a1b | 188 | if (errorDO) { |
tobyspark | 0:533cfae24a1b | 189 | signErrorTimeout.detach(); |
tobyspark | 0:533cfae24a1b | 190 | signErrorTimeout.attach(this, &SPKTVOne::signErrorOff, 0.25); |
tobyspark | 0:533cfae24a1b | 191 | *errorDO = 1; |
tobyspark | 0:533cfae24a1b | 192 | } |
tobyspark | 0:533cfae24a1b | 193 | |
tobyspark | 0:533cfae24a1b | 194 | if (debug) { |
tobyspark | 14:da403a01f9ef | 195 | debug->printf("TVOne serial error. Time from finishing writing command: %ims. Received %i ack chars:", timer.read_ms(), ackPos); |
tobyspark | 14:da403a01f9ef | 196 | for (int i = 0; i<ackLength; i++) |
tobyspark | 14:da403a01f9ef | 197 | { |
tobyspark | 14:da403a01f9ef | 198 | debug->printf("%c", ackBuffer[i]); |
tobyspark | 14:da403a01f9ef | 199 | } |
tobyspark | 14:da403a01f9ef | 200 | debug->printf("\r\n"); |
tobyspark | 0:533cfae24a1b | 201 | } |
tobyspark | 0:533cfae24a1b | 202 | }; |
tobyspark | 0:533cfae24a1b | 203 | |
tobyspark | 0:533cfae24a1b | 204 | return success; |
tobyspark | 0:533cfae24a1b | 205 | } |
tobyspark | 0:533cfae24a1b | 206 | |
tobyspark | 18:d765b0d9271c | 207 | void SPKTVOne::setCommandTimeoutPeriod(int millis) |
tobyspark | 18:d765b0d9271c | 208 | { |
tobyspark | 18:d765b0d9271c | 209 | commandTimeoutPeriod = millis; |
tobyspark | 18:d765b0d9271c | 210 | } |
tobyspark | 18:d765b0d9271c | 211 | |
tobyspark | 18:d765b0d9271c | 212 | void SPKTVOne::setCommandMinimumPeriod(int millis) |
tobyspark | 18:d765b0d9271c | 213 | { |
tobyspark | 18:d765b0d9271c | 214 | commandMinimumPeriod = millis; |
tobyspark | 18:d765b0d9271c | 215 | } |
tobyspark | 18:d765b0d9271c | 216 | |
tobyspark | 18:d765b0d9271c | 217 | void SPKTVOne::increaseCommandPeriods(int millis) |
tobyspark | 18:d765b0d9271c | 218 | { |
tobyspark | 18:d765b0d9271c | 219 | commandTimeoutPeriod += millis; |
tobyspark | 18:d765b0d9271c | 220 | commandMinimumPeriod += millis; |
tobyspark | 18:d765b0d9271c | 221 | |
tobyspark | 18:d765b0d9271c | 222 | if (debug) debug->printf("Command periods increased; minimum: %i, timeout: %i", commandMinimumPeriod, commandTimeoutPeriod); |
tobyspark | 18:d765b0d9271c | 223 | } |
tobyspark | 18:d765b0d9271c | 224 | |
tobyspark | 18:d765b0d9271c | 225 | void SPKTVOne::resetCommandPeriods() |
tobyspark | 18:d765b0d9271c | 226 | { |
tobyspark | 18:d765b0d9271c | 227 | commandTimeoutPeriod = kTV1CommandTimeoutMillis; |
tobyspark | 18:d765b0d9271c | 228 | commandMinimumPeriod = kTV1CommandMinimumMillis; |
tobyspark | 18:d765b0d9271c | 229 | } |
tobyspark | 18:d765b0d9271c | 230 | |
tobyspark | 14:da403a01f9ef | 231 | int SPKTVOne::millisSinceLastCommandSent() |
tobyspark | 14:da403a01f9ef | 232 | { |
tobyspark | 14:da403a01f9ef | 233 | return timer.read_ms(); |
tobyspark | 14:da403a01f9ef | 234 | } |
tobyspark | 14:da403a01f9ef | 235 | |
tobyspark | 18:d765b0d9271c | 236 | bool SPKTVOne::uploadCustomResolutions() |
tobyspark | 0:533cfae24a1b | 237 | { |
tobyspark | 11:90e5a72a0034 | 238 | bool ok = true;; |
tobyspark | 11:90e5a72a0034 | 239 | int unlocked = 0; |
tobyspark | 11:90e5a72a0034 | 240 | int locked = 1; |
tobyspark | 11:90e5a72a0034 | 241 | |
tobyspark | 11:90e5a72a0034 | 242 | ok = ok && command(0, 0, kTV1FunctionAdjustFrontPanelLock, locked); |
tobyspark | 11:90e5a72a0034 | 243 | |
tobyspark | 11:90e5a72a0034 | 244 | ok = ok && set1920x480(kTV1ResolutionTripleHeadVGAp60); |
tobyspark | 11:90e5a72a0034 | 245 | ok = ok && set1600x600(kTV1ResolutionDualHeadSVGAp60); |
tobyspark | 11:90e5a72a0034 | 246 | ok = ok && set2048x768(kTV1ResolutionDualHeadXGAp60); |
tobyspark | 11:90e5a72a0034 | 247 | |
tobyspark | 11:90e5a72a0034 | 248 | ok = ok && command(0, 0, kTV1FunctionAdjustFrontPanelLock, unlocked); |
tobyspark | 11:90e5a72a0034 | 249 | |
tobyspark | 11:90e5a72a0034 | 250 | return ok; |
tobyspark | 0:533cfae24a1b | 251 | } |
tobyspark | 0:533cfae24a1b | 252 | |
tobyspark | 18:d765b0d9271c | 253 | bool SPKTVOne::setResolution(int resolution, int edidSlot) |
tobyspark | 18:d765b0d9271c | 254 | { |
tobyspark | 18:d765b0d9271c | 255 | bool ok; |
tobyspark | 18:d765b0d9271c | 256 | |
tobyspark | 18:d765b0d9271c | 257 | int minPeriodOnEntry = commandMinimumPeriod; |
tobyspark | 18:d765b0d9271c | 258 | int outPeriodOnEntry = commandTimeoutPeriod; |
tobyspark | 18:d765b0d9271c | 259 | |
tobyspark | 18:d765b0d9271c | 260 | for (int i=0; i < 3; i++) |
tobyspark | 18:d765b0d9271c | 261 | { |
tobyspark | 18:d765b0d9271c | 262 | ok = command(0, kTV1WindowIDA, kTV1FunctionAdjustOutputsOutputResolution, resolution); |
tobyspark | 18:d765b0d9271c | 263 | |
tobyspark | 18:d765b0d9271c | 264 | if (ok) break; |
tobyspark | 18:d765b0d9271c | 265 | else increaseCommandPeriods(500); |
tobyspark | 18:d765b0d9271c | 266 | } |
tobyspark | 18:d765b0d9271c | 267 | commandMinimumPeriod = minPeriodOnEntry; |
tobyspark | 18:d765b0d9271c | 268 | commandTimeoutPeriod = outPeriodOnEntry; |
tobyspark | 18:d765b0d9271c | 269 | if (!ok) return ok; |
tobyspark | 18:d765b0d9271c | 270 | |
tobyspark | 18:d765b0d9271c | 271 | for (int i=0; i < 3; i++) |
tobyspark | 18:d765b0d9271c | 272 | { |
tobyspark | 18:d765b0d9271c | 273 | ok = command(kTV1SourceRGB1, kTV1WindowIDA, kTV1FunctionAdjustSourceEDID, edidSlot); |
tobyspark | 18:d765b0d9271c | 274 | ok = ok && command(kTV1SourceRGB2, kTV1WindowIDA, kTV1FunctionAdjustSourceEDID, edidSlot); |
tobyspark | 18:d765b0d9271c | 275 | |
tobyspark | 18:d765b0d9271c | 276 | if (ok) break; |
tobyspark | 18:d765b0d9271c | 277 | else increaseCommandPeriods(500); |
tobyspark | 18:d765b0d9271c | 278 | } |
tobyspark | 18:d765b0d9271c | 279 | commandMinimumPeriod = minPeriodOnEntry; |
tobyspark | 18:d765b0d9271c | 280 | commandTimeoutPeriod = outPeriodOnEntry; |
tobyspark | 18:d765b0d9271c | 281 | |
tobyspark | 18:d765b0d9271c | 282 | return ok; |
tobyspark | 18:d765b0d9271c | 283 | } |
tobyspark | 18:d765b0d9271c | 284 | |
tobyspark | 3:03e7e7b7a870 | 285 | bool SPKTVOne::setHDCPOn(bool state) |
tobyspark | 0:533cfae24a1b | 286 | { |
tobyspark | 18:d765b0d9271c | 287 | bool ok; |
tobyspark | 18:d765b0d9271c | 288 | |
tobyspark | 18:d765b0d9271c | 289 | int minPeriodOnEntry = commandMinimumPeriod; |
tobyspark | 18:d765b0d9271c | 290 | int outPeriodOnEntry = commandTimeoutPeriod; |
tobyspark | 18:d765b0d9271c | 291 | |
tobyspark | 18:d765b0d9271c | 292 | // HDCP can sometimes take a little time to settle down |
tobyspark | 18:d765b0d9271c | 293 | for (int i=0; i < 3; i++) |
tobyspark | 18:d765b0d9271c | 294 | { |
tobyspark | 18:d765b0d9271c | 295 | // Turn HDCP off on the output |
tobyspark | 18:d765b0d9271c | 296 | ok = command(0, kTV1WindowIDA, kTV1FunctionAdjustOutputsHDCPRequired, state); |
tobyspark | 18:d765b0d9271c | 297 | |
tobyspark | 18:d765b0d9271c | 298 | // Likewise on inputs A and B |
tobyspark | 18:d765b0d9271c | 299 | ok = ok && command(kTV1SourceRGB1, kTV1WindowIDA, kTV1FunctionAdjustSourceHDCPAdvertize, state); |
tobyspark | 18:d765b0d9271c | 300 | ok = ok && command(kTV1SourceRGB2, kTV1WindowIDA, kTV1FunctionAdjustSourceHDCPAdvertize, state); |
tobyspark | 19:350d1191e466 | 301 | |
tobyspark | 19:350d1191e466 | 302 | // This verify code is accurate but too misleading for D-Fuser use - eg. actual HDCP state requires source / output connection. |
tobyspark | 19:350d1191e466 | 303 | // // Now verify whats actually going on. |
tobyspark | 19:350d1191e466 | 304 | // int32_t payload = -1; |
tobyspark | 19:350d1191e466 | 305 | // ok = ok && readCommand(0, kTV1WindowIDA, kTV1FunctionAdjustOutputsHDCPStatus, payload); |
tobyspark | 19:350d1191e466 | 306 | // switch (payload) |
tobyspark | 19:350d1191e466 | 307 | // { |
tobyspark | 19:350d1191e466 | 308 | // case 0: ok = ok && !state; break; |
tobyspark | 19:350d1191e466 | 309 | // case 1: ok = ok && !state; break; |
tobyspark | 19:350d1191e466 | 310 | // case 2: ok = ok && state; break; |
tobyspark | 19:350d1191e466 | 311 | // case 3: ok = ok && !state; break; |
tobyspark | 19:350d1191e466 | 312 | // case 4: ok = ok && state; break; |
tobyspark | 19:350d1191e466 | 313 | // default: ok = false; |
tobyspark | 19:350d1191e466 | 314 | // } |
tobyspark | 19:350d1191e466 | 315 | // |
tobyspark | 19:350d1191e466 | 316 | // payload = -1; |
tobyspark | 19:350d1191e466 | 317 | // ok = ok && readCommand(kTV1SourceRGB1, kTV1WindowIDA, kTV1FunctionAdjustSourceHDCPStatus, payload); |
tobyspark | 19:350d1191e466 | 318 | // ok = ok && (payload == state); |
tobyspark | 19:350d1191e466 | 319 | // |
tobyspark | 19:350d1191e466 | 320 | // payload = -1; |
tobyspark | 19:350d1191e466 | 321 | // ok = ok && readCommand(kTV1SourceRGB2, kTV1WindowIDA, kTV1FunctionAdjustSourceHDCPStatus, payload); |
tobyspark | 19:350d1191e466 | 322 | // ok = ok && (payload == state); |
tobyspark | 18:d765b0d9271c | 323 | |
tobyspark | 18:d765b0d9271c | 324 | if (ok) break; |
tobyspark | 18:d765b0d9271c | 325 | else increaseCommandPeriods(500); |
tobyspark | 18:d765b0d9271c | 326 | } |
tobyspark | 18:d765b0d9271c | 327 | commandMinimumPeriod = minPeriodOnEntry; |
tobyspark | 18:d765b0d9271c | 328 | commandTimeoutPeriod = outPeriodOnEntry; |
tobyspark | 0:533cfae24a1b | 329 | |
tobyspark | 18:d765b0d9271c | 330 | return ok; |
tobyspark | 0:533cfae24a1b | 331 | } |
tobyspark | 0:533cfae24a1b | 332 | |
tobyspark | 11:90e5a72a0034 | 333 | bool SPKTVOne::set1920x480(int resStoreNumber) |
tobyspark | 0:533cfae24a1b | 334 | { |
tobyspark | 11:90e5a72a0034 | 335 | bool ok; |
tobyspark | 11:90e5a72a0034 | 336 | |
tobyspark | 11:90e5a72a0034 | 337 | ok = command(0, 0, kTV1FunctionAdjustResolutionImageToAdjust, resStoreNumber); |
tobyspark | 18:d765b0d9271c | 338 | |
tobyspark | 18:d765b0d9271c | 339 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionInterlaced, 0); |
tobyspark | 18:d765b0d9271c | 340 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionFreqCoarseH, 31475); |
tobyspark | 18:d765b0d9271c | 341 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionFreqFineH, 31475); |
tobyspark | 18:d765b0d9271c | 342 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionActiveH, 1920); |
tobyspark | 18:d765b0d9271c | 343 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionActiveV, 480); |
tobyspark | 18:d765b0d9271c | 344 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionStartH, 240); |
tobyspark | 18:d765b0d9271c | 345 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionStartV, 5); |
tobyspark | 18:d765b0d9271c | 346 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionCLKS, 2400); |
tobyspark | 18:d765b0d9271c | 347 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionLines, 525); |
tobyspark | 18:d765b0d9271c | 348 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionSyncH, 192); |
tobyspark | 18:d765b0d9271c | 349 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionSyncV, 30); |
tobyspark | 18:d765b0d9271c | 350 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionSyncPolarity, 0); |
tobyspark | 11:90e5a72a0034 | 351 | |
tobyspark | 11:90e5a72a0034 | 352 | return ok; |
tobyspark | 0:533cfae24a1b | 353 | } |
tobyspark | 0:533cfae24a1b | 354 | |
tobyspark | 11:90e5a72a0034 | 355 | bool SPKTVOne::set1600x600(int resStoreNumber) |
tobyspark | 0:533cfae24a1b | 356 | { |
tobyspark | 11:90e5a72a0034 | 357 | bool ok; |
tobyspark | 11:90e5a72a0034 | 358 | |
tobyspark | 11:90e5a72a0034 | 359 | ok = command(0, 0, kTV1FunctionAdjustResolutionImageToAdjust, resStoreNumber); |
tobyspark | 18:d765b0d9271c | 360 | |
tobyspark | 18:d765b0d9271c | 361 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionInterlaced, 0); |
tobyspark | 18:d765b0d9271c | 362 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionFreqCoarseH, 37879); |
tobyspark | 18:d765b0d9271c | 363 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionFreqFineH, 37879); |
tobyspark | 18:d765b0d9271c | 364 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionActiveH, 1600); |
tobyspark | 18:d765b0d9271c | 365 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionActiveV, 600); |
tobyspark | 18:d765b0d9271c | 366 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionStartH, 192); |
tobyspark | 18:d765b0d9271c | 367 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionStartV, 14); |
tobyspark | 18:d765b0d9271c | 368 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionCLKS, 2112); |
tobyspark | 18:d765b0d9271c | 369 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionLines, 628); |
tobyspark | 18:d765b0d9271c | 370 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionSyncH, 160); |
tobyspark | 18:d765b0d9271c | 371 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionSyncV, 13); |
tobyspark | 18:d765b0d9271c | 372 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionSyncPolarity, 0); |
tobyspark | 11:90e5a72a0034 | 373 | |
tobyspark | 11:90e5a72a0034 | 374 | return ok; |
tobyspark | 0:533cfae24a1b | 375 | } |
tobyspark | 0:533cfae24a1b | 376 | |
tobyspark | 11:90e5a72a0034 | 377 | bool SPKTVOne::set2048x768(int resStoreNumber) |
tobyspark | 2:af9e9ab63b23 | 378 | { |
tobyspark | 11:90e5a72a0034 | 379 | bool ok; |
tobyspark | 11:90e5a72a0034 | 380 | |
tobyspark | 11:90e5a72a0034 | 381 | ok = command(0, 0, kTV1FunctionAdjustResolutionImageToAdjust, resStoreNumber); |
tobyspark | 18:d765b0d9271c | 382 | |
tobyspark | 18:d765b0d9271c | 383 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionInterlaced, 0); |
tobyspark | 18:d765b0d9271c | 384 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionFreqCoarseH, 48363); |
tobyspark | 18:d765b0d9271c | 385 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionFreqFineH, 48363); |
tobyspark | 18:d765b0d9271c | 386 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionActiveH, 2048); |
tobyspark | 18:d765b0d9271c | 387 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionActiveV, 768); |
tobyspark | 18:d765b0d9271c | 388 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionStartH, 224); |
tobyspark | 18:d765b0d9271c | 389 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionStartV, 11); |
tobyspark | 18:d765b0d9271c | 390 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionCLKS, 2688); |
tobyspark | 18:d765b0d9271c | 391 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionLines, 806); |
tobyspark | 18:d765b0d9271c | 392 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionSyncH, 368); |
tobyspark | 18:d765b0d9271c | 393 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionSyncV, 24); |
tobyspark | 18:d765b0d9271c | 394 | ok = ok && command(0, 0, kTV1FunctionAdjustResolutionSyncPolarity, 0); |
tobyspark | 11:90e5a72a0034 | 395 | |
tobyspark | 11:90e5a72a0034 | 396 | return ok; |
tobyspark | 2:af9e9ab63b23 | 397 | } |
tobyspark | 2:af9e9ab63b23 | 398 | |
tobyspark | 0:533cfae24a1b | 399 | void SPKTVOne::signErrorOff() { |
tobyspark | 0:533cfae24a1b | 400 | *errorDO = 0; |
tobyspark | 0:533cfae24a1b | 401 | } |
tobyspark | 14:da403a01f9ef | 402 | |
tobyspark | 18:d765b0d9271c | 403 | SPKTVOne::processorType SPKTVOne::getProcessorType() |
tobyspark | 18:d765b0d9271c | 404 | { |
tobyspark | 18:d765b0d9271c | 405 | bool ok; |
tobyspark | 18:d765b0d9271c | 406 | int32_t payload = 0; |
tobyspark | 18:d765b0d9271c | 407 | |
tobyspark | 18:d765b0d9271c | 408 | if (processor.version == -1) |
tobyspark | 18:d765b0d9271c | 409 | { |
tobyspark | 18:d765b0d9271c | 410 | ok = readCommand(0, kTV1WindowIDA, kTV1FunctionReadSoftwareVersion, payload); |
tobyspark | 18:d765b0d9271c | 411 | if (ok && payload > 0) processor.version = payload; |
tobyspark | 18:d765b0d9271c | 412 | } |
tobyspark | 18:d765b0d9271c | 413 | |
tobyspark | 18:d765b0d9271c | 414 | if (processor.productType == -1) |
tobyspark | 18:d765b0d9271c | 415 | { |
tobyspark | 18:d765b0d9271c | 416 | ok = readCommand(0, kTV1WindowIDA, kTV1FunctionReadProductType, payload); |
tobyspark | 18:d765b0d9271c | 417 | if (ok && payload > 0) processor.productType = payload; |
tobyspark | 18:d765b0d9271c | 418 | } |
tobyspark | 18:d765b0d9271c | 419 | |
tobyspark | 18:d765b0d9271c | 420 | if (processor.boardType == -1) |
tobyspark | 18:d765b0d9271c | 421 | { |
tobyspark | 18:d765b0d9271c | 422 | ok = readCommand(0, kTV1WindowIDA, kTV1FunctionReadBoardType, payload); |
tobyspark | 18:d765b0d9271c | 423 | if (ok && payload > 0) processor.boardType = payload; |
tobyspark | 18:d765b0d9271c | 424 | } |
tobyspark | 18:d765b0d9271c | 425 | |
tobyspark | 18:d765b0d9271c | 426 | if (debug) debug->printf("v: %i, p: %i, b: %i", processor.version, processor.productType, processor.boardType); |
tobyspark | 18:d765b0d9271c | 427 | |
tobyspark | 18:d765b0d9271c | 428 | return processor; |
tobyspark | 18:d765b0d9271c | 429 | } |
tobyspark | 18:d765b0d9271c | 430 | |
tobyspark | 17:68b9bb89da2b | 431 | void SPKTVOne::timerCheck() { |
tobyspark | 17:68b9bb89da2b | 432 | // timers are based on 32-bit int microsecond counters, so can only time up to a maximum of 2^31-1 microseconds i.e. 30 minutes. |
tobyspark | 17:68b9bb89da2b | 433 | // this method is called once a minute, and resets the timer if we've been idle for 25mins. |
tobyspark | 18:d765b0d9271c | 434 | if (timer.read_ms() > 1000*60*25) |
tobyspark | 17:68b9bb89da2b | 435 | { |
tobyspark | 17:68b9bb89da2b | 436 | if (debug) debug->printf("TVOne Timer reset at %ims", timer.read_ms()); |
tobyspark | 17:68b9bb89da2b | 437 | timer.reset(); |
tobyspark | 17:68b9bb89da2b | 438 | } |
tobyspark | 17:68b9bb89da2b | 439 | } |
tobyspark | 17:68b9bb89da2b | 440 | |
tobyspark | 16:ed8d08386034 | 441 | bool SPKTVOne::uploadEDID(FILE *file, int edidSlotIndex) |
tobyspark | 14:da403a01f9ef | 442 | { |
tobyspark | 16:ed8d08386034 | 443 | bool success; |
tobyspark | 14:da403a01f9ef | 444 | |
tobyspark | 14:da403a01f9ef | 445 | // To write EDID, its broken into chunks and sent as a series of extra-long commands |
tobyspark | 14:da403a01f9ef | 446 | // Command: 8 bytes of command (see code below) + 32 bytes of EDID payload + End byte |
tobyspark | 14:da403a01f9ef | 447 | // Acknowledgement: 53 02 40 95 (Hex) |
tobyspark | 16:ed8d08386034 | 448 | // We want to upload full EDID slot, ie. zero out to 256 even if edidData is only 128bytes. |
tobyspark | 14:da403a01f9ef | 449 | |
tobyspark | 16:ed8d08386034 | 450 | if (debug) debug->printf("Upload EDID to index %i \r\n", edidSlotIndex); |
tobyspark | 16:ed8d08386034 | 451 | |
tobyspark | 16:ed8d08386034 | 452 | success = uploadFile(0x07, file, 256, edidSlotIndex); |
tobyspark | 16:ed8d08386034 | 453 | |
tobyspark | 16:ed8d08386034 | 454 | return success; |
tobyspark | 16:ed8d08386034 | 455 | } |
tobyspark | 16:ed8d08386034 | 456 | |
tobyspark | 16:ed8d08386034 | 457 | bool SPKTVOne::uploadImage(FILE *file, int sisIndex) |
tobyspark | 16:ed8d08386034 | 458 | { |
tobyspark | 16:ed8d08386034 | 459 | bool success; |
tobyspark | 16:ed8d08386034 | 460 | |
tobyspark | 16:ed8d08386034 | 461 | int imageDataLength = 0; |
tobyspark | 16:ed8d08386034 | 462 | |
tobyspark | 16:ed8d08386034 | 463 | while (fgetc(file) != EOF) imageDataLength++ ; |
tobyspark | 16:ed8d08386034 | 464 | |
tobyspark | 16:ed8d08386034 | 465 | if (debug) debug->printf("Upload Image with length %i to index %i \r\n", imageDataLength, sisIndex); |
tobyspark | 16:ed8d08386034 | 466 | |
tobyspark | 16:ed8d08386034 | 467 | success = uploadFile(0x00, file, imageDataLength, sisIndex); |
tobyspark | 16:ed8d08386034 | 468 | |
tobyspark | 16:ed8d08386034 | 469 | return success; |
tobyspark | 16:ed8d08386034 | 470 | } |
tobyspark | 16:ed8d08386034 | 471 | |
tobyspark | 16:ed8d08386034 | 472 | bool SPKTVOne::uploadFile(char instruction, FILE* file, int dataLength, int index) |
tobyspark | 16:ed8d08386034 | 473 | { |
tobyspark | 16:ed8d08386034 | 474 | // TASK: Upload Data |
tobyspark | 16:ed8d08386034 | 475 | |
tobyspark | 18:d765b0d9271c | 476 | // Lets be conservative with timings |
tobyspark | 18:d765b0d9271c | 477 | setCommandMinimumPeriod(100); |
tobyspark | 18:d765b0d9271c | 478 | setCommandTimeoutPeriod(300); |
tobyspark | 18:d765b0d9271c | 479 | |
tobyspark | 16:ed8d08386034 | 480 | // This command is reverse engineered. It implements an 'S' command, not the documented 'F'. |
tobyspark | 14:da403a01f9ef | 481 | |
tobyspark | 14:da403a01f9ef | 482 | bool success = false; |
tobyspark | 14:da403a01f9ef | 483 | |
tobyspark | 16:ed8d08386034 | 484 | int dataChunkSize = 32; |
tobyspark | 14:da403a01f9ef | 485 | int ackLength = 4; |
tobyspark | 14:da403a01f9ef | 486 | char goodAck[] = {0x53, 0x02, 0x40, 0x95}; |
tobyspark | 14:da403a01f9ef | 487 | |
tobyspark | 16:ed8d08386034 | 488 | fseek(file, 0, SEEK_SET); |
tobyspark | 16:ed8d08386034 | 489 | |
tobyspark | 16:ed8d08386034 | 490 | for (int i=0; i<dataLength; i=i+dataChunkSize) |
tobyspark | 14:da403a01f9ef | 491 | { |
tobyspark | 16:ed8d08386034 | 492 | int dataRemaining = dataLength - i; |
tobyspark | 18:d765b0d9271c | 493 | int actualDataChunkSize = (dataRemaining < dataChunkSize) ? dataRemaining : dataChunkSize; |
tobyspark | 16:ed8d08386034 | 494 | |
tobyspark | 16:ed8d08386034 | 495 | int commandLength = 8+dataChunkSize+1; |
tobyspark | 14:da403a01f9ef | 496 | char command[commandLength]; |
tobyspark | 14:da403a01f9ef | 497 | |
tobyspark | 14:da403a01f9ef | 498 | command[0] = 0x53; |
tobyspark | 18:d765b0d9271c | 499 | command[1] = 6 + actualDataChunkSize + 1; // Subsequent number of bytes in command |
tobyspark | 14:da403a01f9ef | 500 | command[2] = 0x22; |
tobyspark | 16:ed8d08386034 | 501 | command[3] = instruction; |
tobyspark | 16:ed8d08386034 | 502 | command[4] = index; |
tobyspark | 14:da403a01f9ef | 503 | command[5] = 0; |
tobyspark | 16:ed8d08386034 | 504 | command[6] = (i / dataChunkSize) & 0xFF; // chunk index LSB |
tobyspark | 16:ed8d08386034 | 505 | command[7] = ((i / dataChunkSize) >> 8) & 0xFF; // chunk index MSB |
tobyspark | 14:da403a01f9ef | 506 | |
tobyspark | 18:d765b0d9271c | 507 | for (int j=0; j<actualDataChunkSize; j++) |
tobyspark | 14:da403a01f9ef | 508 | { |
tobyspark | 16:ed8d08386034 | 509 | char data = fgetc(file); |
tobyspark | 16:ed8d08386034 | 510 | if (!feof(file)) |
tobyspark | 16:ed8d08386034 | 511 | *(command+8+j) = data; |
tobyspark | 14:da403a01f9ef | 512 | else |
tobyspark | 14:da403a01f9ef | 513 | *(command+8+j) = 0; |
tobyspark | 14:da403a01f9ef | 514 | } |
tobyspark | 14:da403a01f9ef | 515 | |
tobyspark | 18:d765b0d9271c | 516 | command[8+actualDataChunkSize] = 0x3F; |
tobyspark | 14:da403a01f9ef | 517 | |
tobyspark | 14:da403a01f9ef | 518 | if (debug) |
tobyspark | 14:da403a01f9ef | 519 | { |
tobyspark | 16:ed8d08386034 | 520 | debug->printf("Command: "); |
tobyspark | 14:da403a01f9ef | 521 | for (int k=0; k < commandLength; k++) debug->printf(" %x", command[k]); |
tobyspark | 14:da403a01f9ef | 522 | debug->printf("\r\n"); |
tobyspark | 14:da403a01f9ef | 523 | } |
tobyspark | 14:da403a01f9ef | 524 | |
tobyspark | 18:d765b0d9271c | 525 | while (serial->readable() || timer.read_ms() < commandMinimumPeriod) |
tobyspark | 14:da403a01f9ef | 526 | { |
tobyspark | 14:da403a01f9ef | 527 | if (serial->readable()) serial->getc(); |
tobyspark | 14:da403a01f9ef | 528 | } |
tobyspark | 14:da403a01f9ef | 529 | |
tobyspark | 14:da403a01f9ef | 530 | for (int k=0; k < commandLength; k++) serial->putc(command[k]); |
tobyspark | 14:da403a01f9ef | 531 | |
tobyspark | 14:da403a01f9ef | 532 | timer.reset(); |
tobyspark | 14:da403a01f9ef | 533 | |
tobyspark | 14:da403a01f9ef | 534 | char ackBuffer[4]; |
tobyspark | 14:da403a01f9ef | 535 | int ackPos = 0; |
tobyspark | 18:d765b0d9271c | 536 | while (timer.read_ms() < commandTimeoutPeriod) |
tobyspark | 14:da403a01f9ef | 537 | { |
tobyspark | 14:da403a01f9ef | 538 | if (serial->readable()) ackBuffer[ackPos++] = serial->getc(); |
tobyspark | 14:da403a01f9ef | 539 | if (ackPos == 4) break; |
tobyspark | 14:da403a01f9ef | 540 | } |
tobyspark | 16:ed8d08386034 | 541 | |
tobyspark | 14:da403a01f9ef | 542 | if (memcmp(ackBuffer, goodAck, ackLength) == 0) |
tobyspark | 14:da403a01f9ef | 543 | { |
tobyspark | 14:da403a01f9ef | 544 | success = true; |
tobyspark | 14:da403a01f9ef | 545 | } |
tobyspark | 14:da403a01f9ef | 546 | else |
tobyspark | 14:da403a01f9ef | 547 | { |
tobyspark | 14:da403a01f9ef | 548 | success = false; |
tobyspark | 14:da403a01f9ef | 549 | if (debug) |
tobyspark | 14:da403a01f9ef | 550 | { |
tobyspark | 18:d765b0d9271c | 551 | debug->printf("Data Part write failed. Ack:"); |
tobyspark | 14:da403a01f9ef | 552 | for (int k = 0; k < ackLength; k++) debug->printf(" %x", ackBuffer[k]); |
tobyspark | 14:da403a01f9ef | 553 | debug->printf("\r\n"); |
tobyspark | 14:da403a01f9ef | 554 | } |
tobyspark | 14:da403a01f9ef | 555 | break; |
tobyspark | 14:da403a01f9ef | 556 | } |
tobyspark | 14:da403a01f9ef | 557 | } |
tobyspark | 14:da403a01f9ef | 558 | |
tobyspark | 18:d765b0d9271c | 559 | resetCommandPeriods(); |
tobyspark | 18:d765b0d9271c | 560 | |
tobyspark | 14:da403a01f9ef | 561 | return success; |
tobyspark | 14:da403a01f9ef | 562 | } |