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 <time.h> 19 #include <unistd.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_USBCTL 15 50 #define MAX_REG_PINCTL 17 51 #define MAX_REG_REVISION 18 52 #define MAX_REG_MODE 27 53 #define MAX_REG_HRSL 31 54 55 #define MAX_USBIRQ_OSCOKIRQ 1 56 #define MAX_USBIRQ_NOVBUSIRQ 32 57 #define MAX_USBIRQ_VBUSIRQ 64 58 59 #define MAX_USBCTL_PWRDOWN 16 60 #define MAX_USBCTL_CHIPRES 32 61 62 #define MAX_PINCTL_POSINT_LOW 0 63 #define MAX_PINCTL_POSINT_HIGH 4 64 #define MAX_PINCTL_INTLEVEL_EDGE 0 65 #define MAX_PINCTL_INTLEVEL_LEVEL 8 66 #define MAX_PINCTL_FDUPSPI_HALF 0 67 #define MAX_PINCTL_FDUPSPI_FULL 16 68 69 #define MAX_MODE_PERIPHERAL 0 70 #define MAX_MODE_HOST 1 71 #define MAX_MODE_SEPIRQ_OFF 0 72 #define MAX_MODE_SEPIRQ_ON 16 73 #define MAX_MODE_DMPULLDN 64 74 #define MAX_MODE_DPPULLDN 128 75 76 #define max_reg(n) ((uint8_t) (n << 3)) 77 #define max_reg_read(n) (max_reg(n) | MAX_REG_READ) 78 #define max_reg_write(n) (max_reg(n) | MAX_REG_WRITE) 79 80 struct timespec tCSS = {0, 20}, 81 tL = {0, 30}, 82 tCSW = {0, 200}, 83 tCL = {0, 17}, 84 tCH = {0, 17}, 85 tDS = {0, 5}, 86 tDH = {0, 10}, 87 tDO = {0, 15}, 88 tDI = {0, 15}, 89 tON = {0, 4}, 90 tRESET = {0, 200}, 91 tTEST = {0, 100}; 92 93 void spi_begin() 94 { 95 CLR(MAX_SS); 96 nanosleep(&tL, NULL); /* tCSS is the minimum, but tL is more conservative */ 97 } 98 99 void spi_end() 100 { 101 SET(MAX_SS); 102 nanosleep(&tCSW, NULL); 103 } 104 105 /** 106 * Send the given value via MOSI while receiving a value via MISO. 107 * This requires full-duplex SPI and will produce a status value for the first 108 * value sent (the command). 109 */ 110 uint8_t spi_sendrecv(uint8_t v) 111 { 112 uint8_t result = 0; 113 uint8_t mask; 114 115 for (mask = 0x80; mask; mask >>= 1) 116 { 117 if (v & mask) 118 { 119 printf("1"); 120 SET(MAX_MOSI); 121 } 122 else 123 { 124 printf("0"); 125 CLR(MAX_MOSI); 126 } 127 128 /* Wait for stable output signal. */ 129 130 nanosleep(&tDS, NULL); 131 132 SET(MAX_SCLK); 133 134 if (PIN(MAX_MISO)) 135 result |= mask; 136 137 nanosleep(&tCH, NULL); 138 CLR(MAX_SCLK); 139 nanosleep(&tCL, NULL); 140 } 141 142 printf("\n"); 143 return result; 144 } 145 146 uint8_t max_read(uint8_t reg, uint8_t *status) 147 { 148 uint8_t result = 0, tmpstatus = 0; 149 150 tmpstatus = 0; 151 152 spi_begin(); 153 tmpstatus = spi_sendrecv(max_reg_read(reg)); 154 result = spi_sendrecv(0); 155 spi_end(); 156 157 if (status != NULL) 158 *status = tmpstatus; 159 160 return result; 161 } 162 163 uint8_t max_write(uint8_t reg, uint8_t value) 164 { 165 uint8_t status = 0; 166 167 spi_begin(); 168 status = spi_sendrecv(max_reg_write(reg)); 169 spi_sendrecv(value); 170 spi_end(); 171 172 return status; 173 } 174 175 void reset() 176 { 177 SET(MAX_RESET); 178 nanosleep(&tRESET, NULL); 179 CLR(MAX_RESET); 180 } 181 182 void chipreset() 183 { 184 printf("Resetting...\n"); 185 max_write(MAX_REG_USBCTL, MAX_USBCTL_CHIPRES); 186 187 printf("Clearing the reset...\n"); 188 max_write(MAX_REG_USBCTL, 0); 189 } 190 191 uint8_t check() 192 { 193 uint8_t oscillator; 194 195 oscillator = max_read(MAX_REG_USBIRQ, NULL); 196 197 return (oscillator & ~(MAX_USBIRQ_NOVBUSIRQ | MAX_USBIRQ_VBUSIRQ)) == MAX_USBIRQ_OSCOKIRQ; 198 } 199 200 int main(int argc, char *argv[]) 201 { 202 uint8_t status = 0, revision = 0, hrsl = 0; 203 204 if (ubb_open(0) < 0) { 205 perror("ubb_open"); 206 return 1; 207 } 208 209 ubb_power(1); 210 printf("Power on.\n"); 211 212 OUT(MAX_SS); 213 OUT(MAX_MOSI); 214 OUT(MAX_SCLK); 215 OUT(MAX_RESET); 216 IN(MAX_INT); 217 IN(MAX_MISO); 218 219 /* Initialise SPI. */ 220 /* Set SS to 1 (or SS~ to 0). */ 221 222 SET(MAX_SS); 223 CLR(MAX_MOSI); 224 CLR(MAX_SCLK); 225 CLR(MAX_RESET); 226 227 /* Initialise the MAX3421E. */ 228 229 /* Set full-duplex, interrupt signalling. */ 230 231 printf("Setting pin control...\n"); 232 max_write(MAX_REG_PINCTL, MAX_PINCTL_INTLEVEL_LEVEL | MAX_PINCTL_FDUPSPI_FULL); 233 234 reset(); 235 chipreset(); 236 printf("Ready? %d\n", check()); 237 238 /* Set host mode. */ 239 240 printf("Setting mode...\n"); 241 status = max_write(MAX_REG_MODE, MAX_MODE_HOST | MAX_MODE_SEPIRQ_OFF | MAX_MODE_DMPULLDN | MAX_MODE_DPPULLDN); 242 printf("Status = %x\n", status); 243 printf("Ready? %d\n", check()); 244 245 /* Read from the REVISION register. */ 246 247 printf("Reading...\n"); 248 revision = max_read(MAX_REG_REVISION, &status); 249 printf("Status = %x\n", status); 250 printf("Revision = %x\n", revision); 251 printf("Ready? %d\n", check()); 252 253 printf("HRSL...\n"); 254 hrsl = max_read(MAX_REG_HRSL, &status); 255 printf("Status = %x\n", status); 256 printf("HRSL = %x\n", hrsl); 257 printf("Ready? %d\n", check()); 258 259 printf("Closing...\n"); 260 ubb_close(0); 261 262 return 0; 263 }