USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
usbhost.h
Go to the documentation of this file.
1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
2 
3 This software may be distributed and modified under the terms of the GNU
4 General Public License version 2 (GPL2) as published by the Free Software
5 Foundation and appearing in the file GPL2.TXT included in the packaging of
6 this file. Please note that GPL2 Section 2[b] requires that all works based
7 on this software must also be made publicly available under the terms of
8 the GPL2 ("Copyleft").
9 
10 Contact information
11 -------------------
12 
13 Circuits At Home, LTD
14 Web : http://www.circuitsathome.com
15 e-mail : support@circuitsathome.com
16  */
17 /* MAX3421E-based USB Host Library header file */
18 
19 
20 #if !defined(_usb_h_) || defined(_USBHOST_H_)
21 #error "Never include usbhost.h directly; include Usb.h instead"
22 #else
23 #define _USBHOST_H_
24 
25 #if USING_SPI4TEENSY3
26 #include <spi4teensy3.h>
27 #include <sys/types.h>
28 #endif
29 
30 /* SPI initialization */
31 template< typename CLK, typename MOSI, typename MISO, typename SPI_SS > class SPi {
32 #if USING_SPI4TEENSY3
33 public:
34 
35  static void init() {
36  // spi4teensy3 inits everything for us, except /SS
37  // CLK, MOSI and MISO are hard coded for now.
38  // spi4teensy3::init(0,0,0); // full speed, cpol 0, cpha 0
39  spi4teensy3::init(); // full speed, cpol 0, cpha 0
40  SPI_SS::SetDirWrite();
41  SPI_SS::Set();
42  }
43 
44 #else
45 public:
46 
47  static void init() {
48  //uint8_t tmp;
49  CLK::SetDirWrite();
50  MOSI::SetDirWrite();
51  MISO::SetDirRead();
52  SPI_SS::SetDirWrite();
53  /* mode 00 (CPOL=0, CPHA=0) master, fclk/2. Mode 11 (CPOL=11, CPHA=11) is also supported by MAX3421E */
54  SPCR = 0x50;
55  SPSR = 0x01; // 0x01
56 
57  //tmp = SPSR;
58  //tmp = SPDR;
59  }
60 #endif
61 };
62 
63 /* SPI pin definitions. see avrpins.h */
64 #if defined(__AVR_ATmega1280__) || (__AVR_ATmega2560__) || defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
65 typedef SPi< Pb1, Pb2, Pb3, Pb0 > spi;
66 #elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
67 typedef SPi< Pb5, Pb3, Pb4, Pb2 > spi;
68 #elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)
69 typedef SPi< Pb7, Pb5, Pb6, Pb4 > spi;
70 #elif defined(__MK20DX128__)
71 typedef SPi< P13, P11, P12, P10 > spi;
72 #else
73 #error "No SPI entry in usbhost.h"
74 #endif
75 
76 typedef enum {
77  vbus_on = 0,
79 } VBUS_t;
80 
81 template< typename SS, typename INTR > class MAX3421e /* : public spi */ {
82  static uint8_t vbusState;
83 
84 public:
85  MAX3421e();
86  void regWr(uint8_t reg, uint8_t data);
87  uint8_t* bytesWr(uint8_t reg, uint8_t nbytes, uint8_t* data_p);
88  void gpioWr(uint8_t data);
89  uint8_t regRd(uint8_t reg);
90  uint8_t* bytesRd(uint8_t reg, uint8_t nbytes, uint8_t* data_p);
91  uint8_t gpioRd();
92  uint16_t reset();
93  int8_t Init();
94  int8_t Init(int mseconds);
95 
96  void vbusPower(VBUS_t state) {
97  regWr(rPINCTL, (bmFDUPSPI | bmINTLEVEL | state));
98  }
99 
100  uint8_t getVbusState(void) {
101  return vbusState;
102  };
103  void busprobe();
104  uint8_t GpxHandler();
105  uint8_t IntHandler();
106  uint8_t Task();
107 };
108 
109 template< typename SS, typename INTR >
111 
112 /* constructor */
113 template< typename SS, typename INTR >
115  // Leaving ADK hardware setup in here, for now. This really belongs with the other parts.
116 #ifdef BOARD_MEGA_ADK
117  // For Mega ADK, which has a Max3421e on-board, set MAX_RESET to output mode, and then set it to HIGH
118  P55::SetDirWrite();
119  P55::Set();
120 #endif
121 };
122 
123 /* write single byte into MAX3421 register */
124 template< typename SS, typename INTR >
125 void MAX3421e< SS, INTR >::regWr(uint8_t reg, uint8_t data) {
127  SS::Clear();
128 #if USING_SPI4TEENSY3
129  uint8_t c[2];
130  c[0] = reg | 0x02;
131  c[1] = data;
132  spi4teensy3::send(c, 2);
133 #else
134  SPDR = (reg | 0x02);
135  while(!(SPSR & (1 << SPIF)));
136  SPDR = data;
137  while(!(SPSR & (1 << SPIF)));
138 #endif
139  SS::Set();
141  return;
142 };
143 /* multiple-byte write */
144 
145 /* returns a pointer to memory position after last written */
146 template< typename SS, typename INTR >
147 uint8_t* MAX3421e< SS, INTR >::bytesWr(uint8_t reg, uint8_t nbytes, uint8_t* data_p) {
149  SS::Clear();
150 #if USING_SPI4TEENSY3
151  spi4teensy3::send(reg | 0x02);
152  spi4teensy3::send(data_p, nbytes);
153  data_p += nbytes;
154 #else
155  SPDR = (reg | 0x02); //set WR bit and send register number
156  while(nbytes--) {
157  while(!(SPSR & (1 << SPIF))); //check if previous byte was sent
158  SPDR = (*data_p); // send next data byte
159  data_p++; // advance data pointer
160  }
161  while(!(SPSR & (1 << SPIF)));
162 #endif
163  SS::Set();
165  return( data_p);
166 }
167 /* GPIO write */
168 /*GPIO byte is split between 2 registers, so two writes are needed to write one byte */
169 
170 /* GPOUT bits are in the low nibble. 0-3 in IOPINS1, 4-7 in IOPINS2 */
171 template< typename SS, typename INTR >
172 void MAX3421e< SS, INTR >::gpioWr(uint8_t data) {
173  regWr(rIOPINS1, data);
174  data >>= 4;
175  regWr(rIOPINS2, data);
176  return;
177 }
178 
179 /* single host register read */
180 template< typename SS, typename INTR >
181 uint8_t MAX3421e< SS, INTR >::regRd(uint8_t reg) {
183  SS::Clear();
184 #if USING_SPI4TEENSY3
185  spi4teensy3::send(reg);
186  uint8_t rv = spi4teensy3::receive();
187  SS::Set();
188 #else
189  SPDR = reg;
190  while(!(SPSR & (1 << SPIF)));
191  SPDR = 0; //send empty byte
192  while(!(SPSR & (1 << SPIF)));
193  SS::Set();
194  uint8_t rv = SPDR;
195 #endif
197  return(rv);
198 }
199 /* multiple-byte register read */
200 
201 /* returns a pointer to a memory position after last read */
202 template< typename SS, typename INTR >
203 uint8_t* MAX3421e< SS, INTR >::bytesRd(uint8_t reg, uint8_t nbytes, uint8_t* data_p) {
205  SS::Clear();
206 #if USING_SPI4TEENSY3
207  spi4teensy3::send(reg);
208  spi4teensy3::receive(data_p, nbytes);
209  data_p += nbytes;
210 #else
211  SPDR = reg;
212  while(!(SPSR & (1 << SPIF))); //wait
213  while(nbytes) {
214  SPDR = 0; //send empty byte
215  nbytes--;
216  while(!(SPSR & (1 << SPIF)));
217 #if 0
218  {
219  *data_p = SPDR;
220  printf("%2.2x ", *data_p);
221  }
222  data_p++;
223  }
224  printf("\r\n");
225 #else
226  *data_p++ = SPDR;
227  }
228 #endif
229 #endif
230  SS::Set();
232  return( data_p);
233 }
234 /* GPIO read. See gpioWr for explanation */
235 
236 /* GPIN pins are in high nibbles of IOPINS1, IOPINS2 */
237 template< typename SS, typename INTR >
239  uint8_t gpin = 0;
240  gpin = regRd(rIOPINS2); //pins 4-7
241  gpin &= 0xf0; //clean lower nibble
242  gpin |= (regRd(rIOPINS1) >> 4); //shift low bits and OR with upper from previous operation.
243  return( gpin);
244 }
245 
246 /* reset MAX3421E. Returns number of cycles it took for PLL to stabilize after reset
247  or zero if PLL haven't stabilized in 65535 cycles */
248 template< typename SS, typename INTR >
250  uint16_t i = 0;
251  regWr(rUSBCTL, bmCHIPRES);
252  regWr(rUSBCTL, 0x00);
253  while(++i) {
254  if((regRd(rUSBIRQ) & bmOSCOKIRQ)) {
255  break;
256  }
257  }
258  return( i);
259 }
260 
261 /* initialize MAX3421E. Set Host mode, pullups, and stuff. Returns 0 if success, -1 if not */
262 template< typename SS, typename INTR >
265  // Moved here.
266  // you really should not init hardware in the constructor when it involves locks.
267  // Also avoids the vbus flicker issue confusing some devices.
268  /* pin and peripheral setup */
269  SS::SetDirWrite();
270  SS::Set();
271  spi::init();
272  INTR::SetDirRead();
274  /* MAX3421E - full-duplex SPI, level interrupt */
275  // GPX pin on. Moved here, otherwise we flicker the vbus.
276  regWr(rPINCTL, (bmFDUPSPI | bmINTLEVEL));
277 
278  if(reset() == 0) { //OSCOKIRQ hasn't asserted in time
279  return( -1);
280  }
281 
282  regWr(rMODE, bmDPPULLDN | bmDMPULLDN | bmHOST); // set pull-downs, Host
283 
284  regWr(rHIEN, bmCONDETIE | bmFRAMEIE); //connection detection
285 
286  /* check if device is connected */
287  regWr(rHCTL, bmSAMPLEBUS); // sample USB bus
288  while(!(regRd(rHCTL) & bmSAMPLEBUS)); //wait for sample operation to finish
289 
290  busprobe(); //check if anything is connected
291 
292  regWr(rHIRQ, bmCONDETIRQ); //clear connection detect interrupt
293  regWr(rCPUCTL, 0x01); //enable interrupt pin
294 
295  return( 0);
296 }
297 
298 /* initialize MAX3421E. Set Host mode, pullups, and stuff. Returns 0 if success, -1 if not */
299 template< typename SS, typename INTR >
300 int8_t MAX3421e< SS, INTR >::Init(int mseconds) {
302  // Moved here.
303  // you really should not init hardware in the constructor when it involves locks.
304  // Also avoids the vbus flicker issue confusing some devices.
305  /* pin and peripheral setup */
306  SS::SetDirWrite();
307  SS::Set();
308  spi::init();
309  INTR::SetDirRead();
311  /* MAX3421E - full-duplex SPI, level interrupt, vbus off */
312  regWr(rPINCTL, (bmFDUPSPI | bmINTLEVEL | GPX_VBDET));
313 
314  if(reset() == 0) { //OSCOKIRQ hasn't asserted in time
315  return( -1);
316  }
317 
318  // Delay a minimum of 1 second to ensure any capacitors are drained.
319  // 1 second is required to make sure we do not smoke a Microdrive!
320  if(mseconds < 1000) mseconds = 1000;
321  delay(mseconds);
322 
323  regWr(rMODE, bmDPPULLDN | bmDMPULLDN | bmHOST); // set pull-downs, Host
324 
325  regWr(rHIEN, bmCONDETIE | bmFRAMEIE); //connection detection
326 
327  /* check if device is connected */
328  regWr(rHCTL, bmSAMPLEBUS); // sample USB bus
329  while(!(regRd(rHCTL) & bmSAMPLEBUS)); //wait for sample operation to finish
330 
331  busprobe(); //check if anything is connected
332 
333  regWr(rHIRQ, bmCONDETIRQ); //clear connection detect interrupt
334  regWr(rCPUCTL, 0x01); //enable interrupt pin
335 
336  // GPX pin on. This is done here so that busprobe will fail if we have a switch connected.
337  regWr(rPINCTL, (bmFDUPSPI | bmINTLEVEL));
338 
339  return( 0);
340 }
341 
342 /* probe bus to determine device presence and speed and switch host to this speed */
343 template< typename SS, typename INTR >
345  uint8_t bus_sample;
346  bus_sample = regRd(rHRSL); //Get J,K status
347  bus_sample &= (bmJSTATUS | bmKSTATUS); //zero the rest of the byte
348  switch(bus_sample) { //start full-speed or low-speed host
349  case( bmJSTATUS):
350  if((regRd(rMODE) & bmLOWSPEED) == 0) {
351  regWr(rMODE, MODE_FS_HOST); //start full-speed host
352  vbusState = FSHOST;
353  } else {
354  regWr(rMODE, MODE_LS_HOST); //start low-speed host
355  vbusState = LSHOST;
356  }
357  break;
358  case( bmKSTATUS):
359  if((regRd(rMODE) & bmLOWSPEED) == 0) {
360  regWr(rMODE, MODE_LS_HOST); //start low-speed host
361  vbusState = LSHOST;
362  } else {
363  regWr(rMODE, MODE_FS_HOST); //start full-speed host
364  vbusState = FSHOST;
365  }
366  break;
367  case( bmSE1): //illegal state
368  vbusState = SE1;
369  break;
370  case( bmSE0): //disconnected state
371  regWr(rMODE, bmDPPULLDN | bmDMPULLDN | bmHOST | bmSEPIRQ);
372  vbusState = SE0;
373  break;
374  }//end switch( bus_sample )
375 }
376 
377 /* MAX3421 state change task and interrupt handler */
378 template< typename SS, typename INTR >
380  uint8_t rcode = 0;
381  uint8_t pinvalue;
382  //USB_HOST_SERIAL.print("Vbus state: ");
383  //USB_HOST_SERIAL.println( vbusState, HEX );
384  pinvalue = INTR::IsSet(); //Read();
385  //pinvalue = digitalRead( MAX_INT );
386  if(pinvalue == 0) {
387  rcode = IntHandler();
388  }
389  // pinvalue = digitalRead( MAX_GPX );
390  // if( pinvalue == LOW ) {
391  // GpxHandler();
392  // }
393  // usbSM(); //USB state machine
394  return( rcode);
395 }
396 
397 template< typename SS, typename INTR >
399  uint8_t HIRQ;
400  uint8_t HIRQ_sendback = 0x00;
401  HIRQ = regRd(rHIRQ); //determine interrupt source
402  //if( HIRQ & bmFRAMEIRQ ) { //->1ms SOF interrupt handler
403  // HIRQ_sendback |= bmFRAMEIRQ;
404  //}//end FRAMEIRQ handling
405  if(HIRQ & bmCONDETIRQ) {
406  busprobe();
407  HIRQ_sendback |= bmCONDETIRQ;
408  }
409  /* End HIRQ interrupts handling, clear serviced IRQs */
410  regWr(rHIRQ, HIRQ_sendback);
411  return( HIRQ_sendback);
412 }
413 //template< typename SS, typename INTR >
414 //uint8_t MAX3421e< SS, INTR >::GpxHandler()
415 //{
416 // uint8_t GPINIRQ = regRd( rGPINIRQ ); //read GPIN IRQ register
423 // return( GPINIRQ );
424 //}
425 
426 #endif //_USBHOST_H_
#define GPX_VBDET
Definition: max3421e.h:80
#define rIOPINS1
Definition: max3421e.h:86
#define rHIEN
Definition: max3421e.h:153
#define FSHOST
Definition: max3421e.h:35
#define rHCTL
Definition: max3421e.h:179
void busprobe()
Definition: usbhost.h:344
#define bmCONDETIRQ
Definition: max3421e.h:149
#define bmCONDETIE
Definition: max3421e.h:161
#define bmSE1
Definition: max3421e.h:209
MAX3421e()
Definition: usbhost.h:114
#define bmCHIPRES
Definition: max3421e.h:62
void regWr(uint8_t reg, uint8_t data)
Definition: usbhost.h:125
uint8_t GpxHandler()
#define rCPUCTL
Definition: max3421e.h:65
static void init()
Definition: usbhost.h:47
#define bmJSTATUS
Definition: max3421e.h:207
#define MODE_LS_HOST
Definition: max3421e.h:230
#define bmINTLEVEL
Definition: max3421e.h:74
#define rUSBCTL
Definition: max3421e.h:60
#define bmSE0
Definition: max3421e.h:208
#define bmHOST
Definition: max3421e.h:168
#define bmSEPIRQ
Definition: max3421e.h:172
void gpioWr(uint8_t data)
Definition: usbhost.h:172
#define LSHOST
Definition: max3421e.h:36
#define rMODE
Definition: max3421e.h:165
#define MODE_FS_HOST
Definition: max3421e.h:229
#define bmKSTATUS
Definition: max3421e.h:206
#define rHRSL
Definition: max3421e.h:201
#define XMEM_ACQUIRE_SPI()
Definition: settings.h:83
int8_t Init()
Definition: usbhost.h:263
#define bmLOWSPEED
Definition: max3421e.h:169
Definition: usbhost.h:31
uint8_t Task()
Definition: usbhost.h:379
#define rHIRQ
Definition: max3421e.h:142
#define rUSBIRQ
Definition: max3421e.h:48
void vbusPower(VBUS_t state)
Definition: usbhost.h:96
uint8_t IntHandler()
Definition: usbhost.h:398
uint8_t * bytesWr(uint8_t reg, uint8_t nbytes, uint8_t *data_p)
Definition: usbhost.h:147
#define XMEM_RELEASE_SPI()
Definition: settings.h:84
#define bmFDUPSPI
Definition: max3421e.h:73
#define rPINCTL
Definition: max3421e.h:71
VBUS_t
Definition: usbhost.h:76
uint8_t gpioRd()
Definition: usbhost.h:238
#define bmFRAMEIE
Definition: max3421e.h:162
#define bmOSCOKIRQ
Definition: max3421e.h:52
uint8_t * bytesRd(uint8_t reg, uint8_t nbytes, uint8_t *data_p)
Definition: usbhost.h:203
uint16_t reset()
Definition: usbhost.h:249
#define SE0
Definition: max3421e.h:33
#define rIOPINS2
Definition: max3421e.h:98
#define bmDMPULLDN
Definition: max3421e.h:174
#define SE1
Definition: max3421e.h:34
#define bmSAMPLEBUS
Definition: max3421e.h:183
#define bmDPPULLDN
Definition: max3421e.h:175
uint8_t getVbusState(void)
Definition: usbhost.h:100
uint8_t regRd(uint8_t reg)
Definition: usbhost.h:181