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

Commit 10e27e2

Browse files
Merge pull request #5 from alfran/development
- SPI rework - added new SPI1
2 parents eba1aec + ed94251 commit 10e27e2

File tree

2 files changed

+145
-130
lines changed

2 files changed

+145
-130
lines changed

cores/arduino/SPI.cpp

100755100644
Lines changed: 101 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111
* published by the Free Software Foundation.
1212
*/
1313

14+
/*
15+
* Arduino srl - www.arduino.org
16+
* 2016 Jun 9: Edited Francesco Alessi (alfran) - francesco@arduino.org
17+
*/
18+
1419
extern "C" {
1520
#include <stdlib.h>
1621
#include <string.h>
1722
#include <inttypes.h>
1823
#include "stm32f4xx_hal.h"
19-
2024
}
2125

2226
#include "SPI.h"
@@ -27,51 +31,81 @@ SPIClass::SPIClass(SPI_TypeDef *spiInstance)
2731
}
2832

2933
/**
30-
* @brief Configure SPI, configure relatives IOs and enable SPIx
34+
* @brief Configure SPI, configure relatives IOs and enable SPI2
3135
* @param None
3236
* @retval None
3337
*/
3438
void SPIClass::begin()
3539
{
36-
if(hSPIx.Instance == SPIx)
40+
if(hSPIx.Instance == HAL_SPI2)
3741
{
3842
GPIO_InitTypeDef GPIO_InitStruct;
3943

4044
/* 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();
4448

45-
/* Enable SPI clock */
46-
SPIx_CLK_ENABLE();
49+
/* Enable SPI2 clock */
50+
SPI2_CLK_ENABLE();
4751

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;
5054
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
5155
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
5256
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);
7169
}
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();
7282

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 */
75109
hSPIx.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
76110
hSPIx.Init.Direction = SPI_DIRECTION_2LINES;
77111
hSPIx.Init.CLKPhase = SPI_PHASE_1EDGE;
@@ -94,30 +128,9 @@ void SPIClass::begin()
94128
*/
95129
void SPIClass::begin(uint8_t slaveSelectPin)
96130
{
97-
/* SPIx configuration */
131+
/* SPIx configuration */
98132
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);
121134
}
122135

123136
/**
@@ -185,7 +198,13 @@ uint16_t SPIClass::transfer16(uint16_t data)
185198
void SPIClass::transfer(void *buf, size_t count)
186199
{
187200
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+
}
189208
}
190209

191210
/**
@@ -196,34 +215,14 @@ void SPIClass::transfer(void *buf, size_t count)
196215
uint8_t SPIClass::transfer(uint8_t slaveSelectPin, uint8_t val, SPITransferMode transferMode)
197216
{
198217
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);
212219
HAL_SPI_TransmitReceive(&hSPIx, (uint8_t *)&val, (uint8_t*)&rxdata, 1,5000);
213220

214221
/* If transferMode is SPI_CONTINUE we need to hold CS GPIO pin low */
215222
/* If transferMode is SPI_LAST we need to put CS GPIO pin high */
216223
if(transferMode == SPI_LAST)
217224
{
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);
227226
}
228227

229228
return rxdata;
@@ -302,19 +301,33 @@ void SPIClass::setClockDivider(uint8_t clockDiv)
302301
*/
303302
void SPIClass::end()
304303
{
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)
306319
{
307320
/*##-1- Reset peripherals ##################################################*/
308-
SPIx_FORCE_RESET();
309-
SPIx_RELEASE_RESET();
321+
SPI1_FORCE_RESET();
322+
SPI1_RELEASE_RESET();
310323

311324
/*##-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);
318331
}
319332

320333
HAL_SPI_DeInit(&hSPIx);
@@ -327,19 +340,9 @@ void SPIClass::end()
327340
*/
328341
void SPIClass::end(uint8_t slaveSelectPin)
329342
{
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();
343345
}
344346

345347
SPIClass SPI = SPIClass(HAL_SPI2);
348+
SPIClass SPI1 = SPIClass(HAL_SPI1);

cores/arduino/SPI.h

100755100644
Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,48 @@
1616

1717
#include <Arduino.h>
1818

19-
#define SPIx HAL_SPI2
20-
#define SPIx_CLK_ENABLE() __HAL_RCC_SPI2_CLK_ENABLE()
21-
#define SPIx_SCK_CLK_ENABLE() __HAL_RCC_GPIOD_CLK_ENABLE()
22-
#define SPIx_MISO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
23-
#define SPIx_MOSI_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
24-
#define SPIx_NSS_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
25-
26-
#define SPIx_FORCE_RESET() __HAL_RCC_SPI2_FORCE_RESET()
27-
#define SPIx_RELEASE_RESET() __HAL_RCC_SPI2_RELEASE_RESET()
28-
#define SPIx_NSS_CLK_DISABLE __HAL_RCC_GPIOA_CLK_DISABLE
29-
30-
/* Definition for SPIx Pins */
31-
#define SPIx_SCK_PIN GPIO_PIN_3
32-
#define SPIx_SCK_GPIO_PORT HAL_GPIOD
33-
#define SPIx_SCK_AF GPIO_AF5_SPI2
34-
#define SPIx_MISO_PIN GPIO_PIN_14
35-
#define SPIx_MISO_GPIO_PORT HAL_GPIOB
36-
#define SPIx_MISO_AF GPIO_AF5_SPI2
37-
#define SPIx_MOSI_PIN GPIO_PIN_3
38-
#define SPIx_MOSI_GPIO_PORT HAL_GPIOC
39-
#define SPIx_MOSI_AF GPIO_AF5_SPI2
40-
41-
#define SPIx_NSS_PIN GPIO_PIN_15
42-
#define SPIx_NSS_GPIO_PORT HAL_GPIOA
43-
#define SPIx_NSS_AF GPIO_AF5_SPI2
44-
19+
#define SPI2_CLK_ENABLE() __HAL_RCC_SPI2_CLK_ENABLE()
20+
#define SPI2_SCK_CLK_ENABLE() __HAL_RCC_GPIOD_CLK_ENABLE()
21+
#define SPI2_MISO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
22+
#define SPI2_MOSI_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
23+
24+
#define SPI2_FORCE_RESET() __HAL_RCC_SPI2_FORCE_RESET()
25+
#define SPI2_RELEASE_RESET() __HAL_RCC_SPI2_RELEASE_RESET()
26+
27+
/* Definition for SPI2 Pins */
28+
#define SPI2_SCK_PIN GPIO_PIN_3
29+
#define SPI2_SCK_GPIO_PORT HAL_GPIOD
30+
#define SPI2_SCK_AF GPIO_AF5_SPI2
31+
#define SPI2_MISO_PIN GPIO_PIN_14
32+
#define SPI2_MISO_GPIO_PORT HAL_GPIOB
33+
#define SPI2_MISO_AF GPIO_AF5_SPI2
34+
#define SPI2_MOSI_PIN GPIO_PIN_3
35+
#define SPI2_MOSI_GPIO_PORT HAL_GPIOC
36+
#define SPI2_MOSI_AF GPIO_AF5_SPI2
37+
38+
#define SPI1_CLK_ENABLE() __HAL_RCC_SPI1_CLK_ENABLE()
39+
#define SPI1_SCK_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
40+
#define SPI1_MISO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
41+
#define SPI1_MOSI_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
42+
#define SPI1_NSS_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
43+
44+
#define SPI1_FORCE_RESET() __HAL_RCC_SPI1_FORCE_RESET()
45+
#define SPI1_RELEASE_RESET() __HAL_RCC_SPI1_RELEASE_RESET()
46+
47+
/* Definition for SPI2 Pins */
48+
#define SPI1_SCK_PIN GPIO_PIN_3
49+
#define SPI1_SCK_GPIO_PORT HAL_GPIOB
50+
#define SPI1_SCK_AF GPIO_AF5_SPI1
51+
#define SPI1_MISO_PIN GPIO_PIN_4
52+
#define SPI1_MISO_GPIO_PORT HAL_GPIOB
53+
#define SPI1_MISO_AF GPIO_AF5_SPI1
54+
#define SPI1_MOSI_PIN GPIO_PIN_5
55+
#define SPI1_MOSI_GPIO_PORT HAL_GPIOB
56+
#define SPI1_MOSI_AF GPIO_AF5_SPI1
57+
58+
#define SPI1_NSS_PIN GPIO_PIN_15
59+
#define SPI1_NSS_GPIO_PORT HAL_GPIOA
60+
#define SPI1_NSS_AF GPIO_AF5_SPI1
4561

4662
#ifndef LSBFIRST
4763
#define LSBFIRST 0
@@ -50,9 +66,6 @@
5066
#define MSBFIRST 1
5167
#endif
5268

53-
54-
55-
5669
#define SPI_CLOCK_DIV2 SPI_BAUDRATEPRESCALER_2
5770
#define SPI_CLOCK_DIV4 SPI_BAUDRATEPRESCALER_4
5871
#define SPI_CLOCK_DIV8 SPI_BAUDRATEPRESCALER_8
@@ -72,7 +85,6 @@ enum SPITransferMode {
7285
SPI_LAST
7386
};
7487

75-
7688
class SPISettings {
7789
public:
7890
SPISettings(uint32_t clock, uint8_t bitOrder, uint8_t dataMode)
@@ -138,7 +150,6 @@ class SPISettings {
138150
friend class SPIClass;
139151
};
140152

141-
142153
class SPIClass {
143154
public:
144155
/* Constructor */
@@ -182,5 +193,6 @@ class SPIClass {
182193
};
183194

184195
extern SPIClass SPI;
185-
//extern SPISettings SPISettings(SPI_CLOCK_DIV4, MSBFIRST, SPI_MODE0);
196+
extern SPIClass SPI1;
197+
186198
#endif

0 commit comments

Comments
 (0)