1
1
/* mbed Microcontroller Library
2
- * Copyright (c) 2006-2016 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.
20
20
21
21
TIM_HandleTypeDef TimMasterHandle ;
22
22
23
- bool us_ticker_initialized = false;
23
+ uint32_t timer_cnt_reg ;
24
+ uint32_t timer_ccr1_reg ;
25
+ uint32_t timer_dier_reg ;
24
26
25
27
const ticker_info_t * us_ticker_get_info ()
26
28
{
@@ -36,6 +38,9 @@ void us_ticker_irq_handler(void);
36
38
// ************************************ 16-bit timer ************************************
37
39
#if TIM_MST_BIT_WIDTH == 16
38
40
41
+ extern uint32_t prev_time ;
42
+ extern uint32_t elapsed_time ;
43
+
39
44
#if defined(TARGET_STM32F0 )
40
45
void timer_update_irq_handler (void )
41
46
{
@@ -60,32 +65,8 @@ void timer_oc_irq_handler(void)
60
65
}
61
66
}
62
67
63
- // ************************************ 32-bit timer ************************************
64
- #else
65
-
66
- void timer_irq_handler (void )
68
+ void init_16bit_timer (void )
67
69
{
68
- TimMasterHandle .Instance = TIM_MST ;
69
- if (__HAL_TIM_GET_FLAG (& TimMasterHandle , TIM_FLAG_CC1 ) == SET ) {
70
- if (__HAL_TIM_GET_IT_SOURCE (& TimMasterHandle , TIM_IT_CC1 ) == SET ) {
71
- __HAL_TIM_CLEAR_IT (& TimMasterHandle , TIM_IT_CC1 );
72
- us_ticker_irq_handler ();
73
- }
74
- }
75
- }
76
-
77
- #endif // 16-bit/32-bit timer
78
-
79
- void us_ticker_init (void )
80
- {
81
- if (us_ticker_initialized ) {
82
- __HAL_TIM_DISABLE_IT (& TimMasterHandle , TIM_IT_CC1 );
83
- __HAL_TIM_CLEAR_FLAG (& TimMasterHandle , TIM_FLAG_CC1 );
84
- return ;
85
- }
86
-
87
- // ************************************ 16-bit timer ************************************
88
- #if TIM_MST_BIT_WIDTH == 16
89
70
// Enable timer clock
90
71
TIM_MST_RCC ;
91
72
@@ -137,9 +118,27 @@ void us_ticker_init(void)
137
118
138
119
__HAL_TIM_DISABLE_IT (& TimMasterHandle , TIM_IT_CC1 );
139
120
121
+ // Used by HAL_GetTick()
122
+ prev_time = 0 ;
123
+ elapsed_time = 0 ;
124
+ }
125
+
140
126
// ************************************ 32-bit timer ************************************
141
127
#else
142
128
129
+ void timer_irq_handler (void )
130
+ {
131
+ TimMasterHandle .Instance = TIM_MST ;
132
+ if (__HAL_TIM_GET_FLAG (& TimMasterHandle , TIM_FLAG_CC1 ) == SET ) {
133
+ if (__HAL_TIM_GET_IT_SOURCE (& TimMasterHandle , TIM_IT_CC1 ) == SET ) {
134
+ __HAL_TIM_CLEAR_IT (& TimMasterHandle , TIM_IT_CC1 );
135
+ us_ticker_irq_handler ();
136
+ }
137
+ }
138
+ }
139
+
140
+ void init_32bit_timer (void )
141
+ {
143
142
RCC_ClkInitTypeDef RCC_ClkInitStruct ;
144
143
uint32_t PclkFreq ;
145
144
@@ -162,8 +161,8 @@ void us_ticker_init(void)
162
161
TIM_MST_RESET_OFF ;
163
162
164
163
// Configure time base
165
- TimMasterHandle .Instance = TIM_MST ;
166
- TimMasterHandle .Init .Period = 0xFFFFFFFF ;
164
+ TimMasterHandle .Instance = TIM_MST ;
165
+ TimMasterHandle .Init .Period = 0xFFFFFFFF ;
167
166
168
167
// TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx
169
168
#if TIM_MST_PCLK == 1
@@ -199,11 +198,14 @@ void us_ticker_init(void)
199
198
#endif
200
199
201
200
__HAL_TIM_DISABLE_IT (& TimMasterHandle , TIM_IT_CC1 );
201
+ }
202
202
203
203
#endif // 16-bit/32-bit timer
204
204
205
- us_ticker_initialized = true;
206
-
205
+ void us_ticker_init (void )
206
+ {
207
+ // Timer is already initialized in HAL_InitTick()
208
+ __HAL_TIM_DISABLE_IT (& TimMasterHandle , TIM_IT_CC1 );
207
209
}
208
210
209
211
uint32_t us_ticker_read ()
@@ -241,10 +243,6 @@ void us_ticker_clear_interrupt(void)
241
243
__HAL_TIM_CLEAR_FLAG (& TimMasterHandle , TIM_FLAG_CC1 );
242
244
}
243
245
244
- uint32_t timer_cnt_reg ;
245
- uint32_t timer_ccr1_reg ;
246
- uint32_t timer_dier_reg ;
247
-
248
246
void save_timer_ctx (void )
249
247
{
250
248
timer_cnt_reg = __HAL_TIM_GET_COUNTER (& TimMasterHandle );
@@ -257,4 +255,4 @@ void restore_timer_ctx(void)
257
255
__HAL_TIM_SET_COUNTER (& TimMasterHandle , timer_cnt_reg );
258
256
__HAL_TIM_SET_COMPARE (& TimMasterHandle , TIM_CHANNEL_1 , timer_ccr1_reg );
259
257
TIM_MST -> DIER = timer_dier_reg ;
260
- }
258
+ }
0 commit comments