Skip to content

Commit 7bd9868

Browse files
adustm0xc0170
authored andcommitted
[STM32F1 F4] Fix #1705 MBED_37 (#1707)
* [STM32F1 F4] Fix #1705 MBED_37 The transmit data register needs to be flushed at the initialisation of the uart. In case previous transmission was interrupted by a uart init, uart may contain a char that will be transmitted at the next start. This is the case in MBED_37 test (serial_auto_nc_rx). The MCU is writting {{start}}\n At the moment of the \n the main program is handling 'new serial'. The next time the main program is handling a printf, the previous \n is still present in the uart->DR register and is transmitted. This cannot happen anymore with this commit * [STM32_F1] Fix #1705 MBED_37 by resetting the uart
1 parent 13f0c1f commit 7bd9868

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F1/serial_api.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ serial_t stdio_uart;
5050

5151
static void init_uart(serial_t *obj)
5252
{
53+
5354
UartHandle.Instance = (USART_TypeDef *)(obj->uart);
5455

5556
UartHandle.Init.BaudRate = obj->baudrate;
@@ -67,6 +68,7 @@ static void init_uart(serial_t *obj)
6768
}
6869

6970
HAL_UART_Init(&UartHandle);
71+
7072
}
7173

7274
void serial_init(serial_t *obj, PinName tx, PinName rx)
@@ -81,14 +83,20 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
8183

8284
// Enable UART clock
8385
if (obj->uart == UART_1) {
86+
__USART1_FORCE_RESET();
87+
__USART1_RELEASE_RESET();
8488
__HAL_RCC_USART1_CLK_ENABLE();
8589
obj->index = 0;
8690
}
8791
if (obj->uart == UART_2) {
92+
__USART2_FORCE_RESET();
93+
__USART2_RELEASE_RESET();
8894
__HAL_RCC_USART2_CLK_ENABLE();
8995
obj->index = 1;
9096
}
9197
if (obj->uart == UART_3) {
98+
__USART3_FORCE_RESET();
99+
__USART3_RELEASE_RESET();
92100
__HAL_RCC_USART3_CLK_ENABLE();
93101
obj->index = 2;
94102
}

libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F4/serial_api.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ serial_t stdio_uart;
101101

102102
static void init_uart(serial_t *obj, UARTName instance)
103103
{
104-
105104
UART_HandleTypeDef *handle = &UartHandle[SERIAL_OBJ(index)];
106105
handle->Instance = (USART_TypeDef *)instance;
107106

@@ -186,6 +185,7 @@ static void init_uart(serial_t *obj, UARTName instance)
186185
if (HAL_UART_Init(handle) != HAL_OK) {
187186
error("Cannot initialize UART\n");
188187
}
188+
189189
}
190190

191191
void serial_init(serial_t *obj, PinName tx, PinName rx)
@@ -202,13 +202,17 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
202202
// Enable USART clock
203203
switch (instance) {
204204
case UART_1:
205+
__USART1_FORCE_RESET();
206+
__USART1_RELEASE_RESET();
205207
__HAL_RCC_USART1_CLK_ENABLE();
206208
SERIAL_OBJ(index) = 0;
207209
#if DEVICE_SERIAL_ASYNCH_DMA
208210
__HAL_RCC_DMA2_CLK_ENABLE();
209211
#endif
210212
break;
211213
case UART_2:
214+
__USART2_FORCE_RESET();
215+
__USART2_RELEASE_RESET();
212216
__HAL_RCC_USART2_CLK_ENABLE();
213217
SERIAL_OBJ(index) = 1;
214218
#if DEVICE_SERIAL_ASYNCH_DMA
@@ -217,6 +221,8 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
217221
break;
218222
#if defined(USART3_BASE)
219223
case UART_3:
224+
__USART3_FORCE_RESET();
225+
__USART3_RELEASE_RESET();
220226
__HAL_RCC_USART3_CLK_ENABLE();
221227
SERIAL_OBJ(index) = 2;
222228
#if DEVICE_SERIAL_ASYNCH_DMA
@@ -226,6 +232,8 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
226232
#endif
227233
#if defined(UART4_BASE)
228234
case UART_4:
235+
__USART4_FORCE_RESET();
236+
__USART4_RELEASE_RESET();
229237
__HAL_RCC_UART4_CLK_ENABLE();
230238
SERIAL_OBJ(index) = 3;
231239
#if DEVICE_SERIAL_ASYNCH_DMA
@@ -235,6 +243,8 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
235243
#endif
236244
#if defined(UART5_BASE)
237245
case UART_5:
246+
__USART5_FORCE_RESET();
247+
__USART5_RELEASE_RESET();
238248
__HAL_RCC_UART5_CLK_ENABLE();
239249
SERIAL_OBJ(index) = 4;
240250
#if DEVICE_SERIAL_ASYNCH_DMA
@@ -244,6 +254,8 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
244254
#endif
245255
#if defined(USART6_BASE)
246256
case UART_6:
257+
__USART6_FORCE_RESET();
258+
__USART6_RELEASE_RESET();
247259
__HAL_RCC_USART6_CLK_ENABLE();
248260
SERIAL_OBJ(index) = 5;
249261
#if DEVICE_SERIAL_ASYNCH_DMA
@@ -253,6 +265,8 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
253265
#endif
254266
#if defined(UART7_BASE)
255267
case UART_7:
268+
__USART8_FORCE_RESET();
269+
__USART8_RELEASE_RESET();
256270
__HAL_RCC_UART7_CLK_ENABLE();
257271
SERIAL_OBJ(index) = 6;
258272
#if DEVICE_SERIAL_ASYNCH_DMA
@@ -262,6 +276,8 @@ void serial_init(serial_t *obj, PinName tx, PinName rx)
262276
#endif
263277
#if defined(UART8_BASE)
264278
case UART_8:
279+
__USART8_FORCE_RESET();
280+
__USART8_RELEASE_RESET();
265281
__HAL_RCC_UART8_CLK_ENABLE();
266282
SERIAL_OBJ(index) = 7;
267283
#if DEVICE_SERIAL_ASYNCH_DMA

0 commit comments

Comments
 (0)