How to start external flash with SPI in esp32
Posted: 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.
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.