Skip to content

Commit f2fe8c3

Browse files
committed
[UART] Reduce serial_t size
baudrate, databits, stopbits and parity are used only during init. So, they are no reason to save them in the structure. This allow to save space. Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 09d23e1 commit f2fe8c3

File tree

3 files changed

+20
-27
lines changed

3 files changed

+20
-27
lines changed

cores/arduino/HardwareSerial.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,9 @@ int HardwareSerial::_tx_complete_irq(serial_t *obj)
290290
void HardwareSerial::begin(unsigned long baud, byte config)
291291
{
292292
uint32_t databits = 0;
293+
uint32_t stopbits = 0;
294+
uint32_t parity = 0;
293295

294-
_serial.baudrate = (uint32_t)baud;
295296
_config = config;
296297

297298
// Manage databits
@@ -311,40 +312,40 @@ void HardwareSerial::begin(unsigned long baud, byte config)
311312
}
312313

313314
if ((config & 0x30) == 0x30) {
314-
_serial.parity = UART_PARITY_ODD;
315+
parity = UART_PARITY_ODD;
315316
databits++;
316317
} else if ((config & 0x20) == 0x20) {
317-
_serial.parity = UART_PARITY_EVEN;
318+
parity = UART_PARITY_EVEN;
318319
databits++;
319320
} else {
320-
_serial.parity = UART_PARITY_NONE;
321+
parity = UART_PARITY_NONE;
321322
}
322323

323324
if ((config & 0x08) == 0x08) {
324-
_serial.stopbits = UART_STOPBITS_2;
325+
stopbits = UART_STOPBITS_2;
325326
} else {
326-
_serial.stopbits = UART_STOPBITS_1;
327+
stopbits = UART_STOPBITS_1;
327328
}
328329

329330
switch (databits) {
330331
#ifdef UART_WORDLENGTH_7B
331332
case 7:
332-
_serial.databits = UART_WORDLENGTH_7B;
333+
databits = UART_WORDLENGTH_7B;
333334
break;
334335
#endif
335336
case 8:
336-
_serial.databits = UART_WORDLENGTH_8B;
337+
databits = UART_WORDLENGTH_8B;
337338
break;
338339
case 9:
339-
_serial.databits = UART_WORDLENGTH_9B;
340+
databits = UART_WORDLENGTH_9B;
340341
break;
341342
default:
342343
case 0:
343344
Error_Handler();
344345
break;
345346
}
346347

347-
uart_init(&_serial);
348+
uart_init(&_serial, (uint32_t)baud, databits, parity, stopbits);
348349
uart_attach_rx_callback(&_serial, _rx_complete_irq);
349350
}
350351

cores/arduino/stm32/uart.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ serial_t *get_serial_obj(UART_HandleTypeDef *huart)
118118
* @param obj : pointer to serial_t structure
119119
* @retval None
120120
*/
121-
void uart_init(serial_t *obj)
121+
void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t parity, uint32_t stopbits)
122122
{
123123
if (obj == NULL) {
124124
return;
@@ -293,10 +293,10 @@ void uart_init(serial_t *obj)
293293
/* Configure uart */
294294
uart_handlers[obj->index] = huart;
295295
huart->Instance = (USART_TypeDef *)(obj->uart);
296-
huart->Init.BaudRate = obj->baudrate;
297-
huart->Init.WordLength = obj->databits;
298-
huart->Init.StopBits = obj->stopbits;
299-
huart->Init.Parity = obj->parity;
296+
huart->Init.BaudRate = baudrate;
297+
huart->Init.WordLength = databits;
298+
huart->Init.StopBits = stopbits;
299+
huart->Init.Parity = parity;
300300
huart->Init.Mode = UART_MODE_TX_RX;
301301
huart->Init.HwFlowCtl = UART_HWCONTROL_NONE;
302302
huart->Init.OverSampling = UART_OVERSAMPLING_16;
@@ -315,7 +315,7 @@ void uart_init(serial_t *obj)
315315
* check Reference Manual
316316
*/
317317
if (obj->uart == LPUART1) {
318-
if (obj->baudrate <= 9600) {
318+
if (baudrate <= 9600) {
319319
#if defined(USART_CR3_UCESM)
320320
HAL_UARTEx_EnableClockStopMode(huart);
321321
#endif
@@ -332,7 +332,7 @@ void uart_init(serial_t *obj)
332332
}
333333
/* Trying to change LPUART clock source */
334334
/* If baudrate is lower than or equal to 9600 try to change to LSE */
335-
if (obj->baudrate <= 9600) {
335+
if (baudrate <= 9600) {
336336
/* Enable the clock if not already set by user */
337337
enableClock(LSE_CLOCK);
338338

@@ -578,12 +578,8 @@ void uart_debug_init(void)
578578
#else
579579
serial_debug.pin_tx = pinmap_pin(DEBUG_UART, PinMap_UART_TX);
580580
#endif
581-
serial_debug.baudrate = DEBUG_UART_BAUDRATE;
582-
serial_debug.parity = UART_PARITY_NONE;
583-
serial_debug.databits = UART_WORDLENGTH_8B;
584-
serial_debug.stopbits = UART_STOPBITS_1;
585581

586-
uart_init(&serial_debug);
582+
uart_init(&serial_debug, DEBUG_UART_BAUDRATE, UART_WORDLENGTH_8B, UART_PARITY_NONE, UART_STOPBITS_1);
587583
}
588584
}
589585

cores/arduino/stm32/uart.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ struct serial_s {
6262
UART_HandleTypeDef handle;
6363
void (*rx_callback)(serial_t *);
6464
int (*tx_callback)(serial_t *);
65-
uint32_t baudrate;
66-
uint32_t databits;
67-
uint32_t stopbits;
68-
uint32_t parity;
6965
PinName pin_tx;
7066
PinName pin_rx;
7167
IRQn_Type irq;
@@ -165,7 +161,7 @@ struct serial_s {
165161

166162
/* Exported macro ------------------------------------------------------------*/
167163
/* Exported functions ------------------------------------------------------- */
168-
void uart_init(serial_t *obj);
164+
void uart_init(serial_t *obj, uint32_t baudrate, uint32_t databits, uint32_t parity, uint32_t stopbits);
169165
void uart_deinit(serial_t *obj);
170166
#if defined(HAL_PWR_MODULE_ENABLED) && defined(UART_IT_WUF)
171167
void uart_config_lowpower(serial_t *obj);

0 commit comments

Comments
 (0)