[OpenHarmony] Porting LiteOS-M to STM32 under VSCode

[OpenHarmony] Porting LiteOS-M to STM32 under VSCode

Before, I configured the STM32 development environment under VSCode. In this section, we will make some fun based on VSCode, transplant the LiteOS-M kernel of OpenHarmony to the STM32 project, and support Hongmeng. I transplanted it a long time ago. The LiteOS version is not the version merged into OpenHarmony. The reason is that the code was downloaded. During the compilation process, it was found that several files were missing. The file reference of the kal layer is strange. I am quite pessimistic about the future direction of OpenHarmony. It's not good, it's merged for the sake of merging, and it feels like running away halfway through, but the core of LiteOS-A is developing very fast, it has been updated basically all the time, the activity is quite high, and the final effect is unknown, wait slowly write. . .

get to the point

1. Obtaining the source code

Download the source code on Gitee, pay attention to select the Develop branch of the warehouse, and pull it to the local:

20220320220407

Pull code to local

20220320221520

2. Preparing the project

Prepare an engineering environment for VSCode to develop STM32. I have written this article before, and configure the STM32 development environment according to that article.

VSCode builds STM32 development environment

We generate an STM32 development environment based on CubeMX, modify the system clock to TIM1 to prevent conflicts with System, then configure two LED light IO ports, and finally generate a Makefile project

20220320223214

Then open the project and enter:

20220320223830

In the project directory, create an OpenHarmony folder to store the LiteOS source code, and then copy the source code part downloaded from the warehouse, and the copy part is below:

20220320224552

Then make some modifications to the copied content:

  • arch only leaves the files and gcc folders under the m4 core folder under arm, as well as all include, src, and commom folders. Do not delete these common files by mistake.

20220320224750

  • target leaves OS_CONFIG in f1 in the directory as a reference file for further configuration of the system, and deletes all others

20220321131904

  • Only the following memory allocation methods are left under kernel -> base -> mem:

20220320231216

others remain the same

3. Add source code to compilation

The source code is added, and the next step is to modify the makefile to realize source code compilation. Here, a small script is generated using a path I wrote to quickly obtain the source code compilation path.

A script to develop STM32 under VSCode

The file below is my generated path information

**************************************************
* 头文件路径
**************************************************
-IOpenHarmony/arch/arm/arm-m/cortex-m4\
-IOpenHarmony/arch/arm/arm-m/include\
-IOpenHarmony/arch/arm/common/cmsis\
-IOpenHarmony/kernel/base/include\
-IOpenHarmony/kernel/extended/include\
-IOpenHarmony/kernel/include\
-IOpenHarmony/osdepends/liteos/cmsis/1.0\
-IOpenHarmony/osdepends/liteos/cmsis/2.0\
-IOpenHarmony/osdepends/liteos/cmsis\
-IOpenHarmony/targets/OS_CONFIG
**************************************************
* C文件路径
**************************************************
OpenHarmony/arch/arm/arm-m/cortex-m4/los_exc.c\
OpenHarmony/arch/arm/arm-m/src/los_hw.c\
OpenHarmony/arch/arm/arm-m/src/los_hwi.c\
OpenHarmony/arch/arm/arm-m/src/los_hw_tick.c\
OpenHarmony/kernel/base/core/los_priqueue.c\
OpenHarmony/kernel/base/core/los_swtmr.c\
OpenHarmony/kernel/base/core/los_sys.c\
OpenHarmony/kernel/base/core/los_task.c\
OpenHarmony/kernel/base/core/los_tick.c\
OpenHarmony/kernel/base/core/los_timeslice.c\
OpenHarmony/kernel/base/ipc/los_event.c\
OpenHarmony/kernel/base/ipc/los_mux.c\
OpenHarmony/kernel/base/ipc/los_queue.c\
OpenHarmony/kernel/base/ipc/los_sem.c\
OpenHarmony/kernel/base/mem/bestfit_little/los_heap.c\
OpenHarmony/kernel/base/mem/bestfit_little/los_memory.c\
OpenHarmony/kernel/base/mem/common/los_memcheck.c\
OpenHarmony/kernel/base/mem/common/los_memstat.c\
OpenHarmony/kernel/base/mem/common/los_slab.c\
OpenHarmony/kernel/base/mem/common/los_slabmem.c\
OpenHarmony/kernel/base/mem/membox/los_membox.c\
OpenHarmony/kernel/base/misc/los_misc.c\
OpenHarmony/kernel/base/om/los_err.c\
OpenHarmony/kernel/extended/tickless/los_tickless.c\
OpenHarmony/kernel/los_init.c\
OpenHarmony/osdepends/liteos/atiny_osdep.c\
OpenHarmony/osdepends/liteos/cmsis/1.0/cmsis_liteos1.c\
OpenHarmony/osdepends/liteos/cmsis/2.0/cmsis_liteos2.c\
OpenHarmony/osdepends/liteos/cmsis/cmsis_liteos.c
**************************************************
* 汇编文件路径
**************************************************
OpenHarmony/arch/arm/arm-m/cortex-m4/gcc/los_dispatch_gcc.S\
OpenHarmony/arch/arm/arm-m/cortex-m4/gcc/los_hw_exc_gcc.S\
./startup_stm32f407xx.s
**************************************************

Among them we want to delete some content

header file removal

-IOpenHarmony/osdepends/liteos/cmsis/1.0\
-IOpenHarmony/osdepends/liteos/cmsis/2.0\

C file delete

OpenHarmony/osdepends/liteos/atiny_osdep.c\
OpenHarmony/osdepends/liteos/cmsis/1.0/cmsis_liteos1.c\
OpenHarmony/osdepends/liteos/cmsis/2.0/cmsis_liteos2.c\

assembly delete

OpenHarmony/arch/arm/arm-m/cortex-m4/gcc/los_hw_exc_gcc.S\
./startup_stm32f407xx.s

Will

OpenHarmony/arch/arm/arm-m/cortex-m4/gcc/los_dispatch_gcc.S\

change into

OpenHarmony/arch/arm/arm-m/cortex-m4/gcc/los_dispatch_gcc.s\

And change the suffix of the file name to lowercase, because the generated Makefile builds the file according to lowercase

Then copy the rest to the makefile respectively

C files follow C_SOURCES, header file paths follow C_INCLUDES, and assembly files follow ASM_SOURCES

After the copy is completed, delete the kernel timer interrupt and pendsv abnormal interrupt under stm32f4xx_it.c in the code generated by STM32CubeMX, because LiteOS needs to take over these two interrupts

20220320231216

Then compile the code, there may be some warnings, as follows:

20220321125212

The error is that the function los_printf lacks the header file stdio.h. After adding the header file, no error will be reported. The compilation results are as follows:

20220320232230

Fourth, add lighting tasks

Modify the memory code in the target_config.h file, and modify the SRAM to 112K. Although the RAM of the STM32F407 has 192K, it is divided into three sections of memory, not continuous memory space. Here we use the RAM section, starting from 0x20000000 112K space

20220321150154

Enter the man.c function and call the cmsis interface

#include "cmsis_os.h"

Then define the task handle

osThreadId_t led_taskHandle;
const osThreadAttr_t led_task_attributes = {
    
    
    .name = "led_task",
    .stack_size = 512 * 1,
    .priority = (osPriority_t)osPriorityNormal,
};

write task entity

void Led_Task(void *argument)
{
    
    
    while (1)
    {
    
    
        HAL_GPIO_TogglePin(LED0_GPIO_Port, LED0_Pin);
        HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
        osDelay(1000);
    }
}

In the main function, initialize the system, the task handle and start the system kernel

    osKernelInitialize();
    led_taskHandle = osThreadNew(Led_Task, NULL, &led_task_attributes);
    osKernelStart();

Post-download phenomenon

Please add image description

Guess you like

Origin blog.csdn.net/qq_45396672/article/details/123637034