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