Skip to content

Commit 680feb4

Browse files
committed
Support SPI CPP objects with different pinouts sharing the same peripheral
1 parent 632586e commit 680feb4

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

targets/TARGET_Silicon_Labs/TARGET_EFM32/common/objects.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ struct serial_s {
117117
#if DEVICE_SPI
118118
struct spi_s {
119119
USART_TypeDef *spi;
120-
int location;
120+
uint32_t location;
121+
uint32_t route;
121122
uint8_t bits;
122123
uint8_t master;
123124
#if DEVICE_SPI_ASYNCH

targets/TARGET_Silicon_Labs/TARGET_EFM32/spi_api.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,12 @@ void spi_enable_pins(spi_t *obj, uint8_t enable, PinName mosi, PinName miso, Pin
213213
obj->spi.spi->ROUTELOC0 &= ~_USART_ROUTELOC0_CSLOC_MASK;
214214
obj->spi.spi->ROUTELOC0 |= pin_location(cs, PinMap_SPI_MOSI)<<_USART_ROUTELOC0_CSLOC_SHIFT;
215215
}
216+
obj->spi.location = obj->spi.spi->ROUTELOC0;
217+
obj->spi.route = route;
216218
obj->spi.spi->ROUTEPEN = route;
217219
}
218220
#else
219-
uint32_t route = USART_ROUTE_CLKPEN | (obj->spi.location << _USART_ROUTE_LOCATION_SHIFT);
221+
uint32_t route = USART_ROUTE_CLKPEN;
220222

221223
if (mosi != NC) {
222224
route |= USART_ROUTE_TXPEN;
@@ -227,7 +229,9 @@ void spi_enable_pins(spi_t *obj, uint8_t enable, PinName mosi, PinName miso, Pin
227229
if (!obj->spi.master) {
228230
route |= USART_ROUTE_CSPEN;
229231
}
232+
route |= obj->spi.location << _USART_ROUTE_LOCATION_SHIFT;
230233
obj->spi.spi->ROUTE = route;
234+
obj->spi.route = route;
231235
}
232236
#endif
233237
void spi_enable(spi_t *obj, uint8_t enable)
@@ -324,25 +328,17 @@ void spi_format(spi_t *obj, int bits, int mode, int slave)
324328
default:
325329
clockMode = usartClockMode0;
326330
}
327-
328-
//save state
329-
#ifdef _USART_ROUTEPEN_RESETVALUE
330-
uint32_t route = obj->spi.spi->ROUTEPEN;
331-
uint32_t loc = obj->spi.spi->ROUTELOC0;
332-
#else
333-
uint32_t route = obj->spi.spi->ROUTE;
334-
#endif
335331
uint32_t iflags = obj->spi.spi->IEN;
336332
bool enabled = (obj->spi.spi->STATUS & (USART_STATUS_RXENS | USART_STATUS_TXENS)) != 0;
337333

338334
usart_init(obj, 100000, databits, (slave ? false : true), clockMode);
339335

340336
//restore state
341337
#ifdef _USART_ROUTEPEN_RESETVALUE
342-
obj->spi.spi->ROUTEPEN = route;
343-
obj->spi.spi->ROUTELOC0 = loc;
338+
obj->spi.spi->ROUTEPEN = obj->spi.route;
339+
obj->spi.spi->ROUTELOC0 = obj->spi.location;
344340
#else
345-
obj->spi.spi->ROUTE = route;
341+
obj->spi.spi->ROUTE = obj->spi.route;
346342
#endif
347343
obj->spi.spi->IEN = iflags;
348344

0 commit comments

Comments
 (0)