Page 1 of 2
Internal oscilator bluepill stm32f103
Posted: Tue Sep 17, 2024 12:17 pm
by marce002
Hello all!!
Im very new to mcu like this generic stm32f103c8 so need to learn a lot but in this case maybe is is easy for you to help with this
Please, I need to make a mod to this simple program to use the internal clock (hsi oscilator) but i have not found how to be able to do it,it is now working fine in my arduino ide environment with my stlink v2 upload method.. what would you change or add to code, to use the int clock in a bluepill?
Code: Select all
HardwareSerial my_serial_name(USART3, PB10, PB11);
#define pinLED PC13
void setup() {
my_serial_name.begin(9600); // on PB11 PB10
pinMode(pinLED, OUTPUT);
}
void loop() {
digitalWrite(pinLED, HIGH); // turn the LED on (HIGH is the voltage level)
delay(2000); // wait for a second
digitalWrite(pinLED, LOW); // turn the LED off by making the voltage LOW
delay(1000);
my_serial_name.println("La verdad es que anda");
}
Re: Internal oscilator bluepill stm32f103
Posted: Tue Sep 17, 2024 12:39 pm
by GonzoG
You need to add SystemClock_Config(void) with clock setup to your sketch.
You can generate it using STM32CubeMX:
https://github.com/stm32duino/Arduino_C ... figuration
Or you can use "Generic F103C8", as all generic variants use HSI as clock source.
Re: Internal oscilator bluepill stm32f103
Posted: Tue Sep 17, 2024 1:54 pm
by marce002
Thanks gonzo
, i did mod to: (but getting error: Compilation error: 'RCC_OscInitTypeDef' was not declared in this scope , is a .H header file needed or what else need to declare?
Code: Select all
#define USE_HSI_CLOCK 1
HardwareSerial my_serial_name(USART3, PB10, PB11);
#define pinLED PC13
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
void setup() {
my_serial_name.begin(9600); // on PB11 PB10
pinMode(pinLED, OUTPUT);
}
void loop() {
digitalWrite(pinLED, HIGH); // turn the LED on (HIGH is the voltage level)
delay(2000); // wait for a second
digitalWrite(pinLED, LOW); // turn the LED off by making the voltage LOW
delay(1000);
my_serial_name.println("La verdad es que anda");
}
Re: Internal oscilator bluepill stm32f103
Posted: Tue Sep 17, 2024 2:22 pm
by marce002
please im so begginer, can you help with the entire INO ? because i was expecting to
only need to copy void SystemClock_Config(){...} from main.c generated by cubemx and paste it DIRECTLY into .ino file and add extern "C" right before void SystemClock_Config(){
but header files instead are part of the solution???
Re: Internal oscilator bluepill stm32f103
Posted: Tue Sep 17, 2024 2:37 pm
by ag123
you can try making a new variant
https://github.com/stm32duino/Arduino_C ... 28board%29
some main parts to patch are in [*]boards.txt[*] in the installed core folder
basically copy the variant files in the variant folder to different names, then patch boards.txt replicating the structure into the new names
then you can go on and edit your variant's copy of SystemClock_config()
and selecting your variant from the menu
and we are assuming that you are using "official" stm core
https://github.com/stm32duino/Arduino_Core_STM32
wiki is here
https://github.com/stm32duino/Arduino_Core_STM32/wiki
a little blurb, I tend to think that using an external crystal for oscillator would have less drift, and likely less issues with usb which needs to be precisely 48 mhz
Re: Internal oscilator bluepill stm32f103
Posted: Tue Sep 17, 2024 3:17 pm
by fpiSTM
It seems you used maple core based on the Serial definition. So the previous post are not relevant.
Re: Internal oscilator bluepill stm32f103
Posted: Tue Sep 17, 2024 3:29 pm
by marce002
Sorry Im confused...I will not be able to solve my own problem, so the people that reads this & will need an internal oscilator working with bluepill and read my post, will not have an INO working, unless you can help with my code. Im on Arduino IDE 2.3.2 and Generic STM32F103c8 , what have so far is giving: 'RCC_OscInitTypeDef' was not declared in this scope
Re: Internal oscilator bluepill stm32f103
Posted: Tue Sep 17, 2024 4:07 pm
by fpiSTM
You use this core:
https://github.com/rogerclarkmelbourne/ ... er/STM32F1
The answers above are for this one:
https://github.com/stm32duino/Arduino_Core_STM32
Unfortunately I don't know how to redefine a clock config for maple core. I guess other members will be able to answer.
Re: Internal oscilator bluepill stm32f103
Posted: Tue Sep 17, 2024 5:15 pm
by marce002
I am a beginner with the Arduino environment and I need some help figuring out how to use some .c source files in a sketch and or even .h header files. maybe i need help integrating them in my code. I tried to find the answer on the forum but I couldn't, I apologize if the solution to my problem is somewhere in the posts and I just missed it.
Currently only have my .INO code and nothing else, of course my stlink v2 and my board with internal crystal only... the definition inside my SystemClock_Config(void) is from another user who gave me a working internal clock in cubeide
Re: Internal oscilator bluepill stm32f103
Posted: Wed Sep 18, 2024 12:25 am
by ag123
start with one of these boards
https://www.st.com/en/evaluation-tools/ ... 401re.html
https://www.st.com/en/evaluation-tools/ ... 411re.html
the Nucleo's have the st-link built-in you don't need another st-link for that
Nucleos are ST originals
Adafruit has one slightly pricy but fast and has lots of flash and sram and lots of on chip peripherals
https://www.adafruit.com/product/4382
and adafruit has complete tutorials
https://learn.adafruit.com/adafruit-stm ... er-express
but that if you are based in the americas, canada and even europe etc this could be cheaper for shipping
then for the cost conscious, try one from weact
https://github.com/WeActStudio/WeActStu ... iSTM32F4x1
https://www.aliexpress.com/item/1005001456186625.html
Weact carries quite a number of different stm32 boards including the new g4 and h5 and stm32wb boards.
https://weactstudio.aliexpress.com/store/910567080
but the stm32f4x1 is recommended because it is quite likely it works with stm core directly out of the box
https://github.com/stm32duino/Arduino_Core_STM32/wiki
the stm32f4xx series also can normally be programmed with just a normal (phone) usb cable, no st-link required.
smaller stm32f103 soc normally has 20k sram and like 64k flash, it is normally enough to 'blink a led', but if you try to pack lots of things e.g. + tft lcd lilbrary + sensors + rtos, you would quickly run out of memory. And you need st-link just to program it
while the stm32f4xx family normally are shipped with more ram (e.g. 32k, 64k etc) and flash (128k, 256k etc), runs (much) faster (has fpu and 'ART' accelerator) and makes for a much painless start, you can literally install *fat* binaries e.g. including like micropython and they 'just works'
don't use the internal (HSI) oscillator, especially if you intended to use usb as well.
external crystal oscillator (HSE) is normally required to reach the advertised speeds and a good and accurate crystal is normally more stable, precise and accurate vs HSI (internal) oscillator.
to use HSI, one 'simple' way in fact is to use an empty SystemClock_Config() or simply return from there.