From 16d792d62b59cca96cf7e22a84cc4a7b374da709 Mon Sep 17 00:00:00 2001 From: Roy_01 Date: Mon, 30 Mar 2026 17:22:53 +0800 Subject: [PATCH] feat: add control commands for DDS parameters --- .../Examples/template/dds/inc/main.h | 15 +-- .../Examples/template/dds/src/init.c | 14 +-- .../Examples/template/dds/src/main.c | 96 ++++++++++++++----- 3 files changed, 87 insertions(+), 38 deletions(-) diff --git a/pec930_sdk-v1.0.1/Examples/template/dds/inc/main.h b/pec930_sdk-v1.0.1/Examples/template/dds/inc/main.h index 9cad044..8e03787 100644 --- a/pec930_sdk-v1.0.1/Examples/template/dds/inc/main.h +++ b/pec930_sdk-v1.0.1/Examples/template/dds/inc/main.h @@ -44,14 +44,15 @@ extern "C" // Public Function Definition //============================================================================= -struct dds_param_t -{ - uint32_t phase_acc_base; - uint32_t set_dds_ftw; - int32_t set_dds_amp; -}; + struct dds_param_t + { + uint32_t phase_acc_base; + uint32_t set_dds_ftw; + int32_t set_dds_amp; + uint8_t notify_en; + }; -extern struct dds_param_t dds; + extern struct dds_param_t dds; #ifdef __cplusplus } diff --git a/pec930_sdk-v1.0.1/Examples/template/dds/src/init.c b/pec930_sdk-v1.0.1/Examples/template/dds/src/init.c index 26d2afc..31343b8 100644 --- a/pec930_sdk-v1.0.1/Examples/template/dds/src/init.c +++ b/pec930_sdk-v1.0.1/Examples/template/dds/src/init.c @@ -9,7 +9,6 @@ #include "init.h" #include "isr.h" - //============================================================================= // Constant Definition //============================================================================= @@ -31,11 +30,12 @@ void SYS_Config() SYSCFG_ClkInitTypeDef SysClkInit = { 0 }; SysClkInit.ClkSource = SYSCFG_ClkSrc_HSI; SYSCFG_SysClkConfig(&SysClkInit); - - // Wait for clock to stabilize after power-on reset - for(volatile uint32_t i = 0; i < 20000; i++); - + sys_config_systick(SYS_TICK_1_MS); + sys_delay(3000); + + /* Disable ICE I/O */ + SYSCFG_SetICEPin2NormalIO(true); gpio_config(); opa_config(); @@ -241,9 +241,9 @@ void uart_config() uart_init.Mode = UART_Mode_TxRx; UART_Init(UART0, &uart_init); UART_Start(UART0); - + // Wait for UART to be ready after initialization - for(volatile uint32_t i = 0; i < 10000; i++); + for (volatile uint32_t i = 0; i < 10000; i++); UART_ITConfig(UART0, UART_FLAG_RXNE, ENABLE); { /* Configure TIM interrupts */ diff --git a/pec930_sdk-v1.0.1/Examples/template/dds/src/main.c b/pec930_sdk-v1.0.1/Examples/template/dds/src/main.c index b456eac..eb711b2 100644 --- a/pec930_sdk-v1.0.1/Examples/template/dds/src/main.c +++ b/pec930_sdk-v1.0.1/Examples/template/dds/src/main.c @@ -9,6 +9,7 @@ * @license * @description */ +#include #include #include "main.h" @@ -39,8 +40,6 @@ const uint16_t pwm_table[PWM_TABLE_SIZE] 95, 53, 24, 6, 0, 6, 24, 53, 95, 147, 210, 283, 366, 457, 555, 660, 771, 887, 1006, 1127 }; - - // 64-point sine wave lookup table (-2048-2047 range) // Sine_table[i] = sin(i*2*pi/64)*(4095/2) where i = 0 to 63 // Use sine_amplitude parameter to scale to desired duty cycle range later @@ -74,18 +73,18 @@ const int16_t LUT_cos[64] struct dds_cal_param { -uint32_t phase_acc; -uint8_t lut_idx; -uint8_t nquadrant; -uint8_t last_quadrant; -int16_t lut_sin_val; -int16_t lut_cos_val; -uint16_t duty; + uint32_t phase_acc; + uint8_t lut_idx; + uint8_t nquadrant; + uint8_t last_quadrant; + int16_t lut_sin_val; + int16_t lut_cos_val; + uint16_t duty; }; -struct dds_param_t dds ={0}; +struct dds_param_t dds = { 0 }; #define PHASE_COUNT 3 -const uint32_t phase_offset[PHASE_COUNT] = { 0, 1398101, 2796203 }; +const uint32_t phase_offset[PHASE_COUNT] = { 0, 1398101, 2796203 }; static struct dds_cal_param phase[PHASE_COUNT] = { 0 }; void dds_pwm_duty_cycle_update(void) @@ -105,7 +104,8 @@ void dds_pwm_duty_cycle_update(void) p->nquadrant = p->lut_idx >> 4; p->lut_sin_val = LUT_sin[p->lut_idx]; p->lut_cos_val = LUT_cos[p->lut_idx]; - p->duty = (((int32_t)p->lut_sin_val * dds.set_dds_amp) >> 18) + 1250; // 0~2500 duty cycle range + p->duty = (((int32_t)p->lut_sin_val * dds.set_dds_amp) >> 18) + + 1250; // 0~2500 duty cycle range } pwm_set_duty(phase[0].duty, phase[1].duty, phase[2].duty); @@ -143,12 +143,15 @@ static void cmd_parser(char *data, uint32_t len) { printf("FW_VER_1.0.0_20260320\r\n"); } - else if (strncmp(data, "pwm_enable", 10) == 0) + else if (strncmp(data, "dds_init", 8) == 0) { memset(phase, 0, sizeof(phase)); dds.phase_acc_base = 0; - dds.set_dds_ftw = 1740; - dds.set_dds_amp = 160000; + dds.set_dds_ftw = 174; + dds.set_dds_amp = 0; + } + else if (strncmp(data, "pwm_enable", 10) == 0) + { pwm_enable(); printf("PWM enabled.\r\n"); } @@ -157,6 +160,49 @@ static void cmd_parser(char *data, uint32_t len) pwm_disable(); printf("PWM disabled.\r\n"); } + else if (strncmp(data, "dds_amp++", 9) == 0) + { + dds.set_dds_amp++; + printf("dds_amp++\r\n"); + } + else if (strncmp(data, "dds_amp--", 9) == 0) + { + dds.set_dds_amp--; + printf("dds_amp--\r\n"); + } + else if (strncmp(data, "dds_ftw++", 9) == 0) + { + dds.set_dds_ftw++; + printf("dds_ftw++\r\n"); + } + else if (strncmp(data, "dds_ftw--", 9) == 0) + { + dds.set_dds_ftw--; + printf("dds_ftw--\r\n"); + } + else if (strncmp(data, "dds_amp:", 8) == 0) + { + dds.set_dds_amp = atoi(data + 8); + printf("dds_amp set to %d\r\n", dds.set_dds_amp); + } + else if (strncmp(data, "dds_ftw:", 8) == 0) + { + dds.set_dds_ftw = atoi(data + 8); + printf("dds_ftw set to %d\r\n", dds.set_dds_ftw); + } + else if (strncmp(data, "notify_en:", 10) == 0) + { + dds.notify_en = atoi(data + 10); + if (dds.notify_en) + { + printf("Notification enabled.\r\n"); + } + else + { + printf("Notification disabled.\r\n"); + } + } + else { printf("Unknown command. Type 'help' for available commands.\r\n"); @@ -166,21 +212,24 @@ static void cmd_parser(char *data, uint32_t len) int main(void) { uint16_t base_idx = 0; - uint32_t timer = sys_get_tick(); + uint32_t timer; SYS_Config(); printf("This is a demo project: JF_02\n"); + #ifdef ENABLE_DDS pwm_set_duty(1250, 1250, 1250); dds.phase_acc_base = 0; - dds.set_dds_ftw = 1740; - dds.set_dds_amp = 160000; + dds.set_dds_ftw = 174; + dds.set_dds_amp = 0; pwm_enable(); #else uvw_set_duty(base_idx); pwm_enable(); #endif + timer = sys_get_tick(); + while (1) { if (stringComplete) @@ -189,16 +238,15 @@ int main(void) stringComplete = false; } - if (sys_get_tick() - timer >= 1) - { - printf("%d tick\n", sys_get_tick()); - timer = sys_get_tick(); - } - #ifdef ENABLE_DDS if (flag_24Khz_timer) { dds_pwm_duty_cycle_update(); + if (dds.notify_en) + { + printf("%d tick: %d, %d, %d\n", sys_get_tick(), phase[0].duty, + phase[1].duty, phase[2].duty); + } flag_24Khz_timer = false; } #else