From effe6f6ddd5132db4ab4f88bf8616aab575e7691 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Sat, 10 Aug 2019 11:43:52 -0400 Subject: [PATCH] lcd_st7920: Add brief delay around gpio toggle calls on fast MCUs On fast MCUs (like the SAMD51) it is possible for the gpio to toggle faster than the st7920 can accept. Add a small delay around the clock rise signal. Signed-off-by: Lars R. Hansen Signed-off-by: Kevin O'Connor --- src/lcd_st7920.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/lcd_st7920.c b/src/lcd_st7920.c index 8cd76c50..0f6a444d 100644 --- a/src/lcd_st7920.c +++ b/src/lcd_st7920.c @@ -4,6 +4,7 @@ // // This file may be distributed under the terms of the GNU GPLv3 license. +#include "autoconf.h" // CONFIG_CLOCK_FREQ #include "basecmd.h" // oid_alloc #include "board/gpio.h" // gpio_out_write #include "board/irq.h" // irq_poll @@ -21,6 +22,23 @@ struct st7920 { * Transmit functions ****************************************************************/ +static uint32_t +nsecs_to_ticks(uint32_t ns) +{ + return timer_from_us(ns * 1000) / 1000000; +} + +static inline void +ndelay(uint32_t nsecs) +{ + if (CONFIG_CLOCK_FREQ <= 48000000) + // Slower MCUs don't require a delay + return; + uint32_t end = timer_read_time() + nsecs_to_ticks(nsecs); + while (timer_is_before(timer_read_time(), end)) + irq_poll(); +} + #define SYNC_CMD 0xf8 #define SYNC_DATA 0xfa @@ -35,7 +53,9 @@ st7920_xmit_byte(struct st7920 *s, uint8_t data) gpio_out_toggle(sid); data = ~data; } + ndelay(200); gpio_out_toggle(sclk); + ndelay(200); data <<= 1; gpio_out_toggle(sclk); }