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:
fritz291
Date:
Fri Jun 26 19:30:55 2015 +0000
Revision:
26:53970cabf56d
Parent:
24:f4d432f36ec2
Fixed small error in testAT function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 11:61843badd06e 1 // Simple USBHost for FRDM-KL46Z
fritz291 24:f4d432f36ec2 2 #if defined(TARGET_KL46Z)||defined(TARGET_KL25Z)||defined(TARGET_K64F)||defined(TARGET_MTS_GAMBIT)
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);
fritz291 24:f4d432f36ec2 66 #if defined(TARGET_MTS_GAMBIT)
fritz291 21:b4d53cc6d6ac 67 initPins();
fritz291 21:b4d53cc6d6ac 68 #endif
fritz291 24:f4d432f36ec2 69 #if defined(TARGET_K64F)||defined(TARGET_MTS_GAMBIT)
va009039 16:981c3104f6c0 70 MPU->CESR=0;
va009039 16:981c3104f6c0 71 #endif
va009039 16:981c3104f6c0 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 18:61554f238584 154 USB_TEST_ASSERT(!(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 18:61554f238584 179 setAddr(dev->getAddress(), dev->getSpeed());
va009039 18:61554f238584 180
va009039 11:61843badd06e 181 for(int retry = 0;; retry++) {
va009039 11:61843badd06e 182 token_ready();
va009039 11:61843badd06e 183 USB0->ENDPOINT[0].ENDPT = (root_lowSpeed ? USB_ENDPT_HOSTWOHUB_MASK : 0x00) |
va009039 11:61843badd06e 184 USB_ENDPT_RETRYDIS_MASK|
va009039 11:61843badd06e 185 USB_ENDPT_EPRXEN_MASK|
va009039 11:61843badd06e 186 USB_ENDPT_EPTXEN_MASK|
va009039 11:61843badd06e 187 USB_ENDPT_EPHSHK_MASK;
va009039 11:61843badd06e 188 CTASSERT(sizeof(SETUP_PACKET) == 8);
va009039 11:61843badd06e 189 setup->wLength = wLength;
va009039 11:61843badd06e 190 int idx = EP0_BDT_IDX(TX, tx_ptr);
va009039 11:61843badd06e 191 bdt[idx].setBuffer((uint8_t*)setup, sizeof(SETUP_PACKET));
va009039 11:61843badd06e 192 bdt[idx].info = BD_OWN_MASK |
va009039 11:61843badd06e 193 BD_DTS_MASK; // always data0
va009039 11:61843badd06e 194 token_done = false;
va009039 11:61843badd06e 195 USB0->TOKEN = USB_TOKEN_TOKENPID(SETUP_TOKEN)|
va009039 11:61843badd06e 196 USB_TOKEN_TOKENENDPT(ep->getAddress() & 0x7f);
va009039 11:61843badd06e 197 while(!token_done);
va009039 11:61843badd06e 198 LastStatus = bdt[idx].getStatus();
va009039 11:61843badd06e 199 if (LastStatus == ACK) {
va009039 11:61843badd06e 200 if (retry > 0) {
va009039 11:61843badd06e 201 USB_DBG("retry=%d %02x", retry, prev_LastStatus);
va009039 11:61843badd06e 202 }
va009039 11:61843badd06e 203 break;
va009039 11:61843badd06e 204 } else if (LastStatus == STALL) {
va009039 11:61843badd06e 205 report.stall++;
va009039 11:61843badd06e 206 return STALL;
va009039 11:61843badd06e 207 }
va009039 11:61843badd06e 208 if (retry > 10) {
va009039 11:61843badd06e 209 USB_DBG("retry=%d", retry);
va009039 11:61843badd06e 210 break;
va009039 11:61843badd06e 211 }
va009039 11:61843badd06e 212 prev_LastStatus = LastStatus;
va009039 11:61843badd06e 213 wait_ms(100 * retry);
va009039 11:61843badd06e 214 }
va009039 11:61843badd06e 215 ep->setData01(DATA1); // next toggle
va009039 11:61843badd06e 216 return LastStatus;
va009039 11:61843badd06e 217 }
va009039 11:61843badd06e 218
va009039 11:61843badd06e 219 int USBHALHost::token_in(USBEndpoint* ep, uint8_t* data, int size, int retryLimit) {
va009039 18:61554f238584 220 USBDeviceConnected* dev = ep->getDevice();
va009039 18:61554f238584 221 setAddr(dev->getAddress(), dev->getSpeed());
va009039 18:61554f238584 222 setEndpoint();
va009039 18:61554f238584 223
va009039 11:61843badd06e 224 for(int retry = 0;; retry++) {
va009039 11:61843badd06e 225 token_ready();
va009039 11:61843badd06e 226 int idx = EP0_BDT_IDX(RX, rx_ptr);
va009039 11:61843badd06e 227 bdt[idx].setBuffer(data, size);
va009039 11:61843badd06e 228 bdt[idx].info = BD_OWN_MASK|
va009039 11:61843badd06e 229 BD_DTS_MASK|
va009039 11:61843badd06e 230 (ep->getData01() == DATA1 ? BD_DATA01_MASK : 0);
va009039 11:61843badd06e 231 token_done = false;
va009039 11:61843badd06e 232 USB0->TOKEN = USB_TOKEN_TOKENPID(IN_TOKEN)|
va009039 11:61843badd06e 233 USB_TOKEN_TOKENENDPT(ep->getAddress()&0x7f);
va009039 11:61843badd06e 234 while(!token_done);
va009039 11:61843badd06e 235 LastStatus = bdt[idx].getStatus();
va009039 11:61843badd06e 236 int len = bdt[idx].byte_count;
va009039 11:61843badd06e 237 if (LastStatus == DATA0 || LastStatus == DATA1) {
va009039 11:61843badd06e 238 USB_TEST_ASSERT(ep->getData01() == LastStatus);
va009039 11:61843badd06e 239 ep->setData01(LastStatus == DATA0 ? DATA1 : DATA0);
va009039 11:61843badd06e 240 if (retry > 0) {
va009039 11:61843badd06e 241 //USB_DBG("len=%d retry=%d %02x", len, retry, prev_LastStatus);
va009039 11:61843badd06e 242 }
va009039 11:61843badd06e 243 return len;
va009039 11:61843badd06e 244 } else if (LastStatus == STALL) {
va009039 11:61843badd06e 245 report.stall++;
va009039 11:61843badd06e 246 return -1;
va009039 11:61843badd06e 247 } else if (LastStatus == NAK) {
va009039 11:61843badd06e 248 report.nak++;
va009039 11:61843badd06e 249 if (retry >= retryLimit) {
va009039 11:61843badd06e 250 if (retryLimit > 0) {
va009039 11:61843badd06e 251 USB_DBG("retry=%d retryLimit=%d", retry, retryLimit);
va009039 11:61843badd06e 252 }
va009039 11:61843badd06e 253 return -1;
va009039 11:61843badd06e 254 }
va009039 11:61843badd06e 255 wait_ms(100 * retry);
va009039 11:61843badd06e 256 } else if (LastStatus == Bus_Timeout) {
va009039 11:61843badd06e 257 if (retry >= retryLimit) {
va009039 11:61843badd06e 258 if (retryLimit > 0) {
va009039 11:61843badd06e 259 USB_DBG("Bus_Timeout retry=%d retryLimit=%d", retry, retryLimit);
va009039 11:61843badd06e 260 }
va009039 11:61843badd06e 261 return -1;
va009039 11:61843badd06e 262 }
va009039 11:61843badd06e 263 wait_ms(500 + 100 * retry);
va009039 11:61843badd06e 264 } else {
va009039 11:61843badd06e 265 return -1;
va009039 11:61843badd06e 266 }
va009039 11:61843badd06e 267 prev_LastStatus = LastStatus;
va009039 11:61843badd06e 268 }
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 18:61554f238584 272 USBDeviceConnected* dev = ep->getDevice();
va009039 18:61554f238584 273 USB_TEST_ASSERT(dev);
va009039 18:61554f238584 274 setAddr(dev->getAddress(), dev->getSpeed());
va009039 18:61554f238584 275 setEndpoint();
va009039 18:61554f238584 276
va009039 11:61843badd06e 277 for(int retry = 0;; retry++) {
va009039 11:61843badd06e 278 token_ready();
va009039 11:61843badd06e 279 int idx = EP0_BDT_IDX(TX, tx_ptr);
va009039 11:61843badd06e 280 bdt[idx].setBuffer((uint8_t*)data, size);
va009039 11:61843badd06e 281 bdt[idx].info = BD_OWN_MASK|
va009039 11:61843badd06e 282 BD_DTS_MASK|
va009039 11:61843badd06e 283 (ep->getData01() == DATA1 ? BD_DATA01_MASK : 0);
va009039 11:61843badd06e 284 token_done = false;
va009039 11:61843badd06e 285 USB0->TOKEN = USB_TOKEN_TOKENPID(OUT_TOKEN)|
va009039 11:61843badd06e 286 USB_TOKEN_TOKENENDPT(ep->getAddress()&0x7f);
va009039 11:61843badd06e 287 while(!token_done);
va009039 11:61843badd06e 288 LastStatus = bdt[idx].getStatus();
va009039 11:61843badd06e 289 int len = bdt[idx].byte_count;
va009039 11:61843badd06e 290 //USB_DBG("len=%d %02x", len, LastStatus);
va009039 11:61843badd06e 291 if (LastStatus == ACK) {
va009039 11:61843badd06e 292 ep->toggleData01();
va009039 11:61843badd06e 293 if (retry > 0) {
va009039 11:61843badd06e 294 USB_DBG("len=%d retry=%d %02x", len, retry, prev_LastStatus);
va009039 11:61843badd06e 295 }
va009039 11:61843badd06e 296 return len;
va009039 11:61843badd06e 297 } else if (LastStatus == STALL) {
va009039 11:61843badd06e 298 report.stall++;
va009039 11:61843badd06e 299 return -1;
va009039 11:61843badd06e 300 } else if (LastStatus == NAK) {
va009039 11:61843badd06e 301 report.nak++;
va009039 11:61843badd06e 302 if (retry > retryLimit) {
va009039 11:61843badd06e 303 USB_DBG("retry=%d retryLimit=%d", retry, retryLimit);
va009039 11:61843badd06e 304 return -1;
va009039 11:61843badd06e 305 }
va009039 11:61843badd06e 306 wait_ms(100 * retry);
va009039 11:61843badd06e 307 } else {
va009039 11:61843badd06e 308 return -1;
va009039 11:61843badd06e 309 }
va009039 11:61843badd06e 310 prev_LastStatus = LastStatus;
va009039 11:61843badd06e 311 }
va009039 11:61843badd06e 312 }
va009039 11:61843badd06e 313
va009039 18:61554f238584 314 void USBHALHost::multi_token_inNB(USBEndpoint* ep, uint8_t* data, int size) {
va009039 18:61554f238584 315 USB_TRACE1(size);
va009039 18:61554f238584 316 USB_TEST_ASSERT(ep->getState() != USB_TYPE_PROCESSING);
va009039 18:61554f238584 317 ep->setBuffer(data, size);
va009039 18:61554f238584 318 ep->setState(USB_TYPE_PROCESSING);
va009039 18:61554f238584 319 }
va009039 18:61554f238584 320
va009039 18:61554f238584 321 USB_TYPE USBHALHost::multi_token_inNB_result(USBEndpoint* ep) {
va009039 18:61554f238584 322 USB_TEST_ASSERT(ep->getState() == USB_TYPE_PROCESSING);
va009039 18:61554f238584 323 uint8_t* buf = ep->getBufStart();
va009039 18:61554f238584 324 int size = ep->getBufSize();
va009039 18:61554f238584 325 int result = multi_token_in(ep, buf, size, false);
va009039 18:61554f238584 326 USB_TRACE1(result);
va009039 18:61554f238584 327 if (result < 0) {
va009039 18:61554f238584 328 return USB_TYPE_PROCESSING;
va009039 18:61554f238584 329 }
va009039 18:61554f238584 330 ep->setLengthTransferred(result);
va009039 18:61554f238584 331 ep->setState(USB_TYPE_IDLE);
va009039 18:61554f238584 332 return USB_TYPE_OK;
va009039 18:61554f238584 333 }
va009039 18:61554f238584 334
va009039 18:61554f238584 335 void USBHALHost::setToggle(USBEndpoint* ep, uint8_t toggle) {
va009039 18:61554f238584 336 USB_TEST_ASSERT(toggle == 1);
va009039 18:61554f238584 337 ep->setData01(toggle == 0 ? DATA0 : DATA1);
va009039 18:61554f238584 338 }
va009039 18:61554f238584 339
va009039 11:61843badd06e 340 int USBHALHost::token_iso_in(USBEndpoint* ep, uint8_t* data, int size) {
va009039 18:61554f238584 341 USBDeviceConnected* dev = ep->getDevice();
va009039 18:61554f238584 342 USB_TEST_ASSERT(dev);
va009039 18:61554f238584 343 setAddr(dev->getAddress());
va009039 18:61554f238584 344
va009039 11:61843badd06e 345 while(USB0->CTL & USB_CTL_TXSUSPENDTOKENBUSY_MASK); // TOKEN_BUSY ?
va009039 11:61843badd06e 346 USB0->ISTAT |= USB_ISTAT_SOFTOK_MASK; // Clear SOF
va009039 11:61843badd06e 347 while (!(USB0->ISTAT & USB_ISTAT_SOFTOK_MASK));
va009039 11:61843badd06e 348 USB0->SOFTHLD = 0; // this is needed as without this you can get errors
va009039 11:61843badd06e 349 USB0->ISTAT |= USB_ISTAT_SOFTOK_MASK; // clear SOF
va009039 11:61843badd06e 350
va009039 11:61843badd06e 351 USB0->ENDPOINT[0].ENDPT = USB_ENDPT_EPCTLDIS_MASK|
va009039 11:61843badd06e 352 USB_ENDPT_RETRYDIS_MASK|
va009039 11:61843badd06e 353 USB_ENDPT_EPRXEN_MASK|
va009039 11:61843badd06e 354 USB_ENDPT_EPTXEN_MASK;
va009039 11:61843badd06e 355 int idx = EP0_BDT_IDX(RX, rx_ptr);
va009039 11:61843badd06e 356 bdt[idx].setBuffer(data, size);
va009039 11:61843badd06e 357 bdt[idx].info = BD_OWN_MASK|
va009039 11:61843badd06e 358 BD_DTS_MASK; // always DATA0
va009039 11:61843badd06e 359 token_done = false;
va009039 11:61843badd06e 360 USB0->TOKEN = USB_TOKEN_TOKENPID(IN_TOKEN)|
va009039 11:61843badd06e 361 USB_TOKEN_TOKENENDPT(ep->getAddress()&0x7f);
va009039 11:61843badd06e 362 while(!token_done);
va009039 11:61843badd06e 363 LastStatus = bdt[idx].getStatus();
va009039 11:61843badd06e 364 int len = bdt[idx].byte_count;
va009039 11:61843badd06e 365 if (LastStatus == DATA0) {
va009039 11:61843badd06e 366 return len;
va009039 11:61843badd06e 367 }
va009039 11:61843badd06e 368 return -1;
va009039 11:61843badd06e 369 }
va009039 11:61843badd06e 370
va009039 11:61843badd06e 371 void USBHALHost::token_ready() {
va009039 11:61843badd06e 372 while(USB0->CTL & USB_CTL_TXSUSPENDTOKENBUSY_MASK) { // TOKEN_BUSY ?
va009039 11:61843badd06e 373 wait_ms(1);
va009039 11:61843badd06e 374 }
va009039 11:61843badd06e 375 USB0->ISTAT |= USB_ISTAT_SOFTOK_MASK; // Clear SOF
va009039 11:61843badd06e 376 while (!(USB0->ISTAT & USB_ISTAT_SOFTOK_MASK));
va009039 11:61843badd06e 377 USB0->SOFTHLD = 0; // this is needed as without this you can get errors
va009039 11:61843badd06e 378 USB0->ISTAT |= USB_ISTAT_SOFTOK_MASK; // clear SOF
va009039 11:61843badd06e 379 }
va009039 11:61843badd06e 380
va009039 16:981c3104f6c0 381 int USBHALHost::multi_token_in(USBEndpoint* ep, uint8_t* data, size_t total, bool block) {
va009039 16:981c3104f6c0 382 if (total == 0) {
va009039 16:981c3104f6c0 383 return token_in(ep);
va009039 16:981c3104f6c0 384 }
va009039 16:981c3104f6c0 385 int retryLimit = block ? 10 : 0;
va009039 16:981c3104f6c0 386 int read_len = 0;
va009039 16:981c3104f6c0 387 for(int n = 0; read_len < total; n++) {
va009039 16:981c3104f6c0 388 int size = std::min((int)total-read_len, ep->getSize());
va009039 16:981c3104f6c0 389 int result = token_in(ep, data+read_len, size, retryLimit);
va009039 16:981c3104f6c0 390 if (result < 0) {
va009039 16:981c3104f6c0 391 if (block) {
va009039 16:981c3104f6c0 392 return -1;
va009039 16:981c3104f6c0 393 }
va009039 16:981c3104f6c0 394 if (LastStatus == NAK) {
va009039 16:981c3104f6c0 395 if (n == 0) {
va009039 16:981c3104f6c0 396 return -1;
va009039 16:981c3104f6c0 397 }
va009039 16:981c3104f6c0 398 break;
va009039 16:981c3104f6c0 399 }
va009039 16:981c3104f6c0 400 return result;
va009039 16:981c3104f6c0 401 }
va009039 16:981c3104f6c0 402 read_len += result;
va009039 16:981c3104f6c0 403 if (result < ep->getSize()) {
va009039 16:981c3104f6c0 404 break;
va009039 16:981c3104f6c0 405 }
va009039 16:981c3104f6c0 406 }
va009039 16:981c3104f6c0 407 return read_len;
va009039 16:981c3104f6c0 408 }
va009039 16:981c3104f6c0 409
va009039 18:61554f238584 410 int USBHALHost::multi_token_out(USBEndpoint* ep, const uint8_t* data, size_t total) {
va009039 16:981c3104f6c0 411 if (total == 0) {
va009039 16:981c3104f6c0 412 return token_out(ep);
va009039 16:981c3104f6c0 413 }
va009039 16:981c3104f6c0 414 int write_len = 0;
va009039 16:981c3104f6c0 415 for(int n = 0; write_len < total; n++) {
va009039 16:981c3104f6c0 416 int size = std::min((int)total-write_len, ep->getSize());
va009039 16:981c3104f6c0 417 int result = token_out(ep, data+write_len, size);
va009039 16:981c3104f6c0 418 if (result < 0) {
va009039 16:981c3104f6c0 419 if (LastStatus == NAK) {
va009039 16:981c3104f6c0 420 if (n == 0) {
va009039 16:981c3104f6c0 421 return -1;
va009039 16:981c3104f6c0 422 }
va009039 16:981c3104f6c0 423 break;
va009039 16:981c3104f6c0 424 }
va009039 16:981c3104f6c0 425 USB_DBG("token_out result=%d %02x", result, LastStatus);
va009039 16:981c3104f6c0 426 return result;
va009039 16:981c3104f6c0 427 }
va009039 16:981c3104f6c0 428 write_len += result;
va009039 16:981c3104f6c0 429 if (result < ep->getSize()) {
va009039 16:981c3104f6c0 430 break;
va009039 16:981c3104f6c0 431 }
va009039 16:981c3104f6c0 432 }
va009039 16:981c3104f6c0 433 return write_len;
va009039 16:981c3104f6c0 434 }
va009039 16:981c3104f6c0 435
va009039 11:61843badd06e 436 void USBHALHost::_usbisr(void) {
va009039 11:61843badd06e 437 if (instHost) {
va009039 11:61843badd06e 438 instHost->UsbIrqhandler();
va009039 11:61843badd06e 439 }
va009039 11:61843badd06e 440 }
va009039 11:61843badd06e 441
va009039 11:61843badd06e 442 void USBHALHost::UsbIrqhandler() {
va009039 11:61843badd06e 443 if (USB0->ISTAT & USB_ISTAT_TOKDNE_MASK) {
va009039 11:61843badd06e 444 uint8_t stat = USB0->STAT;
va009039 11:61843badd06e 445 ODD_EVEN next_ptr = (stat & USB_STAT_ODD_MASK) ? ODD : EVEN;
va009039 11:61843badd06e 446 if (stat & USB_STAT_TX_MASK) {
va009039 11:61843badd06e 447 tx_ptr = next_ptr;
va009039 11:61843badd06e 448 } else {
va009039 11:61843badd06e 449 rx_ptr = next_ptr;
va009039 11:61843badd06e 450 }
va009039 11:61843badd06e 451 USB0->ISTAT = USB_ISTAT_TOKDNE_MASK;
va009039 11:61843badd06e 452 token_done = true;
va009039 11:61843badd06e 453 }
va009039 11:61843badd06e 454 if (USB0->ISTAT & USB_ISTAT_ATTACH_MASK) {
va009039 11:61843badd06e 455 USB0->INTEN &= ~USB_INTEN_ATTACHEN_MASK;
va009039 11:61843badd06e 456 USB0->ISTAT = USB_ISTAT_ATTACH_MASK;
va009039 11:61843badd06e 457 attach_done = true;
va009039 11:61843badd06e 458 }
va009039 11:61843badd06e 459 if (USB0->ISTAT & USB_ISTAT_ERROR_MASK) {
va009039 11:61843badd06e 460 uint8_t errstat = USB0->ERRSTAT;
va009039 11:61843badd06e 461 if (errstat & USB_ERRSTAT_PIDERR_MASK) {
va009039 11:61843badd06e 462 report.errstat_piderr++;
va009039 11:61843badd06e 463 }
va009039 11:61843badd06e 464 if (errstat & USB_ERRSTAT_CRC5EOF_MASK) {
va009039 11:61843badd06e 465 report.errstat_crc5eof++;
va009039 11:61843badd06e 466 }
va009039 11:61843badd06e 467 if (errstat & USB_ERRSTAT_CRC16_MASK) {
va009039 11:61843badd06e 468 report.errstat_crc16++;
va009039 11:61843badd06e 469 }
va009039 11:61843badd06e 470 if (errstat & USB_ERRSTAT_DFN8_MASK) {
va009039 11:61843badd06e 471 report.errstat_dfn8++;
va009039 11:61843badd06e 472 }
va009039 11:61843badd06e 473 if (errstat & USB_ERRSTAT_BTOERR_MASK) {
va009039 11:61843badd06e 474 report.errstat_btoerr++;
va009039 11:61843badd06e 475 }
va009039 11:61843badd06e 476 if (errstat & USB_ERRSTAT_DMAERR_MASK) {
va009039 11:61843badd06e 477 report.errstat_dmaerr++;
va009039 11:61843badd06e 478 }
va009039 11:61843badd06e 479 if (errstat & USB_ERRSTAT_BTSERR_MASK) {
va009039 11:61843badd06e 480 report.errstat_btoerr++;
va009039 11:61843badd06e 481 }
va009039 11:61843badd06e 482 USB0->ERRSTAT = errstat;
va009039 11:61843badd06e 483 USB0->ISTAT = USB_ISTAT_ERROR_MASK;
va009039 11:61843badd06e 484 }
va009039 11:61843badd06e 485 }
va009039 11:61843badd06e 486
va009039 11:61843badd06e 487 void Report::clear() {
va009039 11:61843badd06e 488 errstat_piderr = 0;
va009039 11:61843badd06e 489 errstat_crc5eof = 0;
va009039 11:61843badd06e 490 errstat_crc16 = 0;
va009039 11:61843badd06e 491 errstat_dfn8 = 0;
va009039 11:61843badd06e 492 errstat_btoerr = 0;
va009039 11:61843badd06e 493 errstat_dmaerr = 0;
va009039 11:61843badd06e 494 errstat_bsterr = 0;
va009039 11:61843badd06e 495 //
va009039 11:61843badd06e 496 nak = 0;
va009039 11:61843badd06e 497 }
va009039 11:61843badd06e 498
va009039 11:61843badd06e 499 void Report::print_errstat() {
va009039 11:61843badd06e 500 printf("ERRSTAT PID: %d, CRC5EOF: %d, CRC16: %d, DFN8: %d, BTO: %d, DMA: %d, BST: %d\n",
va009039 11:61843badd06e 501 errstat_piderr, errstat_crc5eof,
va009039 11:61843badd06e 502 errstat_crc16, errstat_dfn8,
va009039 11:61843badd06e 503 errstat_btoerr, errstat_dmaerr, errstat_bsterr);
va009039 11:61843badd06e 504 }
va009039 11:61843badd06e 505
va009039 11:61843badd06e 506 void debug_hex(uint8_t* buf, int size) {
va009039 11:61843badd06e 507 int n = 0;
va009039 11:61843badd06e 508 for(int i = 0; i < size; i++) {
va009039 11:61843badd06e 509 fprintf(stderr, "%02x ", buf[i]);
va009039 11:61843badd06e 510 if (++n >= 16) {
va009039 11:61843badd06e 511 fprintf(stderr, "\n");
va009039 11:61843badd06e 512 n = 0;
va009039 11:61843badd06e 513 }
va009039 11:61843badd06e 514 }
va009039 11:61843badd06e 515 if (n > 0) {
va009039 11:61843badd06e 516 fprintf(stderr, "\n");
va009039 11:61843badd06e 517 }
va009039 11:61843badd06e 518 }
va009039 11:61843badd06e 519
fritz291 21:b4d53cc6d6ac 520 void USBHALHost::initPins()
fritz291 21:b4d53cc6d6ac 521 {
fritz291 21:b4d53cc6d6ac 522 //Start with usb hub disabled in reset mode
fritz291 21:b4d53cc6d6ac 523 DigitalOut usb_hub_nreset(PTA8,0);
fritz291 21:b4d53cc6d6ac 524 //cell_nreset resets the cell radio
fritz291 21:b4d53cc6d6ac 525 DigitalOut cell_nreset(PTA2,0);
fritz291 21:b4d53cc6d6ac 526 printf("Resetting the radio to prevent processor freeze....\n");
fritz291 21:b4d53cc6d6ac 527 printf("Start with USB hub disabled to prevent any USB transaction with Telit Module\n");
fritz291 21:b4d53cc6d6ac 528 wait(1);
fritz291 21:b4d53cc6d6ac 529 cell_nreset = 1;
fritz291 21:b4d53cc6d6ac 530 wait(4);
fritz291 21:b4d53cc6d6ac 531 //turn hub on
fritz291 21:b4d53cc6d6ac 532 usb_hub_nreset = 1;
fritz291 21:b4d53cc6d6ac 533 printf("turning USB hub back on\n");
fritz291 21:b4d53cc6d6ac 534 wait(1);
fritz291 21:b4d53cc6d6ac 535 }
fritz291 21:b4d53cc6d6ac 536
va009039 11:61843badd06e 537 #endif
va009039 11:61843badd06e 538
va009039 11:61843badd06e 539