注意:本项目目前还在开发中...还不能正常使用...待完善...
一个目前主要为嵌入式系统打造的,基于对象系统的,类似于GObject/COM的通用高解耦、跨平台的统一开发基础库。(理论上Linux也能用)
编译平台: platformio (未来可能支持CMake)
但是对platformio的项目结构做了些许改造,使应用层代码放在了app目录下,其他目录为框架核心代码。
NXCore库是核心的面向系统库 PAL层用于统合各个平台/framework的抽象(如Arduino、ESP-IDF、STM32_HAL库等),提供统一接口,并实现不同平台之间的差异化处理。 便于项目的移植和扩展。 目前正在考虑是否让PAL层独立运行。
试图内嵌一些平台支持,如CubeMX等,使得可以直接使用CubeMX生成的代码。
主要设计模式有: 面向对象(Object Oriented) 依赖注入(Dependency Injection) 统一架构 (Unified Architecture) 跨平台硬件抽象 (Platform Abstraction Layer) 并支持混合驱动策略 (Hybrid HAL/LL)
隔离原则:平台相关代码(CubeMX/HAL)与平台无关代码(业务逻辑/对象系统)严格隔离 单向依赖:高层模块依赖抽象接口,绝不依赖具体实现;依赖方向始终从应用层流向硬件层 渐进式架构:采用垂直切片开发,每个阶段都交付可运行的完整功能,而非一次性构建完整架构 库优先思维:NXCore是独立的可复用库,而非项目模板,具备完整的自描述能力(library.json) 显式优于隐式:所有平台选择、功能配置都在编译时通过宏明确指定,避免运行时意外
-
✨ 核心特性
-
🏗️ 统一对象模型:内置基于C语言的高内聚、低耦合对象系统,提供类、继承、多态等OOP特性,让嵌入式代码更模块化、更易维护。
-
🌉 跨平台硬件抽象层(PAL):定义了一套稳定的硬件操作接口。平台实现(如 PAL-STM32, PAL-Linux)负责对接具体驱动(HAL库、Linux内核驱动等),应用层代码无需关心底层差异。
-
⚡ 混合驱动策略(HAL/LL):在STM32平台,我们提供智能的混合驱动方案。框架可自动或按配置选择使用ST的HAL库或更高效的LL库,在开发效率与运行性能间取得最佳平衡。
-
🔌 CubeMX 兼容:尊重现有开发流程。生成的HAL初始化代码可被无缝集成到框架中,保护您的既有投资。
-
📚 开箱即用的组件库:基于此框架构建了一系列通用组件(如日志系统、环形缓冲区、命令行接口、设备驱动模型等),加速项目开发。
-
✨ Core Features
-
🏗️ Unified Object Model: Built on C, provides class, inheritance, polymorphism OOP features, making embedded code more modular and easier to maintain.
-
🌉 Platform Abstraction Layer (PAL): Defines a stable hardware operation interface. Platform implementations (such as PAL-STM32, PAL-Linux) are responsible for connecting to specific drivers (HAL libraries, Linux kernel drivers), and application layer code does not need to care about the underlying differences.
-
⚡ Hybrid Driver Strategy (HAL/LL): On the STM32 platform, we provide an intelligent hybrid driver solution. The framework can automatically or configurably choose to use ST's HAL library or the more efficient LL library, achieving the best balance between development efficiency and runtime performance.
-
🔌 CubeMX Compatible: Respecting existing development processes. The generated HAL initialization code can be integrated seamlessly into the framework, protecting your existing investment.
-
📚 Out-of-the-box Component Library: A series of common components (such as logging system, ring buffer, command line interface, device driver model, etc.) are built based on this framework to accelerate project development
都是想法...待实现
框架架构:
-
框架核心:
- 对象系统(Object System)
- 内存管理(Memory Management)
- 类型注册(Type Registry)
- 系统服务(System Services)
- 策略管理(Strategy Management)
- 工厂系统(Factory System)
- 元工厂(Meta Factory)
- 管理器系统(Manager System)
- 插件系统(Plugin System)
-
由NXCore的Include提供了框架核心的接口:
- Core组件:对象系统、类型注册等基础设施
-
PAL组件:平台抽象层,定义硬件接口
- 实际上是对NXCore的一些接口的实现。
- 根据编译时宏定义,选择对应的平台实现。
- 组合方式:PAL库实则实现了一个具体的“工厂”并注册到NXCore的元工厂中。由工厂创建具体硬件对象时便可无感地使用。
-
内嵌组件:
- 一些用NXCore提前实现好的模块,如日志服务、内存池、队列、性能检测器等。
- 模块化,可独立编译、可动态加载。
-
外置组件:
- 用户自定义的模块,如具体的传感器驱动、业务逻辑等。
- 通过包含NXCore的头文件,使用其提供的对象系统、工厂系统等功能.
- 我自己也写了一点外置组件,分开库有助于提升可维护性。
此处仅作简介,具体见各子目录的README.md 仅供参考,项目仍在开发中,结构可能会有变动(实际上已经大幅度改动了), 这万恶的提前优化... 请以实际代码库为准。 后续完善中...
English: Here is only a brief introduction, and the actual code repository is referenced. The structure may change. Please refer to the actual code repository.
NXCore_DEV/ # PlatformIO项目根目录
├── .gitignore
├── platformio.ini # 极简配置(<15行)
├── README.md
├── CMakeLists.txt # 顶层CMake构建脚本(目前使用platformio构建,不存在此文件)
├── doc/ # 文档目录
│ └── ... # 各类设计文档、说明文档等
│
├── app/ # 【用户空间】业务逻辑,完全平台无关
│ ├── include/ # 应用头文件
│ | ├── main.h # 应用入口声明
│ │ ├── app_config.h # 应用级配置
│ │ ├── led_manager.h # 用户业务模块示例
│ │ └── sensor_processor.h
│ │
│ └── src/ # 应用源文件
| ├── main.c # 应用入口(平台无关)
│ ├── main_platform.c # 平台相关入口(唯一例外)
│ ├── app_core.c # 应用核心初始化
│ ├── led_manager.c
│ └── sensor_processor.c
│
├── lib/ # PlatformIO库目录
│ ├── NXCore/ # 【框架库】核心开发区
│ │ ├── library.json # NXCore库元数据
│ │ ├── CMakeLists.txt # 未来CMake支持
│ │ ├── LICENSE
│ │ ├── README.md # 库使用文档
│ │ │
│ │ ├── include/ # 公共API接口层
| │ | ├── Core/ # Core组件公共接口
| │ │ │ ├── NXCore_Object.h # 对象系统公共接口
| │ │ │ ├── NXCore_Memory.h # 内存管理公共接口
| │ │ │ ├── NXCore_Registry.h # 类型注册公共接口
| │ | ├── NXCore.h # 总入口:包含所有公共头文件
│ │ │ ├── NXCore_Config.h # 配置宏定义
| │ | ├── NXCore_Factory.h # 工厂系统公共接口
│ │ │ ├── NXCore_GPIO.h # GPIO接口定义
│ │ │ ├── NXCore_UART.h # UART接口定义(待创建)
│ │ │ ├── NXCore_I2C.h # I2C接口定义(待创建)
│ │ │ ├── NXCore_SPI.h # SPI接口定义(待创建)
│ │ │ ├── NXCore_Timer.h # Timer接口定义(待创建)
│ │ │ ├── nxcore_pal.h # PAL抽象接口汇总
│ │ │ ├── nxcore_object.h # 对象系统接口
│ │ │ ├── nxcore_factory.h # 工厂系统接口
│ │ │ ├── nxcore_service.h # 服务系统接口
│ │ │ └── nxcore_manager.h # 管理器系统接口
│ │ │
│ │ └── src/ # 内部实现层(用户不直接包含)
│ │ ├── core/ # Core组件:平台无关基础设施
│ │ │ ├── object/
│ │ │ │ ├── nxcore_object.c
│ │ │ │ ├── nxcore_interface.c
│ │ │ │ └── nxcore_refcount.c
│ │ │ ├── memory/
│ │ │ │ ├── nxcore_mempool.c
│ │ │ │ └── nxcore_allocator.c
│ │ │ ├── registry/
│ │ | | ├── nxcore_registry.c
│ │ | | └── nxcore_typeinfo.c
│ │ │ ├── system/
│ │ │ │ ├── nxcore_system.c
│ │ │ │ └── nxcore_thread.c
│ │ │ ├── strategy/
│ │ │ │ ├── nxcore_strategy.c
│ │ │ │ └── nxcore_strategy_manager.c
│ │ │ └── ...
| │ ├── Framework/ # Framework组件:硬件无关接口定义
│ │ └── ...
│ └── PAL/ # PAL组件:平台抽象层(核心解耦层)
│ │ ├── nxpal_core.c # PAL核心分发器
│ │ ├── interface/ # 纯C抽象接口定义
│ │ │ ├── nxpal_gpio.h
│ │ │ ├── nxpal_uart.h
│ │ │ ├── nxpal_i2c.h
│ │ │ ├── nxpal_spi.h
│ │ │ └── nxpal_timer.h
│ │ │
│ │ └── platforms/ # 平台具体实现
│ │ ├── stm32/ # STM32实现(基于CubeMX/HAL)
│ │ │ ├── nxpal_stm32_gpio.c
│ │ │ ├── nxpal_stm32_uart.c
│ │ │ ├── nxpal_stm32_hal_adapter.c
│ │ │ └── cubemx/ # CubeMX生成代码(视为只读资源)
│ │ │ ├── Core/
│ │ │ ├── Drivers/
│ │ │ └── ... # CubeMX项目原样保留
│ │ │
│ │ └── linux/ # Linux/POSIX实现
│ │ ├── nxpal_linux_gpio.c
│ │ ├── nxpal_linux_uart.c
│ │ └── nxpal_posix_adapter.c
│ │
│ ├── factories/ # 工厂组件:硬件对象创建
│ │ ├── nxcore_gpio_factory.c
│ │ ├── nxcore_uart_factory.c
│ │ └── nxcore_factory_manager.c
│ │
│ ├── services/ # 服务组件:系统级服务
│ │ ├── nxcore_log_service.c
│ │ ├── nxcore_timer_service.c
│ │ └── nxcore_event_service.c
│ │
│ └── managers/ # 管理器组件:对象生命周期管理
│ ├── nxcore_object_manager.c
│ ├── nxcore_interface_manager.c
│ └── nxcore_plugin_manager.c
│
│
├── boards/ # PlatformIO板级配置(可选)
├── tools/ # 工具链目录
│ ├── scripts/ # 配置生成、代码同步等脚本
│ └── cmake/ # CMake工具链文件
│
└── test/ # 项目级集成测试
├── unit/ # 单元测试(在Linux上运行)
└── integration/ # 集成测试
- 核心层(Core Layer)
- 框架层(Framework Layer)
- 硬件抽象层(PAL Layer)
- 平台实现层(Platform Layer)
Chinese:
- 使用CubeMX打开lib/NXCore/Platform/STM32_Cube目录下的对应主控IOC文件,生成初始化代码,配置好时钟、外设等。
- 将生成的代码集成到NXCore的Platform/STM32_Cube目录中。(一般情况下默认就会生成在该目录下)
- 此目录下的Drivers不会同步到git仓库中,理论上这里并不会用到(用Platformio提供的framework而非这里)
English:
- Open the corresponding main control IOC file in the lib/NXCore/Platform/STM32_Cube directory using CubeMX to generate initialization code and configure clocks, peripherals, etc.
- Integrate the generated code into the NXCore's Platform/STM32_Cube directory. (In general, it will be generated in this directory by default)
- The Drivers in this directory will not be synchronized to the git repository, and theoretically, it will not be used here (use the framework provided by Platformio instead of here).
- 直接使用Platformio构建运行即可,无需额外配置. (实际上Linux还没有开始开发...)