/** * @brief HRTIM configuration for checking that HRTIM is up and running * @param None * @retval None */ static void HRTIM_Config_HRTIMCheck(void) { /* ------------------------------------------------- */ /* HRTIM Global initialization: Clock and DLL init */ /* ------------------------------------------------- */ /* Initialize the HRTIM structure (minimal configuration) */ hhrtim.Instance = HRTIM1; hhrtim.Init.HRTIMInterruptResquests = HRTIM_IT_NONE; hhrtim.Init.SyncOptions = HRTIM_SYNCOPTION_NONE; /* Initialize HRTIM */ HAL_HRTIM_Init(&hhrtim); /* HRTIM DLL calibration: periodic calibration, set period to 14µs */ HAL_HRTIM_DLLCalibrationStart(&hhrtim, HRTIM_CALIBRATIONRATE_14); /* Wait calibration completion*/ if (HAL_HRTIM_PollForDLLCalibration(&hhrtim, 100) != HAL_OK) { Error_Handler(); /* if DLL or clock is not correctly set */ } /* ---------------*/ /* HRTIM start-up */ /* ---------------*/ /* Enable HRTIM's outputs TD1 */ /* Note: it is necessary to enable also GPIOs to have outputs functional */ /* This must be done after HRTIM initialization */ HAL_HRTIM_WaveformOutputStart(&hhrtim, HRTIM_OUTPUT_TD1); }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /* STM32F3xx HAL library initialization: - Configure the Flash prefetch - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Global MSP (MCU Support Package) initialization */ HAL_Init(); /* Configure the system clock to have a system clock = 72 MHz */ SystemClock_Config(); /* Initialize STM32F3348-DISCO LEDs */ BSP_LED_Init(LED3); BSP_LED_Init(LED4); BSP_LED_Init(LED6); /* Initialize User_Button on STM32F3348-DISCO */ BSP_PB_Init(BUTTON_USER, BUTTON_MODE_GPIO); /* Initialize ADC to be triggered by the HRTIMER */ ADC_Config(); /* Initialize HRTIM and related inputs */ HRTIM_Config(); /* Initialize HRTIM outputs (it has to be done after HRTIM init) */ GPIO_HRTIM_outputs_Config(); /* Infinite loop */ while (1) { /* ---------------- */ /* Fault management */ /* ---------------- */ while(__HAL_HRTIM_GET_FLAG(&hhrtim, HRTIM_FLAG_FLT1) == SET) { /* LED3 is flashing in case of fault */ BSP_LED_On(LED3); HAL_Delay(20); BSP_LED_Off(LED3); HAL_Delay(80); /* Re-arm HRTIM TD1 output if "User" push button is pressed*/ if((BSP_PB_GetState(BUTTON_USER) == SET)) { __HAL_HRTIM_CLEAR_IT(&hhrtim, HRTIM_IT_FLT1); HAL_HRTIM_WaveformOutputStart(&hhrtim, HRTIM_OUTPUT_TD1); } } /* ---------------- */ /* Normal operation */ /* ---------------- */ /* LED6 toggling to show MCU activity */ BSP_LED_On(LED6); HAL_Delay(100); BSP_LED_Off(LED6); HAL_Delay(400); /* -------------------------------------------------------------------- */ /* ADC monitoring (to be viewed with watch) */ /* PA4 can be connected to the RC filter or any voltage to be monitored */ /* -------------------------------------------------------------------- */ ADCReadout = HAL_ADCEx_InjectedGetValue(&AdcHandle, ADC_INJECTED_RANK_1); } }
/** * @brief HRTIM configuration * @param None * @retval None */ static void HRTIM_Config(void) { HRTIM_TimeBaseCfgTypeDef timebase_config; HRTIM_TimerCfgTypeDef timer_config; HRTIM_OutputCfgTypeDef output_config_TD1; HRTIM_OutputCfgTypeDef output_config_TD2; HRTIM_CompareCfgTypeDef compare_config; HRTIM_ADCTriggerCfgTypeDef adc_trigger_config; HRTIM_FaultCfgTypeDef fault_config; /* ----------------------------*/ /* HRTIM Global initialization */ /* ----------------------------*/ /* Initialize the HRTIM structure (minimal configuration) */ hhrtim.Instance = HRTIM1; hhrtim.Init.HRTIMInterruptResquests = HRTIM_IT_NONE; hhrtim.Init.SyncOptions = HRTIM_SYNCOPTION_NONE; /* Initialize HRTIM */ HAL_HRTIM_Init(&hhrtim); /* HRTIM DLL calibration: periodic calibration, set period to 14µs */ HAL_HRTIM_DLLCalibrationStart(&hhrtim, HRTIM_CALIBRATIONRATE_14); /* Wait calibration completion*/ if (HAL_HRTIM_PollForDLLCalibration(&hhrtim, 100) != HAL_OK) { Error_Handler(); // if DLL or clock is not correctly set } /* --------------------------------------------------- */ /* TIMERD initialization: timer mode and PWM frequency */ /* --------------------------------------------------- */ timebase_config.Period = BUCK_PWM_PERIOD;// 100kHz switching frequency timebase_config.RepetitionCounter = 3; timebase_config.PrescalerRatio = HRTIM_PRESCALERRATIO_MUL32; timebase_config.Mode = HRTIM_MODE_CONTINUOUS; HAL_HRTIM_TimeBaseConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_D, &timebase_config); /* --------------------------------------------------------------------- */ /* TIMERD global configuration: cnt reset, sync, update, fault, burst... */ /* --------------------------------------------------------------------- */ timer_config.DMARequests = HRTIM_TIM_DMA_NONE; timer_config.DMASrcAddress = 0x0; timer_config.DMADstAddress = 0x0; timer_config.DMASize = 0x0; timer_config.HalfModeEnable = HRTIM_HALFMODE_DISABLED; timer_config.StartOnSync = HRTIM_SYNCSTART_DISABLED; timer_config.ResetOnSync = HRTIM_SYNCRESET_DISABLED; timer_config.DACSynchro = HRTIM_DACSYNC_NONE; timer_config.PreloadEnable = HRTIM_PRELOAD_ENABLED; timer_config.UpdateGating = HRTIM_UPDATEGATING_INDEPENDENT; timer_config.BurstMode = HRTIM_TIMERBURSTMODE_MAINTAINCLOCK; timer_config.RepetitionUpdate = HRTIM_UPDATEONREPETITION_ENABLED; timer_config.ResetUpdate = HRTIM_TIMUPDATEONRESET_DISABLED; timer_config.InterruptRequests = HRTIM_TIM_IT_REP; timer_config.PushPull = HRTIM_TIMPUSHPULLMODE_DISABLED; timer_config.FaultEnable = HRTIM_TIMFAULTENABLE_FAULT1; timer_config.FaultLock = HRTIM_TIMFAULTLOCK_READWRITE; timer_config.DeadTimeInsertion = HRTIM_TIMDEADTIMEINSERTION_DISABLED; timer_config.DelayedProtectionMode = HRTIM_TIMER_A_B_C_DELAYEDPROTECTION_DISABLED; timer_config.UpdateTrigger= HRTIM_TIMUPDATETRIGGER_NONE; timer_config.ResetTrigger = HRTIM_TIMRESETTRIGGER_NONE; HAL_HRTIM_WaveformTimerConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_D,&timer_config); /* --------------------------------- */ /* TD1 and TD2 waveforms description */ /* --------------------------------- */ output_config_TD1.Polarity = HRTIM_OUTPUTPOLARITY_HIGH; output_config_TD1.SetSource = HRTIM_OUTPUTSET_TIMPER; output_config_TD1.ResetSource = HRTIM_OUTPUTRESET_TIMCMP1; output_config_TD1.IdleMode = HRTIM_OUTPUTIDLEMODE_NONE; output_config_TD1.IdleLevel = HRTIM_OUTPUTIDLELEVEL_INACTIVE; output_config_TD1.FaultLevel = HRTIM_OUTPUTFAULTLEVEL_INACTIVE; output_config_TD1.ChopperModeEnable = HRTIM_OUTPUTCHOPPERMODE_DISABLED; output_config_TD1.BurstModeEntryDelayed = HRTIM_OUTPUTBURSTMODEENTRY_REGULAR; HAL_HRTIM_WaveformOutputConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_D, HRTIM_OUTPUT_TD1, &output_config_TD1); output_config_TD2.Polarity = HRTIM_OUTPUTPOLARITY_HIGH; output_config_TD2.SetSource = HRTIM_OUTPUTSET_TIMCMP2; output_config_TD2.ResetSource = HRTIM_OUTPUTRESET_TIMPER; output_config_TD2.IdleMode = HRTIM_OUTPUTIDLEMODE_NONE; output_config_TD2.IdleLevel = HRTIM_OUTPUTIDLELEVEL_INACTIVE; output_config_TD2.FaultLevel = HRTIM_OUTPUTFAULTLEVEL_NONE; output_config_TD2.ChopperModeEnable = HRTIM_OUTPUTCHOPPERMODE_DISABLED; output_config_TD2.BurstModeEntryDelayed = HRTIM_OUTPUTBURSTMODEENTRY_REGULAR; HAL_HRTIM_WaveformOutputConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_D, HRTIM_OUTPUT_TD2, &output_config_TD2); /* Set compare registers for duty cycle on TD1 */ compare_config.CompareValue = BUCK_PWM_PERIOD/20; // 5% duty cycle HAL_HRTIM_WaveformCompareConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_D, HRTIM_COMPAREUNIT_1, &compare_config); /* Set compare registers for duty cycle on TD2 */ compare_config.AutoDelayedMode = HRTIM_AUTODELAYEDMODE_REGULAR; compare_config.AutoDelayedTimeout = 0; compare_config.CompareValue = BUCK_PWM_PERIOD - (BUCK_PWM_PERIOD/3); // 33% duty cycle (NB: from CMP2 to period) HAL_HRTIM_WaveformCompareConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_D, HRTIM_COMPAREUNIT_2, &compare_config); /* --------------------------------------------------- */ /* ADC trigger intialization (CMP3 and CMP4 registers) */ /* --------------------------------------------------- */ /* Set compare 3 registers for 1st ADC trigger */ compare_config.CompareValue = BUCK_PWM_PERIOD/10; // samples in middle of duty cycle (NB: from CMP2 to period) HAL_HRTIM_WaveformCompareConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_D, HRTIM_COMPAREUNIT_3, &compare_config); /* Set compare 4 registers for 2nd ADC trigger */ compare_config.AutoDelayedMode = HRTIM_AUTODELAYEDMODE_REGULAR; compare_config.AutoDelayedTimeout = 0; compare_config.CompareValue = (BUCK_PWM_PERIOD - ((BUCK_PWM_PERIOD/3)/2)); // samples in middle of duty cycle HAL_HRTIM_WaveformCompareConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_D, HRTIM_COMPAREUNIT_4, &compare_config); adc_trigger_config.Trigger = HRTIM_ADCTRIGGEREVENT24_TIMERD_CMP3 + HRTIM_ADCTRIGGEREVENT24_TIMERD_CMP4; adc_trigger_config.UpdateSource = HRTIM_ADCTRIGGERUPDATE_TIMER_D; HAL_HRTIM_ADCTriggerConfig(&hhrtim, HRTIM_ADCTRIGGER_2, &adc_trigger_config); /* ---------------------*/ /* FAULT initialization */ /* ---------------------*/ fault_config.Filter = HRTIM_FAULTFILTER_NONE; fault_config.Lock = HRTIM_FAULTLOCK_READWRITE; fault_config.Polarity = HRTIM_FAULTPOLARITY_LOW; fault_config.Source = HRTIM_FAULTSOURCE_DIGITALINPUT; HAL_HRTIM_FaultConfig(&hhrtim, HRTIM_FAULT_1, &fault_config); HAL_HRTIM_FaultModeCtl(&hhrtim, HRTIM_FAULT_1, HRTIM_FAULTMODECTL_ENABLED); /* ---------------*/ /* HRTIM start-up */ /* ---------------*/ /* Enable HRTIM's outputs TD1 and TD2 */ /* Note: it is necessary to enable also GPIOs to have outputs functional */ /* This must be done after HRTIM initialization */ HAL_HRTIM_WaveformOutputStart(&hhrtim, HRTIM_OUTPUT_TD1 | HRTIM_OUTPUT_TD2); /* Start HRTIM's TIMER D*/ HAL_HRTIM_WaveformCounterStart_IT(&hhrtim, HRTIM_TIMERID_TIMER_D); }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /* STM32F3xx HAL library initialization: - Configure the Flash prefetch - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Global MSP (MCU Support Package) initialization */ HAL_Init(); /* Configure the system clock to have a system clock = 72 MHz */ SystemClock_Config(); /* Initialize STM32F3348-DISCO LEDs */ BSP_LED_Init(LED3); BSP_LED_Init(LED4); BSP_LED_Init(LED6); /* Initialize User_Button on STM32F3348-DISCO */ BSP_PB_Init(BUTTON_USER, BUTTON_MODE_GPIO); /* Initialize ADC to be triggered by the HRTIMER */ ADC_Config(); /* Initialize HRTIM and related inputs */ #ifdef SNIPPET HRTIM_Config_NoHAL(); #else HRTIM_Config(); #endif /* Initialize BUCK outputs (it has to be done after HRTIM init) */ GPIO_BUCK_outputs_Config(); /* Turn ON T6 MOSFET on discovery board */ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_SET); /* Infinite loop */ while (1) { /* ---------------- */ /* Fault management */ /* ---------------- */ #ifdef SNIPPET /* If Fault occured */ while(HRTIM1->sCommonRegs.ISR & HRTIM_ICR_FLT1C) { /* LED3 is flashing in case of fault */ BSP_LED_On(LED3); HAL_Delay(20); BSP_LED_Off(LED3); HAL_Delay(80); /* Re-arm HRTIM TD1 output if "User" push button is pressed*/ if((BSP_PB_GetState(BUTTON_USER) == SET)) { /* Clear interrupt flag */ HRTIM1->sCommonRegs.ICR = HRTIM_ICR_FLT1C; /* Re-enable TA1 and TA2 */ HRTIM1->sCommonRegs.OENR = HRTIM_OENR_TA1OEN + HRTIM_OENR_TA2OEN; } } #else while(__HAL_HRTIM_GET_FLAG(&hhrtim, HRTIM_FLAG_FLT1) == SET) { /* LED3 is flashing in case of fault */ BSP_LED_On(LED3); HAL_Delay(20); BSP_LED_Off(LED3); HAL_Delay(80); /* Re-arm HRTIM TD1 output if "User" push button is pressed*/ if((BSP_PB_GetState(BUTTON_USER) == SET)) { __HAL_HRTIM_CLEAR_IT(&hhrtim, HRTIM_IT_FLT1); HAL_HRTIM_WaveformOutputStart(&hhrtim, HRTIM_OUTPUT_TA1 | HRTIM_OUTPUT_TA2); } } #endif /* ---------------- */ /* Normal operation */ /* ---------------- */ /* LED6 toggling to show MCU activity */ BSP_LED_On(LED6); HAL_Delay(100); BSP_LED_Off(LED6); HAL_Delay(400); /* -----------------------------------------------------------------------*/ /* Input and output voltages can be displayed real-time in a watch window */ /* -----------------------------------------------------------------------*/ Vin = (HAL_ADCEx_InjectedGetValue(&AdcHandle, ADC_INJECTED_RANK_1) * ADC_VREF)/0x1000; /* VIN bridge conversion is 4.97 (6.8K / 6.8K + 27K) */ Vin = (497 * Vin )/100; Vout = (HAL_ADCEx_InjectedGetValue(&AdcHandle, ADC_INJECTED_RANK_2) * ADC_VREF)/0x1000; /* VOUT bridge conversion is 5.03 (3.3K / 3.3K + 13.3K) */ Vout = (503 * Vout)/100; } }
/** * @brief HRTIM configuration for generating an arbitrary waveform * @param None * @retval None */ static void HRTIM_Config_ArbitraryWaveform(void) { HRTIM_TimeBaseCfgTypeDef timebase_config; HRTIM_TimerCfgTypeDef timer_config; HRTIM_OutputCfgTypeDef output_config_TDx; HRTIM_CompareCfgTypeDef compare_config; /* ----------------------------------------------- */ /* HRTIM Global initialization: Clock and DLL init */ /* ----------------------------------------------- */ /* Initialize the HRTIM structure (minimal configuration) */ hhrtim.Instance = HRTIM1; hhrtim.Init.HRTIMInterruptResquests = HRTIM_IT_NONE; hhrtim.Init.SyncOptions = HRTIM_SYNCOPTION_NONE; /* Initialize HRTIM */ HAL_HRTIM_Init(&hhrtim); /* HRTIM DLL calibration: periodic calibration, set period to 14µs */ HAL_HRTIM_DLLCalibrationStart(&hhrtim, HRTIM_CALIBRATIONRATE_14); /* Wait calibration completion*/ if (HAL_HRTIM_PollForDLLCalibration(&hhrtim, 100) != HAL_OK) { Error_Handler(); /* if DLL or clock is not correctly set */ } /* ------------------------------------------------------------ */ /* TIMERD initialization: set PWM frequency and continuous mode */ /* ------------------------------------------------------------ */ timebase_config.Period = _100KHz_PERIOD; timebase_config.RepetitionCounter = 0; timebase_config.PrescalerRatio = HRTIM_PRESCALERRATIO_MUL32; timebase_config.Mode = HRTIM_MODE_CONTINUOUS; HAL_HRTIM_TimeBaseConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_D, &timebase_config); /* --------------------------------------------------------- */ /* TIMERD global configuration: preload enabled on REP event */ /* --------------------------------------------------------- */ timer_config.DMARequests = HRTIM_TIM_DMA_NONE; timer_config.HalfModeEnable = HRTIM_HALFMODE_DISABLED; timer_config.StartOnSync = HRTIM_SYNCSTART_DISABLED; timer_config.ResetOnSync = HRTIM_SYNCRESET_DISABLED; timer_config.DACSynchro = HRTIM_DACSYNC_NONE; timer_config.PreloadEnable = HRTIM_PRELOAD_ENABLED; timer_config.UpdateGating = HRTIM_UPDATEGATING_INDEPENDENT; timer_config.BurstMode = HRTIM_TIMERBURSTMODE_MAINTAINCLOCK; timer_config.RepetitionUpdate = HRTIM_UPDATEONREPETITION_ENABLED; timer_config.ResetUpdate = HRTIM_TIMUPDATEONRESET_DISABLED; timer_config.InterruptRequests = HRTIM_TIM_IT_NONE; timer_config.PushPull = HRTIM_TIMPUSHPULLMODE_DISABLED; timer_config.FaultEnable = HRTIM_TIMFAULTENABLE_NONE; timer_config.FaultLock = HRTIM_TIMFAULTLOCK_READWRITE; timer_config.DeadTimeInsertion = HRTIM_TIMDEADTIMEINSERTION_DISABLED; timer_config.DelayedProtectionMode = HRTIM_TIMER_A_B_C_DELAYEDPROTECTION_DISABLED; timer_config.UpdateTrigger= HRTIM_TIMUPDATETRIGGER_NONE; timer_config.ResetTrigger = HRTIM_TIMRESETTRIGGER_NONE; HAL_HRTIM_WaveformTimerConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_D,&timer_config); /* --------------------------------------------------------------------- */ /* TD1 waveform description: toggles on TIMD period, CMP1 and CMP2 event */ /* --------------------------------------------------------------------- */ output_config_TDx.Polarity = HRTIM_OUTPUTPOLARITY_HIGH; output_config_TDx.SetSource = HRTIM_OUTPUTSET_TIMPER + HRTIM_OUTPUTSET_TIMCMP1 + HRTIM_OUTPUTSET_TIMCMP2; output_config_TDx.ResetSource = HRTIM_OUTPUTRESET_TIMPER + HRTIM_OUTPUTRESET_TIMCMP1 + HRTIM_OUTPUTRESET_TIMCMP2; output_config_TDx.IdleMode = HRTIM_OUTPUTIDLEMODE_NONE; output_config_TDx.IdleLevel = HRTIM_OUTPUTIDLELEVEL_INACTIVE; output_config_TDx.FaultLevel = HRTIM_OUTPUTFAULTLEVEL_NONE; output_config_TDx.ChopperModeEnable = HRTIM_OUTPUTCHOPPERMODE_DISABLED; output_config_TDx.BurstModeEntryDelayed = HRTIM_OUTPUTBURSTMODEENTRY_REGULAR; HAL_HRTIM_WaveformOutputConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_D, HRTIM_OUTPUT_TD1, &output_config_TDx); /* ------------------------------------------------------ */ /* TD2 waveform description: set on TIMD PER and CMP2 and */ /* reset on TIMD CMP1 and CMP3 event */ /* Note: TDx init structure is re-used */ /* ------------------------------------------------------ */ output_config_TDx.SetSource = HRTIM_OUTPUTSET_TIMPER + HRTIM_OUTPUTSET_TIMCMP2; output_config_TDx.ResetSource = HRTIM_OUTPUTRESET_TIMCMP1 + HRTIM_OUTPUTRESET_TIMCMP3; HAL_HRTIM_WaveformOutputConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_D, HRTIM_OUTPUT_TD2, &output_config_TDx); /* Set compare register 1 in Timer D */ compare_config.CompareValue = _100KHz_PERIOD/4; /* 25% of the period */ HAL_HRTIM_WaveformCompareConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_D, HRTIM_COMPAREUNIT_1, &compare_config); /* Set compare register 2 in Timer D */ compare_config.AutoDelayedMode = HRTIM_AUTODELAYEDMODE_REGULAR; compare_config.AutoDelayedTimeout = 0; compare_config.CompareValue = (3*_100KHz_PERIOD)/8; /* 37.5% of the period */ HAL_HRTIM_WaveformCompareConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_D, HRTIM_COMPAREUNIT_2, &compare_config); /* Set compare register 3 in Timer D */ compare_config.CompareValue = _100KHz_PERIOD/2; /* 50% of the period */ HAL_HRTIM_WaveformCompareConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_D, HRTIM_COMPAREUNIT_3, &compare_config); /* ---------------*/ /* HRTIM start-up */ /* ---------------*/ /* Enable HRTIM's outputs TD1 and TD2 */ /* Note: it is necessary to enable also GPIOs to have outputs functional */ /* This must be done after HRTIM initialization */ HAL_HRTIM_WaveformOutputStart(&hhrtim, HRTIM_OUTPUT_TD1 + HRTIM_OUTPUT_TD2); /* Start HRTIM's TIMER D */ HAL_HRTIM_WaveformCounterStart(&hhrtim, HRTIM_TIMERID_TIMER_D); }