@@ -202,12 +202,15 @@ int serial_getc(serial_t *obj)
202202 struct serial_s * obj_s = SERIAL_S (obj );
203203 UART_HandleTypeDef * huart = & uart_handlers [obj_s -> index ];
204204
205+ /* Computation of UART mask to apply to RDR register */
206+ UART_MASK_COMPUTATION (huart );
207+ uint16_t uhMask = huart -> Mask ;
208+
205209 while (!serial_readable (obj ));
206- if (obj_s -> databits == UART_WORDLENGTH_8B ) {
207- return (int )(huart -> Instance -> RDR & (uint8_t )0xFF );
208- } else {
209- return (int )(huart -> Instance -> RDR & (uint16_t )0x1FF );
210- }
210+ /* When receiving with the parity enabled, the value read in the MSB bit
211+ * is the received parity bit.
212+ */
213+ return (int )(huart -> Instance -> RDR & uhMask );
211214}
212215
213216void serial_putc (serial_t * obj , int c )
@@ -216,11 +219,12 @@ void serial_putc(serial_t *obj, int c)
216219 UART_HandleTypeDef * huart = & uart_handlers [obj_s -> index ];
217220
218221 while (!serial_writable (obj ));
219- if (obj_s -> databits == UART_WORDLENGTH_8B ) {
220- huart -> Instance -> TDR = (uint8_t )(c & (uint8_t )0xFF );
221- } else {
222- huart -> Instance -> TDR = (uint16_t )(c & (uint16_t )0x1FF );
223- }
222+ /* When transmitting with the parity enabled (PCE bit set to 1 in the
223+ * USART_CR1 register), the value written in the MSB (bit 7 or bit 8
224+ * depending on the data length) has no effect because it is replaced
225+ * by the parity.
226+ */
227+ huart -> Instance -> TDR = (uint16_t )(c & 0x1FFU );
224228}
225229
226230void serial_clear (serial_t * obj )
0 commit comments