refactor(test): encapsulate FFT and DMA into unified module

This commit is contained in:
2026-04-13 17:24:58 +08:00
parent c4a34f7b38
commit 63fb8f6cc1
5 changed files with 100 additions and 1 deletions

View File

@@ -39,7 +39,9 @@ C_SRCS += $(COMMON_DIR)/drivers/core_time.c
C_SRCS += $(COMMON_DIR)/drivers/common_func.c C_SRCS += $(COMMON_DIR)/drivers/common_func.c
C_SRCS += $(COMMON_DIR)/drivers/dvi.c \ C_SRCS += $(COMMON_DIR)/drivers/dvi.c \
$(COMMON_DIR)/drivers/led.c \ $(COMMON_DIR)/drivers/led.c \
$(COMMON_DIR)/drivers/seg7.c $(COMMON_DIR)/drivers/seg7.c \
$(COMMON_DIR)/drivers/dma.c \
$(COMMON_DIR)/drivers/fft.c
INCLUDES += -I./ \ INCLUDES += -I./ \
-I$(COMMON_DIR)/include \ -I$(COMMON_DIR)/include \

View File

@@ -0,0 +1,37 @@
#include "dma.h"
void dma_start_transfer(int ch_id, uint32_t src, uint32_t dst, uint32_t bytes, uint32_t tag) {
volatile dma_ch_regs_t* ch = DMA_CH(ch_id);
// 检查通道是否空闲 防止覆盖正在运行的任务
if (ch->STATUS & STATUS_BUSY_BIT) {
// printf("channel %d busy\n", ch_id);
return; // 通道正忙,处理报错或重试
}
// 写入基本地址和长度
ch->SRC_ADDR = src;
ch->DST_ADDR = dst;
ch->LENGTH = bytes;
ch->TAG = tag; // 可选填入任务ID
// 置 Burst 属性并触发传输
// AXI SIZE: 2 (代表 2^2 = 4 Bytes与 32-bit 数据线匹配)
// AXI LEN: 15 (代表 16 beats 突发传输,最高效)
uint32_t ctrl_val = CTRL_BURST_SIZE(2) | CTRL_BURST_LEN(15);
// 写入 CTRL 并拉高 Bit 0 (Start)
ch->CTRL = ctrl_val | CTRL_START_BIT;
}
void dma_wait_polling(int ch_id) {
volatile dma_ch_regs_t* ch = DMA_CH(ch_id);
// 死等 Done bit 置 1
while (!(ch->STATUS & STATUS_DONE_BIT)) {
}
// 清除 Done 标志位 (Write 1 to Clear Bit 1)
// 注意:写 1 清 0 的设计,所以我们对 bit 1 写入 1
ch->STATUS = STATUS_DONE_BIT;
}

View File

@@ -0,0 +1,14 @@
#include "fft.h"
#include "common_func.h"
void fft_start() {
RegWrite(FFT_CSR_REG, FFT_CTRL_START);
}
void fft_wait() {
while ((RegRead(FFT_CSR_REG) & FFT_STAT_DONE) == 0) {}
}
unsigned int fft_get_csr() {
return RegRead(FFT_CSR_REG);
}

View File

@@ -0,0 +1,30 @@
#pragma once
#include <stdint.h>
typedef struct {
volatile uint32_t SRC_ADDR; // 0x00: 源地址
volatile uint32_t DST_ADDR; // 0x04: 目的地址
volatile uint32_t LENGTH; // 0x08: 传输总字节数
volatile uint32_t TAG; // 0x0C: 软件 Tag (硬件不处理,留给软件标记任务用)
volatile uint32_t CTRL; // 0x10: 控制寄存器 (配置 Burst 属性 + 启动)
volatile uint32_t STATUS; // 0x14: 状态寄存器 (Busy, Done)
volatile uint32_t RESV[10]; // 0x18 ~ 0x3C: 保留空间,凑齐 0x40 字节
} dma_ch_regs_t;
// 8通道 DMA 控制器基地址
#define CDMA_MUX_BASE 0xbf300000
#define DMA_CH(i) ((volatile dma_ch_regs_t*)(CDMA_MUX_BASE + (i) * 0x40))
// CTRL 寄存器位定义
#define CTRL_START_BIT (1 << 0)
#define CTRL_BURST_SIZE(x) (((x) & 0x7) << 3) // Bits [5:3]: AXI AxSIZE (0=1B, 1=2B, 2=4B, 3=8B)
#define CTRL_BURST_LEN(x) (((x) & 0xFF) << 6) // Bits [13:6]: AXI AxLEN (0=1 beat, 15=16 beats)
// STATUS 寄存器位定义
#define STATUS_BUSY_BIT (1 << 0)
#define STATUS_DONE_BIT (1 << 1)
void dma_start_transfer(int ch_id, uint32_t src, uint32_t dst, uint32_t bytes, uint32_t tag);
void dma_wait_polling(int ch_id);

View File

@@ -0,0 +1,16 @@
#pragma once
#define FFT_BASE 0xbf400000
#define FFT_IN_RE_BASE (FFT_BASE + 0x1000)
#define FFT_IN_IM_BASE (FFT_BASE + 0x2000)
#define FFT_OUT_RE_BASE (FFT_BASE + 0x3000)
#define FFT_OUT_IM_BASE (FFT_BASE + 0x4000)
#define FFT_CSR_REG (FFT_BASE + 0xF000)
#define FFT_CTRL_START (1 << 4)
#define FFT_STAT_DONE (1 << 1)
#define FFT_STAT_BUSY (1 << 0)
#define FFT_POINT_NUM 1024
void fft_start();
void fft_wait();
unsigned int fft_get_csr();