@@ -159,6 +159,12 @@ uint32_t spi_getClkFreqInst(SPI_TypeDef *spi_inst)
159
159
}
160
160
}
161
161
#endif // SPI6_BASE
162
+ #if defined(SUBGHZSPI_BASE )
163
+ if (spi_inst == SUBGHZSPI ) {
164
+ /* Source CLK is APB3 (PCLK3) is derived from AHB3 clock */
165
+ spi_freq = HAL_RCC_GetHCLK3Freq ();
166
+ }
167
+ #endif // SUBGHZSPI_BASE
162
168
#endif
163
169
}
164
170
return spi_freq ;
@@ -224,41 +230,50 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
224
230
uint32_t spi_freq = 0 ;
225
231
uint32_t pull = 0 ;
226
232
227
- // Determine the SPI to use
228
- SPI_TypeDef * spi_mosi = pinmap_peripheral (obj -> pin_mosi , PinMap_SPI_MOSI );
229
- SPI_TypeDef * spi_miso = pinmap_peripheral (obj -> pin_miso , PinMap_SPI_MISO );
230
- SPI_TypeDef * spi_sclk = pinmap_peripheral (obj -> pin_sclk , PinMap_SPI_SCLK );
231
- SPI_TypeDef * spi_ssel = pinmap_peripheral (obj -> pin_ssel , PinMap_SPI_SSEL );
233
+ #if defined(SUBGHZSPI_BASE )
234
+ if (handle -> Instance != SUBGHZSPI ) {
235
+ #endif
236
+ // Determine the SPI to use
237
+ SPI_TypeDef * spi_mosi = pinmap_peripheral (obj -> pin_mosi , PinMap_SPI_MOSI );
238
+ SPI_TypeDef * spi_miso = pinmap_peripheral (obj -> pin_miso , PinMap_SPI_MISO );
239
+ SPI_TypeDef * spi_sclk = pinmap_peripheral (obj -> pin_sclk , PinMap_SPI_SCLK );
240
+ SPI_TypeDef * spi_ssel = pinmap_peripheral (obj -> pin_ssel , PinMap_SPI_SSEL );
232
241
233
- /* Pins MOSI/MISO/SCLK must not be NP. ssel can be NP. */
234
- if (spi_mosi == NP || spi_miso == NP || spi_sclk == NP ) {
235
- core_debug ("ERROR: at least one SPI pin has no peripheral\n" );
236
- return ;
237
- }
242
+ /* Pins MOSI/MISO/SCLK must not be NP. ssel can be NP. */
243
+ if (spi_mosi == NP || spi_miso == NP || spi_sclk == NP ) {
244
+ core_debug ("ERROR: at least one SPI pin has no peripheral\n" );
245
+ return ;
246
+ }
238
247
239
- SPI_TypeDef * spi_data = pinmap_merge_peripheral (spi_mosi , spi_miso );
240
- SPI_TypeDef * spi_cntl = pinmap_merge_peripheral (spi_sclk , spi_ssel );
248
+ SPI_TypeDef * spi_data = pinmap_merge_peripheral (spi_mosi , spi_miso );
249
+ SPI_TypeDef * spi_cntl = pinmap_merge_peripheral (spi_sclk , spi_ssel );
241
250
242
- obj -> spi = pinmap_merge_peripheral (spi_data , spi_cntl );
251
+ obj -> spi = pinmap_merge_peripheral (spi_data , spi_cntl );
243
252
244
- // Are all pins connected to the same SPI instance?
245
- if (spi_data == NP || spi_cntl == NP || obj -> spi == NP ) {
246
- core_debug ("ERROR: SPI pins mismatch\n" );
247
- return ;
248
- }
253
+ // Are all pins connected to the same SPI instance?
254
+ if (spi_data == NP || spi_cntl == NP || obj -> spi == NP ) {
255
+ core_debug ("ERROR: SPI pins mismatch\n" );
256
+ return ;
257
+ }
249
258
250
- // Configure the SPI pins
251
- if (obj -> pin_ssel != NC ) {
252
- handle -> Init .NSS = SPI_NSS_HARD_OUTPUT ;
259
+ // Configure the SPI pins
260
+ if (obj -> pin_ssel != NC ) {
261
+ handle -> Init .NSS = SPI_NSS_HARD_OUTPUT ;
262
+ } else {
263
+ handle -> Init .NSS = SPI_NSS_SOFT ;
264
+ }
265
+ #if defined(SUBGHZSPI_BASE )
253
266
} else {
254
267
handle -> Init .NSS = SPI_NSS_SOFT ;
255
268
}
269
+ #endif
256
270
257
271
/* Fill default value */
258
272
handle -> Instance = obj -> spi ;
259
273
handle -> Init .Mode = SPI_MODE_MASTER ;
260
274
261
275
spi_freq = spi_getClkFreqInst (obj -> spi );
276
+ /* For SUBGHZSPI, 'SPI_BAUDRATEPRESCALER_*' == 'SUBGHZSPI_BAUDRATEPRESCALER_*' */
262
277
if (speed >= (spi_freq / SPI_SPEED_CLOCK_DIV2_MHZ )) {
263
278
handle -> Init .BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2 ;
264
279
} else if (speed >= (spi_freq / SPI_SPEED_CLOCK_DIV4_MHZ )) {
@@ -318,18 +333,23 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
318
333
handle -> Init .MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_ENABLE ; /* Recommended setting to avoid glitches */
319
334
#endif
320
335
321
- /* Configure SPI GPIO pins */
322
- pinmap_pinout (obj -> pin_mosi , PinMap_SPI_MOSI );
323
- pinmap_pinout (obj -> pin_miso , PinMap_SPI_MISO );
324
- pinmap_pinout (obj -> pin_sclk , PinMap_SPI_SCLK );
325
- /*
326
- * According the STM32 Datasheet for SPI peripheral we need to PULLDOWN
327
- * or PULLUP the SCK pin according the polarity used.
328
- */
329
- pull = (handle -> Init .CLKPolarity == SPI_POLARITY_LOW ) ? GPIO_PULLDOWN : GPIO_PULLUP ;
330
- pin_PullConfig (get_GPIO_Port (STM_PORT (obj -> pin_sclk )), STM_LL_GPIO_PIN (obj -> pin_sclk ), pull );
331
- pinmap_pinout (obj -> pin_ssel , PinMap_SPI_SSEL );
332
-
336
+ #if defined(SUBGHZSPI_BASE )
337
+ if (handle -> Instance != SUBGHZSPI ) {
338
+ #endif
339
+ /* Configure SPI GPIO pins */
340
+ pinmap_pinout (obj -> pin_mosi , PinMap_SPI_MOSI );
341
+ pinmap_pinout (obj -> pin_miso , PinMap_SPI_MISO );
342
+ pinmap_pinout (obj -> pin_sclk , PinMap_SPI_SCLK );
343
+ /*
344
+ * According the STM32 Datasheet for SPI peripheral we need to PULLDOWN
345
+ * or PULLUP the SCK pin according the polarity used.
346
+ */
347
+ pull = (handle -> Init .CLKPolarity == SPI_POLARITY_LOW ) ? GPIO_PULLDOWN : GPIO_PULLUP ;
348
+ pin_PullConfig (get_GPIO_Port (STM_PORT (obj -> pin_sclk )), STM_LL_GPIO_PIN (obj -> pin_sclk ), pull );
349
+ pinmap_pinout (obj -> pin_ssel , PinMap_SPI_SSEL );
350
+ #if defined(SUBGHZSPI_BASE )
351
+ }
352
+ #endif
333
353
#if defined SPI1_BASE
334
354
// Enable SPI clock
335
355
if (handle -> Instance == SPI1 ) {
@@ -379,6 +399,14 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
379
399
}
380
400
#endif
381
401
402
+ #if defined SUBGHZSPI_BASE
403
+ if (handle -> Instance == SUBGHZSPI ) {
404
+ __HAL_RCC_SUBGHZSPI_CLK_ENABLE ();
405
+ __HAL_RCC_SUBGHZSPI_FORCE_RESET ();
406
+ __HAL_RCC_SUBGHZSPI_RELEASE_RESET ();
407
+ }
408
+ #endif
409
+
382
410
HAL_SPI_Init (handle );
383
411
384
412
/* In order to set correctly the SPI polarity we need to enable the peripheral */
@@ -448,6 +476,14 @@ void spi_deinit(spi_t *obj)
448
476
__HAL_RCC_SPI6_CLK_DISABLE ();
449
477
}
450
478
#endif
479
+
480
+ #if defined SUBGHZSPI_BASE
481
+ if (handle -> Instance == SUBGHZSPI ) {
482
+ __HAL_RCC_SUBGHZSPI_FORCE_RESET ();
483
+ __HAL_RCC_SUBGHZSPI_RELEASE_RESET ();
484
+ __HAL_RCC_SUBGHZSPI_CLK_DISABLE ();
485
+ }
486
+ #endif
451
487
}
452
488
453
489
/**
0 commit comments