From 712107eeebe29c5fbd4bceb7a8f8c23011a98e7f Mon Sep 17 00:00:00 2001 From: LiteOS2021 Date: Wed, 26 Jan 2022 18:05:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20cksy=E5=92=8Cesp32=20=E7=9A=84printf?= =?UTF-8?q?=E4=BF=AE=E6=94=B9,=20readme=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cksy和esp32 的printf修改, readme更新 Signed-off-by: LiteOS2021 --- SmartL_E802/README.md | 50 +++++--- SmartL_E802/README_zh.md | 50 +++++--- SmartL_E802/liteos_m/board/libc/dprintf.c | 137 ++++------------------ esp32/README.md | 46 +++++--- esp32/README_zh.md | 46 +++++--- esp32/liteos_m/board/libc/dprintf.c | 131 +++------------------ 6 files changed, 162 insertions(+), 298 deletions(-) diff --git a/SmartL_E802/README.md b/SmartL_E802/README.md index 9f57868..87ea388 100644 --- a/SmartL_E802/README.md +++ b/SmartL_E802/README.md @@ -13,7 +13,7 @@ QEMU can simulate the kernel to run on different boards, freeing the dependence To use the default environment, execute '3.Get source code' and then install the default compiler in the root directory by executing the following instructions. ```shell - $ sh build/prebuilts_download.sh + sh build/prebuilts_download.sh ``` Optional compiler installation process: @@ -21,22 +21,36 @@ QEMU can simulate the kernel to run on different boards, freeing the dependence a) Create the `csky_toolchain` folder and enter it ```shell - $ mkdir csky_toolchain && cd csky_toolchain + mkdir csky_toolchain && cd csky_toolchain ``` b) Download csky-elfabiv2-tools-x86_64-minilibc-20210423.tar.gz to `csky_toolchain`folder and unzip,download link: https://occ.t-head.cn/community/download?id=3885366095506644992 ```shell - $ wget https://occ-oss-prod.oss-cn-hangzhou.aliyuncs.com/resource/1356021/1619529111421/csky-elfabiv2-tools-x86_64-minilibc-20210423.tar.gz - $ tar -xf csky-elfabiv2-tools-x86_64-minilibc-20210423.tar.gz + wget https://occ-oss-prod.oss-cn-hangzhou.aliyuncs.com/resource/1356021/1619529111421/csky-elfabiv2-tools-x86_64-minilibc-20210423.tar.gz + tar -xf csky-elfabiv2-tools-x86_64-minilibc-20210423.tar.gz ``` c) Add the csky-elfabiv2 compilation tool chain to the environment variable (modify user_toolchain_xxx_path to your own installation path): ```shell - $ vim ~/.bashrc - $ export PATH=$PATH:user_toolchain_xxx_path/csky_toolchain/bin - $ source ~/.bashrc + vim ~/.bashrc + export PATH=$PATH:user_toolchain_xxx_path/csky_toolchain/bin + source ~/.bashrc + ``` + + d) Delete the default compiler path: + + change SmartL_E802\liteos_m\config.gni: + + ```c + board_toolchain_path = "$ohos_root_path/prebuilts/gcc/linux-x86/csky/csky/bin" + ``` + + to + + ```c + board_toolchain_path = "" ``` 2. Install qemu @@ -44,28 +58,28 @@ QEMU can simulate the kernel to run on different boards, freeing the dependence a) create `csky_qemu` folder and enter it ```shell - $ mkdir csky_qemu && cd csky_qemu + mkdir csky_qemu && cd csky_qemu ``` b) download csky-qemu-x86_64-Ubuntu-16.04-20210202-1445.tar.gz to `csky_qemu`folder and unzip,download link: https://occ.t-head.cn/community/download?id=636946310057951232 ```shell - $ wget https://occ-oss-prod.oss-cn-hangzhou.aliyuncs.com/resource/1356021/1612269502091/csky-qemu-x86_64-Ubuntu-16.04-20210202-1445.tar.gz - $ tar -xf csky-qemu-x86_64-Ubuntu-16.04-20210202-1445.tar.gz + wget https://occ-oss-prod.oss-cn-hangzhou.aliyuncs.com/resource/1356021/1612269502091/csky-qemu-x86_64-Ubuntu-16.04-20210202-1445.tar.gz + tar -xf csky-qemu-x86_64-Ubuntu-16.04-20210202-1445.tar.gz ``` c) Add qemu to the environment variable (modify user_qemu_xxx_path to your own installation path): ```shell - $ vim ~/.bashrc + vim ~/.bashrc export PATH=$PATH:user_qemu_xxx_path/csky-qemu/bin - $ source ~/.bashrc + source ~/.bashrc ``` d) Installation dependencies ```shell - $ ldd qemu_installation_path/bin/qemu-system-cskyv2 + ldd qemu_installation_path/bin/qemu-system-cskyv2 ``` According to the execution result of ldd, install the missing dependent libraries @@ -85,8 +99,8 @@ Hint : You can use the `repo` command to get the source code. 2. Execute the hb clean && hb build command to build the executable file that produces `OHOS_Image`. ```shell - $ hb set - $ hb clean && hb build + hb set + hb clean && hb build ``` 3. After the buildding is complete,the corresponding executable file is in the home directory: @@ -100,7 +114,7 @@ Hint : You can use the `repo` command to get the source code. 1. Run qemu(Don't cooperate with GDB) ```shell - $ ./qemu-run + ./qemu-run ``` 2. Run qemu(Cooperate with GDB) @@ -108,13 +122,13 @@ Hint : You can use the `repo` command to get the source code. a) Start the GDB server and wait for the connection ```shell - $ ./qemu-run -g + ./qemu-run -g ``` b) Create a new terminal and use GDB to connect to qemu ```shell - $ csky-abiv2-elf-gdb out/SmartL_E802/qemu_csky_mini_system_demo/OHOS_Image -ex "target remote localhost:1234" + csky-abiv2-elf-gdb out/SmartL_E802/qemu_csky_mini_system_demo/OHOS_Image -ex "target remote localhost:1234" ``` Annotation: The way to exit qemu : press ctrl and a,then release and press x. diff --git a/SmartL_E802/README_zh.md b/SmartL_E802/README_zh.md index 1de60a4..6c83129 100644 --- a/SmartL_E802/README_zh.md +++ b/SmartL_E802/README_zh.md @@ -13,7 +13,7 @@ QEMU可以模拟内核运行在不同的单板,解除对物理开发板的依 若要使用默认环境请先执行 '3.获取源码' ,然后在根目录下执行下列指令安装默认编译器。 ```shell - $ sh build/prebuilts_download.sh + sh build/prebuilts_download.sh ``` 可选的编译器安装流程: @@ -21,22 +21,36 @@ QEMU可以模拟内核运行在不同的单板,解除对物理开发板的依 a) 创建`csky_toolchain`文件夹并进入 ```shell - $ mkdir csky_toolchain && cd csky_toolchain + mkdir csky_toolchain && cd csky_toolchain ``` b) 下载csky-elfabiv2-tools-x86_64-minilibc-20210423.tar.gz 到`csky_toolchain`文件夹并解压,下载地址:https://occ.t-head.cn/community/download?id=3885366095506644992 ```shell - $ wget https://occ-oss-prod.oss-cn-hangzhou.aliyuncs.com/resource/1356021/1619529111421/csky-elfabiv2-tools-x86_64-minilibc-20210423.tar.gz - $ tar -xf csky-elfabiv2-tools-x86_64-minilibc-20210423.tar.gz + wget https://occ-oss-prod.oss-cn-hangzhou.aliyuncs.com/resource/1356021/1619529111421/csky-elfabiv2-tools-x86_64-minilibc-20210423.tar.gz + tar -xf csky-elfabiv2-tools-x86_64-minilibc-20210423.tar.gz ``` c) 将csky-elfabiv2编译工具链加入环境变量(将user_toolchain_xxx_path修改为自己的安装路径): ```shell - $ vim ~/.bashrc - $ export PATH=$PATH:user_toolchain_xxx_path/csky_toolchain/bin - $ source ~/.bashrc + vim ~/.bashrc + export PATH=$PATH:user_toolchain_xxx_path/csky_toolchain/bin + source ~/.bashrc + ``` + + d) 删除默认的编译器路径: + + 修改SmartL_E802\liteos_m\config.gni: + + ```c + board_toolchain_path = "$ohos_root_path/prebuilts/gcc/linux-x86/csky/csky/bin" + ``` + + 改为 + + ```c + board_toolchain_path = "" ``` 2. qemu安装 @@ -44,28 +58,28 @@ QEMU可以模拟内核运行在不同的单板,解除对物理开发板的依 a) 创建`csky_qemu`文件夹并进入 ```shell - $ mkdir csky_qemu && cd csky_qemu + mkdir csky_qemu && cd csky_qemu ``` b) 下载csky-qemu-x86_64-Ubuntu-16.04-20210202-1445.tar.gz到`csky_qemu`文件夹下并解压,下载地址:https://occ.t-head.cn/community/download?id=636946310057951232 ```shell - $ wget https://occ-oss-prod.oss-cn-hangzhou.aliyuncs.com/resource/1356021/1612269502091/csky-qemu-x86_64-Ubuntu-16.04-20210202-1445.tar.gz - $ tar -xf csky-qemu-x86_64-Ubuntu-16.04-20210202-1445.tar.gz + wget https://occ-oss-prod.oss-cn-hangzhou.aliyuncs.com/resource/1356021/1612269502091/csky-qemu-x86_64-Ubuntu-16.04-20210202-1445.tar.gz + tar -xf csky-qemu-x86_64-Ubuntu-16.04-20210202-1445.tar.gz ``` c) 将qemu加入环境变量(将user_qemu_xxx_path修改为自己的安装路径): ```shell - $ vim ~/.bashrc + vim ~/.bashrc export PATH=$PATH:user_qemu_xxx_path/csky-qemu/bin - $ source ~/.bashrc + source ~/.bashrc ``` d) 安装依赖 ```shell - $ ldd qemu_installation_path/bin/qemu-system-cskyv2 + ldd qemu_installation_path/bin/qemu-system-cskyv2 ``` 根据ldd执行结果,安装缺少的依赖库 @@ -85,8 +99,8 @@ QEMU可以模拟内核运行在不同的单板,解除对物理开发板的依 2. 执行hb clean && hb build命令构建产生 `OHOS_Image` 的可执行文件。 ```shell - $ hb set - $ hb clean && hb build + hb set + hb clean && hb build ``` 3. 在构建完成之后,对应的可执行文件在主目录下: @@ -100,7 +114,7 @@ QEMU可以模拟内核运行在不同的单板,解除对物理开发板的依 1. 启动qemu(不配合GDB) ```shell - $ ./qemu-run + ./qemu-run ``` 2. 启动qemu(配合GDB) @@ -108,13 +122,13 @@ QEMU可以模拟内核运行在不同的单板,解除对物理开发板的依 a) 启动GDB服务器,等待连接 ```shell - $ ./qemu-run -g + ./qemu-run -g ``` b) 新建终端并使用GDB连接qemu ```shell - $ csky-abiv2-elf-gdb out/SmartL_E802/qemu_csky_mini_system_demo/OHOS_Image -ex "target remote localhost:1234" + csky-abiv2-elf-gdb out/SmartL_E802/qemu_csky_mini_system_demo/OHOS_Image -ex "target remote localhost:1234" ``` 注:qemu退出方式为:按下ctrl加a键,然后松开再按下x键。 diff --git a/SmartL_E802/liteos_m/board/libc/dprintf.c b/SmartL_E802/liteos_m/board/libc/dprintf.c index e870f93..3d60914 100644 --- a/SmartL_E802/liteos_m/board/libc/dprintf.c +++ b/SmartL_E802/liteos_m/board/libc/dprintf.c @@ -31,17 +31,15 @@ #include "dprintf.h" #include -#include "stdio.h" +#include +#include #include "pin.h" #include "drv_gpio.h" #include "drv_usart.h" -#include #include "los_compiler.h" +#include "los_interrupt.h" -#define PLACEHOLDER_OFFSET 2 -#define HEXADECIMAL_NUM 16 -#define DECIMALISM_NUM 10 -#define BOUNDARY_NUM 9 +#define BUFSIZE 256 #define UART_BAUDRATE 115200 usart_handle_t g_consoleHandle; @@ -61,123 +59,30 @@ STATIC VOID UartPutc(CHAR c) (VOID)csi_usart_putchar(g_consoleHandle, c); } -STATIC VOID MatchedOut(CHAR data, va_list *ap) +STATIC VOID FuncPuts(const CHAR *s, VOID (*funcPutc)(CHAR c)) { - CHAR c = 0; - UINT32 v, n; - UINT32 val[100]; - CHAR *s = NULL; - INT32 i = 0; - INT32 j = 0; + UINT32 intSave; - switch (data) { - case 'c': - c = va_arg(*ap, UINT32); - UartPutc(c); - break; - case 's': - s = va_arg(*ap, CHAR*); - while (*s) { - UartPutc(*s++); - } - break; - case 'd': - v = va_arg(*ap, UINT32); - i = 0; - while (v) { - n = v % DECIMALISM_NUM; - val[i] = n; - v = v / DECIMALISM_NUM; - i++; - } - if (i == 0) { - UartPutc('0'); - } - for (j = i - 1; j >= 0; j--) { - UartPutc('0' + val[j]); - } - break; - case 'x': - v = va_arg(*ap, UINT32); - i = 0; - if (v == 0) { - val[i] = 0; - i++; - } - while (v) { - n = v % HEXADECIMAL_NUM; - val[i] = n; - v = v / HEXADECIMAL_NUM; - i++; - } - if (i == 0) { - UartPutc('0'); - } - for (j = i - 1; j >= 0; j--) { - if (val[j] > BOUNDARY_NUM) { - UartPutc('a' + val[j] - DECIMALISM_NUM); - } else { - UartPutc('0' + val[j]); - } - } - break; - case 'p': - v = va_arg(*ap, UINT32); - i = 0; - if (v == 0) { - val[i] = 0; - i++; - } - while (v) { - n = v % HEXADECIMAL_NUM; - val[i] = n; - v = v / HEXADECIMAL_NUM; - i++; - } - UartPutc('0'); - UartPutc('x'); - if (i == 0) { - UartPutc('0'); - } - for (j = i - 1; j >= 0; j--) { - if (val[j] > BOUNDARY_NUM) { - UartPutc('a' + val[j] - DECIMALISM_NUM); - } else { - UartPutc('0' + val[j]); - } - } - break; - default: - break; + intSave = LOS_IntLock(); + while (*s) { + funcPutc(*s++); } + LOS_IntRestore(intSave); } INT32 printf(const CHAR *fmt, ...) { + CHAR buf[BUFSIZE] = { 0 }; va_list ap; - (VOID)va_start(ap, fmt); - while (*fmt != '\0') { - switch (*fmt) { - case '%': - MatchedOut(fmt[1], &ap); - fmt += PLACEHOLDER_OFFSET; - break; - case '\n': - UartPutc('\r'); - UartPutc('\n'); - fmt++; - break; - case '\r': - UartPutc('\r'); - UartPutc('\n'); - fmt++; - break; - default: - UartPutc(*fmt); - fmt++; - break; - } + va_start(ap, fmt); + int len = vsnprintf_s(buf, sizeof(buf), BUFSIZE - 1, fmt, ap); + va_end(ap); + if (len > 0) { + FuncPuts(buf, UartPutc); + } else { + FuncPuts("printf error!\n", UartPutc); } - (VOID)va_end(ap); - return 0; + + return len; } + diff --git a/esp32/README.md b/esp32/README.md index 928cba6..863b4d7 100644 --- a/esp32/README.md +++ b/esp32/README.md @@ -19,14 +19,14 @@ QEMU can simulate the kernel to run on different boards ,eliminate dependence on To use the default environment, execute '3.Get source code' and then install the default compiler in the root directory by executing the following instructions. ```shell - $ sh build/prebuilts_download.sh + sh build/prebuilts_download.sh ``` Optional compiler installation process: - 1. Download the esp official release the SDK package: https://www.espressif.com/zh-hans/support/download/sdks-demos?keys=&field_type_tid%5B%5D=13 + a) Download the esp official release the SDK package: https://www.espressif.com/zh-hans/support/download/sdks-demos?keys=&field_type_tid%5B%5D=13 - 2. Put the downloaded SDK package into the Linux system, go to the directory, and run the following commands: + b) Put the downloaded SDK package into the Linux system, go to the directory, and run the following commands: ```shell unzip esp-idf-v4.3.1.zip @@ -35,6 +35,20 @@ QEMU can simulate the kernel to run on different boards ,eliminate dependence on . ./export.sh ``` + c) Delete the default compiler path: + + change esp32\liteos_m\config.gni: + + ```c + board_toolchain_path = "$ohos_root_path/prebuilts/gcc/linux-x86/esp/esp-2019r2-8.2.0/xtensa-esp32-elf/bin/" + ``` + + to + + ```c + board_toolchain_path = "" + ``` + Annotation: The version of the toolchain used in the test is GCC Version 8.2.0 (Crosstool-ng ESP-2019R2) or GCC Version 8.4.0 (Crosstool-ng ESP-2021R1). 2. Install esptool @@ -53,9 +67,9 @@ QEMU can simulate the kernel to run on different boards ,eliminate dependence on a) Install and compile ```shell - $ git clone https://github.com/espressif/qemu.git - $ cd qemu - $ ./configure --target-list=xtensa-softmmu \ + git clone https://github.com/espressif/qemu.git + cd qemu + ./configure --target-list=xtensa-softmmu \ --enable-gcrypt \ --enable-debug --enable-sanitizers \ --disable-strip --disable-user \ @@ -66,21 +80,21 @@ QEMU can simulate the kernel to run on different boards ,eliminate dependence on b) Waitting for the completion of the compilation and executing the installation command (If the compilation fail, please refer to https://github.com/espressif/qemu/issues/21): ```shell - $ ninja -C build + ninja -C build ``` c) Add qemu to the environment variable (modify user_qemu_xxx_path to your own installation path): ```shell - $ vim ~/.bashrc - $ export QEMU=user_qemu_xxx_path/qemu/build - $ source ~/.bashrc + vim ~/.bashrc + export QEMU=user_qemu_xxx_path/qemu/build + source ~/.bashrc ``` d) Installation dependencies ```shell - $ ldd $QEMU/qemu-system-xtensa + ldd $QEMU/qemu-system-xtensa ``` According to the execution result of ldd, install the missing dependent libraries @@ -100,8 +114,8 @@ Hint : You can use the `repo` command to get the source code. 2. Execute the hb clean && hb build command to build the executable file that produces `OHOS_Image`. ```shell - $ hb set - $ hb clean && hb build + hb set + hb clean && hb build ``` 3. After the buildding is complete, the corresponding executable file is in the home directory: @@ -115,7 +129,7 @@ Hint : You can use the `repo` command to get the source code. 1. Run qemu(Don't cooperate with GDB ) ```shell - $ ./qemu-run + ./qemu-run ``` 2. Run qemu(Cooperate with GDB) @@ -123,13 +137,13 @@ Hint : You can use the `repo` command to get the source code. a) Start the GDB server and wait for the connection ```shell - $ ./qemu-run -g + ./qemu-run -g ``` b) Create a new terminal and use GDB to connect to qemu ```shell - $ xtensa-esp32-elf-gdb out/esp32/qemu_xtensa_mini_system_demo/OHOS_Image -ex "target remote :1234" + xtensa-esp32-elf-gdb out/esp32/qemu_xtensa_mini_system_demo/OHOS_Image -ex "target remote :1234" ``` Annotation:Since the qemu-system-xtensa tool of qemu has the same name as the qemu-system-xtensa tool of esp32, the absolute path is used to execute the qemu-system-xtensa tool of esp32. diff --git a/esp32/README_zh.md b/esp32/README_zh.md index 82ecda9..7879768 100644 --- a/esp32/README_zh.md +++ b/esp32/README_zh.md @@ -19,14 +19,14 @@ QEMU可以模拟内核运行在不同的单板,解除对物理开发板的依 若要使用默认环境请先执行 '3.获取源码' ,然后在根目录下执行下列指令安装默认编译器。 ```shell - $ sh build/prebuilts_download.sh + sh build/prebuilts_download.sh ``` 可选的编译器安装流程: - 1.下载官方release的SDK包:https://www.espressif.com/zh-hans/support/download/sdks-demos?keys=&field_type_tid%5B%5D=13 + a) 下载官方release的SDK包:https://www.espressif.com/zh-hans/support/download/sdks-demos?keys=&field_type_tid%5B%5D=13 - 2.将下载好的SDK包放入linux系统,进入目录执行如下指令: + b) 将下载好的SDK包放入linux系统,进入目录执行如下指令: ```shell unzip esp-idf-v4.3.1.zip @@ -35,6 +35,20 @@ QEMU可以模拟内核运行在不同的单板,解除对物理开发板的依 . ./export.sh ``` + c) 删除默认的编译器路径: + + 修改esp32\liteos_m\config.gni: + + ```c + board_toolchain_path = "$ohos_root_path/prebuilts/gcc/linux-x86/esp/esp-2019r2-8.2.0/xtensa-esp32-elf/bin/" + ``` + + 改为 + + ```c + board_toolchain_path = "" + ``` + 注:本教程使用的工具链版本为gcc version 8.2.0 (crosstool-NG esp-2019r2)或gcc version 8.4.0 (crosstool-NG esp-2021r1) 2. esptool安装 @@ -53,9 +67,9 @@ QEMU可以模拟内核运行在不同的单板,解除对物理开发板的依 a) 编译安装 ```shell - $ git clone https://github.com/espressif/qemu.git - $ cd qemu - $ ./configure --target-list=xtensa-softmmu \ + git clone https://github.com/espressif/qemu.git + cd qemu + ./configure --target-list=xtensa-softmmu \ --enable-gcrypt \ --enable-debug --enable-sanitizers \ --disable-strip --disable-user \ @@ -66,21 +80,21 @@ QEMU可以模拟内核运行在不同的单板,解除对物理开发板的依 b) 等待编译结束,执行安装命令(如果编译失败请参考https://github.com/espressif/qemu/issues/21): ```shell - $ ninja -C build + ninja -C build ``` c) 将qemu添加到环境变量中(user_qemu_xxx_path修改为自己的安装路径): ```shell - $ vim ~/.bashrc - $ export QEMU=user_qemu_xxx_path/qemu/build - $ source ~/.bashrc + vim ~/.bashrc + export QEMU=user_qemu_xxx_path/qemu/build + source ~/.bashrc ``` d) 安装依赖 ```shell - $ ldd $QEMU/qemu-system-xtensa + ldd $QEMU/qemu-system-xtensa ``` 根据ldd执行结果,安装缺少的依赖库 @@ -100,8 +114,8 @@ QEMU可以模拟内核运行在不同的单板,解除对物理开发板的依 2. 执行hb clean && hb build命令构建产生 `OHOS_Image` 的可执行文件。 ```shell - $ hb set - $ hb clean && hb build + hb set + hb clean && hb build ``` 3. 在构建完成之后,对应的可执行文件在主目录下: @@ -115,7 +129,7 @@ QEMU可以模拟内核运行在不同的单板,解除对物理开发板的依 1. 运行qemu(不配合GDB) ```shell - $ ./qemu-run + ./qemu-run ``` 2. 启动qemu(配合GDB) @@ -123,13 +137,13 @@ QEMU可以模拟内核运行在不同的单板,解除对物理开发板的依 a) 启动GDB服务器,等待连接 ```shell - $ ./qemu-run -g + ./qemu-run -g ``` b) 新建终端并使用GDB连接qemu ```shell - $ xtensa-esp32-elf-gdb out/esp32/qemu_xtensa_mini_system_demo/OHOS_Image -ex "target remote :1234" + xtensa-esp32-elf-gdb out/esp32/qemu_xtensa_mini_system_demo/OHOS_Image -ex "target remote :1234" ``` 注:由于默认安装的qemu自带qemu-system-xtensa工具与当前安装的qemu-system-xtensa工具重名,因此采用绝对路径执行当前的qemu-system-xtensa工具。 diff --git a/esp32/liteos_m/board/libc/dprintf.c b/esp32/liteos_m/board/libc/dprintf.c index 4e0d157..27f90e4 100644 --- a/esp32/liteos_m/board/libc/dprintf.c +++ b/esp32/liteos_m/board/libc/dprintf.c @@ -33,133 +33,36 @@ #include #include "los_interrupt.h" -#define PLACEHOLDER_OFFSET 2 -#define HEXADECIMAL_NUM 16 -#define DECIMALISM_NUM 10 -#define BOUNDARY_NUM 9 +#define BUFSIZE 256 STATIC VOID UartPutc(CHAR c) { (VOID)uart_tx_one_char(c); } -STATIC VOID MatchedOut(CHAR data, va_list *ap) +STATIC VOID FuncPuts(const CHAR *s, VOID (*funcPutc)(CHAR c)) { - CHAR c = 0; - UINT32 v, n; - UINT32 val[100]; - CHAR *s = NULL; - INT32 i = 0; - INT32 j = 0; + UINT32 intSave; - switch (data) { - case 'c': - c = va_arg(*ap, UINT32); - UartPutc(c); - break; - case 's': - s = va_arg(*ap, CHAR*); - while (*s) { - UartPutc(*s++); - } - break; - case 'd': - v = va_arg(*ap, UINT32); - i = 0; - while (v) { - n = v % DECIMALISM_NUM; - val[i] = n; - v = v / DECIMALISM_NUM; - i++; - } - if (i == 0) { - UartPutc('0'); - } - for (j = i - 1; j >= 0; j--) { - UartPutc('0' + val[j]); - } - break; - case 'x': - v = va_arg(*ap, UINT32); - i = 0; - if (v == 0) { - val[i] = 0; - i++; - } - while (v) { - n = v % HEXADECIMAL_NUM; - val[i] = n; - v = v / HEXADECIMAL_NUM; - i++; - } - if (i == 0) { - UartPutc('0'); - } - for (j = i - 1; j >= 0; j--) { - if (val[j] > BOUNDARY_NUM) { - UartPutc('a' + val[j] - DECIMALISM_NUM); - } else { - UartPutc('0' + val[j]); - } - } - break; - case 'p': - v = va_arg(*ap, UINT32); - i = 0; - if (v == 0) { - val[i] = 0; - i++; - } - while (v) { - n = v % HEXADECIMAL_NUM; - val[i] = n; - v = v / HEXADECIMAL_NUM; - i++; - } - UartPutc('0'); - UartPutc('x'); - if (i == 0) { - UartPutc('0'); - } - for (j = i - 1; j >= 0; j--) { - if (val[j] > BOUNDARY_NUM) { - UartPutc('a' + val[j] - DECIMALISM_NUM); - } else { - UartPutc('0' + val[j]); - } - } - break; - default: - break; + intSave = LOS_IntLock(); + while (*s) { + funcPutc(*s++); } + LOS_IntRestore(intSave); } INT32 printf(const CHAR *fmt, ...) { + CHAR buf[BUFSIZE] = { 0 }; va_list ap; - (VOID)va_start(ap, fmt); - while (*fmt != '\0') { - switch (*fmt) { - case '%': - MatchedOut(fmt[1], &ap); - fmt += PLACEHOLDER_OFFSET; - break; - case '\n': - UartPutc('\r'); - UartPutc('\n'); - fmt++; - break; - case '\r': - UartPutc('\r'); - UartPutc('\n'); - fmt++; - break; - default: - UartPutc(*fmt); - fmt++; - break; - } + va_start(ap, fmt); + int len = vsnprintf_s(buf, sizeof(buf), BUFSIZE - 1, fmt, ap); + va_end(ap); + if (len > 0) { + FuncPuts(buf, UartPutc); + } else { + FuncPuts("printf error!\n", UartPutc); } - (VOID)va_end(ap); - return 0; + return len; } + -- Gitee