实现开发板三盏灯点亮熄灭
typedef struct {
volatile unsigned int MODER; // 0x00
volatile unsigned int OTYPER; // 0x04
volatile unsigned int OSPEEDR; // 0x08
volatile unsigned int PUPDR; // 0x0C
volatile unsigned int IDR; // 0x10
volatile unsigned int ODR; // 0x14
volatile unsigned int BSRR; // 0x18
volatile unsigned int LCKR; // 0x1C
volatile unsigned int AFRL; // 0x20
volatile unsigned int AFRH; // 0x24
volatile unsigned int BRR; // 0x28
volatile unsigned int res;
volatile unsigned int SECCFGR; // 0x30
}gpio_t;
#define GPIOE (0x50006000)
#define GPIOF (0x50007000)
- 三盏灯逻辑 kbuf[0]:表示操作哪一盏灯 kbuf[1]:表示操作灯的状态
- kbuf[0] = 1==> 操作LED1 ==> kbuf[1] = 1 点亮 kbuf[1] = 0 熄灭
- kbuf[0] = 2 ==> 操作LED2 ==> kbuf[1] = 1 点亮 kbuf[1] = 0 熄灭
- kbuf[0] = 3 ==> 操作LED3 ==> kbuf[1] = 1 点亮 kbuf[1] = 0 熄灭
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uacce.h>
#include <linux/io.h>
#include "myled.h"
#define CNAME "myled"
unsigned int major = 0;
char kbuf[100] = "";
unsigned int *rcc_virt = NULL;
gpio_t *gpioe_virt = NULL;
gpio_t *gpiof_virt = NULL;
#define LED1_ON (gpioe_virt->ODR |= (0x1 << 10))
#define LED1_OFF (gpioe_virt->ODR &= (~(0x1 << 10)))
#define LED2_ON (gpiof_virt->ODR |= (0x1 << 10))
#define LED2_OFF (gpiof_virt->ODR &= (~(0x1 << 10)))
#define LED3_ON (gpioe_virt->ODR |= (0x1 << 8))
#define LED3_OFF (gpioe_virt->ODR &= (~(0x1 << 8)))
gpio_t gpio;
int myled_open(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
ssize_t myled_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loffs)
{
int ret;
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
if (size > sizeof(kbuf))
size = sizeof(kbuf);
ret = copy_from_user(kbuf, ubuf, size);
if (ret)
{
printk("copy from user is error\n");
return -EIO;
}
switch (kbuf[0])
{
case 1:
if (kbuf[1] == 1)
{
LED1_ON;
}
else
{
LED1_OFF;
}
break;
case 2:
if (kbuf[1] == 1)
{
LED2_ON;
}
else
{
LED2_OFF;
}
break;
case 3:
if (kbuf[1] == 1)
{
LED3_ON;
}
else
{
LED3_OFF;
}
break;
default:
break;
}
return size;
}
int myled_close(struct inode *inode, struct file *file)
{
printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
return 0;
}
const struct file_operations fops = {
.open = myled_open,
.write = myled_write,
.release = myled_close,
};
static int __init demo_init(void)
{
// 注册字符设备驱动
major = register_chrdev(0, CNAME, &fops);
if (major < 0)
{
printk("register chrdev is error\n");
return -EIO;
}
printk("major = %d\n", major);
rcc_virt = ioremap(RCC_MP_AHB4ENSETR, 4);
if (rcc_virt == NULL)
{
printk("ioremap rcc is error\n");
return -EIO;
}
gpioe_virt = ioremap(GPIOE, sizeof(gpio));
if (gpioe_virt == NULL)
{
printk("ioremap gpioe moder is error\n");
return -EIO;
}
gpiof_virt = ioremap(GPIOF, sizeof(gpio));
if (gpiof_virt == NULL)
{
printk("ioremap gpiof is error\n");
return -EIO;
}
*rcc_virt |= (0x3 << 4);
gpioe_virt->MODER &= (~(0x3 << 20));
gpioe_virt->MODER |= (0x1 << 20);
gpioe_virt->ODR |= (0x1 << 10);
gpiof_virt->MODER &= (~(0x3 << 20));
gpiof_virt->MODER |= (0x1 << 20);
gpiof_virt->ODR |= (0x1 << 10);
gpioe_virt->MODER &= (~(0x3 << 16));
gpioe_virt->MODER |= (0x1 << 16);
gpioe_virt->ODR |= (0x1 << 8);
return 0;
}
static void __exit demo_exit(void)
{
iounmap(gpiof_virt);
iounmap(gpioe_virt);
iounmap(rcc_virt);
// 注销字符设备驱动
unregister_chrdev(major, CNAME);
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");
#ifndef __MYLED_H__
#define __MYLED_H__
typedef struct {
volatile unsigned int MODER; // 0x00
volatile unsigned int OTYPER; // 0x04
volatile unsigned int OSPEEDR; // 0x08
volatile unsigned int PUPDR; // 0x0C
volatile unsigned int IDR; // 0x10
volatile unsigned int ODR; // 0x14
volatile unsigned int BSRR; // 0x18
volatile unsigned int LCKR; // 0x1C
volatile unsigned int AFRL; // 0x20
volatile unsigned int AFRH; // 0x24
volatile unsigned int BRR; // 0x28
volatile unsigned int res;
volatile unsigned int SECCFGR; // 0x30
}gpio_t;
#define RCC_MP_AHB4ENSETR 0x50000A28
// #define GPIOE_MODER 0x50006000
// #define GPIOE_ODR 0x50006014
#define GPIOE (0x50006000)
#define GPIOF (0x50007000)
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char buf[128] = "";
int fd = -1;
fd = open("/dev/myled", O_RDWR);
if (fd == -1)
{
perror("open is error");
exit(1);
}
// buf[0]=1 led1 buf[0]=2 led2 buf[0]=3 led3
// buf[1]=0灭 buf[1]=1亮
while (1)
{
buf[0] = 1;
buf[1] = 1;
write(fd, buf, sizeof(buf));
sleep(1);
buf[1] = 0;
write(fd, buf, sizeof(buf));
sleep(1);
buf[0] = 2;
buf[1] = 1;
write(fd, buf, sizeof(buf));
sleep(1);
buf[1] = 0;
write(fd, buf, sizeof(buf));
sleep(1);
buf[0] = 3;
buf[1] = 1;
write(fd, buf, sizeof(buf));
sleep(1);
buf[1] = 0;
write(fd, buf, sizeof(buf));
sleep(1);
}
close(fd);
return 0;
}