paul@7 | 1 | /* |
paul@7 | 2 | * Ben NanoNote and Arduino USB Host shield communication. |
paul@7 | 3 | * |
paul@7 | 4 | * Copyright 2013 Paul Boddie |
paul@7 | 5 | * |
paul@7 | 6 | * SPI functions derived from those in lib/atben.c by Werner Almesberger: |
paul@7 | 7 | * |
paul@7 | 8 | * Copyright 2010-2011 Werner Almesberger |
paul@7 | 9 | * |
paul@7 | 10 | * This program is free software; you can redistribute it and/or modify |
paul@7 | 11 | * it under the terms of the GNU General Public License as published by |
paul@7 | 12 | * the Free Software Foundation; either version 2 of the License, or |
paul@7 | 13 | * (at your option) any later version. |
paul@7 | 14 | */ |
paul@7 | 15 | |
paul@7 | 16 | #include <ubb/ubb.h> |
paul@7 | 17 | #include <stdio.h> |
paul@7 | 18 | #include <time.h> |
paul@7 | 19 | #include <unistd.h> |
paul@7 | 20 | |
paul@7 | 21 | /* Pin assignments: |
paul@7 | 22 | * |
paul@7 | 23 | * Sniffer UBB Shield |
paul@7 | 24 | * ------- ---- ------ |
paul@7 | 25 | * DAT2 DAT2 9 (INT) |
paul@7 | 26 | * CD DAT3 10 (SS) |
paul@7 | 27 | * CMD CMD 7 (RESET) |
paul@7 | 28 | * VCC VDD VIN |
paul@7 | 29 | * CLK CLK 13 (SCLK) |
paul@7 | 30 | * GND GND GND |
paul@7 | 31 | * DAT0 DAT0 11 (MOSI) |
paul@7 | 32 | * DAT1 DAT1 12 (MISO) |
paul@7 | 33 | * 8 (GPX) (not assigned) |
paul@7 | 34 | */ |
paul@7 | 35 | |
paul@7 | 36 | #define MAX_RESET UBB_CMD |
paul@7 | 37 | #define MAX_SCLK UBB_CLK |
paul@7 | 38 | #define MAX_MOSI UBB_DAT0 |
paul@7 | 39 | #define MAX_MISO UBB_DAT1 |
paul@7 | 40 | #define MAX_INT UBB_DAT2 |
paul@7 | 41 | #define MAX_SS UBB_DAT3 |
paul@7 | 42 | |
paul@7 | 43 | /* MAX3421E definitions. */ |
paul@7 | 44 | |
paul@7 | 45 | #define MAX_REG_READ 0x00 |
paul@7 | 46 | #define MAX_REG_WRITE 0x02 |
paul@7 | 47 | |
paul@7 | 48 | #define MAX_REG_USBIRQ 13 |
paul@7 | 49 | #define MAX_REG_USBIEN 14 |
paul@7 | 50 | #define MAX_REG_USBCTL 15 |
paul@7 | 51 | #define MAX_REG_CPUCTL 16 |
paul@7 | 52 | #define MAX_REG_PINCTL 17 |
paul@7 | 53 | #define MAX_REG_REVISION 18 |
paul@7 | 54 | #define MAX_REG_GPINPOL 24 |
paul@7 | 55 | #define MAX_REG_HIRQ 25 |
paul@7 | 56 | #define MAX_REG_HIEN 26 |
paul@7 | 57 | #define MAX_REG_MODE 27 |
paul@7 | 58 | #define MAX_REG_HCTL 29 |
paul@7 | 59 | #define MAX_REG_HRSL 31 |
paul@7 | 60 | |
paul@7 | 61 | #define MAX_USBIRQ_OSCOKIRQ 1 |
paul@7 | 62 | #define MAX_USBIRQ_NOVBUSIRQ 32 |
paul@7 | 63 | #define MAX_USBIRQ_VBUSIRQ 64 |
paul@7 | 64 | |
paul@7 | 65 | #define MAX_USBCTL_PWRDOWN 16 |
paul@7 | 66 | #define MAX_USBCTL_CHIPRES 32 |
paul@7 | 67 | |
paul@7 | 68 | #define MAX_CPUCTL_IE 1 |
paul@7 | 69 | |
paul@7 | 70 | #define MAX_PINCTL_POSINT_LOW 0 |
paul@7 | 71 | #define MAX_PINCTL_POSINT_HIGH 4 |
paul@7 | 72 | #define MAX_PINCTL_INTLEVEL_EDGE 0 |
paul@7 | 73 | #define MAX_PINCTL_INTLEVEL_LEVEL 8 |
paul@7 | 74 | #define MAX_PINCTL_FDUPSPI_HALF 0 |
paul@7 | 75 | #define MAX_PINCTL_FDUPSPI_FULL 16 |
paul@7 | 76 | |
paul@7 | 77 | #define MAX_HIRQ_BUSEVENTIRQ 1 |
paul@7 | 78 | #define MAX_HIRQ_RWUIRQ 2 |
paul@7 | 79 | #define MAX_HIRQ_RCVDAVIRQ 4 |
paul@7 | 80 | #define MAX_HIRQ_SNDBAVIRQ 8 |
paul@7 | 81 | #define MAX_HIRQ_SUSDNIRQ 16 |
paul@7 | 82 | #define MAX_HIRQ_CONDETIRQ 32 |
paul@7 | 83 | #define MAX_HIRQ_FRAMEIRQ 64 |
paul@7 | 84 | #define MAX_HIRQ_HXFRDNIRQ 128 |
paul@7 | 85 | |
paul@7 | 86 | #define MAX_HIEN_CONDETIE 32 |
paul@7 | 87 | |
paul@7 | 88 | #define MAX_MODE_PERIPHERAL 0 |
paul@7 | 89 | #define MAX_MODE_HOST 1 |
paul@7 | 90 | #define MAX_MODE_LOWSPEED 2 |
paul@7 | 91 | #define MAX_MODE_SOFKAENAB 8 |
paul@7 | 92 | #define MAX_MODE_SEPIRQ_OFF 0 |
paul@7 | 93 | #define MAX_MODE_SEPIRQ_ON 16 |
paul@7 | 94 | #define MAX_MODE_DMPULLDN 64 |
paul@7 | 95 | #define MAX_MODE_DPPULLDN 128 |
paul@7 | 96 | |
paul@7 | 97 | #define MAX_HCTL_SAMPLEBUS 4 |
paul@7 | 98 | |
paul@7 | 99 | #define MAX_HRSL_JSTATUS 128 |
paul@7 | 100 | #define MAX_HRSL_KSTATUS 64 |
paul@7 | 101 | |
paul@7 | 102 | #define max_reg(n) ((uint8_t) (n << 3)) |
paul@7 | 103 | #define max_reg_read(n) (max_reg(n) | MAX_REG_READ) |
paul@7 | 104 | #define max_reg_write(n) (max_reg(n) | MAX_REG_WRITE) |
paul@7 | 105 | |
paul@7 | 106 | #define nanosleep(a, b) if (0) |
paul@7 | 107 | #define usleep(a) if (0) |
paul@7 | 108 | |
paul@7 | 109 | struct timespec tCSS = {0, 20}, |
paul@7 | 110 | tL = {0, 30}, |
paul@7 | 111 | tCSW = {0, 200}, |
paul@7 | 112 | tCL = {0, 17}, |
paul@7 | 113 | tCH = {0, 17}, |
paul@7 | 114 | tDS = {0, 5}, |
paul@7 | 115 | tDH = {0, 10}, |
paul@7 | 116 | tDO = {0, 15}, |
paul@7 | 117 | tDI = {0, 15}, |
paul@7 | 118 | tON = {0, 4}, |
paul@7 | 119 | tRESET = {0, 200}, |
paul@7 | 120 | tTEST = {0, 100}; |
paul@7 | 121 | |
paul@7 | 122 | void spi_begin() |
paul@7 | 123 | { |
paul@7 | 124 | CLR(MAX_SS); |
paul@7 | 125 | nanosleep(&tL, NULL); /* tCSS is the minimum, but tL is more conservative */ |
paul@7 | 126 | } |
paul@7 | 127 | |
paul@7 | 128 | void spi_end() |
paul@7 | 129 | { |
paul@7 | 130 | SET(MAX_SS); |
paul@7 | 131 | nanosleep(&tCSW, NULL); |
paul@7 | 132 | } |
paul@7 | 133 | |
paul@7 | 134 | /** |
paul@7 | 135 | * Send the given value via MOSI while receiving a value via MISO. |
paul@7 | 136 | * This requires full-duplex SPI and will produce a status value for the first |
paul@7 | 137 | * value sent (the command). |
paul@7 | 138 | */ |
paul@7 | 139 | uint8_t spi_sendrecv(uint8_t v) |
paul@7 | 140 | { |
paul@7 | 141 | uint8_t result = 0; |
paul@7 | 142 | uint8_t mask; |
paul@7 | 143 | |
paul@7 | 144 | for (mask = 0x80; mask; mask >>= 1) |
paul@7 | 145 | { |
paul@7 | 146 | if (v & mask) |
paul@7 | 147 | { |
paul@7 | 148 | #ifdef DEBUG |
paul@7 | 149 | printf("1"); |
paul@7 | 150 | #endif |
paul@7 | 151 | SET(MAX_MOSI); |
paul@7 | 152 | } |
paul@7 | 153 | else |
paul@7 | 154 | { |
paul@7 | 155 | #ifdef DEBUG |
paul@7 | 156 | printf("0"); |
paul@7 | 157 | #endif |
paul@7 | 158 | CLR(MAX_MOSI); |
paul@7 | 159 | } |
paul@7 | 160 | |
paul@7 | 161 | /* Wait for stable output signal. */ |
paul@7 | 162 | |
paul@7 | 163 | nanosleep(&tDS, NULL); |
paul@7 | 164 | |
paul@7 | 165 | SET(MAX_SCLK); |
paul@7 | 166 | |
paul@7 | 167 | if (PIN(MAX_MISO)) |
paul@7 | 168 | result |= mask; |
paul@7 | 169 | |
paul@7 | 170 | nanosleep(&tCH, NULL); |
paul@7 | 171 | CLR(MAX_SCLK); |
paul@7 | 172 | nanosleep(&tCL, NULL); |
paul@7 | 173 | } |
paul@7 | 174 | |
paul@7 | 175 | #ifdef DEBUG |
paul@7 | 176 | printf("\n"); |
paul@7 | 177 | #endif |
paul@7 | 178 | return result; |
paul@7 | 179 | } |
paul@7 | 180 | |
paul@7 | 181 | uint8_t max_read(uint8_t reg, uint8_t *status) |
paul@7 | 182 | { |
paul@7 | 183 | uint8_t result = 0, tmpstatus = 0; |
paul@7 | 184 | |
paul@7 | 185 | tmpstatus = 0; |
paul@7 | 186 | |
paul@7 | 187 | spi_begin(); |
paul@7 | 188 | tmpstatus = spi_sendrecv(max_reg_read(reg)); |
paul@7 | 189 | result = spi_sendrecv(0); |
paul@7 | 190 | spi_end(); |
paul@7 | 191 | |
paul@7 | 192 | if (status != NULL) |
paul@7 | 193 | *status = tmpstatus; |
paul@7 | 194 | |
paul@7 | 195 | return result; |
paul@7 | 196 | } |
paul@7 | 197 | |
paul@7 | 198 | uint8_t max_write(uint8_t reg, uint8_t value) |
paul@7 | 199 | { |
paul@7 | 200 | uint8_t status = 0; |
paul@7 | 201 | |
paul@7 | 202 | spi_begin(); |
paul@7 | 203 | status = spi_sendrecv(max_reg_write(reg)); |
paul@7 | 204 | spi_sendrecv(value); |
paul@7 | 205 | spi_end(); |
paul@7 | 206 | |
paul@7 | 207 | return status; |
paul@7 | 208 | } |
paul@7 | 209 | |
paul@7 | 210 | void chipreset() |
paul@7 | 211 | { |
paul@7 | 212 | printf("Resetting...\n"); |
paul@7 | 213 | max_write(MAX_REG_USBCTL, MAX_USBCTL_CHIPRES); |
paul@7 | 214 | |
paul@7 | 215 | printf("Clearing the reset...\n"); |
paul@7 | 216 | max_write(MAX_REG_USBCTL, 0); |
paul@7 | 217 | } |
paul@7 | 218 | |
paul@7 | 219 | uint8_t check() |
paul@7 | 220 | { |
paul@7 | 221 | uint8_t oscillator; |
paul@7 | 222 | |
paul@7 | 223 | oscillator = max_read(MAX_REG_USBIRQ, NULL); |
paul@7 | 224 | |
paul@7 | 225 | return (oscillator & ~(MAX_USBIRQ_NOVBUSIRQ | MAX_USBIRQ_VBUSIRQ)) == MAX_USBIRQ_OSCOKIRQ; |
paul@7 | 226 | } |
paul@7 | 227 | |
paul@7 | 228 | uint8_t wait() |
paul@7 | 229 | { |
paul@7 | 230 | uint16_t timeout = 1024; |
paul@7 | 231 | |
paul@7 | 232 | /* Wait for the oscillator before performing USB activity. */ |
paul@7 | 233 | |
paul@7 | 234 | printf("Waiting...\n"); |
paul@7 | 235 | |
paul@7 | 236 | while ((timeout > 0) && (!check())) |
paul@7 | 237 | { |
paul@7 | 238 | usleep(3000); /* 3ms */ |
paul@7 | 239 | timeout--; |
paul@7 | 240 | } |
paul@7 | 241 | |
paul@7 | 242 | printf("Iterations remaining: %d\n", timeout); |
paul@7 | 243 | |
paul@7 | 244 | return timeout; |
paul@7 | 245 | } |
paul@7 | 246 | |
paul@7 | 247 | int main(int argc, char *argv[]) |
paul@7 | 248 | { |
paul@7 | 249 | uint8_t status = 0, revision = 0, result = 0; |
paul@7 | 250 | uint16_t count; |
paul@7 | 251 | |
paul@7 | 252 | if (ubb_open(0) < 0) { |
paul@7 | 253 | perror("ubb_open"); |
paul@7 | 254 | return 1; |
paul@7 | 255 | } |
paul@7 | 256 | |
paul@7 | 257 | ubb_power(1); |
paul@7 | 258 | printf("Power on.\n"); |
paul@7 | 259 | |
paul@7 | 260 | OUT(MAX_SS); |
paul@7 | 261 | OUT(MAX_MOSI); |
paul@7 | 262 | OUT(MAX_SCLK); |
paul@7 | 263 | OUT(MAX_RESET); |
paul@7 | 264 | IN(MAX_INT); |
paul@7 | 265 | IN(MAX_MISO); |
paul@7 | 266 | |
paul@7 | 267 | /* Initialise SPI. */ |
paul@7 | 268 | /* Set SS# to 1. */ |
paul@7 | 269 | |
paul@7 | 270 | SET(MAX_SS); |
paul@7 | 271 | CLR(MAX_MOSI); |
paul@7 | 272 | CLR(MAX_SCLK); |
paul@7 | 273 | SET(MAX_RESET); |
paul@7 | 274 | |
paul@7 | 275 | /* Initialise the MAX3421E. */ |
paul@7 | 276 | |
paul@7 | 277 | /* Set full-duplex, interrupt signalling. */ |
paul@7 | 278 | |
paul@7 | 279 | printf("Setting pin control...\n"); |
paul@7 | 280 | max_write(MAX_REG_PINCTL, MAX_PINCTL_INTLEVEL_LEVEL | MAX_PINCTL_FDUPSPI_FULL); |
paul@7 | 281 | |
paul@7 | 282 | chipreset(); |
paul@7 | 283 | printf("Ready? %d\n", wait()); |
paul@7 | 284 | |
paul@7 | 285 | /* Read from the REVISION register. */ |
paul@7 | 286 | |
paul@7 | 287 | printf("Reading...\n"); |
paul@7 | 288 | revision = max_read(MAX_REG_REVISION, &status); |
paul@7 | 289 | printf("Status = %x\n", status); |
paul@7 | 290 | printf("Revision = %x\n", revision); |
paul@7 | 291 | |
paul@7 | 292 | /* Check various registers. */ |
paul@7 | 293 | |
paul@7 | 294 | for (count = 0; count < 256; count++) |
paul@7 | 295 | { |
paul@7 | 296 | max_write(MAX_REG_GPINPOL, count); |
paul@7 | 297 | result = max_read(MAX_REG_GPINPOL, NULL); |
paul@7 | 298 | if (count != result) |
paul@7 | 299 | { |
paul@7 | 300 | printf("Count: %d\n", count); |
paul@7 | 301 | printf("Retrieved: %d\n", result); |
paul@7 | 302 | printf("Status = %x\n", status); |
paul@7 | 303 | } |
paul@7 | 304 | } |
paul@7 | 305 | |
paul@7 | 306 | printf("Closing...\n"); |
paul@7 | 307 | ubb_close(0); |
paul@7 | 308 | |
paul@7 | 309 | return 0; |
paul@7 | 310 | } |