====== Building LiteOS With Keil ======
===== - 摘要 =====
Porting LiteOS to NRF52832, using Keil to build.
本文使用LiteOS的版本为 V200R001C50B037_20181226,获取方式如下:
git clone https://gitee.com/LiteOS/LiteOS.git
git checkout tag_LiteOS_V200R001C50B037_20181226
===== - 准备一个裸机工程 =====
使用SDK中的例子工程,nRF5_SDK_17.0.2_d674dde\examples\peripheral\blinky :
* 复制工程配置文件 blinky_pca10040.uvprojx (位于目录nRF5_SDK_17.0.2_d674dde\examples\peripheral\blinky\pca10040\blank\arm5_no_packs)
* 复制代码文件 main.c (位于目录E:\nrfProjects\nRF5_SDK_17.0.2_d674dde\examples\peripheral\blinky)
* 复制SDK配置文件 sdk_config.h (位于目录E:\nRF5_SDK_17.0.2_d674dde\examples\peripheral\blinky\pca10040\blank\config)
* 链接(或复制)SDK到工程更目录(mklink /D nRF5_SDK_17.0.2_d674dde ..\nRF5_SDK_17.0.2_d674dde)
* 在Keil中打开工程(可能会缺少工具包,跳过,在软件内设置或安装工具包)
工程经过拷贝后,路径发生已改变,需要重新设置:
* 代码文件路径(直接在工程配置文件.uvprojx中批量修改)
* 修改编译参数中的头文件搜索路径(否侧缺失头文件报错)
* 修改调试器(根据实际使用的调试器设置,例如:J-LINK,SW模式)
* 编译、加载运行
* 4个LED交替点亮/熄灭
裸机工程准备完毕。
===== - *准备打印输出端口 =====
使用 RTT 作为打印输出端口 :
* 链接(或复制)RTT代码包到当前工程(mklink /D RTT ..\SEGGER_RTT_V765b)
* 添加代码文件 SEGGER_RTT.c, SEGGER_RTT_printf (路径: RTT\RTT)
* 添加头文件搜索目录 ( RTT\RTT, RTT\Config)
* 测试打印端口,添加打印代码
#include "SEGGER_RTT.h"
...
...
SEGGER_RTT_WriteString(0, "SEGGER Real-Time-Terminal For LiteOS Builder\r\n\r\n");
^ 模块 ^ 根路径 ^ 目录 ^ 文件 ^ 说明 ^
| arch | arch\arm\arm-m | cortex-m4\keil | los_dispatch_keil.S | |
| ::: | ::: | include | los_exc.h los_hw.h los_hw_tick.h los_hwi.h | |
| ::: | ::: | src | los_hw.c | |
| ::: | ::: | ::: | los_hw_tick.c | |
| ::: | ::: | ::: | los_hwi.c | |
| kernel | kernel || los_init.c | |
| ::: | kernel | base\core | * | |
| ::: | ::: | base\ipc | * | |
| ::: | ::: | base\mem\bestfit_little | * | |
| ::: | ::: | base\mem\membox | * | |
| ::: | ::: | base\misc | * | |
| ::: | ::: | base\om | * | |
| ::: | ::: | extended\tickless | * | |
| config | targets\Cloud_STM32F429IGTx_FIRE\OS_CONFIG || target_config.h | 1. 复制所有文件用以建立新的硬件版本(例如targets\NRF52)\\ 2. 修改target_config.h中芯片定义文件引用:#include "stm32f4xx.h" --> #include "nrf52.h" \\ 3. 不接管中断,修改target_config.h, #define LOSCFG_PLATFORM_HWI NO |
| ::: | ::: | ::: | los_printf.h | ::: |
| ::: | ::: | ::: | los_builddef.h | ::: |
===== - 代码修改 =====
^ 文件 ^ 修改内容 ^ 说明 ^
|target_config.h| #define LOSCFG_PLATFORM_HWI YES | 改为 #define LOSCFG_PLATFORM_HWI NO, LiteOS不接管中断 |
|:::| #include "stm32f4xx.h" | 改为#include "nrf52.h",使用NRF的器件定义|
|los_printf.h|添加 \\ #include "SEGGER_RTT.h" \\ #define printf(fmt,args...) SEGGER_RTT_printf(0,fmt, ##args) | 通过RTT实现LiteOS打印输出 |
修改 main.c,测试建立任务和调度任务。
#include
#include
#include "nrf_delay.h"
#include "SEGGER_RTT.h"
#include "boards.h"
#include "los_sys.h"
#include "los_task.h"
#define __HEAP_SIZE__ (16384)
uint8_t _heap[__HEAP_SIZE__];
uint32_t __LOS_HEAP_ADDR_START__;
uint32_t __LOS_HEAP_ADDR_END__;
void* led_task(UINT32 arg)
{
for(;;)
{
if( arg < 4 ) bsp_board_led_invert(arg);
LOS_TaskDelay(1000);
}
}
UINT32 led1_task_handle;
UINT32 led2_task_handle;
int main(void)
{
TSK_INIT_PARAM_S task_param;
TSK_INIT_PARAM_S task_param2;
__LOS_HEAP_ADDR_START__ = (UINT32)_heap;
__LOS_HEAP_ADDR_END__ = __LOS_HEAP_ADDR_START__ + __HEAP_SIZE__ -1;
SEGGER_RTT_WriteString(0, "SEGGER Real-Time-Terminal For LiteOS Builder\r\n\r\n");
/* Configure board. */
bsp_board_init(BSP_INIT_LEDS);
uint32_t retcd;
retcd = LOS_KernelInit();
if( retcd == LOS_OK ) {
PRINT_EMG("Initialize LiteOS Kernel successful!\r\n");
}
else {
PRINT_ERR("Initialize LiteOS Kernel failed!\r\n");
}
task_param.pcName = "led1_task";
task_param.pfnTaskEntry = led_task;
task_param.uwStackSize = 512;
task_param.usTaskPrio = 4;
task_param.uwArg = 0;
retcd = LOS_TaskCreate(&led1_task_handle, &task_param);
if( retcd == LOS_OK) {
PRINT_EMG("Create led1 task successful!\r\n");
}
else {
PRINT_ERR("Create led1 task failed!\r\n");
}
task_param2.pcName = "led2_task";
task_param2.pfnTaskEntry = led_task;
task_param2.uwStackSize = 512;
task_param2.usTaskPrio = 4;
task_param2.uwArg = 3;
retcd = LOS_TaskCreate(&led2_task_handle, &task_param2);
if( retcd == LOS_OK) {
PRINT_EMG("Create led2 task successful!\r\n");
}
else {
PRINT_ERR("Create led2 task failed!\r\n");
}
LOS_Start();
/* Toggle LEDs. */
while (true) {
for (int i = 0; i < LEDS_NUMBER; i++) {
bsp_board_led_invert(i);
nrf_delay_ms(500);
}
}
}