Showing results for 
Search instead for 
Did you mean: 

Reading data from external flash using SPI - STM32L4xx with AT45DB041E



I am trying to implement a bootloader, which will take a look each time it starts to an external flash memory (AT45DB041E) to see if something is written into it. If the external flash contains new data I need to read that data an write it to my internal flash memory. I am using STM32L4xx microcontroller and I have correctly implement the SPI peripheral in order to communicate with the external flash since I am able to read its manufacturer ID. However, I don't know how to use the read function that I have implemented. Before continue let me tell you that, this external flash is written by a LoRa module and contains a binary data from cloud which is actually a new firmware. All I need to do is to manage some how to read those data from the external flash but I don't know how to start. What buffer size should I use when I am using the at45db_read function which I have implemented? Since the external flash memory is 4MBIT I can't read it all, so where to start reading data? Is that a proper way to do (Using buffer to store data)? 

This is my function where I have implemented based on datasheet of the external flash.





void at45db_read_data(uint32_t address, uint8_t *data, uint16_t size)
	uint8_t address_bytes[4];
	uint8_t opcode = CONTINUOUS_ARRAY_READ_H_MODE2;

	/*Set the address bytes*/
	address_bytes[0] = ((address >> 16) & 0xFF);
	address_bytes[1] = ((address >>  & 0xFF);
	address_bytes[2] = (address & 0xFF);
	address_bytes[3] = 0;

	/*Select the device*/

	/*Transmit the proper opcode*/
	SPIx_transmit(SPI_PERIPH, &opcode, 1);

	/*Release the slave device*/

	/*Select the device*/

	/*Transmit the address bytes*/
	SPIx_transmit(SPI_PERIPH, address_bytes, 4);

	/*Receive the data from the external flash memory*/
	SPIx_receive(SPI_PERIPH, data, size);

	/*Release the slave device*/





Is that the proper way or I maybe use another technic for that?

ST Employee

Hello @ngrigoriadis ,

I apologize for my late reply.

The STM32CubeProgrammer’s External Loader is a feature that allows a direct access to external memories by STM32CubeProgrammer and even by the STM32CubeIDE to read, program, and erase data without the use of any additional tool other than a regular STLINK and even without ever changing the internal flash memory of the STM32. 

May these FAQs can help you:

Also you can take a look at How to play Audio from external Flash memory, In this article, we demonstrated how to create a driver using the SPI interface, which is available in all STM32 microcontrollers, to access an external serial flash memory. We went through a sample firmware implementation that can be easily tailored to any other STM32. We also went through the usage of SPI flash driver APIs created to implement the memory write/read operations and store the audio files.

How to play Audio files from External Flash memory using STM32 Part1

How to play Audio files from External Flash memory using STM32 Part2

How to play Audio files from External Flash memory using STM32 Part3

I hope this help you.


To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Well you're definitely going to need to figure something out.

There should be examples within CubeL4 illustrating how to erase and write the Internal FLASH memory.

How you store and identify what data you have stashed in the External memory is on you, and whomever is on your team.

I'd advise splitting the L4 Internal memory into two sections, one with a smaller loader which can manage the External FLASH, and make a determination if there is an update, and a second section which contains your application code. The loader can authenticate / validate the application code before jumping to it, or starting an update.

An External Loader, in the STM32 Cube Programmer sense, is only needed if you plan on writing content into the External FLASH from your desktop, or at the factory.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..