Skip to content

Commit 4f211ce

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 133bde2 commit 4f211ce

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
@@ -664,12 +676,12 @@ static int wait_tx_ready(const struct device *dev)
664676
#endif
665677

666678
if (res) {
667-
key = irq_lock();
679+
IRQ_LOCK(key);
668680
if (is_tx_ready(dev)) {
669681
break;
670682
}
671683

672-
irq_unlock(key);
684+
IRQ_UNLOCK(key);
673685
}
674686
if (IS_ENABLED(CONFIG_MULTITHREADING)) {
675687
k_msleep(1);
@@ -919,11 +931,12 @@ static int uarte_nrfx_tx(const struct device *dev, const uint8_t *buf,
919931
{
920932
struct uarte_nrfx_data *data = dev->data;
921933
NRF_UARTE_Type *uarte = get_uarte_instance(dev);
934+
unsigned int key;
922935

923-
unsigned int key = irq_lock();
936+
IRQ_LOCK(key);
924937

925938
if (data->async->tx.len) {
926-
irq_unlock(key);
939+
IRQ_UNLOCK(key);
927940
return -EBUSY;
928941
}
929942

@@ -958,7 +971,7 @@ static int uarte_nrfx_tx(const struct device *dev, const uint8_t *buf,
958971

959972
start_tx_locked(dev, data);
960973

961-
irq_unlock(key);
974+
IRQ_UNLOCK(key);
962975

963976
if (has_hwfc(dev) && timeout != SYS_FOREVER_US) {
964977
k_timer_start(&data->async->tx.timer, K_USEC(timeout), K_NO_WAIT);
@@ -1179,10 +1192,12 @@ static int uarte_nrfx_rx_enable(const struct device *dev, uint8_t *buf,
11791192
async_rx->enabled = true;
11801193

11811194
if (LOW_POWER_ENABLED(cfg)) {
1182-
unsigned int key = irq_lock();
1195+
unsigned int key;
1196+
1197+
IRQ_LOCK(key);
11831198

11841199
uarte_enable_locked(dev, UARTE_FLAG_LOW_POWER_RX);
1185-
irq_unlock(key);
1200+
IRQ_UNLOCK(key);
11861201
}
11871202

11881203
nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STARTRX);
@@ -1197,7 +1212,9 @@ static int uarte_nrfx_rx_buf_rsp(const struct device *dev, uint8_t *buf,
11971212
struct uarte_async_rx *async_rx = &data->async->rx;
11981213
int err;
11991214
NRF_UARTE_Type *uarte = get_uarte_instance(dev);
1200-
unsigned int key = irq_lock();
1215+
unsigned int key;
1216+
1217+
IRQ_LOCK(key);
12011218

12021219
if (async_rx->buf == NULL) {
12031220
err = -EACCES;
@@ -1232,7 +1249,7 @@ static int uarte_nrfx_rx_buf_rsp(const struct device *dev, uint8_t *buf,
12321249
err = -EBUSY;
12331250
}
12341251

1235-
irq_unlock(key);
1252+
IRQ_UNLOCK(key);
12361253

12371254
return err;
12381255
}
@@ -1537,7 +1554,9 @@ static void endrx_isr(const struct device *dev)
15371554
* and here we just do the swap of which buffer the driver is following,
15381555
* the next rx_timeout() will update the rx_offset.
15391556
*/
1540-
unsigned int key = irq_lock();
1557+
unsigned int key;
1558+
1559+
IRQ_LOCK(key);
15411560

15421561
if (async_rx->buf) {
15431562
/* Check is based on assumption that ISR handler handles
@@ -1553,7 +1572,7 @@ static void endrx_isr(const struct device *dev)
15531572
nrf_uarte_task_trigger(uarte, NRF_UARTE_TASK_STOPRX);
15541573
}
15551574

1556-
irq_unlock(key);
1575+
IRQ_UNLOCK(key);
15571576
}
15581577

15591578
#if !defined(CONFIG_UART_NRFX_UARTE_ENHANCED_RX)
@@ -1674,7 +1693,7 @@ static void txstopped_isr(const struct device *dev)
16741693
NRF_UARTE_Type *uarte = get_uarte_instance(dev);
16751694
unsigned int key;
16761695

1677-
key = irq_lock();
1696+
IRQ_LOCK(key);
16781697

16791698
size_t amount = (data->async->tx.amount >= 0) ?
16801699
data->async->tx.amount : nrf_uarte_tx_amount_get(uarte);
@@ -1690,7 +1709,7 @@ static void txstopped_isr(const struct device *dev)
16901709
uarte_disable_locked(dev, UARTE_FLAG_LOW_POWER_TX);
16911710
}
16921711

1693-
irq_unlock(key);
1712+
IRQ_UNLOCK(key);
16941713

16951714
if (!data->async->tx.buf) {
16961715
return;
@@ -1701,9 +1720,9 @@ static void txstopped_isr(const struct device *dev)
17011720
* TXSTOPPED interrupt means that uart_poll_out has completed.
17021721
*/
17031722
if (data->async->tx.pending) {
1704-
key = irq_lock();
1723+
IRQ_LOCK(key);
17051724
start_tx_locked(dev, data);
1706-
irq_unlock(key);
1725+
IRQ_UNLOCK(key);
17071726
return;
17081727
}
17091728

@@ -1715,9 +1734,9 @@ static void txstopped_isr(const struct device *dev)
17151734
if (amount == data->async->tx.xfer_len) {
17161735
data->async->tx.cache_offset += amount;
17171736
if (setup_tx_cache(dev)) {
1718-
key = irq_lock();
1737+
IRQ_LOCK(key);
17191738
start_tx_locked(dev, data);
1720-
irq_unlock(key);
1739+
IRQ_UNLOCK(key);
17211740
return;
17221741
}
17231742

@@ -1917,7 +1936,7 @@ static void uarte_nrfx_poll_out(const struct device *dev, unsigned char c)
19171936

19181937
if (isr_mode) {
19191938
while (1) {
1920-
key = irq_lock();
1939+
IRQ_LOCK(key);
19211940
if (is_tx_ready(dev)) {
19221941
#if UARTE_ANY_ASYNC
19231942
if (data->async && data->async->tx.len &&
@@ -1928,7 +1947,7 @@ static void uarte_nrfx_poll_out(const struct device *dev, unsigned char c)
19281947
break;
19291948
}
19301949

1931-
irq_unlock(key);
1950+
IRQ_UNLOCK(key);
19321951
Z_SPIN_DELAY(3);
19331952
}
19341953
} else {
@@ -1965,7 +1984,7 @@ static void uarte_nrfx_poll_out(const struct device *dev, unsigned char c)
19651984
*config->poll_out_byte = c;
19661985
tx_start(dev, config->poll_out_byte, 1);
19671986

1968-
irq_unlock(key);
1987+
IRQ_UNLOCK(key);
19691988
}
19701989

19711990

@@ -1976,6 +1995,7 @@ static int uarte_nrfx_fifo_fill(const struct device *dev,
19761995
int len)
19771996
{
19781997
struct uarte_nrfx_data *data = dev->data;
1998+
unsigned int key;
19791999

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

1988-
unsigned int key = irq_lock();
2008+
IRQ_LOCK(key);
19892009

19902010
if (!is_tx_ready(dev)) {
19912011
data->int_driven->fifo_fill_lock = 0;
@@ -1994,7 +2014,7 @@ static int uarte_nrfx_fifo_fill(const struct device *dev,
19942014
tx_start(dev, data->int_driven->tx_buffer, len);
19952015
}
19962016

1997-
irq_unlock(key);
2017+
IRQ_UNLOCK(key);
19982018

19992019
return len;
20002020
}
@@ -2030,18 +2050,19 @@ static void uarte_nrfx_irq_tx_enable(const struct device *dev)
20302050
{
20312051
NRF_UARTE_Type *uarte = get_uarte_instance(dev);
20322052
struct uarte_nrfx_data *data = dev->data;
2053+
unsigned int key;
20332054

20342055
if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
20352056
pm_device_runtime_get(dev);
20362057
}
20372058

2038-
unsigned int key = irq_lock();
2059+
IRQ_LOCK(key);
20392060

20402061
data->int_driven->disable_tx_irq = false;
20412062
data->int_driven->tx_irq_enabled = true;
20422063
nrf_uarte_int_enable(uarte, NRF_UARTE_INT_TXSTOPPED_MASK);
20432064

2044-
irq_unlock(key);
2065+
IRQ_UNLOCK(key);
20452066
}
20462067

20472068
/** Interrupt driven transfer disabling function */

0 commit comments

Comments
 (0)