ESP8266读写flash的实现

创建结构体

struct startup_config{
char baudrate[4];
char ap_ssid[32];
char ip[16];
char sta_ssid[32];
char sta_pwd[64];
};
struct startup_config config;

注意: 这里的结构体中每个字段必须是4字节的整数倍

写入flash

void write_config(void) {
spi_flash_erase_sector(0x3F);
spi_flash_write(0x3F * 4096, (uint32 *)&config, sizeof(config));
}

读取flash内容

void read_config(void) {
os_memset(&config, 0, sizeof(config));
spi_flash_read(0x3F * 4096, (uint32 *)&config, sizeof(config));
}

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是 ESP32 IDF 中 Flash 的完整代码示例: ```c #include <stdio.h> #include "esp_log.h" #include "esp_system.h" #include "nvs_flash.h" #include "driver/gpio.h" #include "driver/spi_master.h" #include "esp_partition.h" static const char* TAG = "flash-example"; #define SPI_DMA_CHAN 2 #define FLASH_SECTOR_SIZE 0x1000 #define SPI_FLASH_MOSI_PIN 23 #define SPI_FLASH_MISO_PIN 19 #define SPI_FLASH_CLK_PIN 18 #define SPI_FLASH_CS_PIN 5 // Function to initialize SPI bus static void spi_init() { // Define bus configuration spi_bus_config_t buscfg = { .miso_io_num = SPI_FLASH_MISO_PIN, .mosi_io_num = SPI_FLASH_MOSI_PIN, .sclk_io_num = SPI_FLASH_CLK_PIN, .quadwp_io_num = -1, .quadhd_io_num = -1, .max_transfer_sz = 0 }; // Define device configuration spi_device_interface_config_t devcfg = { .clock_speed_hz = 10*1000*1000, // Clock out at 10 MHz .mode = 0, // SPI mode 0 .spics_io_num = SPI_FLASH_CS_PIN, // CS pin .queue_size = 7, // We want to be able to queue 7 transactions at a time .pre_cb = NULL, // No pre-transfer callback .post_cb = NULL, // No post-transfer callback .flags = SPI_DEVICE_NO_DUMMY }; // Initialize the SPI bus esp_err_t ret = spi_bus_initialize(VSPI_HOST, &buscfg, SPI_DMA_CHAN); ESP_ERROR_CHECK(ret); // Attach the flash chip to the SPI bus spi_device_handle_t spi_handle; ret = spi_bus_add_device(VSPI_HOST, &devcfg, &spi_handle); ESP_ERROR_CHECK(ret); } // Function to read from flash static void read_flash() { ESP_LOGI(TAG, "Reading flash..."); // Open flash partition const esp_partition_t* partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "my_partition"); if (!partition) { ESP_LOGE(TAG, "Partition not found"); return; } // Initialize SPI bus spi_init(); // Set up transfer spi_transaction_t t = { .flags = SPI_TRANS_USE_RXDATA | SPI_TRANS_USE_TXDATA, .cmd = 0x03, // Read command .addr = 0x0, // Start address of read .length = 8*FLASH_SECTOR_SIZE // Read data size }; // Read data from flash uint8_t* data = malloc(FLASH_SECTOR_SIZE); t.rx_buffer = data; for (int i = 0; i < partition->size; i += FLASH_SECTOR_SIZE) { t.addr = i; esp_err_t ret = spi_device_polling_transmit(spi_handle, &t); ESP_ERROR_CHECK(ret); ESP_LOGI(TAG, "Data at address 0x%x:", i); for (int j = 0; j < FLASH_SECTOR_SIZE; j++) { printf("%02x ", data[j]); } printf("\n"); } // Clean up free(data); spi_bus_remove_device(spi_handle); spi_bus_free(VSPI_HOST); } // Function to write to flash static void write_flash() { ESP_LOGI(TAG, "Writing flash..."); // Open flash partition const esp_partition_t* partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "my_partition"); if (!partition) { ESP_LOGE(TAG, "Partition not found"); return; } // Initialize SPI bus spi_init(); // Erase flash sector esp_err_t ret = spi_flash_erase_sector(partition->address); ESP_ERROR_CHECK(ret); // Set up transfer spi_transaction_t t = { .flags = SPI_TRANS_USE_RXDATA | SPI_TRANS_USE_TXDATA, .cmd = 0x02, // Write command .addr = 0x0, // Start address of write .length = 8*FLASH_SECTOR_SIZE // Write data size }; // Write data to flash uint8_t* data = malloc(FLASH_SECTOR_SIZE); for (int i = 0; i < FLASH_SECTOR_SIZE; i++) { data[i] = i % 255; } t.tx_buffer = data; ret = spi_device_polling_transmit(spi_handle, &t); ESP_ERROR_CHECK(ret); // Clean up free(data); spi_bus_remove_device(spi_handle); spi_bus_free(VSPI_HOST); } void app_main() { // Initialize NVS esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } ESP_ERROR_CHECK(ret); // Write data to flash write_flash(); // Read data from flash read_flash(); } ``` 这个示例代码演示了如何使用 ESP32 的 SPI 总线来 Flash。其中,`spi_init()` 函数初始化了 SPI 总线,`read_flash()` 函数从 Flash取数据,`write_flash()` 函数将数据Flash。在这个示例代码中,我们使用了 SPI 总线连接的 Flash,但是也可以连接其他的 SPI 设备。同时,我们使用了 ESP-IDF 中提供的 `spi_device_polling_transmit()` 函数来进行数据的。如果你需要进行更高级的操作,可以使用 `spi_device_queue_trans()` 函数来设置队列,并使用回调函数来处理数据。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值