STM32F1 微控制器的链接和内存问题
Linking and memory issues for STM32F1 microcontrollers
让我们看一下STM32F103的链接描述文件:
/* Entry Point */
ENTRY(Reset_Handler)
/* Highest address of the user mode stack */
_estack = 0x20005000; /* End of 20K RAM */
/* Generate a link error if heap and stack don't fit into RAM */
_Min_Heap_Size = 0; /* Required amount of heap */
_Min_Stack_Size = 0x100; /* Required amount of stack */
/* Specify the memory areas */
MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 128K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K
MEMORY_B1 (rx) : ORIGIN = 0x60000000, LENGTH = 0K
}
/* Define output sections */
SECTIONS
{
/* The startup code goes first into FLASH */
.isr_vector :
{
. = ALIGN(4);
KEEP(*(.isr_vector)) /* Startup code */
. = ALIGN(4);
} >FLASH
/* The program code and other data goes into FLASH */
.text :
{
. = ALIGN(4);
*(.text) /* .text sections (code) */
*(.text*) /* .text* sections (code) */
*(.rodata) /* .rodata sections (constants, strings, etc.) */
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
*(.glue_7) /* Glue arm to thumb code */
*(.glue_7t) /* Glue thumb to arm code */
KEEP (*(.init))
KEEP (*(.fini))
. = ALIGN(4);
_etext = .; /* Define a global symbols at end of code */
} >FLASH
.ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH
.ARM : {
__exidx_start = .;
*(.ARM.exidx*)
__exidx_end = .;
} >FLASH
.ARM.attributes : { *(.ARM.attributes) } > FLASH
.preinit_array :
{
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP (*(.preinit_array*))
PROVIDE_HIDDEN (__preinit_array_end = .);
} >FLASH
.init_array :
{
PROVIDE_HIDDEN (__init_array_start = .);
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array*))
PROVIDE_HIDDEN (__init_array_end = .);
} >FLASH
.fini_array :
{
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP (*(.fini_array*))
KEEP (*(SORT(.fini_array.*)))
PROVIDE_HIDDEN (__fini_array_end = .);
} >FLASH
/* Used by the startup to initialize data */
_sidata = .;
/* Initialized data sections goes into RAM, load LMA copy after code */
.data : AT ( _sidata )
{
. = ALIGN(4);
_sdata = .; /* Create a global symbol at data start */
*(.data) /* .data sections */
*(.data*) /* .data* sections */
. = ALIGN(4);
_edata = .; /* Define a global symbol at data end */
} >RAM
/* Uninitialized data section */
. = ALIGN(4);
.bss :
{
/* This is used by the startup in order to initialize the .bss secion */
_sbss = .; /* Define a global symbol at BSS start */
__bss_start__ = _sbss;
*(.bss)
*(.bss*)
*(COMMON)
. = ALIGN(4);
_ebss = .; /* Define a global symbol at BSS end */
__bss_end__ = _ebss;
} >RAM
PROVIDE ( end = _ebss );
PROVIDE ( _end = _ebss );
/* User_heap_stack section, used to check that there is enough RAM left */
._user_heap_stack :
{
. = ALIGN(4);
. = . + _Min_Heap_Size;
. = . + _Min_Stack_Size;
. = ALIGN(4);
} >RAM
/* MEMORY_bank1 section, code must be located here explicitly */
/* Example: extern int foo(void) __attribute__ ((section (".mb1text"))); */
.memory_b1_text :
{
*(.mb1text) /* .mb1text sections (code) */
*(.mb1text*) /* .mb1text* sections (code) */
*(.mb1rodata) /* read-only data (constants) */
*(.mb1rodata*)
} >MEMORY_B1
/* Remove information from the standard libraries */
/DISCARD/ :
{
libc.a ( * )
libm.a ( * )
libgcc.a ( * )
}
}
我可以看到ISR vector是放在flash中的,所以如果程序需要调用存储在某个vector中的地址,它会从flash memory中读取。
第一个问题:硬件如何在读取RAM和闪存之间没有区别?为什么我需要特殊的寄存器来在代码中写入或读取闪存,而不能直接写入或读取其地址?
第二个问题关于从闪存读取比从RAM读取慢多少?如果我知道我的代码中使用最多的功能是什么,那么我是否能够将它移动到 RAM 部分以关键地加快它的执行速度?我相信MEMORY_B1
在这个脚本中是专门为此目的而制作的。
第三个问题:如果长度为0,我们如何将任何东西放入MEMORY_B1
?
最后一个问题:如果我在闪存中创建一个额外的部分,那么我可以创建一些简单的虚拟内存模拟吗?我认为这个问题的答案取决于第一个问题。
硬件不在乎,因为两个内存(实际上任何其他内存)都映射到公共地址 space。然而,这并不意味着您可以轻松地写入闪存以将其视为 RAM,因为这很慢并且会很快损坏内存(它的典型写入耐久性为 100k 周期)。此外,它只能整页擦除(对于某些 STM32 芯片可能高达 128 kB),这使得它作为 RAM 的替代品确实存在问题。
速度上的差异可以忽略不计。 运行 ARM Cortex-M 微控制器上来自 RAM 的代码会比您预期的要慢,因为 RAM 连接到不同的总线(用于数据)并且使用它执行代码需要使用较慢的 "interconnect" .
你不能。如果您想在那里放置一些东西,则必须增加内存的大小(并且 "normal RAM" 的大小 - 减少)。
通常你可以这样做,但是速度会非常慢,而且你很快就会损坏闪光灯。
1/2. 不在 STM32F1 上。只有核心速度超过 100 Mhz,闪存预取和缓存未命中才会让您付出代价。即使这个芯片也有缓存和预取。
如果在某些特殊情况下将向量 table 放入 RAM 中,使用此内核可能会产生微不足道的小利润。
然而,可能存在适用于所用访问 宽度 的硬件限制。但是这个闪光灯不受那个影响。
3. 是的,你当然可以。你可以在里面放一个文件系统。但是您可以可靠地写入闪存的温度范围是有限的。而且,由于只有一组闪存,所有 activity 都会停止,直到 erase/flash 成功。除非代码是来自 RAM 的 运行。
两个额外的注意事项是闪存 programming/erasing 可能需要几毫秒,并且您必须考虑每个 2 kB 的页面擦除,但所有闪存控制器都必须考虑。
如果您需要额外的 RAM,请在您的电路板上放置一些 SPI FRAM。
让我们看一下STM32F103的链接描述文件:
/* Entry Point */
ENTRY(Reset_Handler)
/* Highest address of the user mode stack */
_estack = 0x20005000; /* End of 20K RAM */
/* Generate a link error if heap and stack don't fit into RAM */
_Min_Heap_Size = 0; /* Required amount of heap */
_Min_Stack_Size = 0x100; /* Required amount of stack */
/* Specify the memory areas */
MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 128K
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K
MEMORY_B1 (rx) : ORIGIN = 0x60000000, LENGTH = 0K
}
/* Define output sections */
SECTIONS
{
/* The startup code goes first into FLASH */
.isr_vector :
{
. = ALIGN(4);
KEEP(*(.isr_vector)) /* Startup code */
. = ALIGN(4);
} >FLASH
/* The program code and other data goes into FLASH */
.text :
{
. = ALIGN(4);
*(.text) /* .text sections (code) */
*(.text*) /* .text* sections (code) */
*(.rodata) /* .rodata sections (constants, strings, etc.) */
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
*(.glue_7) /* Glue arm to thumb code */
*(.glue_7t) /* Glue thumb to arm code */
KEEP (*(.init))
KEEP (*(.fini))
. = ALIGN(4);
_etext = .; /* Define a global symbols at end of code */
} >FLASH
.ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH
.ARM : {
__exidx_start = .;
*(.ARM.exidx*)
__exidx_end = .;
} >FLASH
.ARM.attributes : { *(.ARM.attributes) } > FLASH
.preinit_array :
{
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP (*(.preinit_array*))
PROVIDE_HIDDEN (__preinit_array_end = .);
} >FLASH
.init_array :
{
PROVIDE_HIDDEN (__init_array_start = .);
KEEP (*(SORT(.init_array.*)))
KEEP (*(.init_array*))
PROVIDE_HIDDEN (__init_array_end = .);
} >FLASH
.fini_array :
{
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP (*(.fini_array*))
KEEP (*(SORT(.fini_array.*)))
PROVIDE_HIDDEN (__fini_array_end = .);
} >FLASH
/* Used by the startup to initialize data */
_sidata = .;
/* Initialized data sections goes into RAM, load LMA copy after code */
.data : AT ( _sidata )
{
. = ALIGN(4);
_sdata = .; /* Create a global symbol at data start */
*(.data) /* .data sections */
*(.data*) /* .data* sections */
. = ALIGN(4);
_edata = .; /* Define a global symbol at data end */
} >RAM
/* Uninitialized data section */
. = ALIGN(4);
.bss :
{
/* This is used by the startup in order to initialize the .bss secion */
_sbss = .; /* Define a global symbol at BSS start */
__bss_start__ = _sbss;
*(.bss)
*(.bss*)
*(COMMON)
. = ALIGN(4);
_ebss = .; /* Define a global symbol at BSS end */
__bss_end__ = _ebss;
} >RAM
PROVIDE ( end = _ebss );
PROVIDE ( _end = _ebss );
/* User_heap_stack section, used to check that there is enough RAM left */
._user_heap_stack :
{
. = ALIGN(4);
. = . + _Min_Heap_Size;
. = . + _Min_Stack_Size;
. = ALIGN(4);
} >RAM
/* MEMORY_bank1 section, code must be located here explicitly */
/* Example: extern int foo(void) __attribute__ ((section (".mb1text"))); */
.memory_b1_text :
{
*(.mb1text) /* .mb1text sections (code) */
*(.mb1text*) /* .mb1text* sections (code) */
*(.mb1rodata) /* read-only data (constants) */
*(.mb1rodata*)
} >MEMORY_B1
/* Remove information from the standard libraries */
/DISCARD/ :
{
libc.a ( * )
libm.a ( * )
libgcc.a ( * )
}
}
我可以看到ISR vector是放在flash中的,所以如果程序需要调用存储在某个vector中的地址,它会从flash memory中读取。
第一个问题:硬件如何在读取RAM和闪存之间没有区别?为什么我需要特殊的寄存器来在代码中写入或读取闪存,而不能直接写入或读取其地址?
第二个问题关于从闪存读取比从RAM读取慢多少?如果我知道我的代码中使用最多的功能是什么,那么我是否能够将它移动到 RAM 部分以关键地加快它的执行速度?我相信MEMORY_B1
在这个脚本中是专门为此目的而制作的。
第三个问题:如果长度为0,我们如何将任何东西放入MEMORY_B1
?
最后一个问题:如果我在闪存中创建一个额外的部分,那么我可以创建一些简单的虚拟内存模拟吗?我认为这个问题的答案取决于第一个问题。
硬件不在乎,因为两个内存(实际上任何其他内存)都映射到公共地址 space。然而,这并不意味着您可以轻松地写入闪存以将其视为 RAM,因为这很慢并且会很快损坏内存(它的典型写入耐久性为 100k 周期)。此外,它只能整页擦除(对于某些 STM32 芯片可能高达 128 kB),这使得它作为 RAM 的替代品确实存在问题。
速度上的差异可以忽略不计。 运行 ARM Cortex-M 微控制器上来自 RAM 的代码会比您预期的要慢,因为 RAM 连接到不同的总线(用于数据)并且使用它执行代码需要使用较慢的 "interconnect" .
你不能。如果您想在那里放置一些东西,则必须增加内存的大小(并且 "normal RAM" 的大小 - 减少)。
通常你可以这样做,但是速度会非常慢,而且你很快就会损坏闪光灯。
1/2. 不在 STM32F1 上。只有核心速度超过 100 Mhz,闪存预取和缓存未命中才会让您付出代价。即使这个芯片也有缓存和预取。 如果在某些特殊情况下将向量 table 放入 RAM 中,使用此内核可能会产生微不足道的小利润。
然而,可能存在适用于所用访问 宽度 的硬件限制。但是这个闪光灯不受那个影响。
3. 是的,你当然可以。你可以在里面放一个文件系统。但是您可以可靠地写入闪存的温度范围是有限的。而且,由于只有一组闪存,所有 activity 都会停止,直到 erase/flash 成功。除非代码是来自 RAM 的 运行。 两个额外的注意事项是闪存 programming/erasing 可能需要几毫秒,并且您必须考虑每个 2 kB 的页面擦除,但所有闪存控制器都必须考虑。
如果您需要额外的 RAM,请在您的电路板上放置一些 SPI FRAM。