chore: migrate project into clean repository

This commit is contained in:
yuuux
2026-08-13 16:50:52 +08:00
commit d1d25a09e7
27405 changed files with 9422808 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 3.13)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
find_package(listenai-cmake REQUIRED HINTS $ENV{ARCS_BASE})
project(arcs)
listenai_add_executable(${PROJECT_NAME})
target_sources(${PROJECT_NAME} PRIVATE
src/main.cpp
src/io_stream_test.cpp
src/fs.c
)
listenai_include_directories(src)
listenai_link_libraries(lsfs)
listenai_link_libraries(disk)
listenai_link_libraries(elmfat)

View File

@@ -0,0 +1 @@
osource "$ARCS_BASE/Kconfig"

View File

@@ -0,0 +1,230 @@
C++ IO 流示例
==============
功能说明
--------
演示 C++ 标准库中的输入输出流(IO Stream)功能,包括控制台输入输出、文件流操作、字符串流以及格式化输出。本示例展示了如何使用 C++ 流式 API 进行各种 IO 操作。
硬件连接
--------
无需外部连接,IO 流为软件功能。如果使用文件流功能,需要确保文件系统已正确初始化。
示例内容
--------
1. 基本控制台输入输出(``std::cout``、``std::cerr``)
2. 文件输出流(``std::ofstream``)写入文件
3. 文件输入流(``std::ifstream``)读取文件
4. 文件异常处理
5. 字符串流(``std::ostringstream``、``std::istringstream``)
6. 格式化输出(精度、宽度、填充、进制等)
7. 自定义类型的流操作符重载
编译
--------
.. include:: /sample_build.rst
烧录固件
--------
.. include:: /sample_flash.rst
预期输出
--------
.. code-block:: text
======= Starting IO Stream Tests =======
=== IO Stream Tests ===
=== Basic Console I/O Example ===
这是标准输出流示例。
数字输出: 42
这是标准错误流示例。
嵌入式系统中一般通过其他方式如UART、文件或消息队列获取输入
=== File Output Stream Example ===
before outFile: test_file.txt
filename: test_file.txt
成功写入文件: test_file.txt
=== File Input Stream Example ===
文件 test_file.txt 内容:
这是第一行文本。
这是第二行文本。
数字: 12345
项目 1: 1
项目 2: 4
项目 3: 9
项目 4: 16
项目 5: 25
=== File Exception Handling Example ===
尝试打开不存在的文件: this_file_does_not_exist.txt
捕获到文件操作异常: ...
文件 'this_file_does_not_exist.txt' 可能不存在或无法访问
另一种异常处理方式 - 使用ifstream构造后立即检查状态:
捕获到运行时异常: 无法打开文件: this_file_does_not_exist.txt
=== String Stream Example ===
ostringstream的内容:
这是一个字符串流。
可以像使用其他ostream一样使用它:
整数: 42
浮点数: 3.14159
从istringstream解析的数值:
a = 12, b = 34, c = 56, d = 78
总和: 180
=== Formatting Output Example ===
默认精度的π: 3.14159
设置精度为10的π: 3.141592654
默认字段宽度:
42
设置字段宽度为10:
42
设置填充字符为'*'并左对齐:
42********
十进制: 255
十六进制: ff
八进制: 377
默认布尔值输出: 1, 0
字面布尔值输出: true, false
=== Custom Type Stream Example ===
点p1: (10, 20)
点集合:
(1, 2)
(3, 4)
(5, 6)
预设的点: (7, 8)
======= IO Stream Tests Completed =======
核心 API
--------
.. list-table::
:header-rows: 1
* - API
- 说明
* - ``std::cout``
- 标准输出流
* - ``std::cerr``
- 标准错误流
* - ``std::ofstream``
- 文件输出流,用于写入文件
* - ``std::ifstream``
- 文件输入流,用于读取文件
* - ``std::ostringstream``
- 输出字符串流
* - ``std::istringstream``
- 输入字符串流
* - ``std::setprecision()``
- 设置浮点数精度
* - ``std::setw()``
- 设置字段宽度
* - ``std::setfill()``
- 设置填充字符
* - ``operator<<``
- 输出操作符重载
* - ``operator>>``
- 输入操作符重载
关键代码
--------
.. code-block:: cpp
// 基本控制台输出
std::cout << "这是标准输出流示例。" << std::endl;
std::cout << "数字输出: " << 42 << std::endl;
std::cerr << "这是标准错误流示例。" << std::endl;
// 文件输出流(示例:函数参数为 filename)
std::ofstream outFile(filename);
if (!outFile) {
std::cerr << "无法打开文件进行写入: " << filename << std::endl;
return;
}
outFile << "这是第一行文本。" << std::endl;
outFile.close();
// 文件输入流(示例:函数参数为 filename)
std::ifstream inFile(filename);
if (!inFile) {
std::cerr << "无法打开文件进行读取: " << filename << std::endl;
return;
}
std::string line;
while (std::getline(inFile, line)) {
std::cout << " " << line << std::endl;
}
inFile.close();
// 字符串流
std::ostringstream oss;
oss << "整数: " << 42 << std::endl;
std::string result = oss.str();
std::istringstream iss("12 34 56 78");
int a, b, c, d;
iss >> a >> b >> c >> d;
// 格式化输出
std::cout << std::setprecision(10) << pi << std::endl;
std::cout << std::left << std::setfill('*') << std::setw(10) << 42 << std::endl;
std::cout << std::hex << value << std::endl;
// 自定义类型流操作符重载(简化示例)
class Point {
int x, y;
friend std::ostream& operator<<(std::ostream& os, const Point& p);
};
std::ostream& operator<<(std::ostream& os, const Point& p) {
os << "(" << p.x << ", " << p.y << ")";
return os;
}
格式化选项
----------
数值格式
~~~~~~~~
- **精度**: ``std::setprecision(n)`` - 设置浮点数精度
- **宽度**: ``std::setw(n)`` - 设置字段宽度
- **填充**: ``std::setfill(c)`` - 设置填充字符
- **对齐**: ``std::left``、``std::right`` - 设置对齐方式
进制格式
~~~~~~~~
- **十进制**: ``std::dec`` - 默认进制
- **十六进制**: ``std::hex`` - 输出十六进制
- **八进制**: ``std::oct`` - 输出八进制
布尔值格式
~~~~~~~~~~
- **数字格式**: 默认输出 ``1`` 或 ``0``
- **字面格式**: ``std::boolalpha`` - 输出 ``true`` 或 ``false``
注意事项
--------
1. **文件检查**: 打开文件后必须检查文件流状态(``if (!file)``),确保文件成功打开
2. **资源管理**: 文件流在析构时会自动关闭,但显式调用 ``close()`` 更清晰
3. **异常处理**: 可以使用 ``file.exceptions()`` 设置异常掩码,或手动检查文件状态
4. **字符串流**: ``ostringstream`` 和 ``istringstream`` 在内存中操作,不涉及文件系统
5. **格式化状态**: 某些格式化选项(如 ``std::hex``)会持续生效,需要手动重置(``std::dec``)
6. **操作符重载**: 自定义类型的流操作符重载应声明为友元函数,以便访问私有成员
7. **缓冲区**: ``std::endl`` 会刷新缓冲区,在性能敏感场景中可考虑使用 ``'\n'`` 代替
8. **文件系统**: 使用文件流前需要确保文件系统已正确初始化(如调用 ``test_lsfs_init()``)

View File

@@ -0,0 +1,177 @@
#!/bin/bash
set -e
usage() {
echo "使用方式: $0 [选项]"
echo "选项:"
echo " -S, --Source <path> 指定项目源码路径 (默认为当前脚本所在目录)"
echo " -t, --target <target> 指定构建目标 (如 menuconfig)"
echo " -C, --Clean 清理构建目录"
echo " -B, --build 构建输出目录"
echo " -h, --help 显示此帮助信息"
echo " -r, --release 以 Release 模式构建 (移除 DEBUG_PATH 信息)"
echo " -w, --warnings-as-errors 将警告视为错误"
echo " -D<var>=<value> 传递 CMake 变量 (可多次使用)"
echo ""
echo "示例:"
echo " $0 -S samples/helloworld -DBOARD=arcs_mini 指定板型构建"
echo " $0 -S samples/helloworld -DBOARD=arcs_evb 使用 EVB 板型"
echo " $0 -S samples/helloworld -t menuconfig -DBOARD=arcs_mini 运行 menuconfig"
echo " $0 -C -S samples/helloworld -DBOARD=arcs_mini 清理并重新构建"
echo " $0 -S samples/helloworld -DBOARD=my_board -DBOARD_SEARCH_PATH=/path/to/boards 使用自定义板型"
exit 1
}
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
PROJECT_PATH="$SCRIPT_DIR"
TARGET=""
CLEAN=false
OUTPUT="build"
WARNINGS_AS_ERRORS=false
RELEASE=false
ARCS_BASE_DIR_NAME="arcs-sdk"
ARCS_DEV_TOOLS_DIR_NAME="listenai-dev-tools"
ARCS_DEV_TOOL_TOOLCHAIN_DIR_NAME="gcc"
ARCS_DEV_TOOL_LISTENAI_TOOLS_DIR_NAME="listenai-tools"
find_arcs_base() {
local current_dir=$(cd "$(dirname "$0")" && pwd)
local dir_name="$ARCS_BASE_DIR_NAME"
while [ "$current_dir" != "/" ]; do
if [ -d "$current_dir/$dir_name" ]; then
echo "Found ARCS_BASE: $current_dir/$dir_name"
export ARCS_BASE="$current_dir/$dir_name"
return 0
fi
current_dir=$(dirname "$current_dir")
done
echo "ARCS_BASE not found, Please add ARCS_BASE environment variable or set ARCS_BASE_DIR_NAME to the correct directory."
echo "Current Target ARCS_BASE directory name: $ARCS_BASE_DIR_NAME."
exit 1
}
find_dev_tools() {
local current_dir=$(cd "$(dirname "$0")" && pwd)
local dir_name="$ARCS_DEV_TOOLS_DIR_NAME"
echo "trying to find $dir_name in parent directories..."
while [ "$current_dir" != "/" ]; do
if [ -d "$current_dir/$dir_name" ]; then
echo "Found $dir_name: $current_dir/$dir_name"
if [ -d "$current_dir/$dir_name/$ARCS_DEV_TOOL_LISTENAI_TOOLS_DIR_NAME" ]; then
echo "Found LISTENAI_TOOLS_PATH: $current_dir/$dir_name/$ARCS_DEV_TOOL_LISTENAI_TOOLS_DIR_NAME"
export LISTENAI_TOOLS_PATH="$current_dir/$dir_name/$ARCS_DEV_TOOL_LISTENAI_TOOLS_DIR_NAME"
fi
if [ -d "$current_dir/$dir_name/$ARCS_DEV_TOOL_TOOLCHAIN_DIR_NAME" ]; then
echo "Found NUCLEI_TOOLCHAIN_PATH: $current_dir/$dir_name/$ARCS_DEV_TOOL_TOOLCHAIN_DIR_NAME"
export NUCLEI_TOOLCHAIN_PATH="$current_dir/$dir_name/$ARCS_DEV_TOOL_TOOLCHAIN_DIR_NAME"
fi
return 0
fi
current_dir=$(dirname "$current_dir")
done
}
while [[ $# -gt 0 ]]; do
case $1 in
-S|--Source)
PROJECT_PATH="$2"
shift 2
;;
-B|--build)
OUTPUT="$2"
shift 2
;;
-t|--target)
TARGET="$2"
shift 2
;;
-C|--Clean)
CLEAN=true
shift 1
;;
-w|--warnings-as-errors)
WARNINGS_AS_ERRORS=true
shift 1
;;
-h|--help)
usage
;;
-r|--release)
RELEASE=true
shift 1
;;
-D*)
CMAKE_VARS+=("$1")
shift 1
;;
*)
echo "未知参数: $1"
usage
;;
esac
done
echo "Source: $PROJECT_PATH"
echo "Target: $TARGET"
echo "Clean : $CLEAN"
if [ -z "$LISTENAI_TOOLS_PATH" ] || [ -z "$NUCLEI_TOOLCHAIN_PATH" ]; then
find_dev_tools
fi
if [ -z "${LISTENAI_TOOLS_PATH}" ]; then
export LISTENAI_TOOLS_PATH="请添加 LISTENAI_TOOLS_PATH 环境变量,或在此行设置正确的路径"
echo "请添加 LISTENAI_TOOLS_PATH 环境变量或者修改脚本后, 注释脚本第 $LINENO 行";exit 1;
fi
if [ -z "${NUCLEI_TOOLCHAIN_PATH}" ]; then
export NUCLEI_TOOLCHAIN_PATH="请添加 NUCLEI_TOOLCHAIN_PATH 环境变量,或在此行设置正确的路径"
echo "请添加 NUCLEI_TOOLCHAIN_PATH 环境变量或者修改脚本后, 注释脚本第 $LINENO 行";exit 1;
fi
############### 下面代码不用修改 ##################
# 构建工具的位置
CMAKE_PROGRAM="$LISTENAI_TOOLS_PATH/cmake/bin/cmake"
NINJA_PROGRAM="$LISTENAI_TOOLS_PATH/ninja/ninja"
# 配置环境变量 ARCS_BASE
if [ -z "$ARCS_BASE" ]; then
find_arcs_base
fi
if [ "$CLEAN" = true ]; then
rm -rf $OUTPUT
fi
# Initialize CMAKE_VARS array if it doesn't exist
declare -a CMAKE_VARS
# Add warnings-as-errors flag if enabled
if [ "$WARNINGS_AS_ERRORS" = true ]; then
CMAKE_VARS+=("-DCMAKE_C_FLAGS=-Werror")
CMAKE_VARS+=("-DCMAKE_CXX_FLAGS=-Werror")
echo "Treating warnings as errors"
fi
# Add release flags if enabled
if [ "$RELEASE" = true ]; then
CMAKE_VARS+=("-DENABLE_DEBUG_PATH=OFF")
echo "Release mode enabled (-DENABLE_DEBUG_PATH=OFF)"
fi
$CMAKE_PROGRAM -B "$OUTPUT" -G Ninja -S "$PROJECT_PATH" \
-DCMAKE_MAKE_PROGRAM="$NINJA_PROGRAM" \
"${CMAKE_VARS[@]}"
if [ -z "$TARGET" ]; then
$CMAKE_PROGRAM --build "$OUTPUT" -j4
else
$CMAKE_PROGRAM --build "$OUTPUT" --target "$TARGET"
fi

View File

@@ -0,0 +1,28 @@
# 项目可根据需要在此文件中设置对应的配置
CONFIG_MEM_CONFIG=y
CONFIG_BACK_TRACE=y
CONFIG_FREERTOS_STATIC_ALLOCATION_ENABLE=1
CONFIG_FREERTOS_CPP11=y
CONFIG_LINK_OPTION_NONE_SPECS=y
# 文件系统配置
CONFIG_FILE_SYSTEM=y
CONFIG_FATFS_FILESYSTEM=y
# DISK驱动使能
CONFIG_DISK_DRIVER=y
CONFIG_DISK_DRIVER_SDMMC=y
# 抽象文件系统
CONFIG_LSFS=y
CONFIG_LSFS_FAT=y
CONFIG_LSFS_REGISTER_VFS=y
# 虚拟文件系统
CONFIG_LVFS=y
CONFIG_LVFS_POSIX_API=y
CONFIG_LVFS_POSIX_API_ALIAS=y

View File

@@ -0,0 +1,8 @@
tests:
samples.cpp.io_stream:
programmer: arcs
runner: uart
log_analyzer:
type: "regex"
pattern: "IO Stream Tests Completed"

View File

@@ -0,0 +1,171 @@
#include <string.h>
#include "lsfs.h"
#include "disk/disk_access.h"
#include "lisa_sdmmc.h"
#include "log_print.h"
#if CONFIG_LVFS_POSIX_API
#include "lvfs.h"
#endif
#define RAMDISK_DEVICE "RAM:"
#define RAMDISK_MOUNT_POINT "/" RAMDISK_DEVICE
#define RAMDISK_FILE RAMDISK_MOUNT_POINT "/ram.txt"
#define FLASHDISK_DEVICE "NAND:"
#define FLASHDISK_MOUNT_POINT "/" FLASHDISK_DEVICE
#define FLASHDISK_FILE FLASHDISK_MOUNT_POINT "/flash.txt"
#define SDMMC_DEVICE "SD:"
#define SDMMC_MOUNT_POINT "/" SDMMC_DEVICE
#define SDMMC_FILE SDMMC_MOUNT_POINT "/sdmmc.txt"
static struct lsfs_mount_t ram_lsfs_mnt = {
.type = LSFS_FATFS,
.mnt_point = RAMDISK_MOUNT_POINT,
.fs_data = NULL,
};
static struct lsfs_mount_t flash_lsfs_mnt = {
.type = LSFS_FATFS,
.mnt_point = FLASHDISK_MOUNT_POINT,
.fs_data = NULL,
};
static struct lsfs_mount_t sdmmc_lsfs_mnt = {
.type = LSFS_FATFS,
.mnt_point = SDMMC_MOUNT_POINT,
.fs_data = NULL,
};
static int fs_mount(struct lsfs_mount_t *mp)
{
int ret;
ret = lsfs_mount(mp);
if (ret != 0) {
LOGD("No file system reformatting");
ret = lsfs_mkfs(mp->type, &mp->mnt_point[1], NULL, 0);
if (ret == 0) {
LOGD("mkfs success");
ret = lsfs_mount(mp);
} else {
LOGD("mkfs failed,fr:%d", ret);
}
}
if (ret != 0) {
LOGD("Failed to mount filesystem: %d", ret);
return ret;
}
LOGD("%s mounted successfully", mp->mnt_point);
}
static int fs_unmount(struct lsfs_mount_t *mp)
{
int ret;
ret = lsfs_unmount(mp);
if (ret != 0) {
LOGD("%s unmount failed!\n", mp->mnt_point);
return ret;
}
LOGD("%s unmount success!\n", mp->mnt_point);
return 0;
}
static int list_dir(const char *path)
{
struct lsfs_dir_t dir;
struct lsfs_dirent entry;
int ret;
int nfile = 0, ndir = 0;
/* 初始化目录对象 */
lsfs_dir_t_init(&dir);
ret = lsfs_opendir(&dir, path);
LOGD("%s opendir: %d", path, ret);
if (ret == 0) {
while (1) {
ret = lsfs_readdir(&dir, &entry);
if (ret != 0 || entry.name[0] == 0) {
break; /* Error or end of dir */
}
if (entry.type == LSFS_DIR_ENTRY_DIR) {
/* Directory */
LOGD(" <DIR> %s", entry.name);
ndir++;
} else {
/* File */
LOGD("%10lu %s", entry.size, entry.name);
nfile++;
}
}
lsfs_closedir(&dir);
LOGD("%d dirs, %d files.", ndir, nfile);
} else {
LOGD("Failed to open \"%s\". (%u)", path, ret);
}
return ret;
}
int fs_write_read(const char *fullpath)
{
struct lsfs_file_t file;
int ret;
char buffer[64];
lsfs_file_t_init(&file);
ret = lsfs_open(&file, fullpath, LSFS_O_CREATE | LSFS_O_TRUNC | LSFS_O_WRITE);
if (ret != 0) {
LOGD("Failed to open file: %d", ret);
return ret;
}
ret = lsfs_write(&file, fullpath, strlen(fullpath));
if (ret != strlen(fullpath)) {
LOGD("Failed to write to file: %d", ret);
return ret;
}
LOGD("Write to file: %s", fullpath);
lsfs_close(&file);
lsfs_file_t_init(&file);
ret = lsfs_open(&file, fullpath, LSFS_O_READ);
if (ret != 0) {
LOGD("Failed to open file for reading: %d", ret);
return ret;
}
ret = lsfs_read(&file, buffer, sizeof(buffer) - 1);
if (ret <= 0) {
LOGD("Failed to read from file: %d", ret);
return ret;
}
buffer[ret] = '\0';
LOGD("Read from file: %s", buffer);
lsfs_close(&file);
return 0;
}
int test_lsfs_init()
{
lisa_sdmmc_probe(lisa_device_get("sdmmc0"));
disk_init(NULL);
#if CONFIG_LVFS_POSIX_API
lvfs_init();
#endif
lsfs_init();
fs_mount(&sdmmc_lsfs_mnt);
fs_write_read(SDMMC_FILE);
list_dir(SDMMC_MOUNT_POINT "/");
return 0;
}

View File

@@ -0,0 +1,19 @@
/*
* Copyright (c) 2025, LISTENAI
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
int test_lsfs_init();
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,245 @@
#include "io_stream_test.h"
// 基本控制台输入输出示例
void basicConsoleIOExample() {
std::cout << "\n=== Basic Console I/O Example ===" << std::endl;
// 基本输出
std::cout << "这是标准输出流示例。" << std::endl;
// 也可以使用std::endl来刷新缓冲区
std::cout << "数字输出: " << 42 << std::endl;
// 错误输出流
std::cerr << "这是标准错误流示例。" << std::endl;
// 在嵌入式环境中我们一般不使用交互式输入
std::cout << "嵌入式系统中一般通过其他方式如UART、文件或消息队列获取输入" << std::endl;
}
// 文件流示例 - 写入文件
void fileOutputExample(const std::string& filename) {
std::cout << "\n=== File Output Stream Example ===" << std::endl;
printf("before outFile: %s\n", filename.c_str());
// 创建输出文件流
std::ofstream outFile(filename);
printf("filename: %s\n", filename.c_str());
if (!outFile) {
std::cerr << "无法打开文件进行写入: " << filename << std::endl;
return;
}
// 写入文本内容
outFile << "这是第一行文本。" << std::endl;
outFile << "这是第二行文本。" << std::endl;
outFile << "数字: " << 12345 << std::endl;
// 写入一个数字列表
for (int i = 1; i <= 5; i++) {
outFile << "项目 " << i << ": " << (i * i) << std::endl;
}
// 文件流会在销毁时自动关闭,但显式关闭更清晰
outFile.close();
std::cout << "成功写入文件: " << filename << std::endl;
}
// 文件流示例 - 读取文件
void fileInputExample(const std::string& filename) {
std::cout << "\n=== File Input Stream Example ===" << std::endl;
// 创建输入文件流
std::ifstream inFile(filename);
if (!inFile) {
std::cerr << "无法打开文件进行读取: " << filename << std::endl;
return;
}
std::cout << "文件 " << filename << " 内容:" << std::endl;
// 逐行读取文件
std::string line;
while (std::getline(inFile, line)) {
std::cout << " " << line << std::endl;
}
inFile.close();
}
// 字符串流示例
void stringStreamExample() {
std::cout << "\n=== String Stream Example ===" << std::endl;
// 输出字符串流
std::ostringstream oss;
oss << "这是一个字符串流。" << std::endl;
oss << "可以像使用其他ostream一样使用它:" << std::endl;
oss << "整数: " << 42 << std::endl;
oss << "浮点数: " << 3.14159 << std::endl;
// 获取流中的字符串
std::string result = oss.str();
std::cout << "ostringstream的内容:\n" << result << std::endl;
// 输入字符串流
std::string data = "12 34 56 78";
std::istringstream iss(data);
int a, b, c, d;
iss >> a >> b >> c >> d;
std::cout << "从istringstream解析的数值:" << std::endl;
std::cout << "a = " << a << ", b = " << b << ", c = " << c << ", d = " << d << std::endl;
std::cout << "总和: " << (a + b + c + d) << std::endl;
}
// 格式化输出示例
void formattingExample() {
std::cout << "\n=== Formatting Output Example ===" << std::endl;
// 设置浮点数精度
double pi = 3.1415926535897932384626;
std::cout << "默认精度的π: " << pi << std::endl;
std::cout << "设置精度为10的π: " << std::setprecision(10) << pi << std::endl;
// 字段宽度
std::cout << "默认字段宽度:" << std::endl;
std::cout << 42 << std::endl;
std::cout << "设置字段宽度为10:" << std::endl;
std::cout << std::setw(10) << 42 << std::endl;
// 填充字符
std::cout << "设置填充字符为'*'并左对齐:" << std::endl;
std::cout << std::left << std::setfill('*') << std::setw(10) << 42 << std::endl;
// 恢复默认格式
std::cout.unsetf(std::ios::left);
std::cout << std::setfill(' ');
// 不同数值进制
int value = 255;
std::cout << "十进制: " << std::dec << value << std::endl;
std::cout << "十六进制: " << std::hex << value << std::endl;
std::cout << "八进制: " << std::oct << value << std::endl;
// 重置为十进制
std::cout << std::dec;
// 布尔值格式化
std::cout << "默认布尔值输出: " << true << ", " << false << std::endl;
std::cout << "字面布尔值输出: " << std::boolalpha << true << ", " << false << std::endl;
// 恢复默认布尔值格式
std::cout << std::noboolalpha;
}
// 输出运算符重载实现
std::ostream& operator<<(std::ostream& os, const Point& p) {
os << "(" << p.x << ", " << p.y << ")";
return os;
}
// 输入运算符重载实现
std::istream& operator>>(std::istream& is, Point& p) {
char dummy;
is >> dummy >> p.x >> dummy >> p.y >> dummy;
return is;
}
// 自定义类型的流示例
void customTypeStreamExample() {
std::cout << "\n=== Custom Type Stream Example ===" << std::endl;
// 使用重载的输出运算符
Point p1(10, 20);
std::cout << "点p1: " << p1 << std::endl;
// 点集合
std::vector<Point> points = {Point(1, 2), Point(3, 4), Point(5, 6)};
std::cout << "点集合:" << std::endl;
for (const auto& p : points) {
std::cout << " " << p << std::endl;
}
// 使用预设数据创建点
Point p2(7, 8);
std::cout << "预设的点: " << p2 << std::endl;
}
// 文件异常处理示例 - 尝试打开不存在的文件
void fileExceptionExample() {
std::cout << "\n=== File Exception Handling Example ===" << std::endl;
// 定义一个不存在的文件名
std::string nonExistentFile = "this_file_does_not_exist.txt";
std::cout << "尝试打开不存在的文件: " << nonExistentFile << std::endl;
try {
// 尝试打开文件,并使用exceptions方法设置异常掩码
std::ifstream file(nonExistentFile);
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
// 如果文件打开失败,上面的行将抛出异常,以下代码不会执行
std::string line;
while (std::getline(file, line)) {
std::cout << line << std::endl;
}
file.close();
}
catch (const std::ios_base::failure& e) {
std::cerr << "捕获到文件操作异常: " << e.what() << std::endl;
std::cerr << "文件 '" << nonExistentFile << "' 可能不存在或无法访问" << std::endl;
}
catch (const std::exception& e) {
std::cerr << "捕获到标准异常: " << e.what() << std::endl;
}
std::cout << "\n另一种异常处理方式 - 使用ifstream构造后立即检查状态:" << std::endl;
try {
std::ifstream file2(nonExistentFile);
if (!file2.is_open()) {
throw std::runtime_error("无法打开文件: " + nonExistentFile);
}
// 如果文件打开失败,以下代码不会执行
std::string content;
file2 >> content;
std::cout << "文件内容: " << content << std::endl;
}
catch (const std::runtime_error& e) {
std::cerr << "捕获到运行时异常: " << e.what() << std::endl;
}
}
void runIOStreamTests() {
std::cout << "=== IO Stream Tests ===" << std::endl;
// 运行基本控制台输入输出示例
basicConsoleIOExample();
// 运行文件流示例
std::string testFile = "test_file.txt";
fileOutputExample(testFile);
fileInputExample(testFile);
// 运行文件异常处理示例
fileExceptionExample();
// 运行字符串流示例
stringStreamExample();
// 运行格式化输出示例
formattingExample();
// 运行自定义类型的流示例
customTypeStreamExample();
}

View File

@@ -0,0 +1,53 @@
#ifndef IO_STREAM_TEST_H
#define IO_STREAM_TEST_H
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <iomanip>
// 基本控制台输入输出示例
void basicConsoleIOExample();
// 文件流示例 - 写入文件
void fileOutputExample(const std::string& filename);
// 文件流示例 - 读取文件
void fileInputExample(const std::string& filename);
// 字符串流示例
void stringStreamExample();
// 格式化输出示例
void formattingExample();
// 自定义类型的输入输出流操作符重载
class Point {
private:
int x, y;
public:
Point(int x = 0, int y = 0) : x(x), y(y) {}
int getX() const { return x; }
int getY() const { return y; }
// 友元函数声明,使其能访问私有成员
friend std::ostream& operator<<(std::ostream& os, const Point& p);
friend std::istream& operator>>(std::istream& is, Point& p);
};
// 输出运算符重载声明
std::ostream& operator<<(std::ostream& os, const Point& p);
// 输入运算符重载声明
std::istream& operator>>(std::istream& is, Point& p);
// 自定义类型的流示例
void customTypeStreamExample();
// 运行所有IO流测试
void runIOStreamTests();
#endif // IO_STREAM_TEST_H

View File

@@ -0,0 +1,22 @@
#include <iostream>
#include <thread>
#include "io_stream_test.h"
#include "fs.h"
void printNumbers(int threadID) {
for (int i = 1; i <= 5; ++i) {
std::cout << "Thread " << threadID << ": " << i << std::endl;
}
}
int main(int argc, char **argv)
{
test_lsfs_init();
// 运行IO Stream的测试
std::cout << "\n\n======= Starting IO Stream Tests =======\n" << std::endl;
runIOStreamTests();
std::cout << "\n======= IO Stream Tests Completed =======\n" << std::endl;
return 0;
}

View File

@@ -0,0 +1,105 @@
#include <errno.h>
#include <stdint.h>
#include "lib_sdc.h"
#include "drv_sdc.h"
#include "IOMuxManager.h"
#include "arcs_ap.h"
#include "esp_heap_caps.h"
#include "FreeRTOS.h"
#define SD_PORT SD_0
static SDCardInfo sd_info;
static uint8_t *dma_buf = NULL;
static int sdmmc_setup(void)
{
IOMuxManager_PinConfigure(CSK_IOMUX_PAD_A, 6, CSK_IOMUX_FUNC_ALTER15); // sd_clk
IOMuxManager_PinConfigure(CSK_IOMUX_PAD_A, 7, CSK_IOMUX_FUNC_ALTER15); // sd_cmd
IOMuxManager_PinConfigure(CSK_IOMUX_PAD_A, 5, CSK_IOMUX_FUNC_ALTER15); // sd_dat0
IOMuxManager_PinConfigure(CSK_IOMUX_PAD_A, 4, CSK_IOMUX_FUNC_ALTER15); // sd_dat1
IOMuxManager_PinConfigure(CSK_IOMUX_PAD_A, 9, CSK_IOMUX_FUNC_ALTER15); // sd_dat2
IOMuxManager_PinConfigure(CSK_IOMUX_PAD_A, 8, CSK_IOMUX_FUNC_ALTER15); // sd_dat3
IP_AP_CFG->REG_CLK_CFG1.bit.ENA_SDIOH_CLK = 1; // Enable SDIO Host Clock
IP_SDIOH->REG_VR1.bit.LO_SD_RSTN = 1; // SD Output Low-Active Reset Signal to RSTN of eMMC
IP_SDIOH->REG_CCR_TCR_SRR.bit.UPPER_BIT_SD_CLK_SEL = 0; // SD Clock Frequency Divider[9:8]: DIVx2
IP_SDIOH->REG_CCR_TCR_SRR.bit.LOW_BIT_SD_CLK_SEL = 0; // SD Clock Frequency Divider[7:0]: DIVx2
IP_SDIOH->REG_CCR_TCR_SRR.bit.SD_CLK_EN = 1; // SD Clock Enable
IP_SDIOH->REG_HC1_PCR_BGCR.bit.SD_BUS_POW = 1; // SD Bus Power Enable
IP_SDIOH->REG_HC1_PCR_BGCR.bit.SD_BUS_VOL = 7; // SD Bus Voltage Select: 5:1.8V 6:3.0V 7:3.3V
}
static int sdmmc_probe(uint32_t timeout_ms)
{
u32 ret;
ret = gm_api_sdc_platform_init(SDC_OPTION_ENABLE | SDC_OPTION_CD_INVERT, 0, NULL, (u32)&sd_info);
if (ret != ERR_SD_NO_ERROR) {
CLOGE("gm_api_sdc_platform_init failed:%d", ret);
goto _ERR1;
}
ret = gm_sdc_api_action(SD_PORT, GM_SDC_ACTION_INIT, NULL, NULL);
if (ret != ERR_SD_NO_ERROR) {
CLOGE("gm_sdc_api_action GM_SDC_ACTION_INIT failed:%d", ret);
goto _ERR1;
}
dma_buf = heap_caps_aligned_alloc(CACHE_LINE_SIZE(DCACHE), sd_info.FlowSet.sdma_bound_mask + 1,
MALLOC_CAP_DEFAULT | MALLOC_CAP_SPIRAM);
if (dma_buf == NULL) {
CLOGE("heap_caps_aligned_alloc failed");
goto _ERR1;
}
ret = gm_sdc_api_action(SD_PORT, GM_SDC_ACTION_SET_ADMA_BUFER, dma_buf, NULL);
if (ret != ERR_SD_NO_ERROR) {
CLOGE("gm_sdc_api_action GM_SDC_ACTION_SET_ADMA_BUFER failed:%d", ret);
goto _ERR1;
}
ret = gm_sdc_api_action(SD_PORT, GM_SDC_ACTION_SOFT_RESET, &(u32){SDHCI_SOFTRST_ALL}, NULL);
if (ret != ERR_SD_NO_ERROR) {
CLOGE("gm_sdc_api_action GM_SDC_ACTION_SOFT_RESET failed:%d", ret);
goto _ERR1;
}
for (int i = 0; i < timeout_ms; i += 10) {
// 0:SDR12/LS 1:SDR25/HS 2:SDR50 3:SDR104 4:DDR50
ret = gm_sdc_api_action(SD_PORT, GM_SDC_ACTION_CARD_SCAN, &(u32){UHS_SDR25_BUS_SPEED}, NULL);
if (ERR_SD_NO_ERROR == ret) {
break;
}
if (ERR_SD_CARD_NOT_EXIST != ret) {
CLOGW("Scan card failed:%d", ret);
}
vTaskDelay(pdMS_TO_TICKS(100));
}
if (ret != ERR_SD_NO_ERROR) {
goto _ERR1;
}
ret = gm_sdc_api_action(SD_PORT, GM_SDC_ACTION_SET_BUS_WIDTH, &(u32){4}, NULL);
if (ret != ERR_SD_NO_ERROR) {
goto _ERR1;
}
extern u8 SDC_SD_menu(u8 ip_idx);
SDC_SD_menu(SD_PORT);
return 0;
_ERR1:
if (dma_buf != NULL) {
heap_caps_free(dma_buf);
}
return -EIO;
}
int sdmmc_hard_init(void)
{
sdmmc_setup();
return sdmmc_probe(3000);
}