add basic PWM output driver functions

This commit is contained in:
Arnaud Vergnet 2020-11-01 16:21:18 +01:00
parent 8f7cdc4766
commit b73da08273
3 changed files with 62 additions and 26 deletions

View file

@ -51,7 +51,7 @@ void TIM4_IRQHandler(void)
* @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
* @retval None
*/
void Timer_IT_Enable(TIM_TypeDef * timer)
void Timer_IT_enable(TIM_TypeDef * timer)
{
LL_TIM_EnableIT_UPDATE(timer);
}
@ -63,7 +63,7 @@ void Timer_IT_Enable(TIM_TypeDef * timer)
* @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
* @retval None
*/
void Timer_IT_Disable(TIM_TypeDef * timer)
void Timer_IT_disable(TIM_TypeDef * timer)
{
LL_TIM_DisableIT_UPDATE(timer);
}
@ -76,7 +76,7 @@ void Timer_IT_Disable(TIM_TypeDef * timer)
* int Prio : priorité associée à l'interruption
* @retval None
*/
void Timer_IT_Conf(TIM_TypeDef * timer, void (*it_callback) (void), int prio)
void Timer_IT_conf(TIM_TypeDef * timer, void (*it_callback) (void), int prio)
{
// affectation de la fonction
if (timer == TIM1) it_callback_TIM1 = it_callback;
@ -114,7 +114,6 @@ void Timer_IT_Conf(TIM_TypeDef * timer, void (*it_callback) (void), int prio)
*/
void Timer_start(TIM_TypeDef * timer)
{
Timer_IT_Enable(timer);
LL_TIM_EnableCounter(timer);
}
@ -126,7 +125,6 @@ void Timer_start(TIM_TypeDef * timer)
*/
void Timer_stop(TIM_TypeDef * timer)
{
Timer_IT_Disable(timer);
LL_TIM_DisableCounter(timer);
}
@ -138,9 +136,9 @@ void Timer_stop(TIM_TypeDef * timer)
* int Psc : valeur à placer dans PSC
* @retval None
*/
void Timer_conf(TIM_TypeDef * timer, int arr, int psc, void (*it_callback) (void))
void Timer_conf(TIM_TypeDef * timer, int arr, int psc)
{
LL_TIM_InitTypeDef My_LL_Tim_Init_Struct;
LL_TIM_InitTypeDef init_struct;
// Validation horloge locale
if (timer == TIM1) LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1);
@ -149,15 +147,16 @@ void Timer_conf(TIM_TypeDef * timer, int arr, int psc, void (*it_callback) (void
else LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM4);
// chargement structure Arr, Psc, Up Count
My_LL_Tim_Init_Struct.Autoreload = arr;
My_LL_Tim_Init_Struct.Prescaler = psc;
My_LL_Tim_Init_Struct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
My_LL_Tim_Init_Struct.CounterMode = LL_TIM_COUNTERMODE_UP;
My_LL_Tim_Init_Struct.RepetitionCounter = 0;
init_struct.Autoreload = arr;
init_struct.Prescaler = psc;
init_struct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
init_struct.CounterMode = LL_TIM_COUNTERMODE_UP;
init_struct.RepetitionCounter = 0;
LL_TIM_Init(timer,&My_LL_Tim_Init_Struct);
LL_TIM_Init(timer,&init_struct);
Timer_IT_Conf(timer, it_callback, 3);
// Blocage interruptions
Timer_IT_disable(timer);
// Blocage Timer
Timer_stop(timer);
@ -172,14 +171,31 @@ void Timer_conf(TIM_TypeDef * timer, int arr, int psc, void (*it_callback) (void
* PWM OUTPUT
***************************************************************************/
void PWMo_conf(TIM_TypeDef * timer, int channel, int arr, int psc)
int getArrFromFreq(float freq_khz)
{
return (72000 / freq_khz) - 1;
}
void PWMo_setDutyCycle(TIM_TypeDef * timer, int dutyCycle)
void PWMo_conf(TIM_TypeDef * timer, int channel, float freq_khz, float dutyCycle)
{
Timer_conf(timer, getArrFromFreq(freq_khz), 0);
LL_TIM_OC_InitTypeDef init_struct;
LL_TIM_OC_StructInit(&init_struct);
init_struct.OCMode = LL_TIM_OCMODE_PWM1;
init_struct.OCState = LL_TIM_OCSTATE_ENABLE;
init_struct.CompareValue= dutyCycle * getArrFromFreq(freq_khz);
LL_TIM_OC_Init(timer, channel, &init_struct);
}
void PWMo_setDutyCycle(TIM_TypeDef * timer, int channel, float freq_khz, float dutyCycle)
{
int compare = dutyCycle * getArrFromFreq(freq_khz);
if (channel == LL_TIM_CHANNEL_CH1) LL_TIM_OC_SetCompareCH1(timer, compare);
else if (channel == LL_TIM_CHANNEL_CH2) LL_TIM_OC_SetCompareCH2(timer, compare);
else if (channel == LL_TIM_CHANNEL_CH3) LL_TIM_OC_SetCompareCH3(timer, compare);
else LL_TIM_OC_SetCompareCH4(timer, compare);
}
/****************************************************************************

View file

@ -3,6 +3,16 @@
#include "stm32f103xb.h"
/****************************************************************************
* INTERRUPTIONS
***************************************************************************/
void Timer_IT_enable(TIM_TypeDef * timer);
void Timer_IT_disable(TIM_TypeDef * timer);
void Timer_IT_conf(TIM_TypeDef * timer, void (*it_callback) (void), int prio);
/****************************************************************************
* TIMER
@ -17,7 +27,7 @@
* int Psc : valeur à placer dans PSC
* @retval None
*/
void Timer_conf(TIM_TypeDef * timer, int arr, int psc, void (*it_callback) (void));
void Timer_conf(TIM_TypeDef * timer, int arr, int psc);
/**
@ -42,19 +52,25 @@ void Timer_stop(TIM_TypeDef * timer);
* PWM INPUT
***************************************************************************/
void PWMi_conf(TIM_TypeDef * timer, int channel, int arr, int psc);
int PWMi_getDutyCycle(TIM_TypeDef * timer);
void PWMi_conf(TIM_TypeDef * timer, int channel);
int PWMi_getDutyCycle(TIM_TypeDef * timer, int channel);
/****************************************************************************
* PWM OUTPUT
***************************************************************************/
void PWMo_conf(TIM_TypeDef * timer, int channel, int arr, int psc);
void PWMo_conf(TIM_TypeDef * timer, int channel, float freq_khz, float dutyCycle);
void PWMo_setDutyCycle(TIM_TypeDef * timer, int dutyCycle);
/**
* @brief Arrêt le timer considéré
* @note
* @param TIM_TypeDef Timer : indique le timer à utiliser : TIM1, TIM2, TIM3 ou TIM4
* int channel : Le channel utilisé par la PWM
* float dutyCycle : Valeur entre 0 et 1
* @retval None
*/
void PWMo_setDutyCycle(TIM_TypeDef * timer, int channel, float freq_khz, float dutyCycle);
/****************************************************************************
* ENCODER

View file

@ -37,7 +37,11 @@ void Chrono_Conf(TIM_TypeDef * Timer)
Chrono_Timer = Timer;
// Réglage Timer pour un débordement à 10ms
Timer_conf(Chrono_Timer, 999, 719, Chrono_Task_10ms);
Timer_conf(Chrono_Timer, 999, 719);
// Réglage des interruptions
Timer_IT_conf(Chrono_Timer, Chrono_Task_10ms, 3);
Timer_IT_enable(Chrono_Timer);
}