1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
32
33
36
37
38
39
40
43
44
45
46
47
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
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
180
181
182
183
184
185
186
191
192
193
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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
256
261
262
263
264
265
266
267
268
269
270
271
280
281
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
312
313
323
324
329
330
331
332
333
334
335
336
337
338
341
342
344
345
346
347
348
351
352
353
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
396
397
398
399
400
401
402
403
406
407
408
409
410
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
433
438
439
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
471
472
473
474
475
476
477
478
482
483
488
493
494
495
496
515
516
519
520
/* ... */
#include "main.h"
/* ... */
/* ... */
Includes
#define RX_BUFFER_SIZE 12
Private define
__IO uint8_t ubButtonPress = 0;
/* ... */
uint8_t aTextInfoStart[] = "\r\nUSART Example : Enter characters to fill reception buffers.\r\n";
uint8_t aTextInfoSwap1[] = "\r\n- Current RX buffer is full : ";
uint8_t aTextInfoSwap2[] = "\r\n- Reception will go on in alternate buffer\r\n";
/* ... */
uint8_t aRXBufferA[RX_BUFFER_SIZE];
uint8_t aRXBufferB[RX_BUFFER_SIZE];
__IO uint32_t uwNbReceivedChars;
__IO uint32_t uwBufferReadyIndication;
uint8_t *pBufferReadyForUser;
uint8_t *pBufferReadyForReception;
Private variables
void SystemClock_Config(void);
void Configure_USART(void);
void StartReception(void);
void HandleContinuousReception(void);
void LED_Init(void);
void LED_Off(void);
void LED_Blinking(uint32_t Period);
void UserButton_Init(void);
void WaitForUserButtonPress(void);
void PrintInfo(uint8_t *String, uint32_t Size);
void UserDataTreatment(uint8_t *DataBuffer, uint32_t Size);
Private function prototypes
/* ... */
int main(void)
{
SystemClock_Config();
LED_Init();
LED_Off();
UserButton_Init();
Configure_USART();
WaitForUserButtonPress();
StartReception();
while (1)
{
HandleContinuousReception();
}while (1) { ... }
}{ ... }
/* ... */
void Configure_USART(void)
{
USARTx_GPIO_CLK_ENABLE();
LL_GPIO_SetPinMode(USARTx_TX_GPIO_PORT, USARTx_TX_PIN, LL_GPIO_MODE_ALTERNATE);
USARTx_SET_TX_GPIO_AF();
LL_GPIO_SetPinSpeed(USARTx_TX_GPIO_PORT, USARTx_TX_PIN, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(USARTx_TX_GPIO_PORT, USARTx_TX_PIN, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinPull(USARTx_TX_GPIO_PORT, USARTx_TX_PIN, LL_GPIO_PULL_UP);
LL_GPIO_SetPinMode(USARTx_RX_GPIO_PORT, USARTx_RX_PIN, LL_GPIO_MODE_ALTERNATE);
USARTx_SET_RX_GPIO_AF();
LL_GPIO_SetPinSpeed(USARTx_RX_GPIO_PORT, USARTx_RX_PIN, LL_GPIO_SPEED_FREQ_HIGH);
LL_GPIO_SetPinOutputType(USARTx_RX_GPIO_PORT, USARTx_RX_PIN, LL_GPIO_OUTPUT_PUSHPULL);
LL_GPIO_SetPinPull(USARTx_RX_GPIO_PORT, USARTx_RX_PIN, LL_GPIO_PULL_UP);
NVIC_SetPriority(USARTx_IRQn, 0);
NVIC_EnableIRQ(USARTx_IRQn);
USARTx_CLK_ENABLE();
LL_USART_SetTransferDirection(USARTx_INSTANCE, LL_USART_DIRECTION_TX_RX);
LL_USART_ConfigCharacter(USARTx_INSTANCE, LL_USART_DATAWIDTH_8B, LL_USART_PARITY_NONE, LL_USART_STOPBITS_1);
/* ... */
LL_USART_SetBaudRate(USARTx_INSTANCE, SystemCoreClock/APB_Div, LL_USART_OVERSAMPLING_16, 115200);
LL_USART_Enable(USARTx_INSTANCE);
}{ ... }
/* ... */
void StartReception(void)
{
/* ... */
pBufferReadyForReception = aRXBufferA;
pBufferReadyForUser = aRXBufferB;
uwNbReceivedChars = 0;
uwBufferReadyIndication = 0;
PrintInfo(aTextInfoStart, sizeof(aTextInfoStart));
LL_USART_ClearFlag_ORE(USARTx_INSTANCE);
LL_USART_EnableIT_RXNE(USARTx_INSTANCE);
LL_USART_EnableIT_ERROR(USARTx_INSTANCE);
}{ ... }
/* ... */
void HandleContinuousReception(void)
{
if (uwBufferReadyIndication != 0)
{
uwBufferReadyIndication = 0;
UserDataTreatment(pBufferReadyForUser, RX_BUFFER_SIZE);
}if (uwBufferReadyIndication != 0) { ... }
}{ ... }
/* ... */
void LED_Init(void)
{
LED2_GPIO_CLK_ENABLE();
LL_GPIO_SetPinMode(LED2_GPIO_PORT, LED2_PIN, LL_GPIO_MODE_OUTPUT);
}{ ... }
/* ... */
void LED_Off(void)
{
LL_GPIO_ResetOutputPin(LED2_GPIO_PORT, LED2_PIN);
}{ ... }
/* ... */
void LED_Blinking(uint32_t Period)
{
while (1)
{
LL_GPIO_TogglePin(LED2_GPIO_PORT, LED2_PIN);
LL_mDelay(Period);
}while (1) { ... }
}{ ... }
/* ... */
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_SetPriority(USER_BUTTON_EXTI_IRQn, 3);
NVIC_EnableIRQ(USER_BUTTON_EXTI_IRQn);
}{ ... }
/* ... */
void WaitForUserButtonPress(void)
{
while (ubButtonPress == 0)
{
LL_GPIO_TogglePin(LED2_GPIO_PORT, LED2_PIN);
LL_mDelay(LED_BLINK_FAST);
}while (ubButtonPress == 0) { ... }
LED_Off();
}{ ... }
/* ... */
void PrintInfo(uint8_t *String, uint32_t Size)
{
uint32_t index = 0;
uint8_t *pchar = String;
for (index = 0; index < Size; index++)
{
while (!LL_USART_IsActiveFlag_TXE(USARTx_INSTANCE))
{
}while (!LL_USART_IsActiveFlag_TXE(USARTx_INSTANCE)) { ... }
/* ... */
LL_USART_TransmitData8(USARTx_INSTANCE, *pchar++);
}for (index = 0; index < Size; index++) { ... }
while (!LL_USART_IsActiveFlag_TC(USARTx_INSTANCE))
{
}while (!LL_USART_IsActiveFlag_TC(USARTx_INSTANCE)) { ... }
}{ ... }
/* ... */
void UserDataTreatment(uint8_t *DataBuffer, uint32_t Size)
{
PrintInfo(aTextInfoSwap1, sizeof(aTextInfoSwap1));
PrintInfo(DataBuffer, Size);
PrintInfo(aTextInfoSwap2, sizeof(aTextInfoSwap2));
LL_GPIO_TogglePin(LED2_GPIO_PORT, LED2_PIN);
}{ ... }
/* ... */
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)
{
ubButtonPress = 1;
}{ ... }
/* ... */
void USART_CharReception_Callback(void)
{
uint8_t *ptemp;
pBufferReadyForReception[uwNbReceivedChars++] = LL_USART_ReceiveData8(USARTx_INSTANCE);
if (uwNbReceivedChars >= RX_BUFFER_SIZE)
{
uwBufferReadyIndication = 1;
ptemp = pBufferReadyForUser;
pBufferReadyForUser = pBufferReadyForReception;
pBufferReadyForReception = ptemp;
uwNbReceivedChars = 0;
}if (uwNbReceivedChars >= RX_BUFFER_SIZE) { ... }
}{ ... }
/* ... */
void Error_Callback(void)
{
__IO uint32_t sr_reg;
NVIC_DisableIRQ(USARTx_IRQn);
/* ... */
sr_reg = LL_USART_ReadReg(USARTx_INSTANCE, SR);
if (sr_reg & LL_USART_SR_NE)
{
LL_USART_ClearFlag_NE(USARTx_INSTANCE);
}if (sr_reg & LL_USART_SR_NE) { ... }
else
{
LED_Blinking(LED_BLINK_ERROR);
}else { ... }
}{ ... }
#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
/* ... */
/* ... */