Skip to content

Commit 7fd5119

Browse files
authored
Merge pull request #12341 from fkjagodzinski/fix-stm-hal_fpga
STM32L4: Fix the UART RX & TX data reg bitmasks
2 parents 6777288 + 2368a07 commit 7fd5119

File tree

8 files changed

+112
-40
lines changed

8 files changed

+112
-40
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_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)

targets/TARGET_STM/TARGET_STM32WB/serial_device.c

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

149+
/* Computation of UART mask to apply to RDR register */
150+
UART_MASK_COMPUTATION(huart);
151+
uint16_t uhMask = huart->Mask;
152+
149153
while (!serial_readable(obj));
150-
if (obj_s->databits == UART_WORDLENGTH_8B) {
151-
return (int)(huart->Instance->RDR & (uint8_t)0xFF);
152-
} else {
153-
return (int)(huart->Instance->RDR & (uint16_t)0x1FF);
154-
}
154+
/* When receiving with the parity enabled, the value read in the MSB bit
155+
* is the received parity bit.
156+
*/
157+
return (int)(huart->Instance->RDR & uhMask);
155158
}
156159

157160
void serial_putc(serial_t *obj, int c)
@@ -160,11 +163,12 @@ void serial_putc(serial_t *obj, int c)
160163
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
161164

162165
while (!serial_writable(obj));
163-
if (obj_s->databits == UART_WORDLENGTH_8B) {
164-
huart->Instance->TDR = (uint8_t)(c & (uint8_t)0xFF);
165-
} else {
166-
huart->Instance->TDR = (uint16_t)(c & (uint16_t)0x1FF);
167-
}
166+
/* When transmitting with the parity enabled (PCE bit set to 1 in the
167+
* USART_CR1 register), the value written in the MSB (bit 7 or bit 8
168+
* depending on the data length) has no effect because it is replaced
169+
* by the parity.
170+
*/
171+
huart->Instance->TDR = (uint16_t)(c & 0x1FFU);
168172
}
169173

170174
void serial_clear(serial_t *obj)

0 commit comments

Comments
 (0)