timer 예제


#include <asm/arch/irqs.h> // IRQ_OST1
#include <asm/hardware.h> // OIER_E1, ...
#include <asm/arch/pxa-regs.h>  // OIER, ...


/* TIMER */
#define OST1_TIMER_ON OIER |= OIER_E1; OSSR |= OSSR_M1; OSMR1 = OSCR+timerClkVal
#define OST1_TIMER_OFF OIER &= ~OIER_E1; OSSR |= OSSR_M1
#define SEC_CLK 3686400

static int timerPeriod=1, timerClkVal=1*SEC_CLK;


/* interrupt callback */
void ost1_interrupt(int irq, void *dev_id, struct pt_regs *regs) {
printk("os timer1 interrupt\n");
OST1_TIMER_OFF;
flag_ost1_int = 1;
}

int fnd_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg) {

       switch(cmd)
{

case 3 :
/* param : ioctl(fd, 3, timerPeriod); */
timerPeriod=arg;
timerClkVal=timerPeriod*SEC_CLK;
break;
default:
break;
}
return 0;
}


static int __init fnd_init(void) {

int result;
...
result = request_irq(IRQ_OST1, ost1_interrupt, SA_INTERRUPT, "fnd", NULL);
if (result != 0) {
printk("irq error");
return -1;
}
...

}




보드에서 insmod


[root@Xhyper255 /root]$insmod fnd.o
FND module is up...
FND_MAJOR = 253
[root@Xhyper255 /root]$
[root@Xhyper255 /root]$
[root@Xhyper255 /root]$mknod /dev/FND c 253 0




interrupt가 등록되었음을 확인할 수 있다. 19번으로 등록됨을 확인할 수 있다.

[root@Xhyper255 /root]$cat /proc/interrupts
  0:          0   eth0
  2:          0   GPIO 2-80
  3:          0   PXA USB core
  6:         42   AC97
 14:      15160   serial
 15:          9   MMC controller
 17:          0   DMA
 18:    2070369   timer
 19:          0   fnd
 25:          0   PXA PCMCIA CD
 27:          0   ads7843
 39:          0   PXA CF CD
Err:          0





fnd_app.c

/***************************************

 * Filename: fnd_app.c

 * Title: fnd Device Application

 * Desc: Implementation of system call

 ***************************************/

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <fcntl.h>


int main(void)

{

    int retn;

    int fd;


    int flag = 0;

    int menu;

short data, timerPeriod;

short fndValue;

    

    fd = open("/dev/FND", O_RDWR);

    printf("fd = %d\n", fd);

    

    if (fd<0) {

        perror("/dev/FND");

        exit(-1);

    }

    else

        printf("[APP]FND detected...\n");

    

    while(1)

    {

        printf("\n\n===== FND Test Program ====\n");

        printf(" 1. OSTimer_1 Interrupt Period Setting\n");

        printf(" 2. FND0 Read\n");

        printf(" 3. FND0 Write\n");

        printf(" 4. FND Clear\n");

        printf(" 0. Program Quit\n");

        do {

            printf(" Select Menu => ");

            scanf("%d", &menu);

        } while(menu<0 || menu>4);

        if(menu == 0) break;


        switch(menu) {

            case 1:

                printf("Input OSTimer_1 Int. Period Value(1~10) => ");

                scanf("%hd", &timerPeriod);

     ioctl(fd, 3, timerPeriod);

printf("\n[APP]OSTimer_1 Period Setting Done!!\n");

                break;

            case 2:

                printf("[APP]Wait OSTimer_1 Int...\n");

                read(fd, &data, sizeof(short));

printf("\n[APP]FND0 Value Read(%04x) Done!!\n", data);

                break;

            case 3:

                printf("Input FND0 Write Value(0~9) => ");

                scanf("%hd", &fndValue);

write(fd, &fndValue, sizeof(short));

printf("\n[APP]FND0 Value Write Done!!\n");

                break;

            case 4:

     ioctl(fd, 4, flag);

                break;

        }

    }


    close(fd);

    

    return 0;

}



 
실행결과 , 동작 잘됨


[root@Xhyper255 /root]$./fnd_app
fnd_open
fd = 3
[APP]FND detected...


===== FND Test Program ====
 1. OSTimer_1 Interrupt Period Setting
 2. FND0 Read
 3. FND0 Write
 4. FND Clear
 0. Program Quit
 Select Menu => 1
Input OSTimer_1 Int. Period Value(1~10) => 5
[FND_DD]ioctl(cmd:3)..

[APP]OSTimer_1 Period Setting Done!!


===== FND Test Program ====
 1. OSTimer_1 Interrupt Period Setting
 2. FND0 Read
 3. FND0 Write
 4. FND Clear
 0. Program Quit
 Select Menu => 2
[APP]Wait OSTimer_1 Int...
os timer1 interrupt
read data => 0000

[APP]FND0 Value Read(0000) Done!!


===== FND Test Program ====
 1. OSTimer_1 Interrupt Period Setting
 2. FND0 Read
 3. FND0 Write
 4. FND Clear
 0. Program Quit
 Select Menu =>



Posted by '김용환'
,