ChibiOS/HAL 9.0.0
GPT Driver

Generic GPT Driver. More...

Detailed Description

Generic GPT Driver.

This module implements a generic GPT (General Purpose Timer) driver. The timer can be programmed in order to trigger callbacks after a specified time period or continuously with a specified interval.

Precondition
In order to use the GPT driver the HAL_USE_GPT option must be enabled in halconf.h.

Driver State Machine

The driver implements a state machine internally, not all the driver functionalities can be used in any moment, any transition not explicitly shown in the following diagram has to be considered an error and shall be captured by an assertion (if enabled).

dot_inline_dotgraph_4.png

GPT Operations.

This driver abstracts a generic timer composed of:

  • A clock prescaler.
  • A main up counter.
  • A comparator register that resets the main counter to zero when the limit is reached. A callback is invoked when this happens.

The timer can operate in three different modes:

  • Continuous Mode, a periodic callback is invoked until the driver is explicitly stopped.
  • One Shot Mode, a callback is invoked after the programmed period and then the timer automatically stops.
  • Delay Mode, the timer is used for inserting a brief delay into the execution flow, no callback is invoked in this mode.
Collaboration diagram for GPT Driver:

PLATFORM configuration options

#define PLATFORM_GPT_USE_GPT1   FALSE
 GPTD1 driver enable switch.

Data Structures

struct  GPTConfig
 Driver configuration structure. More...
struct  GPTDriver
 Structure representing a GPT driver. More...

Macros

#define gptChangeIntervalI(gptp, interval)
 Changes the interval of GPT peripheral.
#define gptGetIntervalX(gptp)
 Returns the interval of GPT peripheral.
#define gptGetCounterX(gptp)
 Returns the counter value of GPT peripheral.
#define _gpt_isr_invoke_cb(gptp)
 Common ISR code, GPT period event.
#define gpt_lld_change_interval(gptp, interval)
 Changes the interval of GPT peripheral.

Typedefs

typedef struct GPTDriver GPTDriver
 Type of a structure representing a GPT driver.
typedef void(* gptcallback_t) (GPTDriver *gptp)
 GPT notification callback type.
typedef uint32_t gptfreq_t
 GPT frequency type.
typedef uint16_t gptcnt_t
 GPT counter type.

Enumerations

enum  gptstate_t {
  GPT_UNINIT = 0 , GPT_STOP = 1 , GPT_READY = 2 , GPT_CONTINUOUS = 3 ,
  GPT_ONESHOT = 4
}
 Driver state machine possible states. More...

Functions

void gptInit (void)
 GPT Driver initialization.
void gptObjectInit (GPTDriver *gptp)
 Initializes the standard part of a GPTDriver structure.
msg_t gptStart (GPTDriver *gptp, const GPTConfig *config)
 Configures and activates the GPT peripheral.
void gptStop (GPTDriver *gptp)
 Deactivates the GPT peripheral.
void gptChangeInterval (GPTDriver *gptp, gptcnt_t interval)
 Changes the interval of GPT peripheral.
void gptStartContinuous (GPTDriver *gptp, gptcnt_t interval)
 Starts the timer in continuous mode.
void gptStartContinuousI (GPTDriver *gptp, gptcnt_t interval)
 Starts the timer in continuous mode.
void gptStartOneShot (GPTDriver *gptp, gptcnt_t interval)
 Starts the timer in one shot mode.
void gptStartOneShotI (GPTDriver *gptp, gptcnt_t interval)
 Starts the timer in one shot mode.
void gptStopTimer (GPTDriver *gptp)
 Stops the timer.
void gptStopTimerI (GPTDriver *gptp)
 Stops the timer.
void gptPolledDelay (GPTDriver *gptp, gptcnt_t interval)
 Starts the timer in one shot mode and waits for completion.
void gpt_lld_init (void)
 Low level GPT driver initialization.
void gpt_lld_start (GPTDriver *gptp)
 Configures and activates the GPT peripheral.
void gpt_lld_stop (GPTDriver *gptp)
 Deactivates the GPT peripheral.
void gpt_lld_start_timer (GPTDriver *gptp, gptcnt_t interval)
 Starts the timer in continuous mode.
void gpt_lld_stop_timer (GPTDriver *gptp)
 Stops the timer.
void gpt_lld_polled_delay (GPTDriver *gptp, gptcnt_t interval)
 Starts the timer in one shot mode and waits for completion.

Variables

GPTDriver GPTD1
 GPTD1 driver identifier.

Macro Definition Documentation

◆ gptChangeIntervalI

#define gptChangeIntervalI ( gptp,
interval )
Value:
{ \
gpt_lld_change_interval(gptp, interval); \
}

Changes the interval of GPT peripheral.

This function changes the interval of a running GPT unit.

Precondition
The GPT unit must be running in continuous mode.
Postcondition
The GPT unit interval is changed to the new value.
Parameters
[in]gptppointer to a GPTDriver object
[in]intervalnew cycle time in timer ticks
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 86 of file hal_gpt.h.

Referenced by gptChangeInterval().

◆ gptGetIntervalX

#define gptGetIntervalX ( gptp)
Value:
gpt_lld_get_interval(gptp)

Returns the interval of GPT peripheral.

Precondition
The GPT unit must be running in continuous mode.
Parameters
[in]gptppointer to a GPTDriver object
Returns
The current interval.
Function Class:
This is an X-Class API, this function can be invoked from any context.

Definition at line 99 of file hal_gpt.h.

◆ gptGetCounterX

#define gptGetCounterX ( gptp)
Value:
gpt_lld_get_counter(gptp)

Returns the counter value of GPT peripheral.

Precondition
The GPT unit must be running in continuous mode.
Note
The nature of the counter is not defined, it may count upward or downward, it could be continuously running or not.
Parameters
[in]gptppointer to a GPTDriver object
Returns
The current counter value.
Function Class:
This is an X-Class API, this function can be invoked from any context.

Definition at line 112 of file hal_gpt.h.

◆ _gpt_isr_invoke_cb

#define _gpt_isr_invoke_cb ( gptp)
Value:
do { \
if ((gptp)->state == GPT_ONESHOT) { \
(gptp)->state = GPT_READY; \
gpt_lld_stop_timer(gptp); \
} \
if ((gptp)->config->callback != NULL) { \
(gptp)->config->callback(gptp); \
} \
} while (0)
@ GPT_READY
Definition hal_gpt.h:52
@ GPT_ONESHOT
Definition hal_gpt.h:54

Common ISR code, GPT period event.

Parameters
[in]gptppointer to the GPTDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 121 of file hal_gpt.h.

◆ PLATFORM_GPT_USE_GPT1

#define PLATFORM_GPT_USE_GPT1   FALSE

GPTD1 driver enable switch.

If set to TRUE the support for GPTD1 is included.

Note
The default is FALSE.

Definition at line 48 of file hal_gpt_lld.h.

◆ gpt_lld_change_interval

#define gpt_lld_change_interval ( gptp,
interval )
Value:
{ \
(void)gptp; \
(void)interval; \
}

Changes the interval of GPT peripheral.

This function changes the interval of a running GPT unit.

Precondition
The GPT unit must have been activated using gptStart().
The GPT unit must have been running in continuous mode using gptStartContinuous().
Postcondition
The GPT unit interval is changed to the new value.
Note
The function has effect at the next cycle start.
Parameters
[in]gptppointer to a GPTDriver object
[in]intervalnew cycle time in timer ticks
Function Class:
Not an API, this function is for internal use only.

Definition at line 124 of file hal_gpt_lld.h.

Typedef Documentation

◆ GPTDriver

typedef struct GPTDriver GPTDriver

Type of a structure representing a GPT driver.

Definition at line 60 of file hal_gpt.h.

◆ gptcallback_t

typedef void(* gptcallback_t) (GPTDriver *gptp)

GPT notification callback type.

Parameters
[in]gptppointer to a GPTDriver object

Definition at line 67 of file hal_gpt.h.

◆ gptfreq_t

typedef uint32_t gptfreq_t

GPT frequency type.

Definition at line 63 of file hal_gpt_lld.h.

◆ gptcnt_t

typedef uint16_t gptcnt_t

GPT counter type.

Definition at line 68 of file hal_gpt_lld.h.

Enumeration Type Documentation

◆ gptstate_t

enum gptstate_t

Driver state machine possible states.

Enumerator
GPT_UNINIT 

Not initialized.

GPT_STOP 

Stopped.

GPT_READY 

Ready.

GPT_CONTINUOUS 

Active in continuous mode.

GPT_ONESHOT 

Active in one shot mode.

Definition at line 49 of file hal_gpt.h.

Function Documentation

◆ gptInit()

void gptInit ( void )

GPT Driver initialization.

Note
This function is implicitly invoked by halInit(), there is no need to explicitly initialize the driver.
Function Class:
Object or module nitializer function.

Definition at line 56 of file hal_gpt.c.

References gpt_lld_init().

Referenced by halInit().

Here is the call graph for this function:

◆ gptObjectInit()

void gptObjectInit ( GPTDriver * gptp)

Initializes the standard part of a GPTDriver structure.

Parameters
[out]gptppointer to the GPTDriver object
Function Class:
Object or module nitializer function.

Definition at line 68 of file hal_gpt.c.

References GPTDriver::config, GPT_STOP, and GPTDriver::state.

Referenced by gpt_lld_init().

◆ gptStart()

msg_t gptStart ( GPTDriver * gptp,
const GPTConfig * config )

Configures and activates the GPT peripheral.

Parameters
[in]gptppointer to the GPTDriver object
[in]configpointer to the GPTConfig object
Returns
The operation status.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 83 of file hal_gpt.c.

References GPTDriver::config, gpt_lld_start(), GPT_READY, GPT_STOP, HAL_RET_SUCCESS, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), and GPTDriver::state.

Here is the call graph for this function:

◆ gptStop()

void gptStop ( GPTDriver * gptp)

Deactivates the GPT peripheral.

Parameters
[in]gptppointer to the GPTDriver object
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 121 of file hal_gpt.c.

References GPTDriver::config, gpt_lld_stop(), GPT_READY, GPT_STOP, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), and GPTDriver::state.

Here is the call graph for this function:

◆ gptChangeInterval()

void gptChangeInterval ( GPTDriver * gptp,
gptcnt_t interval )

Changes the interval of GPT peripheral.

This function changes the interval of a running GPT unit.

Precondition
The GPT unit must be running in continuous mode.
Postcondition
The GPT unit interval is changed to the new value.
Parameters
[in]gptppointer to a GPTDriver object
[in]intervalnew cycle time in timer ticks
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 148 of file hal_gpt.c.

References GPT_CONTINUOUS, gptChangeIntervalI, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), and GPTDriver::state.

Here is the call graph for this function:

◆ gptStartContinuous()

void gptStartContinuous ( GPTDriver * gptp,
gptcnt_t interval )

Starts the timer in continuous mode.

Parameters
[in]gptppointer to the GPTDriver object
[in]intervalperiod in ticks
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 167 of file hal_gpt.c.

References gptStartContinuousI(), osalSysLock(), and osalSysUnlock().

Here is the call graph for this function:

◆ gptStartContinuousI()

void gptStartContinuousI ( GPTDriver * gptp,
gptcnt_t interval )

Starts the timer in continuous mode.

Parameters
[in]gptppointer to the GPTDriver object
[in]intervalperiod in ticks
Function Class:
This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 182 of file hal_gpt.c.

References GPT_CONTINUOUS, gpt_lld_start_timer(), GPT_READY, osalDbgAssert, osalDbgCheck, osalDbgCheckClassI, and GPTDriver::state.

Referenced by gptStartContinuous().

Here is the call graph for this function:

◆ gptStartOneShot()

void gptStartOneShot ( GPTDriver * gptp,
gptcnt_t interval )

Starts the timer in one shot mode.

Parameters
[in]gptppointer to the GPTDriver object
[in]intervaltime interval in ticks
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 201 of file hal_gpt.c.

References gptStartOneShotI(), osalSysLock(), and osalSysUnlock().

Here is the call graph for this function:

◆ gptStartOneShotI()

void gptStartOneShotI ( GPTDriver * gptp,
gptcnt_t interval )

Starts the timer in one shot mode.

Parameters
[in]gptppointer to the GPTDriver object
[in]intervaltime interval in ticks
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 216 of file hal_gpt.c.

References GPTConfig::callback, GPTDriver::config, gpt_lld_start_timer(), GPT_ONESHOT, GPT_READY, osalDbgAssert, osalDbgCheck, osalDbgCheckClassI, and GPTDriver::state.

Referenced by gptStartOneShot().

Here is the call graph for this function:

◆ gptStopTimer()

void gptStopTimer ( GPTDriver * gptp)

Stops the timer.

Parameters
[in]gptppointer to the GPTDriver object
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 235 of file hal_gpt.c.

References gptStopTimerI(), osalSysLock(), and osalSysUnlock().

Here is the call graph for this function:

◆ gptStopTimerI()

void gptStopTimerI ( GPTDriver * gptp)

Stops the timer.

Parameters
[in]gptppointer to the GPTDriver object
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 249 of file hal_gpt.c.

References GPT_CONTINUOUS, gpt_lld_stop_timer(), GPT_ONESHOT, GPT_READY, osalDbgAssert, osalDbgCheck, osalDbgCheckClassI, and GPTDriver::state.

Referenced by gptStopTimer().

Here is the call graph for this function:

◆ gptPolledDelay()

void gptPolledDelay ( GPTDriver * gptp,
gptcnt_t interval )

Starts the timer in one shot mode and waits for completion.

This function specifically polls the timer waiting for completion in order to not have extra delays caused by interrupt servicing, this function is only recommended for short delays.

Note
The configured callback is not invoked when using this function.
Parameters
[in]gptppointer to the GPTDriver object
[in]intervaltime interval in ticks
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 273 of file hal_gpt.c.

References gpt_lld_polled_delay(), GPT_ONESHOT, GPT_READY, osalDbgAssert, and GPTDriver::state.

Here is the call graph for this function:

◆ gpt_lld_init()

void gpt_lld_init ( void )

Low level GPT driver initialization.

Function Class:
Not an API, this function is for internal use only.

Definition at line 65 of file hal_gpt_lld.c.

References GPTD1, and gptObjectInit().

Referenced by gptInit().

Here is the call graph for this function:

◆ gpt_lld_start()

void gpt_lld_start ( GPTDriver * gptp)

Configures and activates the GPT peripheral.

Parameters
[in]gptppointer to the GPTDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 80 of file hal_gpt_lld.c.

References GPT_STOP, GPTD1, and GPTDriver::state.

Referenced by gptStart().

◆ gpt_lld_stop()

void gpt_lld_stop ( GPTDriver * gptp)

Deactivates the GPT peripheral.

Parameters
[in]gptppointer to the GPTDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 101 of file hal_gpt_lld.c.

References GPT_READY, GPTD1, and GPTDriver::state.

Referenced by gptStop().

◆ gpt_lld_start_timer()

void gpt_lld_start_timer ( GPTDriver * gptp,
gptcnt_t interval )

Starts the timer in continuous mode.

Parameters
[in]gptppointer to the GPTDriver object
[in]intervalperiod in ticks
Function Class:
Not an API, this function is for internal use only.

Definition at line 123 of file hal_gpt_lld.c.

Referenced by gptStartContinuousI(), and gptStartOneShotI().

◆ gpt_lld_stop_timer()

void gpt_lld_stop_timer ( GPTDriver * gptp)

Stops the timer.

Parameters
[in]gptppointer to the GPTDriver object
Function Class:
Not an API, this function is for internal use only.

Definition at line 137 of file hal_gpt_lld.c.

Referenced by gptStopTimerI().

◆ gpt_lld_polled_delay()

void gpt_lld_polled_delay ( GPTDriver * gptp,
gptcnt_t interval )

Starts the timer in one shot mode and waits for completion.

This function specifically polls the timer waiting for completion in order to not have extra delays caused by interrupt servicing, this function is only recommended for short delays.

Parameters
[in]gptppointer to the GPTDriver object
[in]intervaltime interval in ticks
Function Class:
Not an API, this function is for internal use only.

Definition at line 154 of file hal_gpt_lld.c.

Referenced by gptPolledDelay().

Variable Documentation

◆ GPTD1

GPTDriver GPTD1

GPTD1 driver identifier.

Definition at line 41 of file hal_gpt_lld.c.

Referenced by gpt_lld_init(), gpt_lld_start(), and gpt_lld_stop().