Skip to content

Commit 9dc4505

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 b00bd29 commit 9dc4505

File tree

4 files changed

+23
-28
lines changed

4 files changed

+23
-28
lines changed

cores/arduino/HardwareSerial.cpp

+13-11
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ void HardwareSerial::configForLowPower(void)
244244
// Reconfigure properly Serial instance to use HSI as clock source
245245
end();
246246
uart_config_lowpower(&_serial);
247-
begin(_serial.baudrate, _config);
247+
begin(_baud, _config);
248248
#endif
249249
}
250250

@@ -290,8 +290,10 @@ 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;
296+
_baud = baud;
295297
_config = config;
296298

297299
// Manage databits
@@ -311,40 +313,40 @@ void HardwareSerial::begin(unsigned long baud, byte config)
311313
}
312314

313315
if ((config & 0x30) == 0x30) {
314-
_serial.parity = UART_PARITY_ODD;
316+
parity = UART_PARITY_ODD;
315317
databits++;
316318
} else if ((config & 0x20) == 0x20) {
317-
_serial.parity = UART_PARITY_EVEN;
319+
parity = UART_PARITY_EVEN;
318320
databits++;
319321
} else {
320-
_serial.parity = UART_PARITY_NONE;
322+
parity = UART_PARITY_NONE;
321323
}
322324

323325
if ((config & 0x08) == 0x08) {
324-
_serial.stopbits = UART_STOPBITS_2;
326+
stopbits = UART_STOPBITS_2;
325327
} else {
326-
_serial.stopbits = UART_STOPBITS_1;
328+
stopbits = UART_STOPBITS_1;
327329
}
328330

329331
switch (databits) {
330332
#ifdef UART_WORDLENGTH_7B
331333
case 7:
332-
_serial.databits = UART_WORDLENGTH_7B;
334+
databits = UART_WORDLENGTH_7B;
333335
break;
334336
#endif
335337
case 8:
336-
_serial.databits = UART_WORDLENGTH_8B;
338+
databits = UART_WORDLENGTH_8B;
337339
break;
338340
case 9:
339-
_serial.databits = UART_WORDLENGTH_9B;
341+
databits = UART_WORDLENGTH_9B;
340342
break;
341343
default:
342344
case 0:
343345
Error_Handler();
344346
break;
345347
}
346348

347-
uart_init(&_serial);
349+
uart_init(&_serial, (uint32_t)baud, databits, parity, stopbits);
348350
uart_attach_rx_callback(&_serial, _rx_complete_irq);
349351
}
350352

cores/arduino/HardwareSerial.h

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ class HardwareSerial : public Stream {
150150
static int _tx_complete_irq(serial_t *obj);
151151
private:
152152
uint8_t _config;
153+
unsigned long _baud;
153154
void init(void);
154155
void configForLowPower(void);
155156
};

cores/arduino/stm32/uart.c

+8-12
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

+1-5
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)