#include #include #include #include static void gpio_init(void); static void clock_config(void); void board_init(void) { HAL_Init(); clock_config(); gpio_init(); MX_USB_DEVICE_Init(); } /* * Wait for a device to connect to the VCP */ void wait_for_vcp(void) { for (;;) { if (vcp_status(&hUsbDeviceFS.request)) break; } } void Error_Handler(void) { /* XXX - handle error */ } static void clock_config(void) { RCC_OscInitTypeDef RCC_OscInitStruct; RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_PeriphCLKInitTypeDef PeriphClkInit; /* Configure internal regulator output voltage */ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /* Initializes CPU, AHB and APB busses clocks */ RCC_OscInitStruct = (RCC_OscInitTypeDef){ .OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE, .HSEState = RCC_HSE_ON, .LSIState = RCC_LSI_ON, .PLL.PLLState = RCC_PLL_ON, .PLL.PLLSource = RCC_PLLSOURCE_HSE, .PLL.PLLMUL = RCC_PLL_MUL12, .PLL.PLLDIV = RCC_PLL_DIV3, }; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /* Initializes CPU, AHB and APB busses clocks */ RCC_ClkInitStruct = (RCC_ClkInitTypeDef){ .ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2, .SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK, .AHBCLKDivider = RCC_SYSCLK_DIV1, .APB1CLKDivider = RCC_HCLK_DIV1, .APB2CLKDivider = RCC_HCLK_DIV1, }; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { Error_Handler(); } PeriphClkInit = (RCC_PeriphCLKInitTypeDef){ .PeriphClockSelection = RCC_PERIPHCLK_RTC, .RTCClockSelection = RCC_RTCCLKSOURCE_LSI, }; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); } } static void gpio_init(void) { GPIO_InitTypeDef GPIO_InitStruct; __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOH_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3|GPIO_PIN_4, GPIO_PIN_RESET); /* configure PA3 and PA4 */ GPIO_InitStruct = (GPIO_InitTypeDef){ .Pin = GPIO_PIN_3|GPIO_PIN_4, .Mode = GPIO_MODE_OUTPUT_PP, .Pull = GPIO_NOPULL, .Speed = GPIO_SPEED_FREQ_LOW, }; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* configure PB0, PB1, PB10 and PB11 */ GPIO_InitStruct = (GPIO_InitTypeDef){ .Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_10|GPIO_PIN_11, .Mode = GPIO_MODE_INPUT, .Pull = GPIO_NOPULL, .Speed = GPIO_SPEED_FREQ_LOW, }; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET); /* configure PB8 */ GPIO_InitStruct = (GPIO_InitTypeDef){ .Pin = GPIO_PIN_8, .Mode = GPIO_MODE_OUTPUT_PP, .Pull = GPIO_NOPULL, .Speed = GPIO_SPEED_FREQ_LOW, }; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); }