voilier-team-1/driver-siyo/timerdriver.c

162 lines
3.8 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "timerdriver.h"
void (* pFnc) (void); /* déclaration dun 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);
}
void MyTimer_PWM_Init(MyPWM_Struct_TypeDef * PWM)
{
switch(PWM->channel)
{
case 2:
PWM->Timer->CCMR1 |= (PWMMode_1<<OC24M_START); //We activate the PWM Mode 1 for the given channel
PWM->Timer->CCR2 = PWM->CCR; //Compare Capture register count
break;
case 3:
PWM->Timer->CCMR2 |= (PWMMode_1<<OC13M_START);
PWM->Timer->CCR3 = PWM->CCR;
break;
case 4:
PWM->Timer->CCMR2 |= (PWMMode_1<<OC24M_START);
PWM->Timer->CCR4 = PWM->CCR;
break;
default:
PWM->Timer->CCMR1 |= (PWMMode_1<<OC13M_START);
PWM->Timer->CCR1 = PWM->CCR;
break;
}
PWM->Timer->CCER |= (1<<4*(PWM->channel-1)); //enable capture/compare registers
}
MyGPIO_Struct_TypeDef GPIOFromPWM(MyPWM_Struct_TypeDef PWM)
{
//use of C99 compound literal for return statement, may not work on C90.
if(PWM.Timer == TIM2)
{
//PA0 -> TIM2,CH1... iteration
return (MyGPIO_Struct_TypeDef){GPIOA,PWM.channel-1,AltOut_Ppull};
}
else if(PWM.Timer == TIM3)
{
if(PWM.channel > 2) {
return (MyGPIO_Struct_TypeDef){GPIOB,PWM.channel-3,AltOut_Ppull}; //PB0 -> TIM3,CH3;PB1 -> TIM3,CH4
}
else {
return (MyGPIO_Struct_TypeDef){GPIOA,PWM.channel+5,AltOut_Ppull}; //PA6 -> TIM3,CH1;PA7 -> TIM3,CH2
}
}
else if(PWM.Timer == TIM4)
{
return (MyGPIO_Struct_TypeDef){GPIOB,PWM.channel+5,AltOut_Ppull}; //PB6 -> TIM4,CH1... iteration
}
else { //TIM1 case
return (MyGPIO_Struct_TypeDef){GPIOA,PWM.channel+7,AltOut_Ppull};//PA8 -> TIM1,CH1... iteration
}
}
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 */
TIM1->SR &= ~(0x1<<UIF);//reset flag
}
void TIM2_IRQHandler(void)
{
if (pFnc != 0)
(*pFnc) (); /* appel indirect de la fonction */
TIM2->SR &= ~(0x1<<UIF);//reset flag
}
void TIM3_IRQHandler(void)
{
if (pFnc != 0)
(*pFnc) (); /* appel indirect de la fonction */
TIM3->SR &= ~(0x1<<UIF);//reset flag
}
void TIM4_IRQHandler(void)
{
if (pFnc != 0)
(*pFnc) (); /* appel indirect de la fonction */
TIM4->SR &= ~(0x1<<UIF);//reset flag
}