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