12 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
8 changed files with 613 additions and 759 deletions
@@ -10,7 +10,6 @@ extern "C"
{
#endif
#include "isr.h"
#include "main.h"
//=============================================================================
@@ -27,6 +26,7 @@ extern "C"
/* flash*/
#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_PARAM_MAGIC 0x50464C54U
//=============================================================================
// Structure Definition
@@ -39,6 +39,14 @@ extern "C"
int32_t a1;
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
//=============================================================================
@@ -64,9 +72,11 @@ extern "C"
void uart_cmd_parse(char *cmd_buf, uint32_t size);
/* flash */
void bode_plot_param_read_from_flash(struct bode_plot_param_t *param, uint32_t addr);
extern struct bode_plot_param_t bode_plot_param;
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);
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;
#ifdef __cplusplus
@@ -1,8 +1,5 @@
/*
* init.h
*
* Created on: 2025年10月9日
* Author: jim_liao
/**
* Copyright (c) 2026 Wisetop. All Rights Reserved.
*/
#ifndef INC_INIT_H_
@@ -10,18 +7,14 @@
#include "main.h"
void SYS_Config();
void gpio_config();
void opa_config();
void adc_config();
//-------------------------------------
void llc_pwm_config(); // LLC
//------------------------------------
void pfc_pwm_config(); // PFC
//-----------------------------------
void SYS_Config(void);
void sysclk_init(void);
void gpio_init(void);
void opa_init(void);
void adc_init(void);
void tim0_init(void);
void llc_pwm_init(void);
void pfc_pwm_init(void);
void pwm_brake();
void tim0_config();
#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
@@ -108,6 +108,10 @@ extern "C"
//=============================================================================
// 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_Off();
void Key1_Scan();
@@ -126,6 +130,8 @@ extern "C"
void isr_adc_handle(void);
void isr_tim0_handle(void);
void capture_start(void);
#ifdef __cplusplus
}
#endif
@@ -8,11 +8,9 @@
#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;
static int bode_plot_param_write_to_flash(const struct bode_plot_param_t *param, uint32_t addr);
/* sys clock*/
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(" help - show this help\r\n");
msg(" version - show firmware version\r\n");
msg(" bodeplot=B0,B1,B2,A1,A2\r\n");
msg(" bodeplot? - show current bode plot parameters\r\n");
msg(" cloop=B0,B1,B2,A1,A2\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(" 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)
{
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;
if (parse_i32_csv5(arg, bplot_p))
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, 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
{
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)
{
struct bode_plot_param_t *bplot_p = &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",
bplot_p->b0,
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);
struct bode_plot_param_t *cloop_p = &pfc_cloop_bode_plot_param;
struct bode_plot_param_t *vloop_p = &pfc_vloop_bode_plot_param;
print_bodeplot_params(cloop_p, vloop_p);
}
else if (strcmp(cmd_buf, "flash_bodeplot?") == 0)
{
struct bode_plot_param_t tmp_bplot;
bode_plot_param_read_from_flash(&tmp_bplot, FLASH_PROG_ADDR_USER_CALI);
peek_bodeplot_params_in_flash(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
{
@@ -296,59 +338,87 @@ static void dump_flash_bytes_8perline(uint32_t addr, uint32_t size)
}
#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;
uint32_t word_data[5] = { 0 };
if (!param)
if (!cloop || !vloop)
return -1;
/* FLASH_ProgData requires 4-byte aligned length, so pad the last halfword. */
word_data[0] = param->b0;
word_data[1] = param->b1;
word_data[2] = param->b2;
word_data[3] = param->a1;
word_data[4] = param->a2;
struct flash_bode_plot_blob_t blob = {
.magic = FLASH_PARAM_MAGIC,
.cloop = *cloop,
.vloop = *vloop,
.reserved = 0,
};
rval = FLASH_ErasePage(addr);
if (rval)
{
err(" ! erase plot param fail (%d)\n", rval);
err(" ! erase plot/vloop param fail (%d)\n", rval);
return rval;
}
rval = FLASH_ProgData(addr, word_data, sizeof(word_data));
rval = FLASH_ProgData(addr, (uint32_t *)&blob, sizeof(blob));
if (rval)
{
err(" ! prog plot param fail (%d)\n", rval);
err(" ! prog plot/vloop param fail (%d)\n", rval);
return rval;
}
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;
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",
param->b0,
param->b1,
param->b2,
param->a1,
param->a2);
msg("bode plot param read from flash: b0=%d, b1=%d, b2=%d, a1=%d, a2=%d\r\n",
param->b0,
param->b1,
param->b2,
param->a1,
param->a2);
if (blob.magic != FLASH_PARAM_MAGIC)
{
err(" ! invalid flash blob magic: 0x%08X\n", blob.magic);
return;
}
*cloop = blob.cloop;
*vloop = blob.vloop;
}
void print_bodeplot_params(struct bode_plot_param_t *cloop, struct bode_plot_param_t *vloop)
{
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*/
@@ -358,16 +428,37 @@ void app(void)
sysclk_systick_init(SYSCFG_ClkSrc_HSI, SYS_TICK_1_MS);
sys_disable_ice();
// sys_disable_ice();
uart_init();
info("This is a app demo project\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
struct bode_plot_param_t *bplot_p = &bode_plot_param;
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);
while (1)
{
@@ -1,19 +1,14 @@
/*
* init.c
*
* Created on: 2025年8月18日
* Author: jim_liao
/**
* Copyright (c) 2026 Wisetop. All Rights Reserved.
*/
#include "init.h"
#include "app.h"
#include "hal_device.h"
#include "isr.h"
//=============================================================================
// Constant Definition
//=============================================================================
// #define CONFIG_OPA_PGA_GAIN OPAMP_PGAGain_1 //OPAMP_PGAGain_5
//=============================================================================
// Public Function Definition
@@ -62,23 +57,210 @@ _Post_Proc(PWR_ModeTypeDef pwr_mode)
return;
}
#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);
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
gpio_config();
opa_config();
adc_config();
llc_pwm_config(); // 初始化但不輸出 EPWM_CH1
pfc_pwm_config(); // 初始化但不輸出 TIM2_CH1
gpio_init();
opa_init();
adc_init();
llc_pwm_init(); // 初始化但不輸出 EPWM_CH1
pfc_pwm_init(); // 初始化但不輸出 TIM2_CH1
__NOP();
__NOP();
@@ -86,260 +268,6 @@ void SYS_Config()
__NOP();
__NOP(); // timer shift between pfc and tim0
tim0_config(); // trigger ADC, current loop frequency
tim0_init(); // trigger ADC, current loop frequency
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 "app.h"
#include "init.h"
#include "isr.h"
//=============================================================================
// Constant Definition
//=============================================================================
#define DATA_LOG 0
#define DATA_MONITOR 0
#define DEBOUNCE_TIME 20
#define BUF_SIZE 300 // > 300 記憶體不夠用(4組) > 700 記憶體不夠用(2組)
#define CAPTURE_BUF_SIZE 160
#define CR_1P1Z_B0_Q14 6313
#define CR_1P1Z_B1_Q14 -5113
#define CR_1P1Z_A1_Q14 -5803
#define DEBOUNCE_TIME 20
#define CR_1P1Z_B0_Q14 6313
#define CR_1P1Z_B1_Q14 -5113
#define CR_1P1Z_A1_Q14 -5803
// 前饋輸出限制(請根據您主環 Q14 格式的數值範圍調整)
// 假設最大前饋量限制為實數值 50.0 -> 50.0 * 16384 = 819200
#define CR_FF_MAX_Q14 1638400 //819200 // 50 duty << 14
#define CR_FF_MIN_Q14 -1638400 //-819200
#define CR_FF_MAX_Q14 1638400 // 819200 // 50 duty << 14
#define CR_FF_MIN_Q14 -1638400 //-819200
//=============================================================================
// Macro Definition
@@ -45,26 +43,36 @@ typedef enum
STATE_PAUSE // 暫停,測試用
} SystemState_t;
typedef struct
{
// uint32_t timestamp;
uint16_t iac;
// uint16_t vac;
// uint16_t vbus;
// uint16_t vcr;
} CaptureSample_t;
//=============================================================================
// Global Data Definition
//=============================================================================
// #define kp 250
// #define ki 2
// #define kd 0
/* PID 轉換 2P2Z係數*/
// #define kp 250
// #define ki 2
// #define kd 0
// #define ff_max 8000
// #define v_kp 10
#define v_ki 10
#define v_kd 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
SystemState_t sys_state = STATE_INIT;
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 pfc_running = 0;
static uint8_t last_is_zero_crossing = 0;
uint8_t is_brown_in = 0; // 0: 關機/保護中, 1: 正常工作
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_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_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;
int32_t vbus_err = 0;
int32_t vbus_err_prev = 0;
int32_t vbus_comp = 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;
// 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;
uint32_t key1Timer = 0;
@@ -110,95 +104,54 @@ uint8_t key1_action = 0;
uint8_t keyInput;
//-----------------------------------------------
uint8_t power_on_cmd = 0;
uint8_t temp_off = 0;
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 vac_avg1;
uint32_t vbus_avg = 0;
uint32_t vcr_avg;
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;
int16_t fb_enent;
uint16_t llc_period_ss = LLC_SS_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_cnt2 = 0;
uint16_t brownout_cnt = 0;
uint8_t volatile tim0_trigger = 0;
// 全局或靜態變數(需在函數外或 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_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_2 = 0; // u[k-2] (前兩次輸出)
static int32_t vbus_err_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;
int32_t vbus_comp_prev1, vbus_comp_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
//=============================================================================
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;
vbus_set = (VBUS_SET * 4096 / VBUS_2ADC / 3.3);
pfc_ss = 0;
pfc_ss_cnt = 0;
v_loop_cnt = 0;
temp_off = 0;
iac_err_0 = 0;
iac_err_1 = 0;
iac_err_2 = 0;
iac_comp_1 = 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
TIM2->ARR = PFC_PERIOD;
TIM2->CCR1 = PFC_DUTY_MIN; // min for ss
@@ -210,7 +163,6 @@ void A_Para_Reset()
pfc_running = 0;
llc_running = 0;
brownout_cnt = 0;
}
//------------------------------------------------------------------------------
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()
{
if (power_on_cmd == 0)
@@ -355,22 +361,22 @@ void Vol_Loop_PFC_1P1Z(void)
int32_t vbus_err_n = (int32_t)vbus_set - (int32_t)vbus_avg;
// --- 策略 A:動態增益調整 ---
int32_t b0 = 139;
int32_t b1 = -129;
int32_t b0 = 139;
int32_t b1 = -129;
// vloop_p->b0 = 139;
// vloop_p->b1 = -129;
// vloop_p->a1 = -16383;
/*
if (vbus_err_n > 40 || vbus_err_n < -40) // ~ 20V
/*
if (vbus_err_n > 40 || vbus_err_n < -40) // ~ 20V
{
// 大誤差時(通常是剛啟動或大跳載),強化參數加快反應
//b0 = 250;
//b1 = -249;
//b0 = 180; // 稍微調降強增益的力道
//b1 = -178;
b0 = 500;
b1 = -499;
//b0 = 250;
//b1 = -249;
//b0 = 180; // 稍微調降強增益的力道
//b1 = -178;
b0 = 500;
b1 = -499;
}
// --- 策略 B:接近目標時微調零點 (抗過衝) ---
else if (vbus_err_n < 20 && vbus_err_n > -20) // ~ 5V
@@ -379,7 +385,7 @@ void Vol_Loop_PFC_1P1Z(void)
b0 = 120;
b1 = -119; // 如果不能用浮點數,請維持 120 / -119,增加阻尼感
}
*/
*/
// 2. 差分方程計算
int64_t acc = (int64_t)b0 * vbus_err_n;
acc += (int64_t)b1 * vbus_err_1;
@@ -420,12 +426,12 @@ void Vol_Loop_PFC_1P1Z(void)
// 5. 輸出 Duty >> 14
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 應該是對的
// if (sys_state == STATE_RUN_NORMAL) // 正常工作時間輸出 COMP 值
vbus_comp2 = (vbus_comp_1 >> 9) + (cr_feed_forward_output_q14 << 1); // 太大會直接 OVP
//vbus_comp2 = (vbus_comp_1 >> 9);
// vbus_comp2 = (vbus_comp_1 >> 9);
}
//=============================================================
static void Vol_Loop_PFC_1P1Z_UI(void) // ZERO: 120HZ, POLE:0.1HZ, G:10
@@ -456,7 +462,7 @@ static void Vol_Loop_PFC_1P1Z_UI(void) // ZERO: 120HZ, POLE:0.1HZ, G:10
// 5. 輸出 Duty >> 14
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 應該是對的
// if (sys_state == STATE_RUN_NORMAL) // 正常工作時間輸出 COMP 值
@@ -486,11 +492,12 @@ __attribute__((always_inline)) static inline void Cur_Loop_PFC_2P2Z(void) // 優
// 2. 2P2Z 運算 (Fixed-point Q14)
#if 1
struct bode_plot_param_t *bplot_p = &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);
struct bode_plot_param_t *cloop_p = &pfc_cloop_bode_plot_param;
int64_t acc2 = -((int64_t)bplot_p->a1 * iac_comp_1);
acc2 -= ((int64_t)bplot_p->a2 * iac_comp_2);
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);
int64_t acc2 = -((int64_t)cloop_p->a1 * iac_comp_1);
acc2 -= ((int64_t)cloop_p->a2 * iac_comp_2);
#endif
acc1 += acc2;
@@ -528,28 +535,6 @@ __attribute__((always_inline)) static inline void Cur_Loop_PFC_2P2Z(void) // 優
else if (local_duty < PFC_DUTY_MIN)
local_duty = PFC_DUTY_MIN; // MIN = 12
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
// -------------------------------------------
}
//============================================================================
@@ -636,6 +621,13 @@ __INTERRUPT void isr_adc_handle(void)
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);
RESTORE_IRQ_CSR_CONTEXT();
@@ -644,46 +636,13 @@ __INTERRUPT void isr_adc_handle(void)
//============================================================================
void reset_pid_parameters(void)
{
vbus_int = 0;
vbus_comp = 1000;
iac_err_0 = 0;
iac_err_1 = 0;
iac_err_2 = 0;
iac_comp = 0;
iac_comp_1 = 0;
iac_comp_2 = 0;
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\r\n", vac);
// 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, vac_pk2);
//msg("%d %d %d", iac, vac, vbus);
// 電壓環輸出最大 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)
{
@@ -744,64 +703,57 @@ static void uart_rx_task(void)
msg("RX: %s\r\n", 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
//=============================================================================
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;
// 基礎硬體底層設定 (HIRC 60MHz, GPIO, ADC 採樣率 1Msps) [cite: 55, 127, 184]
SYS_Config();
//pfc_set_duty(15);
//pfc_pwm_enable();
//while(1);
//llc_set_period(600);
//llc_pwm_enable();
//while(1);
/*
while(1)
{
Handle_UART_Monitor();
}
*/
// PFC 電流環參數
bode_plot_param_read_from_flash(bplot_p, FLASH_PROG_ADDR_USER_CALI);
/*********************************************************************/
/* 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 = 3353681;
//bplot_p->b1 = -1577632;
//bplot_p->b2 = -1712512;
//bplot_p->a1 = -20921;
//bplot_p->a2 = 4545;
/*********************************************************************/
// read plot param from flash
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);
#if 0 // 單測 PFC 電壓環 (OK)
/* 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;
}
// Not used
cloop_p->b0 = 0;
cloop_p->b1 = 0;
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; // 關閉電流環,專測電壓環
sys_state = STATE_PFC_SOFT_START;
@@ -840,19 +792,25 @@ int main(void)
Key1_Scan();
//Handle_UART_Monitor();
}
#endif
#if 0 // 單測 PFC 電流環 (OK)
/* PFC 電流環參數*/
// Z1=200, Z2=1000000, P1=10, P2=20000, G=10000
bplot_p->b0 = 3353681;
bplot_p->b1 = -1577632;
bplot_p->b2 = -1712512;
bplot_p->a1 = -20114;
bplot_p->a2 = 3737;
// 2P2Z 參數: Z1=200, Z2=100000, P1=10, P2=20000, G=10000, fs=100K, bQ14, aQ14
cloop_p->b0 = 3353681;
cloop_p->b1 = -1577632;
cloop_p->b2 = -1712512;
cloop_p->a1 = -20114;
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; // 開啟電流環,專測電流環
while (1)
@@ -907,97 +865,94 @@ int main(void)
#endif
#if 0 // 測試 PFC 電壓環 + 電流環 (OK)
TIM2->CCR1 = 12; // min duty
sys_state = STATE_PFC_SOFT_START;
TIM2->CCR1 = 12; // min duty
sys_state = STATE_PFC_SOFT_START;
cur_loop_fun_en = 0; // 一開始關閉電流環,專測電流環
{
vloop_p->a2 = 0;
vloop_p->b2 = 0;
cur_loop_fun_en = 0; // 一開始關閉電流環,STATE_RUN_NORMAL再開電流環
// 120HZ, 0.1Hz, 100G
vloop_p->b0 = 139;
vloop_p->b1 = -129;
vloop_p->a1 = -16383;
}
{
bplot_p->b0 = 3353681;
bplot_p->b1 = -1577632;
bplot_p->b2 = -1712512;
bplot_p->a1 = -20114;
bplot_p->a2 = 3737;
}
// 2P2Z 參數: Z1=200, Z2=100000, P1=10, P2=20000, G=10000, fs=100K, bQ14, aQ14
cloop_p->b0 = 3353681;
cloop_p->b1 = -1577632;
cloop_p->b2 = -1712512;
cloop_p->a1 = -20114;
cloop_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
vbus_avg = (vbus_avg - (vbus_avg >> 4)) + (vbus >> 4); // for voltage loop
if (vbus > vbus_ovp) Power_Off(); // ovp
Vac_Peak_Detector();
Vac_Peak_Detector();
if (power_on_cmd == 1)
{
if (sys_state == STATE_PFC_SOFT_START)
{
if (power_on_cmd == 1)
{
if (sys_state == STATE_PFC_SOFT_START)
{
if (pfc_running == 0) {
pfc_pwm_enable();
pfc_running = 1;
}
pfc_pwm_enable();
pfc_running = 1;
}
if (++v_loop_cnt >= 10) // 10KHZ
{
if (++v_loop_cnt >= 10) // 10KHZ
{
Vol_Loop_PFC_1P1Z_UI();
//Vol_Loop_PFC_1P1Z();
v_loop_cnt = 0;
}
v_loop_cnt = 0;
}
if (vbus_avg > vbus_set_l && is_zero_crossing == 1)
{
sys_state = STATE_RUN_NORMAL;
// --- 關鍵修正:重新初始化電壓環內部狀態 ---
// 給予一個極小的初始 COMP 值,讓它從輕載慢慢往上爬
if (vbus_avg > vbus_set_l && is_zero_crossing == 1)
{
sys_state = STATE_RUN_NORMAL;
// --- 關鍵修正:重新初始化電壓環內部狀態 ---
// 給予一個極小的初始 COMP 值,讓它從輕載慢慢往上爬
vbus_comp_1 = (10 << 14); // 假設從一個極小的係數開始
vbus_err_1 = 0; // 清除誤差歷史
vbus_comp2 = (vbus_comp_1 >> 9); // 讓電流環拿到的初始值是 10
vbus_comp2 = (vbus_comp_1 >> 9); // 讓電流環拿到的初始值是 10
cur_loop_fun_en = 1; // 開啟電流環
}
}
}
}
else if (sys_state == STATE_RUN_NORMAL)
{
else if (sys_state == STATE_RUN_NORMAL)
{
if (pfc_running == 0) {
pfc_pwm_enable();
pfc_running = 1;
}
pfc_pwm_enable();
pfc_running = 1;
}
if (++v_loop_cnt >= 10) // 10KHZ
{
if (++v_loop_cnt >= 10) // 10KHZ
{
Vol_Loop_PFC_1P1Z_UI();
//Vol_Loop_PFC_1P1Z();
v_loop_cnt = 0;
}
}
}
}
Key1_Scan();
//Handle_UART_Monitor();
v_loop_cnt = 0;
}
}
}
}
Key1_Scan();
}
#endif
// main loop
/* PFC 電流環參數*/
// Z1=200, Z2=100000, P1=10, P2=20000, G=8000
bplot_p->b0 = 3353681;
bplot_p->b1 = -1577632;
bplot_p->b2 = -1712512;
bplot_p->a1 = -20114;
bplot_p->a2 = 3737;
cloop_p->b0 = 3353681;
cloop_p->b1 = -1577632;
cloop_p->b2 = -1712512;
cloop_p->a1 = -20114;
cloop_p->a2 = 3737;
cur_loop_fun_en = 1; // 開啟電流環,專測電流環
while (1)
@@ -1070,7 +1025,7 @@ int main(void)
if (++v_loop_cnt >= 10) // 10KHZ
{
Vol_Loop_PFC_1P1Z();
//Vol_Loop_PFC_1P1Z();
// Vol_Loop_PFC_1P1Z();
v_loop_cnt = 0;
}
@@ -1129,20 +1084,18 @@ int main(void)
if (++v_loop_cnt >= 10)
{
// 先算前饋,再算主環,最後疊加
Load_Loop_1P1Z(); // Cr 前饋
Load_Loop_1P1Z(); // Cr 前饋
Vol_Loop_PFC_1P1Z(); // PFC 電壓環
//Vol_Loop_LLC(); // LLC 電壓環
// Vol_Loop_LLC(); // LLC 電壓環
v_loop_cnt = 0;
}
if (++v_loop_cnt2 >= 5) // 好一點
{
Vol_Loop_LLC(); // LLC 電壓環
v_loop_cnt2 = 0;
}
break;
case STATE_RUN_SKIP: // 5
@@ -1166,14 +1119,9 @@ int main(void)
// --- 3. 非即時任務 (UI、通訊、Data Log) ---
Key1_Scan();
#if DATA_LOG
if (is_full == 1) Handle_UART_Log(); // 利用 PEC930 UART 輸出數據 [cite: 105, 336]
#endif
#if DATA_MONITOR
Handle_UART_Monitor();
#endif
Capture_Dump_IfReady();
capture_continuous_task();
} // while --------------------------------------------------------------------
return 0;