| 
									
										
										
										
											2019-03-20 16:11:42 +07:00
										 |  |  | /* 
 | 
					
						
							|  |  |  |  * The MIT License (MIT) | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-05-14 11:48:05 +07:00
										 |  |  |  * Copyright (c) 2019 Ha Thach (tinyusb.org) | 
					
						
							| 
									
										
										
										
											2019-03-20 16:11:42 +07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Permission is hereby granted, free of charge, to any person obtaining a copy | 
					
						
							|  |  |  |  * of this software and associated documentation files (the "Software"), to deal | 
					
						
							|  |  |  |  * in the Software without restriction, including without limitation the rights | 
					
						
							|  |  |  |  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
					
						
							|  |  |  |  * copies of the Software, and to permit persons to whom the Software is | 
					
						
							|  |  |  |  * furnished to do so, subject to the following conditions: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The above copyright notice and this permission notice shall be included in | 
					
						
							|  |  |  |  * all copies or substantial portions of the Software. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
					
						
							|  |  |  |  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
					
						
							|  |  |  |  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
					
						
							|  |  |  |  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
					
						
							|  |  |  |  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
					
						
							|  |  |  |  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
					
						
							|  |  |  |  * THE SOFTWARE. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is part of the TinyUSB stack. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-11-27 18:11:03 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-22 22:06:48 +07:00
										 |  |  | #include "../board.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-27 18:11:03 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-25 23:52:40 -04:00
										 |  |  | #include "stm32f4xx_hal.h"
 | 
					
						
							| 
									
										
										
										
											2019-07-22 18:35:22 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 16:37:09 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // Forward USB interrupt events to TinyUSB IRQ Handler
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | void OTG_FS_IRQHandler(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-04-17 12:27:53 +07:00
										 |  |  |   tud_int_handler(0); | 
					
						
							| 
									
										
										
										
											2020-04-08 16:37:09 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // MACRO TYPEDEF CONSTANT ENUM
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-02 14:58:46 +07:00
										 |  |  | #define LED_PORT              GPIOD
 | 
					
						
							|  |  |  | #define LED_PIN               GPIO_PIN_14
 | 
					
						
							|  |  |  | #define LED_STATE_ON          1
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define BUTTON_PORT           GPIOA
 | 
					
						
							|  |  |  | #define BUTTON_PIN            GPIO_PIN_0
 | 
					
						
							|  |  |  | #define BUTTON_STATE_ACTIVE   1
 | 
					
						
							| 
									
										
										
										
											2018-11-27 18:11:03 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-25 23:52:40 -04:00
										 |  |  | // Enable PA2 as the debug log UART
 | 
					
						
							|  |  |  | // It is not routed to the ST/Link on the Discovery board.
 | 
					
						
							|  |  |  | #define UARTx                 USART2
 | 
					
						
							|  |  |  | #define UART_GPIO_PORT        GPIOA
 | 
					
						
							|  |  |  | #define UART_GPIO_AF          GPIO_AF7_USART2
 | 
					
						
							|  |  |  | #define UART_TX_PIN           GPIO_PIN_2
 | 
					
						
							|  |  |  | #define UART_RX_PIN           GPIO_PIN_3
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | UART_HandleTypeDef UartHandle; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-11 14:03:36 +07:00
										 |  |  | // enable all LED, Button, Uart, USB clock
 | 
					
						
							|  |  |  | static void all_rcc_clk_enable(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   __HAL_RCC_GPIOA_CLK_ENABLE();  // USB D+, D-, Button
 | 
					
						
							|  |  |  |   __HAL_RCC_GPIOD_CLK_ENABLE();  // LED
 | 
					
						
							| 
									
										
										
										
											2020-03-25 23:52:40 -04:00
										 |  |  |   __HAL_RCC_USART2_CLK_ENABLE(); // Uart module
 | 
					
						
							| 
									
										
										
										
											2019-12-11 14:03:36 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 18:53:52 +07:00
										 |  |  | /**
 | 
					
						
							|  |  |  |   * @brief  System Clock Configuration | 
					
						
							|  |  |  |   *         The system Clock is configured as follow : | 
					
						
							|  |  |  |   *            System Clock source            = PLL (HSE) | 
					
						
							|  |  |  |   *            SYSCLK(Hz)                     = 168000000 | 
					
						
							|  |  |  |   *            HCLK(Hz)                       = 168000000 | 
					
						
							|  |  |  |   *            AHB Prescaler                  = 1 | 
					
						
							|  |  |  |   *            APB1 Prescaler                 = 4 | 
					
						
							|  |  |  |   *            APB2 Prescaler                 = 2 | 
					
						
							|  |  |  |   *            HSE Frequency(Hz)              = 8000000 | 
					
						
							| 
									
										
										
										
											2020-01-10 15:10:16 +07:00
										 |  |  |   *            PLL_M                          = HSE_VALUE/1000000 | 
					
						
							| 
									
										
										
										
											2019-07-22 18:53:52 +07:00
										 |  |  |   *            PLL_N                          = 336 | 
					
						
							|  |  |  |   *            PLL_P                          = 2 | 
					
						
							|  |  |  |   *            PLL_Q                          = 7 | 
					
						
							|  |  |  |   *            VDD(V)                         = 3.3 | 
					
						
							|  |  |  |   *            Main regulator output voltage  = Scale1 mode | 
					
						
							|  |  |  |   *            Flash Latency(WS)              = 5 | 
					
						
							|  |  |  |   * @param  None | 
					
						
							|  |  |  |   * @retval None | 
					
						
							| 
									
										
										
										
											2019-07-22 18:35:22 +07:00
										 |  |  |   */ | 
					
						
							| 
									
										
										
										
											2019-07-22 18:53:52 +07:00
										 |  |  | static void SystemClock_Config(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-07-22 18:35:22 +07:00
										 |  |  |   RCC_ClkInitTypeDef RCC_ClkInitStruct; | 
					
						
							|  |  |  |   RCC_OscInitTypeDef RCC_OscInitStruct; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Enable Power Control clock */ | 
					
						
							|  |  |  |   __HAL_RCC_PWR_CLK_ENABLE(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* The voltage scaling allows optimizing the power consumption when the device is
 | 
					
						
							|  |  |  |      clocked below the maximum system frequency, to update the voltage scaling value | 
					
						
							|  |  |  |      regarding system frequency refer to product datasheet.  */ | 
					
						
							|  |  |  |   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Enable HSE Oscillator and activate PLL with HSE as source */ | 
					
						
							|  |  |  |   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; | 
					
						
							|  |  |  |   RCC_OscInitStruct.HSEState = RCC_HSE_ON; | 
					
						
							|  |  |  |   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; | 
					
						
							|  |  |  |   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; | 
					
						
							| 
									
										
										
										
											2020-01-10 15:10:16 +07:00
										 |  |  |   RCC_OscInitStruct.PLL.PLLM = HSE_VALUE/1000000; | 
					
						
							| 
									
										
										
										
											2019-07-22 18:35:22 +07:00
										 |  |  |   RCC_OscInitStruct.PLL.PLLN = 336; | 
					
						
							|  |  |  |   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; | 
					
						
							|  |  |  |   RCC_OscInitStruct.PLL.PLLQ = 7; | 
					
						
							|  |  |  |   HAL_RCC_OscConfig(&RCC_OscInitStruct); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
 | 
					
						
							|  |  |  |      clocks dividers */ | 
					
						
							|  |  |  |   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); | 
					
						
							|  |  |  |   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; | 
					
						
							|  |  |  |   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; | 
					
						
							|  |  |  |   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; | 
					
						
							|  |  |  |   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; | 
					
						
							|  |  |  |   HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5); | 
					
						
							| 
									
										
										
										
											2019-07-22 18:53:52 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void board_init(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-25 23:52:40 -04:00
										 |  |  |   SystemClock_Config(); | 
					
						
							|  |  |  |   all_rcc_clk_enable(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-28 11:19:50 -04:00
										 |  |  | #if CFG_TUSB_OS == OPT_OS_NONE
 | 
					
						
							| 
									
										
										
										
											2019-07-22 18:53:52 +07:00
										 |  |  |   // 1ms tick timer
 | 
					
						
							|  |  |  |   SysTick_Config(SystemCoreClock / 1000); | 
					
						
							|  |  |  | #elif CFG_TUSB_OS == OPT_OS_FREERTOS
 | 
					
						
							|  |  |  |   // If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
 | 
					
						
							|  |  |  |   //NVIC_SetPriority(USB0_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-22 18:35:22 +07:00
										 |  |  |   GPIO_InitTypeDef  GPIO_InitStruct; | 
					
						
							| 
									
										
										
										
											2018-11-27 18:11:03 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-02 14:58:46 +07:00
										 |  |  |   // LED
 | 
					
						
							|  |  |  |   GPIO_InitStruct.Pin = LED_PIN; | 
					
						
							|  |  |  |   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; | 
					
						
							|  |  |  |   GPIO_InitStruct.Pull = GPIO_PULLUP; | 
					
						
							|  |  |  |   GPIO_InitStruct.Speed = GPIO_SPEED_FAST; | 
					
						
							|  |  |  |   HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   board_led_write(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Button
 | 
					
						
							|  |  |  |   GPIO_InitStruct.Pin = BUTTON_PIN; | 
					
						
							|  |  |  |   GPIO_InitStruct.Mode = GPIO_MODE_INPUT; | 
					
						
							|  |  |  |   GPIO_InitStruct.Pull = GPIO_PULLDOWN; | 
					
						
							|  |  |  |   GPIO_InitStruct.Speed = GPIO_SPEED_FAST; | 
					
						
							|  |  |  |   HAL_GPIO_Init(BUTTON_PORT, &GPIO_InitStruct); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-27 18:11:03 -08:00
										 |  |  |   // USB Pin Init
 | 
					
						
							| 
									
										
										
										
											2019-01-13 17:34:09 -05:00
										 |  |  |   // PA9- VUSB, PA10- ID, PA11- DM, PA12- DP
 | 
					
						
							| 
									
										
										
										
											2019-07-22 18:35:22 +07:00
										 |  |  |   /* Configure DM DP Pins */ | 
					
						
							|  |  |  |   GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12; | 
					
						
							|  |  |  |   GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; | 
					
						
							|  |  |  |   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; | 
					
						
							|  |  |  |   GPIO_InitStruct.Pull = GPIO_NOPULL; | 
					
						
							|  |  |  |   GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; | 
					
						
							|  |  |  |   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* Configure VBUS Pin */ | 
					
						
							|  |  |  |   GPIO_InitStruct.Pin = GPIO_PIN_9; | 
					
						
							|  |  |  |   GPIO_InitStruct.Mode = GPIO_MODE_INPUT; | 
					
						
							|  |  |  |   GPIO_InitStruct.Pull = GPIO_NOPULL; | 
					
						
							|  |  |  |   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* This for ID line debug */ | 
					
						
							|  |  |  |   GPIO_InitStruct.Pin = GPIO_PIN_10; | 
					
						
							|  |  |  |   GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; | 
					
						
							|  |  |  |   GPIO_InitStruct.Pull = GPIO_PULLUP; | 
					
						
							|  |  |  |   GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; | 
					
						
							|  |  |  |   GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; | 
					
						
							|  |  |  |   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); | 
					
						
							| 
									
										
										
										
											2019-09-11 17:37:23 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-25 23:52:40 -04:00
										 |  |  |   GPIO_InitStruct.Pin       = UART_TX_PIN | UART_RX_PIN; | 
					
						
							|  |  |  |   GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP; | 
					
						
							|  |  |  |   GPIO_InitStruct.Pull      = GPIO_PULLUP; | 
					
						
							|  |  |  |   GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_HIGH; | 
					
						
							|  |  |  |   GPIO_InitStruct.Alternate = UART_GPIO_AF; | 
					
						
							|  |  |  |   HAL_GPIO_Init(UART_GPIO_PORT, &GPIO_InitStruct); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   UartHandle = (UART_HandleTypeDef){ | 
					
						
							|  |  |  |     .Instance        = UARTx, | 
					
						
							|  |  |  |     .Init.BaudRate   = CFG_BOARD_UART_BAUDRATE, | 
					
						
							|  |  |  |     .Init.WordLength = UART_WORDLENGTH_8B, | 
					
						
							|  |  |  |     .Init.StopBits   = UART_STOPBITS_1, | 
					
						
							|  |  |  |     .Init.Parity     = UART_PARITY_NONE, | 
					
						
							|  |  |  |     .Init.HwFlowCtl  = UART_HWCONTROL_NONE, | 
					
						
							|  |  |  |     .Init.Mode       = UART_MODE_TX_RX, | 
					
						
							|  |  |  |     .Init.OverSampling = UART_OVERSAMPLING_16 | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   HAL_UART_Init(&UartHandle); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-11 17:37:23 +07:00
										 |  |  |   // Enable USB OTG clock
 | 
					
						
							|  |  |  |   __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); | 
					
						
							| 
									
										
										
										
											2019-12-11 14:03:36 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Enable VBUS sense (B device) via pin PA9
 | 
					
						
							|  |  |  |   USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_NOVBUSSENS; | 
					
						
							|  |  |  |   USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_VBUSBSEN; | 
					
						
							| 
									
										
										
										
											2018-11-27 18:11:03 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-15 17:14:15 +07:00
										 |  |  | //--------------------------------------------------------------------+
 | 
					
						
							|  |  |  | // Board porting API
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2018-11-27 18:11:03 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-02 18:16:04 +07:00
										 |  |  | void board_led_write(bool state) | 
					
						
							| 
									
										
										
										
											2018-11-27 18:11:03 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-09-02 14:58:46 +07:00
										 |  |  |   HAL_GPIO_WritePin(LED_PORT, LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON)); | 
					
						
							| 
									
										
										
										
											2018-11-27 18:11:03 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-02 02:16:54 +07:00
										 |  |  | uint32_t board_button_read(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-09-02 14:58:46 +07:00
										 |  |  |   return BUTTON_STATE_ACTIVE == HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN); | 
					
						
							| 
									
										
										
										
											2019-04-02 02:16:54 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-24 12:20:06 +07:00
										 |  |  | int board_uart_read(uint8_t* buf, int len) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   (void) buf; (void) len; | 
					
						
							|  |  |  |   return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int board_uart_write(void const * buf, int len) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-03-25 23:52:40 -04:00
										 |  |  |   HAL_UART_Transmit(&UartHandle, (uint8_t*) buf, len, 0xffff); | 
					
						
							|  |  |  |   return len; | 
					
						
							| 
									
										
										
										
											2019-10-24 12:20:06 +07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-27 18:11:03 -08:00
										 |  |  | #if CFG_TUSB_OS  == OPT_OS_NONE
 | 
					
						
							|  |  |  | volatile uint32_t system_ticks = 0; | 
					
						
							|  |  |  | void SysTick_Handler (void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   system_ticks++; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-23 22:31:07 +07:00
										 |  |  | uint32_t board_millis(void) | 
					
						
							| 
									
										
										
										
											2019-03-23 16:51:07 +07:00
										 |  |  | { | 
					
						
							|  |  |  |   return system_ticks; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-11-27 18:11:03 -08:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void HardFault_Handler (void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   asm("bkpt"); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-03-04 10:41:05 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Required by __libc_init_array in startup code if we are compiling using
 | 
					
						
							|  |  |  | // -nostdlib/-nostartfiles.
 | 
					
						
							|  |  |  | void _init(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |