Axeda Ready Demo for Freescale FRDM-KL46Z as accident alert system

Dependencies:   FRDM_MMA8451Q KL46Z-USBHost MAG3110 SocketModem TSI mbed FATFileSystem

Fork of AxedaGo-Freescal_FRDM-KL46Z revert by Axeda Corp

Committer:
AxedaCorp
Date:
Tue Jul 01 21:31:54 2014 +0000
Revision:
0:65004368569c
Made initial

Who changed what in which revision?

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