新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > ARM—2410中斷控制LED裸機(jī)程序

ARM—2410中斷控制LED裸機(jī)程序

作者: 時(shí)間:2016-11-11 來源:網(wǎng)絡(luò) 收藏
讀者注意:>>后面跟的是文件名,下面是代碼,程序分為幾個(gè)文件??!文件使用2410開發(fā)板調(diào)試,如換其他開發(fā)板需做部分修改!

>>一/匯編部分head.S
@******************************************************************************
@ File:head.S
@ 功能:初始化,設(shè)置中斷模式、管理模式的棧,設(shè)置好中斷處理函數(shù)
@******************************************************************************

.extern main
.text
.global _start
_start:
@******************************************************************************
@ 中斷向量,本程序中,除Reset和HandleIRQ外,其它異常都沒有使用
@******************************************************************************
b Reset

@ 0x04: 未定義指令中止模式的向量地址
HandleUndef:
b HandleUndef

@ 0x08: 管理模式的向量地址,通過SWI指令進(jìn)入此模式
HandleSWI:
b HandleSWI

@ 0x0c: 指令預(yù)取終止導(dǎo)致的異常的向量地址
HandlePrefetchAbort:
b HandlePrefetchAbort

@ 0x10: 數(shù)據(jù)訪問終止導(dǎo)致的異常的向量地址
HandleDataAbort:
b HandleDataAbort

@ 0x14: 保留
HandleNotUsed:
b HandleNotUsed

@ 0x18: 中斷模式的向量地址
b HandleIRQ

@ 0x1c: 快中斷模式的向量地址
HandleFIQ:
b HandleFIQ

Reset:
ldr sp, =4096 @ 設(shè)置棧指針,以下都是C函數(shù),調(diào)用前需要設(shè)好棧
bl disable_watch_dog @ 關(guān)閉WATCHDOG,否則CPU會(huì)不斷重啟

msr cpsr_c, #0xd2 @ 進(jìn)入中斷模式
ldr sp, =3072 @ 設(shè)置中斷模式棧指針

msr cpsr_c, #0xd3 @ 進(jìn)入管理模式
ldr sp, =4096 @ 設(shè)置管理模式棧指針,
@ 其實(shí)復(fù)位之后,CPU就處于管理模式,
@ 前面的“ldr sp, =4096”完成同樣的功能,此句可省略

bl init_led @ 初始化LED的GPIO管腳
bl init_irq @ 調(diào)用中斷初始化函數(shù),在init.c中
msr cpsr_c, #0x5f @ 設(shè)置I-bit=0,開IRQ中斷

ldr lr, =halt_loop @ 設(shè)置返回地址
ldr pc, =main @ 調(diào)用main函數(shù)
halt_loop:
b halt_loop

HandleIRQ:
sub lr, lr, #4 @ 計(jì)算返回地址
stmdb sp!, { r0-r12,lr } @ 保存使用到的寄存器
@ 注意,此時(shí)的sp是中斷模式的sp
@ 初始值是上面設(shè)置的3072

ldr lr, =int_return @ 設(shè)置調(diào)用ISR即EINT_Handle函數(shù)后的返回地址
ldr pc, =EINT_Handle @ 調(diào)用中斷服務(wù)函數(shù),在interrupt.c中
int_return:
ldmia sp!, { r0-r12,pc }^ @ 中斷返回, ^表示將spsr的值復(fù)制到cpsr

>>main.c
int main()
{
while(1);
return 0;
}

>>init.c
/*
* init.c: 進(jìn)行一些初始化
*/

#include "s3c24xx.h"

/*
* LED0,LED1,LED2對(duì)應(yīng)GPc4、GPC5、GPC6
*/

#define GPC5_mask (3<<(5*2))
#define GPC6_mask (3<<(6*2))
#define GPC7_mask (3<<(7*2))

#define GPC5_out (1<<(5*2))
#define GPC6_out (1<<(6*2))
#define GPC7_out (1<<(7*2))


/*
* KEY0對(duì)應(yīng)GPF5
*/
#define GPF5_eint (0x2<<(5*2))
#define GPF5_mask (3<<(5*2))



/*
* 關(guān)閉WATCHDOG,否則CPU會(huì)不斷重啟
*/
void disable_watch_dog(void)
{
WTCON = 0; // 關(guān)閉WATCHDOG很簡(jiǎn)單,往這個(gè)寄存器寫0即可
}

void init_led(void)
{
// LED0,LED1,LED2對(duì)應(yīng)的3根引腳設(shè)為輸出
GPCCON &= ~(GPC5_mask | GPC6_mask | GPC7_mask);
GPCCON |= GPC5_out | GPC6_out | GPC7_out;
GPCDAT |= (0x7<<5); // 所有LED熄滅
}

/*
* 初始化GPIO引腳為外部中斷
* GPIO引腳用作外部中斷時(shí),默認(rèn)為低電平觸發(fā)、IRQ方式(不用設(shè)置INTMOD)
*/
void init_irq( )
{
// key0對(duì)應(yīng)的引腳設(shè)為中斷引腳 EINT5
GPFCON &= ~GPF5_mask;
GPFCON |= GPF5_eint;



// 對(duì)于EINT5,需要在EINTMASK寄存器中使能它
EINTMASK &= ~(1<<5);

/*
* 設(shè)定優(yōu)先級(jí):
* ARB_SEL0 = 00b, ARB_MODE0 = 0: REQ1 > REQ3,即EINT0 > EINT2
* 仲裁器1、6無需設(shè)置
* 最終:
* EINT0 > EINT2 > EINT11即K2 > K3 > K4
*/
//PRIORITY = (PRIORITY & ((~0x01) | (0x3<<7))) | (0x0 << 7) ;

// EINT0、EINT2、EINT8_23使能
INTMSK &= ~(1<<4);
}



>>interrupt.c
#include "s3c24xx.h"
static void delay(int n);

void EINT_Handle()
{
unsigned long oft = INTOFFSET;

switch( oft )
{
// key0被按下
case 4:
{
GPCDAT &= ~(1<<5); // LED0點(diǎn)亮
delay(300000);
GPCDAT |= (0x7<<5); // 所有LED熄滅
break;
}
default:
break;
}

//清中斷
if( oft == 4 )
EINTPEND = (1<<5); // EINT4_7合用IRQ4
SRCPND = 1< INTPND = 1<}

static void delay(int n)
{
int i = 0;
for(i = 0; i}

>>Makefile(注意makefile編寫時(shí)候命令前面是一個(gè)制表符Table)
objs := head.o init.o interrupt.o main.o

int.bin: $(objs)
arm-linux-ld -Ttext 0x00000000 -o int_elf $^
arm-linux-objcopy -O binary -S int_elf $@
arm-linux-objdump -D -m arm int_elf > int.dis

%.o:%.c
arm-linux-gcc -Wall -O2 -c -o $@ $<

%.o:%.S
arm-linux-gcc -Wall -O2 -c -o $@ $<

clean:
rm -f int.bin int_elf int.dis *.o

>>s3c24xx.h
/* WOTCH DOG register */
#define WTCON (*(volatile unsigned long *)0x53000000)

/* SDRAM regisers */
#define MEM_CTL_BASE 0x48000000
#define SDRAM_BASE 0x30000000

/* NAND Flash registers */
#define NFCONF (*(volatile unsigned int *)0x4e000000)
#define NFCMD (*(volatile unsigned char *)0x4e000004)
#define NFADDR (*(volatile unsigned char *)0x4e000008)
#define NFDATA (*(volatile unsigned char *)0x4e00000c)
#define NFSTAT (*(volatile unsigned char *)0x4e000010)

/*GPIO registers*/
#define GPBCON (*(volatile unsigned long *)0x56000010)
#define GPBDAT (*(volatile unsigned long *)0x56000014)

#define GPCCON (*(volatile unsigned long *)0x56000020)
#define GPCDAT (*(volatile unsigned long *)0x56000024)

#define GPFCON (*(volatile unsigned long *)0x56000050)
#define GPFDAT (*(volatile unsigned long *)0x56000054)
#define GPFUP (*(volatile unsigned long *)0x56000058)

#define GPGCON (*(volatile unsigned long *)0x56000060)
#define GPGDAT (*(volatile unsigned long *)0x56000064)
#define GPGUP (*(volatile unsigned long *)0x56000068)

#define GPHCON (*(volatile unsigned long *)0x56000070)
#define GPHDAT (*(volatile unsigned long *)0x56000074)
#define GPHUP (*(volatile unsigned long *)0x56000078)



/*UART registers*/
#define ULCON0 (*(volatile unsigned long *)0x50000000)
#define UCON0 (*(volatile unsigned long *)0x50000004)
#define UFCON0 (*(volatile unsigned long *)0x50000008)
#define UMCON0 (*(volatile unsigned long *)0x5000000c)
#define UTRSTAT0 (*(volatile unsigned long *)0x50000010)
#define UTXH0 (*(volatile unsigned char *)0x50000020)
#define URXH0 (*(volatile unsigned char *)0x50000024)
#define UBRDIV0 (*(volatile unsigned long *)0x50000028)


/*interrupt registes*/
#define SRCPND (*(volatile unsigned long *)0x4A000000)
#define INTMOD (*(volatile unsigned long *)0x4A000004)
#define INTMSK (*(volatile unsigned long *)0x4A000008)
#define PRIORITY (*(volatile unsigned long *)0x4A00000c)
#define INTPND (*(volatile unsigned long *)0x4A000010)
#define INTOFFSET (*(volatile unsigned long *)0x4A000014)
#define SUBSRCPND (*(volatile unsigned long *)0x4A000018)
#define INTSUBMSK (*(volatile unsigned long *)0x4A00001c)

/*external interrupt registers*/
#define EINTMASK (*(volatile unsigned long *)0x560000a4)
#define EINTPEND (*(volatile unsigned long *)0x560000a8)


關(guān)鍵詞: ARM2410中斷控制LE

評(píng)論


技術(shù)專區(qū)

關(guān)閉