2.8 Timeout Driver
2.8.1 Introduction
The MPLAB® Code Configurator (MCC) Melody Timeout Driver generates APIs to support timeout functionality on all target MCUs.
The Timeout Driver aims to provide an abstraction interface to Timer-type peripherals such as the Timer0/1/2 peripherals and their derivatives; the Signal Measurement Timer (SMT); and the Hardware Limit Timer (HLT). On top of this hardware abstraction, the application side interface allows users to create applications that need a timeout event after a specified time. The driver can register multiple timers with different callbacks that trigger different events upon their corresponding timer expiration. There are also options to run timers just once or repeatedly to provide users the flexibility of using the timer peripherals as one-off countdown timers or periodic timers.
2.8.2 Supported Device Families
PIC12F | PIC16(L)F | PIC18F(L)F |
AVR Dx | AVR Ex | ATtiny |
2.8.3 Required header files:
#include "mcc_generated_files/timer/timeout.h"
2.8.4 Module Documentation
2.8.4.1 Timeout Driver
This file contains the API prototypes and other data types for the Timeout module.
2.8.4.1.1 Module description
This file contains the API prototypes and other data types for the Timeout module.
Note: Enable interrupts for the dependent timers for this driver to work.
Data structures
struct tmrStruct_t
Defines a timer instance.
Functions
void stopTimeouts (void)
Disables all the timers without deleting them. Timers can be restarted by calling starTimerAtHead.
INLINE void setTimerDuration (uint32_t duration)
Sets the number of ticks the timer will run for.
INLINE uint32_t makeAbsolute (uint32_t timeout)
Returns the total number of timer ticks since the last time-out occurred or since the module was started.
INLINE uint32_t rebaseList (void)
Adjusts the time base so that the timer can never wrap.
-
Prints the times for each of the timers on the queue.
bool sortedInsert (timerStruct_t *timer)
Shows where the specific timer will be placed on the queue.
void startTimerAtHead (void)
Starts the timer queue from the beginning.
bool timeout_deleteHelper (timerStruct_t *volatile *list, timerStruct_t *timer)
Looks for a specific timer instance and removes it from the queue.
INLINE void enqueueCallback (timerStruct_t *timer)
Queues the timers that need their callback called with callNextCallback.
void timeout_initialize (void)
Sets the time-out Interrupt Service Routine (ISR) handler as the interrupt handler for the timer.
void timeout_create (timerStruct_t *timer, uint32_t timeout)
Queues and starts the timer. The running timer is cancelled and a new timer will start.
void timeout_delete (timerStruct_t *timer)
Cancels and removes a running timer.
void timeout_flushAll (void)
Cancels and removes all the timers in the queue.
bool timeout_hasPendingTimeouts (void)
Checks if there are any timers running or waiting to be called.
bool timeout_hasPendingCallbacks (void)
Checks if there are events being executed or waiting to be executed.
INLINE void timeout_callNextCallback (void)
Checks the list of expired timers and calls the first one.
void timeout_isr (void)
Handler function called whenever the timer peripheral interrupts. It counts the time elapsed and calls the next timer in the queue. It assumesthat the callback is complete before the next timer tick.
void timeout_startTimer (timerStruct_t *timer)
Starts a timer with maximum time-out which can be up to the maximum range of the timer divided by two only in Stopwatch/Cycle Counter mode.
uint32_t timeout_stopTimer (timerStruct_t *timer)
Stops the running timer and returns the number of ticks it counted, but only in Stopwatch/Cycle Counter mode.
uint32_t timeout_getTimeRemaining (timerStruct_t *timer)
Gets the number of ticks remaining before a specific timer runs out.
2.8.4.1.2 Function Documentation
enqueueCallback()
void enqueueCallback (timerStruct_t * timer)[inline]
Queues the timers that need their callback called with callNextCallback.
Timer |
- Timer instance whose callback have to be called |
None. |
makeAbsolute()
uint32_t makeAbsolute (uint32_t timeout)[inline]
Returns the total number of timer ticks since the last time-out occurred or since the module was started.
Duration |
- Number of ticks of a specific timer |
Time-out - Number of timer ticks since the last time-out occurred |
printList()
void printList (void )[inline]
Prints the times for each of the timers on the queue.
None. |
None. |
rebaseList()
uint32_t rebaseList (void )[inline]
Adjusts the time base so that the timer can never wrap.
None. |
Number of timer ticks since the last time-out occurred |
setTimerDuration()
void setTimerDuration (uint32_t duration)[inline]
Sets the number of ticks the timer will run for.
Duration |
- Number of timer ticks |
None. |
sortedInsert()
bool sortedInsert (timerStruct_t * timer)
Shows where the specific timer will be placed on the queue.
Timer |
- Instance of a timer added to the queue |
True |
- If inserted at the top of the queue |
False |
- Not inserted at the top of the queue |
startTimerAtHead()
void startTimerAtHead (void )
Starts the timer queue from the beginning.
None. |
None. |
stopTimeouts()
void stopTimeouts (void )
Disables all the timers without deleting them. Timers can be restarted by calling starTimerAtHead.
None. |
None. |
timeout_callNextCallback()
INLINE void timeout_callNextCallback (void )[inline]
Checks the list of expired timers and calls the first one.
If the callback returned a value greater than 0, it reschedules the timer. It is recommended this is called from the main superloop (while(1)) in the code but, by design, it can also be called from the timer ISR as the last action in timeout_isr.
None. |
None. |
timeout_create()
void timeout_create (timerStruct_t * timer, uint32_t timeout)
Queues and starts the timer. The running timer is cancelled and a new timer will start.
Timer |
- Timer instance to be created and added to the list |
Time-out |
- Number of timer ticks before this timer expires |
None. |
timeout_delete()
void timeout_delete (timerStruct_t * timer)
Cancels and removes a running timer.
Timer |
- Timer instance to be removed |
None. |
timeout_deleteHelper()
bool timeout_deleteHelper (timerStruct_t *volatile * list, timerStruct_t * timer)
Looks for a specific timer instance and removes it from the queue.
List |
- Pointer to the timer instance at the top of the queue |
Timer |
- Timer instance to be removed |
True |
- If the timer is found and successfully removed from the list |
False |
- The operation was not successful |
timeout_flushAll()
void timeout_flushAll (void )
Cancels and removes all the timers in the queue.
None. |
None. |
timeout_getTimeRemaining()
uint32_t timeout_getTimeRemaining (timerStruct_t * timer)
Gets the number of ticks remaining before a specific timer runs out.
Timer |
- The specific timer |
Number of ticks remaining before a timer runs out |
timeout_hasPendingCallbacks()
bool timeout_hasPendingCallbacks (void )
Checks if there are events being executed or waiting to be executed.
None. |
True |
- There are no more events lined up |
False |
- Events are running or waiting to be called |
timeout_hasPendingTimeouts()
bool timeout_hasPendingTimeouts (void )
Checks if there are any timers running or waiting to be called.
None. |
True |
- There are no more events lined up |
False |
- Events are running or waiting to be called |
timeout_initialize()
void timeout_initialize (void )
Sets the time-out Interrupt Service Routine (ISR) handler as the interrupt handler for the timer.
Timer |
- Timer instance whose callback have to be called |
None. |
timeout_isr()
void timeout_isr (void )
Handler function called whenever the timer peripheral interrupts. It counts the time elapsed and calls the next timer in the queue. It assumesthat the callback is complete before the next timer tick.
None. |
None. |
timeout_startTimer()
void timeout_startTimer (timerStruct_t * timer)
Starts a timer with maximum time-out which can be up to the maximum range of the timer divided by two only in Stopwatch/Cycle Counter mode.
Timer |
- timer instance that will be used in stopwatch mode. |
None. |
timeout_stopTimer()
uint32_t timeout_stopTimer (timerStruct_t * timer)
Stops the running timer and returns the number of ticks it counted, but only in Stopwatch/Cycle Counter mode.
Timer |
- Timer instance used in Stopwatch mode |
Number of ticks counted by the timer |
2.8.5 Class Documentation
2.8.5.1 tmrStruct Struct Reference
#include <timeout.h>
2.8.5.1.1 Public Attributes
void * payload
uint32_t absoluteTime
2.8.5.1.2 Member Data Documentation
absoluteTime
uint32_t absoluteTime
The number of ticks the timer will count down.
callbackPtr
timercallback_ptr_t callbackPtr
Pointer to the callback handler.
next
struct tmrStruct* next
Pointer to the next timer on the queue.
payload
void* payload
The value that can be passed on to the callback.
2.8.5.2 tmrStruct_t Struct Reference
Defines a timer instance.
2.8.5.2.1 Detailed Description
Defines a timer instance.
#include <timeout.h>
The documentation for this struct was generated from the following file:
source/
2.8.6 File Documentation
2.8.6.1 source/timeout.c File Reference
Contains the driver code for Time-out module.
#include <stdio.h> #include "../timeout.h" #include "../tmr0.h"
2.8.6.1.1 Functions
uint32_t dummyHandler (void *payload)
void startTimerAtHead (void)
Starts the timer queue from the beginning.
INLINE void enqueueCallback (timerStruct_t *timer)
Queues the timers that need their callback called with callNextCallback.
INLINE void setTimerDuration (uint32_t duration)
Sets the number of ticks the timer will run for.
INLINE uint32_t makeAbsolute (uint32_t timeout)
Returns the total number of timer ticks since the last time-out occurred or since the module was started.
INLINE uint32_t rebaseList (void)
Adjusts the time base so that the timer can never wrap.
-
Prints the times for each of the timers on the queue.
bool timeout_hasPendingTimeouts (void)
Checks if there are any timers running or waiting to be called.
bool timeout_hasPendingCallbacks (void)
Checks if there are events being executed or waiting to be executed.
void stopTimeouts (void)
Disables all the timers without deleting them. Timers can be restarted by calling starTimerAtHead.
uint32_t timeout_getTimeRemaining (timerStruct_t *timer)
Gets the number of ticks remaining before a specific timer runs out.
bool sortedInsert (timerStruct_t *timer)
Shows where the specific timer will be placed on the queue.
void timeout_flushAll (void)
Cancels and removes all the timers in the queue.
bool timeout_deleteHelper (timerStruct_t *volatile *list, timerStruct_t *timer)
Looks for a specific timer instance and removes it from the queue.
void timeout_delete (timerStruct_t *timer)
Cancels and removes a running timer.
void timeout_callNextCallback (void)
Checks the list of expired timers and calls the first one.
void timeout_initialize (void)
Sets the time-out Interrupt Service Routine (ISR) handler as the interrupt handler for the timer.
void timeout_create (timerStruct_t *timer, uint32_t timeout)
Queues and starts the timer. The running timer is cancelled and a new timer will start.
void timeout_isr (void)
Handler function called whenever the timer peripheral interrupts. It counts the time elapsed and calls the next timer in the queue. It assumesthat the callback is complete before the next timer tick.
void timeout_startTimer (timerStruct_t *timer)
Starts a timer with maximum time-out which can be up to the maximum range of the timer divided by two only in Stopwatch/Cycle Counter mode.
uint32_t timeout_stopTimer (timerStruct_t *timer)
Stops the running timer and returns the number of ticks it counted, but only in Stopwatch/Cycle Counter mode.
2.8.6.1.2 Variables
timerStruct_t * listHead = NULL
timerStruct_t *volatile executeQueueHead = NULL
volatile uint32_t absoluteTimeofLastTimeout = 0
volatile uint32_t lastTimerLoad = 0
volatile bool isRunning = false
2.8.6.1.3 Detailed Description
Contains the driver code for Time-out module.
TIMEOUTDRIVER Generated Driver File
2.8.6.1.4 Function Documentation
dummyHandler()
uint32_t dummyHandler (void * payload)
2.8.6.1.5 Variable Documentation
absoluteTimeofLastTimeout
volatile uint32_t absoluteTimeofLastTimeout = 0
dummy
timerStruct_t dummy
dummy_exec
timerStruct_t dummy_exec = {dummyHandler}
executeQueueHead
timerStruct_t* volatile executeQueueHead = NULL
isRunning
volatile bool isRunning = false
lastTimerLoad
volatile uint32_t lastTimerLoad = 0
listHead
timerStruct_t* listHead = NULL
2.8.6.2 source/timeout.h File Reference
#include <stdint.h> #include <stdbool.h> #include <stddef.h>
2.8.6.2.1 Data structures
struct tmrStruct
2.8.6.2.2 Functions
void timeout_initialize (void)
Sets the time-out Interrupt Service Routine (ISR) handler as the interrupt handler for the timer.
void timeout_create (timerStruct_t *timer, uint32_t timeout)
Queues and starts the timer. The running timer is cancelled and a new timer will start.
void timeout_delete (timerStruct_t *timer)
Cancels and removes a running timer.
void timeout_flushAll (void)
Cancels and removes all the timers in the queue.
bool timeout_hasPendingTimeouts (void)
Checks if there are any timers running or waiting to be called.
bool timeout_hasPendingCallbacks (void)
Checks if there are events being executed or waiting to be executed.
INLINE void timeout_callNextCallback (void)
Checks the list of expired timers and calls the first one.
void timeout_isr (void)
Handler function called whenever the timer peripheral interrupts. It counts the time elapsed and calls the next timer in the queue. It assumesthat the callback is complete before the next timer tick.
void timeout_startTimer (timerStruct_t *timer)
Starts a timer with maximum time-out which can be up to the maximum range of the timer divided by two only in Stopwatch/Cycle Counter mode.
uint32_t timeout_stopTimer (timerStruct_t *timer)
Stops the running timer and returns the number of ticks it counted, but only in Stopwatch/Cycle Counter mode.
uint32_t timeout_getTimeRemaining (timerStruct_t *timer)
Gets the number of ticks remaining before a specific timer runs out.
2.8.6.2.3 Macros
#define INLINE inline
#define timeout_mSecToTicks(a) ( ((uint32_t)(a)) * 32UL )
#define timeout_ticksToMsec(a) ( ((uint32_t)(a)) / 32UL )
2.8.6.2.4 Typedefs
typedef uint32_t(* timercallback_ptr_t) (void *payload)
typedef struct tmrStruct timerStruct_t
2.8.6.2.5 Detailed Description
TIMEOUTDRIVER Generated Driver API Header File
2.8.6.2.6 Macro Definition Documentation
INLINE
#define INLINE inline
timeout_mSecToTicks
#define timeout_mSecToTicks( a) ( ((uint32_t)(a)) * 32UL )
timeout_ticksToMsec
#define timeout_ticksToMsec( a) ( ((uint32_t)(a)) / 32UL )
2.8.6.2.7 Typedef Documentation
timercallback_ptr_t
typedef uint32_t(* timercallback_ptr_t) (void *payload)
timerStruct_t
typedef struct tmrStruct timerStruct_t