USB device stack

Dependents:   USBMSD_step1 USBMSD_step1_5 picossd_step1_2cs

Committer:
muraguchi
Date:
Wed Sep 15 16:31:51 2021 +0000
Revision:
73:72808bd55ce2
Parent:
71:53949e6131f6
AirioBase + 2 chip PicoSSD board

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 71:53949e6131f6 1 /* Copyright (c) 2016 mbed.org, MIT License
Kojto 71:53949e6131f6 2 *
Kojto 71:53949e6131f6 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Kojto 71:53949e6131f6 4 * and associated documentation files (the "Software"), to deal in the Software without
Kojto 71:53949e6131f6 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
Kojto 71:53949e6131f6 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Kojto 71:53949e6131f6 7 * Software is furnished to do so, subject to the following conditions:
Kojto 71:53949e6131f6 8 *
Kojto 71:53949e6131f6 9 * The above copyright notice and this permission notice shall be included in all copies or
Kojto 71:53949e6131f6 10 * substantial portions of the Software.
Kojto 71:53949e6131f6 11 *
Kojto 71:53949e6131f6 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Kojto 71:53949e6131f6 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Kojto 71:53949e6131f6 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Kojto 71:53949e6131f6 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Kojto 71:53949e6131f6 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Kojto 71:53949e6131f6 17 */
Kojto 71:53949e6131f6 18 #ifndef USBHAL_STM32_144_64
Kojto 71:53949e6131f6 19 #define USBHAL_STM32_144_64
Kojto 71:53949e6131f6 20
Kojto 71:53949e6131f6 21 #define USBHAL_IRQn OTG_FS_IRQn
Kojto 71:53949e6131f6 22 /* must be multiple of 4 bytes */
Kojto 71:53949e6131f6 23 #define NB_ENDPOINT 4
Kojto 71:53949e6131f6 24 #define MAXTRANSFER_SIZE 0x200
Kojto 71:53949e6131f6 25 #define FIFO_USB_RAM_SIZE (MAXTRANSFER_SIZE+MAX_PACKET_SIZE_EP0+MAX_PACKET_SIZE_EP1+MAX_PACKET_SIZE_EP2+MAX_PACKET_SIZE_EP3)
Kojto 71:53949e6131f6 26 #if (FIFO_USB_RAM_SIZE > 0x500)
Kojto 71:53949e6131f6 27 #error "FIFO dimensioning incorrect"
Kojto 71:53949e6131f6 28 #endif
Kojto 71:53949e6131f6 29
Kojto 71:53949e6131f6 30 typedef struct
Kojto 71:53949e6131f6 31 {
Kojto 71:53949e6131f6 32 USBHAL *inst;
Kojto 71:53949e6131f6 33 void (USBHAL::*bus_reset)(void);
Kojto 71:53949e6131f6 34 void (USBHAL::*sof)(int frame);
Kojto 71:53949e6131f6 35 void (USBHAL::*connect_change)(unsigned int connected);
Kojto 71:53949e6131f6 36 void (USBHAL::*suspend_change)(unsigned int suspended);
Kojto 71:53949e6131f6 37 void (USBHAL::*ep0_setup)(void);
Kojto 71:53949e6131f6 38 void (USBHAL::*ep0_in)(void);
Kojto 71:53949e6131f6 39 void (USBHAL::*ep0_out)(void);
Kojto 71:53949e6131f6 40 void (USBHAL::*ep0_read)(void);
Kojto 71:53949e6131f6 41 bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags);
Kojto 71:53949e6131f6 42 bool (USBHAL::*epCallback[2*NB_ENDPOINT-2])(void);
Kojto 71:53949e6131f6 43 /* memorize dummy buffer used for reception */
Kojto 71:53949e6131f6 44 uint32_t pBufRx[MAXTRANSFER_SIZE>>2];
Kojto 71:53949e6131f6 45 uint32_t pBufRx0[MAX_PACKET_SIZE_EP0>>2];
Kojto 71:53949e6131f6 46 uint8_t epComplete[2*NB_ENDPOINT];
Kojto 71:53949e6131f6 47 }USBHAL_Private_t;
Kojto 71:53949e6131f6 48
Kojto 71:53949e6131f6 49 uint32_t HAL_PCDEx_GetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo)
Kojto 71:53949e6131f6 50 {
Kojto 71:53949e6131f6 51 uint32_t len;
Kojto 71:53949e6131f6 52 if (fifo == 0) len = hpcd->Instance->DIEPTXF0_HNPTXFSIZ>>16;
Kojto 71:53949e6131f6 53 else
Kojto 71:53949e6131f6 54 len = hpcd->Instance->DIEPTXF[fifo - 1] >> 16;
Kojto 71:53949e6131f6 55 return len*4;
Kojto 71:53949e6131f6 56 }
Kojto 71:53949e6131f6 57 void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
Kojto 71:53949e6131f6 58 {
Kojto 71:53949e6131f6 59 USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
Kojto 71:53949e6131f6 60 USBHAL *obj= priv->inst;
Kojto 71:53949e6131f6 61 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
Kojto 71:53949e6131f6 62 uint32_t sofnum = (USBx_DEVICE->DSTS & USB_OTG_DSTS_FNSOF) >> 8;
Kojto 71:53949e6131f6 63 void (USBHAL::*func)(int frame) = priv->sof;
Kojto 71:53949e6131f6 64 (obj->*func)(sofnum);
Kojto 71:53949e6131f6 65 }
Kojto 71:53949e6131f6 66
Kojto 71:53949e6131f6 67
Kojto 71:53949e6131f6 68 USBHAL * USBHAL::instance;
Kojto 71:53949e6131f6 69
Kojto 71:53949e6131f6 70 USBHAL::USBHAL(void) {
Kojto 71:53949e6131f6 71 /* init parameter */
Kojto 71:53949e6131f6 72 USBHAL_Private_t *HALPriv = new(USBHAL_Private_t);
Kojto 71:53949e6131f6 73 hpcd.Instance = USB_OTG_FS;
Kojto 71:53949e6131f6 74 memset(&hpcd.Init, 0, sizeof(hpcd.Init));
Kojto 71:53949e6131f6 75 hpcd.Init.dev_endpoints = NB_ENDPOINT;
Kojto 71:53949e6131f6 76 hpcd.Init.ep0_mps = MAX_PACKET_SIZE_EP0;
Kojto 71:53949e6131f6 77 hpcd.Init.phy_itface = PCD_PHY_EMBEDDED;
Kojto 71:53949e6131f6 78 hpcd.Init.Sof_enable = 1;
Kojto 71:53949e6131f6 79
Kojto 71:53949e6131f6 80 hpcd.Init.speed = PCD_SPEED_FULL;
Kojto 71:53949e6131f6 81 //hpcd.Init.vbus_sensing_enable = 0;
Kojto 71:53949e6131f6 82 //hpcd.Init.lpm_enable = 0;
Kojto 71:53949e6131f6 83 /* pass instance for usage inside call back */
Kojto 71:53949e6131f6 84 HALPriv->inst = this;
Kojto 71:53949e6131f6 85 HALPriv->bus_reset = &USBHAL::busReset;
Kojto 71:53949e6131f6 86 HALPriv->suspend_change = &USBHAL::suspendStateChanged;
Kojto 71:53949e6131f6 87 HALPriv->connect_change = &USBHAL::connectStateChanged;
Kojto 71:53949e6131f6 88 HALPriv->sof = &USBHAL::SOF;
Kojto 71:53949e6131f6 89 HALPriv->ep0_setup = &USBHAL::EP0setupCallback;
Kojto 71:53949e6131f6 90 HALPriv->ep_realise = &USBHAL::realiseEndpoint;
Kojto 71:53949e6131f6 91 HALPriv->ep0_in = &USBHAL::EP0in;
Kojto 71:53949e6131f6 92 HALPriv->ep0_out = &USBHAL::EP0out;
Kojto 71:53949e6131f6 93 HALPriv->ep0_read = &USBHAL::EP0read;
Kojto 71:53949e6131f6 94 hpcd.pData = (void*)HALPriv;
Kojto 71:53949e6131f6 95 HALPriv->epCallback[0] = &USBHAL::EP1_OUT_callback;
Kojto 71:53949e6131f6 96 HALPriv->epCallback[1] = &USBHAL::EP1_IN_callback;
Kojto 71:53949e6131f6 97 HALPriv->epCallback[2] = &USBHAL::EP2_OUT_callback;
Kojto 71:53949e6131f6 98 HALPriv->epCallback[3] = &USBHAL::EP2_IN_callback;
Kojto 71:53949e6131f6 99 HALPriv->epCallback[4] = &USBHAL::EP3_OUT_callback;
Kojto 71:53949e6131f6 100 HALPriv->epCallback[5] = &USBHAL::EP3_IN_callback;
Kojto 71:53949e6131f6 101 instance = this;
Kojto 71:53949e6131f6 102 // Enable power and clocking
Kojto 71:53949e6131f6 103 /* board 144 pin all similar */
Kojto 71:53949e6131f6 104 __HAL_RCC_GPIOA_CLK_ENABLE();
Kojto 71:53949e6131f6 105 pin_function(PA_8, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); /* OTG_FS_SOF */
Kojto 71:53949e6131f6 106 pin_function(PA_9, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, GPIO_AF10_OTG_FS)); /* OTG_FS_VBUS */
Kojto 71:53949e6131f6 107 pin_function(PA_10, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_PULLUP, GPIO_AF10_OTG_FS)); /* OTG_FS_ID */
Kojto 71:53949e6131f6 108 pin_function(PA_11, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); /* OTG_FS_DM */
Kojto 71:53949e6131f6 109 pin_function(PA_12, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF10_OTG_FS)); /* OTG_FS_DP */
Kojto 71:53949e6131f6 110
Kojto 71:53949e6131f6 111 __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
Kojto 71:53949e6131f6 112 __HAL_RCC_SYSCFG_CLK_ENABLE();
Kojto 71:53949e6131f6 113 hpcd.State = HAL_PCD_STATE_RESET;
Kojto 71:53949e6131f6 114 HAL_PCD_Init(&hpcd);
Kojto 71:53949e6131f6 115 /* 1.25kbytes */
Kojto 71:53949e6131f6 116 /* min value 16 (= 16 x 4 bytes) */
Kojto 71:53949e6131f6 117 /* max value 256 (= 1K bytes ) */
Kojto 71:53949e6131f6 118 /* maximum sum is 0x140 */
Kojto 71:53949e6131f6 119 HAL_PCDEx_SetRxFiFo(&hpcd, (MAXTRANSFER_SIZE/4));
Kojto 71:53949e6131f6 120 /* bulk/int 64 bytes in FS */
Kojto 71:53949e6131f6 121 HAL_PCDEx_SetTxFiFo(&hpcd, 0, (MAX_PACKET_SIZE_EP0/4)+1);
Kojto 71:53949e6131f6 122 /* bulk/int bytes in FS */
Kojto 71:53949e6131f6 123 HAL_PCDEx_SetTxFiFo(&hpcd, 1, (MAX_PACKET_SIZE_EP1/4)+1);
Kojto 71:53949e6131f6 124 HAL_PCDEx_SetTxFiFo(&hpcd, 2, (MAX_PACKET_SIZE_EP2/4));
Kojto 71:53949e6131f6 125 /* ISOchronous */
Kojto 71:53949e6131f6 126 HAL_PCDEx_SetTxFiFo(&hpcd, 3, (MAX_PACKET_SIZE_EP3/4));
Kojto 71:53949e6131f6 127 NVIC_SetVector(USBHAL_IRQn, (uint32_t)&_usbisr);
Kojto 71:53949e6131f6 128 NVIC_SetPriority(USBHAL_IRQn, 1);
Kojto 71:53949e6131f6 129 HAL_PCD_Start(&hpcd);
Kojto 71:53949e6131f6 130 }
Kojto 71:53949e6131f6 131 #endif
Kojto 71:53949e6131f6 132