RK3568使能UART串口设备,并生成/dev/ttySx设备节点的代码
时间: 2024-09-25 17:19:25 浏览: 133
Station P2(ROC-RK3568-PC) 裸机开发之串口收发代码
在Linux系统中,启用RK3568芯片的UART串口并将其映射到/dev/ttySx设备节点通常涉及到硬件初始化、注册字符设备驱动以及配置文件设置等步骤。这里是一个简化版的示例代码片段,假设你已经完成了相关的硬件连接和头文件包含:
```c
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/platform_device.h>
#include <linux/tty.h>
#include <linux/serial_core.h>
#include <asm/io.h>
#define UART_NAME "rk3568_uart"
#define DEVICE_NAME "ttyS0" // 这里可以根据实际情况更改
static struct serial_port uart_data = {
.port_id = 0, // 需要确认实际的端口号
.irq = RXIrqLine, // RX中断线
};
static int rk3568_uart_probe(struct platform_device *pdev) {
struct resource *res;
int ret;
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
if (!res) {
pr_err("Failed to get memory resource\n");
return -ENODEV;
}
/* 初始化UART硬件 */
// ... (具体的初始化步骤, 包括地址偏移和控制寄存器操作)
/* 注册字符设备 */
ret = alloc_chrdev_region(&minor, 0, 1, UART_NAME);
if (ret < 0) {
pr_err("Failed to allocate device region\n");
return ret;
}
uart_data.dev = &device;
device.type = TTY_DEV;
device.major = MAJOR(UART_MINOR);
device.minor = minor;
device.name = UART_NAME;
// 创建tty_driver实例
tty_driver_init(&driver);
driver.name = UART_NAME;
driver.owner = THIS_MODULE;
driver.driver_features = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_CHANNELS;
driver.init_termios = init_termios;
driver_termios(driver);
// 将驱动与硬件关联起来
ret = serial8250_register(&uart_data, &driver);
if (ret < 0) {
unregister_chrdev_region(minor, 1);
return ret;
}
/* 设置设备名称 */
device_node_name(res->node, DEVICE_NAME);
platform_set_drvdata(pdev, &device);
printk(KERN_INFO "%s: Registered as %s at /dev/%s\n", __func__, UART_NAME, DEVICE_NAME);
return 0;
}
static void rk3568_uart_remove(struct platform_device *pdev) {
struct tty_driver *driver = platform_get_drvdata(pdev);
serial8250_unregister(driver);
unregister_chrdev_region(driver->minor, 1);
device_destroy(device_tree_root, devm_of_node_to_resource(pdev->dev.of_node, 0)->res_id);
}
static struct of_device_id rk3568_uart_match[] = {
{ .compatible = "rk3568,uart", },
{ /* end of list */ }
};
MODULE_DEVICE_TABLE(of, rk3568_uart_match);
static struct platform_driver rk3568_uart_driver = {
.probe = rk3568_uart_probe,
.remove = rk3568_uart_remove,
.driver = {
.name = UART_NAME,
.of_match_table = rk3568_uart_match,
},
};
module_platform_driver(rk3568_uart_driver);
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Raspberry Pi 3568 UART Driver");
MODULE_LICENSE("GPL");
//
阅读全文