9th_led_bus_drv_dev

来源:互联网 发布:薛之谦的淘宝店名称 编辑:程序博客网 时间:2024/06/05 17:46
#include <linux/module.h>
#include <linux/version.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/timer.h>
#include <linux/init.h>
#include <linux/serial_core.h>
#include <linux/platform_device.h>

/* 分配/设置/注册一个platform_device */

static struct resource led_resource[] = {
    [0] = {
        .start = 0x56000050,
        .end   = 0x56000050 + 8 - 1,
        .flags = IORESOURCE_MEM,
    },
    [1] = {
        .start = 5,
        .end   = 5,
        .flags = IORESOURCE_IRQ,
    }
};

static void led_release(struct device * dev)
{
}

static struct platform_device led_dev = {
    .name         = "myled",
    .id       = -1,
    .num_resources    = ARRAY_SIZE(led_resource),
    .resource     = led_resource,
    .dev = { 
    .release = led_release, 
},
};

static int led_dev_init(void)
{
platform_device_register(&led_dev);
return 0;
}


static void led_dev_exit(void)
{
platform_device_unregister(&led_dev);
}


module_init(led_dev_init);
module_exit(led_dev_exit);

MODULE_LICENSE("GPL");

//////////////////////////////////////led_drv.c

/* 分配/设置/注册一个platform_driver */
#include <linux/module.h>
#include <linux/version.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/sched.h>
#include <linux/pm.h>
#include <linux/sysctl.h>
#include <linux/proc_fs.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/input.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/io.h>

static int major;

static struct class *cls;
static volatile unsigned long *gpio_con;
static volatile unsigned long *gpio_dat;
static int pin;

static int led_open(struct inode *inode, struct file *file)
{
//printk("first_drv_open\n");
/* 配置为输出 */
*gpio_con &= ~(0x3<<(pin*2));
*gpio_con |= (0x1<<(pin*2));
return 0;
}

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

//printk("first_drv_write\n");

copy_from_user(&val, buf, count); //copy_to_user();

if (val == 1)
{
// 点灯
*gpio_dat &= ~(1<<pin);
}
else
{
// 灭灯
*gpio_dat |= (1<<pin);
}

return 0;
}
static struct file_operations led_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   led_open,     
.write = led_write,   
};

static int led_probe(struct platform_device *pdev)
{
struct resource*res;

/* 根据platform_device的资源进行ioremap */
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
gpio_con = ioremap(res->start, res->end - res->start + 1);
gpio_dat = gpio_con + 1;

res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
pin = res->start;

/* 注册字符设备驱动程序 */

printk("led_probe, found led\n");

major = register_chrdev(0, "myled", &led_fops);

cls = class_create(THIS_MODULE, "myled");

class_device_create(cls, NULL, MKDEV(major, 0), NULL, "led"); /* /dev/led */

return 0;
}

static int led_remove(struct platform_device *pdev)
{
/* 卸载字符设备驱动程序 */
/* iounmap */
printk("led_remove, remove led\n");

class_device_destroy(cls, MKDEV(major, 0));
class_destroy(cls);
unregister_chrdev(major, "myled");
iounmap(gpio_con);

return 0;
}

struct platform_driver led_drv = {
.probe = led_probe,
.remove = led_remove,
.driver = {
.name = "myled",
}
};

static int led_drv_init(void)
{
platform_driver_register(&led_drv);
return 0;
}

static void led_drv_exit(void)
{
platform_driver_unregister(&led_drv);
}

module_init(led_drv_init);
module_exit(led_drv_exit);
MODULE_LICENSE("GPL");


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

/* led_test on
 * led_test off
 */
int main(int argc, char **argv)
{
int fd;
int val = 1;
fd = open("/dev/led", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
if (argc != 2)
{
printf("Usage :\n");
printf("%s <on|off>\n", argv[0]);
return 0;
}


if (strcmp(argv[1], "on") == 0)
{
val  = 1;
}
else
{
val = 0;
}

write(fd, &val, 4);
return 0;
}





0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 公公现在就想要白关婷中文字幕 丈夫上司侵犯系列在线 义父犯美媳嫁樱花叶菜在线播放 儿子的妻子在线中文字幕云播 老公去世公公每天晚上抱着我睡 被水电工掠夺的妻子在线播放b 佐佐木明希大嫂中文字幕全集 善良的小妻子字幕 公公在我洗碗时在厨房要了我 大嫂被肉翻了天佐佐木明希1 教师蕾丝短裙中文字幕 瓜棚里和大嫂乱禽 强睡大嫂中文字 免费 佐佐木明希 房东中文字幕 与父亲干柴烈火 中文字幕丈夫不在 9大嫂被禽翻中文字幕 侵犯你的贞洁 中文字幕 年轻的妻子在线观中文字幕 大嫂被翻天了佐佐木b希中文7 美丽的大嫂中文字幕影迅雷下载 邻居的妻子中文字幕下载 神马电影院电影中文 神马电影院理论中文 女儿的朋友5中文神马电影院 97手机2019电影院专用版中文 厨房里进入朋友的老婆 中文版电影院 神马电影院 中文 儿子的妻子中文字幕 下载 樱桃中文版电影院 大富豪电影院韩国中文 老婆的闺蜜们喝醉了在家 中文潮人影院您手中的电影院 朋友不在晚上去他家干 趁兄弟喝醉上他女朋友在线播放 神马电影院午伦中文 朋友喝醉上其妻 我朋友的妻子韩语中文2018 在朋友家趁朋友喝醉上他老婆 日本朋友的妻子和母亲中文版