14
14
* limitations under the License.
15
15
*/
16
16
#include "us_ticker_api.h"
17
+ #include "mbed_critical.h"
17
18
18
19
#define TMR16A_100US 0x960 // fsys = fc = 24MHz, Ttmra = 1/24us, 100us*24us = 2400 = 0x960
19
20
#define TMR16A_SYSCK ((uint32_t)0x00000001)
20
21
#define TMR16A_RUN ((uint32_t)0x00000001)
21
22
#define TMR16A_STOP ((uint32_t)0x00000000)
23
+ #define OVERFLOW_32BIT (0xFFFFFFFF / 0x64)
22
24
23
- static uint8_t us_ticker_inited = 0 ; // Is ticker initialized yet?
24
- static volatile uint32_t acc_us_ticker = 0 ;
25
-
26
- // 16Bb high timer counter
27
- static volatile uint32_t us_ticker_16h = 0 ;
25
+ static uint8_t us_ticker_inited = 0 ; // Is ticker initialized yet?
26
+ static volatile uint32_t ticker_int_counter = 0 ; // Amount of overflows until user interrupt
27
+ static volatile uint32_t us_ticker = 0 ; // timer counter
28
28
29
29
void INT16A0_IRQHandler (void )
30
30
{
31
- us_ticker_16h ++ ;
32
- if ( us_ticker_16h >= 0xFFFF ) {
33
- acc_us_ticker ++ ;
34
- us_ticker_16h = 0 ;
31
+ us_ticker ++ ;
32
+
33
+ if ( us_ticker > OVERFLOW_32BIT ) {
34
+ us_ticker = 0 ;
35
35
}
36
36
}
37
37
@@ -67,18 +67,30 @@ uint32_t us_ticker_read(void)
67
67
if (!us_ticker_inited ) {
68
68
us_ticker_init ();
69
69
}
70
- ret_val = (((acc_us_ticker << 16 ) + us_ticker_16h ) * 100 );
70
+
71
+ uint32_t tickerbefore = 0 ;
72
+ do {
73
+ tickerbefore = us_ticker ;
74
+ ret_val = (us_ticker * 100 );
75
+ } while (tickerbefore != us_ticker );
76
+
71
77
return ret_val ;
72
78
}
73
79
74
80
void us_ticker_set_interrupt (timestamp_t timestamp )
75
81
{
76
82
int delta = 0 ;
83
+
77
84
// Stops and clear count operation
78
85
TSB_T16A1 -> RUN = TMR16A_STOP ;
79
86
TSB_T16A1 -> CR = TMR16A_SYSCK ;
80
87
// Set the compare register
81
88
delta = (int )(timestamp - us_ticker_read ());
89
+ if (delta < 0 ) {
90
+ // Ticker interrupt handle
91
+ us_ticker_irq_handler ();
92
+ return ;
93
+ }
82
94
TSB_T16A1 -> RG = delta ;
83
95
// Set Interrupt
84
96
NVIC_EnableIRQ (INT16A1_IRQn );
@@ -98,5 +110,5 @@ void us_ticker_disable_interrupt(void)
98
110
99
111
void us_ticker_clear_interrupt (void )
100
112
{
101
- //no flags to clear
113
+ NVIC_ClearPendingIRQ ( INT16A1_IRQn );
102
114
}
0 commit comments