打印到proc虚拟文件

25 篇文章 0 订阅
1 篇文章 0 订阅

1、系统默认的打印缓冲区/proc/kmsg

通过dmsge或cat/proc/kmsg可查看打印信息

cat proc/kmsg
<6>Booting Linux on physical CPU 0x0
<6>Initializing cgroup subsys cpu
<5>Linux version 3.18.20 (osrc@osrc) (gcc version 4.9.4 20150629 (prerelease) (Hisilicon_v500_20170922) ) #25 SMP Mon Jan 13 02:11:11 EST 2020
<6>CPU: ARMv7 Processor [410fc075] revision 5 (ARMv7), cr=10c5387d
<6>CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
<6>Machine model: Hisilicon HI3519V101 DEMO Board
<6>cmz zone is not set!
<6>cma: Reserved 16 MiB at 0x85000000
<6>Memory policy: Data cache writealloc
<7>On node 0 totalpages: 24576

为打印级别

2、制作属于自己的打印缓冲区

2.1proc_mymsg/mymsg.c

//参考 fs/proc/kmsg.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/uaccess.h>
#include <linux/irq.h>
#include <asm/irq.h>
#include <linux/interrupt.h>
#include <linux/poll.h>
#include <linux/sched.h>

#define MYLOG_BUF_LEN 1024
static DECLARE_WAIT_QUEUE_HEAD(mylog_waitq);

static char mylog_buf[MYLOG_BUF_LEN];
static char tmp_buf[MYLOG_BUF_LEN];
static int mylog_r = 0, mylog_w = 0;

//判断空
static int is_mylog_empty(void)
{
    return (mylog_r == mylog_w);
}

//判断满
static int is_mylog_full(void)
{
    return((mylog_w + 1) % MYLOG_BUF_LEN == mylog_r);
}

//写入数据
static void mylog_putc(char c)
{
    if(is_mylog_full())
        mylog_r =  (mylog_r + 1) % MYLOG_BUF_LEN;
    mylog_buf[mylog_w] = c;
    mylog_w =  (mylog_w + 1) % MYLOG_BUF_LEN;

    wake_up_interruptible(&mylog_waitq); //唤醒休眠的进程
}

//读出数据
static int mylog_getc(char *p)
{
    if(is_mylog_empty())
        return 0;
    *p = mylog_buf[mylog_r];
    mylog_r =  (mylog_r + 1) % MYLOG_BUF_LEN;
    return 1;
}

int myprintk(const char *fmt, ...)
{
	va_list args;
	int i;
    int j;

	va_start(args, fmt);
	i = vsnprintf(tmp_buf, INT_MAX, fmt, args);
	va_end(args);

    for(j = 0; j < i;j++)
        mylog_putc(tmp_buf[j]);

	return i;
}

static ssize_t mymsg_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
    int error = 0;
    int i = 0;
    char c;

	if ((file->f_flags & O_NONBLOCK) && is_mylog_empty())
		return -EAGAIN;
    //置1 不进休眠,清0 进休眠
    error = wait_event_interruptible(mylog_waitq, !is_mylog_empty());

    while (!error && (mylog_getc(&c)) && i < count)
    {
        error = __put_user(c, buf);
		buf++;
        i++;        
    }
    if(!error)
        error = i;

    return error;
}

const struct file_operations proc_mymsg_operations = {
    .read = mymsg_read,
};

static int mymsg_init(void)
{
    sprintf(mylog_buf, "sajfsdjfauwaoerfsf");
    proc_create("mymsg", S_IRUSR, NULL, &proc_mymsg_operations);
	return 0;
}

static void mymsg_exit(void)
{
    remove_proc_entry("mymsg", NULL);
}

module_init(mymsg_init);
module_exit(mymsg_exit);

EXPORT_SYMBOL(myprintk);
MODULE_LICENSE("GPL");

2.2proc_mymsg/test_myprintk.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <asm/io.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include <linux/ioport.h>
#include <linux/of.h>
#include <linux/uaccess.h>

extern int myprintk(const char *fmt, ...);

// 操作管脚为gpio3_2
// 管脚复用寄存器默认为gpio,这里就不去操作了
#define led_phy_base 0x12143000 //led管脚物理地址
#define led_phy_data 0x010		//led管脚数据偏移地址,0b00_0001_0000

#define gpio_phy_dir 0x400		//管脚方向寄存器偏移地址
#define gpio_out 0x4 //gpio3_2 配置为输出,1输出 0输入def
#define gpio_out_h 0xFF
#define gpio_out_l 0x00

static struct class *leddrv_class;
static struct class_device *leddrv_class_dev;

static void __iomem *led_base; //寄存器基地址
static void __iomem *led_dir;  //方向

static int led_drv_open(struct inode *inode, struct file *filp)
{
	iowrite8(gpio_out, led_dir);
	myprintk("set gpio out mode\n");
	return 0;
}

static int led_drv_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{
	int val;

	__copy_from_user(&val, buf, count); //用户到内核,__copy_to_user();//内核到用户
	if (val == 1)
	{
		iowrite8(gpio_out_h, led_base);
		myprintk("set gpio out h\r\n");
	}
	else
	{
		iowrite8(gpio_out_l, led_base);
		myprintk("set gpio out l\r\n");
	}

	return 0;
}

static struct file_operations led_drv_fops =
{
		.owner = THIS_MODULE, //这是一个宏,推向编译模块时自动创建的__this_module变量
		.open = led_drv_open,
		.write = led_drv_write,
};

int major;

int led_drv_init(void)
{

	major = register_chrdev(0, "led_drv", &led_drv_fops); // 注册, 告诉内核
	leddrv_class = class_create(THIS_MODULE, "led_drv");
	leddrv_class_dev = device_create(leddrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); //dev/xyz

	//地址映射:把物理地址转换为虚拟地址
	led_base = ioremap(led_phy_base, 1); //读取1个字节长度够用了
	led_dir = led_base + gpio_phy_dir;
	led_base = led_base + led_phy_data;

	myprintk("led_base = 0x%x\n", (unsigned int)led_base);
	myprintk("led_dir = 0x%x\n", (unsigned int)led_dir);
	return 0;
}

void led_drv_exit(void)
{
	unregister_chrdev(major, "led_drv");
	device_unregister(leddrv_class_dev);
	class_destroy(leddrv_class);
	iounmap(led_base); //取消物理地址的映射
}

module_init(led_drv_init); //模块初始化接口
module_exit(led_drv_exit); //模块推出接口
MODULE_LICENSE("GPL");

2.3 测试

只能cat一次

insmod mymsg.ko
insmod test_myprintk.ko
cat proc/mymsg
led_base = 0xfea43010
led_dir = 0xfea43400
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值