USB Host Driver with Socket Modem support. Works with revision 323 of mbed-src but broken with any later version.

Dependencies:   FATFileSystem

Fork of F401RE-USBHost by Norimasa Okamoto

Committer:
va009039
Date:
Mon Jun 09 09:00:49 2014 +0000
Revision:
11:61843badd06e
Child:
16:981c3104f6c0
Simple USBHost for Nucleo F401RE

Who changed what in which revision?

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