paul@0 | 1 | /* |
paul@0 | 2 | * Ben NanoNote and Arduino USB Host shield communication. |
paul@0 | 3 | * |
paul@36 | 4 | * Copyright (C) 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@36 | 8 | * Copyright (C) 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@35 | 625 | * Obtain a device descriptor with usable structure members. |
paul@35 | 626 | */ |
paul@35 | 627 | struct usb_device_descriptor *usb_get_device_descriptor(uint8_t *data) |
paul@35 | 628 | { |
paul@35 | 629 | struct usb_device_descriptor *desc = (struct usb_device_descriptor *) data; |
paul@35 | 630 | desc->bcdUSB = le16toh(desc->bcdUSB); |
paul@35 | 631 | desc->idVendor = le16toh(desc->idVendor); |
paul@35 | 632 | desc->idProduct = le16toh(desc->idProduct); |
paul@35 | 633 | desc->bcdDevice = le16toh(desc->bcdDevice); |
paul@35 | 634 | return desc; |
paul@35 | 635 | } |
paul@35 | 636 | |
paul@35 | 637 | /** |
paul@35 | 638 | * Obtain a configuration descriptor with usable structure members. |
paul@35 | 639 | */ |
paul@35 | 640 | struct usb_config_descriptor *usb_get_config_descriptor(uint8_t *data) |
paul@35 | 641 | { |
paul@35 | 642 | struct usb_config_descriptor *conf = (struct usb_config_descriptor *) data; |
paul@35 | 643 | conf->wTotalLength = le16toh(conf->wTotalLength); |
paul@35 | 644 | return conf; |
paul@35 | 645 | } |
paul@35 | 646 | |
paul@35 | 647 | /** |
paul@35 | 648 | * Obtain an endpoint descriptor with usable structure members. |
paul@35 | 649 | */ |
paul@35 | 650 | struct usb_endpoint_descriptor *usb_get_endpoint_descriptor(uint8_t *data) |
paul@35 | 651 | { |
paul@35 | 652 | struct usb_endpoint_descriptor *endp = (struct usb_endpoint_descriptor *) data; |
paul@35 | 653 | endp->wMaxPacketSize = le16toh(endp->wMaxPacketSize); |
paul@35 | 654 | return endp; |
paul@35 | 655 | } |
paul@35 | 656 | |
paul@35 | 657 | /** |
paul@32 | 658 | * Get a descriptor from the device having the given type, value, index and length. |
paul@23 | 659 | * The descriptor buffer is static and must be copied if it is to be preserved. |
paul@23 | 660 | */ |
paul@32 | 661 | uint8_t *max_get_descriptor_with_length(max_device *device, uint8_t type, uint8_t value, uint8_t index, uint8_t length) |
paul@22 | 662 | { |
paul@32 | 663 | static uint8_t data[1024]; |
paul@32 | 664 | uint8_t len = length, setup[8]; |
paul@22 | 665 | |
paul@32 | 666 | setup_packet(setup, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, usb_descriptor_type(type) | value, index, length); |
paul@27 | 667 | |
paul@22 | 668 | max_control(setup); |
paul@22 | 669 | if (!max_control_input(data, &len, device)) |
paul@22 | 670 | { |
paul@22 | 671 | printf("Failed.\n"); |
paul@22 | 672 | return NULL; |
paul@22 | 673 | } |
paul@22 | 674 | max_control_status(true); |
paul@22 | 675 | |
paul@32 | 676 | if (len >= length) |
paul@22 | 677 | return data; |
paul@22 | 678 | else |
paul@23 | 679 | { |
paul@32 | 680 | printf("Expected %d but received %d.\n", length, len); |
paul@22 | 681 | return NULL; |
paul@23 | 682 | } |
paul@22 | 683 | } |
paul@22 | 684 | |
paul@21 | 685 | /** |
paul@32 | 686 | * Get a descriptor from the device having the given type, value, index and |
paul@32 | 687 | * length. If the length is given as zero, the descriptor header will first be |
paul@32 | 688 | * received and then the full descriptor will be retrieved. |
paul@32 | 689 | * The descriptor buffer is static and must be copied if it is to be preserved. |
paul@32 | 690 | */ |
paul@32 | 691 | uint8_t *max_get_descriptor(max_device *device, uint8_t type, uint8_t value, uint8_t index, uint8_t length) |
paul@32 | 692 | { |
paul@32 | 693 | uint8_t *data; |
paul@32 | 694 | |
paul@32 | 695 | printf("Sending descriptor request to address %d, endpoint 0...\n", device->address); |
paul@32 | 696 | |
paul@32 | 697 | max_write(MAX_REG_PERADDR, device->address); |
paul@32 | 698 | |
paul@32 | 699 | if (!length) |
paul@32 | 700 | { |
paul@32 | 701 | data = max_get_descriptor_with_length(device, type, value, index, usb_descriptor_size(type)); |
paul@32 | 702 | if (data == NULL) |
paul@32 | 703 | return NULL; |
paul@32 | 704 | |
paul@32 | 705 | if (type == USB_DT_CONFIG) |
paul@35 | 706 | length = usb_get_config_descriptor(data)->wTotalLength; |
paul@32 | 707 | else |
paul@32 | 708 | length = ((struct usb_descriptor_header *) data)->bLength; |
paul@32 | 709 | |
paul@32 | 710 | if (length == usb_descriptor_size(type)) |
paul@32 | 711 | return data; |
paul@32 | 712 | } |
paul@32 | 713 | |
paul@32 | 714 | return max_get_descriptor_with_length(device, type, value, index, length); |
paul@32 | 715 | } |
paul@32 | 716 | |
paul@32 | 717 | bool usb_init_device(max_device *device, uint8_t *data) |
paul@32 | 718 | { |
paul@35 | 719 | struct usb_device_descriptor *desc; |
paul@32 | 720 | |
paul@32 | 721 | if (data == NULL) |
paul@32 | 722 | return false; |
paul@32 | 723 | |
paul@35 | 724 | desc = usb_get_device_descriptor(data); |
paul@32 | 725 | memcpy(&device->desc, desc, sizeof(struct usb_device_descriptor)); |
paul@32 | 726 | return true; |
paul@32 | 727 | } |
paul@32 | 728 | |
paul@32 | 729 | /** |
paul@21 | 730 | * Perform initialisation on a device, obtaining the device details and storing |
paul@21 | 731 | * this information in the device structure, returning true if successful and |
paul@21 | 732 | * false otherwise. |
paul@21 | 733 | */ |
paul@19 | 734 | bool max_init_device(max_device *device) |
paul@15 | 735 | { |
paul@32 | 736 | uint8_t *data; |
paul@15 | 737 | |
paul@15 | 738 | printf("Sending control request to address 0, endpoint 0...\n"); |
paul@20 | 739 | |
paul@22 | 740 | device->address = 0; |
paul@21 | 741 | |
paul@21 | 742 | /* Send a "get descriptor" request for the device descriptor. */ |
paul@21 | 743 | |
paul@32 | 744 | data = max_get_descriptor(device, USB_DT_DEVICE, 0, 0, 8); |
paul@27 | 745 | |
paul@32 | 746 | if (data != NULL) |
paul@27 | 747 | { |
paul@32 | 748 | usb_init_device(device, data); |
paul@27 | 749 | device->max_packet_size = device->desc.bMaxPacketSize0; |
paul@27 | 750 | |
paul@27 | 751 | /* Reset the device. */ |
paul@27 | 752 | |
paul@27 | 753 | max_write(MAX_REG_HCTL, MAX_HCTL_BUSRST); |
paul@27 | 754 | return true; |
paul@27 | 755 | } |
paul@27 | 756 | |
paul@27 | 757 | return false; |
paul@27 | 758 | } |
paul@27 | 759 | |
paul@21 | 760 | /** |
paul@21 | 761 | * Assign a new address to the given device. |
paul@21 | 762 | */ |
paul@21 | 763 | void max_set_address(max_device *device) |
paul@21 | 764 | { |
paul@21 | 765 | uint8_t setup[8]; |
paul@21 | 766 | |
paul@21 | 767 | max_write(MAX_REG_PERADDR, 0); |
paul@21 | 768 | |
paul@21 | 769 | device->address = next_address++; |
paul@21 | 770 | |
paul@21 | 771 | printf("Setting device address to %d...\n", device->address); |
paul@21 | 772 | |
paul@21 | 773 | setup_packet(setup, USB_ENDPOINT_OUT, USB_REQ_SET_ADDRESS, device->address, 0, 0); |
paul@21 | 774 | max_control(setup); |
paul@21 | 775 | max_control_status(false); |
paul@21 | 776 | } |
paul@21 | 777 | |
paul@35 | 778 | /** |
paul@35 | 779 | * Set the configuration of the given device. |
paul@35 | 780 | */ |
paul@35 | 781 | void max_set_configuration(max_device *device, uint8_t config) |
paul@35 | 782 | { |
paul@35 | 783 | uint8_t setup[8]; |
paul@35 | 784 | |
paul@35 | 785 | max_write(MAX_REG_PERADDR, device->address); |
paul@35 | 786 | |
paul@35 | 787 | setup_packet(setup, USB_ENDPOINT_OUT, USB_REQ_SET_CONFIGURATION, config, 0, 0); |
paul@35 | 788 | max_control(setup); |
paul@35 | 789 | max_control_status(false); |
paul@35 | 790 | } |
paul@35 | 791 | |
paul@35 | 792 | /** |
paul@35 | 793 | * Return a string representation of an endpoint address. |
paul@35 | 794 | */ |
paul@34 | 795 | const char *usb_endpoint_address_str(uint8_t bEndpointAddress) |
paul@34 | 796 | { |
paul@34 | 797 | static char output[] = "255 (127 OUT)"; |
paul@34 | 798 | |
paul@34 | 799 | sprintf(output, "%03d (%03d %3s)", bEndpointAddress, |
paul@34 | 800 | bEndpointAddress & ~USB_ENDPOINT_IN, |
paul@34 | 801 | bEndpointAddress & USB_ENDPOINT_IN ? "IN" : "OUT"); |
paul@34 | 802 | |
paul@34 | 803 | return output; |
paul@34 | 804 | } |
paul@34 | 805 | |
paul@35 | 806 | /** |
paul@35 | 807 | * Return a string representation of endpoint attributes. |
paul@35 | 808 | */ |
paul@34 | 809 | const char *usb_endpoint_attributes_str(uint8_t bmAttributes) |
paul@34 | 810 | { |
paul@34 | 811 | switch (bmAttributes & USB_ENDPOINT_TYPE_MASK) |
paul@34 | 812 | { |
paul@34 | 813 | case USB_ENDPOINT_TYPE_CONTROL: |
paul@34 | 814 | return "CONTROL"; |
paul@34 | 815 | |
paul@34 | 816 | case USB_ENDPOINT_TYPE_ISOCHRONOUS: |
paul@34 | 817 | return "ISO"; |
paul@34 | 818 | |
paul@34 | 819 | case USB_ENDPOINT_TYPE_BULK: |
paul@34 | 820 | return "BULK"; |
paul@34 | 821 | |
paul@34 | 822 | case USB_ENDPOINT_TYPE_INTERRUPT: |
paul@34 | 823 | return "INT"; |
paul@34 | 824 | |
paul@34 | 825 | default: |
paul@34 | 826 | return ""; |
paul@34 | 827 | } |
paul@34 | 828 | } |
paul@34 | 829 | |
paul@32 | 830 | void usb_show_device(max_device *device) |
paul@32 | 831 | { |
paul@32 | 832 | printf("bLength: %d\n", device->desc.bLength); |
paul@32 | 833 | printf("bDescriptorType: %d\n", device->desc.bDescriptorType); |
paul@32 | 834 | printf("bcdUSB: %04x\n", device->desc.bcdUSB); |
paul@32 | 835 | printf("bDeviceClass: %d\n", device->desc.bDeviceClass); |
paul@32 | 836 | printf("bDeviceSubClass: %d\n", device->desc.bDeviceSubClass); |
paul@32 | 837 | printf("bDeviceProtocol: %d\n", device->desc.bDeviceProtocol); |
paul@32 | 838 | printf("bMaxPacketSize0: %d\n", device->desc.bMaxPacketSize0); |
paul@32 | 839 | printf("idVendor: %04x\n", device->desc.idVendor); |
paul@32 | 840 | printf("idProduct: %04x\n", device->desc.idProduct); |
paul@32 | 841 | printf("bcdDevice: %04x\n", device->desc.bcdDevice); |
paul@32 | 842 | printf("iManufacturer: %d\n", device->desc.iManufacturer); |
paul@32 | 843 | printf("iProduct: %d\n", device->desc.iProduct); |
paul@32 | 844 | printf("iSerialNumber: %d\n", device->desc.iSerialNumber); |
paul@32 | 845 | printf("bNumConfigurations: %d\n", device->desc.bNumConfigurations); |
paul@32 | 846 | } |
paul@32 | 847 | |
paul@22 | 848 | void usb_show_languages(uint8_t *data) |
paul@21 | 849 | { |
paul@22 | 850 | struct usb_string_descriptor *desc = (struct usb_string_descriptor *) data; |
paul@21 | 851 | uint16_t *lang; |
paul@21 | 852 | |
paul@22 | 853 | if (data == NULL) |
paul@21 | 854 | return; |
paul@21 | 855 | |
paul@22 | 856 | printf("bLength: %d\n", desc->bLength); |
paul@22 | 857 | printf("bDescriptorType: %d\n", desc->bDescriptorType); |
paul@21 | 858 | |
paul@22 | 859 | for (lang = desc->wData; lang < (uint16_t *) (data + desc->bLength); lang++) |
paul@22 | 860 | { |
paul@22 | 861 | printf("wLangId: %04x\n", le16toh(*lang)); |
paul@21 | 862 | } |
paul@21 | 863 | } |
paul@21 | 864 | |
paul@34 | 865 | uint8_t *usb_descriptor_start(struct usb_config_descriptor *conf) |
paul@34 | 866 | { |
paul@34 | 867 | return ((uint8_t *) conf) + conf->bLength; |
paul@34 | 868 | } |
paul@34 | 869 | |
paul@34 | 870 | uint8_t *usb_descriptor_end(struct usb_config_descriptor *conf) |
paul@34 | 871 | { |
paul@34 | 872 | return ((uint8_t *) conf) + conf->wTotalLength; |
paul@34 | 873 | } |
paul@23 | 874 | |
paul@34 | 875 | uint8_t *usb_descriptor_next(uint8_t *current) |
paul@34 | 876 | { |
paul@34 | 877 | struct usb_descriptor_header *desc = (struct usb_descriptor_header *) current; |
paul@34 | 878 | if (desc->bLength) |
paul@34 | 879 | return current + desc->bLength; |
paul@34 | 880 | else |
paul@34 | 881 | return NULL; |
paul@34 | 882 | } |
paul@34 | 883 | |
paul@34 | 884 | void usb_show_configuration(struct usb_config_descriptor *conf) |
paul@34 | 885 | { |
paul@23 | 886 | printf("bLength: %d\n", conf->bLength); |
paul@23 | 887 | printf("bDescriptorType: %d\n", conf->bDescriptorType); |
paul@34 | 888 | printf("wTotalLength: %d\n", conf->wTotalLength); |
paul@23 | 889 | printf("bNumInterfaces: %d\n", conf->bNumInterfaces); |
paul@23 | 890 | printf("bConfigurationValue: %d\n", conf->bConfigurationValue); |
paul@23 | 891 | printf("iConfiguration: %d\n", conf->iConfiguration); |
paul@23 | 892 | printf("bmAttributes: %x\n", conf->bmAttributes); |
paul@23 | 893 | printf("MaxPower: %d\n", conf->MaxPower); |
paul@34 | 894 | } |
paul@23 | 895 | |
paul@34 | 896 | void usb_show_interface(struct usb_interface_descriptor *intf) |
paul@34 | 897 | { |
paul@34 | 898 | printf("bInterfaceNumber: %d\n", intf->bInterfaceNumber); |
paul@34 | 899 | printf("bAlternateSetting: %d\n", intf->bAlternateSetting); |
paul@34 | 900 | printf("bNumEndpoints: %d\n", intf->bNumEndpoints); |
paul@34 | 901 | printf("bInterfaceClass: %x\n", intf->bInterfaceClass); |
paul@34 | 902 | printf("bInterfaceSubClass: %x\n", intf->bInterfaceSubClass); |
paul@34 | 903 | printf("bInterfaceProtocol: %x\n", intf->bInterfaceProtocol); |
paul@34 | 904 | printf("iInterface: %d\n", intf->iInterface); |
paul@34 | 905 | } |
paul@34 | 906 | |
paul@34 | 907 | void usb_show_endpoint(struct usb_endpoint_descriptor *endp) |
paul@34 | 908 | { |
paul@34 | 909 | printf("bEndpointAddress: %s\n", usb_endpoint_address_str(endp->bEndpointAddress)); |
paul@34 | 910 | printf("bmAttributes: %s\n", usb_endpoint_attributes_str(endp->bmAttributes)); |
paul@34 | 911 | printf("wMaxPacketSize: %d\n", endp->wMaxPacketSize); |
paul@34 | 912 | printf("bInterval: %d\n", endp->bInterval); |
paul@34 | 913 | } |
paul@34 | 914 | |
paul@34 | 915 | void usb_show_configuration_full(uint8_t *data) |
paul@34 | 916 | { |
paul@34 | 917 | struct usb_config_descriptor *conf; |
paul@34 | 918 | struct usb_descriptor_header *desc; |
paul@34 | 919 | struct usb_interface_descriptor *intf; |
paul@34 | 920 | struct usb_endpoint_descriptor *endp; |
paul@34 | 921 | uint8_t *current, *last; |
paul@34 | 922 | |
paul@34 | 923 | if (data == NULL) |
paul@34 | 924 | return; |
paul@34 | 925 | |
paul@34 | 926 | conf = usb_get_config_descriptor(data); |
paul@34 | 927 | current = usb_descriptor_start(conf); |
paul@34 | 928 | last = usb_descriptor_end(conf); |
paul@23 | 929 | |
paul@23 | 930 | while (current < last) |
paul@23 | 931 | { |
paul@23 | 932 | desc = (struct usb_descriptor_header *) current; |
paul@23 | 933 | printf("bLength: %d\n", desc->bLength); |
paul@23 | 934 | printf("bDescriptorType: %d\n", desc->bDescriptorType); |
paul@23 | 935 | |
paul@23 | 936 | switch (desc->bDescriptorType) |
paul@23 | 937 | { |
paul@23 | 938 | case USB_DT_INTERFACE: |
paul@23 | 939 | intf = (struct usb_interface_descriptor *) current; |
paul@34 | 940 | usb_show_interface(intf); |
paul@23 | 941 | break; |
paul@23 | 942 | |
paul@23 | 943 | case USB_DT_ENDPOINT: |
paul@34 | 944 | endp = usb_get_endpoint_descriptor(current); |
paul@34 | 945 | usb_show_endpoint(endp); |
paul@23 | 946 | break; |
paul@23 | 947 | |
paul@23 | 948 | default: |
paul@23 | 949 | break; |
paul@23 | 950 | } |
paul@23 | 951 | |
paul@34 | 952 | current = usb_descriptor_next(current); |
paul@34 | 953 | |
paul@34 | 954 | if (current == NULL) |
paul@23 | 955 | { |
paul@23 | 956 | printf("END (zero length record)\n"); |
paul@23 | 957 | break; |
paul@23 | 958 | } |
paul@23 | 959 | } |
paul@23 | 960 | } |
paul@23 | 961 | |
paul@35 | 962 | void usb_show_configurations(max_device *device) |
paul@35 | 963 | { |
paul@35 | 964 | uint8_t n; |
paul@35 | 965 | |
paul@35 | 966 | for (n = 0; n < device->desc.bNumConfigurations; n++) |
paul@35 | 967 | { |
paul@35 | 968 | usb_show_configuration_full(max_get_descriptor(device, USB_DT_CONFIG, n, 0, 0)); |
paul@35 | 969 | } |
paul@35 | 970 | } |
paul@35 | 971 | |
paul@35 | 972 | bool usb_configuration_has_interface(uint8_t *data, uint8_t bInterfaceClass, uint8_t bInterfaceProtocol) |
paul@35 | 973 | { |
paul@35 | 974 | struct usb_config_descriptor *conf; |
paul@35 | 975 | struct usb_descriptor_header *desc; |
paul@35 | 976 | struct usb_interface_descriptor *intf; |
paul@35 | 977 | uint8_t *current, *last; |
paul@35 | 978 | |
paul@35 | 979 | if (data == NULL) |
paul@35 | 980 | return false; |
paul@35 | 981 | |
paul@35 | 982 | conf = usb_get_config_descriptor(data); |
paul@35 | 983 | current = usb_descriptor_start(conf); |
paul@35 | 984 | last = usb_descriptor_end(conf); |
paul@35 | 985 | |
paul@35 | 986 | while ((current != NULL) && (current < last)) |
paul@35 | 987 | { |
paul@35 | 988 | desc = (struct usb_descriptor_header *) current; |
paul@35 | 989 | switch (desc->bDescriptorType) |
paul@35 | 990 | { |
paul@35 | 991 | case USB_DT_INTERFACE: |
paul@35 | 992 | intf = (struct usb_interface_descriptor *) current; |
paul@35 | 993 | if ((intf->bInterfaceClass == bInterfaceClass) && (intf->bInterfaceProtocol == bInterfaceProtocol)) |
paul@35 | 994 | return true; |
paul@35 | 995 | break; |
paul@35 | 996 | |
paul@35 | 997 | default: |
paul@35 | 998 | break; |
paul@35 | 999 | } |
paul@35 | 1000 | |
paul@35 | 1001 | current = usb_descriptor_next(current); |
paul@35 | 1002 | } |
paul@35 | 1003 | |
paul@35 | 1004 | return false; |
paul@35 | 1005 | } |
paul@35 | 1006 | |
paul@35 | 1007 | bool usb_configure_device(max_device *device) |
paul@35 | 1008 | { |
paul@35 | 1009 | uint8_t n, *data; |
paul@35 | 1010 | |
paul@35 | 1011 | for (n = 0; n < device->desc.bNumConfigurations; n++) |
paul@35 | 1012 | { |
paul@35 | 1013 | data = max_get_descriptor(device, USB_DT_CONFIG, n, 0, 0); |
paul@35 | 1014 | |
paul@35 | 1015 | /* USB HID mouse. */ |
paul@35 | 1016 | |
paul@35 | 1017 | if (usb_configuration_has_interface(data, 3, 2)) |
paul@35 | 1018 | { |
paul@35 | 1019 | return true; |
paul@35 | 1020 | } |
paul@35 | 1021 | } |
paul@35 | 1022 | |
paul@35 | 1023 | return false; |
paul@35 | 1024 | } |
paul@35 | 1025 | |
paul@21 | 1026 | /** |
paul@21 | 1027 | * Handle termination of the process. |
paul@21 | 1028 | */ |
paul@8 | 1029 | void shutdown(int signum) |
paul@8 | 1030 | { |
paul@8 | 1031 | printf("Closing...\n"); |
paul@8 | 1032 | ubb_close(0); |
paul@8 | 1033 | exit(1); |
paul@8 | 1034 | } |
paul@8 | 1035 | |
paul@0 | 1036 | int main(int argc, char *argv[]) |
paul@0 | 1037 | { |
paul@27 | 1038 | uint8_t status = 0, revision = 0, framecount; |
paul@7 | 1039 | uint16_t count; |
paul@19 | 1040 | bool bus_event, data_event, suspended_event, connection_event, frame_event; |
paul@19 | 1041 | max_device device; |
paul@15 | 1042 | max_devstate devstate = MAX_DEVSTATE_INIT; |
paul@0 | 1043 | |
paul@19 | 1044 | device.in_toggle = false; |
paul@19 | 1045 | device.out_toggle = false; |
paul@26 | 1046 | device.max_packet_size = 8; |
paul@19 | 1047 | |
paul@8 | 1048 | signal(SIGINT, &shutdown); |
paul@8 | 1049 | |
paul@0 | 1050 | if (ubb_open(0) < 0) { |
paul@0 | 1051 | perror("ubb_open"); |
paul@0 | 1052 | return 1; |
paul@0 | 1053 | } |
paul@0 | 1054 | |
paul@0 | 1055 | ubb_power(1); |
paul@0 | 1056 | printf("Power on.\n"); |
paul@0 | 1057 | |
paul@0 | 1058 | OUT(MAX_SS); |
paul@0 | 1059 | OUT(MAX_MOSI); |
paul@0 | 1060 | OUT(MAX_SCLK); |
paul@0 | 1061 | OUT(MAX_RESET); |
paul@0 | 1062 | IN(MAX_INT); |
paul@0 | 1063 | IN(MAX_MISO); |
paul@0 | 1064 | |
paul@0 | 1065 | /* Initialise SPI. */ |
paul@7 | 1066 | /* Set SS# to 1. */ |
paul@0 | 1067 | |
paul@0 | 1068 | SET(MAX_SS); |
paul@0 | 1069 | CLR(MAX_MOSI); |
paul@0 | 1070 | CLR(MAX_SCLK); |
paul@7 | 1071 | SET(MAX_RESET); |
paul@0 | 1072 | |
paul@0 | 1073 | /* Initialise the MAX3421E. */ |
paul@0 | 1074 | |
paul@0 | 1075 | /* Set full-duplex, interrupt signalling. */ |
paul@0 | 1076 | |
paul@0 | 1077 | printf("Setting pin control...\n"); |
paul@6 | 1078 | max_write(MAX_REG_PINCTL, MAX_PINCTL_INTLEVEL_LEVEL | MAX_PINCTL_FDUPSPI_FULL); |
paul@0 | 1079 | |
paul@3 | 1080 | chipreset(); |
paul@7 | 1081 | printf("Ready? %d\n", wait()); |
paul@7 | 1082 | |
paul@7 | 1083 | /* Check various registers. */ |
paul@7 | 1084 | |
paul@7 | 1085 | printf("Mode: %x\n", max_read(MAX_REG_MODE, &status)); |
paul@7 | 1086 | printf("IRQ: %x\n", max_read(MAX_REG_HIRQ, &status)); |
paul@0 | 1087 | |
paul@0 | 1088 | /* Set host mode. */ |
paul@0 | 1089 | |
paul@0 | 1090 | printf("Setting mode...\n"); |
paul@12 | 1091 | status = max_write(MAX_REG_MODE, MAX_MODE_HOST_ENABLED); |
paul@7 | 1092 | |
paul@7 | 1093 | printf("Setting INT signalling...\n"); |
paul@7 | 1094 | status = max_write(MAX_REG_CPUCTL, MAX_CPUCTL_IE); |
paul@7 | 1095 | |
paul@15 | 1096 | printf("Setting event signalling...\n"); |
paul@15 | 1097 | status = max_write(MAX_REG_HIEN, MAX_HIEN_CONDETIE | MAX_HIEN_FRAMEIE | MAX_HIEN_BUSEVENTIE); |
paul@7 | 1098 | |
paul@7 | 1099 | /* Check various registers. */ |
paul@7 | 1100 | |
paul@7 | 1101 | printf("Mode: %x\n", max_read(MAX_REG_MODE, &status)); |
paul@7 | 1102 | printf("IRQ: %x\n", max_read(MAX_REG_HIRQ, &status)); |
paul@7 | 1103 | printf("IE: %x\n", max_read(MAX_REG_HIEN, &status)); |
paul@7 | 1104 | printf("CPU: %x\n", max_read(MAX_REG_CPUCTL, &status)); |
paul@7 | 1105 | printf("Pin: %x\n", max_read(MAX_REG_PINCTL, &status)); |
paul@7 | 1106 | printf("USBIRQ: %x\n", max_read(MAX_REG_USBIRQ, &status)); |
paul@7 | 1107 | printf("USBIE: %x\n", max_read(MAX_REG_USBIEN, &status)); |
paul@0 | 1108 | |
paul@0 | 1109 | /* Read from the REVISION register. */ |
paul@0 | 1110 | |
paul@0 | 1111 | printf("Reading...\n"); |
paul@6 | 1112 | revision = max_read(MAX_REG_REVISION, &status); |
paul@0 | 1113 | printf("Revision = %x\n", revision); |
paul@7 | 1114 | |
paul@18 | 1115 | /* Sample the bus for any devices that are already connected. */ |
paul@18 | 1116 | |
paul@18 | 1117 | samplebus(); |
paul@18 | 1118 | |
paul@18 | 1119 | if (devicechanged()) |
paul@18 | 1120 | { |
paul@18 | 1121 | devstate = MAX_DEVSTATE_CONNECTED; |
paul@27 | 1122 | framecount = 200; |
paul@18 | 1123 | } |
paul@18 | 1124 | |
paul@18 | 1125 | /* Enter the main processing loop. */ |
paul@18 | 1126 | |
paul@8 | 1127 | for (count = 0; count <= 65535; count++) |
paul@7 | 1128 | { |
paul@8 | 1129 | if (!PIN(MAX_INT)) |
paul@7 | 1130 | { |
paul@19 | 1131 | /* Obtain interrupt conditions. */ |
paul@19 | 1132 | |
paul@9 | 1133 | status = max_read(MAX_REG_HIRQ, NULL); |
paul@9 | 1134 | |
paul@19 | 1135 | bus_event = status & MAX_HIRQ_BUSEVENTIRQ; |
paul@19 | 1136 | data_event = status & MAX_HIRQ_RCVDAVIRQ; |
paul@19 | 1137 | suspended_event = status & MAX_HIRQ_SUSDNIRQ; |
paul@19 | 1138 | connection_event = status & MAX_HIRQ_CONDETIRQ; |
paul@16 | 1139 | frame_event = status & MAX_HIRQ_FRAMEIRQ; |
paul@19 | 1140 | |
paul@28 | 1141 | #ifdef DEBUG_EVENTS |
paul@19 | 1142 | if (bus_event) printf("Bus "); |
paul@19 | 1143 | if (data_event) printf("Data "); |
paul@19 | 1144 | if (suspended_event) printf("Suspended "); |
paul@19 | 1145 | if (connection_event) printf("Connection "); |
paul@19 | 1146 | if (frame_event) printf("Frame "); |
paul@15 | 1147 | printf("\n"); |
paul@19 | 1148 | #endif |
paul@19 | 1149 | |
paul@19 | 1150 | /* Acknowledge the interrupts. */ |
paul@15 | 1151 | |
paul@16 | 1152 | max_write(MAX_REG_HIRQ, status); |
paul@16 | 1153 | |
paul@15 | 1154 | /* Detect device connection/disconnection. */ |
paul@15 | 1155 | |
paul@16 | 1156 | if ((devstate == MAX_DEVSTATE_INIT) && connection_event && devicechanged()) |
paul@13 | 1157 | { |
paul@15 | 1158 | devstate = MAX_DEVSTATE_CONNECTED; |
paul@27 | 1159 | framecount = 200; |
paul@19 | 1160 | printf("CONNECTED\n"); |
paul@13 | 1161 | } |
paul@15 | 1162 | |
paul@27 | 1163 | /* Handle device connection. */ |
paul@27 | 1164 | |
paul@28 | 1165 | else if ((devstate == MAX_DEVSTATE_CONNECTED) && frame_event && !(--framecount)) |
paul@27 | 1166 | { |
paul@28 | 1167 | /* Reset the device. */ |
paul@27 | 1168 | |
paul@28 | 1169 | max_write(MAX_REG_HCTL, MAX_HCTL_BUSRST); |
paul@28 | 1170 | devstate = MAX_DEVSTATE_START; |
paul@28 | 1171 | printf("START\n"); |
paul@27 | 1172 | } |
paul@27 | 1173 | |
paul@15 | 1174 | /* Handle device reset initiation. */ |
paul@15 | 1175 | |
paul@27 | 1176 | else if ((devstate == MAX_DEVSTATE_START) && bus_event) |
paul@15 | 1177 | { |
paul@15 | 1178 | devstate = MAX_DEVSTATE_RESET; |
paul@27 | 1179 | framecount = 200; |
paul@19 | 1180 | printf("RESET\n"); |
paul@15 | 1181 | } |
paul@15 | 1182 | |
paul@19 | 1183 | /* Handle device reset completion, getting device details and |
paul@27 | 1184 | resetting the device again. */ |
paul@27 | 1185 | |
paul@28 | 1186 | else if ((devstate == MAX_DEVSTATE_RESET) && frame_event && !(--framecount)) |
paul@27 | 1187 | { |
paul@28 | 1188 | if (!max_init_device(&device)) |
paul@27 | 1189 | printf("FAILED: RESET -> INSPECTED\n"); |
paul@27 | 1190 | else |
paul@27 | 1191 | { |
paul@27 | 1192 | devstate = MAX_DEVSTATE_INSPECTED; |
paul@27 | 1193 | printf("INSPECTED\n"); |
paul@27 | 1194 | } |
paul@27 | 1195 | } |
paul@15 | 1196 | |
paul@27 | 1197 | /* Handle second reset initiation. */ |
paul@27 | 1198 | |
paul@27 | 1199 | else if ((devstate == MAX_DEVSTATE_INSPECTED) && bus_event) |
paul@15 | 1200 | { |
paul@27 | 1201 | devstate = MAX_DEVSTATE_RESET_AGAIN; |
paul@27 | 1202 | framecount = 200; |
paul@27 | 1203 | printf("RESET_AGAIN\n"); |
paul@27 | 1204 | } |
paul@27 | 1205 | |
paul@27 | 1206 | /* Handle second reset completion, setting the address. */ |
paul@27 | 1207 | |
paul@28 | 1208 | else if ((devstate == MAX_DEVSTATE_RESET_AGAIN) && frame_event && !(--framecount)) |
paul@27 | 1209 | { |
paul@32 | 1210 | max_set_address(&device); |
paul@32 | 1211 | devstate = MAX_DEVSTATE_ADDRESSING; |
paul@32 | 1212 | framecount = 30; |
paul@32 | 1213 | printf("ADDRESSING\n"); |
paul@27 | 1214 | } |
paul@27 | 1215 | |
paul@27 | 1216 | /* Post-addressing. */ |
paul@27 | 1217 | |
paul@28 | 1218 | else if ((devstate == MAX_DEVSTATE_ADDRESSING) && frame_event && !(--framecount)) |
paul@27 | 1219 | { |
paul@32 | 1220 | if (usb_init_device(&device, max_get_descriptor(&device, USB_DT_DEVICE, 0, 0, usb_descriptor_size(USB_DT_DEVICE)))) |
paul@32 | 1221 | usb_show_device(&device); |
paul@32 | 1222 | else |
paul@32 | 1223 | printf("DEVICE?\n"); |
paul@32 | 1224 | |
paul@32 | 1225 | usb_show_languages(max_get_descriptor(&device, USB_DT_STRING, 0, 0, 0)); |
paul@35 | 1226 | usb_show_configurations(&device); |
paul@28 | 1227 | devstate = MAX_DEVSTATE_READY; |
paul@28 | 1228 | printf("READY\n"); |
paul@35 | 1229 | |
paul@35 | 1230 | /* Detect supported interfaces and configure devices to use them. */ |
paul@35 | 1231 | |
paul@35 | 1232 | /* usb_configure_device(&device); */ |
paul@15 | 1233 | } |
paul@7 | 1234 | |
paul@16 | 1235 | /* Handle device disconnection. */ |
paul@16 | 1236 | |
paul@33 | 1237 | else if ((devstate != MAX_DEVSTATE_INIT) && (devstate != MAX_DEVSTATE_CONNECTED) |
paul@33 | 1238 | && connection_event && !devicechanged()) |
paul@16 | 1239 | { |
paul@16 | 1240 | devstate = MAX_DEVSTATE_INIT; |
paul@19 | 1241 | printf("INIT\n"); |
paul@16 | 1242 | } |
paul@7 | 1243 | } |
paul@7 | 1244 | } |
paul@0 | 1245 | |
paul@0 | 1246 | printf("Closing...\n"); |
paul@0 | 1247 | ubb_close(0); |
paul@0 | 1248 | |
paul@0 | 1249 | return 0; |
paul@0 | 1250 | } |