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
39
40
41
42
43
44
45
46
47
48
49
54
55
56
61
62
63
64
70
71
72
73
74
75
76
77
78
79
80
81
82
83
89
90
91
92
93
94
95
96
97
98
99
103
104
105
106
107
108
109
110
116
117
120
121
122
123
124
125
126
127
129
133
134
135
138
139
140
141
142
143
147
148
149
150
151
152
153
157
158
159
162
163
164
165
166
167
169
173
174
175
176
177
180
181
182
183
184
185
189
190
191
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
227
228
229
230
231
232
233
234
235
236
237
238
242
243
245
246
247
248
249
250
254
255
256
263
264
265
266
267
268
269
270
271
272
279
280
281
282
283
284
285
286
287
288
295
300
301
308
322
323
328
336
337
338
339
340
341
342
343
344
345
355
356
357
360
361
/* ... */
#include "main.h"
/* ... */
/* ... */
Includes
#define TRANSMITTER_BOARD
Private define
UART_HandleTypeDef UartHandle;
__IO ITStatus UartReady = RESET;
uint8_t aTxBuffer[] = " ****UART_TwoBoards communication based on DMA**** ****UART_TwoBoards communication based on DMA**** ****UART_TwoBoards communication based on DMA**** ";
uint8_t aRxBuffer[RXBUFFERSIZE];
Private variables
static void SystemClock_Config(void);
static void Error_Handler(void);
static uint16_t Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
Private function prototypes
/* ... */
int main(void)
{
/* ... */
HAL_Init();
BSP_LED_Init(LED3);
BSP_LED_Init(LED4);
BSP_LED_Init(LED5);
BSP_LED_Init(LED6);
SystemClock_Config();
/* ... */
UartHandle.Instance = USARTx;
UartHandle.Init.BaudRate = 9600;
UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
UartHandle.Init.StopBits = UART_STOPBITS_1;
UartHandle.Init.Parity = UART_PARITY_NONE;
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UartHandle.Init.Mode = UART_MODE_TX_RX;
UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
if(HAL_UART_Init(&UartHandle) != HAL_OK)
{
Error_Handler();
}if (HAL_UART_Init(&UartHandle) != HAL_OK) { ... }
#ifdef TRANSMITTER_BOARD
BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO);
while (BSP_PB_GetState(BUTTON_KEY) == RESET)
{
BSP_LED_Toggle(LED3);
HAL_Delay(40);
}while (BSP_PB_GetState(BUTTON_KEY) == RESET) { ... }
while (BSP_PB_GetState(BUTTON_KEY) == SET)
{
}while (BSP_PB_GetState(BUTTON_KEY) == SET) { ... }
BSP_LED_Off(LED3);
/* ... */
if(HAL_UART_Transmit_DMA(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK)
{
Error_Handler();
}if (HAL_UART_Transmit_DMA(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK) { ... }
while (UartReady != SET)
{
}while (UartReady != SET) { ... }
UartReady = RESET;
if(HAL_UART_Receive_DMA(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
{
Error_Handler();
}if (HAL_UART_Receive_DMA(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK) { ... }
/* ... */
#else
if(HAL_UART_Receive_DMA(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK)
{
Error_Handler();
}if (HAL_UART_Receive_DMA(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK) { ... }
while (UartReady != SET)
{
}while (UartReady != SET) { ... }
UartReady = RESET;
/* ... */
if(HAL_UART_Transmit_DMA(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK)
{
Error_Handler();
}if (HAL_UART_Transmit_DMA(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK) { ... }
/* ... */
#endif
while (UartReady != SET)
{
}while (UartReady != SET) { ... }
UartReady = RESET;
if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,RXBUFFERSIZE))
{
Error_Handler();
}if (Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,RXBUFFERSIZE)) { ... }
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_SCALE2);
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 = 336;
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_2) != HAL_OK)
{
Error_Handler();
}if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { ... }
}{ ... }
/* ... */
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
{
UartReady = SET;
BSP_LED_On(LED6);
}{ ... }
/* ... */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
UartReady = SET;
BSP_LED_On(LED4);
}{ ... }
/* ... */
void HAL_UART_ErrorCallback(UART_HandleTypeDef *UartHandle)
{
BSP_LED_On(LED3);
}{ ... }
/* ... */
static uint16_t Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
{
while (BufferLength--)
{
if ((*pBuffer1) != *pBuffer2)
{
return BufferLength;
}if ((*pBuffer1) != *pBuffer2) { ... }
pBuffer1++;
pBuffer2++;
}while (BufferLength--) { ... }
return 0;
}{ ... }
/* ... */
static void Error_Handler(void)
{
BSP_LED_On(LED5);
while(1)
{
}while (1) { ... }
}{ ... }
#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
/* ... */
/* ... */