2.6. Memory
RP2040 has embedded ROM and SRAM, and access to external Flash via a QSPI interface. Details of internal memory are given below.
2.6.1. ROM
A 16kB read-only memory (ROM) is at address
0x00000000. The ROM contents are fixed at the time the silicon is manufactured. It contains:
- • Initial startup routine
- • Flash boot sequence
- • Flash programming routines
- • USB mass storage device with UF2 support
- • Utility libraries such as fast floating point
The boot sequence of the chip is defined in Section 2.8.1 , and the ROM contents is described in more detail in Section 2.8 . The full source code for the RP2040 bootrom is available at:
The ROM offers single-cycle read-only bus access, and is on a dedicated AHB-Lite arbiter, so it can be accessed simultaneously with other memory devices. Attempting to write to the ROM has no effect (no bus fault is generated).
2.6.2. SRAM
There is a total of 264kB of on-chip SRAM. Physically this is partitioned into six banks, as this vastly improves memory bandwidth for multiple masters, but software may treat it as a single 264kB memory region. There are no restrictions on what is stored in each bank: processor code, data buffers, or a mixture. There are four 16k x 32-bit banks (64kB each) and two 1k x 32-bit banks (4kB each).
! IMPORTANT
Banking is a physical partitioning of SRAM which improves performance by allowing multiple simultaneous accesses. Logically there is a single 264kB contiguous memory.
Each SRAM bank is accessed via a dedicated AHB-Lite arbiter. This means different bus masters can access different SRAM banks in parallel, so up to four 32-bit SRAM accesses can take place every system clock cycle (one per master).
SRAM is mapped to system addresses starting at
0x20000000. The first 256kB address region is word-striped across the four larger banks, which provides a significant memory parallelism benefits for most use cases.
Consecutive words in the system address space are routed to different RAM banks as shown in Table 153 .
Table 153. SRAM bank0/1/2/3 striped mapping.
| System address | SRAM Bank | SRAM word address |
|---|---|---|
0x20000000 | Bank 0 | 0 |
0x20000004 | Bank 1 | 0 |
0x20000008 | Bank 2 | 0 |
0x2000000c | Bank 3 | 0 |
0x20000010 | Bank 0 | 1 |
0x20000014 | Bank 1 | 1 |
0x20000018 | Bank 2 | 1 |
0x2000001c | Bank 3 | 1 |
0x20000020 | Bank 0 | 2 |
0x20000024 | Bank 1 | 2 |
0x20000028 | Bank 2 | 2 |
0x2000002c | Bank 3 | 2 |
| etc |
The next two 4kB regions (starting at
0x20040000 and
0x20041000) are mapped directly to the smaller, 4kB memory banks. Software
may
choose to use these for per-core purposes, e.g. stack and frequently-executed code, guaranteeing that the processors never stall on these accesses. However, like all SRAM on RP2040, these banks have single-cycle access from
all
masters providing no other masters are accessing the bank in the same cycle, so it is reasonable to treat memory as a single 264kB device.
The four 64kB banks are also available at a non-striped mirror. The four 64kB regions starting at
0x21000000,
0x21010000,
0x21020000,
0x21030000 are each mapped directly to one of the four 64kB SRAM banks. Software can explicitly allocate data and code across the physical memory banks, for improved memory performance in exceptionally demanding cases. This is often unnecessary, as memory striping usually provides sufficient parallelism with less software complexity.
The non-striped mirror starts at an offset of +16MB above the base of SRAM, as this is the maximum offset that allows ARMv6M subroutine calls between the smaller banks and the non-striped larger banks.
2.6.2.1. Other On-chip Memory
Besides the 264kB main memory, there are two other dedicated RAM blocks that may be used in some circumstances:
- • If flash XIP caching is disabled, the cache becomes available as a 16kB memory starting at
0x15000000 - • If the USB is not used, the USB data DPRAM can be used as a 4kB memory starting at
0x50100000
This gives a total of 284kB of on-chip SRAM. There are no restrictions on how these memories are used, e.g. it is possible to execute code from the USB data RAM if you choose.
2.6.3. Flash
External Flash is accessed via the QSPI interface using the execute-in-place (XIP) hardware. This allows an external flash memory to be addressed and accessed by the system as though it were internal memory. Bus reads to a 16MB memory window starting at
0x10000000 are translated into a serial flash transfer, and the result is returned to the master that initiated the read. This process is transparent to the master, so a processor can execute code from the external flash without first copying the code to internal memory, hence "execute in place". An internal cache remembers the contents of recently-accessed flash locations, which accelerates the average bandwidth and latency of the interface.
Once correctly configured by RP2040's bootrom and the flash second stage, the XIP hardware is largely transparent, and software can treat flash as a large read-only memory. However, it does provide a number of additional features to serve more demanding software use cases.
Figure 14. Flash execute-in-place (XIP) subsystem. System accesses via the main AHB-Lite slave are decoded to determine if they are XIP accesses, direct accesses to the SSI e.g. for configuration, or accesses to various other hardware and control registers in the XIP subsystem. XIP accesses are first looked up in the cache, to accelerate accesses to recently-used data. If the data is not found in the cache, an external serial access is generated via the SSI, and the resulting data is stored in the cache and forwarded on to the system bus.

graph TD
MainAHBL[MainAHBL Slave] <--> Decode[Decode and Config]
Decode <--> AHBLBridge[AHBL-APB Bridge for SSI Configuration]
Decode <--> Cache[Read-only Cache]
Decode <--> StreamingFIFO[Streaming FIFO]
Cache <--> Mux[Mux]
Mux <--> Interposer[Atomic RWType Interposer]
Interposer <--> SSI[SSI]
SSI <--> QSPI[QSPI]
StreamingFIFO <--> AuxAHBL[Aux AHBL Slave (streaming FIFO only)]
NOTE
The serial flash interface is configured by the flash second stage when using the SDK to run at an integer divider of the system clock. All the included second stage boot implementations support a
PICO_FLASH_SPI_CLKDIV setting (e.g. defaulted to 4 in
https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2040/boot_stage2/boot2_w25q080.S
to make the default interface speed
\(
125/4 = 31.25\text{MHz}
\)
). This divider can be overridden by specifying
PICO_FLASH_SPI_CLKDIV in the particular board config header used with the SDK.
2.6.3.1. XIP Cache
The cache is 16kB, two way set-associative, 1 cycle hit. It is internal to the XIP subsystem, and only affects accesses to XIP flash, so software does not have to consider cache coherence, unless performing flash programming operations. It caches reads from a 24-bit flash address space, which is mirrored multiple times in the RP2040 address space, each alias having different caching behaviour. The eight MSBs of the system address are used for segment decode, leaving 24 bits for flash addressing, so the maximum supported flash size (for XIP operation) is 16MB. The available mirrors
are:
- • 0x10... XIP access, cacheable, allocating - Normal cache operation
- • 0x11... XIP access, cacheable, non-allocating - Check for hit, don't update cache on miss
- • 0x12... XIP access, non-cacheable, allocating - Don't check for hit, always update cache
- • 0x13... XIP access, non-cacheable, non-allocating - Bypass cache completely
- • 0x15... Use XIP cache as SRAM bank, mirrored across entire segment
If the cache is disabled, via the CTRL.EN register bit, then all four of the XIP aliases ( 0x10 to 0x13 ) will bypass the cache, and access the flash directly. This has a significant impact on XIP code execution performance.
Access to the 0x15... segment produces a bus error unless the cache is disabled by clearing CTRL.EN . Once the cache is disabled, this region behaves as an additional 16kB SRAM bank. Reads and writes are one cycle, but there is a wait state on consecutive write-read sequences, i.e. there is no write forwarding buffer.
2.6.3.2. Cache Flushing and Maintenance
The FLUSH register allows the entire cache contents to be flushed. This is necessary if software has reprogrammed the flash contents, and needs to clear out stale data and code, without performing a reboot. Cache flushes are triggered either manually by writing 1 to FLUSH , or automatically when the XIP block is brought out of reset. The flush is implemented by zeroing the cache tag memory using an internal counter, which takes just over 1024 clock cycles (16kB total size / 8 bytes per line / 2 ways per set).
Flushing the cache whilst accessing flash data (perhaps initiating the flush on one core whilst another core may be executing code from flash) is a safe operation, but any master accessing flash data while the flush is in progress will be stalled until completion.
CAUTION
The cache-as-SRAM alias ( 0x15... ) must not be written whilst a cache flush is in progress. Before writing for the first time, if a cache flush has recently been initiated (e.g. via a watchdog reset), a dummy read from FLUSH is recommended to ensure the cache flush has completed. Writing to cache-as-SRAM whilst a flush is in progress can corrupt the data memory contents.
A complete cache flush dramatically slows subsequent code execution, until the cache "warms up" again. There is an alternative, which allows cache contents corresponding to only a certain address range to be invalidated. A write to the 0x10... mirror will look up the addressed location in the cache, and delete any matching entry found. Writing to all word-aligned locations in an address range (e.g. a flash sector that has just been erased and reprogrammed) therefore eliminates the possibility of stale cached data in this range, without suffering the effects of a complete cache flush.
Pico Examples: https://github.com/raspberrypi/pico-examples/blob/master/flash/cache_perfctr/flash_cache_perfctr.c Lines 30 - 55
30 // Flush cache to make sure we miss the first time we access test_data 31 xip_ctrl_hw->flush = 1; 32 while (!(xip_ctrl_hw->stat & XIP_STAT_FLUSH_READY_BITS)) 33 tight_loop_contents(); 34 35 // Clear counters (write any value to clear) 36 xip_ctrl_hw->ctr_acc = 1; 37 xip_ctrl_hw->ctr_hit = 1; 38 39 (void) *test_data_ptr; 40 check(xip_ctrl_hw->ctr_hit == 0 && xip_ctrl_hw->ctr_acc == 1, 41 "First access to data should miss"); 42 43 (void) *test_data_ptr; 44 check(xip_ctrl_hw->ctr_hit == 1 && xip_ctrl_hw->ctr_acc == 2,
45 "Second access to data should hit"); 46 47 // Write to invalidate individual cache lines (64 bits) 48 // Writes must be directed to the cacheable, allocatable alias (address 0x10.....) 49 *test_data_ptr = 0; 50 (void) *test_data_ptr; 51 check(xip_ctrl_hw->ctr_hit == 1 && xip_ctrl_hw->ctr_acc == 3, 52 "Should miss after invalidation"); 53 (void) *test_data_ptr; 54 check(xip_ctrl_hw->ctr_hit == 2 && xip_ctrl_hw->ctr_acc == 4, 55 "Second access after invalidation should hit again");
2.6.3.3. SSI
The execute-in-place functionality is provided by the SSI interface, documented in Section 4.10 . It supports 1, 2 or 4-bit SPI flash interfaces (SPI, DSPI and QSPI), and can insert either an instruction prefix or mode continuation bits on each XIP access. This includes the possibility of issuing a standard 03h serial flash read command for each access, allowing virtually any serial flash device to be used. The maximum SPI clock frequency is half the system clock frequency.
The SSI can also be used as a standard FIFO-based SPI master, with DMA support. This mode is used by the bootrom to extract the second stage bootloader from external flash (see Section 2.8.1 ). The bus interposer allows an atomic set, clear or XOR operation to be posted to SSI control registers, in the same manner as other memory-mapped IO on RP2040. This is described in more detail in Section 2.1.2 .
2.6.3.4. Flash Streaming and Auxiliary Bus Slave
As the flash is generally much larger than SRAM, it's often useful to stream chunks of data into memory from flash. It's convenient to have the DMA stream this data in the background while software in the foreground is doing other things, and it's even more convenient if code can continue to execute from flash whilst this takes place.
This doesn't interact well with standard XIP operation, because of the lengthy bus stalls forced on the DMA whilst the SSI is performing serial transfers. These stalls are tolerable for a processor, because an in-order processor tends to have nothing better to do while waiting for an instruction fetch to retire, and because typical code execution tends to have much higher cache hit rates than bulk streaming of infrequently accessed data. In contrast, stalling the DMA prevents any other active DMA channels from making progress during this time, which slows overall DMA throughput.
The STREAM_ADDR and STREAM_CTR registers are used to program a linear sequence of flash reads, which the XIP subsystem will perform in the background in a best-effort fashion. To minimise impact on code being executed from flash whilst the stream is ongoing, the streaming hardware has lower priority access to the SSI than regular XIP accesses, and there is a brief cooldown (seven cycles) between the last XIP cache miss and resuming streaming. This helps to avoid increase in initial access latency on XIP cache miss.
Pico Examples: https://github.com/raspberrypi/pico-examples/blob/master/flash/xip_stream/flash_xip_stream.c Lines 45 - 48
45 while (!(xip_ctrl_hw->stat & XIP_STAT_FIFO_EMPTY)) 46 (void) xip_ctrl_hw->stream_fifo; 47 xip_ctrl_hw->stream_addr = (uint32_t) &random_test_data[0]; 48 xip_ctrl_hw->stream_ctr = count_of(random_test_data);
The streamed data is pushed to a small FIFO, which generates DREQ signals, telling the DMA to collect the streamed data. As the DMA does not initiate a read until after the data has been read from flash, the DMA is not stalled when accessing the data.
Although this scheme ensures that the data is ready in the streaming FIFO once the DREQ is asserted, the DMA can still be stalled if another master is currently stalled on the XIP slave, e.g. due to a cache miss. This is solved by the auxiliary bus slave, which is a simple bus interface providing access only to the streaming FIFO. This slave is exposed on the
FASTPERI arbiter, which services only native AHB-Lite peripherals which don't generate wait states, so the DMA will never experience stalls when accessing the FIFO at this address, assuming it has high bus priority.
Pico Examples: https://github.com/raspberrypi/pico-examples/blob/master/flash/xip_stream/flash_xip_stream.c Lines 58 - 70
58 const uint dma_chan = 0; 59 dma_channel_config cfg = dma_channel_get_default_config(dma_chan); 60 channel_config_set_read_increment(&cfg, false); 61 channel_config_set_write_increment(&cfg, true); 62 channel_config_set_dreq(&cfg, DREQ_XIP_STREAM); 63 dma_channel_configure( 64 dma_chan, 65 &cfg, 66 (void *) buf, // Write addr 67 (const void *) XIP_AUX_BASE, // Read addr 68 count_of(random_test_data), // Transfer count 69 true // Start immediately! 70 );
2.6.3.5. Performance Counters
The XIP subsystem provides two performance counters. These are 32 bits in size, saturate upon reaching 0xffffffff, and are cleared by writing any value. They count:
- 1. The total number of XIP accesses, to any alias
- 2. The number of XIP accesses which resulted in a cache hit
For common use cases, this allows the cache hit rate to be profiled.
2.6.3.6. List of XIP Registers
The XIP registers start at a base address of 0x14000000 (defined as XIP_CTRL_BASE in SDK).
Table 154. List of XIP registers
| Offset | Name | Info |
|---|---|---|
| 0x00 | CTRL | Cache control |
| 0x04 | FLUSH | Cache Flush control |
| 0x08 | STAT | Cache Status |
| 0x0c | CTR_HIT | Cache Hit counter |
| 0x10 | CTR_ACC | Cache Access counter |
| 0x14 | STREAM_ADDR | FIFO stream address |
| 0x18 | STREAM_CTR | FIFO stream control |
| 0x1c | STREAM_FIFO | FIFO stream data |
XIP: CTRL Register
Offset: 0x00
Description
Cache control
Table 155. CTRL Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:4 | Reserved. | - | - |
| 3 | POWER_DOWN: When 1, the cache memories are powered down. They retain state, but can not be accessed. This reduces static power dissipation. Writing 1 to this bit forces CTRL_EN to 0, i.e. the cache cannot be enabled when powered down. Cache-as-SRAM accesses will produce a bus error response when the cache is powered down. | RW | 0x0 |
| 2 | Reserved. | - | - |
| 1 | ERR_BADWRITE: When 1, writes to any alias other than 0x0 (caching, allocating) will produce a bus fault. When 0, these writes are silently ignored. In either case, writes to the 0x0 alias will deallocate on tag match, as usual. | RW | 0x1 |
| 0 | EN:
When 1, enable the cache. When the cache is disabled, all XIP accesses will go straight to the flash, without querying the cache. When enabled, cacheable XIP accesses will query the cache, and the flash will not be accessed if the tag matches and the valid bit is set. If the cache is enabled, cache-as-SRAM accesses have no effect on the cache data RAM, and will produce a bus error response. | RW | 0x1 |
XIP: FLUSH Register
Offset: 0x04
Description
Cache Flush control
Table 156. FLUSH Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:1 | Reserved. | - | - |
| 0 | Write 1 to flush the cache. This clears the tag memory, but the data memory retains its contents. (This means cache-as-SRAM contents is not affected by flush or reset.) Reading will hold the bus (stall the processor) until the flush completes. Alternatively STAT can be polled until completion. | SC | 0x0 |
XIP: STAT Register
Offset: 0x08
Description
Cache Status
Table 157. STAT Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:3 | Reserved. | - | - |
| 2 | FIFO_FULL: When 1, indicates the XIP streaming FIFO is completely full. The streaming FIFO is 2 entries deep, so the full and empty flag allow its level to be ascertained. | RO | 0x0 |
| 1 | FIFO_EMPTY: When 1, indicates the XIP streaming FIFO is completely empty. | RO | 0x1 |
| Bits | Description | Type | Reset |
|---|---|---|---|
| 0 | FLUSH_READY:
Reads as 0 while a cache flush is in progress, and 1 otherwise. The cache is flushed whenever the XIP block is reset, and also when requested via the FLUSH register. | RO | 0x0 |
XIP: CTR_HIT Register
Offset: 0x0c
Description
Cache Hit counter
Table 158. CTR_HIT Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:0 | A 32 bit saturating counter that increments upon each cache hit, i.e. when an XIP access is serviced directly from cached data. Write any value to clear. | WC | 0x00000000 |
XIP: CTR_ACC Register
Offset: 0x10
Description
Cache Access counter
Table 159. CTR_ACC Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:0 | A 32 bit saturating counter that increments upon each XIP access, whether the cache is hit or not. This includes noncacheable accesses. Write any value to clear. | WC | 0x00000000 |
XIP: STREAM_ADDR Register
Offset: 0x14
Description
FIFO stream address
Table 160. STREAM_ADDR Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:2 | The address of the next word to be streamed from flash to the streaming FIFO. Increments automatically after each flash access. Write the initial access address here before starting a streaming read. | RW | 0x00000000 |
| 1:0 | Reserved. | - | - |
XIP: STREAM_CTR Register
Offset: 0x18
Description
FIFO stream control
Table 161. STREAM_CTR Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:22 | Reserved. | - | - |
| Bits | Description | Type | Reset |
|---|---|---|---|
| 21:0 | Write a nonzero value to start a streaming read. This will then progress in the background, using flash idle cycles to transfer a linear data block from flash to the streaming FIFO. Decrements automatically (1 at a time) as the stream progresses, and halts on reaching 0. Write 0 to halt an in-progress stream, and discard any in-flight read, so that a new stream can immediately be started (after draining the FIFO and reinitialising STREAM_ADDR) | RW | 0x000000 |
XIP: STREAM_FIFO Register
Offset: 0x1c
Description
FIFO stream data
Table 162.
STREAM_FIFO
Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:0 | Streamed data is buffered here, for retrieval by the system DMA. This FIFO can also be accessed via the XIP_AUX slave, to avoid exposing the DMA to bus stalls caused by other XIP traffic. | RF | 0x00000000 |