실습
윗 코드 재사용 하겠으~
make
pi@pi08:/mnt/ubuntu_nfs $ sudo mknod /dev/ledkey c 230 0
pi@pi08:/mnt/ubuntu_nfs $ sudo insmod ledkey_dev.ko
app 실행할 때 sudo 주기 귀찮으니까, others에게 실행권한 주자
pi@pi08:/mnt/ubuntu_nfs $ sudo chmod 666 /dev/ledkey_dev
pi@pi08:/mnt/ubuntu_nfs $ ./ledkey_app 0x66
이 코드의 문제점은 usleep이 없으므로 계속 무한루프를 돌기 때문에 cpu 사용량이 매우 높음.
이걸 intterupt가 적용된 코드로 변경해서 낮춰보자
최종 코드
gpioKeyIrqInit
gpio 핀을 interrupt에 등록하는거고
gpioKeyIrqRequest
등록된 gpio핀을 falling/ rising edge, level edge 등 어떤 방식으로 detect 할지 설정하는 거고
level edge는 계속 누르고 있으면 계속 호출되므로 잘 안씀.
request_irq(key_irq[i],key_isr, IRQF_TRIGGER_RISING, irqName[i],NULL);
gpioKeyIrqFree
테이블에 등록된 gpio 핀을 등록 해제하는거임.
irq free 할때 두번 째 매개변수가 NULL 인데 request_irq의 마지막 매개변수랑 세트로 등록해줘야됨.
irqreturn_t key_isr(int irq, void* data)
ubuntu@ubuntu8:~/pi_bsp/drivers/p369_ledkey_int$ cat ledkey_app.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#define DEVICE_FILENAME "/dev/ledkey_dev"
void print_OX(unsigned char);
int main(int argc,char * argv[])
{
char buff = 0;
char oldBuff = 0;
int dev;
int ret;
unsigned long val;
if(argc < 2)
{
printf("Usage : %s ledValue[0x00~0xff]\n",argv[0]);
return 1;
}
val = strtoul(argv[1],0,16);
if(val<0 || 0xff<val)
{
printf("Usage : %s ledValue[0x00~0xff]\n",argv[0]);
return 2;
}
buff = val;
dev = open( DEVICE_FILENAME, O_RDWR|O_NDELAY );
if(dev<0)
{
perror("open()");
return 2;
}
ret = write(dev,&buff,sizeof(buff));
if(ret < 0)
{
perror("write()");
return 3;
}
buff = 0;
do {
read(dev,&buff,sizeof(buff));
if(oldBuff != buff)
{
if(buff != 0)
{
printf("key : %d\n",buff);
print_OX(buff);
write(dev,&buff,sizeof(buff));
if(buff == 8) //key:8
break;
}
oldBuff = buff;
}
} while(1);
close(dev);
return 0;
}
void print_OX(unsigned char led)
{
int i;
led = 1 << led-1;
puts("1:2:3:4:5:6:7:8");
for(i=0;i<=7;i++)
{
if(led & (0x01 << i))
putchar('O');
else
putchar('X');
if(i < 7 )
putchar(':');
else
putchar('\n');
}
return;
}
ubuntu@ubuntu8:~/pi_bsp/drivers/p369_ledkey_int$ cat ledkey_dev.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#define DEBUG 1
#define LEDKEY_DEV_NAME "ledkey_dev"
#define LEDKEY_DEV_MAJOR 230
#define OFF 0
#define ON 1
#define GPIOLEDCNT 8
#define GPIOKEYCNT 8
static int gpioLed[GPIOLEDCNT] = {6,7,8,9,10,11,12,13};
static int gpioKey[GPIOKEYCNT] = {16,17,18,19,20,21,22,23};
static int key_irq[8] = {0};
static int keyNumber = 0;
static int gpioLedInit(void);
static void gpioLedSet(long);
static void gpioLedFree(void);
static int gpioKeyInit(void);
//static int gpioKeyGet(void);
static void gpioKeyFree(void);
static int gpioKeyIrqInit(void);
static void gpioKeyIrqFree(void);
irqreturn_t key_isr(int irq, void *data)
{
int i;
for(i=0;i<GPIOKEYCNT;i++)
{
if(irq == key_irq[i])
{
keyNumber = i+1;
break;
}
}
#if DEBUG
printk("key_isr() irq : %d, KeyNumber : %d\n",irq, keyNumber);
#endif
return IRQ_HANDLED;
}
static int gpioLedInit(void)
{
int i;
int ret=0;
char gpioName[10];
for(i=0;i<GPIOLEDCNT;i++)
{
sprintf(gpioName,"led%d",i);
ret = gpio_request(gpioLed[i],gpioName);
if(ret < 0) {
printk("Failed gpio_request() gpio%d error \n",i);
return ret;
}
ret = gpio_direction_output(gpioLed[i],OFF);
if(ret < 0) {
printk("Failed gpio_direction_output() gpio%d error \n",i);
return ret;
}
}
return ret;
}
static void gpioLedSet(long val)
{
int i;
for(i=0;i<GPIOLEDCNT;i++)
{
gpio_set_value(gpioLed[i],(val>>i) & 0x1);
}
}
static void gpioLedFree(void)
{
int i;
for(i=0;i<GPIOLEDCNT;i++)
{
gpio_free(gpioLed[i]);
}
}
static int gpioKeyInit(void)
{
int i;
int ret=0;
char gpioName[10];
for(i=0;i<GPIOKEYCNT;i++)
{
sprintf(gpioName,"key%d",gpioKey[i]);
ret = gpio_request(gpioKey[i], gpioName);
if(ret < 0) {
printk("Failed Request gpio%d error\n", 6);
return ret;
}
}
for(i=0;i<GPIOKEYCNT;i++)
{
ret = gpio_direction_input(gpioKey[i]);
if(ret < 0) {
printk("Failed direction_output gpio%d error\n", 6);
return ret;
}
}
return ret;
}
/*
static int gpioKeyGet(void)
{
int i;
int ret;
int keyData=0;
for(i=0;i<GPIOKEYCNT;i++)
{
// ret=gpio_get_value(gpioKey[i]) << i;
// keyData |= ret;
ret=gpio_get_value(gpioKey[i]);
keyData = keyData | ( ret << i );
}
return keyData;
}
*/
static void gpioKeyFree(void)
{
int i;
for(i=0;i<GPIOKEYCNT;i++)
{
gpio_free(gpioKey[i]);
}
}
static int gpioKeyIrqInit(void)
{
int i;
for(i=0;i<GPIOKEYCNT;i++)
{
key_irq[i] = gpio_to_irq(gpioKey[i]);
if(key_irq[i] < 0)
{
printk("gpioKeyIrq() Failed gpio %d\n",gpioKey[i]);
return key_irq[i];
}
}
return 0;
}
static void gpioKeyIrqFree(void)
{
int i;
for(i=0;i<GPIOKEYCNT;i++)
{
free_irq(key_irq[i],NULL);
}
}
int ledkey_open (struct inode *inode, struct file *filp)
{
int num0 = MAJOR(inode->i_rdev);
int num1 = MINOR(inode->i_rdev);
printk( "ledkey open -> major : %d\n", num0 );
printk( "ledkey open -> minor : %d\n", num1 );
try_module_get(THIS_MODULE);
return 0;
}
ssize_t ledkey_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
// int result;
char kbuf;
#if DEBUG
printk( "ledkey read -> buf : %08X, count : %08X \n", (unsigned int)buf, count );
#endif
// kbuf = (char)gpioKeyGet();
kbuf = keyNumber;
put_user(kbuf,buf);
if(keyNumber)
keyNumber = 0;
// result = copy_to_user(buf, &kbuf, count);
return count;
}
ssize_t ledkey_write (struct file *filp, const char *buf, size_t count, loff_t *f_pos)
{
// int i;
// int result;
char kbuff;
/* char kbuff[10];
for(i=0;i<count;i++)
get_user(kbuff[i],buf++);
*/
/* char kbuff[10];
copy_from_user(kbuff,buf,count);
*/
#if DEBUG
printk( "ledkey write -> buf : %08X, count : %08X \n", (unsigned int)buf, count );
#endif
get_user(kbuff,buf);
// result = copy_from_user(&kbuff,buf,count);
gpioLedSet(kbuff);
return count;
}
static long ledkey_ioctl (struct file *filp, unsigned int cmd, unsigned long arg)
{
printk( "ledkey ioctl -> cmd : %08X, arg : %08X \n", cmd, (unsigned int)arg );
return 0x53;
}
int ledkey_release (struct inode *inode, struct file *filp)
{
printk( "ledkey release \n" );
module_put(THIS_MODULE);
return 0;
}
struct file_operations ledkey_fops =
{
// .owner = THIS_MODULE,
.open = ledkey_open,
.read = ledkey_read,
.write = ledkey_write,
.unlocked_ioctl = ledkey_ioctl,
.release = ledkey_release,
};
int ledkey_init(void)
{
int i;
int result;
char * irqName[8] = {"IrqKey0","IrqKey1","IrqKey2","IrqKey3","IrqKey4","IrqKey5","IrqKey6","IrqKey7"};
printk( "ledkey ledkey_init \n" );
result=gpioLedInit();
if(result < 0)
return result;
result=gpioKeyInit();
if(result < 0)
return result;
result = gpioKeyIrqInit();
if(result < 0)
return result;
for(i=0;i<GPIOKEYCNT;i++)
{
result = request_irq(key_irq[i],key_isr,IRQF_TRIGGER_RISING,irqName[i],NULL);
if(result < 0)
{
printk("request_irq() failed irq %d\n",key_irq[i]);
return result;
}
}
result = register_chrdev( LEDKEY_DEV_MAJOR, LEDKEY_DEV_NAME, &ledkey_fops);
if (result < 0) return result;
return 0;
}
void ledkey_exit(void)
{
printk( "ledkey ledkey_exit \n" );
unregister_chrdev( LEDKEY_DEV_MAJOR, LEDKEY_DEV_NAME );
gpioKeyIrqFree();
gpioKeyFree();
gpioLedFree();
}
module_init(ledkey_init);
module_exit(ledkey_exit);
MODULE_LICENSE("Dual BSD/GPL");
ubuntu@ubuntu8:~/pi_bsp/drivers/p369_ledkey_int$ cat Makefile
APP := ledkey_app
MOD := ledkey_dev
OBJ := $(APP).o
obj-m := $(MOD).o
CROSS = ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf-
CC := arm-linux-gnueabihf-gcc
KDIR := /home/ubuntu/pi_bsp/kernel/linux
PWD := $(shell pwd)
default:$(OBJ)
$(MAKE) -C $(KDIR) M=$(PWD) modules $(CROSS)
cp $(MOD).ko /srv/nfs
%.o:%.c
$(CC) -o $(APP) $<
cp $(APP) /srv/nfs
clean:
rm -rf *.ko
rm -rf *.mod.*
rm -rf .*.cmd
rm -rf *.o
rm -rf modules.order
rm -rf Module.symvers
rm -rf $(MOD).mod
rm -rf .tmp_versions
rm -rf $(APP)
우분투에서 make하고 라즈베리파이에서
pi@pi08:/mnt/ubuntu_nfs $ sudo rmmod ledkey_dev
pi@pi08:/mnt/ubuntu_nfs $ sudo insmod ledkey_dev.ko
pi@pi08:/mnt/ubuntu_nfs $ ./ledkey_app 0x0f
실행하면 버튼을 누를 때 인터럽트로 led 제어하는 코드가 동작함.
Interrupt 서비스 테이블 출력
pi@pi08:/mnt/ubuntu_nfs $ sudo cat /proc/interrupts
전역변수를 쓰기 싫다면 매개변수를 통해 key_data와 key_irq를 지역변수로 빼고 매개변수로 넣을 수 있음.
'Linux BSP' 카테고리의 다른 글
[Linux Device Driver] Blocking I/O (0) | 2024.02.28 |
---|---|
[Linux Device Driver] 커널 메모리 동적 할당 및 irq매개변수를 통한 인터럽트 데이터 전달 (1) | 2024.02.27 |
[Linux Device Driver] Timer Interrupt (0) | 2024.02.27 |
[Linux Device Driver] Kernel Timer (0) | 2024.02.27 |
[Device Driver] 어플리케이션 없이 insmod만으로 동작하는 모듈 프로그램으로 커널 타이머 GPIO제어 (0) | 2024.02.26 |