Friday, September 25, 2015

Char Device Driver - 2_led on/off

實作 3:  led 燈號控制驅動程式,針對開發版上個別led燈號做 on/off 控制
加入次設備號,次設備號給自己的驅動程式內部使用,並了解如何撰寫驅動程式的硬體控制,與裸機程式相同,但需先利用ioremap 將 i/o 物理記憶體位置,映射到kernel虛擬地址,此後才可以使用。

===========================================================
first_drv.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>

#define DEVICE_NAME "first_drv_leds"


static struct class *leds_class;
static struct class_device *leds_class_devs[4];

volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;


static int first_drv_open(struct inode *inode, struct file *file)
{
    // *gpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2)));
    // *gpfcon |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2)));
    
    // printk("first_drv_open\n");
    
    int minor = MINOR(inode -> i_rdev);     // minor number

    switch(minor)
    {
        // all led on
        case 0:
            // set GPF4,5,6  pin output
            //s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);
            *gpfcon &= ~(0x3<<(4*2));
            *gpfcon |= (1<<(4*2));
            
            //s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);
            *gpfcon &= ~(0x3<<(5*2));
            *gpfcon |= (1<<(5*2));

            //s3c2410_gpio_cfgpin(S3C2410_GPF6, S3C2410_GPF6_OUTP);
            *gpfcon &= ~(0x3<<(6*2));
            *gpfcon |= (1<<(6*2));
            break;


         // led1 on   
         case 1:
            //s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);
            *gpfcon &= ~(0x3<<(4*2));
            *gpfcon |= (1<<(4*2));
            break;

         // led2 on
         case 2:
            //s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);
            *gpfcon &= ~(0x3<<(5*2));
            *gpfcon |= (1<<(5*2));
            break;

         // led3 on
         case 3:
            //s3c2410_gpio_cfgpin(S3C2410_GPF6, S3C2410_GPF6_OUTP);
            *gpfcon &= ~(0x3<<(6*2));
            *gpfcon |= (1<<(6*2));
            break;
    }
            
return 0;
}

static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
char val;

//printk("first_drv_write\n");

    int minor = MINOR(file->f_dentry->d_inode->i_rdev);
copy_from_user(&val, buf, count);  // user space data transfer kernel space

    switch(minor)
    {
        // leds
        case 0:
        if (val == 1)
        {
        // LED ON
        *gpfdat &= ~((1<<4) | (1<<5) | (1<<6));
                       // or
                       //s3c2410_gpio_setpin(S3C2410_GPF4, 0);
                       //GPFDAT &= ~(1<<4);
                       //s3c2410_gpio_setpin(S3C2410_GPF5, 0);
                       //GPFDAT &= ~(1<<5);
                       //s3c2410_gpio_setpin(S3C2410_GPF6, 0);
                       //GPFDAT &= ~(1<<6);
        }
        else
        {
        // LED OFF
        *gpfdat |= (1<<4) | (1<<5) | (1<<6);
        }
        break;

        // led1
        case 1:
            if (val == 1)
        {
        // LED ON
        *gpfdat &= ~(1<<4);
        }
        else
        {
        // LED OFF
        *gpfdat |= (1<<4);
        }
        break;


        // led2
        case 2:
            if (val == 1)
        {
        // LED ON
        *gpfdat &= ~(1<<5);
        }
        else
        {
        // LED OFF
        *gpfdat |= (1<<5);
        }
        break;


        // led3
        case 3:
            if (val == 1)
        {
        // LED ON
        *gpfdat &= ~(1<<6);
        }
        else
        {
        // LED OFF
        *gpfdat |= (1<<6);
        }
        break;
    }
return 0;
}


static struct file_operations first_drv_fops = 
{
.owner = THIS_MODULE, /* 這是一個巨集,推向編譯模組時自動創建的__this_module變數*/
.open = first_drv_open,
.write = first_drv_write,
};

int major;
static int first_drv_init(void)
{
major = register_chrdev(0, "DEVICE_NAME", &first_drv_fops); // 向kernel註冊, major number 由系統決定,成功返回 major number
    int minor = 0;

leds_class = class_create(THIS_MODULE, "leds_class");

    leds_class_devs[0] = class_device_create(leds_class, NULL, MKDEV(major, 0), NULL, "leds");   // 系統自動創建 /dev/xyz
    for (minor = 1; minor < 4; minor++)  /* /dev/led1,2,3 */
{
leds_class_devs[minor] = class_device_create(leds_class, NULL, MKDEV(major, minor), NULL, "led%d", minor);
if (unlikely(IS_ERR(leds_class_devs[minor])))
return PTR_ERR(leds_class_devs[minor]);
}

    
    gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
    gpfdat = gpfcon + 1;
return 0;
}

static void first_drv_exit(void)
{
unregister_chrdev(major, "DEVICE_NAME"); // 卸載

    for (minor = 0; minor < 4; minor++)
{
class_device_unregister(leds_class_devs[minor]);
}
    class_destroy(leds_class);

    iounmap(gpfcon);
}

module_init(first_drv_init);
module_exit(first_drv_exit);

MODULE_LICENSE("GPL");
==================================================================
firstdrvtest.c


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>



void print_usage(char *para)
{
    printf("Usage:\n");
    printf("%s <dev> <on|off>\n", para);
    printf("eg.  \n");
    printf("%s /dev/leds on\n", para);
    printf("%s /dev/leds off\n", para);
    printf("%s /dev/led1 on \n", para);
    printf("%s /dev/led1 off \n", para);
}


/* firstdrvtest on
  * firstdrvtest off
  */
int main(int argc, char **argv)
{
    int fd;
    char val;
    char *filename;


    if(argc != 3)
    {
         print_usage(argv[0]);
         return 0;
    }

    filename = argv[1];
    fd = open(filename, O_RDWR);
    if (fd < 0)     printf("can't open!\n");

    if(!strcmp(argv[2], "on"))
    {
        // LED ON
        val = 1;
        write(fd, &val, 1);
    }
    else if(!strcmp("off", argv[2]))
    {
        // LED OFF
        val = 0;
        write(fd, &val, 1);
    }
    else
        return 0;


    return 0;
    

}
=====================================================================
實驗結果:

Please press Enter to activate this console.
starting pid 772, tty '/dev/s3c2410_serial0': '/bin/sh'
#
# cd mnt/nfs/
# ls
first_drv.ko  firstdrvtest
# insmod first_drv.ko
# ls -l /dev/led*
crw-rw----    1 0        0        252,   1 Jan  1 00:04 /dev/led1
crw-rw----    1 0        0        252,   2 Jan  1 00:04 /dev/led2
crw-rw----    1 0        0        252,   3 Jan  1 00:04 /dev/led3
crw-rw----    1 0        0        252,   0 Jan  1 00:04 /dev/leds
# rmmod first_drv.ko
# ls -l /dev/led*
ls: /dev/led*: No such file or directory
# insmod first_drv.ko
# ./firstdrvtest /dev/leds on    <-- all led on
# ./firstdrvtest /dev/leds off    <-- all led off
# ./firstdrvtest /dev/led1 on    <-- led1 on
# ./firstdrvtest /dev/led1 off    <-- led1 off
# ./firstdrvtest /dev/led2 on
# ./firstdrvtest /dev/led2 off
# ./firstdrvtest /dev/led3 on
# ./firstdrvtest /dev/led3 off

No comments:

Post a Comment