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 22 /* Found in Python's asdl.h. */ 23 24 #ifndef __cplusplus 25 typedef enum {false, true} bool; 26 #endif 27 28 /* Pin assignments: 29 * 30 * Sniffer UBB Shield 31 * ------- ---- ------ 32 * DAT2 DAT2 9 (INT) 33 * CD DAT3 10 (SS) 34 * CMD CMD 7 (RESET) 35 * VCC VDD VIN 36 * CLK CLK 13 (SCLK) 37 * GND GND GND 38 * DAT0 DAT0 11 (MOSI) 39 * DAT1 DAT1 12 (MISO) 40 * 8 (GPX) (not assigned) 41 */ 42 43 #define MAX_RESET UBB_CMD 44 #define MAX_SCLK UBB_CLK 45 #define MAX_MOSI UBB_DAT0 46 #define MAX_MISO UBB_DAT1 47 #define MAX_INT UBB_DAT2 48 #define MAX_SS UBB_DAT3 49 50 /* MAX3421E definitions. */ 51 52 #define MAX_REG_READ 0x00 53 #define MAX_REG_WRITE 0x02 54 55 #define MAX_REG_RCVFIFO 1 56 #define MAX_REG_SNDFIFO 2 57 #define MAX_REG_SUDFIFO 4 58 #define MAX_REG_RCVBC 6 59 #define MAX_REG_SNDBC 7 60 #define MAX_REG_USBIRQ 13 61 #define MAX_REG_USBIEN 14 62 #define MAX_REG_USBCTL 15 63 #define MAX_REG_CPUCTL 16 64 #define MAX_REG_PINCTL 17 65 #define MAX_REG_REVISION 18 66 #define MAX_REG_HIRQ 25 67 #define MAX_REG_HIEN 26 68 #define MAX_REG_MODE 27 69 #define MAX_REG_PERADDR 28 70 #define MAX_REG_HCTL 29 71 #define MAX_REG_HXFR 30 72 #define MAX_REG_HRSL 31 73 74 #define MAX_USBIRQ_OSCOKIRQ 1 75 #define MAX_USBIRQ_NOVBUSIRQ 32 76 #define MAX_USBIRQ_VBUSIRQ 64 77 78 #define MAX_USBCTL_PWRDOWN 16 79 #define MAX_USBCTL_CHIPRES 32 80 81 #define MAX_CPUCTL_IE 1 82 83 #define MAX_PINCTL_POSINT_LOW 0 84 #define MAX_PINCTL_POSINT_HIGH 4 85 #define MAX_PINCTL_INTLEVEL_EDGE 0 86 #define MAX_PINCTL_INTLEVEL_LEVEL 8 87 #define MAX_PINCTL_FDUPSPI_HALF 0 88 #define MAX_PINCTL_FDUPSPI_FULL 16 89 90 #define MAX_HIRQ_BUSEVENTIRQ 1 91 #define MAX_HIRQ_RWUIRQ 2 92 #define MAX_HIRQ_RCVDAVIRQ 4 93 #define MAX_HIRQ_SNDBAVIRQ 8 94 #define MAX_HIRQ_SUSDNIRQ 16 95 #define MAX_HIRQ_CONDETIRQ 32 96 #define MAX_HIRQ_FRAMEIRQ 64 97 #define MAX_HIRQ_HXFRDNIRQ 128 98 99 #define MAX_HIEN_CONDETIE 32 100 101 #define MAX_MODE_PERIPHERAL 0 102 #define MAX_MODE_HOST 1 103 #define MAX_MODE_LOWSPEED 2 104 #define MAX_MODE_SOFKAENAB 8 105 #define MAX_MODE_SEPIRQ_OFF 0 106 #define MAX_MODE_SEPIRQ_ON 16 107 #define MAX_MODE_DMPULLDN 64 108 #define MAX_MODE_DPPULLDN 128 109 110 #define MAX_MODE_HOST_ENABLED MAX_MODE_HOST | MAX_MODE_SEPIRQ_OFF | MAX_MODE_DMPULLDN | MAX_MODE_DPPULLDN 111 #define MAX_MODE_HOST_FULLSPEED MAX_MODE_HOST_ENABLED 112 #define MAX_MODE_HOST_LOWSPEED MAX_MODE_HOST_ENABLED | MAX_MODE_LOWSPEED 113 114 #define MAX_HCTL_SAMPLEBUS 4 115 #define MAX_HCTL_RCVTOG0 16 116 #define MAX_HCTL_RCVTOG1 32 117 #define MAX_HCTL_SNDTOG0 64 118 #define MAX_HCTL_SNDTOG1 128 119 120 #define MAX_HXFR_SETUP 16 121 #define MAX_HXFR_OUTNIN 32 122 #define MAX_HXFR_HS 128 123 124 #define MAX_HRSL_JSTATUS 128 125 #define MAX_HRSL_KSTATUS 64 126 #define MAX_HRSL_SNDTOGRD 32 127 #define MAX_HRSL_RCVTOGRD 16 128 #define MAX_HRSL_HRSLT 15 129 130 #define max_reg(n) ((uint8_t) (n << 3)) 131 #define max_reg_read(n) (max_reg(n) | MAX_REG_READ) 132 #define max_reg_write(n) (max_reg(n) | MAX_REG_WRITE) 133 134 void spi_begin() 135 { 136 CLR(MAX_SS); 137 } 138 139 void spi_end() 140 { 141 SET(MAX_SS); 142 } 143 144 /** 145 * Send the given value via MOSI while receiving a value via MISO. 146 * This requires full-duplex SPI and will produce a status value for the first 147 * value sent (the command). 148 */ 149 uint8_t spi_sendrecv(uint8_t v) 150 { 151 uint8_t result = 0; 152 uint8_t mask; 153 154 for (mask = 0x80; mask; mask >>= 1) 155 { 156 if (v & mask) 157 { 158 #ifdef DEBUG 159 printf("1"); 160 #endif 161 SET(MAX_MOSI); 162 } 163 else 164 { 165 #ifdef DEBUG 166 printf("0"); 167 #endif 168 CLR(MAX_MOSI); 169 } 170 171 /* Wait for stable output signal. */ 172 173 SET(MAX_SCLK); 174 175 if (PIN(MAX_MISO)) 176 result |= mask; 177 178 CLR(MAX_SCLK); 179 } 180 181 #ifdef DEBUG 182 printf("\n"); 183 #endif 184 return result; 185 } 186 187 uint8_t max_read(uint8_t reg, uint8_t *status) 188 { 189 uint8_t result = 0, tmpstatus = 0; 190 191 tmpstatus = 0; 192 193 spi_begin(); 194 tmpstatus = spi_sendrecv(max_reg_read(reg)); 195 result = spi_sendrecv(0); 196 spi_end(); 197 198 if (status != NULL) 199 *status = tmpstatus; 200 201 return result; 202 } 203 204 uint8_t max_write(uint8_t reg, uint8_t value) 205 { 206 uint8_t status = 0; 207 208 spi_begin(); 209 status = spi_sendrecv(max_reg_write(reg)); 210 spi_sendrecv(value); 211 spi_end(); 212 213 return status; 214 } 215 216 /** 217 * Return whether data can be sent. 218 */ 219 bool max_can_send() 220 { 221 uint8_t status = max_read(MAX_REG_HIRQ, NULL); 222 223 return !(status & MAX_HIRQ_SNDBAVIRQ); 224 } 225 226 /** 227 * Set the sending data toggle. 228 */ 229 void max_set_send_toggle(bool toggle) 230 { 231 max_write(MAX_REG_HCTL, toggle ? MAX_HCTL_SNDTOG1 : MAX_HCTL_SNDTOG0); 232 } 233 234 /** 235 * Return the sending data toggle. 236 */ 237 bool max_get_send_toggle() 238 { 239 return (max_read(MAX_REG_HRSL, NULL) & MAX_HRSL_SNDTOGRD) != 0; 240 } 241 242 /** 243 * Set the receiving data toggle. 244 */ 245 void max_set_recv_toggle(bool toggle) 246 { 247 max_write(MAX_REG_HCTL, toggle ? MAX_HCTL_RCVTOG1 : MAX_HCTL_RCVTOG0); 248 } 249 250 /** 251 * Return the receiving data toggle. 252 */ 253 bool max_get_recv_toggle() 254 { 255 return (max_read(MAX_REG_HRSL, NULL) & MAX_HRSL_RCVTOGRD) != 0; 256 } 257 258 /** 259 * Wait for handshake/timeout after a transfer. 260 */ 261 uint8_t max_wait_transfer(uint8_t status) 262 { 263 while (!(status & MAX_HIRQ_HXFRDNIRQ)) 264 { 265 status = max_read(MAX_REG_HIRQ, NULL); 266 } 267 268 return status; 269 } 270 271 /** 272 * Send HS payload for control transfers. 273 */ 274 uint8_t max_send_hs() 275 { 276 uint8_t status = max_write(MAX_REG_HXFR, MAX_HXFR_HS); 277 return max_wait_transfer(status); 278 } 279 280 /** 281 * Write the given data to the FIFO. 282 */ 283 void max_write_fifo(uint8_t endpoint, uint8_t *data, uint8_t len) 284 { 285 uint8_t count; 286 287 for (count = 0; count < len; count++) 288 { 289 max_write(endpoint ? MAX_REG_SNDFIFO : MAX_REG_SUDFIFO, data[count]); 290 } 291 292 if (endpoint) 293 max_write(MAX_REG_SNDBC, len); 294 } 295 296 /** 297 * Read the data from the FIFO. 298 */ 299 void max_read_fifo(uint8_t *data, uint8_t *len, uint8_t *datalimit) 300 { 301 uint8_t count, received = max_read(MAX_REG_RCVBC, NULL); 302 303 *len += received; 304 305 for (count = 0; (count < received) && (data < datalimit); count++) 306 { 307 *data++ = max_read(MAX_REG_RCVFIFO, NULL); 308 } 309 } 310 311 /** 312 * Send a control request to the given address consisting of the given setup 313 * data. 314 */ 315 uint8_t max_control(uint8_t address, uint8_t *setup) 316 { 317 uint8_t status, hrsl = 0; 318 319 max_write_fifo(0, setup, 8); 320 321 /* Set the address. */ 322 323 max_write(MAX_REG_PERADDR, address); 324 325 /* Initiate the transfer. */ 326 327 do 328 { 329 status = max_write(MAX_REG_HXFR, MAX_HXFR_SETUP); 330 status = max_wait_transfer(status); 331 hrsl = max_read(MAX_REG_HRSL, &status); 332 } 333 while ((hrsl & MAX_HRSL_HRSLT) != 0); 334 335 return status; 336 } 337 338 /** 339 * Send a request to the given address and endpoint, using the supplied data 340 * payload with the given length, indicating the preserved toggle state of the 341 * endpoint (which will be updated). 342 */ 343 uint8_t max_send(uint8_t address, uint8_t endpoint, uint8_t *data, uint8_t len, bool *toggle) 344 { 345 uint8_t status, hrsl = 0; 346 347 max_write_fifo(endpoint, data, len); 348 349 if (endpoint) 350 max_set_send_toggle(*toggle); 351 352 /* Set the address. */ 353 354 max_write(MAX_REG_PERADDR, address); 355 356 /* Initiate the transfer. */ 357 358 do 359 { 360 status = max_write(MAX_REG_HXFR, endpoint | MAX_HXFR_OUTNIN); 361 status = max_wait_transfer(status); 362 363 /* Test for usable data. */ 364 365 if (!(status & MAX_HIRQ_SNDBAVIRQ)) 366 continue; 367 368 hrsl = max_read(MAX_REG_HRSL, &status); 369 } 370 while ((hrsl & MAX_HRSL_HRSLT) != 0); 371 372 if (endpoint) 373 *toggle = max_get_send_toggle(); 374 375 return status; 376 } 377 378 /** 379 * Make a request for data from the given address and endpoint, collecting it in 380 * the supplied buffer with the given length, indicating the preserved toggle 381 * state of the endpoint (which will be updated) The length will be updated to 382 * indicate the total length of the received data. 383 */ 384 uint8_t max_recv(uint8_t address, uint8_t endpoint, uint8_t *data, uint8_t *len, bool *toggle) 385 { 386 uint8_t *datalimit = data + *len; 387 uint8_t status, hrsl = 0; 388 389 if (endpoint) 390 max_set_send_toggle(*toggle); 391 392 /* Set the address. */ 393 394 max_write(MAX_REG_PERADDR, address); 395 396 /* Initiate the transfer. */ 397 398 do 399 { 400 status = max_write(MAX_REG_HXFR, endpoint); 401 status = max_wait_transfer(status); 402 403 /* Test for usable data. */ 404 405 if (!(status & MAX_HIRQ_RCVDAVIRQ)) 406 continue; 407 408 hrsl = max_read(MAX_REG_HRSL, &status); 409 } 410 while ((hrsl & MAX_HRSL_HRSLT) != 0); 411 412 do 413 { 414 max_read_fifo(data, len, datalimit); 415 416 /* Indicate that all data has been read. */ 417 418 status = max_write(MAX_REG_HIRQ, MAX_HIRQ_RCVDAVIRQ); 419 } 420 while (status & MAX_HIRQ_RCVDAVIRQ); 421 422 if (endpoint) 423 *toggle = max_get_send_toggle(); 424 425 return status; 426 } 427 428 void chipreset() 429 { 430 printf("Resetting...\n"); 431 max_write(MAX_REG_USBCTL, MAX_USBCTL_CHIPRES); 432 433 printf("Clearing the reset...\n"); 434 max_write(MAX_REG_USBCTL, 0); 435 } 436 437 uint8_t check() 438 { 439 uint8_t oscillator; 440 441 oscillator = max_read(MAX_REG_USBIRQ, NULL); 442 443 return (oscillator & ~(MAX_USBIRQ_NOVBUSIRQ | MAX_USBIRQ_VBUSIRQ)) == MAX_USBIRQ_OSCOKIRQ; 444 } 445 446 uint8_t wait() 447 { 448 uint16_t timeout = 1024; 449 450 /* Wait for the oscillator before performing USB activity. */ 451 452 printf("Waiting...\n"); 453 454 while ((timeout > 0) && (!check())) 455 { 456 timeout--; 457 } 458 459 printf("Iterations remaining: %d\n", timeout); 460 461 return timeout; 462 } 463 464 uint8_t samplebusready() 465 { 466 uint8_t result; 467 468 result = max_read(MAX_REG_HCTL, NULL); 469 470 return !(result & MAX_HCTL_SAMPLEBUS); 471 } 472 473 void samplebus() 474 { 475 max_write(MAX_REG_HCTL, MAX_HCTL_SAMPLEBUS); 476 while (!samplebusready()); 477 } 478 479 /** 480 * Handle the connection or disconnection of a device, returning true if the 481 * device is now connected or false otherwise. 482 */ 483 bool devicechanged() 484 { 485 uint8_t hrsl, mode; 486 487 hrsl = max_read(MAX_REG_HRSL, NULL); 488 mode = max_read(MAX_REG_MODE, NULL); 489 490 if ((hrsl & MAX_HRSL_JSTATUS) && (hrsl & MAX_HRSL_KSTATUS)) 491 { 492 printf("Bad device status.\n"); 493 } 494 else if (!(hrsl & MAX_HRSL_JSTATUS) && !(hrsl & MAX_HRSL_KSTATUS)) 495 { 496 printf("Device disconnected.\n"); 497 } 498 else 499 { 500 printf("Device connected.\n"); 501 502 /* Low speed device when J and lowspeed have the same level. 503 Since J and K should have opposing levels, K can be tested when 504 lowspeed is low. */ 505 506 if (((hrsl & MAX_HRSL_JSTATUS) && (mode & MAX_MODE_LOWSPEED)) || 507 ((hrsl & MAX_HRSL_KSTATUS) && !(mode & MAX_MODE_LOWSPEED))) 508 { 509 printf("Device is low speed.\n"); 510 max_write(MAX_REG_MODE, MAX_MODE_HOST_LOWSPEED); 511 } 512 else 513 { 514 printf("Device is full speed.\n"); 515 max_write(MAX_REG_MODE, MAX_MODE_HOST_FULLSPEED); 516 } 517 518 return true; 519 } 520 521 return false; 522 } 523 524 void setup_packet(uint8_t *setup, uint8_t request_type, uint8_t request, uint16_t value, uint16_t index, uint16_t length) 525 { 526 setup[0] = request_type; 527 setup[1] = request; 528 setup[2] = value & 0xff; 529 setup[3] = value >> 8; 530 setup[4] = index & 0xff; 531 setup[5] = index >> 8; 532 setup[6] = length & 0xff; 533 setup[7] = length >> 8; 534 } 535 536 void shutdown(int signum) 537 { 538 printf("Closing...\n"); 539 ubb_close(0); 540 exit(1); 541 } 542 543 int main(int argc, char *argv[]) 544 { 545 uint8_t status = 0, revision = 0; 546 uint16_t count; 547 bool in_toggle = 0; 548 uint8_t data[64], len = 64, setup[8]; 549 550 signal(SIGINT, &shutdown); 551 552 if (ubb_open(0) < 0) { 553 perror("ubb_open"); 554 return 1; 555 } 556 557 ubb_power(1); 558 printf("Power on.\n"); 559 560 OUT(MAX_SS); 561 OUT(MAX_MOSI); 562 OUT(MAX_SCLK); 563 OUT(MAX_RESET); 564 IN(MAX_INT); 565 IN(MAX_MISO); 566 567 /* Initialise SPI. */ 568 /* Set SS# to 1. */ 569 570 SET(MAX_SS); 571 CLR(MAX_MOSI); 572 CLR(MAX_SCLK); 573 SET(MAX_RESET); 574 575 /* Initialise the MAX3421E. */ 576 577 /* Set full-duplex, interrupt signalling. */ 578 579 printf("Setting pin control...\n"); 580 max_write(MAX_REG_PINCTL, MAX_PINCTL_INTLEVEL_LEVEL | MAX_PINCTL_FDUPSPI_FULL); 581 582 chipreset(); 583 printf("Ready? %d\n", wait()); 584 585 /* Check various registers. */ 586 587 printf("Mode: %x\n", max_read(MAX_REG_MODE, &status)); 588 printf("IRQ: %x\n", max_read(MAX_REG_HIRQ, &status)); 589 590 /* Set host mode. */ 591 592 printf("Setting mode...\n"); 593 status = max_write(MAX_REG_MODE, MAX_MODE_HOST_ENABLED); 594 595 printf("Setting INT signalling...\n"); 596 status = max_write(MAX_REG_CPUCTL, MAX_CPUCTL_IE); 597 598 printf("Setting connection signalling...\n"); 599 status = max_write(MAX_REG_HIEN, MAX_HIEN_CONDETIE); 600 601 /* Check various registers. */ 602 603 printf("Mode: %x\n", max_read(MAX_REG_MODE, &status)); 604 printf("IRQ: %x\n", max_read(MAX_REG_HIRQ, &status)); 605 printf("IE: %x\n", max_read(MAX_REG_HIEN, &status)); 606 printf("CPU: %x\n", max_read(MAX_REG_CPUCTL, &status)); 607 printf("Pin: %x\n", max_read(MAX_REG_PINCTL, &status)); 608 printf("USBIRQ: %x\n", max_read(MAX_REG_USBIRQ, &status)); 609 printf("USBIE: %x\n", max_read(MAX_REG_USBIEN, &status)); 610 611 /* Read from the REVISION register. */ 612 613 printf("Reading...\n"); 614 revision = max_read(MAX_REG_REVISION, &status); 615 printf("Revision = %x\n", revision); 616 617 for (count = 0; count <= 65535; count++) 618 { 619 if (!PIN(MAX_INT)) 620 { 621 status = max_read(MAX_REG_HIRQ, NULL); 622 623 if ((status & MAX_HIRQ_CONDETIRQ) && devicechanged() && max_can_send()) 624 { 625 printf("Sending control request to address 0, endpoint 0...\n"); 626 setup_packet(setup, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, USB_DT_DEVICE, 0, USB_DT_DEVICE_SIZE); 627 max_control(0, setup); 628 max_recv(0, 0, data, &len, &in_toggle); 629 } 630 if (status & MAX_HIRQ_SUSDNIRQ) 631 printf("Suspend done.\n"); 632 if (status & MAX_HIRQ_BUSEVENTIRQ) 633 printf("Bus event.\n"); 634 if (status & MAX_HIRQ_RCVDAVIRQ) 635 printf("Data received.\n"); 636 637 max_write(MAX_REG_HIRQ, status); 638 } 639 } 640 641 printf("Closing...\n"); 642 ubb_close(0); 643 644 return 0; 645 }