1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
31
32
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
70
71
72
73
77
78
84
85
86
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
148
149
150
151
152
153
158
159
160
161
162
163
164
165
166
167
172
173
174
175
176
181
182
183
184
185
186
187
188
193
194
195
200
201
202
207
208
211
212
213
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
292
293
294
295
296
297
298
299
300
301
302
304
305
306
311
312
313
318
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
358
359
360
361
362
363
364
365
366
367
368
369
373
374
376
377
378
379
380
381
385
386
387
388
407
408
409
412
413
/* ... */
#include "main.h"
/* ... */
/* ... */
Includes
#define TIM_FREQUENCIES_NB 6
#define TIM_DUTYCYCLE_NB 2
#define TIM3_ARR_MAX (uint32_t)0xFFFF
Private define
TIM_HandleTypeDef htim3;
TIM_HandleTypeDef htim2;
TIM_IC_InitTypeDef sConfig;
TIM_SlaveConfigTypeDef sSlaveConfig;
__IO uint32_t uwIC2Value = 0;
__IO uint32_t uwDutyCycle = 0;
__IO uint32_t uwFrequency = 0;
uint32_t uhPrescalerValue = 0;
static uint8_t iFrequency = 0;
static uint32_t aFrequency[TIM_FREQUENCIES_NB] = {
2000,
2000,
3000,
3000,
4000,
4000,
...};
static uint8_t iDutyCycle = 0;
static uint32_t aDutyCycle[TIM_DUTYCYCLE_NB] = {
2,
4,
...};
Private variables
static void SystemClock_Config(void);
static void Error_Handler(void);
static void UserButton_Init(void);
static void WaveGeneration_Init(void);
Private function prototypes
/* ... */
int main(void)
{
/* ... */
HAL_Init();
SystemClock_Config();
UserButton_Init();
WaveGeneration_Init();
BSP_LED_Init(LED2);
if (HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}if (HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1) != HAL_OK) { ... }
/* ... */
htim3.Instance = TIMx;
/* ... */
htim3.Init.Period = 0xFFFF;
htim3.Init.Prescaler = 0;
htim3.Init.ClockDivision = 0;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
if (HAL_TIM_IC_Init(&htim3) != HAL_OK)
{
Error_Handler();
}if (HAL_TIM_IC_Init(&htim3) != HAL_OK) { ... }
sConfig.ICPrescaler = TIM_ICPSC_DIV1;
sConfig.ICFilter = 0;
sConfig.ICPolarity = TIM_ICPOLARITY_FALLING;
sConfig.ICSelection = TIM_ICSELECTION_INDIRECTTI;
if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfig, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfig, TIM_CHANNEL_1) != HAL_OK) { ... }
sConfig.ICPolarity = TIM_ICPOLARITY_RISING;
sConfig.ICSelection = TIM_ICSELECTION_DIRECTTI;
if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfig, TIM_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfig, TIM_CHANNEL_2) != HAL_OK) { ... }
sSlaveConfig.SlaveMode = TIM_SLAVEMODE_RESET;
sSlaveConfig.InputTrigger = TIM_TS_TI2FP2;
sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_NONINVERTED;
sSlaveConfig.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1;
sSlaveConfig.TriggerFilter = 0;
if (HAL_TIM_SlaveConfigSynchronization(&htim3, &sSlaveConfig) != HAL_OK)
{
Error_Handler();
}if (HAL_TIM_SlaveConfigSynchronization(&htim3, &sSlaveConfig) != HAL_OK) { ... }
if (HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}if (HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2) != HAL_OK) { ... }
if (HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}if (HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_1) != HAL_OK) { ... }
while (1)
{
}while (1) { ... }
}{ ... }
/* ... */
void WaveGeneration_Init(void)
{
TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC;
htim2.Instance = TIM2;
htim2.Init.Prescaler = uhPrescalerValue;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = (SystemCoreClock/1)/aFrequency[0];
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_PWM_Init(&htim2);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = ((SystemCoreClock/1)/aFrequency[0])/aDutyCycle[0];
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
}{ ... }
/* ... */
void UserButton_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOC_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 2, 0);
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
}{ ... }
/* ... */
void UserButton_Callback()
{
iFrequency = (iFrequency + 1) % TIM_FREQUENCIES_NB;
iDutyCycle = (iDutyCycle + 1) % TIM_DUTYCYCLE_NB;
LL_TIM_SetAutoReload(TIM2, __LL_TIM_CALC_ARR(SystemCoreClock/1, LL_TIM_GetPrescaler(TIM2), aFrequency[iFrequency]));
LL_TIM_OC_SetCompareCH1(TIM2, (LL_TIM_GetAutoReload(TIM2) / aDutyCycle[iDutyCycle]));
}{ ... }
/* ... */
void TimerCaptureCompare_Ch2_Callback()
{
uwIC2Value = LL_TIM_IC_GetCaptureCH2(TIM3);
if (uwIC2Value != 0)
{
uwDutyCycle = (LL_TIM_IC_GetCaptureCH1(TIM3) * 100) / uwIC2Value;
/* ... */
uwFrequency = SystemCoreClock / (1*uwIC2Value);
}if (uwIC2Value != 0) { ... }
else
{
uwDutyCycle = 0;
uwFrequency = 0;
}else { ... }
}{ ... }
/* ... */
static void Error_Handler(void)
{
BSP_LED_On(LED2);
while (1)
{
}while (1) { ... }
}{ ... }
/* ... */
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
__HAL_RCC_PWR_CLK_ENABLE();
/* ... */
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 400;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 7;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { ... }
/* ... */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
{
Error_Handler();
}if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) { ... }
}{ ... }
#ifdef USE_FULL_ASSERT
/* ... */
void assert_failed(uint8_t *file, uint32_t line)
{
/* ... */
while (1)
{
}while (1) { ... }
}assert_failed (uint8_t *file, uint32_t line) { ... }
/* ... */
#endif
/* ... */
/* ... */