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 International N.V.
14 * All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted, provided that the following conditions are met:
19 * 1. Redistribution of source code must retain the above copyright notice,
20 * this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright notice,
22 * this list of conditions and the following disclaimer in the documentation
23 * and/or other materials provided with the distribution.
24 * 3. Neither the name of STMicroelectronics nor the names of other
25 * contributors to this software may be used to endorse or promote products
26 * derived from this software without specific written permission.
27 * 4. This software, including modifications and/or derivative works of this
28 * software, must execute solely and exclusively on microcontroller or
29 * microprocessor devices manufactured by or for STMicroelectronics.
30 * 5. Redistribution and use of this software other than as permitted under
31 * this license is void and will automatically terminate your rights under
34 * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
35 * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
37 * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
38 * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
39 * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
40 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
42 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
43 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
44 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
45 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 ******************************************************************************
49 /* Includes ------------------------------------------------------------------*/
51 #include "stm32f1xx_hal.h"
52 #include "usb_device.h"
54 /* USER CODE BEGIN Includes */
56 /* USER CODE END Includes */
58 /* Private variables ---------------------------------------------------------*/
60 /* USER CODE BEGIN PV */
61 /* Private variables ---------------------------------------------------------*/
62 USBD_HandleTypeDef USBD_Device;
64 /* USER CODE END PV */
66 /* Private function prototypes -----------------------------------------------*/
67 void SystemClock_Config(void);
68 static void MX_GPIO_Init(void);
69 static void MX_I2C1_Init(void);
70 static void MX_I2C2_Init(void);
72 /* USER CODE BEGIN PFP */
73 /* Private function prototypes -----------------------------------------------*/
75 /* USER CODE END PFP */
77 /* USER CODE BEGIN 0 */
82 * @brief The application entry point.
88 /* USER CODE BEGIN 1 */
92 /* MCU Configuration----------------------------------------------------------*/
94 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
97 /* USER CODE BEGIN Init */
99 /* USER CODE END Init */
101 /* Configure the system clock */
102 SystemClock_Config();
104 /* USER CODE BEGIN SysInit */
106 /* USER CODE END SysInit */
108 /* Initialize all configured peripherals */
112 MX_USB_DEVICE_Init();
113 /* USER CODE BEGIN 2 */
115 /* USER CODE END 2 */
118 /* USER CODE BEGIN WHILE */
121 LL_GPIO_SetOutputPin(LED_GPIO_Port, LED_Pin);
123 LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED_Pin);
126 /* USER CODE END WHILE */
128 /* USER CODE BEGIN 3 */
131 /* USER CODE END 3 */
136 * @brief System Clock Configuration
139 void SystemClock_Config(void)
142 LL_FLASH_SetLatency(LL_FLASH_LATENCY_2);
144 if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_2)
150 /* Wait till HSE is ready */
151 while(LL_RCC_HSE_IsReady() != 1)
155 LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE_DIV_1, LL_RCC_PLL_MUL_9);
159 /* Wait till PLL is ready */
160 while(LL_RCC_PLL_IsReady() != 1)
164 LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
166 LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_2);
168 LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
170 LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
172 /* Wait till System clock is ready */
173 while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
177 LL_Init1msTick(72000000);
179 LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK);
181 LL_SetSystemCoreClock(72000000);
183 LL_RCC_SetUSBClockSource(LL_RCC_USB_CLKSOURCE_PLL_DIV_1_5);
185 /* SysTick_IRQn interrupt configuration */
186 NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
189 /* I2C1 init function */
190 static void MX_I2C1_Init(void)
193 LL_I2C_InitTypeDef I2C_InitStruct;
195 LL_GPIO_InitTypeDef GPIO_InitStruct;
197 /**I2C1 GPIO Configuration
201 GPIO_InitStruct.Pin = LL_GPIO_PIN_6|LL_GPIO_PIN_7;
202 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
203 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
204 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
205 LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
207 /* Peripheral clock enable */
208 LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1);
210 /**I2C Initialization
212 I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C;
213 I2C_InitStruct.ClockSpeed = 100000;
214 I2C_InitStruct.DutyCycle = LL_I2C_DUTYCYCLE_2;
215 I2C_InitStruct.OwnAddress1 = 0;
216 I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK;
217 I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
218 LL_I2C_Init(I2C1, &I2C_InitStruct);
220 LL_I2C_SetOwnAddress2(I2C1, 0);
222 LL_I2C_DisableOwnAddress2(I2C1);
224 LL_I2C_DisableGeneralCall(I2C1);
226 LL_I2C_EnableClockStretching(I2C1);
230 /* I2C2 init function */
231 static void MX_I2C2_Init(void)
234 LL_I2C_InitTypeDef I2C_InitStruct;
236 LL_GPIO_InitTypeDef GPIO_InitStruct;
238 /**I2C2 GPIO Configuration
239 PB10 ------> I2C2_SCL
240 PB11 ------> I2C2_SDA
242 GPIO_InitStruct.Pin = LL_GPIO_PIN_10|LL_GPIO_PIN_11;
243 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
244 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
245 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
246 LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
248 /* Peripheral clock enable */
249 LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C2);
251 /**I2C Initialization
253 I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C;
254 I2C_InitStruct.ClockSpeed = 400000;
255 I2C_InitStruct.DutyCycle = LL_I2C_DUTYCYCLE_2;
256 I2C_InitStruct.OwnAddress1 = 0;
257 I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK;
258 I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
259 LL_I2C_Init(I2C2, &I2C_InitStruct);
261 LL_I2C_SetOwnAddress2(I2C2, 0);
263 LL_I2C_DisableOwnAddress2(I2C2);
265 LL_I2C_DisableGeneralCall(I2C2);
267 LL_I2C_EnableClockStretching(I2C2);
271 /** Configure pins as
278 static void MX_GPIO_Init(void)
281 LL_GPIO_InitTypeDef GPIO_InitStruct;
283 /* GPIO Ports Clock Enable */
284 LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC);
285 LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOD);
286 LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB);
287 LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
290 LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED_Pin);
293 GPIO_InitStruct.Pin = LED_Pin;
294 GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
295 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
296 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
297 LL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
300 GPIO_InitStruct.Pin = BMP_DONE_Pin;
301 GPIO_InitStruct.Mode = LL_GPIO_MODE_FLOATING;
302 LL_GPIO_Init(BMP_DONE_GPIO_Port, &GPIO_InitStruct);
306 /* USER CODE BEGIN 4 */
308 /* USER CODE END 4 */
311 * @brief This function is executed in case of error occurrence.
312 * @param file: The file name as string.
313 * @param line: The line in file as a number.
316 void _Error_Handler(char *file, int line)
318 /* USER CODE BEGIN Error_Handler_Debug */
319 /* User can add his own implementation to report the HAL error return state */
323 /* USER CODE END Error_Handler_Debug */
326 #ifdef USE_FULL_ASSERT
328 * @brief Reports the name of the source file and the source line number
329 * where the assert_param error has occurred.
330 * @param file: pointer to the source file name
331 * @param line: assert_param error line source number
334 void assert_failed(uint8_t* file, uint32_t line)
336 /* USER CODE BEGIN 6 */
337 /* User can add his own implementation to report the file name and line number,
338 tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
339 /* USER CODE END 6 */
341 #endif /* USE_FULL_ASSERT */
351 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/