2010年6月25日 星期五

mini2440_buttons.c driver study

下面是 mini2440_buttons driver的完整程式。
#include < linux/module.h>
#include < linux/kernel.h>
#include < linux/fs.h>
#include < linux/init.h>
#include < linux/delay.h>
#include < linux/poll.h>
#include < linux/irq.h>
#include < asm/irq.h>
#include < linux/interrupt.h>
#include < asm/uaccess.h>
#include < mach/regs-gpio.h>
#include < mach/hardware.h>
#include < linux/platform_device.h>
#include < linux/cdev.h>
#include < linux/miscdevice.h>
#include < linux/sched.h>
#include < linux/gpio.h>

#define DEVICE_NAME     "buttons"

struct button_irq_desc {
    int irq;
    int pin;
    int pin_setting;
    int number;
    char *name;   
};

static struct button_irq_desc button_irqs [] = {
    {IRQ_EINT8 , S3C2410_GPG(0) ,  S3C2410_GPG0_EINT8  , 0, "KEY0"},
    {IRQ_EINT11, S3C2410_GPG(3) ,  S3C2410_GPG3_EINT11 , 1, "KEY1"},
    {IRQ_EINT13, S3C2410_GPG(5) ,  S3C2410_GPG5_EINT13 , 2, "KEY2"},
    {IRQ_EINT14, S3C2410_GPG(6) ,  S3C2410_GPG6_EINT14 , 3, "KEY3"},
    {IRQ_EINT15, S3C2410_GPG(7) ,  S3C2410_GPG7_EINT15 , 4, "KEY4"},
    {IRQ_EINT19, S3C2410_GPG(11),  S3C2410_GPG11_EINT19, 5, "KEY5"},
};
static volatile char key_values [] = {'0', '0', '0', '0', '0', '0'};

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

static volatile int ev_press = 0;


static irqreturn_t buttons_interrupt(int irq, void *dev_id)
{
    struct button_irq_desc *button_irqs = (struct button_irq_desc *)dev_id;
    int down;

    // udelay(0);
    down = !s3c2410_gpio_getpin(button_irqs->pin);

    if (down != (key_values[button_irqs->number] & 1)) { // Changed

    key_values[button_irqs->number] = '0' + down;
   
        ev_press = 1;
        wake_up_interruptible(&button_waitq);
    }
   
    return IRQ_RETVAL(IRQ_HANDLED);
}


static int s3c24xx_buttons_open(struct inode *inode, struct file *file)
{
    int i;
    int err = 0;
   
    for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {
    if (button_irqs[i].irq < 0) {
        continue;
    }
        err = request_irq(button_irqs[i].irq, buttons_interrupt, IRQ_TYPE_EDGE_BOTH,
                          button_irqs[i].name, (void *)&button_irqs[i]);
        if (err)
            break;
    }

    if (err) {
        i--;
        for (; i >= 0; i--) {
        if (button_irqs[i].irq < 0) {
        continue;
        }
        disable_irq(button_irqs[i].irq);
            free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);
        }
        return -EBUSY;
    }

    ev_press = 1;
   
    return 0;
}


static int s3c24xx_buttons_close(struct inode *inode, struct file *file)
{
    int i;
   
    for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {
    if (button_irqs[i].irq < 0) {
        continue;
    }
    free_irq(button_irqs[i].irq, (void *)&button_irqs[i]);
    }

    return 0;
}


static int s3c24xx_buttons_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
    unsigned long err;

    if (!ev_press) {
    if (filp->f_flags & O_NONBLOCK)
        return -EAGAIN;
    else
        wait_event_interruptible(button_waitq, ev_press);
    }
   
    ev_press = 0;

    err = copy_to_user(buff, (const void *)key_values, min(sizeof(key_values), count));

    return err ? -EFAULT : min(sizeof(key_values), count);
}

static unsigned int s3c24xx_buttons_poll( struct file *file, struct poll_table_struct *wait)
{
    unsigned int mask = 0;
    poll_wait(file, &button_waitq, wait);
    if (ev_press)
        mask |= POLLIN | POLLRDNORM;
    return mask;
}


static struct file_operations dev_fops = {
    .owner   =   THIS_MODULE,
    .open    =   s3c24xx_buttons_open,
    .release =   s3c24xx_buttons_close,
    .read    =   s3c24xx_buttons_read,
    .poll    =   s3c24xx_buttons_poll,
};

static struct miscdevice misc = {
    .minor = MISC_DYNAMIC_MINOR,
    .name = DEVICE_NAME,
    .fops = &dev_fops,
};

static int __init dev_init(void)
{
    int ret;

    ret = misc_register(&misc);

    printk (DEVICE_NAME"\tinitialized\n");

    return ret;
}

static void __exit dev_exit(void)
{
    misc_deregister(&misc);
}

module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");


下面用程式碼片段,拆開來一步一步學習。

首先,先說linux device driver的架構,大致上的linux device driver,都會長的像下面這樣,
#include < linux/module.h>
#include < linux/kernel.h>
#include < linux/fs.h>
#include < linux/init.h>
#include < linux/delay.h>
#include < linux/poll.h>
#include < linux/irq.h>
#include < asm/irq.h>
#include < linux/interrupt.h>
#include < asm/uaccess.h>
#include < mach/regs-gpio.h>
#include < mach/hardware.h>
#include < linux/platform_device.h>
#include < linux/cdev.h>
#include < linux/miscdevice.h>
#include < linux/sched.h>
#include < linux/gpio.h>

static int s3c24xx_buttons_open(struct inode *inode, struct file *file)
{
...
}

static int s3c24xx_buttons_close(struct inode *inode, struct file *file)
{
...
}

static int s3c24xx_buttons_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
...
}

static struct file_operations dev_fops = {
    .owner   =   THIS_MODULE,
    .open    =   s3c24xx_buttons_open,
    .release =   s3c24xx_buttons_close,
    .read    =   s3c24xx_buttons_read,
};

static struct miscdevice misc = {
    .minor = MISC_DYNAMIC_MINOR,
    .name = DEVICE_NAME,
    .fops = &dev_fops,
};

static int __init dev_init(void)
{
    int ret;

    ret = misc_register(&misc);

    printk (DEVICE_NAME"\tinitialized\n");

    return ret;
}

static void __exit dev_exit(void)
{
    misc_deregister(&misc);
}

module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");

2010年6月20日 星期日

mini2440 study (cross complie tool chain)

1. 下載cross compiler
使用code sourcery所提供的 toolchains,需注意的是,這個 toolchains的版本是 arm-2009q1-203-arm-none-linux-gnueabi-i686-pc-linux-gnu,這個版本對於 mini2440的 CPU(arm920t)來說太新,因為 arm920t是 ARMv4T的架構,是幾年前的產品了,所以需要對 toolchains做點修改。

2. 下載後,找個地方解壓縮
# tar -jxvf arm-2009q1-203-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2 -C /opt/

3. 新增環境變數:
# export PATH=$PATH:/opt/usr/local/arm/4.3.3/bin

4. 修改變數名,將 "arm-none-linux-gnueabi-"開頭改成 "arm-linux-"開頭
ln -s arm-none-linux-gnueabi-gdbtui arm-linux-gdbtui
ln -s arm-none-linux-gnueabi-addr2line arm-linux-addr2line
ln -s arm-none-linux-gnueabi-gprof arm-linux-gprof
ln -s arm-none-linux-gnueabi-ar arm-linux-ar
ln -s arm-none-linux-gnueabi-as arm-linux-as
ln -s arm-none-linux-gnueabi-nm arm-linux-nm
ln -s arm-none-linux-gnueabi-ld arm-linux-ld
ln -s arm-none-linux-gnueabi-c++ arm-linux-c++
ln -s arm-none-linux-gnueabi-objcopy arm-linux-objcopy
ln -s arm-none-linux-gnueabi-c++filt arm-linux-c++filt
ln -s arm-none-linux-gnueabi-cpp arm-linux-cpp
ln -s arm-none-linux-gnueabi-ranlib arm-linux-ranlib
ln -s arm-none-linux-gnueabi-objdump arm-linux-objdump
ln -s arm-none-linux-gnueabi-readelf arm-linux-readelf
ln -s arm-none-linux-gnueabi-size arm-linux-size
ln -s arm-none-linux-gnueabi-gcov arm-linux-gcov
ln -s arm-none-linux-gnueabi-strings arm-linux-strings
ln -s arm-none-linux-gnueabi-gdb arm-linux-gdb
ln -s arm-none-linux-gnueabi-strip arm-linux-strip
ln -s arm-none-linux-gnueabi-sprite arm-linux-sprite


(重點)5. 在 /opt/usr/local/arm/4.3.3/bin/ 資料夾裡新增三個 shell script,將 toolchains降為 armv4t版本
arm-linux-g++的shell script內容:
#!/bin/sh
arm-none-linux-gnueabi-g++ -march=armv4t $*


arm-linux-gcc的shell script內容:
#!/bin/sh
arm-none-linux-gnueabi-gcc -march=armv4t $*


arm-linux-gcc-4.3.3的shell script內容:
#!/bin/sh
arm-none-linux-gnueabi-gcc-4.3.3 -march=armv4t $*


6. 修改權限
# chmod +x arm-linux-g++
# chmod +x arm-linux-gcc
# chmod +x arm-linux-gcc-4.3.3


7. 查看是否成功
# arm-linux-gcc -v