diff --git a/targets/TARGET_STM/stm_spi_api.c b/targets/TARGET_STM/stm_spi_api.c index a62512c504..5976cad898 100644 --- a/targets/TARGET_STM/stm_spi_api.c +++ b/targets/TARGET_STM/stm_spi_api.c @@ -111,21 +111,18 @@ SPIName spi_get_peripheral_name(PinName mosi, PinName miso, PinName sclk) return spi_per; } -void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) +#if EXPLICIT_PINMAP_READY +#define SPI_INIT_DIRECT spi_init_direct +void spi_init_direct(spi_t *obj, const spi_pinmap_t *pinmap) +#else +#define SPI_INIT_DIRECT _spi_init_direct +static void _spi_init_direct(spi_t *obj, const spi_pinmap_t *pinmap) +#endif { struct spi_s *spiobj = SPI_S(obj); SPI_HandleTypeDef *handle = &(spiobj->handle); - // Determine the SPI to use - 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_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL); - - SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); - SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); - - spiobj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); + spiobj->spi = (SPIName)pinmap->peripheral; MBED_ASSERT(spiobj->spi != (SPIName)NC); #if defined SPI1_BASE @@ -172,15 +169,19 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel #endif // Configure the SPI pins - pinmap_pinout(mosi, PinMap_SPI_MOSI); - pinmap_pinout(miso, PinMap_SPI_MISO); - pinmap_pinout(sclk, PinMap_SPI_SCLK); - spiobj->pin_miso = miso; - spiobj->pin_mosi = mosi; - spiobj->pin_sclk = sclk; - spiobj->pin_ssel = ssel; - if (ssel != NC) { - pinmap_pinout(ssel, PinMap_SPI_SSEL); + pin_function(pinmap->mosi_pin, pinmap->mosi_function); + pin_mode(pinmap->mosi_pin, PullNone); + pin_function(pinmap->miso_pin, pinmap->miso_function); + pin_mode(pinmap->miso_pin, PullNone); + pin_function(pinmap->sclk_pin, pinmap->sclk_function); + pin_mode(pinmap->sclk_pin, PullNone); + spiobj->pin_miso = pinmap->miso_pin; + spiobj->pin_mosi = pinmap->mosi_pin; + spiobj->pin_sclk = pinmap->sclk_pin; + spiobj->pin_ssel = pinmap->ssel_pin; + if (pinmap->ssel_pin != NC) { + pin_function(pinmap->ssel_pin, pinmap->ssel_function); + pin_mode(pinmap->ssel_pin, PullNone); handle->Init.NSS = SPI_NSS_HARD_OUTPUT; #if defined(SPI_NSS_PULSE_ENABLE) handle->Init.NSSPMode = SPI_NSS_PULSE_ENABLE; @@ -197,7 +198,7 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel handle->Init.Mode = SPI_MODE_MASTER; handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256; - if (miso != NC) { + if (pinmap->miso_pin != NC) { handle->Init.Direction = SPI_DIRECTION_2LINES; } else { handle->Init.Direction = SPI_DIRECTION_1LINE; @@ -225,6 +226,29 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel init_spi(obj); } +void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) +{ + // determine the SPI to use + uint32_t spi_mosi = pinmap_peripheral(mosi, PinMap_SPI_MOSI); + uint32_t spi_miso = pinmap_peripheral(miso, PinMap_SPI_MISO); + uint32_t spi_sclk = pinmap_peripheral(sclk, PinMap_SPI_SCLK); + uint32_t spi_ssel = pinmap_peripheral(ssel, PinMap_SPI_SSEL); + uint32_t spi_data = pinmap_merge(spi_mosi, spi_miso); + uint32_t spi_cntl = pinmap_merge(spi_sclk, spi_ssel); + + int peripheral = (int)pinmap_merge(spi_data, spi_cntl); + + // pin out the spi pins + int mosi_function = (int)pinmap_find_function(mosi, PinMap_SPI_MOSI); + int miso_function = (int)pinmap_find_function(miso, PinMap_SPI_MISO); + int sclk_function = (int)pinmap_find_function(sclk, PinMap_SPI_SCLK); + int ssel_function = (int)pinmap_find_function(ssel, PinMap_SPI_SSEL); + + const spi_pinmap_t explicit_spi_pinmap = {peripheral, mosi, mosi_function, miso, miso_function, sclk, sclk_function, ssel, ssel_function}; + + SPI_INIT_DIRECT(obj, &explicit_spi_pinmap); +} + void spi_free(spi_t *obj) { struct spi_s *spiobj = SPI_S(obj);