Skip to content

Commit 402f3f1

Browse files
committed
STM32: check for UART ongoing transfers before entering deepsleep
As suggested by Russ Butler in mbed-os issue #7328, and until there is an implementation of mbed-os issue #4408, we are implementing a workaround at HAL level to check if there is any ongoing serial transfer (which happens if HW FIFO is not yet empty). In case a transfer is ongoing, we're not entering deep sleep and return immediately.
1 parent 1e676f6 commit 402f3f1

File tree

11 files changed

+131
-0
lines changed

11 files changed

+131
-0
lines changed

targets/TARGET_STM/TARGET_STM32F0/device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@
3636
#define DEVICE_ID_LENGTH 24
3737

3838
#include "objects.h"
39+
/* WORKAROUND waiting for mbed-os issue 4408 to be addressed */
40+
#include "stm32f0xx_ll_usart.h"
3941

4042
#endif

targets/TARGET_STM/TARGET_STM32F1/device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@
3636
#define DEVICE_ID_LENGTH 24
3737

3838
#include "objects.h"
39+
/* WORKAROUND waiting for mbed-os issue 4408 to be addressed */
40+
#include "stm32f1xx_ll_usart.h"
3941

4042
#endif

targets/TARGET_STM/TARGET_STM32F2/device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,7 @@
3333
#define DEVICE_ID_LENGTH 24
3434

3535
#include "objects.h"
36+
/* WORKAROUND waiting for mbed-os issue 4408 to be addressed */
37+
#include "stm32f2xx_ll_usart.h"
3638

3739
#endif

targets/TARGET_STM/TARGET_STM32F3/device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@
3636
#define DEVICE_ID_LENGTH 24
3737

3838
#include "objects.h"
39+
/* WORKAROUND waiting for mbed-os issue 4408 to be addressed */
40+
#include "stm32f3xx_ll_usart.h"
3941

4042
#endif

targets/TARGET_STM/TARGET_STM32F4/device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@
3636
#define DEVICE_ID_LENGTH 24
3737

3838
#include "objects.h"
39+
/* WORKAROUND waiting for mbed-os issue 4408 to be addressed */
40+
#include "stm32f4xx_ll_usart.h"
3941

4042
#endif

targets/TARGET_STM/TARGET_STM32F7/device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@
3636
#define DEVICE_ID_LENGTH 24
3737

3838
#include "objects.h"
39+
/* WORKAROUND waiting for mbed-os issue 4408 to be addressed */
40+
#include "stm32f7xx_ll_usart.h"
3941

4042
#endif

targets/TARGET_STM/TARGET_STM32L0/device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@
3636
#define DEVICE_ID_LENGTH 24
3737

3838
#include "objects.h"
39+
/* WORKAROUND waiting for mbed-os issue 4408 to be addressed */
40+
#include "stm32l0xx_ll_usart.h"
3941

4042
#endif

targets/TARGET_STM/TARGET_STM32L1/device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@
3636
#define DEVICE_ID_LENGTH 24
3737

3838
#include "objects.h"
39+
/* WORKAROUND waiting for mbed-os issue 4408 to be addressed */
40+
#include "stm32l1xx_ll_usart.h"
3941

4042
#endif

targets/TARGET_STM/TARGET_STM32L4/device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@
3636
#define DEVICE_ID_LENGTH 24
3737

3838
#include "objects.h"
39+
/* WORKAROUND waiting for mbed-os issue 4408 to be addressed */
40+
#include "stm32l4xx_ll_usart.h"
3941

4042
#endif

targets/TARGET_STM/serial_api.c

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,4 +690,105 @@ int8_t get_uart_index(UARTName uart_name)
690690
return -1;
691691
}
692692

693+
/* Function to protect deep sleep while a seral Tx is ongoing on not complete
694+
* yet. Returns 1 if there is at least 1 serial instance with ongoing ransfer
695+
* 0 otherwise.
696+
*/
697+
int serial_IsTxOngoing(void) {
698+
int TxOngoing = 0;
699+
700+
#if defined(USART1_BASE)
701+
if (LL_USART_IsEnabled(USART1) && !LL_USART_IsActiveFlag_TC(USART1)) {
702+
TxOngoing |= 1;
703+
}
704+
#endif
705+
706+
#if defined(USART2_BASE)
707+
if (LL_USART_IsEnabled(USART2) && !LL_USART_IsActiveFlag_TC(USART2)) {
708+
TxOngoing |= 1;
709+
}
710+
#endif
711+
712+
#if defined(USART3_BASE)
713+
if (LL_USART_IsEnabled(USART3) && !LL_USART_IsActiveFlag_TC(USART3)) {
714+
TxOngoing |= 1;
715+
}
716+
#endif
717+
718+
#if defined(UART4_BASE)
719+
if (LL_USART_IsEnabled(UART4) && !LL_USART_IsActiveFlag_TC(UART4)) {
720+
TxOngoing |= 1;
721+
}
722+
#endif
723+
724+
#if defined(USART4_BASE)
725+
if (LL_USART_IsEnabled(USART4) && !LL_USART_IsActiveFlag_TC(USART4)) {
726+
TxOngoing |= 1;
727+
}
728+
#endif
729+
730+
#if defined(UART5_BASE)
731+
if (LL_USART_IsEnabled(UART5) && !LL_USART_IsActiveFlag_TC(UART5)) {
732+
TxOngoing |= 1;
733+
}
734+
#endif
735+
736+
#if defined(USART5_BASE)
737+
if (LL_USART_IsEnabled(USART5) && !LL_USART_IsActiveFlag_TC(USART5)) {
738+
TxOngoing |= 1;
739+
}
740+
#endif
741+
742+
#if defined(USART6_BASE)
743+
if (LL_USART_IsEnabled(USART6) && !LL_USART_IsActiveFlag_TC(USART6)) {
744+
TxOngoing |= 1;
745+
}
746+
#endif
747+
748+
#if defined(UART7_BASE)
749+
if (LL_USART_IsEnabled(UART7) && !LL_USART_IsActiveFlag_TC(UART7)) {
750+
TxOngoing |= 1;
751+
}
752+
#endif
753+
754+
#if defined(USART7_BASE)
755+
if (LL_USART_IsEnabled(USART7) && !LL_USART_IsActiveFlag_TC(USART7)) {
756+
TxOngoing |= 1;
757+
}
758+
#endif
759+
760+
#if defined(UART8_BASE)
761+
if (LL_USART_IsEnabled(UART8) && !LL_USART_IsActiveFlag_TC(UART8)) {
762+
TxOngoing |= 1;
763+
}
764+
#endif
765+
766+
#if defined(USART8_BASE)
767+
if (LL_USART_IsEnabled(USART8) && !LL_USART_IsActiveFlag_TC(USART8)) {
768+
TxOngoing |= 1;
769+
}
770+
#endif
771+
772+
#if defined(UART9_BASE)
773+
if (LL_USART_IsEnabled(UART9) && !LL_USART_IsActiveFlag_TC(UART9)) {
774+
TxOngoing |= 1;
775+
}
776+
#endif
777+
778+
#if defined(UART10_BASE)
779+
if (LL_USART_IsEnabled(UART10) && !LL_USART_IsActiveFlag_TC(UART10)) {
780+
TxOngoing |= 1;
781+
}
782+
#endif
783+
784+
#if defined(LPUART1_BASE)
785+
if (LL_USART_IsEnabled(LPUART1) && !LL_USART_IsActiveFlag_TC(LPUART1)) {
786+
TxOngoing |= 1;
787+
}
788+
#endif
789+
790+
/* If Tx is ongoing, then transfer is */
791+
return TxOngoing;
792+
}
793+
693794
#endif /* DEVICE_SERIAL */

targets/TARGET_STM/sleep.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,20 @@ void hal_sleep(void)
157157
core_util_critical_section_exit();
158158
}
159159

160+
extern int serial_IsTxOngoing(void);
161+
160162
void hal_deepsleep(void)
161163
{
164+
/* WORKAROUND:
165+
* MBED serial driver does not handle deepsleep lock
166+
* to prevent entering deepsleep until HW serial FIFO is empty.
167+
* This is tracked in mbed issue 4408.
168+
* For now, we're checking all Serial HW FIFO. If any transfer is ongoing
169+
* we're not entering deep sleep and returning immediately. */
170+
if(serial_IsTxOngoing()) {
171+
return;
172+
}
173+
162174
// Disable IRQs
163175
core_util_critical_section_enter();
164176

0 commit comments

Comments
 (0)