lFatality / stm32_micro_ros_setup

Example of how to setup micro-ROS on any STM32 microcontroller
MIT License
80 stars 13 forks source link

For STM32F103C8T6 - Step 6 is not clear #2

Closed clive0106 closed 1 year ago

clive0106 commented 1 year ago

6.) Set up the Clock Configuration for your micro controller

In step6 I have watched the video, but the clock configuration settings are different, what am I trying to set them to? Thanks Clive

lFatality commented 1 year ago

Hi there, the setup of the clock configuration is a general step in the microcontroller setup. You don't need a specific clock config to allow micro-ROS to run. But the clock setup will still affect the chip.

The clock setup influences e.g. the speed and power consumption of the chip. Next to that, the clock setup determines the frequency with which the APBs (Advanced Peripheral Bus) run. The different peripherals of your controller (e.g. UART, SPI, I2C, ...) use the different APBs as a base for their speed. If you want to find out which peripheral is connected to which APB, you can have a look at the reference manual. For the STM32F103 you can e.g. find it on page 47 of the reference manual which you can find here: https://www.st.com/en/microcontrollers-microprocessors/stm32f103c8.html#documentation

For the STM32F103C8 the SPI 2 peripheral e.g. is connected to APB 1. Lets say you've configured your clock setup so that APB 1 runs at 4 MHz. For SPI 2 you can also set a prescaler. The prescaler will divide the frequency of the APB 1 bus to provide the final speed of the SPI 2. So if you choose e.g. a prescaler of 16 for your SPI 2 bus, it will run at APB 1 speed / SPI 2 prescaler = 4 MHz / 16 = 250 Kbit / s. If you go into CubeMx and activate SPI 2, it will also display these numbers to you. The 250 Kbit / s is the frequency at which the SPI clock signal will run (that you can observe with an oscilloscope on the SCK pin when it's transmitting).

There are many peripherals connected to a single APB. So if you change the speed of the APB, the clock speed of all these peripherals will change. The prescalers allow you to set different speeds for each peripheral.

To generate the clock in general, your microcontroller uses an oscillator. They are called e.g. HSI (High Speed Internal Clock), HSE (High Speed External Clock), or LSI (Low Speed Internal Clock). The oscillators run at different speeds and have different precision (e.g. it says it runs at 8 MHz but actually runs at 7.99 MHz). The internal oscillator is built into the chip, an external oscillator needs to be connected to the chip (many development boards provide an external oscillator). If you're not sure if you have an external oscillator, you can use the internal one (but they usually have less precision).

A basic (but not necessarily optimal) clock setup for the STM32F103C8 would e.g. be: System Clock Mux: HSI AHB Prescaler: 1 Cortex System Timer: 1 APB1 Prescaler: 8 APB2 Prescaler: 8

That will give you a SYSCLK of 8 MHz and an APB 1 & APB 2 speed of 1 MHz.

clive0106 commented 1 year ago

Thank you so much for the generous and detailed reply :)

lFatality commented 1 year ago

Happy to help :)

sachinkum0009 commented 6 months ago

Hi @lFatality @clive0106, I want to use STM32F103C8 with microros. I followed the guide, everything is working fine. But I am getting an error related to ram overflow.

/opt/gcc-arm/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/bin/ld: build/stm32f103c8_test_project.elf section `.bss' will not fit in region `RAM'
/opt/gcc-arm/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/bin/ld: region `RAM' overflowed by 20408 bytes
/opt/gcc-arm/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/bin/ld: warning: build/stm32f103c8_test_project.elf has a LOAD segment with RWX permissions
collect2: error: ld returned 1 exit status
make: *** [Makefile:202: build/stm32f103c8_test_project.elf] Error 1

Can you please me with this issue? Thanks :pray: