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 samplebusready() 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 samplebus() 233 { 234 max_write(MAX_REG_HCTL, MAX_HCTL_SAMPLEBUS); 235 while (!samplebusready()); 236 } 237 238 void devicechanged() 239 { 240 uint8_t hrsl, mode; 241 242 hrsl = max_read(MAX_REG_HRSL, NULL); 243 mode = max_read(MAX_REG_MODE, NULL); 244 245 if ((hrsl & MAX_HRSL_JSTATUS) && (hrsl & MAX_HRSL_KSTATUS)) 246 { 247 printf("Bad device status.\n"); 248 } 249 else if (!(hrsl & MAX_HRSL_JSTATUS) && !(hrsl & MAX_HRSL_KSTATUS)) 250 { 251 printf("Device disconnected.\n"); 252 } 253 else 254 { 255 printf("Device connected.\n"); 256 257 /* Low speed device when J and lowspeed have the same level. 258 Since J and K should have opposing levels, K can be tested when 259 lowspeed is low. */ 260 261 if (((hrsl & MAX_HRSL_JSTATUS) && (mode & MAX_MODE_LOWSPEED)) || 262 ((hrsl & MAX_HRSL_KSTATUS) && !(mode & MAX_MODE_LOWSPEED))) 263 { 264 printf("Device is low speed.\n"); 265 max_write(MAX_REG_MODE, MAX_MODE_HOST | MAX_MODE_SEPIRQ_OFF | MAX_MODE_DMPULLDN | MAX_MODE_DPPULLDN | MAX_MODE_LOWSPEED); 266 } 267 else 268 { 269 printf("Device is full speed.\n"); 270 max_write(MAX_REG_MODE, MAX_MODE_HOST | MAX_MODE_SEPIRQ_OFF | MAX_MODE_DMPULLDN | MAX_MODE_DPPULLDN); 271 } 272 } 273 } 274 275 void shutdown(int signum) 276 { 277 printf("Closing...\n"); 278 ubb_close(0); 279 exit(1); 280 } 281 282 int main(int argc, char *argv[]) 283 { 284 uint8_t status = 0, revision = 0; 285 uint16_t count; 286 287 signal(SIGINT, &shutdown); 288 289 if (ubb_open(0) < 0) { 290 perror("ubb_open"); 291 return 1; 292 } 293 294 ubb_power(1); 295 printf("Power on.\n"); 296 297 OUT(MAX_SS); 298 OUT(MAX_MOSI); 299 OUT(MAX_SCLK); 300 OUT(MAX_RESET); 301 IN(MAX_INT); 302 IN(MAX_MISO); 303 304 /* Initialise SPI. */ 305 /* Set SS# to 1. */ 306 307 SET(MAX_SS); 308 CLR(MAX_MOSI); 309 CLR(MAX_SCLK); 310 SET(MAX_RESET); 311 312 /* Initialise the MAX3421E. */ 313 314 /* Set full-duplex, interrupt signalling. */ 315 316 printf("Setting pin control...\n"); 317 max_write(MAX_REG_PINCTL, MAX_PINCTL_INTLEVEL_LEVEL | MAX_PINCTL_FDUPSPI_FULL); 318 319 chipreset(); 320 printf("Ready? %d\n", wait()); 321 322 /* Check various registers. */ 323 324 printf("Mode: %x\n", max_read(MAX_REG_MODE, &status)); 325 printf("IRQ: %x\n", max_read(MAX_REG_HIRQ, &status)); 326 327 /* Set host mode. */ 328 329 printf("Setting mode...\n"); 330 status = max_write(MAX_REG_MODE, MAX_MODE_HOST | MAX_MODE_SEPIRQ_OFF | MAX_MODE_DMPULLDN | MAX_MODE_DPPULLDN); 331 332 printf("Setting INT signalling...\n"); 333 status = max_write(MAX_REG_CPUCTL, MAX_CPUCTL_IE); 334 335 printf("Setting connection signalling...\n"); 336 status = max_write(MAX_REG_HIEN, MAX_HIEN_CONDETIE); 337 338 /* Check various registers. */ 339 340 printf("Mode: %x\n", max_read(MAX_REG_MODE, &status)); 341 printf("IRQ: %x\n", max_read(MAX_REG_HIRQ, &status)); 342 printf("IE: %x\n", max_read(MAX_REG_HIEN, &status)); 343 printf("CPU: %x\n", max_read(MAX_REG_CPUCTL, &status)); 344 printf("Pin: %x\n", max_read(MAX_REG_PINCTL, &status)); 345 printf("USBIRQ: %x\n", max_read(MAX_REG_USBIRQ, &status)); 346 printf("USBIE: %x\n", max_read(MAX_REG_USBIEN, &status)); 347 348 /* Read from the REVISION register. */ 349 350 printf("Reading...\n"); 351 revision = max_read(MAX_REG_REVISION, &status); 352 printf("Revision = %x\n", revision); 353 354 for (count = 0; count <= 65535; count++) 355 { 356 if (!PIN(MAX_INT)) 357 { 358 status = max_read(MAX_REG_HIRQ, NULL); 359 360 if (status & MAX_HIRQ_CONDETIRQ) 361 devicechanged(); 362 if (status & MAX_HIRQ_SUSDNIRQ) 363 printf("Suspend done.\n"); 364 if (status & MAX_HIRQ_BUSEVENTIRQ) 365 printf("Bus event.\n"); 366 if (status & MAX_HIRQ_RCVDAVIRQ) 367 printf("Data received.\n"); 368 369 max_write(MAX_REG_HIRQ, status); 370 } 371 } 372 373 printf("Closing...\n"); 374 ubb_close(0); 375 376 return 0; 377 }