Repository navigation
Description
This simple sketch reveals a bug in the Cygnet:
#include <Wire.h>
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
for (int i = 0; i < 5; i++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}
Wire.begin();
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}Describe the bug
The sketch will indefinitely hang at Wire.begin(); until the USB is plugged in. If the USB is plugged in at the beginning of the sketch, the whole sketch will run without interruption.
To Reproduce
Flash the sketch above using SWD when the device is powered by battery, and the Cygnet will blink 5 times quickly, but never transition to the slower blinks.
Plug in the USB and it will continue to the slower blinks.
Expected behavior
Continuous Blinking
Desktop (please complete the following information):
- OS: Linux
- Arduino IDE version: 2.3.8
- STM32 core version: 2.12.0
- Tools menu settings if not the default: n/a
- Upload method: SWD
Board (please complete the following information):
- Name: Blues Cygnet
- Hardware Revision: 1.2
- Extra hardware used if any: n/a
Additional context
I've tested several modifications to the initialization routines, and I was able to narrow it down to the following change in the WEAK void SystemClock_Config(void) function:
From (reproducing):
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC | RCC_PERIPHCLK_USB;
PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_SYSCLK;
PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_MSI;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
Error_Handler();
}To (no repro):
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_SYSCLK;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
Error_Handler();
}When I stepped through the debugger, this is the exact place it hangs in the code:
twi.c:
#if defined(I2C3_BASE)
if (i2c == I2C3) {
#if defined(RCC_PERIPHCLK_I2C3) || defined(RCC_PERIPHCLK_I2C35)
#ifdef RCC_PERIPHCLK_I2C3
clkSrcFreq = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2C3);
#else
clkSrcFreq = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_I2C35);
#endif
if (clkSrcFreq == 0)
#endif
{
#if defined(__HAL_RCC_GET_I2C3_SOURCE)
switch (__HAL_RCC_GET_I2C3_SOURCE()) {
#ifdef RCC_I2C3CLKSOURCE_HSI
case RCC_I2C3CLKSOURCE_HSI:
clkSrcFreq = HSI_VALUE;
break;
#endif
#ifdef RCC_I2C3CLKSOURCE_SYSCLK
case RCC_I2C3CLKSOURCE_SYSCLK:
clkSrcFreq = SystemCoreClock;
break;
#endif
#if defined(RCC_I2C3CLKSOURCE_PCLK1) || defined(RCC_I2C3CLKSOURCE_D2PCLK1)
#ifdef RCC_I2C3CLKSOURCE_PCLK1
case RCC_I2C3CLKSOURCE_PCLK1:
#endif
#ifdef RCC_I2C3CLKSOURCE_D2PCLK1
case RCC_I2C3CLKSOURCE_D2PCLK1:
#endif
clkSrcFreq = HAL_RCC_GetPCLK1Freq();
break;
#endif
#ifdef RCC_I2C3CLKSOURCE_CSI
case RCC_I2C3CLKSOURCE_CSI:
clkSrcFreq = CSI_VALUE;
break;
#endif
#ifdef RCC_I2C3CLKSOURCE_PLL3
case RCC_I2C3CLKSOURCE_PLL3:
HAL_RCCEx_GetPLL3ClockFreq(&PLL3_Clocks);
clkSrcFreq = PLL3_Clocks.PLL3_R_Frequency;
break;
#endif
default:
Error_Handler();
}
#else
/* STM32 G0 I2C3 has no independent clock */
clkSrcFreq = HAL_RCC_GetPCLK1Freq();
#endif
}
}
#endif // I2C3_BASEI don't understand the relationship between the USB and the I2C peripherals.
Activity
fpistm commentedon Mar 13, 2026
Hi @zfields
Quickl look at the clock config, it seems something is not correct:
Arduino_Core_STM32/variants/STM32L4xx/L433C(B-C)(T-U)_L443CC(T-U)/variant_CYGNET.cpp
Line 152 in 734f525
STM32L433CC has no SDMMC.
Else I saw nothing which can explain this as you simplu set the USB clock source. Is USB enabled in the menu?
fpistm commentedon Mar 13, 2026
Going deeper, it seems even if STM32L433CC have no SDMM1C, I wonder why there is no build error.
In fact even if no SDMM1C it is defined in the CMSIS device header and so available in the
RCC_PeriphCLKInitTypeDef#if defined(SDMMC1)
uint32_t Sdmmc1ClockSelection; /*!< Specifies SDMMC1 clock source (warning: same source for USB and RNG).
This parameter can be a value of @ref RCCEx_SDMMC1_Clock_Source */
#endif /* SDMMC1 */
But in that case it is initialised by the
HAL_RCCEx_PeriphCLKConfigwhich probably doing weird things.zfields commentedon Mar 13, 2026
Yes. I have tried it both enabled and disabled. It reproduces only when USB is enabled.
Yes, I noticed the SDMMC1 mistake and removed it during one of my iterations. It made no difference.
Here are the functions I was testing when I provided the bug report...
zfields commentedon Mar 13, 2026
I tested the Nucleo L432KC, and referenced its clock configuration; it appears to map well to the Cygnet.
Based on the Nucleo config, here is a working config for the Cygnet.
zfields commentedon Mar 13, 2026
AI Summary
What changed
───
Why the old config failed
The root cause is two problems that compound each other.
Problem 1 — live USB clock on battery power.
When PeriphCLKConfig ran with RCC_USBCLKSOURCE_MSI, it wrote CLK48SEL while MSI was already running at 48 MHz. The USB peripheral immediately received a valid clock and started its hardware state machine. On battery power with no VBUS, this put the USB peripheral into an error state that corrupted subsequent initialization.
Problem 2 — SYSCLK=MSI makes HAL_GetTick() unreliable during EnableMSIPLLMode.
When EnableMSIPLLMode() was called, it set MSIPLLEN, causing MSI to briefly deassert MSIRDY while re-locking to LSE. Because SYSCLK was also MSI, SysTick stalled during the transient. Any HAL timeout loop running during or just after that window could deadlock. The watchdog eventually fired, and after a reset or two the I2C peripheral was left in a corrupted state that caused Wire.begin() to hang permanently.
───
Why the new config works
Fix 1 — PLLSAI1 for USB.
RCCEx_PLLSAI1_Config (called inside PeriphCLKConfig) writes CLK48SEL to point at PLLSAI1 before enabling it. During that brief window the USB peripheral has no clock and stays quiescent. There is no race with VBUS state.
Fix 2 — SYSCLK=PLL decouples SysTick from MSI.
The main PLL (80 MHz) provides SYSCLK. SysTick and HAL_GetTick() are completely isolated from any MSI transient. When EnableMSIPLLMode() sets MSIPLLEN after PeriphCLKConfig, the MSIRDY re-lock transient is harmless — SysTick never stalls, no timeout loop can deadlock.
Fix 3 — ordering protects the PLLSAI1 MSIRDY guard.
RCCEx_PLLSAI1_Config explicitly checks if(MSIRDY == 0) return HAL_ERROR before enabling PLLSAI1. Calling EnableMSIPLLMode() after PeriphCLKConfig ensures PLLSAI1 is fully configured before the MSIRDY transient can occur.
zfields commentedon Mar 13, 2026
The AI summary was generated after several iterations of testing with an AI side-kick; I cannot speak to it's accuracy.
@fpistm, I am happy to test any curiosities you may have, in case you suspect a different root cause than the machine has offered.
fpistm commentedon Mar 16, 2026
See nothing related in the errata sheet.
Maybe something linked to VddUSB.
Looking at the reference manual, found this but MSI seems usable as direct source.
Guess point 1 is valid but not an expert.
fix: Cygnet initialization
fix: Cygnet initialization
fpistm commentedon Mar 27, 2026
Hi @zfields
any update on this?
fpistm commentedon Mar 30, 2026
Seems well explained by https://discuss.blues.com/t/wire-begin-causes-mcu-reset-hang-on-cygnet/3486/3
@zfields I guess you will provide a PR to fix clock config.
zfields commentedon Mar 30, 2026
Yes, I have a fix, and I'm doing my final testing. Should be done by end-of-day today.
fix: Cygnet initialization