SPI1 External Flash Code esp_flash_

arduic
Posts: 3
Joined: Tue Jul 25, 2023 12:59 am

SPI1 External Flash Code esp_flash_

Postby arduic » Tue Jul 25, 2023 1:15 am

I'm attempting to interface some external flash to my device using SPI1.

I understand that SPI0/1 work with internal flash and that SPI1 can be pulled out with some additional labor https://docs.espressif.com/projects/esp ... n-spi1-bus

I tried just adding IRAM_ATTR to my functions and skipping the spi_init call (since the bootloader already did that), but no luck this just caused the system to enter an infinite reboot (I'm guessing the cache is getting all screwed up causing faults).

I found the example that Espressif provides for using SPI1 external https://github.com/espressif/esp-idf/bl ... i_eeprom.c but this seems to completely implement the EEPROM interface within the code and marks a ton of support libraries as noflash in the linker.

If I want to inherit all of the flash code already written and be close to the flash example https://github.com/espressif/esp-idf/bl ... ple_main.c but using external flash off of SPI1 do I need to mark all the support libraries as noflash? Or alternatively write a FLASH interface from scratch (this option is not terrible but I would prefer to know before doing the work)?

Thank you for any help provided.

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

Re: SPI1 External Flash Code esp_flash_

Postby ESP_igrr » Tue Jul 25, 2023 11:26 am

Just to make sure I understand what you are trying to achieve: do you intend to have two SPI NOR flash chips on SPI1 bus? If yes, do you intend to use the second chip just for data storage or for code execution as well? Also, what size of flash do you intend to connect? (Would it be feasible to get a module with larger flash size, in the first place, to not have to connect an external one?)

arduic
Posts: 3
Joined: Tue Jul 25, 2023 12:59 am

Re: SPI1 External Flash Code esp_flash_

Postby arduic » Tue Jul 25, 2023 3:39 pm

So this is possibly a miss understanding on my part (tell me if it is), but I thought the ESP32 had internal flash which is what SPI0/1 was connected to.

The board I am working with has an external W25Q32JVZPIG chip which is connected on SPI1. I wanted to use this chip to hold ~500KB of data.

I thought my program was stored on flash inside the ESP32 is that wrong and the code lives on that same SPI FLASH chip?

Using SPI0 I believe I just need to not call init and pass NULL as the handler correct?

ESP_Sprite
Posts: 9312
Joined: Thu Nov 26, 2015 4:08 am

Re: SPI1 External Flash Code esp_flash_

Postby ESP_Sprite » Wed Jul 26, 2023 3:54 am

arduic wrote:
Tue Jul 25, 2023 3:39 pm
So this is possibly a miss understanding on my part (tell me if it is), but I thought the ESP32 had internal flash which is what SPI0/1 was connected to.
Some versions of the chip do, some of them need external flash. The type number on the chip will tell you which it is.
The board I am working with has an external W25Q32JVZPIG chip which is connected on SPI1. I wanted to use this chip to hold ~500KB of data.
The fact that it's connected to SPI1 makes it very, very likely that that chip is the only flash in the system, and your application normally goes there.

If you also need a place to store data, rather than trying to talk to the flash yourself, I'd suggest creating a partition for that and using the partition API to read/write data from/to it. It's easier and better integrated than doing the SPI communications yourself. Alternatively, on an even higher level, ESP-IDF supports creating a file system on top of a partition in main flash as well, allowing you to simply read and write files using the standard POSIX api.

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

Re: SPI1 External Flash Code esp_flash_

Postby ESP_igrr » Wed Jul 26, 2023 6:25 am

If that's the only flash chip connected to SPI1, then you can indeed use esp_flash functions, passing a NULL argument instead of the chip handle. Or, even better, as Sprite suggests — use esp_partition APIs. Please check some of the examples in IDF under examples/storage/ directory for this.

arduic
Posts: 3
Joined: Tue Jul 25, 2023 12:59 am

Re: SPI1 External Flash Code esp_flash_

Postby arduic » Wed Jul 26, 2023 11:59 am

Ah yep I just checked my specific chip and sure enough no internal flash. Well that's my gotcha for just assuming it had internal flash haha.

I actually was already planning on implementing the partitions as you suggested it seems incredibly simple which is nice now that I can just use the NULL flash handler.

The only thing I want to make sure of. If I create a data partition on the same FLASH as the code the bootloader grabs from I assume there is some checks in the bootloader to only grab some partition names/regions. Is that information documented anywhere?

ESP_Sprite
Posts: 9312
Joined: Thu Nov 26, 2015 4:08 am

Re: SPI1 External Flash Code esp_flash_

Postby ESP_Sprite » Thu Jul 27, 2023 1:46 am

Yes. You define your partitions in the partition table which gets generated from a CSV file. In that partition table, each partition gets a name, type and subtype. The bootloader will only look at partitions with type=app (which is the same as type=0), so if you give your data partition a different type, it'll ignore it.

Who is online

Users browsing this forum: Addi2438 and 72 guests