1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
32
33
37
38
39
40
45
46
47
48
49
50
51
52
53
54
55
62
63
64
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
87
88
89
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
118
119
120
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
175
176
177
178
179
180
181
182
183
184
185
186
187
203
204
205
206
207
208
209
210
211
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
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
284
285
286
287
288
289
290
291
294
295
296
297
298
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
321
322
323
324
325
326
327
328
329
330
337
347
348
349
368
369
372
373
/* ... */
#include "main.h"
/* ... */
/* ... */
Includes
#define TIM_OC_MODES_NB 3
Private define
static uint32_t aOCMode[TIM_OC_MODES_NB] = {
LL_TIM_OCMODE_TOGGLE,
LL_TIM_OCMODE_ACTIVE,
LL_TIM_OCMODE_INACTIVE
...};
static uint8_t iOCMode = 0;
static uint32_t uwCompareMatchCount = 0;
static uint32_t TimOutClock = 1;
Private variables
__STATIC_INLINE void SystemClock_Config(void);
__STATIC_INLINE void Configure_TIMOutputCompare(void);
__STATIC_INLINE void Configure_OCMode(uint32_t OCMode);
__STATIC_INLINE void LED_Init(void);
__STATIC_INLINE void UserButton_Init(void);
Private function prototypes
/* ... */
int main(void)
{
SystemClock_Config();
LED_Init();
UserButton_Init();
Configure_TIMOutputCompare();
while (1)
{
}while (1) { ... }
}{ ... }
/* ... */
__STATIC_INLINE void Configure_TIMOutputCompare(void)
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_5, LL_GPIO_MODE_ALTERNATE);
LL_GPIO_SetPinPull(GPIOA, LL_GPIO_PIN_5, LL_GPIO_PULL_DOWN);
LL_GPIO_SetPinSpeed(GPIOA, LL_GPIO_PIN_5, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetAFPin_0_7(GPIOA, LL_GPIO_PIN_5, LL_GPIO_AF_1);
...
NVIC_SetPriority(TIM2_IRQn, 0);
NVIC_EnableIRQ(TIM2_IRQn);
...
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
...
LL_TIM_SetPrescaler(TIM2, __LL_TIM_CALC_PSC(SystemCoreClock, 10000));
TimOutClock = SystemCoreClock/2;
LL_TIM_SetAutoReload(TIM2, __LL_TIM_CALC_ARR(TimOutClock, LL_TIM_GetPrescaler(TIM2), 10));
...
LL_TIM_OC_SetMode(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_OCMODE_TOGGLE);
LL_TIM_OC_SetPolarity(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_OCPOLARITY_HIGH);
LL_TIM_OC_SetCompareCH1(TIM2, (LL_TIM_GetAutoReload(TIM2) / 2));
...
LL_TIM_EnableIT_CC1(TIM2);
...
LL_TIM_CC_EnableChannel(TIM2, LL_TIM_CHANNEL_CH1);
LL_TIM_EnableCounter(TIM2);
LL_TIM_GenerateEvent_UPDATE(TIM2);
}{ ... }
/* ... */
__STATIC_INLINE void Configure_OCMode(uint32_t OCMode)
{
LL_TIM_DisableCounter(TIM2);
LL_TIM_SetCounter(TIM2, 0);
uwCompareMatchCount = 0;
switch (OCMode)
{
case LL_TIM_OCMODE_TOGGLE:
case LL_TIM_OCMODE_ACTIVE:
LL_TIM_OC_SetMode(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_OCMODE_FORCED_INACTIVE);
break;
case LL_TIM_OCMODE_ACTIVE:
case LL_TIM_OCMODE_INACTIVE:
LL_TIM_OC_SetMode(TIM2, LL_TIM_CHANNEL_CH1, LL_TIM_OCMODE_FORCED_ACTIVE);
break;
case LL_TIM_OCMODE_INACTIVE:
default:
break;default
}switch (OCMode) { ... }
LL_TIM_OC_SetMode(TIM2, LL_TIM_CHANNEL_CH1, OCMode);
LL_TIM_EnableCounter(TIM2);
}{ ... }
/* ... */
__STATIC_INLINE void LED_Init(void)
{
LED2_GPIO_CLK_ENABLE();
LL_GPIO_SetPinMode(LED2_GPIO_PORT, LED2_PIN, LL_GPIO_MODE_OUTPUT);
}{ ... }
/* ... */
__STATIC_INLINE void UserButton_Init(void)
{
USER_BUTTON_GPIO_CLK_ENABLE();
LL_GPIO_SetPinMode(USER_BUTTON_GPIO_PORT, USER_BUTTON_PIN, LL_GPIO_MODE_INPUT);
LL_GPIO_SetPinPull(USER_BUTTON_GPIO_PORT, USER_BUTTON_PIN, LL_GPIO_PULL_NO);
USER_BUTTON_SYSCFG_SET_EXTI();
USER_BUTTON_EXTI_LINE_ENABLE();
USER_BUTTON_EXTI_FALLING_TRIG_ENABLE();
NVIC_EnableIRQ(USER_BUTTON_EXTI_IRQn);
NVIC_SetPriority(USER_BUTTON_EXTI_IRQn,0x03);
}{ ... }
/* ... */
void SystemClock_Config(void)
{
LL_RCC_HSE_EnableBypass();
LL_RCC_HSE_Enable();
while(LL_RCC_HSE_IsReady() != 1)
{
}while (LL_RCC_HSE_IsReady() != 1) { ... };
LL_FLASH_SetLatency(LL_FLASH_LATENCY_3);
LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE, LL_RCC_PLLM_DIV_8, 400, LL_RCC_PLLP_DIV_4);
LL_RCC_PLL_Enable();
while(LL_RCC_PLL_IsReady() != 1)
{
}while (LL_RCC_PLL_IsReady() != 1) { ... };
LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
{
}while (LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL) { ... };
LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_2);
LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
SysTick_Config(100000000 / 1000);
SystemCoreClock = 100000000;
}{ ... }
/* ... */
void UserButton_Callback(void)
{
iOCMode = (iOCMode + 1) % TIM_OC_MODES_NB;
Configure_OCMode(aOCMode[iOCMode]);
}{ ... }
/* ... */
void TimerCaptureCompare_Callback(void)
{
if(LL_TIM_GetCounter(TIM2) == LL_TIM_OC_GetCompareCH1(TIM2))
{
uwCompareMatchCount++;
}if (LL_TIM_GetCounter(TIM2) == LL_TIM_OC_GetCompareCH1(TIM2)) { ... }
}{ ... }
#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
/* ... */
/* ... */