|
30 | 30 | #ifdef ARDUINO_ARCH_STM32 |
31 | 31 |
|
32 | 32 | #include "DCCTimer.h" |
33 | | -#ifdef DEBUG_ADC |
34 | 33 | #include "TrackManager.h" |
35 | | -#endif |
36 | 34 | #include "DIAG.h" |
37 | 35 | #include <wiring_private.h> |
38 | 36 |
|
@@ -215,13 +213,49 @@ void DCCTimer::begin(INTERRUPT_CALLBACK callback) { |
215 | 213 | interrupts(); |
216 | 214 | } |
217 | 215 |
|
218 | | -void DCCTimer::startRailcomTimer(byte brakePin) { |
219 | | - // TODO: for intended operation see DCCTimerAVR.cpp |
220 | | - (void) brakePin; |
| 216 | +static HardwareTimer *railcomTimer = nullptr; |
| 217 | + |
| 218 | +void DCCTimer::startRailcomTimer() { |
| 219 | + // Initialize main timer if not already done |
| 220 | + if (!railcomTimer) { |
| 221 | + railcomTimer = new HardwareTimer(TIM3); |
| 222 | + railcomTimer->pause(); |
| 223 | + } |
| 224 | + |
| 225 | + // Start the timer to begin the cutout in ~29us |
| 226 | + railcomTimer->resume(); |
| 227 | +} |
| 228 | + |
| 229 | +void startRailcomCallback() { |
| 230 | + TrackManager::setMainBrake(true, true); |
| 231 | + if (railcomTimer) { |
| 232 | + railcomTimer->pause(); |
| 233 | + } |
221 | 234 | } |
222 | 235 |
|
223 | 236 | void DCCTimer::ackRailcomTimer() { |
224 | | - // TODO: for intended operation see DCCTimerAVR.cpp |
| 237 | + uint32_t cutoutOffset; |
| 238 | + |
| 239 | + // Un-set the track brake |
| 240 | + TrackManager::setMainBrake(false, true); |
| 241 | + |
| 242 | + // Immediately end the Railcom cutout and set up the timer for the next |
| 243 | + // cutout. We're in the no-man's land between the end of the cutout window and |
| 244 | + // the start of the preamble so we can do time-consuming timer configuration |
| 245 | + // without disrupting the DCC signal. |
| 246 | + if (railcomTimer) { |
| 247 | + railcomTimer->setPrescaleFactor(1); |
| 248 | + |
| 249 | + // Sync up with the primary DCC timer. It will continue to track while paused. |
| 250 | + cutoutOffset = dcctimer.getOverflow(MICROSEC_FORMAT) + 26; |
| 251 | + railcomTimer->setOverflow(cutoutOffset, MICROSEC_FORMAT); |
| 252 | + railcomTimer->setCount(dcctimer.getCount()); |
| 253 | + |
| 254 | + // Arm the timer for the next cutout |
| 255 | + railcomTimer->refresh(); |
| 256 | + railcomTimer->attachInterrupt(startRailcomCallback); |
| 257 | + railcomTimer->pause(); |
| 258 | + } |
225 | 259 | } |
226 | 260 |
|
227 | 261 | bool DCCTimer::isPWMPin(byte pin) { |
|
0 commit comments