143 lines
4 KiB
C
143 lines
4 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"
|
|
#include "MyTimer.h"
|
|
|
|
static void (*tim1_handler)(void) = 0;
|
|
static void (*tim2_handler)(void) = 0;
|
|
static void (*tim3_handler)(void) = 0;
|
|
static void (*tim4_handler)(void) = 0;
|
|
|
|
|
|
// TODO: les callbacks ne sont pas appelés :(
|
|
void TIM1_UP_IRQHandler() {
|
|
TIM1->SR &= ~TIM_SR_UIF_Msk;
|
|
(*tim1_handler)();
|
|
}
|
|
|
|
void TIM2_IRQHandler() {
|
|
TIM2->SR &= ~TIM_SR_UIF_Msk;
|
|
(*tim2_handler)();
|
|
}
|
|
|
|
void TIM3_IRQHandler() {
|
|
TIM3->SR &= ~TIM_SR_UIF_Msk;
|
|
(*tim3_handler)();
|
|
}
|
|
|
|
void TIM4_IRQHandler() {
|
|
TIM4->SR &= ~TIM_SR_UIF_Msk;
|
|
(*tim4_handler)();
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* @brief Active l'horloge et règle l'ARR et le PSC du timer visé
|
|
* @note Fonction à lancer avant toute autre. Le timer n'est pas encore lancé (voir MyTimerStart)
|
|
* @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
|
|
* int Arr : valeur à placer dans ARR (autoreload)
|
|
* int Psc : valeur à placer dans PSC (prescaler)
|
|
* @retval None
|
|
*/
|
|
void MyTimer_Conf(TIM_TypeDef * Timer, int Arr, int Psc) {
|
|
|
|
// Activation de la clock correspondante au timer
|
|
if (Timer == TIM1) {
|
|
RCC->APB2ENR = RCC->APB2ENR | RCC_APB2ENR_TIM1EN;
|
|
} else if (Timer == TIM2) {
|
|
RCC->APB1ENR = RCC->APB1ENR | RCC_APB1ENR_TIM2EN;
|
|
} else if (Timer == TIM3) {
|
|
RCC->APB1ENR = RCC->APB1ENR | RCC_APB1ENR_TIM3EN;
|
|
} else if (Timer == TIM4) {
|
|
RCC->APB1ENR = RCC->APB1ENR | RCC_APB1ENR_TIM4EN;
|
|
}
|
|
|
|
// Règlage ARR
|
|
Timer->ARR = Arr;
|
|
|
|
// Réglage PSC
|
|
Timer->PSC = Psc;
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Démarre le timer considéré
|
|
* @note
|
|
* @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
|
|
* @retval None
|
|
*/
|
|
void MyTimer_Start(TIM_TypeDef * Timer) {
|
|
Timer->CR1 = Timer->CR1 | TIM_CR1_CEN_Msk;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Arrêt le timer considéré
|
|
* @note
|
|
* @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
|
|
* @retval None
|
|
*/
|
|
void MyTimer_Stop(TIM_TypeDef * Timer) {
|
|
Timer->CR1 = Timer->CR1 & ~TIM_CR1_CEN_Msk;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Configure le Timer considéré en interruption sur débordement.
|
|
* @note A ce stade, les interruptions ne sont pas validés (voir MyTimer_IT_Enable )
|
|
* @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
|
|
* void (*IT_function) (void) : nom (adresse) de la fonction à lancer sur interruption
|
|
* int Prio : priorité associée à l'interruption
|
|
* @retval None
|
|
*/
|
|
void MyTimer_IT_Conf(TIM_TypeDef * Timer, void (*IT_function) (void), int Prio) {
|
|
if (Timer == TIM1) {
|
|
NVIC->ISER[0] |= 1 << TIM1_UP_IRQn;
|
|
tim1_handler = IT_function;
|
|
} else if (Timer == TIM2) {
|
|
NVIC->ISER[0] |= 1 << TIM2_IRQn;
|
|
tim2_handler = IT_function;
|
|
} else if (Timer == TIM3) {
|
|
NVIC->ISER[0] |= 1 << TIM3_IRQn;
|
|
tim3_handler = IT_function;
|
|
} else if (Timer == TIM4) {
|
|
NVIC->ISER[0] |= 1 << TIM4_IRQn;
|
|
tim4_handler = IT_function;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Autorise les interruptions
|
|
* @note
|
|
* @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
|
|
* @retval None
|
|
*/
|
|
void MyTimer_IT_Enable(TIM_TypeDef * Timer) {
|
|
Timer->DIER = Timer->DIER | TIM_DIER_UIE_Msk;
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief Interdit les interruptions
|
|
* @note
|
|
* @param TIM_TypeDef Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
|
|
* @retval None
|
|
*/
|
|
void MyTimer_IT_Disable(TIM_TypeDef * Timer) {
|
|
Timer->DIER = Timer->DIER & ~TIM_DIER_UIE_Msk;
|
|
}
|
|
|
|
|