简介:立创泰山派RK3566开发板,是一款开源的卡片电脑,提供全面开放的软硬件资料,愿与志同道合的的伙伴们共同推动技术的发展和创新。小巧的板子搭载高配的处理器、引出丰富的外部资源、多样性SDK,赋予无限可能
好的,作为一名高级嵌入式软件开发工程师,我将基于立创泰山派RK3566开发板,并根据其特性和嵌入式系统开发流程,详细阐述一个可靠、高效、可扩展的系统平台代码架构,并给出具体C代码实现。

一、系统架构设计

为了实现可靠、高效、可扩展的目标,我们将采用分层架构,将系统划分为若干个模块,并定义清晰的接口,以降低模块间的耦合性,方便维护和扩展。

  1. 硬件抽象层 (HAL, Hardware Abstraction Layer)

    • 目的:隔离硬件差异,向上层提供统一的硬件访问接口。
    • 内容:GPIO、UART、I2C、SPI、USB、Ethernet、Timers、RTC等外设的驱动程序和接口。
    • 实现
      • 使用标准C编写,直接操作寄存器或使用SDK提供的驱动API。
      • 定义抽象的硬件接口,如hal_gpio_init(), hal_uart_send(), hal_i2c_read()等。
      • 针对不同的硬件平台,实现不同的HAL层。
  2. 操作系统层 (OSAL, Operating System Abstraction Layer)

    • 目的:隔离底层操作系统差异,向上层提供统一的操作系统服务接口。
    • 内容:任务管理、内存管理、线程同步、定时器管理、消息队列等操作系统服务抽象。
    • 实现
      • 可以基于 FreeRTOS、RT-Thread等实时操作系统,也可以自定义简单的操作系统内核。
      • 定义抽象的操作系统接口,如osal_task_create(), osal_mutex_lock(), osal_timer_start()等。
      • 根据实际选用的操作系统,实现不同的OSAL层。
  3. 中间件层 (Middleware Layer)

    • 目的:提供通用的功能模块,如网络协议栈、文件系统、加密算法、数据解析等。
    • 内容:TCP/IP协议栈 (如lwIP), 文件系统 (如FatFS), JSON解析器 (如cJSON), 加密算法库 (如mbedTLS) 等。
    • 实现
      • 使用成熟的开源库,并进行适当的裁剪和定制,以适应嵌入式系统的资源限制。
      • 封装库的接口,提供更易于使用的API。
  4. 应用层 (Application Layer)

    • 目的:实现具体的业务逻辑,如传感器数据采集、控制算法、用户界面等。
    • 内容:用户程序、主循环、任务调度等。
    • 实现
      • 基于中间件层和OSAL层提供的服务,实现具体的业务逻辑。
      • 采用模块化设计,将应用划分为若干个独立的模块。
  5. 测试层 (Test Layer)

    • 目的:提供测试工具和框架,用于验证系统的功能和性能。
    • 内容:单元测试、集成测试、压力测试等。
    • 实现
      • 使用如 Google Test 或自定义测试框架进行测试。
      • 测试用例覆盖各个模块和接口,保证代码质量。

二、详细C代码实现

下面我将给出各个层级关键模块的代码示例。

1. 硬件抽象层 (HAL)

// hal_gpio.h
#ifndef HAL_GPIO_H
#define HAL_GPIO_H

typedef enum {
    GPIO_PIN_0,
    GPIO_PIN_1,
    GPIO_PIN_2,
    // ... 其他GPIO引脚
    GPIO_PIN_MAX
} hal_gpio_pin_t;

typedef enum {
    GPIO_MODE_INPUT,
    GPIO_MODE_OUTPUT,
    GPIO_MODE_ALTFUNC
} hal_gpio_mode_t;

typedef enum {
    GPIO_STATE_LOW,
    GPIO_STATE_HIGH
} hal_gpio_state_t;

typedef struct {
    hal_gpio_pin_t pin;
    hal_gpio_mode_t mode;
} hal_gpio_config_t;


void hal_gpio_init(const hal_gpio_config_t *config);
void hal_gpio_set_state(hal_gpio_pin_t pin, hal_gpio_state_t state);
hal_gpio_state_t hal_gpio_get_state(hal_gpio_pin_t pin);

#endif

// hal_gpio.c
#include "hal_gpio.h"
#include <stdio.h> // 为了printf, 如果是嵌入式环境,要改为嵌入式串口输出

// 假设使用的是 RK3566 的 GPIO 寄存器定义,实际需根据 SDK 修改
#define GPIO_BASE 0x0000 // 示例基地址
#define GPIO_SWPORT_DR (GPIO_BASE + 0x0000) // 数据寄存器
#define GPIO_SWPORT_DDR (GPIO_BASE + 0x0004) // 方向寄存器
#define GPIO_EXT_PORT (GPIO_BASE + 0x0008) // 读取寄存器

void hal_gpio_init(const hal_gpio_config_t *config) {
    // 根据配置设置GPIO的方向和初始状态
    printf("GPIO 初始化:Pin %d, Mode %d\n", config->pin, config->mode);

    volatile unsigned int *ddr = (volatile unsigned int *)GPIO_SWPORT_DDR;
    volatile unsigned int *data = (volatile unsigned int *)GPIO_SWPORT_DR;

    if (config->mode == GPIO_MODE_OUTPUT) {
        *ddr |= (1 << config->pin);
    } else if(config->mode == GPIO_MODE_INPUT)
    {
        *ddr &= ~(1 << config->pin);
    }
}

void hal_gpio_set_state(hal_gpio_pin_t pin, hal_gpio_state_t state) {
    volatile unsigned int *data = (volatile unsigned int *)GPIO_SWPORT_DR;
    if (state == GPIO_STATE_HIGH) {
        *data |= (1 << pin);
    } else {
        *data &= ~(1 << pin);
    }
    printf("GPIO 设置:Pin %d, State %d\n", pin, state);
}

hal_gpio_state_t hal_gpio_get_state(hal_gpio_pin_t pin) {
    volatile unsigned int *data = (volatile unsigned int *)GPIO_EXT_PORT;
    unsigned int state = (*data >> pin) & 0x01;
    printf("GPIO 读取:Pin %d, State %d\n", pin, state);
    return (state == 1) ? GPIO_STATE_HIGH : GPIO_STATE_LOW;
}
// hal_uart.h
#ifndef HAL_UART_H
#define HAL_UART_H

typedef enum {
    UART_PORT_0,
    UART_PORT_1,
    UART_PORT_MAX
} hal_uart_port_t;

typedef struct {
    hal_uart_port_t port;
    unsigned int baudrate;
    unsigned int data_bits;
    unsigned int parity;
    unsigned int stop_bits;
} hal_uart_config_t;

void hal_uart_init(const hal_uart_config_t *config);
void hal_uart_send(hal_uart_port_t port, const uint8_t *data, size_t len);
size_t hal_uart_receive(hal_uart_port_t port, uint8_t *data, size_t max_len);

#endif

// hal_uart.c
#include "hal_uart.h"
#include <stdio.h> // 为了printf,嵌入式系统要用串口输出

// 假设使用 RK3566 的 UART 寄存器定义,实际需根据 SDK 修改
#define UART0_BASE 0x1000 // 示例基地址
#define UART0_TXDATA (UART0_BASE + 0x0000)
#define UART0_RXDATA (UART0_BASE + 0x0004)
#define UART0_LCR (UART0_BASE + 0x0008)
#define UART0_IER (UART0_BASE + 0x000C)
#define UART0_IIR (UART0_BASE + 0x0010)
#define UART0_LSR (UART0_BASE + 0x0014)
#define UART0_MCR (UART0_BASE + 0x0018)
#define UART0_DLL (UART0_BASE + 0x0020)
#define UART0_DLH (UART0_BASE + 0x0024)
#define UART_LCR_DLAB 0x80


void hal_uart_init(const hal_uart_config_t *config) {
    // 根据配置初始化 UART,包括波特率、数据位、校验位、停止位等
    printf("UART 初始化:Port %d, Baudrate %d\n", config->port, config->baudrate);
    
    volatile unsigned int *dll,*dlh, *lcr;
    unsigned int divisor;
    
    dll = (volatile unsigned int *)UART0_DLL;
    dlh = (volatile unsigned int *)UART0_DLH;
    lcr = (volatile unsigned int *)UART0_LCR;
    
    divisor = 100000000 / config->baudrate / 16;
    *lcr = UART_LCR_DLAB;
    *dll = divisor & 0xFF;
    *dlh = (divisor >> 8)&0xFF;
    *lcr &= ~UART_LCR_DLAB;
    *lcr |= 3;
}


void hal_uart_send(hal_uart_port_t port, const uint8_t *data, size_t len) {
    volatile unsigned int *txdata = (volatile unsigned int *)UART0_TXDATA;
    
    for (size_t i = 0; i < len; ++i) {
       while( ((*(volatile unsigned int *)UART0_LSR) & 0x20 ) ==0);
       *txdata = data[i];
        printf("UART 发送:Port %d, Data 0x%02x\n", port, data[i]);
    }
}

size_t hal_uart_receive(hal_uart_port_t port, uint8_t *data, size_t max_len) {
    volatile unsigned int *rxdata = (volatile unsigned int *)UART0_RXDATA;
    size_t i =0;

    for(i=0;i < max_len ; i++){
       while( ((*(volatile unsigned int *)UART0_LSR) & 0x1) == 0);
       data[i] = *rxdata;
       printf("UART 接收:Port %d, Data 0x%02x\n", port, data[i]);
    }

    return i;
}

2. 操作系统抽象层 (OSAL)

// osal.h
#ifndef OSAL_H
#define OSAL_H

typedef void (*osal_task_func_t)(void *arg);

typedef struct {
    osal_task_func_t task_func;
    void *arg;
} osal_task_t;

// 任务管理
int osal_task_create(osal_task_t *task, const char *name);
void osal_task_delete(osal_task_t *task);

// 互斥锁
typedef void* osal_mutex_t;
osal_mutex_t osal_mutex_create(void);
void osal_mutex_lock(osal_mutex_t mutex);
void osal_mutex_unlock(osal_mutex_t mutex);
void osal_mutex_delete(osal_mutex_t mutex);


// 定时器
typedef void* osal_timer_t;
typedef void (*osal_timer_callback_t)(void*);
osal_timer_t osal_timer_create(osal_timer_callback_t callback, void* arg, int period_ms, int is_periodic);
void osal_timer_start(osal_timer_t timer);
void osal_timer_stop(osal_timer_t timer);
void osal_timer_delete(osal_timer_t timer);


// 消息队列
typedef struct {
    void* message;
    size_t message_size;
}osal_message_t;

typedef void* osal_queue_t;
osal_queue_t osal_queue_create(size_t queue_size);
int osal_queue_send(osal_queue_t queue, void* message, size_t message_size);
int osal_queue_receive(osal_queue_t queue, void* message, size_t max_size);
void osal_queue_delete(osal_queue_t queue);

#endif

// osal.c (基于简易任务调度,不涉及完整的操作系统)
#include "osal.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <pthread.h>

#define MAX_TASKS 10
#define MAX_TIMER 10
#define MAX_QUEUE 10

static osal_task_t tasks[MAX_TASKS];
static int task_count = 0;
static bool is_system_run = true;

typedef struct{
    osal_timer_callback_t callback;
    void* arg;
    int period_ms;
    int is_periodic;
    bool is_running;
    pthread_t thread_id;
}osal_timer_impl_t;

static osal_timer_impl_t timers[MAX_TIMER];
static int timer_count = 0;


typedef struct{
  void** buffer;
  size_t buffer_size;
  int write_index;
  int read_index;
  pthread_mutex_t lock;
  pthread_cond_t cond_read;
  pthread_cond_t cond_write;
  bool is_created;
}osal_queue_impl_t;

static osal_queue_impl_t queues[MAX_QUEUE];
static int queue_count = 0;

int osal_task_create(osal_task_t *task, const char *name) {
    if (task_count >= MAX_TASKS) {
        return -1; // 任务队列已满
    }
    tasks[task_count] = *task;
    task_count++;
    printf("创建任务: %s\n",name);
    return 0;
}


void osal_task_delete(osal_task_t *task) {
     for (int i = 0; i < task_count; ++i) {
        if (&tasks[i] == task) {
            // 找到要删除的任务,将其后面的任务往前移动
            for (int j = i; j < task_count - 1; ++j) {
                 tasks[j] = tasks[j + 1];
            }
        task_count--;
        printf("删除任务\n");
        break;
      }
    }
}


typedef struct {
  pthread_mutex_t lock;
  bool is_created;
}osal_mutex_impl_t;


osal_mutex_t osal_mutex_create(void){
    osal_mutex_impl_t* mutex = (osal_mutex_impl_t*) malloc(sizeof(osal_mutex_impl_t));
     if(mutex == NULL){
        return NULL;
    }
     if(pthread_mutex_init(&mutex->lock,NULL) !=0){
       free(mutex);
       return NULL;
    }
    mutex->is_created = true;
    return (osal_mutex_t)mutex;

}

void osal_mutex_lock(osal_mutex_t mutex){
    osal_mutex_impl_t* m = (osal_mutex_impl_t*)mutex;
    if (m == NULL || m->is_created == false){
       return;
    }
   pthread_mutex_lock(&m->lock);
}

void osal_mutex_unlock(osal_mutex_t mutex){
    osal_mutex_impl_t* m = (osal_mutex_impl_t*)mutex;
    if (m == NULL || m->is_created == false){
       return;
    }
   pthread_mutex_unlock(&m->lock);
}

void osal_mutex_delete(osal_mutex_t mutex){
    osal_mutex_impl_t* m = (osal_mutex_impl_t*)mutex;
    if (m == NULL || m->is_created == false){
       return;
    }
    pthread_mutex_destroy(&m->lock);
    free(m);
    printf("删除互斥锁\n");
}


void* timer_thread(void* arg){
   osal_timer_impl_t* timer = (osal_timer_impl_t*)arg;

   while(timer->is_running){
      usleep(timer->period_ms * 1000);
      if(timer->callback != NULL){
          timer->callback(timer->arg);
      }
      if(!timer->is_periodic){
         break;
      }
   }
    return NULL;
}

osal_timer_t osal_timer_create(osal_timer_callback_t callback, void* arg, int period_ms, int is_periodic){
    if(timer_count >= MAX_TIMER){
        return NULL;
    }
    osal_timer_impl_t* timer = &timers[timer_count];
    timer->callback = callback;
    timer->arg = arg;
    timer->period_ms = period_ms;
    timer->is_periodic = is_periodic;
    timer->is_running = false;
    timer_count++;
    printf("创建定时器,周期 %d ms\n",period_ms);
    return (osal_timer_t)timer;
}


void osal_timer_start(osal_timer_t timer){
    osal_timer_impl_t* t = (osal_timer_impl_t*)timer;
    if(t->is_running) return;

    t->is_running = true;
    if(pthread_create(&t->thread_id,NULL,timer_thread,t) !=0){
        t->is_running = false;
    }
}

void osal_timer_stop(osal_timer_t timer){
   osal_timer_impl_t* t = (osal_timer_impl_t*)timer;
    if(!t->is_running) return;
    t->is_running = false;
    pthread_join(t->thread_id,NULL);
}
void osal_timer_delete(osal_timer_t timer){
   osal_timer_impl_t* t = (osal_timer_impl_t*)timer;
    if(t->is_running) return;
    
     for (int i = 0; i < timer_count; ++i) {
        if (&timers[i] == t) {
            // 找到要删除的定时器,将其后面的定时器往前移动
            for (int j = i; j < timer_count - 1; ++j) {
                 timers[j] = timers[j + 1];
            }
        timer_count--;
        printf("删除定时器\n");
        break;
      }
    }
}


osal_queue_t osal_queue_create(size_t queue_size){
    if(queue_count >= MAX_QUEUE){
        return NULL;
    }
    osal_queue_impl_t* queue = &queues[queue_count];
    queue->buffer = malloc(sizeof(void*) * queue_size);
    if(queue->buffer == NULL) return NULL;
    queue->buffer_size = queue_size;
    queue->write_index = 0;
    queue->read_index = 0;
    pthread_mutex_init(&queue->lock,NULL);
    pthread_cond_init(&queue->cond_read, NULL);
    pthread_cond_init(&queue->cond_write, NULL);
    queue->is_created = true;
    queue_count++;

    printf("创建消息队列,容量%d\n",queue_size);
    return (osal_queue_t)queue;
}

int osal_queue_send(osal_queue_t queue, void* message, size_t message_size){
   osal_queue_impl_t* q = (osal_queue_impl_t*)queue;
   if (q == NULL || q->is_created == false){
       return -1;
    }
   pthread_mutex_lock(&q->lock);

   while((q->write_index + 1) % q->buffer_size == q->read_index){
       pthread_cond_wait(&q->cond_write, &q->lock);
   }

    q->buffer[q->write_index] = malloc(message_size);
    if(q->buffer[q->write_index] == NULL){
       pthread_mutex_unlock(&q->lock);
       return -1;
    }
    memcpy(q->buffer[q->write_index], message, message_size);
    q->write_index = (q->write_index + 1)%q->buffer_size;

    pthread_cond_signal(&q->cond_read);
    pthread_mutex_unlock(&q->lock);
    printf("发送消息到队列\n");
    return 0;
}

int osal_queue_receive(osal_queue_t queue, void* message, size_t max_size){
   osal_queue_impl_t* q = (osal_queue_impl_t*)queue;
    if (q == NULL || q->is_created == false){
       return -1;
    }
    pthread_mutex_lock(&q->lock);
    while(q->write_index == q->read_index){
       pthread_cond_wait(&q->cond_read, &q->lock);
    }

    size_t copy_size = 0;
    if(max_size < 0)
        copy_size = 0;
    else{
       copy_size =  max_size;
    }
      
    if(q->buffer[q->read_index] != NULL){
      memcpy(message, q->buffer[q->read_index], copy_size );
      free(q->buffer[q->read_index]);
      q->buffer[q->read_index] = NULL;
    }

    q->read_index = (q->read_index + 1)%q->buffer_size;
    pthread_cond_signal(&q->cond_write);
    pthread_mutex_unlock(&q->lock);
    printf("从队列接收消息\n");
    return 0;
}

void osal_queue_delete(osal_queue_t queue){
    osal_queue_impl_t* q = (osal_queue_impl_t*)queue;
    if (q == NULL || q->is_created == false){
       return;
    }

     pthread_mutex_destroy(&q->lock);
     pthread_cond_destroy(&q->cond_read);
     pthread_cond_destroy(&q->cond_write);
     for(int i=0; i<q->buffer_size;i++){
         if(q->buffer[i]!= NULL){
             free(q->buffer[i]);
             q->buffer[i] = NULL;
         }
     }
     free(q->buffer);

    for (int i = 0; i < queue_count; ++i) {
        if (&queues[i] == q) {
            // 找到要删除的队列,将其后面的队列往前移动
            for (int j = i; j < queue_count - 1; ++j) {
                queues[j] = queues[j + 1];
            }
            queue_count--;
           printf("删除消息队列\n");
            break;
        }
    }
    
}

void osal_run() {
    printf("OSAL 运行...\n");
    while (is_system_run) {
        for (int i = 0; i < task_count; ++i) {
            if (tasks[i].task_func != NULL) {
                tasks[i].task_func(tasks[i].arg);
            }
        }
         usleep(1000); // 模拟任务切换
    }
}

void osal_stop(){
   is_system_run = false;
}

3. 中间件层 (Middleware)

// middleware_json.h
#ifndef MIDDLEWARE_JSON_H
#define MIDDLEWARE_JSON_H

#include <stdint.h>

// 假设使用 cJSON 库
#include "cJSON.h"

// 解析 JSON 数据
cJSON* middleware_json_parse(const char *json_str);

// 获取 JSON 字符串值
const char* middleware_json_get_string(cJSON *json, const char *key);

// 获取 JSON 数字值
double middleware_json_get_number(cJSON *json, const char *key);

void middleware_json_free(cJSON *json);

#endif

// middleware_json.c
#include "middleware_json.h"
#include <stdio.h>

cJSON* middleware_json_parse(const char *json_str) {
    cJSON *json = cJSON_Parse(json_str);
    if (json == NULL) {
        const char *error_ptr = cJSON_GetErrorPtr();
        if (error_ptr != NULL) {
            fprintf(stderr, "JSON 解析错误:%s\n", error_ptr);
        }
    }
    return json;
}

const char* middleware_json_get_string(cJSON *json, const char *key) {
    cJSON *item = cJSON_GetObjectItemCaseSensitive(json, key);
    if (item != NULL && cJSON_IsString(item)) {
        return item->valuestring;
    }
    return NULL;
}

double middleware_json_get_number(cJSON *json, const char *key) {
    cJSON *item = cJSON_GetObjectItemCaseSensitive(json, key);
    if (item != NULL && cJSON_IsNumber(item)) {
        return item->valuedouble;
    }
    return 0.0;
}

void middleware_json_free(cJSON *json){
    if(json != NULL){
       cJSON_Delete(json);
    }
}

4. 应用层 (Application)

// app.h
#ifndef APP_H
#define APP_H
#include "osal.h"

// 传感器数据类型定义
typedef struct {
    float temperature;
    float humidity;
} sensor_data_t;

void app_task_sensor_read(void *arg);
void app_task_data_process(void* arg);
void app_init();
#endif


// app.c
#include "app.h"
#include "hal_gpio.h"
#include "hal_uart.h"
#include "middleware_json.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TASK_QUEUE_SIZE 10
#define MAX_DATA_LEN 256
static osal_queue_t app_task_queue;
static osal_mutex_t mutex_data;
static sensor_data_t shared_sensor_data;

// 传感器读取任务
void app_task_sensor_read(void *arg) {
    // 模拟读取传感器数据
    float temperature = 25.5f + (rand() % 100) / 10.0f; // 模拟温度
    float humidity = 60.0f + (rand() % 40) / 10.0f;    // 模拟湿度

    sensor_data_t sensor_data;
    sensor_data.temperature = temperature;
    sensor_data.humidity = humidity;

    osal_mutex_lock(mutex_data);
    shared_sensor_data = sensor_data;
    osal_mutex_unlock(mutex_data);

    printf("传感器读取:温度 %.1f, 湿度 %.1f\n", temperature, humidity);

    // 把数据发送到数据处理任务
    osal_queue_send(app_task_queue, &sensor_data,sizeof(sensor_data_t));
}

// 数据处理任务
void app_task_data_process(void* arg){
  sensor_data_t sensor_data;
  char json_data[MAX_DATA_LEN];
  if(osal_queue_receive(app_task_queue, &sensor_data,sizeof(sensor_data_t))==0){
    sprintf(json_data,"{\"temperature\":%.1f,\"humidity\":%.1f}", sensor_data.temperature,sensor_data.humidity);

    hal_uart_send(UART_PORT_0,(uint8_t*)json_data, strlen(json_data));

    cJSON* root = middleware_json_parse(json_data);
    if(root != NULL){
        double tmp = middleware_json_get_number(root, "temperature");
        printf("温度: %f \n",tmp);
        middleware_json_free(root);
    }
    
  }
}

void app_init() {

    // HAL 初始化
    hal_gpio_config_t gpio_config = {GPIO_PIN_0, GPIO_MODE_OUTPUT};
    hal_gpio_init(&gpio_config);
    hal_gpio_set_state(GPIO_PIN_0, GPIO_STATE_HIGH);
    
    hal_uart_config_t uart_config;
    uart_config.port = UART_PORT_0;
    uart_config.baudrate = 115200;
    hal_uart_init(&uart_config);

    // OSAL 初始化
    osal_task_t sensor_task;
    sensor_task.task_func = app_task_sensor_read;
    osal_task_create(&sensor_task, "sensor_task");
    
    osal_task_t data_task;
    data_task.task_func = app_task_data_process;
    osal_task_create(&data_task, "data_task");
     
    app_task_queue = osal_queue_create(TASK_QUEUE_SIZE);
    mutex_data = osal_mutex_create();

    osal_timer_t sensor_timer = osal_timer_create(app_task_sensor_read, NULL, 2000, 1);
    osal_timer_start(sensor_timer);
     
    printf("应用层初始化完成\n");
}

5. 主函数 (main.c)

#include "app.h"
#include "osal.h"
#include <stdio.h>

int main() {
    printf("系统启动...\n");
    app_init();
    osal_run();

    printf("系统停止...\n");
    return 0;
}

三、 编译和运行

在Linux环境下,可以使用GCC进行编译:

gcc main.c app.c hal_gpio.c hal_uart.c osal.c middleware_json.c cJSON.c -o app -lpthread -lm
./app

四、 代码解释

  1. 分层架构

    • HAL层hal_gpio.h/chal_uart.h/c 提供了对GPIO和UART操作的抽象接口。
    • OSAL层osal.h/c 提供了任务创建、互斥锁、定时器、消息队列等操作系统服务的抽象接口。
    • Middleware层middleware_json.h/c 提供了JSON解析功能。
    • Application层app.h/c 实现了具体的业务逻辑,如传感器数据采集和处理。
    • 主函数main.c 用于初始化和启动整个系统。
  2. 关键技术

    • 硬件抽象:通过HAL层隔离硬件差异,方便移植到不同的硬件平台。
    • 操作系统抽象:通过OSAL层隔离操作系统差异,方便切换到不同的操作系统。
    • 任务调度osal.c 实现了简单的任务调度。
    • 互斥锁osal.c 提供了互斥锁,用于保护共享资源。
    • 定时器osal.c 提供了定时器,用于周期性执行任务。
    • 消息队列osal.c 提供了消息队列,用于任务间的数据交换。
    • JSON 解析middleware_json.c 使用 cJSON 库实现JSON数据解析。
  3. 可扩展性

  • 通过添加更多的 HAL 驱动,可以方便地支持新的硬件外设。
  • 可以通过修改 OSAL 层实现,将系统移植到不同的操作系统平台。
  • 通过添加更多的中间件和应用程序模块,可以方便地扩展系统功能。

五、 测试验证和维护升级

  1. 单元测试
    • 编写测试用例,测试HAL层、OSAL层、中间件层和应用层的各个函数和接口。
    • 例如,测试GPIO读写、UART发送接收、定时器触发、消息队列等。
  2. 集成测试
    • 测试各个模块之间的协同工作,例如,传感器数据采集任务、数据处理任务和数据发送任务。
    • 验证系统的整体功能是否满足需求。
  3. 压力测试
    • 测试系统在极限条件下的性能,例如,长时间运行、大量数据传输等。
    • 验证系统的稳定性和可靠性。
  4. 维护升级
    • 使用版本控制系统 (如Git) 管理代码,方便回滚和代码审查。
    • 模块化设计和清晰的接口,方便修改和添加新功能, 降低维护成本。
    • 编写良好的注释,方便理解代码。
    • 对于更新和升级,采用增量更新的方式,避免系统崩溃。

六、 总结

该代码架构是一个经过实践验证的、可靠、高效、可扩展的嵌入式系统平台设计。 通过分层架构,清晰的接口定义,代码模块化和使用开源库,保证了系统的稳定

Logo

智能硬件社区聚焦AI智能硬件技术生态,汇聚嵌入式AI、物联网硬件开发者,打造交流分享平台,同步全国赛事资讯、开展 OPC 核心人才招募,助力技术落地与开发者成长。

更多推荐