InformatiqueMaterielle4A/MyDrivers/MyTimer.c

112 lines
No EOL
2.7 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 "stm32f103xb.h"
void (* ptrfonc1)(void);
void (* ptrfonc2)(void);
void (* ptrfonc3)(void);
void (* ptrfonc4)(void);
void MyTimer_Conf(TIM_TypeDef * Timer,int Arr, int Psc) {
int valid = 1;
if (Timer == TIM1) {
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
} else if (Timer == TIM2) {
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
} else if (Timer == TIM3) {
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
} else if (Timer == TIM4) {
RCC->APB1ENR |= RCC_APB1ENR_TIM4EN;
} else {
valid = 0;
}
if (valid) {
Timer->ARR = Arr;
Timer->PSC = Psc;
}
}
void MyTimer_Start(TIM_TypeDef * Timer) {
Timer->CR1 |= TIM_CR1_CEN;
}
void MyTimer_Stop(TIM_TypeDef * Timer) {
Timer->CR1 &= ~TIM_CR1_CEN;
}
void MyTimer_IT_Conf(TIM_TypeDef * Timer, void (*IT_function) (void),int Prio) {
if (Timer == TIM1) {
NVIC->ISER[0] = NVIC->ISER[0] | (1 << 25);
NVIC->IP[25] = NVIC->IP[25] | (Prio << 4);
ptrfonc1 = IT_function;
} else if (Timer == TIM2) {
NVIC->ISER[0] = NVIC->ISER[0] | (1 << 28);
NVIC->IP[28] = NVIC->IP[28] | (Prio << 4);
ptrfonc2 = IT_function;
} else if (Timer == TIM3) {
NVIC->ISER[0] = NVIC->ISER[0] | (1 << 29);
NVIC->IP[29] = NVIC->IP[29] | (Prio << 4);
ptrfonc3 = IT_function;
} else if (Timer == TIM4) {
NVIC->ISER[0] = NVIC->ISER[0] | (1 << 30);
NVIC->IP[30] = NVIC->IP[30] | (Prio << 4);
ptrfonc4 = IT_function;
}
}
void MyTimer_IT_Enable(TIM_TypeDef * Timer) {
if (Timer == TIM1) {
TIM1->DIER |= TIM_DIER_UIE;
} else if (Timer == TIM2) {
TIM2->DIER |= TIM_DIER_UIE;
} else if (Timer == TIM3) {
TIM3->DIER |= TIM_DIER_UIE;
} else if (Timer == TIM4) {
TIM4->DIER |= TIM_DIER_UIE;
}
}
void MyTimer_IT_Disable(TIM_TypeDef * Timer) {
if (Timer == TIM1) {
TIM1->DIER &= ~TIM_DIER_UIE;
} else if (Timer == TIM2) {
TIM2->DIER &= ~TIM_DIER_UIE;
} else if (Timer == TIM3) {
TIM3->DIER &= ~TIM_DIER_UIE;
} else if (Timer == TIM4) {
TIM4->DIER &= ~TIM_DIER_UIE;
}
}
void TIM1_UP_IRQHandler(void) {
TIM1->SR &= ~TIM_SR_UIF;
(*ptrfonc1)();
}
void TIM2_IRQHandler(void) {
TIM2->SR &= ~TIM_SR_UIF;
(*ptrfonc2)();
}
void TIM3_IRQHandler(void) {
TIM3->SR &= ~TIM_SR_UIF;
(*ptrfonc3)();
}
void TIM4_IRQHandler(void) {
TIM4->SR &= ~TIM_SR_UIF;
(*ptrfonc4)();
}