From aab1672cb2d1956ae4bc1b160ffc6482c3e0a3a9 Mon Sep 17 00:00:00 2001 From: Roy_01 Date: Thu, 26 Mar 2026 13:13:47 +0800 Subject: [PATCH] [example] add JF_02_motor example --- .../template/JF_02_motor/.clang-format | 52 ++++ .../Examples/template/JF_02_motor/.cproject | 294 ++++++++++++++++++ .../Examples/template/JF_02_motor/.project | 54 ++++ .../template/JF_02_motor/Project.nuproject | 25 ++ .../template/JF_02_motor/component.mk | 16 + .../Examples/template/JF_02_motor/inc/init.h | 24 ++ .../Examples/template/JF_02_motor/inc/isr.h | 54 ++++ .../Examples/template/JF_02_motor/inc/main.h | 51 +++ .../Examples/template/JF_02_motor/readme.md | 8 + .../Examples/template/JF_02_motor/src/init.c | 258 +++++++++++++++ .../Examples/template/JF_02_motor/src/isr.c | 75 +++++ .../Examples/template/JF_02_motor/src/main.c | 127 ++++++++ 12 files changed, 1038 insertions(+) create mode 100644 pec930_sdk-v1.0.1/Examples/template/JF_02_motor/.clang-format create mode 100644 pec930_sdk-v1.0.1/Examples/template/JF_02_motor/.cproject create mode 100644 pec930_sdk-v1.0.1/Examples/template/JF_02_motor/.project create mode 100644 pec930_sdk-v1.0.1/Examples/template/JF_02_motor/Project.nuproject create mode 100644 pec930_sdk-v1.0.1/Examples/template/JF_02_motor/component.mk create mode 100644 pec930_sdk-v1.0.1/Examples/template/JF_02_motor/inc/init.h create mode 100644 pec930_sdk-v1.0.1/Examples/template/JF_02_motor/inc/isr.h create mode 100644 pec930_sdk-v1.0.1/Examples/template/JF_02_motor/inc/main.h create mode 100644 pec930_sdk-v1.0.1/Examples/template/JF_02_motor/readme.md create mode 100644 pec930_sdk-v1.0.1/Examples/template/JF_02_motor/src/init.c create mode 100644 pec930_sdk-v1.0.1/Examples/template/JF_02_motor/src/isr.c create mode 100644 pec930_sdk-v1.0.1/Examples/template/JF_02_motor/src/main.c diff --git a/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/.clang-format b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/.clang-format new file mode 100644 index 0000000..73371c5 --- /dev/null +++ b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/.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: 80 +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: All +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/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/.cproject b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/.cproject new file mode 100644 index 0000000..1a0019e --- /dev/null +++ b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/.cproject @@ -0,0 +1,294 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/.project b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/.project new file mode 100644 index 0000000..2183c12 --- /dev/null +++ b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/.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/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/Project.nuproject b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/Project.nuproject new file mode 100644 index 0000000..0072223 --- /dev/null +++ b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/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/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/component.mk b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/component.mk new file mode 100644 index 0000000..b1b2bc6 --- /dev/null +++ b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/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/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/inc/init.h b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/inc/init.h new file mode 100644 index 0000000..5072036 --- /dev/null +++ b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/inc/init.h @@ -0,0 +1,24 @@ +/* + * init.h + * + * Created on: 2025年10月9日 + * Author: jim_liao + */ + +#ifndef INC_INIT_H_ +#define INC_INIT_H_ + +#include "main.h" + +void SYS_Config(); +void gpio_config(); +void opa_config(); +void adc_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(); +void uart_config(); +void UART_Send_Wait(UART_Type *pHUart, uint16_t value); +#endif /* INC_INIT_H_ */ diff --git a/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/inc/isr.h b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/inc/isr.h new file mode 100644 index 0000000..8effcac --- /dev/null +++ b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/inc/isr.h @@ -0,0 +1,54 @@ +/** + * 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 + //============================================================================= + extern volatile bool flag_24Khz_timer; + extern volatile bool stringComplete; + extern volatile char rxBuffer[128]; + //============================================================================= + // Private Function Definition + //============================================================================= + + //============================================================================= + // Public Function Definition + //============================================================================= + void isr_epwm_handle(void); + void isr_adc_handle(void); + void isr_uart0_handle(void); +#ifdef __cplusplus +} +#endif + +#endif diff --git a/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/inc/main.h b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/inc/main.h new file mode 100644 index 0000000..f49530e --- /dev/null +++ b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/inc/main.h @@ -0,0 +1,51 @@ +/** + * Copyright (c) 2024 Wei-Lun Hsu. All Rights Reserved. + */ +/** @file main.h + * + * @author Wei-Lun Hsu + * @version 0.1 + * @date 2024/08/29 + * @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/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/readme.md b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/readme.md new file mode 100644 index 0000000..a718da5 --- /dev/null +++ b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/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/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/src/init.c b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/src/init.c new file mode 100644 index 0000000..a7c1eeb --- /dev/null +++ b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/src/init.c @@ -0,0 +1,258 @@ +/* + * init.c + * + * Created on: 2025年8月18日 + * Author: jim_liao + */ +#include "init.h" +#include "hal_device.h" +#include "isr.h" +#include "main.h" + + +//============================================================================= +// Constant Definition +//============================================================================= +#define CONFIG_OPA_PGA_GAIN OPAMP_PGAGain_5 + +unsigned long ADC0Sum = 0; +unsigned char k; + +#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(); + opa_config(); + adc_config(); + pwm_config(); + uart_config(); + + __enable_irq(); +} + +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); + //-----------------// + // UART + // PB3:TX PB5:RX + //-----------------// + GPIO_InitTypeDef GPIOA_InitUart; + GPIOA_InitUart.GPIO_Pin = GPIO_Pin_03; + GPIOA_InitUart.GPIO_Mode = GPIO_Mode_AF; + GPIOA_InitUart.GPIO_AF_Mode = GPIO_AF_1; + GPIO_Init(GPIOB, &GPIOA_InitUart); + + GPIO_InitTypeDef GPIOB_InitUart; + GPIOB_InitUart.GPIO_Pin = GPIO_Pin_05; + GPIOB_InitUart.GPIO_Mode = GPIO_Mode_AF; + GPIOB_InitUart.GPIO_AF_Mode = GPIO_AF_1; + GPIO_Init(GPIOB, &GPIOB_InitUart); + //-----------------// + // ADC + // PA12/13/14:OVP/OTP/CMD + //-----------------// + GPIO_InitTypeDef GPIO_InitADC; + GPIO_InitADC.GPIO_Pin = GPIO_Pin_06 | GPIO_Pin_12; + GPIO_InitADC.GPIO_Mode = GPIO_Mode_ANAL; + GPIO_Init(GPIOA, &GPIO_InitADC); +#if 1 + //-----------------// + // test + //-----------------// + GPIO_InitStruct.GPIO_Pin = GPIO_Pin_14; + GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; + GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; + 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; + opa_init.OPAMP_Gain = CONFIG_OPA_PGA_GAIN; + + OPAMP_Init(OPAMP0, &opa_init); + OPAMP_Init(OPAMP1, &opa_init); + + OPAMP_Enable(OPAMP0); + OPAMP_Enable(OPAMP1); +} + +void adc_config(void) +{ + + ADC_InitTypeDef init = { 0 }; + ADC_StructInit(&init); + + //-----------------// + // set + //-----------------// + ADC_InitTypeDef init1 = { 0 }; + ADC_StructInit(&init1); + init1.SelChannels = ADC_Channel_06 | ADC_Channel_08; + init1.ClkPrescaler = ADC_ClkDiv_4; + init1.DataAlign = ADC_DataAlign_Right; + init1.Mode = ADC_Mode_Scan; + ADC_Init(ADC0, &init1); + + ADC_ExtTrigConfig(ADC0, ADC_ExtTrigSource_EPWM_CCR4_UP, + ADC_ExtTrigMode_Enable); + ADC_ExtTrigConfig(ADC0, ADC_ExtTrigSource_EPWM_CCDR4_UP, + ADC_ExtTrigMode_Enable); + +#if 0 + { /* 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 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; + + 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; +} + +void pwm_disable() +{ + REG_CLR_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; +} + +void uart_config() +{ + UART_InitTypeDef uart_init = { 0 }; + uart_init.BaudRate = 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); + + UART_ITConfig(UART0, UART_FLAG_RXNE, ENABLE); + { /* 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(UART0_IRQn, isr_uart0_handle, &irq_attr); + } +} + +void UART_Send_Wait(UART_Type *pHUart, uint16_t value) +{ + UART_SendData(pHUart, value); + UART_WaitTxFifoEmpty(pHUart); +} diff --git a/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/src/isr.c b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/src/isr.c new file mode 100644 index 0000000..275e720 --- /dev/null +++ b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/src/isr.c @@ -0,0 +1,75 @@ +/** + * 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" +#include "main.h" + + +//============================================================================= +// Constant Definition +//============================================================================= + +//============================================================================= +// Macro Definition +//============================================================================= +#define RX_BUFFER_SIZE 128 +//============================================================================= +// Global Data Definition +//============================================================================= +long ADC_DATA[10]; +volatile bool flag_24Khz_timer = false; + +volatile char rxBuffer[RX_BUFFER_SIZE]; +volatile bool stringComplete = false; +volatile uint16_t rxIndex = 0; +//============================================================================= +// Private Function Definition +//============================================================================= + +//============================================================================= +// Public Function Definition +//============================================================================= +__INTERRUPT void isr_epwm_handle(void) +{ + + if ((EPWM->SR & 0x10000) == 0x10000) + { + flag_24Khz_timer = true; + ADC_DATA[0] = ADC0->DAT6_b.DATA; + ADC_DATA[1] = ADC0->DAT8_b.DATA; + TIM_ClearITPendingBit(EPWM, 0x10000); // clear OVIF + } +} + +__INTERRUPT void isr_uart0_handle(void) +{ + if (UART_GetITStatus(UART0, UART_FLAG_RXNE) == 1) + { + char receivedChar = (char)UART_ReceiveData(UART0); + + if (receivedChar == '\n' || receivedChar == '\r') + { + if (rxIndex > 0) + { + rxBuffer[rxIndex] = '\0'; + stringComplete = true; + rxIndex = 0; + msg("Received: %s\n", rxBuffer); + } + } + else if (rxIndex < RX_BUFFER_SIZE - 1) + { + rxBuffer[rxIndex++] = receivedChar; + } + } +} diff --git a/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/src/main.c b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/src/main.c new file mode 100644 index 0000000..e695f08 --- /dev/null +++ b/pec930_sdk-v1.0.1/Examples/template/JF_02_motor/src/main.c @@ -0,0 +1,127 @@ +/** + * Copyright (c) 2024 Wei-Lun Hsu. All Rights Reserved. + */ +/** @file main.c + * + * @author Wei-Lun Hsu + * @version 0.1 + * @date 2024/08/29 + * @license + * @description + */ +#include + +#include "main.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]; +} + +static void cmd_parser(char *data, uint32_t len) +{ + char cmd_reply[64] = { 0 }; + + // echo back received command + sprintf(cmd_reply, "Received command: %s\r\n", data); + msg("%s", cmd_reply); + + // parse commands + if (strncmp(data, "help", 4) == 0) + { + msg("Available commands:\r\n"); + msg(" help - Show this help message\r\n"); + msg(" fw_ver - Show firmware version\r\n"); + } + else if (strncmp(data, "fw_ver", 6) == 0) + { + msg("FW_VER_1.0.0_20260320\r\n"); + } + else if (strncmp(data, "pwm_enable", 10) == 0) + { + pwm_enable(); + msg("PWM enabled.\r\n"); + } + else if (strncmp(data, "pwm_disable", 11) == 0) + { + pwm_disable(); + msg("PWM disabled.\r\n"); + } + else + { + msg("Unknown command. Type 'help' for available commands.\r\n"); + } +} + +int main(void) +{ + uint16_t base_idx = 0; + uint32_t timer = sys_get_tick(); + + SYS_Config(); + info("This is a demo project: JF_02\n"); + uvw_set_duty(base_idx); + pwm_enable(); + + while (1) + { + if (stringComplete) + { + cmd_parser((char *)rxBuffer, strlen((char *)rxBuffer)); + stringComplete = false; + } + + if (sys_get_tick() - timer >= 1) + { + msg("%d tick\n", sys_get_tick()); + timer = sys_get_tick(); + } + + if (flag_24Khz_timer) + { + base_idx = (base_idx + 1) % PWM_TABLE_SIZE; + uvw_set_duty(base_idx); + flag_24Khz_timer = false; + } + } +}