Skip to content

Commit c1870af

Browse files
committed
Bugfix: #4357 simultaneous using of I2C and SPI.
Use serial-box of Nordic nRF5 SDK to share resource between SPI and I2C. SPI is allocated from highest hw instance number resource in order to allocate as many I2C instances as possible.
1 parent cbfb234 commit c1870af

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

targets/TARGET_NORDIC/TARGET_NRF5/i2c_api.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,9 @@ static void twi_clear_bus(twi_info_t *twi_info)
290290

291291
void i2c_init(i2c_t *obj, PinName sda, PinName scl)
292292
{
293+
ret_code_t ret;
293294
int i;
295+
294296
for (i = 0; i < TWI_COUNT; ++i) {
295297
if (m_twi_info[i].initialized &&
296298
m_twi_info[i].pselsda == (uint32_t)sda &&
@@ -304,6 +306,13 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
304306

305307
for (i = 0; i < TWI_COUNT; ++i) {
306308
if (!m_twi_info[i].initialized) {
309+
ret = nrf_drv_common_per_res_acquire(m_twi_instances[i],
310+
m_twi_irq_handlers[i]);
311+
312+
if (ret != NRF_SUCCESS) {
313+
continue; /* the hw resource is busy - test another one */
314+
}
315+
307316
TWI_IDX(obj) = i;
308317

309318
twi_info_t *twi_info = TWI_INFO(obj);
@@ -324,8 +333,6 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
324333
i2c_reset(obj);
325334

326335
#if DEVICE_I2C_ASYNCH
327-
nrf_drv_common_per_res_acquire(m_twi_instances[i],
328-
m_twi_irq_handlers[i]);
329336
NVIC_SetVector(twi_handlers[i].IRQn, twi_handlers[i].vector);
330337
nrf_drv_common_irq_enable(twi_handlers[i].IRQn, TWI_IRQ_PRIORITY);
331338
#endif

targets/TARGET_NORDIC/TARGET_NRF5/spi_api.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,11 @@ void spi_init(spi_t *obj,
277277
}
278278
}
279279

280-
for (i = 0; i < SPI_COUNT; ++i) {
280+
for (i = SPI_COUNT - 1; i >= 0; i--) {
281281
spi_info_t *p_spi_info = &m_spi_info[i];
282+
282283
if (!p_spi_info->initialized) {
284+
283285
p_spi_info->sck_pin = (uint8_t)sclk;
284286
p_spi_info->mosi_pin = (mosi != NC) ?
285287
(uint8_t)mosi : NRF_DRV_SPI_PIN_NOT_USED;
@@ -290,8 +292,6 @@ void spi_init(spi_t *obj,
290292
p_spi_info->spi_mode = (uint8_t)NRF_DRV_SPI_MODE_0;
291293
p_spi_info->frequency = NRF_DRV_SPI_FREQ_1M;
292294

293-
NVIC_SetVector(spi_handler_desc[i].IRQn, spi_handler_desc[i].vector);
294-
295295
// By default each SPI instance is initialized to work as a master.
296296
// Should the slave mode be used, the instance will be reconfigured
297297
// appropriately in 'spi_format'.
@@ -309,7 +309,7 @@ void spi_init(spi_t *obj,
309309
p_spi_info->handler = 0;
310310
#endif
311311
SPI_IDX(obj) = i;
312-
312+
NVIC_SetVector(spi_handler_desc[i].IRQn, spi_handler_desc[i].vector);
313313
return;
314314
}
315315
}

0 commit comments

Comments
 (0)