Skip to content

Commit 3b92472

Browse files
majbthrdfpistm
andcommitted
feat: add the STM32WL SUBGHZSPI to the SPI library
Signed-off-by: Peter Lawrence <12226419+majbthrd@users.noreply.github.com> Co-authored-by: Frederic Pillon <frederic.pillon@st.com>
1 parent 53ad0d7 commit 3b92472

File tree

3 files changed

+98
-12
lines changed

3 files changed

+98
-12
lines changed

libraries/SPI/src/SPI.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,41 @@ void SPIClass::detachInterrupt(void)
421421
{
422422
// Should be disableInterrupt()
423423
}
424+
425+
#if defined(SUBGHZSPI_BASE)
426+
SUBGHZSPIClass SubGHZ_SPI;
427+
428+
void SUBGHZSPIClass::begin(uint8_t _pin)
429+
{
430+
if (_pin != CS_PIN_CONTROLLED_BY_USER) {
431+
LL_PWR_SelectSUBGHZSPI_NSS();
432+
}
433+
SPIClass::begin(_pin);
434+
if (_pin != CS_PIN_CONTROLLED_BY_USER) {
435+
LL_PWR_UnselectSUBGHZSPI_NSS();
436+
}
437+
}
438+
439+
byte SUBGHZSPIClass::transfer(uint8_t _pin, uint8_t _data, SPITransferMode _mode)
440+
{
441+
byte res;
442+
if (_pin != CS_PIN_CONTROLLED_BY_USER) {
443+
LL_PWR_SelectSUBGHZSPI_NSS();
444+
}
445+
res = SPIClass::transfer(_pin, _data, _mode);
446+
if (_pin != CS_PIN_CONTROLLED_BY_USER) {
447+
LL_PWR_UnselectSUBGHZSPI_NSS();
448+
}
449+
return res;
450+
}
451+
452+
void SUBGHZSPIClass::enableDebugPins(void)
453+
{
454+
spi_t* obj = getSpiObj();
455+
/* Configure SPI GPIO pins */
456+
pinmap_pinout(obj->pin_mosi, PinMap_SPI_MOSI);
457+
pinmap_pinout(obj->pin_miso, PinMap_SPI_MISO);
458+
pinmap_pinout(obj->pin_sclk, PinMap_SPI_SCLK);
459+
pinmap_pinout(obj->pin_ssel, PinMap_SPI_SSEL);
460+
}
461+
#endif

libraries/SPI/src/SPI.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ class SPIClass {
227227
void attachInterrupt(void);
228228
void detachInterrupt(void);
229229

230+
spi_t* getSpiObj(void)
231+
{
232+
return &_spi;
233+
}
234+
235+
230236
// Could be used to mix Arduino API and STM32Cube HAL API (ex: DMA). Use at your own risk.
231237
SPI_HandleTypeDef *getHandle(void)
232238
{
@@ -304,4 +310,18 @@ class SPIClass {
304310

305311
extern SPIClass SPI;
306312

313+
#if defined(SUBGHZSPI_BASE)
314+
class SUBGHZSPIClass : public SPIClass {
315+
public:
316+
SUBGHZSPIClass(): SPIClass{SUBGHZSPI_MOSI, SUBGHZSPI_MISO, SUBGHZSPI_SCLK, SUBGHZSPI_SS} {}
317+
318+
void begin(uint8_t _pin = CS_PIN_CONTROLLED_BY_USER);
319+
byte transfer(uint8_t _pin, uint8_t _data, SPITransferMode _mode = SPI_LAST);
320+
321+
void enableDebugPins(void);
322+
};
323+
324+
extern SUBGHZSPIClass SubGHZ_SPI;
307325
#endif
326+
327+
#endif /* _SPI_H_INCLUDED */

libraries/SPI/src/utility/spi_com.c

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
@@ -259,6 +265,7 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
259265
handle->Init.Mode = SPI_MODE_MASTER;
260266

261267
spi_freq = spi_getClkFreqInst(obj->spi);
268+
/* For SUBGHZSPI, 'SPI_BAUDRATEPRESCALER_*' == 'SUBGHZSPI_BAUDRATEPRESCALER_*' */
262269
if (speed >= (spi_freq / SPI_SPEED_CLOCK_DIV2_MHZ)) {
263270
handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
264271
} else if (speed >= (spi_freq / SPI_SPEED_CLOCK_DIV4_MHZ)) {
@@ -318,18 +325,23 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
318325
handle->Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_ENABLE; /* Recommended setting to avoid glitches */
319326
#endif
320327

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-
328+
#if defined(SUBGHZSPI_BASE)
329+
if (handle->Instance != SUBGHZSPI) {
330+
#endif
331+
/* Configure SPI GPIO pins */
332+
pinmap_pinout(obj->pin_mosi, PinMap_SPI_MOSI);
333+
pinmap_pinout(obj->pin_miso, PinMap_SPI_MISO);
334+
pinmap_pinout(obj->pin_sclk, PinMap_SPI_SCLK);
335+
/*
336+
* According the STM32 Datasheet for SPI peripheral we need to PULLDOWN
337+
* or PULLUP the SCK pin according the polarity used.
338+
*/
339+
pull = (handle->Init.CLKPolarity == SPI_POLARITY_LOW) ? GPIO_PULLDOWN : GPIO_PULLUP;
340+
pin_PullConfig(get_GPIO_Port(STM_PORT(obj->pin_sclk)), STM_LL_GPIO_PIN(obj->pin_sclk), pull);
341+
pinmap_pinout(obj->pin_ssel, PinMap_SPI_SSEL);
342+
#if defined(SUBGHZSPI_BASE)
343+
}
344+
#endif
333345
#if defined SPI1_BASE
334346
// Enable SPI clock
335347
if (handle->Instance == SPI1) {
@@ -379,6 +391,14 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
379391
}
380392
#endif
381393

394+
#if defined SUBGHZSPI_BASE
395+
if (handle->Instance == SUBGHZSPI) {
396+
__HAL_RCC_SUBGHZSPI_CLK_ENABLE();
397+
__HAL_RCC_SUBGHZSPI_FORCE_RESET();
398+
__HAL_RCC_SUBGHZSPI_RELEASE_RESET();
399+
}
400+
#endif
401+
382402
HAL_SPI_Init(handle);
383403

384404
/* In order to set correctly the SPI polarity we need to enable the peripheral */
@@ -448,6 +468,14 @@ void spi_deinit(spi_t *obj)
448468
__HAL_RCC_SPI6_CLK_DISABLE();
449469
}
450470
#endif
471+
472+
#if defined SUBGHZSPI_BASE
473+
if (handle->Instance == SUBGHZSPI) {
474+
__HAL_RCC_SUBGHZSPI_FORCE_RESET();
475+
__HAL_RCC_SUBGHZSPI_RELEASE_RESET();
476+
__HAL_RCC_SUBGHZSPI_CLK_DISABLE();
477+
}
478+
#endif
451479
}
452480

453481
/**

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy