@@ -49,24 +49,25 @@ void SPIClass::begin()
49
49
/* Enable SPI2 clock */
50
50
SPI2_CLK_ENABLE ();
51
51
52
- /* Configure SPI2 SCK pin(PD3) as alternate function */
52
+ /* Configure SPI2 SCK PIN defined in SPI.h */
53
53
GPIO_InitStruct.Pin = SPI2_SCK_PIN;
54
54
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
55
55
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
56
56
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
57
57
GPIO_InitStruct.Alternate = SPI2_SCK_AF;
58
58
HAL_GPIO_Init (SPI2_SCK_GPIO_PORT, &GPIO_InitStruct);
59
59
60
- /* Configure SPI2 MISO pin(PB14) as alternate function */
60
+ /* Configure SPI2 MISO PIN defined in SPI.h */
61
61
GPIO_InitStruct.Pin = SPI2_MISO_PIN;
62
62
GPIO_InitStruct.Alternate = SPI2_MISO_AF;
63
63
HAL_GPIO_Init (SPI2_MISO_GPIO_PORT, &GPIO_InitStruct);
64
64
65
- /* Configure SPI2 MOSI pin(PC3) as alternate function */
65
+ /* Configure SPI2 MOSI PIN defined in SPI.h */
66
66
GPIO_InitStruct.Pin = SPI2_MOSI_PIN;
67
67
GPIO_InitStruct.Alternate = SPI2_MOSI_AF;
68
68
HAL_GPIO_Init (SPI2_MOSI_GPIO_PORT, &GPIO_InitStruct);
69
- } else if (hSPIx.Instance == HAL_SPI1)
69
+ }
70
+ else if (hSPIx.Instance == HAL_SPI1)
70
71
{
71
72
GPIO_InitTypeDef GPIO_InitStruct;
72
73
@@ -79,32 +80,32 @@ void SPIClass::begin()
79
80
/* Enable SPI1 clock */
80
81
SPI1_CLK_ENABLE ();
81
82
82
- /* Configure SPI1 SCK pin(PB3) as alternate function */
83
+ /* Configure SPI1 SCK PIN defined in SPI.h */
83
84
GPIO_InitStruct.Pin = SPI1_SCK_PIN;
84
85
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
85
86
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
86
87
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
87
88
GPIO_InitStruct.Alternate = SPI1_SCK_AF;
88
89
HAL_GPIO_Init (SPI1_SCK_GPIO_PORT, &GPIO_InitStruct);
89
90
90
- /* Configure SPI1 MISO pin(PB4) as alternate function */
91
+ /* Configure SPI1 MISO PIN defined in SPI.h */
91
92
GPIO_InitStruct.Pin = SPI1_MISO_PIN;
92
93
GPIO_InitStruct.Alternate = SPI1_MISO_AF;
93
94
HAL_GPIO_Init (SPI1_MISO_GPIO_PORT, &GPIO_InitStruct);
94
95
95
- /* Configure SPI1 MOSI pin(PB5) as alternate function */
96
+ /* Configure SPI1 MOSI PIN defined in SPI.h */
96
97
GPIO_InitStruct.Pin = SPI1_MOSI_PIN;
97
98
GPIO_InitStruct.Alternate = SPI1_MOSI_AF;
98
99
HAL_GPIO_Init (SPI1_MOSI_GPIO_PORT, &GPIO_InitStruct);
99
100
100
101
/* Put SPI1 NSS pin as Output pin in order to be used as normal GPIO in Master mode */
101
102
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;
104
105
HAL_GPIO_Init (SPI1_NSS_GPIO_PORT, &GPIO_InitStruct);
105
106
}
106
107
107
- /* SPI general configuration ---------------------------------------------------- */
108
+ /* SPI general configuration */
108
109
hSPIx.Init .BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
109
110
hSPIx.Init .Direction = SPI_DIRECTION_2LINES;
110
111
hSPIx.Init .CLKPhase = SPI_PHASE_1EDGE;
@@ -127,29 +128,9 @@ void SPIClass::begin()
127
128
*/
128
129
void SPIClass::begin (uint8_t slaveSelectPin)
129
130
{
130
- /* SPIx configuration */
131
+ /* SPIx configuration */
131
132
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);
153
134
}
154
135
155
136
/* *
@@ -234,34 +215,14 @@ void SPIClass::transfer(void *buf, size_t count)
234
215
uint8_t SPIClass::transfer (uint8_t slaveSelectPin, uint8_t val, SPITransferMode transferMode)
235
216
{
236
217
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);
250
219
HAL_SPI_TransmitReceive (&hSPIx, (uint8_t *)&val, (uint8_t *)&rxdata, 1 ,5000 );
251
220
252
221
/* If transferMode is SPI_CONTINUE we need to hold CS GPIO pin low */
253
222
/* If transferMode is SPI_LAST we need to put CS GPIO pin high */
254
223
if (transferMode == SPI_LAST)
255
224
{
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);
265
226
}
266
227
267
228
return rxdata;
@@ -353,7 +314,8 @@ void SPIClass::end()
353
314
HAL_GPIO_DeInit (SPI2_MISO_GPIO_PORT, SPI2_MISO_PIN);
354
315
/* Configure SPI2 MOSI as alternate function */
355
316
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)
357
319
{
358
320
/* ##-1- Reset peripherals ##################################################*/
359
321
SPI1_FORCE_RESET ();
@@ -378,18 +340,7 @@ void SPIClass::end()
378
340
*/
379
341
void SPIClass::end (uint8_t slaveSelectPin)
380
342
{
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);
393
344
end ();
394
345
}
395
346
0 commit comments