RP2040: Writing and Loading Code from QSPI Flash Memory - Page 1
Forum section: electronics / general

author: chandlerkc
author_rank: Newbie
timestamp: June 05, 2022, 03:32:21 pm
content: Hello everyoneI bought an RP2040 board with 16MB QSPI Flash Memory in the board:The Flash Memory w25q128 is connected to QSPI dedicated pins of the RP2040:I was finding out how to access this data from the RP2040 datasheet:But I didn't find out how to:Initialize XIP memory in rp2040 boot;To flash/access data into the w25q128; andRun instructions from the External Flash Memory.I was looking for sample codes at GitHub but I didn't find anything useful for these two items I want.I also found the 2.6.3.1 section of the RP2040 datasheet but I don't know if XIP cache is what I am looking for...Does someone have done anything related to that? I am a newbie in this subject, so I would like to sorry if I did something wrong.Thanks in advance!

author: ataradov
author_rank: Super Contributor
timestamp: June 05, 2022, 04:16:45 pm
content: I',m sure there are many detailed tutorials out there. RP2040 has a bootloader in the ROM. To activate it, plug in the board into a USB conector, press BOOT, press and release RESET, release BOOT. The device would enumerate as a USB drive. You can drag and drop or copy a UF2 image to that drive. UF2 file specifies how to program the flash.To get UF file you can use the official SDK and examples (https://github.com/raspberrypi/pico-sdkandhttps://github.com/raspberrypi/pico-examples) or any number of other projects. I have my own simple self contained starter project herehttps://github.com/ataradov/mcu-starter-projects/tree/master/rp2040

author: chandlerkc
author_rank: Newbie
timestamp: June 05, 2022, 04:37:10 pm
content: I did downloaded your folder rp2040 from your github repo (also starred it too!), I was not able to run the build command from your makefile in the make folder.But do you know how to read/write data from this flash during the execution of the main program?

author: ataradov
author_rank: Super Contributor
timestamp: June 05, 2022, 04:45:34 pm
content: Well, you need to start with figuring out how to build things. You won't get much further without that.To write the flash, you initialize the flash and issue commands as described in the flash datasheet. Keep in mind that the code that does this must run from SRAM, since flash would not be available for execution.The example of flash initialization is available herehttps://github.com/ataradov/mcu-starter-projects/blob/master/rp2040/startup_rp2040.c(boot_entry() function). This code only issues read command, but others are similar.But doing this is an advanced topic, if you can't even build a basic project, start with that.And reading the data is simple, since flash is memory-mapped into the normal address space, so you just read it as you would any internal memory and the flash controller would issue commands automatically. Writing is harder.

author: chandlerkc
author_rank: Newbie
timestamp: June 05, 2022, 05:09:30 pm
content: Let me explain a little bit: I have compiled other rp2040 C projects with the SDK, I did not build yet with your repo.But don't worry, it is a matter of time!I will also take a deeper look at this .c file you mentioned.

author: ataradov
author_rank: Super Contributor
timestamp: June 05, 2022, 05:11:17 pm
content: The official SDK contains examples of working with the flash using their SDK -https://github.com/raspberrypi/pico-examples/blob/master/flash/program/flash_program.c

author: chandlerkc
author_rank: Newbie
timestamp: June 05, 2022, 05:13:04 pm
content: Dude!!!I have not seen this example, what a shame of myself!!It is what I am talking about, thanks a lot.

author: chandlerkc
author_rank: Newbie
timestamp: June 05, 2022, 06:24:16 pm
content: UPDATE: I was able to build the ataradov repo, I could not build before because I am using Windows with PS.Had to modify some lines in Makefile but it works nowMakefile:Code:##############################################################################BUILD = buildBIN = Demo##############################################################################.PHONY: all directory clean sizeCC = arm-none-eabi-gccOBJCOPY = arm-none-eabi-objcopySIZE = arm-none-eabi-sizeMKDIR = mkdirCFLAGS += -W -Wall --std=gnu11 -OsCFLAGS += -ffreestandingCFLAGS += -fno-diagnostics-show-caretCFLAGS += -fdata-sections -ffunction-sectionsCFLAGS += -funsigned-char -funsigned-bitfieldsCFLAGS += -mcpu=cortex-m0plus -mthumbCFLAGS += -MD -MP -MT $(BUILD)/$(*F).o -MF $(BUILD)/$(@F).dLDFLAGS += -mcpu=cortex-m0plus -mthumbLDFLAGS += -nostartfilesLDFLAGS += -Wl,--gc-sectionsLDFLAGS += -Wl,--script=../linker/rp2040.ldINCLUDES += \-I../include \-I..SRCS += \../main.c \../startup_rp2040.cDEFINES += \CFLAGS += $(INCLUDES) $(DEFINES)OBJS = $(addprefix $(BUILD)/, $(notdir %/$(subst .c,.o, $(SRCS))))all: directory $(BUILD)/$(BIN).elf $(BUILD)/$(BIN).hex $(BUILD)/$(BIN).bin $(BUILD)/$(BIN).uf2 size$(BUILD)/$(BIN).elf: $(OBJS)@echo LD $@@$(CC) $(LDFLAGS) $(OBJS) $(LIBS) -o $@$(BUILD)/$(BIN).hex: $(BUILD)/$(BIN).elf@echo OBJCOPY $@@$(OBJCOPY) -O ihex $^ $@$(BUILD)/$(BIN).bin: $(BUILD)/$(BIN).elf@echo OBJCOPY $@@$(OBJCOPY) -O binary $^ $@$(BUILD)/$(BIN).uf2: $(BUILD)/$(BIN).bin@echo BIN2UF2 $@@bin2uf2 -i $^ -o $@%.o:@echo CC $@@$(CC) $(CFLAGS) $(filter %/$(subst .o,.c,$(notdir $@)), $(SRCS)) -c -o $@directory:@$(MKDIR) $(BUILD)size: $(BUILD)/$(BIN).elf@echo size:@$(SIZE) -t $^clean:@echo clean@-rm -rf $(BUILD)-include $(wildcard $(BUILD)/*.d)