1 / 21

KEY 디바이스 드라이버

KEY 디바이스 드라이버. Lecture #12. 차례. GPIO 및 Control Registers KEY 하드웨어 구성 KEY Driver 프로그램 key-driver.c 시험 응용 프로그램 key-app.c. GPIO(General-Purpose I/O). PXA255 processor 는 81 개의 GPIO pin 이 있음 GPIO 는 27 개의 제어 register 에 의하여 제어된다 set pin direction (GPDR0~2) read pin level (GPLR0~2)

gada
Download Presentation

KEY 디바이스 드라이버

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. KEY 디바이스 드라이버 Lecture #12

  2. 차례 • GPIO 및 Control Registers • KEY 하드웨어 구성 • KEY Driver 프로그램 key-driver.c • 시험 응용 프로그램 key-app.c

  3. GPIO(General-Purpose I/O) • PXA255 processor는 81개의 GPIO pin이 있음 • GPIO는 27개의 제어 register에 의하여 제어된다 • set pin direction (GPDR0~2) • read pin level (GPLR0~2) • set or clear pin level (GPSR0~2, GPCR0~2) • set rising/falling edge detection (GRER0~2, GFER0~2) • edge detection (GEDR0~2) • alternate function (GAFR_L0~2, GAFR_U0~2) • 사용하지 않는 GPIO는 output으로 configure하면 전력 소비를 줄인다

  4. GPIO Control Registers (1) • Pin Direction (GPDR) - 3개 • 해당 GPIO 핀을 input 혹은 output으로 사용할 것인지 설정함 • 0이면 input이고 1이면 output으로 설정됨 • Pin Level (GPLR) - 3개 • 해당 GPIO 핀의 현재 level을 표시함 • 0이면 low이고 1이면 high임 • Set or Clear (GPSR, GPCR) - 각각 3개 • 해당 output GPIO 핀의 level을 high로 설정함. 1이면 high • 해당 output GPIO 핀의 level을 low로 설정함. 1이면 low

  5. GPIO Control Registers (2) • Rising or Falling Edge (GRER, GFER) - 각각 3개 • 해당 GPIO 핀의 동작을 지정함. 1이면 rising edge시 동작됨 • 해당 GPIO 핀의 동작을 지정함. 1이면 falling edge 시 동작됨 • Edge Detection (GEDR) - 3개 • 해당 GPIO 핀의 동작을 감지함 • 1이면 rising/falling edge의 동작이 감지되었음을 표시 • 해당 bit를 clear하려면 그 bit에 1을 씀

  6. GPIO Control Registers - 3/3 • Alternate Functions (GAFR_L, GAFR_U) - 6개 • 해당 GPIO 핀을 generic GPIO 핀으로 쓸 것인지 특정 기능을 위한 핀으로 쓸 것인지 설정함. • 00 - GPIO 설정 • 01 - 특정 기능 1 • 10 - 특정 기능 2 • 11 - 특정 기능 3

  7. 0 3 1 2 4 7 5 6 8 b 9 a Key Switch - 하드웨어 구조

  8. Key Encoder – 하드웨어 구조 74C922 chip (Key Encoder) 1. Key Switch에 연결 2. 데이타 생성(4bits) 1. Key Switch에 연결 2. Key가 눌러지면 인터럽트 생성 1. Key Switch에 연결 3. 인터럽트는 GPIO 0에 연결

  9. Bidirectional Transceiver - 하드웨어 구조 4. 데이터 버스로 들어감 3. Key Encoder에서 생성된 데이타

  10. 74C922 및 74LVT245 • 74C922 - Key Encoder • KEY_ROW 1~3과 KEY_COL 1~4 이 Key Switch(3x4)에 연결 • Encoding 된 값은 KEY_1~4 라인으로 출력 • KEY_AVAL 라인은 PXA255 GPIO 0와 연결 (interrupt 생성) • 74LVT245 - Bidirectional Transceiver • KEY_1~4 라인이 입력 • DATA00~03 라인이 출력 • 출력 데이터는 데이터 버스를 통하여 물리 주소 0x14000000로 전달

  11. 커널 Configure/Compile • 현재 커널 이미지에는 key driver가 포함되어 있기 때문에 key driver 프로그래밍을 실습하기 위해서는 커널의 "Character devices"에 포함된 "KEY GPIO" 기능을 disable하고 다시 컴파일 하여야 한다

  12. Key Device Driver - 동작방식 Driver start Application start module_init() open() key_open() 함수 call key_open() key_read() 함수 call Key_read() read() read() blocking Interruptible_sleep_on() waiting waiting key interrupt key_handler() wakeup_interruptible() read() waking

  13. Key Device Driver –커널 함수 • set_GPIO_IRQ_edge() GPDR을 통하여 GPIO 방향 설정 • GAFR을 통하여 alternate function 설정 • 함수로 GPIO detect 방향 설정 • request_irq()함수로 interrupt 등록 • key_handler()함수로 인터럽트 핸들러 구현 • ioremap(), iounmap()을 이용하여 물리주소로 access

  14. key-driver.c: Header files #include <linux/module.h> // 모듈에 관한 정의 ex)MOD_INC_USE_COUNT #include <linux/init.h> // init_module() 과 cleanup_module() #include <linux/delay.h> // udelay() #include <asm/uaccess.h> // copy_to_user() #include <asm/io.h> // GPIO controller에 관한 정의 #include <asm/irq.h> // interrupt에 관한 정의

  15. key-driver.c: 매크로/전역 변수 #define DEVICE_NAME "key" #define KEY_MAJOR 242 #define KEY_ADDRESS 0x14000000 static DECLARE_WAIT_QUEUE_HEAD(wq); static char *key_address; static char key_value;

  16. key-driver.c: 함수 prototype 선언 /* Prototypes */ int init_module (void ); void cleanup_module(void); int key_open(struct inode *, struct file *); int key_release(struct inode *, struct file *); ssize_t key_read (struct file *inode, char *gdata, size_t length, loff_t *off_what); void key_handler(int irq, void *dev_id, struct pt_regs *regs); static struct file_operations key_fops = { open: key_open, read: key_read, release: key_release, };

  17. key-driver.c : init/cleanup int init_module(void) { int i; if((i=register_chrdev(KEY_MAJOR,DEVICE_NAME,&key_fops))<0) return i; GPDR0 &=~(GPIO_0); // GPIO 0은 input GAFR0_L &=~(0x3); // alternate function을 generic GPIO로 // GPIO 0을 falling edge set_GPIO_IRQ_edge(0,GPIO_FALLING_EDGE); if((i=request_irq(IRQ_GPIO(0),key_handler,SA_INTERRUPT,"key",NULL))<0) return i; return 0; } void cleanup_module(void) { unregister_chrdev(KEY_MAJOR,DEVICE_NAME); }

  18. key-driver.c: open/release int key_open(struct inode *inode, struct file *file) { MOD_INC_USE_COUNT; return 0; } int key_release(struct inode *inode, struct file *file) { MOD_DEC_USE_COUNT; return 0; }

  19. key-driver.c : read interrupt ssize_t key_read(struct file *inode, char *gdata, size_t length, loff_t *off_what) { interruptible_sleep_on(&wq); copy_to_user(gdata,&key_value,1); return 1; } void key_handler(int irq, void *dev_id, struct pt_regs *regs) { key_address=(char*)ioremap(KEY_ADDRESS,0x10); key_value=*(key_address)&0x0f; iounmap(key_address); wake_up_interruptible(&wq); }

  20. 시험 응용 프로그램 (key-app.c) #include <stdio.h> #include <fcntl.h> int main(void) { int fd; char c; if((fd=open("/dev/key",O_RDONLY))<0){ fprintf(stderr,"can not open /dev/key\n"); return 1; } while(1){ read(fd,&c,1); printf("c=0x%x\n",c); if(c==0x00) break; } close(fd); return 0; }

  21. Makefile all: key-driver.o key-app key-driver.o: key-driver.c arm-linux-gcc –O2 –Wall -D__KERNEL__ -DMODULE \ -I/root/pxa255pro/linux-2.4.19/include \ -c key-driver.c -o key-driver.o key-app: key-app.c arm-linux-gcc key-app.c -o key-app clean: rm -f key-driver.o key-app

More Related