From 94eb7cea428e83f84cf4b42fb0a49d15af9e5bd3 Mon Sep 17 00:00:00 2001 From: Antonio Borneo Date: Thu, 7 Jul 2016 12:32:36 +0200 Subject: nanohub: firmware: flush usart Entering in sleep-mode freezes several clock in the device; if usart is still transmitting, this will cause sending incorrect data. Wait for usart to complete transmission. Change-Id: Ide972e55b1e850d3310e935912697e9aa17a638c Signed-off-by: Antonio Borneo --- firmware/os/inc/usart.h | 1 + firmware/os/platform/stm32/platform.c | 3 +++ firmware/os/platform/stm32/usart.c | 11 +++++++++++ 3 files changed, 15 insertions(+) diff --git a/firmware/os/inc/usart.h b/firmware/os/inc/usart.h index ab7a477d..1ef18e50 100644 --- a/firmware/os/inc/usart.h +++ b/firmware/os/inc/usart.h @@ -63,6 +63,7 @@ void usartOpen(struct usart* __restrict usart, UsartPort port, /* port number is UsatStopBitsCfg stop_bits, UsartParityCfg parity, UsartFlowControlCfg flow_control); void usartClose(const struct usart* __restrict usart); +void usartFlush(const struct usart* __restrict usart); void usartPutchar(const struct usart* __restrict usart, char c); #ifdef __cplusplus diff --git a/firmware/os/platform/stm32/platform.c b/firmware/os/platform/stm32/platform.c index 5c8082c4..befafe9f 100644 --- a/firmware/os/platform/stm32/platform.c +++ b/firmware/os/platform/stm32/platform.c @@ -184,6 +184,9 @@ void platEarlyLogFlush(void) void platLogFlush(void *userData) { +#ifdef DEBUG_UART_UNITNO + usartFlush(&mDbgUart); +#endif #if defined(DEBUG_LOG_EVT) if (userData && mLateBoot) osEnqueueEvtOrFree(EVENT_TYPE_BIT_DISCARDABLE | EVT_DEBUG_LOG, userData, heapFree); diff --git a/firmware/os/platform/stm32/usart.c b/firmware/os/platform/stm32/usart.c index 302cb852..26f644f9 100644 --- a/firmware/os/platform/stm32/usart.c +++ b/firmware/os/platform/stm32/usart.c @@ -144,6 +144,17 @@ void usartClose(const struct usart* __restrict usart) gpioRelease(usart->tx); } +/* + * don't use this immediately after usart initialization + * the test is valid only after the first char has been sent out + */ +void usartFlush(const struct usart* __restrict usart) +{ + struct StmUsart *block = (struct StmUsart*)mUsartPorts[usart->unit]; + + while ((block->SR & 0x00c0) != 0x00c0); +} + void usartPutchar(const struct usart* __restrict usart, char c) { struct StmUsart *block = (struct StmUsart*)mUsartPorts[usart->unit]; -- cgit v1.2.3