Skip to content

Commit 47f3704

Browse files
committed
chore(uart): add HAL v2 support
Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
1 parent 0d9604d commit 47f3704

4 files changed

Lines changed: 587 additions & 323 deletions

File tree

‎cores/arduino/Serial.cpp‎

Lines changed: 83 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -389,70 +389,114 @@ int Uart::_tx_complete_irq(serial_t *obj)
389389

390390
void Uart::begin(unsigned long baud, uint16_t config)
391391
{
392-
uint32_t databits = 0;
393-
uint32_t stopbits = 0;
394-
uint32_t parity = 0;
395-
396-
_baud = baud;
397-
_config = config;
392+
#if defined(USE_HALV2_DRIVER)
393+
hal_uart_word_length_t databits = HAL_UART_WORD_LENGTH_8_BIT;
394+
hal_uart_stop_bits_t stopbits = HAL_UART_STOP_BIT_1;
395+
hal_uart_parity_t parity = HAL_UART_PARITY_NONE;
396+
397+
/* First manage parity which influences the number of data bits */
398+
if ((config & SERIAL_PARITY_MASK) == SERIAL_PARITY_ODD) {
399+
parity = HAL_UART_PARITY_ODD;
400+
} else if ((config & SERIAL_PARITY_MASK) == SERIAL_PARITY_EVEN) {
401+
parity = HAL_UART_PARITY_EVEN;
402+
}
398403

399404
// Manage databits
400405
switch (config & SERIAL_DATA_MASK) {
406+
#if defined(HAL_UART_WORD_LENGTH_7_BIT)
401407
case SERIAL_DATA_6:
402-
databits = 6;
408+
// Only 6 bits with parity supported
409+
if (parity != HAL_UART_PARITY_NONE) {
410+
databits = HAL_UART_WORD_LENGTH_7_BIT;
411+
} else {
412+
Error_Handler();
413+
}
403414
break;
415+
#endif /* HAL_UART_WORD_LENGTH_7_BIT */
404416
case SERIAL_DATA_7:
405-
databits = 7;
417+
// 7 bits with or without parity
418+
if (parity != HAL_UART_PARITY_NONE) {
419+
databits = HAL_UART_WORD_LENGTH_8_BIT;
420+
} else {
421+
#ifdef HAL_UART_WORD_LENGTH_7_BIT
422+
databits = HAL_UART_WORD_LENGTH_7_BIT;
423+
#else
424+
Error_Handler();
425+
#endif
426+
}
406427
break;
407428
case SERIAL_DATA_8:
408-
databits = 8;
429+
// 8 bits with or without parity
430+
if (parity != HAL_UART_PARITY_NONE) {
431+
databits = HAL_UART_WORD_LENGTH_9_BIT;
432+
} else {
433+
databits = HAL_UART_WORD_LENGTH_8_BIT;
434+
}
409435
break;
410436
default:
411-
databits = 0;
437+
Error_Handler();
412438
break;
413439
}
414440

415-
switch (config & SERIAL_PARITY_MASK) {
416-
case SERIAL_PARITY_ODD:
417-
parity = UART_PARITY_ODD;
418-
databits++; // word length = data bits + parity
419-
break;
420-
case SERIAL_PARITY_EVEN:
421-
parity = UART_PARITY_EVEN;
422-
databits++;
423-
break;
424-
default:
425-
parity = UART_PARITY_NONE;
426-
break;
441+
if ((config & SERIAL_STOP_BIT_MASK) == SERIAL_STOP_BIT_2) {
442+
stopbits = HAL_UART_STOP_BIT_2;
427443
}
428-
429-
switch (config & SERIAL_STOP_BIT_MASK) {
430-
case SERIAL_STOP_BIT_2:
431-
stopbits = UART_STOPBITS_2;
432-
break;
433-
default:
434-
stopbits = UART_STOPBITS_1;
435-
break;
444+
#else
445+
uint32_t databits = UART_WORDLENGTH_8B;
446+
uint32_t stopbits = UART_STOPBITS_1;
447+
uint32_t parity = UART_PARITY_NONE;
448+
449+
/* First manage parity which influences the number of data bits */
450+
if ((config & SERIAL_PARITY_MASK) == SERIAL_PARITY_ODD) {
451+
parity = UART_PARITY_ODD;
452+
} else if ((config & SERIAL_PARITY_MASK) == SERIAL_PARITY_EVEN) {
453+
parity = UART_PARITY_EVEN;
436454
}
437455

438-
switch (databits) {
439-
#ifdef UART_WORDLENGTH_7B
440-
case 7:
441-
databits = UART_WORDLENGTH_7B;
456+
// Manage databits
457+
switch (config & SERIAL_DATA_MASK) {
458+
#if defined(UART_WORDLENGTH_7B)
459+
case SERIAL_DATA_6:
460+
// Only 6 bits with parity supported
461+
if (parity != UART_PARITY_NONE) {
462+
databits = UART_WORDLENGTH_7B;
463+
} else {
464+
Error_Handler();
465+
}
442466
break;
467+
#endif /* UART_WORDLENGTH_7B */
468+
case SERIAL_DATA_7:
469+
// 7 bits with or without parity
470+
if (parity != UART_PARITY_NONE) {
471+
databits = UART_WORDLENGTH_8B;
472+
} else {
473+
#ifdef UART_WORDLENGTH_7B
474+
databits = UART_WORDLENGTH_7B;
475+
#else
476+
Error_Handler();
443477
#endif
444-
case 8:
445-
databits = UART_WORDLENGTH_8B;
478+
}
446479
break;
447-
case 9:
448-
databits = UART_WORDLENGTH_9B;
480+
case SERIAL_DATA_8:
481+
// 8 bits with or without parity
482+
if (parity != UART_PARITY_NONE) {
483+
databits = UART_WORDLENGTH_9B;
484+
} else {
485+
databits = UART_WORDLENGTH_8B;
486+
}
449487
break;
450488
default:
451-
case 0:
452489
Error_Handler();
453490
break;
454491
}
455492

493+
if ((config & SERIAL_STOP_BIT_MASK) == SERIAL_STOP_BIT_2) {
494+
stopbits = UART_STOPBITS_2;
495+
}
496+
#endif /* USE_HALV2_DRIVER */
497+
_baud = baud;
498+
_config = config;
499+
456500
_ready = uart_init(&_serial, (uint32_t)baud, databits, parity, stopbits, _rx_invert, _tx_invert, _data_invert);
457501
if (_ready) {
458502
enableHalfDuplexRx();

‎cores/arduino/Serial.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ typedef enum {
9696
#undef SERIAL_6N1
9797
#undef SERIAL_6N2
9898

99-
#ifndef UART_WORDLENGTH_7B
99+
#if !defined(UART_WORDLENGTH_7B) && !defined(HAL_UART_WORD_LENGTH_7_BIT)
100100
#undef SERIAL_7N1
101101
#undef SERIAL_7N2
102102
#undef SERIAL_6E1
@@ -201,7 +201,11 @@ class Uart : public arduino::HardwareSerial {
201201

202202
#if defined(HAL_UART_MODULE_ENABLED) && !defined(HAL_UART_MODULE_ONLY)
203203
// Could be used to mix Arduino API and STM32Cube HAL API (ex: DMA). Use at your own risk.
204+
#if defined(USE_HALV2_DRIVER)
205+
hal_uart_handle_t *getHandle(void)
206+
#else
204207
UART_HandleTypeDef *getHandle(void)
208+
#endif
205209
{
206210
return &(_serial.handle);
207211
}

‎libraries/SrcWrapper/inc/uart.h‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ struct serial_s {
6767
* to have get_serial_obj() function work as expected
6868
*/
6969
USART_TypeDef *uart;
70+
#if defined(USE_HALV2_DRIVER)
71+
hal_uart_handle_t handle;
72+
#else
7073
UART_HandleTypeDef handle;
74+
#endif
75+
7176
void (*rx_callback)(serial_t *);
7277
int (*tx_callback)(serial_t *);
7378
PinName pin_tx;
@@ -255,7 +260,11 @@ struct serial_s {
255260

256261
/* Exported macro ------------------------------------------------------------*/
257262
/* Exported functions ------------------------------------------------------- */
263+
#if defined(USE_HALV2_DRIVER)
264+
bool uart_init(serial_t *obj, uint32_t baudrate, hal_uart_word_length_t databits, hal_uart_parity_t parity, hal_uart_stop_bits_t stopbits, bool rx_invert, bool tx_invert, bool data_invert);
265+
#else
258266
bool uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t parity, uint32_t stopbits, bool rx_invert, bool tx_invert, bool data_invert);
267+
#endif
259268
void uart_deinit(serial_t *obj);
260269
#if defined(HAL_PWR_MODULE_ENABLED) && (defined(UART_IT_WUF) || defined(LPUART1_BASE))
261270
void uart_config_lowpower(serial_t *obj);
@@ -272,11 +281,21 @@ void uart_enable_rx(serial_t *obj);
272281

273282
size_t uart_debug_write(uint8_t *data, uint32_t size);
274283

284+
#if defined(LPUART1_BASE) || defined(LPUART2_BASE)
285+
#if defined(USE_HALV2_DRIVER)
286+
void uart_init_lpuart(hal_uart_handle_t *huart, uint32_t baudrate);
287+
#else
288+
void uart_init_lpuart(UART_HandleTypeDef *huart);
289+
#endif
290+
#endif
291+
292+
#if defined(USE_HALV2_DRIVER)
293+
hal_uart_prescaler_t uart_compute_prescaler(hal_uart_handle_t *huart, hal_uart_config_t *config);
294+
#endif
275295
#if defined(UART_PRESCALER_DIV1)
276296
uint32_t uart_compute_prescaler(UART_HandleTypeDef *huart);
277297
uint32_t uart_get_clock_source_freq(UART_HandleTypeDef *huart);
278298
#endif
279-
280299
#endif /* HAL_UART_MODULE_ENABLED && !HAL_UART_MODULE_ONLY */
281300
#ifdef __cplusplus
282301
}

0 commit comments

Comments
 (0)