RS232 control for TVOne products

Dependents:   SPK-DVIMXR

Committer:
tobyspark
Date:
Sun Dec 02 01:06:50 2012 +0000
Revision:
16:ed8d08386034
Parent:
14:da403a01f9ef
Child:
17:68b9bb89da2b
uploadEDID is joined by uploadImage.

Who changed what in which revision?

UserRevisionLine numberNew 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 14:da403a01f9ef 39 timeoutCommandPeriod = 100;
tobyspark 14:da403a01f9ef 40 minimumCommandPeriod = 30;
tobyspark 14:da403a01f9ef 41
tobyspark 14:da403a01f9ef 42 timer.start();
tobyspark 14:da403a01f9ef 43
tobyspark 0:533cfae24a1b 44 // Link up debug Serial object
tobyspark 0:533cfae24a1b 45 // Passing in shared object as debugging is shared between all DVI mixer functions
tobyspark 0:533cfae24a1b 46 debug = debugSerial;
tobyspark 0:533cfae24a1b 47 }
tobyspark 0:533cfae24a1b 48
tobyspark 10:5f398fc9b5c1 49 bool SPKTVOne::command(uint8_t channel, uint8_t window, int32_t func, int32_t payload)
tobyspark 10:5f398fc9b5c1 50 {
tobyspark 14:da403a01f9ef 51 int ackBuff[standardAckLength] = {0};
tobyspark 10:5f398fc9b5c1 52
tobyspark 11:90e5a72a0034 53 bool success = command(writeCommandType, ackBuff, standardAckLength, channel, window, func, payload);
tobyspark 11:90e5a72a0034 54
tobyspark 11:90e5a72a0034 55 // TASK: Check return payload is what we tried to set it to
tobyspark 11:90e5a72a0034 56 char payloadStr[7];
tobyspark 11:90e5a72a0034 57 for (int i = 0; i < 6; i++)
tobyspark 11:90e5a72a0034 58 {
tobyspark 11:90e5a72a0034 59 payloadStr[i] = ackBuff[11+i];
tobyspark 11:90e5a72a0034 60 }
tobyspark 11:90e5a72a0034 61 payloadStr[6] = NULL;
tobyspark 11:90e5a72a0034 62
tobyspark 11:90e5a72a0034 63 int payloadBack = strtol (payloadStr, NULL, 16);
tobyspark 11:90e5a72a0034 64
tobyspark 11:90e5a72a0034 65 if (payload != payloadBack)
tobyspark 11:90e5a72a0034 66 {
tobyspark 11:90e5a72a0034 67 success = false;
tobyspark 13:4d659b89a457 68 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 69 }
tobyspark 11:90e5a72a0034 70 return success;
tobyspark 11:90e5a72a0034 71 }
tobyspark 11:90e5a72a0034 72
tobyspark 11:90e5a72a0034 73 bool SPKTVOne::readCommand(uint8_t channel, uint8_t window, int32_t func, int32_t &payload)
tobyspark 11:90e5a72a0034 74 {
tobyspark 14:da403a01f9ef 75 int ackBuff[standardAckLength] = {0};
tobyspark 11:90e5a72a0034 76
tobyspark 11:90e5a72a0034 77 bool success = command(readCommandType, ackBuff, standardAckLength, channel, window, func, payload);
tobyspark 11:90e5a72a0034 78
tobyspark 11:90e5a72a0034 79 char payloadStr[7];
tobyspark 11:90e5a72a0034 80 for (int i = 0; i < 6; i++)
tobyspark 11:90e5a72a0034 81 {
tobyspark 11:90e5a72a0034 82 payloadStr[i] = ackBuff[11+i];
tobyspark 11:90e5a72a0034 83 }
tobyspark 11:90e5a72a0034 84 payloadStr[6] = NULL;
tobyspark 11:90e5a72a0034 85
tobyspark 11:90e5a72a0034 86 payload = strtol (payloadStr, NULL, 16);
tobyspark 10:5f398fc9b5c1 87
tobyspark 10:5f398fc9b5c1 88 return success;
tobyspark 10:5f398fc9b5c1 89 }
tobyspark 10:5f398fc9b5c1 90
tobyspark 10:5f398fc9b5c1 91 bool SPKTVOne::command(commandType readWrite, int* ackBuffer, int ackLength, uint8_t channel, uint8_t window, int32_t func, int32_t payload)
tobyspark 14:da403a01f9ef 92 {
tobyspark 14:da403a01f9ef 93 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 94
tobyspark 0:533cfae24a1b 95 // TASK: Sign start of serial command write
tobyspark 0:533cfae24a1b 96 if (writeDO) *writeDO = 1;
tobyspark 0:533cfae24a1b 97
tobyspark 14:da403a01f9ef 98 // TASK: Prepare to issue command to the TVOne unit
tobyspark 14:da403a01f9ef 99 // - discard anything waiting to be read in the return serial buffer
tobyspark 14:da403a01f9ef 100 // - make sure we're past the minimum time between command sends as the unit can get overloaded
tobyspark 14:da403a01f9ef 101 while (serial->readable() || timer.read_ms() < minimumCommandPeriod) {
tobyspark 14:da403a01f9ef 102 if (serial->readable()) serial->getc();
tobyspark 0:533cfae24a1b 103 }
tobyspark 0:533cfae24a1b 104
tobyspark 0:533cfae24a1b 105 // TASK: Create the bytes of command
tobyspark 0:533cfae24a1b 106
tobyspark 0:533cfae24a1b 107 uint8_t cmd[8];
tobyspark 0:533cfae24a1b 108 uint8_t checksum = 0;
tobyspark 0:533cfae24a1b 109
tobyspark 0:533cfae24a1b 110 // CMD
tobyspark 10:5f398fc9b5c1 111 cmd[0] = readWrite<<7 | 1<<2;
tobyspark 0:533cfae24a1b 112 // CHA
tobyspark 0:533cfae24a1b 113 cmd[1] = channel;
tobyspark 0:533cfae24a1b 114 // WINDOW
tobyspark 0:533cfae24a1b 115 cmd[2] = window;
tobyspark 0:533cfae24a1b 116 // OUTPUT & FUNCTION
tobyspark 0:533cfae24a1b 117 // cmd[3] cmd[4]
tobyspark 0:533cfae24a1b 118 // output 0 = 0000xxx xxxxxxx
tobyspark 0:533cfae24a1b 119 // function = xxxXXXX XXXXXXX
tobyspark 0:533cfae24a1b 120 cmd[3] = func >> 8;
tobyspark 0:533cfae24a1b 121 cmd[4] = func & 0xFF;
tobyspark 0:533cfae24a1b 122 // PAYLOAD
tobyspark 0:533cfae24a1b 123 cmd[5] = (payload >> 16) & 0xFF;
tobyspark 0:533cfae24a1b 124 cmd[6] = (payload >> 8) & 0xFF;
tobyspark 0:533cfae24a1b 125 cmd[7] = payload & 0xFF;
tobyspark 0:533cfae24a1b 126
tobyspark 0:533cfae24a1b 127 // TASK: Write the bytes of command to RS232 as correctly packaged 20 characters of ASCII
tobyspark 10:5f398fc9b5c1 128
tobyspark 11:90e5a72a0034 129 if (readWrite == writeCommandType)
tobyspark 0:533cfae24a1b 130 {
tobyspark 14:da403a01f9ef 131 for (int i=0; i<8; i++) checksum += cmd[i];
tobyspark 10:5f398fc9b5c1 132 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 133 }
tobyspark 11:90e5a72a0034 134 if (readWrite == readCommandType)
tobyspark 10:5f398fc9b5c1 135 {
tobyspark 14:da403a01f9ef 136 for (int i=0; i<5; i++) checksum += cmd[i];
tobyspark 10:5f398fc9b5c1 137 serial->printf("F%02X%02X%02X%02X%02X%02X\r", cmd[0], cmd[1], cmd[2], cmd[3], cmd[4], checksum);
tobyspark 10:5f398fc9b5c1 138 }
tobyspark 10:5f398fc9b5c1 139
tobyspark 0:533cfae24a1b 140 // TASK: Check the unit's return string, to enable return to main program as soon as unit is ready
tobyspark 0:533cfae24a1b 141
tobyspark 0:533cfae24a1b 142 // Handling the timing of this return is critical to effective control.
tobyspark 0:533cfae24a1b 143 // Returning the instant something is received back overloads the processor, as does anything until the full 20 char acknowledgement.
tobyspark 0:533cfae24a1b 144 // TVOne turn out to say that receipt of the ack doesn't guarantee the unit is ready for the next command.
tobyspark 0:533cfae24a1b 145 // According to the manual, operations typically take 30ms, and to simplify programming you can throttle commands to every 100ms.
tobyspark 0:533cfae24a1b 146 // 100ms is too slow for us. Going with returning after 30ms if we've received an acknowledgement, returning after 100ms otherwise.
tobyspark 10:5f398fc9b5c1 147
tobyspark 14:da403a01f9ef 148 bool success = false;
tobyspark 14:da403a01f9ef 149 int ackPos = 0;
tobyspark 14:da403a01f9ef 150 timer.reset();
tobyspark 0:533cfae24a1b 151
tobyspark 14:da403a01f9ef 152 while (timer.read_ms() < timeoutCommandPeriod)
tobyspark 10:5f398fc9b5c1 153 {
tobyspark 14:da403a01f9ef 154 if (serial->readable())
tobyspark 10:5f398fc9b5c1 155 {
tobyspark 14:da403a01f9ef 156 if (ackPos == 0)
tobyspark 9:42c83cac2f6d 157 {
tobyspark 14:da403a01f9ef 158 ackBuffer[0] = serial->getc();
tobyspark 14:da403a01f9ef 159 if (ackBuffer[0] == 'F') ackPos = 1;
tobyspark 14:da403a01f9ef 160 }
tobyspark 14:da403a01f9ef 161 else
tobyspark 14:da403a01f9ef 162 {
tobyspark 14:da403a01f9ef 163 ackBuffer[ackPos] = serial->getc();
tobyspark 14:da403a01f9ef 164 ackPos++;
tobyspark 14:da403a01f9ef 165 if (ackPos == ackLength) break;
tobyspark 0:533cfae24a1b 166 }
tobyspark 10:5f398fc9b5c1 167 }
tobyspark 9:42c83cac2f6d 168 }
tobyspark 14:da403a01f9ef 169
tobyspark 14:da403a01f9ef 170 // Return true if we got the no error acknowledgement from the unit. The rest of the ack will be verified elsewhere if needed.
tobyspark 14:da403a01f9ef 171 if (ackPos > 2 && ackBuffer[1] == '4')
tobyspark 14:da403a01f9ef 172 {
tobyspark 14:da403a01f9ef 173 success = true;
tobyspark 14:da403a01f9ef 174 }
tobyspark 0:533cfae24a1b 175
tobyspark 0:533cfae24a1b 176 // TASK: Sign end of write
tobyspark 0:533cfae24a1b 177
tobyspark 0:533cfae24a1b 178 if (writeDO) *writeDO = 0;
tobyspark 0:533cfae24a1b 179
tobyspark 0:533cfae24a1b 180 if (!success) {
tobyspark 0:533cfae24a1b 181 if (errorDO) {
tobyspark 0:533cfae24a1b 182 signErrorTimeout.detach();
tobyspark 0:533cfae24a1b 183 signErrorTimeout.attach(this, &SPKTVOne::signErrorOff, 0.25);
tobyspark 0:533cfae24a1b 184 *errorDO = 1;
tobyspark 0:533cfae24a1b 185 }
tobyspark 0:533cfae24a1b 186
tobyspark 0:533cfae24a1b 187 if (debug) {
tobyspark 14:da403a01f9ef 188 debug->printf("TVOne serial error. Time from finishing writing command: %ims. Received %i ack chars:", timer.read_ms(), ackPos);
tobyspark 14:da403a01f9ef 189 for (int i = 0; i<ackLength; i++)
tobyspark 14:da403a01f9ef 190 {
tobyspark 14:da403a01f9ef 191 debug->printf("%c", ackBuffer[i]);
tobyspark 14:da403a01f9ef 192 }
tobyspark 14:da403a01f9ef 193 debug->printf("\r\n");
tobyspark 0:533cfae24a1b 194 }
tobyspark 0:533cfae24a1b 195 };
tobyspark 0:533cfae24a1b 196
tobyspark 0:533cfae24a1b 197 return success;
tobyspark 0:533cfae24a1b 198 }
tobyspark 0:533cfae24a1b 199
tobyspark 14:da403a01f9ef 200 int SPKTVOne::millisSinceLastCommandSent()
tobyspark 14:da403a01f9ef 201 {
tobyspark 14:da403a01f9ef 202 return timer.read_ms();
tobyspark 14:da403a01f9ef 203 }
tobyspark 14:da403a01f9ef 204
tobyspark 11:90e5a72a0034 205 bool SPKTVOne::setCustomResolutions()
tobyspark 0:533cfae24a1b 206 {
tobyspark 11:90e5a72a0034 207 bool ok = true;;
tobyspark 11:90e5a72a0034 208 int unlocked = 0;
tobyspark 11:90e5a72a0034 209 int locked = 1;
tobyspark 11:90e5a72a0034 210
tobyspark 11:90e5a72a0034 211 ok = ok && command(0, 0, kTV1FunctionAdjustFrontPanelLock, locked);
tobyspark 11:90e5a72a0034 212
tobyspark 11:90e5a72a0034 213 ok = ok && set1920x480(kTV1ResolutionTripleHeadVGAp60);
tobyspark 11:90e5a72a0034 214 ok = ok && set1600x600(kTV1ResolutionDualHeadSVGAp60);
tobyspark 11:90e5a72a0034 215 ok = ok && set2048x768(kTV1ResolutionDualHeadXGAp60);
tobyspark 11:90e5a72a0034 216
tobyspark 11:90e5a72a0034 217 ok = ok && command(0, 0, kTV1FunctionAdjustFrontPanelLock, unlocked);
tobyspark 11:90e5a72a0034 218
tobyspark 11:90e5a72a0034 219 return ok;
tobyspark 0:533cfae24a1b 220 }
tobyspark 0:533cfae24a1b 221
tobyspark 3:03e7e7b7a870 222 bool SPKTVOne::setHDCPOn(bool state)
tobyspark 0:533cfae24a1b 223 {
tobyspark 11:90e5a72a0034 224 bool ok;
tobyspark 0:533cfae24a1b 225
tobyspark 0:533cfae24a1b 226 // Turn HDCP off on the output
tobyspark 3:03e7e7b7a870 227 ok = command(0, kTV1WindowIDA, kTV1FunctionAdjustOutputsHDCPRequired, state);
tobyspark 3:03e7e7b7a870 228 ok = ok && command(0, kTV1WindowIDA, kTV1FunctionAdjustOutputsHDCPStatus, state);
tobyspark 0:533cfae24a1b 229 // Likewise on inputs A and B
tobyspark 3:03e7e7b7a870 230 ok = ok && command(0, kTV1WindowIDA, kTV1FunctionAdjustSourceHDCPAdvertize, state);
tobyspark 3:03e7e7b7a870 231 ok = ok && command(0, kTV1WindowIDB, kTV1FunctionAdjustSourceHDCPAdvertize, state);
tobyspark 3:03e7e7b7a870 232 ok = ok && command(0, kTV1WindowIDA, kTV1FunctionAdjustSourceHDCPStatus, state);
tobyspark 3:03e7e7b7a870 233 ok = ok && command(0, kTV1WindowIDB, kTV1FunctionAdjustSourceHDCPStatus, state);
tobyspark 0:533cfae24a1b 234
tobyspark 0:533cfae24a1b 235 return ok;
tobyspark 0:533cfae24a1b 236 }
tobyspark 0:533cfae24a1b 237
tobyspark 11:90e5a72a0034 238 bool SPKTVOne::set1920x480(int resStoreNumber)
tobyspark 0:533cfae24a1b 239 {
tobyspark 11:90e5a72a0034 240 bool ok;
tobyspark 11:90e5a72a0034 241
tobyspark 11:90e5a72a0034 242 ok = command(0, 0, kTV1FunctionAdjustResolutionImageToAdjust, resStoreNumber);
tobyspark 11:90e5a72a0034 243 if (ok)
tobyspark 11:90e5a72a0034 244 {
tobyspark 11:90e5a72a0034 245 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionInterlaced, 0);
tobyspark 11:90e5a72a0034 246 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionFreqCoarseH, 31475);
tobyspark 11:90e5a72a0034 247 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionFreqFineH, 31475);
tobyspark 11:90e5a72a0034 248 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionActiveH, 1920);
tobyspark 11:90e5a72a0034 249 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionActiveV, 480);
tobyspark 11:90e5a72a0034 250 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionStartH, 240);
tobyspark 11:90e5a72a0034 251 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionStartV, 5);
tobyspark 11:90e5a72a0034 252 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionCLKS, 2400);
tobyspark 11:90e5a72a0034 253 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionLines, 525);
tobyspark 11:90e5a72a0034 254 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionSyncH, 192);
tobyspark 11:90e5a72a0034 255 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionSyncV, 30);
tobyspark 11:90e5a72a0034 256 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionSyncPolarity, 0);
tobyspark 11:90e5a72a0034 257 }
tobyspark 11:90e5a72a0034 258
tobyspark 11:90e5a72a0034 259 return ok;
tobyspark 0:533cfae24a1b 260 }
tobyspark 0:533cfae24a1b 261
tobyspark 11:90e5a72a0034 262 bool SPKTVOne::set1600x600(int resStoreNumber)
tobyspark 0:533cfae24a1b 263 {
tobyspark 11:90e5a72a0034 264 bool ok;
tobyspark 11:90e5a72a0034 265
tobyspark 11:90e5a72a0034 266 ok = command(0, 0, kTV1FunctionAdjustResolutionImageToAdjust, resStoreNumber);
tobyspark 11:90e5a72a0034 267 if (ok)
tobyspark 11:90e5a72a0034 268 {
tobyspark 11:90e5a72a0034 269 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionInterlaced, 0);
tobyspark 11:90e5a72a0034 270 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionFreqCoarseH, 37879);
tobyspark 11:90e5a72a0034 271 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionFreqFineH, 37879);
tobyspark 11:90e5a72a0034 272 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionActiveH, 1600);
tobyspark 11:90e5a72a0034 273 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionActiveV, 600);
tobyspark 11:90e5a72a0034 274 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionStartH, 192);
tobyspark 11:90e5a72a0034 275 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionStartV, 14);
tobyspark 11:90e5a72a0034 276 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionCLKS, 2112);
tobyspark 11:90e5a72a0034 277 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionLines, 628);
tobyspark 11:90e5a72a0034 278 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionSyncH, 160);
tobyspark 11:90e5a72a0034 279 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionSyncV, 13);
tobyspark 11:90e5a72a0034 280 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionSyncPolarity, 0);
tobyspark 11:90e5a72a0034 281 }
tobyspark 11:90e5a72a0034 282
tobyspark 11:90e5a72a0034 283 return ok;
tobyspark 0:533cfae24a1b 284 }
tobyspark 0:533cfae24a1b 285
tobyspark 11:90e5a72a0034 286 bool SPKTVOne::set2048x768(int resStoreNumber)
tobyspark 2:af9e9ab63b23 287 {
tobyspark 11:90e5a72a0034 288 bool ok;
tobyspark 11:90e5a72a0034 289
tobyspark 11:90e5a72a0034 290 ok = command(0, 0, kTV1FunctionAdjustResolutionImageToAdjust, resStoreNumber);
tobyspark 11:90e5a72a0034 291 if (ok)
tobyspark 11:90e5a72a0034 292 {
tobyspark 11:90e5a72a0034 293 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionInterlaced, 0);
tobyspark 11:90e5a72a0034 294 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionFreqCoarseH, 48363);
tobyspark 11:90e5a72a0034 295 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionFreqFineH, 48363);
tobyspark 11:90e5a72a0034 296 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionActiveH, 2048);
tobyspark 11:90e5a72a0034 297 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionActiveV, 768);
tobyspark 11:90e5a72a0034 298 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionStartH, 224);
tobyspark 11:90e5a72a0034 299 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionStartV, 11);
tobyspark 11:90e5a72a0034 300 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionCLKS, 2688);
tobyspark 11:90e5a72a0034 301 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionLines, 806);
tobyspark 11:90e5a72a0034 302 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionSyncH, 368);
tobyspark 11:90e5a72a0034 303 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionSyncV, 24);
tobyspark 11:90e5a72a0034 304 ok = ok && command(0, 0, kTV1FunctionAdjustResolutionSyncPolarity, 0);
tobyspark 11:90e5a72a0034 305 }
tobyspark 11:90e5a72a0034 306
tobyspark 11:90e5a72a0034 307 return ok;
tobyspark 2:af9e9ab63b23 308 }
tobyspark 2:af9e9ab63b23 309
tobyspark 0:533cfae24a1b 310 void SPKTVOne::signErrorOff() {
tobyspark 0:533cfae24a1b 311 *errorDO = 0;
tobyspark 0:533cfae24a1b 312 }
tobyspark 14:da403a01f9ef 313
tobyspark 16:ed8d08386034 314 bool SPKTVOne::uploadEDID(FILE *file, int edidSlotIndex)
tobyspark 14:da403a01f9ef 315 {
tobyspark 16:ed8d08386034 316 bool success;
tobyspark 14:da403a01f9ef 317
tobyspark 14:da403a01f9ef 318 // To write EDID, its broken into chunks and sent as a series of extra-long commands
tobyspark 14:da403a01f9ef 319 // Command: 8 bytes of command (see code below) + 32 bytes of EDID payload + End byte
tobyspark 14:da403a01f9ef 320 // Acknowledgement: 53 02 40 95 (Hex)
tobyspark 16:ed8d08386034 321 // We want to upload full EDID slot, ie. zero out to 256 even if edidData is only 128bytes.
tobyspark 14:da403a01f9ef 322
tobyspark 16:ed8d08386034 323 if (debug) debug->printf("Upload EDID to index %i \r\n", edidSlotIndex);
tobyspark 16:ed8d08386034 324
tobyspark 16:ed8d08386034 325 success = uploadFile(0x07, file, 256, edidSlotIndex);
tobyspark 16:ed8d08386034 326
tobyspark 16:ed8d08386034 327 return success;
tobyspark 16:ed8d08386034 328 }
tobyspark 16:ed8d08386034 329
tobyspark 16:ed8d08386034 330 bool SPKTVOne::uploadImage(FILE *file, int sisIndex)
tobyspark 16:ed8d08386034 331 {
tobyspark 16:ed8d08386034 332 bool success;
tobyspark 16:ed8d08386034 333
tobyspark 16:ed8d08386034 334 int imageDataLength = 0;
tobyspark 16:ed8d08386034 335
tobyspark 16:ed8d08386034 336 while (fgetc(file) != EOF) imageDataLength++ ;
tobyspark 16:ed8d08386034 337
tobyspark 16:ed8d08386034 338 if (debug) debug->printf("Upload Image with length %i to index %i \r\n", imageDataLength, sisIndex);
tobyspark 16:ed8d08386034 339
tobyspark 16:ed8d08386034 340 success = uploadFile(0x00, file, imageDataLength, sisIndex);
tobyspark 16:ed8d08386034 341
tobyspark 16:ed8d08386034 342 return success;
tobyspark 16:ed8d08386034 343 }
tobyspark 16:ed8d08386034 344
tobyspark 16:ed8d08386034 345 bool SPKTVOne::uploadFile(char instruction, FILE* file, int dataLength, int index)
tobyspark 16:ed8d08386034 346 {
tobyspark 16:ed8d08386034 347 // TASK: Upload Data
tobyspark 16:ed8d08386034 348
tobyspark 16:ed8d08386034 349 // This command is reverse engineered. It implements an 'S' command, not the documented 'F'.
tobyspark 14:da403a01f9ef 350
tobyspark 14:da403a01f9ef 351 bool success = false;
tobyspark 14:da403a01f9ef 352
tobyspark 16:ed8d08386034 353 int dataChunkSize = 32;
tobyspark 14:da403a01f9ef 354 int ackLength = 4;
tobyspark 14:da403a01f9ef 355 char goodAck[] = {0x53, 0x02, 0x40, 0x95};
tobyspark 14:da403a01f9ef 356
tobyspark 16:ed8d08386034 357 fseek(file, 0, SEEK_SET);
tobyspark 16:ed8d08386034 358
tobyspark 16:ed8d08386034 359 for (int i=0; i<dataLength; i=i+dataChunkSize)
tobyspark 14:da403a01f9ef 360 {
tobyspark 16:ed8d08386034 361 int dataRemaining = dataLength - i;
tobyspark 16:ed8d08386034 362 if (dataRemaining < dataChunkSize) dataChunkSize = dataRemaining;
tobyspark 16:ed8d08386034 363
tobyspark 16:ed8d08386034 364 int commandLength = 8+dataChunkSize+1;
tobyspark 14:da403a01f9ef 365 char command[commandLength];
tobyspark 14:da403a01f9ef 366
tobyspark 14:da403a01f9ef 367 command[0] = 0x53;
tobyspark 16:ed8d08386034 368 command[1] = 6 + dataChunkSize + 1; // Subsequent number of bytes in command
tobyspark 14:da403a01f9ef 369 command[2] = 0x22;
tobyspark 16:ed8d08386034 370 command[3] = instruction;
tobyspark 16:ed8d08386034 371 command[4] = index;
tobyspark 14:da403a01f9ef 372 command[5] = 0;
tobyspark 16:ed8d08386034 373 command[6] = (i / dataChunkSize) & 0xFF; // chunk index LSB
tobyspark 16:ed8d08386034 374 command[7] = ((i / dataChunkSize) >> 8) & 0xFF; // chunk index MSB
tobyspark 14:da403a01f9ef 375
tobyspark 16:ed8d08386034 376 for (int j=0; j<dataChunkSize; j++)
tobyspark 14:da403a01f9ef 377 {
tobyspark 16:ed8d08386034 378 char data = fgetc(file);
tobyspark 16:ed8d08386034 379 if (!feof(file))
tobyspark 16:ed8d08386034 380 *(command+8+j) = data;
tobyspark 14:da403a01f9ef 381 else
tobyspark 14:da403a01f9ef 382 *(command+8+j) = 0;
tobyspark 14:da403a01f9ef 383 }
tobyspark 14:da403a01f9ef 384
tobyspark 16:ed8d08386034 385 command[8+dataChunkSize] = 0x3F;
tobyspark 14:da403a01f9ef 386
tobyspark 14:da403a01f9ef 387 if (debug)
tobyspark 14:da403a01f9ef 388 {
tobyspark 16:ed8d08386034 389 debug->printf("Command: ");
tobyspark 14:da403a01f9ef 390 for (int k=0; k < commandLength; k++) debug->printf(" %x", command[k]);
tobyspark 14:da403a01f9ef 391 debug->printf("\r\n");
tobyspark 14:da403a01f9ef 392 }
tobyspark 14:da403a01f9ef 393
tobyspark 16:ed8d08386034 394 while (serial->readable() || timer.read_ms() < 100)
tobyspark 14:da403a01f9ef 395 {
tobyspark 14:da403a01f9ef 396 if (serial->readable()) serial->getc();
tobyspark 14:da403a01f9ef 397 }
tobyspark 14:da403a01f9ef 398
tobyspark 14:da403a01f9ef 399 for (int k=0; k < commandLength; k++) serial->putc(command[k]);
tobyspark 14:da403a01f9ef 400
tobyspark 14:da403a01f9ef 401 timer.reset();
tobyspark 14:da403a01f9ef 402
tobyspark 14:da403a01f9ef 403 char ackBuffer[4];
tobyspark 14:da403a01f9ef 404 int ackPos = 0;
tobyspark 14:da403a01f9ef 405 while (timer.read_ms() < 1000)
tobyspark 14:da403a01f9ef 406 {
tobyspark 14:da403a01f9ef 407 if (serial->readable()) ackBuffer[ackPos++] = serial->getc();
tobyspark 14:da403a01f9ef 408 if (ackPos == 4) break;
tobyspark 14:da403a01f9ef 409 }
tobyspark 16:ed8d08386034 410
tobyspark 14:da403a01f9ef 411 if (memcmp(ackBuffer, goodAck, ackLength) == 0)
tobyspark 14:da403a01f9ef 412 {
tobyspark 14:da403a01f9ef 413 success = true;
tobyspark 14:da403a01f9ef 414 }
tobyspark 14:da403a01f9ef 415 else
tobyspark 14:da403a01f9ef 416 {
tobyspark 14:da403a01f9ef 417 success = false;
tobyspark 14:da403a01f9ef 418 if (debug)
tobyspark 14:da403a01f9ef 419 {
tobyspark 14:da403a01f9ef 420 debug->printf("EDID Part write failed. Ack:");
tobyspark 14:da403a01f9ef 421 for (int k = 0; k < ackLength; k++) debug->printf(" %x", ackBuffer[k]);
tobyspark 14:da403a01f9ef 422 debug->printf("\r\n");
tobyspark 14:da403a01f9ef 423 }
tobyspark 14:da403a01f9ef 424 break;
tobyspark 14:da403a01f9ef 425 }
tobyspark 14:da403a01f9ef 426 }
tobyspark 14:da403a01f9ef 427
tobyspark 14:da403a01f9ef 428 return success;
tobyspark 14:da403a01f9ef 429 }