首页 > 其他分享 >在keil中ARMCC v6 编译器中用C语言实现ARM cortex-M4 实现startup代码

在keil中ARMCC v6 编译器中用C语言实现ARM cortex-M4 实现startup代码

时间:2025-01-12 14:32:34浏览次数:1  
标签:__ keil attribute void startup IRQHandler 编译器 weak extern

#include "stdint.h"

#define Stack_Size 0x00000800U
#define Heap_Size 0x00000400U

uint8_t Stack_Mem[Stack_Size] __attribute__ ((section ("STACK")));
uint32_t* const __initial_sp = (uint32_t*)&Stack_Mem[Stack_Size];


uint8_t Heap_Mem[Heap_Size] __attribute__ ((section ("HEAP")));

uint32_t* const __heap_base = (uint32_t*)&Heap_Mem[0];
uint32_t* const __heap_limit = (uint32_t*)&Heap_Mem[Stack_Size];



extern void NMI_Handler(void) __attribute__((weak));
extern void HardFault_Handler(void) __attribute__((weak));
extern void MemManage_Handler(void) __attribute__((weak));
extern void BusFault_Handler(void) __attribute__((weak));
extern void UsageFault_Handler(void) __attribute__((weak));
extern void SVC_Handler(void) __attribute__((weak));
extern void DebugMon_Handler(void) __attribute__((weak));
extern void PendSV_Handler(void) __attribute__((weak));
extern void SysTick_Handler(void) __attribute__((weak));
extern void WWDGT_IRQHandler(void) __attribute__((weak));
extern void LVD_IRQHandler(void) __attribute__((weak));
extern void RTC_IRQHandler(void) __attribute__((weak));
extern void FMC_IRQHandler(void) __attribute__((weak));
extern void RCU_IRQHandler(void) __attribute__((weak));
extern void EXTI0_IRQHandler(void) __attribute__((weak));
extern void EXTI1_IRQHandler(void) __attribute__((weak));
extern void EXTI2_IRQHandler(void) __attribute__((weak));
extern void EXTI3_IRQHandler(void) __attribute__((weak));
extern void EXTI4_IRQHandler(void) __attribute__((weak));
extern void DMA0_Channel0_IRQHandler(void) __attribute__((weak));
extern void DMA0_Channel1_IRQHandler(void) __attribute__((weak));
extern void DMA0_Channel2_IRQHandler(void) __attribute__((weak));
extern void DMA0_Channel3_IRQHandler(void) __attribute__((weak));
extern void DMA0_Channel4_IRQHandler(void) __attribute__((weak));
extern void DMA0_Channel5_IRQHandler(void) __attribute__((weak));
extern void DMA0_Channel6_IRQHandler(void) __attribute__((weak));
extern void ADC0_1_IRQHandler(void) __attribute__((weak));
extern void CAN0_Message_IRQHandler(void) __attribute__((weak));
extern void CAN0_Busoff_IRQHandler(void) __attribute__((weak));
extern void CAN0_Error_IRQHandler(void) __attribute__((weak));
extern void CAN0_FastError_IRQHandler(void) __attribute__((weak));
extern void CAN0_TEC_IRQHandler(void) __attribute__((weak));
extern void CAN0_REC_IRQHandler(void) __attribute__((weak));
extern void CAN0_WKUP_IRQHandler(void) __attribute__((weak));
extern void TIMER0_BRK_UP_TRG_CMT_IRQHandler(void) __attribute__((weak));
extern void TIMER0_Channel_IRQHandler(void) __attribute__((weak));
extern void TIMER1_IRQHandler(void) __attribute__((weak));
extern void TIMER19_BRK_UP_TRG_CMT_IRQHandler(void) __attribute__((weak));
extern void TIMER19_Channel_IRQHandler(void) __attribute__((weak));
extern void I2C0_EV_IRQHandler(void) __attribute__((weak));
extern void I2C0_ER_IRQHandler(void) __attribute__((weak));
extern void I2C1_EV_IRQHandler(void) __attribute__((weak));
extern void I2C1_ER_IRQHandler(void) __attribute__((weak));
extern void SPI0_IRQHandler(void) __attribute__((weak));
extern void SPI1_IRQHandler(void) __attribute__((weak));
extern void USART0_IRQHandler(void) __attribute__((weak));
extern void USART1_IRQHandler(void) __attribute__((weak));
extern void USART2_IRQHandler(void) __attribute__((weak));
extern void EXTI10_15_IRQHandler(void) __attribute__((weak));
extern void EXTI5_9_IRQHandler(void) __attribute__((weak));
extern void TAMPER_IRQHandler(void) __attribute__((weak));
extern void TIMER20_BRK_UP_TRG_CMT_IRQHandler(void) __attribute__((weak));
extern void TIMER20_Channel_IRQHandler(void) __attribute__((weak));
extern void TIMER7_BRK_UP_TRG_CMT_IRQHandler(void) __attribute__((weak));
extern void TIMER7_Channel_IRQHandler(void) __attribute__((weak));
extern void DMAMUX_IRQHandler(void) __attribute__((weak));
extern void SRAMC_ECCSE_IRQHandler(void) __attribute__((weak));
extern void CMP_IRQHandler(void) __attribute__((weak));
extern void OVD_IRQHandler(void) __attribute__((weak));
extern void TIMER5_DAC_IRQHandler(void) __attribute__((weak));
extern void TIMER6_IRQHandler(void) __attribute__((weak));
extern void DMA1_Channel0_IRQHandler(void) __attribute__((weak));
extern void DMA1_Channel1_IRQHandler(void) __attribute__((weak));
extern void DMA1_Channel2_IRQHandler(void) __attribute__((weak));
extern void DMA1_Channel3_IRQHandler(void) __attribute__((weak));
extern void DMA1_Channel4_IRQHandler(void) __attribute__((weak));
extern void CAN1_WKUP_IRQHandler(void) __attribute__((weak));
extern void CAN1_Message_IRQHandler(void) __attribute__((weak));
extern void CAN1_Busoff_IRQHandler(void) __attribute__((weak));
extern void CAN1_Error_IRQHandler(void) __attribute__((weak));
extern void CAN1_FastError_IRQHandler(void) __attribute__((weak));
extern void CAN1_TEC_IRQHandler(void) __attribute__((weak));
extern void CAN1_REC_IRQHandler(void) __attribute__((weak));
extern void FPU_IRQHandler(void) __attribute__((weak));
extern void MFCOM_IRQHandler(void) __attribute__((weak));

extern void __main(void);
extern void SystemInit(void);

extern void __user_initial_stackheap(
uint32_t **pHeap_Mem,
uint32_t **pStack_Mem_end ,
uint32_t **pHeap_Mem_end,
uint32_t **pStack_Mem);

#define REG_SRAM_DENSITY 0x1FFFF7E0U
#define SRAM_DENSITY_MASK 0xFFFF0000U
#define SRAM_START_ADDR 0x20000000U
//#define SRAM_FILL_VALUE 0x00000000U
#define SRAM_FILL_VALUE 0xAAAAAAAAU

//must optimize level using -O1
__attribute__((noreturn)) void Reset_Handler(void)
{
register uint32_t * pRAMSize = (uint32_t *)REG_SRAM_DENSITY;
register uint32_t RAMSize = (*pRAMSize)&SRAM_DENSITY_MASK;
RAMSize = RAMSize>>16U;
RAMSize = RAMSize<<10U;

register uint32_t* PtrRAM = (uint32_t*)SRAM_START_ADDR;
do
{
*PtrRAM++ = SRAM_FILL_VALUE;
RAMSize-=4;
}
while(RAMSize>0);
SystemInit();
__main();
}


typedef void (*pFunc)(void) ;
const pFunc __Vectors[] __attribute__ ((section ("RESET"))) =
{
(pFunc)__initial_sp, // Top of Stack
Reset_Handler ,// Reset Handler
NMI_Handler ,// NMI Handler
HardFault_Handler ,// Hard Fault Handler
MemManage_Handler ,// MPU Fault Handler
BusFault_Handler ,// Bus Fault Handler
UsageFault_Handler ,// Usage Fault Handler
0 ,// Reserved
0 ,// Reserved
0 ,// Reserved
0 ,// Reserved
SVC_Handler ,// SVCall Handler
DebugMon_Handler ,// Debug Monitor Handler
0 ,// Reserved
PendSV_Handler ,// PendSV Handler
SysTick_Handler ,// SysTick Handler

/* external interrupts handler */
WWDGT_IRQHandler ,// 16:Window Watchdog Timer
LVD_IRQHandler ,// 17:LVD through EXTI Line detect
0 ,// 18:Reserved
RTC_IRQHandler ,// 19:RTC
FMC_IRQHandler ,// 20:FMC
RCU_IRQHandler ,// 21:RCU
EXTI0_IRQHandler ,// 22:EXTI Line 0
EXTI1_IRQHandler ,// 23:EXTI Line 1
EXTI2_IRQHandler ,// 24:EXTI Line 2
EXTI3_IRQHandler ,// 25:EXTI Line 3
EXTI4_IRQHandler ,// 26:EXTI Line 4
DMA0_Channel0_IRQHandler ,// 27:DMA0 Channel0
DMA0_Channel1_IRQHandler ,// 28:DMA0 Channel1
DMA0_Channel2_IRQHandler ,// 29:DMA0 Channel2
DMA0_Channel3_IRQHandler ,// 30:DMA0 Channel3
DMA0_Channel4_IRQHandler ,// 31:DMA0 Channel4
DMA0_Channel5_IRQHandler ,// 32:DMA0 Channel5
DMA0_Channel6_IRQHandler ,// 33:DMA0 Channel6
ADC0_1_IRQHandler ,// 34:ADC0 and ADC1
CAN0_Message_IRQHandler ,// 35:CAN0 Interrupt for message buffer
CAN0_Busoff_IRQHandler ,// 36:CAN0 Interrupt for bus off/bus off done
CAN0_Error_IRQHandler ,// 37:CAN0 Interrupt for error
CAN0_FastError_IRQHandler ,// 38:CAN0 Interrupt for error in fast transmission
CAN0_TEC_IRQHandler ,// 39:CAN0 Interrupt for transmit warning
CAN0_REC_IRQHandler ,// 40:CAN0 Interrupt for receive warning
CAN0_WKUP_IRQHandler ,// 41:CAN0 wakeup through EXTI Line detection interrupt
TIMER0_BRK_UP_TRG_CMT_IRQHandler ,// 42:TIMER0 Break Update Trigger and Commutation
TIMER0_Channel_IRQHandler ,// 43:TIMER0 Channel Capture Compare
TIMER1_IRQHandler ,// 44:TIMER1
TIMER19_BRK_UP_TRG_CMT_IRQHandler ,// 45:TIMER19 Break Update Trigger and Commutation
TIMER19_Channel_IRQHandler ,// 46:TIMER19 Channel Capture Compare
I2C0_EV_IRQHandler ,// 47:I2C0 Event
I2C0_ER_IRQHandler ,// 48:I2C0 Error
I2C1_EV_IRQHandler ,// 49:I2C1 Event
I2C1_ER_IRQHandler ,// 50:I2C1 Error
SPI0_IRQHandler ,// 51:SPI0
SPI1_IRQHandler ,// 52:SPI1
USART0_IRQHandler ,// 53:USART0
USART1_IRQHandler ,// 54:USART1
USART2_IRQHandler ,// 55:USART2
EXTI10_15_IRQHandler ,// 56:EXTI Line10-15
EXTI5_9_IRQHandler ,// 57:EXTI Line5-9
TAMPER_IRQHandler ,// 58:BKP Tamper
TIMER20_BRK_UP_TRG_CMT_IRQHandler ,// 59:TIMER20 Break Update Trigger and Commutation
TIMER20_Channel_IRQHandler ,// 60:TIMER20 Channel Capture Compare
TIMER7_BRK_UP_TRG_CMT_IRQHandler ,// 61:TIMER7 Break Update Trigger and Commutation
TIMER7_Channel_IRQHandler ,// 62:TIMER7 Channel Capture Compare
DMAMUX_IRQHandler ,// 63:DMANUX
SRAMC_ECCSE_IRQHandler ,// 64:Syscfg interrupt(sramc eccse)
CMP_IRQHandler ,// 65:CMP through EXTI Line
0 ,// 66:Reserved
OVD_IRQHandler ,// 67:OVD
0 ,// 68:Reserved
0 ,// 69:Reserved
TIMER5_DAC_IRQHandler ,// 70:TIMER5 and DAC
TIMER6_IRQHandler ,// 71:TIMER6
DMA1_Channel0_IRQHandler ,// 72:DMA1 Channel0
DMA1_Channel1_IRQHandler ,// 73:DMA1 Channel1
DMA1_Channel2_IRQHandler ,// 74:DMA1 Channel2
DMA1_Channel3_IRQHandler ,// 75:DMA1 Channel3
DMA1_Channel4_IRQHandler ,// 76:DMA1 Channel4
0 ,// 77:Reserved
CAN1_WKUP_IRQHandler ,// 78:CAN1 wakeup through EXTI Line detection interrupt
CAN1_Message_IRQHandler ,// 79:CAN1 Interrupt for message buffer
CAN1_Busoff_IRQHandler ,// 80:CAN1 Interrupt for bus off/bus off done
CAN1_Error_IRQHandler ,// 81:CAN1 Interrupt for error
CAN1_FastError_IRQHandler ,// 82:CAN1 Interrupt for error in fast transmission
CAN1_TEC_IRQHandler ,// 83:CAN1 Interrupt for transmit warning
CAN1_REC_IRQHandler ,// 84:CAN1 Interrupt for receive warning
FPU_IRQHandler ,// 85:FPU
MFCOM_IRQHandler , // 86:MFCOM
};


uint32_t* const __Vectors_End = (uint32_t*)&__Vectors[sizeof(__Vectors)/sizeof(pFunc)];
uint32_t __Vectors_Size =sizeof(__Vectors)/sizeof(pFunc);


#ifdef __MICROLIB
extern uint32_t* const __initial_sp;
extern uint32_t* const __heap_base;
extern uint32_t* const __heap_limit;
#else
/*
__attribute__((noinline)) void __user_initial_stackheap1(
uint32_t **pHeap_Mem,
uint32_t **pStack_Mem_end ,
uint32_t **pHeap_Mem_end,
uint32_t **pStack_Mem)
{
*pHeap_Mem = (uint32_t *) &Heap_Mem[0];
*pStack_Mem_end = (uint32_t *)((uint8_t*)&Stack_Mem[0]+ Stack_Size);
*pHeap_Mem_end = (uint32_t *)((uint8_t*)&Heap_Mem[0]+ Heap_Size);
*pStack_Mem = (uint32_t *)&Stack_Mem[0];
}
*/

__attribute__((naked)) void __user_initial_stackheap(
uint32_t **pHeap_Mem,
uint32_t **pStack_Mem_end ,
uint32_t **pHeap_Mem_end,
uint32_t **pStack_Mem)
{
// __ASM volatile ("MSR msp, %0" : : "r" (topOfMainStack) : );
__asm volatile ("MOV r0,%0" :: "r" (&Heap_Mem[0]));
__asm volatile ("MOV r1,%0" :: "r" ((uint8_t*)&Stack_Mem[0]+ Stack_Size));
__asm volatile ("MOV r2,%0" :: "r" ((uint8_t*)&Heap_Mem[0]+ Heap_Size));
__asm volatile ("MOV r3,%0" :: "r" (&Stack_Mem[0]));
}

#endif

  

标签:__,keil,attribute,void,startup,IRQHandler,编译器,weak,extern
From: https://www.cnblogs.com/CChild/p/18666940

相关文章

  • 探索 C++ Insights: 理解编译器背后的 C++ 实现
    C++Insights是什么?C++Insights是一款强大的工具,专注于揭示C++代码在编译器内部的实现细节.它能够帮助开发者深入理解模板展开,隐式类型转换,协程等特性背后的底层机制,是学习和教学现代C++的绝佳利器.C++Insights如何工作ClangBasedTool:C++Insight......
  • 为什么设置MSVC cl.exe到PATH路径,依然无法正常编译?为什么MSVC toolchain有多个cl.exe
    为什么设置MSVCcl.exe到PATH路径,依然无法正常编译?MSVC可以正常编译C/C++代码的前提,还包括INCLUDE/LIB路径等都正确配置,而这无法仅仅通过MSVCcl.exe的PATH路径来完成。VS提供了一个官方的配置MSVC环境的方式,可以通过开始菜单的"DeveloperCommandPromptforVS2019......
  • Linux开发工具--vim编辑器-gcc/g++编译器-gdb调试器
    目录1.vim编辑器 1.1.vim的基本概念1.2vim的基本操作1.3vim三个模式的命令集 插入模式命令模式 末行模式2.gcc/g++编译器2.1gcc如何完成重点概念——函数库 2.2gcc选项3.gdb调试器 3.1.开始使用 1.vim编辑器 1.1.vim的基本概念vim可以帮我们文......
  • IDA Pro 9.0 (macOS, Linux, Windows) - 强大的反汇编程序、反编译器和多功能调试器
    IDAPro9.0(macOS,Linux,Windows)-强大的反汇编程序、反编译器和多功能调试器Apowerfuldisassembler,decompilerandaversatiledebugger.Inonetool.请访问原文链接:https://sysin.org/blog/ida-pro/查看最新版。原创作品,转载请保留出处。作者主页:sysin.orgIDA......
  • 《 C++ 点滴漫谈: 十七 》编译器优化与 C++ volatile:看似简单却不容小觑
    摘要本文深入探讨了C++中的volatile关键字,全面解析其基本概念、典型用途以及在现代编程中的实际意义。通过剖析volatile的核心功能,我们了解了它如何避免编译器优化对硬件交互和多线程环境中变量访问的干扰。同时,文章分析了volatile的局限性,如缺乏线程安全保障,并介......
  • Keil5 的使用教程
    以下是Keil5的使用教程:一、安装与设置下载与安装:从Keil官方网站(https://www.keil.com/)下载KeilμVision5安装包。运行安装程序,按照提示逐步完成安装。注册与激活(如果需要):部分版本可能需要注册和激活才能使用全部功能。按照软件提示进行注册和激活操作。安装......
  • 主流C语言编译器大全(新手必看)
    目录C语言编译器桌面操作系统下的编译器嵌入式系统下的编译器C语言主流IDE大全Windows下如何选择IDE?1)VisualStudio2)DevC++3)VisualC++6.04) Code::Blocks5) TurboC6) C-FreeLinux下如何选择IDE?MacOS下如何选择IDE?C语言编译器由于C语言的......
  • .net framework 中Owin 通过启动类 Startup.cs 使用 SignalR
    1.安装SignalRNuGet包在NuGet包管理器中,搜索 Microsoft.AspNet.SignalR 并安装它2.创建SignalRHubSignalR使用Hub来处理客户端与服务器之间的通信。你可以创建一个SignalRHub类,它会处理客户端和服务器之间的实时消息传递。在你的项目中创建一个名为ChatHu......
  • MinGW-w64编译器和编译问题
    1、不同版本的MinGW带有不同GNU版本,默认支持的C++标准不一致,编译时有时会报错,查看默认支持的命令如下echo|g++-dM-E-xc++-|find"cplusplus"(windows)echo|g++-dM-E-xc++-|grep__cplusplus(linux)#define__cplusplus201703L表示默认支持标准为C++172、......
  • MinGW和MSVC编译器--C2001: 常量中有换行符
    1、两者编译的库不能通用,例如OpenCV(window)是MSVC编译的,Qt中使用MinGW编译器调用OpenCV的dll会报错。2、用MinGW编译通过的源码,换MSVC编译报错如下,主要原因是:编译器对字符的支持不同,深入了解可参考下方链接https://zhuanlan.zhihu.com/p/146543940https://blog.csdn.net/aoxues......