#include <linux/init.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <asm/io.h>
#define GPIO0 0x44E07000
#define GPIO_OE 0x134
#define GPIO_DATAOUT 0x13c
#define GPIO_CLEARDATAOUT 0x190
#define GPIO_SETDATAOUT 0x194
#define GPIO_CTRL 0x130
#define CONTROL_MODULE 0x44E10000
#define SPIO_CS1 0x960 //gpio0_6
#define ECAP0_IN 0x964
static struct class *leds_class;
static struct device *leds_class_dev;
struct LEDS{
volatile unsigned long *gpio_oe;
volatile unsigned long *gpio_dataout;
volatile unsigned long *gpio_setdataout;
volatile unsigned long *gpio_cleardataout;
volatile unsigned long *gpio_ctrl;
volatile unsigned long *spio_cs1;
volatile unsigned long *ecap0_in;
}_leds;
struct LEDS *leds = &_leds;
static int led_open(struct inode *inode , struct file *file)
{
printk("this is open!\n");
return 0;
}
static int led_close(struct inode *inode , struct file *file)
{
printk("this is close!\n");
return 0;
}
static long led_ioctl(struct file *file , unsigned int cmd ,
unsigned long arg)
{
printk("cmd is : %d\n" , cmd);
switch (cmd)
{
default:
break;
}
return 0;
}
static ssize_t led_read(struct file *file , char __user *buf , size_t count , loff_t *pos)
{
return 0;
}
static ssize_t led_write(struct file *file , const char __user *buf , size_t count , loff_t *pos)
{
int val;
copy_from_user(&val,buf,count);
printk("val is =%d\n",val);
if (val==1)
{
*(leds->gpio_dataout)&=~((0x1<<6)|(0x1<<7));//设置gpio输出高/低电平
}
else
{
*(leds->gpio_dataout)|=((0x1<<6)|(0x1<<7));
}
return 0;
}
static struct file_operations fops = {
.owner = THIS_MODULE,
.open = led_open,
.release = led_close,
.unlocked_ioctl = led_ioctl,
.read = led_read,
.write = led_write,
};
int major;
static int __init test_init(void)
{
major = register_chrdev(0, "leds_drv", &fops); // 注册, 告诉内核
leds_class = class_create(THIS_MODULE, "ledsdrv"); //自动创建设备节点
leds_class_dev= device_create(leds_class, NULL, MKDEV(major, 0), NULL, "leds");
leds->gpio_oe = (volatile unsigned long *)ioremap(GPIO0 + GPIO_OE , sizeof(volatile unsigned long ));
leds->gpio_dataout = (volatile unsigned long *)ioremap(GPIO0 + GPIO_DATAOUT, sizeof(volatile unsigned long ));
leds->gpio_setdataout =( volatile unsigned long *)ioremap(GPIO0 + GPIO_SETDATAOUT, sizeof(volatile unsigned long ));
leds->gpio_cleardataout = (volatile unsigned long *)ioremap(GPIO0 + GPIO_CLEARDATAOUT , sizeof(volatile unsigned long ));
leds->gpio_ctrl=(volatile unsigned long *)ioremap(GPIO0+GPIO_CTRL,sizeof(volatile unsigned long ));
leds->ecap0_in=(volatile unsigned long *)ioremap(CONTROL_MODULE+SPIO_CS1,sizeof(volatile unsigned long ));
leds->spio_cs1=(volatile unsigned long *)ioremap(CONTROL_MODULE+ECAP0_IN,sizeof(volatile unsigned long ));
printk("insmod the leds module!\n");
/*
*配置相关引脚的pin_mux模式;
*配置相关寄存器;
*首先是寄存器OE,输出使能;设置0为输出使能;
*其次是寄存器SETDATAOUT,设置允许输出位;设置1为允许;
*最后是寄存器DATAOUT,设置输出高低电平;
*/
*(leds->spio_cs1)|=(0x7);
*(leds->ecap0_in)|=(0x7);
*(leds->gpio_ctrl)&=~(0x1);
*(leds->gpio_oe) &= ~((0x1<<6)|(0x1<<7));
*(leds->gpio_setdataout) |= ((0x1<<6)|(0x1<<7));
*(leds->gpio_dataout) |= ((0x1<<6)|(0x1<<7));
*(leds->gpio_dataout) &= ~((0x1<<6)|(0x1<<7));
return 0;
}
static void __exit test_exit(void)
{
unregister_chrdev(major, "leds_drv"); // 卸载
device_unregister(leds_class_dev);
class_destroy(leds_class);
*(leds->gpio_cleardataout) |= 0xffffffff;
iounmap(leds->gpio_oe);
iounmap(leds->gpio_dataout);
iounmap(leds->gpio_setdataout);
iounmap(leds->gpio_cleardataout);
iounmap(leds->ecap0_in);
iounmap(leds->gpio_ctrl);
iounmap(leds->spio_cs1);
}
module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("xiaogao");
MODULE_DESCRIPTION("Class test");
没有合适的资源?快使用搜索试试~ 我知道了~
LINUX结构,驱动结构,架构描述,经典书(linux linux linux linux linux linux
共5个文件
c:4个
makefile:1个
需积分: 5 0 下载量 110 浏览量
2024-08-15
11:12:32
上传
评论
收藏 4KB ZIP 举报
温馨提示
【Linux】LINUX结构,驱动结构,架构描述,经典书(linux linux linux linux linux linux linux linux linux linux linux ) 【Linux】LINUX结构,驱动结构,架构描述,经典书 (linux linux linux linux linux linux linux linux linux linux linux ) 【Linux】LINUX结构,驱动结构,架构描述,经典书 (linux linux linux linux linux linux linux linux linux linux linux )
资源推荐
资源详情
资源评论
收起资源包目录
2372679.zip (5个子文件)
2372679
am335x_gpio
led_drv - 副本.c 4KB
Makefile 169B
led_drv.c 4KB
readme.c 119B
main.c 429B
共 5 条
- 1
资源评论
hyzixue
- 粉丝: 41
- 资源: 163
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功