RefKEIL/ProjetsKEIL/Drivers/timerdriver.c

85 lines
1.8 KiB
C

#include "timerdriver.h"
#include "gpiodriver.h"
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);
}
void TIM2_IRQHandler(void)
{
MyGPIO_Toggle(GPIOA,5);
//we reset the flag
TIM2->SR &= ~(0x1<<UIF);
}
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;
}
}