Skip to content

Commit 52b9563

Browse files
committed
Added modifications in SDK drivers needed for mbed HAL implementation.
1 parent 082fd40 commit 52b9563

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

hal/targets/hal/TARGET_NORDIC/TARGET_NRF5/sdk/drivers_nrf/spi_master/nrf_drv_spi.c

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ typedef struct
8080

8181
bool tx_done : 1;
8282
bool rx_done : 1;
83+
bool abort : 1;
8384
} spi_control_block_t;
8485
static spi_control_block_t m_cb[SPI_COUNT];
8586

@@ -202,7 +203,7 @@ ret_code_t nrf_drv_spi_init(nrf_drv_spi_t const * const p_instance,
202203

203204
CODE_FOR_SPIM
204205
(
205-
NRF_SPIM_Type * p_spim = p_instance->p_registers;
206+
NRF_SPIM_Type * p_spim = (NRF_SPIM_Type * ) p_instance->p_registers;
206207
nrf_spim_pins_set(p_spim, p_config->sck_pin, mosi_pin, miso_pin);
207208
nrf_spim_frequency_set(p_spim,
208209
(nrf_spim_frequency_t)p_config->frequency);
@@ -221,7 +222,7 @@ ret_code_t nrf_drv_spi_init(nrf_drv_spi_t const * const p_instance,
221222
)
222223
CODE_FOR_SPI
223224
(
224-
NRF_SPI_Type * p_spi = p_instance->p_registers;
225+
NRF_SPI_Type * p_spi = (NRF_SPI_Type * ) p_instance->p_registers;
225226
nrf_spi_pins_set(p_spi, p_config->sck_pin, mosi_pin, miso_pin);
226227
nrf_spi_frequency_set(p_spi,
227228
(nrf_spi_frequency_t)p_config->frequency);
@@ -264,7 +265,7 @@ void nrf_drv_spi_uninit(nrf_drv_spi_t const * const p_instance)
264265

265266
CODE_FOR_SPIM
266267
(
267-
NRF_SPIM_Type * p_spim = p_instance->p_registers;
268+
NRF_SPIM_Type * p_spim = (NRF_SPIM_Type * ) p_instance->p_registers;
268269
if (p_cb->handler)
269270
{
270271
nrf_spim_int_disable(p_spim, DISABLE_ALL);
@@ -280,7 +281,7 @@ void nrf_drv_spi_uninit(nrf_drv_spi_t const * const p_instance)
280281
)
281282
CODE_FOR_SPI
282283
(
283-
NRF_SPI_Type * p_spi = p_instance->p_registers;
284+
NRF_SPI_Type * p_spi = (NRF_SPI_Type * ) p_instance->p_registers;
284285
if (p_cb->handler)
285286
{
286287
nrf_spi_int_disable(p_spi, DISABLE_ALL);
@@ -352,6 +353,19 @@ static bool transfer_byte(NRF_SPI_Type * p_spi, spi_control_block_t * p_cb)
352353
// see how the transfer is started in the 'nrf_drv_spi_transfer'
353354
// function.
354355
uint16_t bytes_used = p_cb->bytes_transferred + 1;
356+
357+
if (p_cb->abort)
358+
{
359+
if (bytes_used < p_cb->evt.data.done.tx_length)
360+
{
361+
p_cb->evt.data.done.tx_length = bytes_used;
362+
}
363+
if (bytes_used < p_cb->evt.data.done.rx_length)
364+
{
365+
p_cb->evt.data.done.rx_length = bytes_used;
366+
}
367+
}
368+
355369
if (bytes_used < p_cb->evt.data.done.tx_length)
356370
{
357371
nrf_spi_txd_set(p_spi, p_cb->evt.data.done.p_tx_buffer[bytes_used]);
@@ -371,8 +385,8 @@ static void spi_xfer(NRF_SPI_Type * p_spi,
371385
spi_control_block_t * p_cb,
372386
nrf_drv_spi_xfer_desc_t const * p_xfer_desc)
373387
{
374-
p_cb->bytes_transferred = 0;
375388
nrf_spi_int_disable(p_spi, NRF_SPI_INT_READY_MASK);
389+
p_cb->bytes_transferred = 0;
376390

377391
nrf_spi_event_clear(p_spi, NRF_SPI_EVENT_READY);
378392

@@ -516,8 +530,8 @@ ret_code_t nrf_drv_spi_xfer(nrf_drv_spi_t const * const p_instance,
516530
{
517531
spi_control_block_t * p_cb = &m_cb[p_instance->drv_inst_idx];
518532
ASSERT(p_cb->state != NRF_DRV_STATE_UNINITIALIZED);
519-
ASSERT(p_tx_buffer != NULL || tx_buffer_length == 0);
520-
ASSERT(p_rx_buffer != NULL || rx_buffer_length == 0);
533+
ASSERT(p_xfer_desc->p_tx_buffer != NULL || p_xfer_desc->tx_length == 0);
534+
ASSERT(p_xfer_desc->p_rx_buffer != NULL || p_xfer_desc->rx_length == 0);
521535

522536
if (p_cb->transfer_in_progress)
523537
{
@@ -534,14 +548,15 @@ ret_code_t nrf_drv_spi_xfer(nrf_drv_spi_t const * const p_instance,
534548
p_cb->evt.data.done = *p_xfer_desc;
535549
p_cb->tx_done = false;
536550
p_cb->rx_done = false;
551+
p_cb->abort = false;
537552

538553
if (p_cb->ss_pin != NRF_DRV_SPI_PIN_NOT_USED)
539554
{
540555
nrf_gpio_pin_clear(p_cb->ss_pin);
541556
}
542557
CODE_FOR_SPIM
543558
(
544-
return spim_xfer(p_instance->p_registers, p_cb, p_xfer_desc, flags);
559+
return spim_xfer((NRF_SPIM_Type * ) p_instance->p_registers, p_cb, p_xfer_desc, flags);
545560
)
546561
CODE_FOR_SPI
547562
(
@@ -550,10 +565,28 @@ ret_code_t nrf_drv_spi_xfer(nrf_drv_spi_t const * const p_instance,
550565
p_cb->transfer_in_progress = false;
551566
return NRF_ERROR_NOT_SUPPORTED;
552567
}
553-
spi_xfer(p_instance->p_registers, p_cb, p_xfer_desc);
568+
spi_xfer((NRF_SPI_Type * ) p_instance->p_registers, p_cb, p_xfer_desc);
554569
return NRF_SUCCESS;
555570
)
556571
}
572+
573+
void nrf_drv_spi_abort(nrf_drv_spi_t const * p_instance)
574+
{
575+
spi_control_block_t * p_cb = &m_cb[p_instance->drv_inst_idx];
576+
ASSERT(p_cb->state != NRF_DRV_STATE_UNINITIALIZED);
577+
578+
CODE_FOR_SPIM
579+
(
580+
nrf_spim_task_trigger(p_spim, NRF_SPIM_TASK_STOP);
581+
while (!nrf_spim_event_check(p_spim, NRF_SPIM_EVENT_STOPPED)) {}
582+
p_cb->transfer_in_progress = false;
583+
)
584+
CODE_FOR_SPI
585+
(
586+
p_cb->abort = true;
587+
)
588+
}
589+
557590
#ifdef SPIM_IN_USE
558591
static void irq_handler_spim(NRF_SPIM_Type * p_spim, spi_control_block_t * p_cb)
559592
{

hal/targets/hal/TARGET_NORDIC/TARGET_NRF5/sdk/drivers_nrf/spi_master/nrf_drv_spi.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,9 @@ uint32_t nrf_drv_spi_start_task_get(nrf_drv_spi_t const * p_instance);
363363
* @return END event address.
364364
*/
365365
uint32_t nrf_drv_spi_end_event_get(nrf_drv_spi_t const * p_instance);
366+
367+
void nrf_drv_spi_abort(nrf_drv_spi_t const * p_instance);
368+
366369
#endif // NRF_DRV_SPI_H__
367370

368371
/** @} */

hal/targets/hal/TARGET_NORDIC/TARGET_NRF5/sdk/drivers_nrf/twi_master/nrf_drv_twi.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,12 @@ ret_code_t nrf_drv_twi_xfer(nrf_drv_twi_t const * p_instance,
737737
return ret;
738738
}
739739

740+
bool nrf_drv_twi_is_busy(nrf_drv_twi_t const * p_instance)
741+
{
742+
twi_control_block_t * p_cb = &m_cb[p_instance->drv_inst_idx];
743+
return p_cb->busy;
744+
}
745+
740746
ret_code_t nrf_drv_twi_tx(nrf_drv_twi_t const * p_instance,
741747
uint8_t address,
742748
uint8_t const * p_data,

hal/targets/hal/TARGET_NORDIC/TARGET_NRF5/sdk/drivers_nrf/twi_master/nrf_drv_twi.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,16 @@ ret_code_t nrf_drv_twi_xfer(nrf_drv_twi_t const * p_instance,
342342
nrf_drv_twi_xfer_desc_t const * p_xfer_desc,
343343
uint32_t flags);
344344

345+
/**
346+
* @brief Function for checking the TWI driver state.
347+
*
348+
* @param[in] p_instance TWI instance.
349+
*
350+
* @retval true If the TWI driver is currently busy performing a transfer.
351+
* @retval false If the TWI driver is ready for a new transfer.
352+
*/
353+
bool nrf_drv_twi_is_busy(nrf_drv_twi_t const * p_instance);
354+
345355
/**
346356
* @brief Function for getting the transferred data count.
347357
*

0 commit comments

Comments
 (0)