조이스틱 모듈 예제.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | #include <linux/module.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/fs.h> #include <linux/miscdevice.h> #include <linux/mutex.h> #include <asm/uaccess.h> #include <linux/gpio.h> //조이스틱 GPIO PIN번호 #define JOG_UP 5 #define JOG_DOWN 6 #define JOG_LEFT 16 #define JOG_RIGHT 20 #define JOG_CENTER 21 static ssize_t drv_hw_sw_read(struct file * file, char * buf, size_t length, loff_t * ofs) { int ret; int stat[5]; stat[0] = gpio_get_value(JOG_UP); stat[1] = gpio_get_value(JOG_DOWN); stat[2] = gpio_get_value(JOG_LEFT); stat[3] = gpio_get_value(JOG_RIGHT); stat[4] = gpio_get_value(JOG_CENTER); // 유저 영역에서 넘어온 buf포인터에 stat을 stat size만큼 복사. ret = copy_to_user(buf, stat, sizeof(stat)); return 0; } static struct file_operations drv_hw_sw_fops = { .owner = THIS_MODULE, .read = drv_hw_sw_read, }; static struct miscdevice drv_hw_sw_driver = { .minor = MISC_DYNAMIC_MINOR, .name = "drv_hw_sw", .fops = &drv_hw_sw_fops, }; static int drv_hw_sw_init(void) { int ret; ret = gpio_request(JOG_UP, "gpio sw"); if(ret) printk("#### FAILED Request gpio %d. error : %d \n", JOG_UP, ret); else gpio_direction_input(JOG_UP); ret = gpio_request(JOG_DOWN, "gpio sw"); if(ret) printk("#### FAILED Request gpio %d. error : %d \n", JOG_DOWN, ret); else gpio_direction_input(JOG_DOWN); ret = gpio_request(JOG_LEFT, "gpio sw"); if(ret) printk("#### FAILED Request gpio %d. error : %d \n", JOG_LEFT, ret); else gpio_direction_input(JOG_LEFT); ret = gpio_request(JOG_RIGHT, "gpio sw"); if(ret) printk("#### FAILED Request gpio %d. error : %d \n", JOG_RIGHT, ret); else gpio_direction_input(JOG_RIGHT); ret = gpio_request(JOG_CENTER, "gpio sw"); if(ret) printk("#### FAILED Request gpio %d. error : %d \n", JOG_CENTER, ret); else gpio_direction_input(JOG_CENTER); return misc_register(&drv_hw_sw_driver); } static void drv_hw_sw_exit(void) { gpio_free(JOG_UP); gpio_free(JOG_DOWN); gpio_free(JOG_LEFT); gpio_free(JOG_RIGHT); gpio_free(JOG_CENTER); misc_deregister(&drv_hw_sw_driver); } module_init(drv_hw_sw_init); module_exit(drv_hw_sw_exit); MODULE_AUTHOR("PlanX Studio"); MODULE_DESCRIPTION("drv_hw_sw"); MODULE_LICENSE("Dual BSD/GPL"); | cs |
조이스틱 어플리케이션 예제.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> #include <fcntl.h> #include <pthread.h> pthread_t thread; /* User Thread */ int thread_start_flag = 1; int thr_id; #define NODE_NAME "/dev/drv_hw_sw" void print_button_state(int i, int* before_state) { if (i == 0) printf(" jog up botton\n"); else if (i == 1) printf (" jog down botton\n"); else if (i == 2) printf(" jog left botton\n"); else if (i == 3) printf(" jog right botton\n"); else if (i == 4) printf(" jog centor botton\n"); else printf("input scope is 0 ~ 4\n"); } void *sw_thread(void * unuse) { int fd; int ret; int before_stat[5]={}; int cur_stat[5]; int cnt=0; int i; fd = open(NODE_NAME, O_RDONLY); while(thread_start_flag) { // 모듈에서의 처리결과 cur_stat에 조이스틱의 현재 값이 복사된다. ret = read(fd, cur_stat, sizeof(cur_stat)); for (i=0; i<5; i++) { if (before_stat[i] != cur_stat[i]) { print_button_state(i, before_stat); before_stat[i] = cur_stat[i]; } //usleep(100000); // 0.1sec } } close(fd); printf("#### Terminate thead...\n"); pthread_exit((void *)fd); } int main(int argc, char * argv[]) { int status, i; thr_id = pthread_create(&thread, 0, sw_thread, 0); if (thr_id < 0) { printf("#### ERROR pthread_create\n"); exit(0); } while (1) pause(); thread_start_flag = 0; pthread_join(thread, (void **)&status); printf("#### Exit program, status = %d\n", status); exit(0); } | cs |
출처 : 인지소프트웨어 기술포럼 ( 전유진 강사님 수업자료)
'임베디드 > 디바이스 드라이버' 카테고리의 다른 글
디바이스 드라이버 인터럽트 (0) | 2017.12.28 |
---|---|
디바이스 드라이버 GPIO 모션 센서 예제 (0) | 2017.12.28 |
Misc 디바이스 드라이버2 (0) | 2017.12.27 |
Misc 디바이스 드라이버1 (0) | 2017.12.26 |
디바이스 드라이버 구조 (0) | 2017.12.26 |