一。环境
开发板:jz2440
系统: ubuntu12.04
编译器:arm-linux-gcc
二。中断系统
先省下。
三。直接贴代码
Makefile:
1 objs := head.o init.o interrupt.o main.o 2 3 int.bin: $(objs) 4 arm-linux-ld -Ttext 0x00000000 -o int_elf $^ 5 arm-linux-objcopy -O binary -S int_elf [email protected] 6 arm-linux-objdump -D -m arm int_elf > int.dis 7 8 %.o:%.c 9 arm-linux-gcc -Wall -O2 -c -o [email protected] $< 10 11 %.o:%.S 12 arm-linux-gcc -Wall -O2 -c -o [email protected] $< 13 14 clean: 15 rm -f int.bin int_elf int.dis *.o 16
head.S
1 @****************************************************************************** 2 @ File:head.S 3 @ 功能:初始化,设置中断模式、管理模式的栈,设置好中断处理函数 4 @****************************************************************************** 5 6 .extern main 7 .text 8 .global _start 9 _start: 10 @****************************************************************************** 11 @ 中断向量,本程序中,除Reset和HandleIRQ外,其它异常都没有使用 12 @****************************************************************************** 13 b Reset 14 15 @ 0x04: 未定义指令中止模式的向量地址 16 HandleUndef: 17 b HandleUndef 18 19 @ 0x08: 管理模式的向量地址,通过SWI指令进入此模式 20 HandleSWI: 21 b HandleSWI 22 23 @ 0x0c: 指令预取终止导致的异常的向量地址 24 HandlePrefetchAbort: 25 b HandlePrefetchAbort 26 27 @ 0x10: 数据访问终止导致的异常的向量地址 28 HandleDataAbort: 29 b HandleDataAbort 30 31 @ 0x14: 保留 32 HandleNotUsed: 33 b HandleNotUsed 34 35 @ 0x18: 中断模式的向量地址 36 b HandleIRQ 37 38 @ 0x1c: 快中断模式的向量地址 39 HandleFIQ: 40 b HandleFIQ 41 42 Reset: 43 ldr sp, =4096 @ 设置栈指针,以下都是C函数,调用前需要设好栈 44 bl disable_watch_dog @ 关闭WATCHDOG,否则CPU会不断重启 45 46 msr cpsr_c, #0xd2 @ 进入中断模式 47 ldr sp, =3072 @ 设置中断模式栈指针 48 49 msr cpsr_c, #0xd3 @ 进入管理模式 50 ldr sp, =4096 @ 设置管理模式栈指针, 51 @ 其实复位之后,CPU就处于管理模式, 52 @ 前面的“ldr sp, =4096”完成同样的功能,此句可省略 53 54 bl init_led @ 初始化LED的GPIO管脚 55 bl init_irq @ 调用中断初始化函数,在init.c中 56 msr cpsr_c, #0x5f @ 设置I-bit=0,开IRQ中断 57 58 ldr lr, =halt_loop @ 设置返回地址 59 ldr pc, =main @ 调用main函数 60 halt_loop: 61 b halt_loop 62 63 HandleIRQ: 64 sub lr, lr, #4 @ 计算返回地址 65 stmdb sp!, { r0-r12,lr } @ 保存使用到的寄存器 66 @ 注意,此时的sp是中断模式的sp 67 @ 初始值是上面设置的3072 68 69 ldr lr, =int_return @ 设置调用ISR即EINT_Handle函数后的返回地址 70 ldr pc, =EINT_Handle @ 调用中断服务函数,在interrupt.c中 71 int_return: 72 ldmia sp!, { r0-r12,pc }^ @ 中断返回, ^表示将spsr的值复制到cpsr 73
init.c
1 /* 2 * init.c: 进行一些初始化 3 */ 4 5 #include "s3c24xx.h" 6 7 /* 8 * LED1,LED2,LED4对应GPF4、GPF5、GPF6 9 */ 10 #define GPF4_out (1<<(4*2)) 11 #define GPF5_out (1<<(5*2)) 12 #define GPF6_out (1<<(6*2)) 13 14 #define GPF4_msk (3<<(4*2)) 15 #define GPF5_msk (3<<(5*2)) 16 #define GPF6_msk (3<<(6*2)) 17 18 /* 19 * S2,S3,S4对应GPF0、GPF2、GPG3 20 */ 21 #define GPF0_eint (0x2<<(0*2)) 22 #define GPF2_eint (0x2<<(2*2)) 23 #define GPG3_eint (0x2<<(3*2)) 24 25 #define GPF0_msk (3<<(0*2)) 26 #define GPF2_msk (3<<(2*2)) 27 #define GPG3_msk (3<<(3*2)) 28 29 /* 30 * 关闭WATCHDOG,否则CPU会不断重启 31 */ 32 void disable_watch_dog(void) 33 { 34 WTCON = 0; // 关闭WATCHDOG很简单,往这个寄存器写0即可 35 } 36 37 void init_led(void) 38 { 39 // LED1,LED2,LED4对应的3根引脚设为输出 40 GPFCON &= ~(GPF4_msk | GPF5_msk | GPF6_msk); 41 GPFCON |= GPF4_out | GPF5_out | GPF6_out; 42 } 43 44 /* 45 * 初始化GPIO引脚为外部中断 46 * GPIO引脚用作外部中断时,默认为低电平触发、IRQ方式(不用设置INTMOD) 47 */ 48 void init_irq( ) 49 { 50 // S2,S3对应的2根引脚设为中断引脚 EINT0,ENT2 51 GPFCON &= ~(GPF0_msk | GPF2_msk); 52 GPFCON |= GPF0_eint | GPF2_eint; 53 54 // S4对应的引脚设为中断引脚EINT11 55 GPGCON &= ~GPG3_msk; 56 GPGCON |= GPG3_eint; 57 58 // 对于EINT11,需要在EINTMASK寄存器中使能它 59 EINTMASK &= ~(1<<11); 60 61 /* 62 * 设定优先级: 63 * ARB_SEL0 = 00b, ARB_MODE0 = 0: REQ1 > REQ3,即EINT0 > EINT2 64 * 仲裁器1、6无需设置 65 * 最终: 66 * EINT0 > EINT2 > EINT11即K2 > K3 > K4 67 */ 68 PRIORITY = (PRIORITY & ((~0x01) | (0x3<<7))) | (0x0 << 7) ; 69 70 // EINT0、EINT2、EINT8_23使能 71 INTMSK &= (~(1<<0)) & (~(1<<2)) & (~(1<<5)); 72 }
interrupt.c
1 #include "s3c24xx.h" 2 3 void EINT_Handle() 4 { 5 unsigned long oft = INTOFFSET; 6 unsigned long val; 7 8 switch( oft ) 9 { 10 // S2被按下 11 case 0: 12 { 13 GPFDAT |= (0x7<<4); // 所有LED熄灭 14 GPFDAT &= ~(1<<4); // LED1点亮 15 break; 16 } 17 18 // S3被按下 19 case 2: 20 { 21 GPFDAT |= (0x7<<4); // 所有LED熄灭 22 GPFDAT &= ~(1<<5); // LED2点亮 23 break; 24 } 25 26 // K4被按下 27 case 5: 28 { 29 GPFDAT |= (0x7<<4); // 所有LED熄灭 30 GPFDAT &= ~(1<<6); // LED4点亮 31 break; 32 } 33 34 default: 35 break; 36 } 37 38 //清中断 39 if( oft == 5 ) 40 EINTPEND = (1<<11); // EINT8_23合用IRQ5 41 SRCPND = 1<<oft; 42 INTPND = 1<<oft; 43 }
main.c里面就是循环。
本代码无须链接脚本。
还有一个头文件:
1 /* WOTCH DOG register */ 2 #define WTCON (*(volatile unsigned long *)0x53000000) 3 4 /* SDRAM regisers */ 5 #define MEM_CTL_BASE 0x48000000 6 #define SDRAM_BASE 0x30000000 7 8 /* NAND Flash registers */ 9 #define NFCONF (*(volatile unsigned int *)0x4e000000) 10 #define NFCMD (*(volatile unsigned char *)0x4e000004) 11 #define NFADDR (*(volatile unsigned char *)0x4e000008) 12 #define NFDATA (*(volatile unsigned char *)0x4e00000c) 13 #define NFSTAT (*(volatile unsigned char *)0x4e000010) 14 15 /*GPIO registers*/ 16 #define GPBCON (*(volatile unsigned long *)0x56000010) 17 #define GPBDAT (*(volatile unsigned long *)0x56000014) 18 19 #define GPFCON (*(volatile unsigned long *)0x56000050) 20 #define GPFDAT (*(volatile unsigned long *)0x56000054) 21 #define GPFUP (*(volatile unsigned long *)0x56000058) 22 23 #define GPGCON (*(volatile unsigned long *)0x56000060) 24 #define GPGDAT (*(volatile unsigned long *)0x56000064) 25 #define GPGUP (*(volatile unsigned long *)0x56000068) 26 27 #define GPHCON (*(volatile unsigned long *)0x56000070) 28 #define GPHDAT (*(volatile unsigned long *)0x56000074) 29 #define GPHUP (*(volatile unsigned long *)0x56000078) 30 31 32 33 /*UART registers*/ 34 #define ULCON0 (*(volatile unsigned long *)0x50000000) 35 #define UCON0 (*(volatile unsigned long *)0x50000004) 36 #define UFCON0 (*(volatile unsigned long *)0x50000008) 37 #define UMCON0 (*(volatile unsigned long *)0x5000000c) 38 #define UTRSTAT0 (*(volatile unsigned long *)0x50000010) 39 #define UTXH0 (*(volatile unsigned char *)0x50000020) 40 #define URXH0 (*(volatile unsigned char *)0x50000024) 41 #define UBRDIV0 (*(volatile unsigned long *)0x50000028) 42 43 44 /*interrupt registes*/ 45 #define SRCPND (*(volatile unsigned long *)0x4A000000) 46 #define INTMOD (*(volatile unsigned long *)0x4A000004) 47 #define INTMSK (*(volatile unsigned long *)0x4A000008) 48 #define PRIORITY (*(volatile unsigned long *)0x4A00000c) 49 #define INTPND (*(volatile unsigned long *)0x4A000010) 50 #define INTOFFSET (*(volatile unsigned long *)0x4A000014) 51 #define SUBSRCPND (*(volatile unsigned long *)0x4A000018) 52 #define INTSUBMSK (*(volatile unsigned long *)0x4A00001c) 53 54 /*external interrupt registers*/ 55 #define EINTMASK (*(volatile unsigned long *)0x560000a4) 56 #define EINTPEND (*(volatile unsigned long *)0x560000a8)
这里面包含来基本2440所用到的寄存器的基地址,这写法值得借鉴。
时间: 2024-10-11 17:45:09