• 基于cortex-M3,M4下硬件层、驱动层的解耦软件框架设计


    概述

    通过对硬件层、驱动层、应用层抽象设计,对于硬件层、驱动层对外均有统一的读写接口,便于应用层的业务逻辑调用,避免应用层对于硬件业务逻辑变化,导致要同步进行改动的情况。该解耦设计框架通用于各类32子板程序,便于后续迭代开发及维护。可以通过子模块的方式引用到工程中。

    硬件层

    • bsp_device_model.c
    /**
     * @file bsp_device_model.c
     * @author kwong 
     * @brief 
     * @version 0.1
     * @date 2023-10-13 
     * @copyright Copyright (c) 2023  
     * 
     */
    
    #include "bsp_device_model.h"
    #include "module_init.h"
    #include 
    
    static bsp_device_model* findDevice(uint32_t fd);
    
    static bsp_device_model_manager m_bsp_device_model_manager = {
        .m_device_list = NULL
    };
    
    /**
     * @brief 查找在注册列表中的对应的设备
     * 
     * @param fd 
     * @return bsp_device_model* 返回该设备指针
     */
    bsp_device_model* findDevice(uint32_t fd)
    {
        bsp_device_model_list *target = m_bsp_device_model_manager.m_device_list; 
    
        while (target->m_device != NULL) {
            if (target->m_device->fd == fd) {
                return target->m_device;
            }
            target = target->next_device;
        }
    
        return NULL;
    }
    
    /**
     * @brief 初始化在注册列表中的所有设备
     * 
     */
    void initRegisterBspDevice()
    {
        bsp_device_model_list *m_device_lists = m_bsp_device_model_manager.m_device_list; 
    
        while (m_device_lists != NULL) {
            if (m_device_lists->m_device != NULL) 
                m_device_lists->m_device->init();
            m_device_lists = m_device_lists->next_device;
        }
    }
    
    /**
     * @brief 写函数:对设备驱动与用户写操作的解耦分离
     * 
     * @param fd 
     * @param buffer 
     * @param size 
     */
    void bsp_write(uint32_t fd,void *buffer,int size)
    {
        bsp_device_model *m_target = findDevice(fd);
        
        if (m_target != NULL) {
            if (m_target->write != NULL) {
                m_target->write(buffer,size);
            }
        }
    }
    
    /**
     * @brief 写函数:对设备驱动与用户读操作的解耦分离
     * 
     * @param fd 
     * @param buffer 
     * @param size 
     * @return int 
     */
    int bsp_read(uint32_t fd,void *buffer,int size)
    {
        int ret = -1;
        bsp_device_model *m_target = findDevice(fd);
    
        if (m_target != NULL) {
            if (m_target->read != NULL) {
                ret = m_target->read(buffer,size);
            }
        }
    
        return ret;
    }
    
    /**
     * @brief 注册设备到注册列表中
     * 
     * @param device 
     */
    void registerBspDevice(bsp_device_model *device)
    {
        bsp_device_model_list **m_list_head = &m_bsp_device_model_manager.m_device_list;
        bsp_device_model_list *tmp = *m_list_head;
        bsp_device_model_list *new_node = (bsp_device_model_list*)malloc(sizeof(bsp_device_model_list));
    
        if (*m_list_head == NULL) {
            *m_list_head = new_node;
            new_node->m_device = device;
            new_node->next_device = NULL;
            return;
        } else {
            while ( tmp->next_device != NULL) {
                tmp = tmp->next_device;
            }
        }
    
        tmp->next_device = new_node;
        new_node->m_device = device;
        new_node->next_device = NULL;
    }
    
    /**
     * @brief 添加设备层解析对应的数据结构
     * 
     */
    int addBspDevicePraseDataFrame(uint32_t fd,void *data)
    {
        bsp_device_model_list *m_device_lists = m_bsp_device_model_manager.m_device_list;
    
        while (m_device_lists->m_device != NULL) {
            if (m_device_lists->m_device->fd == fd) {
                m_device_lists->m_device->addPraseDataFrame(data);
                return 0;
            }
            m_device_lists = m_device_lists->next_device;
        }
    
        return -1;
    }
    
    middleware_init(initRegisterBspDevice)
    
    • 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
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • bsp_device_model.h
    /**
     * @file bsp_device_model.h
     * @author kwong
     * @brief 
     * @version 0.1
     * @date 2023-10-13
     * @copyright Copyright (c) 2023  
     * 
     */
    
    #ifndef BSP_DEVICE_MODEL_H
    #define BSP_DEVICE_MODEL_H
    
    #include 
    
    typedef struct 
    {
        uint32_t fd;
        void (*init)();
        void (*write)(void *buffer,int size);
        int  (*read)(void *buffer,int size);
        void (*addPraseDataFrame)(void *data);//多个设备需要独立处理各自数据通用结构体的情况下,需要实现该函数
    } bsp_device_model;
    
    typedef struct  {
        bsp_device_model *m_device;
        struct bsp_device_model_list *next_device;
        
    } bsp_device_model_list;
    
    typedef struct {
    
        bsp_device_model_list *m_device_list;
    
    } bsp_device_model_manager;
    
    
    void initRegisterBspDevice();
    void bsp_write(uint32_t fd,void *buffer,int size);
    int  bsp_read(uint32_t fd,void *buffer,int size);
    void registerBspDevice(bsp_device_model *device);
    int  addBspDevicePraseDataFrame(uint32_t fd,void *data);
    
    #endif
    
    • 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

    驱动层

    • dev_drv_manager.c
    /**
     * @file dev_drv_manager.c
     * @author kwong
     * @brief 
     * @version 0.1
     * @date 2023-10-19
     * 
     * @copyright Copyright (c) 2023  
     * 
     */
    
    #include "dev_drv_manager.h"
    #include "module_init.h"
    #include 
    #include 
    
    static dev_dri_manager m_dev_dri_manager = {
        .m_device_list = NULL
    };
    
    /**
     * @brief 查找对应的驱动模块
     * 
     * @param type 
     * @return dev_drv_model* 
     */
    dev_drv_model* findDevice(const char *type)
    {
        dev_drv_model_list *target = m_dev_dri_manager.m_device_list;
    
        while (target->m_device != NULL)
        {
            if ( !strcmp(target->m_device->type, type) ) {
                return target->m_device;
            }
            target = target->next_device;
        }
        
        return NULL;
    }
    
    /**
     * @brief 在驱动列表中的驱动依次初始化
     * 
     */
    void initRegisterDeviceDriver()
    {
        dev_drv_model_list *m_dev_drv_lists = m_dev_dri_manager.m_device_list; 
    
        while (m_dev_drv_lists != NULL) {
            if (m_dev_drv_lists->m_device != NULL) {
                if ( m_dev_drv_lists->m_device->init != NULL )
                    m_dev_drv_lists->m_device->init();
            }
            m_dev_drv_lists = m_dev_drv_lists->next_device;
        }
    }
    
    /**
     * @brief 驱动泛型写函数
     * 
     * @param type 
     * @param buffer 
     * @param size 
     */
    void drv_write(char *type,void *buffer,int size)
    {
        dev_drv_model *m_target = findDevice(type);
    
        if (m_target != NULL) {
            if (m_target->write != NULL) {
                m_target->write(buffer,size); 
            }
        }
    }
    
    /**
     * @brief 驱动泛型写函数2
     * 
     * @param type 
     * @param buffer 
     */
    void drv_write_bit(char *type,void *buffer)
    {
        dev_drv_model *m_target = findDevice(type);
    
        if (m_target != NULL) {
            if (m_target->write_bit != NULL) {
                m_target->write_bit(type,buffer);
            }
        }
    }
    
    /**
     * @brief 驱动泛型读函数
     * 
     * @param type 
     * @param buffer 
     * @param size 
     * @return int 
     */
    int  drv_read(char *type,void *buffer,int size)
    {
        int ret = -1;
        dev_drv_model *m_target = findDevice(type);
        
        if (m_target != NULL) {
            if (m_target->read != NULL) {
                ret = m_target->read(buffer,size);
            }
        }
    
        return ret;
    }
    
    /**
     * @brief 驱动泛型读函数2
     * 
     * @param type 
     * @param buffer 
     * @return int 
     */
    int  drv_read_bit(char *type,void *buffer)
    {
        int ret = -1;
        dev_drv_model *m_target = findDevice(type);
    
        if (m_target != NULL) {
            if (m_target->read_bit != NULL) {
                ret = m_target->read_bit(type,buffer);
            }
        }
        return ret;
    }
    
    /**
     * @brief 注册驱动模块到驱动列表管理中
     * 
     * @param dev_drv 
     */
    void registerDeviceDriver(dev_drv_model *dev_drv)
    {
        dev_drv_model_list **m_dev_drv_lists_head = &m_dev_dri_manager.m_device_list;
        dev_drv_model_list *tmp = *m_dev_drv_lists_head;
        dev_drv_model_list *new_node = (dev_drv_model_list*)malloc(sizeof(dev_drv_model_list));
    
        if (*m_dev_drv_lists_head == NULL) {
            *m_dev_drv_lists_head = new_node;
            new_node->m_device = dev_drv;
            new_node->next_device = NULL;
            return;
        } else {
            while ( tmp->next_device != NULL) {
                tmp = tmp->next_device;
            }
        }
    
        tmp->next_device = new_node;
        new_node->m_device = dev_drv;
        new_node->next_device = NULL;
    }
    
    app_init(initRegisterDeviceDriver)
    
    • 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
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • dev_drv_manager.h
    /**
     * @file dev_drv_manager.h
     * @author kwong
     * @brief 
     * @version 0.1
     * @date 2023-10-19
     * @copyright Copyright (c) 2023  
     * 
     */
    
    #ifndef DEV_DRI_MANAGER_H
    #define DEV_DRI_MANAGER_H
    
    typedef struct 
    {
        char* type;
        void (*init)();
        void (*write)(void *buffer,int size);
        void (*write_bit)(char* type,void *buffer);
        int  (*read)(void *buffer,int size);
        int  (*read_bit)(char* type,void *buffer);
        void (*addPraseDataFrame)(void *data);//多个设备需要独立处理各自数据通用结构体的情况下,需要实现该函数
    } dev_drv_model;
    
    typedef struct 
    {
        dev_drv_model *m_device;
        struct dev_drv_model_list *next_device;
    } dev_drv_model_list;
    
    typedef struct 
    {
        dev_drv_model_list *m_device_list;
    } dev_dri_manager;
    
    void initRegisterDeviceDriver();
    void drv_write(char *type,void *buffer,int size);
    void drv_write_bit(char *type,void *buffer);
    int  drv_read(char *type,void *buffer,int size);
    int  drv_read_bit(char *type,void *buffer);
    void registerDeviceDriver(dev_drv_model *dev_dri);
    
    #endif
    
    • 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
  • 相关阅读:
    (哈希表 ) 349. 两个数组的交集 ——【Leetcode每日一题】
    [附源码]计算机毕业设计网咖管理系统Springboot程序
    Python电梯楼层数字识别
    【C语言】深入解开指针(三)
    SQLAlchemy学习-3.(懒人专用)sqlacodegen自动同步数据库中表生成model 代码
    30天刷题计划(四)
    自注意力机制(Self-attention)【第四章】
    electron实现静默打印(各种踩坑解决)
    随笔 | 写在七月末的这一天
    java+Vue +Spring boot技术开发的UWB高精度定位技术系统源码 uwb定位系统+基站定位
  • 原文地址:https://blog.csdn.net/kgs0716/article/details/134272531