@@ -106,6 +106,71 @@ static PinName g_pin_ssel = NC;
106
106
/** @addtogroup STM32F4xx_System_Private_Functions
107
107
* @{
108
108
*/
109
+ /**
110
+ * @brief return clock freq of an SPI instance
111
+ * @param spi_inst : SPI instance
112
+ * @retval clock freq of the instance else SystemCoreClock
113
+ */
114
+ uint32_t spi_getClkFreqInst (SPI_TypeDef * spi_inst )
115
+ {
116
+ uint32_t spi_freq = SystemCoreClock ;
117
+
118
+ #ifdef STM32F0xx
119
+ /* SPIx source CLK is PCKL1 */
120
+ spi_freq = HAL_RCC_GetPCLK1Freq ();
121
+ #else
122
+ if (spi_inst != (SPI_TypeDef * )NC ) {
123
+ /* Get source clock depending on SPI instance */
124
+ switch ((uint32_t )spi_inst ) {
125
+ case (uint32_t )SPI1 :
126
+ #if defined SPI4_BASE
127
+ case (uint32_t )SPI4 :
128
+ #endif
129
+ #if defined SPI5_BASE
130
+ case (uint32_t )SPI5 :
131
+ #endif
132
+ #if defined SPI6_BASE
133
+ case (uint32_t )SPI6 :
134
+ #endif
135
+ /* SPI1, SPI4, SPI5 and SPI6. Source CLK is PCKL2 */
136
+ spi_freq = HAL_RCC_GetPCLK2Freq ();
137
+ break ;
138
+ case (uint32_t )SPI2 :
139
+ #if defined SPI3_BASE
140
+ case (uint32_t )SPI3 :
141
+ #endif
142
+ /* SPI_2 and SPI_3. Source CLK is PCKL1 */
143
+ spi_freq = HAL_RCC_GetPCLK1Freq ();
144
+ break ;
145
+ default :
146
+ printf ("CLK: SPI instance not set" );
147
+ break ;
148
+ }
149
+ }
150
+ #endif
151
+ return spi_freq ;
152
+ }
153
+
154
+ /**
155
+ * @brief return clock freq of an SPI instance
156
+ * @param obj : pointer to spi_t structure
157
+ * @retval clock freq of the instance else SystemCoreClock
158
+ */
159
+ uint32_t spi_getClkFreq (spi_t * obj )
160
+ {
161
+ uint32_t spi_inst = NC ;
162
+ uint32_t spi_freq = SystemCoreClock ;
163
+
164
+ if (obj != NULL ) {
165
+ spi_inst = pinmap_peripheral (obj -> pin_sclk , PinMap_SPI_SCLK );
166
+
167
+ if (spi_inst != NC ) {
168
+ spi_freq = spi_getClkFreqInst (spi_inst );
169
+ }
170
+ }
171
+ return spi_freq ;
172
+ }
173
+
109
174
/**
110
175
* @brief SPI initialization function
111
176
* @param obj : pointer to spi_t structure
@@ -120,6 +185,7 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
120
185
return ;
121
186
122
187
SPI_HandleTypeDef * handle = & (obj -> handle );
188
+ uint32_t spi_freq = 0 ;
123
189
124
190
// Determine the SPI to use
125
191
uint32_t spi_mosi = pinmap_peripheral (obj -> pin_mosi , PinMap_SPI_MOSI );
@@ -153,21 +219,22 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
153
219
handle -> Instance = obj -> spi ;
154
220
handle -> Init .Mode = SPI_MODE_MASTER ;
155
221
156
- if (speed >= SPI_SPEED_CLOCK_DIV2_MHZ ) {
222
+ spi_freq = spi_getClkFreqInst (obj -> spi );
223
+ if (speed >= (spi_freq /SPI_SPEED_CLOCK_DIV2_MHZ )) {
157
224
handle -> Init .BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2 ;
158
- } else if (speed >= SPI_SPEED_CLOCK_DIV4_MHZ ) {
225
+ } else if (speed >= ( spi_freq / SPI_SPEED_CLOCK_DIV4_MHZ ) ) {
159
226
handle -> Init .BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4 ;
160
- } else if (speed >= SPI_SPEED_CLOCK_DIV8_MHZ ) {
227
+ } else if (speed >= ( spi_freq / SPI_SPEED_CLOCK_DIV8_MHZ ) ) {
161
228
handle -> Init .BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8 ;
162
- } else if (speed >= SPI_SPEED_CLOCK_DIV16_MHZ ) {
229
+ } else if (speed >= ( spi_freq / SPI_SPEED_CLOCK_DIV16_MHZ ) ) {
163
230
handle -> Init .BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16 ;
164
- } else if (speed >= SPI_SPEED_CLOCK_DIV32_MHZ ) {
231
+ } else if (speed >= ( spi_freq / SPI_SPEED_CLOCK_DIV32_MHZ ) ) {
165
232
handle -> Init .BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32 ;
166
- } else if (speed >= SPI_SPEED_CLOCK_DIV64_MHZ ) {
233
+ } else if (speed >= ( spi_freq / SPI_SPEED_CLOCK_DIV64_MHZ ) ) {
167
234
handle -> Init .BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64 ;
168
- } else if (speed >= SPI_SPEED_CLOCK_DIV128_MHZ ) {
235
+ } else if (speed >= ( spi_freq / SPI_SPEED_CLOCK_DIV128_MHZ ) ) {
169
236
handle -> Init .BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128 ;
170
- } else if (speed >= SPI_SPEED_CLOCK_DIV256_MHZ ) {
237
+ } else if (speed >= ( spi_freq / SPI_SPEED_CLOCK_DIV256_MHZ ) ) {
171
238
handle -> Init .BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256 ;
172
239
} else {
173
240
handle -> Init .BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16 ;
0 commit comments