7#ifndef _HARDWARE_TIMER_H
8#define _HARDWARE_TIMER_H
11#include "hardware/structs/timer.h"
12#include "hardware/regs/intctrl.h"
57#ifndef PARAM_ASSERTIONS_ENABLED_HARDWARE_TIMER
58#ifdef PARAM_ASSERTIONS_ENABLED_TIMER
59#define PARAM_ASSERTIONS_ENABLED_HARDWARE_TIMER PARAM_ASSERTIONS_ENABLED_TIMER
61#define PARAM_ASSERTIONS_ENABLED_HARDWARE_TIMER 0
74#if NUM_GENERIC_TIMERS == 1
75#define TIMER_NUM(timer) ({ (void) (timer); 0; })
76#elif NUM_GENERIC_TIMERS == 2
77#define TIMER_NUM(timer) ((timer) == timer1_hw)
90#if NUM_GENERIC_TIMERS == 1
91#define TIMER_INSTANCE(num) timer_hw
92#elif NUM_GENERIC_TIMERS == 2
93#define TIMER_INSTANCE(num) ((num) ? timer1_hw : timer0_hw)
105#ifndef TIMER_ALARM_IRQ_NUM
106#if NUM_GENERIC_TIMERS == 1
108#define TIMER_ALARM_IRQ_NUM(timer, alarm_num) (TIMER_IRQ_0 + (alarm_num))
110static_assert(TIMER1_IRQ_3 == TIMER0_IRQ_0 + 7,
"");
111#define TIMER_ALARM_IRQ_NUM(timer, alarm_num) (TIMER0_IRQ_0 + TIMER_NUM(timer) * NUM_ALARMS + (alarm_num))
123#ifndef TIMER_ALARM_NUM_FROM_IRQ
124#if NUM_GENERIC_TIMERS == 1
126#define TIMER_ALARM_NUM_FROM_IRQ(irq_num) (((irq_num) - TIMER_IRQ_0) & 3u)
128static_assert(TIMER1_IRQ_3 == TIMER0_IRQ_0 + 7,
"");
129#define TIMER_ALARM_NUM_FROM_IRQ(irq_num) (((irq_num) - TIMER0_IRQ_0) & 3u)
141#ifndef TIMER_NUM_FROM_IRQ
142#if NUM_GENERIC_TIMERS == 1
144#define TIMER_NUM_FROM_IRQ(irq_num) (((irq_num) - TIMER_IRQ_0) >> 2)
146static_assert(TIMER1_IRQ_3 == TIMER0_IRQ_0 + 7,
"");
147#define TIMER_NUM_FROM_IRQ(irq_num) (((irq_num) - TIMER0_IRQ_0) >> 2)
163#ifndef PICO_DEFAULT_TIMER
164#define PICO_DEFAULT_TIMER 0
175#ifndef PICO_DEFAULT_TIMER_INSTANCE
176#if NUM_GENERIC_TIMERS == 1
177#if PICO_DEFAULT_TIMER
178#error Setting PICO_DEFAULT_TIMER to non zero is meaningless as there is only one TIMER instance on this platform
180#define PICO_DEFAULT_TIMER_INSTANCE() timer_hw
182#define PICO_DEFAULT_TIMER_INSTANCE() (__CONCAT(__CONCAT(timer,PICO_DEFAULT_TIMER), _hw))
184#define timer_hw PICO_DEFAULT_TIMER_INSTANCE()
188static inline void check_hardware_alarm_num_param(__unused uint alarm_num) {
189 invalid_params_if(HARDWARE_TIMER, alarm_num >= NUM_ALARMS);
203 return timer->timerawl;
322 uint32_t hi_target = (uint32_t)(target >> 32u);
323 uint32_t hi = timer->timerawh;
324 return (hi >= hi_target && (timer->timerawl >= (uint32_t) target || hi != hi_target));
558 check_hardware_alarm_num_param(alarm_num);
581 return TIMER_NUM(timer);
592 invalid_params_if(HARDWARE_TIMER, timer_num >= NUM_GENERIC_TIMERS);
593 return TIMER_INSTANCE(timer_num);
@ TIMER_IRQ_0
Select TIMER's IRQ 0 output.
Definition intctrl.h:48
@ TIMER_IRQ_3
Select TIMER's IRQ 3 output.
Definition intctrl.h:51
void hardware_alarm_unclaim(uint alarm_num)
cooperatively release the claim on use of this hardware alarm_num on the default timer instance
Definition timer.c:35
void busy_wait_ms(uint32_t delay_ms)
Busy wait wasting cycles for the given number of milliseconds using the default timer instance.
Definition timer.c:137
static timer_hw_t * timer_get_instance(uint timer_num)
Returns the timer instance with the given timer number.
Definition timer.h:591
static bool time_reached(absolute_time_t t)
Check if the specified timestamp has been reached on the default timer instance.
Definition timer.h:334
void timer_hardware_alarm_set_callback(timer_hw_t *timer, uint alarm_num, hardware_alarm_callback_t callback)
Enable/Disable a callback for a hardware alarm for a given timer instance on this core.
Definition timer.c:183
static bool timer_time_reached(timer_hw_t *timer, absolute_time_t t)
Check if the specified timestamp has been reached on the given timer instance.
Definition timer.h:320
static uint32_t timer_time_us_32(timer_hw_t *timer)
Return a 32 bit timestamp value in microseconds for a given timer instance.
Definition timer.h:202
static uint timer_hardware_alarm_get_irq_num(__unused timer_hw_t *timer, uint alarm_num)
Returns the irq_num_t for the alarm interrupt from the given alarm on the given timer instance.
Definition timer.h:557
int hardware_alarm_claim_unused(bool required)
cooperatively claim the use of a hardware alarm_num on the default timer instance
Definition timer.c:52
bool timer_hardware_alarm_is_claimed(timer_hw_t *timer, uint alarm_num)
Determine if a hardware alarm has been claimed on the given timer instance.
Definition timer.c:39
static uint hardware_alarm_get_irq_num(timer_hw_t *timer, uint alarm_num)
Returns the irq_num_t for the alarm interrupt from the given alarm on the default timer instance.
Definition timer.h:568
void hardware_alarm_force_irq(uint alarm_num)
Force and IRQ for a specific hardware alarm on the default timer instance.
Definition timer.c:292
void hardware_alarm_claim(uint alarm_num)
cooperatively claim the use of this hardware alarm_num on the default timer instance
Definition timer.c:26
void hardware_alarm_cancel(uint alarm_num)
Cancel an existing target (if any) for the specified hardware_alarm on the default timer instance.
Definition timer.c:279
bool hardware_alarm_set_target(uint alarm_num, absolute_time_t t)
Set the current target for the specified hardware alarm on the default timer instance.
Definition timer.c:265
void timer_hardware_alarm_force_irq(timer_hw_t *timer, uint alarm_num)
Force and IRQ for a specific hardware alarm on the given timer instance.
Definition timer.c:283
bool hardware_alarm_is_claimed(uint alarm_num)
Determine if a hardware alarm has been claimed on the default timer instance.
Definition timer.c:44
static uint timer_get_index(timer_hw_t *timer)
Returns the timer number for a timer instance.
Definition timer.h:580
#define TIMER_ALARM_IRQ_NUM(timer, alarm_num)
Returns the irq_num_t for the alarm interrupt from the given alarm on the given timer instance.
Definition timer.h:111
uint64_t time_us_64(void)
Return the current 64 bit timestamp value in microseconds for the default timer instance.
Definition timer.c:125
bool timer_hardware_alarm_set_target(timer_hw_t *timer, uint alarm_num, absolute_time_t t)
Set the current target for a specific hardware alarm on the given timer instance.
Definition timer.c:215
int timer_hardware_alarm_claim_unused(timer_hw_t *timer, bool required)
cooperatively claim the use of a hardware alarm_num on the given timer instance
Definition timer.c:48
void busy_wait_us_32(uint32_t delay_us)
Busy wait wasting cycles for the given (32 bit) number of microseconds using the default timer instan...
Definition timer.c:129
#define PICO_DEFAULT_TIMER_INSTANCE()
Returns the default timer instance on the platform based on the setting of PICO_DEFAULT_TIMER.
Definition timer.h:182
uint64_t timer_time_us_64(timer_hw_t *timer)
Return the current 64 bit timestamp value in microseconds for a given timer instance.
Definition timer.c:57
void timer_busy_wait_until(timer_hw_t *timer, absolute_time_t t)
Busy wait wasting cycles until after the specified timestamp using the given timer instance.
Definition timer.c:110
void timer_busy_wait_us_32(timer_hw_t *timer, uint32_t delay_us)
Busy wait wasting cycles for the given (32 bit) number of microseconds using the given timer instance...
Definition timer.c:77
void timer_busy_wait_us(timer_hw_t *timer, uint64_t delay_us)
Busy wait wasting cycles for the given (64 bit) number of microseconds using the given timer instance...
Definition timer.c:90
void hardware_alarm_set_callback(uint alarm_num, hardware_alarm_callback_t callback)
Enable/Disable a callback for a hardware alarm on the default timer instance on this core.
Definition timer.c:211
void timer_hardware_alarm_claim(timer_hw_t *timer, uint alarm_num)
cooperatively claim the use of this hardware alarm_num on the given timer instance
Definition timer.c:21
void busy_wait_until(absolute_time_t t)
Busy wait wasting cycles until after the specified timestamp using the default timer instance.
Definition timer.c:142
void timer_hardware_alarm_cancel(timer_hw_t *timer, uint alarm_num)
Cancel an existing target (if any) for a specific hardware_alarm on the given timer instance.
Definition timer.c:269
static uint32_t time_us_32(void)
Return a 32 bit timestamp value in microseconds for the default timer instance.
Definition timer.h:215
void busy_wait_us(uint64_t delay_us)
Busy wait wasting cycles for the given (64 bit) number of microseconds using the default timer instan...
Definition timer.c:133
void timer_busy_wait_ms(timer_hw_t *timer, uint32_t delay_ms)
Busy wait wasting cycles for the given number of milliseconds using the given timer instance.
Definition timer.c:101
void timer_hardware_alarm_unclaim(timer_hw_t *timer, uint alarm_num)
cooperatively release the claim on use of this hardware alarm_num on the given timer instance
Definition timer.c:30
void(* hardware_alarm_callback_t)(uint alarm_num)
Definition timer.h:344
static uint64_t to_us_since_boot(absolute_time_t t)
convert an absolute_time_t into a number of microseconds since boot.
Definition types.h:52