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