I’m a newbie to this IDE and realized that I don’t know how the start-up code for the Arduino is implemented behind the scenes. I read through the provided PlatformIO guides on Arduino UNO. Do I need to directly program the fuses to configure clock source and frequency? Or do any of the code/setting I put in the platform.ini file handle all of that hardware based start-up of the board? How does the .ini file work to produce start-up code? For example, lets say that I wanted to use the onboard 16MHz clock source. How do I configure this? What if I wanted to change the clocking source to an internal 8 MHz RC oscillator, how do I change the settings to do so?
Additionally, in my .ini file, I have the following:
platform = atmelavr
board = uno
FYI, I am writing in pure C code. (not using C++ or any arduino framework libraries). Thank you in advance for everyone helping answer my questions
The microcontroller already has some current fuse settings. If those are not what you want, see documentation for fuse programming.
In such a baremetal example, the only thing that configures anything for the startup code is the
board = uno setting which gives the compiler several pieces of information
Note that all of this can be changed dynamically, per documentation.
As thsu the compiler,
avr-gcc, gets the
-D F_CPU=16000000L flags so that it can generate the correct code for it.
Note that the
F_CPU macro does not have the effect of setting the CPU speed to that, it just takes that information as assumed correct and exposes that value as a macro the assembly and C code, which can ten use it. That means that you will have to make sure that the clock frequency info you expose to PlatformIO is actually correct by checking or programming the fuses accordingly (per documentation above).
The Arduino AVR core implementation is completely open source. The entry point of the program is
main(). The compiler only adds the vector table to the firmware so that the reset vector points to a small stub of code that initializes the stack and C library and global variables, then jumps into
main(). This startup code is not explicitly contained in the project and needs no configuring.
From there in
main(), the program starts. See files for implementation.
See fuse programming docs in conjunction with this. As for the
f_cpu change documentation per above. For comfort you can also just select a
board = .. config that has 8MHz configured, e.g this.
Also note the official baremetal example.