works!

Dependencies:   mbed

Committer:
mjoun
Date:
Fri Feb 21 20:40:56 2020 +0000
Revision:
1:6e512faaa17c
works

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mjoun 1:6e512faaa17c 1 /* Copyright 2015 Silicon Labs, http://www.silabs.com
mjoun 1:6e512faaa17c 2 *
mjoun 1:6e512faaa17c 3 * Licensed under the Apache License, Version 2.0 (the "License");
mjoun 1:6e512faaa17c 4 * you may not use this file except in compliance with the License.
mjoun 1:6e512faaa17c 5 * You may obtain a copy of the License at
mjoun 1:6e512faaa17c 6 *
mjoun 1:6e512faaa17c 7 * http://www.apache.org/licenses/LICENSE-2.0
mjoun 1:6e512faaa17c 8 *
mjoun 1:6e512faaa17c 9 * Unless required by applicable law or agreed to in writing, software
mjoun 1:6e512faaa17c 10 * distributed under the License is distributed on an "AS IS" BASIS,
mjoun 1:6e512faaa17c 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mjoun 1:6e512faaa17c 12 * See the License for the specific language governing permissions and
mjoun 1:6e512faaa17c 13 * limitations under the License.
mjoun 1:6e512faaa17c 14 */
mjoun 1:6e512faaa17c 15
mjoun 1:6e512faaa17c 16 #if defined TARGET_EFM32GG_STK3700 || \
mjoun 1:6e512faaa17c 17 defined TARGET_EFM32LG_STK3600 || \
mjoun 1:6e512faaa17c 18 defined TARGET_EFM32WG_STK3800 || \
mjoun 1:6e512faaa17c 19 defined TARGET_EFM32HG_STK3400
mjoun 1:6e512faaa17c 20
mjoun 1:6e512faaa17c 21 #include "USBHAL.h"
mjoun 1:6e512faaa17c 22 #include "em_usb.h"
mjoun 1:6e512faaa17c 23 #include "em_usbtypes.h"
mjoun 1:6e512faaa17c 24 #include "em_usbhal.h"
mjoun 1:6e512faaa17c 25 #include "em_usbd.h"
mjoun 1:6e512faaa17c 26
mjoun 1:6e512faaa17c 27 #include "sleepmodes.h"
mjoun 1:6e512faaa17c 28
mjoun 1:6e512faaa17c 29 enum USBISRCommand {
mjoun 1:6e512faaa17c 30 CMD_HANDLED = 0,
mjoun 1:6e512faaa17c 31 CMD_EP0SETUP,
mjoun 1:6e512faaa17c 32 CMD_EP0IN,
mjoun 1:6e512faaa17c 33 CMD_EP0OUT,
mjoun 1:6e512faaa17c 34 CMD_EP_XFER_COMPLETED,
mjoun 1:6e512faaa17c 35 CMD_SOF,
mjoun 1:6e512faaa17c 36 CMD_BUSRESET,
mjoun 1:6e512faaa17c 37 CMD_SUSPEND_STATE_CHANGED,
mjoun 1:6e512faaa17c 38 CMD_ENUM_END_MARKER
mjoun 1:6e512faaa17c 39 };
mjoun 1:6e512faaa17c 40
mjoun 1:6e512faaa17c 41 enum IEPStatus {
mjoun 1:6e512faaa17c 42 NOT_CONFIGURED = 0,
mjoun 1:6e512faaa17c 43 IDLE = 1,
mjoun 1:6e512faaa17c 44 READ_PENDING = 2,
mjoun 1:6e512faaa17c 45 WRITE_PENDING = 3,
mjoun 1:6e512faaa17c 46 READ_COMPLETE = 4,
mjoun 1:6e512faaa17c 47 WRITE_COMPLETE = 5,
mjoun 1:6e512faaa17c 48 FAILED_INVALID = 6,
mjoun 1:6e512faaa17c 49 FAILED_STALLED = 7
mjoun 1:6e512faaa17c 50 };
mjoun 1:6e512faaa17c 51
mjoun 1:6e512faaa17c 52 typedef struct {
mjoun 1:6e512faaa17c 53 IEPStatus status;
mjoun 1:6e512faaa17c 54 uint32_t byte_count;
mjoun 1:6e512faaa17c 55 uint32_t max_packet;
mjoun 1:6e512faaa17c 56 USB_XferCompleteCb_TypeDef intern_cb;
mjoun 1:6e512faaa17c 57 uint8_t *data_buf;
mjoun 1:6e512faaa17c 58 } ep_state_t;
mjoun 1:6e512faaa17c 59
mjoun 1:6e512faaa17c 60 USBHAL * USBHAL::instance;
mjoun 1:6e512faaa17c 61 static uint8_t ep0setupdata[8];
mjoun 1:6e512faaa17c 62 static ep_state_t ep_state[NUMBER_OF_ENDPOINTS];
mjoun 1:6e512faaa17c 63 #ifdef USB_USE_DYNAMIC_MEMORY
mjoun 1:6e512faaa17c 64 static uint8_t ep0in_data_buf[MAX_PACKET_SIZE_EP0] __attribute__ ((aligned (4)));
mjoun 1:6e512faaa17c 65 static uint8_t ep0out_data_buf[MAX_PACKET_SIZE_EP0]; // FIXME: does this need to be this big?
mjoun 1:6e512faaa17c 66 #else
mjoun 1:6e512faaa17c 67 static uint8_t ep_data_buf[NUMBER_OF_ENDPOINTS][64] __attribute__ ((aligned (4)));
mjoun 1:6e512faaa17c 68 #endif
mjoun 1:6e512faaa17c 69
mjoun 1:6e512faaa17c 70 static void run_cmd(USBISRCommand cmd, uint32_t param);
mjoun 1:6e512faaa17c 71 static void (*isrptr)() = NULL;
mjoun 1:6e512faaa17c 72 static USBISRCommand usb_isrcmd = CMD_HANDLED;
mjoun 1:6e512faaa17c 73 static uint32_t usb_isrcmd_param = 0;
mjoun 1:6e512faaa17c 74
mjoun 1:6e512faaa17c 75 extern "C" void usbhal_allow_em2(bool allow_em2);
mjoun 1:6e512faaa17c 76
mjoun 1:6e512faaa17c 77 #ifdef DEBUG_USB_API
mjoun 1:6e512faaa17c 78 #define TRACE(fmt,...) printf("USB: %s: " fmt "\n", __func__, __VA_ARGS__);
mjoun 1:6e512faaa17c 79 #define TRACE_FUNC_IN printf("USB: > %s\n",__func__);
mjoun 1:6e512faaa17c 80 #define TRACE_FUNC_IN_P(fmt, ...) printf("USB: > %s: " fmt "\n", __func__, __VA_ARGS__);
mjoun 1:6e512faaa17c 81 #else
mjoun 1:6e512faaa17c 82 #define TRACE(fmt,...)
mjoun 1:6e512faaa17c 83 #define TRACE_FUNC_IN
mjoun 1:6e512faaa17c 84 #define TRACE_FUNC_IN_P(fmt, ...)
mjoun 1:6e512faaa17c 85 #endif
mjoun 1:6e512faaa17c 86
mjoun 1:6e512faaa17c 87 static EP_STATUS internEndpointRead(uint8_t ep, uint32_t maxSize);
mjoun 1:6e512faaa17c 88
mjoun 1:6e512faaa17c 89 static int usbhal_xfer_complete_cb(uint8_t epaddr, USB_Status_TypeDef status,
mjoun 1:6e512faaa17c 90 uint32_t xferred, uint32_t remaining);
mjoun 1:6e512faaa17c 91 static void usbhal_free_buffers(void);
mjoun 1:6e512faaa17c 92
mjoun 1:6e512faaa17c 93 /* Internal EP transfer complete callbacks */
mjoun 1:6e512faaa17c 94 #define EPCB(n) static int usbhal_xfer_complete_cb_##n(USB_Status_TypeDef status, \
mjoun 1:6e512faaa17c 95 uint32_t xferred, uint32_t remaining) { \
mjoun 1:6e512faaa17c 96 return usbhal_xfer_complete_cb(n, status, xferred, remaining); \
mjoun 1:6e512faaa17c 97 }
mjoun 1:6e512faaa17c 98 /* ------^ */
mjoun 1:6e512faaa17c 99 EPCB(EP0OUT)
mjoun 1:6e512faaa17c 100 EPCB(EP0IN)
mjoun 1:6e512faaa17c 101 EPCB(EP1OUT)
mjoun 1:6e512faaa17c 102 EPCB(EP1IN)
mjoun 1:6e512faaa17c 103 EPCB(EP2OUT)
mjoun 1:6e512faaa17c 104 EPCB(EP2IN)
mjoun 1:6e512faaa17c 105 EPCB(EP3OUT)
mjoun 1:6e512faaa17c 106 EPCB(EP3IN)
mjoun 1:6e512faaa17c 107 #ifndef TARGET_EFM32HG_STK3400
mjoun 1:6e512faaa17c 108 EPCB(EP4OUT)
mjoun 1:6e512faaa17c 109 EPCB(EP4IN)
mjoun 1:6e512faaa17c 110 EPCB(EP5OUT)
mjoun 1:6e512faaa17c 111 EPCB(EP5IN)
mjoun 1:6e512faaa17c 112 EPCB(EP6OUT)
mjoun 1:6e512faaa17c 113 EPCB(EP6IN)
mjoun 1:6e512faaa17c 114 #endif
mjoun 1:6e512faaa17c 115
mjoun 1:6e512faaa17c 116 static inline bool is_aligned(const void *pointer, size_t byte_count)
mjoun 1:6e512faaa17c 117 {
mjoun 1:6e512faaa17c 118 return ((uintptr_t)pointer % byte_count == 0);
mjoun 1:6e512faaa17c 119 }
mjoun 1:6e512faaa17c 120
mjoun 1:6e512faaa17c 121 USBHAL::USBHAL(void)
mjoun 1:6e512faaa17c 122 {
mjoun 1:6e512faaa17c 123 TRACE_FUNC_IN;
mjoun 1:6e512faaa17c 124
mjoun 1:6e512faaa17c 125 isrptr = &USBHAL::_usbisr;
mjoun 1:6e512faaa17c 126
mjoun 1:6e512faaa17c 127 if (instance) {
mjoun 1:6e512faaa17c 128 TRACE("Assert self failed! instance=%p", instance);
mjoun 1:6e512faaa17c 129 abort();
mjoun 1:6e512faaa17c 130 }
mjoun 1:6e512faaa17c 131 instance = this;
mjoun 1:6e512faaa17c 132
mjoun 1:6e512faaa17c 133 // When USB is active, we can't go below EM1. This block may
mjoun 1:6e512faaa17c 134 // be dynamically removed/reinstated to allow deeper sleep.
mjoun 1:6e512faaa17c 135 usbhal_allow_em2(false);
mjoun 1:6e512faaa17c 136
mjoun 1:6e512faaa17c 137 // When in suspend / Vbus off we can go to EM2, but never below
mjoun 1:6e512faaa17c 138 // that as long as USB is being used. Despite the name the call here
mjoun 1:6e512faaa17c 139 // blocks entering modes _below_ EM2, but allows EM2.
mjoun 1:6e512faaa17c 140 blockSleepMode(EM2);
mjoun 1:6e512faaa17c 141
mjoun 1:6e512faaa17c 142 epCallback[EP0OUT] = NULL;
mjoun 1:6e512faaa17c 143 epCallback[EP0IN ] = NULL;
mjoun 1:6e512faaa17c 144 epCallback[EP1OUT] = &USBHAL::EP1_OUT_callback;
mjoun 1:6e512faaa17c 145 epCallback[EP1IN ] = &USBHAL::EP1_IN_callback;
mjoun 1:6e512faaa17c 146 epCallback[EP2OUT] = &USBHAL::EP2_OUT_callback;
mjoun 1:6e512faaa17c 147 epCallback[EP2IN ] = &USBHAL::EP2_IN_callback;
mjoun 1:6e512faaa17c 148 epCallback[EP3OUT] = &USBHAL::EP3_OUT_callback;
mjoun 1:6e512faaa17c 149 epCallback[EP3IN ] = &USBHAL::EP3_IN_callback;
mjoun 1:6e512faaa17c 150 #ifndef TARGET_EFM32HG_STK3400
mjoun 1:6e512faaa17c 151 epCallback[EP4OUT] = &USBHAL::EP4_OUT_callback;
mjoun 1:6e512faaa17c 152 epCallback[EP4IN ] = &USBHAL::EP4_IN_callback;
mjoun 1:6e512faaa17c 153 epCallback[EP5OUT] = &USBHAL::EP5_OUT_callback;
mjoun 1:6e512faaa17c 154 epCallback[EP5IN ] = &USBHAL::EP5_IN_callback;
mjoun 1:6e512faaa17c 155 epCallback[EP6OUT] = &USBHAL::EP6_OUT_callback;
mjoun 1:6e512faaa17c 156 epCallback[EP6IN ] = &USBHAL::EP6_IN_callback;
mjoun 1:6e512faaa17c 157 #endif
mjoun 1:6e512faaa17c 158
mjoun 1:6e512faaa17c 159 memset(ep_state, 0, sizeof(ep_state));
mjoun 1:6e512faaa17c 160
mjoun 1:6e512faaa17c 161 ep_state[EP0OUT].intern_cb = usbhal_xfer_complete_cb_EP0OUT;
mjoun 1:6e512faaa17c 162 ep_state[EP0IN ].intern_cb = usbhal_xfer_complete_cb_EP0IN;
mjoun 1:6e512faaa17c 163 ep_state[EP1OUT].intern_cb = usbhal_xfer_complete_cb_EP1OUT;
mjoun 1:6e512faaa17c 164 ep_state[EP1IN ].intern_cb = usbhal_xfer_complete_cb_EP1IN;
mjoun 1:6e512faaa17c 165 ep_state[EP2OUT].intern_cb = usbhal_xfer_complete_cb_EP2OUT;
mjoun 1:6e512faaa17c 166 ep_state[EP2IN ].intern_cb = usbhal_xfer_complete_cb_EP2IN;
mjoun 1:6e512faaa17c 167 ep_state[EP3OUT].intern_cb = usbhal_xfer_complete_cb_EP3OUT;
mjoun 1:6e512faaa17c 168 ep_state[EP3IN ].intern_cb = usbhal_xfer_complete_cb_EP3IN;
mjoun 1:6e512faaa17c 169 #ifndef TARGET_EFM32HG_STK3400
mjoun 1:6e512faaa17c 170 ep_state[EP4OUT].intern_cb = usbhal_xfer_complete_cb_EP4OUT;
mjoun 1:6e512faaa17c 171 ep_state[EP4IN ].intern_cb = usbhal_xfer_complete_cb_EP4IN;
mjoun 1:6e512faaa17c 172 ep_state[EP5OUT].intern_cb = usbhal_xfer_complete_cb_EP5OUT;
mjoun 1:6e512faaa17c 173 ep_state[EP5IN ].intern_cb = usbhal_xfer_complete_cb_EP5IN;
mjoun 1:6e512faaa17c 174 ep_state[EP6OUT].intern_cb = usbhal_xfer_complete_cb_EP6OUT;
mjoun 1:6e512faaa17c 175 ep_state[EP6IN ].intern_cb = usbhal_xfer_complete_cb_EP6IN;
mjoun 1:6e512faaa17c 176 #endif
mjoun 1:6e512faaa17c 177
mjoun 1:6e512faaa17c 178 #ifdef USB_USE_DYNAMIC_MEMORY
mjoun 1:6e512faaa17c 179 ep_state[EP0OUT].data_buf = ep0out_data_buf;
mjoun 1:6e512faaa17c 180 ep_state[EP0IN].data_buf = ep0in_data_buf;
mjoun 1:6e512faaa17c 181 #else
mjoun 1:6e512faaa17c 182 for (int i=0 ; i<NUMBER_OF_ENDPOINTS ; i++) {
mjoun 1:6e512faaa17c 183 ep_state[i].data_buf = ep_data_buf[i];
mjoun 1:6e512faaa17c 184 }
mjoun 1:6e512faaa17c 185 #endif
mjoun 1:6e512faaa17c 186 }
mjoun 1:6e512faaa17c 187
mjoun 1:6e512faaa17c 188 USBHAL::~USBHAL(void)
mjoun 1:6e512faaa17c 189 {
mjoun 1:6e512faaa17c 190 TRACE_FUNC_IN;
mjoun 1:6e512faaa17c 191 USBD_AbortAllTransfers();
mjoun 1:6e512faaa17c 192 USBD_Disconnect();
mjoun 1:6e512faaa17c 193 usbhal_free_buffers();
mjoun 1:6e512faaa17c 194
mjoun 1:6e512faaa17c 195 usbhal_allow_em2(true);
mjoun 1:6e512faaa17c 196 unblockSleepMode(EM2);
mjoun 1:6e512faaa17c 197 }
mjoun 1:6e512faaa17c 198
mjoun 1:6e512faaa17c 199 extern "C" void usbhal_allow_em2(bool allow_em2)
mjoun 1:6e512faaa17c 200 {
mjoun 1:6e512faaa17c 201 if (allow_em2) {
mjoun 1:6e512faaa17c 202 // unblockSleepMode is safe to call even if we would unblock
mjoun 1:6e512faaa17c 203 // an already unblocked mode, so no checks here.
mjoun 1:6e512faaa17c 204 unblockSleepMode(EM1);
mjoun 1:6e512faaa17c 205 } else {
mjoun 1:6e512faaa17c 206 blockSleepMode(EM1);
mjoun 1:6e512faaa17c 207 }
mjoun 1:6e512faaa17c 208 }
mjoun 1:6e512faaa17c 209
mjoun 1:6e512faaa17c 210 static void usbhal_reset_cb(void)
mjoun 1:6e512faaa17c 211 {
mjoun 1:6e512faaa17c 212 TRACE_FUNC_IN;
mjoun 1:6e512faaa17c 213 run_cmd(CMD_BUSRESET, 0);
mjoun 1:6e512faaa17c 214 }
mjoun 1:6e512faaa17c 215
mjoun 1:6e512faaa17c 216 #ifdef DEBUG_USB_API
mjoun 1:6e512faaa17c 217 static const char *usbstate[] = { "NONE", "ATTACHED", "POWERED", "DEFAULT",
mjoun 1:6e512faaa17c 218 "ADDRESSED", "CONFIGURED", "SUSPENDED", "???" };
mjoun 1:6e512faaa17c 219 #endif
mjoun 1:6e512faaa17c 220
mjoun 1:6e512faaa17c 221 static void usbhal_state_change_cb(USBD_State_TypeDef oldState,
mjoun 1:6e512faaa17c 222 USBD_State_TypeDef newState)
mjoun 1:6e512faaa17c 223 {
mjoun 1:6e512faaa17c 224 TRACE("state changed %s -> %s", usbstate[oldState], usbstate[newState]);
mjoun 1:6e512faaa17c 225
mjoun 1:6e512faaa17c 226 if (oldState == USBD_STATE_SUSPENDED) {
mjoun 1:6e512faaa17c 227 run_cmd(CMD_SUSPEND_STATE_CHANGED, 0);
mjoun 1:6e512faaa17c 228 }
mjoun 1:6e512faaa17c 229
mjoun 1:6e512faaa17c 230 if (newState == USBD_STATE_SUSPENDED) {
mjoun 1:6e512faaa17c 231 run_cmd(CMD_SUSPEND_STATE_CHANGED, 1);
mjoun 1:6e512faaa17c 232 }
mjoun 1:6e512faaa17c 233
mjoun 1:6e512faaa17c 234 // Should call connectStateChanged from here as well but there is
mjoun 1:6e512faaa17c 235 // no documentation on when to actually do so. (And the implementation
mjoun 1:6e512faaa17c 236 // in USBDevice.cpp is a stub)
mjoun 1:6e512faaa17c 237
mjoun 1:6e512faaa17c 238 // HACK! Since connectStateChanged is not used, indicate the loss
mjoun 1:6e512faaa17c 239 // off connection by reporting a bus reset. This causes USBDevice
mjoun 1:6e512faaa17c 240 // to realise that at least it's not in CONFIGURED anymore, and
mjoun 1:6e512faaa17c 241 // stop trying to read/write in a busyloop.
mjoun 1:6e512faaa17c 242 if (newState == USBD_STATE_NONE) {
mjoun 1:6e512faaa17c 243 run_cmd(CMD_BUSRESET, 0);
mjoun 1:6e512faaa17c 244 }
mjoun 1:6e512faaa17c 245 }
mjoun 1:6e512faaa17c 246
mjoun 1:6e512faaa17c 247 static int usbhal_setupcmd_cb(const USB_Setup_TypeDef *setup)
mjoun 1:6e512faaa17c 248 {
mjoun 1:6e512faaa17c 249 TRACE_FUNC_IN;
mjoun 1:6e512faaa17c 250 if (!setup) {
mjoun 1:6e512faaa17c 251 EFM_ASSERT(false);
mjoun 1:6e512faaa17c 252 return USB_STATUS_REQ_ERR;
mjoun 1:6e512faaa17c 253 }
mjoun 1:6e512faaa17c 254
mjoun 1:6e512faaa17c 255 memcpy(ep0setupdata, setup, 8);
mjoun 1:6e512faaa17c 256 run_cmd(CMD_EP0SETUP, 0);
mjoun 1:6e512faaa17c 257
mjoun 1:6e512faaa17c 258 return USB_STATUS_OK;
mjoun 1:6e512faaa17c 259 }
mjoun 1:6e512faaa17c 260
mjoun 1:6e512faaa17c 261 static void usbhal_sof_cb(uint16_t frameNum)
mjoun 1:6e512faaa17c 262 {
mjoun 1:6e512faaa17c 263 run_cmd(CMD_SOF, frameNum);
mjoun 1:6e512faaa17c 264 }
mjoun 1:6e512faaa17c 265
mjoun 1:6e512faaa17c 266 static void usbhal_free_buffers(void)
mjoun 1:6e512faaa17c 267 {
mjoun 1:6e512faaa17c 268 #ifdef USB_USE_DYNAMIC_MEMORY
mjoun 1:6e512faaa17c 269 TRACE_FUNC_IN;
mjoun 1:6e512faaa17c 270
mjoun 1:6e512faaa17c 271 for (int i=EP1OUT ; i<NUMBER_OF_ENDPOINTS ; i++ ) {
mjoun 1:6e512faaa17c 272 if (ep_state[i].data_buf) {
mjoun 1:6e512faaa17c 273 free(ep_state[i].data_buf);
mjoun 1:6e512faaa17c 274 ep_state[i].data_buf = NULL;
mjoun 1:6e512faaa17c 275 }
mjoun 1:6e512faaa17c 276 }
mjoun 1:6e512faaa17c 277 #endif
mjoun 1:6e512faaa17c 278 }
mjoun 1:6e512faaa17c 279
mjoun 1:6e512faaa17c 280 void USBHAL::connect(void)
mjoun 1:6e512faaa17c 281 {
mjoun 1:6e512faaa17c 282 TRACE_FUNC_IN;
mjoun 1:6e512faaa17c 283
mjoun 1:6e512faaa17c 284 // Init datastructures must be static - driver will use these even after the init function exits!
mjoun 1:6e512faaa17c 285
mjoun 1:6e512faaa17c 286 static const uint8_t buffer_multiplier[] = { 1 }; // Mult 1 for control EP
mjoun 1:6e512faaa17c 287 static const USBD_Callbacks_TypeDef usbd_callbacks = {
mjoun 1:6e512faaa17c 288 .usbReset = usbhal_reset_cb,
mjoun 1:6e512faaa17c 289 .usbStateChange = usbhal_state_change_cb,
mjoun 1:6e512faaa17c 290 .setupCmd = usbhal_setupcmd_cb,
mjoun 1:6e512faaa17c 291 .isSelfPowered = NULL,
mjoun 1:6e512faaa17c 292 .sofInt = usbhal_sof_cb
mjoun 1:6e512faaa17c 293 };
mjoun 1:6e512faaa17c 294
mjoun 1:6e512faaa17c 295 USBD_Init_TypeDef initdata = {
mjoun 1:6e512faaa17c 296 .deviceDescriptor = NULL,
mjoun 1:6e512faaa17c 297 .configDescriptor = NULL,
mjoun 1:6e512faaa17c 298 .stringDescriptors = NULL,
mjoun 1:6e512faaa17c 299 .numberOfStrings = 0,
mjoun 1:6e512faaa17c 300 .bufferingMultiplier = buffer_multiplier,
mjoun 1:6e512faaa17c 301 .callbacks = &usbd_callbacks,
mjoun 1:6e512faaa17c 302 .reserved = 0
mjoun 1:6e512faaa17c 303 };
mjoun 1:6e512faaa17c 304
mjoun 1:6e512faaa17c 305 int ret = USBD_Init(&initdata);
mjoun 1:6e512faaa17c 306
mjoun 1:6e512faaa17c 307 TRACE("init = %d, devicedesc = %lx, configdesc = %lx", ret,
mjoun 1:6e512faaa17c 308 (uint32_t) initdata.deviceDescriptor,
mjoun 1:6e512faaa17c 309 (uint32_t) initdata.configDescriptor);
mjoun 1:6e512faaa17c 310
mjoun 1:6e512faaa17c 311 EFM_ASSERT(ret == USB_STATUS_OK);
mjoun 1:6e512faaa17c 312 }
mjoun 1:6e512faaa17c 313
mjoun 1:6e512faaa17c 314 void USBHAL::disconnect(void)
mjoun 1:6e512faaa17c 315 {
mjoun 1:6e512faaa17c 316 TRACE_FUNC_IN;
mjoun 1:6e512faaa17c 317 USBD_Disconnect();
mjoun 1:6e512faaa17c 318 }
mjoun 1:6e512faaa17c 319
mjoun 1:6e512faaa17c 320 void USBHAL::configureDevice(void)
mjoun 1:6e512faaa17c 321 {
mjoun 1:6e512faaa17c 322 TRACE_FUNC_IN;
mjoun 1:6e512faaa17c 323 USBD_SetUsbState(USBD_STATE_CONFIGURED);
mjoun 1:6e512faaa17c 324 }
mjoun 1:6e512faaa17c 325
mjoun 1:6e512faaa17c 326 void USBHAL::unconfigureDevice(void)
mjoun 1:6e512faaa17c 327 {
mjoun 1:6e512faaa17c 328 TRACE_FUNC_IN;
mjoun 1:6e512faaa17c 329 USBD_SetUsbState(USBD_STATE_DEFAULT);
mjoun 1:6e512faaa17c 330 usbhal_free_buffers();
mjoun 1:6e512faaa17c 331 }
mjoun 1:6e512faaa17c 332
mjoun 1:6e512faaa17c 333 void USBHAL::setAddress(uint8_t address)
mjoun 1:6e512faaa17c 334 {
mjoun 1:6e512faaa17c 335 TRACE_FUNC_IN_P("addr 0x%x", (unsigned)address);
mjoun 1:6e512faaa17c 336 USBD_SetAddress(address);
mjoun 1:6e512faaa17c 337 }
mjoun 1:6e512faaa17c 338
mjoun 1:6e512faaa17c 339 void USBHAL::remoteWakeup(void)
mjoun 1:6e512faaa17c 340 {
mjoun 1:6e512faaa17c 341 TRACE_FUNC_IN;
mjoun 1:6e512faaa17c 342 USBD_RemoteWakeup();
mjoun 1:6e512faaa17c 343 }
mjoun 1:6e512faaa17c 344
mjoun 1:6e512faaa17c 345 void USBHAL::EP0setup(uint8_t *buffer)
mjoun 1:6e512faaa17c 346 {
mjoun 1:6e512faaa17c 347 TRACE_FUNC_IN;
mjoun 1:6e512faaa17c 348 EFM_ASSERT(buffer);
mjoun 1:6e512faaa17c 349 if (buffer) {
mjoun 1:6e512faaa17c 350 memcpy(buffer, ep0setupdata, 8);
mjoun 1:6e512faaa17c 351 }
mjoun 1:6e512faaa17c 352 }
mjoun 1:6e512faaa17c 353
mjoun 1:6e512faaa17c 354 void USBHAL::EP0read(void)
mjoun 1:6e512faaa17c 355 {
mjoun 1:6e512faaa17c 356 TRACE_FUNC_IN;
mjoun 1:6e512faaa17c 357 (void)internEndpointRead(0, MAX_PACKET_SIZE_EP0);
mjoun 1:6e512faaa17c 358 }
mjoun 1:6e512faaa17c 359
mjoun 1:6e512faaa17c 360 void USBHAL::EP0readStage(void)
mjoun 1:6e512faaa17c 361 {
mjoun 1:6e512faaa17c 362 TRACE_FUNC_IN;
mjoun 1:6e512faaa17c 363 // Not needed
mjoun 1:6e512faaa17c 364 }
mjoun 1:6e512faaa17c 365
mjoun 1:6e512faaa17c 366 uint32_t USBHAL::EP0getReadResult(uint8_t *buffer)
mjoun 1:6e512faaa17c 367 {
mjoun 1:6e512faaa17c 368 TRACE_FUNC_IN;
mjoun 1:6e512faaa17c 369 EFM_ASSERT(buffer);
mjoun 1:6e512faaa17c 370
mjoun 1:6e512faaa17c 371 uint32_t read = 0;
mjoun 1:6e512faaa17c 372 endpointReadResult(0, buffer, &read);
mjoun 1:6e512faaa17c 373 return read;
mjoun 1:6e512faaa17c 374 }
mjoun 1:6e512faaa17c 375
mjoun 1:6e512faaa17c 376 static int usbhal_xfer_complete_cb(uint8_t ep, USB_Status_TypeDef status,
mjoun 1:6e512faaa17c 377 uint32_t xferred, uint32_t remaining)
mjoun 1:6e512faaa17c 378 {
mjoun 1:6e512faaa17c 379 TRACE_FUNC_IN_P("ep 0x%x, status %u, xferred %lu, rem %lu",
mjoun 1:6e512faaa17c 380 ep, status, xferred, remaining);
mjoun 1:6e512faaa17c 381
mjoun 1:6e512faaa17c 382 if (ep >= NUMBER_OF_ENDPOINTS) {
mjoun 1:6e512faaa17c 383 EFM_ASSERT(false);
mjoun 1:6e512faaa17c 384 return USB_STATUS_REQ_ERR;
mjoun 1:6e512faaa17c 385 }
mjoun 1:6e512faaa17c 386
mjoun 1:6e512faaa17c 387 switch (ep) {
mjoun 1:6e512faaa17c 388 case EP0OUT:
mjoun 1:6e512faaa17c 389 if (ep_state[EP0OUT].status == READ_PENDING) {
mjoun 1:6e512faaa17c 390 ep_state[EP0OUT].status = READ_COMPLETE;
mjoun 1:6e512faaa17c 391 ep_state[EP0OUT].byte_count = xferred;
mjoun 1:6e512faaa17c 392 // drop zlp
mjoun 1:6e512faaa17c 393 if (xferred == 0) {
mjoun 1:6e512faaa17c 394 break;
mjoun 1:6e512faaa17c 395 }
mjoun 1:6e512faaa17c 396 }
mjoun 1:6e512faaa17c 397 run_cmd(CMD_EP0OUT, 0);
mjoun 1:6e512faaa17c 398 break;
mjoun 1:6e512faaa17c 399
mjoun 1:6e512faaa17c 400 case EP0IN:
mjoun 1:6e512faaa17c 401 run_cmd(CMD_EP0IN, 0);
mjoun 1:6e512faaa17c 402 break;
mjoun 1:6e512faaa17c 403
mjoun 1:6e512faaa17c 404 default:
mjoun 1:6e512faaa17c 405 bool write = ep & 1;
mjoun 1:6e512faaa17c 406
mjoun 1:6e512faaa17c 407 if (status == USB_STATUS_OK) {
mjoun 1:6e512faaa17c 408 if (!write && ep_state[ep].status == READ_PENDING) {
mjoun 1:6e512faaa17c 409 ep_state[ep].status = READ_COMPLETE;
mjoun 1:6e512faaa17c 410 ep_state[ep].byte_count = xferred;
mjoun 1:6e512faaa17c 411 } else if (write && ep_state[ep].status == WRITE_PENDING) {
mjoun 1:6e512faaa17c 412 ep_state[ep].status = WRITE_COMPLETE;
mjoun 1:6e512faaa17c 413 } else {
mjoun 1:6e512faaa17c 414 ep_state[ep].status = FAILED_INVALID;
mjoun 1:6e512faaa17c 415 }
mjoun 1:6e512faaa17c 416 } else {
mjoun 1:6e512faaa17c 417 ep_state[ep].status = FAILED_INVALID;
mjoun 1:6e512faaa17c 418 }
mjoun 1:6e512faaa17c 419
mjoun 1:6e512faaa17c 420 if (ep_state[ep].status != FAILED_INVALID) {
mjoun 1:6e512faaa17c 421 run_cmd(CMD_EP_XFER_COMPLETED, ep);
mjoun 1:6e512faaa17c 422 }
mjoun 1:6e512faaa17c 423 break;
mjoun 1:6e512faaa17c 424 }
mjoun 1:6e512faaa17c 425
mjoun 1:6e512faaa17c 426 return USB_STATUS_OK;
mjoun 1:6e512faaa17c 427 }
mjoun 1:6e512faaa17c 428
mjoun 1:6e512faaa17c 429 void USBHAL::EP0write(uint8_t *buffer, uint32_t size)
mjoun 1:6e512faaa17c 430 {
mjoun 1:6e512faaa17c 431 //TRACE_FUNC_IN_P("buffer %lx, size %lu", (uint32_t) buffer, size);
mjoun 1:6e512faaa17c 432
mjoun 1:6e512faaa17c 433 int ret;
mjoun 1:6e512faaa17c 434 USB_XferCompleteCb_TypeDef cb = ep_state[EP0IN].intern_cb;
mjoun 1:6e512faaa17c 435
mjoun 1:6e512faaa17c 436 EFM_ASSERT((buffer != NULL) || (size == 0));
mjoun 1:6e512faaa17c 437 EFM_ASSERT(size <= MAX_PACKET_SIZE_EP0);
mjoun 1:6e512faaa17c 438
mjoun 1:6e512faaa17c 439 if (!buffer || size == 0) {
mjoun 1:6e512faaa17c 440 // No callback after writing EP0 ZLP
mjoun 1:6e512faaa17c 441 cb = NULL;
mjoun 1:6e512faaa17c 442 }
mjoun 1:6e512faaa17c 443
mjoun 1:6e512faaa17c 444 if (buffer && !is_aligned(buffer,4)) {
mjoun 1:6e512faaa17c 445 // Copy unaligned data to write-buffer before USBD_Write
mjoun 1:6e512faaa17c 446 memcpy(ep_state[EP0IN].data_buf, buffer, size);
mjoun 1:6e512faaa17c 447 ret = USBD_Write(0, ep_state[EP0IN].data_buf, size, cb);
mjoun 1:6e512faaa17c 448 } else {
mjoun 1:6e512faaa17c 449 ret = USBD_Write(0, buffer, size, cb);
mjoun 1:6e512faaa17c 450 }
mjoun 1:6e512faaa17c 451
mjoun 1:6e512faaa17c 452 if (ret != USB_STATUS_OK) {
mjoun 1:6e512faaa17c 453 TRACE("FAILED - ret %d", ret);
mjoun 1:6e512faaa17c 454 }
mjoun 1:6e512faaa17c 455 }
mjoun 1:6e512faaa17c 456
mjoun 1:6e512faaa17c 457 void USBHAL::EP0stall(void)
mjoun 1:6e512faaa17c 458 {
mjoun 1:6e512faaa17c 459 TRACE_FUNC_IN;
mjoun 1:6e512faaa17c 460 USBD_StallEp0();
mjoun 1:6e512faaa17c 461 }
mjoun 1:6e512faaa17c 462
mjoun 1:6e512faaa17c 463 static EP_STATUS internEndpointRead(uint8_t ep, uint32_t maxSize)
mjoun 1:6e512faaa17c 464 {
mjoun 1:6e512faaa17c 465 //TRACE_FUNC_IN_P("endpoint 0x%x, size %ld, cb %d", (unsigned)ep, maxSize, useCallback);
mjoun 1:6e512faaa17c 466
mjoun 1:6e512faaa17c 467 if (ep >= NUMBER_OF_ENDPOINTS) {
mjoun 1:6e512faaa17c 468 EFM_ASSERT(false);
mjoun 1:6e512faaa17c 469 return EP_INVALID;
mjoun 1:6e512faaa17c 470 }
mjoun 1:6e512faaa17c 471
mjoun 1:6e512faaa17c 472 ep_state[ep].status = READ_PENDING;
mjoun 1:6e512faaa17c 473
mjoun 1:6e512faaa17c 474 int ret = USBD_Read(USB_EP_TO_ADDR(ep), ep_state[ep].data_buf, maxSize,
mjoun 1:6e512faaa17c 475 ep_state[ep].intern_cb);
mjoun 1:6e512faaa17c 476
mjoun 1:6e512faaa17c 477 if (ret == USB_STATUS_OK) {
mjoun 1:6e512faaa17c 478 return EP_PENDING;
mjoun 1:6e512faaa17c 479 } else {
mjoun 1:6e512faaa17c 480 TRACE("FAILED - ret %d", ret);
mjoun 1:6e512faaa17c 481
mjoun 1:6e512faaa17c 482 if (ret == USB_STATUS_EP_STALLED) {
mjoun 1:6e512faaa17c 483 return EP_STALLED;
mjoun 1:6e512faaa17c 484 } else {
mjoun 1:6e512faaa17c 485 return EP_INVALID;
mjoun 1:6e512faaa17c 486 }
mjoun 1:6e512faaa17c 487 }
mjoun 1:6e512faaa17c 488 }
mjoun 1:6e512faaa17c 489
mjoun 1:6e512faaa17c 490 EP_STATUS USBHAL::endpointRead(uint8_t endpoint, uint32_t maximumSize)
mjoun 1:6e512faaa17c 491 {
mjoun 1:6e512faaa17c 492 return internEndpointRead(endpoint, maximumSize);
mjoun 1:6e512faaa17c 493 }
mjoun 1:6e512faaa17c 494
mjoun 1:6e512faaa17c 495 EP_STATUS USBHAL::endpointReadResult(uint8_t endpoint, uint8_t *data, uint32_t *bytesRead)
mjoun 1:6e512faaa17c 496 {
mjoun 1:6e512faaa17c 497 TRACE_FUNC_IN;
mjoun 1:6e512faaa17c 498
mjoun 1:6e512faaa17c 499 if (endpoint >= NUMBER_OF_ENDPOINTS) {
mjoun 1:6e512faaa17c 500 EFM_ASSERT(false);
mjoun 1:6e512faaa17c 501 return EP_INVALID;
mjoun 1:6e512faaa17c 502 }
mjoun 1:6e512faaa17c 503
mjoun 1:6e512faaa17c 504 EFM_ASSERT(data);
mjoun 1:6e512faaa17c 505 EFM_ASSERT(bytesRead);
mjoun 1:6e512faaa17c 506 if (!data || !bytesRead) {
mjoun 1:6e512faaa17c 507 return EP_INVALID;
mjoun 1:6e512faaa17c 508 }
mjoun 1:6e512faaa17c 509
mjoun 1:6e512faaa17c 510 switch (ep_state[endpoint].status) {
mjoun 1:6e512faaa17c 511 case READ_PENDING:
mjoun 1:6e512faaa17c 512 return EP_PENDING;
mjoun 1:6e512faaa17c 513
mjoun 1:6e512faaa17c 514 case READ_COMPLETE:
mjoun 1:6e512faaa17c 515 memcpy(data, ep_state[endpoint].data_buf, ep_state[endpoint].byte_count);
mjoun 1:6e512faaa17c 516 *bytesRead = ep_state[endpoint].byte_count;
mjoun 1:6e512faaa17c 517 ep_state[endpoint].status = IDLE;
mjoun 1:6e512faaa17c 518 return EP_COMPLETED;
mjoun 1:6e512faaa17c 519
mjoun 1:6e512faaa17c 520 case FAILED_STALLED:
mjoun 1:6e512faaa17c 521 ep_state[endpoint].status = IDLE;
mjoun 1:6e512faaa17c 522 return EP_STALLED;
mjoun 1:6e512faaa17c 523
mjoun 1:6e512faaa17c 524 default:
mjoun 1:6e512faaa17c 525 ep_state[endpoint].status = IDLE;
mjoun 1:6e512faaa17c 526 return EP_INVALID;
mjoun 1:6e512faaa17c 527 }
mjoun 1:6e512faaa17c 528 }
mjoun 1:6e512faaa17c 529
mjoun 1:6e512faaa17c 530 EP_STATUS USBHAL::endpointWrite(uint8_t endpoint, uint8_t *data, uint32_t size)
mjoun 1:6e512faaa17c 531 {
mjoun 1:6e512faaa17c 532 TRACE_FUNC_IN_P("endpoint 0x%x, data 0x%lx, size %lu", (unsigned )endpoint, (uint32_t)data, size);
mjoun 1:6e512faaa17c 533
mjoun 1:6e512faaa17c 534 EFM_ASSERT(endpoint < NUMBER_OF_ENDPOINTS);
mjoun 1:6e512faaa17c 535 EFM_ASSERT(endpoint > EP0IN);
mjoun 1:6e512faaa17c 536 EFM_ASSERT(size <= ep_state[endpoint].max_packet);
mjoun 1:6e512faaa17c 537 EFM_ASSERT(data);
mjoun 1:6e512faaa17c 538
mjoun 1:6e512faaa17c 539 uint8_t ep = USB_EP_TO_INDEX(endpoint);
mjoun 1:6e512faaa17c 540
mjoun 1:6e512faaa17c 541 if (endpoint >= NUMBER_OF_ENDPOINTS || endpoint <= EP0IN) {
mjoun 1:6e512faaa17c 542 return EP_INVALID;
mjoun 1:6e512faaa17c 543 }
mjoun 1:6e512faaa17c 544
mjoun 1:6e512faaa17c 545 if (size > ep_state[endpoint].max_packet) {
mjoun 1:6e512faaa17c 546 return EP_INVALID;
mjoun 1:6e512faaa17c 547 }
mjoun 1:6e512faaa17c 548
mjoun 1:6e512faaa17c 549 if (!data) {
mjoun 1:6e512faaa17c 550 return EP_INVALID;
mjoun 1:6e512faaa17c 551 }
mjoun 1:6e512faaa17c 552
mjoun 1:6e512faaa17c 553 memcpy(ep_state[ep].data_buf, data, size);
mjoun 1:6e512faaa17c 554
mjoun 1:6e512faaa17c 555 ep_state[ep].status = WRITE_PENDING;
mjoun 1:6e512faaa17c 556 int ret = USBD_Write(USB_EP_TO_ADDR(endpoint), ep_state[ep].data_buf, size, ep_state[ep].intern_cb);
mjoun 1:6e512faaa17c 557
mjoun 1:6e512faaa17c 558 if (ret == USB_STATUS_EP_STALLED) {
mjoun 1:6e512faaa17c 559 ep_state[ep].status = IDLE;
mjoun 1:6e512faaa17c 560 return EP_STALLED;
mjoun 1:6e512faaa17c 561 } else if (ret != USB_STATUS_OK) {
mjoun 1:6e512faaa17c 562 ep_state[ep].status = IDLE;
mjoun 1:6e512faaa17c 563 return EP_INVALID;
mjoun 1:6e512faaa17c 564 }
mjoun 1:6e512faaa17c 565
mjoun 1:6e512faaa17c 566 return EP_PENDING;
mjoun 1:6e512faaa17c 567 }
mjoun 1:6e512faaa17c 568
mjoun 1:6e512faaa17c 569 EP_STATUS USBHAL::endpointWriteResult(uint8_t endpoint)
mjoun 1:6e512faaa17c 570 {
mjoun 1:6e512faaa17c 571 if (endpoint >= NUMBER_OF_ENDPOINTS) {
mjoun 1:6e512faaa17c 572 EFM_ASSERT(false);
mjoun 1:6e512faaa17c 573 return EP_INVALID;
mjoun 1:6e512faaa17c 574 }
mjoun 1:6e512faaa17c 575
mjoun 1:6e512faaa17c 576 switch (ep_state[endpoint].status) {
mjoun 1:6e512faaa17c 577 case WRITE_PENDING:
mjoun 1:6e512faaa17c 578 return EP_PENDING;
mjoun 1:6e512faaa17c 579
mjoun 1:6e512faaa17c 580 case WRITE_COMPLETE:
mjoun 1:6e512faaa17c 581 ep_state[endpoint].status = IDLE;
mjoun 1:6e512faaa17c 582 return EP_COMPLETED;
mjoun 1:6e512faaa17c 583
mjoun 1:6e512faaa17c 584 case FAILED_STALLED:
mjoun 1:6e512faaa17c 585 ep_state[endpoint].status = IDLE;
mjoun 1:6e512faaa17c 586 return EP_STALLED;
mjoun 1:6e512faaa17c 587
mjoun 1:6e512faaa17c 588 default:
mjoun 1:6e512faaa17c 589 ep_state[endpoint].status = IDLE;
mjoun 1:6e512faaa17c 590 return EP_INVALID;
mjoun 1:6e512faaa17c 591 }
mjoun 1:6e512faaa17c 592 }
mjoun 1:6e512faaa17c 593
mjoun 1:6e512faaa17c 594 void USBHAL::stallEndpoint(uint8_t endpoint)
mjoun 1:6e512faaa17c 595 {
mjoun 1:6e512faaa17c 596 TRACE_FUNC_IN;
mjoun 1:6e512faaa17c 597
mjoun 1:6e512faaa17c 598 EFM_ASSERT(endpoint < NUMBER_OF_ENDPOINTS);
mjoun 1:6e512faaa17c 599 EFM_ASSERT((endpoint != EP0OUT) && (endpoint != EP0IN));
mjoun 1:6e512faaa17c 600
mjoun 1:6e512faaa17c 601 USBD_StallEp(USB_EP_TO_ADDR(endpoint));
mjoun 1:6e512faaa17c 602 }
mjoun 1:6e512faaa17c 603
mjoun 1:6e512faaa17c 604 void USBHAL::unstallEndpoint(uint8_t endpoint)
mjoun 1:6e512faaa17c 605 {
mjoun 1:6e512faaa17c 606 TRACE_FUNC_IN;
mjoun 1:6e512faaa17c 607
mjoun 1:6e512faaa17c 608 EFM_ASSERT(endpoint < NUMBER_OF_ENDPOINTS);
mjoun 1:6e512faaa17c 609 EFM_ASSERT((endpoint != EP0OUT) && (endpoint != EP0IN));
mjoun 1:6e512faaa17c 610
mjoun 1:6e512faaa17c 611 USBD_UnStallEp(USB_EP_TO_ADDR(endpoint));
mjoun 1:6e512faaa17c 612 }
mjoun 1:6e512faaa17c 613
mjoun 1:6e512faaa17c 614 bool USBHAL::realiseEndpoint(uint8_t endpoint, uint32_t maxPacket, uint32_t options)
mjoun 1:6e512faaa17c 615 {
mjoun 1:6e512faaa17c 616 TRACE_FUNC_IN_P("endpoint %d, packetsize %ld, options 0x%lx", endpoint,
mjoun 1:6e512faaa17c 617 maxPacket, options);
mjoun 1:6e512faaa17c 618
mjoun 1:6e512faaa17c 619 int mult = 1; // RX/TX buffer size multiplier
mjoun 1:6e512faaa17c 620 int type = USB_EPTYPE_INTR;
mjoun 1:6e512faaa17c 621
mjoun 1:6e512faaa17c 622 if (endpoint >= NUMBER_OF_ENDPOINTS) {
mjoun 1:6e512faaa17c 623 EFM_ASSERT(false);
mjoun 1:6e512faaa17c 624 return false;
mjoun 1:6e512faaa17c 625 }
mjoun 1:6e512faaa17c 626
mjoun 1:6e512faaa17c 627 if (endpoint == EP0IN || endpoint == EP0OUT) {
mjoun 1:6e512faaa17c 628 EFM_ASSERT(false);
mjoun 1:6e512faaa17c 629 return false;
mjoun 1:6e512faaa17c 630 }
mjoun 1:6e512faaa17c 631
mjoun 1:6e512faaa17c 632 ep_state[endpoint].max_packet = 0;
mjoun 1:6e512faaa17c 633
mjoun 1:6e512faaa17c 634 if (endpoint == EPISO_OUT || endpoint == EPISO_IN) {
mjoun 1:6e512faaa17c 635 if (maxPacket > MAX_PACKET_SIZE_EPISO) {
mjoun 1:6e512faaa17c 636 EFM_ASSERT(false);
mjoun 1:6e512faaa17c 637 return false;
mjoun 1:6e512faaa17c 638 }
mjoun 1:6e512faaa17c 639 } else if ((maxPacket > MAX_PACKET_SIZE_EPBULK) || (maxPacket > MAX_PACKET_SIZE_EPINT)) {
mjoun 1:6e512faaa17c 640 EFM_ASSERT(false);
mjoun 1:6e512faaa17c 641 return false;
mjoun 1:6e512faaa17c 642 }
mjoun 1:6e512faaa17c 643
mjoun 1:6e512faaa17c 644 // USBDevice performs a read right after creating the endpoints,
mjoun 1:6e512faaa17c 645 // before calling configureDevice. The read will fail since
mjoun 1:6e512faaa17c 646 // at that point the device state is still ADDRESSED. Workaround
mjoun 1:6e512faaa17c 647 // is to force configured state here.
mjoun 1:6e512faaa17c 648 //
mjoun 1:6e512faaa17c 649 // This relies on USBDevice to not call realiseEndpoint unless
mjoun 1:6e512faaa17c 650 // it is transitioning to the CONFIGURED state.
mjoun 1:6e512faaa17c 651 USBD_SetUsbState(USBD_STATE_CONFIGURED);
mjoun 1:6e512faaa17c 652
mjoun 1:6e512faaa17c 653 // Why doesn't this function have a type param? This is silly...
mjoun 1:6e512faaa17c 654 switch (endpoint) {
mjoun 1:6e512faaa17c 655 case EPBULK_OUT:
mjoun 1:6e512faaa17c 656 case EPBULK_IN:
mjoun 1:6e512faaa17c 657 type = USB_EPTYPE_BULK;
mjoun 1:6e512faaa17c 658 mult = 2;
mjoun 1:6e512faaa17c 659 break;
mjoun 1:6e512faaa17c 660 case EPINT_OUT:
mjoun 1:6e512faaa17c 661 case EPINT_IN:
mjoun 1:6e512faaa17c 662 type = USB_EPTYPE_INTR;
mjoun 1:6e512faaa17c 663 mult = 1;
mjoun 1:6e512faaa17c 664 break;
mjoun 1:6e512faaa17c 665 case EPISO_OUT:
mjoun 1:6e512faaa17c 666 case EPISO_IN:
mjoun 1:6e512faaa17c 667 type = USB_EPTYPE_ISOC;
mjoun 1:6e512faaa17c 668 mult = 2; // ?
mjoun 1:6e512faaa17c 669 break;
mjoun 1:6e512faaa17c 670 }
mjoun 1:6e512faaa17c 671
mjoun 1:6e512faaa17c 672 // Some options force the endpoint to a specific type
mjoun 1:6e512faaa17c 673 if( options & ISOCHRONOUS ) {
mjoun 1:6e512faaa17c 674 type = USB_EPTYPE_ISOC;
mjoun 1:6e512faaa17c 675 mult = 2; // ?
mjoun 1:6e512faaa17c 676 } else if ( options & RATE_FEEDBACK_MODE ) {
mjoun 1:6e512faaa17c 677 // No support for whatever rate feedback is, but for interrupt only
mjoun 1:6e512faaa17c 678 type = USB_EPTYPE_INTR;
mjoun 1:6e512faaa17c 679 mult = 1;
mjoun 1:6e512faaa17c 680 }
mjoun 1:6e512faaa17c 681
mjoun 1:6e512faaa17c 682 #ifdef USB_USE_DYNAMIC_MEMORY
mjoun 1:6e512faaa17c 683 if (ep_state[endpoint].data_buf) {
mjoun 1:6e512faaa17c 684 free(ep_state[endpoint].data_buf);
mjoun 1:6e512faaa17c 685 }
mjoun 1:6e512faaa17c 686
mjoun 1:6e512faaa17c 687 ep_state[endpoint].data_buf = (uint8_t *)malloc(maxPacket);
mjoun 1:6e512faaa17c 688
mjoun 1:6e512faaa17c 689 if (!ep_state[endpoint].data_buf) {
mjoun 1:6e512faaa17c 690 EFM_ASSERT(false);
mjoun 1:6e512faaa17c 691 return false;
mjoun 1:6e512faaa17c 692 }
mjoun 1:6e512faaa17c 693 #endif
mjoun 1:6e512faaa17c 694
mjoun 1:6e512faaa17c 695 int ret = USBD_AddEndpoint(USB_EP_TO_ADDR(endpoint), type, maxPacket, mult);
mjoun 1:6e512faaa17c 696
mjoun 1:6e512faaa17c 697 if (ret == USB_STATUS_OK) {
mjoun 1:6e512faaa17c 698 ep_state[endpoint].status = IDLE;
mjoun 1:6e512faaa17c 699 ep_state[endpoint].max_packet = maxPacket;
mjoun 1:6e512faaa17c 700 return true;
mjoun 1:6e512faaa17c 701 } else {
mjoun 1:6e512faaa17c 702 return false;
mjoun 1:6e512faaa17c 703 }
mjoun 1:6e512faaa17c 704 }
mjoun 1:6e512faaa17c 705
mjoun 1:6e512faaa17c 706 bool USBHAL::getEndpointStallState(unsigned char endpoint)
mjoun 1:6e512faaa17c 707 {
mjoun 1:6e512faaa17c 708 TRACE_FUNC_IN;
mjoun 1:6e512faaa17c 709 if (endpoint >= NUMBER_OF_ENDPOINTS) {
mjoun 1:6e512faaa17c 710 EFM_ASSERT(false);
mjoun 1:6e512faaa17c 711 return false;
mjoun 1:6e512faaa17c 712 }
mjoun 1:6e512faaa17c 713 return USBD_EpIsStalled(USB_EP_TO_ADDR(endpoint));
mjoun 1:6e512faaa17c 714 }
mjoun 1:6e512faaa17c 715
mjoun 1:6e512faaa17c 716 static void run_cmd(USBISRCommand cmd, uint32_t param)
mjoun 1:6e512faaa17c 717 {
mjoun 1:6e512faaa17c 718 if (usb_isrcmd != CMD_HANDLED || cmd >= CMD_ENUM_END_MARKER) {
mjoun 1:6e512faaa17c 719 EFM_ASSERT(false);
mjoun 1:6e512faaa17c 720 abort();
mjoun 1:6e512faaa17c 721 }
mjoun 1:6e512faaa17c 722
mjoun 1:6e512faaa17c 723 usb_isrcmd = cmd;
mjoun 1:6e512faaa17c 724 usb_isrcmd_param = param;
mjoun 1:6e512faaa17c 725 isrptr();
mjoun 1:6e512faaa17c 726 }
mjoun 1:6e512faaa17c 727
mjoun 1:6e512faaa17c 728 void USBHAL::_usbisr(void)
mjoun 1:6e512faaa17c 729 {
mjoun 1:6e512faaa17c 730 EFM_ASSERT(instance);
mjoun 1:6e512faaa17c 731 instance->usbisr();
mjoun 1:6e512faaa17c 732 }
mjoun 1:6e512faaa17c 733
mjoun 1:6e512faaa17c 734 void USBHAL::usbisr(void)
mjoun 1:6e512faaa17c 735 {
mjoun 1:6e512faaa17c 736 //TRACE_FUNC_IN;
mjoun 1:6e512faaa17c 737
mjoun 1:6e512faaa17c 738 // This "ISR" is used just to route callbacks from SiL USB driver
mjoun 1:6e512faaa17c 739 // callback context (which can not call protected/private USBHAL
mjoun 1:6e512faaa17c 740 // methods), to the actual USBHAL.
mjoun 1:6e512faaa17c 741
mjoun 1:6e512faaa17c 742 EFM_ASSERT(usb_isrcmd != CMD_HANDLED);
mjoun 1:6e512faaa17c 743 switch (usb_isrcmd) {
mjoun 1:6e512faaa17c 744 case CMD_EP0SETUP:
mjoun 1:6e512faaa17c 745 this->EP0setupCallback();
mjoun 1:6e512faaa17c 746 break;
mjoun 1:6e512faaa17c 747 case CMD_EP0IN:
mjoun 1:6e512faaa17c 748 this->EP0in();
mjoun 1:6e512faaa17c 749 break;
mjoun 1:6e512faaa17c 750 case CMD_EP0OUT:
mjoun 1:6e512faaa17c 751 this->EP0out();
mjoun 1:6e512faaa17c 752 break;
mjoun 1:6e512faaa17c 753 case CMD_BUSRESET:
mjoun 1:6e512faaa17c 754 this->busReset();
mjoun 1:6e512faaa17c 755 break;
mjoun 1:6e512faaa17c 756 case CMD_EP_XFER_COMPLETED:
mjoun 1:6e512faaa17c 757 if (epCallback[usb_isrcmd_param] && instance) {
mjoun 1:6e512faaa17c 758 (instance->*(epCallback[usb_isrcmd_param]))();
mjoun 1:6e512faaa17c 759 }
mjoun 1:6e512faaa17c 760 break;
mjoun 1:6e512faaa17c 761 case CMD_SOF:
mjoun 1:6e512faaa17c 762 this->SOF(usb_isrcmd_param);
mjoun 1:6e512faaa17c 763 break;
mjoun 1:6e512faaa17c 764 case CMD_SUSPEND_STATE_CHANGED:
mjoun 1:6e512faaa17c 765 this->suspendStateChanged(usb_isrcmd_param);
mjoun 1:6e512faaa17c 766 break;
mjoun 1:6e512faaa17c 767 default:
mjoun 1:6e512faaa17c 768 EFM_ASSERT(false);
mjoun 1:6e512faaa17c 769 break;
mjoun 1:6e512faaa17c 770 }
mjoun 1:6e512faaa17c 771 usb_isrcmd = CMD_HANDLED;
mjoun 1:6e512faaa17c 772 }
mjoun 1:6e512faaa17c 773 #endif
mjoun 1:6e512faaa17c 774
mjoun 1:6e512faaa17c 775 // End of file