• 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)。

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

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

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

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

  • 相关阅读:
    Zookeeper
    RPC
    RabbitMQ学习总结
    ActiveMQ学习总结
    mybatis自动映射和手动映射
    oracle instantclient_12_2安装
    EFK(Elasticsearch+Filebeat+Kibana)收集容器日志
    prometheus-operator监控Kubernetes
    编译安装 keepalived-2.0.16.tar.gz
    Kubernetes pod平滑迁移
  • 原文地址:https://www.cnblogs.com/embedded-linux/p/5460137.html
Copyright © 2020-2023  润新知