RS232 control for TVOne products
spk_tvone_mbed.cpp@8:903062cdeba7, 2012-10-08 (annotated)
- Committer:
- tobyspark
- Date:
- Mon Oct 08 02:09:42 2012 +0000
- Revision:
- 8:903062cdeba7
- Parent:
- 7:c73d4c68a13b
- Child:
- 9:42c83cac2f6d
Hang on RX while TX culprit found: Timer. Removing tests good but library now barbaric: won't send as fast and might overload the TV One on non-mixing commands.
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 | 7:c73d4c68a13b | 30 | // Update - The stock mbed Serial object will hang on rx while tx, which can happen if the TV-One unit's state unexpectedly changes as this is repeated to RS232. |
tobyspark | 7:c73d4c68a13b | 31 | // No fixes will fix here, tried every variant of IRQ disable, sprintf and allsuch. |
tobyspark | 7:c73d4c68a13b | 32 | // However MODSERIAL doesn't hang, and as a bonus will allow much easier handling of these unexpected 'commands' sent *to* our controller. |
tobyspark | 7:c73d4c68a13b | 33 | serial = new MODSERIAL(txPin, rxPin); |
tobyspark | 0:533cfae24a1b | 34 | serial->baud(57600); |
tobyspark | 0:533cfae24a1b | 35 | |
tobyspark | 0:533cfae24a1b | 36 | if (signWritePin != NC) writeDO = new DigitalOut(signWritePin); |
tobyspark | 0:533cfae24a1b | 37 | else writeDO = NULL; |
tobyspark | 0:533cfae24a1b | 38 | |
tobyspark | 0:533cfae24a1b | 39 | if (signErrorPin != NC) errorDO = new DigitalOut(signErrorPin); |
tobyspark | 0:533cfae24a1b | 40 | else errorDO = NULL; |
tobyspark | 0:533cfae24a1b | 41 | |
tobyspark | 0:533cfae24a1b | 42 | // Link up debug Serial object |
tobyspark | 0:533cfae24a1b | 43 | // Passing in shared object as debugging is shared between all DVI mixer functions |
tobyspark | 0:533cfae24a1b | 44 | debug = debugSerial; |
tobyspark | 0:533cfae24a1b | 45 | } |
tobyspark | 0:533cfae24a1b | 46 | |
tobyspark | 0:533cfae24a1b | 47 | bool SPKTVOne::command(uint8_t channel, uint8_t window, int32_t func, int32_t payload) |
tobyspark | 0:533cfae24a1b | 48 | { |
tobyspark | 0:533cfae24a1b | 49 | char i; |
tobyspark | 0:533cfae24a1b | 50 | |
tobyspark | 0:533cfae24a1b | 51 | // TASK: Sign start of serial command write |
tobyspark | 0:533cfae24a1b | 52 | if (writeDO) *writeDO = 1; |
tobyspark | 0:533cfae24a1b | 53 | |
tobyspark | 0:533cfae24a1b | 54 | // TASK: discard anything waiting to be read |
tobyspark | 0:533cfae24a1b | 55 | while (serial->readable()) { |
tobyspark | 0:533cfae24a1b | 56 | serial->getc(); |
tobyspark | 0:533cfae24a1b | 57 | } |
tobyspark | 0:533cfae24a1b | 58 | |
tobyspark | 0:533cfae24a1b | 59 | // TASK: Create the bytes of command |
tobyspark | 0:533cfae24a1b | 60 | |
tobyspark | 0:533cfae24a1b | 61 | uint8_t cmd[8]; |
tobyspark | 0:533cfae24a1b | 62 | uint8_t checksum = 0; |
tobyspark | 0:533cfae24a1b | 63 | |
tobyspark | 0:533cfae24a1b | 64 | // CMD |
tobyspark | 0:533cfae24a1b | 65 | cmd[0] = 1<<2; // write |
tobyspark | 0:533cfae24a1b | 66 | // CHA |
tobyspark | 0:533cfae24a1b | 67 | cmd[1] = channel; |
tobyspark | 0:533cfae24a1b | 68 | // WINDOW |
tobyspark | 0:533cfae24a1b | 69 | cmd[2] = window; |
tobyspark | 0:533cfae24a1b | 70 | // OUTPUT & FUNCTION |
tobyspark | 0:533cfae24a1b | 71 | // cmd[3] cmd[4] |
tobyspark | 0:533cfae24a1b | 72 | // output 0 = 0000xxx xxxxxxx |
tobyspark | 0:533cfae24a1b | 73 | // function = xxxXXXX XXXXXXX |
tobyspark | 0:533cfae24a1b | 74 | cmd[3] = func >> 8; |
tobyspark | 0:533cfae24a1b | 75 | cmd[4] = func & 0xFF; |
tobyspark | 0:533cfae24a1b | 76 | // PAYLOAD |
tobyspark | 0:533cfae24a1b | 77 | cmd[5] = (payload >> 16) & 0xFF; |
tobyspark | 0:533cfae24a1b | 78 | cmd[6] = (payload >> 8) & 0xFF; |
tobyspark | 0:533cfae24a1b | 79 | cmd[7] = payload & 0xFF; |
tobyspark | 0:533cfae24a1b | 80 | |
tobyspark | 0:533cfae24a1b | 81 | // TASK: Write the bytes of command to RS232 as correctly packaged 20 characters of ASCII |
tobyspark | 0:533cfae24a1b | 82 | |
tobyspark | 0:533cfae24a1b | 83 | for (i=0; i<8; i++) |
tobyspark | 0:533cfae24a1b | 84 | { |
tobyspark | 0:533cfae24a1b | 85 | checksum += cmd[i]; |
tobyspark | 0:533cfae24a1b | 86 | } |
tobyspark | 0:533cfae24a1b | 87 | |
tobyspark | 0:533cfae24a1b | 88 | 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 | 89 | |
tobyspark | 0:533cfae24a1b | 90 | // TASK: Check the unit's return string, to enable return to main program as soon as unit is ready |
tobyspark | 0:533cfae24a1b | 91 | |
tobyspark | 0:533cfae24a1b | 92 | // Handling the timing of this return is critical to effective control. |
tobyspark | 0:533cfae24a1b | 93 | // Returning the instant something is received back overloads the processor, as does anything until the full 20 char acknowledgement. |
tobyspark | 0:533cfae24a1b | 94 | // TVOne turn out to say that receipt of the ack doesn't guarantee the unit is ready for the next command. |
tobyspark | 0:533cfae24a1b | 95 | // According to the manual, operations typically take 30ms, and to simplify programming you can throttle commands to every 100ms. |
tobyspark | 0:533cfae24a1b | 96 | // 100ms is too slow for us. Going with returning after 30ms if we've received an acknowledgement, returning after 100ms otherwise. |
tobyspark | 0:533cfae24a1b | 97 | |
tobyspark | 0:533cfae24a1b | 98 | int ack[20]; |
tobyspark | 8:903062cdeba7 | 99 | bool ackReceived = false; |
tobyspark | 8:903062cdeba7 | 100 | bool success = false; |
tobyspark | 0:533cfae24a1b | 101 | |
tobyspark | 8:903062cdeba7 | 102 | i = 0; |
tobyspark | 8:903062cdeba7 | 103 | // BARBARIC: Presence of timer was causing hang, and any use of NVIC_DisableIRQ(TIMER3_IRQn) didn't seem to change that. |
tobyspark | 8:903062cdeba7 | 104 | // So here we are stripped of max throughput timing technique, barbarically dependenant a 30ms wait to ensure TV One isn't overloaded and seeing malformed / error acks. |
tobyspark | 8:903062cdeba7 | 105 | wait_ms(30); |
tobyspark | 8:903062cdeba7 | 106 | while (serial->readable()) { |
tobyspark | 8:903062cdeba7 | 107 | ack[i] = serial->getc(); |
tobyspark | 8:903062cdeba7 | 108 | i++; |
tobyspark | 8:903062cdeba7 | 109 | if (i >= 20) { |
tobyspark | 8:903062cdeba7 | 110 | ackReceived = true; |
tobyspark | 8:903062cdeba7 | 111 | if (ack[0] == 'F' && ack[1] == '4') { // TVOne start of message, acknowledgement with no error, rest will be repeat of sent command |
tobyspark | 8:903062cdeba7 | 112 | success = true; |
tobyspark | 0:533cfae24a1b | 113 | } |
tobyspark | 8:903062cdeba7 | 114 | break; |
tobyspark | 0:533cfae24a1b | 115 | } |
tobyspark | 8:903062cdeba7 | 116 | } |
tobyspark | 0:533cfae24a1b | 117 | |
tobyspark | 0:533cfae24a1b | 118 | // TASK: Sign end of write |
tobyspark | 0:533cfae24a1b | 119 | |
tobyspark | 0:533cfae24a1b | 120 | if (writeDO) *writeDO = 0; |
tobyspark | 0:533cfae24a1b | 121 | |
tobyspark | 0:533cfae24a1b | 122 | if (!success) { |
tobyspark | 0:533cfae24a1b | 123 | if (errorDO) { |
tobyspark | 0:533cfae24a1b | 124 | signErrorTimeout.detach(); |
tobyspark | 0:533cfae24a1b | 125 | signErrorTimeout.attach(this, &SPKTVOne::signErrorOff, 0.25); |
tobyspark | 0:533cfae24a1b | 126 | *errorDO = 1; |
tobyspark | 0:533cfae24a1b | 127 | } |
tobyspark | 0:533cfae24a1b | 128 | |
tobyspark | 0:533cfae24a1b | 129 | if (debug) { |
tobyspark | 8:903062cdeba7 | 130 | //debug->printf("Serial command write error. Time from write finish: %ims \r\n", timer.read_ms()); |
tobyspark | 8:903062cdeba7 | 131 | debug->printf("Serial command write error."); |
tobyspark | 0:533cfae24a1b | 132 | } |
tobyspark | 0:533cfae24a1b | 133 | }; |
tobyspark | 0:533cfae24a1b | 134 | |
tobyspark | 0:533cfae24a1b | 135 | return success; |
tobyspark | 0:533cfae24a1b | 136 | } |
tobyspark | 0:533cfae24a1b | 137 | |
tobyspark | 0:533cfae24a1b | 138 | void SPKTVOne::setCustomResolutions() |
tobyspark | 0:533cfae24a1b | 139 | { |
tobyspark | 0:533cfae24a1b | 140 | set1920x480(kTV1ResolutionTripleHeadVGAp60); |
tobyspark | 0:533cfae24a1b | 141 | set1600x600(kTV1ResolutionDualHeadSVGAp60); |
tobyspark | 2:af9e9ab63b23 | 142 | set2048x768(kTV1ResolutionDualHeadXGAp60); |
tobyspark | 0:533cfae24a1b | 143 | } |
tobyspark | 0:533cfae24a1b | 144 | |
tobyspark | 3:03e7e7b7a870 | 145 | bool SPKTVOne::setHDCPOn(bool state) |
tobyspark | 0:533cfae24a1b | 146 | { |
tobyspark | 0:533cfae24a1b | 147 | bool ok = false; |
tobyspark | 0:533cfae24a1b | 148 | |
tobyspark | 0:533cfae24a1b | 149 | // Turn HDCP off on the output |
tobyspark | 3:03e7e7b7a870 | 150 | ok = command(0, kTV1WindowIDA, kTV1FunctionAdjustOutputsHDCPRequired, state); |
tobyspark | 3:03e7e7b7a870 | 151 | ok = ok && command(0, kTV1WindowIDA, kTV1FunctionAdjustOutputsHDCPStatus, state); |
tobyspark | 0:533cfae24a1b | 152 | // Likewise on inputs A and B |
tobyspark | 3:03e7e7b7a870 | 153 | ok = ok && command(0, kTV1WindowIDA, kTV1FunctionAdjustSourceHDCPAdvertize, state); |
tobyspark | 3:03e7e7b7a870 | 154 | ok = ok && command(0, kTV1WindowIDB, kTV1FunctionAdjustSourceHDCPAdvertize, state); |
tobyspark | 3:03e7e7b7a870 | 155 | ok = ok && command(0, kTV1WindowIDA, kTV1FunctionAdjustSourceHDCPStatus, state); |
tobyspark | 3:03e7e7b7a870 | 156 | ok = ok && command(0, kTV1WindowIDB, kTV1FunctionAdjustSourceHDCPStatus, state); |
tobyspark | 0:533cfae24a1b | 157 | |
tobyspark | 0:533cfae24a1b | 158 | return ok; |
tobyspark | 0:533cfae24a1b | 159 | } |
tobyspark | 0:533cfae24a1b | 160 | |
tobyspark | 0:533cfae24a1b | 161 | void SPKTVOne::set1920x480(int resStoreNumber) |
tobyspark | 0:533cfae24a1b | 162 | { |
tobyspark | 0:533cfae24a1b | 163 | command(0, 0, kTV1FunctionAdjustResolutionImageToAdjust, resStoreNumber); |
tobyspark | 0:533cfae24a1b | 164 | command(0, 0, kTV1FunctionAdjustResolutionInterlaced, 0); |
tobyspark | 0:533cfae24a1b | 165 | command(0, 0, kTV1FunctionAdjustResolutionFreqCoarseH, 31400); |
tobyspark | 0:533cfae24a1b | 166 | command(0, 0, kTV1FunctionAdjustResolutionFreqFineH, 31475); |
tobyspark | 0:533cfae24a1b | 167 | command(0, 0, kTV1FunctionAdjustResolutionActiveH, 1920); |
tobyspark | 0:533cfae24a1b | 168 | command(0, 0, kTV1FunctionAdjustResolutionActiveV, 480); |
tobyspark | 0:533cfae24a1b | 169 | command(0, 0, kTV1FunctionAdjustResolutionStartH, 192); |
tobyspark | 0:533cfae24a1b | 170 | command(0, 0, kTV1FunctionAdjustResolutionStartV, 32); |
tobyspark | 0:533cfae24a1b | 171 | command(0, 0, kTV1FunctionAdjustResolutionCLKS, 2400); |
tobyspark | 0:533cfae24a1b | 172 | command(0, 0, kTV1FunctionAdjustResolutionLines, 525); |
tobyspark | 0:533cfae24a1b | 173 | command(0, 0, kTV1FunctionAdjustResolutionSyncH, 240); |
tobyspark | 0:533cfae24a1b | 174 | command(0, 0, kTV1FunctionAdjustResolutionSyncV, 5); |
tobyspark | 0:533cfae24a1b | 175 | command(0, 0, kTV1FunctionAdjustResolutionSyncPolarity, 0); |
tobyspark | 0:533cfae24a1b | 176 | } |
tobyspark | 0:533cfae24a1b | 177 | |
tobyspark | 0:533cfae24a1b | 178 | void SPKTVOne::set1600x600(int resStoreNumber) |
tobyspark | 0:533cfae24a1b | 179 | { |
tobyspark | 0:533cfae24a1b | 180 | command(0, 0, kTV1FunctionAdjustResolutionImageToAdjust, resStoreNumber); |
tobyspark | 0:533cfae24a1b | 181 | command(0, 0, kTV1FunctionAdjustResolutionInterlaced, 0); |
tobyspark | 0:533cfae24a1b | 182 | command(0, 0, kTV1FunctionAdjustResolutionFreqCoarseH, 37879); |
tobyspark | 0:533cfae24a1b | 183 | command(0, 0, kTV1FunctionAdjustResolutionFreqFineH, 37879); |
tobyspark | 0:533cfae24a1b | 184 | command(0, 0, kTV1FunctionAdjustResolutionActiveH, 1600); |
tobyspark | 0:533cfae24a1b | 185 | command(0, 0, kTV1FunctionAdjustResolutionActiveV, 600); |
tobyspark | 0:533cfae24a1b | 186 | command(0, 0, kTV1FunctionAdjustResolutionStartH, 160); |
tobyspark | 0:533cfae24a1b | 187 | command(0, 0, kTV1FunctionAdjustResolutionStartV, 1); |
tobyspark | 0:533cfae24a1b | 188 | command(0, 0, kTV1FunctionAdjustResolutionCLKS, 2112); |
tobyspark | 0:533cfae24a1b | 189 | command(0, 0, kTV1FunctionAdjustResolutionLines, 628); |
tobyspark | 0:533cfae24a1b | 190 | command(0, 0, kTV1FunctionAdjustResolutionSyncH, 192); |
tobyspark | 0:533cfae24a1b | 191 | command(0, 0, kTV1FunctionAdjustResolutionSyncV, 14); |
tobyspark | 0:533cfae24a1b | 192 | command(0, 0, kTV1FunctionAdjustResolutionSyncPolarity, 0); |
tobyspark | 0:533cfae24a1b | 193 | } |
tobyspark | 0:533cfae24a1b | 194 | |
tobyspark | 2:af9e9ab63b23 | 195 | void SPKTVOne::set2048x768(int resStoreNumber) |
tobyspark | 2:af9e9ab63b23 | 196 | { |
tobyspark | 2:af9e9ab63b23 | 197 | command(0, 0, kTV1FunctionAdjustResolutionImageToAdjust, resStoreNumber); |
tobyspark | 2:af9e9ab63b23 | 198 | command(0, 0, kTV1FunctionAdjustResolutionInterlaced, 0); |
tobyspark | 2:af9e9ab63b23 | 199 | command(0, 0, kTV1FunctionAdjustResolutionFreqCoarseH, 48363); |
tobyspark | 2:af9e9ab63b23 | 200 | command(0, 0, kTV1FunctionAdjustResolutionFreqFineH, 48363); |
tobyspark | 2:af9e9ab63b23 | 201 | command(0, 0, kTV1FunctionAdjustResolutionActiveH, 2048); |
tobyspark | 2:af9e9ab63b23 | 202 | command(0, 0, kTV1FunctionAdjustResolutionActiveV, 768); |
tobyspark | 2:af9e9ab63b23 | 203 | command(0, 0, kTV1FunctionAdjustResolutionStartH, 368); |
tobyspark | 2:af9e9ab63b23 | 204 | command(0, 0, kTV1FunctionAdjustResolutionStartV, 24); |
tobyspark | 2:af9e9ab63b23 | 205 | command(0, 0, kTV1FunctionAdjustResolutionCLKS, 2688); |
tobyspark | 2:af9e9ab63b23 | 206 | command(0, 0, kTV1FunctionAdjustResolutionLines, 806); |
tobyspark | 2:af9e9ab63b23 | 207 | command(0, 0, kTV1FunctionAdjustResolutionSyncH, 224); |
tobyspark | 2:af9e9ab63b23 | 208 | command(0, 0, kTV1FunctionAdjustResolutionSyncV, 11); |
tobyspark | 2:af9e9ab63b23 | 209 | command(0, 0, kTV1FunctionAdjustResolutionSyncPolarity, 0); |
tobyspark | 2:af9e9ab63b23 | 210 | } |
tobyspark | 2:af9e9ab63b23 | 211 | |
tobyspark | 0:533cfae24a1b | 212 | void SPKTVOne::signErrorOff() { |
tobyspark | 0:533cfae24a1b | 213 | *errorDO = 0; |
tobyspark | 0:533cfae24a1b | 214 | } |