20 #if !defined(_usb_h_) || defined(_USBHOST_H_)
21 #error "Never include usbhost.h directly; include Usb.h instead"
26 #include <spi4teensy3.h>
27 #include <sys/types.h>
31 template<
typename SPI_CLK,
typename SPI_MOSI,
typename SPI_MISO,
typename SPI_SS >
class SPi {
33 #if SPI_HAS_TRANSACTION
37 #elif USING_SPI4TEENSY3
43 SPI_SS::SetDirWrite();
48 SPI_SS::SetDirWrite();
51 #if defined(__MIPSEL__)
52 SPI.setClockDivider(1);
53 #elif defined(__ARDUINO_X86__)
54 #ifdef SPI_CLOCK_1M // Hack used to check if setClockSpeed is available
55 SPI.setClockSpeed(12000000);
57 SPI.setClockDivider(SPI_CLOCK_DIV2);
60 SPI.setClockDivider(4);
63 #elif defined(RBL_NRF51822)
65 SPI_SS::SetDirWrite();
73 SPI_CLK::SetDirWrite();
74 SPI_MOSI::SetDirWrite();
75 SPI_MISO::SetDirRead();
76 SPI_SS::SetDirWrite();
88 #if defined(__AVR_ATmega1280__) || (__AVR_ATmega2560__) || defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
90 #elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
92 #elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)
94 #elif (defined(CORE_TEENSY) && (defined(__MK20DX128__) || defined(__MK20DX256__))) || defined(__ARDUINO_X86__) || defined(__MIPSEL__)
96 #elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
98 #elif defined(RBL_NRF51822)
101 #error "No SPI entry in usbhost.h"
109 template<
typename SPI_SS,
typename INTR >
class MAX3421e {
110 static uint8_t vbusState;
114 void regWr(uint8_t reg, uint8_t data);
115 uint8_t*
bytesWr(uint8_t reg, uint8_t nbytes, uint8_t* data_p);
116 void gpioWr(uint8_t data);
117 uint8_t
regRd(uint8_t reg);
118 uint8_t*
bytesRd(uint8_t reg, uint8_t nbytes, uint8_t* data_p);
122 int8_t
Init(
int mseconds);
137 template<
typename SPI_SS,
typename INTR >
141 template<
typename SPI_SS,
typename INTR >
144 #ifdef BOARD_MEGA_ADK
152 template<
typename SPI_SS,
typename INTR >
155 #if SPI_HAS_TRANSACTION
156 SPI.beginTransaction(SPISettings(26000000, MSBFIRST, SPI_MODE0));
160 #if SPI_HAS_TRANSACTION
165 #elif USING_SPI4TEENSY3
169 spi4teensy3::send(c, 2);
171 SPI.transfer(reg | 0x02);
175 while(!(SPSR & (1 << SPIF)));
177 while(!(SPSR & (1 << SPIF)));
181 #if SPI_HAS_TRANSACTION
182 SPI.endTransaction();
190 template<
typename SPI_SS,
typename INTR >
193 #if SPI_HAS_TRANSACTION
194 SPI.beginTransaction(SPISettings(26000000, MSBFIRST, SPI_MODE0));
198 #if SPI_HAS_TRANSACTION
199 SPI.transfer(reg | 0x02);
200 SPI.transfer(data_p, nbytes);
202 #elif USING_SPI4TEENSY3
203 spi4teensy3::send(reg | 0x02);
204 spi4teensy3::send(data_p, nbytes);
206 #elif defined(__ARDUINO_X86__)
207 SPI.transfer(reg | 0x02);
208 SPI.transferBuffer(data_p, NULL, nbytes);
211 SPI.transfer(reg | 0x02);
213 SPI.transfer(*data_p);
220 while(!(SPSR & (1 << SPIF)));
225 while(!(SPSR & (1 << SPIF)));
229 #if SPI_HAS_TRANSACTION
230 SPI.endTransaction();
239 template<
typename SPI_SS,
typename INTR >
248 template<
typename SPI_SS,
typename INTR >
251 #if SPI_HAS_TRANSACTION
252 SPI.beginTransaction(SPISettings(26000000, MSBFIRST, SPI_MODE0));
256 #if !defined(SPDR) || SPI_HAS_TRANSACTION
258 uint8_t rv = SPI.transfer(0);
260 #elif USING_SPI4TEENSY3
261 spi4teensy3::send(reg);
262 uint8_t rv = spi4teensy3::receive();
266 while(!(SPSR & (1 << SPIF)));
268 while(!(SPSR & (1 << SPIF)));
273 #if SPI_HAS_TRANSACTION
274 SPI.endTransaction();
282 template<
typename SPI_SS,
typename INTR >
285 #if SPI_HAS_TRANSACTION
286 SPI.beginTransaction(SPISettings(26000000, MSBFIRST, SPI_MODE0));
290 #if SPI_HAS_TRANSACTION
292 memset(data_p, 0, nbytes);
293 SPI.transfer(data_p, nbytes);
295 #elif USING_SPI4TEENSY3
296 spi4teensy3::send(reg);
297 spi4teensy3::receive(data_p, nbytes);
299 #elif defined(__ARDUINO_X86__)
301 SPI.transferBuffer(NULL, data_p, nbytes);
306 *data_p++ = SPI.transfer(0);
311 while(!(SPSR & (1 << SPIF)));
315 while(!(SPSR & (1 << SPIF)));
319 printf(
"%2.2x ", *data_p);
331 #if SPI_HAS_TRANSACTION
332 SPI.endTransaction();
340 template<
typename SPI_SS,
typename INTR >
351 template<
typename SPI_SS,
typename INTR >
365 template<
typename SPI_SS,
typename INTR >
372 SPI_SS::SetDirWrite();
402 template<
typename SPI_SS,
typename INTR >
409 SPI_SS::SetDirWrite();
423 if(mseconds < 1000) mseconds = 1000;
446 template<
typename SPI_SS,
typename INTR >
449 bus_sample = regRd(
rHRSL);
462 if((regRd(
rMODE) & bmLOWSPEED) == 0) {
481 template<
typename SPI_SS,
typename INTR >
487 pinvalue = INTR::IsSet();
490 rcode = IntHandler();
500 template<
typename SPI_SS,
typename INTR >
503 uint8_t HIRQ_sendback = 0x00;
513 regWr(
rHIRQ, HIRQ_sendback);
514 return ( HIRQ_sendback);
529 #endif // _USBHOST_H_
uint8_t getVbusState(void)
void gpioWr(uint8_t data)
void vbusPower(VBUS_t state)
uint8_t * bytesRd(uint8_t reg, uint8_t nbytes, uint8_t *data_p)
#define XMEM_ACQUIRE_SPI()
uint8_t * bytesWr(uint8_t reg, uint8_t nbytes, uint8_t *data_p)
#define XMEM_RELEASE_SPI()
void regWr(uint8_t reg, uint8_t data)
uint8_t regRd(uint8_t reg)