Skip to content

Commit

Permalink
fft_test
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxCrazy1101 committed Apr 2, 2023
1 parent c28eec2 commit 1ab0aad
Show file tree
Hide file tree
Showing 14 changed files with 342 additions and 98 deletions.
2 changes: 2 additions & 0 deletions App/Include/my_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ extern "C" {
#include "semphr.h"
#include "event_groups.h"

#define SAMPLE_RATE 10000 // 采样频率
#define FFT_SIZE 2048 // FFT变换点数
/**
* @brief 创建任务
*/
Expand Down
157 changes: 121 additions & 36 deletions App/Source/app_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "gui_guider.h"
#include "events_init.h"

#include "pga113.h"

// #include "lv_demos.h"
// #include "lv_examples.h"
/*
Expand Down Expand Up @@ -67,8 +69,22 @@ static TaskHandle_t xHandleTaskTEST = NULL;
static SemaphoreHandle_t xMutex = NULL;
EventGroupHandle_t xCreatedEventGroup = NULL;

arm_rfft_fast_instance_f32 S;
uint32_t fft_size = 2048;
/* FFT变量 */
arm_rfft_fast_instance_f32 fft_cfg;
/* FFT数组 */
uint8_t app_fft_data_ready = 0;
float32_t adc_ndata[FFT_SIZE] = {0}; // 归一化后的adc数据
float32_t fft_data[FFT_SIZE] = {0};
/* 参考信号 */
float32_t ref_iwave[FFT_SIZE]; // 存储参考I信号的数组
float32_t ref_qwave[FFT_SIZE]; // 存储参考Q信号的数组

// // 计算电压值
// static inline float voltage(float adc_value)
// {
// float adc_voltage = adc_value / (1 << ADC_BITS) * ADC_REF_VOLTAGE;
// return adc_voltage;
// }

static void vTaskUsageRate(void)
{
Expand Down Expand Up @@ -146,39 +162,81 @@ void app_create_task(void)
*/
static void task_lcr(void *pvParameters)
{
/* 实数rfft点数 */
fft_size = 2048;

/* 初始化结构体S中的参数 */
arm_rfft_fast_init_f32(&S, fft_size);
arm_rfft_fast_init_f32(&fft_cfg, FFT_SIZE);
// /* 初始化汉明窗 */
// float32_t hamming_window[FFT_SIZE];
// for (uint32_t i = 0; i < FFT_SIZE; i++) {
// hamming_window[i] = 0.54f - 0.46f * arm_cos_f32(2.0f * PI * i / (FFT_SIZE - 1));
// }

/* 生成频率为 1000 Hz的IQ参考信号 */
const float32_t amplitude = 1.0f; // 正弦波幅度
const float32_t sampling_rate = SAMPLE_RATE; // 采样率
const float32_t frequency = 1000.0f; // 正弦波频率
const float32_t phase_i = 0.0f; // 正弦波相位

for (int i = 0; i < FFT_SIZE; i++) {
float32_t time = (float32_t)i / sampling_rate;
ref_iwave[i] = amplitude * arm_sin_f32(2.0f * PI * frequency * time + phase_i);
ref_qwave[i] = amplitude * arm_cos_f32(2.0f * PI * frequency * time + phase_i);
// printf("%f %f\r\n", ref_iwave[i], ref_qwave[i]);
}

// TODO: 加窗
while (1) {
// if (os_evt_wait_or(StartTaskWaitFlag, 0xFFFF) == OS_R_EVT) {
// xResult = os_evt_get();

// switch (xResult) {
// case DspFFT2048Pro_15:
// /* 读取的是ADC3的位置 */
// g_DSO1->usCurPos = 10240 - DMA2_Stream1->NDTR;

// /* 读取的是ADC1的位置 */
// g_DSO2->usCurPos = 10240 - DMA2_Stream0->NDTR;

// DSO2_WaveTrig(g_DSO2->usCurPos);
// DSO1_WaveTrig(g_DSO1->usCurPos);
// DSO2_WaveProcess();
// DSO1_WaveProcess();
// break;

// case DspMultiMeterPro_0:
// g_uiAdcAvgSample = ADC_GetSampleAvgN();
// break;

// /* 其它位暂未使用 */
// default:
// printf_taskdbg("xResult = %x\r\n", xResult);
// break;
// }
// }
if (app_fft_data_ready == 1) {

/* 将ADC采样值转换为F32格式并归一化 */
arm_scale_f32(adc_ndata, 1.0f / 4096, adc_ndata, FFT_SIZE);
/* 与参考I信号相乘 */
arm_mult_f32(adc_ndata, ref_iwave, fft_data, FFT_SIZE);
/* RFFT变换 */
arm_rfft_fast_f32(&fft_cfg, fft_data, fft_data, 0);
/* 计算频率分辨率 */
float freq_resolution = (float)SAMPLE_RATE / FFT_SIZE;

/* 找到最大的幅值和对应的频率 */
float max_amplitude = 0;
int max_index = 0;
for (int i = 0; i < FFT_SIZE / 2 + 1; i++) {
float amplitude = fft_data[i];
if (amplitude > max_amplitude) {
max_amplitude = amplitude;
max_index = i;
}
}
float max_freq = freq_resolution * max_index;
printf("Max frequency: %fHz\n", max_freq);

// 计算电压值
// float adc_voltage = voltage(fft_data[max_index]);
printf("ADC voltage: %fV\n", fft_data[max_index]);

// /* 与参考Q信号相乘 */
arm_mult_f32(adc_ndata, ref_qwave, fft_data, FFT_SIZE);
/* RFFT变换 */
arm_rfft_fast_f32(&fft_cfg, fft_data, fft_data, 0);
/* 找到最大的幅值和对应的频率 */
max_amplitude = 0;
max_index = 0;
for (int i = 0; i < FFT_SIZE / 2 + 1; i++) {
float amplitude = fft_data[i];
if (amplitude > max_amplitude) {
max_amplitude = amplitude;
max_index = i;
}
}
max_freq = freq_resolution * max_index;
printf("Max frequency: %fHz\n", max_freq);

// 计算电压值
// float adc_voltage = voltage(fft_data[max_index]);
printf("ADC voltage: %fV\n", fft_data[max_index]);
app_fft_data_ready = 0;
}

vTaskDelay(1000);
}
}
Expand All @@ -200,7 +258,7 @@ static void task_adc0(void *pvParameters)
vref_value = (adc0_data[2] * 3.3f / 4096);

// printf("The temperature origin data is %d \r ,Battery Voltage origin data: %d \n", adc0_data[0], adc0_data[1]);
thread_safe_printf("Battery: %5.2f V, Temperature: %2.0f degrees Celsius, Vref: %5.2fV.\r\n", adc0_data[0] * 3.3f * 2 / 4096, temperature, vref_value);
// thread_safe_printf("Battery: %5.2f V, Temperature: %2.0f degrees Celsius, Vref: %5.2fV.\r\n", adc0_data[0] * 3.3f * 2 / 4096, temperature, vref_value);
if (battery_value < 0) {
battery_value = 0;
} else if (battery_value > 100) {
Expand All @@ -222,14 +280,25 @@ static void task_user_if(void *pvParameters)
{
uint8_t ucKeyCode;
uint8_t pcWriteBuffer[500];

uint8_t tmp = 1;
while (1) {
ucKeyCode = bsp_key_dequeue();

if (ucKeyCode != KEY_NONE) {
switch (ucKeyCode) {
/* K1键按下 打印任务执行情况 */
case KEY_DOWN_K1:
printf("In KEY_DOWN_K1\r\n");
if (tmp) {
pga113_set_gain(0xE1F1);
pga113_set_gain(PGA11X_CMD_WRITE | PGA11X_GAIN50 << 4 | PGA11X_CH1);
} else {
pga113_set_gain(0xE1F1);
pga113_set_gain(PGA11X_CMD_WRITE | PGA11X_GAIN1 << 4 | PGA11X_CH0_VCAL);
}
tmp = 1 - tmp;
break;
/* K1键按下 打印任务执行情况 */
case KEY_LONG_K1:
/* 任务运行状态的定义如下,跟上面串口打印字母X, B, R, D, S对应:
* #define tskRUNNING_CHAR ( 'X' ) 运行
* #define tskBLOCKED_CHAR ( 'B' ) 阻塞
Expand Down Expand Up @@ -265,9 +334,25 @@ static void task_user_if(void *pvParameters)
*/
static void task_test_only_once(void *pvParameters)
{
uint8_t tmp = 1;
while (1) {
// printf("%d\r\n", adc1_data[0]);
vTaskDelay(100);
// printf("[");
// for (size_t i = 0; i < 1024; i++)
// {
// printf("%d,", adc1_data[i]);
// }
// printf("]\r\n");
dma_interrupt_flag_clear(DMA1, DMA_CH3, DMA_INT_FLAG_FTF); // 清除中断标志
dma_interrupt_enable(DMA1, DMA_CH3, DMA_CHXCTL_FTFIE);
// tmp = 1 - tmp;
// if (tmp == 1) {
// pga113_set_gain(PGA113_CH0_GAIN10);

// } else {
// pga113_set_gain(PGA113_CH0_GAIN1);
// }
vTaskDelay(10000);
}
}

Expand Down
1 change: 0 additions & 1 deletion Firmware/CMSIS/GD/GD32F4xx/Source/system_gd32f4xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ static void system_clock_240m_8m_hxtal(void);
#elif defined (__SYSTEM_CLOCK_240M_PLL_25M_HXTAL)
uint32_t SystemCoreClock = __SYSTEM_CLOCK_240M_PLL_25M_HXTAL;
static void system_clock_240m_25m_hxtal(void);

#endif /* __SYSTEM_CLOCK_IRC16M */

/* configure the system clock */
Expand Down
6 changes: 5 additions & 1 deletion Hardware/Include/adc.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#ifndef ADC_H
#define ADC_H
#include "gd32f4xx.h"

#define ADC_BITS 12 // 采样值位数
#define ADC_REF_VOLTAGE 3.3f // 参考电压

/* ADC通道长度 */
#define ADC0_CHANNEL_LENGTH 3

/* ADC数据缓存大小 */
#define ADC0_BUFFER_LENGTH ADC0_CHANNEL_LENGTH
#define ADC1_BUFFER_LENGTH 1024
#define ADC1_BUFFER_LENGTH 2048

extern uint16_t adc0_data[ADC0_BUFFER_LENGTH];
extern uint16_t adc1_data[ADC1_BUFFER_LENGTH];
Expand Down
13 changes: 13 additions & 0 deletions Hardware/Include/bl1555.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef BL1555_H
#define BL1555_H
#ifdef __cplusplus
extern "C" {
#endif
#include "gd32f4xx.h"

void bl1555_config();

#ifdef __cplusplus
}
#endif
#endif /* BL1555_H */
57 changes: 57 additions & 0 deletions Hardware/Include/pga113.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#ifndef PGA113_H
#define PGA113_H
#ifdef __cplusplus
extern "C" {
#endif
#include "gd32f4xx.h"
#include "main.h"
// 1, 2, 5,10,20,50,100,200
/* PGA11x commands (PGA112/PGA113) */
#define PGA11X_CMD_READ 0x6a00
#define PGA11X_CMD_WRITE 0x2a00
#define PGA11X_CMD_NOOP 0x0000
#define PGA11X_CMD_SDN_DIS 0xe100
#define PGA11X_CMD_SDN_EN 0xe1f1

#define PGA113_CH0_GAIN1 PGA11X_CMD_WRITE | PGA11X_GAIN1_GAIN1 << 4 | PGA11X_CHAN_CAL1

typedef enum {
PGA11X_GAIN1 = 0,
PGA11X_GAIN2 = 1,
PGA11X_GAIN4 = 2,
PGA11X_GAIN5 = 2,
PGA11X_GAIN8 = 3,
PGA11X_GAIN10 = 3,
PGA11X_GAIN16 = 4,
PGA11X_GAIN20 = 4,
PGA11X_GAIN32 = 5,
PGA11X_GAIN50 = 5,
PGA11X_GAIN64 = 6,
PGA11X_GAIN100 = 6,
PGA11X_GAIN128 = 7,
PGA11X_GAIN200 = 7,
} PGA11X_GAIN;

typedef enum {
PGA11X_CH0_VCAL = 0, /* VCAL/CH0 */
PGA11X_CH1 = 1, /* CH1 */
PGA11X_CH2 = 2, /* CH2 (PGA116/PGA117 only) */
PGA11X_CH3 = 3, /* CH3 (PGA116/PGA117 only) */
PGA11X_CH4 = 4, /* CH4 (PGA116/PGA117 only) */
PGA11X_CH5 = 5, /* CH5 (PGA116/PGA117 only) */
PGA11X_CH6 = 6, /* CH6 (PGA116/PGA117 only) */
PGA11X_CH7 = 7, /* CH7 (PGA116/PGA117 only) */
PGA11X_CH8 = 8, /* CH8 (PGA116/PGA117 only) */
PGA11X_CH9 = 9, /* CH9 (PGA116/PGA117 only) */
PGA11X_CAL1 = 12, /* CAL1: connects to GND */
PGA11X_CAL2 = 13, /* CAL2: connects to 0.9VCAL */
PGA11X_CAL3 = 14, /* CAL3: connects to 0.1VCAL */
PGA11X_CAL4 = 15, /* CAL4: connects to VREF */
} PGA11X_CH;

void pga11x_config();
void pga113_set_gain(uint16_t);
#ifdef __cplusplus
}
#endif
#endif /* PGA113_H */
9 changes: 9 additions & 0 deletions Hardware/Source/bl1555.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "bl1555.h"
void bl1555_config()
{
uint32_t config_pin = GPIO_PIN_8;
uint32_t config_port = GPIOA;
/* PA8 */
gpio_mode_set(config_port, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, config_pin);
gpio_output_options_set(config_port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, config_pin);
}
3 changes: 1 addition & 2 deletions Hardware/Source/bsp_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ typedef struct

/* GPIO和PIN定义 */
static const X_GPIO_T s_gpio_list[HARD_KEY_NUM] = {
{GPIO_IDR_CAL(GPIOA, 0), 0}, /* K1 */
{GPIO_IDR_CAL(GPIOA, 0), 1}, /* K1 */
// {GPIOC, GPIO_PIN_13, 0}, /* K2 */
// {GPIOH, GPIO_PIN_4, 0}, /* K3 */
// {GPIOG, GPIO_PIN_2, 0}, /* JOY_U */
Expand Down Expand Up @@ -304,7 +304,6 @@ static void bsp_DetectKey(uint8_t i)
void bsp_KeyScan(void)
{
uint8_t i;

for (i = 0; i < KEY_COUNT; i++) {
bsp_DetectKey(i);
}
Expand Down
11 changes: 7 additions & 4 deletions Hardware/Source/dma.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "dma.h"
#include "adc.h"
const uint16_t SinWaveTable[100] = {0x800, 0x880, 0x900, 0x97f, 0x9fd, 0xa78, 0xaf1, 0xb67, 0xbda, 0xc49, 0xcb3, 0xd19, 0xd79, 0xdd4,
0xe2a, 0xe78, 0xec1, 0xf02, 0xf3d, 0xf70, 0xf9b, 0xfbf, 0xfdb, 0xfef, 0xffb, 0x1000, 0xffb, 0xfef,
0xe2a, 0xe78, 0xec1, 0xf02, 0xf3d, 0xf70, 0xf9b, 0xfbf, 0xfdb, 0xfef, 0xffb, 0xfff, 0xffb, 0xfef,
0xfdb, 0xfbf, 0xf9b, 0xf70, 0xf3d, 0xf02, 0xec1, 0xe78, 0xe2a, 0xdd4, 0xd79, 0xd19, 0xcb3, 0xc49,
0xbda, 0xb67, 0xaf1, 0xa78, 0x9fd, 0x97f, 0x900, 0x880, 0x7ff, 0x77f, 0x6ff, 0x680, 0x602, 0x587,
0x50e, 0x498, 0x425, 0x3b6, 0x34c, 0x2e6, 0x286, 0x22b, 0x1d5, 0x187, 0x13e, 0xfd, 0xc2, 0x8f,
Expand All @@ -24,7 +24,7 @@ void dma_config()
dma_flag_clear(DMA0, DMA_CH4, DMA_INTF_HTFIF);
dma_flag_clear(DMA0, DMA_CH4, DMA_INTF_FTFIF);

/* 配置 DMA0 channel 4 */
/* 配置 DMA0 channel 4 LCD_SPI*/
dma_channel_subperipheral_select(DMA0, DMA_CH4, DMA_SUBPERI0);
dma_struct.periph_addr = (uint32_t)&SPI_DATA(SPI1); // 外设地址 SPI1 data register
// dma_struct.memory0_addr =;//暂不设置传输地址
Expand All @@ -39,7 +39,7 @@ void dma_config()
nvic_irq_enable(DMA0_Channel4_IRQn, 9, 0); // 设置中断优先级
// dma_channel_enable(DMA0, DMA_CH4);//暂不开启

/* 配置 DMA0 channel 5 */
/* 配置 DMA0 channel 5 DAC*/
dma_deinit(DMA0, DMA_CH5);
dma_flag_clear(DMA0, DMA_CH5, DMA_INTF_FEEIF);
dma_flag_clear(DMA0, DMA_CH5, DMA_INTF_SDEIF);
Expand Down Expand Up @@ -103,13 +103,16 @@ void dma_config()
dma_struct.periph_addr = (uint32_t)(&ADC_RDATA(ADC1)); // 外设地址 ADC0右对齐12位输出寄存器
dma_struct.memory0_addr = (uint32_t)adc1_data;
dma_struct.direction = DMA_PERIPH_TO_MEMORY;
dma_struct.number = 1024;
dma_struct.number = ADC1_BUFFER_LENGTH;
dma_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE; // 外设地址不变
dma_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE; // 内存地址自加
dma_struct.periph_memory_width = DMA_PERIPH_WIDTH_16BIT; // 位宽半字
dma_struct.priority = DMA_PRIORITY_ULTRA_HIGH; // 优先级最高
dma_struct.circular_mode = DMA_CIRCULAR_MODE_ENABLE; // 使能循环模式
dma_single_data_mode_init(DMA1, DMA_CH3, &dma_struct);
/* 配置ADC1采样中断 */
nvic_irq_enable(DMA1_Channel3_IRQn, 14, 0);
dma_interrupt_enable(DMA1, DMA_CH3, DMA_CHXCTL_FTFIE);

dma_channel_enable(DMA1, DMA_CH3);
}
Loading

0 comments on commit 1ab0aad

Please sign in to comment.