From 4a99e23bccb8b6d7bc96e48fecf17cf2253b42bf Mon Sep 17 00:00:00 2001 From: Slavey Karadzhov Date: Tue, 24 Oct 2017 18:09:17 -0700 Subject: [PATCH] Fixes to the delay function. --- Sming/SmingCore/Clock.cpp | 15 ++++++++++++--- samples/LED_WS2812/app/application.cpp | 4 ++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Sming/SmingCore/Clock.cpp b/Sming/SmingCore/Clock.cpp index 6b0205ae19..8cd3ab47a5 100644 --- a/Sming/SmingCore/Clock.cpp +++ b/Sming/SmingCore/Clock.cpp @@ -25,11 +25,20 @@ void delay(uint32_t time) int quotient = time / MAX_SAFE_DELAY; int remainder = time % MAX_SAFE_DELAY; for(int i=0, max = quotient + 1; i < max ; i++) { + int microseconds = MAX_SAFE_DELAY * 1000; if(i == quotient) { - os_delay_us((uint16_t)(remainder * 1000)); + microseconds = remainder * 1000; } - else { - os_delay_us((uint16_t)(MAX_SAFE_DELAY * 1000)); + + int q = microseconds / 0xFFFF; + int r = microseconds % 0xFFFF; + for(int j=0, m=q + 1; j < m; j++ ) { + if(j == q) { + os_delay_us(r); + } + else { + os_delay_us(0xFFFF); + } } system_soft_wdt_feed (); diff --git a/samples/LED_WS2812/app/application.cpp b/samples/LED_WS2812/app/application.cpp index 1106402941..29f200af7d 100644 --- a/samples/LED_WS2812/app/application.cpp +++ b/samples/LED_WS2812/app/application.cpp @@ -9,13 +9,13 @@ void init() while (true) { char buffer1[] = "\x40\x00\x00\x00\x40\x00\x00\x00\x40"; ws2812_writergb(LED_PIN, buffer1, sizeof(buffer1)); - os_delay_us(500000); + delay(50); //We need to feed WDT. WDT.alive(); char buffer2[] = "\x00\x40\x40\x40\x00\x40\x40\x40\x00"; ws2812_writergb(LED_PIN, buffer2, sizeof(buffer2)); - os_delay_us(500000); + delay(50); } }