first
This commit is contained in:
6
Utilities/timer/LICENSE.txt
Normal file
6
Utilities/timer/LICENSE.txt
Normal file
@ -0,0 +1,6 @@
|
||||
This software component is provided to you as part of a software package and
|
||||
applicable license terms are in the Package_license file. If you received this
|
||||
software component outside of a package or without applicable license terms,
|
||||
the terms of the BSD-3-Clause license shall apply.
|
||||
You may obtain a copy of the BSD-3-Clause at:
|
||||
https://opensource.org/licenses/BSD-3-Clause
|
||||
542
Utilities/timer/stm32_timer.c
Normal file
542
Utilities/timer/stm32_timer.c
Normal file
@ -0,0 +1,542 @@
|
||||
/*!
|
||||
* \file timer.c
|
||||
*
|
||||
* \brief Timer objects and scheduling management implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
* ______ _
|
||||
* / _____) _ | |
|
||||
* ( (____ _____ ____ _| |_ _____ ____| |__
|
||||
* \____ \| ___ | (_ _) ___ |/ ___) _ \
|
||||
* _____) ) ____| | | || |_| ____( (___| | | |
|
||||
* (______/|_____)_|_|_| \__)_____)\____)_| |_|
|
||||
* (C)2013-2017 Semtech
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author Miguel Luis ( Semtech )
|
||||
*
|
||||
* \author Gregory Cristian ( Semtech )
|
||||
*/
|
||||
|
||||
/******************************************************************************
|
||||
* @file stm32_timer.c
|
||||
* @author MCD Application Team
|
||||
* @brief Time server utility
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2019 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "stm32_timer.h"
|
||||
|
||||
/** @addtogroup TIMER_SERVER
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* Private macro -----------------------------------------------------------*/
|
||||
/**
|
||||
* @defgroup TIMER_SERVER_private_macro TIMER_SERVER private macros
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @brief macro definition to initialize a critical section.
|
||||
*
|
||||
*/
|
||||
#ifndef UTIL_TIMER_INIT_CRITICAL_SECTION
|
||||
#define UTIL_TIMER_INIT_CRITICAL_SECTION( )
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief macro definition to enter a critical section.
|
||||
*
|
||||
*/
|
||||
#ifndef UTIL_TIMER_ENTER_CRITICAL_SECTION
|
||||
#define UTIL_TIMER_ENTER_CRITICAL_SECTION( ) UTILS_ENTER_CRITICAL_SECTION( )
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief macro definition to exit a critical section.
|
||||
*
|
||||
*/
|
||||
#ifndef UTIL_TIMER_EXIT_CRITICAL_SECTION
|
||||
#define UTIL_TIMER_EXIT_CRITICAL_SECTION( ) UTILS_EXIT_CRITICAL_SECTION( )
|
||||
#endif
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/* Private variables -----------------------------------------------------------*/
|
||||
/**
|
||||
* @defgroup TIMER_SERVER_private_varaible TIMER_SERVER private variable
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Timers list head pointer
|
||||
*
|
||||
*/
|
||||
static UTIL_TIMER_Object_t *TimerListHead = NULL;
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @defgroup TIMER_SERVER_private_function TIMER_SERVER private function
|
||||
* @{
|
||||
*/
|
||||
|
||||
void TimerInsertNewHeadTimer( UTIL_TIMER_Object_t *TimerObject );
|
||||
void TimerInsertTimer( UTIL_TIMER_Object_t *TimerObject );
|
||||
void TimerSetTimeout( UTIL_TIMER_Object_t *TimerObject );
|
||||
bool TimerExists( UTIL_TIMER_Object_t *TimerObject );
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/* Functions Definition ------------------------------------------------------*/
|
||||
/**
|
||||
* @addtogroup TIMER_SERVER_exported_function
|
||||
* @{
|
||||
*/
|
||||
|
||||
UTIL_TIMER_Status_t UTIL_TIMER_Init(void)
|
||||
{
|
||||
UTIL_TIMER_INIT_CRITICAL_SECTION();
|
||||
TimerListHead = NULL;
|
||||
return UTIL_TimerDriver.InitTimer();
|
||||
}
|
||||
|
||||
UTIL_TIMER_Status_t UTIL_TIMER_DeInit(void)
|
||||
{
|
||||
return UTIL_TimerDriver.DeInitTimer();
|
||||
}
|
||||
|
||||
UTIL_TIMER_Status_t UTIL_TIMER_Create( UTIL_TIMER_Object_t *TimerObject, uint32_t PeriodValue, UTIL_TIMER_Mode_t Mode, void ( *Callback )( void *), void *Argument)
|
||||
{
|
||||
if((TimerObject != NULL) && (Callback != NULL))
|
||||
{
|
||||
TimerObject->Timestamp = 0U;
|
||||
TimerObject->ReloadValue = UTIL_TimerDriver.ms2Tick(PeriodValue);
|
||||
TimerObject->IsPending = 0U;
|
||||
TimerObject->IsRunning = 0U;
|
||||
TimerObject->IsReloadStopped = 0U;
|
||||
TimerObject->Callback = Callback;
|
||||
TimerObject->argument = Argument;
|
||||
TimerObject->Mode = Mode;
|
||||
TimerObject->Next = NULL;
|
||||
return UTIL_TIMER_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
return UTIL_TIMER_INVALID_PARAM;
|
||||
}
|
||||
}
|
||||
|
||||
UTIL_TIMER_Status_t UTIL_TIMER_Start( UTIL_TIMER_Object_t *TimerObject)
|
||||
{
|
||||
UTIL_TIMER_Status_t ret = UTIL_TIMER_OK;
|
||||
uint32_t elapsedTime;
|
||||
uint32_t minValue;
|
||||
uint32_t ticks;
|
||||
|
||||
if(( TimerObject != NULL ) && ( TimerExists( TimerObject ) == false ) && (TimerObject->IsRunning == 0U))
|
||||
{
|
||||
UTIL_TIMER_ENTER_CRITICAL_SECTION();
|
||||
ticks = TimerObject->ReloadValue;
|
||||
minValue = UTIL_TimerDriver.GetMinimumTimeout( );
|
||||
|
||||
if( ticks < minValue )
|
||||
{
|
||||
ticks = minValue;
|
||||
}
|
||||
|
||||
TimerObject->Timestamp = ticks;
|
||||
TimerObject->IsPending = 0U;
|
||||
TimerObject->IsRunning = 1U;
|
||||
TimerObject->IsReloadStopped = 0U;
|
||||
if( TimerListHead == NULL )
|
||||
{
|
||||
UTIL_TimerDriver.SetTimerContext();
|
||||
TimerInsertNewHeadTimer( TimerObject ); /* insert a timeout at now+obj->Timestamp */
|
||||
}
|
||||
else
|
||||
{
|
||||
elapsedTime = UTIL_TimerDriver.GetTimerElapsedTime( );
|
||||
TimerObject->Timestamp += elapsedTime;
|
||||
|
||||
if( TimerObject->Timestamp < TimerListHead->Timestamp )
|
||||
{
|
||||
TimerInsertNewHeadTimer( TimerObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
TimerInsertTimer( TimerObject);
|
||||
}
|
||||
}
|
||||
UTIL_TIMER_EXIT_CRITICAL_SECTION();
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = UTIL_TIMER_INVALID_PARAM;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
UTIL_TIMER_Status_t UTIL_TIMER_StartWithPeriod( UTIL_TIMER_Object_t *TimerObject, uint32_t PeriodValue)
|
||||
{
|
||||
UTIL_TIMER_Status_t ret = UTIL_TIMER_OK;
|
||||
|
||||
if(NULL == TimerObject)
|
||||
{
|
||||
ret = UTIL_TIMER_INVALID_PARAM;
|
||||
}
|
||||
else
|
||||
{
|
||||
TimerObject->ReloadValue = UTIL_TimerDriver.ms2Tick(PeriodValue);
|
||||
if(TimerExists(TimerObject))
|
||||
{
|
||||
(void)UTIL_TIMER_Stop(TimerObject);
|
||||
}
|
||||
ret = UTIL_TIMER_Start(TimerObject);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
UTIL_TIMER_Status_t UTIL_TIMER_Stop( UTIL_TIMER_Object_t *TimerObject )
|
||||
{
|
||||
UTIL_TIMER_Status_t ret = UTIL_TIMER_OK;
|
||||
|
||||
if (NULL != TimerObject)
|
||||
{
|
||||
UTIL_TIMER_ENTER_CRITICAL_SECTION();
|
||||
UTIL_TIMER_Object_t* prev = TimerListHead;
|
||||
UTIL_TIMER_Object_t* cur = TimerListHead;
|
||||
TimerObject->IsReloadStopped = 1U;
|
||||
|
||||
/* List is empty or the Obj to stop does not exist */
|
||||
if(NULL != TimerListHead)
|
||||
{
|
||||
TimerObject->IsRunning = 0U;
|
||||
|
||||
if( TimerListHead == TimerObject ) /* Stop the Head */
|
||||
{
|
||||
TimerListHead->IsPending = 0;
|
||||
if( TimerListHead->Next != NULL )
|
||||
{
|
||||
TimerListHead = TimerListHead->Next;
|
||||
TimerSetTimeout( TimerListHead );
|
||||
}
|
||||
else
|
||||
{
|
||||
UTIL_TimerDriver.StopTimerEvt( );
|
||||
TimerListHead = NULL;
|
||||
}
|
||||
}
|
||||
else /* Stop an object within the list */
|
||||
{
|
||||
while( cur != NULL )
|
||||
{
|
||||
if( cur == TimerObject )
|
||||
{
|
||||
if( cur->Next != NULL )
|
||||
{
|
||||
cur = cur->Next;
|
||||
prev->Next = cur;
|
||||
}
|
||||
else
|
||||
{
|
||||
cur = NULL;
|
||||
prev->Next = cur;
|
||||
}
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
prev = cur;
|
||||
cur = cur->Next;
|
||||
}
|
||||
}
|
||||
}
|
||||
ret = UTIL_TIMER_OK;
|
||||
}
|
||||
UTIL_TIMER_EXIT_CRITICAL_SECTION();
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = UTIL_TIMER_INVALID_PARAM;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
UTIL_TIMER_Status_t UTIL_TIMER_SetPeriod(UTIL_TIMER_Object_t *TimerObject, uint32_t NewPeriodValue)
|
||||
{
|
||||
UTIL_TIMER_Status_t ret = UTIL_TIMER_OK;
|
||||
|
||||
if(NULL == TimerObject)
|
||||
{
|
||||
ret = UTIL_TIMER_INVALID_PARAM;
|
||||
}
|
||||
else
|
||||
{
|
||||
TimerObject->ReloadValue = UTIL_TimerDriver.ms2Tick(NewPeriodValue);
|
||||
if(TimerExists(TimerObject))
|
||||
{
|
||||
(void)UTIL_TIMER_Stop(TimerObject);
|
||||
ret = UTIL_TIMER_Start(TimerObject);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
UTIL_TIMER_Status_t UTIL_TIMER_SetReloadMode(UTIL_TIMER_Object_t *TimerObject, UTIL_TIMER_Mode_t ReloadMode)
|
||||
{
|
||||
UTIL_TIMER_Status_t ret = UTIL_TIMER_OK;
|
||||
|
||||
if(NULL == TimerObject)
|
||||
{
|
||||
ret = UTIL_TIMER_INVALID_PARAM;
|
||||
}
|
||||
else
|
||||
{
|
||||
TimerObject->Mode = ReloadMode;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
UTIL_TIMER_Status_t UTIL_TIMER_GetRemainingTime(UTIL_TIMER_Object_t *TimerObject, uint32_t *ElapsedTime)
|
||||
{
|
||||
UTIL_TIMER_Status_t ret = UTIL_TIMER_OK;
|
||||
if(TimerExists(TimerObject))
|
||||
{
|
||||
uint32_t time = UTIL_TimerDriver.GetTimerElapsedTime();
|
||||
if (TimerObject->Timestamp < time )
|
||||
{
|
||||
*ElapsedTime = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
*ElapsedTime = TimerObject->Timestamp - time;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = UTIL_TIMER_INVALID_PARAM;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint32_t UTIL_TIMER_IsRunning( UTIL_TIMER_Object_t *TimerObject )
|
||||
{
|
||||
if( TimerObject != NULL )
|
||||
{
|
||||
return TimerObject->IsRunning;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t UTIL_TIMER_GetFirstRemainingTime(void)
|
||||
{
|
||||
uint32_t NextTimer = 0xFFFFFFFFU;
|
||||
|
||||
if(TimerListHead != NULL)
|
||||
{
|
||||
(void)UTIL_TIMER_GetRemainingTime(TimerListHead, &NextTimer);
|
||||
}
|
||||
return NextTimer;
|
||||
}
|
||||
|
||||
void UTIL_TIMER_IRQ_Handler( void )
|
||||
{
|
||||
UTIL_TIMER_Object_t* cur;
|
||||
uint32_t old, now, DeltaContext;
|
||||
|
||||
UTIL_TIMER_ENTER_CRITICAL_SECTION();
|
||||
|
||||
old = UTIL_TimerDriver.GetTimerContext( );
|
||||
now = UTIL_TimerDriver.SetTimerContext( );
|
||||
|
||||
DeltaContext = now - old; /*intentional wrap around */
|
||||
|
||||
/* update timeStamp based upon new Time Reference*/
|
||||
/* because delta context should never exceed 2^32*/
|
||||
if ( TimerListHead != NULL )
|
||||
{
|
||||
cur = TimerListHead;
|
||||
do {
|
||||
if (cur->Timestamp > DeltaContext)
|
||||
{
|
||||
cur->Timestamp -= DeltaContext;
|
||||
}
|
||||
else
|
||||
{
|
||||
cur->Timestamp = 0;
|
||||
}
|
||||
cur = cur->Next;
|
||||
} while(cur != NULL);
|
||||
}
|
||||
|
||||
/* Execute expired timer and update the list */
|
||||
while ((TimerListHead != NULL) && ((TimerListHead->Timestamp == 0U) || (TimerListHead->Timestamp < UTIL_TimerDriver.GetTimerElapsedTime( ))))
|
||||
{
|
||||
cur = TimerListHead;
|
||||
TimerListHead = TimerListHead->Next;
|
||||
cur->IsPending = 0;
|
||||
cur->IsRunning = 0;
|
||||
cur->Callback(cur->argument);
|
||||
if(( cur->Mode == UTIL_TIMER_PERIODIC) && (cur->IsReloadStopped == 0U))
|
||||
{
|
||||
(void)UTIL_TIMER_Start(cur);
|
||||
}
|
||||
}
|
||||
|
||||
/* start the next TimerListHead if it exists and it is not pending*/
|
||||
if(( TimerListHead != NULL ) && (TimerListHead->IsPending == 0U))
|
||||
{
|
||||
TimerSetTimeout( TimerListHead );
|
||||
}
|
||||
UTIL_TIMER_EXIT_CRITICAL_SECTION();
|
||||
}
|
||||
|
||||
UTIL_TIMER_Time_t UTIL_TIMER_GetCurrentTime(void)
|
||||
{
|
||||
uint32_t now = UTIL_TimerDriver.GetTimerValue( );
|
||||
return UTIL_TimerDriver.Tick2ms(now);
|
||||
}
|
||||
|
||||
UTIL_TIMER_Time_t UTIL_TIMER_GetElapsedTime(UTIL_TIMER_Time_t past )
|
||||
{
|
||||
uint32_t nowInTicks = UTIL_TimerDriver.GetTimerValue( );
|
||||
uint32_t pastInTicks = UTIL_TimerDriver.ms2Tick( past );
|
||||
/* intentional wrap around. Works Ok if tick duation below 1ms */
|
||||
return UTIL_TimerDriver.Tick2ms( nowInTicks- pastInTicks );
|
||||
}
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**************************** Private functions *******************************/
|
||||
|
||||
/**
|
||||
* @addtogroup TIMER_SERVER_private_function
|
||||
*
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @brief Check if the Object to be added is not already in the list
|
||||
*
|
||||
* @param TimerObject Structure containing the timer object parameters
|
||||
* @retval 1 (the object is already in the list) or 0
|
||||
*/
|
||||
bool TimerExists( UTIL_TIMER_Object_t *TimerObject )
|
||||
{
|
||||
UTIL_TIMER_Object_t* cur = TimerListHead;
|
||||
|
||||
while( cur != NULL )
|
||||
{
|
||||
if( cur == TimerObject )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
cur = cur->Next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sets a timeout with the duration "timestamp"
|
||||
*
|
||||
* @param TimerObject Structure containing the timer object parameters
|
||||
*/
|
||||
void TimerSetTimeout( UTIL_TIMER_Object_t *TimerObject )
|
||||
{
|
||||
uint32_t minTicks= UTIL_TimerDriver.GetMinimumTimeout( );
|
||||
TimerObject->IsPending = 1;
|
||||
|
||||
/* In case deadline too soon */
|
||||
if(TimerObject->Timestamp < (UTIL_TimerDriver.GetTimerElapsedTime( ) + minTicks) )
|
||||
{
|
||||
TimerObject->Timestamp = UTIL_TimerDriver.GetTimerElapsedTime( ) + minTicks;
|
||||
}
|
||||
UTIL_TimerDriver.StartTimerEvt( TimerObject->Timestamp );
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Adds a timer to the list.
|
||||
*
|
||||
* @remark The list is automatically sorted. The list head always contains the
|
||||
* next timer to expire.
|
||||
*
|
||||
* @param TimerObject Structure containing the timer object parameters
|
||||
*/
|
||||
void TimerInsertTimer( UTIL_TIMER_Object_t *TimerObject)
|
||||
{
|
||||
UTIL_TIMER_Object_t* cur = TimerListHead;
|
||||
UTIL_TIMER_Object_t* next = TimerListHead->Next;
|
||||
|
||||
while (cur->Next != NULL )
|
||||
{
|
||||
if( TimerObject->Timestamp > next->Timestamp )
|
||||
{
|
||||
cur = next;
|
||||
next = next->Next;
|
||||
}
|
||||
else
|
||||
{
|
||||
cur->Next = TimerObject;
|
||||
TimerObject->Next = next;
|
||||
return;
|
||||
|
||||
}
|
||||
}
|
||||
cur->Next = TimerObject;
|
||||
TimerObject->Next = NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Adds or replace the head timer of the list.
|
||||
*
|
||||
* @param TimerObject Structure containing the timer object parameters
|
||||
*
|
||||
* @remark The list is automatically sorted. The list head always contains the
|
||||
* next timer to expire.
|
||||
*/
|
||||
void TimerInsertNewHeadTimer( UTIL_TIMER_Object_t *TimerObject )
|
||||
{
|
||||
UTIL_TIMER_Object_t* cur = TimerListHead;
|
||||
|
||||
if( cur != NULL )
|
||||
{
|
||||
cur->IsPending = 0;
|
||||
}
|
||||
|
||||
TimerObject->Next = cur;
|
||||
TimerListHead = TimerObject;
|
||||
TimerSetTimeout( TimerListHead );
|
||||
}
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
292
Utilities/timer/stm32_timer.h
Normal file
292
Utilities/timer/stm32_timer.h
Normal file
@ -0,0 +1,292 @@
|
||||
/*!
|
||||
* \file timer.h
|
||||
*
|
||||
* \brief Timer objects and scheduling management implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
* ______ _
|
||||
* / _____) _ | |
|
||||
* ( (____ _____ ____ _| |_ _____ ____| |__
|
||||
* \____ \| ___ | (_ _) ___ |/ ___) _ \
|
||||
* _____) ) ____| | | || |_| ____( (___| | | |
|
||||
* (______/|_____)_|_|_| \__)_____)\____)_| |_|
|
||||
* (C)2013-2017 Semtech
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author Miguel Luis ( Semtech )
|
||||
*
|
||||
* \author Gregory Cristian ( Semtech )
|
||||
*/
|
||||
|
||||
/******************************************************************************
|
||||
* @file stm32_timer.h
|
||||
* @author MCD Application Team
|
||||
* @brief This is the header of the timer server driver
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2019 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef UTIL_TIME_SERVER_H__
|
||||
#define UTIL_TIME_SERVER_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** @defgroup TIMER_SERVER timer server
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <cmsis_compiler.h>
|
||||
#include "utilities_conf.h"
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
/** @defgroup TIMER_SERVER_exported_TypeDef TIMER_SERVER exported Typedef
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Timer mode
|
||||
*/
|
||||
typedef enum {
|
||||
UTIL_TIMER_ONESHOT = 0, /*!<One-shot timer. */
|
||||
UTIL_TIMER_PERIODIC = 1 /*!<Periodic timer. */
|
||||
} UTIL_TIMER_Mode_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Timer status
|
||||
*/
|
||||
typedef enum {
|
||||
UTIL_TIMER_OK = 0, /*!<Operation terminated successfully.*/
|
||||
UTIL_TIMER_INVALID_PARAM = 1, /*!<Invalid Parameter. */
|
||||
UTIL_TIMER_HW_ERROR = 2, /*!<Hardware Error. */
|
||||
UTIL_TIMER_UNKNOWN_ERROR = 3 /*!<Unknown Error. */
|
||||
} UTIL_TIMER_Status_t;
|
||||
|
||||
/**
|
||||
* @brief Timer object description
|
||||
*/
|
||||
typedef struct TimerEvent_s
|
||||
{
|
||||
uint32_t Timestamp; /*!<Expiring timer value in ticks from TimerContext */
|
||||
uint32_t ReloadValue; /*!<Reload Value when Timer is restarted */
|
||||
uint8_t IsPending; /*!<Is the timer waiting for an event */
|
||||
uint8_t IsRunning; /*!<Is the timer running */
|
||||
uint8_t IsReloadStopped; /*!<Is the reload stopped */
|
||||
UTIL_TIMER_Mode_t Mode; /*!<Timer type : one-shot/continuous */
|
||||
void ( *Callback )( void *); /*!<callback function */
|
||||
void *argument; /*!<callback argument */
|
||||
struct TimerEvent_s *Next; /*!<Pointer to the next Timer object. */
|
||||
} UTIL_TIMER_Object_t;
|
||||
|
||||
/**
|
||||
* @brief Timer driver definition
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
UTIL_TIMER_Status_t (* InitTimer )( void ); /*!< Initialisation of the low layer timer */
|
||||
UTIL_TIMER_Status_t (* DeInitTimer )( void ); /*!< Un-Initialisation of the low layer timer */
|
||||
|
||||
UTIL_TIMER_Status_t (* StartTimerEvt )( uint32_t timeout ); /*!< Start the low layer timer */
|
||||
UTIL_TIMER_Status_t (* StopTimerEvt )( void); /*!< Stop the low layer timer */
|
||||
|
||||
uint32_t (* SetTimerContext)( void ); /*!< Set the timer context */
|
||||
uint32_t (* GetTimerContext)( void ); /*!< Get the timer context */
|
||||
|
||||
uint32_t (* GetTimerElapsedTime)( void ); /*!< Get elapsed time */
|
||||
uint32_t (* GetTimerValue)( void ); /*!< Get timer value */
|
||||
uint32_t (* GetMinimumTimeout)( void ); /*!< Get Minimum timeout */
|
||||
|
||||
uint32_t (* ms2Tick)( uint32_t timeMicroSec ); /*!< convert ms to tick */
|
||||
uint32_t (* Tick2ms)( uint32_t tick ); /*!< convert tick into ms */
|
||||
} UTIL_TIMER_Driver_s;
|
||||
|
||||
/**
|
||||
* @brief Timer value on 32 bits
|
||||
*/
|
||||
typedef uint32_t UTIL_TIMER_Time_t;
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/* Exported variables ------------------------------------------------------------*/
|
||||
/** @defgroup TIMER_SERVER_exported_Variable TIMER_SERVER exported Variable
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @brief low layer interface to handle timing execution
|
||||
*
|
||||
* @remark This structure is defined and initialized in the specific platform
|
||||
* timer implementation
|
||||
*/
|
||||
extern const UTIL_TIMER_Driver_s UTIL_TimerDriver;
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
/* External variables --------------------------------------------------------*/
|
||||
/* Exported macros -----------------------------------------------------------*/
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
|
||||
/** @defgroup TIMER_SERVER_exported_function TIMER_SERVER exported function
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Initialize the timer server
|
||||
*
|
||||
* @retval Status based on @ref UTIL_TIMER_Status_t
|
||||
*/
|
||||
UTIL_TIMER_Status_t UTIL_TIMER_Init(void);
|
||||
|
||||
/**
|
||||
* @brief Un-Initialize the timer server
|
||||
*
|
||||
* @retval Status based on @ref UTIL_TIMER_Status_t
|
||||
*/
|
||||
UTIL_TIMER_Status_t UTIL_TIMER_DeInit(void);
|
||||
|
||||
/**
|
||||
* @brief Create the timer object
|
||||
*
|
||||
* @remark TimerSetValue function must be called before starting the timer.
|
||||
* this function initializes timestamp and reload value at 0.
|
||||
*
|
||||
* @param TimerObject Structure containing the timer object parameters
|
||||
* @param PeriodValue Period value of the timer in ms
|
||||
* @param Mode @ref UTIL_TIMER_Mode_t
|
||||
* @param Callback Function callback called at the end of the timeout
|
||||
* @param Argument argument for the callback function
|
||||
* @retval Status based on @ref UTIL_TIMER_Status_t
|
||||
*/
|
||||
UTIL_TIMER_Status_t UTIL_TIMER_Create( UTIL_TIMER_Object_t *TimerObject, uint32_t PeriodValue, UTIL_TIMER_Mode_t Mode, void ( *Callback )( void *) , void *Argument);
|
||||
|
||||
/**
|
||||
* @brief Start and adds the timer object to the list of timer events
|
||||
*
|
||||
* @param TimerObject Structure containing the timer object parameters
|
||||
* @retval Status based on @ref UTIL_TIMER_Status_t
|
||||
*/
|
||||
UTIL_TIMER_Status_t UTIL_TIMER_Start( UTIL_TIMER_Object_t *TimerObject );
|
||||
|
||||
/**
|
||||
* @brief Start and adds the timer object to the list of timer events
|
||||
*
|
||||
* @param TimerObject Structure containing the timer object parameters
|
||||
* @param PeriodValue period value of the timer
|
||||
* @retval Status based on @ref UTIL_TIMER_Status_t
|
||||
*/
|
||||
UTIL_TIMER_Status_t UTIL_TIMER_StartWithPeriod( UTIL_TIMER_Object_t *TimerObject, uint32_t PeriodValue);
|
||||
|
||||
/**
|
||||
* @brief Stop and removes the timer object from the list of timer events
|
||||
*
|
||||
* @param TimerObject Structure containing the timer object parameters
|
||||
* @retval Status based on @ref UTIL_TIMER_Status_t
|
||||
*/
|
||||
UTIL_TIMER_Status_t UTIL_TIMER_Stop( UTIL_TIMER_Object_t *TimerObject );
|
||||
|
||||
|
||||
/**
|
||||
* @brief update the period and start the timer
|
||||
*
|
||||
* @param TimerObject Structure containing the timer object parameters
|
||||
* @param NewPeriodValue new period value of the timer
|
||||
* @retval Status based on @ref UTIL_TIMER_Status_t
|
||||
*/
|
||||
UTIL_TIMER_Status_t UTIL_TIMER_SetPeriod(UTIL_TIMER_Object_t *TimerObject, uint32_t NewPeriodValue);
|
||||
|
||||
/**
|
||||
* @brief update the period and start the timer
|
||||
*
|
||||
* @param TimerObject Structure containing the timer object parameters
|
||||
* @param ReloadMode new reload mode @ref UTIL_TIMER_Mode_t
|
||||
* @retval Status based on @ref UTIL_TIMER_Status_t
|
||||
*/
|
||||
UTIL_TIMER_Status_t UTIL_TIMER_SetReloadMode(UTIL_TIMER_Object_t *TimerObject, UTIL_TIMER_Mode_t ReloadMode);
|
||||
|
||||
/**
|
||||
* @brief get the remaining time before timer expiration
|
||||
* *
|
||||
* @param TimerObject Structure containing the timer object parameters
|
||||
* @param Time time before expiration in ms
|
||||
* @retval Status based on @ref UTIL_TIMER_Status_t
|
||||
*/
|
||||
UTIL_TIMER_Status_t UTIL_TIMER_GetRemainingTime(UTIL_TIMER_Object_t *TimerObject, uint32_t *Time);
|
||||
|
||||
/**
|
||||
* @brief return timer state
|
||||
*
|
||||
* @param TimerObject Structure containing the timer object parameters
|
||||
* @retval boolean value is returned 0 = false and 1 = true
|
||||
*/
|
||||
uint32_t UTIL_TIMER_IsRunning( UTIL_TIMER_Object_t *TimerObject );
|
||||
|
||||
|
||||
/**
|
||||
* @brief return the remaining time of the first timer in the chain list
|
||||
*
|
||||
* @retval return the time in ms, the value 0xFFFFFFFF means no timer running
|
||||
*/
|
||||
uint32_t UTIL_TIMER_GetFirstRemainingTime(void);
|
||||
|
||||
/**
|
||||
* @brief return the current time
|
||||
*
|
||||
* @retval time value
|
||||
*/
|
||||
UTIL_TIMER_Time_t UTIL_TIMER_GetCurrentTime(void);
|
||||
|
||||
|
||||
/**
|
||||
* @brief return the elapsed time
|
||||
*
|
||||
* @param past a value returned by the function UTIL_TIMER_GetCurrentTime
|
||||
* @retval elasped time value
|
||||
*/
|
||||
UTIL_TIMER_Time_t UTIL_TIMER_GetElapsedTime(UTIL_TIMER_Time_t past );
|
||||
|
||||
/**
|
||||
* @brief Timer IRQ event handler
|
||||
*
|
||||
* @note Head Timer Object is automatically removed from the List
|
||||
*
|
||||
* @note e.g. it is not needed to stop it
|
||||
*/
|
||||
void UTIL_TIMER_IRQ_Handler( void );
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* UTIL_TIME_SERVER_H__*/
|
||||
|
||||
166
Utilities/timer/stm32_timer_if_template.c
Normal file
166
Utilities/timer/stm32_timer_if_template.c
Normal file
@ -0,0 +1,166 @@
|
||||
/*******************************************************************************
|
||||
* File Name : stm32_timer_if.c
|
||||
* Description : This file provides the ll driver for the time server
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2019 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include <math.h>
|
||||
#include "stm32_timer_if.h"
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
/* Exported variables ---------------------------------------------------------*/
|
||||
const UTIL_TIMER_Driver_s UTIL_TimerDriver =
|
||||
{
|
||||
PPP_Init,
|
||||
PPP_DeInit,
|
||||
|
||||
PPP_StartTimer,
|
||||
PPP_StopTimer,
|
||||
|
||||
PPP_SetTimerContext,
|
||||
PPP_GetTimerContext,
|
||||
|
||||
PPP_GetTimerElapsedTime,
|
||||
PPP_GetTimerValue,
|
||||
PPP_GetMinimumTimeout,
|
||||
|
||||
PPP_ms2Tick,
|
||||
PPP_Tick2ms,
|
||||
};
|
||||
|
||||
/*!
|
||||
* @brief Initializes the PPP timer
|
||||
* @note The timer is based on the PPP
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
UTIL_TIMER_Status_t PPP_Init( void )
|
||||
{
|
||||
return UTIL_TIMER_OK;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Initializes the PPP timer
|
||||
* @note The timer is based on the PPP
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
UTIL_TIMER_Status_t PPP_DeInit( void )
|
||||
{
|
||||
return UTIL_TIMER_OK;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Set the timer
|
||||
* @note The timer is set at now (read in this function) + timeout
|
||||
* @param timeout Duration of the Timer ticks
|
||||
*/
|
||||
UTIL_TIMER_Status_t PPP_StartTimer( uint32_t timeout )
|
||||
{
|
||||
return UTIL_TIMER_OK;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Stop the timer
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
UTIL_TIMER_Status_t PPP_StopTimer( void )
|
||||
{
|
||||
return UTIL_TIMER_OK;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief returns the wake up time in ticks
|
||||
* @param none
|
||||
* @retval wake up time in ticks
|
||||
*/
|
||||
uint32_t PPP_GetMinimumTimeout( void )
|
||||
{
|
||||
return ( 0 );
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief converts time in ms to time in ticks
|
||||
* @param [IN] time in milliseconds
|
||||
* @retval returns time in timer ticks
|
||||
*/
|
||||
uint32_t PPP_ms2Tick( uint32_t timeMicroSec )
|
||||
{
|
||||
return ( 0 );
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief converts time in ticks to time in ms
|
||||
* @param [IN] time in timer ticks
|
||||
* @retval returns time in milliseconds
|
||||
*/
|
||||
uint32_t PPP_Tick2ms( uint32_t tick )
|
||||
{
|
||||
return ( 0 );
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Get the PPP timer elapsed time since the last timer was set
|
||||
* @param none
|
||||
* @retval PPP Elapsed time in ticks
|
||||
*/
|
||||
uint32_t PPP_GetTimerElapsedTime( void )
|
||||
{
|
||||
return ( 0 );
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Get the PPP timer value
|
||||
* @param none
|
||||
* @retval PPP Timer value in ticks
|
||||
*/
|
||||
uint32_t PPP_GetTimerValue( void )
|
||||
{
|
||||
return ( 0 );
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief PPP IRQ Handler on the PPP timer
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void PPP_IrqHandler ( void )
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief set Time Reference set also the sDate and sTime
|
||||
* @param none
|
||||
* @retval Timer Value
|
||||
*/
|
||||
uint32_t PPP_SetTimerContext( void )
|
||||
{
|
||||
return ( 0 );
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Get the PPP timer Reference
|
||||
* @param none
|
||||
* @retval Timer Value in Ticks
|
||||
*/
|
||||
uint32_t PPP_GetTimerContext( void )
|
||||
{
|
||||
return ( 0 );
|
||||
}
|
||||
|
||||
120
Utilities/timer/stm32_timer_if_template.h
Normal file
120
Utilities/timer/stm32_timer_if_template.h
Normal file
@ -0,0 +1,120 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* File Name : stm32_timer_if.h
|
||||
* Description : This file provides the ll driver for the time server
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2019 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software is licensed under terms that can be found in the LICENSE file
|
||||
* in the root directory of this software component.
|
||||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef STM32_TIMER_IF_H__
|
||||
#define STM32_TIMER_IF_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "stm32_timer.h"
|
||||
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
/* External variables --------------------------------------------------------*/
|
||||
/* Exported macros -----------------------------------------------------------*/
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
|
||||
/*!
|
||||
* @brief Initialize the PPP timer
|
||||
* @note The timer is based on the PPP
|
||||
*/
|
||||
UTIL_TIMER_Status_t PPP_Init( void );
|
||||
|
||||
/*!
|
||||
* @brief Un-initialize the PPP timer
|
||||
* @note The timer is based on the PPP
|
||||
*/
|
||||
UTIL_TIMER_Status_t PPP_DeInit( void );
|
||||
|
||||
/*!
|
||||
* @brief Start the timer
|
||||
* @note The timer is set at Reference + timeout
|
||||
* @param timeout Duration of the Timer in ticks
|
||||
*/
|
||||
UTIL_TIMER_Status_t PPP_StartTimer( uint32_t timeout );
|
||||
|
||||
/*!
|
||||
* @brief Stop the timer
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
UTIL_TIMER_Status_t PPP_StopTimer( void );
|
||||
|
||||
/*!
|
||||
* @brief Return the minimum timeout the PPP is able to handle
|
||||
* @param none
|
||||
* @retval minimum value for a timeout
|
||||
*/
|
||||
uint32_t PPP_GetMinimumTimeout( void );
|
||||
|
||||
|
||||
/*!
|
||||
* @brief Get the PPP timer elapsed time since the last Reference was set
|
||||
* @retval PPP Elapsed time in ticks
|
||||
*/
|
||||
uint32_t PPP_GetTimerElapsedTime( void );
|
||||
|
||||
/*!
|
||||
* @brief Get the PPP timer value
|
||||
* @retval none
|
||||
*/
|
||||
uint32_t PPP_GetTimerValue( void );
|
||||
|
||||
/*!
|
||||
* @brief Set the PPP timer Reference
|
||||
* @retval Timer Reference Value in Ticks
|
||||
*/
|
||||
uint32_t PPP_SetTimerContext( void );
|
||||
|
||||
/*!
|
||||
* @brief Get the PPP timer Reference
|
||||
* @retval Timer Value in Ticks
|
||||
*/
|
||||
uint32_t PPP_GetTimerContext( void );
|
||||
/*!
|
||||
* @brief PPP IRQ Handler on the PPP timer
|
||||
* @param none
|
||||
* @retval none
|
||||
*/
|
||||
void PPP_IrqHandler ( void );
|
||||
|
||||
/*!
|
||||
* @brief converts time in ms to time in ticks
|
||||
* @param [IN] time in milliseconds
|
||||
* @retval returns time in ticks
|
||||
*/
|
||||
uint32_t PPP_ms2Tick( uint32_t timeMicroSec );
|
||||
|
||||
/*!
|
||||
* @brief converts time in ticks to time in ms
|
||||
* @param [IN] time in ticks
|
||||
* @retval returns time in milliseconds
|
||||
*/
|
||||
uint32_t PPP_Tick2ms( uint32_t tick );
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* STM32_TIMER_IF_H__ */
|
||||
|
||||
Reference in New Issue
Block a user