代码
mycdev.c
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include "myled.h"
#include <linux/device.h>
#define CNAME "myled"
int major;
char kbuf[128] = {0};
gpio_t *virt_gpioe = NULL;
gpio_t *virt_gpiof = NULL;
unsigned int *virt_rcc = NULL;
#define LED1_ON (virt_gpioe->ODR |= (0x1 << 10))
#define LED1_OFF (virt_gpioe->ODR &= (~(0x1 << 10)))
#define LED2_ON (virt_gpiof->ODR |= (0x1 << 10))
#define LED2_OFF (virt_gpiof->ODR &= (~(0x1 << 10)))
#define LED3_ON (virt_gpioe->ODR |= (0x1 << 8))
#define LED3_OFF (virt_gpioe->ODR &= (~(0x1 << 8)))
struct class *cls;
struct device *dev;
int myled_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
ssize_t myled_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
int ret;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
// 1.校验传输数据的大小,如果用户空间写的数据比内核空间数据大小大,需要更正大小
if (size > sizeof(kbuf))
size = sizeof(kbuf);
// 2.将数据从用户空间拷贝到内核空间
ret = copy_to_user(ubuf, kbuf, size);
if (ret) // 3.判断是否错误
{
printk("copy to user is error\n");
return -EIO;
}
return size; // 5.返回拷贝数据大小
}
ssize_t myled_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
int ret;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
// 1.校验传输数据的大小,如果用户空间写的数据比内核空间数据大小大,需要更正大小
if (size > sizeof(kbuf))
size = sizeof(kbuf);
// 2.将数据从用户空间拷贝到内核空间
ret = copy_fro