NNP STM Generic Remote Module git-main
Loading...
Searching...
No Matches
timer_stm.c
Go to the documentation of this file.
1
11#include "timer.h"
12#include "stm32l4xx.h"
13#include "main.h"
14
15/************************** Module variables **********************************/
16// Store the last timer value to calculate the elapsed time
17static volatile TIMEVAL last_time_set = 0;//TIMEVAL_MAX;
18static volatile TIMEVAL elapsed_time = 0;
19
23void setTimer(TIMEVAL value){
24 uint32_t timer = TIM2->CNT;
25 elapsed_time += timer - last_time_set;
26
27 last_time_set = 65535-value;
28 TIM2->CNT = last_time_set;
29
30 TIM2->CR1 |= 1; //Re-enable timer
31}
32
33void initTimer(void)
34{
35
36}
37
41UNS8 isTimedOut( UNS32 *tRef, UNS32 tAlarm )
42{
43
44 if( (HAL_GetTick() - *tRef) > tAlarm )
45 {
46 return 1;
47 }
48
49 else
50 return 0;
51}
52
56void resetTimeOut( UNS32 *tRef )
57{
58 *tRef = HAL_GetTick();
59}
60
65TIMEVAL getElapsedTime(void)
66{
67 TIMEVAL timer = TIM2->CNT;
68
69 if (timer > last_time_set) // In case the timer value is higher than the last time.
70 return (timer - last_time_set); // Calculate the time difference
71 else if (timer < last_time_set)
72 return (last_time_set - timer); // Calculate the time difference
73 else
74 return TIMEVAL_MAX;
75}
76
77void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
78{
79 if (htim->Instance == TIM2) {
80 last_time_set = 0;
81 elapsed_time = 0;
82 CLEAR_BIT(TIM2->SR, TIM_SR_UIF); //Clear pending interrupt
84 }
85}
#define UNS8
Unsigned int8 representation in CANFest.
Definition applicfg.h:25
#define UNS32
Unsigned int32 representation in CANFest.
Definition applicfg.h:27
void TimeDispatch(void)
TimeDispatch is called on each timer expiration -—.
Definition timer.c:102
: Header for main.c file. This file contains the common defines of the application.
UNS8 isTimedOut(UNS32 *tRef, UNS32 tAlarm)
Checks if a timer has timed out.
Definition timer_stm.c:41
TIMEVAL getElapsedTime(void)
Return the elapsed time to tell the Stack how much time is spent since last call.
Definition timer_stm.c:65
void resetTimeOut(UNS32 *tRef)
Resets a timed out timer.
Definition timer_stm.c:56
void setTimer(TIMEVAL value)
Set the timer peripheral for the next interrupt.
Definition timer_stm.c:23