How to start external flash with SPI in esp32

Ritu21
Posts: 123
Joined: Sat Aug 04, 2018 9:58 am

How to start external flash with SPI in esp32

Postby Ritu21 » Fri Jan 25, 2019 6:17 am

Hi,

I am using an external flash for my project. Flash (ST-25P80) has 8Mb memory. Connections are as follows:

Pin no 30/Gpio no 18 -> CLK
Pin no 31/Gpio no 19 -> MISO
Pin no 37/Gpio no 23 -> MOSI
Pin no 27/Gpio no 16 -> CS

These pins are connected to VSPI.

This is the first time I am using SPI communication.I have tried writing the code to init SPI and start a simple communication. Below is the step-wise procedure I followed:

#define PIN_NUM_MISO 19 //31
#define PIN_NUM_MOSI 23 //37
#define PIN_NUM_CLK 18 //30
#define PIN_NUM_CS 16 //27
esp_err_t retn;
void spi_init() {
spi_device_handle_t spi;
spi_bus_config_t buscfg = {
.miso_io_num=PIN_NUM_MISO,
.mosi_io_num=PIN_NUM_MOSI,
.sclk_io_num=PIN_NUM_CLK,
.quadwp_io_num=-1,
.quadhd_io_num=-1,
.max_transfer_sz=0,
};
retn=spi_bus_initialize(VSPI_HOST, &buscfg, 1);
ESP_ERROR_CHECK(retn);
}
void flash_init(){
spi_device_handle_t spi;
spi_device_interface_config_t devcfg={
.clock_speed_hz=50*1000*1000,
.mode=0,
.spics_io_num=PIN_NUM_CS,
.queue_size=7,
};
retn=spi_bus_add_device(VSPI_HOST, &devcfg, &spi);
ESP_ERROR_CHECK(retn);
}

void app_main()
{
uint8_t wrbuff[8] = "ABCDEFGH";
uint8_t rdbuff[8] = {0};
spi_init();
spi_flash_init();
flash_init();
//spi_flash_erase_sector(0x10);
spi_flash_write(0x10000, (uint32_t*)wrbuff, sizeof(wrbuff));
spi_flash_read(0x10000, (uint32_t*)rdbuff, sizeof(rdbuff));
printf("SPI Read = %s\n", (uint8_t*)rdbuff);
}

Please help me to correct the code as it is not working.

Thanks
R.

User avatar
loboris
Posts: 514
Joined: Wed Dec 21, 2016 7:40 pm

Re: How to start external flash with SPI in esp32

Postby loboris » Fri Jan 25, 2019 9:38 am

You are using the SPI Flash API for write/read, which is unrelated to the SPI Master API you have used to initialize the SPI bus.
You should write your own driver (or use an existing) to access the ST-25P80, which, BTW, is not a Flash chip, but SPI EEPROM.

Ritu21
Posts: 123
Joined: Sat Aug 04, 2018 9:58 am

Re: How to start external flash with SPI in esp32

Postby Ritu21 » Fri Jan 25, 2019 12:55 pm

So, you mean I need to write driver for 25P80 ???

User avatar
loboris
Posts: 514
Joined: Wed Dec 21, 2016 7:40 pm

Re: How to start external flash with SPI in esp32

Postby loboris » Fri Jan 25, 2019 7:51 pm

Yes, it should not be too difficult to adapt an existing SPI eeprom driver or write your own based on datasheet...

Ritu21
Posts: 123
Joined: Sat Aug 04, 2018 9:58 am

Re: How to start external flash with SPI in esp32

Postby Ritu21 » Mon Jan 28, 2019 4:56 am

Hi,

Please send any link to refer.

Thanks

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot] and 258 guests