1.修改链接文件:
STM32F407ZGTx_FLASH.ld文件增加如下内容
①增加SRAM内存区域
/* Specify the memory areas */ MEMORY { RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K CCMRAM (xrw) : ORIGIN = 0x10000000, LENGTH = 64K FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 1024K SRAM (xrw) : ORIGIN = 0x68000000, LENGTH = 1024K }
②增加sram_date段
SECTIONS { ... .sram_data (NOLOAD) : /*不添加(NOLOAD)会导致变量初始化到FLASH*/ { *(.sram_data) /* .sram_data段 */ *(.sram_data*) /* .sram_data*段 */ } >SRAM ... }
2.代码实例
/*定义str数组到.sram_date段*/ uint8_t str[250000] __attribute__((section(".sram_data" ))); int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ //SystemClock_Config(); /* USER CODE BEGIN SysInit */ sys_stm32_clock_init(336, 8, 2, 7); /* 设置时钟,168Mhz */ led_init(); usart_init(115200); sram_init(); /* USER CODE END SysInit */ /* Initialize all configured peripherals */ /* USER CODE BEGIN 2 */ HAL_GPIO_WritePin(GPIOF, GPIO_PIN_9, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOF, GPIO_PIN_10, GPIO_PIN_RESET); /*写入Hello world!到外部sram*/ sram_write((uint8_t *)"Hello World!\r\n", 0x0000, 15); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ HAL_GPIO_TogglePin(GPIOF, GPIO_PIN_9); HAL_GPIO_TogglePin(GPIOF, GPIO_PIN_10); //sram_read(str, 0x0000, 14); printf("%s", str); printf("0x%lx\n", (uint32_t)&str);/*打印str地址*/ HAL_Delay(1000); } /* USER CODE END 3 */ }
3.编译map文件结果:
.sram_data 0x68000000 0x3d090 *(.sram_data) .sram_data 0x68000000 0x3d090 build/Core/Src/main.o 0x68000000 str