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

PIC12FPIC16(L)FPIC18F(L)F
AVR DxAVR ExATtiny

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.

Version: Timeout Driver Version 1.1.0

Note: Enable interrupts for the dependent timers for this driver to work.

Data structures
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.

  • INLINE void printList (void)

    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.

Parameters:
Timer

- Timer instance whose callback have to be called

Returns:

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.

Parameters:
Duration

- Number of ticks of a specific timer

Returns:

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.

Parameters:
None.
Returns:

None.

rebaseList()

uint32_t rebaseList (void )[inline]

Adjusts the time base so that the timer can never wrap.

Parameters:
None.
Returns:

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.

Parameters:
Duration

- Number of timer ticks

Returns:

None.

sortedInsert()

bool sortedInsert (timerStruct_t * timer)

Shows where the specific timer will be placed on the queue.

Parameters:
Timer

- Instance of a timer added to the queue

Return values:
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.

Parameters:
None.
Returns:

None.

stopTimeouts()

void stopTimeouts (void )

Disables all the timers without deleting them. Timers can be restarted by calling starTimerAtHead.

Parameters:
None.
Returns:

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.

Parameters:
None.
Returns:

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.

Parameters:
Timer

- Timer instance to be created and added to the list

Time-out

- Number of timer ticks before this timer expires

Returns:

None.

timeout_delete()

void timeout_delete (timerStruct_t * timer)

Cancels and removes a running timer.

Parameters:
Timer

- Timer instance to be removed

Returns:

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.

Parameters:
List

- Pointer to the timer instance at the top of the queue

Timer

- Timer instance to be removed

Return values:
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.

Parameters:
None.
Returns:

None.

timeout_getTimeRemaining()

uint32_t timeout_getTimeRemaining (timerStruct_t * timer)

Gets the number of ticks remaining before a specific timer runs out.

Parameters:
Timer

- The specific timer

Returns:

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.

Parameters:
None.
Return values:
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.

Parameters:
None.
Return values:
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.

Parameters:
Timer

- Timer instance whose callback have to be called

Returns:

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.

Parameters:
None.
Returns:

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.

Parameters:
Timer

- timer instance that will be used in stopwatch mode.

Returns:

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.

Parameters:
Timer

- Timer instance used in Stopwatch mode

Returns:

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.2 Member Data Documentation

The documentation for this struct was generated from the following file:

source/

timeout.h

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/

timeout.h

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.

  • INLINE void printList (void)

    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.3 Detailed Description

Contains the driver code for Time-out module.

TIMEOUTDRIVER Generated Driver File

Version: Timeout Driver Version 1.1.0

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.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.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