系统环境
- 系统:macOS 11.2
- 网络:联网
软件环境
- 编译器环境:SEGGER Embedded Studio v5.40(SES)
- 镜像烧录器:nRF Connect v3.6.1
- 仿真器驱动:J-Link Software and Documentation Pack v6.94a
- 软件开发包:nRF5 SDK v15.3.0
硬件环境
- 开发板:nRF52 DK v1.1.0
创建库工程
- 在 Embedded Studio 的菜单栏点击“File -> New Project...”,在弹出的向导中选择“An ARM library project.”选项,并为工程添加名字,如此处“xz_alg_plus”。之后点击“Next”。
- 由于要使用 nRF52832 作为硬件平台,这里选择 nRF52832 的内核,即“Cortex-M4”。注意这里没有“Cortex-M4F”选项, 但加入 FPU 可以在后面的工程设置中更改。点击“Next”直到工程建立。
- 在左侧工程导航栏,右键点击工程名,选择“Add New File...”来添加一对头文件和源文件。分别命名为“xz_alg_plus.h”和“xz_alg_plus.c”。
- 这里仅以一个简单的加法例子作为库的功能。在头文件“xz_alg_plus.h”中输入如下示例代码:
/******************************************************************************
* File xz_alg_plus.h
*
* Brief This file contains definitions and prototypes of the plus algorithm
* made by XizhiTek.
*
* Target nRF52832-QFAA
******************************************************************************/
#ifndef XZ_ALG_PLUS_H
#define XZ_ALG_PLUS_H
#ifdef __cplusplus
extern "C"
{
#endif
/******************************************************************************
* Includes
******************************************************************************/
#include <stdint.h>
/******************************************************************************
* Functions
******************************************************************************/
/******************************************************************************
* Fn xz_alg_plus
*
* Brief Get the sum of two addends.
*
* Param [in]addend_a: an addend;
* [in]addend_b: another addend.
*
* Return sum: the sum of the two addends.
******************************************************************************/
extern uint8_t xz_alg_plus(uint8_t addend_a, uint8_t addend_b);
#ifdef __cplusplus
}
#endif
#endif /* XZ_ALG_PLUS_H */
- 在源文件“xz_alg_plus.c”中输入如下示例代码:
/******************************************************************************
* File xz_alg_plus.c
*
* Brief This file contains the implement of the plus algorithm made by
* XizhiTek.
*
* Target nRF52832-QFAA
******************************************************************************/
/******************************************************************************
* Includes
******************************************************************************/
#include "xz_alg_plus.h"
/******************************************************************************
* Functions
******************************************************************************/
/******************************************************************************
* Fn xz_alg_plus
*
* Brief Get the sum of two addends.
*
* Param [in]addend_a: an addend;
* [in]addend_b: another addend.
*
* Return sum: the sum of the two addends.
******************************************************************************/
uint8_t xz_alg_plus(uint8_t addend_a, uint8_t addend_b)
{
uint8_t sum = 0;
sum = addend_a + addend_b;
return sum;
}
- 如果想要启用 nRF52832 的 FPU,需要更改工程设置。在左侧工程导航栏中,右键点击工程名,选择“Option”,在“Code Generation”中,将“ARM FP ABI Type”选项设置为“Hard”,即启用硬件 FPU。再将“ARM FPU Type”设置为“FPv4-SP-D16”。
- 点击右上角的“Build”或者按 F7 键开始构造,可以看到构造无误。左侧出现“Output Files”文件夹,可以看待文件夹内已包含生成的“xz_alg_plus.a”库文件。至此,静态库文件创建完成。
验证库文件
- 在 nRF5 SDK 中的“peripheral”目录下的“blinky”示例做修改,先讲做好的库文件“xz_alg_plus.a”和头文件“xz_alg_plus.h”放在“main.c”所在目录中。这里使用“blank”的工程。
- 在 SES 中打开此工程,将库文件“xz_alg_plus.a”和头文件“xz_alg_plus.h”添加到工程的“Aplicaiton”文件夹下。在“main.c”文件中添加头文件,并修改主函数,来改变延时时间,代码如下:
/** @file
*
* @defgroup blinky_example_main main.c
* @{
* @ingroup blinky_example
* @brief Blinky Example Application main file.
*
* This file contains the source code for a sample application to blink LEDs.
*
*/
#include <stdbool.h>
#include <stdint.h>
#include "nrf_delay.h"
#include "boards.h"
#include "xz_alg_plus.h"
/**
* @brief Function for application main entry.
*/
int main(void)
{
uint8_t time = 0;
/* Configure board. */
bsp_board_init(BSP_INIT_LEDS);
/* Toggle LEDs. */
while (true)
{
for (int i = 0; i < LEDS_NUMBER; i++)
{
bsp_board_led_invert(i);
time = xz_alg_plus(100, 80);
nrf_delay_ms(time);
}
}
}
/**
*@}
**/
- 此时,延时时间应从原来的 500ms 变为 180ms。点击右上角的“Build”或者按 F7 键开始构造,可以看到构造无误。
- 开始调试,可以在断点处看到,变量“time”的值已经变成 0xb4,即 180。这说明,库文件已经正常工作。验证成功。