Skip to content

Commit d34fae9

Browse files
committed
[nrf noup] drivers: serial: nrfx_uarte: Use Softdevice critical section
Use SoftDevice critical section when SoftDevice is present. This is a temporary solution until there is a logging backend using nrfx directly. Signed-off-by: Krzysztof Chruściński <[email protected]> Signed-off-by: Emanuele Di Santo <[email protected]>
1 parent 1355c0c commit d34fae9

File tree

1 file changed

+49
-28
lines changed

1 file changed

+49
-28
lines changed

drivers/serial/uart_nrfx_uarte.c

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,15 @@ BUILD_ASSERT(IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME));
155155
/* Size of hardware fifo in RX path. */
156156
#define UARTE_HW_RX_FIFO_SIZE 5
157157

158+
#ifdef CONFIG_SOFTDEVICE
159+
#include <nrf_nvic.h>
160+
#define IRQ_LOCK(_key) sd_nvic_critical_region_enter((uint8_t *)&_key)
161+
#define IRQ_UNLOCK(_key) sd_nvic_critical_region_exit(_key)
162+
#else
163+
#define IRQ_LOCK(_key) _key = irq_lock()
164+
#define IRQ_UNLOCK(_key) irq_unlock(_key)
165+
#endif
166+
158167
#ifdef UARTE_ANY_ASYNC
159168

160169
struct uarte_async_tx {
@@ -373,15 +382,16 @@ static inline NRF_UARTE_Type *get_uarte_instance(const struct device *dev)
373382
static void endtx_isr(const struct device *dev)
374383
{
375384
NRF_UARTE_Type *uarte = get_uarte_instance(dev);
385+
unsigned int key;
376386

377-
unsigned int key = irq_lock();
387+
IRQ_LOCK(key);
378388

379389
if (nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_ENDTX)) {
380390
nrf_uarte_event_clear(uarte, NRF_UARTE_EVENT_ENDTX);
381391
nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STOPTX);
382392
}
383393

384-
irq_unlock(key);
394+
IRQ_UNLOCK(key);
385395

386396
}
387397

@@ -450,7 +460,9 @@ static void uarte_nrfx_isr_int(const void *arg)
450460
bool txstopped = nrf_uarte_event_check(uarte, NRF_UARTE_EVENT_TXSTOPPED);
451461

452462
if (txstopped && (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME) || LOW_POWER_ENABLED(config))) {
453-
unsigned int key = irq_lock();
463+
unsigned int key;
464+
465+
IRQ_LOCK(key);
454466

455467
if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
456468
if (data->flags & UARTE_FLAG_POLL_OUT) {
@@ -467,7 +479,7 @@ static void uarte_nrfx_isr_int(const void *arg)
467479
nrf_uarte_int_disable(uarte, NRF_UARTE_INT_TXSTOPPED_MASK);
468480
}
469481

470-
irq_unlock(key);
482+
IRQ_UNLOCK(key);
471483
}
472484

473485
#ifdef UARTE_INTERRUPT_DRIVEN
@@ -670,12 +682,12 @@ static int wait_tx_ready(const struct device *dev)
670682
#endif
671683

672684
if (res) {
673-
key = irq_lock();
685+
IRQ_LOCK(key);
674686
if (is_tx_ready(dev)) {
675687
break;
676688
}
677689

678-
irq_unlock(key);
690+
IRQ_UNLOCK(key);
679691
}
680692
if (IS_ENABLED(CONFIG_MULTITHREADING)) {
681693
k_msleep(1);
@@ -925,11 +937,12 @@ static int uarte_nrfx_tx(const struct device *dev, const uint8_t *buf,
925937
{
926938
struct uarte_nrfx_data *data = dev->data;
927939
NRF_UARTE_Type *uarte = get_uarte_instance(dev);
940+
unsigned int key;
928941

929-
unsigned int key = irq_lock();
942+
IRQ_LOCK(key);
930943

931944
if (data->async->tx.len) {
932-
irq_unlock(key);
945+
IRQ_UNLOCK(key);
933946
return -EBUSY;
934947
}
935948

@@ -964,7 +977,7 @@ static int uarte_nrfx_tx(const struct device *dev, const uint8_t *buf,
964977

965978
start_tx_locked(dev, data);
966979

967-
irq_unlock(key);
980+
IRQ_UNLOCK(key);
968981

969982
if (has_hwfc(dev) && timeout != SYS_FOREVER_US) {
970983
k_timer_start(&data->async->tx.timer, K_USEC(timeout), K_NO_WAIT);
@@ -1185,10 +1198,12 @@ static int uarte_nrfx_rx_enable(const struct device *dev, uint8_t *buf,
11851198
async_rx->enabled = true;
11861199

11871200
if (LOW_POWER_ENABLED(cfg)) {
1188-
unsigned int key = irq_lock();
1201+
unsigned int key;
1202+
1203+
IRQ_LOCK(key);
11891204

11901205
uarte_enable_locked(dev, UARTE_FLAG_LOW_POWER_RX);
1191-
irq_unlock(key);
1206+
IRQ_UNLOCK(key);
11921207
}
11931208

11941209
nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STARTRX);
@@ -1203,7 +1218,9 @@ static int uarte_nrfx_rx_buf_rsp(const struct device *dev, uint8_t *buf,
12031218
struct uarte_async_rx *async_rx = &data->async->rx;
12041219
int err;
12051220
NRF_UARTE_Type *uarte = get_uarte_instance(dev);
1206-
unsigned int key = irq_lock();
1221+
unsigned int key;
1222+
1223+
IRQ_LOCK(key);
12071224

12081225
if (async_rx->buf == NULL) {
12091226
err = -EACCES;
@@ -1238,7 +1255,7 @@ static int uarte_nrfx_rx_buf_rsp(const struct device *dev, uint8_t *buf,
12381255
err = -EBUSY;
12391256
}
12401257

1241-
irq_unlock(key);
1258+
IRQ_UNLOCK(key);
12421259

12431260
return err;
12441261
}
@@ -1543,7 +1560,9 @@ static void endrx_isr(const struct device *dev)
15431560
* and here we just do the swap of which buffer the driver is following,
15441561
* the next rx_timeout() will update the rx_offset.
15451562
*/
1546-
unsigned int key = irq_lock();
1563+
unsigned int key;
1564+
1565+
IRQ_LOCK(key);
15471566

15481567
if (async_rx->buf) {
15491568
/* Check is based on assumption that ISR handler handles
@@ -1559,7 +1578,7 @@ static void endrx_isr(const struct device *dev)
15591578
nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STOPRX);
15601579
}
15611580

1562-
irq_unlock(key);
1581+
IRQ_UNLOCK(key);
15631582
}
15641583

15651584
#if !defined(CONFIG_UART_NRFX_UARTE_ENHANCED_RX)
@@ -1680,7 +1699,7 @@ static void txstopped_isr(const struct device *dev)
16801699
NRF_UARTE_Type *uarte = get_uarte_instance(dev);
16811700
unsigned int key;
16821701

1683-
key = irq_lock();
1702+
IRQ_LOCK(key);
16841703

16851704
size_t amount = (data->async->tx.amount >= 0) ?
16861705
data->async->tx.amount : nrf_uarte_tx_amount_get(uarte);
@@ -1696,7 +1715,7 @@ static void txstopped_isr(const struct device *dev)
16961715
uarte_disable_locked(dev, UARTE_FLAG_LOW_POWER_TX);
16971716
}
16981717

1699-
irq_unlock(key);
1718+
IRQ_UNLOCK(key);
17001719

17011720
if (!data->async->tx.buf) {
17021721
return;
@@ -1707,9 +1726,9 @@ static void txstopped_isr(const struct device *dev)
17071726
* TXSTOPPED interrupt means that uart_poll_out has completed.
17081727
*/
17091728
if (data->async->tx.pending) {
1710-
key = irq_lock();
1729+
IRQ_LOCK(key);
17111730
start_tx_locked(dev, data);
1712-
irq_unlock(key);
1731+
IRQ_UNLOCK(key);
17131732
return;
17141733
}
17151734

@@ -1721,9 +1740,9 @@ static void txstopped_isr(const struct device *dev)
17211740
if (amount == data->async->tx.xfer_len) {
17221741
data->async->tx.cache_offset += amount;
17231742
if (setup_tx_cache(dev)) {
1724-
key = irq_lock();
1743+
IRQ_LOCK(key);
17251744
start_tx_locked(dev, data);
1726-
irq_unlock(key);
1745+
IRQ_UNLOCK(key);
17271746
return;
17281747
}
17291748

@@ -1923,7 +1942,7 @@ static void uarte_nrfx_poll_out(const struct device *dev, unsigned char c)
19231942

19241943
if (isr_mode) {
19251944
while (1) {
1926-
key = irq_lock();
1945+
IRQ_LOCK(key);
19271946
if (is_tx_ready(dev)) {
19281947
#if UARTE_ANY_ASYNC
19291948
if (data->async && data->async->tx.len &&
@@ -1934,7 +1953,7 @@ static void uarte_nrfx_poll_out(const struct device *dev, unsigned char c)
19341953
break;
19351954
}
19361955

1937-
irq_unlock(key);
1956+
IRQ_UNLOCK(key);
19381957
Z_SPIN_DELAY(3);
19391958
}
19401959
} else {
@@ -1971,7 +1990,7 @@ static void uarte_nrfx_poll_out(const struct device *dev, unsigned char c)
19711990
*config->poll_out_byte = c;
19721991
tx_start(dev, config->poll_out_byte, 1);
19731992

1974-
irq_unlock(key);
1993+
IRQ_UNLOCK(key);
19751994
}
19761995

19771996

@@ -1982,6 +2001,7 @@ static int uarte_nrfx_fifo_fill(const struct device *dev,
19822001
int len)
19832002
{
19842003
struct uarte_nrfx_data *data = dev->data;
2004+
unsigned int key;
19852005

19862006
len = MIN(len, data->int_driven->tx_buff_size);
19872007
if (!atomic_cas(&data->int_driven->fifo_fill_lock, 0, 1)) {
@@ -1991,7 +2011,7 @@ static int uarte_nrfx_fifo_fill(const struct device *dev,
19912011
/* Copy data to RAM buffer for EasyDMA transfer */
19922012
memcpy(data->int_driven->tx_buffer, tx_data, len);
19932013

1994-
unsigned int key = irq_lock();
2014+
IRQ_LOCK(key);
19952015

19962016
if (!is_tx_ready(dev)) {
19972017
data->int_driven->fifo_fill_lock = 0;
@@ -2000,7 +2020,7 @@ static int uarte_nrfx_fifo_fill(const struct device *dev,
20002020
tx_start(dev, data->int_driven->tx_buffer, len);
20012021
}
20022022

2003-
irq_unlock(key);
2023+
IRQ_UNLOCK(key);
20042024

20052025
return len;
20062026
}
@@ -2036,18 +2056,19 @@ static void uarte_nrfx_irq_tx_enable(const struct device *dev)
20362056
{
20372057
NRF_UARTE_Type *uarte = get_uarte_instance(dev);
20382058
struct uarte_nrfx_data *data = dev->data;
2059+
unsigned int key;
20392060

20402061
if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
20412062
pm_device_runtime_get(dev);
20422063
}
20432064

2044-
unsigned int key = irq_lock();
2065+
IRQ_LOCK(key);
20452066

20462067
data->int_driven->disable_tx_irq = false;
20472068
data->int_driven->tx_irq_enabled = true;
20482069
nrf_uarte_int_enable(uarte, NRF_UARTE_INT_TXSTOPPED_MASK);
20492070

2050-
irq_unlock(key);
2071+
IRQ_UNLOCK(key);
20512072
}
20522073

20532074
/** Interrupt driven transfer disabling function */

0 commit comments

Comments
 (0)