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/structs/timer.h>
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 #define HW_TIMER_BASE_CLK 1000000U
21 
37 __forceinline uint32_t IRAM_ATTR hw_timer_ticks()
38 {
39  return timer_hw->timerawl;
40 }
41 
42 /*************************************
43  *
44  * Timer1 (countdown alarm)
45  *
46  *************************************/
47 
51 #define MAX_HW_TIMER1_INTERVAL 0x7fffffffU
52 
59 #define MIN_HW_TIMER1_INTERVAL_US 50U
60 
61 typedef void (*hw_timer_callback_t)(void* arg);
62 
63 typedef enum {
68 
69 typedef enum {
70  TIMER_EDGE_INT, // edge interrupt
71  TIMER_LEVEL_INT, // level interrupt
73 
74 typedef enum {
78 
79 // Internal data
83  uint32_t timer1_ticks;
85  void* timer1_arg;
86 };
87 
89 
96 void hw_timer1_attach_interrupt(hw_timer_source_type_t source_type, hw_timer_callback_t callback, void* arg);
97 
102 
109 void hw_timer1_enable(hw_timer_clkdiv_t div, hw_timer_intr_type_t intr_type, bool auto_load);
110 
115 __forceinline void IRAM_ATTR hw_timer1_write(uint32_t ticks)
116 {
119  timer_hw->alarm[0] = hw_timer_ticks() + ticks;
120 }
121 
125 __forceinline void IRAM_ATTR hw_timer1_disable()
126 {
127  timer_hw->armed = BIT(0);
128 }
129 
134 __forceinline uint32_t hw_timer1_read()
135 {
136  int time = hw_timer_ticks() - timer_hw->alarm[0];
137  return (time > 0) ? (time >> hw_timer_private.timer1_clkdiv) : 0;
138 }
139 
140 /*************************************
141  *
142  * FRC2 timer
143  *
144  * This is a 32-bit count-up timer.
145  * Used for software timers - see os_timer.h
146  *
147  *************************************/
148 
149 #define HW_TIMER2_CLK HW_TIMER_BASE_CLK
150 
155 __forceinline uint32_t hw_timer2_read()
156 {
157  return hw_timer_ticks();
158 }
159 
162 #ifdef __cplusplus
163 }
164 #endif
#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:147
void hw_timer1_detach_interrupt(void)
Detach interrupt from the timer.
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:126
hw_timer_intr_type_t
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:69
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:74
hw_timer_clkdiv_t
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:63
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:61
uint32_t hw_timer_ticks()
Fetch 32-bit microsecond count.
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:37
uint32_t hw_timer1_read(void)
Get timer1 count.
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:137
@ TIMER_LEVEL_INT
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:71
@ TIMER_EDGE_INT
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:70
@ TIMER_FRC1_SOURCE
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:75
@ TIMER_NMI_SOURCE
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:76
@ TIMER_CLKDIV_1
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:64
@ TIMER_CLKDIV_16
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:65
@ TIMER_CLKDIV_256
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:66
Time< T > time(Unit unit, T value)
Helper function to create a Time and deduce the type.
Definition: NanoTime.h:423
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:80
hw_timer_callback_t timer1_callback
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:82
hw_timer_clkdiv_t timer1_clkdiv
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:81
uint32_t timer1_ticks
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:83
void * timer1_arg
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:85
bool timer1_autoload
Definition: Rp2040/Components/driver/include/driver/hw_timer.h:84