The MCX N series are MCUs with an integrated flash memory like many traditional MCU series. Having an embedded flash memory is particularly useful, as facilitates board design, development, simplifies BOM, software development.
However, the size of the embedded flash might not meet applications required memory footprint. Some customers simply need more flash memory. For example, Graphical applications require store large image buffers in a non-volatile memory. Data logger devices that need to store large amounts of data. Or heavy inference models for object recognition. Applications that need to store backup versions of their firmware or are way too complex and big. To solve this requirement the MCX N94x provides the capability to interface a great variety of wider and external memories. There are several traditional options to expand flash storage, but the one we are addressing is the use of external NOR flash memories.
To access an external memory the MCX N94x integrates modules that support external memory interfacing, those are FlexIO and the FlexSPI. The FlexSPI module is capable of communicating with several kinds of memories. One of them is the Serial NOR flash which can be used as booting device.
The goal of this article is to present the solution of a customer case scenario: How to run code from an external flash memory. Demonstrating the flexibility of the MCX N94x platform as well as introducing several tools, peripherals, and concepts that a developer will need to be familiar with while developing an application that runs from an external flash memory.
The below figure illustrates the goal of this article, Run code from an external flash memory. The same hello world application can be run from the internal and external memory, for example, a NOR serial QuadSPI memory, without any functionality changes. The core simply will fetch and execute the instructions, using dedicated peripherals like FlexSPI.
Figure 1 Executing same code from internal memory vs external memory.
The MCX N94x series integrates a FlexSPI module, which enables running code from an external NOR flash memory.
Figure 2 MCX N94x block diagram
To follow this article, you will need the FRDM-MCXN947 Development board. Which is the evaluation board for the MCX N94x and N54x MCUs. The FRDM-MCXN947 integrates a W25Q64JVSSIQ QuadSPI flash memory. Therefore, this platform is perfect for creating an application that runs from an external flash memory.
Figure 3 External NOR flash memory on FRDM-MCXN947
With the boot ROM that the MCX N integrates you can erase, program, and read the on-chip or external flash memory, which means you can use the boot ROM to download a boot image into the on-chip or external flash memory via the ISP interfaces. This article shows how to load a customized led_blinky demo, from the SDK to the external flash memory using the boot ROM. However, other application projects may be used as well.
Also, the boot ROM takes responsibility for the boot flow. It selects whether to boot from on-chip flash memory, external flash memory, or ISP mode. The figure below shows an extremely simplified boot flow, which gives an overall understanding of the chip boot flow. For more details on the boot flow mechanism refer to the section Top-level boot flow of the MCX Nx4x Reference manual.
Figure 4 Simplified boot Flow
After the reset handling routine, the boot ROM will take control of the chip’s boot flow. It will begin with pertinent chip’s initialization. Then will check the status of the ISP pin. If the ISP pin is currently asserted, the ISP boot handling routine will be executed. ISP or In-system programming is an execution mode where the boot ROM will wait for commands from an external host, over protocols like UART or USB, to do actions like read and write the chip memory, burn fuses, and many more. If the ISP pin is not asserted, the boot ROM will continue to determine the boot mode. Boot mode can be controlled with the CMPA[BOOT_SOURCE] bit of the CMPA of the PFR or eFUSES. If CMPA[BOOT_SOURCE]= 01b the boot ROM will run FlexSPI NOR boot handling routine. If CMPA[BOOT_SOURCE] is 00b or 11b internal flash memory handling routine is executed. After the boot mode selection, the image will be validated. If it is not valid, for example, is not present in memory, is corrupted, or does not have proper format, the ISP boot handling routine is executed. If the image is valid, finally the boot ROM will perform the jump to the user´s application.
The default boot flow is by internal flash, most of the SDK examples will be configured to boot from internal flash, as CMPA[BOOT_SOURCE] equals zero by default, or when CMPA area is erased. According to the Simplified bot Flow diagram, we need to make boot ROM go to FlexSPI NOR boot handling routine, to boot from the external flash. Therefore, we need to make CMPA[BOOT_SOURCE] = 01b.
The PFR holds critical boot and security settings that are monitored by the boot ROM after every reset. As just described earlier, the CMPA area can be used to control boot mode. Therefore, PFR areas need to be carefully configured, as they control boot flow and also security features of the chip. The below figure shows a very minimalistic representation of the PFR containing the CMPA, next to the internal flash.
Figure 5 PFR's CMPA area
For specific details of the PFR please refer to the MCXNx4x_IFR.xlsx, attached to the MCX Nx4x Reference Manual.
To facilitate writing into the CMPA and other areas of the PFR, signing applications, burning fuses. programming applications, NXP already developed several host command tools, APIs, and applications and integrated into a python tool called SPSDK. The below figure shows all the tools contained by the SPSDK. To get details on the SPSDK and description of APIs, Applications, and tools refer to SPSDK documentation at https://spsdk.readthedocs.io/en/latest/spsdk.html#
Figure 6 SPSDK API modules, applications, and tools
The above figure highlights 3 applications, provided by the SPSDK, that is going to be used to configure the chip to boot from the external flash memory those are: nxpdebugmbox, PRF, and BLHOST. Detailed step-by-step usage of these applications will be described later on.
The nxpdebugmbox application sends interfaces with the chip’s debug mailbox over SWD. The purpose of this tool is set MCU into ISP mode, without having to set low ISP pin, also to make a mass erase to the chip's memory and PFR. Once the MCU enters ISP mode the host PC will be able to communicate with the boot ROM over UART or USB protocols. This enables the use of PFR and BLHOST applications. The PFR application will be used to write the CMPA[BOOT_SOURCE] = 01b and enable FlexSPI, port to probe and let the chip communicate with the external flash, in the CMPA area of the PFR. The BLHOST application will be used to erase the chip and program application image, in this particular case, a led_blinky demo.
The following terms are critical to understand how we can execute code in an external NOR flash memory with the MCX N94x.
First of all, Execute-in-Place refers to the capability that the MCU core has to fetch and execute instructions directly from the external memory. It refers to the capability that an MCU has to execute code from the flash memory instead of an internal and traditional flash.
To do XIP the MCX N94x requires a capable peripheral with the ability to fetch instructions from the external memory. The FlexSPI module is capable of doing this. The FlexSPI does support several types of external memories. One of them is Serial NOR Flash.
To interface the external memory with the FlexSPI supports SPI. SPI is an excellent serial protocol for short-distance and high-speed communications. The term QuadSPI could be simply understood as an implementation of the SPI protocol, where instead of having a single Data line, there are 4 data lines. SPI also supports two, and eight data lines.
Flexible Serial Peripheral Interface (FlexSPI) memory controller supports connection to external serial NOR, NAND, and RAMs.
It supports two SPI channels and up to four external devices. Each channel supports Single/Dual/Quad/Octal mode data transfer (1/2/4/8 bidirectional data lines).
Flexible sequence engine (LUT table) to support various vendor devices (serial NOR, serial NAND, HyperBus, FPGA, etc).
Memory-mapped read/write access by AHB Bus
Software triggered Flash read/write access by IP Bus
Figure 7 FlexSPI block diagram
FlexSPI system memory map regions are remapped to different addresses at the FlexSPI sub-module:
Figure 8 FlexSPI memory map
FlexSPI can support serial devices compliant with JESD216.
You will need the following Hardware:
You will need to install the following Software.
We recommend to run the SPSDK on a python virtual environment, as this simplifies the installation of the SPSDK.
Before proceeding with installation steps for the SPSDK we recommend to create a folder in your computer. This is where the virtual environment will be running, the SPSDK tools will be called, and all the files generated by the SPSDK will be stored.
spsdk --version |
This figure shows the example of this command and the spsdk version used for this document.
Figure 9 SPSDK version.
This chapter describes all steps to generate and boot an image from the external memory.
To run the commands listed in this document you will need use your python’s virtual environment. Use the virtual environment you created during SPSDK installation.
This section describes the settings that need to be done in the MCX N94x PFR and bootable image.
In the following steps, we are going to focus on editing CMPA area since it contains boot-related fields for external execution. CFPA area is not required to be edited.
Note:The commands used to generate the templates are based on ‘pfr’ tool For details on the capabilities of these commands, visit: User Guide - pfr — SPSDK documentation
First we are going to use pfr to create our yml CMPA/CFPA templates. These templates contain only the default configurations specified for the CMPA and CFPA areas of device.
If the command run successful the templates will be generated. As shown in figure this figures
Figure 10 Yaml template created.
Open CMPA template in any text editor and follow the below steps.
After editing the cmpa yaml, you will generate the CMPA/CFPA binaries from the yml templates.
This section shows how to create an XIP bootable image from the MCUXpresso SDK led_blinky example.
Figure 11 Import the blinky example.
Figure 12 Project ´properties
Figure 13 MCU settings window
Figure 14 External flash areas in the linker.
Note: Moving a memory area to the top of the “Memory details” table indicates the linker that text and data should be placed preferably in that area.
Type |
Name |
Alias |
Location |
Size |
Driver |
Flash |
QSPI_FLASH |
Flash |
0x80001000 |
0xFFFF000 |
MCXN9xx_SFDP_FlexSPI.cfx |
Flash |
QSPI_FCB |
Flash 2 |
0x80000400 |
0x400 |
MCXN9xx_SFDP_FlexSPI.cfx |
Figure 15 Configured flash areas
Figure 16 Successfully compiled image.
Note: You should see that the QSPI_FLASH section is being used and that the PROGRAM_FLASH section is not.
Figure 17 Create binary.
7 The binary should appear in the debug folder. Get easily get the binary location by doing a right click on the binary and navigating to “Show in>System Explorer”
Figure 18 Get created binary with ease.
ISP (In system programming ) is a mode where a host is able to instruct the ROM bootloader to program a new image or the PFR. The which holds the CMPA field with our configuration to do XIP from the external flash memory. Basically, the Host computer sends commands to the ROM bootloader to program the PFR using NXP’s BLHOST.
To enter into ISP mode there are two options: ISP pin entry and Debugger mailbox. ISP pin entry requires keeping ISP pin asserted during reset sequence and de-asserting this pin after reset has been completed. Debugger mailbox only requires to use of a SWD debugger to make the ROM entry ISP mode.
To enter into ISP mode over ISP pins follow is necessary keep ISP pin asserted during reset sequence and de-assert this pin after reset has been completed. To use the debugger mailbox is only needed having a debugger.
For simplicity, the communication protocol will be USB. However, UART can be used as well.
nxpdebugmbox erase |
This figure shows the expected output when running the two commands from above.
This operation will erase the contents on the internal and external flash memory. Also the PFR contents. Repeating this operation can be useful to get the chip to boot from internal flash memory.
Note: nxpdebugmbox commands need to be run using a SWD-JTAG debugger. For this example on-board debugger of the FRDM-MCXN947 is used.
Run the following command to ping the rom Bootloader, this way we can ensure that the device was set correctly in ISP mode.
blhost -t 2000 -p COMxxx,115200 -j -- get-property 1 |
Figure 19 Ping rom Bootloader
From now on, an external debugger is not strictly required. The communication with the rom bootloader can be done over UART or USB. For simplicity, the UART protocol-based commands are used from now on in the rest of this document.
We are going to follow the steps to write image.
1 Write CMPA binary.
pfr write -p COMxxx,115200 -t cmpa -f mcxn9xx -b cmpa_bin.bin |
2 Write CFPA binary.
pfr write -p COM140,115200 -t cfpa -f mcxn9xx -b cfpa_bin.bin |
3 Provide FlexSPI flash memory configuration
blhost -t 2000 -p COMxxx,115200 -- fill-memory 0x20000000 0x04 0xc0000405 |
4 Configure and fill memory.
blhost -t 2000 -p COMxxx,115200 -- configure-memory 0x09 0x20000000 blhost -t 2000 -p COMxxx,115200 -- fill-memory 0x20003000 0x04 0xf000000f |
5 Erase flash
blhost -t 2000 -p COMxxx,115200 -- flash-erase-region 0x80000000 0x100000 |
6 Configure memory.
blhost -t 2000 -p COMxxx,115200 -- configure-memory 0x09 0x20003000 |
7 Write image.
blhost -t 2000 -p COMxxx,115200 write-memory 0x80001000 frdmmcxn947_led_blinky.bin |
8 Reset.
blhost -t 2000 -p COMxxx,115200 reset |
You might use this command or directly press the reset button from the FRDM board.
The below image shows the successful execution of each command. Your FRDM-MCXN947 should be blinking by now.
Figure 20 Successful image write and reset.
Once the board is reset you will notice that the loaded example will begin to execute.
To double-check that the image is executing from the external flash you can use the disassembly view from MCUXpresso.
1 Attach the debugger to the running target and place a breakpoint at any part of the code.
Figure 21 Attach debugger to a running target.
Figure 22 halt execution.
Figure 23 Breakpoint at main.
Figure 24 Open "Disassembly" view
Figure 25 Running code from external flash.
If you want to return the device to run from internal memory, then you need to go back to default values. If there are no modifications, to the ones shown in this document, to the CMPA and CMPA areas, the nxpdebugmbox command can be executed.