1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
33
34
37
38
45
52
53
54
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
77
78
79
86
87
88
89
90
94
95
96
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
155
156
157
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
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
292
295
296
297
298
299
300
301
302
305
306
307
308
309
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
332
333
334
335
336
337
338
339
340
341
345
349
350
351
357
366
367
373
377
378
379
398
399
402
403
/* ... */
#include "main.h"
/* ... */
/* ... */
Includes
#define CC_VALUE_NB 3
Private define
static uint32_t aCCValue[CC_VALUE_NB] = {0};
static uint32_t TimOutClock = 1;
Private variables
__STATIC_INLINE void SystemClock_Config(void);
__STATIC_INLINE void Configure_DMA(void);
__STATIC_INLINE void Configure_TIM(void);
__STATIC_INLINE void LED_Init(void);
__STATIC_INLINE void LED_Blinking(uint32_t Period);
Private function prototypes
/* ... */
int main(void)
{
SystemClock_Config();
LED_Init();
Configure_DMA();
Configure_TIM();
while (1)
{
}while (1) { ... }
}{ ... }
/* ... */
__STATIC_INLINE void Configure_DMA(void)
{
NVIC_SetPriority(DMA1_Stream7_IRQn, 0);
NVIC_EnableIRQ(DMA1_Stream7_IRQn);
...
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1);
...
LL_DMA_ConfigTransfer(DMA1, LL_DMA_STREAM_7, LL_DMA_DIRECTION_MEMORY_TO_PERIPH |
LL_DMA_PRIORITY_HIGH |
LL_DMA_MODE_CIRCULAR |
LL_DMA_PERIPH_NOINCREMENT |
LL_DMA_MEMORY_INCREMENT |
LL_DMA_PDATAALIGN_WORD |
LL_DMA_MDATAALIGN_WORD);
LL_DMA_SetChannelSelection(DMA1, LL_DMA_STREAM_7, LL_DMA_CHANNEL_5);
LL_DMA_ConfigAddresses(DMA1, LL_DMA_STREAM_7, (uint32_t)&aCCValue, (uint32_t)&TIM3->CCR3, LL_DMA_GetDataTransferDirection(DMA1, LL_DMA_STREAM_7));
LL_DMA_SetDataLength(DMA1, LL_DMA_STREAM_7, CC_VALUE_NB);
LL_DMA_EnableIT_TC(DMA1, LL_DMA_STREAM_7);
LL_DMA_EnableIT_TE(DMA1, LL_DMA_STREAM_7);
...
LL_DMA_EnableStream(DMA1, LL_DMA_STREAM_7);
}{ ... }
/* ... */
__STATIC_INLINE void Configure_TIM(void)
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
LL_GPIO_SetPinMode(GPIOB, LL_GPIO_PIN_0, LL_GPIO_MODE_ALTERNATE);
LL_GPIO_SetPinPull(GPIOB, LL_GPIO_PIN_0, LL_GPIO_PULL_DOWN);
LL_GPIO_SetPinSpeed(GPIOB, LL_GPIO_PIN_0, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetAFPin_0_7(GPIOB, LL_GPIO_PIN_0, LL_GPIO_AF_2);
...
NVIC_SetPriority(TIM3_IRQn, 0);
NVIC_EnableIRQ(TIM3_IRQn);
...
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3);
...
TimOutClock = SystemCoreClock/1;
LL_TIM_SetAutoReload(TIM3, __LL_TIM_CALC_ARR(TimOutClock, LL_TIM_COUNTERMODE_UP, 17570));
LL_TIM_SetRepetitionCounter(TIM3, 4-1);
...
LL_TIM_OC_SetMode(TIM3, LL_TIM_CHANNEL_CH3, LL_TIM_OCMODE_PWM1);
LL_TIM_OC_ConfigOutput(TIM3, LL_TIM_CHANNEL_CH3, LL_TIM_OCPOLARITY_HIGH | LL_TIM_OCIDLESTATE_HIGH);
aCCValue[0] = (uint32_t)(((uint32_t) 75 * (LL_TIM_GetAutoReload(TIM3) - 1)) / 100);
aCCValue[1] = (uint32_t)(((uint32_t) 50 * (LL_TIM_GetAutoReload(TIM3) - 1)) / 100);
aCCValue[2] = (uint32_t)(((uint32_t) 25 * (LL_TIM_GetAutoReload(TIM3) - 1)) / 100);
LL_TIM_OC_SetCompareCH3(TIM3, aCCValue[0]);
LL_TIM_OC_EnablePreload(TIM3, LL_TIM_CHANNEL_CH3);
...
LL_TIM_EnableDMAReq_UPDATE(TIM3);
LL_TIM_EnableDMAReq_CC3(TIM3);
...
LL_TIM_CC_EnableChannel(TIM3, LL_TIM_CHANNEL_CH3);
LL_TIM_EnableAllOutputs(TIM3);
LL_TIM_EnableCounter(TIM3);
LL_TIM_GenerateEvent_UPDATE(TIM3);
}{ ... }
/* ... */
__STATIC_INLINE void LED_Init(void)
{
LED2_GPIO_CLK_ENABLE();
LL_GPIO_SetPinMode(LED2_GPIO_PORT, LED2_PIN, LL_GPIO_MODE_OUTPUT);
LL_GPIO_SetPinOutputType(LED2_GPIO_PORT, LED2_PIN, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinSpeed(LED2_GPIO_PORT, LED2_PIN, LL_GPIO_SPEED_FREQ_LOW);
LL_GPIO_SetPinPull(LED2_GPIO_PORT, LED2_PIN, LL_GPIO_PULL_NO);
}{ ... }
/* ... */
__STATIC_INLINE void LED_Blinking(uint32_t Period)
{
while (1)
{
LL_GPIO_TogglePin(LED2_GPIO_PORT, LED2_PIN);
LL_mDelay(Period);
}while (1) { ... }
}{ ... }
/* ... */
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 TimerUpdate_Callback(void)
{
static uint32_t UpdateEventCnt = 0;
if (LL_TIM_OC_GetCompareCH3(TIM3) != aCCValue[UpdateEventCnt])
{
LED_Blinking(LED_BLINK_ERROR);
}if (LL_TIM_OC_GetCompareCH3(TIM3) != aCCValue[UpdateEventCnt]) { ... }
else
{
UpdateEventCnt = (UpdateEventCnt+1) % CC_VALUE_NB;
}else { ... }
}{ ... }
/* ... */
void TransferComplete_Callback()
{
if (LL_TIM_OC_GetCompareCH3(TIM3) != aCCValue[CC_VALUE_NB-1])
{
LED_Blinking(LED_BLINK_ERROR);
}if (LL_TIM_OC_GetCompareCH3(TIM3) != aCCValue[CC_VALUE_NB-1]) { ... }
}{ ... }
/* ... */
void TransferError_Callback()
{
LED_Blinking(LED_BLINK_ERROR);
}{ ... }
#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
/* ... */
/* ... */