1
1
/* mbed Microcontroller Library
2
- * Copyright (c) 2016 ARM Limited
2
+ * Copyright (c) 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.
17
17
#if DEVICE_LPTICKER
18
18
19
19
#include "lp_ticker_api.h"
20
- #include "fsl_snvs_hp.h"
21
20
#include "fsl_gpt.h"
22
21
#include "cmsis.h"
23
- #include "rtc_api.h"
24
22
25
- #define LOWFREQ_REF_CLK_HZ (32768)
23
+ const ticker_info_t * lp_ticker_get_info ()
24
+ {
25
+ static const ticker_info_t info = {
26
+ 32768 , // 32kHz
27
+ 32 // 32 bit counter
28
+ };
29
+ return & info ;
30
+ }
26
31
27
32
static bool lp_ticker_inited = false;
28
33
29
34
static void gpt_isr (void )
30
35
{
31
36
GPT_ClearStatusFlags (GPT2 , kGPT_OutputCompare1Flag );
32
- GPT_StopTimer (GPT2 );
33
-
37
+ GPT_DisableInterrupts (GPT2 , kGPT_OutputCompare1InterruptEnable );
34
38
lp_ticker_irq_handler ();
35
39
}
36
40
@@ -41,71 +45,48 @@ void lp_ticker_init(void)
41
45
{
42
46
gpt_config_t gptConfig ;
43
47
44
- if (lp_ticker_inited ) {
45
- return ;
46
- }
47
- lp_ticker_inited = true;
48
-
49
- /* Setup low resolution clock - RTC */
50
- if (!rtc_isenabled ()) {
51
- rtc_init ();
48
+ if (!lp_ticker_inited ) {
49
+ /* Setup GPT */
50
+ GPT_GetDefaultConfig (& gptConfig );
51
+ /* Use 32kHz drive */
52
+ gptConfig .clockSource = kGPT_ClockSource_LowFreq ;
53
+ gptConfig .enableFreeRun = true;
54
+ gptConfig .enableMode = false;
55
+
56
+ GPT_Init (GPT2 , & gptConfig );
57
+ GPT_EnableInterrupts (GPT2 , kGPT_OutputCompare1InterruptEnable );
58
+ NVIC_ClearPendingIRQ (GPT2_IRQn );
59
+ NVIC_SetVector (GPT2_IRQn , (uint32_t )gpt_isr );
60
+ EnableIRQ (GPT2_IRQn );
61
+ GPT_StartTimer (GPT2 );
62
+ lp_ticker_inited = true;
63
+ } else {
64
+ GPT_DisableInterrupts (GPT2 , kGPT_OutputCompare1InterruptEnable );
52
65
}
53
-
54
- /* Setup GPT */
55
- GPT_GetDefaultConfig (& gptConfig );
56
- /* Use 32kHz drive */
57
- gptConfig .clockSource = kGPT_ClockSource_LowFreq ;
58
- GPT_Init (GPT2 , & gptConfig );
59
- GPT_EnableInterrupts (GPT2 , kGPT_OutputCompare1InterruptEnable );
60
- NVIC_ClearPendingIRQ (GPT2_IRQn );
61
- NVIC_SetVector (GPT2_IRQn , (uint32_t )gpt_isr );
62
- EnableIRQ (GPT2_IRQn );
63
66
}
64
67
65
68
/** Read the current counter
66
69
*
67
- * @return The current timer's counter value in microseconds
70
+ * @return The current timer's counter value in ticks
68
71
*/
69
72
uint32_t lp_ticker_read (void )
70
73
{
71
- uint32_t ticks = 0 ;
72
- uint64_t tmp = 0 ;
73
-
74
- if (!lp_ticker_inited ) {
75
- lp_ticker_init ();
76
- }
77
-
78
- /* Do consecutive reads until value is correct */
79
- do
80
- {
81
- ticks = tmp ;
82
- tmp = SNVS -> HPRTCLR ;
83
- } while (tmp != ticks );
84
-
85
- return COUNT_TO_USEC (ticks , LOWFREQ_REF_CLK_HZ );;
74
+ return GPT_GetCurrentTimerCount (GPT2 );
86
75
}
87
76
88
77
/** Set interrupt for specified timestamp
89
78
*
90
- * @param timestamp The time in microseconds to be set
79
+ * @param timestamp The time in ticks to be set
91
80
*/
92
81
void lp_ticker_set_interrupt (timestamp_t timestamp )
93
82
{
94
- uint32_t now_us , delta_us , delta_ticks ;
95
-
96
- if (!lp_ticker_inited ) {
97
- lp_ticker_init ();
83
+ if (timestamp == 0 ) {
84
+ timestamp = 1 ;
98
85
}
99
86
100
- now_us = lp_ticker_read ();
101
- delta_us = timestamp > now_us ? timestamp - now_us : (uint32_t )((uint64_t )timestamp + 0xFFFFFFFF - now_us );
102
-
103
- delta_ticks = USEC_TO_COUNT (delta_us , LOWFREQ_REF_CLK_HZ );
104
- if (delta_ticks == 0 ) {
105
- delta_ticks = 1 ;
106
- }
107
-
108
- GPT_SetOutputCompareValue (GPT2 , kGPT_OutputCompare_Channel1 , delta_ticks );
87
+ GPT_StopTimer (GPT2 );
88
+ GPT_SetOutputCompareValue (GPT2 , kGPT_OutputCompare_Channel1 , timestamp );
89
+ GPT_ClearStatusFlags (GPT2 , kGPT_OutputCompare1Flag );
109
90
GPT_EnableInterrupts (GPT2 , kGPT_OutputCompare1InterruptEnable );
110
91
GPT_StartTimer (GPT2 );
111
92
}
0 commit comments