1 | /* USER CODE BEGIN Header */
|
2 | /**
|
3 | ******************************************************************************
|
4 | * @file : main.c
|
5 | * @brief : Main program body
|
6 | ******************************************************************************
|
7 | * @attention
|
8 | *
|
9 | * Copyright (c) 2022 STMicroelectronics.
|
10 | * All rights reserved.
|
11 | *
|
12 | * This software is licensed under terms that can be found in the LICENSE file
|
13 | * in the root directory of this software component.
|
14 | * If no LICENSE file comes with this software, it is provided AS-IS.
|
15 | *
|
16 | ******************************************************************************
|
17 | */
|
18 | /* USER CODE END Header */
|
19 | /* Includes ------------------------------------------------------------------*/
|
20 | #include "main.h"
|
21 |
|
22 |
|
23 | /* Private variables ---------------------------------------------------------*/
|
24 | UART_HandleTypeDef huart1;
|
25 | UART_HandleTypeDef huart2;
|
26 |
|
27 |
|
28 |
|
29 | /* Private function prototypes -----------------------------------------------*/
|
30 | void SystemClock_Config(void);
|
31 | static void MX_GPIO_Init(void);
|
32 | static void MX_USART2_UART_Init(void);
|
33 | static void MX_USART1_UART_Init(void);
|
34 |
|
35 | /* Private user code ---------------------------------------------------------*/
|
36 | /* USER CODE BEGIN 0 */
|
37 | char Test[]="AT";
|
38 | char Reply[]="";
|
39 |
|
40 |
|
41 |
|
42 | int main(void)
|
43 | {
|
44 |
|
45 |
|
46 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
47 | HAL_Init();
|
48 | SystemClock_Config();
|
49 |
|
50 |
|
51 | MX_GPIO_Init();
|
52 | MX_USART2_UART_Init();
|
53 | MX_USART1_UART_Init();
|
54 |
|
55 | while (1)
|
56 | {
|
57 | /* USER CODE END WHILE */
|
58 |
|
59 | HAL_UART_Transmit(&huart1,(uint8_t *)"AT",strlen("AT"),1000); //Send AT Command
|
60 | HAL_UART_Receive(&huart1,(uint8_t *)Reply,5,1000); //Receive AT Result
|
61 |
|
62 | HAL_UART_Transmit(&huart2,(uint8_t *)Reply,strlen(Reply),1000); //Send Result to Terminal
|
63 |
|
64 | }
|
65 |
|
66 | }
|
67 |
|
68 | /**
|
69 | * @brief System Clock Configuration
|
70 | * @retval None
|
71 | */
|
72 | void SystemClock_Config(void)
|
73 | {
|
74 | RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
75 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
|
76 |
|
77 | /** Configure the main internal regulator output voltage
|
78 | */
|
79 | if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
|
80 | {
|
81 | Error_Handler();
|
82 | }
|
83 |
|
84 | /** Initializes the RCC Oscillators according to the specified parameters
|
85 | * in the RCC_OscInitTypeDef structure.
|
86 | */
|
87 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
|
88 | RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
89 | RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
|
90 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
91 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
|
92 | RCC_OscInitStruct.PLL.PLLM = 1;
|
93 | RCC_OscInitStruct.PLL.PLLN = 10;
|
94 | RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
|
95 | RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
|
96 | RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
|
97 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
98 | {
|
99 | Error_Handler();
|
100 | }
|
101 |
|
102 | /** Initializes the CPU, AHB and APB buses clocks
|
103 | */
|
104 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|
105 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
|
106 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
107 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
108 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
|
109 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
110 |
|
111 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
|
112 | {
|
113 | Error_Handler();
|
114 | }
|
115 | }
|
116 |
|
117 | /**
|
118 | * @brief USART1 Initialization Function
|
119 | * @param None
|
120 | * @retval None
|
121 | */
|
122 | static void MX_USART1_UART_Init(void)
|
123 | {
|
124 |
|
125 | /* USER CODE BEGIN USART1_Init 0 */
|
126 |
|
127 | /* USER CODE END USART1_Init 0 */
|
128 |
|
129 | /* USER CODE BEGIN USART1_Init 1 */
|
130 |
|
131 | /* USER CODE END USART1_Init 1 */
|
132 | huart1.Instance = USART1;
|
133 | huart1.Init.BaudRate = 9600;
|
134 | huart1.Init.WordLength = UART_WORDLENGTH_8B;
|
135 | huart1.Init.StopBits = UART_STOPBITS_1;
|
136 | huart1.Init.Parity = UART_PARITY_NONE;
|
137 | huart1.Init.Mode = UART_MODE_TX_RX;
|
138 | huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
139 | huart1.Init.OverSampling = UART_OVERSAMPLING_16;
|
140 | huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
|
141 | huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
|
142 | if (HAL_UART_Init(&huart1) != HAL_OK)
|
143 | {
|
144 | Error_Handler();
|
145 | }
|
146 | /* USER CODE BEGIN USART1_Init 2 */
|
147 |
|
148 | /* USER CODE END USART1_Init 2 */
|
149 |
|
150 | }
|
151 |
|
152 | /**
|
153 | * @brief USART2 Initialization Function
|
154 | * @param None
|
155 | * @retval None
|
156 | */
|
157 | static void MX_USART2_UART_Init(void)
|
158 | {
|
159 |
|
160 | /* USER CODE BEGIN USART2_Init 0 */
|
161 |
|
162 | /* USER CODE END USART2_Init 0 */
|
163 |
|
164 | /* USER CODE BEGIN USART2_Init 1 */
|
165 |
|
166 | /* USER CODE END USART2_Init 1 */
|
167 | huart2.Instance = USART2;
|
168 | huart2.Init.BaudRate = 115200;
|
169 | huart2.Init.WordLength = UART_WORDLENGTH_8B;
|
170 | huart2.Init.StopBits = UART_STOPBITS_1;
|
171 | huart2.Init.Parity = UART_PARITY_NONE;
|
172 | huart2.Init.Mode = UART_MODE_TX_RX;
|
173 | huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
174 | huart2.Init.OverSampling = UART_OVERSAMPLING_16;
|
175 | huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
|
176 | huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
|
177 | if (HAL_UART_Init(&huart2) != HAL_OK)
|
178 | {
|
179 | Error_Handler();
|
180 | }
|
181 | /* USER CODE BEGIN USART2_Init 2 */
|
182 |
|
183 | /* USER CODE END USART2_Init 2 */
|
184 |
|
185 | }
|
186 |
|
187 | /**
|
188 | * @brief GPIO Initialization Function
|
189 | * @param None
|
190 | * @retval None
|
191 | */
|
192 | static void MX_GPIO_Init(void)
|
193 | {
|
194 | GPIO_InitTypeDef GPIO_InitStruct = {0};
|
195 |
|
196 | /* GPIO Ports Clock Enable */
|
197 | __HAL_RCC_GPIOC_CLK_ENABLE();
|
198 | __HAL_RCC_GPIOH_CLK_ENABLE();
|
199 | __HAL_RCC_GPIOA_CLK_ENABLE();
|
200 | __HAL_RCC_GPIOB_CLK_ENABLE();
|
201 |
|
202 | /*Configure GPIO pin Output Level */
|
203 | HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
|
204 |
|
205 | /*Configure GPIO pin : B1_Pin */
|
206 | GPIO_InitStruct.Pin = B1_Pin;
|
207 | GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
|
208 | GPIO_InitStruct.Pull = GPIO_NOPULL;
|
209 | HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
|
210 |
|
211 | /*Configure GPIO pin : LD2_Pin */
|
212 | GPIO_InitStruct.Pin = LD2_Pin;
|
213 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
214 | GPIO_InitStruct.Pull = GPIO_NOPULL;
|
215 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
216 | HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
|
217 |
|
218 | }
|
219 |
|
220 | /* USER CODE BEGIN 4 */
|
221 |
|
222 | /* USER CODE END 4 */
|
223 |
|
224 | /**
|
225 | * @brief This function is executed in case of error occurrence.
|
226 | * @retval None
|
227 | */
|
228 | void Error_Handler(void)
|
229 | {
|
230 | /* USER CODE BEGIN Error_Handler_Debug */
|
231 | /* User can add his own implementation to report the HAL error return state */
|
232 | __disable_irq();
|
233 | while (1)
|
234 | {
|
235 | }
|
236 | /* USER CODE END Error_Handler_Debug */
|
237 | }
|
238 |
|
239 | #ifdef USE_FULL_ASSERT
|
240 | /**
|
241 | * @brief Reports the name of the source file and the source line number
|
242 | * where the assert_param error has occurred.
|
243 | * @param file: pointer to the source file name
|
244 | * @param line: assert_param error line source number
|
245 | * @retval None
|
246 | */
|
247 | void assert_failed(uint8_t *file, uint32_t line)
|
248 | {
|
249 | /* USER CODE BEGIN 6 */
|
250 | /* User can add his own implementation to report the file name and line number,
|
251 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
252 | /* USER CODE END 6 */
|
253 | }
|
254 | #endif /* USE_FULL_ASSERT */
|