Usb read
Dependencies: FATFileSystem
Fork of F401RE-USBHost by
USBHost/USBHALHost_KL46Z.cpp@17:4a710e2ba162, 2014-06-23 (annotated)
- Committer:
- va009039
- Date:
- Mon Jun 23 20:48:53 2014 +0900
- Revision:
- 17:4a710e2ba162
- Parent:
- 16:981c3104f6c0
- Child:
- 18:61554f238584
add FRDM-K64F.(not tested)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 11:61843badd06e | 1 | // Simple USBHost for FRDM-KL46Z |
va009039 |
17:4a710e2ba162 | 2 | #if defined(TARGET_KL46Z)||defined(TARGET_KL25Z)||defined(TARGET_K64F) |
va009039 | 11:61843badd06e | 3 | #include "USBHALHost_KL46Z.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 | #define USB_DBG(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\n");} while(0); |
va009039 | 11:61843badd06e | 13 | #define USB_DBG_HEX(A,B) debug_hex(A,B) |
va009039 | 11:61843badd06e | 14 | void debug_hex(uint8_t* buf, int size); |
va009039 | 11:61843badd06e | 15 | #else |
va009039 | 11:61843badd06e | 16 | #define USB_DBG(...) while(0) |
va009039 | 11:61843badd06e | 17 | #define USB_DBG_HEX(A,B) while(0) |
va009039 | 11:61843badd06e | 18 | #endif |
va009039 | 11:61843badd06e | 19 | |
va009039 | 11:61843badd06e | 20 | #ifdef _USB_TEST |
va009039 | 11:61843badd06e | 21 | #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 | 22 | #define USB_TEST_ASSERT_FALSE(A) USB_TEST_ASSERT(!(A)) |
va009039 | 11:61843badd06e | 23 | #else |
va009039 | 11:61843badd06e | 24 | #define USB_TEST_ASSERT(A) while(0) |
va009039 | 11:61843badd06e | 25 | #define USB_TEST_ASSERT_FALSE(A) while(0) |
va009039 | 11:61843badd06e | 26 | #endif |
va009039 | 11:61843badd06e | 27 | |
va009039 | 11:61843badd06e | 28 | #define USB_INFO(...) do{fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\n");}while(0); |
va009039 | 11:61843badd06e | 29 | |
va009039 | 11:61843badd06e | 30 | #define BD_OWN_MASK (1<<7) |
va009039 | 11:61843badd06e | 31 | #define BD_DATA01_MASK (1<<6) |
va009039 | 11:61843badd06e | 32 | #define BD_KEEP_MASK (1<<5) |
va009039 | 11:61843badd06e | 33 | #define BD_NINC_MASK (1<<4) |
va009039 | 11:61843badd06e | 34 | #define BD_DTS_MASK (1<<3) |
va009039 | 11:61843badd06e | 35 | #define BD_STALL_MASK (1<<2) |
va009039 | 11:61843badd06e | 36 | |
va009039 | 11:61843badd06e | 37 | #define TX 1 |
va009039 | 11:61843badd06e | 38 | #define RX 0 |
va009039 | 11:61843badd06e | 39 | |
va009039 | 11:61843badd06e | 40 | #define EP0_BDT_IDX(dir, odd) (((2 * dir) + (1 * odd))) |
va009039 | 11:61843badd06e | 41 | |
va009039 | 11:61843badd06e | 42 | #define SETUP_TOKEN 0x0D |
va009039 | 11:61843badd06e | 43 | #define IN_TOKEN 0x09 |
va009039 | 11:61843badd06e | 44 | #define OUT_TOKEN 0x01 |
va009039 | 11:61843badd06e | 45 | |
va009039 | 11:61843badd06e | 46 | // for each endpt: 8 bytes |
va009039 | 11:61843badd06e | 47 | struct BDT { |
va009039 | 11:61843badd06e | 48 | uint8_t info; // BD[0:7] |
va009039 | 11:61843badd06e | 49 | uint8_t dummy; // RSVD: BD[8:15] |
va009039 | 11:61843badd06e | 50 | uint16_t byte_count; // BD[16:32] |
va009039 | 11:61843badd06e | 51 | uint32_t address; // Addr |
va009039 | 11:61843badd06e | 52 | void setBuffer(uint8_t* buf, int size) { |
va009039 | 11:61843badd06e | 53 | address = (uint32_t)buf; |
va009039 | 11:61843badd06e | 54 | byte_count = size; |
va009039 | 11:61843badd06e | 55 | } |
va009039 | 11:61843badd06e | 56 | uint8_t getStatus() { |
va009039 | 11:61843badd06e | 57 | return (info>>2)&0x0f; |
va009039 | 11:61843badd06e | 58 | } |
va009039 | 11:61843badd06e | 59 | }; |
va009039 | 11:61843badd06e | 60 | |
va009039 | 11:61843badd06e | 61 | __attribute__((__aligned__(512))) BDT bdt[64]; |
va009039 | 11:61843badd06e | 62 | |
va009039 | 11:61843badd06e | 63 | USBHALHost* USBHALHost::instHost; |
va009039 | 11:61843badd06e | 64 | |
va009039 | 11:61843badd06e | 65 | USBHALHost::USBHALHost() { |
va009039 | 11:61843badd06e | 66 | instHost = this; |
va009039 | 11:61843badd06e | 67 | report.clear(); |
va009039 | 11:61843badd06e | 68 | } |
va009039 | 11:61843badd06e | 69 | |
va009039 | 11:61843badd06e | 70 | void USBHALHost::init() { |
va009039 | 11:61843badd06e | 71 | // Disable IRQ |
va009039 | 11:61843badd06e | 72 | NVIC_DisableIRQ(USB0_IRQn); |
va009039 | 11:61843badd06e | 73 | |
va009039 |
16:981c3104f6c0 | 74 | #if defined(TARGET_K64F) |
va009039 |
16:981c3104f6c0 | 75 | MPU->CESR=0; |
va009039 |
16:981c3104f6c0 | 76 | #endif |
va009039 |
16:981c3104f6c0 | 77 | |
va009039 | 11:61843badd06e | 78 | // choose usb src as PLL |
va009039 | 11:61843badd06e | 79 | SIM->SOPT2 |= (SIM_SOPT2_USBSRC_MASK | SIM_SOPT2_PLLFLLSEL_MASK); |
va009039 | 11:61843badd06e | 80 | |
va009039 | 11:61843badd06e | 81 | // enable OTG clock |
va009039 | 11:61843badd06e | 82 | SIM->SCGC4 |= SIM_SCGC4_USBOTG_MASK; |
va009039 | 11:61843badd06e | 83 | |
va009039 | 11:61843badd06e | 84 | // USB Module Configuration |
va009039 | 11:61843badd06e | 85 | // Reset USB Module |
va009039 | 11:61843badd06e | 86 | USB0->USBTRC0 |= USB_USBTRC0_USBRESET_MASK; |
va009039 | 11:61843badd06e | 87 | while(USB0->USBTRC0 & USB_USBTRC0_USBRESET_MASK); |
va009039 | 11:61843badd06e | 88 | |
va009039 | 11:61843badd06e | 89 | // Clear interrupt flag |
va009039 | 11:61843badd06e | 90 | USB0->ISTAT = 0xff; |
va009039 | 11:61843badd06e | 91 | |
va009039 | 11:61843badd06e | 92 | // Set BDT Base Register |
va009039 | 11:61843badd06e | 93 | USB0->BDTPAGE1=(uint8_t)((uint32_t)bdt>>8); |
va009039 | 11:61843badd06e | 94 | USB0->BDTPAGE2=(uint8_t)((uint32_t)bdt>>16); |
va009039 | 11:61843badd06e | 95 | USB0->BDTPAGE3=(uint8_t)((uint32_t)bdt>>24); |
va009039 | 11:61843badd06e | 96 | |
va009039 | 11:61843badd06e | 97 | // Set SOF threshold |
va009039 | 11:61843badd06e | 98 | USB0->SOFTHLD = USB_SOFTHLD_CNT(1); |
va009039 | 11:61843badd06e | 99 | |
va009039 | 11:61843badd06e | 100 | // pulldown D+ and D- |
va009039 | 11:61843badd06e | 101 | USB0->USBCTRL = USB_USBCTRL_PDE_MASK; |
va009039 | 11:61843badd06e | 102 | |
va009039 | 11:61843badd06e | 103 | USB0->USBTRC0 |= 0x40; |
va009039 | 11:61843badd06e | 104 | |
va009039 | 11:61843badd06e | 105 | // Host mode |
va009039 | 11:61843badd06e | 106 | USB0->CTL |= USB_CTL_HOSTMODEEN_MASK; |
va009039 | 11:61843badd06e | 107 | // Desable SOF packet generation |
va009039 | 11:61843badd06e | 108 | USB0->CTL &= ~USB_CTL_USBENSOFEN_MASK; |
va009039 | 11:61843badd06e | 109 | |
va009039 | 11:61843badd06e | 110 | NVIC_SetVector(USB0_IRQn, (uint32_t)_usbisr); |
va009039 | 11:61843badd06e | 111 | NVIC_EnableIRQ(USB0_IRQn); |
va009039 | 11:61843badd06e | 112 | |
va009039 | 11:61843badd06e | 113 | bool lowSpeed = wait_attach(); |
va009039 | 11:61843badd06e | 114 | |
va009039 | 11:61843badd06e | 115 | for(int retry = 2; retry > 0; retry--) { |
va009039 | 11:61843badd06e | 116 | // Enable RESET |
va009039 | 11:61843badd06e | 117 | USB0->CTL |= USB_CTL_RESET_MASK; |
va009039 | 11:61843badd06e | 118 | wait_ms(500); |
va009039 | 11:61843badd06e | 119 | USB0->CTL &= ~USB_CTL_RESET_MASK; |
va009039 | 11:61843badd06e | 120 | |
va009039 | 11:61843badd06e | 121 | // Enable SOF |
va009039 | 11:61843badd06e | 122 | USB0->CTL |= USB_CTL_USBENSOFEN_MASK; |
va009039 | 11:61843badd06e | 123 | wait_ms(100); |
va009039 | 11:61843badd06e | 124 | |
va009039 | 11:61843badd06e | 125 | // token transfer initialize |
va009039 | 11:61843badd06e | 126 | token_transfer_init(); |
va009039 | 11:61843badd06e | 127 | |
va009039 | 11:61843badd06e | 128 | USB0->INTEN |= USB_INTEN_TOKDNEEN_MASK| |
va009039 | 11:61843badd06e | 129 | USB_INTEN_ERROREN_MASK; |
va009039 | 11:61843badd06e | 130 | USB0->ERREN |= USB_ERREN_PIDERREN_MASK| |
va009039 | 11:61843badd06e | 131 | USB_ERREN_CRC5EOFEN_MASK| |
va009039 | 11:61843badd06e | 132 | USB_ERREN_CRC16EN_MASK| |
va009039 | 11:61843badd06e | 133 | USB_ERREN_DFN8EN_MASK| |
va009039 | 11:61843badd06e | 134 | USB_ERREN_BTOERREN_MASK| |
va009039 | 11:61843badd06e | 135 | USB_ERREN_DMAERREN_MASK| |
va009039 | 11:61843badd06e | 136 | USB_ERREN_BTSERREN_MASK; |
va009039 | 11:61843badd06e | 137 | |
va009039 | 11:61843badd06e | 138 | if (addDevice(NULL, 0, lowSpeed)) { |
va009039 | 11:61843badd06e | 139 | break; |
va009039 | 11:61843badd06e | 140 | } |
va009039 | 11:61843badd06e | 141 | USB_DBG("retry=%d", retry); |
va009039 | 11:61843badd06e | 142 | USB_TEST_ASSERT(retry > 1); |
va009039 | 11:61843badd06e | 143 | } |
va009039 | 11:61843badd06e | 144 | } |
va009039 | 11:61843badd06e | 145 | |
va009039 | 11:61843badd06e | 146 | bool USBHALHost::wait_attach() { |
va009039 | 11:61843badd06e | 147 | attach_done = false; |
va009039 | 11:61843badd06e | 148 | USB0->INTEN = USB_INTEN_ATTACHEN_MASK; |
va009039 | 11:61843badd06e | 149 | Timer t; |
va009039 | 11:61843badd06e | 150 | t.reset(); |
va009039 | 11:61843badd06e | 151 | t.start(); |
va009039 | 11:61843badd06e | 152 | while(!attach_done) { |
va009039 | 11:61843badd06e | 153 | if (t.read_ms() > 5*1000) { |
va009039 | 11:61843badd06e | 154 | t.reset(); |
va009039 | 11:61843badd06e | 155 | USB_INFO("Please attach USB device."); |
va009039 | 11:61843badd06e | 156 | } |
va009039 | 11:61843badd06e | 157 | } |
va009039 | 11:61843badd06e | 158 | wait_ms(100); |
va009039 | 11:61843badd06e | 159 | USB_TEST_ASSERT_FALSE(USB0->CTL & USB_CTL_SE0_MASK); |
va009039 | 11:61843badd06e | 160 | root_lowSpeed = (USB0->CTL & USB_CTL_JSTATE_MASK) ? false : true; |
va009039 | 11:61843badd06e | 161 | return root_lowSpeed; |
va009039 | 11:61843badd06e | 162 | } |
va009039 | 11:61843badd06e | 163 | |
va009039 | 11:61843badd06e | 164 | void USBHALHost::setAddr(int _addr, bool _lowSpeed) { |
va009039 | 11:61843badd06e | 165 | USB0->ADDR = (_lowSpeed ? USB_ADDR_LSEN_MASK : 0x00) | USB_ADDR_ADDR(_addr); |
va009039 | 11:61843badd06e | 166 | } |
va009039 | 11:61843badd06e | 167 | |
va009039 | 11:61843badd06e | 168 | void USBHALHost::setEndpoint() { |
va009039 | 11:61843badd06e | 169 | USB0->ENDPOINT[0].ENDPT = (root_lowSpeed ? USB_ENDPT_HOSTWOHUB_MASK : 0x00)| |
va009039 | 11:61843badd06e | 170 | USB_ENDPT_RETRYDIS_MASK| |
va009039 | 11:61843badd06e | 171 | USB_ENDPT_EPCTLDIS_MASK| |
va009039 | 11:61843badd06e | 172 | USB_ENDPT_EPRXEN_MASK| |
va009039 | 11:61843badd06e | 173 | USB_ENDPT_EPTXEN_MASK| |
va009039 | 11:61843badd06e | 174 | USB_ENDPT_EPHSHK_MASK; |
va009039 | 11:61843badd06e | 175 | } |
va009039 | 11:61843badd06e | 176 | |
va009039 | 11:61843badd06e | 177 | void USBHALHost::token_transfer_init() { |
va009039 | 11:61843badd06e | 178 | tx_ptr = ODD; |
va009039 | 11:61843badd06e | 179 | rx_ptr = ODD; |
va009039 | 11:61843badd06e | 180 | } |
va009039 | 11:61843badd06e | 181 | |
va009039 | 11:61843badd06e | 182 | int USBHALHost::token_setup(USBEndpoint* ep, SETUP_PACKET* setup, uint16_t wLength) { |
va009039 | 11:61843badd06e | 183 | USBDeviceConnected* dev = ep->getDevice(); |
va009039 | 11:61843badd06e | 184 | for(int retry = 0;; retry++) { |
va009039 | 11:61843badd06e | 185 | token_ready(); |
va009039 | 11:61843badd06e | 186 | USB0->ENDPOINT[0].ENDPT = (root_lowSpeed ? USB_ENDPT_HOSTWOHUB_MASK : 0x00) | |
va009039 | 11:61843badd06e | 187 | USB_ENDPT_RETRYDIS_MASK| |
va009039 | 11:61843badd06e | 188 | USB_ENDPT_EPRXEN_MASK| |
va009039 | 11:61843badd06e | 189 | USB_ENDPT_EPTXEN_MASK| |
va009039 | 11:61843badd06e | 190 | USB_ENDPT_EPHSHK_MASK; |
va009039 | 11:61843badd06e | 191 | CTASSERT(sizeof(SETUP_PACKET) == 8); |
va009039 | 11:61843badd06e | 192 | setup->wLength = wLength; |
va009039 | 11:61843badd06e | 193 | int idx = EP0_BDT_IDX(TX, tx_ptr); |
va009039 | 11:61843badd06e | 194 | bdt[idx].setBuffer((uint8_t*)setup, sizeof(SETUP_PACKET)); |
va009039 | 11:61843badd06e | 195 | bdt[idx].info = BD_OWN_MASK | |
va009039 | 11:61843badd06e | 196 | BD_DTS_MASK; // always data0 |
va009039 | 11:61843badd06e | 197 | token_done = false; |
va009039 | 11:61843badd06e | 198 | USB0->TOKEN = USB_TOKEN_TOKENPID(SETUP_TOKEN)| |
va009039 | 11:61843badd06e | 199 | USB_TOKEN_TOKENENDPT(ep->getAddress() & 0x7f); |
va009039 | 11:61843badd06e | 200 | while(!token_done); |
va009039 | 11:61843badd06e | 201 | LastStatus = bdt[idx].getStatus(); |
va009039 | 11:61843badd06e | 202 | if (LastStatus == ACK) { |
va009039 | 11:61843badd06e | 203 | if (retry > 0) { |
va009039 | 11:61843badd06e | 204 | USB_DBG("retry=%d %02x", retry, prev_LastStatus); |
va009039 | 11:61843badd06e | 205 | } |
va009039 | 11:61843badd06e | 206 | break; |
va009039 | 11:61843badd06e | 207 | } else if (LastStatus == STALL) { |
va009039 | 11:61843badd06e | 208 | report.stall++; |
va009039 | 11:61843badd06e | 209 | return STALL; |
va009039 | 11:61843badd06e | 210 | } |
va009039 | 11:61843badd06e | 211 | if (retry > 10) { |
va009039 | 11:61843badd06e | 212 | USB_DBG("retry=%d", retry); |
va009039 | 11:61843badd06e | 213 | break; |
va009039 | 11:61843badd06e | 214 | } |
va009039 | 11:61843badd06e | 215 | prev_LastStatus = LastStatus; |
va009039 | 11:61843badd06e | 216 | wait_ms(100 * retry); |
va009039 | 11:61843badd06e | 217 | } |
va009039 | 11:61843badd06e | 218 | ep->setData01(DATA1); // next toggle |
va009039 | 11:61843badd06e | 219 | return LastStatus; |
va009039 | 11:61843badd06e | 220 | } |
va009039 | 11:61843badd06e | 221 | |
va009039 | 11:61843badd06e | 222 | int USBHALHost::token_in(USBEndpoint* ep, uint8_t* data, int size, int retryLimit) { |
va009039 | 11:61843badd06e | 223 | for(int retry = 0;; retry++) { |
va009039 | 11:61843badd06e | 224 | token_ready(); |
va009039 | 11:61843badd06e | 225 | int idx = EP0_BDT_IDX(RX, rx_ptr); |
va009039 | 11:61843badd06e | 226 | bdt[idx].setBuffer(data, size); |
va009039 | 11:61843badd06e | 227 | bdt[idx].info = BD_OWN_MASK| |
va009039 | 11:61843badd06e | 228 | BD_DTS_MASK| |
va009039 | 11:61843badd06e | 229 | (ep->getData01() == DATA1 ? BD_DATA01_MASK : 0); |
va009039 | 11:61843badd06e | 230 | token_done = false; |
va009039 | 11:61843badd06e | 231 | USB0->TOKEN = USB_TOKEN_TOKENPID(IN_TOKEN)| |
va009039 | 11:61843badd06e | 232 | USB_TOKEN_TOKENENDPT(ep->getAddress()&0x7f); |
va009039 | 11:61843badd06e | 233 | while(!token_done); |
va009039 | 11:61843badd06e | 234 | LastStatus = bdt[idx].getStatus(); |
va009039 | 11:61843badd06e | 235 | int len = bdt[idx].byte_count; |
va009039 | 11:61843badd06e | 236 | if (LastStatus == DATA0 || LastStatus == DATA1) { |
va009039 | 11:61843badd06e | 237 | USB_TEST_ASSERT(ep->getData01() == LastStatus); |
va009039 | 11:61843badd06e | 238 | ep->setData01(LastStatus == DATA0 ? DATA1 : DATA0); |
va009039 | 11:61843badd06e | 239 | if (retry > 0) { |
va009039 | 11:61843badd06e | 240 | //USB_DBG("len=%d retry=%d %02x", len, retry, prev_LastStatus); |
va009039 | 11:61843badd06e | 241 | } |
va009039 | 11:61843badd06e | 242 | return len; |
va009039 | 11:61843badd06e | 243 | } else if (LastStatus == STALL) { |
va009039 | 11:61843badd06e | 244 | report.stall++; |
va009039 | 11:61843badd06e | 245 | return -1; |
va009039 | 11:61843badd06e | 246 | } else if (LastStatus == NAK) { |
va009039 | 11:61843badd06e | 247 | report.nak++; |
va009039 | 11:61843badd06e | 248 | if (retry >= retryLimit) { |
va009039 | 11:61843badd06e | 249 | if (retryLimit > 0) { |
va009039 | 11:61843badd06e | 250 | USB_DBG("retry=%d retryLimit=%d", retry, retryLimit); |
va009039 | 11:61843badd06e | 251 | } |
va009039 | 11:61843badd06e | 252 | return -1; |
va009039 | 11:61843badd06e | 253 | } |
va009039 | 11:61843badd06e | 254 | wait_ms(100 * retry); |
va009039 | 11:61843badd06e | 255 | } else if (LastStatus == Bus_Timeout) { |
va009039 | 11:61843badd06e | 256 | if (retry >= retryLimit) { |
va009039 | 11:61843badd06e | 257 | if (retryLimit > 0) { |
va009039 | 11:61843badd06e | 258 | USB_DBG("Bus_Timeout retry=%d retryLimit=%d", retry, retryLimit); |
va009039 | 11:61843badd06e | 259 | } |
va009039 | 11:61843badd06e | 260 | return -1; |
va009039 | 11:61843badd06e | 261 | } |
va009039 | 11:61843badd06e | 262 | wait_ms(500 + 100 * retry); |
va009039 | 11:61843badd06e | 263 | } else { |
va009039 | 11:61843badd06e | 264 | return -1; |
va009039 | 11:61843badd06e | 265 | } |
va009039 | 11:61843badd06e | 266 | prev_LastStatus = LastStatus; |
va009039 | 11:61843badd06e | 267 | } |
va009039 | 11:61843badd06e | 268 | } |
va009039 | 11:61843badd06e | 269 | |
va009039 | 11:61843badd06e | 270 | int USBHALHost::token_out(USBEndpoint* ep, const uint8_t* data, int size, int retryLimit) { |
va009039 | 11:61843badd06e | 271 | for(int retry = 0;; retry++) { |
va009039 | 11:61843badd06e | 272 | token_ready(); |
va009039 | 11:61843badd06e | 273 | int idx = EP0_BDT_IDX(TX, tx_ptr); |
va009039 | 11:61843badd06e | 274 | bdt[idx].setBuffer((uint8_t*)data, size); |
va009039 | 11:61843badd06e | 275 | bdt[idx].info = BD_OWN_MASK| |
va009039 | 11:61843badd06e | 276 | BD_DTS_MASK| |
va009039 | 11:61843badd06e | 277 | (ep->getData01() == DATA1 ? BD_DATA01_MASK : 0); |
va009039 | 11:61843badd06e | 278 | token_done = false; |
va009039 | 11:61843badd06e | 279 | USB0->TOKEN = USB_TOKEN_TOKENPID(OUT_TOKEN)| |
va009039 | 11:61843badd06e | 280 | USB_TOKEN_TOKENENDPT(ep->getAddress()&0x7f); |
va009039 | 11:61843badd06e | 281 | while(!token_done); |
va009039 | 11:61843badd06e | 282 | LastStatus = bdt[idx].getStatus(); |
va009039 | 11:61843badd06e | 283 | int len = bdt[idx].byte_count; |
va009039 | 11:61843badd06e | 284 | //USB_DBG("len=%d %02x", len, LastStatus); |
va009039 | 11:61843badd06e | 285 | if (LastStatus == ACK) { |
va009039 | 11:61843badd06e | 286 | ep->toggleData01(); |
va009039 | 11:61843badd06e | 287 | if (retry > 0) { |
va009039 | 11:61843badd06e | 288 | USB_DBG("len=%d retry=%d %02x", len, retry, prev_LastStatus); |
va009039 | 11:61843badd06e | 289 | } |
va009039 | 11:61843badd06e | 290 | return len; |
va009039 | 11:61843badd06e | 291 | } else if (LastStatus == STALL) { |
va009039 | 11:61843badd06e | 292 | report.stall++; |
va009039 | 11:61843badd06e | 293 | return -1; |
va009039 | 11:61843badd06e | 294 | } else if (LastStatus == NAK) { |
va009039 | 11:61843badd06e | 295 | report.nak++; |
va009039 | 11:61843badd06e | 296 | if (retry > retryLimit) { |
va009039 | 11:61843badd06e | 297 | USB_DBG("retry=%d retryLimit=%d", retry, retryLimit); |
va009039 | 11:61843badd06e | 298 | return -1; |
va009039 | 11:61843badd06e | 299 | } |
va009039 | 11:61843badd06e | 300 | wait_ms(100 * retry); |
va009039 | 11:61843badd06e | 301 | } else { |
va009039 | 11:61843badd06e | 302 | return -1; |
va009039 | 11:61843badd06e | 303 | } |
va009039 | 11:61843badd06e | 304 | prev_LastStatus = LastStatus; |
va009039 | 11:61843badd06e | 305 | } |
va009039 | 11:61843badd06e | 306 | } |
va009039 | 11:61843badd06e | 307 | |
va009039 | 11:61843badd06e | 308 | int USBHALHost::token_iso_in(USBEndpoint* ep, uint8_t* data, int size) { |
va009039 | 11:61843badd06e | 309 | while(USB0->CTL & USB_CTL_TXSUSPENDTOKENBUSY_MASK); // TOKEN_BUSY ? |
va009039 | 11:61843badd06e | 310 | USB0->ISTAT |= USB_ISTAT_SOFTOK_MASK; // Clear SOF |
va009039 | 11:61843badd06e | 311 | while (!(USB0->ISTAT & USB_ISTAT_SOFTOK_MASK)); |
va009039 | 11:61843badd06e | 312 | USB0->SOFTHLD = 0; // this is needed as without this you can get errors |
va009039 | 11:61843badd06e | 313 | USB0->ISTAT |= USB_ISTAT_SOFTOK_MASK; // clear SOF |
va009039 | 11:61843badd06e | 314 | |
va009039 | 11:61843badd06e | 315 | USB0->ENDPOINT[0].ENDPT = USB_ENDPT_EPCTLDIS_MASK| |
va009039 | 11:61843badd06e | 316 | USB_ENDPT_RETRYDIS_MASK| |
va009039 | 11:61843badd06e | 317 | USB_ENDPT_EPRXEN_MASK| |
va009039 | 11:61843badd06e | 318 | USB_ENDPT_EPTXEN_MASK; |
va009039 | 11:61843badd06e | 319 | int idx = EP0_BDT_IDX(RX, rx_ptr); |
va009039 | 11:61843badd06e | 320 | bdt[idx].setBuffer(data, size); |
va009039 | 11:61843badd06e | 321 | bdt[idx].info = BD_OWN_MASK| |
va009039 | 11:61843badd06e | 322 | BD_DTS_MASK; // always DATA0 |
va009039 | 11:61843badd06e | 323 | token_done = false; |
va009039 | 11:61843badd06e | 324 | USB0->TOKEN = USB_TOKEN_TOKENPID(IN_TOKEN)| |
va009039 | 11:61843badd06e | 325 | USB_TOKEN_TOKENENDPT(ep->getAddress()&0x7f); |
va009039 | 11:61843badd06e | 326 | while(!token_done); |
va009039 | 11:61843badd06e | 327 | LastStatus = bdt[idx].getStatus(); |
va009039 | 11:61843badd06e | 328 | int len = bdt[idx].byte_count; |
va009039 | 11:61843badd06e | 329 | if (LastStatus == DATA0) { |
va009039 | 11:61843badd06e | 330 | return len; |
va009039 | 11:61843badd06e | 331 | } |
va009039 | 11:61843badd06e | 332 | return -1; |
va009039 | 11:61843badd06e | 333 | } |
va009039 | 11:61843badd06e | 334 | |
va009039 | 11:61843badd06e | 335 | void USBHALHost::token_ready() { |
va009039 | 11:61843badd06e | 336 | while(USB0->CTL & USB_CTL_TXSUSPENDTOKENBUSY_MASK) { // TOKEN_BUSY ? |
va009039 | 11:61843badd06e | 337 | wait_ms(1); |
va009039 | 11:61843badd06e | 338 | } |
va009039 | 11:61843badd06e | 339 | USB0->ISTAT |= USB_ISTAT_SOFTOK_MASK; // Clear SOF |
va009039 | 11:61843badd06e | 340 | while (!(USB0->ISTAT & USB_ISTAT_SOFTOK_MASK)); |
va009039 | 11:61843badd06e | 341 | USB0->SOFTHLD = 0; // this is needed as without this you can get errors |
va009039 | 11:61843badd06e | 342 | USB0->ISTAT |= USB_ISTAT_SOFTOK_MASK; // clear SOF |
va009039 | 11:61843badd06e | 343 | } |
va009039 | 11:61843badd06e | 344 | |
va009039 |
16:981c3104f6c0 | 345 | int USBHALHost::multi_token_in(USBEndpoint* ep, uint8_t* data, size_t total, bool block) { |
va009039 |
16:981c3104f6c0 | 346 | if (total == 0) { |
va009039 |
16:981c3104f6c0 | 347 | return token_in(ep); |
va009039 |
16:981c3104f6c0 | 348 | } |
va009039 |
16:981c3104f6c0 | 349 | int retryLimit = block ? 10 : 0; |
va009039 |
16:981c3104f6c0 | 350 | int read_len = 0; |
va009039 |
16:981c3104f6c0 | 351 | for(int n = 0; read_len < total; n++) { |
va009039 |
16:981c3104f6c0 | 352 | int size = std::min((int)total-read_len, ep->getSize()); |
va009039 |
16:981c3104f6c0 | 353 | int result = token_in(ep, data+read_len, size, retryLimit); |
va009039 |
16:981c3104f6c0 | 354 | if (result < 0) { |
va009039 |
16:981c3104f6c0 | 355 | if (block) { |
va009039 |
16:981c3104f6c0 | 356 | return -1; |
va009039 |
16:981c3104f6c0 | 357 | } |
va009039 |
16:981c3104f6c0 | 358 | if (LastStatus == NAK) { |
va009039 |
16:981c3104f6c0 | 359 | if (n == 0) { |
va009039 |
16:981c3104f6c0 | 360 | return -1; |
va009039 |
16:981c3104f6c0 | 361 | } |
va009039 |
16:981c3104f6c0 | 362 | break; |
va009039 |
16:981c3104f6c0 | 363 | } |
va009039 |
16:981c3104f6c0 | 364 | return result; |
va009039 |
16:981c3104f6c0 | 365 | } |
va009039 |
16:981c3104f6c0 | 366 | read_len += result; |
va009039 |
16:981c3104f6c0 | 367 | if (result < ep->getSize()) { |
va009039 |
16:981c3104f6c0 | 368 | break; |
va009039 |
16:981c3104f6c0 | 369 | } |
va009039 |
16:981c3104f6c0 | 370 | } |
va009039 |
16:981c3104f6c0 | 371 | return read_len; |
va009039 |
16:981c3104f6c0 | 372 | } |
va009039 |
16:981c3104f6c0 | 373 | |
va009039 |
16:981c3104f6c0 | 374 | int USBHALHost::multi_token_out(USBEndpoint* ep, const uint8_t* data, size_t total, bool block) { |
va009039 |
16:981c3104f6c0 | 375 | if (total == 0) { |
va009039 |
16:981c3104f6c0 | 376 | return token_out(ep); |
va009039 |
16:981c3104f6c0 | 377 | } |
va009039 |
16:981c3104f6c0 | 378 | int write_len = 0; |
va009039 |
16:981c3104f6c0 | 379 | for(int n = 0; write_len < total; n++) { |
va009039 |
16:981c3104f6c0 | 380 | int size = std::min((int)total-write_len, ep->getSize()); |
va009039 |
16:981c3104f6c0 | 381 | int result = token_out(ep, data+write_len, size); |
va009039 |
16:981c3104f6c0 | 382 | if (result < 0) { |
va009039 |
16:981c3104f6c0 | 383 | if (LastStatus == NAK) { |
va009039 |
16:981c3104f6c0 | 384 | if (n == 0) { |
va009039 |
16:981c3104f6c0 | 385 | return -1; |
va009039 |
16:981c3104f6c0 | 386 | } |
va009039 |
16:981c3104f6c0 | 387 | break; |
va009039 |
16:981c3104f6c0 | 388 | } |
va009039 |
16:981c3104f6c0 | 389 | USB_DBG("token_out result=%d %02x", result, LastStatus); |
va009039 |
16:981c3104f6c0 | 390 | return result; |
va009039 |
16:981c3104f6c0 | 391 | } |
va009039 |
16:981c3104f6c0 | 392 | write_len += result; |
va009039 |
16:981c3104f6c0 | 393 | if (result < ep->getSize()) { |
va009039 |
16:981c3104f6c0 | 394 | break; |
va009039 |
16:981c3104f6c0 | 395 | } |
va009039 |
16:981c3104f6c0 | 396 | } |
va009039 |
16:981c3104f6c0 | 397 | return write_len; |
va009039 |
16:981c3104f6c0 | 398 | } |
va009039 |
16:981c3104f6c0 | 399 | |
va009039 | 11:61843badd06e | 400 | void USBHALHost::_usbisr(void) { |
va009039 | 11:61843badd06e | 401 | if (instHost) { |
va009039 | 11:61843badd06e | 402 | instHost->UsbIrqhandler(); |
va009039 | 11:61843badd06e | 403 | } |
va009039 | 11:61843badd06e | 404 | } |
va009039 | 11:61843badd06e | 405 | |
va009039 | 11:61843badd06e | 406 | void USBHALHost::UsbIrqhandler() { |
va009039 | 11:61843badd06e | 407 | if (USB0->ISTAT & USB_ISTAT_TOKDNE_MASK) { |
va009039 | 11:61843badd06e | 408 | uint8_t stat = USB0->STAT; |
va009039 | 11:61843badd06e | 409 | ODD_EVEN next_ptr = (stat & USB_STAT_ODD_MASK) ? ODD : EVEN; |
va009039 | 11:61843badd06e | 410 | if (stat & USB_STAT_TX_MASK) { |
va009039 | 11:61843badd06e | 411 | tx_ptr = next_ptr; |
va009039 | 11:61843badd06e | 412 | } else { |
va009039 | 11:61843badd06e | 413 | rx_ptr = next_ptr; |
va009039 | 11:61843badd06e | 414 | } |
va009039 | 11:61843badd06e | 415 | USB0->ISTAT = USB_ISTAT_TOKDNE_MASK; |
va009039 | 11:61843badd06e | 416 | token_done = true; |
va009039 | 11:61843badd06e | 417 | } |
va009039 | 11:61843badd06e | 418 | if (USB0->ISTAT & USB_ISTAT_ATTACH_MASK) { |
va009039 | 11:61843badd06e | 419 | USB0->INTEN &= ~USB_INTEN_ATTACHEN_MASK; |
va009039 | 11:61843badd06e | 420 | USB0->ISTAT = USB_ISTAT_ATTACH_MASK; |
va009039 | 11:61843badd06e | 421 | attach_done = true; |
va009039 | 11:61843badd06e | 422 | } |
va009039 | 11:61843badd06e | 423 | if (USB0->ISTAT & USB_ISTAT_ERROR_MASK) { |
va009039 | 11:61843badd06e | 424 | uint8_t errstat = USB0->ERRSTAT; |
va009039 | 11:61843badd06e | 425 | if (errstat & USB_ERRSTAT_PIDERR_MASK) { |
va009039 | 11:61843badd06e | 426 | report.errstat_piderr++; |
va009039 | 11:61843badd06e | 427 | } |
va009039 | 11:61843badd06e | 428 | if (errstat & USB_ERRSTAT_CRC5EOF_MASK) { |
va009039 | 11:61843badd06e | 429 | report.errstat_crc5eof++; |
va009039 | 11:61843badd06e | 430 | } |
va009039 | 11:61843badd06e | 431 | if (errstat & USB_ERRSTAT_CRC16_MASK) { |
va009039 | 11:61843badd06e | 432 | report.errstat_crc16++; |
va009039 | 11:61843badd06e | 433 | } |
va009039 | 11:61843badd06e | 434 | if (errstat & USB_ERRSTAT_DFN8_MASK) { |
va009039 | 11:61843badd06e | 435 | report.errstat_dfn8++; |
va009039 | 11:61843badd06e | 436 | } |
va009039 | 11:61843badd06e | 437 | if (errstat & USB_ERRSTAT_BTOERR_MASK) { |
va009039 | 11:61843badd06e | 438 | report.errstat_btoerr++; |
va009039 | 11:61843badd06e | 439 | } |
va009039 | 11:61843badd06e | 440 | if (errstat & USB_ERRSTAT_DMAERR_MASK) { |
va009039 | 11:61843badd06e | 441 | report.errstat_dmaerr++; |
va009039 | 11:61843badd06e | 442 | } |
va009039 | 11:61843badd06e | 443 | if (errstat & USB_ERRSTAT_BTSERR_MASK) { |
va009039 | 11:61843badd06e | 444 | report.errstat_btoerr++; |
va009039 | 11:61843badd06e | 445 | } |
va009039 | 11:61843badd06e | 446 | USB0->ERRSTAT = errstat; |
va009039 | 11:61843badd06e | 447 | USB0->ISTAT = USB_ISTAT_ERROR_MASK; |
va009039 | 11:61843badd06e | 448 | } |
va009039 | 11:61843badd06e | 449 | } |
va009039 | 11:61843badd06e | 450 | |
va009039 | 11:61843badd06e | 451 | void Report::clear() { |
va009039 | 11:61843badd06e | 452 | errstat_piderr = 0; |
va009039 | 11:61843badd06e | 453 | errstat_crc5eof = 0; |
va009039 | 11:61843badd06e | 454 | errstat_crc16 = 0; |
va009039 | 11:61843badd06e | 455 | errstat_dfn8 = 0; |
va009039 | 11:61843badd06e | 456 | errstat_btoerr = 0; |
va009039 | 11:61843badd06e | 457 | errstat_dmaerr = 0; |
va009039 | 11:61843badd06e | 458 | errstat_bsterr = 0; |
va009039 | 11:61843badd06e | 459 | // |
va009039 | 11:61843badd06e | 460 | nak = 0; |
va009039 | 11:61843badd06e | 461 | } |
va009039 | 11:61843badd06e | 462 | |
va009039 | 11:61843badd06e | 463 | void Report::print_errstat() { |
va009039 | 11:61843badd06e | 464 | printf("ERRSTAT PID: %d, CRC5EOF: %d, CRC16: %d, DFN8: %d, BTO: %d, DMA: %d, BST: %d\n", |
va009039 | 11:61843badd06e | 465 | errstat_piderr, errstat_crc5eof, |
va009039 | 11:61843badd06e | 466 | errstat_crc16, errstat_dfn8, |
va009039 | 11:61843badd06e | 467 | errstat_btoerr, errstat_dmaerr, errstat_bsterr); |
va009039 | 11:61843badd06e | 468 | } |
va009039 | 11:61843badd06e | 469 | |
va009039 | 11:61843badd06e | 470 | void debug_hex(uint8_t* buf, int size) { |
va009039 | 11:61843badd06e | 471 | int n = 0; |
va009039 | 11:61843badd06e | 472 | for(int i = 0; i < size; i++) { |
va009039 | 11:61843badd06e | 473 | fprintf(stderr, "%02x ", buf[i]); |
va009039 | 11:61843badd06e | 474 | if (++n >= 16) { |
va009039 | 11:61843badd06e | 475 | fprintf(stderr, "\n"); |
va009039 | 11:61843badd06e | 476 | n = 0; |
va009039 | 11:61843badd06e | 477 | } |
va009039 | 11:61843badd06e | 478 | } |
va009039 | 11:61843badd06e | 479 | if (n > 0) { |
va009039 | 11:61843badd06e | 480 | fprintf(stderr, "\n"); |
va009039 | 11:61843badd06e | 481 | } |
va009039 | 11:61843badd06e | 482 | } |
va009039 | 11:61843badd06e | 483 | |
va009039 | 11:61843badd06e | 484 | #endif |
va009039 | 11:61843badd06e | 485 | |
va009039 | 11:61843badd06e | 486 |