This section introduces the General Purpose Timer(GPT) driver APIs including terms and acronyms, supported features, software architecture, details on how to use this driver, GPT function groups, enums, structures and functions.
More...
This section introduces the General Purpose Timer(GPT) driver APIs including terms and acronyms, supported features, software architecture, details on how to use this driver, GPT function groups, enums, structures and functions.
Terms and acronyms
Supported features
This controller has a generic design to support various combinations of the timer functionality.
- Support oneshot, repeat and free run modes.
A timer can be configured in one of the modes, oneshot mode, repeat mode and free run mode. The timer mode is configured through the mode configuration.
- Oneshot mode. In this mode, the interrupt is triggered only once when the timer expires.
- Repeat mode. In this mode, the interrupt is triggered when the timer expires. At the same time, the timer is reloaded again with the same value and starts ticking till the next expiration. This pattern repeats until the timer is cancelled. This mode is useful for handling functions that are executed periodically.
- Free run mode. In this mode, the timer simply becomes a counter. There is no interrupt triggered in this mode. The counter keeps running once using hal_gpt_get_free_run_count(), and never stops. Call hal_gpt_get_free_run_count() to get the stamp of counter ticks. This mode is useful when a delay or counting operations are performed.
- Support callback function registration.
A callback function must be registered using hal_gpt_register_callback(), then call hal_gpt_start_timer_ms() or hal_gpt_start_timer_us() to set the oneshot mode or the repeat mode to start the timer.
The callback function is called after the GPT triggers an interrupt in the GPT ISR routine.
- Software timer supporting multiple users.
The software timer supports multiple users. It is based on GPT driver, and occupies the hardware GPT port.
The software timer uses GPT oneshot mode. The maximum number of users is 32. The unit of the software timer is milliseconds.
How to use this driver
- Using GPT oneshot or repeat mode.
Call hal_gpt_get_running_status() function to manually check the GPT status before using the GPT driver in oneshot or repeat mode. If the status is HAL_GPT_RUNNING, user should wait till the status is HAL_GPT_STOPPED.
Call hal_gpt_init() then hal_gpt_start_timer_ms() functions to set the GPT mode and expiration time.
The timer then starts ticking. Once the pre-set time expires in a oneshot mode, the GPT triggers an interrupt, stops the timer, and calls user's callback function. In a repeat mode, when the set time expires, the timer is reloaded with the set time and the user callback function is invoked. To ensure the precise timing, do not overload the callback function. Let the callback return as fast as possible.
- Using GPT in free run mode.
Call hal_gpt_get_free_run_count() to get the first stamp of timer ticks.
Call hal_gpt_get_free_run_count() again to get the second stamp of timer ticks.
If HAL_GPT_CLOCK_SOURCE_32K parameter is in use, the unit of tick is 1/32768 seconds. The difference between the first and the second stamps is the counting ticks. In a free run mode, the GPT runs continuously and never stops. It does not provide interrupt feature. The GPT driver also uses this function implementation similar to the delay function hal_gpt_delay_ms().
- Using the GPT delay function.
Call hal_gpt_delay_ms() function to set the delay time. When in a delay, the GPT driver uses free run mode and polls register value until it expires.
- Sample code:
- Sample code:
- Using software timer.
Call hal_gpt_sw_get_timer() to allocate a software timer handle.
Call hal_gpt_sw_start_timer_ms() to register the callback function and start the timer.
The unit of the software timer is milliseconds.
The software timer is a oneshot timer. To run the software timer continuously, call it again in its callback function. Call hal_gpt_sw_stop_timer_ms() to stop the running timer. If the timer is not running, the HAL_GPT_STATUS_ERROR is returned. Call hal_gpt_sw_get_remaining_time_ms() to get the remaining timeout value at any time. Call hal_gpt_sw_free_timer() to free the allocated timer resource.
|
| hal_gpt_status_t | hal_gpt_init (hal_gpt_port_t gpt_port) |
| | This function initializes the GPT base enironment. More...
|
| |
| hal_gpt_status_t | hal_gpt_deinit (hal_gpt_port_t gpt_port) |
| | This function de-initializes the GPT timer. More...
|
| |
| hal_gpt_status_t | hal_gpt_get_running_status (hal_gpt_port_t gpt_port, hal_gpt_running_status_t *running_status) |
| | This function gets the running status of the port as specified. More...
|
| |
| hal_gpt_status_t | hal_gpt_register_callback (hal_gpt_port_t gpt_port, hal_gpt_callback_t callback, void *user_data) |
| | This function registers a callback function with the timer specified at the port. More...
|
| |
| hal_gpt_status_t | hal_gpt_get_free_run_count (hal_gpt_clock_source_t clock_source, uint32_t *count) |
| | This function gets the current count of timer in the free run mode. More...
|
| |
| hal_gpt_status_t | hal_gpt_start_timer_ms (hal_gpt_port_t gpt_port, uint32_t timeout_time_ms, hal_gpt_timer_type_t timer_type) |
| | This function sets the expiration time in milliseconds and the timer mode, then starts the timer. More...
|
| |
| hal_gpt_status_t | hal_gpt_delay_ms (uint32_t ms) |
| | This function sets the delay time in milliseconds. More...
|
| |
| hal_gpt_status_t | hal_gpt_start_timer_us (hal_gpt_port_t gpt_port, uint32_t timeout_time_us, hal_gpt_timer_type_t timer_type) |
| | This function sets the expiration time in microseconds and the timer mode, then starts the timer. More...
|
| |
| hal_gpt_status_t | hal_gpt_delay_us (uint32_t us) |
| | This function sets delay time in microseconds. More...
|
| |
| hal_gpt_status_t | hal_gpt_stop_timer (hal_gpt_port_t gpt_port) |
| | This function stops the timer only for oneshot mode and repeat mode. More...
|
| |
| hal_gpt_status_t | hal_gpt_get_duration_count (uint32_t start_count, uint32_t end_count, uint32_t *duration_count) |
| | This function calculates the count duration. More...
|
| |
| hal_gpt_status_t | hal_gpt_sw_get_timer (uint32_t *handle) |
| | This function allocates timer handle. More...
|
| |
| hal_gpt_status_t | hal_gpt_sw_free_timer (uint32_t handle) |
| | This function frees timer. More...
|
| |
| hal_gpt_status_t | hal_gpt_sw_start_timer_ms (uint32_t handle, uint32_t timeout_time_ms, hal_gpt_callback_t callback, void *user_data) |
| | This function starts the software timer. More...
|
| |
| hal_gpt_status_t | hal_gpt_sw_stop_timer_ms (uint32_t handle) |
| | This function stops the specified software timer. More...
|
| |
| hal_gpt_status_t | hal_gpt_sw_get_remaining_time_ms (uint32_t handle, uint32_t *remaing_time) |
| | This function gets the remaining timeout value of the specified software timer. More...
|
| |
This function de-initializes the GPT timer.
After calling this function, the callback is cleared, the clock power is turned off, interrupts and GPT module are disabled.
- Parameters
-
| [in] | gpt_port | is the port number. |
- Returns
- HAL_GPT_STATUS_OK, if the operation is successful.
HAL_GPT_STATUS_ERROR_PORT, if the gpt_port value is wrong.
- See also
- hal_gpt_init()
This function sets the delay time in milliseconds.
The maximum delay time = 1/GPT_clock * 0xffffffff.
- Parameters
-
| [in] | ms | is the delay time in milliseconds. |
- Returns
- HAL_GPT_STATUS_OK, if the operation is successful.
HAL_GPT_STATUS_ERROR_PORT, if the gpt_port value is wrong.
This function sets delay time in microseconds.
The maximum delay time = 1/GPT_clock * 0xffffffff.
- Parameters
-
| [in] | us | is the delay time in microseconds. |
- Returns
- HAL_GPT_STATUS_OK, if the operation is successful.
HAL_GPT_STATUS_ERROR_PORT, if the gpt_port value is wrong.
| hal_gpt_status_t hal_gpt_get_duration_count |
( |
uint32_t |
start_count, |
|
|
uint32_t |
end_count, |
|
|
uint32_t * |
duration_count |
|
) |
| |
This function calculates the count duration.
This API is to help user cover count value rollback problem. For example, if start_count < end_count, it means the count value has grown up from 0xffffffff to 0.
- Parameters
-
| [in] | start_count | is the value of start count. |
| [in] | end_count | is the value of end count. |
| [out] | duration_count | is the user's parameter pointer to get count duration. |
- Returns
- HAL_GPT_STATUS_OK, if the operation is successful.
HAL_GPT_STATUS_INVALID_PARAMETER, if the duration_count is null.
This function gets the current count of timer in the free run mode.
- Parameters
-
| [in] | clock_source | is the clock source of the timer in the free run mode. For more details, please refer to Enum about hal_gpt_clock_source_t. |
| [out] | count | is the user's pointer to a parameter to get the count value. |
- Returns
- HAL_GPT_STATUS_OK, if the operation is successful.
This function gets the running status of the port as specified.
- Parameters
-
| [in] | gpt_port | is the port number. |
| [out] | running_status | is the pointer to the running status after the function returns. For more details about this parameter, please refer to hal_gpt_running_status_t. |
- Returns
- HAL_GPT_STATUS_OK, if the operation is successful.
HAL_GPT_STATUS_ERROR_PORT, if the gpt_port value is wrong.
This function registers a callback function with the timer specified at the port.
The callback can only be registered when the timer, as specified by the port, is in HAL_GPT_STOPPED state. If the timer is in HAL_GPT_RUNNING state, the callback cannot be registered and this function returns HAL_GPT_STATUS_ERROR.
- Parameters
-
| [in] | gpt_port | is the port number. |
| [in] | callback | is the function pointer of the callback. This callback will be called when the timer expires. |
| [in] | user_data | is the pointer to the user defined data to pass to the callback function. |
- Returns
- HAL_GPT_STATUS_OK, if the operation is successful.
HAL_GPT_STATUS_ERROR_PORT, if the gpt_port value is wrong.
HAL_GPT_STATUS_ERROR, if the callback registration failed.
This function sets the expiration time in milliseconds and the timer mode, then starts the timer.
The function should only be called while the timer is stopped. An error would be returned if this function is called when the timer is running.
- Parameters
-
| [in] | gpt_port | is the port number. |
| [in] | timeout_time_ms | is the expiration time in milliseconds. |
| [in] | timer_type | is the timer mode, such as oneshot or repeat timer mode defined in hal_gpt_timer_type_t. |
- Returns
- HAL_GPT_STATUS_OK, if the operation is successful.
HAL_GPT_STATUS_ERROR_PORT, if the gpt_port value is wrong.
This function sets the expiration time in microseconds and the timer mode, then starts the timer.
The function should only be called while the timer is stopped. An error would be returned if this function is called when the timer is running.
- Parameters
-
| [in] | gpt_port | is the port number. |
| [in] | timeout_time_us | is the expiration time in microseconds. |
| [in] | timer_type | is the timer mode, i.e. oneshot or repeat timer mode defined in hal_gpt_timer_type_t. |
- Returns
- HAL_GPT_STATUS_OK, if the operation is successful.
HAL_GPT_STATUS_ERROR_PORT, if the gpt_port value is wrong.
This function stops the timer only for oneshot mode and repeat mode.
- Parameters
-
| [in] | gpt_port | is the port number. |
- Returns
- HAL_GPT_STATUS_OK, if the timer is stopped successfully.
HAL_GPT_STATUS_ERROR_PORT, if the gpt_port value is wrong.
| hal_gpt_status_t hal_gpt_sw_get_remaining_time_ms |
( |
uint32_t |
handle, |
|
|
uint32_t * |
remaing_time |
|
) |
| |
This function gets the remaining timeout value of the specified software timer.
- Parameters
-
| [in] | handle | is the handle of the timer to get the remaining time from. |
| [out] | remaing_time | is the pointer to the value of the remaining timeout, after the return of this function. |
- Returns
- HAL_GPT_STATUS_OK, if the operation is successful.
HAL_GPT_STATUS_INVALID_PARAMETER, if the handle is invalid.
HAL_GPT_STATUS_ERROR, if the handle is not allocated.
This function allocates timer handle.
- Parameters
-
| [out] | handle | is an unsigned integer for accessing this timer. It's equivalent to an ID number of the timer. |
- Returns
- HAL_GPT_STATUS_OK, if the operation is successful.
HAL_GPT_STATUS_ERROR, if a timer cannot be allocated.
This function starts the software timer.
- Parameters
-
| [in] | handle | is the handle of the timer to be started. |
| [in] | timeout_time_ms | is the timeout value in milliseconds. |
| [in] | callback | is the callback to be registered with this timer. This callback will be called when the timer times out. |
| [in] | user_data | is the pointer to the user defined data to pass to the callback function. |
- Returns
- HAL_GPT_STATUS_OK, if the operation is successful.
HAL_GPT_STATUS_INVALID_PARAMETER, if the parameter is invalid.
HAL_GPT_STATUS_ERROR, for all other errors.