Usb read

Dependencies:   FATFileSystem

Fork of F401RE-USBHost by Norimasa Okamoto

Committer:
va009039
Date:
Tue Jul 01 18:33:31 2014 +0900
Revision:
18:61554f238584
Parent:
17:4a710e2ba162
add lpc4088/lpc1768

Who changed what in which revision?

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