20 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
aiden 52e95c19c9 feat: 開啟Vol_Loop_LLC()
開啟 vac brown out protector
2026-05-28 11:35:52 +08:00
aiden eee38b6a0c feat: 電流環 ff_term tune done
ff_term = (2800 - vac_avg1) * PFC_DUTY_MAX / 2800 - 200
2026-05-28 10:35:10 +08:00
aiden 86a8cbc5cf feat: "單測 PFC 電流環" OK, 還沒有 ff_term
Cur_Loop_PFC_2P2Z() OK
vbus_comp2 = 1000
ff_term = 0
2026-05-27 17:28:29 +08:00
aiden b20a622f5f feat: "單測 PFC 電流環" 待測合適的值
vbus_comp2 = 3000
ff_term = 0
local_iac_set >> 12
2026-05-27 16:44:03 +08:00
aiden 8cdb477e0f feat: 新增 "單測 PFC 電壓環" 參數 2026-05-27 15:38:58 +08:00
aiden fe22085e48 feat :Vol_Loop_PFC_1P1Z_UI(), Vol_Loop_PFC_1P1Z() OK
UI的電壓偏低
2026-05-27 15:34:13 +08:00
aiden 3ddf1079cb feat: add .clang-format configuration for code styling 2026-05-27 15:01:52 +08:00
10 changed files with 1009 additions and 807 deletions
@@ -0,0 +1,53 @@
#Generated from Visual Studio settings
---
BasedOnStyle: webkit
BraceWrapping:
BeforeCatch: true
BeforeElse: true
AfterClass: true
AfterFunction: true
AfterControlStatement: true
AfterEnum: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
ColumnLimit: 0
BreakBeforeBraces: Custom
NamespaceIndentation: All
TabWidth: 2
IndentWidth: 2
IndentCaseLabels: true
AlignAfterOpenBracket: Align
PointerAlignment: Right
AlignOperands: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortLoopsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
BinPackArguments: false
BinPackParameters: false
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: false
BreakAfterJavaFieldAnnotations: true
AlignTrailingComments: true
BreakConstructorInitializers: AfterColon
AlignConsecutiveMacros:
Enabled: true
AcrossEmptyLines: true
AcrossComments: true
AlignArrayOfStructures: Right
AlignConsecutiveAssignments:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: false
AlignConsecutiveBitFields:
Enabled: true
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: true
PadOperators: true
AlignEscapedNewlines: Right
...
@@ -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,12 @@ 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
@@ -32,11 +32,11 @@ extern "C"
#define YN_MAX (PFC_DUTY_MAX << 14)
#define YN_MIN (PFC_DUTY_MIN << 14)
// VBUS 參數
#define VBUS_SET 360 // 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_L (VBUS_SET - 20) // burst mode out, 在過零區判斷
// #define VBUS_SET_H2 370 // for skip mode
#define VBUS_OVP 410 // 420V 電容
#define VBUS_OVP 420 // 420V 電容
#define VBUS_2ADC 149.15f // 4M + 27K
#define VCOMP_2DUTY 0 // vbus_comp control duty
#define V_COMP_MAX 16000 // (PFC_DUTY_MAX * 32) // 480 * 32 = 16360
@@ -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,9 +8,8 @@
#define TICK_1000_MS 1000
struct bode_plot_param_t bode_plot_param;
static int bode_plot_param_write_to_flash(const struct bode_plot_param_t *param, uint32_t addr);
struct bode_plot_param_t pfc_cloop_bode_plot_param;
struct bode_plot_param_t pfc_vloop_bode_plot_param;
/* sys clock*/
static void sysclk_systick_init(SYSCFG_ClkSrcTypeDef clk_src, uint32_t sys_tick_ms)
@@ -214,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
{
@@ -295,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*/
@@ -357,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;
// 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
File diff suppressed because it is too large Load Diff
@@ -1,5 +1,5 @@
adapter speed 5000
adapter speed 1000
reset_config srst_only
adapter srst pulse_width 100