STM32f系列芯片快速移植OpenHarmony3

EMb-Y 2022-03-21 00:21:59

如何快速在STM32上用上OpenHarmony3

手把手带你从0开始移植鸿蒙到stm32f系列芯片,门槛极低照做就能成功。

本文基于OpenHarmony3.0.2LTS版本进行移植,采用stm32f411芯片,通过cubemx快速生成工程文件。

第一步:通过cubemx生成makefile工程文件

可以看到主要初始化了时钟、usart和一颗led。usart主要用于查看串口打印, led呢就是传统艺能开机先电灯了。

因为cubemx的教程已经很多,这里就不再过多赘述了。但是这其中有最重要的一步,Toolchain/IDE这里一定要选择Makeflie!!!

 配置好以后直接生成项目,我们就会得到一个这样的目录,到这里我们的第一步就已经完成了。

 第二步:在工程中添加OpenHarmony的一些配置文件

首先添加target_config.h文件

/*
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**@defgroup los_config System configuration items
 * @ingroup kernel
 */

#ifndef _TARGET_CONFIG_H
#define _TARGET_CONFIG_H

#include "stm32f4xx.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif /* __cplusplus */

/*=============================================================================
                                        System clock module configuration
=============================================================================*/
#define OS_SYS_CLOCK                                        SystemCoreClock
#define LOSCFG_BASE_CORE_TICK_PER_SECOND                    (1000UL)
#define LOSCFG_BASE_CORE_TICK_HW_TIME                       0
#define LOSCFG_BASE_CORE_TICK_WTIMER                        0
#define LOSCFG_BASE_CORE_TICK_RESPONSE_MAX                  SysTick_LOAD_RELOAD_Msk

/*=============================================================================
                                        Hardware interrupt module configuration
=============================================================================*/
#define LOSCFG_PLATFORM_HWI                                 1
#define LOSCFG_USE_SYSTEM_DEFINED_INTERRUPT                 1
#define LOSCFG_PLATFORM_HWI_LIMIT                           128
/*=============================================================================
                                       Task module configuration
=============================================================================*/
#define LOSCFG_BASE_CORE_TSK_LIMIT                          24
#define LOSCFG_BASE_CORE_TSK_IDLE_STACK_SIZE                (0x500U)
#define LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE             (0x2D0U)
#define LOSCFG_BASE_CORE_TSK_MIN_STACK_SIZE                 (0x130U)
#define LOSCFG_BASE_CORE_TIMESLICE                          1
#define LOSCFG_BASE_CORE_TIMESLICE_TIMEOUT                  20000
/*=============================================================================
                                       Semaphore module configuration
=============================================================================*/
#define LOSCFG_BASE_IPC_SEM                                 1
#define LOSCFG_BASE_IPC_SEM_LIMIT                           48
/*=============================================================================
                                       Mutex module configuration
=============================================================================*/
#define LOSCFG_BASE_IPC_MUX                                 1
#define LOSCFG_BASE_IPC_MUX_LIMIT                           24
/*=============================================================================
                                       Queue module configuration
=============================================================================*/
#define LOSCFG_BASE_IPC_QUEUE                               1
#define LOSCFG_BASE_IPC_QUEUE_LIMIT                         24
/*=============================================================================
                                       Software timer module configuration
=============================================================================*/
#define LOSCFG_BASE_CORE_SWTMR                              1
#define LOSCFG_BASE_CORE_SWTMR_ALIGN                        0
#define LOSCFG_BASE_CORE_SWTMR_LIMIT                        48
/*=============================================================================
                                       Memory module configuration
=============================================================================*/
#define LOSCFG_MEM_MUL_POOL                                 1
#define OS_SYS_MEM_NUM                                      20
/*=============================================================================
                                       Exception module configuration
=============================================================================*/
#define LOSCFG_PLATFORM_EXC                                 1
/* =============================================================================
                                       printf module configuration
============================================================================= */
#define LOSCFG_KERNEL_PRINTF                                1

#define LOSCFG_BASE_CORE_SCHED_SLEEP                        1

#define LOSCFG_SYS_HEAP_SIZE                                0x4000UL

#ifdef __cplusplus
#if __cplusplus
}
#endif /* __cplusplus */
#endif /* __cplusplus */

#endif /* _TARGET_CONFIG_H */

这个文件主要是关于liteos_m内核的一些配置,后面可以根据程序需求进行修改。


接下来是添加build.sh文件,具体内容如下

#!/bin/bash

set -e

OUT_DIR="$1"
TOOLCHAIN_DIR="$2"


function main(){
    ROOT_DIR=$(cd $(dirname "$0");pwd)
    if [ -z "${TOOLCHAIN_DIR}" ]; then
        make clean &&  make -j16 OUT_DIR_PATH=${OUT_DIR}
    else
        make clean &&  make -j16 OUT_DIR_PATH=${OUT_DIR} TOOLCHAIN_DIR_PATH=${TOOLCHAIN_DIR}
    fi
}

main "$@"

紧接着我们需要对我们生成的makefile文件进行一点点简单的修改,具体修改为如下:

##########################################################################################################################
# File automatically-generated by tool: [projectgenerator] version: [3.13.0-B3] date: [Fri Apr 30 08:31:16 CST 2021] 
##########################################################################################################################

# ------------------------------------------------
# Generic Makefile (based on gcc)
#
# ChangeLog :
#	2017-02-10 - Several enhancements + project update mode
#   2015-07-22 - first version
# ------------------------------------------------

######################################
# target
######################################
TARGET = stm32f411core_ninjia

#######################################
# paths
#######################################
# Build path
BUILD_DIR = $(OUT_DIR_PATH)

#######################################
# toolchain paths
#######################################
# Toolchain path
ifneq ($(TOOLCHAIN_DIR_PATH), )
GCC_PATH = $(TOOLCHAIN_DIR_PATH)
endif

#######################################
# binaries
#######################################
PREFIX = arm-none-eabi-
# The gcc compiler bin path can be either defined in make command via GCC_PATH variable (> make GCC_PATH=xxx)
# either it can be added to the PATH environment variable.
ifneq ($(GCC_PATH), )
CC = $(GCC_PATH)/$(PREFIX)gcc
AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp
CP = $(GCC_PATH)/$(PREFIX)objcopy
SZ = $(GCC_PATH)/$(PREFIX)size
else
CC = $(PREFIX)gcc
AS = $(PREFIX)gcc -x assembler-with-cpp
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size
endif
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
 
#######################################
# CFLAGS
#######################################
# cpu
CPU = -mcpu=cortex-m4

# fpu
FPU = -mfpu=fpv4-sp-d16

# float-abi
FLOAT-ABI = -mfloat-abi=hard

# mcu
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)


#######################################
# LDFLAGS
#######################################
# link script
LDSCRIPT = STM32F411RCTx_FLASH.ld

# libraries
STATIC_LIB = -larch -lbacktrace -lcmsis -lcore -lcpup -lexchook -lkernel \
             -lsec_static -lpm -lstartup_stm32f411xe -lSTM32F4xx_HAL_Driver -lutils \
             -lhdf_core -lhdf_osal_lite -lhdf_platform_lite
STATIC_LIB_DIR = -L$(BUILD_DIR)/libs
LIBS = -lc -lm -lnosys
LIBDIR =
LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections

# default action: build all
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin


#######################################
# build the application
#######################################
$(BUILD_DIR)/$(TARGET).elf: Makefile
	$(CC) $(STATIC_LIB_DIR) -Wl,--whole-archive -Wl,--start-group $(STATIC_LIB) -Wl,--end-group -Wl,--no-whole-archive $(LDFLAGS) -o $@
	$(SZ) $@

$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(HEX) $< $@
	
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(BIN) $< $@	
	
$(BUILD_DIR):
	mkdir -p $@

#######################################
# clean up
#######################################
clean:
	-rm -fR $(BUILD_DIR)

# *** EOF ***

这一段内容建议直接复制,然后覆盖掉原来的makefile手动一点一点修改很容易出错,然后将makefile文件的这几处修改为你自己工程文件夹下的ld文件、s文件的文件名以及Device文件夹下的一个下级文件夹的名字。

LDSCRIPT = STM32F411RCTx_FLASH.ld
STATIC_LIB = -larch -lbacktrace -lcmsis -lcore -lcpup -lexchook -lkernel \
             -lsec_static -lpm -lstartup_stm32f411xe -lSTM32F4xx_HAL_Driver -lutils \
             -lhdf_core -lhdf_osal_lite -lhdf_platform_lite

对,这里就是中间的那一行-lstartup_stm32f411xe这个地方也需要修改成你自己的s文件的文件名​​​​​​​,-lSTM32F4xx_HAL_Driver这里要改成你自己的Device文件夹下的下级文件夹的名字。


还需要在工程目录下创建一个叫做liteos_m的文件夹,在这个文件夹下新建一个叫config.gni的文件,然后添加以下内容:

# Copyright (c) 2021 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Kernel type, e.g. "linux", "liteos_a", "liteos_m".
kernel_type = "liteos_m"

# Kernel version.
kernel_version = "3.0.0"

# Board CPU type, e.g. "cortex-a7", "riscv32".
board_cpu = "cortex-m4"

# Board arch, e.g.  "armv7-a", "rv32imac".
board_arch = ""

# Toolchain name used for system compiling.
# E.g. gcc-arm-none-eabi, arm-linux-harmonyeabi-gcc, ohos-clang,  riscv32-unknown-elf.
# Note: The default toolchain is "ohos-clang". It's not mandatory if you use the default toochain.
board_toolchain = "arm-none-eabi-gcc"

# The toolchain path instatlled, it's not mandatory if you have added toolchian path to your ~/.bashrc.
board_toolchain_path = ""

# Compiler prefix.
board_toolchain_prefix = "arm-none-eabi-"

# Compiler type, "gcc" or "clang".
board_toolchain_type = "gcc"

# Board related common compile flags.
board_cflags = [
  "-mcpu=cortex-m4",
  "-mfpu=fpv4-sp-d16",
  "-mfloat-abi=hard",
  "-mthumb",
  "-Og",
  "-fdata-sections",
  "-ffunction-sections",
  "-DUSE_HAL_DRIVER",
  "-DSTM32F411xE",
]
board_cxx_flags = board_cflags

board_ld_flags = []

# Board related headfiles search path.
board_include_dirs = [
  "//kernel/liteos_m/kernel/arch/arm/cortex-m4/gcc",
  "//device/st/stm32f411core",
  "//device/st/stm32f411core/Core/Inc",
  "//device/st/stm32f411core/Drivers/CMSIS/Include",
  "//device/st/stm32f411core/Drivers/CMSIS/Device/ST/STM32F4xx/Include",
  "//device/st/stm32f411core/Drivers/STM32F4xx_HAL_Driver/Inc",
  "//drivers/framework/support/platform/include/common",
  "//kernel/liteos_m/kal/cmsis",
  "//kernel/liteos_m/kernel/include",
  "//kernel/liteos_m/utils",
]

# Board adapter dir for OHOS components.
board_adapter_dir = ""

# Sysroot path.
board_configed_sysroot = ""

# Board storage type, it used for file system generation.
storage_type = ""

这里就是一些编译的描述了,其中也是有一些地方需要视情况修改的,具体如下:

board_cflags = [
  "-mcpu=cortex-m4",
  "-mfpu=fpv4-sp-d16",
  "-mfloat-abi=hard",
  "-mthumb",
  "-Og",
  "-fdata-sections",
  "-ffunction-sections",
  "-DUSE_HAL_DRIVER",
  "-DSTM32F411xE",
]
board_include_dirs = [
  "//kernel/liteos_m/kernel/arch/arm/cortex-m4/gcc",
  "//device/st/stm32f411core",
  "//device/st/stm32f411core/Core/Inc",
  "//device/st/stm32f411core/Drivers/CMSIS/Include",
  "//device/st/stm32f411core/Drivers/CMSIS/Device/ST/STM32F4xx/Include",
  "//device/st/stm32f411core/Drivers/STM32F4xx_HAL_Driver/Inc",
  "//drivers/framework/support/platform/include/common",
  "//kernel/liteos_m/kal/cmsis",
  "//kernel/liteos_m/kernel/include",
  "//kernel/liteos_m/utils",
]

board_cflags的最后两行和board_include_dirs里的内容,board_include_dirs里的内容主要是需要把“stm32f411core”改成你自己的工程文件名以及其中的STM32F4XX改成你自己的cube生成的文件名。


接下来就是编写工程目录下的BUILD.gn文件,内容如下:

# Copyright (c) 2021 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build/lite/config/component/lite_component.gni")

group("stm32f411core") {
}

build_ext_component("stm32f411core_ninjia") {
  exec_path = rebase_path(".", root_build_dir)
  outdir = rebase_path("$root_out_dir")
  print("$board_toolchain")

  if (board_toolchain_path != "") {
    toolchain_path = rebase_path("$board_toolchain_path")
    command = "./build.sh ${outdir} ${toolchain_path}"
  } else {
    command = "./build.sh ${outdir}"
  }
  deps = [ "//build/lite:ohos" ]
}

static_library("startup_stm32f411xe") {
  sources = [ "startup_stm32f411xe.s" ]

  include_dirs = [ "." ]

  deps = [
    "//drivers/adapter/khdf/liteos_m:hdf_lite",
    "//kernel/liteos_m:kernel",
    "//device/st/stm32f411core/Core:core",
    "//device/st/stm32f411core/Drivers/STM32F4xx_HAL_Driver:STM32F4xx_HAL_Driver",
  ]
}

 这个文件中,也有一处需要修改,最好改成现在的工程目录名。

group("stm32f411core") {
}

接下来就是添加Drivers\STM32F4xx_HAL_Driver下的BUILD.gn文件,内容如下:

# Copyright (c) 2021 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

static_library("STM32F4xx_HAL_Driver") {
  sources = [
    "Src/stm32f4xx_hal_tim.c",
    "Src/stm32f4xx_hal_tim_ex.c",
    "Src/stm32f4xx_hal_rcc.c",
    "Src/stm32f4xx_hal_rcc_ex.c",
    "Src/stm32f4xx_hal_flash.c",
    "Src/stm32f4xx_hal_flash_ex.c",
    "Src/stm32f4xx_hal_flash_ramfunc.c",
    "Src/stm32f4xx_hal_gpio.c",
    "Src/stm32f4xx_hal_dma_ex.c",
    "Src/stm32f4xx_hal_dma.c",
    "Src/stm32f4xx_hal_pwr.c",
    "Src/stm32f4xx_hal_pwr_ex.c",
    "Src/stm32f4xx_hal_cortex.c",
    "Src/stm32f4xx_hal.c",
    "Src/stm32f4xx_hal_exti.c",
    "Src/stm32f4xx_hal_uart.c",
    "Src/stm32f4xx_hal_usart.c",
  ]

  include_dirs = [
    "Inc",
    "Inc/Legacy",
    "../CMSIS/Include",
    "../CMSIS/Device/ST/STM32F4xx/Include",
    "//kernel/liteos_m/kal/cmsis",
    "../../Core/Inc",
  ]

}

这里的话相信接触过stm32开发的小伙伴就比较熟悉了,没错这个文件就是在添加hal库的编译规则,同样的里面的include_dirs和sources都是需要看情况修改的。如果和我用相同的芯片可以不用修改。


接下来添加Core下的BUILD.gn内容如下:

# Copyright (c) 2021 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

static_library("core") {
  sources = [
    "Src/main.c",
    "Src/stm32f4xx_hal_msp.c",
    "Src/stm32f4xx_it.c",
    "Src/system_stm32f4xx.c",  
  ]

  include_dirs = [
    "Inc",
    "../",
    "../Drivers/STM32F4xx_HAL_Driver/Inc",
    "../Drivers/CMSIS/Include",
    "../Drivers/CMSIS/Device/ST/STM32F4xx/Include",
    "//kernel/liteos_m/kernel/include",
    "//kernel/liteos_m/utils",
    "//kernel/liteos_m/kernel/arch/include",
    "//kernel/liteos_m/kal/cmsis"
  ]
}

这个就是我们自己的逻辑代码的编译描述,也需要做相应的修改。


到此已经完成了大部分工作

这时候我们需要新建一个多级目录结构如下:device/st/stm32f411core,然后我们需要把我们之前工程中的文件全部移入该目录,这里要注意是移动工程文件下的全部文件不是移动工程文件夹


然后我们退后到device文件夹所在的这一级目录,并新建一个多级目录vendor/st/stm32f411core然后进入这个目录的最内层

在该级目录下新建config.json文件并添加如下内容:

{
    "product_name": "stm32f411core",
    "ohos_version": "OpenHarmony 1.0",
    "device_company": "st",
    "board": "stm32f411core",
    "kernel_type": "liteos_m",
    "kernel_version": "3.0.2",
    "subsystems": [
      {
        "subsystem": "kernel",
        "components": [
          { "component": "liteos_m",
            "features":[
              "enable_ohos_kernel_liteos_m_fs = false",
              "enable_ohos_kernel_liteos_m_kal = true"
            ]
          }
        ]
      }
    ],
    "vendor_adapter_dir": "//device/st/stm32f411core",
    "third_party_dir": "//third_party",
    "product_adapter_dir": "",
    "ohos_product_type":"",
    "ohos_manufacture":"",
    "ohos_brand":"",
    "ohos_market_name":"",
    "ohos_product_series":"",
    "ohos_product_model":"",
    "ohos_software_model":"",
    "ohos_hardware_model":"",
    "ohos_hardware_profile":"",
    "ohos_serial":"",
    "ohos_bootloader_version":"",
    "ohos_secure_patch_level":"",
    "ohos_abi_list":""
}

 


接下来是在该目录下新建BUILD.gn,然后添加如下内容:

# Copyright (c) 2021 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

group("stm32f411core") {
}

然后在这个目录下新建一个文件夹config,在config文件夹下新建一个hdf.hcs并写入如下内容:

//#include "../../../../device/hisilicon/hispark_taurus/sdk_liteos/config/uart/uart_config.hcs"

#include "device_info/device_info.hcs"

root {
    module = "st,stm32_chip";
}

最后看起来是这样的

 到此第二步就结束了

第三步:尝试编译源码

前提条件:配置好鸿蒙的环境和stm32的编译工具链

首先将我们的代码device目录下的内容移动到鸿蒙3.0.2的device下,vendor目录下的内容移动到鸿蒙3.0.2的vendor下,如下图:

然后在ubuntu的命令行输入hb set,首次需要先指定源码目录,指定后再次输入hb set如下图

会发现已经成功识别到我们建的项目接着我们输入hb build -f开始全量编译 

结果不出意料的报错了。。。

看起来是把lwip给编进去了少了一些文件导致的报错,估计是移植过程中针对lwip需要做一些额外的配置,我们不需要lwip所以不编译。

 

这里我们动点小聪明,把源码里的drivers/adapter/khdf/liteos_m/BUILD.gn中的network屏蔽掉。记得以后在编译其他项目的时候加回来。

再次尝试编译

 可以看到最后成功编译,到这已经基本可以认为是成功了,可以先小小的高兴一下。

但是因为我们main.c里面没有做任何事情所以说并不能验证真的成功了,需要给他简单写一点测试程序。

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2022 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stdio.h"
#include "cmsis_os2.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);

static void *LedTask(const char *arg);
static void LedExampleEntry(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int _write(int fd, char *ch, int len)
{
  HAL_UART_Transmit(&huart1, (uint8_t*)ch, len, 0xFFFF);
  return len;
}
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */
  
  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */
  
  osKernelInitialize();
  LedExampleEntry();
  osKernelStart();
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 4;
  RCC_OscInitStruct.PLL.PLLN = 96;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief USART1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_USART1_UART_Init(void)
{

  /* USER CODE BEGIN USART1_Init 0 */

  /* USER CODE END USART1_Init 0 */

  /* USER CODE BEGIN USART1_Init 1 */

  /* USER CODE END USART1_Init 1 */
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 115200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART1_Init 2 */

  /* USER CODE END USART1_Init 2 */

}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_RESET);

  /*Configure GPIO pin : PC0 */
  GPIO_InitStruct.Pin = GPIO_PIN_0;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */
static void *LedTask(const char *arg)
{
    (void)arg;
    char tx_buf[] = "Hello World\n";
    while(1)
    {
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_RESET);
        osDelay(500);
        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET);
        osDelay(500);

        printf("???\r\n");
        HAL_UART_Transmit(&huart1, (unsigned char *)tx_buf, 11, 10);
    }
    return NULL;
}

static void LedExampleEntry(void)
{
    osThreadAttr_t attr;
    
    MX_GPIO_Init();

    attr.name = "LedTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 1024;
    attr.priority = 13;

    if (osThreadNew((osThreadFunc_t)LedTask, NULL, &attr) == NULL) {
        printf("[LedExample] Falied to create LedTask!\n");
    }
}
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

对main函数做以下修改,然后重新编译,然后在out目录可以找到我们的编译输出

 这时候我们将bin文件烧录到开发板并开启串口监视器

重启开发板可以看到如我们所料的打印hello world和?并且开发板上的led灯开始闪烁。

到这里我们的移植就完成了,应该还是相对比较简单的。

移植部分参考了连志安老师的帖子​​​​​​​,这里贴上链接

 

 

 

...全文
8513 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
WebDB 2022-03-29
  • 打赏
  • 举报
回复

hb set 时会出现
[OHOS ERROR] cannot find liteos_m_3.0.2 in /home/openharmony/device/st/stm32f411core。:(

WebDB 2022-03-31
  • 举报
回复
@WebDB 找到原因了。在liteos_m目录中的config.gni文件中,第18行: kernel_version = "3.0.0" 改成 kernel_version = "" 就没问题了。
weixin_45305668 2022-03-26
  • 打赏
  • 举报
回复 1
收藏学习一下,很不错

539

社区成员

发帖
与我相关
我的任务
社区描述
OpenHarmony开发者社区
其他 企业社区
社区管理员
  • csdnsqst0025
  • shewaliujingli
  • BaoWei
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧