248 lines
6.4 KiB
C
248 lines
6.4 KiB
C
// TOUT A FAIRE !! //
|
|
|
|
/*
|
|
indispensable pour pouvoir adresser les registres des périphériques.
|
|
Rem : OBLIGATION d'utiliser les définitions utiles contenues dans ce fichier (ex : TIM_CR1_CEN, RCC_APB1ENR_TIM2EN ...)
|
|
pour une meilleure lisibilité du code.
|
|
|
|
Pour les masques, utiliser également les définitions proposée
|
|
Rappel : pour mettre à 1 , reg = reg | Mask (ou Mask est le représente le ou les bits à positionner à 1)
|
|
pour mettre à 0 , reg = reg&~ Mask (ou Mask est le représente le ou les bits à positionner à 0)
|
|
|
|
*/
|
|
|
|
#include "MyTimer.h"
|
|
#include "stm32f1xx_ll_bus.h" // Pour l'activation des horloges
|
|
#include "stm32f1xx_ll_tim.h"
|
|
|
|
|
|
// variable pointeur de fonction permettant de mémoriser le callback à appeler depuis
|
|
// le handler d'IT
|
|
void (*Ptr_ItFct_TIM1)(void);
|
|
void (*Ptr_ItFct_TIM2)(void);
|
|
void (*Ptr_ItFct_TIM3)(void);
|
|
void (*Ptr_ItFct_TIM4)(void);
|
|
|
|
|
|
|
|
|
|
/**
|
|
* @brief Active l'horloge et règle l'ARR et le PSC du timer visé
|
|
* @note Fonction à lancer avant toute autre. Le timer n'est pas encore lancé (voir MyTimerStart)
|
|
* @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
|
|
* int Arr : valeur à placer dans ARR
|
|
* int Psc : valeur à placer dans PSC
|
|
* @retval None
|
|
*/
|
|
void MyTimer_Conf(TIM_TypeDef * Timer)
|
|
{
|
|
LL_TIM_InitTypeDef My_LL_Tim_Init_Struct;
|
|
|
|
// Validation horloge locale
|
|
if (Timer==TIM1) LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1);
|
|
else if (Timer==TIM2) LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
|
|
else if (Timer==TIM3) LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3);
|
|
else LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM4);
|
|
|
|
// chargement structure Arr, Psc, Up Count
|
|
My_LL_Tim_Init_Struct.Autoreload=0xFFFF;
|
|
My_LL_Tim_Init_Struct.Prescaler=0;
|
|
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;
|
|
|
|
LL_TIM_Init(Timer,&My_LL_Tim_Init_Struct);
|
|
|
|
|
|
// Blocage IT
|
|
LL_TIM_DisableIT_UPDATE(Timer);
|
|
|
|
|
|
// Blocage Timer
|
|
LL_TIM_DisableCounter(Timer);
|
|
|
|
}
|
|
|
|
//CONFIG Timer en compteur
|
|
|
|
void MyTimer_girouette_Conf(void)
|
|
{
|
|
|
|
LL_TIM_ENCODER_InitTypeDef My_LL_Tim_Init_Struct;
|
|
|
|
// Validation horloge locale
|
|
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3);
|
|
|
|
LL_TIM_ENCODER_StructInit(&My_LL_Tim_Init_Struct);
|
|
|
|
LL_TIM_ENCODER_Init(TIM3, &My_LL_Tim_Init_Struct);
|
|
|
|
// Réglage Timer 3
|
|
LL_TIM_SetAutoReload(TIM3,0xFFFF);
|
|
LL_TIM_SetPrescaler(TIM3,0);
|
|
|
|
}
|
|
|
|
//CONFIG Timer en PWM
|
|
|
|
void MyPWM_Conf_Output(TIM_TypeDef *TIMx, uint32_t Channel)
|
|
{
|
|
|
|
LL_TIM_OC_InitTypeDef My_LL_Tim_OC_Init_Struct;
|
|
|
|
//Configuration du output channel en PWM
|
|
LL_TIM_OC_StructInit(&My_LL_Tim_OC_Init_Struct);
|
|
My_LL_Tim_OC_Init_Struct.OCMode = LL_TIM_OCMODE_PWM1;
|
|
|
|
LL_TIM_OC_Init(TIMx,Channel,&My_LL_Tim_OC_Init_Struct);
|
|
|
|
// Réglage Timer
|
|
LL_TIM_SetAutoReload(TIMx,0x707F);
|
|
LL_TIM_SetPrescaler(TIMx,0x31);
|
|
|
|
//Activation du channel (CH3) pour le timer considéré
|
|
LL_TIM_CC_EnableChannel(TIMx, Channel);
|
|
}
|
|
|
|
|
|
void MyPWM_Set_Impulse_Duration(TIM_TypeDef * Timer, int Percentage, int channel)
|
|
{
|
|
static uint32_t Val_ARR;
|
|
static uint32_t CompareValue;
|
|
|
|
Val_ARR = LL_TIM_GetAutoReload(Timer);
|
|
CompareValue = (Val_ARR * Percentage)/10000;
|
|
|
|
if (channel==LL_TIM_CHANNEL_CH1) LL_TIM_OC_SetCompareCH1(Timer, CompareValue);
|
|
else if (channel==LL_TIM_CHANNEL_CH2) LL_TIM_OC_SetCompareCH2(Timer, CompareValue);
|
|
else if (channel==LL_TIM_CHANNEL_CH3) LL_TIM_OC_SetCompareCH3(Timer, CompareValue);
|
|
else LL_TIM_OC_SetCompareCH4(Timer, CompareValue);
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief PWM servo moteur
|
|
* @note
|
|
* @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
|
|
* @retval None
|
|
*/
|
|
|
|
void MyTimer_PWMservo_Conf(void)
|
|
{
|
|
MyPWM_Conf_Output(TIM4, LL_TIM_CHANNEL_CH3);
|
|
}
|
|
|
|
/**
|
|
* @brief Démarre le timer considéré
|
|
* @note
|
|
* @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
|
|
* @retval None
|
|
*/
|
|
void MyTimer_Start(TIM_TypeDef * Timer)
|
|
{
|
|
LL_TIM_EnableCounter(Timer);
|
|
}
|
|
|
|
/**
|
|
* @brief Arrêt le timer considéré
|
|
* @note
|
|
* @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
|
|
* @retval None
|
|
*/
|
|
void MyTimer_Stop(TIM_TypeDef * Timer)
|
|
{
|
|
LL_TIM_DisableCounter(Timer);
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Configure le Timer considéré en interruption sur débordement.
|
|
* @note A ce stade, les interruptions ne sont pas validés (voir MyTimer_IT_Enable )
|
|
* @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
|
|
* void (*IT_function) (void) : nom (adresse) de la fonction à lancer sur interruption
|
|
* int Prio : priorité associée à l'interruption
|
|
* @retval None
|
|
*/
|
|
void MyTimer_IT_Conf(TIM_TypeDef * Timer, void (*IT_function) (void),int Prio)
|
|
{
|
|
// affectation de la fonction
|
|
if (Timer==TIM1) Ptr_ItFct_TIM1=IT_function;
|
|
else if (Timer==TIM2) Ptr_ItFct_TIM2=IT_function;
|
|
else if (Timer==TIM3) Ptr_ItFct_TIM3=IT_function;
|
|
else Ptr_ItFct_TIM4=IT_function;
|
|
|
|
|
|
// Blocage IT (il faudra la débloquer voir fct suivante)
|
|
LL_TIM_DisableIT_UPDATE(Timer);
|
|
|
|
// validation du canal NVIC
|
|
IRQn_Type TIM_irq;
|
|
|
|
if (Timer==TIM1) TIM_irq=TIM1_UP_IRQn;
|
|
else if (Timer==TIM2) TIM_irq=TIM2_IRQn;
|
|
else if (Timer==TIM3) TIM_irq=TIM3_IRQn;
|
|
else TIM_irq=TIM4_IRQn;
|
|
|
|
NVIC_SetPriority(TIM_irq, Prio);
|
|
NVIC_EnableIRQ(TIM_irq);
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Autorise les interruptions
|
|
* @note
|
|
* @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
|
|
* @retval None
|
|
*/
|
|
void MyTimer_IT_Enable(TIM_TypeDef * Timer)
|
|
{
|
|
LL_TIM_EnableIT_UPDATE(Timer);
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Interdit les interruptions
|
|
* @note
|
|
* @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
|
|
* @retval None
|
|
*/
|
|
void MyTimer_IT_Disable(TIM_TypeDef * Timer)
|
|
{
|
|
LL_TIM_DisableIT_UPDATE(Timer);
|
|
}
|
|
|
|
|
|
/*
|
|
============ LES INTERRUPTIONS =================================
|
|
|
|
*/
|
|
|
|
void TIM1_UP_IRQHandler(void)
|
|
{
|
|
// rabaisser le flag d'IT
|
|
LL_TIM_ClearFlag_UPDATE(TIM1);
|
|
(*Ptr_ItFct_TIM1)();
|
|
}
|
|
|
|
void TIM2_IRQHandler(void)
|
|
{
|
|
// rabaisser le flag d'IT
|
|
LL_TIM_ClearFlag_UPDATE(TIM2);
|
|
(*Ptr_ItFct_TIM2)();
|
|
}
|
|
|
|
void TIM3_IRQHandler(void)
|
|
{
|
|
// rabaisser le flag d'IT
|
|
LL_TIM_ClearFlag_UPDATE(TIM3);
|
|
(*Ptr_ItFct_TIM3)();
|
|
}
|
|
|
|
void TIM4_IRQHandler(void)
|
|
{
|
|
// rabaisser le flag d'IT
|
|
LL_TIM_ClearFlag_UPDATE(TIM4);
|
|
(*Ptr_ItFct_TIM4)();
|
|
}
|