Skip to content

Commit f5eb87c

Browse files
Cruz Monrreal IICruz Monrreal II
authored andcommitted
Revert "nRF52 serial: Tighten/simplify atomics"
This reverts commit 2a7f5e2.
1 parent 774ee68 commit f5eb87c

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/serial_api.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ typedef struct {
138138
uint8_t buffer[NUMBER_OF_BANKS][DMA_BUFFER_SIZE];
139139
uint32_t usage_counter;
140140
uint8_t tx_data;
141-
bool tx_in_progress;
142-
bool rx_in_progress;
141+
volatile uint8_t tx_in_progress;
142+
volatile uint8_t rx_in_progress;
143143
bool tx_asynch;
144144
bool rx_asynch;
145145
bool callback_posted;
@@ -252,7 +252,7 @@ static void nordic_nrf5_uart_callback_handler(uint32_t instance)
252252
static void nordic_nrf5_uart_event_handler_endtx(int instance)
253253
{
254254
/* Release mutex. As the owner this call is safe. */
255-
core_util_atomic_store_bool(&nordic_nrf5_uart_state[instance].tx_in_progress, false);
255+
nordic_nrf5_uart_state[instance].tx_in_progress = 0;
256256

257257
/* Check if callback handler and Tx event mask is set. */
258258
uart_irq_handler callback = (uart_irq_handler) nordic_nrf5_uart_state[instance].owner->handler;
@@ -275,8 +275,8 @@ static void nordic_nrf5_uart_event_handler_endtx(int instance)
275275
static void nordic_nrf5_uart_event_handler_endtx_asynch(int instance)
276276
{
277277
/* Set Tx done and reset Tx mode to be not asynchronous. */
278+
nordic_nrf5_uart_state[instance].tx_in_progress = 0;
278279
nordic_nrf5_uart_state[instance].tx_asynch = false;
279-
core_util_atomic_store_bool(&nordic_nrf5_uart_state[instance].tx_in_progress, false);
280280

281281
/* Cast handler to callback function pointer. */
282282
void (*callback)(void) = (void (*)(void)) nordic_nrf5_uart_state[instance].owner->tx_handler;
@@ -482,8 +482,8 @@ static void nordic_nrf5_uart_event_handler_rxstarted(int instance)
482482
static void nordic_nrf5_uart_event_handler_endrx_asynch(int instance)
483483
{
484484
/* Set Rx done and reset Rx mode to be not asynchronous. */
485+
nordic_nrf5_uart_state[instance].rx_in_progress = 0;
485486
nordic_nrf5_uart_state[instance].rx_asynch = false;
486-
core_util_atomic_store_bool(&nordic_nrf5_uart_state[instance].rx_in_progress, false);
487487

488488
/* Cast handler to callback function pointer. */
489489
void (*callback)(void) = (void (*)(void)) nordic_nrf5_uart_state[instance].owner->rx_handler;
@@ -1410,7 +1410,7 @@ int serial_writable(serial_t *obj)
14101410

14111411
int instance = uart_object->instance;
14121412

1413-
return (!core_util_atomic_load_bool(&nordic_nrf5_uart_state[instance].tx_in_progress) &&
1413+
return ((nordic_nrf5_uart_state[instance].tx_in_progress == 0) &&
14141414
(nrf_uarte_event_extra_check(nordic_nrf5_uart_register[instance], NRF_UARTE_EVENT_TXDRDY)));
14151415
}
14161416

@@ -1449,14 +1449,16 @@ int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx
14491449

14501450
/**
14511451
* tx_in_progress acts like a mutex to ensure only one transmission can be active at a time.
1452-
* The flag is modified using the atomic exchange function - only proceed when we see the
1453-
* flag clear and we set it to true.
1452+
* The flag is modified using the atomic compare-and-set function.
14541453
*/
1455-
bool old_mutex;
1454+
bool mutex = false;
14561455

14571456
do {
1458-
old_mutex = core_util_atomic_exchange_bool(&nordic_nrf5_uart_state[instance].tx_in_progress, true);
1459-
} while (old_mutex == true);
1457+
uint8_t expected = 0;
1458+
uint8_t desired = 1;
1459+
1460+
mutex = core_util_atomic_cas_u8((uint8_t *) &nordic_nrf5_uart_state[instance].tx_in_progress, &expected, desired);
1461+
} while (mutex == false);
14601462

14611463
/* State variables. */
14621464
int result = 0;
@@ -1573,14 +1575,16 @@ void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_widt
15731575

15741576
/**
15751577
* rx_in_progress acts like a mutex to ensure only one asynchronous reception can be active at a time.
1576-
* The flag is modified using the atomic exchange function - only proceed when we see the
1577-
* flag clear and we set it to true.
1578+
* The flag is modified using the atomic compare-and-set function.
15781579
*/
1579-
bool old_mutex;
1580+
bool mutex = false;
15801581

15811582
do {
1582-
old_mutex = core_util_atomic_exchange_bool(&nordic_nrf5_uart_state[instance].rx_in_progress, true);
1583-
} while (old_mutex == true);
1583+
uint8_t expected = 0;
1584+
uint8_t desired = 1;
1585+
1586+
mutex = core_util_atomic_cas_u8((uint8_t *) &nordic_nrf5_uart_state[instance].rx_in_progress, &expected, desired);
1587+
} while (mutex == false);
15841588

15851589
/* Store callback handler, mask and reset event value. */
15861590
obj->serial.rx_handler = handler;
@@ -1659,8 +1663,8 @@ void serial_tx_abort_asynch(serial_t *obj)
16591663
nrf_uarte_event_clear(nordic_nrf5_uart_register[instance], NRF_UARTE_EVENT_ENDTX);
16601664

16611665
/* Reset Tx flags. */
1666+
nordic_nrf5_uart_state[instance].tx_in_progress = 0;
16621667
nordic_nrf5_uart_state[instance].tx_asynch = false;
1663-
nordic_nrf5_uart_state[instance].tx_in_progress = false;
16641668

16651669
/* Force reconfiguration. */
16661670
obj->serial.update = true;
@@ -1687,8 +1691,8 @@ void serial_rx_abort_asynch(serial_t *obj)
16871691
core_util_critical_section_enter();
16881692

16891693
/* Reset Rx flags. */
1694+
nordic_nrf5_uart_state[obj->serial.instance].rx_in_progress = 0;
16901695
nordic_nrf5_uart_state[obj->serial.instance].rx_asynch = false;
1691-
nordic_nrf5_uart_state[obj->serial.instance].rx_in_progress = false;
16921696
obj->serial.rx_asynch = false;
16931697

16941698
/* Force reconfiguration. */

0 commit comments

Comments
 (0)