2016-10-04 20:02:44 +00:00
|
|
|
|
|
|
|
/** \addtogroup hal */
|
|
|
|
/** @{*/
|
2015-04-23 13:56:34 +00:00
|
|
|
/* mbed Microcontroller Library
|
|
|
|
* Copyright (c) 2015 ARM Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
#ifndef MBED_TICKER_API_H
|
|
|
|
#define MBED_TICKER_API_H
|
|
|
|
|
2016-05-25 12:34:28 +00:00
|
|
|
#include <stdint.h>
|
2017-03-31 13:01:38 +00:00
|
|
|
#include <stdbool.h>
|
2015-04-23 13:56:34 +00:00
|
|
|
#include "device.h"
|
|
|
|
|
2017-03-31 13:01:38 +00:00
|
|
|
/**
|
|
|
|
* Maximum delta (in us) between too interrupts.
|
|
|
|
*/
|
|
|
|
#define MBED_TICKER_INTERRUPT_TIMESTAMP_MAX_DELTA 0x70000000ULL
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Legacy format representing a timestamp in us.
|
|
|
|
* Given it is modeled as a 32 bit integer, this type can represent timestamp
|
|
|
|
* up to 4294 seconds (71 minutes).
|
|
|
|
* Prefer using us_timestamp_t which store timestamp as 64 bits integer.
|
|
|
|
*/
|
2015-04-23 13:56:34 +00:00
|
|
|
typedef uint32_t timestamp_t;
|
|
|
|
|
2017-03-31 13:01:38 +00:00
|
|
|
/**
|
|
|
|
* A us timestamp stored in a 64 bit integer.
|
|
|
|
* Can store timestamp up to 584810 years.
|
|
|
|
*/
|
|
|
|
typedef uint64_t us_timestamp_t;
|
|
|
|
|
2015-04-23 13:56:34 +00:00
|
|
|
/** Ticker's event structure
|
|
|
|
*/
|
|
|
|
typedef struct ticker_event_s {
|
2017-03-31 13:01:38 +00:00
|
|
|
us_timestamp_t timestamp; /**< Event's timestamp */
|
2015-04-23 13:56:34 +00:00
|
|
|
uint32_t id; /**< TimerEvent object */
|
|
|
|
struct ticker_event_s *next; /**< Next event in the queue */
|
|
|
|
} ticker_event_t;
|
|
|
|
|
|
|
|
typedef void (*ticker_event_handler)(uint32_t id);
|
|
|
|
|
|
|
|
/** Ticker's interface structure - required API for a ticker
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
void (*init)(void); /**< Init function */
|
|
|
|
uint32_t (*read)(void); /**< Read function */
|
|
|
|
void (*disable_interrupt)(void); /**< Disable interrupt function */
|
|
|
|
void (*clear_interrupt)(void); /**< Clear interrupt function */
|
|
|
|
void (*set_interrupt)(timestamp_t timestamp); /**< Set interrupt function */
|
Ticker: add fire interrupt now function
fire_interrupt function should be used for events in the past. As we have now
64bit timestamp, we can figure out what is in the past, and ask a target to invoke
an interrupt immediately. The previous attemps in the target HAL tickers were not ideal, as it can wrap around easily (16 or 32 bit counters). This new
functionality should solve this problem.
set_interrupt for tickers in HAL code should not handle anything but the next match interrupt. If it was in the past is handled by the upper layer.
It is possible that we are setting next event to the close future, so once it is set it is already in the past. Therefore we add a check after set interrupt to verify it is in future.
If it is not, we fire interrupt immediately. This results in
two events - first one immediate, correct one. The second one might be scheduled in far future (almost entire ticker range),
that should be discarded.
The specification for the fire_interrupts are:
- should set pending bit for the ticker interrupt (as soon as possible),
the event we are scheduling is already in the past, and we do not want to skip
any events
- no arguments are provided, neither return value, not needed
- ticker should be initialized prior calling this function (no need to check if it is already initialized)
All our targets provide this new functionality, removing old misleading if (timestamp is in the past) checks.
2017-06-27 11:18:59 +00:00
|
|
|
void (*fire_interrupt)(void); /**< Fire interrupt right-away */
|
2015-04-23 13:56:34 +00:00
|
|
|
} ticker_interface_t;
|
|
|
|
|
2016-05-25 12:34:28 +00:00
|
|
|
/** Ticker's event queue structure
|
2015-04-23 13:56:34 +00:00
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
ticker_event_handler event_handler; /**< Event handler */
|
|
|
|
ticker_event_t *head; /**< A pointer to head */
|
2017-05-15 16:08:53 +00:00
|
|
|
us_timestamp_t present_time; /**< Store the timestamp used for present time */
|
2017-03-31 13:01:38 +00:00
|
|
|
bool initialized; /**< Indicate if the instance is initialized */
|
2015-04-23 13:56:34 +00:00
|
|
|
} ticker_event_queue_t;
|
|
|
|
|
2016-05-25 12:34:28 +00:00
|
|
|
/** Ticker's data structure
|
2015-04-23 13:56:34 +00:00
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
const ticker_interface_t *interface; /**< Ticker's interface */
|
2016-05-25 12:34:28 +00:00
|
|
|
ticker_event_queue_t *queue; /**< Ticker's event queue */
|
2015-04-23 13:56:34 +00:00
|
|
|
} ticker_data_t;
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2016-05-25 12:34:28 +00:00
|
|
|
/**
|
|
|
|
* \defgroup hal_ticker Ticker HAL functions
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** Initialize a ticker and set the event handler
|
2015-04-23 13:56:34 +00:00
|
|
|
*
|
2017-05-15 16:08:53 +00:00
|
|
|
* @param ticker The ticker object.
|
2015-04-23 13:56:34 +00:00
|
|
|
* @param handler A handler to be set
|
|
|
|
*/
|
2017-03-31 13:01:38 +00:00
|
|
|
void ticker_set_handler(const ticker_data_t *const ticker, ticker_event_handler handler);
|
2015-04-23 13:56:34 +00:00
|
|
|
|
2016-05-25 12:34:28 +00:00
|
|
|
/** IRQ handler that goes through the events to trigger overdue events.
|
2015-04-23 13:56:34 +00:00
|
|
|
*
|
2017-05-15 16:08:53 +00:00
|
|
|
* @param ticker The ticker object.
|
2015-04-23 13:56:34 +00:00
|
|
|
*/
|
2017-03-31 13:01:38 +00:00
|
|
|
void ticker_irq_handler(const ticker_data_t *const ticker);
|
2015-04-23 13:56:34 +00:00
|
|
|
|
|
|
|
/** Remove an event from the queue
|
|
|
|
*
|
2017-05-15 16:08:53 +00:00
|
|
|
* @param ticker The ticker object.
|
2016-05-25 12:34:28 +00:00
|
|
|
* @param obj The event object to be removed from the queue
|
2015-04-23 13:56:34 +00:00
|
|
|
*/
|
2017-03-31 13:01:38 +00:00
|
|
|
void ticker_remove_event(const ticker_data_t *const ticker, ticker_event_t *obj);
|
|
|
|
|
|
|
|
/** Insert an event to the queue
|
|
|
|
*
|
|
|
|
* The event will be executed in timestamp - ticker_read().
|
|
|
|
*
|
|
|
|
* @warning This function does not consider timestamp in the past. If an event
|
|
|
|
* is inserted with a timestamp less than the current timestamp then the event
|
|
|
|
* will be executed in timestamp - ticker_read() us.
|
|
|
|
* The internal counter wrap very quickly it is hard to decide weither an
|
|
|
|
* event is in the past or in 1 hour.
|
|
|
|
*
|
|
|
|
* @note prefer the use of ticker_insert_event_us which allows registration of
|
|
|
|
* absolute timestamp.
|
|
|
|
*
|
2017-05-15 16:08:53 +00:00
|
|
|
* @param ticker The ticker object.
|
2017-03-31 13:01:38 +00:00
|
|
|
* @param obj The event object to be inserted to the queue
|
|
|
|
* @param timestamp The event's timestamp
|
|
|
|
* @param id The event object
|
|
|
|
*/
|
|
|
|
void ticker_insert_event(const ticker_data_t *const ticker, ticker_event_t *obj, timestamp_t timestamp, uint32_t id);
|
2015-04-23 13:56:34 +00:00
|
|
|
|
2016-05-25 12:34:28 +00:00
|
|
|
/** Insert an event to the queue
|
2017-03-31 13:01:38 +00:00
|
|
|
*
|
|
|
|
* The event will be executed in timestamp - ticker_read_us() us.
|
|
|
|
*
|
|
|
|
* @warning If an event is inserted with a timestamp less than the current
|
|
|
|
* timestamp then the event will **not** be inserted.
|
2015-04-23 13:56:34 +00:00
|
|
|
*
|
2017-05-15 16:08:53 +00:00
|
|
|
* @param ticker The ticker object.
|
2016-05-25 12:34:28 +00:00
|
|
|
* @param obj The event object to be inserted to the queue
|
2015-04-23 13:56:34 +00:00
|
|
|
* @param timestamp The event's timestamp
|
|
|
|
* @param id The event object
|
|
|
|
*/
|
2017-03-31 13:01:38 +00:00
|
|
|
void ticker_insert_event_us(const ticker_data_t *const ticker, ticker_event_t *obj, us_timestamp_t timestamp, uint32_t id);
|
2015-04-23 13:56:34 +00:00
|
|
|
|
2017-03-31 13:01:38 +00:00
|
|
|
/** Read the current (relative) ticker's timestamp
|
|
|
|
*
|
|
|
|
* @warning Return a relative timestamp because the counter wrap every 4294
|
|
|
|
* seconds.
|
|
|
|
*
|
2017-05-15 16:08:53 +00:00
|
|
|
* @param ticker The ticker object.
|
2017-03-31 13:01:38 +00:00
|
|
|
* @return The current timestamp
|
|
|
|
*/
|
|
|
|
timestamp_t ticker_read(const ticker_data_t *const ticker);
|
|
|
|
|
|
|
|
/** Read the current (absolute) ticker's timestamp
|
|
|
|
*
|
|
|
|
* @warning Return an absolute timestamp counting from the initialization of the
|
|
|
|
* ticker.
|
2015-04-23 13:56:34 +00:00
|
|
|
*
|
2017-05-15 16:08:53 +00:00
|
|
|
* @param ticker The ticker object.
|
2015-04-23 13:56:34 +00:00
|
|
|
* @return The current timestamp
|
|
|
|
*/
|
2017-03-31 13:01:38 +00:00
|
|
|
us_timestamp_t ticker_read_us(const ticker_data_t *const ticker);
|
2015-04-23 13:56:34 +00:00
|
|
|
|
|
|
|
/** Read the next event's timestamp
|
|
|
|
*
|
2017-06-02 21:17:32 +00:00
|
|
|
* @param ticker The ticker object.
|
|
|
|
* @param timestamp The timestamp object.
|
2015-04-23 13:56:34 +00:00
|
|
|
* @return 1 if timestamp is pending event, 0 if there's no event pending
|
|
|
|
*/
|
2017-03-31 13:01:38 +00:00
|
|
|
int ticker_get_next_timestamp(const ticker_data_t *const ticker, timestamp_t *timestamp);
|
2015-04-23 13:56:34 +00:00
|
|
|
|
2016-05-25 12:34:28 +00:00
|
|
|
/**@}*/
|
|
|
|
|
2015-04-23 13:56:34 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
2016-10-04 20:02:44 +00:00
|
|
|
|
|
|
|
/** @}*/
|