ARM裸機(jī)之定時(shí)器中斷
int.c函數(shù)
本文引用地址:http://cafeforensic.com/article/201611/318246.htm/** init.c: 進(jìn)行一些初始化*/#include "s3c24xx.h"void disable_watch_dog(void);void clock_init(void);void memsetup(void);void copy_steppingstone_to_sdram(void);void init_led(void);void timer0_init(void);void init_irq(void);/** 關(guān)閉WATCHDOG,否則CPU會(huì)不斷重啟*/void disable_watch_dog(void){WTCON = 0; // 關(guān)閉WATCHDOG很簡(jiǎn)單,往這個(gè)寄存器寫0即可}#define S3C2410_MPLL_200MHZ ((0x5c<<12)|(0x04<<4)|(0x00))#define S3C2440_MPLL_200MHZ ((0x5c<<12)|(0x01<<4)|(0x02))/** 對(duì)于MPLLCON寄存器,[19:12]為MDIV,[9:4]為PDIV,[1:0]為SDIV* 有如下計(jì)算公式:* S3C2410: MPLL(FCLK) = (m * Fin)/(p * 2^s)* S3C2410: MPLL(FCLK) = (2 * m * Fin)/(p * 2^s)* 其中: m = MDIV + 8, p = PDIV + 2, s = SDIV* 對(duì)于本開發(fā)板,F(xiàn)in = 12MHz* 設(shè)置CLKDIVN,令分頻比為:FCLK:HCLK:PCLK=1:2:4,* FCLK=200MHz,HCLK=100MHz,PCLK=50MHz*/void clock_init(void){// LOCKTIME = 0x00ffffff; // 使用默認(rèn)值即可CLKDIVN = 0x03; // FCLK:HCLK:PCLK=1:2:4, HDIVN=1,PDIVN=1/* 如果HDIVN非0,CPU的總線模式應(yīng)該從“fast bus mode”變?yōu)?ldquo;asynchronous bus mode” */__asm__("mrc p15, 0, r1, c1, c0, 0n" /* 讀出控制寄存器 */"orr r1, r1, #0xc0000000n" /* 設(shè)置為“asynchronous bus mode” */"mcr p15, 0, r1, c1, c0, 0n" /* 寫入控制寄存器 */);/* 判斷是S3C2410還是S3C2440 */if ((GSTATUS1 == 0x32410000) || (GSTATUS1 == 0x32410002)){MPLLCON = S3C2410_MPLL_200MHZ; /* 現(xiàn)在,F(xiàn)CLK=200MHz,HCLK=100MHz,PCLK=50MHz */}else{MPLLCON = S3C2440_MPLL_200MHZ; /* 現(xiàn)在,F(xiàn)CLK=200MHz,HCLK=100MHz,PCLK=50MHz */}}/** 設(shè)置存儲(chǔ)控制器以使用SDRAM*/void memsetup(void){volatile unsigned long *p = (volatile unsigned long *)MEM_CTL_BASE;/* 這個(gè)函數(shù)之所以這樣賦值,而不是像前面的實(shí)驗(yàn)(比如mmu實(shí)驗(yàn))那樣將配置值* 寫在數(shù)組中,是因?yàn)橐?rdquo;位置無關(guān)的代碼”,使得這個(gè)函數(shù)可以在被到* SDRAM之前就可以在steppingstone中運(yùn)行*//* 存儲(chǔ)控制器13個(gè)寄存器的值 */p[0] = 0x22011110; //BWSCONp[1] = 0x00000700; //BANKCON0p[2] = 0x00000700; //BANKCON1p[3] = 0x00000700; //BANKCON2p[4] = 0x00000700; //BANKCON3p[5] = 0x00000700; //BANKCON4p[6] = 0x00000700; //BANKCON5p[7] = 0x00018005; //BANKCON6p[8] = 0x00018005; //BANKCON7/* REFRESH,* HCLK=12MHz: 0x008C07A3,* HCLK=100MHz: 0x008C04F4*/p[9] = 0x008C04F4;p[10] = 0x000000B1; //BANKSIZEp[11] = 0x00000030; //MRSRB6p[12] = 0x00000030; //MRSRB7}void copy_steppingstone_to_sdram(void){unsigned int *pdwSrc = (unsigned int *)0;unsigned int *pdwDest = (unsigned int *)0x30000000;while (pdwSrc < (unsigned int *)4096){*pdwDest = *pdwSrc;pdwDest++;pdwSrc++;}}/** LED1,LED2,LED4對(duì)應(yīng)GPB5、GPB6、GPB7、GPB8*/#define GPB5_out (1<<(5*2))#define GPB6_out (1<<(6*2))#define GPB7_out (1<<(7*2))#define GPB8_out (1<<(8*2))#define GPB5_msk (3<<(5*2))#define GPB6_msk (3<<(6*2))#define GPB7_msk (3<<(7*2))#define GPB8_msk (3<<(8*2))void init_led(void){// LED1,LED2,LED3,LED4對(duì)應(yīng)的4根引腳設(shè)為輸出GPBCON &= ~(GPB5_msk | GPB6_msk | GPB7_msk | GPB8_msk);GPBCON |= GPB5_out | GPB6_out | GPB7_out | GPB8_out;}/** Timer input clock Frequency = PCLK / {prescaler value+1} / {divider value}* {prescaler value} = 0~255* {divider value} = 2, 4, 8, 16* 本實(shí)驗(yàn)的Timer0的時(shí)鐘頻率=100MHz/(99+1)/(16)=62500Hz* 設(shè)置Timer0 0.5秒鐘觸發(fā)一次中斷:*/void timer0_init(void){TCFG0 = 99; // 預(yù)分頻器0 = 99TCFG1 = 0x03; // 選擇16分頻TCNTB0 = 62500; // 0.5秒鐘觸發(fā)一次中斷TCON |= (1<<1); // 手動(dòng)更新TCON = 0x09; // 自動(dòng)加載,清“手動(dòng)更新”位,啟動(dòng)定時(shí)器0}/** 定時(shí)器0中斷使能*/void init_irq(void){// 定時(shí)器0中斷使能INTMSK &= (~(1<<10));}中斷函數(shù)interrupt.c
#include "s3c24xx.h"void Timer0_Handle(void){/** 每次中斷令4個(gè)LED改變狀態(tài),亮滅交替*/if(INTOFFSET == 10){GPBDAT = ~(GPBDAT & (0xf << 5));}//清中斷SRCPND = 1 << INTOFFSET;INTPND = INTPND;}主函數(shù)main.c
int main(void){while(1);return 0;}
本程序根據(jù)韋東山嵌入式linux應(yīng)用開發(fā)修改,適用于mini2440做定時(shí)器中斷實(shí)驗(yàn)。
評(píng)論