zoukankan      html  css  js  c++  java
  • RTT驱动实现步骤

    设备驱动实现步骤:

    1. 按照RT-Thread对象模型,扩展一对象有两种方式:

    1)定义自己的私有数据结构,然后赋值到RT-Thread设备控制空的user_data指针上;

    2)从struct rt_device结构中派生。(推荐)

    2. 实现I/O设备模块中定义的6个公共设备接口,开始可以为空函数(返回rt_err_t的可默认返回RT_EOK)。

    3. 根据自己的设备类型定义自己的私有数据域。

        特别是可能有多个类似设备的情况下(例如串口1,2),

        设备接口可以共用一套接口,不同的只是各自的数据域(寄存器基地址)。

    4. 根据设备的类型,注册到RT-Thread设备框架中。

    现以串口驱动为例:

    /*stm32f10x.h
    *This file contains all the peripheral register's definitions, bits 
    * definitions and memory mapping for STM32F10x Connectivity line, 
    * High density, High density value line, Medium density, 
    * Medium density Value line, Low density, Low density Value line 
    * and XL-density devices. */
    typedef struct
    {
      __IO uint16_t SR;
      uint16_t  RESERVED0;
      __IO uint16_t DR;
      uint16_t  RESERVED1;
      __IO uint16_t BRR;
      uint16_t  RESERVED2;
      __IO uint16_t CR1;
      uint16_t  RESERVED3;
      __IO uint16_t CR2;
      uint16_t  RESERVED4;
      __IO uint16_t CR3;
      uint16_t  RESERVED5;
      __IO uint16_t GTPR;
      uint16_t  RESERVED6;
    } USART_TypeDef;
    struct stm32_uart
    {
        USART_TypeDef* uart_device;
        IRQn_Type irq;
    };
    struct rt_serial_device
    {
        struct rt_device          parent;
    
        const struct rt_uart_ops *ops;
        struct serial_configure   config;
    
        void *serial_rx;
        void *serial_tx;
    };
    typedef struct rt_serial_device rt_serial_t;
    /*
     * serial register
     */
    rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
                                   const char              *name,
                                   rt_uint32_t              flag,
                                   void                    *data)
    {
        struct rt_device *device;
        RT_ASSERT(serial != RT_NULL);
    
        device = &(serial->parent);
    
        device->type        = RT_Device_Class_Char;
        device->rx_indicate = RT_NULL;
        device->tx_complete = RT_NULL;
    
        device->init        = rt_serial_init;
        device->open        = rt_serial_open;
        device->close       = rt_serial_close;
        device->read        = rt_serial_read;
        device->write       = rt_serial_write;
        device->control     = rt_serial_control;
        device->user_data   = data;
    
        /* register a character device */
        return rt_device_register(device, name, flag);
    }
        struct stm32_uart* uart;
    rt_hw_serial_register(&serial1, "uart1", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX , uart);

    rt_serial_device设备驱动采用派生rt_device实现驱动,实现一套共用的驱动函数-------linux中相当于platform_driver;

    rt_serial_device的私有数据保存具体设备相关参数(与硬件密切相关)------linux中相当于platform_device。

    最后通过rt_hw_serial_register()/rt_device_register()将uart(硬件device)和rt_serial_device(驱动)注册到系统驱动框架中(rt_device)。

    上述采用派生和私有数据相结合的方法可使具体硬件与驱动分开,便于单独修改硬件;

    原来(旧版本)将具体硬件与驱动都通过私有数据注册到系统驱动框架中。------自己理解。

    私有数据方式实现驱动可节省空间,效率可能更高。

    当为了实现硬件与业务分开,故可约定俗成:业务用派生实现,硬件用私有数据实现。

  • 相关阅读:
    GeoServer 发布矢量切片服务
    GeoServer 服务 端口修改
    Python 启动 FastAPI 报错 Error: [WinError 10013] 以一种访问权限不允许的方式做了一个访问套接字的尝试
    conda和pip的常用语句
    ArcGIS Pro 图层数据批量执行多部件转单部件
    ArcGIS Pro 中文(简体)语言包-指定路径为空
    2020 Ateneo de Manila University DISCS PrO HS Division
    N皇后问题-汇编解法
    HDU
    HDU
  • 原文地址:https://www.cnblogs.com/embedded-linux/p/5460137.html
Copyright © 2011-2022 走看看