@@ -159,6 +159,12 @@ uint32_t spi_getClkFreqInst(SPI_TypeDef *spi_inst)
159159 }
160160 }
161161#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
162168#endif
163169 }
164170 return spi_freq ;
@@ -175,7 +181,14 @@ uint32_t spi_getClkFreq(spi_t *obj)
175181 uint32_t spi_freq = SystemCoreClock ;
176182
177183 if (obj != NULL ) {
178- spi_inst = pinmap_peripheral (obj -> pin_sclk , PinMap_SPI_SCLK );
184+ #if defined(SUBGHZSPI_BASE )
185+ if (obj -> handle .Instance == SUBGHZSPI ) {
186+ spi_inst = SUBGHZSPI ;
187+ } else
188+ #endif
189+ {
190+ spi_inst = pinmap_peripheral (obj -> pin_sclk , PinMap_SPI_SCLK );
191+ }
179192
180193 if (spi_inst != NP ) {
181194 spi_freq = spi_getClkFreqInst (spi_inst );
@@ -224,28 +237,39 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
224237 uint32_t spi_freq = 0 ;
225238 uint32_t pull = 0 ;
226239
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 );
240+ #if defined(SUBGHZSPI_BASE )
241+ if (obj -> spi != SUBGHZSPI ) {
242+ #endif
243+ // Determine the SPI to use
244+ SPI_TypeDef * spi_mosi = pinmap_peripheral (obj -> pin_mosi , PinMap_SPI_MOSI );
245+ SPI_TypeDef * spi_miso = pinmap_peripheral (obj -> pin_miso , PinMap_SPI_MISO );
246+ SPI_TypeDef * spi_sclk = pinmap_peripheral (obj -> pin_sclk , PinMap_SPI_SCLK );
247+ SPI_TypeDef * spi_ssel = pinmap_peripheral (obj -> pin_ssel , PinMap_SPI_SSEL );
232248
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- }
249+ /* Pins MOSI/MISO/SCLK must not be NP. ssel can be NP. */
250+ if (spi_mosi == NP || spi_miso == NP || spi_sclk == NP ) {
251+ core_debug ("ERROR: at least one SPI pin has no peripheral\n" );
252+ return ;
253+ }
238254
239- SPI_TypeDef * spi_data = pinmap_merge_peripheral (spi_mosi , spi_miso );
240- SPI_TypeDef * spi_cntl = pinmap_merge_peripheral (spi_sclk , spi_ssel );
255+ SPI_TypeDef * spi_data = pinmap_merge_peripheral (spi_mosi , spi_miso );
256+ SPI_TypeDef * spi_cntl = pinmap_merge_peripheral (spi_sclk , spi_ssel );
241257
242- obj -> spi = pinmap_merge_peripheral (spi_data , spi_cntl );
258+ obj -> spi = pinmap_merge_peripheral (spi_data , spi_cntl );
243259
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 ;
260+ // Are all pins connected to the same SPI instance?
261+ if (spi_data == NP || spi_cntl == NP || obj -> spi == NP ) {
262+ core_debug ("ERROR: SPI pins mismatch\n" );
263+ return ;
264+ }
265+ #if defined(SUBGHZSPI_BASE )
266+ } else {
267+ if (obj -> pin_mosi != NC || obj -> pin_miso != NC || obj -> pin_sclk != NC || obj -> pin_ssel != NC ) {
268+ core_debug ("ERROR: SUBGHZ_SPI cannot define custom pins\n" );
269+ return ;
270+ }
248271 }
272+ #endif
249273
250274 // Configure the SPI pins
251275 if (obj -> pin_ssel != NC ) {
@@ -259,6 +283,7 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
259283 handle -> Init .Mode = SPI_MODE_MASTER ;
260284
261285 spi_freq = spi_getClkFreqInst (obj -> spi );
286+ /* For SUBGHZSPI, 'SPI_BAUDRATEPRESCALER_*' == 'SUBGHZSPI_BAUDRATEPRESCALER_*' */
262287 if (speed >= (spi_freq / SPI_SPEED_CLOCK_DIV2_MHZ )) {
263288 handle -> Init .BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2 ;
264289 } else if (speed >= (spi_freq / SPI_SPEED_CLOCK_DIV4_MHZ )) {
@@ -318,18 +343,23 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
318343 handle -> Init .MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_ENABLE ; /* Recommended setting to avoid glitches */
319344#endif
320345
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-
346+ #if defined(SUBGHZSPI_BASE )
347+ if (handle -> Instance != SUBGHZSPI ) {
348+ #endif
349+ /* Configure SPI GPIO pins */
350+ pinmap_pinout (obj -> pin_mosi , PinMap_SPI_MOSI );
351+ pinmap_pinout (obj -> pin_miso , PinMap_SPI_MISO );
352+ pinmap_pinout (obj -> pin_sclk , PinMap_SPI_SCLK );
353+ /*
354+ * According the STM32 Datasheet for SPI peripheral we need to PULLDOWN
355+ * or PULLUP the SCK pin according the polarity used.
356+ */
357+ pull = (handle -> Init .CLKPolarity == SPI_POLARITY_LOW ) ? GPIO_PULLDOWN : GPIO_PULLUP ;
358+ pin_PullConfig (get_GPIO_Port (STM_PORT (obj -> pin_sclk )), STM_LL_GPIO_PIN (obj -> pin_sclk ), pull );
359+ pinmap_pinout (obj -> pin_ssel , PinMap_SPI_SSEL );
360+ #if defined(SUBGHZSPI_BASE )
361+ }
362+ #endif
333363#if defined SPI1_BASE
334364 // Enable SPI clock
335365 if (handle -> Instance == SPI1 ) {
@@ -379,6 +409,14 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
379409 }
380410#endif
381411
412+ #if defined SUBGHZSPI_BASE
413+ if (handle -> Instance == SUBGHZSPI ) {
414+ __HAL_RCC_SUBGHZSPI_CLK_ENABLE ();
415+ __HAL_RCC_SUBGHZSPI_FORCE_RESET ();
416+ __HAL_RCC_SUBGHZSPI_RELEASE_RESET ();
417+ }
418+ #endif
419+
382420 HAL_SPI_Init (handle );
383421
384422 /* In order to set correctly the SPI polarity we need to enable the peripheral */
@@ -448,6 +486,14 @@ void spi_deinit(spi_t *obj)
448486 __HAL_RCC_SPI6_CLK_DISABLE ();
449487 }
450488#endif
489+
490+ #if defined SUBGHZSPI_BASE
491+ if (handle -> Instance == SUBGHZSPI ) {
492+ __HAL_RCC_SUBGHZSPI_FORCE_RESET ();
493+ __HAL_RCC_SUBGHZSPI_RELEASE_RESET ();
494+ __HAL_RCC_SUBGHZSPI_CLK_DISABLE ();
495+ }
496+ #endif
451497}
452498
453499/**
0 commit comments