推送自动初始化模块

This commit is contained in:
MacRsh
2023-02-01 02:01:58 +08:00
parent a898a0f04a
commit 1d6ce616d3
3 changed files with 253 additions and 0 deletions

View File

@@ -0,0 +1,134 @@
`mr-library` `mr-auto-init`
# 简介
**mr-auto-init** 模块为 **mr-library** 项目下的可裁剪模块以C语言编写可快速移植到各种平台主要以嵌入式mcu为主
**mr-auto-init** 模块通过隐式调用初始化函数,使代码更简介更模块化。
----------
# **mr-auto-init** 特点
* 资源占用低。
- 实用高效。
* 无需过多适配。
----------
# 代码目录
**mr-auto-init** 源代码目录结构如下图所示:
|名称|描述|
|:---|:---|
|mr_auto_init.c|源代码|
|mr_auto_init.h|头文件|
----------
# 需适配接口
|函数|描述|
|:---|:---|
|无|无|
----------
# 可使用接口
|函数|描述|
|:---|:---|
|INIT_BOARD_EXPORT()|将函数导出到板级初始化|
|INIT_DEVICE_EXPORT()|将函数导出到设备初始化|
|INIT_ENV_EXPORT()|将函数导出到环境初始化|
|INIT_APP_EXPORT()|将函数导出到应用初始化|
|mr_auto_init()|初始化所有导出的函数|
----------
# 初始化顺序
1. 板级初始化:`INIT_BOARD_EXPORT`
2. 设备初始化:`INIT_DEVICE_EXPORT`
3. 环境初始化:`INIT_ENV_EXPORT`
4. 应用初始化:`INIT_APP_EXPORT`
----------
# 使用流程
1. 复制 `模块(mr_auto_init)` 文件夹到您的工程文件。
2. 调用 `宏函数(INIT_BOARD_EXPORT / INIT_DEVICE_EXPORT / INIT_ENV_EXPORT / INIT_APP_EXPORT)` 导出初始化函数。
3.`头文件(mr_auto_init.h)` 引用到您的工程。
4. 在mian函数中调用 `初始化函数(mr_auto_init)`,将自动按初始化顺序调用导出的初始化函数。
5. 开始愉快的使用。
----------
# 使用示例
```
/* -------------------- 模拟需要导出的初始化函数 -------------------- */
int board_init(void)
{
printf("auto-init: board\r\n");
return 0;
}
INIT_BOARD_EXPORT(board_init);
int device_init(void)
{
printf("auto-init: device\r\n");
return 0;
}
INIT_DEVICE_EXPORT(device_init);
int env_init(void)
{
printf("auto-init: env\r\n");
return 0;
}
INIT_ENV_EXPORT(env_init);
int app_init(void)
{
printf("auto-init: app\r\n");
return 0;
}
INIT_APP_EXPORT(app_init);
/* -------------------- 使用 -------------------- */
int main(void)
{
/* 初始化 */
mr_auto_init();
/* 用户代码 */
...
}
```
----------
# 注意事项
当您使用中出现功能无法实现并且您的编译器为 `GCC` 时,请您手动在 `link.ld` / `SECTIONS` / `.text` 中添加以下代码。
```
/* section information for initial. */
. = ALIGN(4);
_mr_init_ = .;
KEEP(*(SORT(.mri_fn*)))
_mr_init__end = .;
```
----------
# 贡献代码
如果您在使用 **mr-auto-init** 模块中遇到了 bug 或是 您有自己的想法,欢迎您提交 pr 或者联系我(email)macrsh@outlook.com
## 感谢各位对本仓库的贡献!

View File

@@ -0,0 +1,53 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-02-01 MacRsh first version
*/
#include "mr_auto_init.h"
static int mri_start(void)
{
return 0;
}
INIT_EXPORT(mri_start, "0");
static int mri_board_start(void)
{
return 0;
}
INIT_EXPORT(mri_board_start, "0.end");
static int mri_board_end(void)
{
return 0;
}
INIT_EXPORT(mri_board_end, "1.end");
static int mri_end(void)
{
return 0;
}
INIT_EXPORT(mri_end,"6.end");
void mr_auto_init(void)
{
volatile const init_fn_t *fn_ptr;
/* auto-init-board */
for (fn_ptr = &_mr_init_mri_start; fn_ptr < &_mr_init_mri_board_end; fn_ptr++)
{
(*fn_ptr)();
}
/* auto-init-other */
for (fn_ptr = &_mr_init_mri_board_end; fn_ptr < &_mr_init_mri_end; fn_ptr++)
{
(*fn_ptr)();
}
}

View File

@@ -0,0 +1,66 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2023-02-01 MacRsh first version
*/
#ifndef MR_AUTO_INIT_H_
#define MR_AUTO_INIT_H_
/* Compiler Related Definitions */
#if defined(__ARMCC_VERSION) /* ARM Compiler */
#define mr_section(x) __attribute__((section(x)))
#define mr_used __attribute__((used))
#define mr_align(n) __attribute__((aligned(n)))
#define mr_weak __attribute__((weak))
#define mr_inline static __inline
#elif defined (__IAR_SYSTEMS_ICC__) /* for IAR Compiler */
#define mr_section(x) @ x
#define mr_used __root
#define PRAGMA(x) _Pragma(#x)
#define mr_align(n) PRAGMA(data_alignment=n)
#define mr_weak __weak
#define mr_inline static inline
#elif defined (__GNUC__) /* GNU GCC Compiler */
#define mr_section(x) __attribute__((section(x)))
#define mr_used __attribute__((used))
#define mr_align(n) __attribute__((aligned(n)))
#define mr_weak __attribute__((weak))
#define mr_inline static __inline
#elif defined (__ADSPBLACKFIN__) /* for VisualDSP++ Compiler */
#define mr_section(x) __attribute__((section(x)))
#define mr_used __attribute__((used))
#define mr_align(n) __attribute__((aligned(n)))
#define mr_weak __attribute__((weak))
#define mr_inline static inline
#elif defined (_MSC_VER)
#define mr_section(x)
#define mr_used
#define mr_align(n) __declspec(align(n))
#define mr_weak
#define mr_inline static __inline
#elif defined (__TASKING__)
#define mr_section(x) __attribute__((section(x)))
#define mr_used __attribute__((used, protect))
#define mrAGMA(x) _Pragma(#x)
#define mr_align(n) __attribute__((__align(n)))
#define mr_weak __attribute__((weak))
#define mr_inline static inline
#endif /* end of __ARMCC_VERSION */
typedef int (*init_fn_t)(void);
#define INIT_EXPORT(fn,level) \
mr_used const init_fn_t _mr_init_##fn mr_section(".mri_fn."level) = fn
/**
* Export auto-init functions
*/
#define INIT_BOARD_EXPORT(fn) INIT_EXPORT(fn, "1")
#define INIT_DEVICE_EXPORT(fn) INIT_EXPORT(fn, "2")
#define INIT_ENV_EXPORT(fn) INIT_EXPORT(fn, "3")
#define INIT_APP_EXPORT(fn) INIT_EXPORT(fn, "4")
void mr_auto_init(void);
#endif /* end of MR_AUTO_INIT_H_ */