This issue’s topic
Let’s talk about
The storage and execution of programs in microcontrollers have been introduced in previous articles, such as Storage and Execution of STM32 Microcontroller Programs. This article focuses on the basic process of program storage and loading.
For PCs, executable files are stored on disk, and when executed, the contents of the file (code and data) are loaded into memory for execution.
For microcontrollers, program files exist in ROM, and during execution, the instruction code does not need to be loaded into RAM to execute.
The Flash inside the MCU can run code directly, meaning the executable file’s Code and RO Data are not loaded into RAM.
This article will introduce why microcontrollers can run code directly and the related technical principles.
The key technology here is XIP. So what is XIP?
XIP stands for eXecute In Place, which means executing directly from memory, allowing the CPU to read program code directly from storage without needing to read it into memory.
Applications can run directly in Flash memory without needing to read the code into system RAM. Executing in Flash means that NOR Flash does not need initialization and can execute code directly within Flash.
However, it often only executes part of the code, which means the program code does not occupy memory, thus reducing memory requirements.
The so-called on-chip execution does not mean the program executes in storage; the CPU’s basic functions are fetch, decode, and execute.
NOR Flash can be executed on-chip because it meets the CPU’s requirements for fetching, decoding, and executing instructions.
The CPU sends an address, and NOR Flash can provide data for the CPU to execute without needing additional processing operations.
For microcontrollers with Cortex-M3 cores, the internal Flash is connected to the AHB bus.
Accessing instructions and data in Flash is done through the AHB bus. The prefetch module of the microcontroller is used to read instructions via the ICode bus.
Through the ICode bus, the CPU directly reads instructions from on-chip Flash. To improve execution speed, some microcontrollers provide an instruction prefetch buffer that first reads instructions into the prefetch buffer.
For STM32 microcontrollers, there are 2 prefetch buffers, each with a size of 64 bits. They are automatically opened after each reset.
Due to the existence of the prefetch buffer, the CPU can operate at higher clock frequencies. The CPU can fetch up to 32 bits of data at a time, and while one instruction is being executed, the next instruction is already waiting in the buffer. This improves the efficiency of instruction fetching and execution speed.
For 51 microcontrollers, there is no instruction prefetch buffer, and through XIP technology, instructions are read from Flash, then decoded and executed.
Supplement
There are three main methods of code execution in embedded systems:
-
Full Mapping: When the embedded system program runs, all code is copied from non-volatile storage (Flash, ROM, etc.) to RAM for execution.
-
Demand Paging: Only part of the code is copied to RAM, managing the import/export of pages in RAM. If access is made to virtual memory that is not in physical RAM, a page fault occurs, and code and data are mapped to RAM only then.
-
XIP: At system startup, code is not copied to RAM but executed directly in non-volatile storage, with only the data portion that needs to change stored in RAM.
Alright, regarding program execution, welcome to discuss together.
Thank you for reading, keep it up~
Source: Learn Embedded Together
Due to recent changes in the WeChat public platform’s push rules, many readers have reported not being able to see updated articles in time. According to the latest rules, it is recommended to frequently click “Recommend Reading, Share, Collect,” etc., to become a regular user.
Recommended Reading:
-
Multiple locations negotiating to build factories, is Foxconn returning to India for semiconductor expansion?
-
Profiting tens of thousands at once? A woman was found hiding thousands of SD cards in a bicycle!
-
Chinese regulators just approved, and American chip manufacturers immediately “backed out”!
-
Guangqi Toyota layoffs, outsourcing all to N+5?
-
Just now, domestic MRI instruments have broken through technical barriers!
Please click 【View】 to give the editor a boost
