Projet-Voilier-3/driver/Driver_Timer.c
2023-03-27 16:35:57 +02:00

98 lines
1.7 KiB
C

#include "Driver_Timer.h"
#include "stm32f10x.h"
#include "stdio.h"
/* Timer init function */
void MyTimer_Base_Init(MyTimer_Struct_TypeDef * Timer)
{
/* Reset the selected Timer */
if(Timer->Timer == TIM1)
{
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
}
else if(Timer->Timer == TIM2)
{
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
}
else if(Timer->Timer == TIM3)
{
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
}
else if(Timer->Timer == TIM4)
{
RCC->APB1ENR |= RCC_APB1ENR_TIM4EN;
}
Timer->Timer->PSC = Timer->PSC;
Timer->Timer->ARR = Timer->ARR;
}
void MyTimer_Start(MyTimer_Struct_TypeDef * Timer)
{
Timer->Timer->CR1 |= TIM_CR1_CEN;
}
void MyTimer_Stop(MyTimer_Struct_TypeDef * Timer)
{
Timer->Timer->CR1 &= ~TIM_CR1_CEN;
}
void MyTimer_ConfigurePWM(MyTimer_Struct_TypeDef *Timer, uint16_t duty_cycle) {
Timer->Timer->CCMR1 |= 0x6 << 0xC;
Timer->Timer->CCR2 = (duty_cycle * Timer->ARR) / 100;
Timer->Timer->CCER |= TIM_CCER_CC2E;
}
void Bug (void)
{
while(1);
}
void (*TIM2_fx) (void) = &Bug;
void (*TIM3_fx) (void) = &Bug;
void (*TIM4_fx) (void) = &Bug;
void MyTimer_ActiveIT (TIM_TypeDef * Timer, char Prio, void (*IT_function)(void))
{
Timer->DIER |= TIM_DIER_UIE;
if(Timer == TIM1)
{
}
if(Timer == TIM2)
{
NVIC_EnableIRQ(TIM2_IRQn);
NVIC_SetPriority(TIM2_IRQn, Prio);
TIM2_fx = IT_function;
}
if(Timer == TIM3)
{
NVIC_EnableIRQ(TIM3_IRQn);
NVIC_SetPriority(TIM3_IRQn, Prio);
TIM3_fx = IT_function;
}
if(Timer == TIM4)
{
NVIC_EnableIRQ(TIM4_IRQn);
NVIC_SetPriority(TIM4_IRQn, Prio);
TIM4_fx = IT_function;
}
}
void TIM2_IRQHandler (void)
{
TIM2->SR &= ~TIM_SR_UIF;
(*TIM2_fx)();
}
void TIM3_IRQHandler (void)
{
TIM3->SR &= ~TIM_SR_UIF;
(*TIM3_fx)();
}
void TIM4_IRQHandler (void)
{
TIM3->SR &= ~TIM_SR_UIF;
(*TIM4_fx)();
}