-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTIM_ex.c
More file actions
291 lines (242 loc) · 7.05 KB
/
TIM_ex.c
File metadata and controls
291 lines (242 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*!\file TIM_ex.c
** \author SMFSW
** \copyright MIT (c) 2017-2026, SMFSW
** \brief Extensions for TIM peripherals
** \warning Init functions assume that TIM peripherals instance were already configured by HAL
** \warning Shall work for all STM32 C/F/G/H/L/U families only (yet)
** \note \c TIM_MIN_GRANULARITY can defined at project level to tweak to needed minimum granularity
**/
/****************************************************************/
#include "sarmfsw.h"
#include "TIM_ex.h"
#if defined(HAL_TIM_MODULE_ENABLED)
/****************************************************************/
#ifndef TIM_MIN_GRANULARITY
#define TIM_MIN_GRANULARITY 255UL //!< Minimum granularity for TIM channel
//!\note \c TIM_MIN_GRANULARITY can defined at project level to tweak to needed minimum granularity
#endif
uint32_t NONNULL__ RCC_TIMCLKFreq(const TIM_HandleTypeDef * const pTim)
{
uint32_t refCLK;
#if defined(STM32C0) || defined(STM32G0) || defined(STM32U0)
refCLK = HAL_RCC_GetPCLK1Freq();
if ((RCC->CFGR & RCC_CFGR_PPRE) != 0U) { refCLK *= 2UL; }
#elif defined(STM32F0)
refCLK = HAL_RCC_GetPCLK1Freq();
#else
bool notAPB1;
switch ((uint32_t) pTim->Instance)
{
default:
{
notAPB1 = false;
}
break;
#if defined(TIM1)
case TIM1_BASE:
#endif
#if defined(TIM8)
case TIM8_BASE:
#endif
#if defined(TIM9)
case TIM9_BASE:
#endif
#if defined(TIM10)
case TIM10_BASE:
#endif
#if defined(TIM11)
case TIM11_BASE:
#endif
#if defined(TIM15)
case TIM15_BASE:
#endif
#if defined(TIM16)
case TIM16_BASE:
#endif
#if defined(TIM17)
case TIM17_BASE:
#endif
#if defined(TIM21)
case TIM21_BASE: //!< L0 family (TIM on APB2)
#endif
#if defined(TIM22)
case TIM22_BASE: //!< L0 family (TIM on APB2)
#endif
{
notAPB1 = true; // cppcheck-suppress unreachableCode ; successive #if defined() leads to a case where none are defined for cppcheck
}
break;
}
if (notAPB1)
{
#if defined(STM32F3)
if ( ((pTim->Instance == TIM1) && (RCC->CFGR3 & RCC_CFGR3_TIM1SW_PLL))
#if defined(RCC_CFGR3_TIM15SW_PLL)
|| ((pTim->Instance == TIM15) && (RCC->CFGR3 & RCC_CFGR3_TIM15SW_PLL))
#endif
#if defined(RCC_CFGR3_TIM16SW_PLL)
|| ((pTim->Instance == TIM16) && (RCC->CFGR3 & RCC_CFGR3_TIM16SW_PLL))
#endif
#if defined(RCC_CFGR3_TIM17SW_PLL)
|| ((pTim->Instance == TIM17) && (RCC->CFGR3 & RCC_CFGR3_TIM17SW_PLL))
#endif
)
{ // Get SYCLK (HCLK) frequency
refCLK = HAL_RCC_GetHCLKFreq() * 2UL;
}
else
#endif
{
// Get APB2 (PCLK2) frequency
refCLK = HAL_RCC_GetPCLK2Freq();
#if defined(STM32H7)
if ((RCC->D2CFGR & RCC_D2CFGR_D2PPRE2) != 0U)
#elif defined(STM32U3) || defined(STM32U5)
if ((RCC->CFGR2 & RCC_CFGR2_PPRE2) != 0U)
#else
if ((RCC->CFGR & RCC_CFGR_PPRE2) != 0U)
#endif
{
refCLK *= 2UL;
}
}
}
else
{
// Get APB1 (PCLK1) frequency
refCLK = HAL_RCC_GetPCLK1Freq();
#if defined(STM32H7)
if ((RCC->D2CFGR & RCC_D2CFGR_D2PPRE1) != 0U)
#elif defined(STM32U3) || defined(STM32U5)
if ((RCC->CFGR2 & RCC_CFGR2_PPRE1) != 0U)
#else
if ((RCC->CFGR & RCC_CFGR_PPRE1) != 0U)
#endif
{
refCLK *= 2UL;
}
}
#endif
return refCLK;
}
HAL_StatusTypeDef NONNULL__ write_TIM_Preload(TIM_HandleTypeDef * const pTim, const uint32_t chan)
{
HAL_StatusTypeDef st = HAL_OK;
switch (chan) /* Set the pre-load enable bit for channel */
{
default:
st = HAL_ERROR;
break;
case TIM_CHANNEL_1:
pTim->Instance->CCMR1 |= TIM_CCMR1_OC1PE;
break;
case TIM_CHANNEL_2:
pTim->Instance->CCMR1 |= TIM_CCMR1_OC2PE;
break;
case TIM_CHANNEL_3:
pTim->Instance->CCMR2 |= TIM_CCMR2_OC3PE;
break;
case TIM_CHANNEL_4:
pTim->Instance->CCMR2 |= TIM_CCMR2_OC4PE;
break;
#if defined(TIM_CHANNEL_5)
case TIM_CHANNEL_5:
pTim->Instance->CCMR3 |= TIM_CCMR3_OC5PE;
break;
#endif
#if defined(TIM_CHANNEL_6)
case TIM_CHANNEL_6:
pTim->Instance->CCMR3 |= TIM_CCMR3_OC6PE;
break;
#endif
}
return st;
}
HAL_StatusTypeDef NONNULL__ write_TIM_CCR(const TIM_HandleTypeDef * const pTim, const uint32_t chan, const uint32_t CCR_val)
{
HAL_StatusTypeDef st = HAL_OK;
__IO uint32_t * pCCR = NULL;
assert_param(IS_TIM_INSTANCE(pTim->Instance));
assert_param(IS_TIM_CCX_INSTANCE(pTim->Instance, chan));
if (chan <= TIM_CHANNEL_4) { pCCR = &pTim->Instance->CCR1 + (chan / 4UL); }
#if defined(TIM_CHANNEL_6)
else if (chan <= TIM_CHANNEL_6) { pCCR = &pTim->Instance->CCR5 + (chan / 4UL) - 4UL; }
#endif
else { st = HAL_ERROR; }
if (pCCR != NULL) { *pCCR = CCR_val; }
return st;
}
HAL_StatusTypeDef NONNULL__ init_TIM_Base(TIM_HandleTypeDef * const pTim, const uint32_t freq)
{
HAL_StatusTypeDef st;
st = set_TIM_Interrupts(pTim, Off); // Stop interrupts if they were already started
st |= set_TIM_Freq(pTim, freq); // Configure TIM frequency
if (st == HAL_OK)
{
st = set_TIM_Interrupts(pTim, On); // Start interrupts
}
return st;
}
HAL_StatusTypeDef NONNULL__ set_TIM_Freq(TIM_HandleTypeDef * const pTim, const uint32_t freq)
{
HAL_StatusTypeDef st = HAL_ERROR;
if (freq != 0UL) // Avoid div by 0
{
const uint32_t max_prescaler = 0xFFFFUL; // Prescaler is 16b no redeeming timer instance
uint32_t max_period = 0xFFFFUL; // Max period for 16b timers (most common)
uint64_t period; // For 32b timers, period needs to be 64b large for calculations
uint32_t prescaler;
// TODO: init freq tests for 32b timers (init time may take some time)
if ( (pTim->Instance == TIM2)
#if defined(TIM5)
|| (pTim->Instance == TIM5)
#endif
) { max_period = 0xFFFFFFFFUL; } // Max period for 32b timers
const uint32_t refCLK = RCC_TIMCLKFreq(pTim);
if (freq > (refCLK / TIM_MIN_GRANULARITY)) // To guarantee minimum step range
{
st = HAL_ERROR;
}
else
{
for (prescaler = 0UL ; prescaler <= max_prescaler ; prescaler++)
{
period = refCLK;
period /= (uint64_t) freq * (prescaler + 1UL);
period -= 1UL;
if (period <= max_period) { break; } // If in range
}
if (prescaler == (max_prescaler + 1UL)) // If nothing has been found (after last iteration)
{
st = HAL_ERROR;
}
else
{
pTim->Init.Period = period;
pTim->Init.Prescaler = prescaler;
st = HAL_TIM_Base_Init(pTim);
}
}
}
return st;
}
HAL_StatusTypeDef NONNULL__ set_TIM_Tick_Freq(TIM_HandleTypeDef * const pTim, const uint32_t freq)
{
HAL_StatusTypeDef st;
if (freq == 0) { st = HAL_ERROR; } // Avoid div by 0
else
{
if ( (pTim->Instance == TIM2)
#if defined(TIM5)
|| (pTim->Instance == TIM5)
#endif
) { pTim->Init.Period = 0xFFFFFFFFUL; } // Set to max period for 32b timers
else { pTim->Init.Period = 0xFFFFUL; } // Set to max period for 16b timers
pTim->Init.Prescaler = (RCC_TIMCLKFreq(pTim) / freq) - 1UL; // Get prescaler value adjusted to desired frequency
st = HAL_TIM_Base_Init(pTim);
}
return st;
}
/****************************************************************/
#endif /* defined(HAL_TIM_MODULE_ENABLED) */
/****************************************************************/