From 043f815c4c02d361b47c689a8a336d6d689b55dd Mon Sep 17 00:00:00 2001 From: Andrew Makousky Date: Sat, 5 Sep 2020 17:48:46 -0500 Subject: [PATCH] Be defensive with holding RTC output on last cycle. --- firmware/rtc/MacRTC.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/firmware/rtc/MacRTC.c b/firmware/rtc/MacRTC.c index 9aae782..c88c4df 100644 --- a/firmware/rtc/MacRTC.c +++ b/firmware/rtc/MacRTC.c @@ -197,7 +197,6 @@ void halfSecondInterrupt(void) { } else TCNT0 += 7; - } /* @@ -278,11 +277,12 @@ void loop(void) { } else { /* Normally we only perform an action on the rising edge of the serial clock. The main exception is cleanup at the last cycle - of serial output, there we wait until the falling edge before - switching the direction of the data pin back to an input. */ + of serial output, there we wait one full cycle and then until + the falling edge before switching the direction of the data pin + back to an input. */ if (serClockFalling && serialState == SENDING_DATA && - serialBitNum >= 8) { + serialBitNum >= 9) { clearState(); } else if (serClockRising) { boolean writeRequest; @@ -381,15 +381,19 @@ void loop(void) { break; case SENDING_DATA: - digitalWriteOD(SERIAL_DATA_PIN, bitRead(serialData, 7 - serialBitNum)); + if (serialBitNum <= 7) + digitalWriteOD(SERIAL_DATA_PIN, + bitRead(serialData, 7 - serialBitNum)); serialBitNum++; /* if (serialBitNum <= 7) break; */ /* NOTE: The last output cycle is treated specially, hold the - data line as an output until the falling edge of the serial - clock, then switch back to an input and reset the serial - communication state. */ + data line as an output for at least one full next cycle, + then until the falling edge of the serial clock, then + switch back to an input and reset the serial communication + state. It's for bug compatibility with the ROM, but with a + little bit of sanity too. */ break; #if !defined(NoXPRAM) || !NoXPRAM