From 6b58fc2738569fddd9eeb6ce09c018bd9ea0b556 Mon Sep 17 00:00:00 2001 From: Gregor Riepl Date: Tue, 9 Mar 2021 11:52:38 +0100 Subject: [PATCH] Add deadline on display status read --- display/firmware/main.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/display/firmware/main.c b/display/firmware/main.c index d3e91c7..73c0924 100644 --- a/display/firmware/main.c +++ b/display/firmware/main.c @@ -178,15 +178,24 @@ static void write_dr(uint8_t data) { } -static void wait_ready() { - while (read_ir() & _BV(7)); +static bool wait_ready() { + // if the busy flag never goes high, we'll run into a deadlock here. + // let's put a deadline on the wait loop. + for (uint8_t i = 0; i < 100; i++) { + // read_ir() takes about 500ns + if (read_ir() & _BV(7)) return true; + // wait 10us until the next status read + _delay_us(10 - 0.5); + } + return false; } static void write_display(uint8_t command, const uint8_t *data, uint8_t length) { - wait_ready(); - write_ir(command); - for (uint8_t i = 0; i < length; i++) { - write_dr(data[i]); + if (wait_ready()) { + write_ir(command); + for (uint8_t i = 0; i < length; i++) { + write_dr(data[i]); + } } }