1 驱动程序(根据用户空间写入的信息来控制LED灯)
/*
* Filename: /home/ubuntu/vscode/day2_/char_dev/mycdev.c
* Path: /home/ubuntu/vscode/day2_/char_dev
* Created Date: Tuesday, June 13th 2023, 1:39:34 pm
* Author: xinyue
*
* Copyright (c) 2023 Your Company
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include "led.h"
#include <linux/io.h>
//主设备号
int major = 0;
char kbuf[1024] = {0};
char * my_strcat (char * dst, const char * src)
{
char * cp = dst;
while( *cp )
cp++; /* find end of dst */
while((*cp++ = *src++) != '\0') ; /* Copy src to end of dst */
return( dst ); /* return dst */
}
ssize_t mycdev_read (struct file *file, char __user * ubuf, size_t size, loff_t * lof)
{
unsigned long ret;
if(size > sizeof(kbuf))
{
size = sizeof(kbuf); //防止溢出
}
my_strcat(kbuf, "_core");
ret = copy_to_user(ubuf, kbuf, size);
if(ret)
{
printk("copy to user failed\n");
}
printk("%s:%s:%d\n", __FUNCTION__, __FILE__, __LINE__);
return 0;
}
//判断用户写输入的数据
void led_state(void)
{
if(kbuf[0] == '1')
{
if(kbuf[1] == '0')
{
*vir_led_odr &= ~(0x1 << 10);
printk("led1_off\n");
}
else if(kbuf[1] == '1')
{
*vir_led_odr |= (0x1 << 10);
printk("led1_on\n");
}
}
if(kbuf[0] == '2')
{
if(kbuf[1] == '0')
{
*vir_led_odr_f &= ~(0x1 << 10);
printk("led2_off\n");
}
else if(kbuf[1] == '1')
{
*vir_led_odr_f |= (0x1 << 10);
printk("led2_on\n");
}
}
if(kbuf[0] == '3')
{
if(kbuf[1] == '0')
{
*vir_led_odr &= ~(0x1 << 8);
printk("led3_off\n");
}
else if(kbuf[1] == '1')
{
*vir_led_odr |= (0x1 << 8);
printk("led3_on\n");
}
}
}
ssize_t mycdev_write (struct file * file, const char __user * 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("copy from user failed\n");
}
printk("%s:%s:%d\n", __FUNCTION__, __FILE__, __LINE__);
//*vir_led_odr |= (0x1 << 10);
led_state();
return 0;
}
int mycdev_open (struct inode * inode, struct file * file)
{
printk("%s:%s:%d\n", __FUNCTION__, __FILE__, __LINE__);
return 0;
}
int mycdev_close (struct inode * inode, struct file * file)
{
printk("%s:%s:%d\n", __FUNCTION__, __FILE__, __LINE__);
return 0;
}
struct file_operations fops = {
.open=mycdev_open,
.read=mycdev_read,
.write=mycdev_write,
.release=mycdev_close
};
int vir_map(void)
{
vir_led_rcc = ioremap(PHY_LED_RCC, 4);
if(vir_led_rcc == NULL)
{
printk("vir_led_rcc failed\n");
return -1;
}
vir_led_moder = ioremap(PHY_LED_MODER, 4);
if(vir_led_moder == NULL)
{
printk("vir_led_moder failed\n");
return -1;
}
vir_led_moder_f = ioremap(PHY_LED_MODER_F, 4);
if(vir_led_moder_f == NULL)
{
printk("vir_led_moder_f failed\n");
return -1;
}
vir_led_odr = ioremap(PHY_LED_ODR, 4);
if(vir_led_odr == NULL)
{
printk("vir_led_odr failed\n");
return -1;
}
vir_led_odr_f = ioremap(PHY_LED_ODR_F, 4);
if(vir_led_odr_f == NULL)
{
printk("vir_led_odr_f failed\n");
return -1;
}
printk("vir_map successed\n");
return 0;
}
void led_init(void)
{
//PE
*vir_led_rcc |= (0x1 << 4);
//PF
*vir_led_rcc |= (0x1 << 5);
//PE10
*vir_led_moder &= ~(0x3 << 20);
*vir_led_moder |= (0x1 << 20);
//PF10
*vir_led_moder_f &= ~(0x3 << 20);
*vir_led_moder_f |= (0x1 << 20);
//PE8
*vir_led_moder &= ~(0x3 << 16);
*vir_led_moder |= (0x1 << 16);
}
static int __init mycdev_init(void)
{
major = register_chrdev(0, "mycdev", &fops);
if(0 == major)
{
printk("%s\n", "mycdev register failed");
return -1;
}
printk("%s:%s:%d major=%d\n", __FUNCTION__, __FILE__, __LINE__, major);
vir_map();
led_init();
return 0;
}
static void __exit mycdev_exit(void)
{
//取消映射
iounmap(vir_led_odr);
iounmap(vir_led_odr_f);
iounmap(vir_led_moder);
iounmap(vir_led_moder_f);
iounmap(vir_led_rcc);
unregister_chrdev(major, "mycdev");
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
2 LED.h
/*
* Filename: /home/ubuntu/vscode/day2_/char_dev/led.h
* Path: /home/ubuntu/vscode/day2_/char_dev
* Created Date: Tuesday, June 13th 2023, 4:20:09 pm
* Author: xinyue
*
* Copyright (c) 2023 Your Company
*/
#ifndef __LED_H__
#define __LED_H__
//物理地址
#define PHY_LED_RCC 0x50000A28
#define PHY_LED_MODER 0x50006000
#define PHY_LED_MODER_F 0x50007000
#define PHY_LED_ODR 0x50006014
#define PHY_LED_ODR_F 0x50007014
//虚拟地址
unsigned int * vir_led_rcc;
unsigned int * vir_led_moder;
unsigned int * vir_led_moder_f;
unsigned int * vir_led_odr;
unsigned int * vir_led_odr_f;
#endif
3 用户空间逻辑代码
/*
* Filename: /home/ubuntu/vscode/day2_/char_dev/test.c
* Path: /home/ubuntu/vscode/day2_/char_dev
* Created Date: Tuesday, June 13th 2023, 2:25:09 pm
* Author: xinyue
*
* Copyright (c) 2023 Your Company
*/
#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(void)
{
char buf[1024] = {0};
int fd = open("/dev/mycdev", O_RDWR);
if (fd < 0)
{
printf("open failed\n");
return -1;
}
while(1)
{
printf("请输入选择(10代表LED1熄灭)>>>");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = '\0'; //将'\n'置'\0'
write(fd, buf, sizeof(buf));
memset(buf, 0, sizeof(buf)); //清零
}
close(fd);
return 0;
}
4 使用串口工具进行测试