3 ******************************************************************************
5 * @brief : Main program body
6 ******************************************************************************
7 ** This notice applies to any and all portions of this file
8 * that are not between comment pairs USER CODE BEGIN and
9 * USER CODE END. Other portions of this file, whether
10 * inserted by the user or by software development tools
11 * are owned by their respective copyright owners.
13 * COPYRIGHT(c) 2018 STMicroelectronics
15 * Redistribution and use in source and binary forms, with or without modification,
16 * are permitted provided that the following conditions are met:
17 * 1. Redistributions of source code must retain the above copyright notice,
18 * this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright notice,
20 * this list of conditions and the following disclaimer in the documentation
21 * and/or other materials provided with the distribution.
22 * 3. Neither the name of STMicroelectronics nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 ******************************************************************************
39 /* Includes ------------------------------------------------------------------*/
42 /* USER CODE BEGIN Includes */
44 /* USER CODE END Includes */
46 /* Private variables ---------------------------------------------------------*/
48 /* USER CODE BEGIN PV */
49 /* Private variables ---------------------------------------------------------*/
51 /* USER CODE END PV */
53 /* Private function prototypes -----------------------------------------------*/
54 static void LL_Init(void);
55 void SystemClock_Config(void);
56 static void MX_GPIO_Init(void);
57 static void MX_USART2_UART_Init(void);
58 static void MX_I2C1_Init(void);
60 /* USER CODE BEGIN PFP */
61 /* Private function prototypes -----------------------------------------------*/
63 /* USER CODE END PFP */
65 /* USER CODE BEGIN 0 */
70 * @brief The application entry point.
76 /* USER CODE BEGIN 1 */
80 /* MCU Configuration----------------------------------------------------------*/
82 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
85 /* USER CODE BEGIN Init */
87 /* USER CODE END Init */
89 /* Configure the system clock */
92 /* USER CODE BEGIN SysInit */
94 /* USER CODE END SysInit */
96 /* Initialize all configured peripherals */
98 MX_USART2_UART_Init();
100 /* USER CODE BEGIN 2 */
104 /* USER CODE END 2 */
107 /* USER CODE BEGIN WHILE */
110 /* USER CODE END WHILE */
112 /* USER CODE BEGIN 3 */
114 /* USER CODE END 3 */
118 static void LL_Init(void)
122 LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_SYSCFG);
124 /* System interrupt init*/
125 /* SVC_IRQn interrupt configuration */
126 NVIC_SetPriority(SVC_IRQn, 0);
127 /* PendSV_IRQn interrupt configuration */
128 NVIC_SetPriority(PendSV_IRQn, 0);
129 /* SysTick_IRQn interrupt configuration */
130 NVIC_SetPriority(SysTick_IRQn, 0);
135 * @brief System Clock Configuration
138 void SystemClock_Config(void)
141 LL_FLASH_SetLatency(LL_FLASH_LATENCY_1);
143 if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_1)
149 /* Wait till HSI is ready */
150 while(LL_RCC_HSI_IsReady() != 1)
154 LL_RCC_HSI_SetCalibTrimming(16);
156 LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI_DIV_2, LL_RCC_PLL_MUL_8);
160 /* Wait till PLL is ready */
161 while(LL_RCC_PLL_IsReady() != 1)
165 LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
167 LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
169 LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
171 /* Wait till System clock is ready */
172 while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
176 LL_Init1msTick(32000000);
178 LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK);
180 LL_SetSystemCoreClock(32000000);
182 LL_RCC_SetI2CClockSource(LL_RCC_I2C1_CLKSOURCE_HSI);
184 /* SysTick_IRQn interrupt configuration */
185 NVIC_SetPriority(SysTick_IRQn, 0);
188 /* I2C1 init function */
189 static void MX_I2C1_Init(void)
192 LL_I2C_InitTypeDef I2C_InitStruct;
194 LL_GPIO_InitTypeDef GPIO_InitStruct;
196 /**I2C1 GPIO Configuration
200 GPIO_InitStruct.Pin = LL_GPIO_PIN_8;
201 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
202 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
203 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
204 GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
205 GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
206 LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
208 GPIO_InitStruct.Pin = LL_GPIO_PIN_9;
209 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
210 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
211 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
212 GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
213 GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
214 LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
216 /* Peripheral clock enable */
217 LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1);
219 /**I2C Initialization
221 I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C;
222 I2C_InitStruct.Timing = 0x2000090E;
223 I2C_InitStruct.AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE;
224 I2C_InitStruct.DigitalFilter = 0;
225 I2C_InitStruct.OwnAddress1 = 0;
226 I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK;
227 I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
228 LL_I2C_Init(I2C1, &I2C_InitStruct);
230 LL_I2C_EnableAutoEndMode(I2C1);
232 LL_I2C_SetOwnAddress2(I2C1, 0, LL_I2C_OWNADDRESS2_NOMASK);
234 LL_I2C_DisableOwnAddress2(I2C1);
236 LL_I2C_DisableGeneralCall(I2C1);
238 LL_I2C_EnableClockStretching(I2C1);
242 /* USART2 init function */
243 static void MX_USART2_UART_Init(void)
246 LL_USART_InitTypeDef USART_InitStruct;
248 LL_GPIO_InitTypeDef GPIO_InitStruct;
250 /* Peripheral clock enable */
251 LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART2);
253 /**USART2 GPIO Configuration
254 PA2 ------> USART2_TX
255 PA3 ------> USART2_RX
257 GPIO_InitStruct.Pin = USART_TX_Pin;
258 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
259 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
260 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
261 GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
262 GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
263 LL_GPIO_Init(USART_TX_GPIO_Port, &GPIO_InitStruct);
265 GPIO_InitStruct.Pin = USART_RX_Pin;
266 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
267 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
268 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
269 GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
270 GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
271 LL_GPIO_Init(USART_RX_GPIO_Port, &GPIO_InitStruct);
273 USART_InitStruct.BaudRate = 115200;
274 USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
275 USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
276 USART_InitStruct.Parity = LL_USART_PARITY_NONE;
277 USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
278 USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
279 USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
280 LL_USART_Init(USART2, &USART_InitStruct);
282 LL_USART_DisableIT_CTS(USART2);
284 LL_USART_DisableOverrunDetect(USART2);
286 LL_USART_ConfigAsyncMode(USART2);
288 LL_USART_Enable(USART2);
292 /** Configure pins as
299 static void MX_GPIO_Init(void)
302 LL_GPIO_InitTypeDef GPIO_InitStruct;
303 LL_EXTI_InitTypeDef EXTI_InitStruct;
305 /* GPIO Ports Clock Enable */
306 LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC);
307 LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOF);
308 LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
309 LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
312 LL_GPIO_ResetOutputPin(LD2_GPIO_Port, LD2_Pin);
315 LL_SYSCFG_SetEXTISource(LL_SYSCFG_EXTI_PORTC, LL_SYSCFG_EXTI_LINE13);
318 LL_GPIO_SetPinPull(BLUE_BUTTON_GPIO_Port, BLUE_BUTTON_Pin, LL_GPIO_PULL_NO);
321 LL_GPIO_SetPinMode(BLUE_BUTTON_GPIO_Port, BLUE_BUTTON_Pin, LL_GPIO_MODE_INPUT);
324 EXTI_InitStruct.Line_0_31 = LL_EXTI_LINE_13;
325 EXTI_InitStruct.LineCommand = ENABLE;
326 EXTI_InitStruct.Mode = LL_EXTI_MODE_IT;
327 EXTI_InitStruct.Trigger = LL_EXTI_TRIGGER_FALLING;
328 LL_EXTI_Init(&EXTI_InitStruct);
331 GPIO_InitStruct.Pin = LD2_Pin;
332 GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
333 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
334 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
335 GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
336 LL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
339 GPIO_InitStruct.Pin = BMP_DONE_Pin;
340 GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
341 GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
342 LL_GPIO_Init(BMP_DONE_GPIO_Port, &GPIO_InitStruct);
346 /* USER CODE BEGIN 4 */
348 /* USER CODE END 4 */
351 * @brief This function is executed in case of error occurrence.
352 * @param file: The file name as string.
353 * @param line: The line in file as a number.
356 void _Error_Handler(char *file, int line)
358 /* USER CODE BEGIN Error_Handler_Debug */
359 /* User can add his own implementation to report the HAL error return state */
363 /* USER CODE END Error_Handler_Debug */
366 #ifdef USE_FULL_ASSERT
368 * @brief Reports the name of the source file and the source line number
369 * where the assert_param error has occurred.
370 * @param file: pointer to the source file name
371 * @param line: assert_param error line source number
374 void assert_failed(uint8_t* file, uint32_t line)
376 /* USER CODE BEGIN 6 */
377 /* User can add his own implementation to report the file name and line number,
378 tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
379 /* USER CODE END 6 */
381 #endif /* USE_FULL_ASSERT */
391 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/