timer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef _HARDWARE_TIMER_H
8 #define _HARDWARE_TIMER_H
9 
10 #include "pico.h"
11 #include "hardware/structs/timer.h"
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
48 // PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_TIMER, Enable/disable assertions in the timer module, type=bool, default=0, group=hardware_timer
49 #ifndef PARAM_ASSERTIONS_ENABLED_TIMER
50 #define PARAM_ASSERTIONS_ENABLED_TIMER 0
51 #endif
52 
53 static inline void check_hardware_alarm_num_param(__unused uint alarm_num) {
54  invalid_params_if(TIMER, alarm_num >= NUM_TIMERS);
55 }
56 
65 static inline uint32_t time_us_32(void) {
66  return timer_hw->timerawl;
67 }
68 
78 uint64_t time_us_64(void);
79 
85 void busy_wait_us_32(uint32_t delay_us);
86 
92 void busy_wait_us(uint64_t delay_us);
93 
99 void busy_wait_ms(uint32_t delay_ms);
100 
107 
114 static inline bool time_reached(absolute_time_t t) {
115  uint64_t target = to_us_since_boot(t);
116  uint32_t hi_target = (uint32_t)(target >> 32u);
117  uint32_t hi = timer_hw->timerawh;
118  return (hi >= hi_target && (timer_hw->timerawl >= (uint32_t) target || hi != hi_target));
119 }
120 
127 typedef void (*hardware_alarm_callback_t)(uint alarm_num);
128 
137 void hardware_alarm_claim(uint alarm_num);
138 
147 int hardware_alarm_claim_unused(bool required);
148 
155 void hardware_alarm_unclaim(uint alarm_num);
156 
164 bool hardware_alarm_is_claimed(uint alarm_num);
165 
182 void hardware_alarm_set_callback(uint alarm_num, hardware_alarm_callback_t callback);
183 
194 bool hardware_alarm_set_target(uint alarm_num, absolute_time_t t);
195 
202 void hardware_alarm_cancel(uint alarm_num);
203 
216 void hardware_alarm_force_irq(uint alarm_num);
217 #ifdef __cplusplus
218 }
219 #endif
220 #endif
hardware_alarm_force_irq
void hardware_alarm_force_irq(uint alarm_num)
Force and IRQ for a specific hardware alarm.
Definition: timer.c:234
time_us_32
static uint32_t time_us_32(void)
Return a 32 bit timestamp value in microseconds.
Definition: timer.h:65
absolute_time_t
Definition: types.h:33
busy_wait_until
void busy_wait_until(absolute_time_t t)
Busy wait wasting cycles until after the specified timestamp.
Definition: timer.c:94
hardware_alarm_unclaim
void hardware_alarm_unclaim(uint alarm_num)
cooperatively release the claim on use of this hardware alarm_num
Definition: timer.c:26
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.
Definition: timer.c:175
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:44
busy_wait_ms
void busy_wait_ms(uint32_t delay_ms)
Busy wait wasting cycles for the given number of milliseconds.
Definition: timer.c:85
hardware_alarm_cancel
void hardware_alarm_cancel(uint alarm_num)
Cancel an existing target (if any) for a given hardware_alarm.
Definition: timer.c:224
hardware_alarm_is_claimed
bool hardware_alarm_is_claimed(uint alarm_num)
Determine if a hardware alarm has been claimed.
Definition: timer.c:31
hardware_alarm_claim
void hardware_alarm_claim(uint alarm_num)
cooperatively claim the use of this hardware alarm_num
Definition: timer.c:21
pico.h
busy_wait_us
void busy_wait_us(uint64_t delay_us)
Busy wait wasting cycles for the given (64 bit) number of microseconds.
Definition: timer.c:74
hardware_alarm_callback_t
void(* hardware_alarm_callback_t)(uint alarm_num)
Definition: timer.h:127
time_us_64
uint64_t time_us_64(void)
Return the current 64 bit timestamp value in microseconds.
Definition: timer.c:41
hardware_alarm_claim_unused
int hardware_alarm_claim_unused(bool required)
cooperatively claim the use of this hardware alarm_num
Definition: timer.c:36
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.
Definition: timer.c:61
hardware_alarm_set_callback
void hardware_alarm_set_callback(uint alarm_num, hardware_alarm_callback_t callback)
Enable/Disable a callback for a hardware timer on this core.
Definition: timer.c:148
time_reached
static bool time_reached(absolute_time_t t)
Check if the specified timestamp has been reached.
Definition: timer.h:114