Skip to content

Commit 0f846e5

Browse files
Tobias Badertscherdpgeorge
authored andcommitted
stmhal: L4: Add support for machine.sleep on STM32L4 MCUs.
Also raise an exception for machine.freq and machine.deepsleep on this MCU, since they are not yet implemented.
1 parent 7441ba7 commit 0f846e5

1 file changed

Lines changed: 40 additions & 2 deletions

File tree

stmhal/modmachine.c

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ STATIC mp_obj_t machine_freq(mp_uint_t n_args, const mp_obj_t *args) {
190190
// set
191191
mp_int_t wanted_sysclk = mp_obj_get_int(args[0]) / 1000000;
192192

193+
#if defined(MCU_SERIES_L4)
194+
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError, "machine.freq set not supported yet"));
195+
#endif
196+
193197
// default PLL parameters that give 48MHz on PLL48CK
194198
uint32_t m = HSE_VALUE / 1000000, n = 336, p = 2, q = 7;
195199
uint32_t sysclk_source;
@@ -310,9 +314,12 @@ STATIC mp_obj_t machine_freq(mp_uint_t n_args, const mp_obj_t *args) {
310314

311315
// set PLL as system clock source if wanted
312316
if (sysclk_source == RCC_SYSCLKSOURCE_PLLCLK) {
317+
#if !defined(MICROPY_HW_FLASH_LATENCY)
318+
#define MICROPY_HW_FLASH_LATENCY FLASH_LATENCY_5
319+
#endif
313320
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
314321
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
315-
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
322+
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, MICROPY_HW_FLASH_LATENCY) != HAL_OK) {
316323
goto fail;
317324
}
318325
}
@@ -344,6 +351,35 @@ STATIC mp_obj_t machine_freq(mp_uint_t n_args, const mp_obj_t *args) {
344351
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 4, machine_freq);
345352

346353
STATIC mp_obj_t machine_sleep(void) {
354+
#if defined(MCU_SERIES_L4)
355+
356+
// Enter Stop 1 mode
357+
__HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_MSI);
358+
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
359+
360+
// reconfigure system clock after wakeup
361+
// Enable Power Control clock
362+
__HAL_RCC_PWR_CLK_ENABLE();
363+
364+
// Get the Oscillators configuration according to the internal RCC registers
365+
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
366+
HAL_RCC_GetOscConfig(&RCC_OscInitStruct);
367+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
368+
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
369+
HAL_RCC_OscConfig(&RCC_OscInitStruct);
370+
371+
// Get the Clocks configuration according to the internal RCC registers
372+
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
373+
uint32_t pFLatency = 0;
374+
HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &pFLatency);
375+
376+
// Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clock dividers
377+
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
378+
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
379+
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, pFLatency);
380+
381+
#else
382+
347383
// takes longer to wake but reduces stop current
348384
HAL_PWREx_EnableFlashPowerDown();
349385

@@ -366,14 +402,16 @@ STATIC mp_obj_t machine_sleep(void) {
366402
while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL) {
367403
}
368404

405+
#endif
406+
369407
return mp_const_none;
370408
}
371409
MP_DEFINE_CONST_FUN_OBJ_0(machine_sleep_obj, machine_sleep);
372410

373411
STATIC mp_obj_t machine_deepsleep(void) {
374412
rtc_init_finalise();
375413

376-
#if defined(MCU_SERIES_F7)
414+
#if defined(MCU_SERIES_F7) || defined(MCU_SERIES_L4)
377415
printf("machine.deepsleep not supported yet\n");
378416
#else
379417
// We need to clear the PWR wake-up-flag before entering standby, since

0 commit comments

Comments
 (0)