diff --git a/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32F103RB.h b/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_IP_DEVICE.h similarity index 53% rename from features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32F103RB.h rename to features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_IP_DEVICE.h index 271610b217b..d7cfc5b35c1 100644 --- a/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32F103RB.h +++ b/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_IP_DEVICE.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2016 mbed.org, MIT License +/* Copyright (c) 2017 mbed.org, MIT License * * 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 @@ -15,16 +15,29 @@ * 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. */ -#ifndef USBHAL_STM32F103RB -#define USBHAL_STM32F103RB +#ifndef USBHAL_IP_DEVICE_H +#define USBHAL_IP_DEVICE_H +#if defined(TARGET_NUCLEO_F303ZE) +#define USBHAL_IRQn USB_LP_CAN_RX0_IRQn + +#elif defined(TARGET_NUCLEO_F103RB) #define USBHAL_IRQn USB_LP_CAN1_RX0_IRQn +#elif defined(TARGET_DISCO_L072CZ_LRWAN1) || \ + defined(TARGET_DISCO_L053C8) +#define USBHAL_IRQn USB_IRQn + +#else +#error "USB IRQ is not configured !" +#endif + +#define NB_ENDPOINT 8 // Must be a multiple of 4 bytes -#define NB_ENDPOINT 8 -/* must be multiple of 4 bytes */ #define MAXTRANSFER_SIZE 0x200 -#define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3) + +#define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE + MAX_PACKET_SIZE_EP0 + MAX_PACKET_SIZE_EP1 + MAX_PACKET_SIZE_EP2 + MAX_PACKET_SIZE_EP3) + #if (FIFO_USB_RAM_SIZE > 0x500) #error "FIFO dimensioning incorrect" #endif @@ -32,34 +45,33 @@ typedef struct { USBHAL *inst; + void (USBHAL::*bus_reset)(void); void (USBHAL::*sof)(int frame); - void (USBHAL::*connect_change)(unsigned int connected); + void (USBHAL::*connect_change)(unsigned int connected); void (USBHAL::*suspend_change)(unsigned int suspended); void (USBHAL::*ep0_setup)(void); void (USBHAL::*ep0_in)(void); void (USBHAL::*ep0_out)(void); void (USBHAL::*ep0_read)(void); bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags); - bool (USBHAL::*epCallback[2*NB_ENDPOINT-2])(void); - uint8_t epComplete[8]; - /* memorize dummy buffer used for reception */ - uint32_t pBufRx[MAXTRANSFER_SIZE>>2]; - uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2]; - gpio_t usb_switch; -}USBHAL_Private_t; + bool (USBHAL::*epCallback[(2 * NB_ENDPOINT) - 2])(void); -void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state) -{ - USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData)); - gpio_write(&(priv->usb_switch),!state); -} + uint8_t epComplete[2 * NB_ENDPOINT]; + + /* Memorize dummy buffer used for reception */ + uint32_t pBufRx[MAXTRANSFER_SIZE >> 2]; + uint32_t pBufRx0[MAX_PACKET_SIZE_EP0 >> 2]; + + gpio_t usb_switch; +} USBHAL_Private_t; uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo) { return 1024; } -void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) + +void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) { USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData)); USBHAL *obj= priv->inst; @@ -68,24 +80,31 @@ void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) (obj->*func)(sofnum); } -USBHAL * USBHAL::instance; - -USBHAL::USBHAL(void) +void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state) { - /* init parameter */ + USBHAL_Private_t *priv = ((USBHAL_Private_t *)(hpcd->pData)); +#if defined(TARGET_NUCLEO_F103RB) + gpio_write(&(priv->usb_switch), !state); +#else + gpio_write(&(priv->usb_switch), state); +#endif +} + +USBHAL *USBHAL::instance; + +USBHAL::USBHAL(void) { USBHAL_Private_t *HALPriv = new(USBHAL_Private_t); - /* initialized all field of init including 0 field */ - /* constructor does not fill with zero */ + hpcd.Instance = USB; - /* initialized all field of init including 0 field */ - /* constructor does not fill with zero */ + memset(&hpcd.Init, 0, sizeof(hpcd.Init)); hpcd.Init.dev_endpoints = NB_ENDPOINT; - hpcd.Init.ep0_mps = MAX_PACKET_SIZE_EP0; + hpcd.Init.ep0_mps = MAX_PACKET_SIZE_EP0; hpcd.Init.phy_itface = PCD_PHY_EMBEDDED; hpcd.Init.Sof_enable = 1; hpcd.Init.speed = PCD_SPEED_FULL; - /* pass instance for usage inside call back */ + + // Pass instance for usage inside call back HALPriv->inst = this; HALPriv->bus_reset = &USBHAL::busReset; HALPriv->suspend_change = &USBHAL::suspendStateChanged; @@ -96,7 +115,6 @@ USBHAL::USBHAL(void) HALPriv->ep0_in = &USBHAL::EP0in; HALPriv->ep0_out = &USBHAL::EP0out; HALPriv->ep0_read = &USBHAL::EP0read; - hpcd.pData = (void*)HALPriv; HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback; HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback; HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback; @@ -105,43 +123,56 @@ USBHAL::USBHAL(void) HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback; instance = this; + // Configure USB pins and other clocks +#if defined(TARGET_NUCLEO_F303ZE) + __HAL_RCC_GPIOA_CLK_ENABLE(); + pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF14_USB)); // DM + pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF14_USB)); // DP + __HAL_RCC_GPIOG_CLK_ENABLE(); + gpio_init_out(&HALPriv->usb_switch, PG_6); + +#elif defined(TARGET_NUCLEO_F103RB) + // Make sure to connect a 1.5K resistor between USB-DP PA12 pin and +3.3V + __HAL_RCC_GPIOA_CLK_ENABLE(); + pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_MODE_AF_INPUT)); // DM + pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_MODE_AF_INPUT)); // DP + +#elif defined(TARGET_DISCO_L072CZ_LRWAN1) || \ + defined(TARGET_DISCO_L053C8) + __HAL_RCC_GPIOA_CLK_ENABLE(); + pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF2_USB)); // DM + pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF2_USB)); // DP - /* Configure USB VBUS GPIO */ - gpio_init_out(&HALPriv->usb_switch,PB_14); - gpio_mode(&HALPriv->usb_switch,OpenDrain); - /* Configure USB FS GPIOs */ - - /* Configure DM DP Pins - * - USB-DP (D+ of the USB connector) <======> PA12 (Nucleo board) - * Make sure to connect a 1.5KOhm pull up to USB-DP PA12 pin - * (permanent pull-up) - - USB-DM (D- of the USB connector) <======> PA11 (Nucleo board) - */ - - pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_MODE_AF_INPUT)); - pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_MODE_AF_INPUT)); +#else +#error "USB pins are not configured !" +#endif __HAL_RCC_USB_CLK_ENABLE(); + __HAL_RCC_SYSCFG_CLK_ENABLE(); + // Configure PCD and FIFOs + hpcd.pData = (void*)HALPriv; hpcd.State = HAL_PCD_STATE_RESET; - HAL_PCD_Init(&hpcd); - /* hardcoded size of FIFO according definition*/ - HAL_PCDEx_PMAConfig(&hpcd , 0x00 , PCD_SNG_BUF, 0x30); - HAL_PCDEx_PMAConfig(&hpcd , 0x80 , PCD_SNG_BUF, 0x70); - HAL_PCDEx_PMAConfig(&hpcd , 0x01 , PCD_SNG_BUF, 0x90); - HAL_PCDEx_PMAConfig(&hpcd , 0x81 , PCD_SNG_BUF, 0xb0); -#if 0 - HAL_PCDEx_PMAConfig(&hpcd , 0x2, PCD_DBL_BUF, 0x018000b0); -#else - HAL_PCDEx_PMAConfig(&hpcd , 0x2, PCD_SNG_BUF, 0x100); -#endif - HAL_PCDEx_PMAConfig(&hpcd , 0x82, PCD_SNG_BUF, 0x120); - NVIC_SetVector(USBHAL_IRQn,(uint32_t)&_usbisr); - NVIC_SetPriority( USBHAL_IRQn, 1); + // EP0 + HAL_PCDEx_PMAConfig(&hpcd, 0x00, PCD_SNG_BUF, 0x30); + HAL_PCDEx_PMAConfig(&hpcd, 0x80, PCD_SNG_BUF, 0x70); + // EP1 + HAL_PCDEx_PMAConfig(&hpcd, 0x01, PCD_SNG_BUF, 0x90); + HAL_PCDEx_PMAConfig(&hpcd, 0x81, PCD_SNG_BUF, 0xb0); + // EP2 + HAL_PCDEx_PMAConfig(&hpcd, 0x02, PCD_SNG_BUF, 0x100); + HAL_PCDEx_PMAConfig(&hpcd, 0x82, PCD_SNG_BUF, 0x120); + // EP3 + HAL_PCDEx_PMAConfig(&hpcd, 0x03, PCD_DBL_BUF, 0x018000b0); + HAL_PCDEx_PMAConfig(&hpcd, 0x83, PCD_SNG_BUF, 0xb0); + + // Configure interrupt vector + NVIC_SetVector(USBHAL_IRQn, (uint32_t)&_usbisr); + NVIC_SetPriority(USBHAL_IRQn, 1); HAL_PCD_Start(&hpcd); } -#endif +#endif // USBHAL_IP_DEVICE_H diff --git a/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32F769NI.h b/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_IP_OTGFSHS.h similarity index 55% rename from features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32F769NI.h rename to features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_IP_OTGFSHS.h index f2bfa7a1025..e79e01be476 100644 --- a/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32F769NI.h +++ b/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_IP_OTGFSHS.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2016 mbed.org, MIT License +/* Copyright (c) 2017 mbed.org, MIT License * * 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 @@ -15,13 +15,31 @@ * 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. */ -#ifndef USBHAL_STM32F769NI_H -#define USBHAL_STM32F769NI_H +#ifndef USBHAL_IP_OTGFSHS_H +#define USBHAL_IP_OTGFSHS_H + +//================================================================== +// This board has both USB OTG FS and HS connectors. +// Select one line only. +//================================================================== +#if defined(TARGET_DISCO_F746NG) +//#define TARGET_DISCO_F746NG_OTG_FS +#define TARGET_DISCO_F746NG_OTG_HS +#endif + +#if defined(TARGET_DISCO_F769NI) || \ + defined(TARGET_DISCO_F746NG_OTG_HS) #define USBHAL_IRQn OTG_HS_IRQn -/* must be multiple of 4 bytes */ -#define NB_ENDPOINT 4 +#else +#define USBHAL_IRQn OTG_FS_IRQn +#endif + +#define NB_ENDPOINT 4 // Must be a multiple of 4 bytes + #define MAXTRANSFER_SIZE 0x200 -#define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3) + +#define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE + MAX_PACKET_SIZE_EP0 + MAX_PACKET_SIZE_EP1 + MAX_PACKET_SIZE_EP2 + MAX_PACKET_SIZE_EP3) + #if (FIFO_USB_RAM_SIZE > 0x500) #error "FIFO dimensioning incorrect" #endif @@ -29,30 +47,37 @@ typedef struct { USBHAL *inst; + void (USBHAL::*bus_reset)(void); void (USBHAL::*sof)(int frame); - void (USBHAL::*connect_change)(unsigned int connected); + void (USBHAL::*connect_change)(unsigned int connected); void (USBHAL::*suspend_change)(unsigned int suspended); void (USBHAL::*ep0_setup)(void); void (USBHAL::*ep0_in)(void); void (USBHAL::*ep0_out)(void); void (USBHAL::*ep0_read)(void); bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags); - bool (USBHAL::*epCallback[2*NB_ENDPOINT-2])(void); - /* memorize dummy buffer used for reception */ - uint32_t pBufRx[MAXTRANSFER_SIZE>>2]; - uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2]; - uint8_t epComplete[2*NB_ENDPOINT]; -}USBHAL_Private_t; + bool (USBHAL::*epCallback[(2 * NB_ENDPOINT) - 2])(void); + + uint8_t epComplete[2 * NB_ENDPOINT]; + + /* Memorize dummy buffer used for reception */ + uint32_t pBufRx[MAXTRANSFER_SIZE >> 2]; + uint32_t pBufRx0[MAX_PACKET_SIZE_EP0 >> 2]; +} USBHAL_Private_t; uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo) { uint32_t len; - if (fifo == 0) len = hpcd->Instance->DIEPTXF0_HNPTXFSIZ>>16; - else + if (fifo == 0) { + len = hpcd->Instance->DIEPTXF0_HNPTXFSIZ >> 16; + } + else { len = hpcd->Instance->DIEPTXF[fifo - 1] >> 16; - return len*4; + } + return len * 4; } + void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) { USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData)); @@ -60,27 +85,32 @@ void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) USB_OTG_GlobalTypeDef *USBx = hpcd->Instance; uint32_t sofnum = (USBx_DEVICE->DSTS & USB_OTG_DSTS_FNSOF) >> 8; void (USBHAL::*func)(int frame) = priv->sof; - /* fix me call with same frame number */ (obj->*func)(sofnum); } - -USBHAL * USBHAL::instance; +USBHAL *USBHAL::instance; USBHAL::USBHAL(void) { - /* init parameter */ USBHAL_Private_t *HALPriv = new(USBHAL_Private_t); - hpcd.Instance = USB_OTG_HS; + memset(&hpcd.Init, 0, sizeof(hpcd.Init)); - hpcd.Init.dev_endpoints = NB_ENDPOINT; - hpcd.Init.ep0_mps = MAX_PACKET_SIZE_EP0; + +#if defined(TARGET_DISCO_F769NI) || \ + defined(TARGET_DISCO_F746NG_OTG_HS) + hpcd.Instance = USB_OTG_HS; hpcd.Init.phy_itface = PCD_PHY_ULPI; hpcd.Init.Sof_enable = 0; - hpcd.Init.speed = PCD_SPEED_HIGH; - //hpcd.Init.vbus_sensing_enable = 0; - //hpcd.Init.lpm_enable = 0; - /* pass instance for usage inside call back */ +#else + hpcd.Instance = USB_OTG_FS; + hpcd.Init.phy_itface = PCD_PHY_EMBEDDED; + hpcd.Init.Sof_enable = 1; + hpcd.Init.speed = PCD_SPEED_FULL; +#endif + hpcd.Init.dev_endpoints = NB_ENDPOINT; + hpcd.Init.ep0_mps = MAX_PACKET_SIZE_EP0; + + // Pass instance for usage inside call back HALPriv->inst = this; HALPriv->bus_reset = &USBHAL::busReset; HALPriv->suspend_change = &USBHAL::suspendStateChanged; @@ -91,7 +121,6 @@ USBHAL::USBHAL(void) { HALPriv->ep0_in = &USBHAL::EP0in; HALPriv->ep0_out = &USBHAL::EP0out; HALPriv->ep0_read = &USBHAL::EP0read; - hpcd.pData = (void*)HALPriv; HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback; HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback; HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback; @@ -99,16 +128,49 @@ USBHAL::USBHAL(void) { HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback; HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback; instance = this; - /* Enable power and clocking */ + + // Configure USB pins and other clocks + +#if defined(TARGET_NUCLEO_F207ZG) || \ + defined(TARGET_NUCLEO_F401RE) || \ + defined(TARGET_NUCLEO_F411RE) || \ + defined(TARGET_NUCLEO_F412ZG) || \ + defined(TARGET_NUCLEO_F429ZI) || \ + defined(TARGET_NUCLEO_F446RE) || \ + defined(TARGET_NUCLEO_F446ZE) || \ + defined(TARGET_NUCLEO_F767ZI) || \ + defined(TARGET_NUCLEO_F746ZG) || \ + defined(TARGET_DISCO_F407VG) || \ + defined(TARGET_DISCO_F469NI) || \ + defined(TARGET_DISCO_F746NG_OTG_FS) + __HAL_RCC_GPIOA_CLK_ENABLE(); + pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); // DM + pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); // DP + pin_function(PA_9, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, GPIO_AF10_OTG_FS)); // VBUS + pin_function(PA_10, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_PULLUP, GPIO_AF10_OTG_FS)); // ID + pin_function(PA_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); // SOF + __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); + +#elif defined(TARGET_DISCO_L475VG_IOT01A) || \ + defined(TARGET_DISCO_L476VG) + __HAL_RCC_GPIOA_CLK_ENABLE(); + pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); // DM + pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); // DP + __HAL_RCC_GPIOC_CLK_ENABLE(); + pin_function(PC_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); // VBUS + __HAL_RCC_PWR_CLK_ENABLE(); + HAL_PWREx_EnableVddUSB(); + __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); + +#elif defined(TARGET_DISCO_F769NI) || \ + defined(TARGET_DISCO_F746NG_OTG_HS) __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOH_CLK_ENABLE(); __HAL_RCC_GPIOI_CLK_ENABLE(); - pin_function(PA_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // CLK pin_function(PA_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // D0 - pin_function(PB_0, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // D1 pin_function(PB_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // D2 pin_function(PB_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // D3 @@ -116,32 +178,46 @@ USBHAL::USBHAL(void) { pin_function(PB_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // D5 pin_function(PB_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // D6 pin_function(PB_13, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // D7 - pin_function(PC_0, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // STP pin_function(PH_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // NXT +#if defined(TARGET_DISCO_F769NI) pin_function(PI_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // DIR - +#else // TARGET_DISCO_F746NG + pin_function(PC_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_HS)); // DIR +#endif __HAL_RCC_USB_OTG_HS_ULPI_CLK_ENABLE(); __HAL_RCC_USB_OTG_HS_CLK_ENABLE(); +#else +#error "USB pins are not configured !" +#endif + __HAL_RCC_SYSCFG_CLK_ENABLE(); + + // Configure PCD and FIFOs + hpcd.pData = (void*)HALPriv; hpcd.State = HAL_PCD_STATE_RESET; HAL_PCD_Init(&hpcd); - /* 1.25kbytes */ + + /* 1.25 kbytes */ /* min value 16 (= 16 x 4 bytes) */ - /* max value 256 (= 1K bytes ) */ - /* maximum sum is 0x140 */ - HAL_PCDEx_SetRxFiFo(&hpcd, (MAXTRANSFER_SIZE/4)); - /* bulk/int 64 bytes in FS */ - HAL_PCDEx_SetTxFiFo(&hpcd, 0, (MAX_PACKET_SIZE_EP0/4)+1); - /* bulk/int bytes in FS */ - HAL_PCDEx_SetTxFiFo(&hpcd, 1, (MAX_PACKET_SIZE_EP1/4)+1); - HAL_PCDEx_SetTxFiFo(&hpcd, 2, (MAX_PACKET_SIZE_EP2/4)); - /* ISOchronous */ - HAL_PCDEx_SetTxFiFo(&hpcd, 3, (MAX_PACKET_SIZE_EP3/4)); + /* max value 256 (= 1K bytes ) */ + /* maximum sum is 0x140 */ + HAL_PCDEx_SetRxFiFo(&hpcd, (MAXTRANSFER_SIZE / 4)); + // EP0 = Bulk/Int 64 bytes in FS + HAL_PCDEx_SetTxFiFo(&hpcd, 0, (MAX_PACKET_SIZE_EP0 / 4) + 1); + // EP1 = Bulk/Int bytes in FS + HAL_PCDEx_SetTxFiFo(&hpcd, 1, (MAX_PACKET_SIZE_EP1 / 4) + 1); + // EP2 + HAL_PCDEx_SetTxFiFo(&hpcd, 2, (MAX_PACKET_SIZE_EP2 / 4)); + // EP3 = ISOchronous + HAL_PCDEx_SetTxFiFo(&hpcd, 3, (MAX_PACKET_SIZE_EP3 / 4)); + + // Configure interrupt vector NVIC_SetVector(USBHAL_IRQn, (uint32_t)&_usbisr); NVIC_SetPriority(USBHAL_IRQn, 1); + HAL_PCD_Start(&hpcd); } -#endif +#endif // USBHAL_IP_OTGFSHS_H diff --git a/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32.cpp b/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32.cpp index 36c0caf3590..6953f4a0783 100644 --- a/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32.cpp +++ b/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2010-2011 mbed.org, MIT License +/* Copyright (c) 2017 mbed.org, MIT License * * 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 @@ -15,11 +15,13 @@ * 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. */ + /* TARGET NOT STM does not support this HAL */ #ifndef TARGET_STM #define USBSTM_HAL_UNSUPPORTED #endif -/* F4 famlily wihtout USB_STM_HAL use another HAL*/ + +/* STM32F4 family without USB_STM_HAL use another HAL */ #if defined(TARGET_STM) && defined(TARGET_STM32F4) && !defined(USB_STM_HAL) #define USBSTM_HAL_UNSUPPORTED #endif @@ -27,15 +29,15 @@ #ifndef USBSTM_HAL_UNSUPPORTED #include "USBHAL.h" #include "pinmap.h" -/* mbed endpoint definition to hal definition */ + +#include "USBHAL_STM32.h" + +/* mbed endpoint definition to hal definition */ #define EP_ADDR(ep) (((ep) >> 1)|((ep) & 1) << 7) + /* from hal definition to mbed definition */ #define ADDR_EPIN(ep) (((ep) << 1) | 1) #define ADDR_EPOUT(ep) (((ep) << 1)) -/* id to detect if rx buffer is used or not */ - -#include "USBHAL_STM_TARGET.h" - /* this call at device reception completion on a Out Enpoint */ void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum) @@ -318,15 +320,12 @@ bool USBHAL::getEndpointStallState(uint8_t endpoint) { void USBHAL::remoteWakeup(void) { } - void USBHAL::_usbisr(void) { instance->usbisr(); } - void USBHAL::usbisr(void) { - HAL_PCD_IRQHandler(&instance->hpcd); } -#endif +#endif diff --git a/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM_TARGET.h b/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32.h similarity index 54% rename from features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM_TARGET.h rename to features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32.h index b380ba85945..1ed92bad54d 100644 --- a/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM_TARGET.h +++ b/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32.h @@ -15,29 +15,35 @@ * 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. */ +#ifndef USBHAL_STM32_H +#define USBHAL_STM32_H + +#if defined(TARGET_NUCLEO_F207ZG) || \ + defined(TARGET_NUCLEO_F401RE) || \ + defined(TARGET_NUCLEO_F411RE) || \ + defined(TARGET_NUCLEO_F412ZG) || \ + defined(TARGET_NUCLEO_F429ZI) || \ + defined(TARGET_NUCLEO_F446RE) || \ + defined(TARGET_NUCLEO_F446ZE) || \ + defined(TARGET_NUCLEO_F767ZI) || \ + defined(TARGET_NUCLEO_F746ZG) || \ + defined(TARGET_DISCO_F407VG) || \ + defined(TARGET_DISCO_F469NI) || \ + defined(TARGET_DISCO_F746NG) || \ + defined(TARGET_DISCO_F769NI) || \ + defined(TARGET_DISCO_L475VG_IOT01A) || \ + defined(TARGET_DISCO_L476VG) +#include "USBHAL_IP_OTGFSHS.h" + +#elif defined(TARGET_NUCLEO_F103RB) || \ + defined(TARGET_NUCLEO_F303ZE) || \ + defined(TARGET_DISCO_L053C8) || \ + defined(TARGET_DISCO_L072CZ_LRWAN1) +#include "USBHAL_IP_DEVICE.h" + +#else +#error "Target not supported !" -#if defined(TARGET_DISCO_L476VG) -#include "USBHAL_STM32L476VG.h" - -#elif defined(TARGET_DISCO_L475VG_IOT01A) -#include "USBHAL_STM32L475VG.h" - -#elif defined(TARGET_DISCO_L072CZ_LRWAN1) -#include "USBHAL_STM32L072CZ.h" - -#elif defined(TARGET_NUCLEO_F303ZE) -#include "USBHAL_STM32F303ZE.h" - -#elif defined(TARGET_NUCLEO_F103RB) -#include "USBHAL_STM32F103RB.h" - -#elif defined(TARGET_DISCO_F769NI) -#include "USBHAL_STM32F769NI.h" - -#elif defined(TARGET_DISCO_L053C8) -#include "USBHAL_STM32L053C8.h" - -#else /* default configuration */ -#include "USBHAL_STM_144_64pins.h" - #endif + +#endif // USBHAL_STM32_H diff --git a/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32F303ZE.h b/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32F303ZE.h deleted file mode 100644 index fa49ba280a7..00000000000 --- a/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32F303ZE.h +++ /dev/null @@ -1,127 +0,0 @@ -/* Copyright (c) 2016 mbed.org, MIT License -* -* 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. -*/ -#ifndef USBHAL_STM32F303ZE_H -#define USBHAL_STM32F303ZE_H -#define USBHAL_IRQn USB_LP_CAN_RX0_IRQn -/* must be multiple of 4 bytes */ -#define NB_ENDPOINT 8 -#define MAXTRANSFER_SIZE 0x200 -#define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3) -#if (FIFO_USB_RAM_SIZE > 0x500) -#error "FIFO dimensioning incorrect" -#endif - -typedef struct -{ - USBHAL *inst; - void (USBHAL::*bus_reset)(void); - void (USBHAL::*sof)(int frame); - void (USBHAL::*connect_change)(unsigned int connected); - void (USBHAL::*suspend_change)(unsigned int suspended); - void (USBHAL::*ep0_setup)(void); - void (USBHAL::*ep0_in)(void); - void (USBHAL::*ep0_out)(void); - void (USBHAL::*ep0_read)(void); - bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags); - bool (USBHAL::*epCallback[6])(void); - uint8_t epComplete[2*NB_ENDPOINT]; - /* memorize dummy buffer used for reception */ - uint32_t pBufRx[MAXTRANSFER_SIZE>>2]; - uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2]; - gpio_t usb_switch; -}USBHAL_Private_t; - -uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo) -{ - return 1024; -} - -void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state){ - USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData)); - gpio_write(&(priv->usb_switch),state); -} - -void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) { - USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData)); - USBHAL *obj= priv->inst; - uint32_t sofnum = (hpcd->Instance->FNR) & USB_FNR_FN; - void (USBHAL::*func)(int frame) = priv->sof; - (obj->*func)(sofnum); -} - -USBHAL * USBHAL::instance; - -USBHAL::USBHAL(void) { - /* init parameter */ - USBHAL_Private_t *HALPriv = new(USBHAL_Private_t); - hpcd.Instance = USB; - /* initialized Init to zero (constructor does not zero initialized the - * area */ - /* initialized all field of init including 0 field */ - /* constructor does not fill with zero */ - memset(&hpcd.Init, 0, sizeof(hpcd.Init)); - hpcd.Init.dev_endpoints = NB_ENDPOINT; - hpcd.Init.ep0_mps = MAX_PACKET_SIZE_EP0; - hpcd.Init.phy_itface = PCD_PHY_EMBEDDED; - hpcd.Init.Sof_enable = 1; - hpcd.Init.speed = PCD_SPEED_FULL; - /* pass instance for usage inside call back */ - HALPriv->inst = this; - HALPriv->bus_reset = &USBHAL::busReset; - HALPriv->suspend_change = &USBHAL::suspendStateChanged; - HALPriv->connect_change = &USBHAL::connectStateChanged; - HALPriv->sof = &USBHAL::SOF; - HALPriv->ep0_setup = &USBHAL::EP0setupCallback; - HALPriv->ep_realise = &USBHAL::realiseEndpoint; - HALPriv->ep0_in = &USBHAL::EP0in; - HALPriv->ep0_out = &USBHAL::EP0out; - HALPriv->ep0_read = &USBHAL::EP0read; - hpcd.pData = (void*)HALPriv; - HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback; - HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback; - HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback; - HALPriv->epCallback[3] = &USBHAL::EP2_IN_callback; - HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback; - HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback; - instance = this; - __HAL_RCC_GPIOA_CLK_ENABLE(); - /* Configure USB DM pin. This is optional, and maintained only for user guidance. */ - pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF14_USB)); - pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF14_USB)); - __HAL_RCC_GPIOG_CLK_ENABLE(); - gpio_init_out(&HALPriv->usb_switch,PG_6); - /* Enable USB Clock */ - __HAL_RCC_USB_CLK_ENABLE(); - /* Enable SYSCFG Clock */ - __HAL_RCC_SYSCFG_CLK_ENABLE(); - hpcd.State = HAL_PCD_STATE_RESET; - HAL_PCD_Init(&hpcd); - /* hardcoded size of FIFO according definition*/ - HAL_PCDEx_PMAConfig(&hpcd , 0x00 , PCD_SNG_BUF, 0x30); - HAL_PCDEx_PMAConfig(&hpcd , 0x80 , PCD_SNG_BUF, 0x70); -#if 1 - HAL_PCDEx_PMAConfig(&hpcd , 0x3, PCD_DBL_BUF, 0x018000b0); -#else - HAL_PCDEx_PMAConfig(&hpcd , 0x3, PCD_SNG_BUF, 0x180); -#endif - HAL_PCDEx_PMAConfig(&hpcd , 0x83, PCD_SNG_BUF, 0xb0); - NVIC_SetVector(USBHAL_IRQn,(uint32_t)&_usbisr); - NVIC_SetPriority(USBHAL_IRQn, 1); - HAL_PCD_Start(&hpcd); -} -#endif diff --git a/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32L053C8.h b/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32L053C8.h deleted file mode 100644 index 69b6b8196cb..00000000000 --- a/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32L053C8.h +++ /dev/null @@ -1,134 +0,0 @@ -/* Copyright (c) 2016 mbed.org, MIT License -* -* 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. -*/ -#ifndef USBHAL_STM32L053C8_H -#define USBHAL_STM32L053C8_H - -#define USBHAL_IRQn USB_IRQn - -/* must be multiple of 4 bytes */ -#define NB_ENDPOINT 8 -#define MAXTRANSFER_SIZE 0x200 -#define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3) -#if (FIFO_USB_RAM_SIZE > 0x500) -#error "FIFO dimensioning incorrect" -#endif - -typedef struct -{ - USBHAL *inst; - void (USBHAL::*bus_reset)(void); - void (USBHAL::*sof)(int frame); - void (USBHAL::*connect_change)(unsigned int connected); - void (USBHAL::*suspend_change)(unsigned int suspended); - void (USBHAL::*ep0_setup)(void); - void (USBHAL::*ep0_in)(void); - void (USBHAL::*ep0_out)(void); - void (USBHAL::*ep0_read)(void); - bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags); - bool (USBHAL::*epCallback[6])(void); - uint8_t epComplete[2*NB_ENDPOINT]; - /* memorize dummy buffer used for reception */ - uint32_t pBufRx[MAXTRANSFER_SIZE>>2]; - uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2]; - gpio_t usb_switch; -}USBHAL_Private_t; - -uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo) -{ - return 1024; -} - -void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state) -{ - USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData)); - gpio_write(&(priv->usb_switch),state); -} - -void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) -{ - USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData)); - USBHAL *obj= priv->inst; - uint32_t sofnum = (hpcd->Instance->FNR) & USB_FNR_FN; - void (USBHAL::*func)(int frame) = priv->sof; - (obj->*func)(sofnum); -} - -USBHAL * USBHAL::instance; - -USBHAL::USBHAL(void) -{ - /* init parameter */ - USBHAL_Private_t *HALPriv = new(USBHAL_Private_t); - hpcd.Instance = USB; - /* initialized Init to zero (constructor does not zero initialized the - * area */ - /* initialized all field of init including 0 field */ - /* constructor does not fill with zero */ - memset(&hpcd.Init, 0, sizeof(hpcd.Init)); - hpcd.Init.dev_endpoints = NB_ENDPOINT; - hpcd.Init.ep0_mps = MAX_PACKET_SIZE_EP0; - hpcd.Init.phy_itface = PCD_PHY_EMBEDDED; - hpcd.Init.Sof_enable = 1; - hpcd.Init.speed = PCD_SPEED_FULL; - /* pass instance for usage inside call back */ - HALPriv->inst = this; - HALPriv->bus_reset = &USBHAL::busReset; - HALPriv->suspend_change = &USBHAL::suspendStateChanged; - HALPriv->connect_change = &USBHAL::connectStateChanged; - HALPriv->sof = &USBHAL::SOF; - HALPriv->ep0_setup = &USBHAL::EP0setupCallback; - HALPriv->ep_realise = &USBHAL::realiseEndpoint; - HALPriv->ep0_in = &USBHAL::EP0in; - HALPriv->ep0_out = &USBHAL::EP0out; - HALPriv->ep0_read = &USBHAL::EP0read; - hpcd.pData = (void*)HALPriv; - HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback; - HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback; - HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback; - HALPriv->epCallback[3] = &USBHAL::EP2_IN_callback; - HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback; - HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback; - instance = this; - - /* Configure USB DM pin. This is optional, and maintained only for user guidance. */ - __HAL_RCC_GPIOA_CLK_ENABLE(); - pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF2_USB)); - pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF2_USB)); - - /* Enable USB Clock */ - __HAL_RCC_USB_CLK_ENABLE(); - - /* Enable SYSCFG Clock */ - __HAL_RCC_SYSCFG_CLK_ENABLE(); - hpcd.State = HAL_PCD_STATE_RESET; - HAL_PCD_Init(&hpcd); - - /* hardcoded size of FIFO according definition*/ - HAL_PCDEx_PMAConfig(&hpcd , 0x00 , PCD_SNG_BUF, 0x30); - HAL_PCDEx_PMAConfig(&hpcd , 0x80 , PCD_SNG_BUF, 0x70); -#if 1 - HAL_PCDEx_PMAConfig(&hpcd , 0x3, PCD_DBL_BUF, 0x018000b0); -#else - HAL_PCDEx_PMAConfig(&hpcd , 0x3, PCD_SNG_BUF, 0x180); -#endif - HAL_PCDEx_PMAConfig(&hpcd , 0x83, PCD_SNG_BUF, 0xb0); - NVIC_SetVector(USBHAL_IRQn,(uint32_t)&_usbisr); - NVIC_SetPriority(USBHAL_IRQn, 1); - HAL_PCD_Start(&hpcd); -} -#endif diff --git a/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32L072CZ.h b/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32L072CZ.h deleted file mode 100644 index 628701af4bb..00000000000 --- a/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32L072CZ.h +++ /dev/null @@ -1,131 +0,0 @@ -/* Copyright (c) 2016 mbed.org, MIT License -* -* 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. -*/ -#ifndef USBHAL_STM32L072CZ_H -#define USBHAL_STM32L072CZ_H - -#define USBHAL_IRQn USB_IRQn - -/* must be multiple of 4 bytes */ -#define NB_ENDPOINT 8 -#define MAXTRANSFER_SIZE 0x200 -#define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3) -#if (FIFO_USB_RAM_SIZE > 0x500) -#error "FIFO dimensioning incorrect" -#endif - -typedef struct -{ - USBHAL *inst; - void (USBHAL::*bus_reset)(void); - void (USBHAL::*sof)(int frame); - void (USBHAL::*connect_change)(unsigned int connected); - void (USBHAL::*suspend_change)(unsigned int suspended); - void (USBHAL::*ep0_setup)(void); - void (USBHAL::*ep0_in)(void); - void (USBHAL::*ep0_out)(void); - void (USBHAL::*ep0_read)(void); - bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags); - bool (USBHAL::*epCallback[6])(void); - uint8_t epComplete[2*NB_ENDPOINT]; - /* memorize dummy buffer used for reception */ - uint32_t pBufRx[MAXTRANSFER_SIZE>>2]; - uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2]; - gpio_t usb_switch; -}USBHAL_Private_t; - -uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo) -{ - return 1024; -} - -void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state) -{ - USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData)); - gpio_write(&(priv->usb_switch),state); -} - -void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) -{ - USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData)); - USBHAL *obj= priv->inst; - uint32_t sofnum = (hpcd->Instance->FNR) & USB_FNR_FN; - void (USBHAL::*func)(int frame) = priv->sof; - (obj->*func)(sofnum); -} - -USBHAL * USBHAL::instance; - -USBHAL::USBHAL(void) -{ - /* init parameter */ - USBHAL_Private_t *HALPriv = new(USBHAL_Private_t); - hpcd.Instance = USB; - /* initialized Init to zero (constructor does not zero initialized the - * area */ - /* initialized all field of init including 0 field */ - /* constructor does not fill with zero */ - memset(&hpcd.Init, 0, sizeof(hpcd.Init)); - hpcd.Init.dev_endpoints = NB_ENDPOINT; - hpcd.Init.ep0_mps = MAX_PACKET_SIZE_EP0; - hpcd.Init.phy_itface = PCD_PHY_EMBEDDED; - hpcd.Init.Sof_enable = 1; - hpcd.Init.speed = PCD_SPEED_FULL; - /* pass instance for usage inside call back */ - HALPriv->inst = this; - HALPriv->bus_reset = &USBHAL::busReset; - HALPriv->suspend_change = &USBHAL::suspendStateChanged; - HALPriv->connect_change = &USBHAL::connectStateChanged; - HALPriv->sof = &USBHAL::SOF; - HALPriv->ep0_setup = &USBHAL::EP0setupCallback; - HALPriv->ep_realise = &USBHAL::realiseEndpoint; - HALPriv->ep0_in = &USBHAL::EP0in; - HALPriv->ep0_out = &USBHAL::EP0out; - HALPriv->ep0_read = &USBHAL::EP0read; - hpcd.pData = (void*)HALPriv; - HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback; - HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback; - HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback; - HALPriv->epCallback[3] = &USBHAL::EP2_IN_callback; - HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback; - HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback; - instance = this; - - /* Configure USB DM pin. This is optional, and maintained only for user guidance. */ - __HAL_RCC_GPIOA_CLK_ENABLE(); - pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF2_USB)); - pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF2_USB)); - - /* Enable USB Clock */ - __HAL_RCC_USB_CLK_ENABLE(); - - /* Enable SYSCFG Clock */ - __HAL_RCC_SYSCFG_CLK_ENABLE(); - hpcd.State = HAL_PCD_STATE_RESET; - HAL_PCD_Init(&hpcd); - - /* hardcoded size of FIFO according definition*/ - HAL_PCDEx_PMAConfig(&hpcd , 0x00 , PCD_SNG_BUF, 0x30); - HAL_PCDEx_PMAConfig(&hpcd , 0x80 , PCD_SNG_BUF, 0x70); - HAL_PCDEx_PMAConfig(&hpcd , 0x3, PCD_DBL_BUF, 0x018000b0); - HAL_PCDEx_PMAConfig(&hpcd , 0x83, PCD_SNG_BUF, 0xb0); - - NVIC_SetVector(USBHAL_IRQn,(uint32_t)&_usbisr); - NVIC_SetPriority(USBHAL_IRQn, 1); - HAL_PCD_Start(&hpcd); -} -#endif diff --git a/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32L475VG.h b/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32L475VG.h deleted file mode 100644 index 39a9df449c1..00000000000 --- a/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32L475VG.h +++ /dev/null @@ -1,145 +0,0 @@ -/* Copyright (c) 2016 mbed.org, MIT License -* -* 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. -*/ -#ifndef USBHAL_STM32L475VG -#define USBHAL_STM32L475VG - -#define USBHAL_IRQn OTG_FS_IRQn - - -#define NB_ENDPOINT 4 -/* must be multiple of 4 bytes */ -#define MAXTRANSFER_SIZE 0x200 -#define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3) -#if (FIFO_USB_RAM_SIZE > 0x500) -#error "FIFO dimensioning incorrect" -#endif - -typedef struct -{ - USBHAL *inst; - void (USBHAL::*bus_reset)(void); - void (USBHAL::*sof)(int frame); - void (USBHAL::*connect_change)(unsigned int connected); - void (USBHAL::*suspend_change)(unsigned int suspended); - void (USBHAL::*ep0_setup)(void); - void (USBHAL::*ep0_in)(void); - void (USBHAL::*ep0_out)(void); - void (USBHAL::*ep0_read)(void); - bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags); - bool (USBHAL::*epCallback[2*NB_ENDPOINT-2])(void); - uint8_t epComplete[8]; - /* memorize dummy buffer used for reception */ - uint32_t pBufRx[MAXTRANSFER_SIZE>>2]; - uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2]; -}USBHAL_Private_t; - -uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo) -{ - uint32_t len; - if (fifo == 0) len = hpcd->Instance->DIEPTXF0_HNPTXFSIZ>>16; - else - len = hpcd->Instance->DIEPTXF[fifo - 1] >> 16; - return len*4; -} -void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) { - USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData)); - USBHAL *obj= priv->inst; - USB_OTG_GlobalTypeDef *USBx = hpcd->Instance; - uint32_t sofnum = (USBx_DEVICE->DSTS & USB_OTG_DSTS_FNSOF) >> 8; - void (USBHAL::*func)(int frame) = priv->sof; - /* fix me call with same frame number */ - (obj->*func)(sofnum); -} - -USBHAL * USBHAL::instance; - -USBHAL::USBHAL(void) { - /* init parameter */ - USBHAL_Private_t *HALPriv = new(USBHAL_Private_t); - /* initialized all field of init including 0 field */ - /* constructor does not fill with zero */ - hpcd.Instance = USB_OTG_FS; - /* initialized all field of init including 0 field */ - /* constructor does not fill with zero */ - memset(&hpcd.Init, 0, sizeof(hpcd.Init)); - hpcd.Init.dev_endpoints = NB_ENDPOINT; - hpcd.Init.ep0_mps = MAX_PACKET_SIZE_EP0; - hpcd.Init.phy_itface = PCD_PHY_EMBEDDED; - hpcd.Init.Sof_enable = 1; - hpcd.Init.speed = PCD_SPEED_FULL; - /* pass instance for usage inside call back */ - HALPriv->inst = this; - HALPriv->bus_reset = &USBHAL::busReset; - HALPriv->suspend_change = &USBHAL::suspendStateChanged; - HALPriv->connect_change = &USBHAL::connectStateChanged; - HALPriv->sof = &USBHAL::SOF; - HALPriv->ep0_setup = &USBHAL::EP0setupCallback; - HALPriv->ep_realise = &USBHAL::realiseEndpoint; - HALPriv->ep0_in = &USBHAL::EP0in; - HALPriv->ep0_out = &USBHAL::EP0out; - HALPriv->ep0_read = &USBHAL::EP0read; - hpcd.pData = (void*)HALPriv; - HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback; - HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback; - HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback; - HALPriv->epCallback[3] = &USBHAL::EP2_IN_callback; - HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback; - HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback; - instance = this; - - __HAL_RCC_PWR_CLK_ENABLE(); - - HAL_PWREx_EnableVddUSB(); - /* Configure USB VBUS GPIO */ - __HAL_RCC_GPIOC_CLK_ENABLE(); - - /* Configure USB FS GPIOs */ - __HAL_RCC_GPIOA_CLK_ENABLE(); - - /* Configure DM DP Pins */ - pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); - pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); - - /* Configure VBUS Pin */ - pin_function(PC_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); - - __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); - - hpcd.State = HAL_PCD_STATE_RESET; - - HAL_PCD_Init(&hpcd); - /* 1.25kbytes */ - /* min value 16 (= 16 x 4 bytes) */ - /* max value 256 (= 1K bytes ) */ - /* maximum sum is 0x140 */ - HAL_PCDEx_SetRxFiFo(&hpcd, (MAXTRANSFER_SIZE/4)); - /* bulk/int 64 bytes in FS */ - HAL_PCDEx_SetTxFiFo(&hpcd, 0, (MAX_PACKET_SIZE_EP0/4)+1); - /* bulk/int bytes in FS */ - HAL_PCDEx_SetTxFiFo(&hpcd, 1, (MAX_PACKET_SIZE_EP1/4)+1); - HAL_PCDEx_SetTxFiFo(&hpcd, 2, (MAX_PACKET_SIZE_EP2/4)); - /* ISOchronous */ - HAL_PCDEx_SetTxFiFo(&hpcd, 3, (MAX_PACKET_SIZE_EP3/4)); - - NVIC_SetVector(USBHAL_IRQn,(uint32_t)&_usbisr); - NVIC_SetPriority( USBHAL_IRQn, 1); - - HAL_PCD_Start(&hpcd); -} - -#endif diff --git a/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32L476VG.h b/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32L476VG.h deleted file mode 100644 index b08bce8b40a..00000000000 --- a/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM32L476VG.h +++ /dev/null @@ -1,145 +0,0 @@ -/* Copyright (c) 2016 mbed.org, MIT License -* -* 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. -*/ -#ifndef USBHAL_STM32L476VG -#define USBHAL_STM32L476VG - -#define USBHAL_IRQn OTG_FS_IRQn - - -#define NB_ENDPOINT 4 -/* must be multiple of 4 bytes */ -#define MAXTRANSFER_SIZE 0x200 -#define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3) -#if (FIFO_USB_RAM_SIZE > 0x500) -#error "FIFO dimensioning incorrect" -#endif - -typedef struct -{ - USBHAL *inst; - void (USBHAL::*bus_reset)(void); - void (USBHAL::*sof)(int frame); - void (USBHAL::*connect_change)(unsigned int connected); - void (USBHAL::*suspend_change)(unsigned int suspended); - void (USBHAL::*ep0_setup)(void); - void (USBHAL::*ep0_in)(void); - void (USBHAL::*ep0_out)(void); - void (USBHAL::*ep0_read)(void); - bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags); - bool (USBHAL::*epCallback[2*NB_ENDPOINT-2])(void); - uint8_t epComplete[8]; - /* memorize dummy buffer used for reception */ - uint32_t pBufRx[MAXTRANSFER_SIZE>>2]; - uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2]; -}USBHAL_Private_t; - -uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo) -{ - uint32_t len; - if (fifo == 0) len = hpcd->Instance->DIEPTXF0_HNPTXFSIZ>>16; - else - len = hpcd->Instance->DIEPTXF[fifo - 1] >> 16; - return len*4; -} -void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) { - USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData)); - USBHAL *obj= priv->inst; - USB_OTG_GlobalTypeDef *USBx = hpcd->Instance; - uint32_t sofnum = (USBx_DEVICE->DSTS & USB_OTG_DSTS_FNSOF) >> 8; - void (USBHAL::*func)(int frame) = priv->sof; - /* fix me call with same frame number */ - (obj->*func)(sofnum); -} - -USBHAL * USBHAL::instance; - -USBHAL::USBHAL(void) { - /* init parameter */ - USBHAL_Private_t *HALPriv = new(USBHAL_Private_t); - /* initialized all field of init including 0 field */ - /* constructor does not fill with zero */ - hpcd.Instance = USB_OTG_FS; - /* initialized all field of init including 0 field */ - /* constructor does not fill with zero */ - memset(&hpcd.Init, 0, sizeof(hpcd.Init)); - hpcd.Init.dev_endpoints = NB_ENDPOINT; - hpcd.Init.ep0_mps = MAX_PACKET_SIZE_EP0; - hpcd.Init.phy_itface = PCD_PHY_EMBEDDED; - hpcd.Init.Sof_enable = 1; - hpcd.Init.speed = PCD_SPEED_FULL; - /* pass instance for usage inside call back */ - HALPriv->inst = this; - HALPriv->bus_reset = &USBHAL::busReset; - HALPriv->suspend_change = &USBHAL::suspendStateChanged; - HALPriv->connect_change = &USBHAL::connectStateChanged; - HALPriv->sof = &USBHAL::SOF; - HALPriv->ep0_setup = &USBHAL::EP0setupCallback; - HALPriv->ep_realise = &USBHAL::realiseEndpoint; - HALPriv->ep0_in = &USBHAL::EP0in; - HALPriv->ep0_out = &USBHAL::EP0out; - HALPriv->ep0_read = &USBHAL::EP0read; - hpcd.pData = (void*)HALPriv; - HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback; - HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback; - HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback; - HALPriv->epCallback[3] = &USBHAL::EP2_IN_callback; - HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback; - HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback; - instance = this; - - __HAL_RCC_PWR_CLK_ENABLE(); - - HAL_PWREx_EnableVddUSB(); - /* Configure USB VBUS GPIO */ - __HAL_RCC_GPIOC_CLK_ENABLE(); - - /* Configure USB FS GPIOs */ - __HAL_RCC_GPIOA_CLK_ENABLE(); - - /* Configure DM DP Pins */ - pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); - pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); - - /* Configure VBUS Pin */ - pin_function(PC_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); - - __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); - - hpcd.State = HAL_PCD_STATE_RESET; - - HAL_PCD_Init(&hpcd); - /* 1.25kbytes */ - /* min value 16 (= 16 x 4 bytes) */ - /* max value 256 (= 1K bytes ) */ - /* maximum sum is 0x140 */ - HAL_PCDEx_SetRxFiFo(&hpcd, (MAXTRANSFER_SIZE/4)); - /* bulk/int 64 bytes in FS */ - HAL_PCDEx_SetTxFiFo(&hpcd, 0, (MAX_PACKET_SIZE_EP0/4)+1); - /* bulk/int bytes in FS */ - HAL_PCDEx_SetTxFiFo(&hpcd, 1, (MAX_PACKET_SIZE_EP1/4)+1); - HAL_PCDEx_SetTxFiFo(&hpcd, 2, (MAX_PACKET_SIZE_EP2/4)); - /* ISOchronous */ - HAL_PCDEx_SetTxFiFo(&hpcd, 3, (MAX_PACKET_SIZE_EP3/4)); - - NVIC_SetVector(USBHAL_IRQn,(uint32_t)&_usbisr); - NVIC_SetPriority( USBHAL_IRQn, 1); - - HAL_PCD_Start(&hpcd); -} - -#endif diff --git a/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM_144_64pins.h b/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM_144_64pins.h deleted file mode 100644 index 200369d8dc2..00000000000 --- a/features/unsupported/USBDevice/targets/TARGET_STM/USBHAL_STM_144_64pins.h +++ /dev/null @@ -1,132 +0,0 @@ -/* Copyright (c) 2016 mbed.org, MIT License -* -* 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. -*/ -#ifndef USBHAL_STM32_144_64 -#define USBHAL_STM32_144_64 - -#define USBHAL_IRQn OTG_FS_IRQn -/* must be multiple of 4 bytes */ -#define NB_ENDPOINT 4 -#define MAXTRANSFER_SIZE 0x200 -#define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3) -#if (FIFO_USB_RAM_SIZE > 0x500) -#error "FIFO dimensioning incorrect" -#endif - -typedef struct -{ - USBHAL *inst; - void (USBHAL::*bus_reset)(void); - void (USBHAL::*sof)(int frame); - void (USBHAL::*connect_change)(unsigned int connected); - void (USBHAL::*suspend_change)(unsigned int suspended); - void (USBHAL::*ep0_setup)(void); - void (USBHAL::*ep0_in)(void); - void (USBHAL::*ep0_out)(void); - void (USBHAL::*ep0_read)(void); - bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags); - bool (USBHAL::*epCallback[2*NB_ENDPOINT-2])(void); - /* memorize dummy buffer used for reception */ - uint32_t pBufRx[MAXTRANSFER_SIZE>>2]; - uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2]; - uint8_t epComplete[2*NB_ENDPOINT]; -}USBHAL_Private_t; - -uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo) -{ - uint32_t len; - if (fifo == 0) len = hpcd->Instance->DIEPTXF0_HNPTXFSIZ>>16; - else - len = hpcd->Instance->DIEPTXF[fifo - 1] >> 16; - return len*4; -} -void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd) -{ - USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData)); - USBHAL *obj= priv->inst; - USB_OTG_GlobalTypeDef *USBx = hpcd->Instance; - uint32_t sofnum = (USBx_DEVICE->DSTS & USB_OTG_DSTS_FNSOF) >> 8; - void (USBHAL::*func)(int frame) = priv->sof; - (obj->*func)(sofnum); -} - - -USBHAL * USBHAL::instance; - -USBHAL::USBHAL(void) { - /* init parameter */ - USBHAL_Private_t *HALPriv = new(USBHAL_Private_t); - hpcd.Instance = USB_OTG_FS; - memset(&hpcd.Init, 0, sizeof(hpcd.Init)); - hpcd.Init.dev_endpoints = NB_ENDPOINT; - hpcd.Init.ep0_mps = MAX_PACKET_SIZE_EP0; - hpcd.Init.phy_itface = PCD_PHY_EMBEDDED; - hpcd.Init.Sof_enable = 1; - - hpcd.Init.speed = PCD_SPEED_FULL; - //hpcd.Init.vbus_sensing_enable = 0; - //hpcd.Init.lpm_enable = 0; - /* pass instance for usage inside call back */ - HALPriv->inst = this; - HALPriv->bus_reset = &USBHAL::busReset; - HALPriv->suspend_change = &USBHAL::suspendStateChanged; - HALPriv->connect_change = &USBHAL::connectStateChanged; - HALPriv->sof = &USBHAL::SOF; - HALPriv->ep0_setup = &USBHAL::EP0setupCallback; - HALPriv->ep_realise = &USBHAL::realiseEndpoint; - HALPriv->ep0_in = &USBHAL::EP0in; - HALPriv->ep0_out = &USBHAL::EP0out; - HALPriv->ep0_read = &USBHAL::EP0read; - hpcd.pData = (void*)HALPriv; - HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback; - HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback; - HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback; - HALPriv->epCallback[3] = &USBHAL::EP2_IN_callback; - HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback; - HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback; - instance = this; - // Enable power and clocking - /* board 144 pin all similar */ - __HAL_RCC_GPIOA_CLK_ENABLE(); - pin_function(PA_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); /* OTG_FS_SOF */ - pin_function(PA_9, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, GPIO_AF10_OTG_FS)); /* OTG_FS_VBUS */ - pin_function(PA_10, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_PULLUP, GPIO_AF10_OTG_FS)); /* OTG_FS_ID */ - pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); /* OTG_FS_DM */ - pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); /* OTG_FS_DP */ - - __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); - __HAL_RCC_SYSCFG_CLK_ENABLE(); - hpcd.State = HAL_PCD_STATE_RESET; - HAL_PCD_Init(&hpcd); - /* 1.25kbytes */ - /* min value 16 (= 16 x 4 bytes) */ - /* max value 256 (= 1K bytes ) */ - /* maximum sum is 0x140 */ - HAL_PCDEx_SetRxFiFo(&hpcd, (MAXTRANSFER_SIZE/4)); - /* bulk/int 64 bytes in FS */ - HAL_PCDEx_SetTxFiFo(&hpcd, 0, (MAX_PACKET_SIZE_EP0/4)+1); - /* bulk/int bytes in FS */ - HAL_PCDEx_SetTxFiFo(&hpcd, 1, (MAX_PACKET_SIZE_EP1/4)+1); - HAL_PCDEx_SetTxFiFo(&hpcd, 2, (MAX_PACKET_SIZE_EP2/4)); - /* ISOchronous */ - HAL_PCDEx_SetTxFiFo(&hpcd, 3, (MAX_PACKET_SIZE_EP3/4)); - NVIC_SetVector(USBHAL_IRQn, (uint32_t)&_usbisr); - NVIC_SetPriority(USBHAL_IRQn, 1); - HAL_PCD_Start(&hpcd); -} -#endif - diff --git a/targets/targets.json b/targets/targets.json index 70650962d32..72cde27e67c 100644 --- a/targets/targets.json +++ b/targets/targets.json @@ -1486,6 +1486,7 @@ } }, "detect_code": ["0788"], + "macros_add": ["USB_STM_HAL"], "device_has_add": ["ANALOGOUT", "CAN", "LOWPOWERTIMER", "SERIAL_FC", "TRNG", "FLASH"], "release_versions": ["2", "5"], "device_name": "STM32F469NI"