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,11 @@
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.c)

View File

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

View File

@@ -0,0 +1,270 @@
# DMA 示例
本示例演示如何使用 DMA 进行内存到内存(M2M)数据传输,包括初始化源和目标缓冲区、配置 DMA 通道、传输数据并验证传输结果。
## 📖 示例说明
### 功能演示
本示例演示以下功能:
- **内存到内存传输**:使用 DMA 将数据从源缓冲区传输到目标缓冲区
- **DMA 通道配置**:配置 DMA 传输宽度、突发大小、地址递增等参数
- **事件回调机制**:注册并处理 DMA 传输完成事件
- **传输验证**:使用内存比较验证数据传输的正确性
- **缓存同步**:支持 DMA 传输的缓存一致性管理
### 硬件要求
- ARCS 系列开发板
- USB 转串口工具(用于查看日志输出)
## 🚀 快速开始
### 1. 构建项目
在示例目录下执行构建脚本:
```bash
cd samples/drivers/dma
./build.sh
```
或者在 SDK 根目录执行:
```bash
./build.sh -S samples/drivers/dma -C
```
构建成功后,会在 `build` 目录下生成 `arcs.bin` 文件。
```shell
cskburn -s /dev/ttyUSB0 -b 3000000 0x1000 build/arcs.bin -C arcs
```
参数说明:
- `-s /dev/ttyUSB0`:串口设备路径(根据实际情况修改)
- `-b 3000000`:烧录波特率
- `0x1000`:烧录起始地址
- `build/arcs.bin`:烧录固件路径
> 💡 详细烧录步骤请参考 {ref}`快速开始 - 烧录运行 <flashing>`。
### 3. 查看输出
烧录完成后,复位开发板,通过串口工具(如 minicom)查看日志输出。
## 🔧 配置说明
### 项目配置
本示例在 `prj.conf` 中配置:
```conf
CONFIG_MEM_CONFIG=y
```
### DMA 参数配置
| 参数 | 值 | 说明 |
|------|-----|------|
| `MaxLen` | 1024 | 缓冲区最大长度(单位:32位字) |
| `SourceLen` | 1023 | 源缓冲区大小 |
| `ReceiveLen` | 1023 | 目标缓冲区大小 |
| `UnitLen` | 4 | 单位长度(字节) |
| `SRC_MASTER_SEL` | 0 | 源主设备选择(AP 仅支持 Master 0) |
| `DST_MASTER_SEL` | 0 | 目标主设备选择(AP 仅支持 Master 0) |
### DMA 传输配置
本示例配置的 DMA 传输参数:
```c
DMA_configure_check(&ch,
DMA_WIDTH_WORD, // 源宽度:32位字
DMA_BSIZE_16, // 源突发大小:16
DMA_WIDTH_BYTE, // 目标宽度:字节
DMA_BSIZE_64, // 目标突发大小:64
false, // 地址递增模式
DMA_CACHE_SYNC_AUTO); // 自动缓存同步
```
## 📋 代码解析
### 关键代码段
#### 1. DMA 初始化
```c
int main(int argc, char **argv)
{
// 初始化 DMA 控制器
dma_initialize();
printf("Hello, world! dma\n");
// 配置 DMA 传输
uint8_t ch = 0; // ch可取0-DMA_NUMBER_OF_CHANNELS
DMA_configure_check(&ch, DMA_WIDTH_WORD, DMA_BSIZE_16,
DMA_WIDTH_BYTE, DMA_BSIZE_64,
false, DMA_CACHE_SYNC_AUTO);
return 0;
}
```
#### 2. 缓冲区初始化
```c
void DMA_mem_int(uint32_t total_bytes)
{
DMA_Src_Buf_Gen((uint8_t *)SourceBuf, total_bytes);
DMA_Dst_Buf_Gen((uint8_t *)DestinBuf, total_bytes);
}
// 生成源缓冲区内容(顺序填充0~255)
static void DMA_Src_Buf_Gen(uint8_t *buffer, uint32_t size)
{
int i;
for (i = 0; i < size; i++)
{
buffer[i] = i % 256;
}
}
// 生成目的缓冲区内容(填充平方值 % 256)
static void DMA_Dst_Buf_Gen(uint8_t *buffer, uint32_t size)
{
int i = 0;
for (i = 0; i < size; i++)
{
buffer[i] = (i * i) % 256;
}
}
```
#### 3. DMA 事件回调
```c
static void DMA_DrvEvent(uint32_t event_info, uint32_t xfer_bytes, uint32_t usr_param)
{
printf("[%s]: event = %d, channel = %d, xfer_bytes = %d\r\n", __func__,
event_info & 0xFF, (event_info >> 8) & 0xFF, xfer_bytes);
DMAEvent = event_info & 0xFF;
}
```
#### 4. 等待 DMA 完成
```c
static void DMA_Waiting(void)
{
while (1)
{
if (DMAEvent & DMA_EVENT_TRANSFER_COMPLETE)
{
DMAEvent &= (~DMA_EVENT_TRANSFER_COMPLETE);
break;
}
}
}
```
#### 5. DMA 通道配置
```c
// 申请 DMA 通道
uint8_t ch = dma_channel_select(pch, DMA_DrvEvent, 0, cache_sync);
if (ch == DMA_CHANNEL_ANY)
{
printf("[FAILED] NO free DMA channel!!\r\n");
return false;
}
// 配置 DMA 通道
stat = dma_channel_configure(ch, src_addr, dst_addr,
total_bytes / WIDTH_BYTES(src_width),
control, config_low, config_high, 0, 0);
if (stat == -1)
{
printf("[FAILED] dma_channel_configure error.\r\n");
dma_channel_disable(ch, true);
return false;
}
```
#### 6. 传输验证
```c
// 比较内存,判断是否传输一致
if (memcmp(SourceBuf, DestinBuf, total_bytes) == 0)
{
printf("Memory compare success\n");
return true;
}
else
{
printf("[%s FAILED] channel %d, Memory compare error !!!!!\r\n",
__func__, ch);
return false;
}
```
## 🔍 预期输出
程序运行后,串口将输出以下日志:
```
Hello, world! dma
SourceBuf: DD CC BB AA
DestinBuf (Before): 00 99 88 77
[DMA_DrvEvent]: event = 1, channel = 0, xfer_bytes = 4092
After DMA transfer:
DestinBuf (After ): DD CC BB AA
Memory compare success
```
输出说明:
- `Hello, world! dma`:程序启动信息
- `SourceBuf`:源缓冲区数据(传输前)
- `DestinBuf (Before)`:目标缓冲区数据(传输前)
- `[DMA_DrvEvent]`:DMA 传输完成事件回调
- `event = 1`:传输完成事件
- `channel = 0`:使用的 DMA 通道号
- `xfer_bytes = 4092`:传输的字节数(1023 × 4)
- `DestinBuf (After)`:目标缓冲区数据(传输后)
- `Memory compare success`:内存比较成功,数据传输正确
## ⚠️ 注意事项
1. **烧录地址**:
- 本示例的烧录地址为 `0x1000`
2. **DMA 传输宽度**:
- 源宽度:`DMA_WIDTH_WORD`(32位字)
- 目标宽度:`DMA_WIDTH_BYTE`(字节)
3. **突发大小**:
- 源突发大小:`DMA_BSIZE_16`(16)
- 目标突发大小:`DMA_BSIZE_64`(64)
4. **地址模式**:
- `addr_dec = false`:使用地址递增模式
- `addr_dec = true`:使用地址递减模式(本示例未使用)
5. **缓存同步**:
- `DMA_CACHE_SYNC_AUTO`:自动处理缓存一致性
- DMA 传输时需要确保 CPU 缓存与内存数据同步
6. **传输大小计算**:
- 总传输字节数:`SourceLen × UnitLen = 1023 × 4 = 4092` 字节
- DMA 传输单元数:`total_bytes / WIDTH_BYTES(src_width)`
7. **通道管理**:
- 使用前需要通过 `dma_channel_select()` 申请通道
- 使用后可以通过 `dma_channel_disable()` 释放通道
- 通道号范围:0 到 `DMA_NUMBER_OF_CHANNELS - 1`
8. **错误处理**:
- 检查通道申请是否成功(是否返回 `DMA_CHANNEL_ANY`)
- 检查通道配置是否成功(是否返回 -1)
- 传输完成后验证数据正确性

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,2 @@
# 项目可根据需要在此文件中设置对应的配置
CONFIG_MEM_CONFIG=y

View File

@@ -0,0 +1,9 @@
tests:
samples.dma:
programmer: arcs
runner: uart
log_analyzer:
type: "regex"
pattern: "Memory compare success"

View File

@@ -0,0 +1,189 @@
#include "dma.h"
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#define MASTER_SEL_CNT 4
#define SRC_MASTER_SEL 0 // only support Master 0 on AP (Options: 0 ~ 3)
#define DST_MASTER_SEL 0 // only support Master 0 on AP (Options: 0 ~ 3)
#define SMS_NO SRC_MASTER_SEL
#define DMS_NO DST_MASTER_SEL
// DMA 事件标志
static uint32_t volatile DMAEvent;
// 源/目的缓冲区的最大长度
#define MaxLen 1024 // 1024*5
// 源缓冲区大小
#define SourceLen (MaxLen-1) //100
// 目的缓冲区大小
#define ReceiveLen (MaxLen-1) //100
// 单位长度(字节)
#define UnitLen 4
// 定义源/目的缓冲区(使用32位整型数组)
static uint32_t SourceBuf[MaxLen] = {0};
static uint32_t DestinBuf[MaxLen] = {0};
//=============================================================================
// 生成源缓冲区内容(顺序填充0~255)
static void
DMA_Src_Buf_Gen(uint8_t *buffer, uint32_t size)
{
int i;
for (i = 0; i < size; i++)
{
buffer[i] = i % 256;
}
}
// 生成目的缓冲区内容(填充平方值 % 256)
static void
DMA_Dst_Buf_Gen(uint8_t *buffer, uint32_t size)
{
int i = 0;
for (i = 0; i < size; i++)
{
buffer[i] = (i * i) % 256;
}
}
// DMA 事件回调函数
static void DMA_DrvEvent(uint32_t event_info, uint32_t xfer_bytes, uint32_t usr_param)
{
printf("[%s]: event = %d, channel = %d, xfer_bytes = %d\r\n", __func__,
event_info & 0xFF, (event_info >> 8) & 0xFF, xfer_bytes);
DMAEvent = event_info & 0xFF;
}
// 等待 DMA 完成传输
static void
DMA_Waiting(void)
{
while (1)
{
if (DMAEvent & DMA_EVENT_TRANSFER_COMPLETE)
{
DMAEvent &= (~DMA_EVENT_TRANSFER_COMPLETE);
break;
}
}
}
//=============================================================================
// 初始化源缓冲区与目的缓冲区的数据
void DMA_mem_int(uint32_t total_bytes)//初始化源缓冲区与目的缓冲区打大小
{
DMA_Src_Buf_Gen((uint8_t *)SourceBuf, total_bytes);
DMA_Dst_Buf_Gen((uint8_t *)DestinBuf, total_bytes);
}
// DMA 配置检查:包括缓存一致性、源/目的地址宽度、地址增减设置等
// 返回内存比较结果:true 表示传输正确,false 表示错误或未成功启动 DMA
bool DMA_configure_check(uint8_t *pch, uint32_t src_width, uint32_t src_bsize,
uint32_t dst_width, uint32_t dst_bsize,
bool addr_dec, DMA_CACHE_SYNC cache_sync)
{
int stat = 0;
uint32_t control, config_low, config_high;
uint32_t src_addr, dst_addr, total_bytes = SourceLen * UnitLen;
uint32_t print_size = 1 * UnitLen;//只打印1个单位数据
DMA_mem_int(total_bytes);
// 在 DMA 传输前访问源/目的缓冲区部分内容
SourceBuf[0] = 0xAABBCCDD;
DestinBuf[0] = 0x77889900;
// 打印DMA传输前的源和目的缓冲区数据
uint8_t *src_bytes = (uint8_t *)SourceBuf;
uint8_t *dst_bytes = (uint8_t *)DestinBuf;
printf("SourceBuf: ");
for (size_t i = 0; i < print_size; i++)
{
printf("%02X ", src_bytes[i]);
}
printf("\n");
// Before transfer
printf("DestinBuf (Before): ");
for (size_t i = 0; i < print_size; i++)
{
printf("%02X ", dst_bytes[i]);
}
printf("\n");
src_addr = (uint32_t)SourceBuf;
dst_addr = (uint32_t)DestinBuf;
// 设置 DMA 控制参数
control = DMA_CH_CTLL_INT_EN | DMA_CH_CTLL_DST_WIDTH(dst_width) | DMA_CH_CTLL_SRC_WIDTH(src_width) |
DMA_CH_CTLL_DST_BSIZE(dst_bsize) | DMA_CH_CTLL_SRC_BSIZE(src_bsize) |
DMA_CH_CTLL_TTFC_M2M | DMA_CH_CTLL_DMS(DMS_NO) | DMA_CH_CTLL_SMS(SMS_NO);
if (addr_dec)
{
src_addr += total_bytes - (0x1 << src_width);
dst_addr += total_bytes - (0x1 << dst_width);
control |= DMA_CH_CTLL_DST_DEC | DMA_CH_CTLL_SRC_DEC;
}
else
{
control |= DMA_CH_CTLL_DST_INC | DMA_CH_CTLL_SRC_INC;
}
config_low = DMA_CH_CFGL_CH_PRIOR(0);
config_high = DMA_CH_CFGH_FIFO_MODE;
// 申请 DMA 通道
uint8_t ch = dma_channel_select(pch, DMA_DrvEvent, 0, cache_sync);
if (ch == DMA_CHANNEL_ANY)
{
printf("[FAILED] NO free DMA channel!!\r\n");
return false;
}
// 配置 DMA 通道
stat = dma_channel_configure(ch, src_addr, dst_addr, total_bytes / WIDTH_BYTES(src_width),
control, config_low, config_high, 0, 0);
if (stat == -1)
{
printf("[FAILED] dma_channel_configure error.\r\n");
dma_channel_disable(ch, true); // release the selected DMA channel
return false;
}
DMA_Waiting();
// 打印 DMA 传输后的缓冲区数据
printf("After DMA transfer:\n");
src_bytes = (uint8_t *)SourceBuf;
dst_bytes = (uint8_t *)DestinBuf;
printf("DestinBuf (After ): ");
for (size_t i = 0; i < print_size; i++)
{
printf("%02X ", dst_bytes[i]);
}
printf("\n");
// 比较内存,判断是否传输一致
if (memcmp(SourceBuf, DestinBuf, total_bytes) == 0)
{
printf("Memory compare success\n");
return true;
}
else
{
printf("[%s FAILED] channel %d, Memory compare error !!!!!\r\n", __func__, ch);
return false;
}
}
int main(int argc, char **argv)
{
// 初始化 DMA 控制器
dma_initialize();
printf("Hello, world! dma\n");
// 配置 DMA:目标地址按字节对齐,突发大小为 64
uint8_t ch = 0;// ch可取0-DMA_NUMBER_OF_CHANNELS
DMA_configure_check(&ch, DMA_WIDTH_WORD, DMA_BSIZE_16, DMA_WIDTH_BYTE, DMA_BSIZE_64, false, DMA_CACHE_SYNC_AUTO);
return 0;
}