|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, RT-Thread Development Team |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Change Logs: |
| 7 | + * Date Author Notes |
| 8 | + * 2025-01-17 Supperthomas first version |
| 9 | + */ |
| 10 | +#include <stm32l4xx.h> |
| 11 | +#include "rtthread.h" |
| 12 | +#include "drv_config.h" |
| 13 | +#include "usb_config.h" |
| 14 | +void usb_dc_low_level_init(uint8_t busid) |
| 15 | +{ |
| 16 | +#define USB_OverCurrent_Pin GPIO_PIN_5 |
| 17 | +#define USB_OverCurrent_GPIO_Port GPIOG |
| 18 | +#define USB_PowerSwitchOn_Pin GPIO_PIN_6 |
| 19 | +#define USB_PowerSwitchOn_GPIO_Port GPIOG |
| 20 | +#define STLK_RX_Pin GPIO_PIN_7 |
| 21 | +#define STLK_RX_GPIO_Port GPIOG |
| 22 | +#define STLK_TX_Pin GPIO_PIN_8 |
| 23 | +#define STLK_TX_GPIO_Port GPIOG |
| 24 | +#define USB_SOF_Pin GPIO_PIN_8 |
| 25 | +#define USB_SOF_GPIO_Port GPIOA |
| 26 | +#define USB_VBUS_Pin GPIO_PIN_9 |
| 27 | +#define USB_VBUS_GPIO_Port GPIOA |
| 28 | +#define USB_ID_Pin GPIO_PIN_10 |
| 29 | +#define USB_ID_GPIO_Port GPIOA |
| 30 | +#define USB_DM_Pin GPIO_PIN_11 |
| 31 | +#define USB_DM_GPIO_Port GPIOA |
| 32 | +#define USB_DP_Pin GPIO_PIN_12 |
| 33 | +#define USB_DP_GPIO_Port GPIOA |
| 34 | + GPIO_InitTypeDef GPIO_InitStruct = {0}; |
| 35 | + RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; |
| 36 | + /* USER CODE BEGIN USB_OTG_FS_MspInit 0 */ |
| 37 | + |
| 38 | + /* USER CODE END USB_OTG_FS_MspInit 0 */ |
| 39 | + |
| 40 | + /** Initializes the peripherals clock |
| 41 | + */ |
| 42 | + PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB; |
| 43 | + PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLLSAI1; |
| 44 | + PeriphClkInit.PLLSAI1.PLLSAI1Source = RCC_PLLSOURCE_MSI; |
| 45 | + PeriphClkInit.PLLSAI1.PLLSAI1M = 1; |
| 46 | + PeriphClkInit.PLLSAI1.PLLSAI1N = 24; |
| 47 | + PeriphClkInit.PLLSAI1.PLLSAI1P = RCC_PLLP_DIV2; |
| 48 | + PeriphClkInit.PLLSAI1.PLLSAI1Q = RCC_PLLQ_DIV2; |
| 49 | + PeriphClkInit.PLLSAI1.PLLSAI1R = RCC_PLLR_DIV2; |
| 50 | + PeriphClkInit.PLLSAI1.PLLSAI1ClockOut = RCC_PLLSAI1_48M2CLK; |
| 51 | + if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) |
| 52 | + { |
| 53 | + //Error_Handler(); |
| 54 | + } |
| 55 | + |
| 56 | + __HAL_RCC_GPIOA_CLK_ENABLE(); |
| 57 | + /**USB_OTG_FS GPIO Configuration |
| 58 | + PA8 ------> USB_OTG_FS_SOF |
| 59 | + PA9 ------> USB_OTG_FS_VBUS |
| 60 | + PA10 ------> USB_OTG_FS_ID |
| 61 | + PA11 ------> USB_OTG_FS_DM |
| 62 | + PA12 ------> USB_OTG_FS_DP |
| 63 | + */ |
| 64 | + GPIO_InitStruct.Pin = USB_SOF_Pin|USB_ID_Pin|USB_DM_Pin|USB_DP_Pin; |
| 65 | + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
| 66 | + GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 67 | + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
| 68 | + GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS; |
| 69 | + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 70 | + |
| 71 | + GPIO_InitStruct.Pin = USB_VBUS_Pin; |
| 72 | + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
| 73 | + GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 74 | + HAL_GPIO_Init(USB_VBUS_GPIO_Port, &GPIO_InitStruct); |
| 75 | + |
| 76 | + /* Peripheral clock enable */ |
| 77 | + __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); |
| 78 | + |
| 79 | + /* Enable VDDUSB */ |
| 80 | + if(__HAL_RCC_PWR_IS_CLK_DISABLED()) |
| 81 | + { |
| 82 | + __HAL_RCC_PWR_CLK_ENABLE(); |
| 83 | + HAL_PWREx_EnableVddUSB(); |
| 84 | + __HAL_RCC_PWR_CLK_DISABLE(); |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + HAL_PWREx_EnableVddUSB(); |
| 89 | + } |
| 90 | + /* USB_OTG_FS interrupt Init */ |
| 91 | + HAL_NVIC_SetPriority(OTG_FS_IRQn, 0, 0); |
| 92 | + HAL_NVIC_EnableIRQ(OTG_FS_IRQn); |
| 93 | + /* USER CODE BEGIN USB_OTG_FS_MspInit 1 */ |
| 94 | + |
| 95 | + /* USER CODE END USB_OTG_FS_MspInit 1 */ |
| 96 | + |
| 97 | +} |
| 98 | + |
| 99 | +void usb_dc_low_level_deinit(uint8_t busid) |
| 100 | +{ |
| 101 | + /* USER CODE BEGIN USB_OTG_FS_MspDeInit 0 */ |
| 102 | + |
| 103 | + /* USER CODE END USB_OTG_FS_MspDeInit 0 */ |
| 104 | + /* Peripheral clock disable */ |
| 105 | + __HAL_RCC_USB_OTG_FS_CLK_DISABLE(); |
| 106 | + |
| 107 | + /**USB_OTG_FS GPIO Configuration |
| 108 | + PA8 ------> USB_OTG_FS_SOF |
| 109 | + PA9 ------> USB_OTG_FS_VBUS |
| 110 | + PA10 ------> USB_OTG_FS_ID |
| 111 | + PA11 ------> USB_OTG_FS_DM |
| 112 | + PA12 ------> USB_OTG_FS_DP |
| 113 | + */ |
| 114 | + HAL_GPIO_DeInit(GPIOA, USB_SOF_Pin|USB_VBUS_Pin|USB_ID_Pin|USB_DM_Pin |
| 115 | + |USB_DP_Pin); |
| 116 | + |
| 117 | + /* Disable VDDUSB */ |
| 118 | + if(__HAL_RCC_PWR_IS_CLK_DISABLED()) |
| 119 | + { |
| 120 | + __HAL_RCC_PWR_CLK_ENABLE(); |
| 121 | + HAL_PWREx_DisableVddUSB(); |
| 122 | + __HAL_RCC_PWR_CLK_DISABLE(); |
| 123 | + } |
| 124 | + else |
| 125 | + { |
| 126 | + HAL_PWREx_DisableVddUSB(); |
| 127 | + } |
| 128 | + |
| 129 | + /* USB_OTG_FS interrupt DeInit */ |
| 130 | + HAL_NVIC_DisableIRQ(OTG_FS_IRQn); |
| 131 | + /* USER CODE BEGIN USB_OTG_FS_MspDeInit 1 */ |
| 132 | + |
| 133 | + /* USER CODE END USB_OTG_FS_MspDeInit 1 */ |
| 134 | + |
| 135 | +} |
| 136 | + |
| 137 | +#ifdef RT_CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM |
| 138 | +/* Register the EMAC device */ |
| 139 | +static int rt_hw_stm32_cherryusb_cdc_init(void) |
| 140 | +{ |
| 141 | + extern void cdc_acm_init(uint8_t busid, uintptr_t reg_base); |
| 142 | + cdc_acm_init(0, USB_OTG_FS_PERIPH_BASE); |
| 143 | + |
| 144 | + return 0; |
| 145 | +} |
| 146 | +INIT_COMPONENT_EXPORT(rt_hw_stm32_cherryusb_cdc_init); |
| 147 | +static int cherry_usb_cdc_send(int argc, char **argv) |
| 148 | +{ |
| 149 | + extern void cdc_acm_data_send_with_dtr_test(uint8_t busid); |
| 150 | + cdc_acm_data_send_with_dtr_test(0); |
| 151 | + return 0; |
| 152 | +} |
| 153 | +MSH_CMD_EXPORT(cherry_usb_cdc_send, send the cdc data for test) |
| 154 | +#endif |
| 155 | + |
| 156 | +#ifdef USBD_IRQ_HANDLER |
| 157 | +void USBD_IRQ_HANDLER(void) |
| 158 | +{ |
| 159 | + extern void USBD_IRQHandler(uint8_t busid); |
| 160 | + USBD_IRQHandler(0); |
| 161 | +} |
| 162 | +#else |
| 163 | +#error USBD_IRQ_HANDLER need to USB IRQ like #define USBD_IRQ_HANDLER OTG_HS_IRQHandler |
| 164 | +#endif |
| 165 | + |
0 commit comments