-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add Seeed XIAO STM32C5 support #3048
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
def9340
dc3313b
da8a904
6d7eeb2
af81304
f326cf1
58dbf25
89bbefa
e1884f2
178420b
73e186f
1211767
69d48fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,9 +13,9 @@ | |
|
|
||
| SPIClass SPI; | ||
|
|
||
| void SPIClass::configSpi(const SPISettings &settings) | ||
| void SPIClass::configSpi(const SPISettings &settings, bool force) | ||
| { | ||
| if (_spiSettings != settings) { | ||
| if (force || _spiSettings != settings) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you file an issue for this, please? |
||
| _spiSettings = settings; | ||
|
|
||
| uint32_t clock = settings.getClockFreq(); | ||
|
|
@@ -79,7 +79,7 @@ void SPIClass::begin(SPIBusMode busMode) | |
| SPI_MODE0, | ||
| busMode | ||
| ); | ||
| configSpi(defaultSettings); | ||
| configSpi(defaultSettings, true); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -20,6 +20,17 @@ | |||
| extern "C" { | ||||
| #endif | ||||
|
|
||||
| #if defined(USE_HALV2_DRIVER) | ||||
| /* HAL v2 polling APIs use HAL_GetTick() for their timeout. On the current | ||||
| * Arduino C5 clock path that tick is not guaranteed to advance while polling, | ||||
| * so use the Arduino microsecond clock to bound LL polling instead. */ | ||||
| static bool spi_transfer_timed_out(uint32_t start_us) | ||||
| { | ||||
| return (SPI_TRANSFER_TIMEOUT != HAL_MAX_DELAY) | ||||
| && ((uint32_t)(micros() - start_us) >= (SPI_TRANSFER_TIMEOUT * 1000UL)); | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. micros() uses HAL_GetTick() so wonder if your statement is correct?
|
||||
| } | ||||
| #endif | ||||
|
|
||||
| /* Private Functions */ | ||||
| /** | ||||
| * @brief return clock freq of an SPI instance | ||||
|
|
@@ -564,13 +575,22 @@ spi_status_e spi_transfer(spi_t *obj, const uint8_t *tx_buffer, uint8_t *rx_buff | |||
| { | ||||
| spi_status_e ret = SPI_OK; | ||||
| uint32_t tickstart, size = len; | ||||
| #if defined(USE_HALV2_DRIVER) | ||||
| uint32_t start_us; | ||||
| #endif | ||||
| if (obj == NULL || obj->spi == NP) { | ||||
| return SPI_ERROR; | ||||
| } | ||||
| SPI_TypeDef *_SPI = obj->spi; | ||||
| uint8_t *tx_buf = (uint8_t *)tx_buffer; | ||||
|
|
||||
| if (len == 0) { | ||||
| ret = SPI_ERROR; | ||||
| } else { | ||||
| tickstart = HAL_GetTick(); | ||||
| #if defined(USE_HALV2_DRIVER) | ||||
| start_us = micros(); | ||||
| #endif | ||||
|
|
||||
| #if defined(SPI_CR2_TSIZE) | ||||
| /* Start transfer */ | ||||
|
|
@@ -581,14 +601,32 @@ spi_status_e spi_transfer(spi_t *obj, const uint8_t *tx_buffer, uint8_t *rx_buff | |||
|
|
||||
| while (size--) { | ||||
| #if defined(SPI_SR_TXP) | ||||
| #if defined(USE_HALV2_DRIVER) | ||||
| while (!LL_SPI_IsActiveFlag_TXP(_SPI)) { | ||||
| if (spi_transfer_timed_out(start_us)) { | ||||
| ret = SPI_TIMEOUT; | ||||
| goto spi_transfer_end; | ||||
| } | ||||
| } | ||||
| #else | ||||
| while (!LL_SPI_IsActiveFlag_TXP(_SPI)); | ||||
| #endif | ||||
|
Comment on lines
+601
to
+610
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you think it is required why not add it for all ? and replace direct HAL_GetTick usage. |
||||
| #else | ||||
| while (!LL_SPI_IsActiveFlag_TXE(_SPI)); | ||||
| #endif | ||||
| LL_SPI_TransmitData8(_SPI, tx_buf ? *tx_buf++ : 0XFF); | ||||
|
|
||||
| #if defined(SPI_SR_RXP) | ||||
| #if defined(USE_HALV2_DRIVER) | ||||
| while (!LL_SPI_IsActiveFlag_RXP(_SPI)) { | ||||
| if (spi_transfer_timed_out(start_us)) { | ||||
| ret = SPI_TIMEOUT; | ||||
| goto spi_transfer_end; | ||||
| } | ||||
| } | ||||
| #else | ||||
| while (!LL_SPI_IsActiveFlag_RXP(_SPI)); | ||||
|
Comment on lines
+617
to
625
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||||
| #endif | ||||
| #else | ||||
| while (!LL_SPI_IsActiveFlag_RXNE(_SPI)); | ||||
| #endif | ||||
|
|
@@ -598,17 +636,24 @@ spi_status_e spi_transfer(spi_t *obj, const uint8_t *tx_buffer, uint8_t *rx_buff | |||
| LL_SPI_ReceiveData8(_SPI); | ||||
| } | ||||
| if ((SPI_TRANSFER_TIMEOUT != HAL_MAX_DELAY) && | ||||
| #if defined(USE_HALV2_DRIVER) | ||||
| (spi_transfer_timed_out(start_us))) { | ||||
| #else | ||||
| (HAL_GetTick() - tickstart >= SPI_TRANSFER_TIMEOUT)) { | ||||
| #endif | ||||
| ret = SPI_TIMEOUT; | ||||
| break; | ||||
| } | ||||
| } | ||||
|
|
||||
| #if defined(SPI_IFCR_EOTC) | ||||
| spi_transfer_end: | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there is a trap here. For future HALV2 series based. If SPI_IFCR_EOTC is not defined goto will not reach it. |
||||
| // Add a delay before disabling SPI otherwise last-bit/last-clock may be truncated | ||||
| // See https://github.com/stm32duino/Arduino_Core_STM32/issues/1294 | ||||
| // Computed delay is half SPI clock | ||||
| delayMicroseconds(obj->disable_delay); | ||||
| if (ret == SPI_OK) { | ||||
| delayMicroseconds(obj->disable_delay); | ||||
| } | ||||
|
|
||||
| /* Close transfer */ | ||||
| /* Clear flags */ | ||||
|
|
||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -30,7 +30,17 @@ extern "C" { | |||||||||||||||||||||||||||||||||||||||
| #include "stm32_def.h" | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| #if defined(USE_HALV2_DRIVER) | ||||||||||||||||||||||||||||||||||||||||
| #error "USB library is not yet compatible with HALv2 driver." | ||||||||||||||||||||||||||||||||||||||||
| #if !defined(STM32C5xx) | ||||||||||||||||||||||||||||||||||||||||
| #error "USB library is not yet compatible with this HALv2 driver." | ||||||||||||||||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| /* STM32C5 uses the HAL v2 USB DRD FS PCD. Keep the legacy USB name and | ||||||||||||||||||||||||||||||||||||||||
| * PMA buffer-kind names available to the common endpoint configuration. */ | ||||||||||||||||||||||||||||||||||||||||
| #define USB USB_DRD_FS | ||||||||||||||||||||||||||||||||||||||||
| #define PCD_SNG_BUF HAL_PCD_SNG_BUF | ||||||||||||||||||||||||||||||||||||||||
| #define PCD_DBL_BUF HAL_PCD_DBL_BUF | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+39
to
+41
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better to add it in the stm32_def.h, other series also redefine some USB definition. Arduino_Core_STM32/libraries/SrcWrapper/inc/stm32_def.h Lines 206 to 224 in 36a8f85
USB_BASE should also be defined. |
||||||||||||||||||||||||||||||||||||||||
| #define USB_IRQn USB_DRD_FS_IRQn | ||||||||||||||||||||||||||||||||||||||||
| #define USB_IRQHandler USB_DRD_FS_IRQHandler | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+42
to
+43
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When USB_BASE defined simply add STM32C5xx in the list:
|
||||||||||||||||||||||||||||||||||||||||
| #else | ||||||||||||||||||||||||||||||||||||||||
| #if !defined(USB_BASE) && !defined(USB_OTG_DEVICE_BASE) | ||||||||||||||||||||||||||||||||||||||||
| #error "This board does not support USB! Select 'None' in the 'Tools->USB interface' menu" | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,6 +89,12 @@ USBD_CDC_LineCodingTypeDef linecoding = { | |
| 0x08 /* nb. of bits 8*/ | ||
| }; | ||
|
|
||
| /* Boards that need a CDC line-coding side effect can override this hook. */ | ||
| WEAK void CDC_LineCodingChanged(uint32_t bitrate) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. Seems new nice feature. |
||
| { | ||
| (void)bitrate; | ||
| } | ||
|
|
||
| /* Private functions ---------------------------------------------------------*/ | ||
|
|
||
| /** | ||
|
|
@@ -175,6 +181,7 @@ static int8_t USBD_CDC_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length) | |
| linecoding.format = pbuf[4]; | ||
| linecoding.paritytype = pbuf[5]; | ||
| linecoding.datatype = pbuf[6]; | ||
| CDC_LineCodingChanged(linecoding.bitrate); | ||
| break; | ||
|
|
||
| case CDC_GET_LINE_CODING: | ||
|
|
@@ -406,4 +413,3 @@ uint8_t CDC_getDataBits(void) | |
| #endif /* USBD_USE_CDC */ | ||
| #endif /* USBCON */ | ||
| /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be better to add a more generic menu name, like Seeed Studio or something like that and select the board in the pnum menu (default if first or only one). I guess that some script will fail if no pnum item.
Moreover you add only one menu when you select it with only one option for the USB.
No Serial support?
What about optimization? debug information? C Runtime Library?