Skip to content

nRF52 serial: Tighten/simplify atomics #9616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 18 additions & 22 deletions targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/serial_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ typedef struct {
uint8_t buffer[NUMBER_OF_BANKS][DMA_BUFFER_SIZE];
uint32_t usage_counter;
uint8_t tx_data;
volatile uint8_t tx_in_progress;
volatile uint8_t rx_in_progress;
bool tx_in_progress;
bool rx_in_progress;
bool tx_asynch;
bool rx_asynch;
bool callback_posted;
Expand Down Expand Up @@ -252,7 +252,7 @@ static void nordic_nrf5_uart_callback_handler(uint32_t instance)
static void nordic_nrf5_uart_event_handler_endtx(int instance)
{
/* Release mutex. As the owner this call is safe. */
nordic_nrf5_uart_state[instance].tx_in_progress = 0;
core_util_atomic_store_bool(&nordic_nrf5_uart_state[instance].tx_in_progress, false);

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

/* Cast handler to callback function pointer. */
void (*callback)(void) = (void (*)(void)) nordic_nrf5_uart_state[instance].owner->tx_handler;
Expand Down Expand Up @@ -482,8 +482,8 @@ static void nordic_nrf5_uart_event_handler_rxstarted(int instance)
static void nordic_nrf5_uart_event_handler_endrx_asynch(int instance)
{
/* Set Rx done and reset Rx mode to be not asynchronous. */
nordic_nrf5_uart_state[instance].rx_in_progress = 0;
nordic_nrf5_uart_state[instance].rx_asynch = false;
core_util_atomic_store_bool(&nordic_nrf5_uart_state[instance].rx_in_progress, false);

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

int instance = uart_object->instance;

return ((nordic_nrf5_uart_state[instance].tx_in_progress == 0) &&
return (!core_util_atomic_load_bool(&nordic_nrf5_uart_state[instance].tx_in_progress) &&
(nrf_uarte_event_extra_check(nordic_nrf5_uart_register[instance], NRF_UARTE_EVENT_TXDRDY)));
}

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

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

do {
uint8_t expected = 0;
uint8_t desired = 1;

mutex = core_util_atomic_cas_u8((uint8_t *) &nordic_nrf5_uart_state[instance].tx_in_progress, &expected, desired);
} while (mutex == false);
old_mutex = core_util_atomic_exchange_bool(&nordic_nrf5_uart_state[instance].tx_in_progress, true);
} while (old_mutex == true);

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

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

do {
uint8_t expected = 0;
uint8_t desired = 1;

mutex = core_util_atomic_cas_u8((uint8_t *) &nordic_nrf5_uart_state[instance].rx_in_progress, &expected, desired);
} while (mutex == false);
old_mutex = core_util_atomic_exchange_bool(&nordic_nrf5_uart_state[instance].rx_in_progress, true);
} while (old_mutex == true);

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

/* Reset Tx flags. */
nordic_nrf5_uart_state[instance].tx_in_progress = 0;
nordic_nrf5_uart_state[instance].tx_asynch = false;
nordic_nrf5_uart_state[instance].tx_in_progress = false;

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

/* Reset Rx flags. */
nordic_nrf5_uart_state[obj->serial.instance].rx_in_progress = 0;
nordic_nrf5_uart_state[obj->serial.instance].rx_asynch = false;
nordic_nrf5_uart_state[obj->serial.instance].rx_in_progress = false;
obj->serial.rx_asynch = false;

/* Force reconfiguration. */
Expand Down