#include "gd32F30x.h" #include /* log2024-3-14: Power use PV-Source */ //static __inline__ void __attribute__((__always_inline__, __nodebug__)) __nop(void) { // __builtin_arm_nop(); //} #define __nop() __NOP() #define NOP __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();\ __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();\ __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();\ __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();\ __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();\ __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();\ __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();\ __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();\ __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();\ __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();\ __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();\ __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop(); unsigned short adc_value[10]; extern unsigned short adc_zero; void bemf_adc_init(void); /*! \brief configure the TIMER peripheral \param[in] none \param[out] none \retval none */ void timer2_config(void) { /* ---------------------------------------------------------------------------- TIMER2 Configuration: TIMER2CLK = SystemCoreClock/180 = 1mHz, the period is 65535us. ---------------------------------------------------------------------------- */ // timer_oc_parameter_struct timer_ocinitpara; timer_parameter_struct timer_initpara; /* enable the peripherals clock */ rcu_periph_clock_enable(RCU_TIMER2); /* deinit a TIMER */ timer_deinit(TIMER2); /* initialize TIMER init parameter struct */ timer_struct_para_init(&timer_initpara); /* TIMER2 configuration */ timer_initpara.prescaler = 119; timer_initpara.alignedmode = TIMER_COUNTER_EDGE; timer_initpara.counterdirection = TIMER_COUNTER_UP; timer_initpara.period = 100; timer_initpara.clockdivision = TIMER_CKDIV_DIV1; timer_init(TIMER2, &timer_initpara); /* enable the TIMER interrupt */ timer_interrupt_enable(TIMER2, TIMER_INT_UP); /* enable a TIMER */ timer_enable(TIMER2); } /*! \brief configure the TIMER peripheral \param[in] none \param[out] none \retval none */ void timer0_config(void) { /* ----------------------------------------------------------------------- TIMER0 configuration: generate 3 complementary PWM signal. TIMER0CLK is fixed to systemcoreclock, the TIMER0 prescaler is equal to 180 -120 so the TIMER0 counter clock used is 1MHz. insert a dead time equal to 164/systemcoreclock = 2.28us configure the break feature, active at low level, and using the automatic output enable feature. use the locking parameters level 0. ----------------------------------------------------------------------- */ timer_oc_parameter_struct timer_ocinitpara; timer_parameter_struct timer_initpara; // timer_break_parameter_struct timer_breakpara; rcu_periph_clock_enable(RCU_TIMER0); timer_deinit(TIMER0); /* TIMER0 configuration */ timer_initpara.prescaler = 40;//80;//24-2-5 80 timer_initpara.alignedmode = TIMER_COUNTER_CENTER_UP;//TIMER_COUNTER_EDGE;// timer_initpara.counterdirection = TIMER_COUNTER_UP; timer_initpara.period = 100; timer_initpara.clockdivision = TIMER_CKDIV_DIV1; timer_initpara.repetitioncounter = 0; timer_init(TIMER0,&timer_initpara); /* initialize TIMER channel output parameter struct */ timer_channel_output_struct_para_init(&timer_ocinitpara); /* CH0/CH0N, CH1/CH1N and CH2/CH2N configuration in timing mode */ timer_ocinitpara.outputstate = TIMER_CCX_ENABLE; timer_ocinitpara.outputnstate = TIMER_CCXN_DISABLE; timer_ocinitpara.ocpolarity = TIMER_OC_POLARITY_HIGH; timer_ocinitpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH; timer_ocinitpara.ocidlestate = TIMER_OC_IDLE_STATE_HIGH; timer_ocinitpara.ocnidlestate = TIMER_OCN_IDLE_STATE_HIGH; timer_channel_output_config(TIMER0, TIMER_CH_0, &timer_ocinitpara); timer_channel_output_config(TIMER0, TIMER_CH_1, &timer_ocinitpara); timer_channel_output_config(TIMER0, TIMER_CH_2, &timer_ocinitpara); timer_channel_output_config(TIMER0, TIMER_CH_3, &timer_ocinitpara);//2024-01-08 for adctrig timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,0); timer_channel_output_mode_config(TIMER0,TIMER_CH_0,TIMER_OC_MODE_PWM0); timer_channel_output_shadow_config(TIMER0,TIMER_CH_0,TIMER_OC_SHADOW_ENABLE); timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_1,0); timer_channel_output_mode_config(TIMER0,TIMER_CH_1,TIMER_OC_MODE_PWM0); timer_channel_output_shadow_config(TIMER0,TIMER_CH_1,TIMER_OC_SHADOW_ENABLE); timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_2,0); timer_channel_output_mode_config(TIMER0,TIMER_CH_2,TIMER_OC_MODE_PWM0); timer_channel_output_shadow_config(TIMER0,TIMER_CH_2,TIMER_OC_SHADOW_ENABLE); timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_3,1);//2024-01-08 for adctrig timer_channel_output_mode_config(TIMER0,TIMER_CH_3,TIMER_OC_MODE_PWM0); timer_channel_output_shadow_config(TIMER0,TIMER_CH_3,TIMER_OC_SHADOW_DISABLE); // /* automatic output enable, break, dead time and lock configuration*/ // timer_breakpara.runoffstate = TIMER_ROS_STATE_ENABLE; // timer_breakpara.ideloffstate = TIMER_IOS_STATE_ENABLE ; // timer_breakpara.deadtime = 100; // timer_breakpara.breakpolarity = TIMER_BREAK_POLARITY_LOW; // timer_breakpara.outputautostate = TIMER_OUTAUTO_ENABLE; // timer_breakpara.protectmode = TIMER_CCHP_PROT_OFF; // timer_breakpara.breakstate = TIMER_BREAK_DISABLE; // timer_break_config(TIMER0,&timer_breakpara); // /* TIMER0 primary output function enable */ timer_primary_output_config(TIMER0,ENABLE); // /* TIMER0 channel control update interrupt enable */ // timer_interrupt_enable(TIMER0,TIMER_INT_CMT); // /* TIMER0 break interrupt disable */ // timer_interrupt_disable(TIMER0,TIMER_INT_BRK); /* auto-reload preload enable */ timer_auto_reload_shadow_enable(TIMER0); /* TIMER0 counter enable */ timer_enable(TIMER0); timer_channel_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCX_ENABLE); // timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_0,TIMER_CCXN_ENABLE); /* channel1 configuration */ timer_channel_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCX_ENABLE); // timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_1,TIMER_CCXN_ENABLE); /* channel2 configuration */ timer_channel_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCX_ENABLE);//W // timer_channel_complementary_output_state_config(TIMER0,TIMER_CH_2,TIMER_CCXN_ENABLE);//W } /*! \brief configure the GPIO ports \param[in] none \param[out] none \retval none */ void bemf_gpio_config(void) { rcu_periph_clock_enable(RCU_GPIOE); rcu_periph_clock_enable(RCU_AF); gpio_pin_remap_config(GPIO_TIMER0_FULL_REMAP,ENABLE); gpio_init(GPIOE,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_9); gpio_init(GPIOE,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_11); gpio_init(GPIOE,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_13);//bemf_PWM gpio_init(GPIOE,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_8); gpio_init(GPIOE,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_10); gpio_init(GPIOE,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_12);//bemf_PWMN gpio_init(GPIOE,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_14); gpio_init(GPIOE,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_7); gpio_init(GPIOE,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_15);//bemf_input } void tim_init()//API { // bemf_gpio_config();//1/5 // timer0_config();//2/5 // timer2_config();//3/5 // nvic_irq_enable(TIMER2_IRQn, 0, 0);//4/5 // bemf_adc_init();//5/5 } void adc_delay_us(unsigned int i) { while(i--) { NOP; } } /*! \brief configure the different system clocks \param[in] none \param[out] none \retval none */ void bemf_adc_rcu_config(void) { /* enable ADC clock */ rcu_periph_clock_enable(RCU_ADC0); /* config ADC clock */ rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV4); } /*! \brief configure the GPIO peripheral \param[in] none \param[out] none \retval none */ void bemf_adc_gpio_config(void) { /* enable GPIO clock */ rcu_periph_clock_enable(RCU_GPIOA); /* config the GPIO as analog mode */ gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_MAX, GPIO_PIN_4);//vm gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_MAX, GPIO_PIN_5);//cs } /*! \brief configure the DMA peripheral \param[in] none \param[out] none \retval none */ void bemf_adc_dma_config(void) { /* ADC_DMA_channel configuration */ dma_parameter_struct dma_data_parameter; /* ADC DMA_channel configuration */ dma_deinit(DMA0, DMA_CH0); /* enable DMA0 clock */ rcu_periph_clock_enable(RCU_DMA0); /* initialize DMA single data mode */ dma_data_parameter.periph_addr = (uint32_t)(&ADC_RDATA(ADC0)); dma_data_parameter.periph_inc = DMA_PERIPH_INCREASE_DISABLE; dma_data_parameter.memory_addr = (uint32_t)(&adc_value); dma_data_parameter.memory_inc = DMA_MEMORY_INCREASE_ENABLE; dma_data_parameter.periph_width = DMA_PERIPHERAL_WIDTH_16BIT; dma_data_parameter.memory_width = DMA_MEMORY_WIDTH_16BIT; dma_data_parameter.direction = DMA_PERIPHERAL_TO_MEMORY; dma_data_parameter.number = 2; dma_data_parameter.priority = DMA_PRIORITY_HIGH; dma_init(DMA0, DMA_CH0, &dma_data_parameter); dma_circulation_enable(DMA0, DMA_CH0); /* enable DMA channel */ dma_channel_enable(DMA0, DMA_CH0); } void bemf_adc_regular_dma_config(void) { /* DMA configuration */ bemf_adc_dma_config(); /* ADC mode config */ adc_mode_config(ADC_MODE_FREE); /* ADC continuous function enable */ adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, DISABLE); /* ADC scan function enable */ adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE); /* ADC data alignment config */ adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT); /* ADC channel length config */ adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 2); /* ADC INSERTED channel config */ adc_inserted_channel_config(ADC0, 0, ADC_CHANNEL_4, ADC_SAMPLETIME_55POINT5); adc_inserted_channel_config(ADC0, 1, ADC_CHANNEL_5, ADC_SAMPLETIME_55POINT5); /* ADC trigger config */ adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_EXTTRIG_REGULAR_T0_CH2);//ADC0_1_2_EXTTRIG_REGULAR_NONE );// ); adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE); /* clear the ADC flag */ //adc_interrupt_flag_clear(ADC0, ADC_INT_FLAG_EOC);//use it when no dma // adc_interrupt_flag_clear(ADC0, ADC_INT_FLAG_EOIC); /* enable ADC interrupt */ // adc_interrupt_enable(ADC0, ADC_INT_EOIC);//use it when no dma /* ADC DMA function enable */ adc_dma_mode_enable(ADC0); /* enable ADC interface */ adc_enable(ADC0); //delay_1ms(1); adc_delay_us(1000); /* ADC calibration and reset calibration */ adc_calibration_enable(ADC0); /* ADC software trigger enable */ // adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL); //nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);//use it when no dma //nvic_irq_enable(ADC0_1_IRQn, 0, 0);//use it when no dma gpio_bit_reset(GPIOE, GPIO_PIN_8); gpio_bit_reset(GPIOE, GPIO_PIN_10); gpio_bit_reset(GPIOE, GPIO_PIN_12); timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_0,0); timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_1,0); timer_channel_output_pulse_value_config(TIMER0,TIMER_CH_2,10); } /*! \brief configure the ADC peripheral \param[in] none \param[out] none \retval none */ void bemf_adc_inserted_config(void) { /* ADC mode config */ adc_mode_config(ADC_MODE_FREE); /* ADC continuous function enable */ adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, DISABLE); /* ADC scan function enable */ adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE); /* ADC data alignment config */ adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT); /* ADC channel length config */ adc_channel_length_config(ADC0, ADC_INSERTED_CHANNEL, 2); /* ADC INSERTED channel config */ adc_inserted_channel_config(ADC0, 0, ADC_CHANNEL_4, ADC_SAMPLETIME_55POINT5); adc_inserted_channel_config(ADC0, 1, ADC_CHANNEL_5, ADC_SAMPLETIME_55POINT5); /* ADC trigger config */ adc_external_trigger_source_config(ADC0, ADC_INSERTED_CHANNEL, ADC0_1_EXTTRIG_INSERTED_T0_CH3);//ADC0_1_2_EXTTRIG_INSERTED_NONE );//); adc_external_trigger_config(ADC0, ADC_INSERTED_CHANNEL, ENABLE); /* clear the ADC flag */ //adc_interrupt_flag_clear(ADC0, ADC_INT_FLAG_EOC);//use it when no dma adc_interrupt_flag_clear(ADC0, ADC_INT_FLAG_EOIC); /* enable ADC interrupt */ adc_interrupt_enable(ADC0, ADC_INT_EOIC);//use it when no dma /* ADC DMA function enable */ // adc_dma_mode_enable(ADC0); /* enable ADC interface */ adc_enable(ADC0); //delay_1ms(1); adc_delay_us(1000); /* ADC calibration and reset calibration */ adc_calibration_enable(ADC0); /* ADC software trigger enable */ // adc_software_trigger_enable(ADC0, ADC_INSERTED_CHANNEL); nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);//use it when no dma nvic_irq_enable(ADC0_1_IRQn, 0, 0);//use it when no dma } void bemf_adc_inserted_dma_config(void) { /* DMA configuration */ bemf_adc_dma_config(); /* ADC mode config */ adc_mode_config(ADC_MODE_FREE); /* ADC continuous function enable */ adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, DISABLE); /* ADC scan function enable */ adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE); /* ADC data alignment config */ adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT); /* ADC channel length config */ adc_channel_length_config(ADC0, ADC_INSERTED_CHANNEL, 2); /* ADC INSERTED channel config */ adc_inserted_channel_config(ADC0, 0, ADC_CHANNEL_4, ADC_SAMPLETIME_55POINT5); adc_inserted_channel_config(ADC0, 1, ADC_CHANNEL_5, ADC_SAMPLETIME_55POINT5); /* ADC trigger config */ adc_external_trigger_source_config(ADC0, ADC_INSERTED_CHANNEL, ADC0_1_EXTTRIG_INSERTED_T0_CH3);//ADC0_1_2_EXTTRIG_INSERTED_NONE );//); adc_external_trigger_config(ADC0, ADC_INSERTED_CHANNEL, ENABLE); /* clear the ADC flag */ //adc_interrupt_flag_clear(ADC0, ADC_INT_FLAG_EOC);//use it when no dma //adc_interrupt_flag_clear(ADC0, ADC_INT_FLAG_EOIC); /* enable ADC interrupt */ //adc_interrupt_enable(ADC0, ADC_INT_EOIC);//use it when no dma /* ADC DMA function enable */ adc_dma_mode_enable(ADC0); /* enable ADC interface */ adc_enable(ADC0); //delay_1ms(1); adc_delay_us(1000); /* ADC calibration and reset calibration */ adc_calibration_enable(ADC0); /* ADC software trigger enable */ // adc_software_trigger_enable(ADC0, ADC_INSERTED_CHANNEL); //nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);//use it when no dma //nvic_irq_enable(ADC0_1_IRQn, 0, 0);//use it when no dma } void bemf_adc_init(void) { /* RCU configuration */ bemf_adc_rcu_config(); /* GPIO configuration */ bemf_adc_gpio_config(); /* ADC configuration */ //bemf_adc_regular_dma_config(); bemf_adc_inserted_config(); //bemf_adc_inserted_dma_config();// adc_delay_us(10000); adc_zero = adc_value[1]; } /*! \brief this function handles ADC0_1 interrupt \param[in] none \param[out] none \retval none */ void ADC0_1_IRQHandler(void) { /* clear the ADC flag */ adc_interrupt_flag_clear(ADC0, ADC_INT_FLAG_EOIC); adc_value[0] = adc_inserted_data_read(ADC0,ADC_INSERTED_CHANNEL_0); adc_value[1] = adc_inserted_data_read(ADC0,ADC_INSERTED_CHANNEL_1); // led1(); }