Mr Dk.'s BlogMr Dk.'s Blog
  • 🦆 About Me
  • ⛏️ Technology Stack
  • 🔗 Links
  • 🗒️ About Blog
  • Algorithm
  • C++
  • Compiler
  • Cryptography
  • DevOps
  • Docker
  • Git
  • Java
  • Linux
  • MS Office
  • MySQL
  • Network
  • Operating System
  • Performance
  • PostgreSQL
  • Productivity
  • Solidity
  • Vue.js
  • Web
  • Wireless
  • 🐧 How Linux Works (notes)
  • 🐧 Linux Kernel Comments (notes)
  • 🐧 Linux Kernel Development (notes)
  • 🐤 μc/OS-II Source Code (notes)
  • ☕ Understanding the JVM (notes)
  • ⛸️ Redis Implementation (notes)
  • 🗜️ Understanding Nginx (notes)
  • ⚙️ Netty in Action (notes)
  • ☁️ Spring Microservices (notes)
  • ⚒️ The Annotated STL Sources (notes)
  • ☕ Java Development Kit 8
GitHub
  • 🦆 About Me
  • ⛏️ Technology Stack
  • 🔗 Links
  • 🗒️ About Blog
  • Algorithm
  • C++
  • Compiler
  • Cryptography
  • DevOps
  • Docker
  • Git
  • Java
  • Linux
  • MS Office
  • MySQL
  • Network
  • Operating System
  • Performance
  • PostgreSQL
  • Productivity
  • Solidity
  • Vue.js
  • Web
  • Wireless
  • 🐧 How Linux Works (notes)
  • 🐧 Linux Kernel Comments (notes)
  • 🐧 Linux Kernel Development (notes)
  • 🐤 μc/OS-II Source Code (notes)
  • ☕ Understanding the JVM (notes)
  • ⛸️ Redis Implementation (notes)
  • 🗜️ Understanding Nginx (notes)
  • ⚙️ Netty in Action (notes)
  • ☁️ Spring Microservices (notes)
  • ⚒️ The Annotated STL Sources (notes)
  • ☕ Java Development Kit 8
GitHub
  • 🐧 Linux Kernel Comments
    • Chapter 2 - 微型计算机组成结构

      • Chapter 2 - 微型计算机组成结构
    • Chapter 3 - 内核编程语言和环境

      • Chapter 3 - 内核编程语言和环境
    • Chapter 4 - 80X86 保护模式及其编程

      • Chapter 4.1-4.2 - 80X86 系统寄存器和系统指令 & 保护模式内存管理
      • Chapter 4.3 - 分段机制
      • Chapter 4.4 - 分页机制
      • Chapter 4.5 - 保护
      • Chapter 4.6 - 中断和异常处理
      • Chapter 4.7 - 任务管理
      • Chapter 4.8 - 保护模式编程初始化
      • Chapter 4.9 - 一个简单的多任务内核实例
    • Chapter 5 - Linux 内核体系结构

      • Chapter 5.1-5.2 - Linux 内核模式 & 体系结构
      • Chapter 5.3 - Linux 内核对内存的管理和使用
      • Chapter 5.4-5.6 - 中断机制 & 系统调用 & 系统时间和定时
      • Chapter 5.7-5.9 - Linux 进程控制 & 堆栈使用 & 文件系统
    • Chapter 6 - 引导启动程序 (boot)

      • Chapter 6 - 引导启动程序 (boot)
    • Chapter 7 - 初始化程序 (init)

      • Chapter 7 - 初始化程序 (init)
    • Chapter 8 - 内核代码

      • Chapter 8.1 - 内核代码总体功能
      • Chapter 8.2 - asm.s 程序
      • Chapter 8.3 - traps.c 程序
      • Chapter 8.4 - sys_call.s 程序
      • Chapter 8.5 - mktime.c 程序
      • Chapter 8.6 - sched.c 程序
      • Chapter 8.7 - signal.c 程序
      • Chapter 8.8 - exit.c 程序
      • Chapter 8.9 - fork.c 程序
      • Chapter 8.10 - sys.c 程序
    • Chapter 9 - 块设备驱动程序

      • Chapter 9.1 - 块设备驱动程序 总体功能
      • Chapter 9.2 - blk.h 文件
      • Chapter 9.3 - hd.c 程序
      • Chapter 9.4 - ll_rw_blk.c 程序
      • Chapter 9.5 - ramdisk.c 程序
    • Chapter 10 - 字符设备驱动程序

      • Chapter 10.1 - 字符设备驱动程序 总体功能
      • Chapter 10.2 - keyboard.S 程序
      • Chapter 10.3 - console.c 程序
      • Chapter 10.4 - serial.c 程序
      • Chapter 10.5 - rs_io.s 程序
      • Chapter 10.6 - tty_io.c 程序
      • Chapter 10.7 - tty_ioctl.c 程序
    • Chapter 12 - 文件系统

      • Chapter 12.1 - 文件系统 总体功能
      • Chapter 12.2 - buffer.c 程序
      • Chapter 12.3 - bitmap.c 程序
      • Chapter 12.4 - truncate.c 程序
      • Chapter 12.5 - inode.c 程序
      • Chapter 12.6 - super.c 程序
      • Chapter 12.7 - namei.c 程序
      • Chapter 12.9 - block_dev.c 程序
      • Chapter 12.10 - file_dev.c 程序
      • Chapter 12.11 - pipe.c 程序
      • Chapter 12.12 - char_dev.c 程序
      • Chapter 12.13 - read_write.c 程序
      • Chapter 12.14 - open.c 程序
      • Chapter 12.15 - exec.c 程序
      • Chapter 12.16 - stat.c 程序
      • Chapter 12.17 - fcntl.c 程序
      • Chapter 12.18 - ioctl.c 程序
      • Chapter 12.19 - select.c 程序

Chapter 10.4 - serial.c 程序

Created by : Mr Dk.

2019 / 08 / 26 17:18

Ningbo, Zhejiang, China


10.4 serial.c 程序

10.4.1 功能描述

本程序实现系统串行端口的初始化,为使用串行终端设备做好准备工作。

rs_init() 函数:

  • 设置了默认的串行通信参数
  • 设置串行端口的中断陷阱门

rs_write() 函数:把用于串行终端设备的写缓冲队列中的字符通过串行线路发给远端的终端设备。在文件系统中,操作字符设备文件时被调用。实际上只是开启 串行发送保持器已空中断标志,在 UART 将数据发送出去以后,允许发送中断信号。具体的发送操作在 rs_io.s 程序中完成。

10.4.2 代码注释

init() - 串行端口初始化

设置指定串行端口的波特率,允许除 写保持寄存器空 以外的所有中断源。参数 port 为串行端口基地址:

static void init(int port)
{
    outb_p(0x80, port + 3);
    outb_p(0x30, port);
    outb_p(0x00, port + 1);
    outb_p(0x03, port + 3);
    outb_p(0x0b, port + 4);
    outb_p(0x0d, port + 1);
    (void) inb (port);
}

rs_init() - 初始化串行中断程序和串行接口

void rs_init(void)
{
    set_intr_gate(0x24, rs1_interrupt); // 串口 1 的中断向量 - IRQ4
    set_intr_gate(0x23, rs2_interrupt); // 串口 2 的中断向量 - IRQ3
    init(tty_table[64].read_q->data); // 初始化串口 1
    init(tty_table[65].read_q->data); // 初始化串口 2
    outb(inb_p(0x21) & 0xE7, 0x21); // 允许 8259A 响应 IRQ3、IRQ4
}

rs_write() - 串行接口写函数

该函数在 tty_write() 已将数据放入写缓冲队列后被调用。在该程序中,首先检查写队列是否为空,然后设置相应的中断寄存器。该函数仅开启 发送保持寄存器已空中断 的标志。以下过程由串口中断处理函数完成。

发送保持寄存器为空时,UART 就会产生中断。在中断处理程序中,取出写缓冲队列尾部的字符,输出到发送保持寄存器中。发送出去后,发送保持寄存器变空,从而再次引发中断请求,直到所有字符被发送。此时,程序重新禁止发送保持寄存器已空时发出的中断。

void rs_write(struct tty_struct * tty)
{
    cli(); // 关中断
    if (!EMPTY(tty->write_q))
        // 写队列不为空
        // 读取中断允许寄存器,置位允许中断标志后,写回该寄存器
        outb(inb_p(tty->write_q->data+1) | 0x02, tty->write_q->data+1);
    sti(); // 开中断
}
Edit this page on GitHub
Prev
Chapter 10.3 - console.c 程序
Next
Chapter 10.5 - rs_io.s 程序