diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K66F/TARGET_FRDM/PeripheralNames.h b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K66F/TARGET_FRDM/PeripheralNames.h index 1849dea49ee..ad699b395d9 100644 --- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K66F/TARGET_FRDM/PeripheralNames.h +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K66F/TARGET_FRDM/PeripheralNames.h @@ -124,7 +124,7 @@ typedef enum { DAC_0 = 0 } DACName; - +#define DEVICE_SPI_COUNT 3 typedef enum { SPI_0 = 0, SPI_1 = 1, diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K66F/spi_api.c b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K66F/spi_api.c index 5d516baac54..ee75deef428 100644 --- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K66F/spi_api.c +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_K66F/spi_api.c @@ -32,6 +32,25 @@ static SPI_Type *const spi_address[] = SPI_BASE_PTRS; /* Array of SPI bus clock frequencies */ static clock_name_t const spi_clocks[] = SPI_CLOCK_FREQS; +SPIName spi_get_peripheral_name(PinName mosi, PinName miso, PinName sclk) +{ + SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); + SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); + SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK); + + SPIName spi_per; + + // If 3 wire SPI is used, the miso is not connected. + if (miso == NC) { + spi_per = (SPIName)pinmap_merge(spi_mosi, spi_sclk); + } else { + SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); + spi_per = (SPIName)pinmap_merge(spi_data, spi_sclk); + } + + return spi_per; +} + void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { // determine the SPI to use @@ -95,7 +114,7 @@ void spi_frequency(spi_t *obj, int hz) DSPI_MasterSetDelayTimes(spi_address[obj->instance], kDSPI_Ctar0, kDSPI_LastSckToPcs, busClock, 500000000 / hz); } -static inline int spi_readable(spi_t * obj) +static inline int spi_readable(spi_t *obj) { return (DSPI_GetStatusFlags(spi_address[obj->instance]) & kDSPI_RxFifoDrainRequestFlag); } @@ -119,17 +138,18 @@ int spi_master_write(spi_t *obj, int value) } int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, - char *rx_buffer, int rx_length, char write_fill) { + char *rx_buffer, int rx_length, char write_fill) +{ int total = (tx_length > rx_length) ? tx_length : rx_length; // Default write is done in each and every call, in future can create HAL API instead DSPI_SetDummyData(spi_address[obj->instance], write_fill); - DSPI_MasterTransferBlocking(spi_address[obj->instance], &(dspi_transfer_t){ - .txData = (uint8_t *)tx_buffer, - .rxData = (uint8_t *)rx_buffer, - .dataSize = total, - .configFlags = kDSPI_MasterCtar0 | kDSPI_MasterPcs0 | kDSPI_MasterPcsContinuous, + DSPI_MasterTransferBlocking(spi_address[obj->instance], &(dspi_transfer_t) { + .txData = (uint8_t *)tx_buffer, + .rxData = (uint8_t *)rx_buffer, + .dataSize = total, + .configFlags = kDSPI_MasterCtar0 | kDSPI_MasterPcs0 | kDSPI_MasterPcsContinuous, }); DSPI_ClearStatusFlags(spi_address[obj->instance], kDSPI_RxFifoDrainRequestFlag | kDSPI_EndOfQueueFlag); diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW24D/TARGET_FRDM/PeripheralNames.h b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW24D/TARGET_FRDM/PeripheralNames.h index 03a5b687b50..1a8445e1ce3 100644 --- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW24D/TARGET_FRDM/PeripheralNames.h +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW24D/TARGET_FRDM/PeripheralNames.h @@ -93,7 +93,7 @@ typedef enum { DAC_0 = 0 } DACName; - +#define DEVICE_SPI_COUNT 2 typedef enum { SPI_0 = 0, SPI_1 = 1, diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW24D/spi_api.c b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW24D/spi_api.c index 0182936cb93..f0526032a17 100644 --- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW24D/spi_api.c +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW24D/spi_api.c @@ -32,6 +32,25 @@ static SPI_Type *const spi_address[] = SPI_BASE_PTRS; /* Array of SPI bus clock frequencies */ static clock_name_t const spi_clocks[] = SPI_CLOCK_FREQS; +SPIName spi_get_peripheral_name(PinName mosi, PinName miso, PinName sclk) +{ + SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); + SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); + SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK); + + SPIName spi_per; + + // If 3 wire SPI is used, the miso is not connected. + if (miso == NC) { + spi_per = (SPIName)pinmap_merge(spi_mosi, spi_sclk); + } else { + SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); + spi_per = (SPIName)pinmap_merge(spi_data, spi_sclk); + } + + return spi_per; +} + void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { // determine the SPI to use @@ -94,7 +113,7 @@ void spi_frequency(spi_t *obj, int hz) DSPI_MasterSetDelayTimes(spi_address[obj->instance], kDSPI_Ctar0, kDSPI_LastSckToPcs, busClock, 500000000 / hz); } -static inline int spi_readable(spi_t * obj) +static inline int spi_readable(spi_t *obj) { return (DSPI_GetStatusFlags(spi_address[obj->instance]) & kDSPI_RxFifoDrainRequestFlag); } @@ -118,7 +137,8 @@ int spi_master_write(spi_t *obj, int value) } int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, - char *rx_buffer, int rx_length, char write_fill) { + char *rx_buffer, int rx_length, char write_fill) +{ int total = (tx_length > rx_length) ? tx_length : rx_length; for (int i = 0; i < total; i++) { diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW41Z/TARGET_FRDM/PeripheralNames.h b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW41Z/TARGET_FRDM/PeripheralNames.h index 3436ae848e8..2cee79bba73 100644 --- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW41Z/TARGET_FRDM/PeripheralNames.h +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW41Z/TARGET_FRDM/PeripheralNames.h @@ -63,7 +63,7 @@ typedef enum { DAC_0 = 0 } DACName; - +#define DEVICE_SPI_COUNT 2 typedef enum { SPI_0 = 0, SPI_1 = 1, diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW41Z/TARGET_RAPIDIOT/PeripheralNames.h b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW41Z/TARGET_RAPIDIOT/PeripheralNames.h index c30c20b51f7..1f3d83cc257 100644 --- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW41Z/TARGET_RAPIDIOT/PeripheralNames.h +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW41Z/TARGET_RAPIDIOT/PeripheralNames.h @@ -64,7 +64,7 @@ typedef enum { DAC_0 = 0 } DACName; - +#define DEVICE_SPI_COUNT 2 typedef enum { SPI_0 = 0, SPI_1 = 1, diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW41Z/spi_api.c b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW41Z/spi_api.c index 0182936cb93..f0526032a17 100644 --- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW41Z/spi_api.c +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_KW41Z/spi_api.c @@ -32,6 +32,25 @@ static SPI_Type *const spi_address[] = SPI_BASE_PTRS; /* Array of SPI bus clock frequencies */ static clock_name_t const spi_clocks[] = SPI_CLOCK_FREQS; +SPIName spi_get_peripheral_name(PinName mosi, PinName miso, PinName sclk) +{ + SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); + SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); + SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK); + + SPIName spi_per; + + // If 3 wire SPI is used, the miso is not connected. + if (miso == NC) { + spi_per = (SPIName)pinmap_merge(spi_mosi, spi_sclk); + } else { + SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); + spi_per = (SPIName)pinmap_merge(spi_data, spi_sclk); + } + + return spi_per; +} + void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { // determine the SPI to use @@ -94,7 +113,7 @@ void spi_frequency(spi_t *obj, int hz) DSPI_MasterSetDelayTimes(spi_address[obj->instance], kDSPI_Ctar0, kDSPI_LastSckToPcs, busClock, 500000000 / hz); } -static inline int spi_readable(spi_t * obj) +static inline int spi_readable(spi_t *obj) { return (DSPI_GetStatusFlags(spi_address[obj->instance]) & kDSPI_RxFifoDrainRequestFlag); } @@ -118,7 +137,8 @@ int spi_master_write(spi_t *obj, int value) } int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, - char *rx_buffer, int rx_length, char write_fill) { + char *rx_buffer, int rx_length, char write_fill) +{ int total = (tx_length > rx_length) ? tx_length : rx_length; for (int i = 0; i < total; i++) { diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/TARGET_FRDM/PeripheralNames.h b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/TARGET_FRDM/PeripheralNames.h index 65d2b635b81..66aee9d7b14 100644 --- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/TARGET_FRDM/PeripheralNames.h +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/TARGET_FRDM/PeripheralNames.h @@ -123,7 +123,7 @@ typedef enum { DAC_0 = 0 } DACName; - +#define DEVICE_SPI_COUNT 3 typedef enum { SPI_0 = 0, SPI_1 = 1, diff --git a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/spi_api.c b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/spi_api.c index 61d33135ed4..d7d9c9f6ac5 100644 --- a/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/spi_api.c +++ b/targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/spi_api.c @@ -33,6 +33,25 @@ static SPI_Type *const spi_address[] = SPI_BASE_PTRS; /* Array of SPI bus clock frequencies */ static clock_name_t const spi_clocks[] = SPI_CLOCK_FREQS; +SPIName spi_get_peripheral_name(PinName mosi, PinName miso, PinName sclk) +{ + SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); + SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); + SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK); + + SPIName spi_per; + + // If 3 wire SPI is used, the miso is not connected. + if (miso == NC) { + spi_per = (SPIName)pinmap_merge(spi_mosi, spi_sclk); + } else { + SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); + spi_per = (SPIName)pinmap_merge(spi_data, spi_sclk); + } + + return spi_per; +} + void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { // determine the SPI to use @@ -104,7 +123,7 @@ void spi_frequency(spi_t *obj, int hz) DSPI_MasterSetDelayTimes(spi_address[obj->spi.instance], kDSPI_Ctar0, kDSPI_LastSckToPcs, busClock, 500000000 / hz); } -static inline int spi_readable(spi_t * obj) +static inline int spi_readable(spi_t *obj) { return (DSPI_GetStatusFlags(spi_address[obj->spi.instance]) & kDSPI_RxFifoDrainRequestFlag); } @@ -128,17 +147,18 @@ int spi_master_write(spi_t *obj, int value) } int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, - char *rx_buffer, int rx_length, char write_fill) { + char *rx_buffer, int rx_length, char write_fill) +{ int total = (tx_length > rx_length) ? tx_length : rx_length; // Default write is done in each and every call, in future can create HAL API instead DSPI_SetDummyData(spi_address[obj->spi.instance], write_fill); - DSPI_MasterTransferBlocking(spi_address[obj->spi.instance], &(dspi_transfer_t){ - .txData = (uint8_t *)tx_buffer, - .rxData = (uint8_t *)rx_buffer, - .dataSize = total, - .configFlags = kDSPI_MasterCtar0 | kDSPI_MasterPcs0 | kDSPI_MasterPcsContinuous, + DSPI_MasterTransferBlocking(spi_address[obj->spi.instance], &(dspi_transfer_t) { + .txData = (uint8_t *)tx_buffer, + .rxData = (uint8_t *)rx_buffer, + .dataSize = total, + .configFlags = kDSPI_MasterCtar0 | kDSPI_MasterPcs0 | kDSPI_MasterPcsContinuous, }); DSPI_ClearStatusFlags(spi_address[obj->spi.instance], kDSPI_RxFifoDrainRequestFlag | kDSPI_EndOfQueueFlag); @@ -181,7 +201,7 @@ static int32_t spi_master_transfer_asynch(spi_t *obj) obj->spi.status = kDSPI_Busy; if (obj->spi.spiDmaMasterRx.dmaUsageState == DMA_USAGE_ALLOCATED || - obj->spi.spiDmaMasterRx.dmaUsageState == DMA_USAGE_TEMPORARY_ALLOCATED) { + obj->spi.spiDmaMasterRx.dmaUsageState == DMA_USAGE_TEMPORARY_ALLOCATED) { status = DSPI_MasterTransferEDMA(spi_address[obj->spi.instance], &obj->spi.spi_dma_master_handle, &masterXfer); if (status == kStatus_DSPI_OutOfRange) { if (obj->spi.bits > 8) { @@ -313,14 +333,14 @@ static void spi_buffer_set(spi_t *obj, const void *tx, uint32_t tx_length, void void spi_master_transfer(spi_t *obj, const void *tx, size_t tx_length, void *rx, size_t rx_length, uint8_t bit_width, uint32_t handler, uint32_t event, DMAUsage hint) { - if(spi_active(obj)) { + if (spi_active(obj)) { return; } /* check corner case */ - if(tx_length == 0) { + if (tx_length == 0) { tx_length = rx_length; - tx = (void*) 0; + tx = (void *) 0; } /* First, set the buffer */ @@ -421,13 +441,13 @@ uint32_t spi_irq_handler_asynch(spi_t *obj) void spi_abort_asynch(spi_t *obj) { // If we're not currently transferring, then there's nothing to do here - if(spi_active(obj) == 0) { + if (spi_active(obj) == 0) { return; } // Determine whether we're running DMA or interrupt if (obj->spi.spiDmaMasterRx.dmaUsageState == DMA_USAGE_ALLOCATED || - obj->spi.spiDmaMasterRx.dmaUsageState == DMA_USAGE_TEMPORARY_ALLOCATED) { + obj->spi.spiDmaMasterRx.dmaUsageState == DMA_USAGE_TEMPORARY_ALLOCATED) { DSPI_MasterTransferAbortEDMA(spi_address[obj->spi.instance], &obj->spi.spi_dma_master_handle); /* Release the dma channels if they were opportunistically allocated */ if (obj->spi.spiDmaMasterRx.dmaUsageState == DMA_USAGE_TEMPORARY_ALLOCATED) {