@web-font-path: "roboto-debian.css";
v2.2.0 (RP2040)
Loading...
Searching...
No Matches
timer.h
Go to the documentation of this file.
/*
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#ifndef _HARDWARE_TIMER_H
#define _HARDWARE_TIMER_H
#include "pico.h"
#include "hardware/structs/timer.h"
#include "hardware/regs/intctrl.h"
#ifdef __cplusplus
extern "C" {
#endif
// PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_HARDWARE_TIMER, Enable/disable assertions in the hardware_timer module, type=bool, default=0, group=hardware_timer
#ifndef PARAM_ASSERTIONS_ENABLED_HARDWARE_TIMER
#ifdef PARAM_ASSERTIONS_ENABLED_TIMER // backwards compatibility with SDK < 2.0.0
#define PARAM_ASSERTIONS_ENABLED_HARDWARE_TIMER PARAM_ASSERTIONS_ENABLED_TIMER
#else
#define PARAM_ASSERTIONS_ENABLED_HARDWARE_TIMER 0
#endif
#endif
#ifndef TIMER_NUM
#if NUM_GENERIC_TIMERS == 1
#define TIMER_NUM(timer) ({ (void) (timer); 0; })
#elif NUM_GENERIC_TIMERS == 2
#define TIMER_NUM(timer) ((timer) == timer1_hw)
#endif
#endif
#ifndef TIMER_INSTANCE
#if NUM_GENERIC_TIMERS == 1
#define TIMER_INSTANCE(num) timer_hw
#elif NUM_GENERIC_TIMERS == 2
#define TIMER_INSTANCE(num) ((num) ? timer1_hw : timer0_hw)
#endif
#endif
#ifndef TIMER_ALARM_IRQ_NUM
#if NUM_GENERIC_TIMERS == 1
static_assert(TIMER_IRQ_3 == TIMER_IRQ_0 + 3, "");
#define TIMER_ALARM_IRQ_NUM(timer, alarm_num) ({ ((void)(timer)); (TIMER_IRQ_0 + (alarm_num)); })
#else
static_assert(TIMER1_IRQ_3 == TIMER0_IRQ_0 + 7, "");
#define TIMER_ALARM_IRQ_NUM(timer, alarm_num) (TIMER0_IRQ_0 + TIMER_NUM(timer) * NUM_ALARMS + (alarm_num))
#endif
#endif
#ifndef TIMER_ALARM_NUM_FROM_IRQ
#if NUM_GENERIC_TIMERS == 1
static_assert(TIMER_IRQ_3 == TIMER_IRQ_0 + 3, "");
#define TIMER_ALARM_NUM_FROM_IRQ(irq_num) (((irq_num) - TIMER_IRQ_0) & 3u)
#else
static_assert(TIMER1_IRQ_3 == TIMER0_IRQ_0 + 7, "");
#define TIMER_ALARM_NUM_FROM_IRQ(irq_num) (((irq_num) - TIMER0_IRQ_0) & 3u)
#endif
#endif
#ifndef TIMER_NUM_FROM_IRQ
#if NUM_GENERIC_TIMERS == 1
static_assert(TIMER_IRQ_3 == TIMER_IRQ_0 + 3, "");
#define TIMER_NUM_FROM_IRQ(irq_num) (((irq_num) - TIMER_IRQ_0) >> 2)
#else
static_assert(TIMER1_IRQ_3 == TIMER0_IRQ_0 + 7, "");
#define TIMER_NUM_FROM_IRQ(irq_num) (((irq_num) - TIMER0_IRQ_0) >> 2)
#endif
#endif
// PICO_CONFIG: PICO_DEFAULT_TIMER, Timer instance number to use for RP2040-period hardware_timer APIs that assumed a single timer instance, min=0, max=1, default=0, group=hardware_timer
#ifndef PICO_DEFAULT_TIMER
#define PICO_DEFAULT_TIMER 0
#endif
#ifndef PICO_DEFAULT_TIMER_INSTANCE
#if NUM_GENERIC_TIMERS == 1
#if PICO_DEFAULT_TIMER
#error Setting PICO_DEFAULT_TIMER to non zero is meaningless as there is only one TIMER instance on this platform
#endif
#define PICO_DEFAULT_TIMER_INSTANCE() timer_hw
#else
#define PICO_DEFAULT_TIMER_INSTANCE() (__CONCAT(__CONCAT(timer,PICO_DEFAULT_TIMER), _hw))
// also define timer_hw for backwards compatibility (just accesses the default instance)
#define timer_hw PICO_DEFAULT_TIMER_INSTANCE()
#endif
#endif
static inline void check_hardware_alarm_num_param(__unused uint alarm_num) {
invalid_params_if(HARDWARE_TIMER, alarm_num >= NUM_ALARMS);
}
static inline uint32_t timer_time_us_32(timer_hw_t *timer) {
return timer->timerawl;
}
static inline uint32_t time_us_32(void) {
return timer_time_us_32(PICO_DEFAULT_TIMER_INSTANCE());
}
uint64_t timer_time_us_64(timer_hw_t *timer);
uint64_t time_us_64(void);
void timer_busy_wait_us_32(timer_hw_t *timer, uint32_t delay_us);
void busy_wait_us_32(uint32_t delay_us);
void timer_busy_wait_us(timer_hw_t *timer, uint64_t delay_us);
void busy_wait_us(uint64_t delay_us);
void timer_busy_wait_ms(timer_hw_t *timer, uint32_t delay_ms);
void busy_wait_ms(uint32_t delay_ms);
void timer_busy_wait_until(timer_hw_t *timer, absolute_time_t t);
void busy_wait_until(absolute_time_t t);
static inline bool timer_time_reached(timer_hw_t *timer, absolute_time_t t) {
uint64_t target = to_us_since_boot(t);
uint32_t hi_target = (uint32_t)(target >> 32u);
uint32_t hi = timer->timerawh;
return (hi >= hi_target && (timer->timerawl >= (uint32_t) target || hi != hi_target));
}
static inline bool time_reached(absolute_time_t t) {
return timer_time_reached(PICO_DEFAULT_TIMER_INSTANCE(), t);
}
typedef void (*hardware_alarm_callback_t)(uint alarm_num);
void timer_hardware_alarm_claim(timer_hw_t *timer, uint alarm_num);
void hardware_alarm_claim(uint alarm_num);
int timer_hardware_alarm_claim_unused(timer_hw_t *timer, bool required);
int hardware_alarm_claim_unused(bool required);
void timer_hardware_alarm_unclaim(timer_hw_t *timer, uint alarm_num);
void hardware_alarm_unclaim(uint alarm_num);
bool timer_hardware_alarm_is_claimed(timer_hw_t *timer, uint alarm_num);
bool hardware_alarm_is_claimed(uint alarm_num);
void timer_hardware_alarm_set_callback(timer_hw_t *timer, uint alarm_num, hardware_alarm_callback_t callback);
void hardware_alarm_set_callback(uint alarm_num, hardware_alarm_callback_t callback);
bool timer_hardware_alarm_set_target(timer_hw_t *timer, uint alarm_num, absolute_time_t t);
bool hardware_alarm_set_target(uint alarm_num, absolute_time_t t);
void timer_hardware_alarm_cancel(timer_hw_t *timer, uint alarm_num);
void hardware_alarm_cancel(uint alarm_num);
void timer_hardware_alarm_force_irq(timer_hw_t *timer, uint alarm_num);
void hardware_alarm_force_irq(uint alarm_num);
static inline uint timer_hardware_alarm_get_irq_num(timer_hw_t *timer, uint alarm_num) {
check_hardware_alarm_num_param(alarm_num);
return TIMER_ALARM_IRQ_NUM(timer, alarm_num);
}
static inline uint hardware_alarm_get_irq_num(uint alarm_num) {
return timer_hardware_alarm_get_irq_num(PICO_DEFAULT_TIMER_INSTANCE(), alarm_num);
}
static inline uint timer_get_index(timer_hw_t *timer) {
return TIMER_NUM(timer);
}
static inline timer_hw_t *timer_get_instance(uint timer_num) {
invalid_params_if(HARDWARE_TIMER, timer_num >= NUM_GENERIC_TIMERS);
return TIMER_INSTANCE(timer_num);
}
#ifdef __cplusplus
}
#endif
#endif
TIMER_IRQ_0
@ TIMER_IRQ_0
Select TIMER's IRQ 0 output.
Definition intctrl.h:54
TIMER_IRQ_3
@ TIMER_IRQ_3
Select TIMER's IRQ 3 output.
Definition intctrl.h:57
hardware_alarm_unclaim
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
busy_wait_ms
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
timer_get_instance
static timer_hw_t * timer_get_instance(uint timer_num)
Returns the timer instance with the given timer number.
Definition timer.h:590
time_reached
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
timer_hardware_alarm_set_callback
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:184
timer_time_reached
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
timer_time_us_32
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
hardware_alarm_claim_unused
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
hardware_alarm_get_irq_num
static uint hardware_alarm_get_irq_num(uint alarm_num)
Returns the irq_num_t for the alarm interrupt from the given alarm on the default timer instance.
Definition timer.h:567
timer_hardware_alarm_is_claimed
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
hardware_alarm_force_irq
void hardware_alarm_force_irq(uint alarm_num)
Force and IRQ for a specific hardware alarm on the default timer instance.
Definition timer.c:293
hardware_alarm_claim
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
hardware_alarm_cancel
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:280
hardware_alarm_set_target
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:266
timer_hardware_alarm_force_irq
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:284
timer_hardware_alarm_get_irq_num
static uint timer_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 given timer instance.
Definition timer.h:557
hardware_alarm_is_claimed
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
timer_get_index
static uint timer_get_index(timer_hw_t *timer)
Returns the timer number for a timer instance.
Definition timer.h:579
TIMER_ALARM_IRQ_NUM
#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
time_us_64
uint64_t time_us_64(void)
Return the current 64 bit timestamp value in microseconds for the default timer instance.
Definition timer.c:125
timer_hardware_alarm_set_target
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:216
timer_hardware_alarm_claim_unused
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
busy_wait_us_32
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
PICO_DEFAULT_TIMER_INSTANCE
#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
timer_time_us_64
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
timer_busy_wait_until
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
timer_busy_wait_us_32
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
timer_busy_wait_us
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
hardware_alarm_set_callback
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:212
timer_hardware_alarm_claim
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
busy_wait_until
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
timer_hardware_alarm_cancel
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:270
time_us_32
static uint32_t time_us_32(void)
Return a 32 bit timestamp value in microseconds for the default timer instance.
Definition timer.h:215
busy_wait_us
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
timer_busy_wait_ms
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
timer_hardware_alarm_unclaim
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
hardware_alarm_callback_t
void(* hardware_alarm_callback_t)(uint alarm_num)
Definition timer.h:344
to_us_since_boot
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
absolute_time_t
uint64_t absolute_time_t
An opaque 64 bit timestamp in microseconds.
Definition types.h:43
pico.h
timer_hw_t
Definition timer.h:26