Using SPI Flash API

MohamedTrabelsi
Posts: 4
Joined: Fri Nov 03, 2023 4:07 pm

Using SPI Flash API

Postby MohamedTrabelsi » Thu Jan 25, 2024 9:17 am

I want to read / write flash memory of the esp32c6 on specific addresses to store some configuration data so I used the SPI Flash API functions but I got an error Failed to write to flash and Failed to read from flash and the error code is 258
here is the code

Code: Select all

#include "nvs_flash.h"
#include "esp_flash.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_spi_flash.h"
#include "esp_system.h"
#include "esp_log.h"
#include "esp_flash_spi_init.h"
esp_err_t write_to_flash(uint32_t address, const void *data, uint32_t length)
{
    esp_err_t ret = esp_flash_write(NULL, address, data, length);
    if (ret != ESP_OK)
    {
        ESP_LOGE(TAG, "Failed to write to flash, error %d", ret);
    }
    return ret;
}

esp_err_t read_from_flash(uint32_t address, void *data, uint32_t length)
{
    esp_err_t ret = esp_flash_read(NULL, address, data, length);
    if (ret != ESP_OK)
    {
        ESP_LOGE(TAG, "Failed to read from flash, error %d", ret);
    }
    return ret;
}


uint32_t calculate_flash_address(const char *key)
{
    uint32_t hash = 5381;
    int c;

    while ((c = *key++))
    {
        hash = ((hash << 5) + hash) + c;
    }

    // Assuming flash chip size is 4 MB
    uint32_t flash_size = 4 * 1024 * 1024;
    uint32_t address = 0x9000 + (hash % flash_size);

    if (address >= flash_size)
    {
        // Handle invalid address, perhaps print an error message
        ESP_LOGE(TAG, "Invalid flash address: %08lx", address);

    }

    return address;
}

void write_to_nvs(const char *key_prefix, int32_t value) {
    // Calculate the address based on the key
    uint32_t address = calculate_flash_address(key_prefix);

    write_to_flash(address, &value, sizeof(value));
}

int32_t read_from_nvs(const char *key_prefix) {
    // Calculate the address based on the key
    uint32_t address = calculate_flash_address(key_prefix);

    int32_t value;
    read_from_flash(address, &value, sizeof(value));
    return value;
}

void check_flag() {
    const char *key = "flag";
    uint32_t address = calculate_flash_address(key);

    int32_t value;
    read_from_flash(address, &value, sizeof(value));

    if (value == 0) {
        // Key doesn't exist, create it with a default value
        printf("Key not found, creating with default value\n");
        write_to_flash(address, &value, sizeof(value));
    }
}

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

Re: Using SPI Flash API

Postby ESP_Sprite » Thu Jan 25, 2024 9:32 am

Yeah, don't do that; flash also stores your program and esp-idf doesn't like you writing to the running program or any other dangerous region. If you insist on rolling your own solution for this, use a custom partition table with a partition dedicated to your flash shenanigans and use the partition API to write to that. Another suggestion, if I may: don't try to reinvent the NVS library already included with ESP-IDF.

Who is online

Users browsing this forum: No registered users and 127 guests