mycdev.c
#include
#include
#include
#include
/* myleds{
led1= <&gpioe 10 0>;
led2= <&gpiof 10 0>;
led3= <&gpioe 8 0>;
};
*/
char kbuf[128];
struct device_node *dnode;
int gpiono1, gpiono2, gpiono3;
int mycdev_open(struct inode *inode, struct file *file)
{
return 0;
}
ssize_t mycdev_read(struct file *file, char *ubuf, size_t size, loff_t *lof)
{
return 0;
}
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
unsigned long ret;
// 从用户空间读取数据
if (size > sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_from_user(kbuf, ubuf, size);
if (ret)
{
printk("从用户拷贝失败\n");
return ret;
}
switch (kbuf[0])
{
case '1':
gpio_set_value(gpiono1, 1);
break;
case '2':
gpio_set_value(gpiono1, 0);
break;
case '3':
gpio_set_value(gpiono2, 1);
break;
case '4':
gpio_set_value(gpiono2, 0);
break;
case '5':
gpio_set_value(gpiono3, 1);
break;
case '6':
gpio_set_value(gpiono3, 0);
break;
}
return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
return 0;
}
struct file_operations fops = {
.open = mycdev_open,
.read = mycdev_read,
.write = mycdev_write,
.release = mycdev_close,
};
int major;
static int __init mycdev_init(void)
{
// 注册字符设备驱动
major = register_chrdev(0, "mycdev", &fops);
if (major < 0)
{
printk("字符设备驱动注册失败\n");
return major;
}
printk("字符设备驱动注册成功%d\n", major);
// 通过名字解析设备树驱动注册
dnode = of_find_node_by_name(NULL, "myleds");
if (dnode == NULL)
{
printk("设备树节点解析失败\n");
return -ENOMEM;
}
printk("获取设备树节点成功\n");
// 根据解析到的设备树节点信息结构体解析出GPIO编号
gpiono1 = of_get_named_gpio(dnode, "led1", 0);
if (gpiono1 < 0)
{
printk("获取GPIO编号失败\n");
return 0;
}
printk("获取GPIO编号成功%d\n", gpiono1);
gpiono2 = of_get_named_gpio(dnode, "led2", 0);
if (gpiono1 < 0)
{
printk("获取GPIO编号失败\n");
return 0;
}
printk("获取GPIO编号成功%d\n", gpiono2);
gpiono3 = of_get_named_gpio(dnode, "led3", 0);
if (gpiono1 < 0)
{
printk("获取GPIO编号失败\n");
return 0;
}
printk("获取GPIO编号成功%d\n", gpiono3);
// 申请GPIO编号
gpio_request(gpiono1, NULL);
gpio_request(gpiono2, NULL);
gpio_request(gpiono3, NULL);
// 设置GPIO方向为输出并默认输出低电平
gpio_direction_output(gpiono1, 0);
gpio_direction_output(gpiono2, 0);
gpio_direction_output(gpiono3, 0);
// 默认关灯
gpio_set_value(gpiono1, 0);
gpio_set_value(gpiono2, 0);
gpio_set_value(gpiono3, 0);
return 0;
}
static void __exit mycdev_exit(void)
{
// 灭灯
gpio_set_value(gpiono1, 0);
gpio_set_value(gpiono2, 0);
gpio_set_value(gpiono3, 0);
// 释放GPIO编号
gpio_free(gpiono1);
gpio_free(gpiono2);
gpio_free(gpiono3);
// 注销字符设备驱动
unregister_chrdev(major, "mycdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
test.c
#include
#include
#include
#include
#include
#include
int main(int argc,const char * argv[])
{
char buf[128]={0};
int fd=open("/dev/mycdev",O_RDWR);
if(fd<0){
printf("打开设备文件失败\n");
}
while(1){
//从终端读取
printf("请输入操作那个灯\n");
printf("1(LED1亮) 2(LED1灭) \n3(LED2亮) 4(LED2灭) \n5(LED3亮) 6(LED3灭)\n");
printf("请输入>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
//向设备文件中写
write(fd,buf,sizeof(buf));
}
close(fd);
return 0;
}