13 Commits

Author SHA1 Message Date
aiden 07d5e70f84 refactor: comment out unused fields in CaptureSample_t and streamline capture logging 2026-06-16 11:55:12 +08:00
roy01 411939fbc1 refactor: add capture functionality and enhance UART command parsing 2026-06-15 11:48:39 +08:00
roy01 a7de92ee96 refactor: streamline gpio_init() and sysclk_init() 2026-06-12 10:42:14 +08:00
roy01 14c33ee8ac refactor: enhance adc_init() channel selection comments 2026-06-12 10:41:43 +08:00
roy01 e447f7290a refactor: streamline tim0_init() 2026-06-12 10:41:12 +08:00
roy01 b3df1a54dc refactor: streamline pfc_pwm_init() 2026-06-12 10:40:43 +08:00
roy01 cba70b6754 refactor: streamline llc_pwm_init() 2026-06-12 10:40:13 +08:00
roy01 7b4c689260 refactor: streamline opa_init() 2026-06-12 10:39:39 +08:00
roy01 8164d47b35 refactor: streamline adc_init() 2026-06-12 10:39:14 +08:00
roy01 1257dc34c7 fix: remove useless variables 2026-06-12 10:38:39 +08:00
roy01 70bcbe562b feat: enhance bode plot parameter handling and flash operations 2026-06-12 09:03:36 +08:00
roy01 7d23d69a15 feat: remove isr.c & isr.h 2026-06-12 09:02:40 +08:00
aiden c846ad9793 小改動 2026-06-10 11:36:25 +08:00
9 changed files with 666 additions and 770 deletions
@@ -10,7 +10,6 @@ extern "C"
{ {
#endif #endif
#include "isr.h"
#include "main.h" #include "main.h"
//============================================================================= //=============================================================================
@@ -27,6 +26,7 @@ extern "C"
/* flash*/ /* flash*/
#define FLASH_PROG_ADDR_END sizeof(EFLASH_MEM_Type) #define FLASH_PROG_ADDR_END sizeof(EFLASH_MEM_Type)
#define FLASH_PROG_ADDR_USER_CALI (FLASH_PROG_ADDR_END - FLASH_1_PAGE_SIZE) // 0x8000-512 #define FLASH_PROG_ADDR_USER_CALI (FLASH_PROG_ADDR_END - FLASH_1_PAGE_SIZE) // 0x8000-512
#define FLASH_PARAM_MAGIC 0x50464C54U
//============================================================================= //=============================================================================
// Structure Definition // Structure Definition
@@ -39,6 +39,14 @@ extern "C"
int32_t a1; int32_t a1;
int32_t a2; int32_t a2;
}; };
struct flash_bode_plot_blob_t
{
uint32_t magic;
struct bode_plot_param_t cloop;
struct bode_plot_param_t vloop;
uint32_t reserved;
};
//============================================================================= //=============================================================================
// Global Data Definition // Global Data Definition
//============================================================================= //=============================================================================
@@ -64,9 +72,11 @@ extern "C"
void uart_cmd_parse(char *cmd_buf, uint32_t size); void uart_cmd_parse(char *cmd_buf, uint32_t size);
/* flash */ /* flash */
void bode_plot_param_read_from_flash(struct bode_plot_param_t *param, uint32_t addr); int write_bodeplot_params_to_flash(const struct bode_plot_param_t *cloop, const struct bode_plot_param_t *vloop, uint32_t addr);
void read_bodeplot_params_from_flash(struct bode_plot_param_t *cloop, struct bode_plot_param_t *vloop, uint32_t addr);
extern struct bode_plot_param_t bode_plot_param; void peek_bodeplot_params_in_flash(uint32_t addr);
void print_bodeplot_params(struct bode_plot_param_t *cloop, struct bode_plot_param_t *vloop);
extern struct bode_plot_param_t pfc_cloop_bode_plot_param;
extern struct bode_plot_param_t pfc_vloop_bode_plot_param; extern struct bode_plot_param_t pfc_vloop_bode_plot_param;
#ifdef __cplusplus #ifdef __cplusplus
@@ -1,8 +1,5 @@
/* /**
* init.h * Copyright (c) 2026 Wisetop. All Rights Reserved.
*
* Created on: 2025年10月9日
* Author: jim_liao
*/ */
#ifndef INC_INIT_H_ #ifndef INC_INIT_H_
@@ -10,18 +7,14 @@
#include "main.h" #include "main.h"
void SYS_Config(); void SYS_Config(void);
void gpio_config(); void sysclk_init(void);
void opa_config(); void gpio_init(void);
void adc_config(); void opa_init(void);
//------------------------------------- void adc_init(void);
void llc_pwm_config(); // LLC void tim0_init(void);
void llc_pwm_init(void);
//------------------------------------ void pfc_pwm_init(void);
void pfc_pwm_config(); // PFC
//-----------------------------------
void pwm_brake(); void pwm_brake();
void tim0_config();
#endif /* INC_INIT_H_ */ #endif /* INC_INIT_H_ */
@@ -1,53 +0,0 @@
/**
* Copyright (c) 2024 Wei-Lun Hsu. All Rights Reserved.
*/
/** @file isr.h
*
* @author Wei-Lun Hsu
* @version 0.1
* @date 2024/09/16
* @license
* @description
*/
#ifndef __isr_H_wuraIpBA_lTJm_HvJw_sNDo_uEd5JnucTReY__
#define __isr_H_wuraIpBA_lTJm_HvJw_sNDo_uEd5JnucTReY__
#ifdef __cplusplus
extern "C"
{
#endif
#include "main.h"
//=============================================================================
// Constant Definition
//=============================================================================
//=============================================================================
// Macro Definition
//=============================================================================
//=============================================================================
// Structure Definition
//=============================================================================
//=============================================================================
// Global Data Definition
//=============================================================================
//=============================================================================
// Private Function Definition
//=============================================================================
//=============================================================================
// Public Function Definition
//=============================================================================
// void isr_tim2_handle(void);
// void isr_adc_handle(void);
// void isr_tim0_handle(void);
#ifdef __cplusplus
}
#endif
#endif
@@ -32,7 +32,7 @@ extern "C"
#define YN_MAX (PFC_DUTY_MAX << 14) #define YN_MAX (PFC_DUTY_MAX << 14)
#define YN_MIN (PFC_DUTY_MIN << 14) #define YN_MIN (PFC_DUTY_MIN << 14)
// VBUS 參數 // VBUS 參數
#define VBUS_SET 340 // unit: volt (Transformer N = 14) #define VBUS_SET 350 // unit: volt (Transformer N = 14)
#define VBUS_SET_H (VBUS_SET + 20) // burst mode in, 在過零區判斷 #define VBUS_SET_H (VBUS_SET + 20) // burst mode in, 在過零區判斷
#define VBUS_SET_L (VBUS_SET - 20) // burst mode out, 在過零區判斷 #define VBUS_SET_L (VBUS_SET - 20) // burst mode out, 在過零區判斷
// #define VBUS_SET_H2 370 // for skip mode // #define VBUS_SET_H2 370 // for skip mode
@@ -108,6 +108,10 @@ extern "C"
//============================================================================= //=============================================================================
// Public Function Definition // Public Function Definition
//============================================================================= //=============================================================================
extern uint8_t capture_continuous_mode;
extern int16_t capture_package_cnt;
extern uint16_t capture_packet_gap_ms;
void Power_On(); void Power_On();
void Power_Off(); void Power_Off();
void Key1_Scan(); void Key1_Scan();
@@ -126,6 +130,8 @@ extern "C"
void isr_adc_handle(void); void isr_adc_handle(void);
void isr_tim0_handle(void); void isr_tim0_handle(void);
void capture_start(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
@@ -8,11 +8,9 @@
#define TICK_1000_MS 1000 #define TICK_1000_MS 1000
struct bode_plot_param_t bode_plot_param; struct bode_plot_param_t pfc_cloop_bode_plot_param;
struct bode_plot_param_t pfc_vloop_bode_plot_param; struct bode_plot_param_t pfc_vloop_bode_plot_param;
static int bode_plot_param_write_to_flash(const struct bode_plot_param_t *param, uint32_t addr);
/* sys clock*/ /* sys clock*/
static void sysclk_systick_init(SYSCFG_ClkSrcTypeDef clk_src, uint32_t sys_tick_ms) static void sysclk_systick_init(SYSCFG_ClkSrcTypeDef clk_src, uint32_t sys_tick_ms)
{ {
@@ -215,46 +213,90 @@ void uart_cmd_parse(char *cmd_buf, uint32_t size)
msg("Available commands:\r\n"); msg("Available commands:\r\n");
msg(" help - show this help\r\n"); msg(" help - show this help\r\n");
msg(" version - show firmware version\r\n"); msg(" version - show firmware version\r\n");
msg(" bodeplot=B0,B1,B2,A1,A2\r\n"); msg(" cloop=B0,B1,B2,A1,A2\r\n");
msg(" bodeplot? - show current bode plot parameters\r\n"); msg(" vloop=B0,B1,B2,A1,A2\r\n");
msg(" bodeplot? - show runtime cloop & vloop bode plot parameters\r\n");
msg(" flash_bodeplot? - show raw flash content for bode plot parameters\r\n"); msg(" flash_bodeplot? - show raw flash content for bode plot parameters\r\n");
msg(" notify_en=0 or notify_en=1 or notify_en=255,n\r\n");
msg(" notify_packet_gap=GAP_MS\r\n");
} }
else if (strcmp(cmd_buf, "version") == 0) else if (strcmp(cmd_buf, "version") == 0)
{ {
msg("version: %s\r\n", "1.0.0"); msg("version: %s\r\n", "1.0.0");
} }
else if (cmd_match_with_arg(cmd_buf, "bodeplot=", &arg)) else if (cmd_match_with_arg(cmd_buf, "cloop=", &arg))
{ {
struct bode_plot_param_t *bplot_p = &bode_plot_param; struct bode_plot_param_t *cloop_p = &pfc_cloop_bode_plot_param;
if (parse_i32_csv5(arg, bplot_p)) struct bode_plot_param_t *vloop_p = &pfc_vloop_bode_plot_param;
if (parse_i32_csv5(arg, cloop_p))
{ {
bode_plot_param_write_to_flash(bplot_p, FLASH_PROG_ADDR_USER_CALI); write_bodeplot_params_to_flash(cloop_p, vloop_p, FLASH_PROG_ADDR_USER_CALI);
peek_bodeplot_params_in_flash(FLASH_PROG_ADDR_USER_CALI);
} }
else else
{ {
msg("invalid bodeplot format, use: bodeplot=111,8,5,6,77\r\n"); msg("invalid cloop format, use: cloop=111,8,5,6,77\r\n");
}
}
else if (cmd_match_with_arg(cmd_buf, "vloop=", &arg))
{
struct bode_plot_param_t *cloop_p = &pfc_cloop_bode_plot_param;
struct bode_plot_param_t *vloop_p = &pfc_vloop_bode_plot_param;
if (parse_i32_csv5(arg, vloop_p))
{
write_bodeplot_params_to_flash(cloop_p, vloop_p, FLASH_PROG_ADDR_USER_CALI);
peek_bodeplot_params_in_flash(FLASH_PROG_ADDR_USER_CALI);
}
else
{
msg("invalid vloop format, use: vloop=111,8,5,6,77\r\n");
} }
} }
else if (strcmp(cmd_buf, "bodeplot?") == 0) else if (strcmp(cmd_buf, "bodeplot?") == 0)
{ {
struct bode_plot_param_t *bplot_p = &bode_plot_param; struct bode_plot_param_t *cloop_p = &pfc_cloop_bode_plot_param;
msg("bode plot param read from flash: b0=0x%08X, b1=0x%08X, b2=0x%08X, a1=0x%08X, a2=0x%08X\r\n", struct bode_plot_param_t *vloop_p = &pfc_vloop_bode_plot_param;
bplot_p->b0, print_bodeplot_params(cloop_p, vloop_p);
bplot_p->b1,
bplot_p->b2,
bplot_p->a1,
bplot_p->a2);
msg("bode plot param read from flash: b0=%d, b1=%d, b2=%d, a1=%d, a2=%d\r\n",
bplot_p->b0,
bplot_p->b1,
bplot_p->b2,
bplot_p->a1,
bplot_p->a2);
} }
else if (strcmp(cmd_buf, "flash_bodeplot?") == 0) else if (strcmp(cmd_buf, "flash_bodeplot?") == 0)
{ {
struct bode_plot_param_t tmp_bplot; peek_bodeplot_params_in_flash(FLASH_PROG_ADDR_USER_CALI);
bode_plot_param_read_from_flash(&tmp_bplot, FLASH_PROG_ADDR_USER_CALI); }
else if (cmd_match_with_arg(cmd_buf, "notify_en=", &arg))
{
int notify_en = atoi(arg);
if (notify_en == 1)
{
capture_start();
capture_continuous_mode = 0;
}
else if (notify_en == 0)
{
capture_continuous_mode = 0;
capture_package_cnt = 0;
}
else if (notify_en == 255)
{
const char *comma = strchr(arg, ',');
if (comma)
{
capture_package_cnt = atoi(comma + 1); // 連續模式下每次捕獲 n 包數據
capture_continuous_mode = 1;
}
else
{
msg("invalid notify_en format, use: notify_en=0 or notify_en=1 or notify_en=255,n\r\n");
}
}
else
{
msg("invalid notify_en format, use: notify_en=0 or notify_en=1 or notify_en=255,n\r\n");
}
}
else if (cmd_match_with_arg(cmd_buf, "notify_packet_gap=", &arg))
{
capture_packet_gap_ms = atoi(arg) < 10 ? 10 : atoi(arg); // 設置最小間隔為10ms
} }
else else
{ {
@@ -296,59 +338,87 @@ static void dump_flash_bytes_8perline(uint32_t addr, uint32_t size)
} }
#endif #endif
static int bode_plot_param_write_to_flash(const struct bode_plot_param_t *param, uint32_t addr) int write_bodeplot_params_to_flash(const struct bode_plot_param_t *cloop, const struct bode_plot_param_t *vloop, uint32_t addr)
{ {
int rval = 0; int rval = 0;
uint32_t word_data[5] = { 0 };
if (!param) if (!cloop || !vloop)
return -1; return -1;
/* FLASH_ProgData requires 4-byte aligned length, so pad the last halfword. */ struct flash_bode_plot_blob_t blob = {
word_data[0] = param->b0; .magic = FLASH_PARAM_MAGIC,
word_data[1] = param->b1; .cloop = *cloop,
word_data[2] = param->b2; .vloop = *vloop,
word_data[3] = param->a1; .reserved = 0,
word_data[4] = param->a2; };
rval = FLASH_ErasePage(addr); rval = FLASH_ErasePage(addr);
if (rval) if (rval)
{ {
err(" ! erase plot param fail (%d)\n", rval); err(" ! erase plot/vloop param fail (%d)\n", rval);
return rval; return rval;
} }
rval = FLASH_ProgData(addr, word_data, sizeof(word_data)); rval = FLASH_ProgData(addr, (uint32_t *)&blob, sizeof(blob));
if (rval) if (rval)
{ {
err(" ! prog plot param fail (%d)\n", rval); err(" ! prog plot/vloop param fail (%d)\n", rval);
return rval; return rval;
} }
return 0; return 0;
} }
void bode_plot_param_read_from_flash(struct bode_plot_param_t *param, uint32_t addr) void peek_bodeplot_params_in_flash(uint32_t addr)
{ {
const void *flash_ptr = (const void *)addr; struct flash_bode_plot_blob_t blob = { 0 };
if (!param) memcpy(&blob, (const void *)addr, sizeof(blob));
if (blob.magic != FLASH_PARAM_MAGIC)
{
err(" ! invalid flash blob magic: 0x%08X\n", blob.magic);
return;
}
msg("flash blob content:\r\n");
msg(" magic: 0x%08X\r\n", blob.magic);
print_bodeplot_params(&blob.cloop, &blob.vloop);
}
void read_bodeplot_params_from_flash(struct bode_plot_param_t *cloop, struct bode_plot_param_t *vloop, uint32_t addr)
{
struct flash_bode_plot_blob_t blob = { 0 };
if (!cloop || !vloop)
return; return;
memcpy(param, flash_ptr, sizeof(*param)); memcpy(&blob, (const void *)addr, sizeof(blob));
msg("bode plot param read from flash: b0=0x%08X, b1=0x%08X, b2=0x%08X, a1=0x%08X, a2=0x%08X\r\n", if (blob.magic != FLASH_PARAM_MAGIC)
param->b0, {
param->b1, err(" ! invalid flash blob magic: 0x%08X\n", blob.magic);
param->b2, return;
param->a1, }
param->a2);
msg("bode plot param read from flash: b0=%d, b1=%d, b2=%d, a1=%d, a2=%d\r\n", *cloop = blob.cloop;
param->b0, *vloop = blob.vloop;
param->b1, }
param->b2,
param->a1, void print_bodeplot_params(struct bode_plot_param_t *cloop, struct bode_plot_param_t *vloop)
param->a2); {
msg("cloop: b0=%d, b1=%d, b2=%d, a1=%d, a2=%d\r\n",
cloop->b0,
cloop->b1,
cloop->b2,
cloop->a1,
cloop->a2);
msg("vloop: b0=%d, b1=%d, b2=%d, a1=%d, a2=%d\r\n\n",
vloop->b0,
vloop->b1,
vloop->b2,
vloop->a1,
vloop->a2);
} }
/* main code*/ /* main code*/
@@ -358,16 +428,37 @@ void app(void)
sysclk_systick_init(SYSCFG_ClkSrc_HSI, SYS_TICK_1_MS); sysclk_systick_init(SYSCFG_ClkSrc_HSI, SYS_TICK_1_MS);
sys_disable_ice(); // sys_disable_ice();
uart_init(); uart_init();
info("This is a app demo project\n"); info("This is a app demo project\n");
info("uart cmd demo\n"); info("uart cmd demo\n");
struct bode_plot_param_t *cloop_p = &pfc_cloop_bode_plot_param;
struct bode_plot_param_t *vloop_p = &pfc_vloop_bode_plot_param;
// cloop_p->b0 = 1021;
// cloop_p->b1 = -480;
// cloop_p->b2 = -522;
// cloop_p->a1 = -1951;
// cloop_p->a2 = -14414;
// vloop_p->b0 = 598;
// vloop_p->b1 = -64;
// vloop_p->b2 = -408;
// vloop_p->a1 = -12644;
// vloop_p->a2 = -3615;
// write plot param to flash
// msg("write plot param to flash\r\n");
// write_bodeplot_params_to_flash(cloop_p, vloop_p, FLASH_PROG_ADDR_USER_CALI);
// print_bodeplot_params(cloop_p, vloop_p);
// read plot param from flash // read plot param from flash
struct bode_plot_param_t *bplot_p = &bode_plot_param; msg("read plot param from flash\r\n");
bode_plot_param_read_from_flash(bplot_p, FLASH_PROG_ADDR_USER_CALI); read_bodeplot_params_from_flash(cloop_p, vloop_p, FLASH_PROG_ADDR_USER_CALI);
print_bodeplot_params(cloop_p, vloop_p);
while (1) while (1)
{ {
@@ -1,19 +1,14 @@
/* /**
* init.c * Copyright (c) 2026 Wisetop. All Rights Reserved.
*
* Created on: 2025年8月18日
* Author: jim_liao
*/ */
#include "init.h" #include "init.h"
#include "app.h" #include "app.h"
#include "hal_device.h" #include "hal_device.h"
#include "isr.h"
//============================================================================= //=============================================================================
// Constant Definition // Constant Definition
//============================================================================= //=============================================================================
// #define CONFIG_OPA_PGA_GAIN OPAMP_PGAGain_1 //OPAMP_PGAGain_5
//============================================================================= //=============================================================================
// Public Function Definition // Public Function Definition
@@ -62,23 +57,210 @@ _Post_Proc(PWR_ModeTypeDef pwr_mode)
return; return;
} }
#endif #endif
//======================================================================== //===================================================================================
void SYS_Config() void gpio_init(void)
{ {
SYSCFG_ClkInitTypeDef SysClkInit = { 0 }; GPIO_InitTypeDef gpio_init = { 0 };
// PA12(vfb)(vcomp)
gpio_init.GPIO_Pin = GPIO_Pin_12;
gpio_init.GPIO_Mode = GPIO_Mode_IN;
gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &gpio_init);
// PA15(button)
gpio_init.GPIO_Pin = GPIO_Pin_15;
gpio_init.GPIO_Mode = GPIO_Mode_IN;
gpio_init.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &gpio_init);
#if 0 // 取消了
// PA5(TP1)
// PA6(TP2)
gpio_init.GPIO_Pin = GPIO_Pin_05;
gpio_init.GPIO_Pin |= GPIO_Pin_06;
gpio_init.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(GPIOA, &gpio_init);
#endif
}
//===================================================================================
void opa_init(void)
{
// gpio
GPIO_InitTypeDef gpio_init = { 0 };
gpio_init.GPIO_Pin = GPIO_Pin_07 | GPIO_Pin_08; // Pin_09 is set as power_ok
gpio_init.GPIO_Mode = GPIO_Mode_ANAL;
gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &gpio_init);
// opa
OPAMP_InitTypeDef opa_init = { 0 };
opa_init.OPAMP_VinP = OPAMP_VinP_IO; // PA8
opa_init.OPAMP_VinM = OPAMP_VinM_GND; // internal GND, PIN 留給 POWER_OK
opa_init.OPAMP_Vout = OPAMP_Vout_IO; // PA7, 拉到 IO PA7,需要外部 500 ~ 1000pF 電容
opa_init.OPAMP_Gain = OPAMP_PGAGain_15; // x1~x16 as needed, 使用 15 倍, 減少 RS 功耗
OPAMP_Init(OPAMP0, &opa_init);
OPAMP_Enable(OPAMP0);
}
//===================================================================================
void adc_init(void)
{
// gpio
GPIO_InitTypeDef gpio_init;
gpio_init.GPIO_Pin = GPIO_Pin_10; // vbus = PA10(AIN4)
gpio_init.GPIO_Pin |= GPIO_Pin_07; // iac = PA7(AIN9)
gpio_init.GPIO_Pin |= GPIO_Pin_13; // vac = PA13(AIN7)
gpio_init.GPIO_Pin |= GPIO_Pin_11; // vcr = PA11(AIN5)
gpio_init.GPIO_Mode = GPIO_Mode_ANAL;
GPIO_Init(GPIOA, &gpio_init);
// adc
ADC_InitTypeDef adc_init = { 0 };
ADC_StructInit(&adc_init);
adc_init.SelChannels = ADC_Channel_04;
adc_init.SelChannels |= ADC_Channel_09; // 量測外部 PA7
// adc_init.SelChannels |= ADC_Channel_00; // 若是讀取 PGA0 輸出的 AIN0 通道, 要改成 ADC_Channel_00
adc_init.SelChannels |= ADC_Channel_07;
adc_init.SelChannels |= ADC_Channel_05;
adc_init.ClkPrescaler = ADC_ClkDiv_4; // 60M/4 = 15MHz, ADC max working clock is 16MHz
adc_init.DataAlign = ADC_DataAlign_Right;
adc_init.Mode = ADC_Mode_Scan; // Scan mode
ADC_Init(ADC0, &adc_init);
ADC_ExtTrigConfig(ADC0, ADC_ExtTrigSource_TIM0, ADC_ExtTrigMode_Enable); // tim0 trigger adc
// ADC interrupt config
sys_irq_attr_t irq_attr = {
.disable_vector = false,
.trig_mode = SYS_IRQ_TRIGGER_LEVEL,
.level = SYS_IRQ_LEVEL_H,
.priority = SYS_IRQ_PRIORITY_MIDDEN,
};
sys_register_IRQ(ADC0_IRQn, isr_adc_handle, &irq_attr);
ADC0->CON0_b.INT_EN = 1; // ADC interrupt enable
}
//===================================================================================
void llc_pwm_init(void)
{
// gpio
// PA0(EPWM0P), PA1(EPWM0N)
GPIO_InitTypeDef gpio_init = {
.GPIO_Pin = GPIO_Pin_00 | GPIO_Pin_01,
.GPIO_Mode = GPIO_Mode_AF,
.GPIO_AF_Mode = GPIO_AF_6,
};
GPIO_Init(GPIOA, &gpio_init);
// tim
TIM_TimeBaseInitTypeDef tim_init = {
.TIM_Prescaler = 0,
.TIM_CounterMode = TIM_CounterMode_Up,
.TIM_Period = LLC_PERIOD_SS,
.TIM_ClockDivision = TIM_CKD_Div1,
.TIM_RepetitionCounter = 0,
};
TIM_DeInit(EPWM);
TIM_TimeBaseInit(EPWM, &tim_init);
TIM_ARRPreloadConfig(EPWM, ENABLE);
// pwm
TIM_OCInitTypeDef pwm_init = {
.TIM_OCMode = TIM_OCMode_PWM1,
.TIM_OutputState = TIM_OutputState_Disable,
.TIM_OCPolarity = TIM_OCPolarity_High,
.TIM_OCIdleState = TIM_OCIdleState_Reset,
.TIM_OutputNState = TIM_OutputNState_Disable,
.TIM_OCNPolarity = TIM_OCPolarity_High,
.TIM_Pulse = 1,
};
TIM_OC1Init(EPWM, &pwm_init);
TIM_OC2Init(EPWM, &pwm_init);
{ /* Automatic Output enable, Break, dead time and lock configuration */
TIM_BDTRInitTypeDef bdtr_init = { 0 };
bdtr_init.TIM_LOCKLevel = TIM_LockLevel_OFF;
bdtr_init.TIM_DeadTime = DEAD_TIME_VALUE;
TIM_BDTRConfig(EPWM, &bdtr_init);
}
TIM_Cmd(EPWM, ENABLE);
TIM_CtrlPWMOutputs(EPWM, ENABLE);
EPWM->CCMR1_OUTPUT_b.OC1PE = 1; // Output Compare 1 preload enable
}
//===================================================================================
void pfc_pwm_init(void) // single PWM for PFC
{
// gpio
// PA2(TIM2_CH1)
GPIO_InitTypeDef gpio_init = {
.GPIO_Pin = GPIO_Pin_02,
.GPIO_Mode = GPIO_Mode_AF,
.GPIO_AF_Mode = GPIO_AF_5,
};
GPIO_Init(GPIOA, &gpio_init);
// tim
TIM_TimeBaseInitTypeDef tim_init = {
.TIM_Prescaler = 0,
.TIM_CounterMode = TIM_CounterMode_Up,
.TIM_Period = PFC_PERIOD,
.TIM_ClockDivision = TIM_CKD_Div1,
.TIM_RepetitionCounter = 0,
};
TIM_DeInit(TIM2);
TIM_TimeBaseInit(TIM2, &tim_init);
TIM_ARRPreloadConfig(TIM2, ENABLE);
// pwm
TIM_OCInitTypeDef pwm_init = {
.TIM_OCMode = TIM_OCMode_PWM1,
.TIM_OutputState = TIM_OutputState_Disable,
.TIM_OCPolarity = TIM_OCPolarity_High,
.TIM_OCIdleState = TIM_OCIdleState_Reset,
.TIM_Pulse = 1,
};
TIM_OC1Init(TIM2, &pwm_init);
TIM_Cmd(TIM2, ENABLE); // enable PWM
TIM_CtrlPWMOutputs(TIM2, ENABLE); // enable PWM output
TIM2->CCMR1_OUTPUT_b.OC1PE = 1; // Output Compare 1 preload enable
}
//===================================================================================
void tim0_init(void)
{
LPTIM_InitTypeDef tim0_init = {
.LPTIM_Prescaler = 0,
.LPTIM_ClockSource = LPTIM_CLK_Src_SysClk,
.LPTIM_MatchMode = LPTIM_MatchMode_IRQ | LPTIM_MatchMode_Reset,
.LPTIM_MatchValue = TIM0_PERIOD,
};
LPTIM_Init(TIM0, &tim0_init);
LPTIM_Enable(TIM0);
}
//===================================================================================
void sysclk_init(void)
{
SYSCFG_ClkInitTypeDef SysClkInit = {
.ClkSource = SYSCFG_ClkSrc_HSI,
};
SysClkInit.ClkSource = SYSCFG_SYSCLKCR_CLKSW_HSI;
SYSCFG_SysClkConfig(&SysClkInit); SYSCFG_SysClkConfig(&SysClkInit);
sys_config_systick(SYS_TICK_1_MS); sys_config_systick(SYS_TICK_1_MS);
}
//===================================================================================
void SYS_Config(void)
{
sysclk_init();
// PWR_EnterDeepSleepMode(_Pre_Proc, _Post_Proc); // use WDG wake up 1 sec // PWR_EnterDeepSleepMode(_Pre_Proc, _Post_Proc); // use WDG wake up 1 sec
gpio_init();
gpio_config(); opa_init();
opa_config(); adc_init();
adc_config(); llc_pwm_init(); // 初始化但不輸出 EPWM_CH1
llc_pwm_config(); // 初始化但不輸出 EPWM_CH1 pfc_pwm_init(); // 初始化但不輸出 TIM2_CH1
pfc_pwm_config(); // 初始化但不輸出 TIM2_CH1
__NOP(); __NOP();
__NOP(); __NOP();
@@ -86,260 +268,6 @@ void SYS_Config()
__NOP(); __NOP();
__NOP(); // timer shift between pfc and tim0 __NOP(); // timer shift between pfc and tim0
tim0_config(); // trigger ADC, current loop frequency tim0_init(); // trigger ADC, current loop frequency
uart_init(); uart_init();
} }
//===================================================================================
void gpio_config()
{
//-----------------//
// OPA
// IN+ = PA8
// IN- = PA9
// OUT = PA7
//-----------------//
GPIO_InitTypeDef GPIO_InitStruct;
#if 1
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_07 | GPIO_Pin_08; // Pin_09 is set as power_ok
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_ANAL;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStruct);
#endif
#if 1
//-----------------//
// PWM
// LLC: PA0, PA1
// PFC: PA2
//-----------------//
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_00 | GPIO_Pin_01;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_AF_Mode = GPIO_AF_6; // AF6 = EPWM
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_02;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_AF_Mode = GPIO_AF_5; // AF5 = TIM2
GPIO_Init(GPIOA, &GPIO_InitStruct);
//-----------------//
// ADC
// PA7(iac), PA10(vbus), PA13(vac) // PA11(cr), PA12(vfb)-->GPIO
//-----------------//
GPIO_InitTypeDef GPIO_InitADC;
GPIO_InitADC.GPIO_Pin = GPIO_Pin_07 | GPIO_Pin_10 | GPIO_Pin_13; // | GPIO_Pin_11; // PA11(cr)
GPIO_InitADC.GPIO_Mode = GPIO_Mode_ANAL;
GPIO_Init(GPIOA, &GPIO_InitADC);
//-----------------//
// Button
//-----------------//
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15; // PA15
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; // pull up
GPIO_Init(GPIOA, &GPIO_InitStruct);
//-----------------//
// FB (VCOMP)
//-----------------//
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12; // PA12
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; // no pull
GPIO_Init(GPIOA, &GPIO_InitStruct);
#endif
#if 0 // 取消了
//-----------------//
// test pin PA5: TP1, PA6: TP2
//-----------------//
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_05 | GPIO_Pin_06; // PA6 = TP2
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(GPIOA, &GPIO_InitStruct);
#endif
}
//===================================================================================
void opa_config(void)
{
OPAMP_InitTypeDef opa_init = { 0 };
opa_init.OPAMP_VinP = OPAMP_VinP_IO;
opa_init.OPAMP_VinM = OPAMP_VinM_GND; // 設定內部 GND PIN 留給 POWER_OK
opa_init.OPAMP_Vout = OPAMP_Vout_IO; // 拉到 IO,需要外部 500 ~ 1000pF 電容
opa_init.OPAMP_Gain = OPAMP_PGAGain_15; // 使用最大 15 倍,減少 RS 功耗
// 正向放大器 OK
// 電壓隨耦器 OK
// 反向放大器 NG
// V+ 接 1/2 VCC,差動放大器 OK
OPAMP_Init(OPAMP0, &opa_init);
OPAMP_Enable(OPAMP0);
}
//===================================================================================
void adc_config(void)
{
ADC_InitTypeDef init = { 0 };
ADC_StructInit(&init);
ADC_InitTypeDef init1 = { 0 };
ADC_StructInit(&init1);
// iac = PA7 = ADC_Channel_09
// vac = PA13 = ADC_Channel_07
// vbus = PA10 = ADC_Channel_04
// ----------------------------------------------
// vcr = PA11 = ADC_Channel_05 (未使用)
// vfb = PA12 = ADC_Channel_06 (使用 GPIO)
init1.SelChannels = ADC_Channel_04 | ADC_Channel_07 | ADC_Channel_09; // | ADC_Channel_05;
// init1.SelChannels = ADC_Channel_09 ;// OP_O 指定到 PA7 時,ADC 需要用 CH_09 讀取
init1.ClkPrescaler = ADC_ClkDiv_4; // 60M/4 = 15M,接近 1MSPS (1us 轉換時間), 最快速度
init1.DataAlign = ADC_DataAlign_Right;
init1.Mode = ADC_Mode_Scan; // Scan mode
ADC_Init(ADC0, &init1);
// ADC_ExtTrigConfig(ADC0, ADC_ExtTrigSource_EPWM_CH2R, ADC_ExtTrigMode_Enable);
ADC_ExtTrigConfig(ADC0, ADC_ExtTrigSource_16, ADC_ExtTrigMode_Enable); // use tim0 trigger
#if 1
{ // Configure TIM interrupts
sys_irq_attr_t irq_attr = {
.disable_vector = false,
};
irq_attr.trig_mode = SYS_IRQ_TRIGGER_LEVEL;
irq_attr.level = SYS_IRQ_LEVEL_H;
irq_attr.priority = SYS_IRQ_PRIORITY_MIDDEN;
sys_register_IRQ(ADC0_IRQn, isr_adc_handle, &irq_attr);
ADC0->CON0_b.INT_EN = 1;
}
#endif
}
//===================================================================================
void llc_pwm_config() // complimentary PWM for LLC
{
TIM_OCInitTypeDef TIM_OCInitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
/*
{ // Configure TIM interrupts
sys_irq_attr_t irq_attr = { .disable_vector = false, };
irq_attr.trig_mode = SYS_IRQ_TRIGGER_LEVEL;
irq_attr.level = SYS_IRQ_LEVEL_H;
irq_attr.priority = SYS_IRQ_PRIORITY_MIDDEN;
sys_register_IRQ(EPWM_IRQn, isr_epwm_handle, &irq_attr);
}
*/
TIM_DeInit(EPWM);
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
TIM_TimeBaseInitStruct.TIM_Prescaler = 0;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period = LLC_PERIOD_SS;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_Div1;
TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(EPWM, &TIM_TimeBaseInitStruct);
TIM_ARRPreloadConfig(EPWM, ENABLE);
TIM_OCStructInit(&TIM_OCInitStruct);
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Disable;
TIM_OCInitStruct.TIM_Pulse = 0;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStruct.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStruct.TIM_Pulse = 1;
TIM_OCInitStruct.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStruct.TIM_OCNPolarity = TIM_OCPolarity_High;
TIM_OC1Init(EPWM, &TIM_OCInitStruct);
TIM_OCInitStruct.TIM_Pulse = 1;
TIM_OC2Init(EPWM, &TIM_OCInitStruct);
{ /* Automatic Output enable, Break, dead time and lock configuration */
TIM_BDTRInitTypeDef bdtr_init = { 0 };
bdtr_init.TIM_LOCKLevel = TIM_LockLevel_OFF;
bdtr_init.TIM_DeadTime = DEAD_TIME_VALUE;
TIM_BDTRConfig(EPWM, &bdtr_init);
}
// EPWM->DIER |= 0x1000; // Underflow interrupt enable
//__enable_irq();
TIM_Cmd(EPWM, ENABLE);
TIM_CtrlPWMOutputs(EPWM, ENABLE);
EPWM->CCMR1_OUTPUT_b.OC1PE = 1; // Output Compare 1 preload enable
// EPWM->CCR1 = 300; // for test
// REG_SET_BITS(EPWM->CCER, TIM_CCER_CC1E_Msk | TIM_CCER_CC1NE_Msk); // 需要 SET 才會輸出
}
//===================================================================================
void pfc_pwm_config() // single PWM for PFC
{
TIM_OCInitTypeDef TIM_OCInitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
/*
{ // Configure TIM interrupts
sys_irq_attr_t irq_attr = { .disable_vector = false, };
irq_attr.trig_mode = SYS_IRQ_TRIGGER_LEVEL;
irq_attr.level = SYS_IRQ_LEVEL_H;
irq_attr.priority = SYS_IRQ_PRIORITY_MIDDEN;
sys_register_IRQ(TIM2_IRQn, isr_tim2_handle, &irq_attr);
}
*/
TIM_DeInit(TIM2);
TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
TIM_TimeBaseInitStruct.TIM_Prescaler = 0;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period = PFC_PERIOD;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_Div1;
TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_OCStructInit(&TIM_OCInitStruct);
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Disable;
TIM_OCInitStruct.TIM_Pulse = 0;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStruct.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStruct.TIM_Pulse = 1;
// TIM_OCInitStruct.TIM_OutputNState = TIM_OutputNState_Disable; // no negative output
// TIM_OCInitStruct.TIM_OCNPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &TIM_OCInitStruct);
// TIM2->DIER |= 0x1000; // Underflow interrupt enable
//__enable_irq();
TIM_Cmd(TIM2, ENABLE); // enable PWM
TIM_CtrlPWMOutputs(TIM2, ENABLE); // enable PWM output
TIM2->CCMR1_OUTPUT_b.OC1PE = 1; // Output Compare 1 preload enable
// TIM2->CCR1 = 200; // for test
// REG_SET_BITS(TIM2->CCER, TIM_CCER_CC1E_Msk); // set bit enable output
}
//===================================================================================
void tim0_config()
{
LPTIM_InitTypeDef Init = { 0 };
Init.LPTIM_Prescaler = 0;
Init.LPTIM_ClockSource = LPTIM_CLK_Src_SysClk;
Init.LPTIM_MatchMode = LPTIM_MatchMode_IRQ | LPTIM_MatchMode_Reset; // | LPTIM_MatchMode_Stop;
Init.LPTIM_MatchValue = TIM0_PERIOD;
LPTIM_Init(TIM0, &Init);
#if 0
sys_irq_attr_t irq_attr = { .trig_mode = SYS_IRQ_TRIGGER_LEVEL, };
sys_register_IRQ(TIM0_IRQn, isr_tim0_handle, &irq_attr);
sys_enable_girq();
LPTIM_ITConfig(TIM0, true);
#endif
LPTIM_Enable(TIM0);
}
@@ -1,69 +0,0 @@
/**
* Copyright (c) 2024 Wei-Lun Hsu. All Rights Reserved.
*/
/** @file isr.c
*
* @author Wei-Lun Hsu
* @version 0.1
* @date 2024/09/16
* @license
* @description
*/
#include "isr.h"
#include "init.h"
//=============================================================================
// Constant Definition
//=============================================================================
//=============================================================================
// Macro Definition
//=============================================================================
//=============================================================================
// Global Data Definition
//=============================================================================
// uint8_t volatile tim0_trigger = 0;
// extern uint16_t iac;
// extern uint16_t vac;
// extern uint16_t vbus;
// extern uint16_t vac_avg1, vac_avg2;
// extern uint16_t vbus_avg;
// extern uint8_t hi_line;
//=============================================================================
// Private Function Definition
//=============================================================================
//=============================================================================
// Public Function Definition
//=============================================================================
/*
__INTERRUPT void isr_tim2_handle(void)
{
if((TIM2->SR & 0x20000) == 0x20000) // UDIF: Underflow interrupt flag
{
TIM_ClearITPendingBit(TIM2, 0x20000);
}
}
*/
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
#if 0
__INTERRUPT void isr_tim0_handle(void)
{
SAVE_IRQ_CSR_CONTEXT();
if( LPTIM_GetFlagStatus(TIM0, LPTIM_Flag_Match0) )
{
LPTIM_ClearFlag(TIM0, LPTIM_Flag_Match0);
tim0_trigger = 1;
GPIO_TogglePin(GPIOA, GPIO_Pin_05);
}
RESTORE_IRQ_CSR_CONTEXT();
return;
}
#endif
@@ -2,22 +2,20 @@
#include "main.h" #include "main.h"
#include "app.h" #include "app.h"
#include "init.h" #include "init.h"
#include "isr.h"
//============================================================================= //=============================================================================
// Constant Definition // Constant Definition
//============================================================================= //=============================================================================
#define DATA_LOG 0 #define CAPTURE_BUF_SIZE 160
#define DATA_MONITOR 0
#define DEBOUNCE_TIME 20
#define BUF_SIZE 300 // > 300 記憶體不夠用(4組) > 700 記憶體不夠用(2組)
#define CR_1P1Z_B0_Q14 6313 #define DEBOUNCE_TIME 20
#define CR_1P1Z_B1_Q14 -5113
#define CR_1P1Z_A1_Q14 -5803 #define CR_1P1Z_B0_Q14 6313
#define CR_1P1Z_B1_Q14 -5113
#define CR_1P1Z_A1_Q14 -5803
// 前饋輸出限制(請根據您主環 Q14 格式的數值範圍調整) // 前饋輸出限制(請根據您主環 Q14 格式的數值範圍調整)
// 假設最大前饋量限制為實數值 50.0 -> 50.0 * 16384 = 819200 // 假設最大前饋量限制為實數值 50.0 -> 50.0 * 16384 = 819200
#define CR_FF_MAX_Q14 819200 // 50 duty << 14 #define CR_FF_MAX_Q14 1638400 // 819200 // 50 duty << 14
#define CR_FF_MIN_Q14 -819200 #define CR_FF_MIN_Q14 -1638400 //-819200
//============================================================================= //=============================================================================
// Macro Definition // Macro Definition
@@ -45,26 +43,36 @@ typedef enum
STATE_PAUSE // 暫停,測試用 STATE_PAUSE // 暫停,測試用
} SystemState_t; } SystemState_t;
typedef struct
{
// uint32_t timestamp;
uint16_t iac;
// uint16_t vac;
// uint16_t vbus;
// uint16_t vcr;
} CaptureSample_t;
//============================================================================= //=============================================================================
// Global Data Definition // Global Data Definition
//============================================================================= //=============================================================================
// #define kp 250 /* PID 轉換 2P2Z係數*/
// #define ki 2 // #define kp 250
// #define kd 0 // #define ki 2
// #define kd 0
// #define ff_max 8000 // 2P2Z 係數(這些需要根據你的 PID 增益與控制頻率重新計算或調試)
// int32_t b0 = kp + ki + kd; // 對應比例 + 部分積分/微分貢獻(可調整)
// #define v_kp 10 // int32_t b1 = -kp - kd - kd; // e[k-1] 係數(通常為正或負)
#define v_ki 10 // int32_t b2 = kd; // e[k-2] 係數
#define v_kd 0 // int32_t a1 = 128; // -A1(因為通常寫成 u[k] = ... + A1*u[k-1] + A2*u[k-2],這裡 a1 = 128 代表 A1 ≈ 1
// int32_t a2 = 0; // -A2PID 特例時 a2 常為 0
SystemState_t sys_state = STATE_INIT; SystemState_t sys_state = STATE_INIT;
uint8_t is_zero_crossing = 0; uint8_t is_zero_crossing = 0;
uint8_t burst_mode_active = 0; static uint8_t last_is_zero_crossing = 0;
uint8_t llc_running = 0; uint8_t llc_running = 0;
uint8_t pfc_running = 0; uint8_t pfc_running = 0;
static uint8_t last_is_zero_crossing = 0;
uint8_t is_brown_in = 0; // 0: 關機/保護中, 1: 正常工作 uint8_t is_brown_in = 0; // 0: 關機/保護中, 1: 正常工作
static uint8_t cur_loop_fun_en = 0; static uint8_t cur_loop_fun_en = 0;
@@ -86,23 +94,9 @@ uint32_t vbus_ovp = (VBUS_OVP * 4096 / VBUS_2ADC / 3.3);
uint32_t vbus_set = (VBUS_SET * 4096 / VBUS_2ADC / 3.3); uint32_t vbus_set = (VBUS_SET * 4096 / VBUS_2ADC / 3.3);
uint32_t vbus_set_h = (VBUS_SET_H * 4096 / VBUS_2ADC / 3.3); // burst mode in uint32_t vbus_set_h = (VBUS_SET_H * 4096 / VBUS_2ADC / 3.3); // burst mode in
uint32_t vbus_set_l = (VBUS_SET_L * 4096 / VBUS_2ADC / 3.3); // burst mode out uint32_t vbus_set_l = (VBUS_SET_L * 4096 / VBUS_2ADC / 3.3); // burst mode out
// uint32_t vbus_set_h2 = (VBUS_SET_H2 * 4096 / VBUS_2ADC / 3.3);
// uint32_t digi_vcomp_max = (uint32_t)(VBUS_SET_H * 4096 / VBUS_2ADC / 3.3) << DIGI_COMP_BIT;
uint32_t digi_vcomp_max = (LLC_PERIOD_MAX - LLC_PERIOD_MIN) << LLC_COMP_GAIN_BIT; uint32_t digi_vcomp_max = (LLC_PERIOD_MAX - LLC_PERIOD_MIN) << LLC_COMP_GAIN_BIT;
int32_t vbus_err = 0;
int32_t vbus_err_prev = 0;
int32_t vbus_comp = 0;
int32_t vbus_comp2 = 0; // 過零點用 int32_t vbus_comp2 = 0; // 過零點用
int32_t vbus_comp_prev = 0;
int32_t vbus_int = 0, vbus_por = 0;
uint16_t pfc_duty = 0;
uint8_t pfc_ss = 0;
uint16_t pfc_ss_cnt = 0;
uint16_t llc_ss_cnt = 0; uint16_t llc_ss_cnt = 0;
// volatile int16_t iac_err_0, iac_err_1;
// volatile uint16_t iac_set;
// volatile int32_t iac_int, iac_por, iac_der;
// volatile int16_t iac_comp = 0;
//----------------------------------------------- //-----------------------------------------------
KeyState_t key1State = KEY_IDLE; KeyState_t key1State = KEY_IDLE;
uint32_t key1Timer = 0; uint32_t key1Timer = 0;
@@ -110,95 +104,54 @@ uint8_t key1_action = 0;
uint8_t keyInput; uint8_t keyInput;
//----------------------------------------------- //-----------------------------------------------
uint8_t power_on_cmd = 0; uint8_t power_on_cmd = 0;
uint8_t temp_off = 0; uint32_t vac_avg1;
uint16_t vbus_volt, vac_volt;
int32_t tx_buf1[BUF_SIZE];
int32_t tx_buf2[BUF_SIZE];
// uint16_t tx_buf3[BUF_SIZE];
// uint16_t tx_buf4[BUF_SIZE];
uint16_t buf_idx = 0;
uint8_t is_full = 0;
uint16_t count_down = 2000; // for data log
uint16_t i_ff;
uint32_t vac_avg1, vac_avg2, vac_avg3;
uint32_t vbus_avg = 0; uint32_t vbus_avg = 0;
uint32_t vcr_avg; uint32_t vcr_avg;
static volatile uint16_t vac_pk1, vac_pk2; static volatile uint16_t vac_pk1, vac_pk2;
uint8_t hi_line = 0; // 0=110V, 1=220V
uint8_t vbus_ready = 0;
uint16_t hi_cnt, lo_cnt;
uint32_t static digital_vcomp = (LLC_PERIOD_MAX - LLC_PERIOD_MIN) << LLC_COMP_GAIN_BIT; uint32_t static digital_vcomp = (LLC_PERIOD_MAX - LLC_PERIOD_MIN) << LLC_COMP_GAIN_BIT;
int16_t fb_enent;
uint16_t llc_period_ss = LLC_SS_MIN; uint16_t llc_period_ss = LLC_SS_MIN;
uint16_t llc_run_arr = LLC_PERIOD_MIN; uint16_t llc_run_arr = LLC_PERIOD_MIN;
uint16_t llc_arr_cmp = 0;
uint8_t v_loop_cnt = 0; uint8_t v_loop_cnt = 0;
uint8_t v_loop_cnt2 = 0; uint8_t v_loop_cnt2 = 0;
uint16_t brownout_cnt = 0;
uint8_t volatile tim0_trigger = 0; uint8_t volatile tim0_trigger = 0;
// 全局或靜態變數(需在函數外或 static 保留狀態) // 全局或靜態變數(需在函數外或 static 保留狀態)
static int32_t iac_err_0 = 0; // e[k]
static int32_t iac_err_1 = 0; // e[k-1] static int32_t iac_err_1 = 0; // e[k-1]
static int32_t iac_err_2 = 0; // e[k-2] static int32_t iac_err_2 = 0; // e[k-2]
static int32_t iac_comp = 0; // u[k]
static int32_t iac_comp_1 = 0; // u[k-1] (前一次輸出) static int32_t iac_comp_1 = 0; // u[k-1] (前一次輸出)
static int32_t iac_comp_2 = 0; // u[k-2] (前兩次輸出) static int32_t iac_comp_2 = 0; // u[k-2] (前兩次輸出)
static int32_t vbus_err_1 = 0; static int32_t vbus_err_1 = 0;
static int32_t vbus_comp_1 = 0; static int32_t vbus_comp_1 = 0;
// 2P2Z 係數(這些需要根據你的 PID 增益與控制頻率重新計算或調試)
// int32_t b0 = kp + ki + kd; // 對應比例 + 部分積分/微分貢獻(可調整)
// int32_t b1 = -kp - kd - kd; // e[k-1] 係數(通常為正或負)
// int32_t b2 = kd; // e[k-2] 係數
// int32_t a1 = 128; // -A1(因為通常寫成 u[k] = ... + A1*u[k-1] + A2*u[k-2],這裡 a1 = 128 代表 A1 ≈ 1
// int32_t a2 = 0; // -A2PID 特例時 a2 常為 0
// 電壓環 2p2z 係數
// int32_t v_b0 = v_kp + v_ki + v_kd;
// int32_t v_b1 = -v_kp - v_kd - v_kd;
// int32_t v_b2 = v_kd;
// int32_t v_a1 = 128;
// int32_t v_a2 = 0;
uint16_t updata_cnt = 0; uint16_t updata_cnt = 0;
int32_t vbus_comp_prev1, vbus_comp_prev2; int32_t vbus_comp_prev1, vbus_comp_prev2;
int32_t vbus_err_prev1, vbus_err_prev2; int32_t vbus_err_prev1, vbus_err_prev2;
//-----------------------------------------------
static volatile uint8_t capture_active = 0;
static volatile uint8_t capture_ready = 0;
static volatile uint16_t capture_idx = 0;
static CaptureSample_t capture_buf[CAPTURE_BUF_SIZE];
uint8_t capture_continuous_mode = 0;
int16_t capture_package_cnt = 5;
uint16_t capture_packet_gap_ms = 1000;
//============================================================================= //=============================================================================
// Private Function Definition // Private Function Definition
//============================================================================= //=============================================================================
void A_Para_Reset() void A_Para_Reset()
{ {
vbus_int = 0;
vbus_comp = 1000;
// iac_int = 0; iac_comp = 1000;
digital_vcomp = (LLC_PERIOD_MAX - LLC_PERIOD_MIN) << LLC_COMP_GAIN_BIT; digital_vcomp = (LLC_PERIOD_MAX - LLC_PERIOD_MIN) << LLC_COMP_GAIN_BIT;
vbus_set = (VBUS_SET * 4096 / VBUS_2ADC / 3.3); vbus_set = (VBUS_SET * 4096 / VBUS_2ADC / 3.3);
pfc_ss = 0;
pfc_ss_cnt = 0;
v_loop_cnt = 0; v_loop_cnt = 0;
temp_off = 0;
iac_err_0 = 0;
iac_err_1 = 0; iac_err_1 = 0;
iac_err_2 = 0; iac_err_2 = 0;
iac_comp_1 = 0; iac_comp_1 = 0;
iac_comp_2 = 0; iac_comp_2 = 0;
vbus_err = 0;
vbus_err_prev = 0;
vbus_err_prev1 = 0;
vbus_err_prev2 = 0;
vbus_comp_prev = 0;
vbus_comp_prev1 = 0;
vbus_comp_prev2 = 0;
// vbus_comp2 = 1000;
// set default value // set default value
TIM2->ARR = PFC_PERIOD; TIM2->ARR = PFC_PERIOD;
TIM2->CCR1 = PFC_DUTY_MIN; // min for ss TIM2->CCR1 = PFC_DUTY_MIN; // min for ss
@@ -210,7 +163,6 @@ void A_Para_Reset()
pfc_running = 0; pfc_running = 0;
llc_running = 0; llc_running = 0;
brownout_cnt = 0;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void Key1_Scan() void Key1_Scan()
@@ -253,6 +205,60 @@ void Key1_Scan()
} }
} }
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
void capture_start(void)
{
capture_idx = 0;
capture_ready = 0;
capture_active = 1;
msg("capture start,size=%d\r\n", CAPTURE_BUF_SIZE);
}
//-------------------------------------------------------------------------
static inline void Capture_Sample_ISR(void)
{
if (!capture_active)
return;
uint16_t idx = capture_idx;
if (idx >= CAPTURE_BUF_SIZE)
return;
// capture_buf[idx].timestamp = sys_get_tick();
capture_buf[idx].iac = iac;
// capture_buf[idx].vac = vac;
// capture_buf[idx].vbus = vbus;
// capture_buf[idx].vcr = vcr;
idx++;
capture_idx = idx;
if (idx >= CAPTURE_BUF_SIZE)
{
capture_active = 0;
capture_ready = 1;
}
}
//-------------------------------------------------------------------------
static void Capture_Dump_IfReady(void)
{
if (!capture_ready)
return;
for (uint16_t i = 0; i < CAPTURE_BUF_SIZE; i++)
{
msg("%d\r\n", capture_buf[i].iac);
// msg("%d,%d,%d,%d,%d\r\n",
// capture_buf[i].timestamp,
// capture_buf[i].iac,
// capture_buf[i].vac,
// capture_buf[i].vbus,
// capture_buf[i].vcr);
}
capture_ready = 0;
msg("capture done,count=%d\r\n", CAPTURE_BUF_SIZE);
}
//-------------------------------------------------------------------------
void Key_Action() void Key_Action()
{ {
if (power_on_cmd == 0) if (power_on_cmd == 0)
@@ -328,7 +334,7 @@ void Load_Loop_1P1Z(void)
int32_t y0; int32_t y0;
// 1. 讀取當前 vcr // 1. 讀取當前 vcr
x0 = vcr; x0 = vcr_avg;
// 2. 執行 1P1Z 差分方程計算: // 2. 執行 1P1Z 差分方程計算:
temp_y0 = ((int64_t)CR_1P1Z_B0_Q14 * x0) + ((int64_t)CR_1P1Z_B1_Q14 * vcr_x1_q14) - ((int64_t)CR_1P1Z_A1_Q14 * vcr_y1_q14); temp_y0 = ((int64_t)CR_1P1Z_B0_Q14 * x0) + ((int64_t)CR_1P1Z_B1_Q14 * vcr_x1_q14) - ((int64_t)CR_1P1Z_A1_Q14 * vcr_y1_q14);
@@ -342,32 +348,35 @@ void Load_Loop_1P1Z(void)
// 定點數安全限幅 (Saturation) // 定點數安全限幅 (Saturation)
if (y0 > CR_FF_MAX_Q14) if (y0 > CR_FF_MAX_Q14)
y0 = CR_FF_MAX_Q14; // 868352 = 53 duty y0 = CR_FF_MAX_Q14;
else if (y0 > -CR_FF_MIN_Q14) else if (y0 > -CR_FF_MIN_Q14)
y0 = -CR_FF_MIN_Q14; y0 = -CR_FF_MIN_Q14;
cr_feed_forward_output_q14 = y0; cr_feed_forward_output_q14 = y0;
} }
//========================================================================== //==========================================================================
void Vol_Loop_PFC_1P1Z(void) // TEST OK void Vol_Loop_PFC_1P1Z(void)
{ {
// 1. 計算誤差 // 1. 計算誤差
int32_t vbus_err_n = (int32_t)vbus_set - (int32_t)vbus_avg; int32_t vbus_err_n = (int32_t)vbus_set - (int32_t)vbus_avg;
// --- 策略 A:動態增益調整 --- // --- 策略 A:動態增益調整 ---
// int32_t b0 = 135; int32_t b0 = 139;
// int32_t b1 = -134; int32_t b1 = -129;
int32_t b0 = 180;
int32_t b1 = -175;
// if (vbus_err_n > 30 || vbus_err_n < -30) // vloop_p->b0 = 139;
if (vbus_err_n > 80 || vbus_err_n < -80) // ~ 20V // vloop_p->b1 = -129;
// vloop_p->a1 = -16383;
/*
if (vbus_err_n > 40 || vbus_err_n < -40) // ~ 20V
{ {
// 大誤差時(通常是剛啟動或大跳載),強化參數加快反應 // 大誤差時(通常是剛啟動或大跳載),強化參數加快反應
b0 = 250; //b0 = 250;
b1 = -240; //b1 = -249;
// b0 = 180; // 稍微調降強增益的力道 //b0 = 180; // 稍微調降強增益的力道
// b1 = -175; //b1 = -178;
b0 = 500;
b1 = -499;
} }
// --- 策略 B:接近目標時微調零點 (抗過衝) --- // --- 策略 B:接近目標時微調零點 (抗過衝) ---
else if (vbus_err_n < 20 && vbus_err_n > -20) // ~ 5V else if (vbus_err_n < 20 && vbus_err_n > -20) // ~ 5V
@@ -376,14 +385,35 @@ void Vol_Loop_PFC_1P1Z(void) // TEST OK
b0 = 120; b0 = 120;
b1 = -119; // 如果不能用浮點數,請維持 120 / -119,增加阻尼感 b1 = -119; // 如果不能用浮點數,請維持 120 / -119,增加阻尼感
} }
*/
// 2. 差分方程計算 // 2. 差分方程計算
int64_t acc = (int64_t)b0 * vbus_err_n; int64_t acc = (int64_t)b0 * vbus_err_n;
acc += (int64_t)b1 * vbus_err_1; acc += (int64_t)b1 * vbus_err_1;
acc += ((int64_t)16384 * vbus_comp_1) >> 14; // 16384 = 1 >> 14 acc += ((int64_t)16383 * vbus_comp_1) >> 14; // 16384 = 1 >> 14
int32_t y_n = (int32_t)acc; int32_t y_n = (int32_t)acc;
#if 0 // 有問題
// --- 策略 C:軟限制抗過衝 (Soft Clamping) ---
// 預設最大限制 (Duty 300 << 14)
int32_t current_max_limit = 4915200; // duty 300
// 如果電壓誤差已經很小(例如剩 15V 就到達),甚至已經發生過衝 (err < 0)
// 我們強制壓低 Duty 的允許上限,防止積分器帶著大 Duty 衝過頭
if (vbus_err_n < 15)
{
// 這裡的 3276800 對應 Duty 200,您可以根據 800W 穩態時的 Duty 大約位置來設定
// 核心目標是:不讓 Duty 在接近目標時還維持在 300 這麼高
current_max_limit = 3276800; // duty 200
}
// 如果發生明顯過衝 (電壓高於目標 5V 以上)
if (vbus_err_n < -5)
{
current_max_limit = 1638400; // 強制壓低到 Duty 100 快速拉回
}
#endif
// 3. 輸出限幅執行 // 3. 輸出限幅執行
if (y_n > YN_MAX) if (y_n > YN_MAX)
y_n = YN_MAX; y_n = YN_MAX;
@@ -395,12 +425,13 @@ void Vol_Loop_PFC_1P1Z(void) // TEST OK
vbus_comp_1 = y_n; vbus_comp_1 = y_n;
// 5. 輸出 Duty >> 14 // 5. 輸出 Duty >> 14
// if (sys_state == STATE_PFC_SOFT_START || sys_state == STATE_RUN_SKIP) // 緩啟動期間直接控制 DUTY if (sys_state == STATE_PFC_SOFT_START || sys_state == STATE_RUN_SKIP) // 緩啟動期間直接控制 DUTY
TIM2->CCR1 = (vbus_comp_1 >> 14); // 0 ~ 540 TIM2->CCR1 = (vbus_comp_1 >> 14); // 0 ~ 540
// 6. 輸出 comp >> 9 (變大 32 倍) (MAX ~ 18000) (540*32=17280)差不多,>> 9 應該是對的 // 6. 輸出 comp >> 9 (變大 32 倍) (MAX ~ 18000) (540*32=17280)差不多,>> 9 應該是對的
// if (sys_state == STATE_RUN_NORMAL) // 正常工作時間輸出 COMP 值 // if (sys_state == STATE_RUN_NORMAL) // 正常工作時間輸出 COMP 值
vbus_comp2 = (vbus_comp_1 >> 9); vbus_comp2 = (vbus_comp_1 >> 9) + (cr_feed_forward_output_q14 << 1); // 太大會直接 OVP
// vbus_comp2 = (vbus_comp_1 >> 9);
} }
//============================================================= //=============================================================
static void Vol_Loop_PFC_1P1Z_UI(void) // ZERO: 120HZ, POLE:0.1HZ, G:10 static void Vol_Loop_PFC_1P1Z_UI(void) // ZERO: 120HZ, POLE:0.1HZ, G:10
@@ -430,8 +461,8 @@ static void Vol_Loop_PFC_1P1Z_UI(void) // ZERO: 120HZ, POLE:0.1HZ, G:10
vbus_comp_1 = y_n; vbus_comp_1 = y_n;
// 5. 輸出 Duty >> 14 // 5. 輸出 Duty >> 14
// if (sys_state == STATE_PFC_SOFT_START || sys_state == STATE_RUN_SKIP) // 緩啟動期間直接控制 DUTY if (sys_state == STATE_PFC_SOFT_START || sys_state == STATE_RUN_SKIP) // 緩啟動期間直接控制 DUTY
TIM2->CCR1 = (vbus_comp_1 >> 14); // 0 ~ 540 TIM2->CCR1 = (vbus_comp_1 >> 14); // 0 ~ 540
// 6. 輸出 comp >> 9 (變大 32 倍) (MAX ~ 18000) (540*32=17280)差不多,>> 9 應該是對的 // 6. 輸出 comp >> 9 (變大 32 倍) (MAX ~ 18000) (540*32=17280)差不多,>> 9 應該是對的
// if (sys_state == STATE_RUN_NORMAL) // 正常工作時間輸出 COMP 值 // if (sys_state == STATE_RUN_NORMAL) // 正常工作時間輸出 COMP 值
@@ -461,11 +492,12 @@ __attribute__((always_inline)) static inline void Cur_Loop_PFC_2P2Z(void) // 優
// 2. 2P2Z 運算 (Fixed-point Q14) // 2. 2P2Z 運算 (Fixed-point Q14)
#if 1 #if 1
struct bode_plot_param_t *bplot_p = &bode_plot_param; struct bode_plot_param_t *cloop_p = &pfc_cloop_bode_plot_param;
int64_t acc1 = ((int64_t)bplot_p->b0 * local_iac_err_0) + ((int64_t)bplot_p->b1 * iac_err_1) + ((int64_t)bplot_p->b2 * iac_err_2);
int64_t acc2 = -((int64_t)bplot_p->a1 * iac_comp_1); int64_t acc1 = ((int64_t)cloop_p->b0 * local_iac_err_0) + ((int64_t)cloop_p->b1 * iac_err_1) + ((int64_t)cloop_p->b2 * iac_err_2);
acc2 -= ((int64_t)bplot_p->a2 * iac_comp_2);
int64_t acc2 = -((int64_t)cloop_p->a1 * iac_comp_1);
acc2 -= ((int64_t)cloop_p->a2 * iac_comp_2);
#endif #endif
acc1 += acc2; acc1 += acc2;
@@ -503,28 +535,6 @@ __attribute__((always_inline)) static inline void Cur_Loop_PFC_2P2Z(void) // 優
else if (local_duty < PFC_DUTY_MIN) else if (local_duty < PFC_DUTY_MIN)
local_duty = PFC_DUTY_MIN; // MIN = 12 local_duty = PFC_DUTY_MIN; // MIN = 12
TIM2->CCR1 = local_duty; TIM2->CCR1 = local_duty;
// -------------------------------------------
// 5. data log
#if DATA_LOG
count_down--;
if (count_down < 1000)
{
tx_buf1[buf_idx] = local_iac_err_0; // y[n]
tx_buf2[buf_idx] = acc; // x[n]
// tx_buf3[buf_idx] = local_iac_err_0;
// tx_buf4[buf_idx] = local_duty;
buf_idx++;
}
if (buf_idx >= (BUF_SIZE - 1))
{
is_full = 1;
pfc_pwm_disable();
sys_state = STATE_PAUSE;
}
#endif
// -------------------------------------------
} }
//============================================================================ //============================================================================
@@ -607,10 +617,17 @@ __INTERRUPT void isr_adc_handle(void)
iac = ADC0->DAT9_b.DATA; // PA7, AIN9 iac = ADC0->DAT9_b.DATA; // PA7, AIN9
vac = ADC0->DAT7_b.DATA; // PA13, AIN7 vac = ADC0->DAT7_b.DATA; // PA13, AIN7
vbus = ADC0->DAT4_b.DATA; // PA10, AIN4 vbus = ADC0->DAT4_b.DATA; // PA10, AIN4
// vcr = ADC0->DAT5_b.DATA; // PA11, AIN5 vcr = ADC0->DAT5_b.DATA; // PA11, AIN5
if (cur_loop_fun_en) Cur_Loop_PFC_2P2Z(); // current loop if (cur_loop_fun_en) Cur_Loop_PFC_2P2Z(); // current loop
static uint8_t cnt = 0;
if (cnt++ >= 10)
{
Capture_Sample_ISR();
cnt = 0;
}
// GPIO_ResetBits(GPIOA, GPIO_Pin_06); // GPIO_ResetBits(GPIOA, GPIO_Pin_06);
RESTORE_IRQ_CSR_CONTEXT(); RESTORE_IRQ_CSR_CONTEXT();
@@ -619,45 +636,13 @@ __INTERRUPT void isr_adc_handle(void)
//============================================================================ //============================================================================
void reset_pid_parameters(void) void reset_pid_parameters(void)
{ {
vbus_int = 0;
vbus_comp = 1000;
iac_err_0 = 0;
iac_err_1 = 0; iac_err_1 = 0;
iac_err_2 = 0; iac_err_2 = 0;
iac_comp = 0;
iac_comp_1 = 0; iac_comp_1 = 0;
iac_comp_2 = 0; iac_comp_2 = 0;
digital_vcomp = (LLC_PERIOD_MAX - LLC_PERIOD_MIN) << LLC_COMP_GAIN_BIT; digital_vcomp = (LLC_PERIOD_MAX - LLC_PERIOD_MIN) << LLC_COMP_GAIN_BIT;
} }
//==============================================================================
void Handle_UART_Monitor(void)
{
static uint32_t msg_timer = 0;
if (sys_get_tick() - msg_timer >= 1000) // msg_out = 1, print
{
// printf("%d %d %d\r\n", vac_pk1, vac_pk2, is_brown_in);
// printf("%d %d %d %d %d %d", power_on_cmd, sys_state, vbus_comp2, print1, print2, print3);
msg("%d %d %d", power_on_cmd, sys_state, vbus_comp2);
// 電壓環輸出最大 COMP,但是電流環輸出最小 DUTY
msg_timer = sys_get_tick();
}
// iac 零點大約 3640 ~ 3680
}
//================================================================================
void Handle_UART_Log(void)
{
for (int i = 0; i < BUF_SIZE; i++)
{
// printf("%d:%d:%d:%d\r\n", tx_buf1[i], tx_buf2[i], tx_buf3[i], tx_buf4[i]);
printf("%d:%d\r\n", tx_buf1[i], tx_buf2[i]);
// printf("%d\r\n", tx_buf1[i]);
sys_delay(5);
}
is_full = 0; // [重要] 列印完務必清除旗標,否則會死循環
buf_idx = 0; // 重置索引
}
//================================================================================ //================================================================================
void Vac_Peak_Detector(void) void Vac_Peak_Detector(void)
{ {
@@ -718,54 +703,64 @@ static void uart_rx_task(void)
msg("RX: %s\r\n", cmd_buf); msg("RX: %s\r\n", cmd_buf);
uart_cmd_parse(cmd_buf, sizeof(cmd_buf)); uart_cmd_parse(cmd_buf, sizeof(cmd_buf));
} }
static void capture_continuous_task(void)
{
static uint32_t msg_timer = 0;
if (capture_continuous_mode == 1 && capture_package_cnt > 0)
{
if (sys_get_tick() - msg_timer >= capture_packet_gap_ms)
{
capture_start();
msg_timer = sys_get_tick();
if (capture_package_cnt-- <= 0)
{
capture_continuous_mode = 0;
capture_package_cnt = 0;
}
}
}
}
//============================================================================= //=============================================================================
// Public Function Definition // Public Function Definition
//============================================================================= //=============================================================================
int main(void) int main(void)
{ {
struct bode_plot_param_t *bplot_p = &bode_plot_param; struct bode_plot_param_t *cloop_p = &pfc_cloop_bode_plot_param;
struct bode_plot_param_t *vloop_p = &pfc_vloop_bode_plot_param; struct bode_plot_param_t *vloop_p = &pfc_vloop_bode_plot_param;
// 基礎硬體底層設定 (HIRC 60MHz, GPIO, ADC 採樣率 1Msps) [cite: 55, 127, 184] // 基礎硬體底層設定 (HIRC 60MHz, GPIO, ADC 採樣率 1Msps) [cite: 55, 127, 184]
SYS_Config(); SYS_Config();
// PFC 電流環參數 // read plot param from flash
bode_plot_param_read_from_flash(bplot_p, FLASH_PROG_ADDR_USER_CALI); msg("read plot param from flash\r\n");
read_bodeplot_params_from_flash(cloop_p, vloop_p, FLASH_PROG_ADDR_USER_CALI);
/*********************************************************************/ print_bodeplot_params(cloop_p, vloop_p);
/* PFC 電壓環參數*/
// vloop_p->a2 = 0;
// vloop_p->b2 = 0;
// // 120HZ, 0.1Hz, 100G
// vloop_p->b0 = 139;
// vloop_p->b1 = -129;
// vloop_p->a1 = -16383;
/*********************************************************************/
/* PFC 電流環參數*/
// bplot_p->b0 = 12368803;
// bplot_p->b1 = -19162610;
// bplot_p->b2 = 7037429;
// bplot_p->a1 = -20921;
// bplot_p->a2 = 4545;
/*********************************************************************/
#if 0 // 單測 PFC 電壓環 (OK) #if 0 // 單測 PFC 電壓環 (OK)
/* PFC 電壓環參數*/
{
vloop_p->a2 = 0;
vloop_p->b2 = 0;
// 120HZ, 0.1Hz, 100G // Not used
vloop_p->b0 = 139; cloop_p->b0 = 0;
vloop_p->b1 = -129; cloop_p->b1 = 0;
vloop_p->a1 = -16383; cloop_p->b2 = 0;
} cloop_p->a1 = 0;
cloop_p->a2 = 0;
// 1P1Z 參數: Z=120HZ, P=0.1HZ, G=100 ?
vloop_p->b0 = 139;
vloop_p->b1 = -129;
vloop_p->b2 = 0;
vloop_p->a1 = -16383;
vloop_p->a2 = 0;
print_bodeplot_params(cloop_p, vloop_p);
cur_loop_fun_en = 0; // 關閉電流環,專測電壓環 cur_loop_fun_en = 0; // 關閉電流環,專測電壓環
sys_state = STATE_PFC_SOFT_START;
while (1) while (1)
{ {
uart_rx_task(); //uart_rx_task();
if (tim0_trigger == 1) // 100KHZ if (tim0_trigger == 1) // 100KHZ
{ {
@@ -774,7 +769,7 @@ int main(void)
vac_avg1 = (vac_avg1 - (vac_avg1 >> 3)) + (vac >> 3); // for current loop vac_avg1 = (vac_avg1 - (vac_avg1 >> 3)) + (vac >> 3); // for current loop
vbus_avg = (vbus_avg - (vbus_avg >> 4)) + (vbus >> 4); // for voltage loop vbus_avg = (vbus_avg - (vbus_avg >> 4)) + (vbus >> 4); // for voltage loop
vcr_avg = (vcr_avg - (vcr_avg >> 3)) + (vcr >> 3); vcr_avg = (vcr_avg - (vcr_avg >> 3)) + (vcr >> 3);
if (vbus > vbus_ovp) Power_Off(); // ovp if (vbus_avg > vbus_ovp) Power_Off(); // ovp
Vac_Peak_Detector(); Vac_Peak_Detector();
@@ -788,8 +783,8 @@ int main(void)
if (++v_loop_cnt >= 10) // 10KHZ if (++v_loop_cnt >= 10) // 10KHZ
{ {
Vol_Loop_PFC_1P1Z_UI(); //Vol_Loop_PFC_1P1Z_UI();
// Vol_Loop_PFC_1P1Z(); Vol_Loop_PFC_1P1Z(); // 800W 板子會低 13V
v_loop_cnt = 0; v_loop_cnt = 0;
} }
} }
@@ -797,29 +792,30 @@ int main(void)
Key1_Scan(); Key1_Scan();
// Handle_UART_Monitor();
if (is_full == 1)
{
sys_delay(10);
Handle_UART_Log();
}
} }
#endif #endif
#if 0 // 單測 PFC 電流環 (OK) #if 0 // 單測 PFC 電流環 (OK)
/* PFC 電流環參數*/
// Z1=200, Z2=1000000, P1=10, P2=20000, G=10000 // 2P2Z 參數: Z1=200, Z2=100000, P1=10, P2=20000, G=10000, fs=100K, bQ14, aQ14
bplot_p->b0 = 4192101; cloop_p->b0 = 3353681;
bplot_p->b1 = -1972040; cloop_p->b1 = -1577632;
bplot_p->b2 = -2140640; cloop_p->b2 = -1712512;
bplot_p->a1 = -20114; cloop_p->a1 = -20114;
bplot_p->a2 = 3737; cloop_p->a2 = 3737;
// Not used
vloop_p->b0 = 0;
vloop_p->b1 = 0;
vloop_p->b2 = 0;
vloop_p->a1 = 0;
vloop_p->a2 = 0;
print_bodeplot_params(cloop_p, vloop_p);
cur_loop_fun_en = 1; // 開啟電流環,專測電流環 cur_loop_fun_en = 1; // 開啟電流環,專測電流環
while (1) while (1)
{ {
uart_rx_task(); //uart_rx_task();
if (tim0_trigger == 1) // 100KHZ if (tim0_trigger == 1) // 100KHZ
{ {
tim0_trigger = 0; tim0_trigger = 0;
@@ -841,7 +837,7 @@ int main(void)
// vbus_comp_1 = (10 << 14); // 假設從一個極小的係數開始 // vbus_comp_1 = (10 << 14); // 假設從一個極小的係數開始
vbus_err_1 = 0; // 清除誤差歷史 vbus_err_1 = 0; // 清除誤差歷史
// vbus_comp2 = (vbus_comp_1 >> 9); // 讓電流環拿到的初始值是 10 // vbus_comp2 = (vbus_comp_1 >> 9); // 讓電流環拿到的初始值是 10
vbus_comp2 = 3000; vbus_comp2 = 4000; // 2000 不足,2500 過壓
// vset / 5700 // vset / 5700
// vbus_comp2: 3000, 275V / 0.25A = 68W // vbus_comp2: 3000, 275V / 0.25A = 68W
// vbus_comp2: 4000, 314V / 0.25A = 78W // vbus_comp2: 4000, 314V / 0.25A = 78W
@@ -869,97 +865,94 @@ int main(void)
#endif #endif
#if 0 // 測試 PFC 電壓環 + 電流環 (OK) #if 0 // 測試 PFC 電壓環 + 電流環 (OK)
TIM2->CCR1 = 12; // min duty TIM2->CCR1 = 12; // min duty
sys_state = STATE_PFC_SOFT_START; sys_state = STATE_PFC_SOFT_START;
cur_loop_fun_en = 0; // 一開始關閉電流環,專測電流環 cur_loop_fun_en = 0; // 一開始關閉電流環,STATE_RUN_NORMAL再開電流環
{
vloop_p->a2 = 0;
vloop_p->b2 = 0;
// 120HZ, 0.1Hz, 100G // 2P2Z 參數: Z1=200, Z2=100000, P1=10, P2=20000, G=10000, fs=100K, bQ14, aQ14
vloop_p->b0 = 139; cloop_p->b0 = 3353681;
vloop_p->b1 = -129; cloop_p->b1 = -1577632;
vloop_p->a1 = -16383; cloop_p->b2 = -1712512;
} cloop_p->a1 = -20114;
{ cloop_p->a2 = 3737;
bplot_p->b0 = 4192101;
bplot_p->b1 = -1972040;
bplot_p->b2 = -2140640;
bplot_p->a1 = -20114;
bplot_p->a2 = 3737;
}
while(1) // 1P1Z 參數: Z=120HZ, P=0.1HZ, G=100 ?
vloop_p->b0 = 139;
vloop_p->b1 = -129;
vloop_p->b2 = 0;
vloop_p->a1 = -16383;
vloop_p->a2 = 0;
while (1)
{
if (tim0_trigger == 1) // 100KHZ
{ {
if (tim0_trigger == 1) // 100KHZ tim0_trigger = 0;
{ // --- 1. 高速信號濾波 (簡易位移算法減少算力消耗) ---
tim0_trigger = 0;
// --- 1. 高速信號濾波 (簡易位移算法減少算力消耗) ---
vac_avg1 = (vac_avg1 - (vac_avg1 >> 3)) + (vac >> 3); // for current loop vac_avg1 = (vac_avg1 - (vac_avg1 >> 3)) + (vac >> 3); // for current loop
vbus_avg = (vbus_avg - (vbus_avg >> 4)) + (vbus >> 4); // for voltage loop vbus_avg = (vbus_avg - (vbus_avg >> 4)) + (vbus >> 4); // for voltage loop
if (vbus > vbus_ovp) Power_Off(); // ovp if (vbus > vbus_ovp) Power_Off(); // ovp
Vac_Peak_Detector(); Vac_Peak_Detector();
if (power_on_cmd == 1) if (power_on_cmd == 1)
{ {
if (sys_state == STATE_PFC_SOFT_START) if (sys_state == STATE_PFC_SOFT_START)
{ {
if (pfc_running == 0) { if (pfc_running == 0) {
pfc_pwm_enable(); pfc_pwm_enable();
pfc_running = 1; pfc_running = 1;
} }
if (++v_loop_cnt >= 10) // 10KHZ if (++v_loop_cnt >= 10) // 10KHZ
{ {
//Vol_Loop_PFC_1P1Z_UI(); Vol_Loop_PFC_1P1Z_UI();
Vol_Loop_PFC_1P1Z(); //Vol_Loop_PFC_1P1Z();
v_loop_cnt = 0; v_loop_cnt = 0;
} }
if (vbus_avg > vbus_set_l && is_zero_crossing == 1) if (vbus_avg > vbus_set_l && is_zero_crossing == 1)
{ {
sys_state = STATE_RUN_NORMAL; sys_state = STATE_RUN_NORMAL;
// --- 關鍵修正:重新初始化電壓環內部狀態 --- // --- 關鍵修正:重新初始化電壓環內部狀態 ---
// 給予一個極小的初始 COMP 值,讓它從輕載慢慢往上爬 // 給予一個極小的初始 COMP 值,讓它從輕載慢慢往上爬
vbus_comp_1 = (10 << 14); // 假設從一個極小的係數開始 vbus_comp_1 = (10 << 14); // 假設從一個極小的係數開始
vbus_err_1 = 0; // 清除誤差歷史 vbus_err_1 = 0; // 清除誤差歷史
vbus_comp2 = (vbus_comp_1 >> 9); // 讓電流環拿到的初始值是 10 vbus_comp2 = (vbus_comp_1 >> 9); // 讓電流環拿到的初始值是 10
cur_loop_fun_en = 1; // 開啟電流環 cur_loop_fun_en = 1; // 開啟電流環
} }
} }
else if (sys_state == STATE_RUN_NORMAL) else if (sys_state == STATE_RUN_NORMAL)
{ {
if (pfc_running == 0) { if (pfc_running == 0) {
pfc_pwm_enable(); pfc_pwm_enable();
pfc_running = 1; pfc_running = 1;
} }
if (++v_loop_cnt >= 10) // 10KHZ if (++v_loop_cnt >= 10) // 10KHZ
{ {
//Vol_Loop_PFC_1P1Z_UI(); Vol_Loop_PFC_1P1Z_UI();
Vol_Loop_PFC_1P1Z(); //Vol_Loop_PFC_1P1Z();
v_loop_cnt = 0; v_loop_cnt = 0;
} }
} }
} }
}
Key1_Scan();
//Handle_UART_Monitor();
} }
Key1_Scan();
}
#endif #endif
// main loop // main loop
/* PFC 電流環參數*/ /* PFC 電流環參數*/
// Z1=200, Z2=100000, P1=10, P2=20000, G=8000 // Z1=200, Z2=100000, P1=10, P2=20000, G=8000
// bplot_p->b0 = 3353681; cloop_p->b0 = 3353681;
// bplot_p->b1 = -1577632; cloop_p->b1 = -1577632;
// bplot_p->b2 = -1712512; cloop_p->b2 = -1712512;
// bplot_p->a1 = -20114; cloop_p->a1 = -20114;
// bplot_p->a2 = 3737; cloop_p->a2 = 3737;
cur_loop_fun_en = 1; // 開啟電流環,專測電流環 cur_loop_fun_en = 1; // 開啟電流環,專測電流環
while (1) while (1)
@@ -977,26 +970,27 @@ int main(void)
vcr_avg = (vcr_avg - (vcr_avg >> 3)) + (vcr >> 3); vcr_avg = (vcr_avg - (vcr_avg >> 3)) + (vcr >> 3);
// 系統安全監控 (軟體第二道防護) // 系統安全監控 (軟體第二道防護)
// vbus ovp // vbus ovp
if (vbus > vbus_ovp) if (vbus_avg > vbus_ovp)
{ {
sys_state = STATE_FAULT; sys_state = STATE_FAULT;
Power_Off(); Power_Off();
} }
#if 0
// vac over voltage // vac over voltage
if (vac_pk2 > 2600) if (vac_pk2 > 2600)
{ {
sys_state = STATE_FAULT; sys_state = STATE_FAULT;
Power_Off(); Power_Off();
} }
#endif
#if 0
// vac brown out // vac brown out
if (vac_pk2 < VOLT_BROWN_OUT && pfc_running == 1) if (vac_pk2 < VOLT_BROWN_OUT && pfc_running == 1)
{ {
sys_state = STATE_FAULT; sys_state = STATE_FAULT;
Power_Off(); Power_Off();
} }
#endif
Vac_Peak_Detector(); // VAC 峰值偵測 Vac_Peak_Detector(); // VAC 峰值偵測
#if 1 #if 1
@@ -1030,11 +1024,12 @@ int main(void)
if (++v_loop_cnt >= 10) // 10KHZ if (++v_loop_cnt >= 10) // 10KHZ
{ {
Vol_Loop_PFC_1P1Z_UI(); Vol_Loop_PFC_1P1Z();
// Vol_Loop_PFC_1P1Z();
v_loop_cnt = 0; v_loop_cnt = 0;
} }
if (vbus_avg > vbus_set_l) if (vbus_avg > vbus_set_h)
{ {
sys_state = STATE_LLC_SOFT_START; sys_state = STATE_LLC_SOFT_START;
llc_period_ss = LLC_SS_MIN; llc_period_ss = LLC_SS_MIN;
@@ -1089,13 +1084,13 @@ int main(void)
if (++v_loop_cnt >= 10) if (++v_loop_cnt >= 10)
{ {
// 先算前饋,再算主環,最後疊加 // 先算前饋,再算主環,最後疊加
// Load_Loop_1P1Z(); // Cr 前饋 Load_Loop_1P1Z(); // Cr 前饋
Vol_Loop_PFC_1P1Z(); // PFC 電壓環 Vol_Loop_PFC_1P1Z(); // PFC 電壓環
// Vol_Loop_LLC(); // LLC 電壓環 // Vol_Loop_LLC(); // LLC 電壓環
v_loop_cnt = 0; v_loop_cnt = 0;
} }
if (++v_loop_cnt2 >= 15) if (++v_loop_cnt2 >= 5) // 好一點
{ {
Vol_Loop_LLC(); // LLC 電壓環 Vol_Loop_LLC(); // LLC 電壓環
v_loop_cnt2 = 0; v_loop_cnt2 = 0;
@@ -1124,14 +1119,9 @@ int main(void)
// --- 3. 非即時任務 (UI、通訊、Data Log) --- // --- 3. 非即時任務 (UI、通訊、Data Log) ---
Key1_Scan(); Key1_Scan();
#if DATA_LOG Capture_Dump_IfReady();
if (is_full == 1) Handle_UART_Log(); // 利用 PEC930 UART 輸出數據 [cite: 105, 336]
#endif
#if DATA_MONITOR
Handle_UART_Monitor();
#endif
capture_continuous_task();
} // while -------------------------------------------------------------------- } // while --------------------------------------------------------------------
return 0; return 0;
@@ -1,5 +1,5 @@
adapter speed 5000 adapter speed 1000
reset_config srst_only reset_config srst_only
adapter srst pulse_width 100 adapter srst pulse_width 100