Usb read

Dependencies:   FATFileSystem

Fork of F401RE-USBHost by Norimasa Okamoto

Committer:
va009039
Date:
Mon Jun 23 20:30:04 2014 +0900
Revision:
16:981c3104f6c0
Parent:
14:b167f2b97cb7
Child:
18:61554f238584
add FRDM-K64F.(not tested)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 11:61843badd06e 1 // Simple USBHost for Nucleo F401RE
va009039 11:61843badd06e 2 #if defined(TARGET_NUCLEO_F401RE)
va009039 11:61843badd06e 3 #include "USBHALHost_F401RE.h"
va009039 16:981c3104f6c0 4 #include <algorithm>
va009039 11:61843badd06e 5
va009039 11:61843badd06e 6 template <bool>struct CtAssert;
va009039 11:61843badd06e 7 template <>struct CtAssert<true> {};
va009039 11:61843badd06e 8 #define CTASSERT(A) CtAssert<A>();
va009039 11:61843badd06e 9
va009039 11:61843badd06e 10
va009039 11:61843badd06e 11 #ifdef _USB_DBG
va009039 11:61843badd06e 12 extern RawSerial pc;
va009039 16:981c3104f6c0 13 //RawSerial pc(USBTX,USBRX);
va009039 11:61843badd06e 14 #include "mydebug.h"
va009039 11:61843badd06e 15 #define USB_DBG(...) do{pc.printf("[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);pc.printf(__VA_ARGS__);pc.puts("\n");} while(0);
va009039 11:61843badd06e 16 #define USB_DBG_HEX(A,B) debug_hex<RawSerial>(pc,A,B)
va009039 11:61843badd06e 17
va009039 11:61843badd06e 18 #else
va009039 11:61843badd06e 19 #define USB_DBG(...) while(0)
va009039 11:61843badd06e 20 #define USB_DBG_HEX(A,B) while(0)
va009039 11:61843badd06e 21 #endif
va009039 11:61843badd06e 22
va009039 11:61843badd06e 23 #ifdef _USB_TEST
va009039 11:61843badd06e 24 #define USB_TEST_ASSERT(A) while(!(A)){fprintf(stderr,"\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);};
va009039 11:61843badd06e 25 #define USB_TEST_ASSERT_FALSE(A) USB_TEST_ASSERT(!(A))
va009039 11:61843badd06e 26 #else
va009039 11:61843badd06e 27 #define USB_TEST_ASSERT(A) while(0)
va009039 11:61843badd06e 28 #define USB_TEST_ASSERT_FALSE(A) while(0)
va009039 11:61843badd06e 29 #endif
va009039 11:61843badd06e 30
va009039 11:61843badd06e 31 #ifdef _USB_TRACE
va009039 11:61843badd06e 32 #define USB_TRACE() while(0)
va009039 11:61843badd06e 33 #define USB_TRACE1(A) while(0)
va009039 11:61843badd06e 34 #define USB_TRACE_VIEW() while(0)
va009039 11:61843badd06e 35 #define USB_TRACE_CLEAR() while(0)
va009039 11:61843badd06e 36 #else
va009039 11:61843badd06e 37 #define USB_TRACE() while(0)
va009039 11:61843badd06e 38 #define USB_TRACE1(A) while(0)
va009039 11:61843badd06e 39 #define USB_TRACE_VIEW() while(0)
va009039 11:61843badd06e 40 #define USB_TRACE_CLEAR() while(0)
va009039 11:61843badd06e 41 #endif
va009039 11:61843badd06e 42
va009039 11:61843badd06e 43 #define USB_INFO(...) do{fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\n");}while(0);
va009039 11:61843badd06e 44
va009039 11:61843badd06e 45 __IO bool attach_done = false;
va009039 11:61843badd06e 46
va009039 11:61843badd06e 47 void delay_ms(uint32_t t)
va009039 11:61843badd06e 48 {
va009039 11:61843badd06e 49 HAL_Delay(t);
va009039 11:61843badd06e 50 }
va009039 11:61843badd06e 51
va009039 16:981c3104f6c0 52 // usbh_conf.c
va009039 11:61843badd06e 53 extern HCD_HandleTypeDef hhcd_USB_OTG_FS;
va009039 11:61843badd06e 54
va009039 11:61843badd06e 55 void HAL_HCD_MspInit(HCD_HandleTypeDef* hhcd)
va009039 11:61843badd06e 56 {
va009039 11:61843badd06e 57 GPIO_InitTypeDef GPIO_InitStruct;
va009039 11:61843badd06e 58 if(hhcd->Instance==USB_OTG_FS)
va009039 11:61843badd06e 59 {
va009039 11:61843badd06e 60 /* Peripheral clock enable */
va009039 11:61843badd06e 61 __USB_OTG_FS_CLK_ENABLE();
va009039 11:61843badd06e 62
va009039 11:61843badd06e 63 /**USB_OTG_FS GPIO Configuration
va009039 11:61843badd06e 64 PA11 ------> USB_OTG_FS_DM
va009039 11:61843badd06e 65 PA12 ------> USB_OTG_FS_DP
va009039 11:61843badd06e 66 */
va009039 11:61843badd06e 67 GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
va009039 11:61843badd06e 68 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
va009039 11:61843badd06e 69 GPIO_InitStruct.Pull = GPIO_NOPULL;
va009039 11:61843badd06e 70 GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
va009039 11:61843badd06e 71 GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
va009039 11:61843badd06e 72 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
va009039 11:61843badd06e 73
va009039 11:61843badd06e 74 /* Peripheral interrupt init*/
va009039 11:61843badd06e 75 /* Sets the priority grouping field */
va009039 11:61843badd06e 76 HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0);
va009039 11:61843badd06e 77 HAL_NVIC_SetPriority(OTG_FS_IRQn, 0, 0);
va009039 11:61843badd06e 78 HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
va009039 11:61843badd06e 79 }
va009039 11:61843badd06e 80 }
va009039 11:61843badd06e 81
va009039 16:981c3104f6c0 82 // stm32f4xx_it.c
va009039 11:61843badd06e 83 extern "C" {
va009039 11:61843badd06e 84 void HAL_HCD_Connect_Callback(HCD_HandleTypeDef *hhcd)
va009039 11:61843badd06e 85 {
va009039 11:61843badd06e 86 USB_TRACE();
va009039 11:61843badd06e 87 attach_done = true;
va009039 11:61843badd06e 88 }
va009039 11:61843badd06e 89
va009039 11:61843badd06e 90 } // extern "C"
va009039 11:61843badd06e 91
va009039 11:61843badd06e 92 USBHALHost* USBHALHost::instHost;
va009039 11:61843badd06e 93
va009039 11:61843badd06e 94 USBHALHost::USBHALHost() {
va009039 11:61843badd06e 95 instHost = this;
va009039 11:61843badd06e 96 }
va009039 11:61843badd06e 97
va009039 11:61843badd06e 98 void USBHALHost::init() {
va009039 11:61843badd06e 99 hhcd_USB_OTG_FS.Instance = USB_OTG_FS;
va009039 11:61843badd06e 100 hhcd_USB_OTG_FS.Init.Host_channels = 8;
va009039 11:61843badd06e 101 hhcd_USB_OTG_FS.Init.speed = HCD_SPEED_FULL;
va009039 11:61843badd06e 102 hhcd_USB_OTG_FS.Init.dma_enable = DISABLE;
va009039 11:61843badd06e 103 hhcd_USB_OTG_FS.Init.phy_itface = HCD_PHY_EMBEDDED;
va009039 11:61843badd06e 104 hhcd_USB_OTG_FS.Init.Sof_enable = DISABLE;
va009039 11:61843badd06e 105 hhcd_USB_OTG_FS.Init.low_power_enable = ENABLE;
va009039 11:61843badd06e 106 hhcd_USB_OTG_FS.Init.vbus_sensing_enable = DISABLE;
va009039 11:61843badd06e 107 hhcd_USB_OTG_FS.Init.use_external_vbus = DISABLE;
va009039 11:61843badd06e 108
va009039 11:61843badd06e 109 HAL_HCD_Init(&hhcd_USB_OTG_FS);
va009039 11:61843badd06e 110 HAL_HCD_Start(&hhcd_USB_OTG_FS);
va009039 11:61843badd06e 111
va009039 11:61843badd06e 112 bool lowSpeed = wait_attach();
va009039 11:61843badd06e 113 delay_ms(200);
va009039 11:61843badd06e 114 HAL_HCD_ResetPort(&hhcd_USB_OTG_FS);
va009039 11:61843badd06e 115 delay_ms(100); // Wait for 100 ms after Reset
va009039 11:61843badd06e 116 addDevice(NULL, 0, lowSpeed);
va009039 11:61843badd06e 117 }
va009039 11:61843badd06e 118
va009039 11:61843badd06e 119 bool USBHALHost::wait_attach() {
va009039 11:61843badd06e 120 Timer t;
va009039 11:61843badd06e 121 t.reset();
va009039 11:61843badd06e 122 t.start();
va009039 11:61843badd06e 123 while(!attach_done) {
va009039 11:61843badd06e 124 if (t.read_ms() > 5*1000) {
va009039 11:61843badd06e 125 t.reset();
va009039 11:61843badd06e 126 USB_INFO("Please attach USB device.");
va009039 11:61843badd06e 127 }
va009039 11:61843badd06e 128 }
va009039 11:61843badd06e 129 wait_ms(100);
va009039 11:61843badd06e 130 return HAL_HCD_GetCurrentSpeed(&hhcd_USB_OTG_FS) == USB_OTG_SPEED_LOW;
va009039 11:61843badd06e 131 }
va009039 11:61843badd06e 132
va009039 11:61843badd06e 133 int USBHALHost::token_setup(USBEndpoint* ep, SETUP_PACKET* setup, uint16_t wLength) {
va009039 16:981c3104f6c0 134 const uint8_t ep_addr = 0x00;
va009039 16:981c3104f6c0 135 HC hc;
va009039 11:61843badd06e 136 USBDeviceConnected* dev = ep->getDevice();
va009039 16:981c3104f6c0 137 hc.Init(ep_addr,
va009039 11:61843badd06e 138 dev->getAddress(),
va009039 11:61843badd06e 139 dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_FULL,
va009039 11:61843badd06e 140 EP_TYPE_CTRL, ep->getSize());
va009039 11:61843badd06e 141
va009039 11:61843badd06e 142 setup->wLength = wLength;
va009039 16:981c3104f6c0 143 hc.SubmitRequest((uint8_t*)setup, 8, true); // PID_SETUP
va009039 16:981c3104f6c0 144 while(hc.GetURBState() == URB_IDLE);
va009039 11:61843badd06e 145
va009039 16:981c3104f6c0 146 switch(hc.GetURBState()) {
va009039 11:61843badd06e 147 case URB_DONE:
va009039 11:61843badd06e 148 LastStatus = ACK;
va009039 11:61843badd06e 149 break;
va009039 11:61843badd06e 150 default:
va009039 11:61843badd06e 151 LastStatus = 0xff;
va009039 11:61843badd06e 152 break;
va009039 11:61843badd06e 153 }
va009039 11:61843badd06e 154 ep->setData01(DATA1);
va009039 16:981c3104f6c0 155 return 8;
va009039 11:61843badd06e 156 }
va009039 11:61843badd06e 157
va009039 11:61843badd06e 158 int USBHALHost::token_in(USBEndpoint* ep, uint8_t* data, int size, int retryLimit) {
va009039 11:61843badd06e 159 switch(ep->getType()) {
va009039 11:61843badd06e 160 case CONTROL_ENDPOINT:
va009039 11:61843badd06e 161 return token_ctl_in(ep, data, size, retryLimit);
va009039 11:61843badd06e 162 case INTERRUPT_ENDPOINT:
va009039 11:61843badd06e 163 return token_int_in(ep, data, size);
va009039 11:61843badd06e 164 case BULK_ENDPOINT:
va009039 11:61843badd06e 165 return token_blk_in(ep, data, size, retryLimit);
va009039 11:61843badd06e 166 }
va009039 11:61843badd06e 167 return -1;
va009039 11:61843badd06e 168 }
va009039 11:61843badd06e 169
va009039 11:61843badd06e 170 int USBHALHost::token_ctl_in(USBEndpoint* ep, uint8_t* data, int size, int retryLimit) {
va009039 16:981c3104f6c0 171 const uint8_t ep_addr = 0x80;
va009039 16:981c3104f6c0 172 HC hc;
va009039 11:61843badd06e 173 USBDeviceConnected* dev = ep->getDevice();
va009039 16:981c3104f6c0 174 hc.Init(ep_addr,
va009039 11:61843badd06e 175 dev->getAddress(),
va009039 11:61843badd06e 176 dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_FULL,
va009039 11:61843badd06e 177 EP_TYPE_CTRL, ep->getSize());
va009039 16:981c3104f6c0 178
va009039 16:981c3104f6c0 179 hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1);
va009039 11:61843badd06e 180
va009039 16:981c3104f6c0 181 hc.SubmitRequest(data, size);
va009039 16:981c3104f6c0 182 while(hc.GetURBState() == URB_IDLE);
va009039 11:61843badd06e 183
va009039 16:981c3104f6c0 184 switch(hc.GetURBState()) {
va009039 11:61843badd06e 185 case URB_DONE:
va009039 11:61843badd06e 186 LastStatus = ACK;
va009039 11:61843badd06e 187 break;
va009039 11:61843badd06e 188 default:
va009039 11:61843badd06e 189 LastStatus = 0xff;
va009039 11:61843badd06e 190 return -1;
va009039 11:61843badd06e 191 }
va009039 11:61843badd06e 192 ep->toggleData01();
va009039 16:981c3104f6c0 193 return hc.GetXferCount();
va009039 11:61843badd06e 194 }
va009039 11:61843badd06e 195
va009039 11:61843badd06e 196 int USBHALHost::token_int_in(USBEndpoint* ep, uint8_t* data, int size) {
va009039 16:981c3104f6c0 197 HC hc;
va009039 11:61843badd06e 198 USBDeviceConnected* dev = ep->getDevice();
va009039 16:981c3104f6c0 199 hc.Init(
va009039 11:61843badd06e 200 ep->getAddress(),
va009039 11:61843badd06e 201 dev->getAddress(),
va009039 11:61843badd06e 202 dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_FULL,
va009039 11:61843badd06e 203 EP_TYPE_INTR, ep->getSize());
va009039 16:981c3104f6c0 204
va009039 16:981c3104f6c0 205 hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1);
va009039 11:61843badd06e 206
va009039 16:981c3104f6c0 207 hc.SubmitRequest(data, size);
va009039 16:981c3104f6c0 208 while(hc.GetURBState() == URB_IDLE);
va009039 16:981c3104f6c0 209 switch(hc.GetURBState()) {
va009039 11:61843badd06e 210 case URB_DONE:
va009039 16:981c3104f6c0 211 switch(hc.GetState()) {
va009039 11:61843badd06e 212 case HC_XFRC:
va009039 11:61843badd06e 213 LastStatus = ep->getData01();
va009039 11:61843badd06e 214 ep->toggleData01();
va009039 16:981c3104f6c0 215 return hc.GetXferCount();
va009039 11:61843badd06e 216 case HC_NAK:
va009039 11:61843badd06e 217 LastStatus = NAK;
va009039 11:61843badd06e 218 return -1;
va009039 11:61843badd06e 219 }
va009039 11:61843badd06e 220 break;
va009039 11:61843badd06e 221 }
va009039 11:61843badd06e 222 LastStatus = STALL;
va009039 11:61843badd06e 223 return -1;
va009039 11:61843badd06e 224 }
va009039 11:61843badd06e 225
va009039 11:61843badd06e 226 int USBHALHost::token_blk_in(USBEndpoint* ep, uint8_t* data, int size, int retryLimit) {
va009039 16:981c3104f6c0 227 HC hc;
va009039 11:61843badd06e 228 USBDeviceConnected* dev = ep->getDevice();
va009039 16:981c3104f6c0 229 hc.Init(
va009039 16:981c3104f6c0 230 ep->getAddress(),
va009039 16:981c3104f6c0 231 dev->getAddress(),
va009039 16:981c3104f6c0 232 HCD_SPEED_FULL,
va009039 16:981c3104f6c0 233 EP_TYPE_BULK, ep->getSize());
va009039 16:981c3104f6c0 234
va009039 16:981c3104f6c0 235 hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1);
va009039 16:981c3104f6c0 236
va009039 11:61843badd06e 237 int retry = 0;
va009039 11:61843badd06e 238 do {
va009039 16:981c3104f6c0 239 hc.SubmitRequest(data, size);
va009039 16:981c3104f6c0 240 while(hc.GetURBState() == URB_IDLE);
va009039 11:61843badd06e 241
va009039 16:981c3104f6c0 242 switch(hc.GetURBState()) {
va009039 11:61843badd06e 243 case URB_DONE:
va009039 16:981c3104f6c0 244 switch(hc.GetState()) {
va009039 11:61843badd06e 245 case HC_XFRC:
va009039 11:61843badd06e 246 LastStatus = ep->getData01();
va009039 11:61843badd06e 247 ep->toggleData01();
va009039 16:981c3104f6c0 248 return hc.GetXferCount();
va009039 11:61843badd06e 249 case HC_NAK:
va009039 11:61843badd06e 250 LastStatus = NAK;
va009039 11:61843badd06e 251 if (retryLimit > 0) {
va009039 11:61843badd06e 252 delay_ms(1 + 10 * retry);
va009039 11:61843badd06e 253 }
va009039 11:61843badd06e 254 break;
va009039 11:61843badd06e 255 default:
va009039 11:61843badd06e 256 break;
va009039 11:61843badd06e 257 }
va009039 11:61843badd06e 258 break;
va009039 11:61843badd06e 259 case URB_STALL:
va009039 11:61843badd06e 260 LastStatus = STALL;
va009039 11:61843badd06e 261 return -1;
va009039 11:61843badd06e 262 default:
va009039 11:61843badd06e 263 LastStatus = STALL;
va009039 11:61843badd06e 264 delay_ms(500 + 100 * retry);
va009039 11:61843badd06e 265 break;
va009039 11:61843badd06e 266 }
va009039 11:61843badd06e 267 }while(retry++ < retryLimit);
va009039 11:61843badd06e 268 return -1;
va009039 11:61843badd06e 269 }
va009039 11:61843badd06e 270
va009039 11:61843badd06e 271 int USBHALHost::token_out(USBEndpoint* ep, const uint8_t* data, int size, int retryLimit) {
va009039 11:61843badd06e 272 switch(ep->getType()) {
va009039 11:61843badd06e 273 case CONTROL_ENDPOINT:
va009039 11:61843badd06e 274 return token_ctl_out(ep, data, size, retryLimit);
va009039 11:61843badd06e 275 case INTERRUPT_ENDPOINT:
va009039 11:61843badd06e 276 return token_int_out(ep, data, size);
va009039 11:61843badd06e 277 case BULK_ENDPOINT:
va009039 11:61843badd06e 278 return token_blk_out(ep, data, size, retryLimit);
va009039 11:61843badd06e 279 }
va009039 11:61843badd06e 280 return -1;
va009039 11:61843badd06e 281 }
va009039 11:61843badd06e 282
va009039 11:61843badd06e 283 int USBHALHost::token_ctl_out(USBEndpoint* ep, const uint8_t* data, int size, int retryLimit) {
va009039 16:981c3104f6c0 284 const uint8_t ep_addr = 0x00;
va009039 16:981c3104f6c0 285 HC hc;
va009039 11:61843badd06e 286 USBDeviceConnected* dev = ep->getDevice();
va009039 16:981c3104f6c0 287 hc.Init(ep_addr,
va009039 16:981c3104f6c0 288 dev->getAddress(),
va009039 16:981c3104f6c0 289 dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_FULL,
va009039 16:981c3104f6c0 290 EP_TYPE_CTRL, ep->getSize());
va009039 16:981c3104f6c0 291
va009039 16:981c3104f6c0 292 hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1);
va009039 11:61843badd06e 293
va009039 11:61843badd06e 294 do {
va009039 16:981c3104f6c0 295 hc.SubmitRequest((uint8_t*)data, size);
va009039 16:981c3104f6c0 296 while(hc.GetURBState() == URB_IDLE);
va009039 11:61843badd06e 297
va009039 16:981c3104f6c0 298 switch(hc.GetURBState()) {
va009039 11:61843badd06e 299 case URB_DONE:
va009039 11:61843badd06e 300 LastStatus = ACK;
va009039 11:61843badd06e 301 ep->toggleData01();
va009039 14:b167f2b97cb7 302 return size;
va009039 11:61843badd06e 303
va009039 11:61843badd06e 304 default:
va009039 11:61843badd06e 305 break;
va009039 11:61843badd06e 306 }
va009039 11:61843badd06e 307 delay_ms(1);
va009039 11:61843badd06e 308 }while(retryLimit-- > 0);
va009039 11:61843badd06e 309 return -1;
va009039 11:61843badd06e 310 }
va009039 11:61843badd06e 311
va009039 11:61843badd06e 312 int USBHALHost::token_int_out(USBEndpoint* ep, const uint8_t* data, int size) {
va009039 16:981c3104f6c0 313 HC hc;
va009039 11:61843badd06e 314 USBDeviceConnected* dev = ep->getDevice();
va009039 16:981c3104f6c0 315 hc.Init(
va009039 11:61843badd06e 316 ep->getAddress(),
va009039 11:61843badd06e 317 dev->getAddress(),
va009039 11:61843badd06e 318 dev->getSpeed() ? HCD_SPEED_LOW : HCD_SPEED_FULL,
va009039 11:61843badd06e 319 EP_TYPE_INTR, ep->getSize());
va009039 16:981c3104f6c0 320
va009039 16:981c3104f6c0 321 hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1);
va009039 11:61843badd06e 322
va009039 16:981c3104f6c0 323 hc.SubmitRequest((uint8_t*)data, size);
va009039 16:981c3104f6c0 324 while(hc.GetURBState() == URB_IDLE);
va009039 16:981c3104f6c0 325 if (hc.GetURBState() != URB_DONE) {
va009039 11:61843badd06e 326 return -1;
va009039 11:61843badd06e 327 }
va009039 11:61843badd06e 328 ep->toggleData01();
va009039 14:b167f2b97cb7 329 return size;
va009039 11:61843badd06e 330 }
va009039 11:61843badd06e 331
va009039 11:61843badd06e 332 int USBHALHost::token_blk_out(USBEndpoint* ep, const uint8_t* data, int size, int retryLimit) {
va009039 16:981c3104f6c0 333 HC hc;
va009039 11:61843badd06e 334 USBDeviceConnected* dev = ep->getDevice();
va009039 16:981c3104f6c0 335 hc.Init(
va009039 16:981c3104f6c0 336 ep->getAddress(), dev->getAddress(),
va009039 16:981c3104f6c0 337 HCD_SPEED_FULL, EP_TYPE_BULK, ep->getSize());
va009039 16:981c3104f6c0 338
va009039 16:981c3104f6c0 339 hc.SetToggle((ep->getData01() == DATA0) ? 0 : 1);
va009039 16:981c3104f6c0 340
va009039 11:61843badd06e 341 int retry = 0;
va009039 11:61843badd06e 342 do {
va009039 16:981c3104f6c0 343 hc.SubmitRequest((uint8_t*)data, size);
va009039 16:981c3104f6c0 344 while(hc.GetURBState() == URB_IDLE);
va009039 11:61843badd06e 345
va009039 16:981c3104f6c0 346 switch(hc.GetURBState()) {
va009039 11:61843badd06e 347 case URB_DONE:
va009039 16:981c3104f6c0 348 switch(hc.GetState()) {
va009039 11:61843badd06e 349 case HC_XFRC: // ACK
va009039 11:61843badd06e 350 LastStatus = ep->getData01();
va009039 11:61843badd06e 351 ep->toggleData01();
va009039 14:b167f2b97cb7 352 return size;
va009039 11:61843badd06e 353 default:
va009039 11:61843badd06e 354 break;
va009039 11:61843badd06e 355 }
va009039 11:61843badd06e 356 break;
va009039 11:61843badd06e 357 case URB_NOTREADY: // HC_NAK
va009039 11:61843badd06e 358 LastStatus = NAK;
va009039 11:61843badd06e 359 delay_ms(100 * retry);
va009039 11:61843badd06e 360 break;
va009039 11:61843badd06e 361 default:
va009039 11:61843badd06e 362 LastStatus = STALL;
va009039 11:61843badd06e 363 return -1;
va009039 11:61843badd06e 364 }
va009039 11:61843badd06e 365 } while(retry++ < retryLimit);
va009039 11:61843badd06e 366 return -1;
va009039 11:61843badd06e 367 }
va009039 11:61843badd06e 368
va009039 11:61843badd06e 369 int USBHALHost::token_iso_in(USBEndpoint* ep, uint8_t* data, int size) {
va009039 16:981c3104f6c0 370 HC* hc = ep->getHALData<HC*>();
va009039 16:981c3104f6c0 371 if (hc == NULL) {
va009039 16:981c3104f6c0 372 hc = new HC;
va009039 16:981c3104f6c0 373 ep->setHALData<HC*>(hc);
va009039 13:8774c07f12a5 374 USBDeviceConnected* dev = ep->getDevice();
va009039 16:981c3104f6c0 375 hc->Init(
va009039 13:8774c07f12a5 376 ep->getAddress(), dev->getAddress(),
va009039 13:8774c07f12a5 377 HCD_SPEED_FULL, EP_TYPE_ISOC, ep->getSize());
va009039 13:8774c07f12a5 378 }
va009039 16:981c3104f6c0 379 hc->SubmitRequest(data, size);
va009039 16:981c3104f6c0 380 while(hc->GetURBState() == URB_IDLE);
va009039 16:981c3104f6c0 381 return hc->GetXferCount();
va009039 16:981c3104f6c0 382 }
va009039 16:981c3104f6c0 383
va009039 16:981c3104f6c0 384 int USBHALHost::multi_token_in(USBEndpoint* ep, uint8_t* data, size_t total, bool block) {
va009039 16:981c3104f6c0 385 if (total == 0) {
va009039 16:981c3104f6c0 386 return token_in(ep);
va009039 16:981c3104f6c0 387 }
va009039 16:981c3104f6c0 388 int retryLimit = block ? 10 : 0;
va009039 16:981c3104f6c0 389 int read_len = 0;
va009039 16:981c3104f6c0 390 for(int n = 0; read_len < total; n++) {
va009039 16:981c3104f6c0 391 int size = std::min((int)total-read_len, ep->getSize());
va009039 16:981c3104f6c0 392 int result = token_in(ep, data+read_len, size, retryLimit);
va009039 16:981c3104f6c0 393 if (result < 0) {
va009039 16:981c3104f6c0 394 if (block) {
va009039 16:981c3104f6c0 395 return -1;
va009039 16:981c3104f6c0 396 }
va009039 16:981c3104f6c0 397 if (LastStatus == NAK) {
va009039 16:981c3104f6c0 398 if (n == 0) {
va009039 16:981c3104f6c0 399 return -1;
va009039 16:981c3104f6c0 400 }
va009039 16:981c3104f6c0 401 break;
va009039 16:981c3104f6c0 402 }
va009039 16:981c3104f6c0 403 return result;
va009039 16:981c3104f6c0 404 }
va009039 16:981c3104f6c0 405 read_len += result;
va009039 16:981c3104f6c0 406 if (result < ep->getSize()) {
va009039 16:981c3104f6c0 407 break;
va009039 16:981c3104f6c0 408 }
va009039 16:981c3104f6c0 409 }
va009039 16:981c3104f6c0 410 return read_len;
va009039 16:981c3104f6c0 411 }
va009039 16:981c3104f6c0 412
va009039 16:981c3104f6c0 413 int USBHALHost::multi_token_out(USBEndpoint* ep, const uint8_t* data, size_t total, bool block) {
va009039 16:981c3104f6c0 414 if (total == 0) {
va009039 16:981c3104f6c0 415 return token_out(ep);
va009039 16:981c3104f6c0 416 }
va009039 16:981c3104f6c0 417 int write_len = 0;
va009039 16:981c3104f6c0 418 for(int n = 0; write_len < total; n++) {
va009039 16:981c3104f6c0 419 int size = std::min((int)total-write_len, ep->getSize());
va009039 16:981c3104f6c0 420 int result = token_out(ep, data+write_len, size);
va009039 16:981c3104f6c0 421 if (result < 0) {
va009039 16:981c3104f6c0 422 if (LastStatus == NAK) {
va009039 16:981c3104f6c0 423 if (n == 0) {
va009039 16:981c3104f6c0 424 return -1;
va009039 16:981c3104f6c0 425 }
va009039 16:981c3104f6c0 426 break;
va009039 16:981c3104f6c0 427 }
va009039 16:981c3104f6c0 428 USB_DBG("token_out result=%d %02x", result, LastStatus);
va009039 16:981c3104f6c0 429 return result;
va009039 16:981c3104f6c0 430 }
va009039 16:981c3104f6c0 431 write_len += result;
va009039 16:981c3104f6c0 432 if (result < ep->getSize()) {
va009039 16:981c3104f6c0 433 break;
va009039 16:981c3104f6c0 434 }
va009039 16:981c3104f6c0 435 }
va009039 16:981c3104f6c0 436 return write_len;
va009039 16:981c3104f6c0 437 }
va009039 16:981c3104f6c0 438
va009039 16:981c3104f6c0 439 uint8_t HC::slot = 0x00;
va009039 16:981c3104f6c0 440
va009039 16:981c3104f6c0 441 HC::HC() {
va009039 16:981c3104f6c0 442 uint8_t mask = 0x01;
va009039 16:981c3104f6c0 443 for(int i = 1; i < 8; i++, mask <<= 1) {
va009039 16:981c3104f6c0 444 if (!(slot & mask)) {
va009039 16:981c3104f6c0 445 slot |= mask;
va009039 16:981c3104f6c0 446 _ch = i;
va009039 16:981c3104f6c0 447 return;
va009039 16:981c3104f6c0 448 }
va009039 16:981c3104f6c0 449 }
va009039 16:981c3104f6c0 450 _ch = 0; // ERROR!!!
va009039 16:981c3104f6c0 451 }
va009039 16:981c3104f6c0 452
va009039 16:981c3104f6c0 453 HC::HC(int ch) {
va009039 16:981c3104f6c0 454 _ch = ch;
va009039 16:981c3104f6c0 455 slot |= (1<<_ch);
va009039 16:981c3104f6c0 456 }
va009039 16:981c3104f6c0 457
va009039 16:981c3104f6c0 458 HC::~HC() {
va009039 16:981c3104f6c0 459 slot &= ~(1<<_ch);
va009039 16:981c3104f6c0 460 }
va009039 16:981c3104f6c0 461
va009039 16:981c3104f6c0 462 HAL_StatusTypeDef HC::Init(uint8_t epnum, uint8_t dev_address, uint8_t speed, uint8_t ep_type, uint16_t mps) {
va009039 16:981c3104f6c0 463 _ep_addr = epnum;
va009039 16:981c3104f6c0 464 _ep_type = ep_type;
va009039 16:981c3104f6c0 465 return HAL_HCD_HC_Init(&hhcd_USB_OTG_FS, _ch,
va009039 16:981c3104f6c0 466 epnum, dev_address, speed, ep_type, mps);
va009039 16:981c3104f6c0 467 }
va009039 16:981c3104f6c0 468
va009039 16:981c3104f6c0 469 HAL_StatusTypeDef HC::SubmitRequest(uint8_t* pbuff, uint16_t length, bool setup) {
va009039 16:981c3104f6c0 470 uint8_t direction = (_ep_addr & 0x80) ? DIR_IN : DIR_OUT;
va009039 16:981c3104f6c0 471 if (_ep_type == EP_TYPE_CTRL) {
va009039 16:981c3104f6c0 472 HCD_HCTypeDef* hc = &hhcd_USB_OTG_FS.hc[_ch];
va009039 16:981c3104f6c0 473 if (setup) {
va009039 16:981c3104f6c0 474 hc->data_pid = HC_PID_SETUP;
va009039 16:981c3104f6c0 475 hc->toggle_out = 0;
va009039 16:981c3104f6c0 476 } else {
va009039 16:981c3104f6c0 477 if (direction == DIR_IN) {
va009039 16:981c3104f6c0 478 if (hc->toggle_in == 0) {
va009039 16:981c3104f6c0 479 hc->data_pid = HC_PID_DATA0;
va009039 16:981c3104f6c0 480 } else {
va009039 16:981c3104f6c0 481 hc->data_pid = HC_PID_DATA1;
va009039 16:981c3104f6c0 482 }
va009039 16:981c3104f6c0 483 } else { // OUT
va009039 16:981c3104f6c0 484 if (hc->toggle_out == 0) {
va009039 16:981c3104f6c0 485 hc->data_pid = HC_PID_DATA0;
va009039 16:981c3104f6c0 486 } else {
va009039 16:981c3104f6c0 487 hc->data_pid = HC_PID_DATA1;
va009039 16:981c3104f6c0 488 }
va009039 16:981c3104f6c0 489 }
va009039 16:981c3104f6c0 490 }
va009039 16:981c3104f6c0 491 hc->xfer_buff = pbuff;
va009039 16:981c3104f6c0 492 hc->xfer_len = length;
va009039 16:981c3104f6c0 493 hc->urb_state = URB_IDLE;
va009039 16:981c3104f6c0 494 hc->xfer_count = 0;
va009039 16:981c3104f6c0 495 hc->ch_num = _ch;
va009039 16:981c3104f6c0 496 hc->state = HC_IDLE;
va009039 16:981c3104f6c0 497
va009039 16:981c3104f6c0 498 return USB_HC_StartXfer(hhcd_USB_OTG_FS.Instance, hc, 0);
va009039 16:981c3104f6c0 499 }
va009039 16:981c3104f6c0 500 return HAL_HCD_HC_SubmitRequest(&hhcd_USB_OTG_FS, _ch,
va009039 16:981c3104f6c0 501 direction, _ep_type, 0, pbuff, length, 0);
va009039 16:981c3104f6c0 502 }
va009039 16:981c3104f6c0 503
va009039 16:981c3104f6c0 504 HCD_URBStateTypeDef HC::GetURBState() {
va009039 16:981c3104f6c0 505 return HAL_HCD_HC_GetURBState(&hhcd_USB_OTG_FS, _ch);
va009039 16:981c3104f6c0 506 }
va009039 16:981c3104f6c0 507
va009039 16:981c3104f6c0 508 HCD_HCStateTypeDef HC::GetState() {
va009039 16:981c3104f6c0 509 return HAL_HCD_HC_GetState(&hhcd_USB_OTG_FS, _ch);
va009039 16:981c3104f6c0 510 }
va009039 16:981c3104f6c0 511
va009039 16:981c3104f6c0 512 uint32_t HC::GetXferCount() {
va009039 16:981c3104f6c0 513 return HAL_HCD_HC_GetXferCount(&hhcd_USB_OTG_FS, _ch);
va009039 16:981c3104f6c0 514 }
va009039 16:981c3104f6c0 515
va009039 16:981c3104f6c0 516 void HC::SetToggle(uint8_t toggle) {
va009039 16:981c3104f6c0 517 if (_ep_addr & 0x80) { // IN
va009039 16:981c3104f6c0 518 hhcd_USB_OTG_FS.hc[_ch].toggle_in = toggle;
va009039 16:981c3104f6c0 519 } else { // OUT
va009039 16:981c3104f6c0 520 hhcd_USB_OTG_FS.hc[_ch].toggle_out = toggle;
va009039 16:981c3104f6c0 521 }
va009039 11:61843badd06e 522 }
va009039 11:61843badd06e 523
va009039 11:61843badd06e 524 #endif
va009039 11:61843badd06e 525
va009039 11:61843badd06e 526