1 /**
2 ******************************************************************************
3 * File Name : main.c
4 * Description : Main program body
5 ******************************************************************************
6 ** This notice applies to any and all portions of this file
7 * that are not between comment pairs USER CODE BEGIN and
8 * USER CODE END. Other portions of this file, whether
9 * inserted by the user or by software development tools
10 * are owned by their respective copyright owners.
11 *
12 * COPYRIGHT(c) 2017 STMicroelectronics
13 *
14 * Redistribution and use in source and binary forms, with or without modification,
15 * are permitted provided that the following conditions are met:
16 * 1. Redistributions of source code must retain the above copyright notice,
17 * this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright notice,
19 * this list of conditions and the following disclaimer in the documentation
20 * and/or other materials provided with the distribution.
21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 ******************************************************************************
37 */
38 /* Includes ------------------------------------------------------------------*/
39 #include "main.h"
40 #include "stm32f1xx_hal.h"
41
42 /* USER CODE BEGIN Includes */
43
44 /* USER CODE END Includes */
45
46 /* Private variables ---------------------------------------------------------*/
47 UART_HandleTypeDef huart1;
48 UART_HandleTypeDef huart3;
49
50 /* USER CODE BEGIN PV */
51 /* Private variables ---------------------------------------------------------*/
52
53 /* USER CODE END PV */
54
55 /* Private function prototypes -----------------------------------------------*/
56 void SystemClock_Config(void);
57 static void MX_GPIO_Init(void);
58 static void MX_USART1_UART_Init(void);
59 static void MX_USART3_UART_Init(void);
60
61 /* USER CODE BEGIN PFP */
62 /* Private function prototypes -----------------------------------------------*/
63
64 /* USER CODE END PFP */
65
66 /* USER CODE BEGIN 0 */
67
68 /* USER CODE END 0 */
69
70 int main(void)
71 {
72
73 /* USER CODE BEGIN 1 */
74
75 /* USER CODE END 1 */
76
77 /* MCU Configuration----------------------------------------------------------*/
78
79 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
80 HAL_Init();
81
82 /* USER CODE BEGIN Init */
83
84 /* USER CODE END Init */
85
86 /* Configure the system clock */
87 SystemClock_Config();
88
89 /* USER CODE BEGIN SysInit */
90
91 /* USER CODE END SysInit */
92
93 /* Initialize all configured peripherals */
94 MX_GPIO_Init();
95 MX_USART1_UART_Init();
96 MX_USART3_UART_Init();
97
98 /* USER CODE BEGIN 2 */
99 //熄灭三个LED灯
100 HAL_GPIO_WritePin(GPIOB,
101 GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_5, 1);
102
103 HAL_UART_Transmit(&huart1,
104 "WIFI配置...\n",12,1000);
105
106 //数组用来存放串口接收的数据
107 uint8_t rec[100] = {0};
108 //WIFI使能 PB8 = 1
109 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,1);
110 //WIFI取消复位 PB9 = 1
111 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,1);
112 HAL_Delay(1000);
113 //测试AT指令
114 HAL_UART_Transmit(&huart3,"AT\r\n",4,1000);
115 HAL_UART_Receive(&huart3,rec,20,2000);
116 HAL_UART_Transmit(&huart1,rec,20,1000);
117
118 /*设置工作方式指令:AT+CWMODE=mode
119 mode = 1是station模式
120 mode = 2是AP模式
121 mode = 3是station+AP模式
122 */
123 HAL_UART_Transmit(&huart3,"AT+CWMODE=2\r\n",13,1000);
124 HAL_UART_Receive(&huart3,rec,20,2000);
125 HAL_UART_Transmit(&huart1,rec,20,1000);
126
127 /*设置为单路连接模式:AT+CIPMUX=mode
128 mode = 0为单路连接
129 mode = 1为多路连接
130 */
131 HAL_UART_Transmit(&huart3,"AT+CIPMUX=1\r\n",
132 13,1000);
133 HAL_UART_Receive(&huart3,rec,20,2000);
134 HAL_UART_Transmit(&huart1,rec,20,1000);
135
136
137 /*获取本机IP地址:AT+CIFS
138 */
139 HAL_UART_Transmit(&huart3,
140 "AT+CIFSR\r\n",
141 10,1000);
142 HAL_UART_Receive(&huart3,rec,50,2000);
143 HAL_UART_Transmit(&huart1,rec,50,1000);
144
145 /*发出WIFI热点指令:
146 AT+CWSAP=<ssid>,<pwd>,<chl>,<ecn>
147 ssid是热点名称
148 pwd是密码
149 ch1是通道号
150 ecn是加密方式
151 0 OPEN
152 1 WEP
153 2 WPA_PSK
154 3 WPA2_PSK
155 4 WPA_WPA2_PSK
156 */
157 HAL_UART_Transmit(&huart3,
158 "AT+CWSAP=\"发出WIFI\",\"zzaaqq11\",1,3\r\n",
159 36,1000);
160 HAL_UART_Receive(&huart3,rec,50,2000);
161 HAL_UART_Transmit(&huart1,rec,50,1000);
162 HAL_Delay(500);
163
164 /* USER CODE END 2 */
165
166 /* Infinite loop */
167 /* USER CODE BEGIN WHILE */
168 while (1)
169 {
170 /* USER CODE END WHILE */
171
172 /* USER CODE BEGIN 3 */
173
174 }
175 /* USER CODE END 3 */
176
177 }
178
179 /** System Clock Configuration
180 */
181 void SystemClock_Config(void)
182 {
183
184 RCC_OscInitTypeDef RCC_OscInitStruct;
185 RCC_ClkInitTypeDef RCC_ClkInitStruct;
186
187 /**Initializes the CPU, AHB and APB busses clocks
188 */
189 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
190 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
191 RCC_OscInitStruct.HSICalibrationValue = 16;
192 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
193 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
194 {
195 _Error_Handler(__FILE__, __LINE__);
196 }
197
198 /**Initializes the CPU, AHB and APB busses clocks
199 */
200 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
201 |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
202 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
203 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
204 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
205 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
206
207 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
208 {
209 _Error_Handler(__FILE__, __LINE__);
210 }
211
212 /**Configure the Systick interrupt time
213 */
214 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
215
216 /**Configure the Systick
217 */
218 HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
219
220 /* SysTick_IRQn interrupt configuration */
221 HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
222 }
223
224 /* USART1 init function */
225 static void MX_USART1_UART_Init(void)
226 {
227
228 huart1.Instance = USART1;
229 huart1.Init.BaudRate = 9600;
230 huart1.Init.WordLength = UART_WORDLENGTH_8B;
231 huart1.Init.StopBits = UART_STOPBITS_1;
232 huart1.Init.Parity = UART_PARITY_NONE;
233 huart1.Init.Mode = UART_MODE_TX_RX;
234 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
235 huart1.Init.OverSampling = UART_OVERSAMPLING_16;
236 if (HAL_UART_Init(&huart1) != HAL_OK)
237 {
238 _Error_Handler(__FILE__, __LINE__);
239 }
240
241 }
242
243 /* USART3 init function */
244 static void MX_USART3_UART_Init(void)
245 {
246
247 huart3.Instance = USART3;
248 huart3.Init.BaudRate = 115200;
249 huart3.Init.WordLength = UART_WORDLENGTH_8B;
250 huart3.Init.StopBits = UART_STOPBITS_1;
251 huart3.Init.Parity = UART_PARITY_NONE;
252 huart3.Init.Mode = UART_MODE_TX_RX;
253 huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
254 huart3.Init.OverSampling = UART_OVERSAMPLING_16;
255 if (HAL_UART_Init(&huart3) != HAL_OK)
256 {
257 _Error_Handler(__FILE__, __LINE__);
258 }
259
260 }
261
262 /** Configure pins as
263 * Analog
264 * Input
265 * Output
266 * EVENT_OUT
267 * EXTI
268 */
269 static void MX_GPIO_Init(void)
270 {
271
272 GPIO_InitTypeDef GPIO_InitStruct;
273
274 /* GPIO Ports Clock Enable */
275 __HAL_RCC_GPIOB_CLK_ENABLE();
276 __HAL_RCC_GPIOA_CLK_ENABLE();
277
278 /*Configure GPIO pin Output Level */
279 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_5|WIFI_CMD_Pin
280 |WIFI_RST_Pin, GPIO_PIN_RESET);
281
282 /*Configure GPIO pins : PB0 PB1 PB5 WIFI_CMD_Pin
283 WIFI_RST_Pin */
284 GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_5|WIFI_CMD_Pin
285 |WIFI_RST_Pin;
286 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
287 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
288 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
289
290 }
291
292 /* USER CODE BEGIN 4 */
293
294 /* USER CODE END 4 */
295
296 /**
297 * @brief This function is executed in case of error occurrence.
298 * @param None
299 * @retval None
300 */
301 void _Error_Handler(char * file, int line)
302 {
303 /* USER CODE BEGIN Error_Handler_Debug */
304 /* User can add his own implementation to report the HAL error return state */
305 while(1)
306 {
307 }
308 /* USER CODE END Error_Handler_Debug */
309 }
310
311 #ifdef USE_FULL_ASSERT
312
313 /**
314 * @brief Reports the name of the source file and the source line number
315 * where the assert_param error has occurred.
316 * @param file: pointer to the source file name
317 * @param line: assert_param error line source number
318 * @retval None
319 */
320 void assert_failed(uint8_t* file, uint32_t line)
321 {
322 /* USER CODE BEGIN 6 */
323 /* User can add his own implementation to report the file name and line number,
324 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
325 /* USER CODE END 6 */
326
327 }
328
329 #endif
330
331 /**
332 * @}
333 */
334
335 /**
336 * @}
337 */
338
339 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/