STM32: Fix the UART RX & TX data reg bitmasks

pull/12341/head
jeromecoutant 2020-02-06 15:04:11 +01:00 committed by Qinghao Shi
parent ae635d5cd4
commit 2368a07244
7 changed files with 98 additions and 30 deletions

View File

@ -263,8 +263,15 @@ int serial_getc(serial_t *obj)
struct serial_s *obj_s = SERIAL_S(obj);
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
/* Computation of UART mask to apply to RDR register */
UART_MASK_COMPUTATION(huart);
uint16_t uhMask = huart->Mask;
while (!serial_readable(obj));
return (int)(huart->Instance->RDR & (uint16_t)0xFF);
/* When receiving with the parity enabled, the value read in the MSB bit
* is the received parity bit.
*/
return (int)(huart->Instance->RDR & uhMask);
}
void serial_putc(serial_t *obj, int c)
@ -273,7 +280,12 @@ void serial_putc(serial_t *obj, int c)
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
while (!serial_writable(obj));
huart->Instance->TDR = (uint32_t)(c & (uint16_t)0xFF);
/* When transmitting with the parity enabled (PCE bit set to 1 in the
* USART_CR1 register), the value written in the MSB (bit 7 or bit 8
* depending on the data length) has no effect because it is replaced
* by the parity.
*/
huart->Instance->TDR = (uint16_t)(c & 0x1FFU);
}
void serial_clear(serial_t *obj)

View File

@ -202,12 +202,15 @@ int serial_getc(serial_t *obj)
struct serial_s *obj_s = SERIAL_S(obj);
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
/* Computation of UART mask to apply to RDR register */
UART_MASK_COMPUTATION(huart);
uint16_t uhMask = huart->Mask;
while (!serial_readable(obj));
if (obj_s->databits == UART_WORDLENGTH_8B) {
return (int)(huart->Instance->RDR & (uint8_t)0xFF);
} else {
return (int)(huart->Instance->RDR & (uint16_t)0x1FF);
}
/* When receiving with the parity enabled, the value read in the MSB bit
* is the received parity bit.
*/
return (int)(huart->Instance->RDR & uhMask);
}
void serial_putc(serial_t *obj, int c)
@ -216,11 +219,12 @@ void serial_putc(serial_t *obj, int c)
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
while (!serial_writable(obj));
if (obj_s->databits == UART_WORDLENGTH_8B) {
huart->Instance->TDR = (uint8_t)(c & (uint8_t)0xFF);
} else {
huart->Instance->TDR = (uint16_t)(c & (uint16_t)0x1FF);
}
/* When transmitting with the parity enabled (PCE bit set to 1 in the
* USART_CR1 register), the value written in the MSB (bit 7 or bit 8
* depending on the data length) has no effect because it is replaced
* by the parity.
*/
huart->Instance->TDR = (uint16_t)(c & 0x1FFU);
}
void serial_clear(serial_t *obj)

View File

@ -234,8 +234,15 @@ int serial_getc(serial_t *obj)
struct serial_s *obj_s = SERIAL_S(obj);
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
/* Computation of UART mask to apply to RDR register */
UART_MASK_COMPUTATION(huart);
uint16_t uhMask = huart->Mask;
while (!serial_readable(obj));
return (int)(huart->Instance->RDR & 0x1FF);
/* When receiving with the parity enabled, the value read in the MSB bit
* is the received parity bit.
*/
return (int)(huart->Instance->RDR & uhMask);
}
void serial_putc(serial_t *obj, int c)
@ -244,7 +251,12 @@ void serial_putc(serial_t *obj, int c)
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
while (!serial_writable(obj));
huart->Instance->TDR = (uint32_t)(c & 0x1FF);
/* When transmitting with the parity enabled (PCE bit set to 1 in the
* USART_CR1 register), the value written in the MSB (bit 7 or bit 8
* depending on the data length) has no effect because it is replaced
* by the parity.
*/
huart->Instance->TDR = (uint16_t)(c & 0x1FFU);
}
void serial_clear(serial_t *obj)

View File

@ -190,8 +190,15 @@ int serial_getc(serial_t *obj)
struct serial_s *obj_s = SERIAL_S(obj);
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
/* Computation of UART mask to apply to RDR register */
UART_MASK_COMPUTATION(huart);
uint16_t uhMask = huart->Mask;
while (!serial_readable(obj));
return (int)(huart->Instance->RDR & (uint16_t)0xFF);
/* When receiving with the parity enabled, the value read in the MSB bit
* is the received parity bit.
*/
return (int)(huart->Instance->RDR & uhMask);
}
void serial_putc(serial_t *obj, int c)
@ -200,7 +207,12 @@ void serial_putc(serial_t *obj, int c)
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
while (!serial_writable(obj));
huart->Instance->TDR = (uint32_t)(c & (uint16_t)0xFF);
/* When transmitting with the parity enabled (PCE bit set to 1 in the
* USART_CR1 register), the value written in the MSB (bit 7 or bit 8
* depending on the data length) has no effect because it is replaced
* by the parity.
*/
huart->Instance->TDR = (uint16_t)(c & 0x1FFU);
}
void serial_clear(serial_t *obj)

View File

@ -247,8 +247,15 @@ int serial_getc(serial_t *obj)
struct serial_s *obj_s = SERIAL_S(obj);
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
/* Computation of UART mask to apply to RDR register */
UART_MASK_COMPUTATION(huart);
uint16_t uhMask = huart->Mask;
while (!serial_readable(obj));
return (int)(huart->Instance->RDR & 0x1FF);
/* When receiving with the parity enabled, the value read in the MSB bit
* is the received parity bit.
*/
return (int)(huart->Instance->RDR & uhMask);
}
void serial_putc(serial_t *obj, int c)
@ -257,7 +264,12 @@ void serial_putc(serial_t *obj, int c)
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
while (!serial_writable(obj));
huart->Instance->TDR = (uint32_t)(c & 0x1FF);
/* When transmitting with the parity enabled (PCE bit set to 1 in the
* USART_CR1 register), the value written in the MSB (bit 7 or bit 8
* depending on the data length) has no effect because it is replaced
* by the parity.
*/
huart->Instance->TDR = (uint16_t)(c & 0x1FFU);
}
void serial_clear(serial_t *obj)

View File

@ -204,8 +204,15 @@ int serial_getc(serial_t *obj)
struct serial_s *obj_s = SERIAL_S(obj);
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
/* Computation of UART mask to apply to RDR register */
UART_MASK_COMPUTATION(huart);
uint16_t uhMask = huart->Mask;
while (!serial_readable(obj));
return (int)(huart->Instance->RDR & (uint16_t)0xFF);
/* When receiving with the parity enabled, the value read in the MSB bit
* is the received parity bit.
*/
return (int)(huart->Instance->RDR & uhMask);
}
void serial_putc(serial_t *obj, int c)
@ -214,7 +221,12 @@ void serial_putc(serial_t *obj, int c)
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
while (!serial_writable(obj));
huart->Instance->TDR = (uint32_t)(c & (uint16_t)0xFF);
/* When transmitting with the parity enabled (PCE bit set to 1 in the
* USART_CR1 register), the value written in the MSB (bit 7 or bit 8
* depending on the data length) has no effect because it is replaced
* by the parity.
*/
huart->Instance->TDR = (uint16_t)(c & 0x1FFU);
}
void serial_clear(serial_t *obj)

View File

@ -160,12 +160,15 @@ int serial_getc(serial_t *obj)
struct serial_s *obj_s = SERIAL_S(obj);
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
/* Computation of UART mask to apply to RDR register */
UART_MASK_COMPUTATION(huart);
uint16_t uhMask = huart->Mask;
while (!serial_readable(obj));
if (obj_s->databits == UART_WORDLENGTH_8B) {
return (int)(huart->Instance->RDR & (uint8_t)0xFF);
} else {
return (int)(huart->Instance->RDR & (uint16_t)0x1FF);
}
/* When receiving with the parity enabled, the value read in the MSB bit
* is the received parity bit.
*/
return (int)(huart->Instance->RDR & uhMask);
}
void serial_putc(serial_t *obj, int c)
@ -174,11 +177,12 @@ void serial_putc(serial_t *obj, int c)
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
while (!serial_writable(obj));
if (obj_s->databits == UART_WORDLENGTH_8B) {
huart->Instance->TDR = (uint8_t)(c & (uint8_t)0xFF);
} else {
huart->Instance->TDR = (uint16_t)(c & (uint16_t)0x1FF);
}
/* When transmitting with the parity enabled (PCE bit set to 1 in the
* USART_CR1 register), the value written in the MSB (bit 7 or bit 8
* depending on the data length) has no effect because it is replaced
* by the parity.
*/
huart->Instance->TDR = (uint16_t)(c & 0x1FFU);
}
void serial_clear(serial_t *obj)