forked from trocache/RefKEIL
110 lines
2.3 KiB
C
110 lines
2.3 KiB
C
#include "timerdriver.h"
|
||
#include "gpiodriver.h"
|
||
|
||
void (* pFnc) (void); /* déclaration d’un pointeur de fonction */
|
||
|
||
void Init_Periph (void (* ptrFonction)(void))
|
||
{
|
||
pFnc = ptrFonction; /* affectation du pointeur */
|
||
}
|
||
|
||
void MyTimer_Base_Init(MyTimer_Struct_TypeDef * Timer)
|
||
{
|
||
//TIM1 uses the APB2ENR register from RCC. The others uses the APB1ENR, so we check this value.
|
||
if(Timer->Timer == TIM1)
|
||
{
|
||
RCC->APB2ENR |= TimerX2Int(Timer->Timer);
|
||
} else {
|
||
RCC->APB1ENR |= TimerX2Int(Timer->Timer);
|
||
}
|
||
Timer->Timer->ARR = Timer->ARR;
|
||
Timer->Timer->PSC = Timer->PSC;
|
||
}
|
||
|
||
void MyTimer_ActiveIT(TIM_TypeDef * TimerX, uint8_t Prio)
|
||
{
|
||
uint8_t positionTimerIT = TimerIT2UInt(TimerX);
|
||
TimerX->DIER |= (0x1<<UIE);
|
||
NVIC->IP[positionTimerIT] |= (Prio << 0x4);
|
||
NVIC->ISER[0] |= (0x1<<positionTimerIT);
|
||
}
|
||
|
||
int TimerX2Int(TIM_TypeDef * TimerX)
|
||
{
|
||
if(TimerX == TIM1)
|
||
{
|
||
return (0x01 << 11);
|
||
} else if (TimerX == TIM2){
|
||
return (0x01 << 0);
|
||
} else if (TimerX == TIM3){
|
||
return (0x01 << 1);
|
||
} else if (TimerX == TIM4){
|
||
return (0x01 << 2);
|
||
} /*else if (TimerX == TIM5){
|
||
return (0x01 << 3);
|
||
} else if (TimerX == TIM6){
|
||
return (0x01 << 4);
|
||
} else if (TimerX == TIM7){
|
||
return (0x01 << 5);
|
||
} else if (TimerX == TIM8){
|
||
return (0x01 << 13);
|
||
} else if (TimerX == TIM9){ //For now we dont do timer > 4
|
||
return (0x01 << 19);
|
||
} else if (TimerX == TIM10){
|
||
return (0x01 << 20);
|
||
} else if (TimerX == TIM11){
|
||
return (0x01 << 21);
|
||
} else if (TimerX == TIM12){
|
||
return (0x01 << 6);
|
||
} else if (TimerX == TIM13){
|
||
return (0x01 << 7);
|
||
} else if (TimerX == TIM14){
|
||
return (0x01 << 8);
|
||
}*/ else {
|
||
return -1;
|
||
};
|
||
}
|
||
|
||
uint8_t TimerIT2UInt(TIM_TypeDef * TimerX)
|
||
{
|
||
if(TimerX == TIM1)
|
||
{
|
||
return TIM1_CC_IRQn;
|
||
} else if(TimerX == TIM2)
|
||
{
|
||
return TIM2_IRQn;
|
||
} else if(TimerX == TIM3)
|
||
{
|
||
return TIM3_IRQn;
|
||
} else if(TimerX == TIM4)
|
||
{
|
||
return 30;
|
||
} else {
|
||
return 0;
|
||
}
|
||
}
|
||
|
||
void TIM1_CC_IRQHandler(void)
|
||
{
|
||
if (pFnc != 0)
|
||
(*pFnc) (); /* appel indirect de la fonction */
|
||
}
|
||
|
||
void TIM2_IRQHandler(void)
|
||
{
|
||
if (pFnc != 0)
|
||
(*pFnc) (); /* appel indirect de la fonction */
|
||
TIM2->SR &= ~(0x1<<UIF);
|
||
}
|
||
|
||
void TIM3_IRQHandler(void)
|
||
{
|
||
if (pFnc != 0)
|
||
(*pFnc) (); /* appel indirect de la fonction */
|
||
}
|
||
|
||
void TIM4_IRQHandler(void)
|
||
{
|
||
if (pFnc != 0)
|
||
(*pFnc) (); /* appel indirect de la fonction */
|
||
}
|