Skip to content

STM32: Fix Meowbit startup and associated bugs #3320

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

Merged
merged 4 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ports/stm/boards/meowbit_v121/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void board_init(void) {
&pin_PB03,
NO_BRIGHTNESS_COMMAND,
1.0f, // brightness (ignored)
true, // auto_brightness
false, // auto_brightness
false, // single_byte_bounds
false, // data_as_commands
true, // auto_refresh
Expand Down
3 changes: 1 addition & 2 deletions ports/stm/boards/meowbit_v121/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000)

#define HSE_VALUE ((uint32_t)12000000U)
#define LSE_VALUE ((uint32_t)32000U)
#define BOARD_HAS_LOW_SPEED_CRYSTAL (1)
#define BOARD_HAS_LOW_SPEED_CRYSTAL (0)

#define BOARD_NO_VBUS_SENSE (1)
#define BOARD_VTOR_DEFER (1) //Leave VTOR relocation to bootloader
Expand Down
2 changes: 1 addition & 1 deletion ports/stm/boards/meowbit_v121/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ OPTIMIZATION_FLAGS = -Os

LD_COMMON = boards/common_default.ld
LD_FILE = boards/STM32F401xe_boot.ld
# use for internal flash
# For debugging - also comment BOOTLOADER_OFFSET and BOARD_VTOR_DEFER
# LD_FILE = boards/STM32F401xe_fs.ld
3 changes: 2 additions & 1 deletion ports/stm/common-hal/pwmio/PWMOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,14 @@ void common_hal_pwmio_pwmout_deinit(pwmio_pwmout_obj_t* self) {
HAL_TIM_PWM_Stop(&self->handle, self->channel);
}
reset_pin_number(self->tim->pin->port,self->tim->pin->number);
self->tim = NULL;

//if reserved timer has no active channels, we can disable it
if (!reserved_tim[self->tim->tim_index - 1]) {
tim_frequencies[self->tim->tim_index - 1] = 0x00;
stm_peripherals_timer_free(self->handle.Instance);
}
Comment on lines -242 to 247
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am disappointed that gcc did not show a diagnostic for this. Amazing! glad you found it.


self->tim = NULL;
}

void common_hal_pwmio_pwmout_set_duty_cycle(pwmio_pwmout_obj_t* self, uint16_t duty) {
Expand Down
17 changes: 3 additions & 14 deletions ports/stm/peripherals/stm32f4/clocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ void stm32_peripherals_clocks_init(void) {
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
bool lse_failure = false;

// Set voltage scaling in accordance with system clock speed
__HAL_RCC_PWR_CLK_ENABLE();
Expand All @@ -76,15 +75,9 @@ void stm32_peripherals_clocks_init(void) {
#endif

if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
// Failure likely means a LSE issue - attempt to swap to LSI, and set to crash
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
RCC_OscInitStruct.OscillatorType |= RCC_OSCILLATORTYPE_LSI;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
// No HSE means no USB, so just fail forever
while(1);
}
lse_failure = true;
// Clock issues are too problematic to even attempt recovery.
// If you end up here, check whether your LSE settings match your board.
while(1);
}

// Configure bus clock sources and divisors
Expand Down Expand Up @@ -113,8 +106,4 @@ void stm32_peripherals_clocks_init(void) {
#endif

HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);

if (lse_failure) {
reset_into_safe_mode(HARD_CRASH); //TODO: make safe mode category CLOCK_FAULT?
}
}
17 changes: 3 additions & 14 deletions ports/stm/peripherals/stm32f7/clocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ void stm32_peripherals_clocks_init(void) {
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
bool lse_failure = false;

// Configure LSE Drive
HAL_PWR_EnableBkUpAccess();
Expand Down Expand Up @@ -68,15 +67,9 @@ void stm32_peripherals_clocks_init(void) {
RCC_OscInitStruct.PLL.PLLQ = CPY_CLK_PLLQ;

if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
// Failure likely means a LSE issue - attempt to swap to LSI, and set to crash
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
RCC_OscInitStruct.OscillatorType |= RCC_OSCILLATORTYPE_LSI;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
// No HSE means no USB, so just fail forever
while(1);
}
lse_failure = true;
// Clock issues are too problematic to even attempt recovery.
// If you end up here, check whether your LSE settings match your board.
while(1);
}

/* Activate the OverDrive to reach the 216 MHz Frequency */
Expand Down Expand Up @@ -111,8 +104,4 @@ void stm32_peripherals_clocks_init(void) {
#endif

HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);

if (lse_failure) {
reset_into_safe_mode(HARD_CRASH); //TODO: make safe mode category CLOCK_FAULT?
}
}
17 changes: 3 additions & 14 deletions ports/stm/peripherals/stm32h7/clocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ void stm32_peripherals_clocks_init(void) {
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
bool lse_failure = false;

// Set voltage scaling in accordance with system clock speed
HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
Expand Down Expand Up @@ -73,15 +72,9 @@ void stm32_peripherals_clocks_init(void) {
RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
RCC_OscInitStruct.PLL.PLLFRACN = 0;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
// Failure likely means a LSE issue - attempt to swap to LSI, and set to crash
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
RCC_OscInitStruct.OscillatorType |= RCC_OSCILLATORTYPE_LSI;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
// No HSE means no USB, so just fail forever
while(1);
}
lse_failure = true;
// Clock issues are too problematic to even attempt recovery.
// If you end up here, check whether your LSE settings match your board.
while(1);
}

// Configure bus clock sources and divisors
Expand Down Expand Up @@ -116,8 +109,4 @@ void stm32_peripherals_clocks_init(void) {

// Enable USB Voltage detector
HAL_PWREx_EnableUSBVoltageDetector();

if (lse_failure) {
reset_into_safe_mode(HARD_CRASH); //TODO: make safe mode category CLOCK_FAULT?
}
}