?
1:PRESERVE8:
Reguire8和Preserve8
C和匯編有8位對齊的要求,這兩個(gè)偽指令可以滿足此要求,存在REQUIRE8<——> PRESERVE8的對應(yīng)關(guān)系,但不是說有一個(gè)REQUIRE8就要有一個(gè) PRESERVE8,如果是一個(gè)c文件和一個(gè)匯編文件的調(diào)用,也就涉及一個(gè)PRESERVE8或者是一個(gè)REQUIRE8.
另外,REQUIRE8和PRESERVE8并不完成8 byte 對齊的操作,對齊由ALIGN完成。
將ADS的代碼移植到KEIL MDK上需要做的修改:
當(dāng)用戶擁有ADS遺留工程的所有源代碼時(shí),使用MDK重新編譯鏈接全部代碼是最好的解決方法,MDK中的新版本編譯工具會(huì)重新生成滿足堆棧8BYTE對齊要求的目標(biāo)文件,避免由于堆棧不對齊引起的鏈接錯(cuò)誤.
從ADS到KEIL很重要的一個(gè)修改的地方就是這里的8BYTE對齊,想要編譯通過,在startup.s里面我們必須加入PRESERVE8指令,使得寄存器8BYTE對齊.
代碼:
CODE32
PRESERVE8? ;這個(gè)在KEIL里面是必須的,要求8BYTE對齊.在ADS的啟動(dòng)代碼中就沒有.
AREA vectors,CODE,READONLY
2: ARM的處理器可工作于多種模式,下面設(shè)置個(gè)模式的一些參數(shù).
Mode_USR??????? EQU???? 0x10? 用戶模式??
Mode_FIQ??????? EQU???? 0x11? 快中斷模式
Mode_IRQ??????? EQU???? 0x12? 中斷模式
Mode_SVC??????? EQU???? 0x13? 管理模式
Mode_ABT??????? EQU???? 0x17? 中止模式
Mode_UND??????? EQU???? 0x1B? 未定義模式
Mode_SYS??????? EQU???? 0x1F? 系統(tǒng)模式
參數(shù)的由來:這里各個(gè)模式的參數(shù)是由寄存器CPSR的模式位設(shè)置M[4:0]得來的,比如這里的用戶模式,CPSR的M[4:0]設(shè)置為10000就是0x10,同理其他.詳見<
3:?
I_Bit?????????? EQU???? 0x80??????????? ; when I bit is set, IRQ is disabled
F_Bit?????????? EQU???? 0x40??????????? ; when F bit is set, FIQ is disabled
也和CPSR寄存器的設(shè)置有關(guān),這里兩位是禁止/開啟快速中斷和一般中斷的設(shè)置.
4: 各模式下定義的堆棧地址.
UND_Stack_Size? EQU???? 0x00000000
SVC_Stack_Size? EQU???? 0x00000100
ABT_Stack_Size? EQU???? 0x00000000
FIQ_Stack_Size? EQU???? 0x00000000
IRQ_Stack_Size? EQU???? 0x00000100
USR_Stack_Size? EQU???? 0x00000200
設(shè)置堆棧大小
Stack_Size????? EQU???? (UND_Stack_Size + SVC_Stack_Size + ABT_Stack_Size +?
???????????????????????? FIQ_Stack_Size + IRQ_Stack_Size + USR_Stack_Size)
??????????????? AREA??? STACK, NOINIT, READWRITE, ALIGN=3
Stack_Mem?????? SPACE?? Stack_Size
Stack_Top?????? EQU???? Stack_Mem + Stack_Size
堆棧大小的設(shè)置,各公司寫的啟動(dòng)代碼有所不同,但是不影響大局,可以借鑒一些你認(rèn)為比較簡單的啟動(dòng)代碼,然后寫自己的堆棧地址和大小設(shè)置程序.
5:堆的設(shè)置
Heap_Size?????? EQU???? 0x00000000
??????????????? AREA??? HEAP, NOINIT, READWRITE, ALIGN=3
Heap_Mem??????? SPACE?? Heap_Size
AREA Init,CODE,READONLY,ALIGN=3 //指定后面的指令為8位對齊(2的3次方)
align n
它的含義就是使得下面的代碼按一定規(guī)則對齊,.align n 指令的對齊值有兩種方案,n 或 2^n ,各種平臺(tái)最初的匯編器一般都不是gas,采取方案1或2的都很多,gas的目標(biāo)是取代原來的匯編器,必然要保持和原來匯編器的兼容,因此在gas中如何解釋 .align指令會(huì)顯得有些混亂,原因在于保持兼容。arm-linux是按照2^n的方案對齊的. ARM的.align 5就是2的5次方(32位)對齊,也就是4字節(jié)(32位)對齊.
6: AREA??? RESET, CODE, READONLY
?? ARM
下面的是ARM的代碼,不是THUMB.
7: 中斷向量表
Reset
??????? LDR???? PC, ResetAddr
??????? LDR???? PC, UndefinedAddr
??????? LDR???? PC, SWI_Addr
??????? LDR???? PC, PrefetchAddr
??????? LDR???? PC, DataAbortAddr
??????? DCD???? 0xb9205f80
??????? LDR???? PC, [PC, #-0xff0]
??????? LDR???? PC, FIQ_Addr
ResetAddr?????????? DCD???? ResetInit
UndefinedAddr?????? DCD???? Undefined
SWI_Addr??????????? DCD???? SoftwareInterrupt
PrefetchAddr??????? DCD???? PrefetchAbort
DataAbortAddr?????? DCD???? DataAbort
Nouse?????????????? DCD???? 0
IRQ_Addr??????????? DCD???? 0
FIQ_Addr??????????? DCD???? FIQ_Handler
;未定義指令
Undefined
??????? B?????? Undefined
;軟中斷
SoftwareInterrupt???
??????? B?????? SoftwareInterrupt?
;取指令中止
PrefetchAbort
??????? B?????? PrefetchAbort
;取數(shù)據(jù)中止
DataAbort
??????? B?????? DataAbort
;快速中斷
FIQ_Handler
??????? STMFD?? SP!, {R0-R3, LR}
??????? BL????? FIQ_Exception
??????? LDMFD?? SP!, {R0-R3, LR}
??????? SUBS??? PC,? LR,? #4
8: InitStack????
??????? MOV???? R0, LR
;Build the SVC stack
;設(shè)置管理模式堆棧
??????? MSR???? CPSR_c, #0xd3??
??????? LDR???? SP, StackSvc?
;Build the IRQ stack?
;設(shè)置中斷模式堆棧
??????? MSR???? CPSR_c, #0xd2
??????? LDR???? SP, StackIrq
;Build the FIQ stack
;設(shè)置快速中斷模式堆棧
??????? MSR???? CPSR_c, #0xd1
??????? LDR???? SP, StackFiq
;Build the DATAABORT stack
;設(shè)置中止模式堆棧
??????? MSR???? CPSR_c, #0xd7
??????? LDR???? SP, StackAbt
;Build the UDF stack
;設(shè)置未定義模式堆棧
??????? MSR???? CPSR_c, #0xdb
??????? LDR???? SP, StackUnd
;Build the SYS stack
;設(shè)置系統(tǒng)模式堆棧
??????? MSR???? CPSR_c, #0x5f????????????????? ;#0xdf
??????? LDR???? SP, =StackUsr
??????? MOV???? PC, R0
9:? BL? InitStack?????????????? ;初始化堆棧 Initialize the stack
??? BL? TargetResetIni;目標(biāo)板基本初始化??????????????????????????????????????? ;跳轉(zhuǎn)到c語言入口 Jump to the entry point of C program
B?????? __main
周立功啟動(dòng)代碼:
;/****************************************Copyright (c)**************************************************
;**?????????????????????????????? Guangzou ZLG-MCU Development Co.,LTD.
;**????????????????????????????????????? graduate school
;**???????????????????????????????? http://www.zlgmcu.com
;**
;**--------------File Info-------------------------------------------------------------------------------
;** File name: ???Startup.s
;** Last modified Date:? 2004-09-17
;** Last Version: ??1.0
;** Descriptions: ??The start up codes for LPC2100, including the initializing codes for the entry point of exceptions and the stacks of user tasks.
;**????Every project should have a independent copy of this file for related modifications
;**------------------------------------------------------------------------------------------------------
;** Created by: ???Chenmingji
;** Created date:?? ??2004-02-02
;** Version:????1.0
;** Descriptions: ??The original version//原著
;**
;**------------------------------------------------------------------------------------------------------
;** Modified by: ??Chenmingji
;** Modified date:??2004-09-17
;** Version:????1.01
;** Descriptions: ??Modified the bus setting to adapt for many common situations?
;**???????????????????????????? //改進(jìn)了總線,可以根據(jù)這里的更改來控制總線的速率
;**------------------------------------------------------------------------------------------------------
;** Modified by: ??Chenmingji
;** Modified date:??2004-09-17
;** Version:????1.02
;** Descriptions: ??Added codes to support the enciphering of the chip
;**???????????????????????????? //增加了芯片加密
;**------------------------------------------------------------------------------------------------------
;** Modified by: ??Chenmingji
;** Modified date:??2004-09-17
;** Version:????1.04
;** Descriptions: ??Renewed the template, added codes to support more compilers?
;**???????????????????????????? //重建模板,加入更多代碼來支持更多的編譯
;**------------------------------------------------------------------------------------------------------
;** Modified by: ??
;** Modified date:
;** Version:?
;** Descriptions:?
;**
;********************************************************************************************************/
;define the stack size
;定義堆棧的大小
SVC_STACK_LEGTH???????? EQU???????? 0
FIQ_STACK_LEGTH???????? EQU???????? 0
IRQ_STACK_LEGTH???????? EQU???????? 256
ABT_STACK_LEGTH???????? EQU???????? 0
UND_STACK_LEGTH???????? EQU???????? 0
NoInt?????? EQU 0x80
;定義處理器模式,用戶/管理/系統(tǒng)/中斷
USR32Mode?? EQU 0x10
SVC32Mode?? EQU 0x13
SYS32Mode?? EQU 0x1f
IRQ32Mode?? EQU 0x12
FIQ32Mode?? EQU 0x11
PINSEL2???? EQU 0xE002C014//定義PINSEL2地址,這個(gè)地址的值一般用戶不需要改變,和芯片的加密有關(guān)
??????????????????????????? //更改后有可能使得JTAG調(diào)試失效,進(jìn)入芯片加密狀態(tài).
BCFG0?????? EQU 0xFFE00000
BCFG1?????? EQU 0xFFE00004
BCFG2?????? EQU 0xFFE00008
BCFG3?????? EQU 0xFFE0000C//定義存儲(chǔ)器組配置寄存器
BCFG_16DEF?EQU???? 0x10000400????? ;// 16Bit Bus
BCFG_CS3??? EQU??(BCFG_16DEF | (0x01<<00) | (0x07<<05) | (0x07<<11))?? ;// 分別是IDCY/WST1/WST2對應(yīng)讀寫速率等
????????????????????????????????? ;//從第0位開始對其寫入0001,
?????????????????????????????????????????????? ;//從第5位開始寫入0111
???????????????????????????????????????????????????????????? ;//從11位開始寫入0111(0x07)/11111(0x1f)?
??? IMPORT __use_no_semihosting_swi
;The imported labels
;引入的外部標(biāo)號(hào)在這聲明
??? IMPORT? FIQ_Exception?????????????????? ;Fast interrupt exceptions handler 快速中斷異常處理程序
??? IMPORT? __main????????????????????????? ;The entry point to the main function C語言主程序入口?
??? IMPORT? TargetResetInit???????????????? ;initialize the target board 目標(biāo)板基本初始化
;The emported labels
;給外部使用的標(biāo)號(hào)在這聲明
??? EXPORT? bottom_of_heap
??? EXPORT? StackUsr
????
??? EXPORT? Reset
??? EXPORT __user_initial_stackheap
??? CODE32
??? AREA??? vectors,CODE,READONLY
??????? ENTRY
;interrupt vectors
;中斷向量表
Reset
??????? LDR???? PC, ResetAddr
??????? LDR???? PC, UndefinedAddr
??????? LDR???? PC, SWI_Addr
??????? LDR???? PC, PrefetchAddr
??????? LDR???? PC, DataAbortAddr
??????? DCD???? 0xb9205f80
??????? LDR???? PC, [PC, #-0xff0]
??????? LDR???? PC, FIQ_Addr
ResetAddr?????????? DCD???? ResetInit
UndefinedAddr?????? DCD???? Undefined
SWI_Addr??????????? DCD???? SoftwareInterrupt
PrefetchAddr??????? DCD???? PrefetchAbort
DataAbortAddr?????? DCD???? DataAbort
Nouse?????????????? DCD???? 0
IRQ_Addr??????????? DCD???? 0
FIQ_Addr??????????? DCD???? FIQ_Handler
;未定義指令
Undefined
??????? B?????? Undefined
;軟中斷
SoftwareInterrupt???
??????? B?????? SoftwareInterrupt?
;取指令中止
PrefetchAbort
??????? B?????? PrefetchAbort
;取數(shù)據(jù)中止
DataAbort
??????? B?????? DataAbort
;快速中斷
FIQ_Handler
??????? STMFD?? SP!, {R0-R3, LR}
??????? BL????? FIQ_Exception
??????? LDMFD?? SP!, {R0-R3, LR}
??????? SUBS??? PC,? LR,? #4
????????
;/*********************************************************************************************************
;** unction name ?函數(shù)名稱: ?InitStack
;** Descriptions ?功能描述: ?Initialize the stacks? 初始化堆棧
;** input parameters ?輸 入:?? ?None 無
;** Returned value?? ?輸 出 :? ?None 無
;** Used global variables 全局變量: ?None 無
;** Calling modules ?調(diào)用模塊: ?None 無
;**?
;** Created by ??作 者: ?Chenmingji 陳明計(jì)
;** Created Date ?日 期: ?2004/02/02 2004年2月2日
;**-------------------------------------------------------------------------------------------------------
;** Modified by ?修 改:?
;** Modified date ?日 期:?
;**-------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
InitStack????
??????? MOV???? R0, LR
;Build the SVC stack
;設(shè)置管理模式堆棧
??????? MSR???? CPSR_c, #0xd3??
??????? LDR???? SP, StackSvc?
;Build the IRQ stack?
;設(shè)置中斷模式堆棧
??????? MSR???? CPSR_c, #0xd2
??????? LDR???? SP, StackIrq
;Build the FIQ stack
;設(shè)置快速中斷模式堆棧
??????? MSR???? CPSR_c, #0xd1
??????? LDR???? SP, StackFiq
;Build the DATAABORT stack
;設(shè)置中止模式堆棧
??????? MSR???? CPSR_c, #0xd7
??????? LDR???? SP, StackAbt
;Build the UDF stack
;設(shè)置未定義模式堆棧
??????? MSR???? CPSR_c, #0xdb
??????? LDR???? SP, StackUnd
;Build the SYS stack
;設(shè)置系統(tǒng)模式堆棧
??????? MSR???? CPSR_c, #0x5f????????????????? ;#0xdf
??????? LDR???? SP, =StackUsr
??????? MOV???? PC, R0
;/*********************************************************************************************************
;** unction name ?函數(shù)名稱: ?ResetInit
;** Descriptions ?功能描述: ?RESET? 復(fù)位入口
;** input parameters ?輸 入:?? ?None 無
;** Returned value?? ?輸 出 :? ?None 無
;** Used global variables 全局變量: ?None 無
;** Calling modules ?調(diào)用模塊: ?None 無
;**?
;** Created by ??作 者: ?Chenmingji 陳明計(jì)
;** Created Date ?日 期: ?2004/02/02 2004年2月2日
;**-------------------------------------------------------------------------------------------------------
;** Modified by ?修 改: Chenmingji 陳明計(jì)
;** Modified date ?日 期: 2004/02/02 2004年3月3日
;**-------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
ResetInit
;初始化外部總線控制器,根據(jù)目標(biāo)板決定配置
;
;?????????? LDR???? R0, =PINSEL2
;?? IF :DEF: EN_CRP
;??????? LDR???? R1, =0x0f814910
;??? ELSE
;??????? LDR???? R1, =0x0f814914
;??? ENDIF
;??????? STR???? R1, [R0]
??????? LDR???? R0, =BCFG0
??????? LDR???? R1, =0x1000ffef?????????? ;0x00001046
??????? STR???? R1, [R0]
??????? LDR???? R0, =BCFG1
??????? LDR???? R1, =BCFG_CS3?????????????? ;0x1000ffef????????? ;0x1000ffef;;
??????? STR???? R1, [R0]
??????? LDR???? R0, =BCFG2
??????? LDR???? R1, =0x2000ffef
??????? STR???? R1, [R0]
;??????? LDR???? R0, =BCFG3
;??????? LDR???? R1, =0x00000CA0?????????? ;0x2000ffef
;??????? STR???? R1, [R0]
???????????????
??????? BL????? InitStack?????????????? ;初始化堆棧 Initialize the stack
??????? BL????? TargetResetInit???????? ;目標(biāo)板基本初始化 Initialize the target board
??????????????????????????????????????? ;跳轉(zhuǎn)到c語言入口 Jump to the entry point of C program
??????
??????? B?????? __main
;/*********************************************************************************************************
;** unction name ?函數(shù)名稱: ?__user_initial_stackheap
;** Descriptions ?功能描述: ?Initial the function library stacks and heaps, can not deleted!?? 庫函數(shù)初始化堆和棧,不能刪除
;** input parameters ?輸 入:?? ?reference by function library 參考庫函數(shù)手冊
;** Returned value?? ?輸 出 :? ?reference by function library 參考庫函數(shù)手冊
;** Used global variables 全局變量: ?None 無
;** Calling modules ?調(diào)用模塊: ?None 無
;**?
;** Created by ??作 者: ?Chenmingji 陳明計(jì)
;** Created Date ?日 期: ?2004/02/02 2004年2月2日
;**-------------------------------------------------------------------------------------------------------
;** Modified by ?
;** Modified date ?
;**-------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
__user_initial_stackheap????
??? LDR?? r0,=bottom_of_heap
;??? LDR?? r1,=StackUsr
??? MOV?? pc,lr
StackSvc?????????? DCD???? SvcStackSpace + (SVC_STACK_LEGTH - 1)* 4
StackIrq?????????? DCD???? IrqStackSpace + (IRQ_STACK_LEGTH - 1)* 4
StackFiq?????????? DCD???? FiqStackSpace + (FIQ_STACK_LEGTH - 1)* 4
StackAbt?????????? DCD???? AbtStackSpace + (ABT_STACK_LEGTH - 1)* 4
StackUnd?????????? DCD???? UndtStackSpace + (UND_STACK_LEGTH - 1)* 4
;/*********************************************************************************************************
;** unction name ?函數(shù)名稱: ?CrpData
;** Descriptions ?功能描述: ?encrypt the chip
;** input parameters ?輸 入:?? ?None 無
;** Returned value?? ?輸 出 :? ?None 無
;** Used global variables 全局變量: ?None 無
;** Calling modules ?調(diào)用模塊: ?None 無
;**?
;** Created by ??作 者: ?Chenmingji 陳明計(jì)
;** Created Date ?日 期: ?2004/03/27 2004年3月27日
;**-------------------------------------------------------------------------------------------------------
;** Modified by ?修 改:?
;** Modified date ?日 期:?
;**-------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
??? IF :DEF: EN_CRP
??????? IF? . >= 0x1fc
??????? INFO??? 1," The data at 0x000001fc must be 0x87654321. Please delete some source before this line."
??????? ENDIF
CrpData
??? WHILE . < 0x1fc
??? NOP
??? WEND
CrpData1
??? DCD???? 0x87654321????????? ;/*When the Data is 為0x87654321,user code be protected. 當(dāng)此數(shù)為0x87654321時(shí),用戶程序被保護(hù) */
??? ENDIF
????
;/* 分配堆??臻g */
??????? AREA??? MyStacks, DATA, NOINIT, ALIGN=2
SvcStackSpace????? SPACE?? SVC_STACK_LEGTH * 4? ;Stack spaces for Administration Mode 管理模式堆??臻g
IrqStackSpace????? SPACE?? IRQ_STACK_LEGTH * 4? ;Stack spaces for Interrupt ReQuest Mode 中斷模式堆??臻g
FiqStackSpace????? SPACE?? FIQ_STACK_LEGTH * 4? ;Stack spaces for Fast Interrupt reQuest Mode 快速中斷模式堆??臻g
AbtStackSpace????? SPACE?? ABT_STACK_LEGTH * 4? ;Stack spaces for Suspend Mode 中止義模式堆??臻g
UndtStackSpace???? SPACE?? UND_STACK_LEGTH * 4? ;Stack spaces for Undefined Mode 未定義模式堆棧
??????? AREA??? Heap, DATA, NOINIT
bottom_of_heap??? SPACE?? 1
??????? AREA??? Stacks, DATA, NOINIT
StackUsr
??? END
;/*********************************************************************************************************
;**??????????????????????????? End Of File
;***************************************************************************************************
評(píng)論
查看更多