Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 07d5e70f84 | |||
| 411939fbc1 | |||
| a7de92ee96 | |||
| 14c33ee8ac | |||
| e447f7290a | |||
| b3df1a54dc | |||
| cba70b6754 | |||
| 7b4c689260 | |||
| 8164d47b35 | |||
| 1257dc34c7 | |||
| 70bcbe562b | |||
| 7d23d69a15 | |||
| c846ad9793 | |||
| 52e95c19c9 | |||
| eee38b6a0c | |||
| 86a8cbc5cf | |||
| b20a622f5f | |||
| 8cdb477e0f | |||
| fe22085e48 | |||
| 3ddf1079cb |
@@ -21,7 +21,7 @@
|
||||
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.addtools.createflash.249191345" name="Create flash image" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.addtools.createflash" value="true" valueType="boolean"/>
|
||||
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.addtools.createlisting.243120876" name="Create extended listing" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.addtools.createlisting" value="true" valueType="boolean"/>
|
||||
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.addtools.printsize.1566043258" name="Print size" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.addtools.printsize" value="true" valueType="boolean"/>
|
||||
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.optimization.level.1801001334" name="Optimization Level" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.optimization.level" value="ilg.gnumcueclipse.managedbuild.cross.riscv.option.optimization.level.more" valueType="enumerated"/>
|
||||
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.optimization.level.1801001334" name="Optimization Level" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.optimization.level" value="ilg.gnumcueclipse.managedbuild.cross.riscv.option.optimization.level.optimize" valueType="enumerated"/>
|
||||
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.optimization.messagelength.651682061" name="Message length (-fmessage-length=0)" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.optimization.messagelength" value="true" valueType="boolean"/>
|
||||
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.optimization.signedchar.1219903449" name="'char' is signed (-fsigned-char)" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.optimization.signedchar" value="true" valueType="boolean"/>
|
||||
<option id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.optimization.functionsections.168524687" name="Function sections (-ffunction-sections)" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.optimization.functionsections" value="true" valueType="boolean"/>
|
||||
|
||||
BIN
Binary file not shown.
@@ -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
|
||||
@@ -32,11 +32,11 @@ extern "C"
|
||||
#define YN_MAX (PFC_DUTY_MAX << 14)
|
||||
#define YN_MIN (PFC_DUTY_MIN << 14)
|
||||
// VBUS 參數
|
||||
#define VBUS_SET 340 // unit: volt (Transformer N = 14)
|
||||
#define VBUS_SET_H (VBUS_SET + 30) // burst mode in, 在過零區判斷
|
||||
#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,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)
|
||||
{
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -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
|
||||
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user