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 ------------------------------------------------------------------*/
41 #include "stm32f1xx_hal.h"
43 /* USER CODE BEGIN Includes */
50 /* USER CODE END Includes */
52 /* Private variables ---------------------------------------------------------*/
54 PCD_HandleTypeDef hpcd_USB_FS;
56 /* USER CODE BEGIN PV */
57 /* Private variables ---------------------------------------------------------*/
60 /* USER CODE END PV */
62 /* Private function prototypes -----------------------------------------------*/
63 void SystemClock_Config(void);
64 static void MX_GPIO_Init(void);
65 static void MX_I2C1_Init(void);
66 static void MX_I2C2_Init(void);
67 static void MX_USB_PCD_Init(void);
68 static void MX_TIM4_Init(void);
70 /* USER CODE BEGIN PFP */
71 /* Private function prototypes -----------------------------------------------*/
73 /* USER CODE END PFP */
75 /* USER CODE BEGIN 0 */
80 * @brief The application entry point.
86 /* USER CODE BEGIN 1 */
90 /* MCU Configuration----------------------------------------------------------*/
92 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
95 /* USER CODE BEGIN Init */
97 /* USER CODE END Init */
99 /* Configure the system clock */
100 SystemClock_Config();
102 /* USER CODE BEGIN SysInit */
103 usb_init(&usb, &hpcd_USB_FS);
105 /* USER CODE END SysInit */
107 /* Initialize all configured peripherals */
110 // A hack to let USB host reset us
111 LL_GPIO_InitTypeDef gpio;
112 gpio.Pin = LL_GPIO_PIN_12 | LL_GPIO_PIN_13;
113 gpio.Mode = LL_GPIO_MODE_OUTPUT;
114 gpio.Speed = LL_GPIO_SPEED_FREQ_HIGH;
115 gpio.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
116 LL_GPIO_Init(GPIOA, &gpio);
117 LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_12);
118 LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_13);
125 /* USER CODE BEGIN 2 */
130 LL_TIM_EnableCounter(TIM4);
131 LL_TIM_EnableIT_UPDATE(TIM4);
132 LL_TIM_GenerateEvent_UPDATE(TIM4);
136 byte buf[5] = { 0xff, 0xff, 10, 0xff, 0xff };
141 /* USER CODE END 2 */
144 /* USER CODE BEGIN WHILE */
147 if (rx_packet_state == 1 && !tx_packet_state)
149 static byte led_state;
151 LL_GPIO_SetOutputPin(LED_GPIO_Port, LED_Pin);
153 LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED_Pin);
156 // display_buffer(rx_packet);
158 tx_packet[0] = adjusted_temp >> 8;
159 tx_packet[1] = adjusted_temp & 0xff;
160 tx_packet[2] = adjusted_press >> 8;
161 tx_packet[3] = adjusted_press & 0xff;
162 usb_ep_send(&usb, 0x82, tx_packet, 8);
164 usb_ep_receive(&usb, 0x01, rx_packet, 64);
169 // debug_printf("Counter = %d\n", cnt);
170 // display_counter(cnt);
174 /* USER CODE END WHILE */
176 /* USER CODE BEGIN 3 */
179 /* USER CODE END 3 */
184 * @brief System Clock Configuration
187 void SystemClock_Config(void)
190 LL_FLASH_SetLatency(LL_FLASH_LATENCY_2);
192 if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_2)
198 /* Wait till HSE is ready */
199 while(LL_RCC_HSE_IsReady() != 1)
203 LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE_DIV_1, LL_RCC_PLL_MUL_9);
207 /* Wait till PLL is ready */
208 while(LL_RCC_PLL_IsReady() != 1)
212 LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
214 LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_2);
216 LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
218 LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
220 /* Wait till System clock is ready */
221 while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
225 LL_Init1msTick(72000000);
227 LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK);
229 LL_SetSystemCoreClock(72000000);
231 LL_RCC_SetUSBClockSource(LL_RCC_USB_CLKSOURCE_PLL_DIV_1_5);
233 /* SysTick_IRQn interrupt configuration */
234 NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
237 /* I2C1 init function */
238 static void MX_I2C1_Init(void)
241 LL_I2C_InitTypeDef I2C_InitStruct;
243 LL_GPIO_InitTypeDef GPIO_InitStruct;
245 /**I2C1 GPIO Configuration
249 GPIO_InitStruct.Pin = LL_GPIO_PIN_6|LL_GPIO_PIN_7;
250 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
251 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
252 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
253 LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
255 /* Peripheral clock enable */
256 LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1);
258 /* I2C1 interrupt Init */
259 NVIC_SetPriority(I2C1_EV_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
260 NVIC_EnableIRQ(I2C1_EV_IRQn);
262 /**I2C Initialization
264 I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C;
265 I2C_InitStruct.ClockSpeed = 400000;
266 I2C_InitStruct.DutyCycle = LL_I2C_DUTYCYCLE_2;
267 I2C_InitStruct.OwnAddress1 = 0;
268 I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK;
269 I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
270 LL_I2C_Init(I2C1, &I2C_InitStruct);
272 LL_I2C_SetOwnAddress2(I2C1, 0);
274 LL_I2C_DisableOwnAddress2(I2C1);
276 LL_I2C_DisableGeneralCall(I2C1);
278 LL_I2C_EnableClockStretching(I2C1);
282 /* I2C2 init function */
283 static void MX_I2C2_Init(void)
286 LL_I2C_InitTypeDef I2C_InitStruct;
288 LL_GPIO_InitTypeDef GPIO_InitStruct;
290 /**I2C2 GPIO Configuration
291 PB10 ------> I2C2_SCL
292 PB11 ------> I2C2_SDA
294 GPIO_InitStruct.Pin = LL_GPIO_PIN_10|LL_GPIO_PIN_11;
295 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
296 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
297 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
298 LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
300 /* Peripheral clock enable */
301 LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C2);
303 /**I2C Initialization
305 I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C;
306 I2C_InitStruct.ClockSpeed = 400000;
307 I2C_InitStruct.DutyCycle = LL_I2C_DUTYCYCLE_2;
308 I2C_InitStruct.OwnAddress1 = 0;
309 I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK;
310 I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
311 LL_I2C_Init(I2C2, &I2C_InitStruct);
313 LL_I2C_SetOwnAddress2(I2C2, 0);
315 LL_I2C_DisableOwnAddress2(I2C2);
317 LL_I2C_DisableGeneralCall(I2C2);
319 LL_I2C_EnableClockStretching(I2C2);
323 /* TIM4 init function */
324 static void MX_TIM4_Init(void)
327 LL_TIM_InitTypeDef TIM_InitStruct;
328 LL_TIM_OC_InitTypeDef TIM_OC_InitStruct;
330 /* Peripheral clock enable */
331 LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM4);
333 /* TIM4 interrupt Init */
334 NVIC_SetPriority(TIM4_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
335 NVIC_EnableIRQ(TIM4_IRQn);
337 TIM_InitStruct.Prescaler = 7200;
338 TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
339 TIM_InitStruct.Autoreload = 1000;
340 TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
341 LL_TIM_Init(TIM4, &TIM_InitStruct);
343 LL_TIM_DisableARRPreload(TIM4);
345 LL_TIM_SetClockSource(TIM4, LL_TIM_CLOCKSOURCE_INTERNAL);
347 TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_FROZEN;
348 TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
349 TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
350 TIM_OC_InitStruct.CompareValue = 0;
351 TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
352 LL_TIM_OC_Init(TIM4, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
354 LL_TIM_OC_DisableFast(TIM4, LL_TIM_CHANNEL_CH1);
356 LL_TIM_SetTriggerOutput(TIM4, LL_TIM_TRGO_RESET);
358 LL_TIM_DisableMasterSlaveMode(TIM4);
362 /* USB init function */
363 static void MX_USB_PCD_Init(void)
366 hpcd_USB_FS.Instance = USB;
367 hpcd_USB_FS.Init.dev_endpoints = 8;
368 hpcd_USB_FS.Init.speed = PCD_SPEED_FULL;
369 hpcd_USB_FS.Init.ep0_mps = DEP0CTL_MPS_64;
370 hpcd_USB_FS.Init.low_power_enable = DISABLE;
371 hpcd_USB_FS.Init.lpm_enable = DISABLE;
372 hpcd_USB_FS.Init.battery_charging_enable = DISABLE;
373 if (HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK)
375 _Error_Handler(__FILE__, __LINE__);
380 /** Configure pins as
387 static void MX_GPIO_Init(void)
390 LL_GPIO_InitTypeDef GPIO_InitStruct;
392 /* GPIO Ports Clock Enable */
393 LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC);
394 LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOD);
395 LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB);
396 LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
399 LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED_Pin);
402 GPIO_InitStruct.Pin = LED_Pin;
403 GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
404 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
405 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
406 LL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
409 GPIO_InitStruct.Pin = BMP_DONE_Pin;
410 GPIO_InitStruct.Mode = LL_GPIO_MODE_FLOATING;
411 LL_GPIO_Init(BMP_DONE_GPIO_Port, &GPIO_InitStruct);
415 /* USER CODE BEGIN 4 */
417 /* USER CODE END 4 */
420 * @brief This function is executed in case of error occurrence.
421 * @param file: The file name as string.
422 * @param line: The line in file as a number.
425 void _Error_Handler(char *file, int line)
427 /* USER CODE BEGIN Error_Handler_Debug */
428 /* User can add his own implementation to report the HAL error return state */
432 /* USER CODE END Error_Handler_Debug */
435 #ifdef USE_FULL_ASSERT
437 * @brief Reports the name of the source file and the source line number
438 * where the assert_param error has occurred.
439 * @param file: pointer to the source file name
440 * @param line: assert_param error line source number
443 void assert_failed(uint8_t* file, uint32_t line)
445 /* USER CODE BEGIN 6 */
446 /* User can add his own implementation to report the file name and line number,
447 tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
448 /* USER CODE END 6 */
450 #endif /* USE_FULL_ASSERT */
460 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/