7.1创建日志输出接口

7.1创建日志输出接口

//D:\code\x86\code\start\start\source\kernel\include\tools\log.h
#ifndef LOG_H
#define LOG_H


void log_init (void); //后面课时讲
void log_printf(const char * fmt, ...);//后面的可变参数

#endif // LOG_H

我们将输出到这个串行接口(不是VGA接口)上,下面代码是这个串行接口初始化和设置一些参数,不用学这个接口这个早就淘汰了,先学一下这个接口过渡一下,后面还是要写函数输出到屏幕上面来

//D:\code\x86\code\start\start\source\kernel\tools\log.c
#include "comm/cpu_instr.h"
#include "tools/log.h"
#include "os_cfg.h"

// 目标用串口,参考资料:https://wiki.osdev.org/Serial_Ports
#define COM1_PORT           0x3F8       // RS232端口0初始化
/**
 * @brief 初始化日志输出
 */
void log_init (void) {
    //初始化串行接口
    outb(COM1_PORT + 1, 0x00);    // Disable all interrupts
    outb(COM1_PORT + 3, 0x80);    // Enable DLAB (set baud rate divisor)
    outb(COM1_PORT + 0, 0x03);    // Set divisor to 3 (lo byte) 38400 baud
    outb(COM1_PORT + 1, 0x00);    //                  (hi byte)
    outb(COM1_PORT + 3, 0x03);    // 8 bits, no parity, one stop bit
    outb(COM1_PORT + 2, 0xC7);    // Enable FIFO, clear them, with 14-byte threshold
  
    // If serial is not faulty set it in normal operation mode
    // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
    outb(COM1_PORT + 4, 0x0F);
}

/**
 * @brief 日志打印
 */
void log_printf(const char * fmt, ...) {
    const char * p = fmt;
    while (*p != '\0') {
        while ((inb(COM1_PORT + 5) & (1 << 6)) == 0);   //看串行接口是不是处于忙的状态
        outb(COM1_PORT, *p++);
    }

    outb(COM1_PORT, '\r'); //换行,改变了列号变为0
    outb(COM1_PORT, '\n');//回车,改变了行号变为下一行行号
}

小知识点

这里面的\n和c语言里面的\n效果是不一样的

这里面\n+\r等价于c语言里面的\n

如果单独输出这个\n

如果单独输出这个\r

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值