Skip to content

Commit 206e1b7

Browse files
fmannoadbridge
authored andcommitted
STM32s Serial does not properly handle parity bits
Reworked the serial_format() function for STM32F0x devices to take the format in the form: data_bits - parity - stop_bits E.g. 8 - N - 1 where data_bits exclude the parity bit. Added a case for 7 bits data as at least the chips STM32F0x1/STM32F0x2/STM32F0x8 support 7 bits data. Consolidated serial_format() and uart_init() functions into a general TARGET_STM serial_api.c file since the functions are common to all STM targets. Fixes #4189
1 parent b1f3ef4 commit 206e1b7

File tree

11 files changed

+206
-664
lines changed

11 files changed

+206
-664
lines changed

targets/TARGET_STM/TARGET_STM32F0/serial_api.c renamed to targets/TARGET_STM/TARGET_STM32F0/serial_device.c

Lines changed: 4 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*/
3030
#include "mbed_assert.h"
3131
#include "serial_api.h"
32+
#include "serial_api_hal.h"
3233

3334
#if DEVICE_SERIAL
3435

@@ -49,52 +50,13 @@
4950
#endif
5051

5152
static uint32_t serial_irq_ids[UART_NUM] = {0};
52-
static UART_HandleTypeDef uart_handlers[UART_NUM];
53+
UART_HandleTypeDef uart_handlers[UART_NUM];
5354

5455
static uart_irq_handler irq_handler;
5556

5657
int stdio_uart_inited = 0;
5758
serial_t stdio_uart;
5859

59-
#if DEVICE_SERIAL_ASYNCH
60-
#define SERIAL_S(obj) (&((obj)->serial))
61-
#else
62-
#define SERIAL_S(obj) (obj)
63-
#endif
64-
65-
static void init_uart(serial_t *obj)
66-
{
67-
struct serial_s *obj_s = SERIAL_S(obj);
68-
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
69-
huart->Instance = (USART_TypeDef *)(obj_s->uart);
70-
71-
huart->Init.BaudRate = obj_s->baudrate;
72-
huart->Init.WordLength = obj_s->databits;
73-
huart->Init.StopBits = obj_s->stopbits;
74-
huart->Init.Parity = obj_s->parity;
75-
#if DEVICE_SERIAL_FC
76-
huart->Init.HwFlowCtl = obj_s->hw_flow_ctl;
77-
#else
78-
huart->Init.HwFlowCtl = UART_HWCONTROL_NONE;
79-
#endif
80-
huart->TxXferCount = 0;
81-
huart->TxXferSize = 0;
82-
huart->RxXferCount = 0;
83-
huart->RxXferSize = 0;
84-
85-
if (obj_s->pin_rx == NC) {
86-
huart->Init.Mode = UART_MODE_TX;
87-
} else if (obj_s->pin_tx == NC) {
88-
huart->Init.Mode = UART_MODE_RX;
89-
} else {
90-
huart->Init.Mode = UART_MODE_TX_RX;
91-
}
92-
93-
if (HAL_UART_Init(huart) != HAL_OK) {
94-
error("Cannot initialize UART\n");
95-
}
96-
}
97-
9860
void serial_init(serial_t *obj, PinName tx, PinName rx)
9961
{
10062
struct serial_s *obj_s = SERIAL_S(obj);
@@ -293,39 +255,6 @@ void serial_baud(serial_t *obj, int baudrate)
293255
init_uart(obj);
294256
}
295257

296-
void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
297-
{
298-
struct serial_s *obj_s = SERIAL_S(obj);
299-
300-
if (data_bits == 9) {
301-
obj_s->databits = UART_WORDLENGTH_9B;
302-
} else {
303-
obj_s->databits = UART_WORDLENGTH_8B;
304-
}
305-
306-
switch (parity) {
307-
case ParityOdd:
308-
obj_s->parity = UART_PARITY_ODD;
309-
break;
310-
case ParityEven:
311-
obj_s->parity = UART_PARITY_EVEN;
312-
break;
313-
default: // ParityNone
314-
case ParityForced0: // unsupported!
315-
case ParityForced1: // unsupported!
316-
obj_s->parity = UART_PARITY_NONE;
317-
break;
318-
}
319-
320-
if (stop_bits == 2) {
321-
obj_s->stopbits = UART_STOPBITS_2;
322-
} else {
323-
obj_s->stopbits = UART_STOPBITS_1;
324-
}
325-
326-
init_uart(obj);
327-
}
328-
329258
/******************************************************************************
330259
* INTERRUPTS HANDLING
331260
******************************************************************************/
@@ -345,6 +274,7 @@ static void uart_irq(int id)
345274
if (__HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE) != RESET) {
346275
irq_handler(serial_irq_ids[id], RxIrq);
347276
volatile uint32_t tmpval = huart->Instance->RDR; // Clear RXNE flag
277+
UNUSED(tmpval);
348278
}
349279
}
350280
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) != RESET) {
@@ -975,6 +905,7 @@ void serial_rx_abort_asynch(serial_t *obj)
975905
// clear flags
976906
__HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF | UART_CLEAR_FEF | UART_CLEAR_OREF);
977907
volatile uint32_t tmpval = huart->Instance->RDR; // Clear RXNE flag
908+
UNUSED(tmpval);
978909

979910
// reset states
980911
huart->RxXferCount = 0;

targets/TARGET_STM/TARGET_STM32F1/serial_api.c renamed to targets/TARGET_STM/TARGET_STM32F1/serial_device.c

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*/
3030
#include "mbed_assert.h"
3131
#include "serial_api.h"
32+
#include "serial_api_hal.h"
3233

3334
#if DEVICE_SERIAL
3435

@@ -41,52 +42,13 @@
4142
#define UART_NUM (3)
4243

4344
static uint32_t serial_irq_ids[UART_NUM] = {0};
44-
static UART_HandleTypeDef uart_handlers[UART_NUM];
45+
UART_HandleTypeDef uart_handlers[UART_NUM];
4546

4647
static uart_irq_handler irq_handler;
4748

4849
int stdio_uart_inited = 0;
4950
serial_t stdio_uart;
5051

51-
#if DEVICE_SERIAL_ASYNCH
52-
#define SERIAL_S(obj) (&((obj)->serial))
53-
#else
54-
#define SERIAL_S(obj) (obj)
55-
#endif
56-
57-
static void init_uart(serial_t *obj)
58-
{
59-
struct serial_s *obj_s = SERIAL_S(obj);
60-
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
61-
huart->Instance = (USART_TypeDef *)(obj_s->uart);
62-
63-
huart->Init.BaudRate = obj_s->baudrate;
64-
huart->Init.WordLength = obj_s->databits;
65-
huart->Init.StopBits = obj_s->stopbits;
66-
huart->Init.Parity = obj_s->parity;
67-
#if DEVICE_SERIAL_FC
68-
huart->Init.HwFlowCtl = obj_s->hw_flow_ctl;
69-
#else
70-
huart->Init.HwFlowCtl = UART_HWCONTROL_NONE;
71-
#endif
72-
huart->TxXferCount = 0;
73-
huart->TxXferSize = 0;
74-
huart->RxXferCount = 0;
75-
huart->RxXferSize = 0;
76-
77-
if (obj_s->pin_rx == NC) {
78-
huart->Init.Mode = UART_MODE_TX;
79-
} else if (obj_s->pin_tx == NC) {
80-
huart->Init.Mode = UART_MODE_RX;
81-
} else {
82-
huart->Init.Mode = UART_MODE_TX_RX;
83-
}
84-
85-
if (HAL_UART_Init(huart) != HAL_OK) {
86-
error("Cannot initialize UART\n");
87-
}
88-
}
89-
9052
void serial_init(serial_t *obj, PinName tx, PinName rx)
9153
{
9254
struct serial_s *obj_s = SERIAL_S(obj);
@@ -188,39 +150,6 @@ void serial_baud(serial_t *obj, int baudrate)
188150
init_uart(obj);
189151
}
190152

191-
void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
192-
{
193-
struct serial_s *obj_s = SERIAL_S(obj);
194-
195-
if (data_bits == 9) {
196-
obj_s->databits = UART_WORDLENGTH_9B;
197-
} else {
198-
obj_s->databits = UART_WORDLENGTH_8B;
199-
}
200-
201-
switch (parity) {
202-
case ParityOdd:
203-
obj_s->parity = UART_PARITY_ODD;
204-
break;
205-
case ParityEven:
206-
obj_s->parity = UART_PARITY_EVEN;
207-
break;
208-
default: // ParityNone
209-
case ParityForced0: // unsupported!
210-
case ParityForced1: // unsupported!
211-
obj_s->parity = UART_PARITY_NONE;
212-
break;
213-
}
214-
215-
if (stop_bits == 2) {
216-
obj_s->stopbits = UART_STOPBITS_2;
217-
} else {
218-
obj_s->stopbits = UART_STOPBITS_1;
219-
}
220-
221-
init_uart(obj);
222-
}
223-
224153
/******************************************************************************
225154
* INTERRUPTS HANDLING
226155
******************************************************************************/

targets/TARGET_STM/TARGET_STM32F2/serial_api.c renamed to targets/TARGET_STM/TARGET_STM32F2/serial_device.c

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
*/
3030
#include "mbed_assert.h"
3131
#include "serial_api.h"
32+
#include "serial_api_hal.h"
3233

3334
#if DEVICE_SERIAL
3435

@@ -41,52 +42,13 @@
4142
#define UART_NUM (8)
4243

4344
static uint32_t serial_irq_ids[UART_NUM] = {0};
44-
static UART_HandleTypeDef uart_handlers[UART_NUM];
45+
UART_HandleTypeDef uart_handlers[UART_NUM];
4546

4647
static uart_irq_handler irq_handler;
4748

4849
int stdio_uart_inited = 0;
4950
serial_t stdio_uart;
5051

51-
#if DEVICE_SERIAL_ASYNCH
52-
#define SERIAL_S(obj) (&((obj)->serial))
53-
#else
54-
#define SERIAL_S(obj) (obj)
55-
#endif
56-
57-
static void init_uart(serial_t *obj)
58-
{
59-
struct serial_s *obj_s = SERIAL_S(obj);
60-
UART_HandleTypeDef *huart = &uart_handlers[obj_s->index];
61-
huart->Instance = (USART_TypeDef *)(obj_s->uart);
62-
63-
huart->Init.BaudRate = obj_s->baudrate;
64-
huart->Init.WordLength = obj_s->databits;
65-
huart->Init.StopBits = obj_s->stopbits;
66-
huart->Init.Parity = obj_s->parity;
67-
#if DEVICE_SERIAL_FC
68-
huart->Init.HwFlowCtl = obj_s->hw_flow_ctl;
69-
#else
70-
huart->Init.HwFlowCtl = UART_HWCONTROL_NONE;
71-
#endif
72-
huart->TxXferCount = 0;
73-
huart->TxXferSize = 0;
74-
huart->RxXferCount = 0;
75-
huart->RxXferSize = 0;
76-
77-
if (obj_s->pin_rx == NC) {
78-
huart->Init.Mode = UART_MODE_TX;
79-
} else if (obj_s->pin_tx == NC) {
80-
huart->Init.Mode = UART_MODE_RX;
81-
} else {
82-
huart->Init.Mode = UART_MODE_TX_RX;
83-
}
84-
85-
if (HAL_UART_Init(huart) != HAL_OK) {
86-
error("Cannot initialize UART\n");
87-
}
88-
}
89-
9052
void serial_init(serial_t *obj, PinName tx, PinName rx)
9153
{
9254
struct serial_s *obj_s = SERIAL_S(obj);
@@ -275,39 +237,6 @@ void serial_baud(serial_t *obj, int baudrate)
275237
init_uart(obj);
276238
}
277239

278-
void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
279-
{
280-
struct serial_s *obj_s = SERIAL_S(obj);
281-
282-
if (data_bits == 9) {
283-
obj_s->databits = UART_WORDLENGTH_9B;
284-
} else {
285-
obj_s->databits = UART_WORDLENGTH_8B;
286-
}
287-
288-
switch (parity) {
289-
case ParityOdd:
290-
obj_s->parity = UART_PARITY_ODD;
291-
break;
292-
case ParityEven:
293-
obj_s->parity = UART_PARITY_EVEN;
294-
break;
295-
default: // ParityNone
296-
case ParityForced0: // unsupported!
297-
case ParityForced1: // unsupported!
298-
obj_s->parity = UART_PARITY_NONE;
299-
break;
300-
}
301-
302-
if (stop_bits == 2) {
303-
obj_s->stopbits = UART_STOPBITS_2;
304-
} else {
305-
obj_s->stopbits = UART_STOPBITS_1;
306-
}
307-
308-
init_uart(obj);
309-
}
310-
311240
/******************************************************************************
312241
* INTERRUPTS HANDLING
313242
******************************************************************************/

0 commit comments

Comments
 (0)