Periph-Voilier/Services/Chrono.c
2020-11-24 22:56:56 +01:00

235 lines
5.1 KiB
C

// A COMPLETER
/*
Service permettant de chornométrer jusqu'à 59mn 59s 99 1/100
Utilise un timer au choix (TIMER1 à TIMER4).
Utilise la lib MyTimers.h /.c
*/
#include "Chrono.h"
#include "MyTimer.h"
#include "stm32f1xx_ll_gpio.h"
#include "stm32f1xx_ll_bus.h" // Pour l'activation des horloges
// variable privée de type Time qui mémorise la durée mesurée
static Time Chrono_Time; // rem : static rend la visibilité de la variable Chrono_Time limitée à ce fichier
// variable privée qui mémorise pour le module le timer utilisé par le module
static TIM_TypeDef * Chrono_Timer=TIM1; // init par défaut au cas où l'utilisateur ne lance pas Chrono_Conf avant toute autre fct.
//mémorise l'état précédent du bouton Start/Stop
static int Prev_Start_Stop_BP_Status=0;
//mémorise l'état du chrono (lancé ou pas)
static int Started=1;
// déclaration callback appelé toute les 10ms
void Chrono_Task_10ms(void);
//Configuration des I/O
void Chrono_Conf_io(void);
/**
* @brief Configure le chronomètre.
* @note A lancer avant toute autre fonction.
* @param Timer : indique le timer à utiliser par le chronomètre, TIM1, TIM2, TIM3 ou TIM4
* @retval None
*/
void Chrono_Conf(TIM_TypeDef * Timer)
{
// Reset Time
Chrono_Time.Hund=0;
Chrono_Time.Sec=0;
Chrono_Time.Min=0;
// Fixation du Timer
Chrono_Timer=Timer;
// Réglage Timer par défaut
MyTimer_Conf(Chrono_Timer,0xFFFF,0);
// Réglage interruption du Timer avec callback : Chrono_Task_10ms()
MyTimer_IT_Conf(Chrono_Timer, Chrono_Task_10ms,3);
// Validation IT
MyTimer_IT_Enable(Chrono_Timer);
}
/**
* @brief Démarre le chronomètre.
* @note si la durée dépasse 59mn 59sec 99 Hund, elle est remise à zéro et repart
* @param Aucun
* @retval Aucun
*/
void Chrono_Start(void)
{
Chrono_Timer=TIM3;
MyTimer_Start(Chrono_Timer); //Start Timer 3
Chrono_Timer=TIM1;
MyTimer_Start(Chrono_Timer); //Start Timer 1
}
/**
* @brief Arrête le chronomètre.
* @note
* @param Aucun
* @retval Aucun
*/
void Chrono_Stop(void)
{
MyTimer_Stop(Chrono_Timer);
}
/**
* @brief Remet le chronomètre à 0
* @note
* @param Aucun
* @retval Aucun
*/
void Chrono_Reset(void)
{
// Arrêt Chrono
MyTimer_Stop(Chrono_Timer);
// Reset Time
Chrono_Time.Hund=0;
Chrono_Time.Sec=0;
Chrono_Time.Min=0;
}
/**
* @brief Renvoie l'adresse de la variable Time privée gérée dans le module Chrono.c
* @note
* @param Aucun
* @retval adresse de la variable Time
*/
Time * Chrono_Read(void)
{
return &Chrono_Time;
}
/**
* @brief incrémente la variable privée Chron_Time modulo 60mn
* @note
* @param Aucun
* @retval Aucun
*/
void Chrono_Task_10ms(void)
{
Chrono_Time.Hund++;
if (Chrono_Time.Hund==100)
{
Chrono_Time.Sec++;
Chrono_Time.Hund=0;
LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_5);
}
if (Chrono_Time.Sec==60)
{
Chrono_Time.Min++;
Chrono_Time.Sec=0;
}
if (Chrono_Time.Min==60)
{
Chrono_Time.Hund=0;
}
}
/**
* @brief polle les 3 entrées et gènère les actions à faire
* @note Fct à lancer en tâche de fond (non bloquante)
* @param None
* @retval None
*/
void Chrono_Background(void) {
//Lecture PC8 bouton Start/Stop
int Start_Stop_BP_Status = LL_GPIO_IsInputPinSet(GPIOC, LL_GPIO_PIN_8);
//Changement de l'état du chrono sur front montant du BP
if(Start_Stop_BP_Status && (Start_Stop_BP_Status != Prev_Start_Stop_BP_Status)) {
if(!Started){
Chrono_Start();
Started = 1;
}
else {
Chrono_Stop();
Started = 0;
}
}
//Actualisation mémoire BP
Prev_Start_Stop_BP_Status = Start_Stop_BP_Status;
//Reset sur PC13 high
if(!LL_GPIO_IsInputPinSet(GPIOC, LL_GPIO_PIN_13)) {
Chrono_Time.Hund=0;
Chrono_Time.Sec=0;
Chrono_Time.Min=0;
}
}
/**
* @brief configure les 3 pins d'I/O
* @note
* @param None
* @retval None
*/
void Chrono_Conf_io(void) {
//Validation horloge locale
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC);
GPIO_TypeDef * GPIO;
LL_GPIO_InitTypeDef My_GPIO_Init_Struct;
//PA5 (LED de la carte) en output push-pull
GPIO = GPIOA;
My_GPIO_Init_Struct.Pin = LL_GPIO_PIN_5;
My_GPIO_Init_Struct.Mode = LL_GPIO_MODE_OUTPUT;
My_GPIO_Init_Struct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
My_GPIO_Init_Struct.Speed = LL_GPIO_MODE_OUTPUT_10MHz;
My_GPIO_Init_Struct.Pull = LL_GPIO_PULL_DOWN;
LL_GPIO_Init(GPIO, &My_GPIO_Init_Struct);
//PC8 en floating input
GPIO = GPIOC;
My_GPIO_Init_Struct.Pin = LL_GPIO_PIN_8;
My_GPIO_Init_Struct.Mode = LL_GPIO_MODE_FLOATING;
My_GPIO_Init_Struct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
My_GPIO_Init_Struct.Speed = LL_GPIO_SPEED_FREQ_LOW;
My_GPIO_Init_Struct.Pull = LL_GPIO_PULL_DOWN;
LL_GPIO_Init(GPIO, &My_GPIO_Init_Struct);
//PC13 (USER Button bleu carte) en floating input
GPIO = GPIOC;
My_GPIO_Init_Struct.Pin = LL_GPIO_PIN_13;
My_GPIO_Init_Struct.Mode = LL_GPIO_MODE_FLOATING;
My_GPIO_Init_Struct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
My_GPIO_Init_Struct.Speed = LL_GPIO_SPEED_FREQ_LOW;
My_GPIO_Init_Struct.Pull = LL_GPIO_PULL_DOWN;
LL_GPIO_Init(GPIO, &My_GPIO_Init_Struct);
}