@@ -53,11 +53,18 @@ const ticker_info_t* us_ticker_get_info()
53
53
return & info ;
54
54
}
55
55
56
- const uint8_t alarm_num = 0 ;
56
+ static const uint8_t alarm_num = 0 ;
57
+
58
+ static void us_ticker_irq_handler_internal (uint alarm_src ) {
59
+ if (alarm_num == alarm_src ) {
60
+ us_ticker_irq_handler ();
61
+ }
62
+ }
57
63
58
64
void us_ticker_init (void )
59
65
{
60
- hardware_alarm_set_callback (alarm_num , us_ticker_irq_handler );
66
+ hardware_alarm_claim (alarm_num );
67
+ hardware_alarm_set_callback (alarm_num , us_ticker_irq_handler_internal );
61
68
}
62
69
63
70
uint32_t us_ticker_read ()
@@ -68,8 +75,25 @@ uint32_t us_ticker_read()
68
75
void us_ticker_set_interrupt (timestamp_t timestamp )
69
76
{
70
77
core_util_critical_section_enter ();
71
- absolute_time_t target = {timestamp };
78
+
79
+ uint64_t _timestamp = (uint64_t )timestamp ;
80
+
81
+ if (timestamp < time_us_32 ()) {
82
+ //32 bit timestamp has been wrapped
83
+ //We need to provide a 64 bit timestamp able to fire the irq for this round
84
+ _timestamp = (((time_us_64 () >> 32 ) + 1 ) << 32 ) + timestamp ;
85
+ } else {
86
+ //Then, at the next round, wrap the 64 bit timer to follow the 32 bit one
87
+ if ((time_us_64 () >> 32 ) > 0 ) {
88
+ uint64_t current_time = time_us_64 ();
89
+ uint64_t wrapped_time = current_time - 0xFFFFFFFF ;
90
+ timer_hw -> timelw = (uint32_t )wrapped_time ;
91
+ timer_hw -> timehw = 0 ;
92
+ }
93
+ }
94
+ absolute_time_t target = { _timestamp };
72
95
hardware_alarm_set_target (alarm_num , target );
96
+
73
97
core_util_critical_section_exit ();
74
98
}
75
99
@@ -90,4 +114,5 @@ void us_ticker_clear_interrupt(void)
90
114
91
115
void us_ticker_free (void )
92
116
{
117
+ hardware_alarm_unclaim (alarm_num );
93
118
}
0 commit comments