What is the start up code in microcontroller

Thread Starter

Kittu20

Joined Oct 12, 2022
484
I have written few codes for a microcontroller, I write code in c language but I don't exactly understand what is referred to as the c startup code. Is it called before the main function or after main ?

At the beginning of my code, in the main function, I write a function that initializes all the port pins and peripherals. The main purpose of this function is to initialize the hardware.

Can you please explain what do you understand by startup code in microcontroller and is responsible for?
 

BobTPH

Joined Jun 5, 2013
9,292
The startup code for a C program does things required to make a C program run. Here are several examples:

Setting up the stack pointer and possibly other registers required for the C execution environment.

Initializing all uninitialized data to zeros.

The code is very dependent on the microcontroller architecture and on the particular C environment. Other languages will have different requirements. At the lowest level of assembler programming, there is no startup code.
 

BobaMosfet

Joined Jul 1, 2009
2,123
I have written few codes for a microcontroller, I write code in c language but I don't exactly understand what is referred to as the c startup code. Is it called before the main function or after main ?

At the beginning of my code, in the main function, I write a function that initializes all the port pins and peripherals. The main purpose of this function is to initialize the hardware.

Can you please explain what do you understand by startup code in microcontroller and is responsible for?
The MCU is not smart. It jumps to location 0x0000 - that is ALL it knows about. Your compiler on the other hand, particularly regarding the linker, sets up jump tables, and various 'frames' to organize your code and make it relatively connected wherever it sits in memory. An MCU normally has 2 sections- a bit of memory for a boot-loader, and the program memory. How you configure 'fuses' on your MCU determines the size and usage for both these sections, or whether the MCU gets a bootloader, at all. Bootloaders are normally pieces of code that have enough smarts to allow someone to update the program on the MCU. Without a bootloader, it has to be done via another method (like a programmer module during post compile time).

Stack frames are another compiler 'frame'. These are created by the compiler, and live on the stack, because they are what allows your code to shuffle variables and information between functions & procedures versus your 'main' code. This is also how variable scope is controlled to an extent for sub-routine variables.
 

nsaspook

Joined Aug 27, 2009
13,578
For XC8, you can run asm before the C runtime code is executed.

https://developerhelp.microchip.com/xwiki/bin/view/software-tools/xc8/reset/
The code to initialize these variables can be very time-consuming, particularly when there are many values to read from program memory and write to RAM. Given this potential and very significant time delay, what can you do if you have impatient hardware that is threatening to meltdown unless it can be configured?

The MPLAB® XC8 C Compiler provides a special hook to the reset vector to allow the execution of a special code called a "powerup routine". If you define a powerup routine, it will be executed first, before the runtime startup code.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
484
I believe A startup file is a piece of software code written in either assembly or C language that executes before the main() function of an embedded application. It performs various initialization steps by configuring the hardware of the microcontroller so that the user application can run.

When a microcontroller resets, it executes a reset handler and performs hardware initialization from the start-up code before executing the main code.

Some steps may include:

  • Disabling all interrupts
  • Initializing variables and RAM

Startup code and the App code both will reside in the same Program.

For XC8, you can run asm before the C runtime code is executed.

https://developerhelp.microchip.com/xwiki/bin/view/software-tools/xc8/reset/
Where is the start-up code located on 18-bit devices with XC-8 compiler ?
 

nsaspook

Joined Aug 27, 2009
13,578
I believe A startup file is a piece of software code written in either assembly or C language that executes before the main() function of an embedded application. It performs various initialization steps by configuring the hardware of the microcontroller so that the user application can run.

When a microcontroller resets, it executes a reset handler and performs hardware initialization from the start-up code before executing the main code.

Some steps may include:

  • Disabling all interrupts
  • Initializing variables and RAM

Startup code and the App code both will reside in the same Program.


Where is the start-up code located on 18-bit devices with XC-8 compiler ?
https://forum.allaboutcircuits.com/threads/what-happens-before-main-solved.181491/post-1661688
 

nsaspook

Joined Aug 27, 2009
13,578
Now you tell me!
Sorry, you never asked.

The only time I've used it (or similar methods) before was for some cryptographic routines that needed to capture the raw power-up distribution of SRAM bits for a unique hardware identifier.
https://forum.allaboutcircuits.com/threads/random-number-generator.47970/post-317181

https://www.embedded.com/basics-of-sram-puf-and-how-to-deploy-it-for-iot-security/
What is an SRAM PUF?

A PUF utilizes deep submicron variations that occur naturally during semiconductor production, and which give each transistor slightly random electric properties. These unique properties form the basis of a chip’s unique identity.

An SRAM PUF is based on the behavior of standard SRAM memory that is available in any digital chip. Every SRAM cell has its own preferred state every time the SRAM is powered, resulting from random differences in the transistor threshold voltages. Hence, when powering SRAM memories every memory will yield a unique and random pattern of 0s and 1s. These patterns are like chip fingerprints, because each one is unique to a particular SRAM and hence to a particular chip.

However, this so-called SRAM PUF response is a “noisy” fingerprint and turning it into a high-quality and secure cryptographic key requires further processing. By using so-called “Fuzzy Extractor” IP it is possible to reconstruct exactly the same cryptographic key every time and under all environmental circumstances.
 

Ian0

Joined Aug 7, 2020
10,277
Why don't you just look for the startup code file and analyze that for your particular environment? It cannot be that difficult.
I've used NXP's MCUXPresso and Renesas's E2Studio and there's reams of startup code.
When I translated one of my programs from assembler to C the startup code was bigger than my assembler program.
I looked through the startup code and couldn't make out what most of it did, and presumed that it didn't actually do anything useful as my assembler routine functioned perfectly well without it.
Both MCUXPresso and E2Studio write the initialisation values from the data into flash just above the code, so variables in assembler can be initialised with a simple three or four line copy routine from flash to RAM.
 
Top