这个程序叫做守护程序:又叫做喂狗程序。
看门狗的逻辑运算图如下:
今天,有看门狗来写了一个程序:隔一段时间来触发一个中断,每次中断来的时候,将会让板子上面的灯和蜂鸣器做出相应的反映:
首先:在头文件中将具体的寄存器声明:
1 2 #define gpiobase 0x11 3 #define GPM4CON (*(volatile unsigned long *)(gpiobase + 0x02E0)) 4 #define GPM4DAT (*(volatile unsigned long *)(gpiobase + 0x02E4)) 5 #define GPX3CON (*(volatile unsigned long *)(gpiobase + 0x0C60)) 6 #define GPX3DAT (*(volatile unsigned long *)(gpiobase + 0x0C64)) 7 #define GPD0CON (*(volatile unsigned long *)(gpiobase + 0x4A0)) 8 #define GPD0DAT (*(volatile unsigned long *)(gpiobase + 0x4A4)) 9 10 #define ICC 0x10480 11 12 #define ICCICR_CPU0 (*(volatile unsigned long *)(ICC + 0x0)) 13 #define ICCPMR_CPU0 (*(volatile unsigned long *)(ICC + 0x4)) 14 #define ICCBPR_CPU0 (*(volatile unsigned long *)(ICC + 0x8)) 15 #define ICCIAR_CPU0 (*(volatile unsigned long *)(ICC + 0xC)) 16 #define ICCEOIR_CPU0 (*(volatile unsigned long *)(ICC + 0x0010)) 17 #define ICCRPR_CPU0 (*(volatile unsigned long *)(ICC + 0x0014)) 18 #define ICCHPIR_CPU0 (*(volatile unsigned long *)(ICC + 0x0018)) 19 #define ICCABPR_CPU0 (*(volatile unsigned long *)(ICC + 0x001C)) 20 #define INTEG_EN_C_CPU0 (*(volatile unsigned long *)(ICC + 0x0040)) 21 #define ICCIIDR (*(volatile unsigned long *)(ICC + 0x00FC)) 22 23 #define ICD 0x10490 24 25 #define ICDDCR (*(volatile unsigned long *)(ICD + 0x0)) 26 #define ICDICTR (*(volatile unsigned long *)(ICD + 0x4)) 27 #define ICDIIDR (*(volatile unsigned long *)(ICD + 0x8)) 28 #define ICDISR0_CPU0 (*(volatile unsigned long *)(ICD + 0x0080)) 29 #define ICDISER0_CPU0 (*(volatile unsigned long *)(ICD + 0x0100)) 30 #define ICDISER2_CPU0 (*(volatile unsigned long *)(ICD + 0x0108)) 31 #define ICDICER0_CPU0 (*(volatile unsigned long *)(ICD + 0x0180)) 32 #define ICDISPR0_CPU0 (*(volatile unsigned long *)(ICD + 0x0200)) 33 #define ICDICPR0_CPU0 (*(volatile unsigned long *)(ICD + 0x0280)) 34 #define ICDABR0_CPU0 (*(volatile unsigned long *)(ICD + 0x0300)) 35 #define ICDIPR0_CPU0 (*(volatile unsigned long *)(ICD + 0x0400)) 36 #define ICDIPR1_CPU0 (*(volatile unsigned long *)(ICD + 0x0404)) 37 #define ICDIPR2_CPU0 (*(volatile unsigned long *)(ICD + 0x0408)) 38 #define ICDIPR3_CPU0 (*(volatile unsigned long *)(ICD + 0x040C)) 39 #define ICDIPR4_CPU0 (*(volatile unsigned long *)(ICD + 0x0410)) 40 #define ICDIPR5_CPU0 (*(volatile unsigned long *)(ICD + 0x0414)) 41 #define ICDIPR6_CPU0 (*(volatile unsigned long *)(ICD + 0x0418)) 42 #define ICDIPR7_CPU0 (*(volatile unsigned long *)(ICD + 0x041C)) 43 #define ICDIPR18_CPU0 (*(volatile unsigned long *)(ICD + 0x448)) 44 45 #define ICDIPTR0_CPU0 (*(volatile unsigned long *)(ICD + 0x0800)) 46 #define ICDIPTR1_CPU0 (*(volatile unsigned long *)(ICD + 0x0804)) 47 #define ICDIPTR18_CPU0 (*(volatile unsigned long *)(ICD + 0x0848)) 48 #define ICDSGIR (*(volatile unsigned long *)(ICD + 0x0F00)) 49 50 51 #define WTCON (*(volatile unsigned long *)0x10060) 52 #define WTDAT (*(volatile unsigned long *)0x10064) 53 #define WTCNT (*(volatile unsigned long *)0x10068) 54 #define WTCLRINT (*(volatile unsigned long *)0x1006C) 55 56 57
在主要的文件中:
1 #include"regs.h" 2 3 int (*printf)(char *, ...) = 0xc3e114d8; 4 int(*delay)(int)=0xc3e25f90; 5 6 void init_ttb(unsigned long *addr); 7 void enable_mmu(void); 8 unsigned long data_abort_init(); 9 void memcopy(unsigned long* dest,unsigned long* source,int len); 10 void do_irq(); 11 void pwm_on(void); 12 void pwm_off(void); 13 void led_on(void); 14 void led_on(void); 15 16 17 18 int main() 19 { 20 *(unsigned long *)0x66 = do_irq; 21 22 //发生异常时会进入异常模式跳转到0 4地址处理异常事件 23 unsigned long source_addr=data_abort_init(); 24 //异常事件处理函数 25 printf("swi_souce addr is %x\n",source_addr); 26 //将异常处理地址的值放到0x64 27 memcopy(0x60,source_addr,0x1); 28 29 enable_mmu(); 30 //内存映射将0x04映射到0x6004 31 32 //step 1: cpu cpsr 33 __asm__ __volatile__( 34 "mrs r0, cpsr\n" 35 "bic r0, r0, #0x80\n"//设置CPSR的I位,将IRQ位打开 36 "msr cpsr, r0\n" 37 ::: "r0" 38 ); 39 40 //step 2: GIC 41 ICCICR_CPU0 = 1;//CPU接口控制寄存器 42 ICCPMR_CPU0 = 0xff;//中断优先标志寄存器 43 44 //75 45 ICDDCR = 1; 46 //ICDIPR0_CPU0 = (0x00 << 0); 47 ICDIPR18_CPU0 = (0x0 << 24); 48 //ICDIPTR0_CPU0 = 1; 49 ICDIPTR18_CPU0 = (0x1 << 24); 50 //ICDISER0_CPU0 = (1 << 0); 51 ICDISER2_CPU0 = (1 << 11); 52 53 //step 3: interrupt source watchdog 54 WTCON = 0 (1 << 2) (3 << 3) (1 << 5) (250 << 8); 55 WTCNT = 0x8; 56 WTDAT = 0x1; 57 58 printf("welcome back! \n"); 59 60 61 } 62 63 void pwm_on(void) 64 { 65 GPD0CON &= ~0xffff; 66 GPD0CON = 0x1;//配置寄存器为2 67 GPD0DAT = 0x1;//date=0xf 68 } 69 70 void pwm_off(void) 71 { 72 GPD0CON &= ~0xffff; 73 GPD0CON = 0x0; 74 // GPD0DAT &=0x0 ;//date=0xf 75 76 } 77 void led_off(void) 78 { 79 GPM4CON &= ~0xffff;//清零 80 GPM4CON = 0x0;//03位清零 81 GPM4DAT = 0x0;//date=0xf关闭置一 82 } 83 void led_on(void) 84 { 85 GPM4CON &= ~0xffff; 86 GPM4CON = 0x1;//配置寄存器3-0--3-3全为1,全为输出模式 87 GPM4DAT &= ~0xf;//打开置0-4位为0 88 } 89 90 void do_irq() 91 { 92 unsigned long data = ICCIAR_CPU0; 93 unsigned long irq_id = data & 0x3ff; 94 unsigned long cpu_id = (data >> 10) & 0x7; 95 ICCEOIR_CPU0 = irq_id (cpu_id << 10); 96 printf("irq is %d, cpu is %d\n", irq_id, cpu_id); 97 98 pwm_on(); 99 led_on(); 100 printf("hello dog!\n"); 101 delay(6); 102 pwm_off(); 103 led_off(); 104 WTCLRINT = 0x1; 105 } 106 107 void memcopy(unsigned long* dest, unsigned long* source,int len) 108 { 109 int i=0;; 110 for(i=0;i> 20] = pa 2; 217 //2的目的是将0-2位置为10此时将是小页模式4K 218 } 219 220 //00-10 ==== 6070 221 for(va=0x00; va<=0x10; va+=0x100){ pa = va+0x60; 223 addr[va >> 20] = pa 2; 224 } 225 226 //10-14 ==== 1014 227 for(va=0x10; va<=0x14; va+=0x100){ 228 pa = va; 229 addr[va >> 20] = pa 2; 230 } 231 232 //30-40 ==== 5060 233 for(va=0x30; va<0x40; va+=0x100){ 234 pa = va + 0x20; 235 addr[va >> 20] = pa 2; 236 } 237 }
主要看主函数部分:
39
40 //step 2: GIC
41 ICCICR_CPU0 = 1;//CPU接口控制寄存器
42 ICCPMR_CPU0 = 0xff;//中断优先标志寄存器
43
44 //75
45 ICDDCR = 1;
46 //ICDIPR0_CPU0 = (0x00 << 0);
47 ICDIPR18_CPU0 = (0x0 << 24);
48 //ICDIPTR0_CPU0 = 1;
49 ICDIPTR18_CPU0 = (0x1 << 24);
50 //ICDISER0_CPU0 = (1 << 0);
51 ICDISER2_CPU0 = (1 << 11);
52
53 //step 3: interrupt source watchdog
54 WTCON = 0 (1 << 2) (3 << 3) (1 << 5) (250 << 8);
55 WTCNT = 0x8;
56 WTDAT = 0x1;
57
58 printf("welcome back! \n");
还有:
90 void do_irq()
91 {
92 unsigned long data = ICCIAR_CPU0;
93 unsigned long irq_id = data & 0x3ff;
94 unsigned long cpu_id = (data >> 10) & 0x7;
95 ICCEOIR_CPU0 = irq_id (cpu_id << 10);
96 printf("irq is %d, cpu is %d\n", irq_id, cpu_id);
97
98 pwm_on();
99 led_on();
100 printf("hello dog!\n");
101 delay(6);
102 pwm_off();
103 led_off();
104 WTCLRINT = 0x1;
105 }
其中,各个寄存器详见:1352芯片手册。
运行成功:
将会发现板子,各一段时间就会叫一次。LED会闪烁一次!
改变WTDAT的值就会修改叫的频率!
上一篇:ARM中链接寄存器LR和指令寄存器IR的关系
下一篇:ARM中的中断
推荐阅读最新更新时间:2024-03-16 14:58