feat: uart cmd to ctrl pec930
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Wisetop. All Rights Reserved.
|
||||
*/
|
||||
|
||||
#ifndef __APP_H__
|
||||
#define __APP_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include "isr.h"
|
||||
#include "main.h"
|
||||
|
||||
//=============================================================================
|
||||
// Constant Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Macro Definition
|
||||
//=============================================================================
|
||||
#define UART_RX_BUF_SIZE 64
|
||||
#define UART_SERIAL_BAUD 115200
|
||||
#define UART_RX_TIMEOUT_MS 100
|
||||
|
||||
/* 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
|
||||
|
||||
//=============================================================================
|
||||
// Structure Definition
|
||||
//=============================================================================
|
||||
struct bode_plot_param_t
|
||||
{
|
||||
int32_t b0;
|
||||
int32_t b1;
|
||||
int32_t b2;
|
||||
int32_t a1;
|
||||
int32_t a2;
|
||||
};
|
||||
//=============================================================================
|
||||
// Global Data Definition
|
||||
//=============================================================================
|
||||
static volatile char g_rx_buf[UART_RX_BUF_SIZE];
|
||||
static volatile uint32_t g_rx_idx = 0;
|
||||
static volatile uint32_t g_rx_len = 0;
|
||||
static volatile int g_rx_ready = 0;
|
||||
static volatile uint32_t g_rx_last_tick = 0; /* sys_tick when last byte arrived */
|
||||
//=============================================================================
|
||||
// Private Function Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Public Function Definition
|
||||
//=============================================================================
|
||||
void app(void);
|
||||
|
||||
/* uart and cmd*/
|
||||
void uart_init(void);
|
||||
void uart_rx_handler(void);
|
||||
int is_rx_frame_ready(void);
|
||||
int uart_rx_getline(char *pBuf, uint32_t size);
|
||||
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;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -22,8 +22,6 @@ void pfc_pwm_config(); // PFC
|
||||
|
||||
//-----------------------------------
|
||||
void pwm_brake();
|
||||
void uart_config();
|
||||
void UART_Send_Wait(UART_Type *pHUart, uint16_t value);
|
||||
void tim0_config();
|
||||
|
||||
#endif /* INC_INIT_H_ */
|
||||
|
||||
@@ -14,36 +14,37 @@
|
||||
#define __isr_H_wuraIpBA_lTJm_HvJw_sNDo_uEd5JnucTReY__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include "main.h"
|
||||
//=============================================================================
|
||||
// Constant Definition
|
||||
//=============================================================================
|
||||
//=============================================================================
|
||||
// Constant Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Macro Definition
|
||||
//=============================================================================
|
||||
//=============================================================================
|
||||
// Macro Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Structure Definition
|
||||
//=============================================================================
|
||||
//=============================================================================
|
||||
// Structure Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Global Data Definition
|
||||
//=============================================================================
|
||||
//=============================================================================
|
||||
// Global Data Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Private Function Definition
|
||||
//=============================================================================
|
||||
//=============================================================================
|
||||
// Private Function Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Public Function Definition
|
||||
//=============================================================================
|
||||
//void isr_tim2_handle(void);
|
||||
//void isr_adc_handle(void);
|
||||
//void isr_tim0_handle(void);
|
||||
//=============================================================================
|
||||
// Public Function Definition
|
||||
//=============================================================================
|
||||
// void isr_tim2_handle(void);
|
||||
// void isr_adc_handle(void);
|
||||
// void isr_tim0_handle(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -14,10 +14,10 @@
|
||||
#define __main_H_wxBnwdPw_lq7D_H4I5_sHPp_uXof15pFLp4V__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
|
||||
#include "hal_device.h"
|
||||
#include "syslog.h"
|
||||
//=============================================================================
|
||||
@@ -26,104 +26,103 @@ extern "C" {
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// PFC DUTY 參數
|
||||
#define PFC_PERIOD 600 // 600 = 100khz
|
||||
#define PFC_DUTY_MAX 540 //(PFC_PERIOD * 0.9) // 600 * 0.85 = 510
|
||||
#define PFC_DUTY_MIN 12 //(PFC_PERIOD * 0.02) // 600 * 0.02 = 12
|
||||
#define PFC_PERIOD 600 // 600 = 100khz
|
||||
#define PFC_DUTY_MAX 540 //(PFC_PERIOD * 0.9) // 600 * 0.85 = 510
|
||||
#define PFC_DUTY_MIN 12 //(PFC_PERIOD * 0.02) // 600 * 0.02 = 12
|
||||
// VBUS 參數
|
||||
#define VBUS_SET 360 // 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_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
|
||||
#define V_COMP_MIN 300 // (PFC_DUTY_MAX * 32) // 12 * 32 = 384
|
||||
#define V_INT_GAIN_BIT 9 // 7=/128, 9=/512 大約除 120 ~ 500 之間 (粗調用bit, 細調用整數)
|
||||
#define V_POR_GAIN_BIT 8 // 6=/64, 8=/256 大約除 60 ~ 250 之間
|
||||
#define VBUS_INT_MAX (V_COMP_MAX << V_INT_GAIN_BIT)
|
||||
#define VBUS_POR_MAX (V_COMP_MAX << (V_POR_GAIN_BIT-1))
|
||||
#define VBUS_SET 360 // 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_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
|
||||
#define V_COMP_MIN 300 // (PFC_DUTY_MAX * 32) // 12 * 32 = 384
|
||||
#define V_INT_GAIN_BIT 9 // 7=/128, 9=/512 大約除 120 ~ 500 之間 (粗調用bit, 細調用整數)
|
||||
#define V_POR_GAIN_BIT 8 // 6=/64, 8=/256 大約除 60 ~ 250 之間
|
||||
#define VBUS_INT_MAX (V_COMP_MAX << V_INT_GAIN_BIT)
|
||||
#define VBUS_POR_MAX (V_COMP_MAX << (V_POR_GAIN_BIT - 1))
|
||||
//---------------------------------------------------------------
|
||||
#define DIGI_COMP_BIT 10
|
||||
//#define DIGI_COMP_MAX (VBUS_SET_H * 4096 / VBUS_2ADC / 3.3) << 4
|
||||
#define DIGI_COMP_BIT 10
|
||||
// #define DIGI_COMP_MAX (VBUS_SET_H * 4096 / VBUS_2ADC / 3.3) << 4
|
||||
//------------------------------------------------------------------
|
||||
// 武士 12V 變壓器 + 47nF 諧振頻率 ~ 75KHZ
|
||||
// 實測值 106uH + 47nF = 73KHZ --> PERIOD = 820 ~ 830
|
||||
// 高正 40uH + 68nF ~ 95KHZ
|
||||
#define LLC_PERIOD_SS 400 // 600 = 100khz (EPWM initial use)
|
||||
#define DEAD_TIME 500 // ns
|
||||
#define DEAD_TIME_VALUE (DEAD_TIME * 0.06) // double
|
||||
// 武士 12V 變壓器 + 47nF 諧振頻率 ~ 75KHZ
|
||||
// 實測值 106uH + 47nF = 73KHZ --> PERIOD = 820 ~ 830
|
||||
// 高正 40uH + 68nF ~ 95KHZ
|
||||
#define LLC_PERIOD_SS 400 // 600 = 100khz (EPWM initial use)
|
||||
#define DEAD_TIME 500 // ns
|
||||
#define DEAD_TIME_VALUE (DEAD_TIME * 0.06) // double
|
||||
// LLC free run, 改控 vbus_set
|
||||
#define LLC_PERIOD_MIN 400 // MIN, 400 = 150KHZ (>760不會振)
|
||||
#define LLC_PERIOD_MAX 820 // MAX, 800 = 75KHZ // 可調範圍太大會共振
|
||||
#define LLC_COMP_GAIN_BIT 10 // 要慢才不會振
|
||||
#define LLC_COMP_MAX ((LLC_PERIOD_MAX-LLC_PERIOD_MIN) << LLC_COMP_GAIN_BIT)
|
||||
#define LLC_PERIOD_MIN 400 // MIN, 400 = 150KHZ (>760不會振)
|
||||
#define LLC_PERIOD_MAX 820 // MAX, 800 = 75KHZ // 可調範圍太大會共振
|
||||
#define LLC_COMP_GAIN_BIT 10 // 要慢才不會振
|
||||
#define LLC_COMP_MAX ((LLC_PERIOD_MAX - LLC_PERIOD_MIN) << LLC_COMP_GAIN_BIT)
|
||||
// LLC Soft Start 掃頻 2倍 ~ 1倍 = 150KHZ ~ 73KHZ
|
||||
#define LLC_SS_MIN (LLC_PERIOD_MIN << 2) // 精度不夠,放大 4 倍, 1600
|
||||
#define LLC_SS_MAX (LLC_PERIOD_MAX << 2) // 精度不夠,放大 4 倍, 3280
|
||||
#define LLC_SS_COUNT (LLC_SS_MAX - LLC_SS_MIN) // 3280 - 1600 = 1680 * 10us = 16.8ms
|
||||
#define LLC_SS_MIN (LLC_PERIOD_MIN << 2) // 精度不夠,放大 4 倍, 1600
|
||||
#define LLC_SS_MAX (LLC_PERIOD_MAX << 2) // 精度不夠,放大 4 倍, 3280
|
||||
#define LLC_SS_COUNT (LLC_SS_MAX - LLC_SS_MIN) // 3280 - 1600 = 1680 * 10us = 16.8ms
|
||||
// TIM0 參數 = ADC 取樣頻率
|
||||
#define TIM0_PERIOD 600 // 60M / 600 = 100KHZ
|
||||
#define TIM0_PERIOD 600 // 60M / 600 = 100KHZ
|
||||
// IAC 參數
|
||||
#define I_COMP_MAX 16000 // = V_COMP_MAX * 1.414
|
||||
#define I_INT_GAIN_BIT 2
|
||||
//#define I_POR_GAIN_BIT 6
|
||||
#define I_INT_MAX (I_COMP_MAX << 3)
|
||||
#define I_POR_MAX (I_INT_MAX) // 減半,影響 50%
|
||||
#define I_COMP_MAX 16000 // = V_COMP_MAX * 1.414
|
||||
#define I_INT_GAIN_BIT 2
|
||||
// #define I_POR_GAIN_BIT 6
|
||||
#define I_INT_MAX (I_COMP_MAX << 3)
|
||||
#define I_POR_MAX (I_INT_MAX) // 減半,影響 50%
|
||||
|
||||
//#define VAC_BROWN_IN 1113 // 約 95V (你原本的邏輯)
|
||||
//#define VAC_BROWN_OUT 930 // 約 80V (根據你的比例計算)
|
||||
#define VAC_ZERO 400
|
||||
#define VAC_ZERO_RISE 600
|
||||
#define BROWNOUT_DELAY 500 // 延遲計數,避免瞬時雜訊觸發
|
||||
#define VOLT_BROWN_IN 900 // 78V = 912
|
||||
#define VOLT_BROWN_OUT 855 // 73V = 855
|
||||
// #define VAC_BROWN_IN 1113 // 約 95V (你原本的邏輯)
|
||||
// #define VAC_BROWN_OUT 930 // 約 80V (根據你的比例計算)
|
||||
#define VAC_ZERO 400
|
||||
#define VAC_ZERO_RISE 600
|
||||
#define BROWNOUT_DELAY 500 // 延遲計數,避免瞬時雜訊觸發
|
||||
#define VOLT_BROWN_IN 900 // 78V = 912
|
||||
#define VOLT_BROWN_OUT 855 // 73V = 855
|
||||
// VAC 110V = 1260
|
||||
// VAC 220V = 2483
|
||||
// 定義控制參數
|
||||
#define STEP_FAST 16 // 遠離目標時的步長
|
||||
#define STEP_SLOW 1 // 接近目標時的步長
|
||||
#define STABLE_THRES 5 // 狀態翻轉幾次後判定為進入穩態
|
||||
#define STEP_FAST 16 // 遠離目標時的步長
|
||||
#define STEP_SLOW 1 // 接近目標時的步長
|
||||
#define STABLE_THRES 5 // 狀態翻轉幾次後判定為進入穩態
|
||||
|
||||
#define CURR_OFFSET 3840 // 零點電流 = 3820 ~ 3860
|
||||
#define CURR_OFFSET 3840 // 零點電流 = 3820 ~ 3860
|
||||
|
||||
//=============================================================================
|
||||
// Macro Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Macro Definition
|
||||
//=============================================================================
|
||||
//=============================================================================
|
||||
// Structure Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Structure Definition
|
||||
//=============================================================================
|
||||
//=============================================================================
|
||||
// Global Data Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Global Data Definition
|
||||
//=============================================================================
|
||||
//=============================================================================
|
||||
// Private Function Definition
|
||||
//=============================================================================
|
||||
|
||||
//=============================================================================
|
||||
// Private Function Definition
|
||||
//=============================================================================
|
||||
//=============================================================================
|
||||
// Public Function Definition
|
||||
//=============================================================================
|
||||
void Power_On();
|
||||
void Power_Off();
|
||||
void Key1_Scan();
|
||||
void Key_Action();
|
||||
void Vol_Loop_PFC();
|
||||
void llc_set_period(uint16_t val);
|
||||
void llc_pwm_enable();
|
||||
void llc_pwm_disable();
|
||||
void pfc_set_duty(uint16_t val);
|
||||
void pfc_pwm_enable();
|
||||
void pfc_pwm_disable();
|
||||
void A_Pata_Reset();
|
||||
void Vol_Loop_LLC(void);
|
||||
void Cur_Loop_PFC(void);
|
||||
|
||||
//=============================================================================
|
||||
// Public Function Definition
|
||||
//=============================================================================
|
||||
void Power_On();
|
||||
void Power_Off();
|
||||
void Key1_Scan();
|
||||
void Key_Action();
|
||||
void Vol_Loop_PFC();
|
||||
void llc_set_period(uint16_t val);
|
||||
void llc_pwm_enable();
|
||||
void llc_pwm_disable();
|
||||
void pfc_set_duty(uint16_t val);
|
||||
void pfc_pwm_enable();
|
||||
void pfc_pwm_disable();
|
||||
void A_Pata_Reset();
|
||||
void Vol_Loop_LLC(void);
|
||||
void Cur_Loop_PFC(void);
|
||||
|
||||
void isr_adc_handle(void);
|
||||
void isr_tim0_handle(void);
|
||||
void isr_adc_handle(void);
|
||||
void isr_tim0_handle(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -0,0 +1,380 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Wisetop. All Rights Reserved.
|
||||
*/
|
||||
#include "app.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#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);
|
||||
|
||||
/* sys clock*/
|
||||
static void sysclk_systick_init(SYSCFG_ClkSrcTypeDef clk_src, uint32_t sys_tick_ms)
|
||||
{
|
||||
SYSCFG_ClkInitTypeDef SysClkInit = { 0 };
|
||||
|
||||
SysClkInit.ClkSource = clk_src;
|
||||
SYSCFG_SysClkConfig(&SysClkInit);
|
||||
|
||||
sys_config_systick(sys_tick_ms);
|
||||
}
|
||||
|
||||
static void sys_disable_ice(void)
|
||||
{
|
||||
/* Disable ICE I/O for free run mode to use UART */
|
||||
sys_delay(5000);
|
||||
SYSCFG_SetICEPin2NormalIO(true);
|
||||
}
|
||||
|
||||
/* uart*/
|
||||
void uart_init(void)
|
||||
{
|
||||
GPIO_InitTypeDef gpio_init = { 0 };
|
||||
UART_InitTypeDef uart_init = { 0 };
|
||||
sys_irq_attr_t irq_attr = {
|
||||
.trig_mode = SYS_IRQ_TRIGGER_LEVEL,
|
||||
};
|
||||
|
||||
/* UART Tx: PB3 AF1 */
|
||||
gpio_init.GPIO_Pin = GPIO_Pin_03;
|
||||
gpio_init.GPIO_AF_Mode = GPIO_AF_1;
|
||||
gpio_init.GPIO_Mode = GPIO_Mode_AF;
|
||||
GPIO_Init(GPIOB, &gpio_init);
|
||||
|
||||
/* UART Rx: PB5 AF1 */
|
||||
gpio_init.GPIO_Pin = GPIO_Pin_05;
|
||||
gpio_init.GPIO_AF_Mode = GPIO_AF_1;
|
||||
GPIO_Init(GPIOB, &gpio_init);
|
||||
|
||||
uart_init.BaudRate = UART_SERIAL_BAUD;
|
||||
uart_init.WordLength = UART_WordLength_8b;
|
||||
uart_init.StopBits = UART_StopBits_1;
|
||||
uart_init.Parity = UART_Parity_No;
|
||||
uart_init.Mode = UART_Mode_TxRx;
|
||||
UART_Init(UART0, &uart_init);
|
||||
|
||||
// clear RX FIFO and state
|
||||
UART_ResetRxFIFO(UART0);
|
||||
g_rx_idx = 0;
|
||||
g_rx_len = 0;
|
||||
g_rx_ready = 0;
|
||||
|
||||
UART_ITConfig(UART0, UART_IE_RXNEE_Msk, ENABLE);
|
||||
sys_register_IRQ(UART0_IRQn, uart_rx_handler, &irq_attr);
|
||||
sys_enable_girq();
|
||||
|
||||
UART_Start(UART0);
|
||||
return;
|
||||
}
|
||||
|
||||
int uart_rx_getline(char *pBuf, uint32_t size)
|
||||
{
|
||||
if (!pBuf || size == 0 || !g_rx_ready)
|
||||
return 0;
|
||||
|
||||
uint32_t len = (g_rx_len < size - 1) ? g_rx_len : (size - 1);
|
||||
for (uint32_t i = 0; i < len; i++)
|
||||
pBuf[i] = g_rx_buf[i];
|
||||
pBuf[len] = '\0';
|
||||
|
||||
g_rx_len = 0;
|
||||
g_rx_ready = 0;
|
||||
g_rx_idx = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int is_rx_frame_ready(void)
|
||||
{
|
||||
return g_rx_ready;
|
||||
}
|
||||
|
||||
uint32_t __FASTCODE fastcode_proc(void)
|
||||
{
|
||||
log_color(SLOG_CYAN, "run at fast area: $pc= x%08X\n", (uint32_t)&fastcode_proc);
|
||||
return 0;
|
||||
}
|
||||
|
||||
__INTERRUPT void uart_rx_handler(void)
|
||||
{
|
||||
SAVE_IRQ_CSR_CONTEXT();
|
||||
|
||||
while (UART_GetITStatus(UART0, UART_IE_RXNEE_Msk))
|
||||
{
|
||||
uint8_t ch = (uint8_t)UART_ReceiveData(UART0);
|
||||
g_rx_last_tick = sys_get_tick(); // record time of last received byte
|
||||
|
||||
if (g_rx_ready) continue; // not ready to receive new frame, discard incoming data
|
||||
|
||||
if (ch == '\r' || ch == '\n')
|
||||
{
|
||||
if (g_rx_idx > 0)
|
||||
{
|
||||
g_rx_buf[g_rx_idx] = '\0';
|
||||
g_rx_len = g_rx_idx;
|
||||
g_rx_idx = 0;
|
||||
g_rx_ready = 1;
|
||||
}
|
||||
}
|
||||
else if (g_rx_idx < (UART_RX_BUF_SIZE - 1))
|
||||
{
|
||||
g_rx_buf[g_rx_idx++] = (char)ch;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_rx_idx = 0; // overflow, reset buffer index and discard data
|
||||
}
|
||||
}
|
||||
|
||||
RESTORE_IRQ_CSR_CONTEXT();
|
||||
}
|
||||
|
||||
/* cmd*/
|
||||
static bool cmd_match_with_arg(const volatile char *buf, const char *prefix, const char **arg_out)
|
||||
{
|
||||
size_t len = strlen(prefix);
|
||||
|
||||
if (strncmp((const char *)buf, prefix, len) != 0)
|
||||
return false;
|
||||
|
||||
*arg_out = (const char *)&buf[len];
|
||||
return true;
|
||||
}
|
||||
|
||||
static void cmd_trim_eol(char *buf)
|
||||
{
|
||||
size_t len = strlen(buf);
|
||||
|
||||
while (len > 0)
|
||||
{
|
||||
char ch = buf[len - 1];
|
||||
if (ch == '\r' || ch == '\n' || ch == ' ' || ch == '\t')
|
||||
{
|
||||
buf[len - 1] = '\0';
|
||||
len--;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static bool parse_i32_csv5(const char *arg, struct bode_plot_param_t *out)
|
||||
{
|
||||
int32_t vals[5] = { 0 };
|
||||
char *end = NULL;
|
||||
|
||||
if (!arg || !out)
|
||||
return false;
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
long v = strtol(arg, &end, 10);
|
||||
if (end == arg || v < -2147483648L || v > 2147483647L)
|
||||
return false;
|
||||
|
||||
vals[i] = (int32_t)v;
|
||||
|
||||
if (i < 4)
|
||||
{
|
||||
if (*end != ',')
|
||||
return false;
|
||||
arg = end + 1;
|
||||
}
|
||||
else if (*end != '\0')
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
out->b0 = vals[0];
|
||||
out->b1 = vals[1];
|
||||
out->b2 = vals[2];
|
||||
out->a1 = vals[3];
|
||||
out->a2 = vals[4];
|
||||
return true;
|
||||
}
|
||||
|
||||
void uart_cmd_parse(char *cmd_buf, uint32_t size)
|
||||
{
|
||||
if (!cmd_buf || size == 0)
|
||||
return;
|
||||
|
||||
const char *arg = NULL;
|
||||
cmd_trim_eol(cmd_buf);
|
||||
|
||||
/* Skip empty commands (e.g., from CR+LF line endings) */
|
||||
if (strlen(cmd_buf) == 0)
|
||||
return;
|
||||
|
||||
if (strcmp(cmd_buf, "help") == 0)
|
||||
{
|
||||
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(" flash_bodeplot? - show raw flash content for bode plot parameters\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))
|
||||
{
|
||||
struct bode_plot_param_t *bplot_p = &bode_plot_param;
|
||||
if (parse_i32_csv5(arg, bplot_p))
|
||||
{
|
||||
bode_plot_param_write_to_flash(bplot_p, FLASH_PROG_ADDR_USER_CALI);
|
||||
}
|
||||
else
|
||||
{
|
||||
msg("invalid bodeplot format, use: bodeplot=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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
else
|
||||
{
|
||||
msg("Unknown command: %s\r\n", cmd_buf);
|
||||
}
|
||||
}
|
||||
|
||||
/* flash*/
|
||||
#if 0
|
||||
static void dump_flash_bytes(uint32_t addr, uint32_t size)
|
||||
{
|
||||
const uint8_t *pData = (const uint8_t *)addr;
|
||||
|
||||
msg("flash dump source base address: 0x%08X, size=%u bytes\n", addr, (unsigned int)size);
|
||||
|
||||
for (uint32_t i = 0; i < size; i++)
|
||||
{
|
||||
msg("read [0x%08X] = 0x%02X\n", (unsigned int)(addr + i), pData[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void dump_flash_bytes_8perline(uint32_t addr, uint32_t size)
|
||||
{
|
||||
const uint8_t *pData = (const uint8_t *)addr;
|
||||
|
||||
msg("flash dump source base address: 0x%08X, size=%u bytes\n", addr, (unsigned int)size);
|
||||
|
||||
for (uint32_t i = 0; i < size; i += 8)
|
||||
{
|
||||
msg("read [0x%08X]", (unsigned int)(addr + i));
|
||||
|
||||
for (uint32_t j = 0; j < 8 && (i + j) < size; j++)
|
||||
{
|
||||
msg(" %02X", pData[i + j]);
|
||||
}
|
||||
|
||||
msg("\n");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static int bode_plot_param_write_to_flash(const struct bode_plot_param_t *param, uint32_t addr)
|
||||
{
|
||||
int rval = 0;
|
||||
uint32_t word_data[5] = { 0 };
|
||||
|
||||
if (!param)
|
||||
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;
|
||||
|
||||
rval = FLASH_ErasePage(addr);
|
||||
if (rval)
|
||||
{
|
||||
err(" ! erase plot param fail (%d)\n", rval);
|
||||
return rval;
|
||||
}
|
||||
|
||||
rval = FLASH_ProgData(addr, word_data, sizeof(word_data));
|
||||
if (rval)
|
||||
{
|
||||
err(" ! prog plot 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)
|
||||
{
|
||||
const void *flash_ptr = (const void *)addr;
|
||||
|
||||
if (!param)
|
||||
return;
|
||||
|
||||
memcpy(param, flash_ptr, sizeof(*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",
|
||||
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);
|
||||
}
|
||||
|
||||
/* main code*/
|
||||
void app(void)
|
||||
{
|
||||
char cmd_buf[UART_RX_BUF_SIZE];
|
||||
|
||||
sysclk_systick_init(SYSCFG_ClkSrc_HSI, SYS_TICK_1_MS);
|
||||
|
||||
sys_disable_ice();
|
||||
|
||||
uart_init();
|
||||
|
||||
info("This is a app demo project\n");
|
||||
info("uart cmd demo\n");
|
||||
|
||||
// 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);
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (is_rx_frame_ready())
|
||||
{
|
||||
uart_rx_getline(cmd_buf, sizeof(cmd_buf));
|
||||
msg("RX: %s\r\n", cmd_buf);
|
||||
uart_cmd_parse(cmd_buf, sizeof(cmd_buf));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,23 @@
|
||||
#if 0
|
||||
|
||||
#include "main.h"
|
||||
#include "isr.h"
|
||||
#include "init.h"
|
||||
#include "isr.h"
|
||||
#include "main.h"
|
||||
//=============================================================================
|
||||
// Constant Definition
|
||||
//=============================================================================
|
||||
#define DATA_LOG 0
|
||||
#define DATA_MONITOR 1
|
||||
#define DEBOUNCE_TIME 20
|
||||
#define BUF_SIZE 100 // > 300 記憶體不夠用(4組) > 700 記憶體不夠用(2組)
|
||||
#define DATA_LOG 0
|
||||
#define DATA_MONITOR 1
|
||||
#define DEBOUNCE_TIME 20
|
||||
#define BUF_SIZE 100 // > 300 記憶體不夠用(4組) > 700 記憶體不夠用(2組)
|
||||
|
||||
#define CR_1P1Z_B0_Q14 6313
|
||||
#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 格式的數值範圍調整)
|
||||
// 假設最大前饋量限制為實數值 50.0 -> 50.0 * 16384 = 819200
|
||||
#define CR_FF_MAX_Q14 819200 // 50 duty << 14
|
||||
#define CR_FF_MIN_Q14 -819200
|
||||
#define CR_FF_MAX_Q14 819200 // 50 duty << 14
|
||||
#define CR_FF_MIN_Q14 -819200
|
||||
|
||||
//=============================================================================
|
||||
// Macro Definition
|
||||
@@ -54,8 +54,8 @@ typedef enum {
|
||||
//#define ff_max 8000
|
||||
|
||||
//#define v_kp 10
|
||||
#define v_ki 10
|
||||
#define v_kd 0
|
||||
#define v_ki 10
|
||||
#define v_kd 0
|
||||
|
||||
SystemState_t sys_state = STATE_INIT;
|
||||
uint8_t is_zero_crossing = 0;
|
||||
@@ -561,7 +561,7 @@ __attribute__((always_inline)) static inline void Cur_Loop_PFC_2P2Z(void) // 優
|
||||
//count1++;
|
||||
// -------------------------------------------
|
||||
// 5. data log
|
||||
#if DATA_LOG
|
||||
#if DATA_LOG
|
||||
count_down--;
|
||||
if (count_down < 1000)
|
||||
{
|
||||
@@ -578,7 +578,7 @@ __attribute__((always_inline)) static inline void Cur_Loop_PFC_2P2Z(void) // 優
|
||||
pfc_pwm_disable();
|
||||
sys_state = STATE_PAUSE;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
// -------------------------------------------
|
||||
}
|
||||
//============================================================================
|
||||
@@ -834,7 +834,7 @@ int main(void)
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 1 // 測試 PFC 電壓環 + 電流環 (電流環無法穩壓) (電壓環 + 電流環有 BUG,會多一個小 PULSE)
|
||||
#if 1 // 測試 PFC 電壓環 + 電流環 (電流環無法穩壓) (電壓環 + 電流環有 BUG,會多一個小 PULSE)
|
||||
TIM2->CCR1 = 12; // min duty
|
||||
sys_state = STATE_PFC_SOFT_START;
|
||||
|
||||
@@ -1082,13 +1082,13 @@ int main(void)
|
||||
// --- 3. 非即時任務 (UI、通訊、Data Log) ---
|
||||
Key1_Scan();
|
||||
|
||||
#if DATA_LOG
|
||||
#if DATA_LOG
|
||||
if (is_full == 1) Handle_UART_Log(); // 利用 PEC930 UART 輸出數據 [cite: 105, 336]
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if DATA_MONITOR
|
||||
#if DATA_MONITOR
|
||||
Handle_UART_Monitor();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
} // while --------------------------------------------------------------------
|
||||
|
||||
|
||||
@@ -5,16 +5,15 @@
|
||||
* Author: jim_liao
|
||||
*/
|
||||
|
||||
//#include "main.h"
|
||||
#include "isr.h"
|
||||
#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
|
||||
|
||||
// #define CONFIG_OPA_PGA_GAIN OPAMP_PGAGain_1 //OPAMP_PGAGain_5
|
||||
|
||||
//=============================================================================
|
||||
// Public Function Definition
|
||||
@@ -66,168 +65,164 @@ _Post_Proc(PWR_ModeTypeDef pwr_mode)
|
||||
//========================================================================
|
||||
void SYS_Config()
|
||||
{
|
||||
SYSCFG_ClkInitTypeDef SysClkInit = {0};
|
||||
SYSCFG_ClkInitTypeDef SysClkInit = { 0 };
|
||||
|
||||
SysClkInit.ClkSource = SYSCFG_SYSCLKCR_CLKSW_HSI;
|
||||
SYSCFG_SysClkConfig(&SysClkInit);
|
||||
SysClkInit.ClkSource = SYSCFG_SYSCLKCR_CLKSW_HSI;
|
||||
SYSCFG_SysClkConfig(&SysClkInit);
|
||||
|
||||
sys_config_systick(SYS_TICK_1_MS);
|
||||
sys_config_systick(SYS_TICK_1_MS);
|
||||
|
||||
//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_config();
|
||||
opa_config();
|
||||
adc_config();
|
||||
llc_pwm_config(); // 初始化但不輸出 EPWM_CH1
|
||||
pfc_pwm_config(); // 初始化但不輸出 TIM2_CH1
|
||||
gpio_config();
|
||||
opa_config();
|
||||
adc_config();
|
||||
llc_pwm_config(); // 初始化但不輸出 EPWM_CH1
|
||||
pfc_pwm_config(); // 初始化但不輸出 TIM2_CH1
|
||||
|
||||
__NOP();__NOP();__NOP();__NOP();__NOP(); // timer shift between pfc and tim0
|
||||
__NOP();
|
||||
__NOP();
|
||||
__NOP();
|
||||
__NOP();
|
||||
__NOP(); // timer shift between pfc and tim0
|
||||
|
||||
tim0_config(); // trigger ADC, current loop frequency
|
||||
uart_config(); // UART
|
||||
tim0_config(); // trigger ADC, current loop frequency
|
||||
uart_init();
|
||||
}
|
||||
//===================================================================================
|
||||
void gpio_config()
|
||||
{
|
||||
//-----------------//
|
||||
// OPA
|
||||
// IN+ = PA8
|
||||
// IN- = PA9
|
||||
// OUT = PA7
|
||||
//-----------------//
|
||||
//-----------------//
|
||||
// OPA
|
||||
// IN+ = PA8
|
||||
// IN- = PA9
|
||||
// OUT = PA7
|
||||
//-----------------//
|
||||
|
||||
GPIO_InitTypeDef GPIO_InitStruct;
|
||||
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
|
||||
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);
|
||||
#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);
|
||||
//-----------------//
|
||||
// UART
|
||||
// PB3:TX PB5:RX
|
||||
//-----------------//
|
||||
GPIO_InitTypeDef GPIOA_InitUart;
|
||||
GPIO_InitTypeDef GPIOB_InitUart;
|
||||
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);
|
||||
|
||||
GPIOB_InitUart.GPIO_Pin = GPIO_Pin_03 | GPIO_Pin_05; // PB3, PB5
|
||||
GPIOB_InitUart.GPIO_Mode = GPIO_Mode_AF;
|
||||
GPIOB_InitUart.GPIO_AF_Mode = GPIO_AF_1;
|
||||
GPIO_Init(GPIOB, &GPIOB_InitUart);
|
||||
//-----------------//
|
||||
// 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);
|
||||
//-----------------//
|
||||
// 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);
|
||||
//-----------------//
|
||||
// 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
|
||||
//-----------------//
|
||||
// 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 // 取消了
|
||||
#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
|
||||
#endif
|
||||
}
|
||||
//===================================================================================
|
||||
void opa_config(void)
|
||||
{
|
||||
OPAMP_InitTypeDef opa_init = {0};
|
||||
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 功耗
|
||||
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
|
||||
// 正向放大器 OK
|
||||
// 電壓隨耦器 OK
|
||||
// 反向放大器 NG
|
||||
// V+ 接 1/2 VCC,差動放大器 OK
|
||||
|
||||
OPAMP_Init(OPAMP0, &opa_init);
|
||||
OPAMP_Init(OPAMP0, &opa_init);
|
||||
|
||||
OPAMP_Enable(OPAMP0);
|
||||
OPAMP_Enable(OPAMP0);
|
||||
}
|
||||
//===================================================================================
|
||||
void adc_config(void)
|
||||
{
|
||||
ADC_InitTypeDef init = {0};
|
||||
ADC_StructInit(&init);
|
||||
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_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
|
||||
// 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, };
|
||||
#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
|
||||
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;
|
||||
TIM_OCInitTypeDef TIM_OCInitStruct;
|
||||
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
|
||||
|
||||
/*
|
||||
/*
|
||||
{ // Configure TIM interrupts
|
||||
sys_irq_attr_t irq_attr = { .disable_vector = false, };
|
||||
|
||||
@@ -236,57 +231,57 @@ void llc_pwm_config() // complimentary PWM for LLC
|
||||
irq_attr.priority = SYS_IRQ_PRIORITY_MIDDEN;
|
||||
sys_register_IRQ(EPWM_IRQn, isr_epwm_handle, &irq_attr);
|
||||
}
|
||||
*/
|
||||
TIM_DeInit(EPWM);
|
||||
*/
|
||||
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_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_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_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_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);
|
||||
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;
|
||||
{ /* 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_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->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 才會輸出
|
||||
// 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;
|
||||
/*
|
||||
TIM_OCInitTypeDef TIM_OCInitStruct;
|
||||
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
|
||||
/*
|
||||
{ // Configure TIM interrupts
|
||||
sys_irq_attr_t irq_attr = { .disable_vector = false, };
|
||||
|
||||
@@ -295,74 +290,56 @@ void pfc_pwm_config() // single PWM for PFC
|
||||
irq_attr.priority = SYS_IRQ_PRIORITY_MIDDEN;
|
||||
sys_register_IRQ(TIM2_IRQn, isr_tim2_handle, &irq_attr);
|
||||
}
|
||||
*/
|
||||
*/
|
||||
|
||||
TIM_DeInit(TIM2);
|
||||
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_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);
|
||||
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
|
||||
// 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 uart_config()
|
||||
{
|
||||
UART_InitTypeDef uart_init = {0};
|
||||
uart_init.BaudRate = 9600; //115200;
|
||||
uart_init.WordLength = UART_WordLength_8b;
|
||||
uart_init.StopBits = UART_StopBits_1;
|
||||
uart_init.Parity = UART_Parity_No;
|
||||
uart_init.Mode = UART_Mode_TxRx;
|
||||
UART_Init(UART0, &uart_init);
|
||||
UART_Start(UART0);
|
||||
}
|
||||
//===================================================================================
|
||||
void UART_Send_Wait(UART_Type *pHUart, uint16_t value)
|
||||
{
|
||||
UART_SendData(pHUart,value);
|
||||
UART_WaitTxFifoEmpty(pHUart);
|
||||
}
|
||||
//===================================================================================
|
||||
void tim0_config()
|
||||
{
|
||||
LPTIM_InitTypeDef Init = {0};
|
||||
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);
|
||||
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
|
||||
#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
|
||||
#endif
|
||||
|
||||
LPTIM_Enable(TIM0);
|
||||
LPTIM_Enable(TIM0);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
* @description
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include "isr.h"
|
||||
#include "init.h"
|
||||
|
||||
@@ -25,13 +24,13 @@
|
||||
//=============================================================================
|
||||
// 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;
|
||||
// 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
|
||||
//=============================================================================
|
||||
@@ -42,9 +41,9 @@
|
||||
/*
|
||||
__INTERRUPT void isr_tim2_handle(void)
|
||||
{
|
||||
if((TIM2->SR & 0x20000) == 0x20000) // UDIF: Underflow interrupt flag
|
||||
if((TIM2->SR & 0x20000) == 0x20000) // UDIF: Underflow interrupt flag
|
||||
{
|
||||
TIM_ClearITPendingBit(TIM2, 0x20000);
|
||||
TIM_ClearITPendingBit(TIM2, 0x20000);
|
||||
}
|
||||
}
|
||||
*/
|
||||
@@ -68,5 +67,3 @@ __INTERRUPT void isr_tim0_handle(void)
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user