Skip to content
This repository was archived by the owner on Feb 21, 2020. It is now read-only.

Commit ed94251

Browse files
committed
Improoved SPI, now is possible to use all pins as NSS in SPIx.begin(pin)
1 parent a2baafe commit ed94251

File tree

1 file changed

+18
-67
lines changed

1 file changed

+18
-67
lines changed

cores/arduino/SPI.cpp

Lines changed: 18 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,25 @@ void SPIClass::begin()
4949
/* Enable SPI2 clock */
5050
SPI2_CLK_ENABLE();
5151

52-
/* Configure SPI2 SCK pin(PD3) as alternate function */
52+
/* Configure SPI2 SCK PIN defined in SPI.h */
5353
GPIO_InitStruct.Pin = SPI2_SCK_PIN;
5454
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
5555
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
5656
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
5757
GPIO_InitStruct.Alternate = SPI2_SCK_AF;
5858
HAL_GPIO_Init(SPI2_SCK_GPIO_PORT, &GPIO_InitStruct);
5959

60-
/* Configure SPI2 MISO pin(PB14) as alternate function */
60+
/* Configure SPI2 MISO PIN defined in SPI.h */
6161
GPIO_InitStruct.Pin = SPI2_MISO_PIN;
6262
GPIO_InitStruct.Alternate = SPI2_MISO_AF;
6363
HAL_GPIO_Init(SPI2_MISO_GPIO_PORT, &GPIO_InitStruct);
6464

65-
/* Configure SPI2 MOSI pin(PC3) as alternate function */
65+
/* Configure SPI2 MOSI PIN defined in SPI.h */
6666
GPIO_InitStruct.Pin = SPI2_MOSI_PIN;
6767
GPIO_InitStruct.Alternate = SPI2_MOSI_AF;
6868
HAL_GPIO_Init(SPI2_MOSI_GPIO_PORT, &GPIO_InitStruct);
69-
} else if (hSPIx.Instance == HAL_SPI1)
69+
}
70+
else if (hSPIx.Instance == HAL_SPI1)
7071
{
7172
GPIO_InitTypeDef GPIO_InitStruct;
7273

@@ -79,32 +80,32 @@ void SPIClass::begin()
7980
/* Enable SPI1 clock */
8081
SPI1_CLK_ENABLE();
8182

82-
/* Configure SPI1 SCK pin(PB3) as alternate function */
83+
/* Configure SPI1 SCK PIN defined in SPI.h */
8384
GPIO_InitStruct.Pin = SPI1_SCK_PIN;
8485
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
8586
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
8687
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
8788
GPIO_InitStruct.Alternate = SPI1_SCK_AF;
8889
HAL_GPIO_Init(SPI1_SCK_GPIO_PORT, &GPIO_InitStruct);
8990

90-
/* Configure SPI1 MISO pin(PB4) as alternate function */
91+
/* Configure SPI1 MISO PIN defined in SPI.h */
9192
GPIO_InitStruct.Pin = SPI1_MISO_PIN;
9293
GPIO_InitStruct.Alternate = SPI1_MISO_AF;
9394
HAL_GPIO_Init(SPI1_MISO_GPIO_PORT, &GPIO_InitStruct);
9495

95-
/* Configure SPI1 MOSI pin(PB5) as alternate function */
96+
/* Configure SPI1 MOSI PIN defined in SPI.h */
9697
GPIO_InitStruct.Pin = SPI1_MOSI_PIN;
9798
GPIO_InitStruct.Alternate = SPI1_MOSI_AF;
9899
HAL_GPIO_Init(SPI1_MOSI_GPIO_PORT, &GPIO_InitStruct);
99100

100101
/* Put SPI1 NSS pin as Output pin in order to be used as normal GPIO in Master mode */
101102
GPIO_InitStruct.Pin = SPI1_NSS_PIN;
102-
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
103-
GPIO_InitStruct.Pull = GPIO_NOPULL;
103+
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
104+
GPIO_InitStruct.Pull = GPIO_NOPULL;
104105
HAL_GPIO_Init(SPI1_NSS_GPIO_PORT, &GPIO_InitStruct);
105106
}
106107

107-
/* SPI general configuration ----------------------------------------------------*/
108+
/* SPI general configuration */
108109
hSPIx.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
109110
hSPIx.Init.Direction = SPI_DIRECTION_2LINES;
110111
hSPIx.Init.CLKPhase = SPI_PHASE_1EDGE;
@@ -127,29 +128,9 @@ void SPIClass::begin()
127128
*/
128129
void SPIClass::begin(uint8_t slaveSelectPin)
129130
{
130-
/* SPIx configuration */
131+
/* SPIx configuration */
131132
begin();
132-
133-
/* TOBEFIXED: The correct way to proceed here it is to map the Arduino slaveSelectPin provided as parameter */
134-
/* with the Cube GPIO port and pin number and then call the HAL_GPIO_Init with the correct values */
135-
/* At the moment only pin 10 is supported */
136-
switch(slaveSelectPin)
137-
{
138-
case 10: /* Pin(PA15) */
139-
default:
140-
{
141-
GPIO_InitTypeDef GPIO_InitStruct;
142-
143-
__HAL_RCC_GPIOA_CLK_ENABLE();
144-
GPIO_InitStruct.Pin = GPIO_PIN_15;
145-
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
146-
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
147-
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
148-
149-
HAL_GPIO_Init(HAL_GPIOA, &GPIO_InitStruct);
150-
break;
151-
}
152-
}
133+
pinMode(slaveSelectPin, OUTPUT);
153134
}
154135

155136
/**
@@ -234,34 +215,14 @@ void SPIClass::transfer(void *buf, size_t count)
234215
uint8_t SPIClass::transfer(uint8_t slaveSelectPin, uint8_t val, SPITransferMode transferMode)
235216
{
236217
uint8_t rxdata;
237-
238-
/* TOBEFIXED: The correct way to proceed here it is to map the Arduino pin provided as parameter */
239-
/* with the Cube GPIO port and pin number and then call the HAL_GPIO_WritePin */
240-
switch(slaveSelectPin)
241-
{
242-
case 10: /* Pin(PA15) */
243-
default:
244-
{
245-
HAL_GPIO_WritePin(HAL_GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
246-
break;
247-
}
248-
}
249-
218+
digitalWrite(slaveSelectPin,LOW);
250219
HAL_SPI_TransmitReceive(&hSPIx, (uint8_t *)&val, (uint8_t*)&rxdata, 1,5000);
251220

252221
/* If transferMode is SPI_CONTINUE we need to hold CS GPIO pin low */
253222
/* If transferMode is SPI_LAST we need to put CS GPIO pin high */
254223
if(transferMode == SPI_LAST)
255224
{
256-
switch(slaveSelectPin)
257-
{
258-
case 10: /* Pin(PA15) */
259-
default:
260-
{
261-
HAL_GPIO_WritePin(HAL_GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
262-
break;
263-
}
264-
}
225+
digitalWrite(slaveSelectPin, HIGH);
265226
}
266227

267228
return rxdata;
@@ -353,7 +314,8 @@ void SPIClass::end()
353314
HAL_GPIO_DeInit(SPI2_MISO_GPIO_PORT, SPI2_MISO_PIN);
354315
/* Configure SPI2 MOSI as alternate function */
355316
HAL_GPIO_DeInit(SPI2_MOSI_GPIO_PORT, SPI2_MOSI_PIN);
356-
} else if (hSPIx.Instance == HAL_SPI1)
317+
}
318+
else if (hSPIx.Instance == HAL_SPI1)
357319
{
358320
/*##-1- Reset peripherals ##################################################*/
359321
SPI1_FORCE_RESET();
@@ -378,18 +340,7 @@ void SPIClass::end()
378340
*/
379341
void SPIClass::end(uint8_t slaveSelectPin)
380342
{
381-
/* TOBEFIXED: The correct way to proceed here it is to map the Arduino slaveSelectPin provided as parameter */
382-
/* with the Cube GPIO port and pin number and then call the HAL_GPIO_DeInit with the correct values */
383-
switch(slaveSelectPin)
384-
{
385-
case 10: /* Pin(PA15) */
386-
default:
387-
{
388-
HAL_GPIO_DeInit(HAL_GPIOA, GPIO_PIN_15);
389-
break;
390-
}
391-
}
392-
343+
pinMode(slaveSelectPin, INPUT);
393344
end();
394345
}
395346

0 commit comments

Comments
 (0)