prococo 发表于 2010-5-10 09:52:46

【新手学习】ARM11 linux下流水灯程序的实现--欢迎高手朋友们指教

上周买了块OK6410的开发板,仿照他们给的LED程序写了一个流水灯的程序。感兴趣的朋友们可以多来交流,一起学习。也请坛里的高手朋友多多指教,让我更快进步O(∩_∩)O


//#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <asm/irq.h>
#include <mach/gpio.h>
#include <plat/regs-gpio.h>
#include <plat/gpio-cfg.h>
#include <mach/hardware.h>
#include <linux/io.h>

#define DEVICE_NAME        "leds"
#define LED_MAJOR 231


static unsigned long led_table [] = {
       S3C64XX_GPM(0),
       S3C64XX_GPM(1),
       S3C64XX_GPM(2),
       S3C64XX_GPM(3),
};

static unsigned int led_cfg_table [] = {
        S3C64XX_GPM_OUTPUT(0),
        S3C64XX_GPM_OUTPUT(1),
        S3C64XX_GPM_OUTPUT(2),
        S3C64XX_GPM_OUTPUT(3),
};

static int s3c6410_leds_ioctl(
        struct inode *inode,
        struct file *file,
        unsigned int cmd,
        unsigned long arg)
{
        unsigned long tmp;
        switch(cmd) {
        case 0:
        case 1:
                if (arg > 4) {
                        return -EINVAL;
                }
                tmp = __raw_readl(S3C64XX_GPMDAT);
                if(cmd)
                        tmp &= (~(1<<arg));
                else
                        tmp |= (1<<arg);
                __raw_writel(tmp,S3C64XX_GPMDAT);
//                gpio_set_value(led_table, !cmd);
                return 0;
        default:
                return -EINVAL;
        }
}


static struct file_operations s3c6410_leds_fops = {
        .owner        =        THIS_MODULE,
        .ioctl        =        s3c6410_leds_ioctl,
};

static struct cdev cdev_leds;
struct class * my_class;

static int __init s3c6410_leds_init(void)
{
        int ret;
        unsigned long tmp;
        int i;
        dev_t devno;
        printk(KERN_NOTICE "enter s3c6410_leds_init\n");


        devno = MKDEV(LED_MAJOR,0);

        ret = register_chrdev_region(devno,1,DEVICE_NAME);
        ret = 0;
        if(ret<0)
        {
                printk(KERN_NOTICE "can not register led device");
                return ret;
        }
       
        cdev_init(&cdev_leds,&s3c6410_leds_fops);
        cdev_leds.owner = THIS_MODULE;

        ret =cdev_add(&cdev_leds,devno,1);
        if(ret)
        {
                printk(KERN_NOTICE "can not add leds device");
                return ret;
        }

        my_class = class_create(THIS_MODULE,"my_class");
        if(IS_ERR(my_class))
        {
                printk("Err: Failed in creating class\n");
                return -1;       
        }

        device_create(my_class,NULL,MKDEV(LED_MAJOR,0),NULL,DEVICE_NAME);

        //gpm0-3 pull up
        tmp = __raw_readl(S3C64XX_GPMPUD);
        tmp &= (~0xFF);
        tmp |= 0xaa;
        __raw_writel(tmp,S3C64XX_GPMPUD);

        //gpm0-3 output mode
        tmp = __raw_readl(S3C64XX_GPMCON);
        tmp &= (~0xFFFF);
        tmp |= 0x1111;
        __raw_writel(tmp,S3C64XX_GPMCON);
       
        //gpm0-3 output 0
        tmp = __raw_readl(S3C64XX_GPMDAT);
        tmp |= 0x10;
        __raw_writel(tmp,S3C64XX_GPMDAT);

        //printk("S3C64XX_GPMCON is %x\n",__raw_readl(S3C64XX_GPMCON));
        //printk("S3C64XX_GPMDAT is %x\n",__raw_readl(S3C64XX_GPMDAT));
        //printk("S3C64XX_GPMPUD is %x\n",__raw_readl(S3C64XX_GPMPUD));

        printk(DEVICE_NAME " initialized\n");

        return 0;
}

static void __exit s3c6410_leds_exit(void)
{
        cdev_del(&cdev_leds);

        unregister_chrdev_region(MKDEV(LED_MAJOR,0),1);

        printk(KERN_NOTICE "s3c2440_leds_exit\n");
}


module_init(s3c6410_leds_init);
module_exit(s3c6410_leds_exit);

MODULE_LICENSE("GPL");

int main(void)
{
        int on=1;
        int led;
        int fd;
       
        fd = open("/dev/leds",0);
        if(fd<0)
        {
                perror("open device leds");
                exit(1);
        }

        printf("leds test show.press ctrl+c to exit\n");
        while(1)
        {
                for(led=0;led<4;led++)
                {
                        ioctl(fd,on,led);
                        usleep(60000);
                }
                on = !on;
        }
        close(fd);
        return 0;
}

http://cache.amobbs.com/bbs_upload782111/files_29/ourdev_552768.jpg
(原文件名:OK6410 LED.jpg)

PS:
图片是在飞凌的技术论坛截的图,原版的可以在www.witech.com.cn里看。

lescy 发表于 2010-5-13 10:03:06

呵呵,加油啊

xiangpingfly1 发表于 2012-4-20 23:35:39

你的调试成功了吗?在板子上功能实现了吗?
页: [1]
查看完整版本: 【新手学习】ARM11 linux下流水灯程序的实现--欢迎高手朋友们指教