Skip to content

Commit da27610

Browse files
ccli8adbridge
authored andcommitted
Refine SPI PDMA code
1 parent 61f3784 commit da27610

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

targets/TARGET_NUVOTON/TARGET_M451/spi_api.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,10 @@ void spi_master_transfer(spi_t *obj, const void *tx, size_t tx_length, void *rx,
314314
MBED_ASSERT(modinit != NULL);
315315
MBED_ASSERT(modinit->modname == obj->spi.spi);
316316

317+
PDMA_T *pdma_base = dma_modbase();
318+
317319
// Configure tx DMA
318-
PDMA->CHCTL |= 1 << obj->spi.dma_chn_id_tx; // Enable this DMA channel
320+
pdma_base->CHCTL |= 1 << obj->spi.dma_chn_id_tx; // Enable this DMA channel
319321
PDMA_SetTransferMode(obj->spi.dma_chn_id_tx,
320322
((struct nu_spi_var *) modinit->var)->pdma_perp_tx, // Peripheral connected to this PDMA
321323
0, // Scatter-gather disabled
@@ -339,7 +341,7 @@ void spi_master_transfer(spi_t *obj, const void *tx, size_t tx_length, void *rx,
339341
dma_set_handler(obj->spi.dma_chn_id_tx, (uint32_t) spi_dma_handler_tx, (uint32_t) obj, DMA_EVENT_ALL);
340342

341343
// Configure rx DMA
342-
PDMA->CHCTL |= 1 << obj->spi.dma_chn_id_rx; // Enable this DMA channel
344+
pdma_base->CHCTL |= 1 << obj->spi.dma_chn_id_rx; // Enable this DMA channel
343345
PDMA_SetTransferMode(obj->spi.dma_chn_id_rx,
344346
((struct nu_spi_var *) modinit->var)->pdma_perp_rx, // Peripheral connected to this PDMA
345347
0, // Scatter-gather disabled
@@ -380,6 +382,7 @@ void spi_master_transfer(spi_t *obj, const void *tx, size_t tx_length, void *rx,
380382
void spi_abort_asynch(spi_t *obj)
381383
{
382384
SPI_T *spi_base = (SPI_T *) NU_MODBASE(obj->spi.spi);
385+
PDMA_T *pdma_base = dma_modbase();
383386

384387
if (obj->spi.dma_usage != DMA_USAGE_NEVER) {
385388
// Receive FIFO Overrun in case of tx length > rx length on DMA way
@@ -388,18 +391,18 @@ void spi_abort_asynch(spi_t *obj)
388391
}
389392

390393
if (obj->spi.dma_chn_id_tx != DMA_ERROR_OUT_OF_CHANNELS) {
391-
PDMA_DisableInt(obj->spi.dma_chn_id_tx, 0);
394+
PDMA_DisableInt(obj->spi.dma_chn_id_tx, PDMA_INT_TRANS_DONE);
392395
// FIXME: On NUC472, next PDMA transfer will fail with PDMA_STOP() called. Cause is unknown.
393396
//PDMA_STOP(obj->spi.dma_chn_id_tx);
394-
PDMA->CHCTL &= ~(1 << obj->spi.dma_chn_id_tx);
397+
pdma_base->CHCTL &= ~(1 << obj->spi.dma_chn_id_tx);
395398
}
396399
SPI_DISABLE_TX_PDMA(((SPI_T *) NU_MODBASE(obj->spi.spi)));
397400

398401
if (obj->spi.dma_chn_id_rx != DMA_ERROR_OUT_OF_CHANNELS) {
399-
PDMA_DisableInt(obj->spi.dma_chn_id_rx, 0);
402+
PDMA_DisableInt(obj->spi.dma_chn_id_rx, PDMA_INT_TRANS_DONE);
400403
// FIXME: On NUC472, next PDMA transfer will fail with PDMA_STOP() called. Cause is unknown.
401404
//PDMA_STOP(obj->spi.dma_chn_id_rx);
402-
PDMA->CHCTL &= ~(1 << obj->spi.dma_chn_id_rx);
405+
pdma_base->CHCTL &= ~(1 << obj->spi.dma_chn_id_rx);
403406
}
404407
SPI_DISABLE_RX_PDMA(((SPI_T *) NU_MODBASE(obj->spi.spi)));
405408
}

targets/TARGET_NUVOTON/TARGET_NUC472/spi_api.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,10 @@ void spi_master_transfer(spi_t *obj, const void *tx, size_t tx_length, void *rx,
317317
MBED_ASSERT(modinit != NULL);
318318
MBED_ASSERT(modinit->modname == obj->spi.spi);
319319

320+
PDMA_T *pdma_base = dma_modbase();
321+
320322
// Configure tx DMA
321-
PDMA->CHCTL |= 1 << obj->spi.dma_chn_id_tx; // Enable this DMA channel
323+
pdma_base->CHCTL |= 1 << obj->spi.dma_chn_id_tx; // Enable this DMA channel
322324
PDMA_SetTransferMode(obj->spi.dma_chn_id_tx,
323325
((struct nu_spi_var *) modinit->var)->pdma_perp_tx, // Peripheral connected to this PDMA
324326
0, // Scatter-gather disabled
@@ -340,7 +342,7 @@ void spi_master_transfer(spi_t *obj, const void *tx, size_t tx_length, void *rx,
340342
dma_set_handler(obj->spi.dma_chn_id_tx, (uint32_t) spi_dma_handler_tx, (uint32_t) obj, DMA_EVENT_ALL);
341343

342344
// Configure rx DMA
343-
PDMA->CHCTL |= 1 << obj->spi.dma_chn_id_rx; // Enable this DMA channel
345+
pdma_base->CHCTL |= 1 << obj->spi.dma_chn_id_rx; // Enable this DMA channel
344346
PDMA_SetTransferMode(obj->spi.dma_chn_id_rx,
345347
((struct nu_spi_var *) modinit->var)->pdma_perp_rx, // Peripheral connected to this PDMA
346348
0, // Scatter-gather disabled
@@ -379,6 +381,7 @@ void spi_master_transfer(spi_t *obj, const void *tx, size_t tx_length, void *rx,
379381
void spi_abort_asynch(spi_t *obj)
380382
{
381383
SPI_T *spi_base = (SPI_T *) NU_MODBASE(obj->spi.spi);
384+
PDMA_T *pdma_base = dma_modbase();
382385

383386
if (obj->spi.dma_usage != DMA_USAGE_NEVER) {
384387
// Receive FIFO Overrun in case of tx length > rx length on DMA way
@@ -390,15 +393,15 @@ void spi_abort_asynch(spi_t *obj)
390393
PDMA_DisableInt(obj->spi.dma_chn_id_tx, 0);
391394
// FIXME: Next PDMA transfer will fail with PDMA_STOP() called. Cause is unknown.
392395
//PDMA_STOP(obj->spi.dma_chn_id_tx);
393-
PDMA->CHCTL &= ~(1 << obj->spi.dma_chn_id_tx);
396+
pdma_base->CHCTL &= ~(1 << obj->spi.dma_chn_id_tx);
394397
}
395398
SPI_DISABLE_TX_PDMA(((SPI_T *) NU_MODBASE(obj->spi.spi)));
396399

397400
if (obj->spi.dma_chn_id_rx != DMA_ERROR_OUT_OF_CHANNELS) {
398401
PDMA_DisableInt(obj->spi.dma_chn_id_rx, 0);
399402
// FIXME: Next PDMA transfer will fail with PDMA_STOP() called. Cause is unknown.
400403
//PDMA_STOP(obj->spi.dma_chn_id_rx);
401-
PDMA->CHCTL &= ~(1 << obj->spi.dma_chn_id_rx);
404+
pdma_base->CHCTL &= ~(1 << obj->spi.dma_chn_id_rx);
402405
}
403406
SPI_DISABLE_RX_PDMA(((SPI_T *) NU_MODBASE(obj->spi.spi)));
404407
}

0 commit comments

Comments
 (0)