#include <stdint.h>
#define ROM_ADDR 0x2200U
void write_rom_const(void) {
volatile uint8_t *p = (volatile uint8_t *)ROM_ADDR;
*p = 0x01; // 寫入
// uint8_t val = *p; // 讀取
}
const uint8_t my_const __attribute__((section(".my_rom_section"))) = 0x01;
// 連結腳本 .ld 控制 .my_rom_section 載入地址
// .my_rom_section 0x2200 : { *(.my_rom_section) }
// C51
unsigned char code my_const _at_ 0x2200 = 0x01; // code 關鍵字放入程式碼區(ROM)
// ARM (ARMCC/ARMCLANG)
__attribute__((section(".ARM.__at_0x2200"))) const uint8_t my_const = 0x01;
// 或 #pragma arm section rodata = "MY_CONST"
#pragma location=0x2200
__root const uint8_t my_const = 0x01;
將所有需固定地址的資料/代碼集中定義在自定義段,由連結腳本 (GCC `.ld`)、散載文件 (Keil `.sct` / IAR `.icf`) 統一指定載入地址 (VMA/LMA),避免硬編碼地址散落業務代碼中。
若需在應用程式啟動前初始化特定地址 (如向量表、配置字、加密金鑰),在啟動檔 (`startup.s` / `Reset_Handler` 前) 用匯編或 C 代碼 (`__low_level_init`) 完成,再跳轉主程式。