The sequence of booting between the bootloader and the user firmware on a Cortex-M3 microcontroller typically follows these steps:
- The microcontroller is powered on or reset.
- The bootloader code is loaded into memory and executed.
- The bootloader checks for a valid user firmware image in
non-volatile memory (e.g. flash memory).
- If a valid user firmware image is found, the bootloader copies it
from non-volatile memory into RAM and executes it.
- If a valid user firmware image is not found, the bootloader waits
for a new firmware image to be uploaded via a communication
interface (e.g. UART, USB, Ethernet).
Here is an example of bootloader code in C that performs these steps:
#include <stdint.h>
#include "memory.h" // Memory mapping and manipulation functions
#include "flash.h" // Flash memory functions
#include "uart.h" // UART communication functions
#define FIRMWARE_ADDRESS 0x08000000 // Address of firmware in flash memory
#define FIRMWARE_SIZE 0x4000 // Size of firmware in bytes
int main(void)
{
uint8_t buffer[FIRMWARE_SIZE]; // Buffer for storing firmware image
int i;
// Check if valid firmware image is present in flash memory
if (flash_verify(FIRMWARE_ADDRESS, FIRMWARE_SIZE, buffer))
{
// Copy firmware image from flash memory to RAM
for (i = 0; i < FIRMWARE_SIZE; i++)
{
*((uint8_t*)(FIRMWARE_ADDRESS + i)) = buffer[i];
}
// Jump to firmware
((void(*)(void))FIRMWARE_ADDRESS)();
}
else
{
// Wait for firmware image to be uploaded via UART
uart_init();
uart_puts("Waiting for firmware image...\n");
while (1)
{
if (uart_gets(buffer, FIRMWARE_SIZE))
{
// Erase flash memory
flash_erase(FIRMWARE_ADDRESS, FIRMWARE_SIZE);
// Write firmware image to flash memory
flash_write(FIRMWARE_ADDRESS, FIRMWARE_SIZE, buffer);
// Jump to firmware
((void(*)(void))FIRMWARE_ADDRESS)();
}
}
}
return 0;
}
This is just one example of how a bootloader might work. There are many different ways to implement a bootloader, and the specific details will depend on the specific microcontroller and the requirements of the application.
As for a "Hello" firmware example, here is some simple firmware code in C that will print "Hello, World!" to a UART interface:
#include <stdio.h>
#include "uart.h" // UART communication functions
int main(void)
{
uart_init(); // Initialize UART
printf("Hello, World!\n"); // Print message to UART
return 0;
}