11
11
* published by the Free Software Foundation.
12
12
*/
13
13
14
+ /*
15
+ * Arduino srl - www.arduino.org
16
+ * 2016 Jun 9: Edited Francesco Alessi (alfran) - francesco@arduino.org
17
+ */
18
+
14
19
extern " C" {
15
20
#include < stdlib.h>
16
21
#include < string.h>
17
22
#include < inttypes.h>
18
23
#include " stm32f4xx_hal.h"
19
-
20
24
}
21
25
22
26
#include " SPI.h"
@@ -27,51 +31,81 @@ SPIClass::SPIClass(SPI_TypeDef *spiInstance)
27
31
}
28
32
29
33
/* *
30
- * @brief Configure SPI, configure relatives IOs and enable SPIx
34
+ * @brief Configure SPI, configure relatives IOs and enable SPI2
31
35
* @param None
32
36
* @retval None
33
37
*/
34
38
void SPIClass::begin ()
35
39
{
36
- if (hSPIx.Instance == SPIx )
40
+ if (hSPIx.Instance == HAL_SPI2 )
37
41
{
38
42
GPIO_InitTypeDef GPIO_InitStruct;
39
43
40
44
/* Enable GPIO clock */
41
- SPIx_SCK_CLK_ENABLE ();
42
- SPIx_MISO_CLK_ENABLE ();
43
- SPIx_MOSI_CLK_ENABLE ();
45
+ SPI2_SCK_CLK_ENABLE ();
46
+ SPI2_MISO_CLK_ENABLE ();
47
+ SPI2_MOSI_CLK_ENABLE ();
44
48
45
- /* Enable SPI clock */
46
- SPIx_CLK_ENABLE ();
49
+ /* Enable SPI2 clock */
50
+ SPI2_CLK_ENABLE ();
47
51
48
- /* Configure SPI SCK pin(PB10) as alternate function */
49
- GPIO_InitStruct.Pin = SPIx_SCK_PIN ;
52
+ /* Configure SPI2 SCK PIN defined in SPI.h */
53
+ GPIO_InitStruct.Pin = SPI2_SCK_PIN ;
50
54
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
51
55
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
52
56
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
53
- GPIO_InitStruct.Alternate = SPIx_SCK_AF;
54
- HAL_GPIO_Init (SPIx_SCK_GPIO_PORT, &GPIO_InitStruct);
55
-
56
- /* Configure SPI MISO pin(PB14) as alternate function */
57
- GPIO_InitStruct.Pin = SPIx_MISO_PIN;
58
- GPIO_InitStruct.Alternate = SPIx_MISO_AF;
59
- HAL_GPIO_Init (SPIx_MISO_GPIO_PORT, &GPIO_InitStruct);
60
-
61
- /* Configure SPI MOSI pin(PB15) as alternate function */
62
- GPIO_InitStruct.Pin = SPIx_MOSI_PIN;
63
- GPIO_InitStruct.Alternate = SPIx_MOSI_AF;
64
- HAL_GPIO_Init (SPIx_MOSI_GPIO_PORT, &GPIO_InitStruct);
65
-
66
- /* Put NSS pin as Output pin in order to be used as normal GPIO in Master mode */
67
- GPIO_InitStruct.Pin = SPIx_NSS_PIN;
68
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
69
- GPIO_InitStruct.Pull = GPIO_NOPULL;
70
- HAL_GPIO_Init (SPIx_NSS_GPIO_PORT, &GPIO_InitStruct);
57
+ GPIO_InitStruct.Alternate = SPI2_SCK_AF;
58
+ HAL_GPIO_Init (SPI2_SCK_GPIO_PORT, &GPIO_InitStruct);
59
+
60
+ /* Configure SPI2 MISO PIN defined in SPI.h */
61
+ GPIO_InitStruct.Pin = SPI2_MISO_PIN;
62
+ GPIO_InitStruct.Alternate = SPI2_MISO_AF;
63
+ HAL_GPIO_Init (SPI2_MISO_GPIO_PORT, &GPIO_InitStruct);
64
+
65
+ /* Configure SPI2 MOSI PIN defined in SPI.h */
66
+ GPIO_InitStruct.Pin = SPI2_MOSI_PIN;
67
+ GPIO_InitStruct.Alternate = SPI2_MOSI_AF;
68
+ HAL_GPIO_Init (SPI2_MOSI_GPIO_PORT, &GPIO_InitStruct);
71
69
}
70
+ else if (hSPIx.Instance == HAL_SPI1)
71
+ {
72
+ GPIO_InitTypeDef GPIO_InitStruct;
73
+
74
+ /* Enable GPIO clock */
75
+ SPI1_SCK_CLK_ENABLE ();
76
+ SPI1_MISO_CLK_ENABLE ();
77
+ SPI1_MOSI_CLK_ENABLE ();
78
+ SPI1_NSS_CLK_ENABLE ();
79
+
80
+ /* Enable SPI1 clock */
81
+ SPI1_CLK_ENABLE ();
72
82
73
- /* SPIx configuration ----------------------------------------------------*/
74
- hSPIx.Instance = SPIx;
83
+ /* Configure SPI1 SCK PIN defined in SPI.h */
84
+ GPIO_InitStruct.Pin = SPI1_SCK_PIN;
85
+ GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
86
+ GPIO_InitStruct.Pull = GPIO_PULLDOWN;
87
+ GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
88
+ GPIO_InitStruct.Alternate = SPI1_SCK_AF;
89
+ HAL_GPIO_Init (SPI1_SCK_GPIO_PORT, &GPIO_InitStruct);
90
+
91
+ /* Configure SPI1 MISO PIN defined in SPI.h */
92
+ GPIO_InitStruct.Pin = SPI1_MISO_PIN;
93
+ GPIO_InitStruct.Alternate = SPI1_MISO_AF;
94
+ HAL_GPIO_Init (SPI1_MISO_GPIO_PORT, &GPIO_InitStruct);
95
+
96
+ /* Configure SPI1 MOSI PIN defined in SPI.h */
97
+ GPIO_InitStruct.Pin = SPI1_MOSI_PIN;
98
+ GPIO_InitStruct.Alternate = SPI1_MOSI_AF;
99
+ HAL_GPIO_Init (SPI1_MOSI_GPIO_PORT, &GPIO_InitStruct);
100
+
101
+ /* Put SPI1 NSS pin as Output pin in order to be used as normal GPIO in Master mode */
102
+ GPIO_InitStruct.Pin = SPI1_NSS_PIN;
103
+ GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
104
+ GPIO_InitStruct.Pull = GPIO_NOPULL;
105
+ HAL_GPIO_Init (SPI1_NSS_GPIO_PORT, &GPIO_InitStruct);
106
+ }
107
+
108
+ /* SPI general configuration */
75
109
hSPIx.Init .BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
76
110
hSPIx.Init .Direction = SPI_DIRECTION_2LINES;
77
111
hSPIx.Init .CLKPhase = SPI_PHASE_1EDGE;
@@ -94,30 +128,9 @@ void SPIClass::begin()
94
128
*/
95
129
void SPIClass::begin (uint8_t slaveSelectPin)
96
130
{
97
- /* SPIx configuration */
131
+ /* SPIx configuration */
98
132
begin ();
99
-
100
- /* TOBEFIXED: The correct way to proceed here it is to map the Arduino slaveSelectPin provided as parameter */
101
- /* with the Cube GPIO port and pin number and then call the HAL_GPIO_Init with the correct values */
102
- /* At the moment only NSS pin is supported */
103
- switch (slaveSelectPin)
104
- {
105
- case 10 :
106
- default :
107
- {
108
- GPIO_InitTypeDef GPIO_InitStruct;
109
-
110
- SPIx_NSS_CLK_ENABLE ();
111
- GPIO_InitStruct.Pin = SPIx_NSS_PIN;
112
- GPIO_InitStruct.Alternate = SPIx_NSS_AF;
113
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
114
- GPIO_InitStruct.Pull = GPIO_PULLDOWN;
115
- GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
116
-
117
- HAL_GPIO_Init (SPIx_NSS_GPIO_PORT, &GPIO_InitStruct);
118
- break ;
119
- }
120
- }
133
+ pinMode (slaveSelectPin, OUTPUT);
121
134
}
122
135
123
136
/* *
@@ -185,7 +198,13 @@ uint16_t SPIClass::transfer16(uint16_t data)
185
198
void SPIClass::transfer (void *buf, size_t count)
186
199
{
187
200
uint8_t rxdata;
188
- HAL_SPI_TransmitReceive (&hSPIx, (uint8_t *)buf, (uint8_t *)&rxdata, count,5000 );
201
+ int i;
202
+
203
+ for (i = 0 ; i < count; i++)
204
+ {
205
+ HAL_SPI_TransmitReceive (&hSPIx, ((uint8_t *)buf + i), (uint8_t *)&rxdata, 1 ,5000 );
206
+ *((uint8_t *)buf + i) = rxdata;
207
+ }
189
208
}
190
209
191
210
/* *
@@ -196,34 +215,14 @@ void SPIClass::transfer(void *buf, size_t count)
196
215
uint8_t SPIClass::transfer (uint8_t slaveSelectPin, uint8_t val, SPITransferMode transferMode)
197
216
{
198
217
uint8_t rxdata;
199
-
200
- /* TOBEFIXED: The correct way to proceed here it is to map the Arduino pin provided as parameter */
201
- /* with the Cube GPIO port and pin number and then call the HAL_GPIO_WritePin */
202
- switch (slaveSelectPin)
203
- {
204
- case 10 :
205
- default :
206
- {
207
- HAL_GPIO_WritePin (SPIx_NSS_GPIO_PORT, SPIx_NSS_PIN, GPIO_PIN_RESET);
208
- break ;
209
- }
210
- }
211
-
218
+ digitalWrite (slaveSelectPin,LOW);
212
219
HAL_SPI_TransmitReceive (&hSPIx, (uint8_t *)&val, (uint8_t *)&rxdata, 1 ,5000 );
213
220
214
221
/* If transferMode is SPI_CONTINUE we need to hold CS GPIO pin low */
215
222
/* If transferMode is SPI_LAST we need to put CS GPIO pin high */
216
223
if (transferMode == SPI_LAST)
217
224
{
218
- switch (slaveSelectPin)
219
- {
220
- case 10 :
221
- default :
222
- {
223
- HAL_GPIO_WritePin (SPIx_NSS_GPIO_PORT, SPIx_NSS_PIN, GPIO_PIN_SET);
224
- break ;
225
- }
226
- }
225
+ digitalWrite (slaveSelectPin, HIGH);
227
226
}
228
227
229
228
return rxdata;
@@ -302,19 +301,33 @@ void SPIClass::setClockDivider(uint8_t clockDiv)
302
301
*/
303
302
void SPIClass::end ()
304
303
{
305
- if (hSPIx.Instance == SPIx)
304
+ if (hSPIx.Instance == HAL_SPI2)
305
+ {
306
+ /* ##-1- Reset peripherals ##################################################*/
307
+ SPI2_FORCE_RESET ();
308
+ SPI2_RELEASE_RESET ();
309
+
310
+ /* ##-2- Disable peripherals and GPIO Clocks ################################*/
311
+ /* Configure SPI2 SCK as alternate function */
312
+ HAL_GPIO_DeInit (SPI2_SCK_GPIO_PORT, SPI2_SCK_PIN);
313
+ /* Configure SPI2 MISO as alternate function */
314
+ HAL_GPIO_DeInit (SPI2_MISO_GPIO_PORT, SPI2_MISO_PIN);
315
+ /* Configure SPI2 MOSI as alternate function */
316
+ HAL_GPIO_DeInit (SPI2_MOSI_GPIO_PORT, SPI2_MOSI_PIN);
317
+ }
318
+ else if (hSPIx.Instance == HAL_SPI1)
306
319
{
307
320
/* ##-1- Reset peripherals ##################################################*/
308
- SPIx_FORCE_RESET ();
309
- SPIx_RELEASE_RESET ();
321
+ SPI1_FORCE_RESET ();
322
+ SPI1_RELEASE_RESET ();
310
323
311
324
/* ##-2- Disable peripherals and GPIO Clocks ################################*/
312
- /* Configure SPI SCK as alternate function */
313
- HAL_GPIO_DeInit (SPIx_SCK_GPIO_PORT, SPIx_SCK_PIN );
314
- /* Configure SPI MISO as alternate function */
315
- HAL_GPIO_DeInit (SPIx_MISO_GPIO_PORT, SPIx_MISO_PIN );
316
- /* Configure SPI MOSI as alternate function */
317
- HAL_GPIO_DeInit (SPIx_MOSI_GPIO_PORT, SPIx_MOSI_PIN );
325
+ /* Configure SPI1 SCK as alternate function */
326
+ HAL_GPIO_DeInit (SPI1_SCK_GPIO_PORT, SPI1_SCK_PIN );
327
+ /* Configure SPI1 MISO as alternate function */
328
+ HAL_GPIO_DeInit (SPI1_MISO_GPIO_PORT, SPI1_MISO_PIN );
329
+ /* Configure SPI1 MOSI as alternate function */
330
+ HAL_GPIO_DeInit (SPI1_MOSI_GPIO_PORT, SPI1_MOSI_PIN );
318
331
}
319
332
320
333
HAL_SPI_DeInit (&hSPIx);
@@ -327,19 +340,9 @@ void SPIClass::end()
327
340
*/
328
341
void SPIClass::end (uint8_t slaveSelectPin)
329
342
{
330
- /* TOBEFIXED: The correct way to proceed here it is to map the Arduino slaveSelectPin provided as parameter */
331
- /* with the Cube GPIO port and pin number and then call the HAL_GPIO_DeInit with the correct values */
332
- switch (slaveSelectPin)
333
- {
334
- case 10 :
335
- default :
336
- {
337
- HAL_GPIO_DeInit (SPIx_NSS_GPIO_PORT, SPIx_NSS_PIN);
338
- break ;
339
- }
340
- }
341
-
342
- HAL_SPI_DeInit (&hSPIx);
343
+ pinMode (slaveSelectPin, INPUT);
344
+ end ();
343
345
}
344
346
345
347
SPIClass SPI = SPIClass(HAL_SPI2);
348
+ SPIClass SPI1 = SPIClass(HAL_SPI1);
0 commit comments