USB device stack

Dependents:   mbed-mX-USB-TEST1 USBMSD_SD_HID_HelloWorld HidTest MIDI_usb_bridge ... more

Legacy Warning

This is an mbed 2 library. To learn more about mbed OS 5, visit the docs.

Pull requests against this repository are no longer supported. Please raise against mbed OS 5 as documented above.

Committer:
Kojto
Date:
Thu Jul 20 10:14:36 2017 +0100
Revision:
70:2c525a50f1b6
Update libraries (ed9d1da)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kojto 70:2c525a50f1b6 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
Kojto 70:2c525a50f1b6 2 *
Kojto 70:2c525a50f1b6 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Kojto 70:2c525a50f1b6 4 * and associated documentation files (the "Software"), to deal in the Software without
Kojto 70:2c525a50f1b6 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
Kojto 70:2c525a50f1b6 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Kojto 70:2c525a50f1b6 7 * Software is furnished to do so, subject to the following conditions:
Kojto 70:2c525a50f1b6 8 *
Kojto 70:2c525a50f1b6 9 * The above copyright notice and this permission notice shall be included in all copies or
Kojto 70:2c525a50f1b6 10 * substantial portions of the Software.
Kojto 70:2c525a50f1b6 11 *
Kojto 70:2c525a50f1b6 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Kojto 70:2c525a50f1b6 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Kojto 70:2c525a50f1b6 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Kojto 70:2c525a50f1b6 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Kojto 70:2c525a50f1b6 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Kojto 70:2c525a50f1b6 17 */
Kojto 70:2c525a50f1b6 18 /* TARGET NOT STM does not support this HAL */
Kojto 70:2c525a50f1b6 19 #ifndef TARGET_STM
Kojto 70:2c525a50f1b6 20 #define USBSTM_HAL_UNSUPPORTED
Kojto 70:2c525a50f1b6 21 #endif
Kojto 70:2c525a50f1b6 22 /* F4 famlily wihtout USB_STM_HAL use another HAL*/
Kojto 70:2c525a50f1b6 23 #if defined(TARGET_STM) && defined(TARGET_STM32F4) && !defined(USB_STM_HAL)
Kojto 70:2c525a50f1b6 24 #define USBSTM_HAL_UNSUPPORTED
Kojto 70:2c525a50f1b6 25 #endif
Kojto 70:2c525a50f1b6 26
Kojto 70:2c525a50f1b6 27 #ifndef USBSTM_HAL_UNSUPPORTED
Kojto 70:2c525a50f1b6 28 #include "USBHAL.h"
Kojto 70:2c525a50f1b6 29 #include "pinmap.h"
Kojto 70:2c525a50f1b6 30 /* mbed endpoint definition to hal definition */
Kojto 70:2c525a50f1b6 31 #define EP_ADDR(ep) (((ep) >> 1)|((ep) & 1) << 7)
Kojto 70:2c525a50f1b6 32 /* from hal definition to mbed definition */
Kojto 70:2c525a50f1b6 33 #define ADDR_EPIN(ep) (((ep) << 1) | 1)
Kojto 70:2c525a50f1b6 34 #define ADDR_EPOUT(ep) (((ep) << 1))
Kojto 70:2c525a50f1b6 35 /* id to detect if rx buffer is used or not */
Kojto 70:2c525a50f1b6 36
Kojto 70:2c525a50f1b6 37 #include "USBHAL_STM_TARGET.h"
Kojto 70:2c525a50f1b6 38
Kojto 70:2c525a50f1b6 39
Kojto 70:2c525a50f1b6 40 /* this call at device reception completion on a Out Enpoint */
Kojto 70:2c525a50f1b6 41 void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
Kojto 70:2c525a50f1b6 42 {
Kojto 70:2c525a50f1b6 43 USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
Kojto 70:2c525a50f1b6 44 USBHAL *obj= priv->inst;
Kojto 70:2c525a50f1b6 45 uint8_t endpoint = ADDR_EPOUT(epnum);
Kojto 70:2c525a50f1b6 46 priv->epComplete[endpoint] = 1;
Kojto 70:2c525a50f1b6 47 /* -2 endpoint 0 In out are not in call back list */
Kojto 70:2c525a50f1b6 48 if (epnum) {
Kojto 70:2c525a50f1b6 49 bool (USBHAL::*func)(void) = priv->epCallback[endpoint-2];
Kojto 70:2c525a50f1b6 50 (obj->*func)();
Kojto 70:2c525a50f1b6 51 } else {
Kojto 70:2c525a50f1b6 52 void (USBHAL::*func)(void) = priv->ep0_out;
Kojto 70:2c525a50f1b6 53 (obj->*func)();
Kojto 70:2c525a50f1b6 54 }
Kojto 70:2c525a50f1b6 55 }
Kojto 70:2c525a50f1b6 56
Kojto 70:2c525a50f1b6 57 /* this is call at device transmission completion on In endpoint */
Kojto 70:2c525a50f1b6 58 void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
Kojto 70:2c525a50f1b6 59 {
Kojto 70:2c525a50f1b6 60 USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
Kojto 70:2c525a50f1b6 61 USBHAL *obj= priv->inst;
Kojto 70:2c525a50f1b6 62 uint8_t endpoint = ADDR_EPIN(epnum);
Kojto 70:2c525a50f1b6 63 priv->epComplete[endpoint] = 1;
Kojto 70:2c525a50f1b6 64 /* -2 endpoint 0 In out are not in call back list */
Kojto 70:2c525a50f1b6 65 if (epnum) {
Kojto 70:2c525a50f1b6 66 bool (USBHAL::*func)(void) = priv->epCallback[endpoint-2];
Kojto 70:2c525a50f1b6 67 (obj->*func)();
Kojto 70:2c525a50f1b6 68 } else {
Kojto 70:2c525a50f1b6 69 void (USBHAL::*func)(void) = priv->ep0_in;
Kojto 70:2c525a50f1b6 70 (obj->*func)();
Kojto 70:2c525a50f1b6 71 }
Kojto 70:2c525a50f1b6 72 }
Kojto 70:2c525a50f1b6 73 /* This is call at device set up reception */
Kojto 70:2c525a50f1b6 74 void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
Kojto 70:2c525a50f1b6 75 {
Kojto 70:2c525a50f1b6 76 USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
Kojto 70:2c525a50f1b6 77 USBHAL *obj= priv->inst;
Kojto 70:2c525a50f1b6 78 void (USBHAL::*func)(void)=priv->ep0_setup;
Kojto 70:2c525a50f1b6 79 void (USBHAL::*func1)(void)=priv->ep0_read;
Kojto 70:2c525a50f1b6 80 (obj->*func)();
Kojto 70:2c525a50f1b6 81 (obj->*func1)();
Kojto 70:2c525a50f1b6 82 }
Kojto 70:2c525a50f1b6 83
Kojto 70:2c525a50f1b6 84 void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
Kojto 70:2c525a50f1b6 85 {
Kojto 70:2c525a50f1b6 86 USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
Kojto 70:2c525a50f1b6 87 USBHAL *obj= priv->inst;
Kojto 70:2c525a50f1b6 88 void (USBHAL::*func)(unsigned int suspended) = priv->suspend_change;
Kojto 70:2c525a50f1b6 89 (obj->*func)(1);
Kojto 70:2c525a50f1b6 90 }
Kojto 70:2c525a50f1b6 91
Kojto 70:2c525a50f1b6 92 void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
Kojto 70:2c525a50f1b6 93 {
Kojto 70:2c525a50f1b6 94 USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
Kojto 70:2c525a50f1b6 95 USBHAL *obj= priv->inst;
Kojto 70:2c525a50f1b6 96 void (USBHAL::*func)(unsigned int suspended) = priv->suspend_change;
Kojto 70:2c525a50f1b6 97 (obj->*func)(0);
Kojto 70:2c525a50f1b6 98 }
Kojto 70:2c525a50f1b6 99
Kojto 70:2c525a50f1b6 100 void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
Kojto 70:2c525a50f1b6 101 {
Kojto 70:2c525a50f1b6 102 USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
Kojto 70:2c525a50f1b6 103 USBHAL *obj= priv->inst;
Kojto 70:2c525a50f1b6 104 void (USBHAL::*func)(unsigned int suspended) = priv->connect_change;
Kojto 70:2c525a50f1b6 105 (obj->*func)(1);
Kojto 70:2c525a50f1b6 106 }
Kojto 70:2c525a50f1b6 107
Kojto 70:2c525a50f1b6 108 void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
Kojto 70:2c525a50f1b6 109 {
Kojto 70:2c525a50f1b6 110 USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
Kojto 70:2c525a50f1b6 111 USBHAL *obj= priv->inst;
Kojto 70:2c525a50f1b6 112 void (USBHAL::*func)(unsigned int suspended) = priv->connect_change;
Kojto 70:2c525a50f1b6 113 (obj->*func)(0);
Kojto 70:2c525a50f1b6 114 }
Kojto 70:2c525a50f1b6 115
Kojto 70:2c525a50f1b6 116 void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
Kojto 70:2c525a50f1b6 117 {
Kojto 70:2c525a50f1b6 118 USBHAL_Private_t *priv=((USBHAL_Private_t *)(hpcd->pData));
Kojto 70:2c525a50f1b6 119 USBHAL *obj= priv->inst;
Kojto 70:2c525a50f1b6 120 unsigned int i;
Kojto 70:2c525a50f1b6 121 for(i=0;i<hpcd->Init.dev_endpoints;i++) {
Kojto 70:2c525a50f1b6 122 priv->epComplete[2*i]=0;
Kojto 70:2c525a50f1b6 123 HAL_PCD_EP_Close(hpcd,EP_ADDR(2*i));
Kojto 70:2c525a50f1b6 124 HAL_PCD_EP_Flush(hpcd,EP_ADDR(2*i));
Kojto 70:2c525a50f1b6 125 priv->epComplete[2*i+1]=0;
Kojto 70:2c525a50f1b6 126 HAL_PCD_EP_Close(hpcd,EP_ADDR(2*i+1));
Kojto 70:2c525a50f1b6 127 HAL_PCD_EP_Flush(hpcd,EP_ADDR(2*i+1));
Kojto 70:2c525a50f1b6 128
Kojto 70:2c525a50f1b6 129 }
Kojto 70:2c525a50f1b6 130 void (USBHAL::*func)(void)=priv->bus_reset;
Kojto 70:2c525a50f1b6 131 bool (USBHAL::*ep_realise)(uint8_t endpoint, uint32_t maxPacket, uint32_t flags) = priv->ep_realise;
Kojto 70:2c525a50f1b6 132 (obj->*func)();
Kojto 70:2c525a50f1b6 133 (obj->*ep_realise)(EP0IN, MAX_PACKET_SIZE_EP0,0);
Kojto 70:2c525a50f1b6 134 (obj->*ep_realise)(EP0OUT, MAX_PACKET_SIZE_EP0,0);
Kojto 70:2c525a50f1b6 135 }
Kojto 70:2c525a50f1b6 136
Kojto 70:2c525a50f1b6 137
Kojto 70:2c525a50f1b6 138 /* hal pcd handler , used for STM32 HAL PCD Layer */
Kojto 70:2c525a50f1b6 139
Kojto 70:2c525a50f1b6 140 uint32_t USBHAL::endpointReadcore(uint8_t endpoint, uint8_t *buffer) {
Kojto 70:2c525a50f1b6 141 return 0;
Kojto 70:2c525a50f1b6 142 }
Kojto 70:2c525a50f1b6 143
Kojto 70:2c525a50f1b6 144 USBHAL::~USBHAL(void) {
Kojto 70:2c525a50f1b6 145 USBHAL_Private_t *HALPriv = (USBHAL_Private_t *)(hpcd.pData);
Kojto 70:2c525a50f1b6 146 HAL_PCD_DeInit(&hpcd);
Kojto 70:2c525a50f1b6 147 delete HALPriv;
Kojto 70:2c525a50f1b6 148 }
Kojto 70:2c525a50f1b6 149
Kojto 70:2c525a50f1b6 150 void USBHAL::connect(void) {
Kojto 70:2c525a50f1b6 151 NVIC_EnableIRQ(USBHAL_IRQn);
Kojto 70:2c525a50f1b6 152 }
Kojto 70:2c525a50f1b6 153
Kojto 70:2c525a50f1b6 154 void USBHAL::disconnect(void) {
Kojto 70:2c525a50f1b6 155 NVIC_DisableIRQ(USBHAL_IRQn);
Kojto 70:2c525a50f1b6 156 }
Kojto 70:2c525a50f1b6 157
Kojto 70:2c525a50f1b6 158 void USBHAL::configureDevice(void) {
Kojto 70:2c525a50f1b6 159 // Not needed
Kojto 70:2c525a50f1b6 160 }
Kojto 70:2c525a50f1b6 161
Kojto 70:2c525a50f1b6 162 void USBHAL::unconfigureDevice(void) {
Kojto 70:2c525a50f1b6 163 // Not needed
Kojto 70:2c525a50f1b6 164 }
Kojto 70:2c525a50f1b6 165
Kojto 70:2c525a50f1b6 166 void USBHAL::setAddress(uint8_t address) {
Kojto 70:2c525a50f1b6 167 HAL_PCD_SetAddress(&hpcd, address);
Kojto 70:2c525a50f1b6 168 EP0write(0, 0);
Kojto 70:2c525a50f1b6 169 }
Kojto 70:2c525a50f1b6 170
Kojto 70:2c525a50f1b6 171 bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t flags) {
Kojto 70:2c525a50f1b6 172 uint32_t epIndex = EP_ADDR(endpoint);
Kojto 70:2c525a50f1b6 173 uint32_t type;
Kojto 70:2c525a50f1b6 174 uint32_t len;
Kojto 70:2c525a50f1b6 175 HAL_StatusTypeDef ret;
Kojto 70:2c525a50f1b6 176 switch (endpoint) {
Kojto 70:2c525a50f1b6 177 case EP0IN:
Kojto 70:2c525a50f1b6 178 case EP0OUT:
Kojto 70:2c525a50f1b6 179 type = 0;
Kojto 70:2c525a50f1b6 180 break;
Kojto 70:2c525a50f1b6 181 case EPISO_IN:
Kojto 70:2c525a50f1b6 182 case EPISO_OUT:
Kojto 70:2c525a50f1b6 183 type = 1;
Kojto 70:2c525a50f1b6 184 break;
Kojto 70:2c525a50f1b6 185 case EPBULK_IN:
Kojto 70:2c525a50f1b6 186 case EPBULK_OUT:
Kojto 70:2c525a50f1b6 187 type = 2;
Kojto 70:2c525a50f1b6 188 break;
Kojto 70:2c525a50f1b6 189 case EPINT_IN:
Kojto 70:2c525a50f1b6 190 case EPINT_OUT:
Kojto 70:2c525a50f1b6 191 type = 3;
Kojto 70:2c525a50f1b6 192 break;
Kojto 70:2c525a50f1b6 193 }
Kojto 70:2c525a50f1b6 194 if (maxPacket > MAXTRANSFER_SIZE) return false;
Kojto 70:2c525a50f1b6 195 if (epIndex & 0x80) {
Kojto 70:2c525a50f1b6 196 len = HAL_PCDEx_GetTxFiFo(&hpcd,epIndex & 0x7f);
Kojto 70:2c525a50f1b6 197 MBED_ASSERT(len >= maxPacket);
Kojto 70:2c525a50f1b6 198 }
Kojto 70:2c525a50f1b6 199 ret = HAL_PCD_EP_Open(&hpcd, epIndex, maxPacket, type);
Kojto 70:2c525a50f1b6 200 MBED_ASSERT(ret!=HAL_BUSY);
Kojto 70:2c525a50f1b6 201 return (ret == HAL_OK) ? true:false;
Kojto 70:2c525a50f1b6 202 }
Kojto 70:2c525a50f1b6 203
Kojto 70:2c525a50f1b6 204 // read setup packet
Kojto 70:2c525a50f1b6 205 void USBHAL::EP0setup(uint8_t *buffer) {
Kojto 70:2c525a50f1b6 206 memcpy(buffer, hpcd.Setup, MAX_PACKET_SIZE_SETUP);
Kojto 70:2c525a50f1b6 207 memset(hpcd.Setup,0,MAX_PACKET_SIZE_SETUP);
Kojto 70:2c525a50f1b6 208 }
Kojto 70:2c525a50f1b6 209
Kojto 70:2c525a50f1b6 210 void USBHAL::EP0readStage(void) {
Kojto 70:2c525a50f1b6 211 }
Kojto 70:2c525a50f1b6 212
Kojto 70:2c525a50f1b6 213 void USBHAL::EP0read(void) {
Kojto 70:2c525a50f1b6 214 USBHAL_Private_t *HALPriv = (USBHAL_Private_t *)hpcd.pData;
Kojto 70:2c525a50f1b6 215 uint32_t epIndex = EP_ADDR(EP0OUT);
Kojto 70:2c525a50f1b6 216 uint8_t *pBuf = (uint8_t *)HALPriv->pBufRx0;
Kojto 70:2c525a50f1b6 217 HAL_StatusTypeDef ret;
Kojto 70:2c525a50f1b6 218 HALPriv->epComplete[EP0OUT] = 2;
Kojto 70:2c525a50f1b6 219 ret = HAL_PCD_EP_Receive(&hpcd, epIndex, pBuf, MAX_PACKET_SIZE_EP0 );
Kojto 70:2c525a50f1b6 220 MBED_ASSERT(ret!=HAL_BUSY);
Kojto 70:2c525a50f1b6 221
Kojto 70:2c525a50f1b6 222 }
Kojto 70:2c525a50f1b6 223
Kojto 70:2c525a50f1b6 224 uint32_t USBHAL::EP0getReadResult(uint8_t *buffer) {
Kojto 70:2c525a50f1b6 225 USBHAL_Private_t *HALPriv = (USBHAL_Private_t *)hpcd.pData;
Kojto 70:2c525a50f1b6 226 uint32_t length = (uint32_t) HAL_PCD_EP_GetRxCount(&hpcd, 0);
Kojto 70:2c525a50f1b6 227 HALPriv->epComplete[EP0OUT] = 0;
Kojto 70:2c525a50f1b6 228 if (length) {
Kojto 70:2c525a50f1b6 229 uint8_t *buff = (uint8_t *)HALPriv->pBufRx0;
Kojto 70:2c525a50f1b6 230 memcpy(buffer, buff, length);
Kojto 70:2c525a50f1b6 231 }
Kojto 70:2c525a50f1b6 232 return length;
Kojto 70:2c525a50f1b6 233 }
Kojto 70:2c525a50f1b6 234
Kojto 70:2c525a50f1b6 235 void USBHAL::EP0write(uint8_t *buffer, uint32_t size) {
Kojto 70:2c525a50f1b6 236 /* check that endpoint maximum size is not exceeding TX fifo */
Kojto 70:2c525a50f1b6 237 MBED_ASSERT(hpcd.IN_ep[0].maxpacket >= size);
Kojto 70:2c525a50f1b6 238 endpointWrite(EP0IN, buffer, size);
Kojto 70:2c525a50f1b6 239 }
Kojto 70:2c525a50f1b6 240
Kojto 70:2c525a50f1b6 241 void USBHAL::EP0getWriteResult(void) {
Kojto 70:2c525a50f1b6 242
Kojto 70:2c525a50f1b6 243 }
Kojto 70:2c525a50f1b6 244
Kojto 70:2c525a50f1b6 245 void USBHAL::EP0stall(void) {
Kojto 70:2c525a50f1b6 246 stallEndpoint(EP0IN);
Kojto 70:2c525a50f1b6 247 }
Kojto 70:2c525a50f1b6 248
Kojto 70:2c525a50f1b6 249 EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize) {
Kojto 70:2c525a50f1b6 250 USBHAL_Private_t *HALPriv = (USBHAL_Private_t *)(hpcd.pData);
Kojto 70:2c525a50f1b6 251 uint32_t epIndex = EP_ADDR(endpoint);
Kojto 70:2c525a50f1b6 252 uint8_t* pBuf = (uint8_t *)HALPriv->pBufRx;
Kojto 70:2c525a50f1b6 253 HAL_StatusTypeDef ret;
Kojto 70:2c525a50f1b6 254 // clean reception end flag before requesting reception
Kojto 70:2c525a50f1b6 255 HALPriv->epComplete[endpoint] = 2;
Kojto 70:2c525a50f1b6 256 ret = HAL_PCD_EP_Receive(&hpcd, epIndex, pBuf, maximumSize);
Kojto 70:2c525a50f1b6 257 MBED_ASSERT(ret!=HAL_BUSY);
Kojto 70:2c525a50f1b6 258 return EP_PENDING;
Kojto 70:2c525a50f1b6 259 }
Kojto 70:2c525a50f1b6 260
Kojto 70:2c525a50f1b6 261 EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t * buffer, uint32_t *bytesRead) {
Kojto 70:2c525a50f1b6 262 USBHAL_Private_t *HALPriv = (USBHAL_Private_t *)(hpcd.pData);
Kojto 70:2c525a50f1b6 263 if (HALPriv->epComplete[endpoint]==0) {
Kojto 70:2c525a50f1b6 264 /* no reception possible !!! */
Kojto 70:2c525a50f1b6 265 bytesRead = 0;
Kojto 70:2c525a50f1b6 266 return EP_COMPLETED;
Kojto 70:2c525a50f1b6 267 }else if ((HALPriv->epComplete[endpoint]!=1))
Kojto 70:2c525a50f1b6 268 return EP_PENDING;
Kojto 70:2c525a50f1b6 269 uint32_t epIndex = EP_ADDR(endpoint);
Kojto 70:2c525a50f1b6 270 uint8_t *buff = (uint8_t *)HALPriv->pBufRx;
Kojto 70:2c525a50f1b6 271 uint32_t length = (uint32_t) HAL_PCD_EP_GetRxCount(&hpcd, epIndex);
Kojto 70:2c525a50f1b6 272 memcpy(buffer, buff, length);
Kojto 70:2c525a50f1b6 273 *bytesRead = length;
Kojto 70:2c525a50f1b6 274 HALPriv->epComplete[endpoint]= 0;
Kojto 70:2c525a50f1b6 275 return EP_COMPLETED;
Kojto 70:2c525a50f1b6 276 }
Kojto 70:2c525a50f1b6 277
Kojto 70:2c525a50f1b6 278 EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size) {
Kojto 70:2c525a50f1b6 279 USBHAL_Private_t *HALPriv = (USBHAL_Private_t *)(hpcd.pData);
Kojto 70:2c525a50f1b6 280 uint32_t epIndex = EP_ADDR(endpoint);
Kojto 70:2c525a50f1b6 281 HAL_StatusTypeDef ret;
Kojto 70:2c525a50f1b6 282 // clean transmission end flag before requesting transmission
Kojto 70:2c525a50f1b6 283 HALPriv->epComplete[endpoint] = 2;
Kojto 70:2c525a50f1b6 284 ret = HAL_PCD_EP_Transmit(&hpcd, epIndex, data, size);
Kojto 70:2c525a50f1b6 285 MBED_ASSERT(ret!=HAL_BUSY);
Kojto 70:2c525a50f1b6 286 // update the status
Kojto 70:2c525a50f1b6 287 if (ret != HAL_OK) return EP_INVALID;
Kojto 70:2c525a50f1b6 288 // fix me return is too simple
Kojto 70:2c525a50f1b6 289 return EP_PENDING;
Kojto 70:2c525a50f1b6 290 }
Kojto 70:2c525a50f1b6 291
Kojto 70:2c525a50f1b6 292 EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint) {
Kojto 70:2c525a50f1b6 293 USBHAL_Private_t *HALPriv = (USBHAL_Private_t *)(hpcd.pData);
Kojto 70:2c525a50f1b6 294 if (HALPriv->epComplete[endpoint] == 1)
Kojto 70:2c525a50f1b6 295 return EP_COMPLETED;
Kojto 70:2c525a50f1b6 296 return EP_PENDING;
Kojto 70:2c525a50f1b6 297 }
Kojto 70:2c525a50f1b6 298
Kojto 70:2c525a50f1b6 299 void USBHAL::stallEndpoint(uint8_t endpoint) {
Kojto 70:2c525a50f1b6 300 USBHAL_Private_t *HALPriv = (USBHAL_Private_t *)(hpcd.pData);
Kojto 70:2c525a50f1b6 301 HAL_StatusTypeDef ret;
Kojto 70:2c525a50f1b6 302 HALPriv->epComplete[endpoint] = 0;
Kojto 70:2c525a50f1b6 303 ret = HAL_PCD_EP_SetStall(&hpcd, EP_ADDR(endpoint));
Kojto 70:2c525a50f1b6 304 MBED_ASSERT(ret!=HAL_BUSY);
Kojto 70:2c525a50f1b6 305 }
Kojto 70:2c525a50f1b6 306
Kojto 70:2c525a50f1b6 307 void USBHAL::unstallEndpoint(uint8_t endpoint) {
Kojto 70:2c525a50f1b6 308 HAL_StatusTypeDef ret;
Kojto 70:2c525a50f1b6 309 ret = HAL_PCD_EP_ClrStall(&hpcd, EP_ADDR(endpoint));
Kojto 70:2c525a50f1b6 310 MBED_ASSERT(ret!=HAL_BUSY);
Kojto 70:2c525a50f1b6 311
Kojto 70:2c525a50f1b6 312 }
Kojto 70:2c525a50f1b6 313
Kojto 70:2c525a50f1b6 314 bool USBHAL::getEndpointStallState(uint8_t endpoint) {
Kojto 70:2c525a50f1b6 315 return false;
Kojto 70:2c525a50f1b6 316 }
Kojto 70:2c525a50f1b6 317
Kojto 70:2c525a50f1b6 318 void USBHAL::remoteWakeup(void) {
Kojto 70:2c525a50f1b6 319 }
Kojto 70:2c525a50f1b6 320
Kojto 70:2c525a50f1b6 321
Kojto 70:2c525a50f1b6 322 void USBHAL::_usbisr(void) {
Kojto 70:2c525a50f1b6 323 instance->usbisr();
Kojto 70:2c525a50f1b6 324 }
Kojto 70:2c525a50f1b6 325
Kojto 70:2c525a50f1b6 326
Kojto 70:2c525a50f1b6 327 void USBHAL::usbisr(void) {
Kojto 70:2c525a50f1b6 328
Kojto 70:2c525a50f1b6 329 HAL_PCD_IRQHandler(&instance->hpcd);
Kojto 70:2c525a50f1b6 330 }
Kojto 70:2c525a50f1b6 331 #endif
Kojto 70:2c525a50f1b6 332