• 串口子系统


    本文主要通过学习韦东山老师的课程,详情可通过百问网驱动大全进行学习。

    串口,通用异步接收机和发射机(Universal Asynchronous Receiver and Transmitter)。PC 机一般有两个串行口COM 1 和COM 2 。串行口不同于并行口之处在于它的数据和控制信息是一位接一位地传送出去的。

    主要功能:①通过串口打印调试信息。②外接各种模块:GPS/蓝牙

    如何使用串口:
    ①设置波特率
    ②格式:数据位/停止位/校验位/流控

    示例:发送1Byte,比如’A’?
    ‘A’,二进制位:0b01000001
    ①双方约定波特率:每一位占据的时间
    在这里插入图片描述

    编程部分:
    1.构造驱动基本框架

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    #include 
    #include 
    #include 
    #include 
    
    static struct uart_port	*virt_port;//uart_port include haedware
    
    static struct uart_driver virtual_uart_driver = {
    	.owner			=	THIS_MODULE,
    	.driver_name	=	"VIRT_UART",
    	.major			=	0,
    	.minor			=	0,
    	.nr				=	1,//number,drv support 1 serial
    };
    
    //Interrupt func
    static irqreturn_t virtual_rxint(int irq, void *dev_id)
    {
    	struct uart_port *port = dev_id;
    	unsigned int rx, flg, ignored = 0;
    	struct tty_port *port = &sport->port.state->port;
    	unsigned long flags, temp;
    }
    
    static int serial_imx_probe(struct platform_device *pdev)
    {
    	int rxirq;
    	int ret;
    
    	/*通过设备树获取硬件信息*/
    	rxirq=platform_get_irq(pdev,0);
    
    	/* 分配设置注册uart_port */
    	virt_port = devm_kzalloc(&pdev->dev, sizeof(*virt_port), GFP_KERNEL);//使用devm前缀时,系统可自动释放内存
    
    	virt_port->dev = &pdev->dev;
    	virt_port->iotype = UPIO_MEM;
    	virt_port->irq = rxirq;
    	virt_port->fifosize = 32;
    	virt_port->ops = &virt_pops;//硬件相关函数
    	virt_port->flags = UPF_BOOT_AUTOCONF;
    	virt_port->type = PORT_8250;
    
    	/*注册中断*/
    	ret = devm_request_irq(&pdev->dev, rxirq, virtual_rxint, 0,
    				       dev_name(&pdev->dev), virt_port);
    	
    	return uart_add_one_port(virtual_uart_driver,virt_port);//向uart_driver注册uart_port
    }
    
    
    static int serial_imx_remove(struct platform_device *pdev)
    {
    	uart_remove_one_port(virtual_uart_driver, virt_port);
    }
    
    static const struct of_match_id virtual_uart_of_match[]={
    	{.compatible = "test,virtual_uart",},
    	{},
    };
    
    
    static struct platform_driver virtual_uart_platform_driver = {
    	.probe	=	virtual_uart_probe,
    	.remove =	virtual_uart_remove,
    	.driver	=	{
    			.name = "test_virtual_uart",
    			.of_match_table = of_match_ptr(virtual_uart_of_match);//通过compatible方式来匹配
    	}
    };
    
    static int __init virtual_uart_init(void)
    {
    	int ret = uart_register_driver(&virtual_uart_driver);//注册串口驱动
    	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
    
    	if (ret)
    		return ret;
    
    	//注册平台驱动
    	ret = platform_driver_register(&virtual_uart_platform_driver);
    	if (ret != 0)
    		uart_unregister_driver(&virtual_uart_platform_driver);
    
    	return ret;
    }
    
    static void __exit virtual_uart_exit(void)
    {
    	platform_driver_unregister(&virtual_uart_platform_driver);
    	uart_unregister_driver(&virtual_uart_driver);
    }
    
    module_init(virtual_uart_init);
    module_exit(virtual_uart_exit);
    
    MODULE_LICENSE("GPL");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117

    其核心就在于uart_add_one_port,这个函数作用为:添加一个串口。

  • 相关阅读:
    ubuntu如何查看系统信息、cpu型号
    【C++】string类模拟实现
    SpringMVC之自定义注解
    PostgreSQL中的技术内幕
    deque容器 常用 API操作
    「Flask」路由+视图函数
    一次对pool的误用导致的.net频繁gc的诊断分析
    springboot323基于Java的美妆购物网站的设计与实现
    PMP考试技巧&PMP考试大纲
    SQL基础查询与排序
  • 原文地址:https://blog.csdn.net/qq_44823010/article/details/126767638