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_USB_PCD_Init(void);
66 static void MX_TIM4_Init(void);
67 static void MX_USART1_UART_Init(void);
69 /* USER CODE BEGIN PFP */
70 /* Private function prototypes -----------------------------------------------*/
72 /* USER CODE END PFP */
74 /* USER CODE BEGIN 0 */
79 * @brief The application entry point.
85 /* USER CODE BEGIN 1 */
89 /* MCU Configuration----------------------------------------------------------*/
91 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
94 /* USER CODE BEGIN Init */
96 /* USER CODE END Init */
98 /* Configure the system clock */
101 /* USER CODE BEGIN SysInit */
102 usb_init(&usb, &hpcd_USB_FS);
104 /* USER CODE END SysInit */
106 /* Initialize all configured peripherals */
109 // A hack to let USB host reset us
110 LL_GPIO_InitTypeDef gpio;
111 gpio.Pin = LL_GPIO_PIN_12 | LL_GPIO_PIN_13;
112 gpio.Mode = LL_GPIO_MODE_OUTPUT;
113 gpio.Speed = LL_GPIO_SPEED_FREQ_HIGH;
114 gpio.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
115 LL_GPIO_Init(GPIOA, &gpio);
116 LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_12);
117 LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_13);
122 MX_USART1_UART_Init();
123 /* USER CODE BEGIN 2 */
126 LL_TIM_EnableCounter(TIM4);
127 LL_TIM_EnableIT_UPDATE(TIM4);
128 LL_TIM_GenerateEvent_UPDATE(TIM4);
130 /* USER CODE END 2 */
133 /* USER CODE BEGIN WHILE */
136 if (rx_packet_state == 1 && !tx_packet_state)
138 static byte led_state;
140 LL_GPIO_SetOutputPin(LED_GPIO_Port, LED_Pin);
142 LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED_Pin);
146 put_u32_be(tx_packet, 42);
147 usb_ep_send(&usb, 0x82, tx_packet, 12);
149 usb_ep_receive(&usb, 0x01, rx_packet, 64);
152 // debug_printf("Counter = %d\n", cnt);
156 /* USER CODE END WHILE */
158 /* USER CODE BEGIN 3 */
161 /* USER CODE END 3 */
166 * @brief System Clock Configuration
169 void SystemClock_Config(void)
172 LL_FLASH_SetLatency(LL_FLASH_LATENCY_2);
174 if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_2)
180 /* Wait till HSE is ready */
181 while(LL_RCC_HSE_IsReady() != 1)
185 LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE_DIV_1, LL_RCC_PLL_MUL_9);
189 /* Wait till PLL is ready */
190 while(LL_RCC_PLL_IsReady() != 1)
194 LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
196 LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_2);
198 LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
200 LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
202 /* Wait till System clock is ready */
203 while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
207 LL_Init1msTick(72000000);
209 LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK);
211 LL_SetSystemCoreClock(72000000);
213 LL_RCC_SetUSBClockSource(LL_RCC_USB_CLKSOURCE_PLL_DIV_1_5);
215 /* SysTick_IRQn interrupt configuration */
216 NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
219 /* TIM4 init function */
220 static void MX_TIM4_Init(void)
223 LL_TIM_InitTypeDef TIM_InitStruct;
224 LL_TIM_OC_InitTypeDef TIM_OC_InitStruct;
226 /* Peripheral clock enable */
227 LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM4);
229 /* TIM4 interrupt Init */
230 NVIC_SetPriority(TIM4_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
231 NVIC_EnableIRQ(TIM4_IRQn);
233 TIM_InitStruct.Prescaler = 7200;
234 TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
235 TIM_InitStruct.Autoreload = 1000;
236 TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
237 LL_TIM_Init(TIM4, &TIM_InitStruct);
239 LL_TIM_DisableARRPreload(TIM4);
241 LL_TIM_SetClockSource(TIM4, LL_TIM_CLOCKSOURCE_INTERNAL);
243 TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_FROZEN;
244 TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
245 TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
246 TIM_OC_InitStruct.CompareValue = 0;
247 TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
248 LL_TIM_OC_Init(TIM4, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
250 LL_TIM_OC_DisableFast(TIM4, LL_TIM_CHANNEL_CH1);
252 LL_TIM_SetTriggerOutput(TIM4, LL_TIM_TRGO_RESET);
254 LL_TIM_DisableMasterSlaveMode(TIM4);
258 /* USART1 init function */
259 static void MX_USART1_UART_Init(void)
262 LL_USART_InitTypeDef USART_InitStruct;
264 LL_GPIO_InitTypeDef GPIO_InitStruct;
266 /* Peripheral clock enable */
267 LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);
269 /**USART1 GPIO Configuration
270 PA9 ------> USART1_TX
271 PA10 ------> USART1_RX
273 GPIO_InitStruct.Pin = LL_GPIO_PIN_9;
274 GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
275 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
276 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
277 LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
279 GPIO_InitStruct.Pin = LL_GPIO_PIN_10;
280 GPIO_InitStruct.Mode = LL_GPIO_MODE_FLOATING;
281 LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
283 USART_InitStruct.BaudRate = 115200;
284 USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
285 USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
286 USART_InitStruct.Parity = LL_USART_PARITY_NONE;
287 USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
288 USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
289 LL_USART_Init(USART1, &USART_InitStruct);
291 LL_USART_ConfigAsyncMode(USART1);
293 LL_USART_Enable(USART1);
297 /* USB init function */
298 static void MX_USB_PCD_Init(void)
301 hpcd_USB_FS.Instance = USB;
302 hpcd_USB_FS.Init.dev_endpoints = 8;
303 hpcd_USB_FS.Init.speed = PCD_SPEED_FULL;
304 hpcd_USB_FS.Init.ep0_mps = DEP0CTL_MPS_64;
305 hpcd_USB_FS.Init.low_power_enable = DISABLE;
306 hpcd_USB_FS.Init.lpm_enable = DISABLE;
307 hpcd_USB_FS.Init.battery_charging_enable = DISABLE;
308 if (HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK)
310 _Error_Handler(__FILE__, __LINE__);
315 /** Configure pins as
322 static void MX_GPIO_Init(void)
325 LL_GPIO_InitTypeDef GPIO_InitStruct;
327 /* GPIO Ports Clock Enable */
328 LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC);
329 LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOD);
330 LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB);
331 LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
334 LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED_Pin);
337 LL_GPIO_ResetOutputPin(GPIOB, SSR3_Pin|SSR4_Pin|SSR1_Pin|SSR2_Pin);
340 GPIO_InitStruct.Pin = LED_Pin;
341 GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
342 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
343 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
344 LL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
347 GPIO_InitStruct.Pin = SSR3_Pin|SSR4_Pin|SSR1_Pin|SSR2_Pin;
348 GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
349 GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
350 GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
351 LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
355 /* USER CODE BEGIN 4 */
357 /* USER CODE END 4 */
360 * @brief This function is executed in case of error occurrence.
361 * @param file: The file name as string.
362 * @param line: The line in file as a number.
365 void _Error_Handler(char *file, int line)
367 /* USER CODE BEGIN Error_Handler_Debug */
368 /* User can add his own implementation to report the HAL error return state */
372 /* USER CODE END Error_Handler_Debug */
375 #ifdef USE_FULL_ASSERT
377 * @brief Reports the name of the source file and the source line number
378 * where the assert_param error has occurred.
379 * @param file: pointer to the source file name
380 * @param line: assert_param error line source number
383 void assert_failed(uint8_t* file, uint32_t line)
385 /* USER CODE BEGIN 6 */
386 /* User can add his own implementation to report the file name and line number,
387 tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
388 /* USER CODE END 6 */
390 #endif /* USE_FULL_ASSERT */
400 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/