|
|
@@ -18,13 +18,6 @@ DMA0 ch4 -> timer0 update event
|
|
|
*/
|
|
|
|
|
|
static void _init_pwm_timer(void);
|
|
|
-#ifdef ENABLE_AUX_TIMER
|
|
|
-static void _init_adc_timer(void);
|
|
|
-static void _init_aux_timer(void);
|
|
|
-#endif
|
|
|
-#if SHUNT_NUM==ONE_SHUNT_SAMPLE
|
|
|
-static void timer0_dma_config(void);
|
|
|
-#endif
|
|
|
static void _pwm_gpio_config(void);
|
|
|
#if USER_ITMER_BRAKE==0
|
|
|
static void _gpio_brakein_irq_enable(void);
|
|
|
@@ -33,13 +26,6 @@ u16 timer_update_buffer[6] = {0};
|
|
|
|
|
|
void pwm_3phase_init(void){
|
|
|
_init_pwm_timer();
|
|
|
-#ifdef ENABLE_AUX_TIMER
|
|
|
- _init_adc_timer();
|
|
|
- _init_aux_timer();
|
|
|
-#endif
|
|
|
-#if SHUNT_NUM==ONE_SHUNT_SAMPLE
|
|
|
- timer0_dma_config();
|
|
|
-#endif
|
|
|
_pwm_gpio_config();
|
|
|
}
|
|
|
|
|
|
@@ -167,23 +153,7 @@ static void _init_pwm_timer(void) {
|
|
|
_gpio_brakein_irq_enable();
|
|
|
#endif
|
|
|
|
|
|
-#ifdef ENABLE_AUX_TIMER
|
|
|
- /* select the master slave mode */
|
|
|
- timer_master_slave_mode_config(timer,TIMER_MASTER_SLAVE_MODE_ENABLE);
|
|
|
- /* slave mode selection: enabled when time2 is enable*/
|
|
|
- timer_slave_mode_select(timer,TIMER_SLAVE_MODE_EVENT);
|
|
|
- timer_input_trigger_source_select(timer,TIMER_SMCFG_TRGSEL_ITI2);
|
|
|
-#endif
|
|
|
-#if SHUNT_NUM==ONE_SHUNT_SAMPLE
|
|
|
- timer_channel_output_shadow_config(timer,TIMER_CH_0,TIMER_OC_SHADOW_DISABLE);
|
|
|
- timer_channel_output_shadow_config(timer,TIMER_CH_1,TIMER_OC_SHADOW_DISABLE);
|
|
|
- timer_channel_output_shadow_config(timer,TIMER_CH_2,TIMER_OC_SHADOW_DISABLE);
|
|
|
-
|
|
|
- timer_dma_transfer_config(TIMER0, TIMER_DMACFG_DMATA_CH0CV, TIMER_DMACFG_DMATC_3TRANSFER);
|
|
|
- timer_dma_enable(TIMER0, TIMER_DMA_UPD);
|
|
|
-#else
|
|
|
timer_master_slave_mode_config(timer,TIMER_MASTER_SLAVE_MODE_DISABLE);
|
|
|
-#endif /* ONE_SHUNT_SAMPLE_1 */
|
|
|
|
|
|
pwm_enable_channel();
|
|
|
|
|
|
@@ -192,9 +162,7 @@ static void _init_pwm_timer(void) {
|
|
|
#ifdef ENABLE_PWM_UP_IRQ
|
|
|
nvic_irq_enable(TIMER0_UP_IRQn, TIMER_UP_IRQ_PRIORITY, 0);
|
|
|
#endif
|
|
|
-#if SHUNT_NUM==THREE_SHUNTS_SAMPLE
|
|
|
timer_enable(timer);
|
|
|
-#endif
|
|
|
|
|
|
#ifdef GD32_FOC_DEMO
|
|
|
/* IR2136S enable */
|
|
|
@@ -203,107 +171,7 @@ static void _init_pwm_timer(void) {
|
|
|
|
|
|
}
|
|
|
|
|
|
-#ifdef ENABLE_AUX_TIMER
|
|
|
-static void _init_adc_timer(void) {
|
|
|
- timer_oc_parameter_struct timer_ocintpara;
|
|
|
- timer_parameter_struct timer_initpara;
|
|
|
- u32 timer = adc_timer;
|
|
|
- u32 half_period = FOC_PWM_Half_Period;
|
|
|
-
|
|
|
- rcu_periph_clock_enable(_rcu_clk(timer));
|
|
|
-
|
|
|
- timer_deinit(timer);
|
|
|
- memset(&timer_initpara, 0, sizeof(timer_initpara));
|
|
|
- memset(&timer_ocintpara, 0, sizeof(timer_ocintpara));
|
|
|
-
|
|
|
- timer_initpara.prescaler = 0;
|
|
|
- timer_initpara.alignedmode = TIMER_COUNTER_CENTER_UP;
|
|
|
- timer_initpara.period = half_period;
|
|
|
- timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
|
|
|
- timer_initpara.repetitioncounter = 0;
|
|
|
- timer_init(timer,&timer_initpara);
|
|
|
-
|
|
|
- timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
|
|
|
- timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
|
|
|
- timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
|
|
|
- timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
|
|
|
- timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
|
|
|
- timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
|
|
|
|
|
|
- timer_channel_output_config(timer,TIMER_CH_0,&timer_ocintpara);
|
|
|
-
|
|
|
- timer_channel_output_pulse_value_config(timer,TIMER_CH_0,half_period-1);
|
|
|
-#if SHUNT_NUM==ONE_SHUNT_SAMPLE
|
|
|
- timer_channel_output_mode_config(timer,TIMER_CH_0,TIMER_OC_MODE_PWM1);
|
|
|
-#else
|
|
|
- timer_channel_output_mode_config(timer,TIMER_CH_0,TIMER_OC_MODE_PWM0);
|
|
|
-#endif
|
|
|
- timer_channel_output_shadow_config(timer,TIMER_CH_0,TIMER_OC_SHADOW_ENABLE);
|
|
|
-
|
|
|
- /* select the master slave mode */
|
|
|
- timer_master_slave_mode_config(timer,TIMER_MASTER_SLAVE_MODE_ENABLE);
|
|
|
- /* slave mode selection: enabled when time2 is enable*/
|
|
|
- timer_slave_mode_select(timer,TIMER_SLAVE_MODE_EVENT);
|
|
|
- timer_input_trigger_source_select(timer,TIMER_SMCFG_TRGSEL_ITI2);
|
|
|
-
|
|
|
- //timer_primary_output_config(timer,ENABLE);
|
|
|
-
|
|
|
- /* auto-reload preload enable */
|
|
|
- timer_auto_reload_shadow_enable(timer);
|
|
|
-
|
|
|
- //timer_enable(timer);
|
|
|
-}
|
|
|
-
|
|
|
-static void _init_aux_timer(void) {
|
|
|
- timer_parameter_struct timer_initpara;
|
|
|
- u32 timer = aux_timer;
|
|
|
- rcu_periph_clock_enable(_rcu_clk(timer));
|
|
|
-
|
|
|
- timer_deinit(timer);
|
|
|
- memset(&timer_initpara, 0, sizeof(timer_initpara));
|
|
|
-
|
|
|
- timer_initpara.prescaler = 0;
|
|
|
- timer_initpara.alignedmode = TIMER_COUNTER_CENTER_UP;
|
|
|
- timer_initpara.period = FOC_PWM_period;
|
|
|
- timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
|
|
|
- timer_initpara.repetitioncounter = 0;
|
|
|
-
|
|
|
- timer_init(timer,&timer_initpara);
|
|
|
-
|
|
|
- /* select the master slave mode */
|
|
|
- timer_master_slave_mode_config(timer,TIMER_MASTER_SLAVE_MODE_ENABLE);
|
|
|
- /* timer update event is used as trigger output */
|
|
|
- timer_master_output_trigger_source_select(timer,TIMER_TRI_OUT_SRC_ENABLE);
|
|
|
-
|
|
|
- timer_primary_output_config(timer,ENABLE);
|
|
|
-
|
|
|
- timer_enable(timer);
|
|
|
-}
|
|
|
-
|
|
|
-#endif
|
|
|
-
|
|
|
-#if SHUNT_NUM==ONE_SHUNT_SAMPLE
|
|
|
-static void timer0_dma_config(void)
|
|
|
-{
|
|
|
- dma_parameter_struct dma_init_struct;
|
|
|
- rcu_periph_clock_enable(RCU_DMA0);
|
|
|
-
|
|
|
- /* TIMER0 update */
|
|
|
- dma_deinit(DMA0, DMA_CH4);
|
|
|
- dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
|
|
|
- dma_init_struct.memory_addr = (uint32_t)timer_update_buffer;
|
|
|
- dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
|
|
|
- dma_init_struct.memory_width = DMA_MEMORY_WIDTH_16BIT;
|
|
|
- dma_init_struct.number = 6;
|
|
|
- dma_init_struct.periph_addr = (uint32_t)0x40012C4C;
|
|
|
- dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
|
|
|
- dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
|
|
|
- dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
|
|
|
- dma_init(DMA0, DMA_CH4, &dma_init_struct);
|
|
|
- dma_circulation_enable(DMA0, DMA_CH4);
|
|
|
- dma_memory_to_memory_disable(DMA0, DMA_CH4);
|
|
|
-}
|
|
|
-#endif
|
|
|
|
|
|
#if USER_ITMER_BRAKE==0
|
|
|
static void _gpio_brakein_irq_enable(void){
|
|
|
@@ -335,9 +203,7 @@ void pwm_start(void){
|
|
|
timer_flag_clear(pwm_timer, TIMER_FLAG_UP);
|
|
|
|
|
|
timer_primary_output_config(pwm_timer,ENABLE);
|
|
|
-#ifdef ENABLE_AUX_TIMER
|
|
|
- timer_primary_output_config(adc_timer,ENABLE);
|
|
|
-#endif
|
|
|
+
|
|
|
timer_flag_clear(pwm_timer, TIMER_FLAG_UP);
|
|
|
#ifdef ENABLE_PWM_UP_IRQ
|
|
|
timer_interrupt_enable(pwm_timer, TIMER_INT_UP);
|
|
|
@@ -346,9 +212,7 @@ void pwm_start(void){
|
|
|
|
|
|
void pwm_stop(void){
|
|
|
timer_primary_output_config(pwm_timer,DISABLE);
|
|
|
-#ifdef ENABLE_AUX_TIMER
|
|
|
- timer_primary_output_config(adc_timer,DISABLE);
|
|
|
-#endif
|
|
|
+
|
|
|
timer_interrupt_disable(pwm_timer, TIMER_INT_UP);
|
|
|
/* wait for a new PWM period to flush last HF task */
|
|
|
timer_flag_clear(pwm_timer, TIMER_FLAG_UP);
|
|
|
@@ -375,22 +239,9 @@ void pwm_turn_on_low_side(void)
|
|
|
while (timer_flag_get(pwm_timer,TIMER_FLAG_UP) == RESET );
|
|
|
/* Main PWM Output Enable */
|
|
|
timer_primary_output_config(pwm_timer, ENABLE);
|
|
|
-#ifdef ENABLE_AUX_TIMER
|
|
|
- timer_primary_output_config(adc_timer, ENABLE);
|
|
|
-#endif
|
|
|
}
|
|
|
|
|
|
void pwm_update_sample(u32 samp1, u32 samp2, u8 sector) {
|
|
|
-#if 0
|
|
|
- pwm_update_2smaples(samp1, samp2);
|
|
|
-#ifdef ENABLE_AUX_TIMER
|
|
|
- if (samp1 < FOC_PWM_Half_Period) {
|
|
|
- adc_update_ext_trigger(ADC_TRIGGER_PHASE);
|
|
|
- }else {
|
|
|
- adc_update_ext_trigger(ADC_TRIGGER_PHASE2);
|
|
|
- }
|
|
|
-#endif
|
|
|
-#else
|
|
|
if (samp1 < FOC_PWM_Half_Period) {
|
|
|
TIMER_CH3CV(pwm_timer) = samp1;
|
|
|
pwm_change_t3_mode(TIMER_OC_MODE_PWM1);
|
|
|
@@ -398,6 +249,5 @@ void pwm_update_sample(u32 samp1, u32 samp2, u8 sector) {
|
|
|
TIMER_CH3CV(pwm_timer) = samp2;
|
|
|
pwm_change_t3_mode(TIMER_OC_MODE_PWM0);
|
|
|
}
|
|
|
-#endif
|
|
|
adc_current_sample_config(sector);
|
|
|
}
|