ESP32S3指定变量地址

ESP32S3无法类似STM32那样使用绝对地址指定变量地址

const char __attribute__((at(0x3c020132))) testBuff[] = "test1234\n";//NG

有个不太方便的方法是要修改链接器脚本文件(.ld文件):

以下以常量举例:

1.首先要修改sections.ld.in文件,在原来的的常量段.rodata前面,添加.myrodata段;

(CMake会通过sections.ld.in生成sections.ld文件)

ESP32S3指定变量地址_第1张图片

2.使用__attribute__((section(".myrodata")))指定变量区段

使用bootloader_flash_read()从spi flash读取数据

#include 
#include 
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_flash.h"

#define START_ADDR ((uint32_t) & _rodata_start - 0X20)

typedef struct
{
    char name[16];
    uint8_t grade;
    uint8_t number;
} StudentTypedef;

__attribute__((section(".myrodata"))) const StudentTypedef student1 = 
    {
        .name = "Zhang san",
        .grade = 6,
        .number = 34,
};

extern int _rodata_start;   //
extern int _myrodata_start; //

extern esp_err_t bootloader_flash_read(size_t src, void *dest, size_t size, bool allow_decrypt);

void app_main(void)
{
    printf("Hello world!\n");

    printf("_rodata_start   addr:0X%0lx\n", (uint32_t)&_rodata_start);
    printf("_myrodata_start addr:0X%0lx\n", (uint32_t)&_myrodata_start);

    printf("--addr--------------------------\n");
    printf("student1 \taddr:0x%0lx\n", (uint32_t)&student1);
    printf("name \taddr:0x%0lx\n", (uint32_t)&student1.name);
    printf("grade \taddr:0x%0lx\n", (uint32_t)&student1.grade);
    printf("number \taddr:0x%0lx\n", (uint32_t)&student1.number);

    printf("--read flash--------------------------\n");
    uint32_t testAddr = 0x10000 + (uint32_t)&student1.name - START_ADDR;
    StudentTypedef readStudent =
        {
            .name = "----",
            .grade = 0,
            .number = 0,
        };
    printf("addr:0x%0lx\n", testAddr);
    if (bootloader_flash_read(testAddr, &readStudent, sizeof(StudentTypedef), true) != ESP_OK)
    {
        printf("bootloader_flash_read faile!\n");
    }
    printf("readStudent.name:%s\n", readStudent.name);
    printf("readStudent.grade:%d\n", readStudent.grade);
    printf("readStudent.number:%d\n", readStudent.number);

    while (1)
    {
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

由于factory应用程序在spi flash的偏移地址为0x10000,所以读spi flash时要在相对地址基础上偏移0x10000;

_rodata_start(0x3C02 0020),对应在bin文件偏移0x20,所以可以以此为参考位置计算地址。

ESP32S3指定变量地址_第2张图片

3.运行验证结果

ESP32S3指定变量地址_第3张图片

查看bin文件,.myrodata段在偏移0x120位置。

ESP32S3指定变量地址_第4张图片

 

 

你可能感兴趣的:(ESP32,单片机,嵌入式硬件,mcu)