When using an external flash memory with a Cortex-M1 microcontroller, configuring the reset vector appropriately is crucial for the system to boot up and run properly. The reset vector indicates the memory location that contains the first instruction to be executed after a reset event. This 120-word opening paragraph gives a quick answer – the Cortex-M1 reset vector must point to the start of the code in the external flash memory for the system to initialize and execute code externally after reset.
Cortex-M1 Reset Behavior
On power-up or external reset event, the Cortex-M1 microcontroller starts executing code from address 0x00000000. This is where the reset vector is located by default. The processor fetches the first instruction from this address and begins executing. In a typical setup, the on-chip flash memory is mapped at address 0x00000000. So the reset vector points to a location inside the internal flash which contains the startup code.
However, when using external flash, the internal flash is either not programmed or it may contain a small bootloader program. The main application code is located in the external flash which is mapped at some other address in the memory map. The reset vector needs to be reconfigured to point to the startup code in the external flash. Otherwise, the processor will fetch the first instruction from 0x00000000 (internal flash) which would typically result in a hard fault or hang condition.
External Flash Memory Interface
The Cortex-M1 microcontroller interfaces with external flash memory via the External Bus Interface (EBI). The EBI allows mapping the external flash at different locations in the microcontroller’s physical address space. Some common address mappings for external flash are:
- 0x60000000
- 0x80000000
- 0xA0000000
The actual address mapping is configurable and depends on the external memory controller configuration. The external flash memory is where the main application firmware image is stored. The reset vector needs to point to the startup code of this firmware image.
Configuring the Reset Vector
There are two main methods for configuring the reset vector on Cortex-M1 to point to external flash:
1. Modify the Vectors Table Offset Register
The Cortex-M1 has a Vectors Table Offset Register (VTOR) which holds the address of the exception vectors table. By default, this points to address 0x00000000. To redirect it, program the VTOR with the start address of the vector table in external flash.
For example, if the external flash is mapped at 0x80000000 and the vector table is located at offset 0x100, set the VTOR to 0x80000100. This points the processor to fetch the first vector from offset 0x100 of external flash. The vector table would contain the external startup code address in the reset vector entry.
2. Remap Internal Flash at External Flash Address
Another method is to keep the VTOR pointing to 0x00000000 (internal flash) but remap the internal flash to appear at the external flash address instead. This is done by configuring the memory mapper in the system to map internal flash at the external flash address.
For example, map the internal flash at 0x80000000 where external flash containing firmware is located. The reset vector in internal flash then points to the startup code in external flash. The processor fetches first instruction from 0x00000000 which is mapped to external flash.
Startup Code for External Flash
The startup code executes crucial initialization routines before launching into the main application. When using external flash, the startup code is modified compared to internal flash. Key differences include:
- Configure external memory interface
- Enable caches and MPU if needed
- Copy initialized data from flash to RAM
- Configure stack pointer for RAM
The startup code must also initialize the external flash controller correctly to access the flash. The SystemInit() routine takes care of these initializations required for external flash. The reset vector must point to a valid startup sequence customized for external flash.
Vector Table Structure
The vector table mapped at the reset vector address contains the following entries:
- Initial stack pointer value
- Reset vector – points to startup code
- NMI exception handler
- HardFault exception handler
- Other system exception handlers
With external flash, the address of the startup code must be programmed into the reset vector entry. The stack pointer and exception handlers’ addresses also need to match the application being loaded from external flash.
Bootloader Impact
If the Cortex-M1 boots up from an internal bootloader, the bootloader has the responsibility of configuring the reset vector and stack correctly before jumping to the external application. Typical steps include:
- Initialize system clock
- Configure EBI for external flash access
- Program VTOR register or remap internal flash
- Initialize and configure external flash
- Copy vector table from external flash to RAM
- Update stack pointer value
- Branch to external application reset handler
So the bootloader code must setup all the prerequisites for jumping to the external application including the reset vector. The application firmware sees it as a normal system reset.
Cortex-M1 Flash Programming Model
The Cortex-M1 flash programming model also needs to be adapted differently for internal and external flash:
Internal Flash Programming
- Embedded flash controller programs internal flash
- Flash controller generates interrupts on completion
- ISRs handle flash programming callbacks
External Flash Programming
- CPU/DMA programs external flash
- EBI/memory mapped flash interface
- Polling or interrupts to detect programming completion
So the software mechanisms used to program internal and external flash memory are different despite having a common interface in the ARM CMSIS libraries.
Debugging Considerations
While debugging and validating external flash setup, use hardware breakpoints to halt execution during reset handler. Monitor key registers like VTOR, stack pointer, and startup sequence. Force soft resets via debugger and monitor reset sequence. The IDE debugger may need awareness of flash remapping.
Risks of Incorrect Configuration
If the Cortex-M1 reset vector is not configured properly to point to external flash, effects can include:
- Lockups on reset – code execution halts
- Hard faults or exceptions – invalid opcode or memory access
- Execution of wrong code image – unpredictable behavior
- Hanging in bootloader mode – unable to launch application
So it is crucial the reset vector targets the expected startup code address for robust system operation.
Fallback Mechanism
As a fail-safe, include a fallback mechanism to reconfigure the reset vector from default value 0x00000000 to the external flash. For example:
- Little bootloader code at 0x00000000
- Tries to detect external flash device presence
- If found, reconfigures reset vector and branches to external flash
- Allows recovery from incorrect reset vector
This way even if reset vector is invalid, the bootloader will redirect it to the proper external flash startup address.
Summary
Configuring the Cortex-M1 reset vector to external flash requires reprogramming the VTOR register or remapping the internal flash. The startup code must be adapted for external flash by initializing the flash controller and implementing other optimizations. A bootloader may be responsible for configuring the reset vector before jumping to the external application. Proper configuration is critical for robust system operation.