From f8b55f6d3fcbf152a1cb6d6fc722bf1607418b28 Mon Sep 17 00:00:00 2001 From: TingHuang <92705854+TingSHub@users.noreply.github.com> Date: Tue, 6 Dec 2022 22:15:03 +0800 Subject: [PATCH] Patch uart (#99) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 添加UART驱动相关文件 * 添加驱动核心文件,将rust编写的驱动代码加入Package中 * 添加glib.h文件生成rust代码,添加uart驱动代码 * 添加串口发送及接收相关代码 * 添加字符串发送函数,未实现具体功能 * 为调用uart驱动的代码添加rust接口 * 添加字符串发送函数,修改C语言调用接口 * 添加rust串口驱动 * 添加uart.h头文件,将串口端口类型改为enum * 添加注释,规范代码 --- kernel/src/driver/Makefile | 2 +- kernel/src/driver/mod.rs | 1 + kernel/src/driver/uart/Makefile | 7 - kernel/src/driver/uart/mod.rs | 1 + kernel/src/driver/uart/uart.c | 112 ----------- kernel/src/driver/uart/uart.h | 63 +----- kernel/src/driver/uart/uart.rs | 258 +++++++++++++++++++++++++ kernel/src/driver/video/video.c | 2 +- kernel/src/include/bindings/wrapper.h | 2 +- kernel/src/lib.rs | 1 + kernel/src/libs/libUI/screen_manager.c | 7 +- kernel/src/libs/libUI/textui.c | 14 +- kernel/src/main.c | 2 +- 13 files changed, 280 insertions(+), 192 deletions(-) create mode 100644 kernel/src/driver/mod.rs delete mode 100644 kernel/src/driver/uart/Makefile create mode 100644 kernel/src/driver/uart/mod.rs delete mode 100644 kernel/src/driver/uart/uart.c create mode 100644 kernel/src/driver/uart/uart.rs diff --git a/kernel/src/driver/Makefile b/kernel/src/driver/Makefile index 3b84c7ef..a20d91ec 100644 --- a/kernel/src/driver/Makefile +++ b/kernel/src/driver/Makefile @@ -1,7 +1,7 @@ CFLAGS += -I . -kernel_driver_subdirs:=video interrupt usb pci uart acpi disk keyboard mouse multiboot2 timers tty hid +kernel_driver_subdirs:=video interrupt usb pci acpi disk keyboard mouse multiboot2 timers tty hid ECHO: @echo "$@" diff --git a/kernel/src/driver/mod.rs b/kernel/src/driver/mod.rs new file mode 100644 index 00000000..8c203607 --- /dev/null +++ b/kernel/src/driver/mod.rs @@ -0,0 +1 @@ +pub mod uart; diff --git a/kernel/src/driver/uart/Makefile b/kernel/src/driver/uart/Makefile deleted file mode 100644 index 9f068e5f..00000000 --- a/kernel/src/driver/uart/Makefile +++ /dev/null @@ -1,7 +0,0 @@ - -all: uart.o - -CFLAGS += -I . - -uart.o: uart.c - $(CC) $(CFLAGS) -c uart.c -o uart.o diff --git a/kernel/src/driver/uart/mod.rs b/kernel/src/driver/uart/mod.rs new file mode 100644 index 00000000..8c203607 --- /dev/null +++ b/kernel/src/driver/uart/mod.rs @@ -0,0 +1 @@ +pub mod uart; diff --git a/kernel/src/driver/uart/uart.c b/kernel/src/driver/uart/uart.c deleted file mode 100644 index c17e3073..00000000 --- a/kernel/src/driver/uart/uart.c +++ /dev/null @@ -1,112 +0,0 @@ -#include "uart.h" -#include - -#define UART_MAX_BITS_RATE 115200 - -/** - * @brief 当前是否有数据到达 - * - */ -#define serial_received(p) ((io_in8(p + 5) & 1)) - -/** - * @brief 当前是否有数据正等待发送 - * - */ -#define is_transmit_empty(p) ((io_in8(p + 5) & 0x20)) - -/** - * @brief 初始化com口 - * - * @param port com口的端口号 - * @param bits_rate 通信的比特率 - */ -int uart_init(uint32_t port, uint32_t bits_rate) -{ - // 错误的比特率 - if (bits_rate > UART_MAX_BITS_RATE || UART_MAX_BITS_RATE % bits_rate != 0) - return E_UART_BITS_RATE_ERROR; - - io_out8(port + 1, 0x00); // Disable all interrupts - io_out8(port + 3, 0x80); // Enable DLAB (set baud rate divisor) - - uint16_t divisor = UART_MAX_BITS_RATE / bits_rate; - - io_out8(port + 0, divisor & 0xff); // Set divisor (lo byte) - io_out8(port + 1, (divisor >> 8) & 0xff); // (hi byte) - io_out8(port + 3, 0x03); // 8 bits, no parity, one stop bit - io_out8(port + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold - io_out8(port + 4, 0x08); // IRQs enabled, RTS/DSR clear (现代计算机上一般都不需要hardware flow control,因此不需要置位RTS/DSR) - io_out8(port + 4, 0x1E); // Set in loopback mode, test the serial chip - io_out8(port + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte) - - // Check if serial is faulty (i.e: not same byte as sent) - if (io_in8(port + 0) != 0xAE) - { - return E_UART_SERIAL_FAULT; - } - - // If serial is not faulty set it in normal operation mode - // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled) - io_out8(port + 4, 0x08); - - char init_text2[] = "uart initialized.\n"; - for (int i = 0; i < sizeof(init_text2) - 1; ++i) - uart_send(COM1, init_text2[i]); - return UART_SUCCESS; - - /* - Notice that the initialization code above writes to [PORT + 1] - twice with different values. This is once to write to the Divisor - register along with [PORT + 0] and once to write to the Interrupt - register as detailed in the previous section. - The second write to the Line Control register [PORT + 3] - clears the DLAB again as well as setting various other bits. - */ -} - -/** - * @brief 发送数据 - * - * @param port 端口号 - * @param c 要发送的数据 - */ -void uart_send(uint32_t port, char c) -{ - while (is_transmit_empty(port) == 0) - pause(); - io_out8(port, c); -} - -/** - * @brief 从uart接收数据 - * - * @param port 端口号 - * @return uchar 接收到的数据 - */ -uchar uart_read(uint32_t port) -{ - while (serial_received(port) == 0) - pause(); - - return io_in8(port); -} - -/** - * @brief 通过串口发送整个字符串 - * - * @param port 串口端口 - * @param str 字符串 - */ -void uart_send_str(uint32_t port, const char *str) -{ - if ((unlikely(str == NULL))) - return; - while (1) - { - if (unlikely(*str == '\0')) - return; - uart_send(port, *str); - ++str; - } -} \ No newline at end of file diff --git a/kernel/src/driver/uart/uart.h b/kernel/src/driver/uart/uart.h index 8a011685..0531d769 100644 --- a/kernel/src/driver/uart/uart.h +++ b/kernel/src/driver/uart/uart.h @@ -1,20 +1,6 @@ -/** - * @file uart.h - * @author longjin (longjin@RinGoTek.cn) - * @brief uart驱动程序 RS-232驱动 - * @version 0.1 - * @date 2022-04-15 - * - * @copyright Copyright (c) 2022 - * - */ -#pragma once - #include -#define UART_SUCCESS 0 -#define E_UART_BITS_RATE_ERROR 1 -#define E_UART_SERIAL_FAULT 2 +//driver/uart/uart.rs --rust function enum uart_port_io_addr { COM1 = 0x3f8, @@ -26,47 +12,6 @@ enum uart_port_io_addr COM7 = 0x5e8, COM8 = 0x4E8, }; - -enum uart_register_offset -{ - REG_DATA = 0, - REG_INTERRUPT_ENABLE = 1, - REG_II_FIFO = 2, // Interrupt Identification and FIFO control registers - REG_LINE_CONTROL = 3, - REG_MODEM_CONTROL = 4, - REG_LINE_STATUS = 5, - REG_MODEM_STATUE = 6, - REG_SCRATCH = 7 -}; - -/** - * @brief 初始化com口 - * - * @param port com口的端口号 - * @param bits_rate 通信的比特率 - */ -int uart_init(uint32_t port, uint32_t bits_rate); - -/** - * @brief 发送数据 - * - * @param port 端口号 - * @param c 要发送的数据 - */ -void uart_send(uint32_t port, char c); - -/** - * @brief 从uart接收数据 - * - * @param port 端口号 - * @return uchar 接收到的数据 - */ -uchar uart_read(uint32_t port); - -/** - * @brief 通过串口发送整个字符串 - * - * @param port 串口端口 - * @param str 字符串 - */ -void uart_send_str(uint32_t port, const char *str); \ No newline at end of file +extern int c_uart_init(uint16_t port, uint32_t baud_rate); +extern void c_uart_send(uint16_t port, char c); +extern void c_uart_send_str(uint16_t port, const char *str); \ No newline at end of file diff --git a/kernel/src/driver/uart/uart.rs b/kernel/src/driver/uart/uart.rs new file mode 100644 index 00000000..db6afae7 --- /dev/null +++ b/kernel/src/driver/uart/uart.rs @@ -0,0 +1,258 @@ +use crate::include::bindings::bindings::{io_in8, io_out8}; +use core::{str, char, intrinsics::offset}; + +const UART_SUCCESS: i32 = 0; +const E_UART_BITS_RATE_ERROR: i32 = 1; +const E_UART_SERIAL_FAULT: i32 = 2; +const UART_MAX_BITS_RATE: u32 = 115200; + +#[allow(dead_code)] +#[repr(u16)] +#[derive(Clone)] +pub enum UartPort { + COM1 = 0x3f8, + COM2 = 0x2f8, + COM3 = 0x3e8, + COM4 = 0x2e8, + COM5 = 0x5f8, + COM6 = 0x4f8, + COM7 = 0x5e8, + COM8 = 0x4e8, +} + +impl UartPort { + ///@brief 将u16转换为UartPort枚举类型 + ///@param val 要转换的u16类型 + ///@return 输入的端口地址正确,返回UartPort类型,错误,返回错误信息 + #[allow(dead_code)] + pub fn from_u16(val: u16) -> Result { + match val { + 0x3f8 => Ok(Self::COM1), + 0x2f8 => Ok(Self::COM2), + 0x3e8 => Ok(Self::COM3), + 0x2e8 => Ok(Self::COM4), + 0x5f8 => Ok(Self::COM5), + 0x4f8 => Ok(Self::COM6), + 0x5e8 => Ok(Self::COM7), + 0x4e8 => Ok(Self::COM8), + _ => Err("port error!"), + } + } + + ///@brief 将UartPort枚举类型转换为u16类型 + ///@param self 要转换的UartPort + ///@return 转换的u16值 + #[allow(dead_code)] + pub fn to_u16(self: &Self) -> u16 { + match self { + Self::COM1 => 0x3f8, + Self::COM2 => 0x2f8, + Self::COM3 => 0x3e8, + Self::COM4 => 0x2e8, + Self::COM5 => 0x5f8, + Self::COM6 => 0x4f8, + Self::COM7 => 0x5e8, + Self::COM8 => 0x4e8, + } + } +} + +#[allow(dead_code)] +#[repr(C)] +#[derive(Debug, Copy, Clone)] +struct UartRegister { + reg_data: u8, + reg_interrupt_enable: u8, + reg_ii_fifo: u8, // Interrupt Identification and FIFO control registers + reg_line_config: u8, + reg_modem_config: u8, + reg_line_status: u8, + reg_modem_statue: u8, + reg_scartch: u8, +} + +#[repr(C)] +pub struct UartDriver { + port: UartPort, + baud_rate: u32, +} + +impl Default for UartDriver { + fn default() -> Self { + Self {port: UartPort::COM1, baud_rate: 115200} + } +} + +impl UartDriver { + /// @brief 串口初始化 + /// @param uart_port 端口号 + /// @param baud_rate 波特率 + /// @return 初始化成功,返回0,失败,返回错误信息 + #[allow(dead_code)] + pub fn uart_init(uart_port: &UartPort, baud_rate: u32) -> Result { + let message: &'static str = "uart init."; + let port = uart_port.to_u16(); + // 错误的比特率 + if baud_rate > UART_MAX_BITS_RATE || UART_MAX_BITS_RATE % baud_rate != 0 { + return Err("uart init error."); + } + + unsafe { + io_out8(port + 1, 0x00); // Disable all interrupts + io_out8(port + 3, 0x80); // Enable DLAB (set baud rate divisor) + + let divisor = UART_MAX_BITS_RATE / baud_rate; + + io_out8(port + 0, (divisor & 0xff) as u8); // Set divisor (lo byte) + io_out8(port + 1, ((divisor >> 8) & 0xff) as u8); // (hi byte) + io_out8(port + 3, 0x03); // 8 bits, no parity, one stop bit + io_out8(port + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold + io_out8(port + 4, 0x08); // IRQs enabled, RTS/DSR clear (现代计算机上一般都不需要hardware flow control,因此不需要置位RTS/DSR) + io_out8(port + 4, 0x1E); // Set in loopback mode, test the serial chip + io_out8(port + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte) + + // Check if serial is faulty (i.e: not same byte as sent) + if io_in8(port + 0) != 0xAE { + return Err("uart faulty"); + } + + // If serial is not faulty set it in normal operation mode + // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled) + io_out8(port + 4, 0x08); + } + UartDriver::uart_send(uart_port, message); + Ok(0) + /* + Notice that the initialization code above writes to [PORT + 1] + twice with different values. This is once to write to the Divisor + register along with [PORT + 0] and once to write to the Interrupt + register as detailed in the previous section. + The second write to the Line Control register [PORT + 3] + clears the DLAB again as well as setting various other bits. + */ + } + + fn serial_received(offset: u16) -> bool { + if unsafe{ io_in8(offset + 5) } & 1 != 0 { + true + } else { + false + } + } + + fn is_transmit_empty(offset: u16) -> bool { + if unsafe{ io_in8(offset + 5) } & 0x20 != 0 { + true + } else { + false + } + } + + /// @brief 串口发送 + /// @param uart_port 端口号 + /// @param str 发送字符切片 + /// @return None + #[allow(dead_code)] + fn uart_send(uart_port: &UartPort, str: &str) { + let port = uart_port.to_u16(); + while UartDriver::is_transmit_empty(port) == false { + for c in str.bytes() { + unsafe { io_out8(port, c); } + } + } //TODO:pause + } + + /// @brief 串口接收一个字节 + /// @param uart_port 端口号 + /// @return 接收的字节 + #[allow(dead_code)] + fn uart_read_byte(uart_port: &UartPort) -> char { + let port = uart_port.to_u16(); + while UartDriver::serial_received(port) == false {} //TODO:pause + unsafe { io_in8(port) as char } + } + +} + +///@brief 发送数据 +///@param port 端口号 +///@param c 要发送的数据 +#[no_mangle] +pub extern "C" fn c_uart_send(port: u16, c: u8) { + while UartDriver::is_transmit_empty(port) == false {} //TODO:pause + unsafe { io_out8(port, c); } +} + +///@brief 从uart接收数据 +///@param port 端口号 +///@return u8 接收到的数据 +#[no_mangle] +pub extern "C" fn c_uart_read(port: u16) -> u8 { + while UartDriver::serial_received(port) == false {} //TODO:pause + unsafe { io_in8(port) } +} + +///@brief 通过串口发送整个字符串 +///@param port 串口端口 +///@param str 字符串S +#[no_mangle] +pub extern "C" fn c_uart_send_str(port: u16, str: *const u8) +{ + unsafe { + let mut i = 0; + while *offset(str, i) != '\0' as u8 { + c_uart_send(port, *offset(str, i)); + i = i + 1; + } + } +} + +/// @brief 串口初始化 +/// @param u16 端口号 +/// @param baud_rate 波特率 +/// @return 初始化成功,返回0,失败,返回错误码 +#[no_mangle] +pub extern "C" fn c_uart_init(port: u16, baud_rate: u32) -> i32 { + let message: &'static str = "uart init\n"; + // 错误的比特率 + if baud_rate > UART_MAX_BITS_RATE || UART_MAX_BITS_RATE % baud_rate != 0 { + return -E_UART_BITS_RATE_ERROR; + } + + unsafe { + io_out8(port + 1, 0x00); // Disable all interrupts + io_out8(port + 3, 0x80); // Enable DLAB (set baud rate divisor) + + let divisor = UART_MAX_BITS_RATE / baud_rate; + + io_out8(port + 0, (divisor & 0xff) as u8); // Set divisor (lo byte) + io_out8(port + 1, ((divisor >> 8) & 0xff) as u8); // (hi byte) + io_out8(port + 3, 0x03); // 8 bits, no parity, one stop bit + io_out8(port + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold + io_out8(port + 4, 0x08); // IRQs enabled, RTS/DSR clear (现代计算机上一般都不需要hardware flow control,因此不需要置位RTS/DSR) + io_out8(port + 4, 0x1E); // Set in loopback mode, test the serial chip + io_out8(port + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte) + + // Check if serial is faulty (i.e: not same byte as sent) + if io_in8(port + 0) != 0xAE { + return -E_UART_SERIAL_FAULT; + } + + // If serial is not faulty set it in normal operation mode + // (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled) + io_out8(port + 4, 0x08); + let bytes = message.as_bytes(); + for c in bytes { + c_uart_send(port, *c); + } + } + return UART_SUCCESS; + /* + Notice that the initialization code above writes to [PORT + 1] + twice with different values. This is once to write to the Divisor + register along with [PORT + 0] and once to write to the Interrupt + register as detailed in the previous section. + The second write to the Line Control register [PORT + 3] + clears the DLAB again as well as setting various other bits. + */ +} diff --git a/kernel/src/driver/video/video.c b/kernel/src/driver/video/video.c index 387d293b..f65d66a9 100644 --- a/kernel/src/driver/video/video.c +++ b/kernel/src/driver/video/video.c @@ -185,7 +185,7 @@ int video_init() io_mfence(); char init_text2[] = "Video driver initialized.\n"; for (int i = 0; i < sizeof(init_text2) - 1; ++i) - uart_send(COM1, init_text2[i]); + c_uart_send(COM1, init_text2[i]); return 0; } \ No newline at end of file diff --git a/kernel/src/include/bindings/wrapper.h b/kernel/src/include/bindings/wrapper.h index dea4d6e1..881ee129 100644 --- a/kernel/src/include/bindings/wrapper.h +++ b/kernel/src/include/bindings/wrapper.h @@ -22,7 +22,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index 14ad3097..bb68f0b0 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -21,6 +21,7 @@ mod mm; mod process; mod sched; mod smp; +mod driver; extern crate alloc; diff --git a/kernel/src/libs/libUI/screen_manager.c b/kernel/src/libs/libUI/screen_manager.c index b8516f6e..cbc12f9a 100644 --- a/kernel/src/libs/libUI/screen_manager.c +++ b/kernel/src/libs/libUI/screen_manager.c @@ -16,6 +16,7 @@ static struct scm_ui_framework_t *__current_framework; // 当前拥有屏幕控 static uint32_t scm_ui_max_id = 0; static bool __scm_alloc_enabled = false; // 允许动态申请内存的标志位 static bool __scm_double_buffer_enabled = false; // 允许双缓冲的标志位 + /** * @brief 创建新的帧缓冲区 * @@ -250,11 +251,11 @@ int scm_enable_double_buffer() { if (ptr->buf == &video_frame_buffer_info) { - uart_send_str(COM1, "##init double buffer##\n"); + c_uart_send_str(COM1, "##init double buffer##\n"); struct scm_buffer_info_t *buf = __create_buffer(SCM_BF_DB | SCM_BF_PIXEL); if ((uint64_t)(buf) == (uint64_t)-ENOMEM) return -ENOMEM; - uart_send_str(COM1, "##to change double buffer##\n"); + c_uart_send_str(COM1, "##to change double buffer##\n"); if (ptr->ui_ops->change(buf) != 0) // 这里的change回调函数不会是空指针吗 问题2 { @@ -270,7 +271,7 @@ int scm_enable_double_buffer() video_set_refresh_target(__current_framework->buf); // 通知显示驱动,启动双缓冲 video_reinitialize(true); - uart_send_str(COM1, "##initialized double buffer##\n"); + c_uart_send_str(COM1, "##initialized double buffer##\n"); return 0; } diff --git a/kernel/src/libs/libUI/textui.c b/kernel/src/libs/libUI/textui.c index 13224268..357a3fdf 100644 --- a/kernel/src/libs/libUI/textui.c +++ b/kernel/src/libs/libUI/textui.c @@ -1,6 +1,6 @@ #include "textui.h" -#include "driver/uart/uart.h" +#include #include "screen_manager.h" #include #include @@ -69,7 +69,7 @@ static int __textui_init_window(struct textui_window_t *window, uint8_t flags, u int textui_install_handler(struct scm_buffer_info_t *buf) { // return printk_init(buf); - uart_send_str(COM1, "textui_install_handler"); + c_uart_send_str(COM1, "textui_install_handler"); return 0; } @@ -80,7 +80,7 @@ int textui_uninstall_handler(void *args) int textui_enable_handler(void *args) { - uart_send_str(COM1, "textui_enable_handler\n"); + c_uart_send_str(COM1, "textui_enable_handler\n"); return 0; } @@ -214,11 +214,11 @@ int textui_putchar_window(struct textui_window_t *window, uint16_t character, ui // uint64_t rflags = 0; // 加锁后rflags存储到这里 spin_lock(&window->lock); - uart_send(COM1, character); + c_uart_send(COM1, character); if (unlikely(character == '\n')) { // 换行时还需要输出\r - uart_send(COM1, '\r'); + c_uart_send(COM1, '\r'); __textui_new_line(window, window->vline_operating); // spin_unlock_irqrestore(&window->lock, rflags); spin_unlock(&window->lock); @@ -319,7 +319,7 @@ int textui_init() int retval = scm_register(&textui_framework); if (retval != 0) { - uart_send_str(COM1, "text ui init failed\n"); + c_uart_send_str(COM1, "text ui init failed\n"); while (1) pause(); } @@ -343,6 +343,6 @@ int textui_init() __private_info.default_window = &__initial_window; __private_info.actual_line = textui_framework.buf->height / TEXTUI_CHAR_HEIGHT; - uart_send_str(COM1, "text ui initialized\n"); + c_uart_send_str(COM1, "text ui initialized\n"); return 0; } diff --git a/kernel/src/main.c b/kernel/src/main.c index 231104d7..763dae39 100644 --- a/kernel/src/main.c +++ b/kernel/src/main.c @@ -71,7 +71,7 @@ void reload_idt() void system_initialize() { - uart_init(COM1, 115200); + c_uart_init(COM1, 115200); video_init(); scm_init();