Rp2040/Components/driver/include/driver/hw_timer.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * hw_timer.h
8  *
9  ****/
10 
11 #pragma once
12 
13 #include <esp_systemapi.h>
14 #include <hardware/timer.h>
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 #define HW_TIMER_BASE_CLK 1000000U
21 
22 #define HW_TIMER_NUM 0
23 #define HW_TIMER_INST TIMER_INSTANCE(HW_TIMER_NUM)
24 
40 __forceinline uint32_t IRAM_ATTR hw_timer_ticks()
41 {
42  return timer_time_us_32(HW_TIMER_INST);
43 }
44 
45 /*************************************
46  *
47  * Timer1 (countdown alarm)
48  *
49  *************************************/
50 
54 #define MAX_HW_TIMER1_INTERVAL 0x7fffffffU
55 
62 #define MIN_HW_TIMER1_INTERVAL_US 50U
63 
64 typedef void (*hw_timer_callback_t)(void* arg);
65 
66 typedef enum {
71 
72 typedef enum {
73  TIMER_EDGE_INT, // edge interrupt
74  TIMER_LEVEL_INT, // level interrupt
76 
77 typedef enum {
81 
82 // Internal data
86  uint32_t timer1_ticks;
88  void* timer1_arg;
89 };
90 
92 
99 void hw_timer1_attach_interrupt(hw_timer_source_type_t source_type, hw_timer_callback_t callback, void* arg);
100 
105 
112 void hw_timer1_enable(hw_timer_clkdiv_t div, hw_timer_intr_type_t intr_type, bool auto_load);
113 
118 __forceinline void IRAM_ATTR hw_timer1_write(uint32_t ticks)
119 {
122  HW_TIMER_INST->alarm[0] = hw_timer_ticks() + ticks;
123 }
124 
128 __forceinline void IRAM_ATTR hw_timer1_disable()
129 {
130  HW_TIMER_INST->armed = BIT(0);
131 }
132 
137 __forceinline uint32_t hw_timer1_read()
138 {
139  int time = hw_timer_ticks() - HW_TIMER_INST->alarm[0];
140  return (time > 0) ? (time >> hw_timer_private.timer1_clkdiv) : 0;
141 }
142 
143 /*************************************
144  *
145  * FRC2 timer
146  *
147  * This is a 32-bit count-up timer.
148  * Used for software timers - see os_timer.h
149  *
150  *************************************/
151 
152 #define HW_TIMER2_CLK HW_TIMER_BASE_CLK
153 
158 __forceinline uint32_t hw_timer2_read()
159 {
160  return hw_timer_ticks();
161 }
162 
165 #ifdef __cplusplus
166 }
167 #endif
#define HW_TIMER_INST
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:23
#define BIT(nr)
Definition: c_types.h:30
void hw_timer1_attach_interrupt(hw_timer_source_type_t source_type, hw_timer_callback_t callback, void *arg)
Attach an interrupt for the timer.
static uint32_t hw_timer2_read(void)
Read current timer2 value.
Definition: Esp32/Components/driver/include/driver/hw_timer.h:150
void hw_timer1_detach_interrupt(void)
Detach interrupt from the timer.
hw_timer_intr_type_t
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:72
void hw_timer1_disable(void)
Disable the timer.
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:117
void hw_timer1_enable(hw_timer_clkdiv_t div, hw_timer_intr_type_t intr_type, bool auto_load)
Enable the timer.
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:90
struct hw_timer_private_t hw_timer_private
hw_timer_source_type_t
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:77
hw_timer_clkdiv_t
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:66
void hw_timer1_write(uint32_t ticks)
Set the timer interval.
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:109
void(* hw_timer_callback_t)(void *arg)
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:64
uint32_t hw_timer_ticks()
Fetch 32-bit microsecond count.
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:40
uint32_t hw_timer1_read(void)
Get timer1 count.
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:132
@ TIMER_LEVEL_INT
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:74
@ TIMER_EDGE_INT
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:73
@ TIMER_FRC1_SOURCE
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:78
@ TIMER_NMI_SOURCE
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:79
@ TIMER_CLKDIV_1
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:67
@ TIMER_CLKDIV_16
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:68
@ TIMER_CLKDIV_256
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:69
Time< T > time(Unit unit, T value)
Helper function to create a Time and deduce the type.
Definition: NanoTime.h:432
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:83
hw_timer_callback_t timer1_callback
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:85
hw_timer_clkdiv_t timer1_clkdiv
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:84
uint32_t timer1_ticks
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:86
void * timer1_arg
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:88
bool timer1_autoload
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:87