Files
MSE-SoftwEng/pico-sensor/McuLib/src/McuTimeout.c
2025-05-06 13:07:01 +00:00

284 lines
10 KiB
C

/* ###################################################################
** This component module is generated by Processor Expert. Do not modify it.
** Filename : McuTimeout.h
** Project : FRDM-K64F_Generator
** Processor : MK64FN1M0VLL12
** Component : Timeout
** Version : Component 01.038, Driver 01.00, CPU db: 3.00.000
** Compiler : GNU C Compiler
** Date/Time : 2020-08-14, 06:24, # CodeGen: 679
** Abstract :
**
The module implements timeout functionality. With this implementation,
it is possible to wait for a given time, and the time is counted by
a periodic interrupt.
** Settings :
** Component name : McuTimeout
** Maximum counters : 1
** Counter tick period (ms) : 10
** RTOS : Disabled
** Contents :
** GetCounter - McuTimeout_CounterHandle McuTimeout_GetCounter(McuTimeout_CounterType nofTicks);
** LeaveCounter - void McuTimeout_LeaveCounter(McuTimeout_CounterHandle handle);
** Value - McuTimeout_CounterType McuTimeout_Value(McuTimeout_CounterHandle handle);
** SetCounter - McuTimeout_CounterType McuTimeout_SetCounter(McuTimeout_CounterHandle handle,...
** CounterExpired - bool McuTimeout_CounterExpired(McuTimeout_CounterHandle handle);
** AddTick - void McuTimeout_AddTick(void);
** Init - void McuTimeout_Init(void);
**
** * Copyright (c) 2011-2020, Erich Styger
** * Web: https://mcuoneclipse.com
** * SourceForge: https://sourceforge.net/projects/mcuoneclipse
** * Git: https://github.com/ErichStyger/McuOnEclipse_PEx
** * All rights reserved.
** *
** * Redistribution and use in source and binary forms, with or without modification,
** * are permitted provided that the following conditions are met:
** *
** * - Redistributions of source code must retain the above copyright notice, this list
** * of conditions and the following disclaimer.
** *
** * - Redistributions in binary form must reproduce the above copyright notice, this
** * list of conditions and the following disclaimer in the documentation and/or
** * other materials provided with the distribution.
** *
** * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
** * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
** * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
** * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
** * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
** * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
** * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
** * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
** ###################################################################*/
/*!
** @file McuTimeout.h
** @version 01.00
** @brief
**
The module implements timeout functionality. With this implementation,
it is possible to wait for a given time, and the time is counted by
a periodic interrupt.
*/
/*!
** @addtogroup McuTimeout_module McuTimeout module documentation
** @{
*/
/* MODULE McuTimeout. */
#include "McuTimeout.h"
#define McuTimeout_NOF_COUNTERS 1 /* number of timeout counters available */
static McuTimeout_CounterType McuTimeout_Counters[McuTimeout_NOF_COUNTERS]; /* array of timeout counters */
static bool McuTimeout_FreeCounters[McuTimeout_NOF_COUNTERS]; /* array to indicate which counters are free */
/*
** ===================================================================
** Method : GetCounter (component Timeout)
**
** Description :
** Initializes a new timeout counter and returns the handle to
** it. At the end, use LeaveCounter() to free up the resource.
** Parameters :
** NAME - DESCRIPTION
** nofTicks - Number of ticks for the counter
** until it expires.
** Returns :
** --- - Handle to the counter, to be used for
** further API calls.
** ===================================================================
*/
McuTimeout_CounterHandle McuTimeout_GetCounter(McuTimeout_CounterType nofTicks)
{
McuTimeout_CounterHandle handle;
McuCriticalSection_CriticalVariable();
handle = 0;
if (nofTicks==0) {
nofTicks = 1; /* wait at least for one tick, otherwise will timeout immediately */
}
McuCriticalSection_EnterCritical();
while (handle<McuTimeout_NOF_COUNTERS && !McuTimeout_FreeCounters[handle]) {
handle++;
}
if (handle<McuTimeout_NOF_COUNTERS) {
McuTimeout_FreeCounters[handle]=FALSE;
McuTimeout_Counters[handle] = nofTicks;
}
McuCriticalSection_ExitCritical();
if (handle==McuTimeout_NOF_COUNTERS) {
return McuTimeout_OUT_OF_HANDLE;
}
return handle;
}
/*
** ===================================================================
** Method : LeaveCounter (component Timeout)
**
** Description :
** To be called to return the counter. Note that a counter
** always should be returned so it can be reused.
** Parameters :
** NAME - DESCRIPTION
** handle - Counter handle
** Returns : Nothing
** ===================================================================
*/
void McuTimeout_LeaveCounter(McuTimeout_CounterHandle handle)
{
McuCriticalSection_CriticalVariable();
if (handle==McuTimeout_OUT_OF_HANDLE) {
return;
}
McuCriticalSection_EnterCritical();
McuTimeout_Counters[handle] = 0;
McuTimeout_FreeCounters[handle]=TRUE;
McuCriticalSection_ExitCritical();
}
/*
** ===================================================================
** Method : CounterExpired (component Timeout)
**
** Description :
** Returns true if the timeout counter has been expired
** Parameters :
** NAME - DESCRIPTION
** handle - The timeout handle retrieved using
** GetCounter()
** Returns :
** --- - Returns TRUE if the counter has been
** expired, FALSE otherwise
** ===================================================================
*/
bool McuTimeout_CounterExpired(McuTimeout_CounterHandle handle)
{
bool res;
McuCriticalSection_CriticalVariable();
if (handle==McuTimeout_OUT_OF_HANDLE) {
return TRUE;
}
McuCriticalSection_EnterCritical();
res = (bool)(McuTimeout_Counters[handle]==0);
McuCriticalSection_ExitCritical();
return res;
}
/*
** ===================================================================
** Method : AddTick (component Timeout)
**
** Description :
** Method to be called from a periodic timer or interrupt. It
** will decrement all current counters by one down to zero.
** Parameters : None
** Returns : Nothing
** ===================================================================
*/
void McuTimeout_AddTick(void)
{
uint8_t i;
McuCriticalSection_CriticalVariable();
McuCriticalSection_EnterCritical();
for(i=0;i<McuTimeout_NOF_COUNTERS;i++) {
if (McuTimeout_Counters[i]>0) {
McuTimeout_Counters[i]--;
}
}
McuCriticalSection_ExitCritical();
}
/*
** ===================================================================
** Method : Init (component Timeout)
**
** Description :
** Initialization of the driver
** Parameters : None
** Returns : Nothing
** ===================================================================
*/
void McuTimeout_Init(void)
{
uint8_t i;
for(i=0;i<McuTimeout_NOF_COUNTERS;i++) {
McuTimeout_Counters[i] = 0;
McuTimeout_FreeCounters[i] = TRUE;
}
}
/*
** ===================================================================
** Method : Value (component Timeout)
**
** Description :
** Return the current value of the counter (in ticks)
** Parameters :
** NAME - DESCRIPTION
** handle - Handle of the timeout counter
** Returns :
** --- - Returns the value of the timeout counter.
** ===================================================================
*/
McuTimeout_CounterType McuTimeout_Value(McuTimeout_CounterHandle handle)
{
McuTimeout_CounterType val;
McuCriticalSection_CriticalVariable();
if (handle==McuTimeout_OUT_OF_HANDLE) {
return 0; /* return dummy value */
}
McuCriticalSection_EnterCritical();
val = McuTimeout_Counters[handle];
McuCriticalSection_ExitCritical();
return val;
}
/*
** ===================================================================
** Method : SetCounter (component Timeout)
**
** Description :
** Sets the counter to a new value and returns the value just
** prior to the call.
** Parameters :
** NAME - DESCRIPTION
** handle - Counter handle which shall get a new
** value.
** nofTicks - New value (tick count) of the
** timeout counter. Pass zero to have it
** expire immediately.
** Returns :
** --- - Value of counter before reset.
** ===================================================================
*/
McuTimeout_CounterType McuTimeout_SetCounter(McuTimeout_CounterHandle handle, McuTimeout_CounterType nofTicks)
{
McuTimeout_CounterType res;
McuCriticalSection_CriticalVariable();
if (handle==McuTimeout_OUT_OF_HANDLE) {
return 0; /* return dummy value */
}
McuCriticalSection_EnterCritical();
res = McuTimeout_Counters[handle];
McuTimeout_Counters[handle] = nofTicks;
McuCriticalSection_ExitCritical();
return res;
}
/* END McuTimeout. */
/*!
** @}
*/