本文介绍下如何创建hello world。 遇到的问题是虽然按照官方教程创建好了,但是运行结果没用出来。 重点在于gn文件的配置。
一、 参考 二、创建步骤 1 创建一个目录存放code 【命令】cd vendor/lockzhiner/rk2206/samples/ 【命令】mkdir z1_hello_world
2 直接在这个路径下vendor/lockzhiner/rk2206/samples/z1_hello_world下创建两个文件 vendorlockzhinerrk2206samplesz1_hello_world hello_world.c #定义module_func tion,例如task_example2() BUILD.gn #配置module_name,加载hello_world.c文件 hello_world.c内容
- #include "los_task.h" // OpenHARMony LiteOS的任务管理头文件
- /***************************************************************
- * 函数名称: task_helloworld
- * 说 明: 线程函数helloworld
- * 参 数: 无
- * 返 回 值: 无
- ***************************************************************/
- void task_helloworld()
- {
- while (1)
- {
- printf("Hello World: this is youkai's examplen");
- /* 睡眠1秒。该函数为OpenHarmony LiteoS内核睡眠函数,单位为:毫秒 */
- LOS_Msleep(1000);
- }
- }
- /***************************************************************
- * 函数名称: task_openharmony
- * 说 明: 线程函数
- * 参 数: 无
- * 返 回 值: 无
- ***************************************************************/
- void task_openharmony()
- {
- while (1)
- {
- printf("Hello OpenHarmonyn");
- /* 睡眠1秒。该函数为OpenHarmony内核睡眠函数,单位为:毫秒 */
- LOS_Msleep(2000);
- }
- }
- /***************************************************************
- * 函数名称: task_example
- * 说 明: 内核任务创建例程
- * 参 数: 无
- * 返 回 值: 无
- ***************************************************************/
- void task_example2()
- {
- /* 任务id */
- unsigned int thread_id1;
- unsigned int thread_id2;
- /* 任务参数 */
- TSK_INIT_PARAM_S task1 = {0};
- TSK_INIT_PARAM_S task2 = {0};
- /* 返回值 */
- unsigned int ret = LOS_OK;
- /* 创建HelloWorld任务 */
- task1.pfnTaskEntry = (TSK_ENTRY_FUNC)task_helloworld; // 运行函数入口
- task1.uwStackSize = 2048; // 堆栈大小
- task1.pcName = "task_helloworld"; // 函数注册名称
- task1.usTaskPrio = 24; // 任务的优先级,从0~63
- ret = LOS_TaskCreate(&thread_id1, &task1); // 创建任务
- if (ret != LOS_OK)
- {
- printf("Falied to create task_helloworld ret:0x%xn", ret);
- return;
- }
- task2.pfnTaskEntry = (TSK_ENTRY_FUNC)task_openharmony; // 运行函数入口
- task2.uwStackSize = 2048; // 堆栈大小
- task2.pcName = "task_openharmony"; // 函数注册名称
- task2.usTaskPrio = 25; // 任务的优先级,从0~63
- ret = LOS_TaskCreate(&thread_id2, &task2); // 创建任务
- if (ret != LOS_OK)
- {
- printf("Falied to create task_openharmony ret:0x%xn", ret);
- return;
- }
- }
复制代码
BUILD.gn
- static_library("task_helloworld2") {
- sources = [
- "hello_world.c",
- ]
- include_dirs = [
- "//utils/native/lite/include",
- ]
- }
复制代码
注意static_library是c文件主函数的名字。
以上两个文件是我们自己创建的。
3 更改前一级的gn文件
vendorlockzhinerrk2206samples
BUILD.gn # 在deps中添加,格式为"path:module_name",例如"./z1_hello_world:task_helloworld2"
- # Copyright (c) 2022 FuZhou Lockzhiner Electronic Co., Ltd. All rights reserved.
- # 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")
- lite_component("app") {
- features = [
- # 内核类
- #"./a1_kernal_task:task_example",
- #"./a2_kernel_semaphore:semaphore_example",
- #"./a3_kernel_timer:timer_example",
- #"./a4_kernel_mutex:mutex_example",
- #"./a5_kernel_queue:queue_example",
- #"./a6_kernel_event:event_example",
- # 基础外设类
- #"./b1_adc:adc_example",
- #"./b2_NFC:nfc_example",
- #"./b3_eeprom:eeprom_example",
- #"./b4_lcd:lcd_example",
- #"./b5_oled:oled_example",
- #"./b6_uart:uart_example",
- #"./b7_wifi_tcp:wifi_tcp_example",
-
- # E53模块
- #"./c1_e53_intelligent_agriculture:e53_ia_example",
- #"./c2_e53_smart_covers:e53_sc_example",
- #"./c3_e53_intelligent_street_lamp:e53_isl_example",
- #"./c4_e53_intelligent_vehicle_01:e53_iv01_example",
- #"./c5_e53_body_induction:e53_bi_example",
- #"./c6_e53_gesture_sensor:e53_gs_example",
- #"./c7_e53_intelligent_smoke_sensor:e53_iss_example",
- #"./c8_e53_temperature_measurement:e53_tm_example",
- #"./c9_e53_heart_rate_detection:e53_hrd_example",
- # Iot云应用
- #"./d2_iot_cloud_smart_covers:iot_cloud_sc_example",
- #"./d3_iot_cloud_intelligent_street_lamp:iot_cloud_isl_example",
- #"./d4_iot_cloud_intelligent_vehicle:iot_cloud_iv_example",
- #"./d5_iot_cloud_body_induction:iot_cloud_bi_example",
- #"./d6_iot_cloud_gesture_sensor:iot_cloud_gs_example",
- #"./d7_iot_cloud_intelligent_agriculture:iot_cloud_ia_example",
- ]
- deps = [
- "./z1_hello_world:task_helloworld2"
- ]
- }
复制代码
注意在deps中添加刚刚的路径。 ./表示同级目录,z1_hello_world是创建的文件名,task_helloworld2就是输入函数。
PS:这里我们只需要添加一句就行。
4 makefile中添加模块信息
devicerockchiprk2206sdk_liteos
Makefile # hardware_LIBS中追加'-l'+module_name,例如-ltask_helloworld2
- # Copyright (c) 2022 FuZhou Lockzhiner Electronic Co., Ltd. All rights reserved.
- # 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.
- TARGET = liteos
- BUILD_DIR = $(OUTDIR)
- BOARD_DIR = $(shell pwd)
- PREFIX = arm-none-eabi-
- CC = $(PREFIX)gcc
- AS = $(PREFIX)gcc -x assembler-with-cpp
- CP = $(PREFIX)objcopy
- SZ = $(PREFIX)size
- 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=soft
- # mcu
- MCU = $(CPU) -mthumb #$(FPU) $(FLOAT-ABI)
- #######################################
- # LDFLAGS
- #######################################
- LDSCRIPT = board.ld
- boot_LIBS = -lbootstrap -lbroadcast
- hardware_LIBS = -lhal_iothardware -lhardware -ltask_helloworld2
- hdf_LIBS = -lhdf_config -lhdf_core -lhdf_osal_lite -lhdf_platform_lite
- #-lsample_driver -lgpio_driver -lpwm_driver -li2c_driver -ladc_driver -lspi_driver
- app_LIBS =
- xts_LIBS = -lmodule_ActsBootstrapTest -lmodule_ActsDfxFuncTest -lmodule_ActsHieventLiteTest -lhuks_test_common -lmodule_ActsHuksHalFunctionTest -lmodule_ActsKvStoreTest
- -lmodule_ActsLwipTest -lmodule_ActsParameterTest -lmodule_ActsSamgrTest -lmodule_ActsUpdaterFuncTest -lmodule_ActsUtilsFileTest
- -lmodule_ActsWifiIotTest -lmodule_ActsWifiServiceTest -lhctest -lmbedtls -lhal_update_static -lhota
- common_LIBS =
- -larch -lcjson_static -ldump_static -lhal_wifiaware -lhuks_3.0_sdk -lnative_file -lsec_static -lwifiaware
- -lauthmanager -lcmsis -lexchook -lkernel -lpm -lsysparam -lwifiservice
- -lbacktrace -lcppsupport -lhal_file_static -lhichainsdk -lposix -ltoken_static
- -lboard -lcpup -lhievent_lite -lmbedtls -lsamgr -ltrans_service
- -ldiscovery -lhal_sysparam -lhilog_lite -lmusl-c -lsamgr_adapter -lutils
- -llwip -lhal_token_static -lhiview_lite -lmusl-m -lsamgr_source -lutils_kv_store
- -lpahomqtt_static -llzlittlefs
- LIBS= -Wl,--start-group
- -Wl,--whole-archive $(boot_LIBS) $(hardware_LIBS) $(hdf_LIBS) $(app_LIBS) -Wl,--no-whole-archive
- $(common_LIBS)
- -Wl,--end-group
- LIB_DIR = -L$(BUILD_DIR)/libs
- LDFLAGS = $(MCU)
- --specs=nosys.specs
- -T$(LDSCRIPT)
- $(LIB_DIR)
- -Wl,--start-group $(LIBS) -Wl,--end-group
- -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref
- -Wl,--gc-sections
- all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
- $(BUILD_DIR)/$(TARGET).elf:
- $(CC) $(LDFLAGS) -o $@
- $(SZ) $@
- $(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf
- $(HEX) [ DISCUZ_CODE_3097 ]lt; $@
- $(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf
- $(BIN) [ DISCUZ_CODE_3097 ]lt; $@
- -include $(wildcard $(BUILD_DIR)/*.d)
复制代码
注意,这里著需要在hardware_LIBS后面追加模块信息,格式为-l+函数名
5 主函数调用
devicerockchiprk2206sdk_liteosboard
main.c #主函数,这里添加module_function的宏定义,以及调用module_function
- /*
- * Copyright (c) 2022 FuZhou Lockzhiner Electronic Co., Ltd. All rights reserved.
- * 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.
- */
- #include "los_tick.h"
- #include "los_task.h"
- #include "los_config.h"
- #include "los_interrupt.h"
- #include "los_debug.h"
- #include "los_compiler.h"
- #include "lz_hardware.h"
- #include "config_network.h"
- #define MAIN_TAG "MAIN"
- int DeviceManagerStart();
- void IotInit(void);
- void task_example2();
- /*****************************************************************************
- Function : main
- Description : Main function entry
- Input : None
- Output : None
- Return : None
- *****************************************************************************/
- LITE_OS_SEC_TEXT_INIT int Main(void)
- {
- int ret;
- LZ_HARDWARE_LOGD(MAIN_TAG, "%s: enter ...", __func__);
-
- HalInit();
- ret = LOS_KernelInit();
- if (ret == LOS_OK) {
- IotInit();
- task_example2();
- OHOS_SystemInit();
- ClkDevInit();
- /* 开启驱动管理服务 */
- //DeviceManagerStart();
- //ExternalTaskConfigNetwork();
- LZ_HARDWARE_LOGD(MAIN_TAG, "%s: LOS_Start ...", __func__);
- LOS_Start();
- }
- while (1) {
- __asm volatile("wfi");
- }
- }
复制代码
main.c函数中添加两个,一个是宏定义,一个是调用。
三、结果
至此,完成了hello_world的创建。
0
|
|
|
|