Skip to content

Commit 78ba608

Browse files
nordic-krchcarlescufi
authored andcommitted
drivers: serial: nrfx_uarte: Optimize static peripheral configuration
When runtime configuration is not enable then we can determine at compile time what are the hardware settings. Function which translates zephyr values to nRF register values is not needed as macros can do that at compile time. This optimization saves almost 400 bytes of code. Signed-off-by: Krzysztof Chruściński <[email protected]>
1 parent 52f2668 commit 78ba608

File tree

1 file changed

+102
-90
lines changed

1 file changed

+102
-90
lines changed

drivers/serial/uart_nrfx_uarte.c

Lines changed: 102 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ struct uarte_nrfx_int_driven {
155155
/* Device data structure */
156156
struct uarte_nrfx_data {
157157
const struct device *dev;
158+
#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
158159
struct uart_config uart_config;
160+
#endif
159161
#ifdef UARTE_INTERRUPT_DRIVEN
160162
struct uarte_nrfx_int_driven *int_driven;
161163
#endif
@@ -182,6 +184,31 @@ struct uarte_nrfx_data {
182184
*/
183185
#define UARTE_CFG_FLAG_LOW_POWER BIT(4)
184186

187+
/* Macro for converting numerical baudrate to register value. It is convenient
188+
* to use this approach because for constant input it can calculate nrf setting
189+
* at compile time.
190+
*/
191+
#define NRF_BAUDRATE(baudrate) ((baudrate) == 300 ? 0x00014000 :\
192+
(baudrate) == 600 ? 0x00027000 : \
193+
(baudrate) == 1200 ? NRF_UARTE_BAUDRATE_1200 : \
194+
(baudrate) == 2400 ? NRF_UARTE_BAUDRATE_2400 : \
195+
(baudrate) == 4800 ? NRF_UARTE_BAUDRATE_4800 : \
196+
(baudrate) == 9600 ? NRF_UARTE_BAUDRATE_9600 : \
197+
(baudrate) == 14400 ? NRF_UARTE_BAUDRATE_14400 : \
198+
(baudrate) == 19200 ? NRF_UARTE_BAUDRATE_19200 : \
199+
(baudrate) == 28800 ? NRF_UARTE_BAUDRATE_28800 : \
200+
(baudrate) == 31250 ? NRF_UARTE_BAUDRATE_31250 : \
201+
(baudrate) == 38400 ? NRF_UARTE_BAUDRATE_38400 : \
202+
(baudrate) == 56000 ? NRF_UARTE_BAUDRATE_56000 : \
203+
(baudrate) == 57600 ? NRF_UARTE_BAUDRATE_57600 : \
204+
(baudrate) == 76800 ? NRF_UARTE_BAUDRATE_76800 : \
205+
(baudrate) == 115200 ? NRF_UARTE_BAUDRATE_115200 : \
206+
(baudrate) == 230400 ? NRF_UARTE_BAUDRATE_230400 : \
207+
(baudrate) == 250000 ? NRF_UARTE_BAUDRATE_250000 : \
208+
(baudrate) == 460800 ? NRF_UARTE_BAUDRATE_460800 : \
209+
(baudrate) == 921600 ? NRF_UARTE_BAUDRATE_921600 : \
210+
(baudrate) == 1000000 ? NRF_UARTE_BAUDRATE_1000000 : 0)
211+
185212
/**
186213
* @brief Structure for UARTE configuration.
187214
*/
@@ -191,6 +218,10 @@ struct uarte_nrfx_config {
191218
uint32_t flags;
192219
bool disable_rx;
193220
const struct pinctrl_dev_config *pcfg;
221+
#ifndef CONFIG_UART_USE_RUNTIME_CONFIGURE
222+
nrf_uarte_baudrate_t baudrate;
223+
nrf_uarte_config_t hw_config;
224+
#endif
194225
#ifdef UARTE_ANY_ASYNC
195226
nrfx_timer_t timer;
196227
#endif
@@ -290,6 +321,7 @@ static void uarte_nrfx_isr_int(const void *arg)
290321
}
291322
#endif /* UARTE_ANY_NONE_ASYNC */
292323

324+
#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
293325
/**
294326
* @brief Set the baud rate
295327
*
@@ -303,77 +335,11 @@ static void uarte_nrfx_isr_int(const void *arg)
303335
static int baudrate_set(const struct device *dev, uint32_t baudrate)
304336
{
305337
const struct uarte_nrfx_config *config = dev->config;
306-
nrf_uarte_baudrate_t nrf_baudrate; /* calculated baudrate divisor */
338+
/* calculated baudrate divisor */
339+
nrf_uarte_baudrate_t nrf_baudrate = NRF_BAUDRATE(baudrate);
307340
NRF_UARTE_Type *uarte = get_uarte_instance(dev);
308341

309-
switch (baudrate) {
310-
case 300:
311-
/* value not supported by Nordic HAL */
312-
nrf_baudrate = 0x00014000;
313-
break;
314-
case 600:
315-
/* value not supported by Nordic HAL */
316-
nrf_baudrate = 0x00027000;
317-
break;
318-
case 1200:
319-
nrf_baudrate = NRF_UARTE_BAUDRATE_1200;
320-
break;
321-
case 2400:
322-
nrf_baudrate = NRF_UARTE_BAUDRATE_2400;
323-
break;
324-
case 4800:
325-
nrf_baudrate = NRF_UARTE_BAUDRATE_4800;
326-
break;
327-
case 9600:
328-
nrf_baudrate = NRF_UARTE_BAUDRATE_9600;
329-
break;
330-
case 14400:
331-
nrf_baudrate = NRF_UARTE_BAUDRATE_14400;
332-
break;
333-
case 19200:
334-
nrf_baudrate = NRF_UARTE_BAUDRATE_19200;
335-
break;
336-
case 28800:
337-
nrf_baudrate = NRF_UARTE_BAUDRATE_28800;
338-
break;
339-
#if defined(UARTE_BAUDRATE_BAUDRATE_Baud31250)
340-
case 31250:
341-
nrf_baudrate = NRF_UARTE_BAUDRATE_31250;
342-
break;
343-
#endif
344-
case 38400:
345-
nrf_baudrate = NRF_UARTE_BAUDRATE_38400;
346-
break;
347-
#if defined(UARTE_BAUDRATE_BAUDRATE_Baud56000)
348-
case 56000:
349-
nrf_baudrate = NRF_UARTE_BAUDRATE_56000;
350-
break;
351-
#endif
352-
case 57600:
353-
nrf_baudrate = NRF_UARTE_BAUDRATE_57600;
354-
break;
355-
case 76800:
356-
nrf_baudrate = NRF_UARTE_BAUDRATE_76800;
357-
break;
358-
case 115200:
359-
nrf_baudrate = NRF_UARTE_BAUDRATE_115200;
360-
break;
361-
case 230400:
362-
nrf_baudrate = NRF_UARTE_BAUDRATE_230400;
363-
break;
364-
case 250000:
365-
nrf_baudrate = NRF_UARTE_BAUDRATE_250000;
366-
break;
367-
case 460800:
368-
nrf_baudrate = NRF_UARTE_BAUDRATE_460800;
369-
break;
370-
case 921600:
371-
nrf_baudrate = NRF_UARTE_BAUDRATE_921600;
372-
break;
373-
case 1000000:
374-
nrf_baudrate = NRF_UARTE_BAUDRATE_1000000;
375-
break;
376-
default:
342+
if (nrf_baudrate == 0) {
377343
return -EINVAL;
378344
}
379345

@@ -460,7 +426,6 @@ static int uarte_nrfx_configure(const struct device *dev,
460426
return 0;
461427
}
462428

463-
#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
464429
static int uarte_nrfx_config_get(const struct device *dev,
465430
struct uart_config *cfg)
466431
{
@@ -781,6 +746,19 @@ static bool setup_tx_cache(struct uarte_nrfx_data *data)
781746
return true;
782747
}
783748

749+
static bool has_hwfc(const struct device *dev)
750+
{
751+
#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
752+
struct uarte_nrfx_data *data = dev->data;
753+
754+
return data->uart_config.flow_ctrl == UART_CFG_FLOW_CTRL_RTS_CTS;
755+
#else
756+
const struct uarte_nrfx_config *config = dev->config;
757+
758+
return config->hw_config.hwfc == NRF_UARTE_HWFC_ENABLED;
759+
#endif
760+
}
761+
784762
static int uarte_nrfx_tx(const struct device *dev, const uint8_t *buf,
785763
size_t len,
786764
int32_t timeout)
@@ -811,10 +789,8 @@ static int uarte_nrfx_tx(const struct device *dev, const uint8_t *buf,
811789

812790
irq_unlock(key);
813791

814-
if (data->uart_config.flow_ctrl == UART_CFG_FLOW_CTRL_RTS_CTS
815-
&& timeout != SYS_FOREVER_US) {
816-
k_timer_start(&data->async->tx_timeout_timer, K_USEC(timeout),
817-
K_NO_WAIT);
792+
if (has_hwfc(dev) && timeout != SYS_FOREVER_US) {
793+
k_timer_start(&data->async->tx_timeout_timer, K_USEC(timeout), K_NO_WAIT);
818794
}
819795
return 0;
820796
}
@@ -1811,10 +1787,15 @@ static int uarte_instance_init(const struct device *dev,
18111787
return err;
18121788
}
18131789

1790+
#ifdef CONFIG_UART_USE_RUNTIME_CONFIGURE
18141791
err = uarte_nrfx_configure(dev, &data->uart_config);
18151792
if (err) {
18161793
return err;
18171794
}
1795+
#else
1796+
nrf_uarte_baudrate_set(uarte, cfg->baudrate);
1797+
nrf_uarte_configure(uarte, &cfg->hw_config);
1798+
#endif
18181799

18191800
if (IS_ENABLED(UARTE_ENHANCED_POLL_OUT) &&
18201801
cfg->flags & UARTE_CFG_FLAG_PPI_ENDTX) {
@@ -2033,6 +2014,43 @@ static int uarte_nrfx_pm_action(const struct device *dev,
20332014
#define UARTE_DISABLE_RX_INIT(node_id) \
20342015
.disable_rx = DT_PROP(node_id, disable_rx)
20352016

2017+
#define UARTE_GET_FREQ(idx) DT_PROP(DT_CLOCKS_CTLR(UARTE(idx)), clock_frequency)
2018+
2019+
#define UARTE_GET_BAUDRATE_DIV(idx) \
2020+
COND_CODE_1(DT_CLOCKS_HAS_IDX(UARTE(idx), 0), \
2021+
((UARTE_GET_FREQ(idx) / NRF_UARTE_BASE_FREQUENCY_16MHZ)), (1))
2022+
2023+
/* When calculating baudrate we need to take into account that some instances
2024+
* must have baudrate adjusted to the ratio between UARTE clocking frequency and 16 MHz.
2025+
*/
2026+
#define UARTE_GET_BAUDRATE(idx) \
2027+
(NRF_BAUDRATE(UARTE_PROP(idx, current_speed)) / UARTE_GET_BAUDRATE_DIV(idx))
2028+
2029+
/* Macro for setting nRF specific configuration structures. */
2030+
#define UARTE_NRF_CONFIG(idx) { \
2031+
.hwfc = (UARTE_PROP(idx, hw_flow_control) == \
2032+
UART_CFG_FLOW_CTRL_RTS_CTS) ? \
2033+
NRF_UARTE_HWFC_ENABLED : NRF_UARTE_HWFC_DISABLED, \
2034+
.parity = IS_ENABLED(CONFIG_UART_##idx##_NRF_PARITY_BIT) ? \
2035+
NRF_UARTE_PARITY_INCLUDED : NRF_UARTE_PARITY_EXCLUDED, \
2036+
IF_ENABLED(UARTE_HAS_STOP_CONFIG, (.stop = NRF_UARTE_STOP_ONE,))\
2037+
IF_ENABLED(UARTE_ODD_PARITY_ALLOWED, \
2038+
(.paritytype = NRF_UARTE_PARITYTYPE_EVEN,)) \
2039+
}
2040+
2041+
/* Macro for setting zephyr specific configuration structures. */
2042+
#define UARTE_CONFIG(idx) { \
2043+
.baudrate = UARTE_PROP(idx, current_speed), \
2044+
.data_bits = UART_CFG_DATA_BITS_8, \
2045+
.stop_bits = UART_CFG_STOP_BITS_1, \
2046+
.parity = IS_ENABLED(CONFIG_UART_##idx##_NRF_PARITY_BIT) \
2047+
? UART_CFG_PARITY_EVEN \
2048+
: UART_CFG_PARITY_NONE, \
2049+
.flow_ctrl = UARTE_PROP(idx, hw_flow_control) \
2050+
? UART_CFG_FLOW_CTRL_RTS_CTS \
2051+
: UART_CFG_FLOW_CTRL_NONE, \
2052+
}
2053+
20362054
#define UART_NRF_UARTE_DEVICE(idx) \
20372055
NRF_DT_CHECK_NODE_HAS_PINCTRL_SLEEP(UARTE(idx)); \
20382056
UARTE_INT_DRIVEN(idx); \
@@ -2041,13 +2059,22 @@ static int uarte_nrfx_pm_action(const struct device *dev,
20412059
static uint8_t uarte##idx##_char_out UARTE_MEMORY_SECTION(idx); \
20422060
static uint8_t uarte##idx##_rx_data UARTE_MEMORY_SECTION(idx); \
20432061
static struct uarte_nrfx_data uarte_##idx##_data = { \
2044-
UARTE_CONFIG(idx), \
2062+
.char_out = &uarte##idx##_char_out, \
2063+
.rx_data = &uarte##idx##_rx_data, \
2064+
IF_ENABLED(CONFIG_UART_USE_RUNTIME_CONFIGURE, \
2065+
(.uart_config = UARTE_CONFIG(idx),)) \
20452066
IF_ENABLED(CONFIG_UART_##idx##_ASYNC, \
20462067
(.async = &uarte##idx##_async,)) \
20472068
IF_ENABLED(CONFIG_UART_##idx##_INTERRUPT_DRIVEN, \
20482069
(.int_driven = &uarte##idx##_int_driven,)) \
20492070
}; \
2071+
COND_CODE_1(CONFIG_UART_USE_RUNTIME_CONFIGURE, (), \
2072+
(BUILD_ASSERT(NRF_BAUDRATE(UARTE_PROP(idx, current_speed)) > 0,\
2073+
"Unsupported baudrate");)) \
20502074
static const struct uarte_nrfx_config uarte_##idx##z_config = { \
2075+
COND_CODE_1(CONFIG_UART_USE_RUNTIME_CONFIGURE, (), \
2076+
(.baudrate = UARTE_GET_BAUDRATE(idx), \
2077+
.hw_config = UARTE_NRF_CONFIG(idx),)) \
20512078
.pcfg = PINCTRL_DT_DEV_CONFIG_GET(UARTE(idx)), \
20522079
.uarte_regs = _CONCAT(NRF_UARTE, idx), \
20532080
.flags = \
@@ -2085,21 +2112,6 @@ static int uarte_nrfx_pm_action(const struct device *dev,
20852112
CONFIG_SERIAL_INIT_PRIORITY, \
20862113
&uart_nrfx_uarte_driver_api)
20872114

2088-
#define UARTE_CONFIG(idx) \
2089-
.char_out = &uarte##idx##_char_out, \
2090-
.rx_data = &uarte##idx##_rx_data, \
2091-
.uart_config = { \
2092-
.baudrate = UARTE_PROP(idx, current_speed), \
2093-
.data_bits = UART_CFG_DATA_BITS_8, \
2094-
.stop_bits = UART_CFG_STOP_BITS_1, \
2095-
.parity = IS_ENABLED(CONFIG_UART_##idx##_NRF_PARITY_BIT) \
2096-
? UART_CFG_PARITY_EVEN \
2097-
: UART_CFG_PARITY_NONE, \
2098-
.flow_ctrl = UARTE_PROP(idx, hw_flow_control) \
2099-
? UART_CFG_FLOW_CTRL_RTS_CTS \
2100-
: UART_CFG_FLOW_CTRL_NONE, \
2101-
}
2102-
21032115
#define UARTE_ASYNC(idx) \
21042116
IF_ENABLED(CONFIG_UART_##idx##_ASYNC, ( \
21052117
static uint8_t \

0 commit comments

Comments
 (0)