From f53cc9df17cbb787d36687a09b3d2afae25b07aa Mon Sep 17 00:00:00 2001 From: Roy_01 Date: Fri, 20 Mar 2026 14:56:43 +0800 Subject: [PATCH] [example] JF_01 & JF_02 --- Examples/template/JF_01/.cproject | 294 ++++++++++++++++++++++ Examples/template/JF_01/.project | 54 ++++ Examples/template/JF_01/Project.nuproject | 25 ++ Examples/template/JF_01/component.mk | 16 ++ Examples/template/JF_01/inc/init.h | 22 ++ Examples/template/JF_01/inc/isr.h | 50 ++++ Examples/template/JF_01/inc/main.h | 51 ++++ Examples/template/JF_01/readme.md | 8 + Examples/template/JF_01/src/init.c | 232 +++++++++++++++++ Examples/template/JF_01/src/isr.c | 55 ++++ Examples/template/JF_01/src/main.c | 53 ++++ Examples/template/JF_02/.cproject | 294 ++++++++++++++++++++++ Examples/template/JF_02/.project | 54 ++++ Examples/template/JF_02/Project.nuproject | 25 ++ Examples/template/JF_02/component.mk | 16 ++ Examples/template/JF_02/inc/init.h | 22 ++ Examples/template/JF_02/inc/isr.h | 50 ++++ Examples/template/JF_02/inc/main.h | 51 ++++ Examples/template/JF_02/readme.md | 8 + Examples/template/JF_02/src/init.c | 233 +++++++++++++++++ Examples/template/JF_02/src/isr.c | 56 +++++ Examples/template/JF_02/src/main.c | 53 ++++ 22 files changed, 1722 insertions(+) create mode 100644 Examples/template/JF_01/.cproject create mode 100644 Examples/template/JF_01/.project create mode 100644 Examples/template/JF_01/Project.nuproject create mode 100644 Examples/template/JF_01/component.mk create mode 100644 Examples/template/JF_01/inc/init.h create mode 100644 Examples/template/JF_01/inc/isr.h create mode 100644 Examples/template/JF_01/inc/main.h create mode 100644 Examples/template/JF_01/readme.md create mode 100644 Examples/template/JF_01/src/init.c create mode 100644 Examples/template/JF_01/src/isr.c create mode 100644 Examples/template/JF_01/src/main.c create mode 100644 Examples/template/JF_02/.cproject create mode 100644 Examples/template/JF_02/.project create mode 100644 Examples/template/JF_02/Project.nuproject create mode 100644 Examples/template/JF_02/component.mk create mode 100644 Examples/template/JF_02/inc/init.h create mode 100644 Examples/template/JF_02/inc/isr.h create mode 100644 Examples/template/JF_02/inc/main.h create mode 100644 Examples/template/JF_02/readme.md create mode 100644 Examples/template/JF_02/src/init.c create mode 100644 Examples/template/JF_02/src/isr.c create mode 100644 Examples/template/JF_02/src/main.c diff --git a/Examples/template/JF_01/.cproject b/Examples/template/JF_01/.cproject new file mode 100644 index 0000000..1a0019e --- /dev/null +++ b/Examples/template/JF_01/.cproject @@ -0,0 +1,294 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Examples/template/JF_01/.project b/Examples/template/JF_01/.project new file mode 100644 index 0000000..2183c12 --- /dev/null +++ b/Examples/template/JF_01/.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/JF_01/Project.nuproject b/Examples/template/JF_01/Project.nuproject new file mode 100644 index 0000000..0072223 --- /dev/null +++ b/Examples/template/JF_01/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/JF_01/component.mk b/Examples/template/JF_01/component.mk new file mode 100644 index 0000000..b1b2bc6 --- /dev/null +++ b/Examples/template/JF_01/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/JF_01/inc/init.h b/Examples/template/JF_01/inc/init.h new file mode 100644 index 0000000..adac39e --- /dev/null +++ b/Examples/template/JF_01/inc/init.h @@ -0,0 +1,22 @@ +/* + * init.h + * + * Created on: 2025年10月9日 + * Author: jim_liao + */ + +#ifndef INC_INIT_H_ +#define INC_INIT_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/Examples/template/JF_01/inc/isr.h b/Examples/template/JF_01/inc/isr.h new file mode 100644 index 0000000..3c103a3 --- /dev/null +++ b/Examples/template/JF_01/inc/isr.h @@ -0,0 +1,50 @@ +/** + * 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_epwm_handle(void); +void isr_adc_handle(void); +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Examples/template/JF_01/inc/main.h b/Examples/template/JF_01/inc/main.h new file mode 100644 index 0000000..6bc013b --- /dev/null +++ b/Examples/template/JF_01/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/Examples/template/JF_01/readme.md b/Examples/template/JF_01/readme.md new file mode 100644 index 0000000..a718da5 --- /dev/null +++ b/Examples/template/JF_01/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/JF_01/src/init.c b/Examples/template/JF_01/src/init.c new file mode 100644 index 0000000..7f2038d --- /dev/null +++ b/Examples/template/JF_01/src/init.c @@ -0,0 +1,232 @@ +/* + * init.c + * + * Created on: 2025年8月18日 + * Author: jim_liao + */ + +#include "main.h" +#include "isr.h" +#include "init.h" +#include "hal_device.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 100000 //Hz +#define PWM_PERIOD HIRC_Freq/CurrentLoopIsrFreq/2 +#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); + + gpio_config(); + opa_config(); + adc_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); +//-----------------// +// UART +// PA15:TX PB0:RX +//-----------------// + GPIO_InitTypeDef GPIOA_InitUart; + GPIOA_InitUart.GPIO_Pin = GPIO_Pin_15; + GPIOA_InitUart.GPIO_Mode = GPIO_Mode_AF; + GPIOA_InitUart.GPIO_AF_Mode = GPIO_AF_1; + GPIO_Init(GPIOA, &GPIOA_InitUart); + + GPIO_InitTypeDef GPIOB_InitUart; + GPIOB_InitUart.GPIO_Pin = GPIO_Pin_00; + 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_10|GPIO_Pin_11|GPIO_Pin_12 | GPIO_Pin_13; + GPIO_InitADC.GPIO_Mode = GPIO_Mode_ANAL; + GPIO_Init(GPIOA, &GPIO_InitADC); +#if 1 +//-----------------// +// test +//-----------------// +// GPIO_InitStruct.GPIO_Pin = GPIO_Pin_06 | GPIO_Pin_07; + GPIO_InitStruct.GPIO_Pin = GPIO_Pin_06; + 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; + 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_00 | ADC_Channel_04 | ADC_Channel_05 |ADC_Channel_06 |ADC_Channel_07; + 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_CH2R,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_CenterAligned1; + 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); + + + + { /* 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; //under flow + __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; +} + + + +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); +} + +void UART_Send_Wait(UART_Type *pHUart, uint16_t value) +{ + UART_SendData(pHUart,value); + UART_WaitTxFifoEmpty(pHUart); +} diff --git a/Examples/template/JF_01/src/isr.c b/Examples/template/JF_01/src/isr.c new file mode 100644 index 0000000..c794c24 --- /dev/null +++ b/Examples/template/JF_01/src/isr.c @@ -0,0 +1,55 @@ +/** + * 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 "main.h" +#include "isr.h" +#include "init.h" + +//============================================================================= +// Constant Definition +//============================================================================= + +//============================================================================= +// Macro Definition +//============================================================================= + +//============================================================================= +// Global Data Definition +//============================================================================= +long ADC_DATA[10]; +//============================================================================= +// Private Function Definition +//============================================================================= + +//============================================================================= +// Public Function Definition +//============================================================================= +__INTERRUPT void isr_epwm_handle(void) +{ + + if((EPWM->SR&0x20000) ==0x20000) + { + TIM_ClearITPendingBit(EPWM, 0x20000); + ADC_DATA[0] = ADC0->DAT4_b.DATA; + ADC_DATA[1] = ADC0->DAT5_b.DATA; + ADC_DATA[2] = ADC0->DAT6_b.DATA; + ADC_DATA[3] = ADC0->DAT7_b.DATA; + pwm_enable(); + pwm_set_duty(100,200,0); + GPIO_TogglePin(GPIOA, GPIO_Pin_06); + + } + +} + + + diff --git a/Examples/template/JF_01/src/main.c b/Examples/template/JF_01/src/main.c new file mode 100644 index 0000000..9dba9ba --- /dev/null +++ b/Examples/template/JF_01/src/main.c @@ -0,0 +1,53 @@ +/** + * 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 "main.h" +#include "isr.h" +#include "init.h" +#include "hal_tim.h" +#include "hal_gpio.h" +//============================================================================= +// Constant Definition +//============================================================================= + +//============================================================================= +// Macro Definition +//============================================================================= + +//============================================================================= +// Structure Definition +//============================================================================= + +//============================================================================= +// Global Data Definition +//============================================================================= + +//============================================================================= +// Private Function Definition +//============================================================================= + +//============================================================================= +// Public Function Definition +//============================================================================= +int main(void) +{ + + SYS_Config(); + + while(1) + { + + } +} + + diff --git a/Examples/template/JF_02/.cproject b/Examples/template/JF_02/.cproject new file mode 100644 index 0000000..1a0019e --- /dev/null +++ b/Examples/template/JF_02/.cproject @@ -0,0 +1,294 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Examples/template/JF_02/.project b/Examples/template/JF_02/.project new file mode 100644 index 0000000..2183c12 --- /dev/null +++ b/Examples/template/JF_02/.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/JF_02/Project.nuproject b/Examples/template/JF_02/Project.nuproject new file mode 100644 index 0000000..0072223 --- /dev/null +++ b/Examples/template/JF_02/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/JF_02/component.mk b/Examples/template/JF_02/component.mk new file mode 100644 index 0000000..b1b2bc6 --- /dev/null +++ b/Examples/template/JF_02/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/JF_02/inc/init.h b/Examples/template/JF_02/inc/init.h new file mode 100644 index 0000000..adac39e --- /dev/null +++ b/Examples/template/JF_02/inc/init.h @@ -0,0 +1,22 @@ +/* + * init.h + * + * Created on: 2025年10月9日 + * Author: jim_liao + */ + +#ifndef INC_INIT_H_ +#define INC_INIT_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/Examples/template/JF_02/inc/isr.h b/Examples/template/JF_02/inc/isr.h new file mode 100644 index 0000000..3c103a3 --- /dev/null +++ b/Examples/template/JF_02/inc/isr.h @@ -0,0 +1,50 @@ +/** + * 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_epwm_handle(void); +void isr_adc_handle(void); +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Examples/template/JF_02/inc/main.h b/Examples/template/JF_02/inc/main.h new file mode 100644 index 0000000..6bc013b --- /dev/null +++ b/Examples/template/JF_02/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/Examples/template/JF_02/readme.md b/Examples/template/JF_02/readme.md new file mode 100644 index 0000000..a718da5 --- /dev/null +++ b/Examples/template/JF_02/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/JF_02/src/init.c b/Examples/template/JF_02/src/init.c new file mode 100644 index 0000000..3265d96 --- /dev/null +++ b/Examples/template/JF_02/src/init.c @@ -0,0 +1,233 @@ +/* + * init.c + * + * Created on: 2025年8月18日 + * Author: jim_liao + */ + +#include "main.h" +#include "isr.h" +#include "init.h" +#include "hal_device.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/2 +#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); + + gpio_config(); + opa_config(); + adc_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); +//-----------------// +// UART +// PA15:TX PB0:RX +//-----------------// + GPIO_InitTypeDef GPIOA_InitUart; + GPIOA_InitUart.GPIO_Pin = GPIO_Pin_15; + GPIOA_InitUart.GPIO_Mode = GPIO_Mode_AF; + GPIOA_InitUart.GPIO_AF_Mode = GPIO_AF_1; + GPIO_Init(GPIOA, &GPIOA_InitUart); + + GPIO_InitTypeDef GPIOB_InitUart; + GPIOB_InitUart.GPIO_Pin = GPIO_Pin_00; + 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 0 +//-----------------// +// test +//-----------------// +// GPIO_InitStruct.GPIO_Pin = GPIO_Pin_06 | GPIO_Pin_07; + GPIO_InitStruct.GPIO_Pin = GPIO_Pin_06; + 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; + 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_CenterAligned1; + 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 |= 0x1000; //under flow + __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; +} + + + +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); +} + +void UART_Send_Wait(UART_Type *pHUart, uint16_t value) +{ + UART_SendData(pHUart,value); + UART_WaitTxFifoEmpty(pHUart); +} diff --git a/Examples/template/JF_02/src/isr.c b/Examples/template/JF_02/src/isr.c new file mode 100644 index 0000000..875d9d3 --- /dev/null +++ b/Examples/template/JF_02/src/isr.c @@ -0,0 +1,56 @@ +/** + * 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 "main.h" +#include "isr.h" +#include "init.h" + +//============================================================================= +// Constant Definition +//============================================================================= + +//============================================================================= +// Macro Definition +//============================================================================= + +//============================================================================= +// Global Data Definition +//============================================================================= +long ADC_DATA[10]; +//============================================================================= +// Private Function Definition +//============================================================================= + +//============================================================================= +// Public Function Definition +//============================================================================= +__INTERRUPT void isr_epwm_handle(void) +{ + + if((EPWM->SR&0x20000) ==0x20000) + { + TIM_ClearITPendingBit(EPWM, 0x20000); + EPWM->CCR4 = 200; + EPWM->CCDR4 = 100; + ADC_DATA[0] = ADC0->DAT6_b.DATA; + ADC_DATA[1] = ADC0->DAT8_b.DATA; + + pwm_enable(); + +// GPIO_TogglePin(GPIOA, GPIO_Pin_06); + + } + +} + + + diff --git a/Examples/template/JF_02/src/main.c b/Examples/template/JF_02/src/main.c new file mode 100644 index 0000000..fa6dc97 --- /dev/null +++ b/Examples/template/JF_02/src/main.c @@ -0,0 +1,53 @@ +/** + * 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 "main.h" +#include "isr.h" +#include "init.h" +#include "hal_tim.h" +#include "hal_gpio.h" +//============================================================================= +// Constant Definition +//============================================================================= + +//============================================================================= +// Macro Definition +//============================================================================= + +//============================================================================= +// Structure Definition +//============================================================================= + +//============================================================================= +// Global Data Definition +//============================================================================= + +//============================================================================= +// Private Function Definition +//============================================================================= + +//============================================================================= +// Public Function Definition +//============================================================================= +int main(void) +{ + + SYS_Config(); + + while(1) + { + pwm_set_duty(100,200,500); + } +} + +