历史上的今天
今天是:2024年10月11日(星期五)
2018年10月11日 | OK6410分层分离(总线-驱动-设备模型)
2018-10-11 来源:eefocus
led_dev.c源码:
#include "linux/module.h"
#include "linux/moduleparam.h"
#include "linux/ioport.h"
#include "linux/init.h"
#include "linux/delay.h"
#include "linux/platform_device.h"
// 1. 分配一个platform_device
// 2. 设置
static struct resource led_resoures[] = {
[0] = {
.start = 0x7F008820, // GPMCON
.end = 0x7F008827,
.flags = IORESOURCE_MEM,
},
[1] = {
.start = 1,
.end = 1,
.flags = IORESOURCE_IRQ,
},
};
static void led_release(struct device *dev)
{
}
static struct platform_device led_dev = {
.name = "up6410_led",
.id = 0,
.num_resources = 2,
.resource = led_resoures,
.dev = {
.release = led_release,
},
};
// 3. 注册
static int led_dev_init(void)
{
platform_device_register(&led_dev);
return 0;
}
static void led_dev_exit(void)
{
platform_device_del(&led_dev);
}
module_init(led_dev_init);
module_exit(led_dev_exit);
MODULE_LICENSE("GPL");
=====================================================================
led_drv.c源码:
#include "linux/module.h"
#include "linux/compat.h"
#include "linux/types.h"
#include "linux/errno.h"
#include "linux/smp_lock.h"
#include "linux/kernel.h"
#include "linux/major.h"
#include "linux/slab.h"
#include "linux/mm.h"
#include "linux/mman.h"
#include "linux/vt.h"
#include "linux/init.h"
#include "linux/linux_logo.h"
#include "linux/proc_fs.h"
#include "linux/seq_file.h"
#include "linux/console.h"
#include "linux/kmod.h"
#include "linux/err.h"
#include "linux/device.h"
#include "asm/uaccess.h"
#include "asm/io.h"
#include "linux/platform_device.h"
static int major = 0;
static volatile unsigned long *gpxcon;
static volatile unsigned long *gpxdat;
static int pin;
static struct class *cls;
static int led_open(struct inode *inode, struct file *file)
{
*gpxcon &= ~(0xf << (pin * 4));
*gpxcon |= (0x1 << (pin * 4));
return 0;
}
static ssize_t led_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
{
char val;
copy_from_user(&val, buf, 1);
if (val == 0)
{
// 开灯
*gpxdat &= ~(1< } else { *gpxdat |= (1< } return 1; } static struct file_operations led_fops = { .owner = THIS_MODULE, .open = led_open, .write = led_write, }; static int led_probe(struct platform_device *dev) { struct resource *res; res = platform_get_resource(dev, IORESOURCE_MEM, 0); gpxcon = ioremap(res->start, res->end - res->start + 1); gpxdat = gpxcon + 1; res = platform_get_resource(dev, IORESOURCE_IRQ, 0); pin = res->start; major = register_chrdev(0, "led", &led_fops); cls = class_create(THIS_MODULE, "led"); // sysfs device_create(cls, NULL, MKDEV(major, 0), NULL, "led"); return 0; } static int led_remove(struct platform_device *dev) { device_destroy(cls, MKDEV(major, 0)); class_destroy(cls); unregister_chrdev(major, "led"); iounmap(gpxcon); return 0; } // 1. 分配一个platform_driver // 2. 设置 static struct platform_driver led_drv = { .probe = led_probe, .remove = __devexit_p(led_remove), .driver = { .name = "up6410_led", .owner = THIS_MODULE, }, }; // 3. 注册 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 "sys/mman.h" #include "stdio.h" #include "stdlib.h" #include "string.h" void print_usage(char *file) { printf("Usage:\n"); printf("%s \n", file); } int main(int argc, char **argv) { int fd; char val; if (argc != 2) { print_usage(argv[0]); return -1; } fd = open("/dev/led", O_RDWR); if (fd < 0) { printf("can't open /dev/led\n"); return -1; } if (strcmp(argv[1], "on") == 0) { val = 0; } else { val = 1; } write(fd, &val, 1); return 0; } ==================================================================== Makefile文件: KERN_DIR = /home/linux/linux-3.0.1 all: make -C $(KERN_DIR) M=`pwd` modules clean: make -C $(KERN_DIR) M=`pwd` modules clean rm -rf modules.order obj-m += led_drv.o obj-m += led_dev.o ==================================================================== 编译驱动程序:make 编译应用程序:arm-linux-gcc led_test led_test.c
上一篇:OK6410之USB设备驱动程序
下一篇:OK6410触摸屏驱动
史海拾趣
|
哥们今天刚画的16口以太网交换机的原理图(改进过)哥们今天刚画的16口以太网交换机的原理图(PROTEL99SE格式),改进过的,朋友们辛苦啦! [ 本帖最后由 西门 于 2009-5-12 18:50 编辑 ]… 查看全部问答> |
|
LM3S系列单片机采用的是结构简单、性能出众的ARM Cortex-M3处理器内核。Luminary官方(已被TI收购)免费公开地提供了基于C语言(符合ANSI C标准)的驱动库软件包,并且源代码是公开的。因此用户完全可以摒弃晦涩难懂的汇编语言,也不需要掌握底层寄 ...… 查看全部问答> |
|
这是一本关于Intel 80C51 以及广大的51 系列单片机的书 这本书介绍给读者一些新的技术使你的8051 工程和开发过程变得简单 请注意这本书的目的可不是教你各种8051 嵌入式系统的解决方法。 周立功… 查看全部问答> |
|
悬赏2000元现金,解决液晶屏闪烁问题(北京回龙观龙泽附近) 选用的S3C2440A,Wince系统。直接用群创的8寸800*600的LCD(需要时序板)可以正常工作。新选用了AUO的7寸屏也是800*600(不需要时序板)启动过程中闪烁,进入wince桌面后不闪,但是只要切换系统界面就会闪烁。(AUO的屏的DCLK范围是35-45Mhz) 由于着急 ...… 查看全部问答> |
|
我把nboot烧写到FLASH的BL0块中,然后把EBOOT烧写到BL2块中,然后通过EBOOT把NK.BIN再下载到FLASH中, 问题一: 那么这个NK.BIN是被EBOOT下载到FLASH哪个地方的呢?是不是代码在EBOOT哪个地方?还是在别的地方?能否告诉下小弟! 问题二: 为什么EBOOT非 ...… 查看全部问答> |
|
博创平台UPNET2410-S 键盘由MEGA8控制,MEGA8接IIC总线。 #define I2COWNER_ADDRESS (0x10>>1) #define MEGA8_I2CADDRESS (0x60 ...… 查看全部问答> |
|
我用的GSM模块是EM310 现在硬件电路还没出来,公司让我写设计先, 我想知道,如果发送命令 第一步:(AT%ETCPIP=”dtu”,”123”), 注册网络用户名dtu和密码123 第二步:(AT%ETCPIP= ,”333”) 那么用户名是被设置为了默认值还是空或者还 ...… 查看全部问答> |




