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]); + } } }