diff --git a/Examples/template/dds/.clang-format b/Examples/template/dds/.clang-format new file mode 100644 index 0000000..f8a98eb --- /dev/null +++ b/Examples/template/dds/.clang-format @@ -0,0 +1,52 @@ +#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: 120 +BreakBeforeBraces: Custom +NamespaceIndentation: All +TabWidth: 4 +IndentCaseLabels: true +AlignAfterOpenBracket: Align +PointerAlignment: Right +AlignOperands: true +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortLoopsOnASingleLine: true +AllowShortIfStatementsOnASingleLine: true +BinPackArguments: true +BinPackParameters: true +BreakBeforeBinaryOperators: None +BreakBeforeTernaryOperators: false +BreakAfterJavaFieldAnnotations: true +AlignTrailingComments: true +BreakConstructorInitializers: AfterColon +AlignConsecutiveMacros: + Enabled: true + AcrossEmptyLines: true + AcrossComments: true +AlignArrayOfStructures: Right +AlignConsecutiveAssignments: + Enabled: true + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: true + PadOperators: true +AlignConsecutiveBitFields: + Enabled: true + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: true + PadOperators: true +AlignEscapedNewlines: Right +... diff --git a/Examples/template/dds/.cproject b/Examples/template/dds/.cproject new file mode 100644 index 0000000..1a0019e --- /dev/null +++ b/Examples/template/dds/.cproject @@ -0,0 +1,294 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Examples/template/dds/.project b/Examples/template/dds/.project new file mode 100644 index 0000000..2183c12 --- /dev/null +++ b/Examples/template/dds/.project @@ -0,0 +1,54 @@ + + + Project + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + + + Common + 2 + PARENT-3-PROJECT_LOC/Common + + + Drivers + 2 + PARENT-3-PROJECT_LOC/Drivers + + + Profiles + 2 + PARENT-3-PROJECT_LOC/Tools/scripts/Profiles + + + + + 0 + + 6 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-*.nuproject + + + + diff --git a/Examples/template/dds/Project.nuproject b/Examples/template/dds/Project.nuproject new file mode 100644 index 0000000..0072223 --- /dev/null +++ b/Examples/template/dds/Project.nuproject @@ -0,0 +1,25 @@ +SDK_CONFIG: + Debug: + core: N203E + extra_asmflags: + archext: + extra_cxxflags: + cmodel: medlow + extra_commonflags: + abi: ilp32e + arch: rv32ema_zba_zbb_zbs_zca_zcb_zcmp_zcmt_zicond + toolchain: gnu + extra_cflags: + extra_ldflags: + Release: + core: N203E + extra_asmflags: + archext: + extra_cxxflags: + cmodel: medlow + extra_commonflags: + abi: ilp32e + arch: rv32emac_zba_zbb_zbs + toolchain: gnu + extra_cflags: + extra_ldflags: diff --git a/Examples/template/dds/component.mk b/Examples/template/dds/component.mk new file mode 100644 index 0000000..b1b2bc6 --- /dev/null +++ b/Examples/template/dds/component.mk @@ -0,0 +1,16 @@ +# +# component Makefile +# + + +C_SOURCES += \ + $(srctree)/$(PROJ)/src/isr.c \ + $(srctree)/$(PROJ)/src/main.c + +C_INCLUDES += -I$(srctree)/$(PROJ)/inc + + +LIBS += +CFLAGS += +LDFLAGS += +LIBDIR += diff --git a/Examples/template/dds/inc/init.h b/Examples/template/dds/inc/init.h new file mode 100644 index 0000000..4d641d8 --- /dev/null +++ b/Examples/template/dds/inc/init.h @@ -0,0 +1,23 @@ +/** + * Copyright (c) 2026 Wisetop. All Rights Reserved. + */ +/** @file init.h + * + * @author Roy Luo + * @version 0.1 + * @date 2026/03/20 + * @license + * @description + */ + +#ifndef INC_INIT_H_ +#define INC_INIT_H_ + +void SYS_Config(); +void gpio_config(); +void pwm_config(); +void pwm_set_duty(uint16_t ch1_val, uint16_t ch2_val, uint16_t ch3_val); +void pwm_enable(); +void pwm_disable(); +void pwm_brake(); +#endif /* INC_INIT_H_ */ diff --git a/Examples/template/dds/inc/isr.h b/Examples/template/dds/inc/isr.h new file mode 100644 index 0000000..13e3d9a --- /dev/null +++ b/Examples/template/dds/inc/isr.h @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2026 Wisetop. All Rights Reserved. + */ +/** @file isr.h + * + * @author Roy Luo + * @version 0.1 + * @date 2026/03/20 + * @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 + //============================================================================= + extern volatile bool flag_24Khz_timer; + //============================================================================= + // Private Function Definition + //============================================================================= + + //============================================================================= + // Public Function Definition + //============================================================================= + void isr_epwm_handle(void); +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Examples/template/dds/inc/main.h b/Examples/template/dds/inc/main.h new file mode 100644 index 0000000..3d593ec --- /dev/null +++ b/Examples/template/dds/inc/main.h @@ -0,0 +1,51 @@ +/** + * Copyright (c) 2026 Wisetop. All Rights Reserved. + */ +/** @file main.h + * + * @author Roy Luo + * @version 0.1 + * @date 2026/03/20 + * @license + * @description + */ + +#ifndef __main_H_wxBnwdPw_lq7D_H4I5_sHPp_uXof15pFLp4V__ +#define __main_H_wxBnwdPw_lq7D_H4I5_sHPp_uXof15pFLp4V__ + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include "hal_device.h" +#include "syslog.h" + //============================================================================= + // Constant Definition + //============================================================================= + + //============================================================================= + // Macro Definition + //============================================================================= + + //============================================================================= + // Structure Definition + //============================================================================= + + //============================================================================= + // Global Data Definition + //============================================================================= + + //============================================================================= + // Private Function Definition + //============================================================================= + + //============================================================================= + // Public Function Definition + //============================================================================= + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Examples/template/dds/readme.md b/Examples/template/dds/readme.md new file mode 100644 index 0000000..a718da5 --- /dev/null +++ b/Examples/template/dds/readme.md @@ -0,0 +1,8 @@ +demo_project +--- + +This example is a demo project to descript `how to log message` + +## Log + +Use serial port with baudrate 115200, 8bit, no parity check. diff --git a/Examples/template/dds/src/init.c b/Examples/template/dds/src/init.c new file mode 100644 index 0000000..044db9e --- /dev/null +++ b/Examples/template/dds/src/init.c @@ -0,0 +1,143 @@ +/** + * Copyright (c) 2026 Wisetop. All Rights Reserved. + */ +/** @file init.c + * + * @author Roy Luo + * @version 0.1 + * @date 2026/03/20 + * @license + * @description + */ + +#include "init.h" +#include "hal_device.h" +#include "isr.h" +#include "main.h" + + +//============================================================================= +// Constant Definition +//============================================================================= +#define HIRC_Freq 60000000 // Hz +#define CurrentLoopIsrFreq 24000 // Hz +#define PWM_PERIOD HIRC_Freq / CurrentLoopIsrFreq +#define DEAD_TIME 200 // ns +#define DEAD_TIME_VALUE DEAD_TIME * 0.06 +//============================================================================= +// Public Function Definition +//============================================================================= +void SYS_Config() +{ + SYSCFG_ClkInitTypeDef SysClkInit = { 0 }; + + SysClkInit.ClkSource = SYSCFG_ClkSrc_HSI; + SYSCFG_SysClkConfig(&SysClkInit); + + sys_config_systick(SYS_TICK_1_MS); + + gpio_config(); + pwm_config(); +} + +void gpio_config() +{ + //-----------------// + // PWM + //-----------------// + GPIO_InitTypeDef GPIO_InitStruct; + GPIO_InitStruct.GPIO_Pin = GPIO_Pin_00 | GPIO_Pin_01 | GPIO_Pin_02 | GPIO_Pin_03 | GPIO_Pin_04 | GPIO_Pin_05; + GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; + GPIO_InitStruct.GPIO_AF_Mode = GPIO_AF_6; + GPIO_Init(GPIOA, &GPIO_InitStruct); + +#if 1 + //-----------------// + // test + //-----------------// + GPIO_InitTypeDef GPIO_TEST_InitStruct; + GPIO_TEST_InitStruct.GPIO_Pin = GPIO_Pin_14; + GPIO_TEST_InitStruct.GPIO_Mode = GPIO_Mode_OUT; + GPIO_Init(GPIOA, &GPIO_TEST_InitStruct); +#endif +} + +void pwm_config() +{ + 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 = PWM_PERIOD - 1; + 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); + + TIM_OCInitStruct.TIM_Pulse = 1; + TIM_OC3Init(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 |= 0x0800; // over flow + + // enable preload for CCR1, CCR2 and CCR3 + EPWM->CCMR1_OUTPUT_b.OC1PE = 1; + EPWM->CCMR1_OUTPUT_b.OC2PE = 1; + EPWM->CCMR2_OUTPUT_b.OC3PE = 1; + + __enable_irq(); + TIM_Cmd(EPWM, ENABLE); + TIM_CtrlPWMOutputs(EPWM, ENABLE); +} + +void pwm_set_duty(uint16_t ch1_val, uint16_t ch2_val, uint16_t ch3_val) +{ + EPWM->CCR1 = ch1_val; + EPWM->CCR2 = ch2_val; + EPWM->CCR3 = ch3_val; + return; +} +void pwm_enable() +{ + REG_SET_BITS(EPWM->CCER, TIM_CCER_CC1E_Msk | TIM_CCER_CC1NE_Msk | TIM_CCER_CC2E_Msk | TIM_CCER_CC2NE_Msk | + TIM_CCER_CC3E_Msk | TIM_CCER_CC3NE_Msk); + return; +} diff --git a/Examples/template/dds/src/isr.c b/Examples/template/dds/src/isr.c new file mode 100644 index 0000000..bab0801 --- /dev/null +++ b/Examples/template/dds/src/isr.c @@ -0,0 +1,43 @@ +/** + * Copyright (c) 2026 Wisetop. All Rights Reserved. + */ +/** @file isr.c + * + * @author Roy Luo + * @version 0.1 + * @date 2026/03/20 + * @license + * @description + */ +#include "isr.h" +#include "init.h" +#include "main.h" + + +//============================================================================= +// Constant Definition +//============================================================================= + +//============================================================================= +// Macro Definition +//============================================================================= + +//============================================================================= +// Global Data Definition +//============================================================================= +bool volatile flag_24Khz_timer = false; +//============================================================================= +// Private Function Definition +//============================================================================= + +//============================================================================= +// Public Function Definition +//============================================================================= +__INTERRUPT void isr_epwm_handle(void) +{ + if ((EPWM->SR & 0x10000) == 0x10000) + { + flag_24Khz_timer = true; + TIM_ClearITPendingBit(EPWM, 0x10000); + } +} diff --git a/Examples/template/dds/src/main.c b/Examples/template/dds/src/main.c new file mode 100644 index 0000000..1788539 --- /dev/null +++ b/Examples/template/dds/src/main.c @@ -0,0 +1,88 @@ +/** + * Copyright (c) 2026 Wisetop. All Rights Reserved. + */ +/** @file main.c + * + * @author Roy Luo + * @version 0.1 + * @date 2026/03/20 + * @license + * @description + */ + +#include "main.h" +#include "hal_gpio.h" +#include "hal_tim.h" +#include "init.h" +#include "isr.h" +//============================================================================= +// Constant Definition +//============================================================================= + +//============================================================================= +// Macro Definition +//============================================================================= +#define PWM_TABLE_SIZE 64 +//============================================================================= +// Structure Definition +//============================================================================= + +//============================================================================= +// Global Data Definition +//============================================================================= +const uint16_t pwm_table[PWM_TABLE_SIZE] = { 1250, 1372, 1493, 1612, 1728, 1839, 1944, 2042, 2133, 2216, 2289, + 2352, 2404, 2446, 2475, 2493, 2499, 2493, 2475, 2446, 2404, 2352, + 2289, 2216, 2133, 2042, 1944, 1839, 1728, 1612, 1493, 1372, 1250, + 1127, 1006, 887, 771, 660, 555, 457, 366, 283, 210, 147, + 95, 53, 24, 6, 0, 6, 24, 53, 95, 147, 210, + 283, 366, 457, 555, 660, 771, 887, 1006, 1127 }; +//============================================================================= +// Private Function Definition +//============================================================================= + +//============================================================================= +// Public Function Definition +//============================================================================= +void uvw_set_duty(uint16_t base_idx) +{ + uint16_t iA = base_idx; + uint16_t iB = (base_idx + PWM_TABLE_SIZE / 3) % PWM_TABLE_SIZE; + uint16_t iC = (base_idx + 2 * PWM_TABLE_SIZE / 3) % PWM_TABLE_SIZE; + + EPWM->CCR1 = pwm_table[iA]; + EPWM->CCR2 = pwm_table[iB]; + EPWM->CCR3 = pwm_table[iC]; +} + +int main(void) +{ + static uint16_t base_idx = 0; + uint32_t timer = sys_get_tick(); + + SYS_Config(); + uvw_set_duty(base_idx); + pwm_enable(); + + while (1) + { +#if 0 + if (sys_get_tick() - timer >= 1) + { + GPIO_TogglePin(GPIOA, GPIO_Pin_14); + base_idx = (base_idx + 1) % PWM_TABLE_SIZE; + uvw_set_duty(base_idx); + timer = sys_get_tick(); + } +#endif + +#if 1 + if (flag_24Khz_timer) + { + GPIO_TogglePin(GPIOA, GPIO_Pin_14); + base_idx = (base_idx + 1) % PWM_TABLE_SIZE; + uvw_set_duty(base_idx); + flag_24Khz_timer = false; + } +#endif + } +}