Skip to content

Commit 2368a07

Browse files
jeromecoutantQinghao Shi
authored andcommitted
STM32: Fix the UART RX & TX data reg bitmasks
1 parent ae635d5 commit 2368a07

File tree

7 files changed

+98
-30
lines changed

7 files changed

+98
-30
lines changed

targets/TARGET_STM/TARGET_STM32F0/serial_device.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,15 @@ int serial_getc(serial_t *obj)
263263
struct serial_s *obj_s = SERIAL_S(obj);
264264
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
265265

266+
/* Computation of UART mask to apply to RDR register */
267+
UART_MASK_COMPUTATION(huart);
268+
uint16_t uhMask = huart->Mask;
269+
266270
while (!serial_readable(obj));
267-
return (int)(huart->Instance->RDR & (uint16_t)0xFF);
271+
/* When receiving with the parity enabled, the value read in the MSB bit
272+
* is the received parity bit.
273+
*/
274+
return (int)(huart->Instance->RDR & uhMask);
268275
}
269276

270277
void serial_putc(serial_t *obj, int c)
@@ -273,7 +280,12 @@ void serial_putc(serial_t *obj, int c)
273280
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
274281

275282
while (!serial_writable(obj));
276-
huart->Instance->TDR = (uint32_t)(c & (uint16_t)0xFF);
283+
/* When transmitting with the parity enabled (PCE bit set to 1 in the
284+
* USART_CR1 register), the value written in the MSB (bit 7 or bit 8
285+
* depending on the data length) has no effect because it is replaced
286+
* by the parity.
287+
*/
288+
huart->Instance->TDR = (uint16_t)(c & 0x1FFU);
277289
}
278290

279291
void serial_clear(serial_t *obj)

targets/TARGET_STM/TARGET_STM32F3/serial_device.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

213216
void 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

226230
void serial_clear(serial_t *obj)

targets/TARGET_STM/TARGET_STM32F7/serial_device.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,15 @@ int serial_getc(serial_t *obj)
234234
struct serial_s *obj_s = SERIAL_S(obj);
235235
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
236236

237+
/* Computation of UART mask to apply to RDR register */
238+
UART_MASK_COMPUTATION(huart);
239+
uint16_t uhMask = huart->Mask;
240+
237241
while (!serial_readable(obj));
238-
return (int)(huart->Instance->RDR & 0x1FF);
242+
/* When receiving with the parity enabled, the value read in the MSB bit
243+
* is the received parity bit.
244+
*/
245+
return (int)(huart->Instance->RDR & uhMask);
239246
}
240247

241248
void serial_putc(serial_t *obj, int c)
@@ -244,7 +251,12 @@ void serial_putc(serial_t *obj, int c)
244251
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
245252

246253
while (!serial_writable(obj));
247-
huart->Instance->TDR = (uint32_t)(c & 0x1FF);
254+
/* When transmitting with the parity enabled (PCE bit set to 1 in the
255+
* USART_CR1 register), the value written in the MSB (bit 7 or bit 8
256+
* depending on the data length) has no effect because it is replaced
257+
* by the parity.
258+
*/
259+
huart->Instance->TDR = (uint16_t)(c & 0x1FFU);
248260
}
249261

250262
void serial_clear(serial_t *obj)

targets/TARGET_STM/TARGET_STM32G0/serial_device.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,15 @@ int serial_getc(serial_t *obj)
190190
struct serial_s *obj_s = SERIAL_S(obj);
191191
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
192192

193+
/* Computation of UART mask to apply to RDR register */
194+
UART_MASK_COMPUTATION(huart);
195+
uint16_t uhMask = huart->Mask;
196+
193197
while (!serial_readable(obj));
194-
return (int)(huart->Instance->RDR & (uint16_t)0xFF);
198+
/* When receiving with the parity enabled, the value read in the MSB bit
199+
* is the received parity bit.
200+
*/
201+
return (int)(huart->Instance->RDR & uhMask);
195202
}
196203

197204
void serial_putc(serial_t *obj, int c)
@@ -200,7 +207,12 @@ void serial_putc(serial_t *obj, int c)
200207
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
201208

202209
while (!serial_writable(obj));
203-
huart->Instance->TDR = (uint32_t)(c & (uint16_t)0xFF);
210+
/* When transmitting with the parity enabled (PCE bit set to 1 in the
211+
* USART_CR1 register), the value written in the MSB (bit 7 or bit 8
212+
* depending on the data length) has no effect because it is replaced
213+
* by the parity.
214+
*/
215+
huart->Instance->TDR = (uint16_t)(c & 0x1FFU);
204216
}
205217

206218
void serial_clear(serial_t *obj)

targets/TARGET_STM/TARGET_STM32H7/serial_device.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,15 @@ int serial_getc(serial_t *obj)
247247
struct serial_s *obj_s = SERIAL_S(obj);
248248
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
249249

250+
/* Computation of UART mask to apply to RDR register */
251+
UART_MASK_COMPUTATION(huart);
252+
uint16_t uhMask = huart->Mask;
253+
250254
while (!serial_readable(obj));
251-
return (int)(huart->Instance->RDR & 0x1FF);
255+
/* When receiving with the parity enabled, the value read in the MSB bit
256+
* is the received parity bit.
257+
*/
258+
return (int)(huart->Instance->RDR & uhMask);
252259
}
253260

254261
void serial_putc(serial_t *obj, int c)
@@ -257,7 +264,12 @@ void serial_putc(serial_t *obj, int c)
257264
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
258265

259266
while (!serial_writable(obj));
260-
huart->Instance->TDR = (uint32_t)(c & 0x1FF);
267+
/* When transmitting with the parity enabled (PCE bit set to 1 in the
268+
* USART_CR1 register), the value written in the MSB (bit 7 or bit 8
269+
* depending on the data length) has no effect because it is replaced
270+
* by the parity.
271+
*/
272+
huart->Instance->TDR = (uint16_t)(c & 0x1FFU);
261273
}
262274

263275
void serial_clear(serial_t *obj)

targets/TARGET_STM/TARGET_STM32L0/serial_device.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,15 @@ int serial_getc(serial_t *obj)
204204
struct serial_s *obj_s = SERIAL_S(obj);
205205
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
206206

207+
/* Computation of UART mask to apply to RDR register */
208+
UART_MASK_COMPUTATION(huart);
209+
uint16_t uhMask = huart->Mask;
210+
207211
while (!serial_readable(obj));
208-
return (int)(huart->Instance->RDR & (uint16_t)0xFF);
212+
/* When receiving with the parity enabled, the value read in the MSB bit
213+
* is the received parity bit.
214+
*/
215+
return (int)(huart->Instance->RDR & uhMask);
209216
}
210217

211218
void serial_putc(serial_t *obj, int c)
@@ -214,7 +221,12 @@ void serial_putc(serial_t *obj, int c)
214221
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
215222

216223
while (!serial_writable(obj));
217-
huart->Instance->TDR = (uint32_t)(c & (uint16_t)0xFF);
224+
/* When transmitting with the parity enabled (PCE bit set to 1 in the
225+
* USART_CR1 register), the value written in the MSB (bit 7 or bit 8
226+
* depending on the data length) has no effect because it is replaced
227+
* by the parity.
228+
*/
229+
huart->Instance->TDR = (uint16_t)(c & 0x1FFU);
218230
}
219231

220232
void serial_clear(serial_t *obj)

targets/TARGET_STM/TARGET_STM32WB/serial_device.c

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

163+
/* Computation of UART mask to apply to RDR register */
164+
UART_MASK_COMPUTATION(huart);
165+
uint16_t uhMask = huart->Mask;
166+
163167
while (!serial_readable(obj));
164-
if (obj_s->databits == UART_WORDLENGTH_8B) {
165-
return (int)(huart->Instance->RDR & (uint8_t)0xFF);
166-
} else {
167-
return (int)(huart->Instance->RDR & (uint16_t)0x1FF);
168-
}
168+
/* When receiving with the parity enabled, the value read in the MSB bit
169+
* is the received parity bit.
170+
*/
171+
return (int)(huart->Instance->RDR & uhMask);
169172
}
170173

171174
void serial_putc(serial_t *obj, int c)
@@ -174,11 +177,12 @@ void serial_putc(serial_t *obj, int c)
174177
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
175178

176179
while (!serial_writable(obj));
177-
if (obj_s->databits == UART_WORDLENGTH_8B) {
178-
huart->Instance->TDR = (uint8_t)(c & (uint8_t)0xFF);
179-
} else {
180-
huart->Instance->TDR = (uint16_t)(c & (uint16_t)0x1FF);
181-
}
180+
/* When transmitting with the parity enabled (PCE bit set to 1 in the
181+
* USART_CR1 register), the value written in the MSB (bit 7 or bit 8
182+
* depending on the data length) has no effect because it is replaced
183+
* by the parity.
184+
*/
185+
huart->Instance->TDR = (uint16_t)(c & 0x1FFU);
182186
}
183187

184188
void serial_clear(serial_t *obj)

0 commit comments

Comments
 (0)