27 #if !defined(_usb_h_) || defined(_USBHOST_H_)
28 #error "Never include usbhost.h directly; include Usb.h instead"
33 #include <spi4teensy3.h>
34 #include <sys/types.h>
38 template<
typename SPI_CLK,
typename SPI_MOSI,
typename SPI_MISO,
typename SPI_SS >
class SPi {
46 SPI_SS::SetDirWrite();
49 #elif defined(SPI_HAS_TRANSACTION)
52 SPI_SS::SetDirWrite();
55 #elif defined(STM32F4)
56 #warning "You need to initialize the SPI interface manually when using the STM32F4 platform"
62 SPI_SS::SetDirWrite();
65 #if defined(__MIPSEL__)
67 #elif defined(__ARDUINO_X86__)
68 #ifdef SPI_CLOCK_1M // Hack used to check if setClockSpeed is available
69 USB_SPI.setClockSpeed(12000000);
71 USB_SPI.setClockDivider(SPI_CLOCK_DIV2);
73 #elif !defined(RBL_NRF51822)
80 SPI_CLK::SetDirWrite();
81 SPI_MOSI::SetDirWrite();
82 SPI_MISO::SetDirRead();
83 SPI_SS::SetDirWrite();
95 #if defined(PIN_SPI_SCK) && defined(PIN_SPI_MOSI) && defined(PIN_SPI_MISO) && defined(PIN_SPI_SS)
98 #define NOTHING_EXTRACT
99 #define EXTRACT(...) EXTRACT __VA_ARGS__
100 #define PASTE(x, ...) x ## __VA_ARGS__
101 #define EVALUATING_PASTE(x, ...) PASTE(x, __VA_ARGS__)
102 #define UNPAREN(x) EVALUATING_PASTE(NOTHING_, EXTRACT x)
103 #define APPEND_PIN(pin) P ## pin // Appends the pin to 'P', e.g. 1 becomes P1
104 #define MAKE_PIN(x) EVALUATING_PASTE(APPEND_, PIN(UNPAREN(x)))
107 #elif defined(__AVR_ATmega1280__) || (__AVR_ATmega2560__) || defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
109 #elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
111 #elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)
113 #elif (defined(CORE_TEENSY) && (defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) || defined(__MKL26Z64__))) || defined(__ARDUINO_ARC__) || defined(__ARDUINO_X86__) || defined(__MIPSEL__) || defined(STM32F4)
115 #elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
117 #elif defined(RBL_NRF51822)
119 #elif defined(ESP8266)
124 #error "No SPI entry in usbhost.h"
132 template<
typename SPI_SS,
typename INTR >
class MAX3421e {
133 static uint8_t vbusState;
137 void regWr(uint8_t reg, uint8_t data);
138 uint8_t*
bytesWr(uint8_t reg, uint8_t nbytes, uint8_t* data_p);
139 void gpioWr(uint8_t data);
140 uint8_t
regRd(uint8_t reg);
141 uint8_t*
bytesRd(uint8_t reg, uint8_t nbytes, uint8_t* data_p);
146 int8_t
Init(
int mseconds);
161 template<
typename SPI_SS,
typename INTR >
165 template<
typename SPI_SS,
typename INTR >
168 #ifdef BOARD_MEGA_ADK
176 template<
typename SPI_SS,
typename INTR >
179 #if defined(SPI_HAS_TRANSACTION)
180 USB_SPI.beginTransaction(SPISettings(26000000, MSBFIRST, SPI_MODE0));
184 #if USING_SPI4TEENSY3
188 spi4teensy3::send(c, 2);
189 #elif defined(SPI_HAS_TRANSACTION) && !defined(ESP8266) && !defined(ESP32)
194 #elif defined(STM32F4)
198 HAL_SPI_Transmit(&SPI_Handle, c, 2, HAL_MAX_DELAY);
199 #elif !defined(SPDR) // ESP8266, ESP32
204 while(!(SPSR & (1 << SPIF)));
206 while(!(SPSR & (1 << SPIF)));
210 #if defined(SPI_HAS_TRANSACTION)
219 template<
typename SPI_SS,
typename INTR >
222 #if defined(SPI_HAS_TRANSACTION)
223 USB_SPI.beginTransaction(SPISettings(26000000, MSBFIRST, SPI_MODE0));
227 #if USING_SPI4TEENSY3
228 spi4teensy3::send(reg | 0x02);
229 spi4teensy3::send(data_p, nbytes);
231 #elif defined(SPI_HAS_TRANSACTION) && !defined(ESP8266) && !defined(ESP32)
233 USB_SPI.transfer(data_p, nbytes);
235 #elif defined(__ARDUINO_X86__)
237 USB_SPI.transferBuffer(data_p, NULL, nbytes);
239 #elif defined(STM32F4)
240 uint8_t data = reg | 0x02;
241 HAL_SPI_Transmit(&SPI_Handle, &data, 1, HAL_MAX_DELAY);
242 HAL_SPI_Transmit(&SPI_Handle, data_p, nbytes, HAL_MAX_DELAY);
244 #elif !defined(SPDR) // ESP8266, ESP32
255 while(!(SPSR & (1 << SPIF)));
260 while(!(SPSR & (1 << SPIF)));
264 #if defined(SPI_HAS_TRANSACTION)
274 template<
typename SPI_SS,
typename INTR >
283 template<
typename SPI_SS,
typename INTR >
286 #if defined(SPI_HAS_TRANSACTION)
287 USB_SPI.beginTransaction(SPISettings(26000000, MSBFIRST, SPI_MODE0));
291 #if USING_SPI4TEENSY3
292 spi4teensy3::send(reg);
293 uint8_t rv = spi4teensy3::receive();
295 #elif defined(STM32F4)
296 HAL_SPI_Transmit(&SPI_Handle, ®, 1, HAL_MAX_DELAY);
298 HAL_SPI_Receive(&SPI_Handle, &rv, 1, HAL_MAX_DELAY);
300 #elif !defined(SPDR) || defined(SPI_HAS_TRANSACTION)
302 uint8_t rv =
USB_SPI.transfer(0);
306 while(!(SPSR & (1 << SPIF)));
308 while(!(SPSR & (1 << SPIF)));
313 #if defined(SPI_HAS_TRANSACTION)
322 template<
typename SPI_SS,
typename INTR >
325 #if defined(SPI_HAS_TRANSACTION)
326 USB_SPI.beginTransaction(SPISettings(26000000, MSBFIRST, SPI_MODE0));
330 #if USING_SPI4TEENSY3
331 spi4teensy3::send(reg);
332 spi4teensy3::receive(data_p, nbytes);
334 #elif defined(SPI_HAS_TRANSACTION) && !defined(ESP8266) && !defined(ESP32)
336 memset(data_p, 0, nbytes);
337 USB_SPI.transfer(data_p, nbytes);
339 #elif defined(__ARDUINO_X86__)
341 USB_SPI.transferBuffer(NULL, data_p, nbytes);
343 #elif defined(STM32F4)
344 HAL_SPI_Transmit(&SPI_Handle, ®, 1, HAL_MAX_DELAY);
345 memset(data_p, 0, nbytes);
346 HAL_SPI_Receive(&SPI_Handle, data_p, nbytes, HAL_MAX_DELAY);
348 #elif !defined(SPDR) // ESP8266, ESP32
352 *data_p++ =
USB_SPI.transfer(0);
357 while(!(SPSR & (1 << SPIF)));
361 while(!(SPSR & (1 << SPIF)));
365 printf(
"%2.2x ", *data_p);
377 #if defined(SPI_HAS_TRANSACTION)
389 template<
typename SPI_SS,
typename INTR >
402 template<
typename SPI_SS,
typename INTR >
413 template<
typename SPI_SS,
typename INTR >
427 template<
typename SPI_SS,
typename INTR >
434 SPI_SS::SetDirWrite();
464 template<
typename SPI_SS,
typename INTR >
471 SPI_SS::SetDirWrite();
485 if(mseconds < 1000) mseconds = 1000;
508 template<
typename SPI_SS,
typename INTR >
511 bus_sample = regRd(
rHRSL);
524 if((regRd(
rMODE) & bmLOWSPEED) == 0) {
543 template<
typename SPI_SS,
typename INTR >
549 pinvalue = INTR::IsSet();
552 rcode = IntHandler();
562 template<
typename SPI_SS,
typename INTR >
565 uint8_t HIRQ_sendback = 0x00;
575 regWr(
rHIRQ, HIRQ_sendback);
576 return ( HIRQ_sendback);
591 #endif // _USBHOST_H_
uint8_t getVbusState(void)
uint8_t gpioRdOutput()
Reads the current GPI output values.
void gpioWr(uint8_t data)
uint8_t gpioRd()
Reads the current GPI input values.
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)