Skip to content

Fix the issue #4357: NRF52 doesn't support simultaneous use of I2C and SPI. #4634

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
Show file tree
Hide file tree
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
11 changes: 9 additions & 2 deletions targets/TARGET_NORDIC/TARGET_NRF5/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ static void twi_clear_bus(twi_info_t *twi_info)

void i2c_init(i2c_t *obj, PinName sda, PinName scl)
{
ret_code_t ret;
int i;

for (i = 0; i < TWI_COUNT; ++i) {
if (m_twi_info[i].initialized &&
m_twi_info[i].pselsda == (uint32_t)sda &&
Expand All @@ -304,6 +306,13 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)

for (i = 0; i < TWI_COUNT; ++i) {
if (!m_twi_info[i].initialized) {
ret = nrf_drv_common_per_res_acquire(m_twi_instances[i],
m_twi_irq_handlers[i]);

if (ret != NRF_SUCCESS) {
continue; /* the hw resource is busy - test another one */
}

TWI_IDX(obj) = i;

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

#if DEVICE_I2C_ASYNCH
nrf_drv_common_per_res_acquire(m_twi_instances[i],
m_twi_irq_handlers[i]);
NVIC_SetVector(twi_handlers[i].IRQn, twi_handlers[i].vector);
nrf_drv_common_irq_enable(twi_handlers[i].IRQn, TWI_IRQ_PRIORITY);
#endif
Expand Down
12 changes: 6 additions & 6 deletions targets/TARGET_NORDIC/TARGET_NRF5/spi_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,11 @@ void spi_init(spi_t *obj,
}
}

for (i = 0; i < SPI_COUNT; ++i) {
for (i = SPI_COUNT - 1; i >= 0; i--) {
spi_info_t *p_spi_info = &m_spi_info[i];

if (!p_spi_info->initialized) {

p_spi_info->sck_pin = (uint8_t)sclk;
p_spi_info->mosi_pin = (mosi != NC) ?
(uint8_t)mosi : NRF_DRV_SPI_PIN_NOT_USED;
Expand All @@ -290,8 +292,6 @@ void spi_init(spi_t *obj,
p_spi_info->spi_mode = (uint8_t)NRF_DRV_SPI_MODE_0;
p_spi_info->frequency = NRF_DRV_SPI_FREQ_1M;

NVIC_SetVector(spi_handler_desc[i].IRQn, spi_handler_desc[i].vector);

// By default each SPI instance is initialized to work as a master.
// Should the slave mode be used, the instance will be reconfigured
// appropriately in 'spi_format'.
Expand All @@ -305,11 +305,11 @@ void spi_init(spi_t *obj,
p_spi_info->initialized = true;
p_spi_info->master = true;
p_spi_info->flag.busy = false;
#if DEVICE_SPI_ASYNCH
#if DEVICE_SPI_ASYNCH
p_spi_info->handler = 0;
#endif
#endif
SPI_IDX(obj) = i;

NVIC_SetVector(spi_handler_desc[i].IRQn, spi_handler_desc[i].vector);
return;
}
}
Expand Down