天嵌 ARM开发社区

 找回密码
 注册
查看: 1312|回复: 1

关于tq2440led编程问题

[复制链接]
hua_1010 发表于 2013-4-24 15:10:19 | 显示全部楼层 |阅读模式
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/uaccess.h>
#include <linux/cdev.h>
#include <asm/io.h>
#include <linux/device.h>
static struct class *led_class;
static unsigned long gpfbase;
#define GPBCON (*(volatile unsigned long *)(gpfbase +0x0))
#define GPBDAT (*(volatile unsigned long *)(gpfbase +0x4))
#define GPBUP (*(volatile unsigned long *)(gpfbase +0x8))

static int tq2440_led_open(struct inode *inode, struct file *file)
{
        printk("first dirver open call! \n");
        GPBCON= (GPBCON & ~(0xff<<10))|(0x55<<10);
        GPBUP = (GPBUP & ~(0xf<<5))|(0xf<<5);
        GPBDAT=0x0;
        return 0;
}


static int tq2440_led_release(struct inode * inode, struct file * file)
{
        printk("first dirver release call! \n");
        GPBDAT=0xf<<5;
        return 0;
}


static char led_states = 0x0;
static ssize_t tq2440_led_write(struct file *file, const char __user *userbuf,size_t bytes, loff_t *off)
{
        char val;
        copy_from_user(&val,userbuf,1);
        printk("dirver write call%d\n",val);
        GPBDAT = ~(val<<5);
        led_states = val;
        return sizeof(int);
}


static ssize_t tq2440_led_read(struct file *file, char __user *userbuf,size_t bytes, loff_t *off)
{
        copy_to_user(userbuf, &led_states, sizeof(char));
        printk("first dirver read call! \n");
        return sizeof(char);
}

static struct file_operations fops = {
        .owner = THIS_MODULE,
        .open = tq2440_led_open,
        .release = tq2440_led_release,
        .write = tq2440_led_write,
        .read = tq2440_led_read,
};

static int major = 0;
static struct cdev * tq2440_led_cdev;
#define FIRST_DRV_NAME "led23"

static int __init tq2440_led_init(void)
{
        dev_t dev;
        alloc_chrdev_region(&dev, 0, 1, FIRST_DRV_NAME);
        major = MAJOR(dev);
        tq2440_led_cdev = cdev_alloc();
        cdev_init(tq2440_led_cdev, &fops);
        tq2440_led_cdev->owner = THIS_MODULE;
        gpfbase = ioremap(0x56000010, 12);
        if(gpfbase == NULL)
        {
                printk("ioremap error! \n");
                return - 1;
        }
        cdev_add(tq2440_led_cdev, dev, 1);
        led_class = class_create(THIS_MODULE, "led23");
        device_create(led_class, NULL, dev, NULL, "led23");
        return 0;
}

static void __exit tq2440_led_exit(void)
{
        dev_t dev;
        dev = MKDEV(major,0);
        cdev_del(tq2440_led_cdev);
        unregister_chrdev_region(dev, 1);
        iounmap(gpfbase);
        device_destroy(led_class, dev);
        class_destroy(led_class);
}

module_init(tq2440_led_init);
module_exit(tq2440_led_exit);
MODULE_LICENSE("GPL");

为什么led灯控制还是不正常,我用了虚拟映射,报了个warming,应用请问有什么问题,求指教
 楼主| hua_1010 发表于 2013-4-24 15:12:20 | 显示全部楼层
这是我的应用层文件
#include <stdio.h>
#include <stdlib.h>
#include  <fcntl.h>
#include <unistd.h>
#define uchar unsigned char
#define uint unsigned int
uchar buffer;
uint num;
uchar table[]={0x00,0x01,0x02,0x04,0x08};
void main(void)
{       
        int fd;
        fd=open("/dev/led23",O_RDWR);
        if(fd<0)
                {
                        printf("error of open\n");
                        exit(-1);
                }
        sleep(2);
        read(fd,&buffer,1);
        for(num=4;num>0;num--)
        {
                buffer=table[num];
                write(fd,&buffer,1);
                sleep(1);
        }
        sleep(2);
        close(fd);
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

i.MX8系列ARM cortex A53 M4 工控板上一条 /1 下一条

Archiver|手机版|小黑屋|天嵌 嵌入式开发社区 ( 粤ICP备11094220号-2 )

GMT+8, 2024-6-26 11:05 , Processed in 1.046875 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表