디바이스드라이버 응용

  • View
    72

  • Download
    2

Embed Size (px)

DESCRIPTION

. LED FND LCD . (dedicated I/O isolated I/O) (memory mapped I/O). PXA270 - PowerPoint PPT Presentation

Transcript

  • LED FND LCD

  • (dedicated I/O isolated I/O) (memory mapped I/O)

  • PXA270

    ARM

  • LED 01 #include 02 #include 03 #include 04 #include 05 #include 0607 #define LED_MAJOR 23908 #define LED_NAME "LED"09 #define LED_MODULE_VERSION "LED IO PORT V0.1"10 #define LED_ADDRESS 0x1240000011 #define LED_CS (*((volatile unsigned char *)(mem_base)))1213 static void *mem_base;14 unsigned long mem_addr, mem_len;1516 static int led_clear(void) {17 LED_CS = 0xFF;18 return 0;19 }2021 int led_open(struct inode *minode, struct file *mfile)22 {23 return 0;24 }2526 int led_release(struct inode *minode, struct file *mfile)27 {28 return 0;29 }3031 ssize_t led_write_byte(struct file *inode, const char *gdata, size_t length, loff_t *off_what)32 {33 unsigned char c;34 get_user(c, (unsigned char *)gdata);35 LED_CS = c;36 return length;37 }

  • 3839 static struct file_operations led_fops = {40 .owner = THIS_MODULE,41 .write = led_write_byte,42 .open = led_open,43 .release = led_release,44 };4546 int led_init(void)47 {48 int result;49 result = register_chrdev(LED_MAJOR, LED_NAME, &led_fops);50 if(result < 0) {51 printk(KERN_WARNING "can't get any major number.\n");52 return result;53 }54 mem_addr = LED_ADDRESS;55 mem_len = 0x1000;56 mem_base = ioremap_nocache(mem_addr, mem_len);57 if( !mem_base) {58 printk("Error mapping LED memory\n");59 release_mem_region(mem_addr, mem_len);60 return -EBUSY;61 }62 printk("init module, LED major number : %d\n", LED_MAJOR);63 return 0;64 }6566 void led_exit(void)67 {68 led_clear();69 if (unregister_chrdev(LED_MAJOR, LED_NAME))70 printk(KERN_WARNING"%s driver cleanup failed.\n", LED_NAME);71 iounmap(mem_base);72 }7374 MODULE_LICENSE("GPL");75 module_init(led_init);76 module_exit(led_exit); LED

  • 01 #include 02 #include 03 #include 04 #include 05 #include 06 #include 0708 int main(int argc, char **argv)09 {10 int dev;11 char buff;1213 if(argc
  • LED Makefile (11/led/Makefile) 01 KDIR := /embed/kernel/linux0203 obj-m := led.o0405 build:06 make -C $(KDIR) SUBDIRS=$(PWD) modules070809 clean:10 rm -rf *.o *.ko *.mod.c LED

  • LED

  • LED

  • FND

  • 001 #include 010 #include 011012 #define FND_MAJOR 231013 #define FND_NAME "FND"014 #define MAX_FND 8015016 #define FPGA_FND_CS0 (0x11000000)023 #define FPGA_FND_CS7 (0x11700000)024025 #define FND_CS0 (*((volatile unsigned char *)(mem_fnd_cs0)))032 #define FND_CS7 (*((volatile unsigned char *)(mem_fnd_cs7)))033034 void *mem_fnd_cs0, *mem_fnd_cs1, *mem_fnd_cs2, *mem_fnd_cs3,*mem_fnd_cs4, *mem_fnd_cs5, *mem_fnd_cs6, *mem_fnd_cs7;035036 static char disp[8] = {0};037038 static int fnd_clear(void)039 {040 FND_CS0 = 0x00;047 FND_CS7 = 0x00;048 return 0;049 }050 FND

  • 051 static int fnd_open(struct inode *minode, struct file *mfile)052 {053 fnd_clear();054 return 0;055 }056057 static int fnd_release(struct inode *minode, struct file *mfile)058 {059 return 0;060 }061062 static ssize_t fnd_write (struct file *filp, const char *buf,size_t count, loff_t *f_pos)063 {064 copy_from_user(disp, buf, count);065 FND_CS0 = disp[0]; mdelay(100);072 FND_CS7 = disp[7]; mdelay(100);073 return 0;074 }075076 static struct file_operations device_fops = {077 .owner = THIS_MODULE,078 .open = fnd_open,079 .write = fnd_write,080 .release = fnd_release,081 };082 FND

  • 083 static int fnd_init(void)084 {085 int result;086 unsigned long mem_addr_fnd0, mem_addr_fnd1, mem_addr_fnd2, mem_addr_fnd3, mem_addr_fnd4, mem_addr_fnd5,mem_addr_fnd6, mem_addr_fnd7;087 unsigned long mem_len;088089 result = register_chrdev(FND_MAJOR, FND_NAME, &device_fops);090 printk("FPGA %s MAJOR %d IRQ %d\n", FND_NAME, FND_MAJOR,result);091 mem_addr_fnd0 = FPGA_FND_CS0;098 mem_addr_fnd7 = FPGA_FND_CS7;099 mem_len = 0x1000;100101 mem_fnd_cs0 = ioremap_nocache ( mem_addr_fnd0, mem_len);102 if( !mem_fnd_cs0) {103 printk("Error mapping fnd0 memery");104 return -EBUSY;105 }143 mem_fnd_cs7 = ioremap_nocache ( mem_addr_fnd7, mem_len);144 if( !mem_fnd_cs7) {145 printk("Error mapping fnd7 memery");146 return -EBUSY;147 }148 return 0;149 }150151 static void fnd_exit(void)152 {153 fnd_clear();154 iounmap(mem_fnd_cs0);161 iounmap(mem_fnd_cs7);162 unregister_chrdev(FND_MAJOR, FND_NAME);163 printk("%s unregisterd.\n", FND_NAME);164 }165 MODULE_LICENSE("GPL");166 module_init(fnd_init); // fnd_init 167 module_exit(fnd_exit); // fnd_exit FND

  • 01 #include 02 #include 0304 #define MAXFND 80506 static char fndDev[] = "/dev/FND";07 static int fndFd = (-1);0809 asc2fnd(char *s, int n)10 {11 char c;12 while (n-- > 0) {13 c = *s;14 switch (c) { // 15 case '0': c = 0x3f; break;24 case '9': c = 0x67; break;25 default: c = 0x00; break;26 }27 *s++ = c;28 }29 }3031 main(int ac, char *av[])32 {33 int n;34 char buf[MAXFND+1];3536 fndFd = open( fndDev, O_RDWR);37 if (fndFd < 0) {38 fprintf(stderr, "cannot open FND (%d)", fndFd);39 exit(2);40 }41 memset(buf, 0, sizeof(buf));42 if (ac > 1) {43 n = strlen(av[1]);44 if (n > MAXFND)45 n = MAXFND;46 memcpy(buf, av[1], n);47 }48 asc2fnd(buf, MAXFND);49 write(fndFd, buf, MAXFND);50 } FND

  • FND Makefile

    FND

  • FND

  • , ,

    #include void (*signal(int signum, void(*handler)(int))) (int);

    int kill_proc(pid_t pid, int signum, int priv);

  • : HZ( ) jiffies( ) jiffies timer->expires expires jiffies

  • 001 #include -------------010 #include 011012 #define KEY_MAJOR 233013 #define KEY_NAME "KEY"014 #define SCAN_NUM 4015016 #define FPGA_KEY_OUT (0x11D00000)017 #define FPGA_KEY_IN (0x11E00000)018019 #define KEYOUT (*(volatile unsigned char *)mem_key_out)020 #define KEYIN (*(volatile unsigned char *)mem_key_in)021022 static void *mem_key_out, *mem_key_in;023 static struct timer_list key_timer_str;024 static unsigned char key_data;025 static pid_t id;026 static int n = 1;027028 int key_open (struct inode *inode, struct file *filp)029 {030 return 0;031 }032033 int key_release (struct inode *inode, struct file *filp)034 {035 del_timer(&key_timer_str);036 return 0;037 }038

  • 039 ssize_t key_read (struct file *filp, char *buf, size_tcount, loff_t *f_pos)040 {041 copy_to_user(buf, &key_data, sizeof(key_data));042 return 0;043 }044045 void timer_function(unsigned long data)046 {047 unsigned int i, in, out;048049 for(i=0; i
  • 074 struct file_operations key_fops = {075 .owner = THIS_MODULE,076 .read = key_read,077 .write = key_write,078 .open = key_open,079 .release = key_release,080 };081082 static int key_init(void)083 {084 int result;085 unsigned long mem_addr_out, mem_addr_in, mem_len;086087 result = register_chrdev(KEY_MAJOR, KEY_NAME, &key_fops);092 mem_addr_out = FPGA_KEY_OUT;093 mem_addr_in = FPGA_KEY_IN;094 mem_len = 0x1000;095 mem_key_out = ioremap_nocache (mem_addr_out, mem_len);100 mem_key_in = ioremap_nocache (mem_addr_in, mem_len);105 printk("FPGA %s MAJOR %d\n", KEY_NAME, KEY_MAJOR);106 return 0;107 }108109 static void key_exit(void)110 {111 unregister_chrdev (KEY_MAJOR, KEY_NAME);112 }113114 MODULE_LICENSE("GPL");115 module_init(key_init);116 module_exit(key_exit);

  • 01 #include 08 #include 0910 static int dev;11 static unsigned char vkey;1213 void keysignal(int sig)14 {15 read(dev, &vkey, 1);16 printf("SIGNAL occur!!!!\n");17 }1819 int main(void)20 {21 pid_t id;2223 if ((dev = open("/dev/KEY", O_RDWR)) < 0) {24 printf("return Value is %d\n",dev);25 perror("open failed /dev/KEY");26 close(dev);27 exit(-1);28 }29 (void) signal (SIGUSR1, keysignal);30 id = getpid();31 write(dev, &id, 4);32 printf("press the push button!\n");33 (void) signal (SIGUSR1, keysignal);34 while(1) {35 if(vkey != 0) {36 printf("vkey is 0x%x\n", vkey);37 vkey=0;38 }39 }40 close(dev);41 return 0;42 }

  • Makefile

  • HD44780 LCD 16 2 . 57. 2 (RS, R/W) (D0~D7) [ 11-5] Enable On/Off LCD

  • LCD

    001 #include 009 #include 010011 #define CLCD_MAJOR 238012 #define CLCD_NAME "CLCD"013014 #define FPGA_CLCD_WR_ADD (0x12300000)015 #define FPGA_CLCD_RS_ADD (0x12380000)016 #define CLCD_CMD_ADDR (*((volatile unsigned char*)(mem_base_wr)))017 #define CLCD_DATA_ADDR (*((volatile unsigned char*)(mem_base_rs)))018019 static void *mem_base_wr, *mem_base_rs;020 static unsigned long mem_addr_wr, mem_addr_rs, mem_len;021022 static void lcd_init(void)023 {024 CLCD_CMD_ADDR = 0x38;025 mdelay(300);026 CLCD_CMD_ADDR = 0x0e;027 mdelay(100);028 CLCD_CMD_ADDR = 0x02;029 mdelay(100);030 CLCD_CMD_ADDR = 0x01;031 mdelay(100);032 }033034 static void string_out(char *str)035 {036 char *s;037 int i=0;038039 printk("%s\n", str);040 lcd_init();041 for (s=str; *s; s++) {042 CLCD_DATA_ADDR = *s;043 if(i == 15) {044 udelay(100);045 CLCD_CMD_ADDR = 0xC0;046 }047 udelay(100);048 i++;049 }050 }

  • 062 ssize_t clcd_write (struct file *filp, const char *buf, size_t count, loff_t *f_pos)063 {064 char data[32];065066 memset(data, 0 , 32);067 copy_from_user(data, buf, count);068 string_out(data);069 return 0;070 }071072 struct file_operations clcd_fops = {073 .owner = THIS_MODULE,074 .write = clcd_write,075 .open = clcd_open,076 .release = clcd_release,077 };078079 static int clcd_init(void)080 {081 int result;082083 result = register_chrdev(CLCD_MAJOR, CLCD_NAME, &clcd_fops);088 mem_addr_wr = FPGA_CLCD_WR_ADD;089 mem_addr_rs = FPGA_CLCD_RS_ADD;090 mem_len = 0x1000;091092 mem_base_wr = ioremap_nocache ( mem_addr_wr, mem_len);098 mem_base_rs = ioremap_nocache ( mem_addr_rs, mem_len);103 printk("FPGA %s MAJOR %d\n",CLCD_NAME, result);104 return 0;105 }106107 static void clcd_exit(void)108 {109 unregister_