Cortex-M3 - Programming STM32-Discovery using GNU tools Startup code
来源:互联网 发布:最优化理论与方法答案 编辑:程序博客网 时间:2024/06/06 00:52
Start up code is run just after microcontroller is reset and is executed before main program. As linker script, startup code usually is implemented as universal code for all same microcontroller type. So usually you don’t need to write one from scratch. Anyway it is good to know what happens there anyway.
As we said linker script has to go along with startup code. This means that it will initialize MCU automatically according to data defined in linker. Startup code initializes variables, copies defined variables from Flash to RAM, initializes stack and then gives resources to main program. You will find that startup codes usually are written in assembly language, but this can also be done in C which is easier to read and modify if necessary. First of all in linker script we have pointed entry point to startup with following command:
ENTRY(handler_reset)
So in startup code this will be first function called.
void
handler_reset(
void
)
{
unsigned
long
*source;
unsigned
long
*destination;
// Copying data from Flash to RAM
source = &_data_flash;
for
(destination = &_data_begin; destination < &_data_end;)
{
*(destination++) = *(source++);
}
// default zero to undefined variables
for
(destination = &_bss_begin; destination < &_bss_end;)
{
*(destination++) = 0;
}
// starting main program
main();
}
Startup code takes source and destination addresses from linker script as external variables:
extern
unsigned
long
_data_flash;
extern
unsigned
long
_data_begin;
extern
unsigned
long
_data_end;
extern
unsigned
long
_bss_begin;
extern
unsigned
long
_bss_end;
extern
unsigned
long
_stack_end;
These we defined when described sections. So the only thing left for startup code is take values from source addresses in Flash memory and copy them to destination addresses in RAM. Also variables stored in .bss section are defaulted to zero values. So no need to null them in source when used.
Next step is that startup does is to allocate exception handler vector table. Due to ARM Cortex architecture the first address in vector table is used to store address of stack end. This is convenient and efficient way to define it.
__attribute__ ((section(
".interrupt_vector"
)))
void
(*
const
table_interrupt_vector[])(
void
) =
{
(
void
*) &_stack_end,
// 0 - stack
handler_reset,
// 1
handler_default,
// 2
handler_default,
// 3
handler_default,
// 4
handler_default,
// 5
handler_default,
// 6
0,
// 7
0,
// 8
0,
// 9
0,
// 10
handler_default,
// 11
handler_default,
// 12
0,
// 13
handler_default,
// 14
handler_default,
// 15
// peripherals
handler_default,
// 0
handler_default,
// 1
handler_default,
// 2
handler_default,
// 3
handler_default,
// 4
-
//-
handler_default,
// 59
handler_default
// 60
};
In linker script we defined that “.interrupt_vector” section is starting at 0×00000000 address so stack pointer is located at 0×00000000 address of Flash. Then goes reset handler which then copies variable data to RAM and nulls undefined variables and then it is over with startup code and all resources are alocated to main() routine.
void
handler_default(
void
)
{
while
(1)
{
//loop
}
}
handler default routine simply handles unexpected interrupts and puts MCU to endless loop. This is very simplified version of linker script and startup code. Ir explains thing pretty well I guess. For starters probably it is best to find working example of Arm Cortex M3 GCC code copy linker script and startup code as they are and focus on software writing. Barely you’ll need to change these soon.
2 Comments so far
- Cortex-M3 - Programming STM32-Discovery using GNU tools Startup code
- Programming STM32-Discovery using GNU tools. Startup code
- Programming STM32-Discovery using GNU tools. Linker script
- Cortex M3 - STM32 Developing with GCC Tools
- STM32(Cortex-M3)中断优先级
- STM32 中断优先级(Cortex-M3)
- 如何使用GNU编译器GCC来编译Cortex-M3?GCC + STM32
- cortex-M3 GNU开发环境搭建
- STM32(Cortex-M3)中的优先级概念
- STM32(Cortex-M3)中的优先级概念
- STM32(Cortex-M3)中优先级的概念
- STM32(Cortex-M3)中的优先级概念
- STM32(Cortex-M3)中的优先级概念
- STM32(Cortex-M3)中的优先级概念
- STM32(Cortex-M3)中的优先级分组理解
- STM32(Cortex-M3)中的优先级理解
- STM32(Cortex-M3)中优先级的概念
- STM32 Cortex M3 GPIO学习笔记
- Request.ServerVariables参数
- 向SQL Server2005数据库中还原备份*.bak文件
- Android gravity属性详解
- 如何运行php文件
- php与mysql连接空框架
- Cortex-M3 - Programming STM32-Discovery using GNU tools Startup code
- 华为机试题(3)
- oracle pl/sql应用解析以及存储过程对应到java中的调用方法
- vim 技巧积累
- c#命名规则
- 数学之美系列 4 -- 怎样度量信息?
- HTTP 协议详解2
- c#关键字
- tracert用法详解
hi,
i am new to embeddeds system and i was trying since one month to make a linker script and startup file for Arm7tdmi – TMS470R1B1M chip, to be compiled under GNU GCC ARM Non-EABI tool chain in code-sourcery lite version.
I would be great help if you provide the startup and linker file for above which can work with interrupts also.
If you can’t, it would be a great help if you just provide what changes has to be made to the linker and startup files explained by you on this webpage.
Thanks.