Skip to content

Commit ae635d5

Browse files
Filip Jagodzinskimprse
authored andcommitted
STM32L4: Fix the UART RX & TX data reg bitmasks
The existing logic was insufficient to properly handle odd and even parity setting, e.g. serial_getc() returned 9-bit data for 8O1 transmission format.
1 parent 7101e92 commit ae635d5

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

targets/TARGET_STM/TARGET_STM32L4/serial_device.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,15 @@ int serial_getc(serial_t *obj)
215215
struct serial_s *obj_s = SERIAL_S(obj);
216216
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
217217

218+
/* Computation of UART mask to apply to RDR register */
219+
UART_MASK_COMPUTATION(huart);
220+
uint16_t uhMask = huart->Mask;
221+
218222
while (!serial_readable(obj));
219-
if (obj_s->databits == UART_WORDLENGTH_8B) {
220-
return (int)(huart->Instance->RDR & (uint8_t)0xFF);
221-
} else {
222-
return (int)(huart->Instance->RDR & (uint16_t)0x1FF);
223-
}
223+
/* When receiving with the parity enabled, the value read in the MSB bit
224+
* is the received parity bit.
225+
*/
226+
return (int)(huart->Instance->RDR & uhMask);
224227
}
225228

226229
void serial_putc(serial_t *obj, int c)
@@ -229,11 +232,12 @@ void serial_putc(serial_t *obj, int c)
229232
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
230233

231234
while (!serial_writable(obj));
232-
if (obj_s->databits == UART_WORDLENGTH_8B) {
233-
huart->Instance->TDR = (uint8_t)(c & (uint8_t)0xFF);
234-
} else {
235-
huart->Instance->TDR = (uint16_t)(c & (uint16_t)0x1FF);
236-
}
235+
/* When transmitting with the parity enabled (PCE bit set to 1 in the
236+
* USART_CR1 register), the value written in the MSB (bit 7 or bit 8
237+
* depending on the data length) has no effect because it is replaced
238+
* by the parity.
239+
*/
240+
huart->Instance->TDR = (uint16_t)(c & 0x1FFU);
237241
}
238242

239243
void serial_clear(serial_t *obj)

0 commit comments

Comments
 (0)