Skip to content

Fix HAL not properly initialized on F105x series #1310

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 1 commit into from
Mar 10, 2021

Conversation

ttimasdf
Copy link
Contributor

Summary

This PR fixes the necessary initialization procedures which should be run before user code is called.

premain() function in main.cpp is essential for the HAL library to initize properly, more specifically it called HAL_Init which setup the systick interrupt handler which is necessary for writing a hello world.....

The issue #869 has already mentioned this bug but didn't find a solution to it. After some trials and errors and comparasion between assemblies for different products, the patch here is likely solving it.

If possible, is there's any chance we talk about this a bit further because I don't know why this workaround works in fact...

Validation

Demostration:

Detail step-by-step is To-Be-Filled. The following code is a modified blinking LED use loop based manual_delay() and HAL provided delay() altogether. The original delay() would stuck if HAL_Init is not called beforehand, the same as described in #869.

/*
 * Blink
 * Turns on an LED on for one second,
 * then off for one second, repeatedly.
 */

#include <Arduino.h>

#ifndef LED_BUILTIN
  #define LED_BUILTIN PD2
#endif

uint32_t manual_delay(volatile uint32_t time) {
  volatile uint32_t factor = 0xFF, i = time;
  while (factor > 0) {
    while (i > 0)  {
      i -= 1;
    }
    factor -= 1;
  }
  return time;
}

uint32_t blink(int pin, int times) {
  int i = times;
  while (i > 0) {
    uint32_t delay = 0xFFFF * 4;
    // turn the LED on (HIGH is the voltage level)
    digitalWrite(LED_BUILTIN, HIGH);
    manual_delay(delay);
    // wait for a second
    // delay(1000);
    // turn the LED off by making the voltage LOW
    digitalWrite(LED_BUILTIN, LOW);
    manual_delay(delay);
    // wait for a second
    // delay(1000);

    i -= 1;
  }
  return i;
}

void setup()
{
  // initialize LED digital pin as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
  blink(LED_BUILTIN, 10);
  delay(1000);
}

Code formatting

  • Ensure AStyle check is passed thanks CI

Closing issues

Fixes #869

Copy link
Member

@fpistm fpistm left a comment

Choose a reason for hiding this comment

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

LGTM

@fpistm
Copy link
Member

fpistm commented Feb 25, 2021

Hi @ttimasdf
Thanks for the PR.
Could you open an issue on the official repo: https://github.com/STMicroelectronics/STM32CubeF1/issues
or in the https://github.com/STMicroelectronics/cmsis_device_f1

This way you will get feedback from expert and this will allow to correct in the original repo.

@ttimasdf
Copy link
Contributor Author

Sure. All I know was that this part of the code belongs to HAL library, didn't realize the project is also on Github

@jrahlf
Copy link

jrahlf commented Mar 1, 2021

Why would you call SystemInit before __libc_init_array ?
Makes no sense to me.

@ttimasdf
Copy link
Contributor Author

ttimasdf commented Mar 2, 2021

Why would you call SystemInit before __libc_init_array ?
Makes no sense to me.

SystemInit just clears some registers and do not rely on any static objects, so called before __libc_init_array is quite safe.

And do a search in official repository you could find that they do it de facto on most of the other boards..

@fpistm fpistm added the fix 🩹 Bug fix label Mar 9, 2021
@fpistm fpistm merged commit 37470b5 into stm32duino:master Mar 10, 2021
@ttimasdf ttimasdf deleted the fix-f105xc-startup branch March 24, 2022 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix 🩹 Bug fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[New Variant] Can Filter with STM32F105RBT6
3 participants