Skip to content

Commit 6cec180

Browse files
author
Marcus Chang
committed
Fix SPI initialization for NRF52 series
New changes to Mbed error reporting in 5.9 exposed bug in SPI driver where an instance was uninitialized twice which triggered an ASSERT. This fix keeps track of which instance has been initialized and only calls uninit when it is safe.
1 parent 84d6b79 commit 6cec180

File tree

1 file changed

+14
-2
lines changed
  • targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52

1 file changed

+14
-2
lines changed

targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/spi_api.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ static const nrf_drv_spi_t nordic_nrf5_spi_instance[3] = {
5252
NRF_DRV_SPI_INSTANCE(2)
5353
};
5454

55+
/* Keep track of which instance has been initialized. */
56+
static bool nordic_nrf5_spi_initialized[3] = { false, false, false };
57+
5558
/* Forware declare interrupt handler. */
5659
#if DEVICE_SPI_ASYNCH
5760
static void nordic_nrf5_spi_event_handler(nrf_drv_spi_evt_t const *p_event, void *p_context);
@@ -93,8 +96,10 @@ static void spi_configure_driver_instance(spi_t *obj)
9396
/* Update applied, reset flag. */
9497
spi_inst->update = false;
9598

96-
/* clean up and initialize peripheral. */
97-
nrf_drv_spi_uninit(&nordic_nrf5_spi_instance[instance]);
99+
/* Clean up and uninitialize peripheral if already initialized. */
100+
if (nordic_nrf5_spi_initialized[instance]) {
101+
nrf_drv_spi_uninit(&nordic_nrf5_spi_instance[instance]);
102+
}
98103

99104
#if DEVICE_SPI_ASYNCH
100105
/* Set callback handler in asynchronous mode. */
@@ -107,6 +112,10 @@ static void spi_configure_driver_instance(spi_t *obj)
107112
/* Set callback handler to NULL in synchronous mode. */
108113
nrf_drv_spi_init(&nordic_nrf5_spi_instance[instance], &(spi_inst->config), NULL, NULL);
109114
#endif
115+
116+
/* Mark instance as initialized. */
117+
nordic_nrf5_spi_initialized[instance] = true;
118+
110119
/* Claim ownership of peripheral. */
111120
object_owner_spi2c_set(instance, obj);
112121
}
@@ -201,6 +210,9 @@ void spi_free(spi_t *obj)
201210

202211
/* Use driver uninit to free instance. */
203212
nrf_drv_spi_uninit(&nordic_nrf5_spi_instance[instance]);
213+
214+
/* Mark instance as uninitialized. */
215+
nordic_nrf5_spi_initialized[instance] = false;
204216
}
205217

206218
/** Configure the SPI format

0 commit comments

Comments
 (0)