From 946321032cbf9858f1e56c1d87d08914f1f213c8 Mon Sep 17 00:00:00 2001 From: Steven Cooreman Date: Thu, 30 Mar 2017 12:42:25 +0200 Subject: [PATCH] Support SPI CPP objects with different pinouts sharing the same peripheral --- .../TARGET_EFM32/common/objects.h | 3 ++- .../TARGET_EFM32/spi_api.c | 20 ++++++++----------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/targets/TARGET_Silicon_Labs/TARGET_EFM32/common/objects.h b/targets/TARGET_Silicon_Labs/TARGET_EFM32/common/objects.h index 0576ea5e21..596e178a0d 100644 --- a/targets/TARGET_Silicon_Labs/TARGET_EFM32/common/objects.h +++ b/targets/TARGET_Silicon_Labs/TARGET_EFM32/common/objects.h @@ -117,7 +117,8 @@ struct serial_s { #if DEVICE_SPI struct spi_s { USART_TypeDef *spi; - int location; + uint32_t location; + uint32_t route; uint8_t bits; uint8_t master; #if DEVICE_SPI_ASYNCH diff --git a/targets/TARGET_Silicon_Labs/TARGET_EFM32/spi_api.c b/targets/TARGET_Silicon_Labs/TARGET_EFM32/spi_api.c index ad387d86ae..abb401247f 100644 --- a/targets/TARGET_Silicon_Labs/TARGET_EFM32/spi_api.c +++ b/targets/TARGET_Silicon_Labs/TARGET_EFM32/spi_api.c @@ -213,10 +213,12 @@ void spi_enable_pins(spi_t *obj, uint8_t enable, PinName mosi, PinName miso, Pin obj->spi.spi->ROUTELOC0 &= ~_USART_ROUTELOC0_CSLOC_MASK; obj->spi.spi->ROUTELOC0 |= pin_location(cs, PinMap_SPI_MOSI)<<_USART_ROUTELOC0_CSLOC_SHIFT; } + obj->spi.location = obj->spi.spi->ROUTELOC0; + obj->spi.route = route; obj->spi.spi->ROUTEPEN = route; } #else - uint32_t route = USART_ROUTE_CLKPEN | (obj->spi.location << _USART_ROUTE_LOCATION_SHIFT); + uint32_t route = USART_ROUTE_CLKPEN; if (mosi != NC) { route |= USART_ROUTE_TXPEN; @@ -227,7 +229,9 @@ void spi_enable_pins(spi_t *obj, uint8_t enable, PinName mosi, PinName miso, Pin if (!obj->spi.master) { route |= USART_ROUTE_CSPEN; } + route |= obj->spi.location << _USART_ROUTE_LOCATION_SHIFT; obj->spi.spi->ROUTE = route; + obj->spi.route = route; } #endif void spi_enable(spi_t *obj, uint8_t enable) @@ -324,14 +328,6 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) default: clockMode = usartClockMode0; } - - //save state -#ifdef _USART_ROUTEPEN_RESETVALUE - uint32_t route = obj->spi.spi->ROUTEPEN; - uint32_t loc = obj->spi.spi->ROUTELOC0; -#else - uint32_t route = obj->spi.spi->ROUTE; -#endif uint32_t iflags = obj->spi.spi->IEN; bool enabled = (obj->spi.spi->STATUS & (USART_STATUS_RXENS | USART_STATUS_TXENS)) != 0; @@ -339,10 +335,10 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) //restore state #ifdef _USART_ROUTEPEN_RESETVALUE - obj->spi.spi->ROUTEPEN = route; - obj->spi.spi->ROUTELOC0 = loc; + obj->spi.spi->ROUTEPEN = obj->spi.route; + obj->spi.spi->ROUTELOC0 = obj->spi.location; #else - obj->spi.spi->ROUTE = route; + obj->spi.spi->ROUTE = obj->spi.route; #endif obj->spi.spi->IEN = iflags;