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_DMA_Init(void);
58 static void MX_USART2_UART_Init(void);
59 static void MX_I2C1_Init(void);
60 static void MX_TIM3_Init(void);
62 /* USER CODE BEGIN PFP */
63 /* Private function prototypes -----------------------------------------------*/
65 /* USER CODE END PFP */
67 /* USER CODE BEGIN 0 */
72 * @brief The application entry point.
78 /* USER CODE BEGIN 1 */
82 /* MCU Configuration----------------------------------------------------------*/
84 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
87 /* USER CODE BEGIN Init */
89 /* USER CODE END Init */
91 /* Configure the system clock */
94 /* USER CODE BEGIN SysInit */
96 /* USER CODE END SysInit */
98 /* Initialize all configured peripherals */
101 MX_USART2_UART_Init();
104 /* USER CODE BEGIN 2 */
108 /* USER CODE END 2 */
111 /* USER CODE BEGIN WHILE */
114 /* USER CODE END WHILE */
116 /* USER CODE BEGIN 3 */
118 /* USER CODE END 3 */
122 static void LL_Init(void)
126 LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_SYSCFG);
128 /* System interrupt init*/
129 /* SVC_IRQn interrupt configuration */
130 NVIC_SetPriority(SVC_IRQn, 0);
131 /* PendSV_IRQn interrupt configuration */
132 NVIC_SetPriority(PendSV_IRQn, 0);
133 /* SysTick_IRQn interrupt configuration */
134 NVIC_SetPriority(SysTick_IRQn, 0);
139 * @brief System Clock Configuration
142 void SystemClock_Config(void)
145 LL_FLASH_SetLatency(LL_FLASH_LATENCY_1);
147 if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_1)
153 /* Wait till HSI is ready */
154 while(LL_RCC_HSI_IsReady() != 1)
158 LL_RCC_HSI_SetCalibTrimming(16);
160 LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI_DIV_2, LL_RCC_PLL_MUL_8);
164 /* Wait till PLL is ready */
165 while(LL_RCC_PLL_IsReady() != 1)
169 LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
171 LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
173 LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
175 /* Wait till System clock is ready */
176 while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
180 LL_Init1msTick(32000000);
182 LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK);
184 LL_SetSystemCoreClock(32000000);
186 LL_RCC_SetI2CClockSource(LL_RCC_I2C1_CLKSOURCE_HSI);
188 /* SysTick_IRQn interrupt configuration */
189 NVIC_SetPriority(SysTick_IRQn, 0);
192 /* I2C1 init function */
193 static void MX_I2C1_Init(void)
196 LL_I2C_InitTypeDef I2C_InitStruct;
198 LL_GPIO_InitTypeDef GPIO_InitStruct;
200 /**I2C1 GPIO Configuration
204 GPIO_InitStruct.Pin = LL_GPIO_PIN_8;
205 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
206 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
207 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
208 GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
209 GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
210 LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
212 GPIO_InitStruct.Pin = LL_GPIO_PIN_9;
213 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
214 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
215 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
216 GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
217 GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
218 LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
220 /* Peripheral clock enable */
221 LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1);
223 /**I2C Initialization
225 I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C;
226 I2C_InitStruct.Timing = 0x2000090E;
227 I2C_InitStruct.AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE;
228 I2C_InitStruct.DigitalFilter = 0;
229 I2C_InitStruct.OwnAddress1 = 0;
230 I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK;
231 I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
232 LL_I2C_Init(I2C1, &I2C_InitStruct);
234 LL_I2C_EnableAutoEndMode(I2C1);
236 LL_I2C_SetOwnAddress2(I2C1, 0, LL_I2C_OWNADDRESS2_NOMASK);
238 LL_I2C_DisableOwnAddress2(I2C1);
240 LL_I2C_DisableGeneralCall(I2C1);
242 LL_I2C_EnableClockStretching(I2C1);
246 /* TIM3 init function */
247 static void MX_TIM3_Init(void)
250 LL_TIM_InitTypeDef TIM_InitStruct;
251 LL_TIM_OC_InitTypeDef TIM_OC_InitStruct;
253 LL_GPIO_InitTypeDef GPIO_InitStruct;
255 /* Peripheral clock enable */
256 LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3);
260 /* TIM3_CH1_TRIG Init */
261 LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_4, LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
263 LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_4, LL_DMA_PRIORITY_VERYHIGH);
265 LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_4, LL_DMA_MODE_NORMAL);
267 LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_4, LL_DMA_PERIPH_NOINCREMENT);
269 LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_4, LL_DMA_MEMORY_NOINCREMENT);
271 LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_4, LL_DMA_PDATAALIGN_WORD);
273 LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_4, LL_DMA_MDATAALIGN_WORD);
275 /* TIM3 interrupt Init */
276 NVIC_SetPriority(TIM3_IRQn, 0);
277 NVIC_EnableIRQ(TIM3_IRQn);
279 TIM_InitStruct.Prescaler = 31;
280 TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
281 TIM_InitStruct.Autoreload = 0;
282 TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
283 LL_TIM_Init(TIM3, &TIM_InitStruct);
285 LL_TIM_DisableARRPreload(TIM3);
287 LL_TIM_SetClockSource(TIM3, LL_TIM_CLOCKSOURCE_INTERNAL);
289 TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_FROZEN;
290 TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
291 TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
292 TIM_OC_InitStruct.CompareValue = 0;
293 TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
294 LL_TIM_OC_Init(TIM3, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
296 LL_TIM_OC_DisableFast(TIM3, LL_TIM_CHANNEL_CH1);
298 TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_ACTIVE;
299 TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
300 TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
301 LL_TIM_OC_Init(TIM3, LL_TIM_CHANNEL_CH2, &TIM_OC_InitStruct);
303 LL_TIM_OC_DisableFast(TIM3, LL_TIM_CHANNEL_CH2);
305 LL_TIM_SetTriggerOutput(TIM3, LL_TIM_TRGO_RESET);
307 LL_TIM_DisableMasterSlaveMode(TIM3);
309 /**TIM3 GPIO Configuration
312 GPIO_InitStruct.Pin = THERMO_Pin;
313 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
314 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
315 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
316 GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
317 GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
318 LL_GPIO_Init(THERMO_GPIO_Port, &GPIO_InitStruct);
322 /* USART2 init function */
323 static void MX_USART2_UART_Init(void)
326 LL_USART_InitTypeDef USART_InitStruct;
328 LL_GPIO_InitTypeDef GPIO_InitStruct;
330 /* Peripheral clock enable */
331 LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART2);
333 /**USART2 GPIO Configuration
334 PA2 ------> USART2_TX
335 PA3 ------> USART2_RX
337 GPIO_InitStruct.Pin = USART_TX_Pin;
338 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
339 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
340 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
341 GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
342 GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
343 LL_GPIO_Init(USART_TX_GPIO_Port, &GPIO_InitStruct);
345 GPIO_InitStruct.Pin = USART_RX_Pin;
346 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
347 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
348 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
349 GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
350 GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
351 LL_GPIO_Init(USART_RX_GPIO_Port, &GPIO_InitStruct);
353 USART_InitStruct.BaudRate = 115200;
354 USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
355 USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
356 USART_InitStruct.Parity = LL_USART_PARITY_NONE;
357 USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
358 USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
359 USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
360 LL_USART_Init(USART2, &USART_InitStruct);
362 LL_USART_DisableIT_CTS(USART2);
364 LL_USART_DisableOverrunDetect(USART2);
366 LL_USART_ConfigAsyncMode(USART2);
368 LL_USART_Enable(USART2);
373 * Enable DMA controller clock
375 static void MX_DMA_Init(void)
377 /* Init with LL driver */
378 /* DMA controller clock enable */
379 LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1);
381 /* DMA interrupt init */
382 /* DMA1_Channel4_5_IRQn interrupt configuration */
383 NVIC_SetPriority(DMA1_Channel4_5_IRQn, 0);
384 NVIC_EnableIRQ(DMA1_Channel4_5_IRQn);
388 /** Configure pins as
395 static void MX_GPIO_Init(void)
398 LL_GPIO_InitTypeDef GPIO_InitStruct;
399 LL_EXTI_InitTypeDef EXTI_InitStruct;
401 /* GPIO Ports Clock Enable */
402 LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC);
403 LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOF);
404 LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
405 LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
408 LL_GPIO_ResetOutputPin(LD2_GPIO_Port, LD2_Pin);
411 LL_SYSCFG_SetEXTISource(LL_SYSCFG_EXTI_PORTC, LL_SYSCFG_EXTI_LINE13);
414 LL_GPIO_SetPinPull(BLUE_BUTTON_GPIO_Port, BLUE_BUTTON_Pin, LL_GPIO_PULL_NO);
417 LL_GPIO_SetPinMode(BLUE_BUTTON_GPIO_Port, BLUE_BUTTON_Pin, LL_GPIO_MODE_INPUT);
420 EXTI_InitStruct.Line_0_31 = LL_EXTI_LINE_13;
421 EXTI_InitStruct.LineCommand = ENABLE;
422 EXTI_InitStruct.Mode = LL_EXTI_MODE_IT;
423 EXTI_InitStruct.Trigger = LL_EXTI_TRIGGER_FALLING;
424 LL_EXTI_Init(&EXTI_InitStruct);
427 GPIO_InitStruct.Pin = LD2_Pin;
428 GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
429 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
430 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
431 GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
432 LL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
435 GPIO_InitStruct.Pin = BMP_DONE_Pin;
436 GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
437 GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
438 LL_GPIO_Init(BMP_DONE_GPIO_Port, &GPIO_InitStruct);
442 /* USER CODE BEGIN 4 */
444 /* USER CODE END 4 */
447 * @brief This function is executed in case of error occurrence.
448 * @param file: The file name as string.
449 * @param line: The line in file as a number.
452 void _Error_Handler(char *file, int line)
454 /* USER CODE BEGIN Error_Handler_Debug */
455 /* User can add his own implementation to report the HAL error return state */
459 /* USER CODE END Error_Handler_Debug */
462 #ifdef USE_FULL_ASSERT
464 * @brief Reports the name of the source file and the source line number
465 * where the assert_param error has occurred.
466 * @param file: pointer to the source file name
467 * @param line: assert_param error line source number
470 void assert_failed(uint8_t* file, uint32_t line)
472 /* USER CODE BEGIN 6 */
473 /* User can add his own implementation to report the file name and line number,
474 tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
475 /* USER CODE END 6 */
477 #endif /* USE_FULL_ASSERT */
487 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/