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"
44 /* USER CODE BEGIN Includes */
46 /* USER CODE END Includes */
48 /* Private variables ---------------------------------------------------------*/
50 PCD_HandleTypeDef hpcd_USB_FS;
52 /* USER CODE BEGIN PV */
53 /* Private variables ---------------------------------------------------------*/
56 /* USER CODE END PV */
58 /* Private function prototypes -----------------------------------------------*/
59 void SystemClock_Config(void);
60 static void MX_GPIO_Init(void);
61 static void MX_I2C1_Init(void);
62 static void MX_I2C2_Init(void);
63 static void MX_USB_PCD_Init(void);
65 /* USER CODE BEGIN PFP */
66 /* Private function prototypes -----------------------------------------------*/
68 /* USER CODE END PFP */
70 /* USER CODE BEGIN 0 */
76 void semi_put_char(char c)
78 // This is tricky, we need to work around GCC bugs
81 "mov r0, #0x03\n" /* SYS_WRITEC */
90 void semi_write_string(char *c)
93 "mov r0, #0x04\n" /* SYS_WRITE0 */
102 void debug_putc(int c)
104 static char debug_buf[16];
106 debug_buf[debug_i++] = c;
107 if (c == '\n' || debug_i >= sizeof(debug_buf) - 1)
109 debug_buf[debug_i] = 0;
110 semi_write_string(debug_buf);
115 void debug_puts(const char *s)
129 static void printf_string(const char *s, uint width, uint flags)
131 uint len = strlen(s);
132 uint pad = (len < width) ? width - len : 0;
133 char pad_char = (flags & PF_ZERO_PAD) ? '0' : ' ';
138 debug_putc(pad_char);
139 if (!(flags & PF_LEFT))
143 static void printf_number(uint i, uint width, uint flags, uint base)
146 char *w = buf + sizeof(buf);
148 if (flags & PF_SIGNED)
153 flags |= PF_NEGATIVE;
160 uint digit = i % base;
164 *--w = ((flags & PF_UPPERCASE) ? 'A' : 'a') + digit - 10;
169 if (flags & PF_NEGATIVE)
172 printf_string(w, width, flags);
175 void debug_printf(const char *fmt, ...)
201 flags |= PF_ZERO_PAD;
204 while (*fmt >= '0' && *fmt <= '9')
205 width = 10*width + *fmt++ - '0';
211 printf_number(va_arg(args, int), width, flags | PF_SIGNED, 10);
214 printf_number(va_arg(args, int), width, flags, 10);
217 flags |= PF_UPPERCASE;
220 printf_number(va_arg(args, int), width, flags, 16);
223 printf_string(va_arg(args, char *), width, flags);
234 /* USER CODE END 0 */
237 * @brief The application entry point.
243 /* USER CODE BEGIN 1 */
245 /* USER CODE END 1 */
247 /* MCU Configuration----------------------------------------------------------*/
249 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
252 /* USER CODE BEGIN Init */
254 /* USER CODE END Init */
256 /* Configure the system clock */
257 SystemClock_Config();
259 /* USER CODE BEGIN SysInit */
261 /* USER CODE END SysInit */
263 /* Initialize all configured peripherals */
268 /* USER CODE BEGIN 2 */
269 usb_init(&usb, &hpcd_USB_FS);
271 /* USER CODE END 2 */
274 /* USER CODE BEGIN WHILE */
278 LL_GPIO_SetOutputPin(LED_GPIO_Port, LED_Pin);
280 LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED_Pin);
282 debug_printf("Counter = %d\n", cnt++);
284 /* USER CODE END WHILE */
286 /* USER CODE BEGIN 3 */
289 /* USER CODE END 3 */
294 * @brief System Clock Configuration
297 void SystemClock_Config(void)
300 LL_FLASH_SetLatency(LL_FLASH_LATENCY_2);
302 if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_2)
308 /* Wait till HSE is ready */
309 while(LL_RCC_HSE_IsReady() != 1)
313 LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE_DIV_1, LL_RCC_PLL_MUL_9);
317 /* Wait till PLL is ready */
318 while(LL_RCC_PLL_IsReady() != 1)
322 LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
324 LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_2);
326 LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
328 LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
330 /* Wait till System clock is ready */
331 while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
335 LL_Init1msTick(72000000);
337 LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK);
339 LL_SetSystemCoreClock(72000000);
341 LL_RCC_SetUSBClockSource(LL_RCC_USB_CLKSOURCE_PLL_DIV_1_5);
343 /* SysTick_IRQn interrupt configuration */
344 NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
347 /* I2C1 init function */
348 static void MX_I2C1_Init(void)
351 LL_I2C_InitTypeDef I2C_InitStruct;
353 LL_GPIO_InitTypeDef GPIO_InitStruct;
355 /**I2C1 GPIO Configuration
359 GPIO_InitStruct.Pin = LL_GPIO_PIN_6|LL_GPIO_PIN_7;
360 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
361 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
362 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
363 LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
365 /* Peripheral clock enable */
366 LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1);
368 /**I2C Initialization
370 I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C;
371 I2C_InitStruct.ClockSpeed = 100000;
372 I2C_InitStruct.DutyCycle = LL_I2C_DUTYCYCLE_2;
373 I2C_InitStruct.OwnAddress1 = 0;
374 I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK;
375 I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
376 LL_I2C_Init(I2C1, &I2C_InitStruct);
378 LL_I2C_SetOwnAddress2(I2C1, 0);
380 LL_I2C_DisableOwnAddress2(I2C1);
382 LL_I2C_DisableGeneralCall(I2C1);
384 LL_I2C_EnableClockStretching(I2C1);
388 /* I2C2 init function */
389 static void MX_I2C2_Init(void)
392 LL_I2C_InitTypeDef I2C_InitStruct;
394 LL_GPIO_InitTypeDef GPIO_InitStruct;
396 /**I2C2 GPIO Configuration
397 PB10 ------> I2C2_SCL
398 PB11 ------> I2C2_SDA
400 GPIO_InitStruct.Pin = LL_GPIO_PIN_10|LL_GPIO_PIN_11;
401 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
402 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
403 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
404 LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
406 /* Peripheral clock enable */
407 LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C2);
409 /**I2C Initialization
411 I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C;
412 I2C_InitStruct.ClockSpeed = 400000;
413 I2C_InitStruct.DutyCycle = LL_I2C_DUTYCYCLE_2;
414 I2C_InitStruct.OwnAddress1 = 0;
415 I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK;
416 I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
417 LL_I2C_Init(I2C2, &I2C_InitStruct);
419 LL_I2C_SetOwnAddress2(I2C2, 0);
421 LL_I2C_DisableOwnAddress2(I2C2);
423 LL_I2C_DisableGeneralCall(I2C2);
425 LL_I2C_EnableClockStretching(I2C2);
429 /* USB init function */
430 static void MX_USB_PCD_Init(void)
432 hpcd_USB_FS.Instance = USB;
433 hpcd_USB_FS.Init.dev_endpoints = 8;
434 hpcd_USB_FS.Init.speed = PCD_SPEED_FULL;
435 hpcd_USB_FS.Init.ep0_mps = DEP0CTL_MPS_64;
436 hpcd_USB_FS.Init.low_power_enable = DISABLE;
437 hpcd_USB_FS.Init.lpm_enable = DISABLE;
438 hpcd_USB_FS.Init.battery_charging_enable = DISABLE;
439 if (HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK)
441 _Error_Handler(__FILE__, __LINE__);
445 /** Configure pins as
452 static void MX_GPIO_Init(void)
455 LL_GPIO_InitTypeDef GPIO_InitStruct;
457 /* GPIO Ports Clock Enable */
458 LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC);
459 LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOD);
460 LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB);
461 LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
464 LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED_Pin);
467 GPIO_InitStruct.Pin = LED_Pin;
468 GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
469 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
470 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
471 LL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
474 GPIO_InitStruct.Pin = BMP_DONE_Pin;
475 GPIO_InitStruct.Mode = LL_GPIO_MODE_FLOATING;
476 LL_GPIO_Init(BMP_DONE_GPIO_Port, &GPIO_InitStruct);
480 /* USER CODE BEGIN 4 */
482 /* USER CODE END 4 */
485 * @brief This function is executed in case of error occurrence.
486 * @param file: The file name as string.
487 * @param line: The line in file as a number.
490 void _Error_Handler(char *file, int line)
492 /* USER CODE BEGIN Error_Handler_Debug */
493 /* User can add his own implementation to report the HAL error return state */
497 /* USER CODE END Error_Handler_Debug */
500 #ifdef USE_FULL_ASSERT
502 * @brief Reports the name of the source file and the source line number
503 * where the assert_param error has occurred.
504 * @param file: pointer to the source file name
505 * @param line: assert_param error line source number
508 void assert_failed(uint8_t* file, uint32_t line)
510 /* USER CODE BEGIN 6 */
511 /* User can add his own implementation to report the file name and line number,
512 tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
513 /* USER CODE END 6 */
515 #endif /* USE_FULL_ASSERT */
525 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/