From c9bc189f08482662e7a4c2e646a5521a233b178d Mon Sep 17 00:00:00 2001 From: Dima Horbenko Date: Wed, 17 May 2023 20:35:55 +0300 Subject: [PATCH 1/3] Add uart demo Signed-off-by: Dima Horbenko --- led_demo/kernel_configs/debug.config | 1 + uart_demo/BUILD.gn | 28 +++++ uart_demo/config.json | 53 +++++++++ uart_demo/hals/utils/sys_param/BUILD.gn | 27 +++++ .../hals/utils/sys_param/hal_sys_param.c | 26 +++++ uart_demo/hals/utils/sys_param/vendor.para | 40 +++++++ uart_demo/hals/utils/token/BUILD.gn | 24 ++++ uart_demo/hals/utils/token/hal_token.c | 108 ++++++++++++++++++ uart_demo/hdf_config/BUILD.gn | 25 ++++ uart_demo/hdf_config/hdf.hcs | 1 + uart_demo/kernel_configs/debug.config | 7 ++ uart_demo/ohos.build | 10 ++ uart_demo/uart_demo/BUILD.gn | 48 ++++++++ uart_demo/uart_demo/app.c | 93 +++++++++++++++ 14 files changed, 491 insertions(+) create mode 100755 uart_demo/BUILD.gn create mode 100755 uart_demo/config.json create mode 100755 uart_demo/hals/utils/sys_param/BUILD.gn create mode 100644 uart_demo/hals/utils/sys_param/hal_sys_param.c create mode 100644 uart_demo/hals/utils/sys_param/vendor.para create mode 100755 uart_demo/hals/utils/token/BUILD.gn create mode 100755 uart_demo/hals/utils/token/hal_token.c create mode 100755 uart_demo/hdf_config/BUILD.gn create mode 100755 uart_demo/hdf_config/hdf.hcs create mode 100755 uart_demo/kernel_configs/debug.config create mode 100755 uart_demo/ohos.build create mode 100755 uart_demo/uart_demo/BUILD.gn create mode 100755 uart_demo/uart_demo/app.c diff --git a/led_demo/kernel_configs/debug.config b/led_demo/kernel_configs/debug.config index 99a780a..0a0dc0d 100755 --- a/led_demo/kernel_configs/debug.config +++ b/led_demo/kernel_configs/debug.config @@ -4,3 +4,4 @@ LOSCFG_FS_LITTLEFS=y LOSCFG_DRIVERS_HDF=y LOSCFG_DRIVERS_HDF_PLATFORM=y LOSCFG_DRIVERS_HDF_PLATFORM_GPIO=y +LOSCFG_DRIVERS_HDF_PLATFORM_UART=y diff --git a/uart_demo/BUILD.gn b/uart_demo/BUILD.gn new file mode 100755 index 0000000..41f25f5 --- /dev/null +++ b/uart_demo/BUILD.gn @@ -0,0 +1,28 @@ +# Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK") +# 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("//device/soc/telink/util/util.gni") + +b91_firmware("uart_demo") { + explicit_libs = [ + "bootstrap", + "broadcast", + ] + + deps = [ + "uart_demo", + "//build/lite:ohos", + ] +} diff --git a/uart_demo/config.json b/uart_demo/config.json new file mode 100755 index 0000000..2a3aae7 --- /dev/null +++ b/uart_demo/config.json @@ -0,0 +1,53 @@ +{ + "product_name": "uart_demo", + "ohos_version": "OpenHarmony 3.x", + "device_company": "telink", + "board": "b91_devkit", + "kernel_type": "liteos_m", + "kernel_version": "3.0.0", + "subsystems": [ + { + "subsystem": "kernel", + "components": [ + { "component": "liteos_m", "features": [] } + ] + }, + { + "subsystem": "startup", + "components": [ + { "component": "bootstrap_lite", "features":[] }, + { + "component": "init_lite", + "features": [ + "enable_ohos_startup_init_feature_begetctl_liteos = true", + "enable_ohos_startup_init_lite_use_posix_file_api = true", + "config_ohos_startup_init_lite_data_path = \"/data/\"", + "config_ohos_startup_syspara_lite_data_path = \"/data/\"" + ] + } + ] + }, + { + "subsystem": "systemabilitymgr", + "components": [ + { "component": "samgr_lite", "features":[] } + ] + }, + { + "subsystem": "hiviewdfx", + "components": [ + { "component": "hilog_lite", "features":[] }, + { "component": "hievent_lite", "features":[] } + ] + }, + { + "subsystem": "commonlibrary", + "components": [ + { "component": "file", "features":[] } + ] + } + ], + "third_party_dir": "", + "vendor_adapter_dir": "//device/soc/telink/b91/adapter", + "product_adapter_dir": "//vendor/telink/led_demo/hals" +} diff --git a/uart_demo/hals/utils/sys_param/BUILD.gn b/uart_demo/hals/utils/sys_param/BUILD.gn new file mode 100755 index 0000000..858bfc5 --- /dev/null +++ b/uart_demo/hals/utils/sys_param/BUILD.gn @@ -0,0 +1,27 @@ +# Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK") +# 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. + +static_library("hal_sysparam") { + sources = [ "hal_sys_param.c" ] + include_dirs = [ "//base/startup/init/interfaces/hals" ] + defines = [ + "INCREMENTAL_VERSION=\"${ohos_version}\"", + "BUILD_TYPE=\"${ohos_build_type}\"", + "BUILD_USER=\"${ohos_build_user}\"", + "BUILD_TIME=\"${ohos_build_time}\"", + "BUILD_HOST=\"${ohos_build_host}\"", + "BUILD_ROOTHASH=\"${ohos_build_roothash}\"", + ] +} diff --git a/uart_demo/hals/utils/sys_param/hal_sys_param.c b/uart_demo/hals/utils/sys_param/hal_sys_param.c new file mode 100644 index 0000000..e63ab53 --- /dev/null +++ b/uart_demo/hals/utils/sys_param/hal_sys_param.c @@ -0,0 +1,26 @@ +/****************************************************************************** + * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK") + * 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 "hal_sys_param.h" + +static const char OHOS_SERIAL[] = {"1234567890"}; // if not provid serial algorithm,Serial will use this default value. + +const char *HalGetSerial(void) +{ + return OHOS_SERIAL; +} \ No newline at end of file diff --git a/uart_demo/hals/utils/sys_param/vendor.para b/uart_demo/hals/utils/sys_param/vendor.para new file mode 100644 index 0000000..0e2ecb2 --- /dev/null +++ b/uart_demo/hals/utils/sys_param/vendor.para @@ -0,0 +1,40 @@ +# Copyright (c) 2022 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. + +const.build.characteristics="Evaluation Board" + +const.product.manufacturer=Telink + +const.product.brand=Telink + +const.product.name="B91 Generic Starter Kit" + +const.build.product="TLSR9" + +const.product.model="v1.0.0" + +const.software.model="v1.0.0" + +const.product.hardwareversion="v1.3" + +const.product.hardwareprofile="BLE:true" + +const.ohos.serial=1234567890 + +const.product.bootloader.version=bootloader + +const.product.cpu.abilist=default + +const.product.software.version="OpenHarmony 3.x" + +const.product.firstapiversion=1 \ No newline at end of file diff --git a/uart_demo/hals/utils/token/BUILD.gn b/uart_demo/hals/utils/token/BUILD.gn new file mode 100755 index 0000000..04b5737 --- /dev/null +++ b/uart_demo/hals/utils/token/BUILD.gn @@ -0,0 +1,24 @@ +# Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK") +# 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. + +static_library("hal_token_static") { + sources = [ "hal_token.c" ] + + include_dirs = [ + "//base/startup/init/interfaces/hals", + "//commonlibrary/utils_lite/include", + ] + deps = [] +} diff --git a/uart_demo/hals/utils/token/hal_token.c b/uart_demo/hals/utils/token/hal_token.c new file mode 100755 index 0000000..b41867e --- /dev/null +++ b/uart_demo/hals/utils/token/hal_token.c @@ -0,0 +1,108 @@ +/****************************************************************************** + * Copyright (c) 2020 Huawei Device 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. + * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK") + * + *****************************************************************************/ + +#include "hal_token.h" +#include "ohos_errno.h" +#include "ohos_types.h" + +static int OEMReadToken(char *token, unsigned int len) +{ + // OEM need add here, read token from device + (void)(token); + (void)(len); + return EC_SUCCESS; +} + +static int OEMWriteToken(const char *token, unsigned int len) +{ + // OEM need add here, write token to device + (void)(token); + (void)(len); + return EC_SUCCESS; +} + +static int OEMGetAcKey(char *acKey, unsigned int len) +{ + // OEM need add here, get AcKey + (void)(acKey); + (void)(len); + return EC_SUCCESS; +} + +static int OEMGetProdId(char *productId, unsigned int len) +{ + // OEM need add here, get ProdId + (void)(productId); + (void)(len); + return EC_SUCCESS; +} + +static int OEMGetProdKey(char *productKey, unsigned int len) +{ + // OEM need add here, get ProdKey + (void)(productKey); + (void)(len); + return EC_SUCCESS; +} + + +int HalReadToken(char *token, unsigned int len) +{ + if (token == NULL) { + return EC_FAILURE; + } + + return OEMReadToken(token, len); +} + +int HalWriteToken(const char *token, unsigned int len) +{ + if (token == NULL) { + return EC_FAILURE; + } + + return OEMWriteToken(token, len); +} + +int HalGetAcKey(char *acKey, unsigned int len) +{ + if (acKey == NULL) { + return EC_FAILURE; + } + + return OEMGetAcKey(acKey, len); +} + +int HalGetProdId(char *productId, unsigned int len) +{ + if (productId == NULL) { + return EC_FAILURE; + } + + return OEMGetProdId(productId, len); +} + +int HalGetProdKey(char *productKey, unsigned int len) +{ + if (productKey == NULL) { + return EC_FAILURE; + } + + return OEMGetProdKey(productKey, len); +} diff --git a/uart_demo/hdf_config/BUILD.gn b/uart_demo/hdf_config/BUILD.gn new file mode 100755 index 0000000..7def747 --- /dev/null +++ b/uart_demo/hdf_config/BUILD.gn @@ -0,0 +1,25 @@ +# Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK") +# 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("//drivers/adapter/khdf/liteos_m/hdf.gni") + +module_name = "hdf_hcs" +hdf_driver(module_name) { + hcs_sources = [ "hdf.hcs" ] + visibility += [ + "$device_path", + ".", + ] +} diff --git a/uart_demo/hdf_config/hdf.hcs b/uart_demo/hdf_config/hdf.hcs new file mode 100755 index 0000000..0f7b438 --- /dev/null +++ b/uart_demo/hdf_config/hdf.hcs @@ -0,0 +1 @@ +#include "../../../../device/soc/telink/b91/hcs/hdf.hcs" diff --git a/uart_demo/kernel_configs/debug.config b/uart_demo/kernel_configs/debug.config new file mode 100755 index 0000000..0a0dc0d --- /dev/null +++ b/uart_demo/kernel_configs/debug.config @@ -0,0 +1,7 @@ +LOSCFG_PLATFORM_QEMU_RISCV32_VIRT=y +LOSCFG_SOC_SERIES_B91=y +LOSCFG_FS_LITTLEFS=y +LOSCFG_DRIVERS_HDF=y +LOSCFG_DRIVERS_HDF_PLATFORM=y +LOSCFG_DRIVERS_HDF_PLATFORM_GPIO=y +LOSCFG_DRIVERS_HDF_PLATFORM_UART=y diff --git a/uart_demo/ohos.build b/uart_demo/ohos.build new file mode 100755 index 0000000..f9d39cd --- /dev/null +++ b/uart_demo/ohos.build @@ -0,0 +1,10 @@ +{ + "parts": { + "product_uart_demo": { + "module_list": [ + "//vendor/telink/uart_demo:uart_demo" + ] + } + }, + "subsystem": "product_uart_demo" +} diff --git a/uart_demo/uart_demo/BUILD.gn b/uart_demo/uart_demo/BUILD.gn new file mode 100755 index 0000000..8c3e2b6 --- /dev/null +++ b/uart_demo/uart_demo/BUILD.gn @@ -0,0 +1,48 @@ +# Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK") +# 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. + +declare_args() { + telink_gpio_irq_sample_enable = false +} + +config("myapp_config") { + include_dirs = [ "//commonlibrary/utils_lite/include" ] + + configs = [ "//device/soc/telink/b91:B91_config" ] +} + +source_set("myapp_inner") { + sources = [ "app.c" ] + + deps = [ "//base/hiviewdfx/hiview_lite" ] + + if (!defined(defines)) { + defines = [] + } + + if (telink_gpio_irq_sample_enable) { + defines += [ "TELINK_GPIO_IRQ_SAMPLE_ENABLE=1" ] + } else { + defines += [ "TELINK_GPIO_IRQ_SAMPLE_ENABLE=0" ] + } + + configs += [ ":myapp_config" ] +} + +static_library("uart_demo") { + deps = [ ":myapp_inner" ] + + configs += [ ":myapp_config" ] +} diff --git a/uart_demo/uart_demo/app.c b/uart_demo/uart_demo/app.c new file mode 100755 index 0000000..225ff41 --- /dev/null +++ b/uart_demo/uart_demo/app.c @@ -0,0 +1,93 @@ +/****************************************************************************** + * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK") + * 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 +#include +#include +#include + +#include +#include + +#include +#include + +#include +#include + +#include + + +#define UART_TASK_PRIORITY LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO +#define DELAY 2000 +#define UART0 0 +#define UART1 1 + + + +// STATIC VOID HelloWorldTask(VOID) +// { +// while (1) { +// time_t t = time(NULL); +// printf("Hello World, time: %lld\r\n", (long long)t); +// LOS_TaskDelay(DELAY); +// } +// } + +// STATIC VOID LedTask(VOID) +// { +// GpioSetDir(LED_BLUE_HDF, GPIO_DIR_OUT); + +// while (1) { +// GpioWrite(LED_BLUE_HDF, GPIO_VAL_HIGH); +// LOS_Msleep(DELAY); + +// GpioWrite(LED_BLUE_HDF, GPIO_VAL_LOW); +// LOS_Msleep(DELAY); +// } +// } + + +STATIC VOID uart_task(VOID) +{ + LOS_TaskDelay(DELAY); + DevHandle *uart1 = UartOpen(UART1); + +} + + +void AppMain(void) +{ + UINT32 ret; + + UINT32 taskId = 0; + + TSK_INIT_PARAM_S taskParam = {0}; + taskParam.pfnTaskEntry = (TSK_ENTRY_FUNC)uart_task; + taskParam.uwArg = 0; + taskParam.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE; + taskParam.pcName = "uart_task"; + taskParam.usTaskPrio = UART_TASK_PRIORITY; + + ret = LOS_TaskCreate(&taskId, &taskParam); + if (ret != LOS_OK) { + HILOG_ERROR(HILOG_MODULE_APP, "ret of LOS_TaskCreate(uart_task) = %#x", ret); + } +} + +SYS_RUN(AppMain); From eb6cba0d2329301695bc15cbc86551b53f35b8be Mon Sep 17 00:00:00 2001 From: Dima Horbenko Date: Wed, 24 May 2023 16:12:09 +0300 Subject: [PATCH 2/3] Driver is functional Signed-off-by: Dima Horbenko --- uart_demo/uart_demo/app.c | 52 +++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/uart_demo/uart_demo/app.c b/uart_demo/uart_demo/app.c index 225ff41..a17a06c 100755 --- a/uart_demo/uart_demo/app.c +++ b/uart_demo/uart_demo/app.c @@ -39,35 +39,39 @@ #define UART1 1 - -// STATIC VOID HelloWorldTask(VOID) -// { -// while (1) { -// time_t t = time(NULL); -// printf("Hello World, time: %lld\r\n", (long long)t); -// LOS_TaskDelay(DELAY); -// } -// } - -// STATIC VOID LedTask(VOID) -// { -// GpioSetDir(LED_BLUE_HDF, GPIO_DIR_OUT); - -// while (1) { -// GpioWrite(LED_BLUE_HDF, GPIO_VAL_HIGH); -// LOS_Msleep(DELAY); - -// GpioWrite(LED_BLUE_HDF, GPIO_VAL_LOW); -// LOS_Msleep(DELAY); -// } -// } +uint8_t txbuf[80] = "\nHDF uart test string 1\n"; -STATIC VOID uart_task(VOID) +STATIC VOID uart_test_task(VOID) { LOS_TaskDelay(DELAY); + + /* See device/soc/telink/b91/hcs/uart/uart_config.hcs + for hdf uart configuration */ DevHandle *uart1 = UartOpen(UART1); + if (uart1 == NULL) { + printf("UartOpen %u: failed!\n", UART1); + } + + uint32_t baud; + UartGetBaud(uart1, &baud); + printf("\n%s: Get baudrate %u\r\n", __func__, baud); + + baud = 115200; + UartSetBaud(uart1, baud); + UartGetBaud(uart1, &baud); + printf("%s: Set baudrate %u\r\n", __func__, baud); + + baud = 921600; + UartSetBaud(uart1, baud); + UartGetBaud(uart1, &baud); + printf("%s: Set baudrate %u\r\n", __func__, baud); + + uint32_t ret = UartWrite(uart1, txbuf, strlen((char *)txbuf)); + if (ret != 0) { + printf("UartWrite %u: failed!\n", UART1); + } } @@ -78,7 +82,7 @@ void AppMain(void) UINT32 taskId = 0; TSK_INIT_PARAM_S taskParam = {0}; - taskParam.pfnTaskEntry = (TSK_ENTRY_FUNC)uart_task; + taskParam.pfnTaskEntry = (TSK_ENTRY_FUNC)uart_test_task; taskParam.uwArg = 0; taskParam.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE; taskParam.pcName = "uart_task"; From d13dfcd79b6e23bb0aa6daa97ad29721c55d6a2f Mon Sep 17 00:00:00 2001 From: Dima Horbenko Date: Thu, 25 May 2023 12:04:42 +0300 Subject: [PATCH 3/3] Fix code style Signed-off-by: Dima Horbenko --- uart_demo/uart_demo/app.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/uart_demo/uart_demo/app.c b/uart_demo/uart_demo/app.c index a17a06c..33b9f5f 100755 --- a/uart_demo/uart_demo/app.c +++ b/uart_demo/uart_demo/app.c @@ -37,6 +37,8 @@ #define DELAY 2000 #define UART0 0 #define UART1 1 +#define BAUD_115200 115200 +#define BAUD_921600 921600 uint8_t txbuf[80] = "\nHDF uart test string 1\n"; @@ -58,12 +60,12 @@ STATIC VOID uart_test_task(VOID) UartGetBaud(uart1, &baud); printf("\n%s: Get baudrate %u\r\n", __func__, baud); - baud = 115200; + baud = BAUD_115200; UartSetBaud(uart1, baud); UartGetBaud(uart1, &baud); printf("%s: Set baudrate %u\r\n", __func__, baud); - baud = 921600; + baud = BAUD_921600; UartSetBaud(uart1, baud); UartGetBaud(uart1, &baud); printf("%s: Set baudrate %u\r\n", __func__, baud);