1
1
/* mbed Microcontroller Library
2
- * Copyright (c) 2006-2013 ARM Limited
2
+ * Copyright (c) 2006-2018 ARM Limited
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
19
19
#include "fsl_pit.h"
20
20
#include "fsl_clock_config.h"
21
21
22
- static int us_ticker_inited = 0 ;
22
+ const ticker_info_t * us_ticker_get_info ()
23
+ {
24
+ static const ticker_info_t info = {
25
+ 1000000 , // 1 MHz
26
+ 32 // 32 bit counter
27
+ };
28
+ return & info ;
29
+ }
30
+
31
+ static bool us_ticker_inited = false;
23
32
24
33
static void pit_isr (void )
25
34
{
@@ -31,72 +40,98 @@ static void pit_isr(void)
31
40
us_ticker_irq_handler ();
32
41
}
33
42
43
+ /** Initialize the high frequency ticker
44
+ *
45
+ */
34
46
void us_ticker_init (void )
35
47
{
36
- if (us_ticker_inited ) {
37
- return ;
38
- }
39
- us_ticker_inited = 1 ;
40
- //Common for ticker/timer
48
+ /* Common for ticker/timer. */
41
49
uint32_t busClock ;
42
- // Structure to initialize PIT
50
+ /* Structure to initialize PIT. */
43
51
pit_config_t pitConfig ;
44
52
45
53
PIT_GetDefaultConfig (& pitConfig );
46
54
PIT_Init (PIT , & pitConfig );
47
55
48
56
busClock = CLOCK_GetFreq (kCLOCK_BusClk );
49
57
50
- //Timer
51
- PIT_SetTimerPeriod (PIT , kPIT_Chnl_0 , busClock / 1000000 - 1 );
52
- PIT_SetTimerPeriod (PIT , kPIT_Chnl_1 , 0xFFFFFFFF );
53
- PIT_SetTimerChainMode (PIT , kPIT_Chnl_1 , true);
54
- PIT_StartTimer (PIT , kPIT_Chnl_0 );
55
- PIT_StartTimer (PIT , kPIT_Chnl_1 );
58
+ /* Let the timer to count if re-init. */
59
+ if (!us_ticker_inited ) {
56
60
57
- //Ticker
61
+ PIT_SetTimerPeriod (PIT , kPIT_Chnl_0 , busClock / 1000000 - 1 );
62
+ PIT_SetTimerPeriod (PIT , kPIT_Chnl_1 , 0xFFFFFFFF );
63
+ PIT_SetTimerChainMode (PIT , kPIT_Chnl_1 , true);
64
+ PIT_StartTimer (PIT , kPIT_Chnl_0 );
65
+ PIT_StartTimer (PIT , kPIT_Chnl_1 );
66
+ }
67
+
68
+ /* Configure interrupt generation counters and disable ticker interrupts. */
69
+ PIT_StopTimer (PIT , kPIT_Chnl_3 );
70
+ PIT_StopTimer (PIT , kPIT_Chnl_2 );
58
71
PIT_SetTimerPeriod (PIT , kPIT_Chnl_2 , busClock / 1000000 - 1 );
59
72
PIT_SetTimerChainMode (PIT , kPIT_Chnl_3 , true);
60
- NVIC_SetVector (PIT3_IRQn , (uint32_t )pit_isr );
73
+ NVIC_SetVector (PIT3_IRQn , (uint32_t ) pit_isr );
61
74
NVIC_EnableIRQ (PIT3_IRQn );
62
- }
75
+ PIT_DisableInterrupts ( PIT , kPIT_Chnl_3 , kPIT_TimerInterruptEnable );
63
76
77
+ us_ticker_inited = true;
78
+ }
64
79
80
+ /** Read the current counter
81
+ *
82
+ * @return The current timer's counter value in ticks
83
+ */
65
84
uint32_t us_ticker_read ()
66
85
{
67
- if (!us_ticker_inited ) {
68
- us_ticker_init ();
69
- }
70
-
71
86
return ~(PIT_GetCurrentTimerCount (PIT , kPIT_Chnl_1 ));
72
87
}
73
88
89
+ /** Disable us ticker interrupt
90
+ *
91
+ */
74
92
void us_ticker_disable_interrupt (void )
75
93
{
76
94
PIT_DisableInterrupts (PIT , kPIT_Chnl_3 , kPIT_TimerInterruptEnable );
77
95
}
78
96
97
+ /** Clear us ticker interrupt
98
+ *
99
+ */
79
100
void us_ticker_clear_interrupt (void )
80
101
{
81
102
PIT_ClearStatusFlags (PIT , kPIT_Chnl_3 , PIT_TFLG_TIF_MASK );
82
103
}
83
104
105
+ /** Set interrupt for specified timestamp
106
+ *
107
+ * @param timestamp The time in ticks when interrupt should be generated
108
+ */
84
109
void us_ticker_set_interrupt (timestamp_t timestamp )
85
110
{
86
- uint32_t now_us , delta_us ;
87
-
88
- now_us = us_ticker_read ();
89
- delta_us = timestamp >= now_us ? timestamp - now_us : (uint32_t )((uint64_t )timestamp + 0xFFFFFFFF - now_us );
111
+ /* We get here absolute interrupt time which takes into account counter overflow.
112
+ * Since we use additional count-down timer to generate interrupt we need to calculate
113
+ * load value based on time-stamp.
114
+ */
115
+ const uint32_t now_ticks = us_ticker_read ();
116
+ uint32_t delta_ticks =
117
+ timestamp >= now_ticks ? timestamp - now_ticks : (uint32_t )((uint64_t ) timestamp + 0xFFFFFFFF - now_ticks );
118
+
119
+ if (delta_ticks == 0 ) {
120
+ /* The requested delay is less than the minimum resolution of this counter. */
121
+ delta_ticks = 1 ;
122
+ }
90
123
91
- uint32_t delta = timestamp - us_ticker_read ();
92
124
PIT_StopTimer (PIT , kPIT_Chnl_3 );
93
125
PIT_StopTimer (PIT , kPIT_Chnl_2 );
94
- PIT_SetTimerPeriod (PIT , kPIT_Chnl_3 , ( uint32_t ) delta_us );
126
+ PIT_SetTimerPeriod (PIT , kPIT_Chnl_3 , delta_ticks );
95
127
PIT_EnableInterrupts (PIT , kPIT_Chnl_3 , kPIT_TimerInterruptEnable );
96
128
PIT_StartTimer (PIT , kPIT_Chnl_3 );
97
129
PIT_StartTimer (PIT , kPIT_Chnl_2 );
98
130
}
99
131
132
+ /** Fire us ticker interrupt
133
+ *
134
+ */
100
135
void us_ticker_fire_interrupt (void )
101
136
{
102
137
NVIC_SetPendingIRQ (PIT3_IRQn );
0 commit comments