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
53
54
55
60
61
62
63
69
70
71
72
73
74
75
76
77
78
79
80
81
82
88
89
90
91
92
93
94
95
96
97
98
102
103
104
105
106
107
108
114
115
118
119
120
121
122
123
124
125
127
131
132
133
134
135
136
140
141
142
143
144
145
146
147
148
149
153
154
155
156
157
158
160
164
165
166
167
168
169
170
171
175
176
177
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
210
213
214
215
216
217
218
219
220
221
222
223
224
228
229
231
232
233
234
235
236
240
241
242
247
248
249
256
261
262
269
283
284
289
297
298
299
318
319
322
323
/* ... */
#include "main.h"
/* ... */
/* ... */
Includes
#define TRANSMITTER_BOARD
Private define
UART_HandleTypeDef UartHandle;
uint8_t aTxBuffer[] = " **** UART_TwoBoards_ComPolling **** **** UART_TwoBoards_ComPolling **** **** UART_TwoBoards_ComPolling **** ";
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(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE, 5000)!= HAL_OK)
{
Error_Handler();
}if (HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE, 5000)!= HAL_OK) { ... }
BSP_LED_On(LED6);
if(HAL_UART_Receive(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE, 5000) != HAL_OK)
{
Error_Handler();
}if (HAL_UART_Receive(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE, 5000) != HAL_OK) { ... }
BSP_LED_On(LED4);
/* ... */
#else
if(HAL_UART_Receive(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE, 5000) != HAL_OK)
{
Error_Handler();
}if (HAL_UART_Receive(&UartHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE, 5000) != HAL_OK) { ... }
BSP_LED_On(LED4);
/* ... */
if(HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE, 5000)!= HAL_OK)
{
Error_Handler();
}if (HAL_UART_Transmit(&UartHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE, 5000)!= HAL_OK) { ... }
BSP_LED_On(LED6);
/* ... */
#endif
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_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 = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
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_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) { ... }
if (HAL_GetREVID() >= 0x1001)
{
__HAL_FLASH_PREFETCH_BUFFER_ENABLE();
}if (HAL_GetREVID() >= 0x1001) { ... }
}{ ... }
/* ... */
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
/* ... */
/* ... */