Il y avait des problèmes dans la libraire...

Fork of USBDEVICE by ST

Committer:
qroche
Date:
Sun Sep 03 23:19:21 2017 +0000
Branch:
master
Revision:
5:3329e56e51d7
Parent:
1:2a3ae13b45ef
fin;

Who changed what in which revision?

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