1 | /**
|
2 | ******************************************************************************
|
3 | * @file : main.c
|
4 | * @brief : 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) 2018 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 "stm32l4xx_hal.h"
|
41 |
|
42 | /* USER CODE BEGIN Includes */
|
43 |
|
44 | /* USER CODE END Includes */
|
45 |
|
46 | /* Private variables ---------------------------------------------------------*/
|
47 | I2C_HandleTypeDef hi2c1;
|
48 |
|
49 | UART_HandleTypeDef huart2;
|
50 |
|
51 | /* USER CODE BEGIN PV */
|
52 | /* Private variables ---------------------------------------------------------*/
|
53 |
|
54 | /* USER CODE END PV */
|
55 |
|
56 | /* Private function prototypes -----------------------------------------------*/
|
57 | void SystemClock_Config(void);
|
58 | static void MX_GPIO_Init(void);
|
59 | static void MX_USART2_UART_Init(void);
|
60 | static void MX_I2C1_Init(void);
|
61 |
|
62 | /* USER CODE BEGIN PFP */
|
63 | /* Private function prototypes -----------------------------------------------*/
|
64 |
|
65 | /* USER CODE END PFP */
|
66 |
|
67 | /* USER CODE BEGIN 0 */
|
68 |
|
69 | /* USER CODE END 0 */
|
70 |
|
71 | /**
|
72 | * @brief The application entry point.
|
73 | *
|
74 | * @retval None
|
75 | */
|
76 | int main(void)
|
77 | {
|
78 | /* USER CODE BEGIN 1 */
|
79 |
|
80 | /* USER CODE END 1 */
|
81 |
|
82 | /* MCU Configuration----------------------------------------------------------*/
|
83 |
|
84 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
85 | HAL_Init();
|
86 |
|
87 | /* USER CODE BEGIN Init */
|
88 |
|
89 | /* USER CODE END Init */
|
90 |
|
91 | /* Configure the system clock */
|
92 | SystemClock_Config();
|
93 |
|
94 | /* USER CODE BEGIN SysInit */
|
95 |
|
96 | /* USER CODE END SysInit */
|
97 |
|
98 | /* Initialize all configured peripherals */
|
99 | MX_GPIO_Init();
|
100 | MX_USART2_UART_Init();
|
101 | MX_I2C1_Init();
|
102 | /* USER CODE BEGIN 2 */
|
103 |
|
104 | /* USER CODE END 2 */
|
105 |
|
106 | /* Infinite loop */
|
107 | /* USER CODE BEGIN WHILE */
|
108 | while (1)
|
109 | {
|
110 | HAL_GPIO_TogglePin(GPIOA,LD2_Pin);
|
111 | HAL_Delay(500);
|
112 | /* USER CODE END WHILE */
|
113 |
|
114 | /* USER CODE BEGIN 3 */
|
115 |
|
116 | }
|
117 | /* USER CODE END 3 */
|
118 |
|
119 | }
|
120 |
|
121 | /**
|
122 | * @brief System Clock Configuration
|
123 | * @retval None
|
124 | */
|
125 | void SystemClock_Config(void)
|
126 | {
|
127 |
|
128 | RCC_OscInitTypeDef RCC_OscInitStruct;
|
129 | RCC_ClkInitTypeDef RCC_ClkInitStruct;
|
130 | RCC_PeriphCLKInitTypeDef PeriphClkInit;
|
131 |
|
132 | /**Initializes the CPU, AHB and APB busses clocks
|
133 | */
|
134 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
|
135 | RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
136 | RCC_OscInitStruct.HSICalibrationValue = 16;
|
137 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
138 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
|
139 | RCC_OscInitStruct.PLL.PLLM = 1;
|
140 | RCC_OscInitStruct.PLL.PLLN = 10;
|
141 | RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
|
142 | RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
|
143 | RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
|
144 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
145 | {
|
146 | _Error_Handler(__FILE__, __LINE__);
|
147 | }
|
148 |
|
149 | /**Initializes the CPU, AHB and APB busses clocks
|
150 | */
|
151 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|
152 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
|
153 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
154 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
155 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
|
156 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
157 |
|
158 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
|
159 | {
|
160 | _Error_Handler(__FILE__, __LINE__);
|
161 | }
|
162 |
|
163 | PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_I2C1;
|
164 | PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
|
165 | PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
|
166 | if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
|
167 | {
|
168 | _Error_Handler(__FILE__, __LINE__);
|
169 | }
|
170 |
|
171 | /**Configure the main internal regulator output voltage
|
172 | */
|
173 | if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
|
174 | {
|
175 | _Error_Handler(__FILE__, __LINE__);
|
176 | }
|
177 |
|
178 | /**Configure the Systick interrupt time
|
179 | */
|
180 | HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
|
181 |
|
182 | /**Configure the Systick
|
183 | */
|
184 | HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
|
185 |
|
186 | /* SysTick_IRQn interrupt configuration */
|
187 | HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
|
188 | }
|
189 |
|
190 | /* I2C1 init function */
|
191 | static void MX_I2C1_Init(void)
|
192 | {
|
193 |
|
194 | hi2c1.Instance = I2C1;
|
195 | hi2c1.Init.Timing = 0x10909CEC;
|
196 | hi2c1.Init.OwnAddress1 = 8;
|
197 | hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
198 | hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
199 | hi2c1.Init.OwnAddress2 = 0;
|
200 | hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
|
201 | hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_ENABLE;
|
202 | hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_ENABLE;
|
203 | if (HAL_I2C_Init(&hi2c1) != HAL_OK)
|
204 | {
|
205 | _Error_Handler(__FILE__, __LINE__);
|
206 | }
|
207 |
|
208 | /**Configure Analogue filter
|
209 | */
|
210 | if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
|
211 | {
|
212 | _Error_Handler(__FILE__, __LINE__);
|
213 | }
|
214 |
|
215 | /**Configure Digital filter
|
216 | */
|
217 | if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
|
218 | {
|
219 | _Error_Handler(__FILE__, __LINE__);
|
220 | }
|
221 |
|
222 | }
|
223 |
|
224 | /* USART2 init function */
|
225 | static void MX_USART2_UART_Init(void)
|
226 | {
|
227 |
|
228 | huart2.Instance = USART2;
|
229 | huart2.Init.BaudRate = 115200;
|
230 | huart2.Init.WordLength = UART_WORDLENGTH_8B;
|
231 | huart2.Init.StopBits = UART_STOPBITS_1;
|
232 | huart2.Init.Parity = UART_PARITY_NONE;
|
233 | huart2.Init.Mode = UART_MODE_TX_RX;
|
234 | huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
235 | huart2.Init.OverSampling = UART_OVERSAMPLING_16;
|
236 | huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
|
237 | huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
|
238 | if (HAL_UART_Init(&huart2) != HAL_OK)
|
239 | {
|
240 | _Error_Handler(__FILE__, __LINE__);
|
241 | }
|
242 |
|
243 | }
|
244 |
|
245 | /** Configure pins as
|
246 | * Analog
|
247 | * Input
|
248 | * Output
|
249 | * EVENT_OUT
|
250 | * EXTI
|
251 | */
|
252 | static void MX_GPIO_Init(void)
|
253 | {
|
254 |
|
255 | GPIO_InitTypeDef GPIO_InitStruct;
|
256 |
|
257 | /* GPIO Ports Clock Enable */
|
258 | __HAL_RCC_GPIOC_CLK_ENABLE();
|
259 | __HAL_RCC_GPIOH_CLK_ENABLE();
|
260 | __HAL_RCC_GPIOA_CLK_ENABLE();
|
261 | __HAL_RCC_GPIOB_CLK_ENABLE();
|
262 |
|
263 | /*Configure GPIO pin Output Level */
|
264 | HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
|
265 |
|
266 | /*Configure GPIO pin Output Level */
|
267 | HAL_GPIO_WritePin(D6_GPIO_Port, D6_Pin, GPIO_PIN_RESET);
|
268 |
|
269 | /*Configure GPIO pin : B1_Pin */
|
270 | GPIO_InitStruct.Pin = B1_Pin;
|
271 | GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
|
272 | GPIO_InitStruct.Pull = GPIO_NOPULL;
|
273 | HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
|
274 |
|
275 | /*Configure GPIO pin : LD2_Pin */
|
276 | GPIO_InitStruct.Pin = LD2_Pin;
|
277 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
278 | GPIO_InitStruct.Pull = GPIO_NOPULL;
|
279 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
280 | HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
|
281 |
|
282 | /*Configure GPIO pin : D6_Pin */
|
283 | GPIO_InitStruct.Pin = D6_Pin;
|
284 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
285 | GPIO_InitStruct.Pull = GPIO_NOPULL;
|
286 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
287 | HAL_GPIO_Init(D6_GPIO_Port, &GPIO_InitStruct);
|
288 |
|
289 | }
|
290 |
|
291 | /* USER CODE BEGIN 4 */
|
292 | void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c1)
|
293 | {
|
294 | /* Toggle LED2: Transfer in reception process is correct */
|
295 | HAL_GPIO_TogglePin(GPIOB,D6_Pin);
|
296 | }
|
297 | /* USER CODE END 4 */
|
298 |
|
299 | /**
|
300 | * @brief This function is executed in case of error occurrence.
|
301 | * @param file: The file name as string.
|
302 | * @param line: The line in file as a number.
|
303 | * @retval None
|
304 | */
|
305 | void _Error_Handler(char *file, int line)
|
306 | {
|
307 | /* USER CODE BEGIN Error_Handler_Debug */
|
308 | /* User can add his own implementation to report the HAL error return state */
|
309 | while(1)
|
310 | {
|
311 | HAL_GPIO_TogglePin(GPIOB,D6_Pin);
|
312 | HAL_Delay(500);
|
313 | }
|
314 | /* USER CODE END Error_Handler_Debug */
|
315 | }
|
316 |
|
317 | #ifdef USE_FULL_ASSERT
|
318 | /**
|
319 | * @brief Reports the name of the source file and the source line number
|
320 | * where the assert_param error has occurred.
|
321 | * @param file: pointer to the source file name
|
322 | * @param line: assert_param error line source number
|
323 | * @retval None
|
324 | */
|
325 | void assert_failed(uint8_t* file, uint32_t line)
|
326 | {
|
327 | /* USER CODE BEGIN 6 */
|
328 | /* User can add his own implementation to report the file name and line number,
|
329 | tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
330 | /* USER CODE END 6 */
|
331 | }
|
332 | #endif /* USE_FULL_ASSERT */
|
333 |
|
334 | /**
|
335 | * @}
|
336 | */
|
337 |
|
338 | /**
|
339 | * @}
|
340 | */
|
341 |
|
342 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|