13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
- //==============================================================================
17
- // STM32F103
18
- //==============================================================================
19
16
#include "spi_api.h"
20
17
21
18
#if DEVICE_SPI
@@ -45,16 +42,33 @@ static const PinMap PinMap_SPI_SCLK[] = {
45
42
46
43
// Only used in Slave mode
47
44
static const PinMap PinMap_SPI_SSEL [] = {
48
- {PA_4 , SPI_1 , STM_PIN_DATA (GPIO_Mode_IN_FLOATING , 0 )},
49
- {PA_15 , SPI_1 , STM_PIN_DATA (GPIO_Mode_IN_FLOATING , 1 )}, // Remap
45
+ {PB_6 , SPI_1 , STM_PIN_DATA (GPIO_Mode_IN_FLOATING , 0 )}, // Generic IO, not real H/W NSS pin
46
+ //{PA_4, SPI_1, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)},
47
+ //{PA_15, SPI_1, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 1)}, // Remap
50
48
{NC , NC , 0 }
51
49
};
52
50
53
- void spi_init (spi_t * obj , PinName mosi , PinName miso , PinName sclk , PinName ssel ) {
54
-
55
- SPI_TypeDef * spi ;
51
+ static void init_spi (spi_t * obj ) {
52
+ SPI_TypeDef * spi = (SPI_TypeDef * )(obj -> spi );
56
53
SPI_InitTypeDef SPI_InitStructure ;
57
-
54
+
55
+ SPI_Cmd (spi , DISABLE );
56
+
57
+ SPI_InitStructure .SPI_Mode = obj -> mode ;
58
+ SPI_InitStructure .SPI_NSS = obj -> nss ;
59
+ SPI_InitStructure .SPI_Direction = SPI_Direction_2Lines_FullDuplex ;
60
+ SPI_InitStructure .SPI_DataSize = obj -> bits ;
61
+ SPI_InitStructure .SPI_CPOL = obj -> cpol ;
62
+ SPI_InitStructure .SPI_CPHA = obj -> cpha ;
63
+ SPI_InitStructure .SPI_BaudRatePrescaler = obj -> br_presc ;
64
+ SPI_InitStructure .SPI_FirstBit = SPI_FirstBit_MSB ;
65
+ SPI_InitStructure .SPI_CRCPolynomial = 7 ;
66
+ SPI_Init (spi , & SPI_InitStructure );
67
+
68
+ SPI_Cmd (spi , ENABLE );
69
+ }
70
+
71
+ void spi_init (spi_t * obj , PinName mosi , PinName miso , PinName sclk , PinName ssel ) {
58
72
// Determine the SPI to use
59
73
SPIName spi_mosi = (SPIName )pinmap_peripheral (mosi , PinMap_SPI_MOSI );
60
74
SPIName spi_miso = (SPIName )pinmap_peripheral (miso , PinMap_SPI_MISO );
@@ -69,9 +83,6 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
69
83
if (obj -> spi == (SPIName )NC ) {
70
84
error ("SPI pinout mapping failed" );
71
85
}
72
-
73
- // Get SPI registers structure address
74
- spi = (SPI_TypeDef * )(obj -> spi );
75
86
76
87
// Enable SPI clock
77
88
if (obj -> spi == SPI_1 ) {
@@ -99,35 +110,19 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
99
110
else { // Slave
100
111
pinmap_pinout (ssel , PinMap_SPI_SSEL );
101
112
obj -> mode = SPI_Mode_Slave ;
102
- obj -> nss = SPI_NSS_Hard ;
113
+ obj -> nss = SPI_NSS_Soft ;
103
114
}
104
115
105
- // SPI configuration
106
- SPI_InitStructure .SPI_Mode = obj -> mode ;
107
- SPI_InitStructure .SPI_NSS = obj -> nss ;
108
- SPI_InitStructure .SPI_Direction = SPI_Direction_2Lines_FullDuplex ;
109
- SPI_InitStructure .SPI_DataSize = obj -> bits ;
110
- SPI_InitStructure .SPI_CPOL = obj -> cpol ;
111
- SPI_InitStructure .SPI_CPHA = obj -> cpha ;
112
- SPI_InitStructure .SPI_BaudRatePrescaler = obj -> br_presc ;
113
- SPI_InitStructure .SPI_FirstBit = SPI_FirstBit_MSB ;
114
- SPI_InitStructure .SPI_CRCPolynomial = 7 ;
115
- SPI_Init (spi , & SPI_InitStructure );
116
-
117
- SPI_Cmd (spi , ENABLE );
116
+ init_spi (obj );
118
117
}
119
118
120
119
void spi_free (spi_t * obj ) {
121
120
SPI_TypeDef * spi = (SPI_TypeDef * )(obj -> spi );
122
121
SPI_I2S_DeInit (spi );
123
122
}
124
123
125
- void spi_format (spi_t * obj , int bits , int mode , int slave ) {
126
- SPI_TypeDef * spi = (SPI_TypeDef * )(obj -> spi );
127
- SPI_InitTypeDef SPI_InitStructure ;
128
-
124
+ void spi_format (spi_t * obj , int bits , int mode , int slave ) {
129
125
// Save new values
130
-
131
126
if (bits == 8 ) {
132
127
obj -> bits = SPI_DataSize_8b ;
133
128
}
@@ -163,26 +158,10 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
163
158
obj -> nss = SPI_NSS_Hard ;
164
159
}
165
160
166
- SPI_Cmd (spi , DISABLE );
167
-
168
- SPI_InitStructure .SPI_Mode = obj -> mode ;
169
- SPI_InitStructure .SPI_NSS = obj -> nss ;
170
- SPI_InitStructure .SPI_Direction = SPI_Direction_2Lines_FullDuplex ;
171
- SPI_InitStructure .SPI_DataSize = obj -> bits ;
172
- SPI_InitStructure .SPI_CPOL = obj -> cpol ;
173
- SPI_InitStructure .SPI_CPHA = obj -> cpha ;
174
- SPI_InitStructure .SPI_BaudRatePrescaler = obj -> br_presc ;
175
- SPI_InitStructure .SPI_FirstBit = SPI_FirstBit_MSB ;
176
- SPI_InitStructure .SPI_CRCPolynomial = 7 ;
177
- SPI_Init (spi , & SPI_InitStructure );
178
-
179
- SPI_Cmd (spi , ENABLE );
161
+ init_spi (obj );
180
162
}
181
163
182
164
void spi_frequency (spi_t * obj , int hz ) {
183
- SPI_TypeDef * spi = (SPI_TypeDef * )(obj -> spi );
184
- SPI_InitTypeDef SPI_InitStructure ;
185
-
186
165
// Get SPI clock frequency
187
166
uint32_t PCLK = SystemCoreClock >> 1 ;
188
167
@@ -203,20 +182,7 @@ void spi_frequency(spi_t *obj, int hz) {
203
182
// Save new value
204
183
obj -> br_presc = ((baud_rate > 7 ) ? (7 << 3 ) : (baud_rate << 3 ));
205
184
206
- SPI_Cmd (spi , DISABLE );
207
-
208
- SPI_InitStructure .SPI_Mode = obj -> mode ;
209
- SPI_InitStructure .SPI_NSS = obj -> nss ;
210
- SPI_InitStructure .SPI_Direction = SPI_Direction_2Lines_FullDuplex ;
211
- SPI_InitStructure .SPI_DataSize = obj -> bits ;
212
- SPI_InitStructure .SPI_CPOL = obj -> cpol ;
213
- SPI_InitStructure .SPI_CPHA = obj -> cpha ;
214
- SPI_InitStructure .SPI_BaudRatePrescaler = obj -> br_presc ;
215
- SPI_InitStructure .SPI_FirstBit = SPI_FirstBit_MSB ;
216
- SPI_InitStructure .SPI_CRCPolynomial = 7 ;
217
- SPI_Init (spi , & SPI_InitStructure );
218
-
219
- SPI_Cmd (spi , ENABLE );
185
+ init_spi (obj );
220
186
}
221
187
222
188
static inline int ssp_readable (spi_t * obj ) {
0 commit comments