1 /* 2 * Ben NanoNote and Arduino USB Host shield communication. 3 * 4 * Copyright 2013 Paul Boddie 5 * 6 * SPI functions derived from those in lib/atben.c by Werner Almesberger: 7 * 8 * Copyright 2010-2011 Werner Almesberger 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 */ 15 16 #include <ubb/ubb.h> 17 #include <stdio.h> 18 #include <signal.h> 19 #include <stdlib.h> 20 #include <usb.h> 21 #include <unistd.h> 22 #include <string.h> 23 #include <endian.h> 24 25 /* Found in Python's asdl.h. */ 26 27 #ifndef __cplusplus 28 typedef enum {false, true} bool; 29 #endif 30 31 /* Initialisation states. */ 32 33 typedef enum 34 { 35 MAX_DEVSTATE_INIT = 0, 36 MAX_DEVSTATE_CONNECTED, 37 MAX_DEVSTATE_START, 38 MAX_DEVSTATE_RESET, 39 MAX_DEVSTATE_INSPECTED, 40 MAX_DEVSTATE_RESET_AGAIN, 41 MAX_DEVSTATE_ADDRESSING, 42 MAX_DEVSTATE_READY 43 } max_devstate; 44 45 /* Device details. */ 46 47 typedef struct 48 { 49 bool in_toggle, out_toggle; 50 struct usb_device_descriptor desc; 51 uint8_t address, max_packet_size; 52 } max_device; 53 54 static uint16_t next_address = 1; 55 56 /* Pin assignments: 57 * 58 * Sniffer UBB Shield 59 * ------- ---- ------ 60 * DAT2 DAT2 9 (INT) 61 * CD DAT3 10 (SS) 62 * CMD CMD 7 (RESET) 63 * VCC VDD VIN 64 * CLK CLK 13 (SCLK) 65 * GND GND GND 66 * DAT0 DAT0 11 (MOSI) 67 * DAT1 DAT1 12 (MISO) 68 * 8 (GPX) (not assigned) 69 */ 70 71 #define MAX_RESET UBB_CMD 72 #define MAX_SCLK UBB_CLK 73 #define MAX_MOSI UBB_DAT0 74 #define MAX_MISO UBB_DAT1 75 #define MAX_INT UBB_DAT2 76 #define MAX_SS UBB_DAT3 77 78 /* MAX3421E definitions. */ 79 80 #define MAX_REG_READ 0x00 81 #define MAX_REG_WRITE 0x02 82 83 #define MAX_REG_RCVFIFO 1 84 #define MAX_REG_SNDFIFO 2 85 #define MAX_REG_SUDFIFO 4 86 #define MAX_REG_RCVBC 6 87 #define MAX_REG_SNDBC 7 88 #define MAX_REG_USBIRQ 13 89 #define MAX_REG_USBIEN 14 90 #define MAX_REG_USBCTL 15 91 #define MAX_REG_CPUCTL 16 92 #define MAX_REG_PINCTL 17 93 #define MAX_REG_REVISION 18 94 #define MAX_REG_HIRQ 25 95 #define MAX_REG_HIEN 26 96 #define MAX_REG_MODE 27 97 #define MAX_REG_PERADDR 28 98 #define MAX_REG_HCTL 29 99 #define MAX_REG_HXFR 30 100 #define MAX_REG_HRSL 31 101 102 #define MAX_USBIRQ_OSCOKIRQ 1 103 #define MAX_USBIRQ_NOVBUSIRQ 32 104 #define MAX_USBIRQ_VBUSIRQ 64 105 106 #define MAX_USBCTL_PWRDOWN 16 107 #define MAX_USBCTL_CHIPRES 32 108 109 #define MAX_CPUCTL_IE 1 110 111 #define MAX_PINCTL_POSINT_LOW 0 112 #define MAX_PINCTL_POSINT_HIGH 4 113 #define MAX_PINCTL_INTLEVEL_EDGE 0 114 #define MAX_PINCTL_INTLEVEL_LEVEL 8 115 #define MAX_PINCTL_FDUPSPI_HALF 0 116 #define MAX_PINCTL_FDUPSPI_FULL 16 117 118 #define MAX_HIRQ_BUSEVENTIRQ 1 119 #define MAX_HIRQ_RWUIRQ 2 120 #define MAX_HIRQ_RCVDAVIRQ 4 121 #define MAX_HIRQ_SNDBAVIRQ 8 122 #define MAX_HIRQ_SUSDNIRQ 16 123 #define MAX_HIRQ_CONDETIRQ 32 124 #define MAX_HIRQ_FRAMEIRQ 64 125 #define MAX_HIRQ_HXFRDNIRQ 128 126 127 #define MAX_HIEN_BUSEVENTIE 1 128 #define MAX_HIEN_CONDETIE 32 129 #define MAX_HIEN_FRAMEIE 64 130 131 #define MAX_MODE_PERIPHERAL 0 132 #define MAX_MODE_HOST 1 133 #define MAX_MODE_LOWSPEED 2 134 #define MAX_MODE_SOFKAENAB 8 135 #define MAX_MODE_SEPIRQ_OFF 0 136 #define MAX_MODE_SEPIRQ_ON 16 137 #define MAX_MODE_DMPULLDN 64 138 #define MAX_MODE_DPPULLDN 128 139 140 #define MAX_MODE_HOST_ENABLED (MAX_MODE_HOST | MAX_MODE_SEPIRQ_OFF | MAX_MODE_DMPULLDN | MAX_MODE_DPPULLDN) 141 #define MAX_MODE_HOST_ACTIVE (MAX_MODE_HOST_ENABLED | MAX_MODE_SOFKAENAB) 142 #define MAX_MODE_HOST_FULLSPEED MAX_MODE_HOST_ACTIVE 143 #define MAX_MODE_HOST_LOWSPEED (MAX_MODE_HOST_ACTIVE | MAX_MODE_LOWSPEED) 144 145 #define MAX_HCTL_BUSRST 1 146 #define MAX_HCTL_SAMPLEBUS 4 147 #define MAX_HCTL_RCVTOG0 16 148 #define MAX_HCTL_RCVTOG1 32 149 #define MAX_HCTL_SNDTOG0 64 150 #define MAX_HCTL_SNDTOG1 128 151 152 #define MAX_HXFR_SETUP 16 153 #define MAX_HXFR_OUTNIN 32 154 #define MAX_HXFR_HS 128 155 156 #define MAX_HRSL_JSTATUS 128 157 #define MAX_HRSL_KSTATUS 64 158 #define MAX_HRSL_SNDTOGRD 32 159 #define MAX_HRSL_RCVTOGRD 16 160 #define MAX_HRSL_HRSLT 15 161 162 #define max_reg(n) ((uint8_t) (n << 3)) 163 #define max_reg_read(n) (max_reg(n) | MAX_REG_READ) 164 #define max_reg_write(n) (max_reg(n) | MAX_REG_WRITE) 165 166 #define usb_descriptor_type(n) ((uint16_t) (n << 8)) 167 168 void spi_begin() 169 { 170 CLR(MAX_SS); 171 } 172 173 void spi_end() 174 { 175 SET(MAX_SS); 176 } 177 178 /** 179 * Send the given value via MOSI while receiving a value via MISO. 180 * This requires full-duplex SPI and will produce a status value for the first 181 * value sent (the command). 182 */ 183 uint8_t spi_sendrecv(uint8_t v) 184 { 185 uint8_t result = 0; 186 uint8_t mask; 187 188 for (mask = 0x80; mask; mask >>= 1) 189 { 190 if (v & mask) 191 { 192 #ifdef DEBUG 193 printf("1"); 194 #endif 195 SET(MAX_MOSI); 196 } 197 else 198 { 199 #ifdef DEBUG 200 printf("0"); 201 #endif 202 CLR(MAX_MOSI); 203 } 204 205 /* Wait for stable output signal. */ 206 207 SET(MAX_SCLK); 208 209 if (PIN(MAX_MISO)) 210 result |= mask; 211 212 CLR(MAX_SCLK); 213 } 214 215 #ifdef DEBUG 216 printf("\n"); 217 #endif 218 return result; 219 } 220 221 uint8_t max_read(uint8_t reg, uint8_t *status) 222 { 223 uint8_t result = 0, tmpstatus = 0; 224 225 tmpstatus = 0; 226 227 spi_begin(); 228 tmpstatus = spi_sendrecv(max_reg_read(reg)); 229 result = spi_sendrecv(0); 230 spi_end(); 231 232 if (status != NULL) 233 *status = tmpstatus; 234 235 return result; 236 } 237 238 uint8_t max_write(uint8_t reg, uint8_t value) 239 { 240 uint8_t status = 0; 241 242 spi_begin(); 243 status = spi_sendrecv(max_reg_write(reg)); 244 spi_sendrecv(value); 245 spi_end(); 246 247 return status; 248 } 249 250 /** 251 * Return whether data can be sent. 252 */ 253 bool max_can_send(uint8_t *status) 254 { 255 if (status == NULL) 256 return max_read(MAX_REG_HIRQ, NULL) & MAX_HIRQ_SNDBAVIRQ; 257 else 258 return *status & MAX_HIRQ_SNDBAVIRQ; 259 } 260 261 /** 262 * Set the sending data toggle. 263 */ 264 void max_set_send_toggle(bool toggle) 265 { 266 max_write(MAX_REG_HCTL, toggle ? MAX_HCTL_SNDTOG1 : MAX_HCTL_SNDTOG0); 267 } 268 269 /** 270 * Return the sending data toggle. 271 */ 272 bool max_get_send_toggle() 273 { 274 return (max_read(MAX_REG_HRSL, NULL) & MAX_HRSL_SNDTOGRD) != 0; 275 } 276 277 /** 278 * Set the receiving data toggle. 279 */ 280 void max_set_recv_toggle(bool toggle) 281 { 282 max_write(MAX_REG_HCTL, toggle ? MAX_HCTL_RCVTOG1 : MAX_HCTL_RCVTOG0); 283 } 284 285 /** 286 * Return the receiving data toggle. 287 */ 288 bool max_get_recv_toggle() 289 { 290 return (max_read(MAX_REG_HRSL, NULL) & MAX_HRSL_RCVTOGRD) != 0; 291 } 292 293 /** 294 * Wait for handshake/timeout after a transfer. 295 */ 296 uint8_t max_wait_transfer(uint8_t status) 297 { 298 while (!(status & MAX_HIRQ_HXFRDNIRQ)) 299 { 300 status = max_read(MAX_REG_HIRQ, NULL); 301 } 302 303 max_write(MAX_REG_HIRQ, MAX_HIRQ_HXFRDNIRQ); 304 return max_read(MAX_REG_HIRQ, NULL); 305 } 306 307 /** 308 * Write the given data to the FIFO. 309 */ 310 void max_write_fifo(uint8_t endpoint, uint8_t *data, uint8_t len) 311 { 312 uint8_t count; 313 314 for (count = 0; count < len; count++) 315 { 316 max_write(endpoint ? MAX_REG_SNDFIFO : MAX_REG_SUDFIFO, data[count]); 317 } 318 319 if (endpoint) 320 max_write(MAX_REG_SNDBC, len); 321 } 322 323 /** 324 * Read the data from the FIFO. 325 */ 326 void max_read_fifo(uint8_t **data, uint8_t *len, uint8_t *datalimit) 327 { 328 uint8_t count, received = max_read(MAX_REG_RCVBC, NULL); 329 printf("Received %d bytes.\n", received); 330 331 *len += received; 332 333 for (count = 0; (count < received) && (*data < datalimit); count++) 334 { 335 *((*data)++) = max_read(MAX_REG_RCVFIFO, NULL); 336 } 337 } 338 339 /** 340 * Send a request to the given endpoint, using the supplied data payload with 341 * the given length, indicating the preserved toggle state of the endpoint 342 * (which will be updated). 343 */ 344 uint8_t max_send(uint8_t endpoint, uint8_t *data, uint8_t len, bool *toggle) 345 { 346 uint8_t status, hrsl = 0; 347 348 max_write_fifo(endpoint, data, len); 349 350 max_set_send_toggle(*toggle); 351 352 /* Initiate the transfer. */ 353 354 do 355 { 356 status = max_write(MAX_REG_HXFR, endpoint | MAX_HXFR_OUTNIN); 357 status = max_wait_transfer(status); 358 359 /* Test for usable data. */ 360 361 if (status & MAX_HIRQ_SNDBAVIRQ) 362 { 363 hrsl = max_read(MAX_REG_HRSL, &status); 364 365 if (!(hrsl & MAX_HRSL_HRSLT)) 366 break; 367 } 368 } 369 while (true); 370 371 *toggle = max_get_send_toggle(); 372 373 return status; 374 } 375 376 /** 377 * Make a request for data from the given endpoint, collecting it in the 378 * supplied buffer with the given length, indicating the preserved toggle state 379 * of the endpoint (which will be updated). The length will be updated to 380 * indicate the total length of the received data. 381 */ 382 bool max_recv(uint8_t endpoint, uint8_t *data, uint8_t *len, bool *toggle) 383 { 384 uint8_t *datalimit = data + *len; 385 uint8_t status, hrsl = 0; 386 uint16_t attempt = 1024; 387 388 max_set_recv_toggle(*toggle); 389 390 /* Initiate the transfer. */ 391 392 do 393 { 394 status = max_write(MAX_REG_HXFR, endpoint); 395 status = max_wait_transfer(status); 396 397 /* Test for usable data. */ 398 399 hrsl = max_read(MAX_REG_HRSL, &status); 400 401 attempt--; 402 } 403 while ((hrsl & MAX_HRSL_HRSLT) && attempt); 404 405 if (!attempt) 406 { 407 printf("HRSL is %x\n", hrsl); 408 return false; 409 } 410 411 while (status & MAX_HIRQ_RCVDAVIRQ) 412 { 413 max_read_fifo(&data, len, datalimit); 414 415 /* Indicate that all data has been read. */ 416 417 max_write(MAX_REG_HIRQ, MAX_HIRQ_RCVDAVIRQ); 418 status = max_read(MAX_REG_HIRQ, NULL); 419 } 420 421 *toggle = max_get_recv_toggle(); 422 423 return true; 424 } 425 426 /** 427 * Send a control request consisting of the given setup data. 428 */ 429 bool max_control(uint8_t *setup) 430 { 431 uint8_t status, hrsl, timer = 255; 432 433 max_write_fifo(0, setup, 8); 434 435 /* Initiate the transfer. */ 436 437 do 438 { 439 status = max_write(MAX_REG_HXFR, MAX_HXFR_SETUP); 440 status = max_wait_transfer(status); 441 hrsl = max_read(MAX_REG_HRSL, &status); 442 printf("HRSL = %x\n", hrsl); 443 } 444 while ((hrsl & MAX_HRSL_HRSLT) && (--timer)); 445 446 return !(hrsl & MAX_HRSL_HRSLT); 447 } 448 449 bool max_control_input(uint8_t *data, uint8_t *len, max_device *device) 450 { 451 device->in_toggle = true; 452 return max_recv(0, data, len, &device->in_toggle); 453 } 454 455 /** 456 * Perform a status transaction as part of a larger control transaction. 457 * The out parameter is used to indicate the kind of status transfer to be 458 * performed and should be the inverse of the control transfer direction. 459 */ 460 uint8_t max_control_status(bool out) 461 { 462 uint8_t status, hrsl; 463 464 do 465 { 466 status = max_write(MAX_REG_HXFR, MAX_HXFR_HS | (out ? MAX_HXFR_OUTNIN : 0)); 467 status = max_wait_transfer(status); 468 hrsl = max_read(MAX_REG_HRSL, &status); 469 } 470 while (hrsl & MAX_HRSL_HRSLT); 471 472 return status; 473 } 474 475 void chipreset() 476 { 477 printf("Resetting...\n"); 478 max_write(MAX_REG_USBCTL, MAX_USBCTL_CHIPRES); 479 480 printf("Clearing the reset...\n"); 481 max_write(MAX_REG_USBCTL, 0); 482 } 483 484 void busreset() 485 { 486 max_write(MAX_REG_HCTL, MAX_HCTL_BUSRST); 487 } 488 489 bool bus_is_reset() 490 { 491 return !(max_read(MAX_REG_HCTL, NULL) & MAX_HCTL_BUSRST); 492 } 493 494 uint8_t check() 495 { 496 uint8_t oscillator; 497 498 oscillator = max_read(MAX_REG_USBIRQ, NULL); 499 500 return (oscillator & ~(MAX_USBIRQ_NOVBUSIRQ | MAX_USBIRQ_VBUSIRQ)) == MAX_USBIRQ_OSCOKIRQ; 501 } 502 503 uint8_t wait() 504 { 505 uint16_t timeout = 1024; 506 507 /* Wait for the oscillator before performing USB activity. */ 508 509 printf("Waiting...\n"); 510 511 while ((timeout > 0) && (!check())) 512 { 513 timeout--; 514 } 515 516 printf("Iterations remaining: %d\n", timeout); 517 518 return timeout; 519 } 520 521 /** 522 * Return whether the bus is ready to be sampled. The application note claims 523 * that the SAMPLEBUS bit is cleared, but this does not seem to be the case. 524 */ 525 uint8_t samplebusready() 526 { 527 uint8_t result; 528 529 result = max_read(MAX_REG_HCTL, NULL); 530 return result & MAX_HCTL_SAMPLEBUS; 531 } 532 533 void samplebus() 534 { 535 max_write(MAX_REG_HCTL, MAX_HCTL_SAMPLEBUS); 536 while (!samplebusready()); 537 } 538 539 /** 540 * Handle the connection or disconnection of a device, returning true if the 541 * device is now connected or false otherwise. If the device is connected, a 542 * bus reset is performed. 543 */ 544 bool devicechanged() 545 { 546 uint8_t hrsl, mode; 547 548 hrsl = max_read(MAX_REG_HRSL, NULL); 549 mode = max_read(MAX_REG_MODE, NULL); 550 551 if ((hrsl & MAX_HRSL_JSTATUS) && (hrsl & MAX_HRSL_KSTATUS)) 552 { 553 printf("Bad device status.\n"); 554 } 555 else if (!(hrsl & MAX_HRSL_JSTATUS) && !(hrsl & MAX_HRSL_KSTATUS)) 556 { 557 printf("Device disconnected.\n"); 558 max_write(MAX_REG_MODE, MAX_MODE_HOST_ENABLED); 559 } 560 else 561 { 562 printf("Device connected.\n"); 563 564 /* Low speed device when J and lowspeed have the same level. 565 Since J and K should have opposing levels, K can be tested when 566 lowspeed is low. */ 567 568 if (((hrsl & MAX_HRSL_JSTATUS) && (mode & MAX_MODE_LOWSPEED)) || 569 ((hrsl & MAX_HRSL_KSTATUS) && !(mode & MAX_MODE_LOWSPEED))) 570 { 571 printf("Device is low speed.\n"); 572 if (max_read(MAX_REG_MODE, NULL) != MAX_MODE_HOST_LOWSPEED) 573 max_write(MAX_REG_MODE, MAX_MODE_HOST_LOWSPEED); 574 } 575 else 576 { 577 printf("Device is full speed.\n"); 578 if (max_read(MAX_REG_MODE, NULL) != MAX_MODE_HOST_FULLSPEED) 579 max_write(MAX_REG_MODE, MAX_MODE_HOST_FULLSPEED); 580 } 581 582 return true; 583 } 584 585 return false; 586 } 587 588 /** 589 * Initialise a USB control request setup payload. 590 */ 591 void setup_packet(uint8_t *setup, uint8_t request_type, uint8_t request, uint16_t value, uint16_t index, uint16_t length) 592 { 593 setup[0] = request_type; 594 setup[1] = request; 595 setup[2] = value & 0xff; 596 setup[3] = value >> 8; 597 setup[4] = index & 0xff; 598 setup[5] = index >> 8; 599 setup[6] = length & 0xff; 600 setup[7] = length >> 8; 601 } 602 603 uint8_t usb_descriptor_size(uint8_t type) 604 { 605 switch (type) 606 { 607 case USB_DT_DEVICE: return USB_DT_DEVICE_SIZE; 608 case USB_DT_CONFIG: return USB_DT_CONFIG_SIZE; 609 case USB_DT_STRING: return sizeof(struct usb_string_descriptor); 610 case USB_DT_INTERFACE: return USB_DT_INTERFACE_SIZE; 611 case USB_DT_ENDPOINT: return USB_DT_ENDPOINT_SIZE; 612 default: return 0; 613 } 614 } 615 616 /** 617 * Get a descriptor from the device having the given type, value and index. 618 * The descriptor buffer is static and must be copied if it is to be preserved. 619 */ 620 uint8_t *max_get_descriptor(max_device *device, uint8_t type, uint8_t value, uint8_t index, bool initial) 621 { 622 static uint8_t data[64]; 623 uint8_t len = 64, setup[8]; 624 625 printf("Sending descriptor request to address %d, endpoint 0...\n", device->address); 626 627 max_write(MAX_REG_PERADDR, device->address); 628 629 setup_packet(setup, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, usb_descriptor_type(type) | value, index, 630 initial ? 8 : 64); 631 632 if (!max_control(setup)) 633 return NULL; 634 635 if (!max_control_input(data, &len, device)) 636 { 637 printf("Failed.\n"); 638 return NULL; 639 } 640 max_control_status(true); 641 642 if ((initial && (len >= 8)) || (len >= usb_descriptor_size(type))) 643 return data; 644 else 645 { 646 printf("Expected %d but received %d.\n", usb_descriptor_size(type), len); 647 return NULL; 648 } 649 } 650 651 /** 652 * Perform initialisation on a device, obtaining the device details and storing 653 * this information in the device structure, returning true if successful and 654 * false otherwise. 655 */ 656 bool max_init_device(max_device *device) 657 { 658 struct usb_device_descriptor *desc; 659 660 printf("Sending control request to address 0, endpoint 0...\n"); 661 662 device->address = 0; 663 664 /* Send a "get descriptor" request for the device descriptor. */ 665 666 desc = (struct usb_device_descriptor *) max_get_descriptor(device, USB_DT_DEVICE, 0, 0, true); 667 668 if (desc != NULL) 669 { 670 device->max_packet_size = device->desc.bMaxPacketSize0; 671 busreset(); 672 return true; 673 } 674 675 return false; 676 } 677 678 bool max_complete_device(max_device *device) 679 { 680 struct usb_device_descriptor *desc; 681 682 printf("Sending control request to address 0, endpoint 0...\n"); 683 684 device->address = 0; 685 686 /* Send a "get descriptor" request for the device descriptor. */ 687 688 desc = (struct usb_device_descriptor *) max_get_descriptor(device, USB_DT_DEVICE, 0, 0, false); 689 690 if (desc != NULL) 691 { 692 memcpy(&device->desc, desc, sizeof(struct usb_device_descriptor)); 693 device->desc.bcdUSB = le16toh(device->desc.bcdUSB); 694 device->desc.idVendor = le16toh(device->desc.idVendor); 695 device->desc.idProduct = le16toh(device->desc.idProduct); 696 device->desc.bcdDevice = le16toh(device->desc.bcdDevice); 697 698 printf("bLength: %d\n", device->desc.bLength); 699 printf("bDescriptorType: %d\n", device->desc.bDescriptorType); 700 printf("bcdUSB: %04x\n", device->desc.bcdUSB); 701 printf("bDeviceClass: %d\n", device->desc.bDeviceClass); 702 printf("bDeviceSubClass: %d\n", device->desc.bDeviceSubClass); 703 printf("bDeviceProtocol: %d\n", device->desc.bDeviceProtocol); 704 printf("bMaxPacketSize0: %d\n", device->desc.bMaxPacketSize0); 705 printf("idVendor: %04x\n", device->desc.idVendor); 706 printf("idProduct: %04x\n", device->desc.idProduct); 707 printf("bcdDevice: %04x\n", device->desc.bcdDevice); 708 printf("iManufacturer: %d\n", device->desc.iManufacturer); 709 printf("iProduct: %d\n", device->desc.iProduct); 710 printf("iSerialNumber: %d\n", device->desc.iSerialNumber); 711 printf("bNumConfigurations: %d\n", device->desc.bNumConfigurations); 712 713 return true; 714 } 715 716 return false; 717 } 718 719 /** 720 * Assign a new address to the given device. 721 */ 722 void max_set_address(max_device *device) 723 { 724 uint8_t setup[8]; 725 726 max_write(MAX_REG_PERADDR, 0); 727 728 device->address = next_address++; 729 730 printf("Setting device address to %d...\n", device->address); 731 732 setup_packet(setup, USB_ENDPOINT_OUT, USB_REQ_SET_ADDRESS, device->address, 0, 0); 733 max_control(setup); 734 max_control_status(false); 735 } 736 737 void usb_show_languages(uint8_t *data) 738 { 739 struct usb_string_descriptor *desc = (struct usb_string_descriptor *) data; 740 uint16_t *lang; 741 742 if (data == NULL) 743 return; 744 745 printf("bLength: %d\n", desc->bLength); 746 printf("bDescriptorType: %d\n", desc->bDescriptorType); 747 748 for (lang = desc->wData; lang < (uint16_t *) (data + desc->bLength); lang++) 749 { 750 printf("wLangId: %04x\n", le16toh(*lang)); 751 } 752 } 753 754 void usb_show_configuration(uint8_t *data) 755 { 756 struct usb_config_descriptor *conf = (struct usb_config_descriptor *) data; 757 uint8_t *current, *last, total = le16toh(conf->wTotalLength); 758 struct usb_descriptor_header *desc; 759 struct usb_interface_descriptor *intf; 760 struct usb_endpoint_descriptor *endp; 761 762 if (data == NULL) 763 return; 764 765 printf("bLength: %d\n", conf->bLength); 766 printf("bDescriptorType: %d\n", conf->bDescriptorType); 767 printf("wTotalLength: %d\n", total); 768 printf("bNumInterfaces: %d\n", conf->bNumInterfaces); 769 printf("bConfigurationValue: %d\n", conf->bConfigurationValue); 770 printf("iConfiguration: %d\n", conf->iConfiguration); 771 printf("bmAttributes: %x\n", conf->bmAttributes); 772 printf("MaxPower: %d\n", conf->MaxPower); 773 774 current = ((uint8_t *) conf) + conf->bLength; 775 last = ((uint8_t *) conf) + total; 776 777 while (current < last) 778 { 779 desc = (struct usb_descriptor_header *) current; 780 printf("bLength: %d\n", desc->bLength); 781 printf("bDescriptorType: %d\n", desc->bDescriptorType); 782 783 switch (desc->bDescriptorType) 784 { 785 case USB_DT_INTERFACE: 786 intf = (struct usb_interface_descriptor *) current; 787 printf("bInterfaceNumber: %d\n", intf->bInterfaceNumber); 788 printf("bAlternateSetting: %d\n", intf->bAlternateSetting); 789 printf("bNumEndpoints: %d\n", intf->bNumEndpoints); 790 printf("bInterfaceClass: %x\n", intf->bInterfaceClass); 791 printf("bInterfaceSubClass: %x\n", intf->bInterfaceSubClass); 792 printf("bInterfaceProtocol: %x\n", intf->bInterfaceProtocol); 793 printf("iInterface: %d\n", intf->iInterface); 794 break; 795 796 case USB_DT_ENDPOINT: 797 endp = (struct usb_endpoint_descriptor *) current; 798 printf("bEndpointAddress: %d\n", endp->bEndpointAddress); 799 printf("bmAttributes: %x\n", endp->bmAttributes); 800 printf("wMaxPacketSize: %d\n", le16toh(endp->wMaxPacketSize)); 801 printf("bInterval: %d\n", endp->bInterval); 802 break; 803 804 default: 805 break; 806 } 807 808 if (desc->bLength) 809 current += desc->bLength; 810 else 811 { 812 printf("END (zero length record)\n"); 813 break; 814 } 815 } 816 } 817 818 /** 819 * Handle termination of the process. 820 */ 821 void shutdown(int signum) 822 { 823 printf("Closing...\n"); 824 ubb_close(0); 825 exit(1); 826 } 827 828 int main(int argc, char *argv[]) 829 { 830 uint8_t status = 0, revision = 0, framecount; 831 uint16_t count; 832 bool bus_event, data_event, suspended_event, connection_event, frame_event; 833 max_device device; 834 max_devstate devstate = MAX_DEVSTATE_INIT; 835 836 device.in_toggle = false; 837 device.out_toggle = false; 838 device.max_packet_size = 8; 839 840 signal(SIGINT, &shutdown); 841 842 if (ubb_open(0) < 0) { 843 perror("ubb_open"); 844 return 1; 845 } 846 847 ubb_power(1); 848 printf("Power on.\n"); 849 850 OUT(MAX_SS); 851 OUT(MAX_MOSI); 852 OUT(MAX_SCLK); 853 OUT(MAX_RESET); 854 IN(MAX_INT); 855 IN(MAX_MISO); 856 857 /* Initialise SPI. */ 858 /* Set SS# to 1. */ 859 860 SET(MAX_SS); 861 CLR(MAX_MOSI); 862 CLR(MAX_SCLK); 863 SET(MAX_RESET); 864 865 /* Initialise the MAX3421E. */ 866 867 /* Set full-duplex, interrupt signalling. */ 868 869 printf("Setting pin control...\n"); 870 max_write(MAX_REG_PINCTL, MAX_PINCTL_INTLEVEL_LEVEL | MAX_PINCTL_FDUPSPI_FULL); 871 872 chipreset(); 873 printf("Ready? %d\n", wait()); 874 875 /* Check various registers. */ 876 877 printf("Mode: %x\n", max_read(MAX_REG_MODE, &status)); 878 printf("IRQ: %x\n", max_read(MAX_REG_HIRQ, &status)); 879 880 /* Set host mode. */ 881 882 printf("Setting mode...\n"); 883 status = max_write(MAX_REG_MODE, MAX_MODE_HOST_ENABLED); 884 885 printf("Setting INT signalling...\n"); 886 status = max_write(MAX_REG_CPUCTL, MAX_CPUCTL_IE); 887 888 printf("Setting event signalling...\n"); 889 status = max_write(MAX_REG_HIEN, MAX_HIEN_CONDETIE | MAX_HIEN_FRAMEIE | MAX_HIEN_BUSEVENTIE); 890 891 /* Check various registers. */ 892 893 printf("Mode: %x\n", max_read(MAX_REG_MODE, &status)); 894 printf("IRQ: %x\n", max_read(MAX_REG_HIRQ, &status)); 895 printf("IE: %x\n", max_read(MAX_REG_HIEN, &status)); 896 printf("CPU: %x\n", max_read(MAX_REG_CPUCTL, &status)); 897 printf("Pin: %x\n", max_read(MAX_REG_PINCTL, &status)); 898 printf("USBIRQ: %x\n", max_read(MAX_REG_USBIRQ, &status)); 899 printf("USBIE: %x\n", max_read(MAX_REG_USBIEN, &status)); 900 901 /* Read from the REVISION register. */ 902 903 printf("Reading...\n"); 904 revision = max_read(MAX_REG_REVISION, &status); 905 printf("Revision = %x\n", revision); 906 907 /* Sample the bus for any devices that are already connected. */ 908 909 samplebus(); 910 911 if (devicechanged()) 912 { 913 devstate = MAX_DEVSTATE_CONNECTED; 914 framecount = 200; 915 } 916 917 /* Enter the main processing loop. */ 918 919 for (count = 0; count <= 65535; count++) 920 { 921 if (!PIN(MAX_INT)) 922 { 923 /* Obtain interrupt conditions. */ 924 925 status = max_read(MAX_REG_HIRQ, NULL); 926 927 bus_event = status & MAX_HIRQ_BUSEVENTIRQ; 928 data_event = status & MAX_HIRQ_RCVDAVIRQ; 929 suspended_event = status & MAX_HIRQ_SUSDNIRQ; 930 connection_event = status & MAX_HIRQ_CONDETIRQ; 931 frame_event = status & MAX_HIRQ_FRAMEIRQ; 932 933 #ifdef DEBUG_EVENTS 934 if (bus_event) printf("Bus "); 935 if (data_event) printf("Data "); 936 if (suspended_event) printf("Suspended "); 937 if (connection_event) printf("Connection "); 938 if (frame_event) printf("Frame "); 939 printf("\n"); 940 #endif 941 942 /* Acknowledge the interrupts. */ 943 944 max_write(MAX_REG_HIRQ, status); 945 946 /* Detect device connection/disconnection. */ 947 948 if ((devstate == MAX_DEVSTATE_INIT) && connection_event && devicechanged()) 949 { 950 devstate = MAX_DEVSTATE_CONNECTED; 951 framecount = 200; 952 printf("CONNECTED\n"); 953 } 954 955 /* Handle device connection. */ 956 957 else if ((devstate == MAX_DEVSTATE_CONNECTED) && frame_event && !(--framecount)) 958 { 959 busreset(); 960 devstate = MAX_DEVSTATE_START; 961 printf("START\n"); 962 } 963 964 /* Handle device reset initiation. */ 965 966 else if ((devstate == MAX_DEVSTATE_START) && bus_event && bus_is_reset()) 967 { 968 devstate = MAX_DEVSTATE_RESET; 969 framecount = 200; 970 printf("RESET\n"); 971 } 972 973 /* Handle device reset completion, getting device details and 974 resetting the device again. */ 975 976 else if ((devstate == MAX_DEVSTATE_RESET) && frame_event && !(--framecount)) 977 { 978 if (!max_init_device(&device)) 979 { 980 printf("FAILED: RESET -> INSPECTED\n"); 981 framecount = 200; 982 } 983 else 984 { 985 devstate = MAX_DEVSTATE_INSPECTED; 986 printf("INSPECTED\n"); 987 } 988 } 989 990 /* Handle second reset initiation. */ 991 992 else if ((devstate == MAX_DEVSTATE_INSPECTED) && bus_event && bus_is_reset()) 993 { 994 devstate = MAX_DEVSTATE_RESET_AGAIN; 995 framecount = 200; 996 printf("RESET_AGAIN\n"); 997 } 998 999 /* Handle second reset completion, setting the address. */ 1000 1001 else if ((devstate == MAX_DEVSTATE_RESET_AGAIN) && frame_event && !(--framecount)) 1002 { 1003 if (!max_complete_device(&device)) 1004 printf("FAILED: RESET_AGAIN -> ADDRESSING\n"); 1005 else 1006 { 1007 max_set_address(&device); 1008 devstate = MAX_DEVSTATE_ADDRESSING; 1009 framecount = 30; 1010 printf("ADDRESSING\n"); 1011 } 1012 } 1013 1014 /* Post-addressing. */ 1015 1016 else if ((devstate == MAX_DEVSTATE_ADDRESSING) && frame_event && !(--framecount)) 1017 { 1018 usb_show_languages(max_get_descriptor(&device, USB_DT_STRING, 0, 0, false)); 1019 usb_show_configuration(max_get_descriptor(&device, USB_DT_CONFIG, 0, 0, false)); 1020 devstate = MAX_DEVSTATE_READY; 1021 printf("READY\n"); 1022 } 1023 1024 /* Handle device disconnection. */ 1025 1026 else if ((devstate != MAX_DEVSTATE_INIT) && connection_event && !devicechanged()) 1027 { 1028 devstate = MAX_DEVSTATE_INIT; 1029 printf("INIT\n"); 1030 } 1031 } 1032 } 1033 1034 printf("Closing...\n"); 1035 ubb_close(0); 1036 1037 return 0; 1038 }