2011-06-22 19:41:22 +02:00
|
|
|
/* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
|
|
|
|
|
|
|
|
This software may be distributed and modified under the terms of the GNU
|
|
|
|
General Public License version 2 (GPL2) as published by the Free Software
|
|
|
|
Foundation and appearing in the file GPL2.TXT included in the packaging of
|
|
|
|
this file. Please note that GPL2 Section 2[b] requires that all works based
|
|
|
|
on this software must also be made publicly available under the terms of
|
|
|
|
the GPL2 ("Copyleft").
|
|
|
|
|
|
|
|
Contact information
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
Circuits At Home, LTD
|
|
|
|
Web : http://www.circuitsathome.com
|
|
|
|
e-mail : support@circuitsathome.com
|
2013-03-28 08:26:02 +01:00
|
|
|
*/
|
2011-01-19 07:27:20 +01:00
|
|
|
/* MAX3421E-based USB Host Library header file */
|
|
|
|
|
2013-09-24 00:40:10 +02:00
|
|
|
|
|
|
|
#if !defined(_usb_h_) || defined(_USBHOST_H_)
|
|
|
|
#error "Never include usbhost.h directly; include Usb.h instead"
|
|
|
|
#else
|
2011-01-19 07:27:20 +01:00
|
|
|
#define _USBHOST_H_
|
|
|
|
|
2013-11-24 14:56:34 +01:00
|
|
|
#if USING_SPI4TEENSY3
|
|
|
|
#include <spi4teensy3.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
|
2011-01-19 07:27:20 +01:00
|
|
|
/* SPI initialization */
|
2013-12-15 22:19:50 +01:00
|
|
|
template< typename SPI_CLK, typename SPI_MOSI, typename SPI_MISO, typename SPI_SS > class SPi {
|
2013-11-24 14:56:34 +01:00
|
|
|
public:
|
2014-08-03 00:06:46 +02:00
|
|
|
#if SPI_HAS_TRANSACTION
|
|
|
|
static void init() {
|
|
|
|
SPI.begin(); // The SPI library with transaction will take care of setting up the pins - settings is set in beginTransaction()
|
|
|
|
}
|
|
|
|
#elif USING_SPI4TEENSY3
|
2013-11-24 14:56:34 +01:00
|
|
|
static void init() {
|
|
|
|
// spi4teensy3 inits everything for us, except /SS
|
|
|
|
// CLK, MOSI and MISO are hard coded for now.
|
|
|
|
// spi4teensy3::init(0,0,0); // full speed, cpol 0, cpha 0
|
|
|
|
spi4teensy3::init(); // full speed, cpol 0, cpha 0
|
|
|
|
SPI_SS::SetDirWrite();
|
|
|
|
SPI_SS::Set();
|
|
|
|
}
|
2014-02-25 11:47:59 +01:00
|
|
|
#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
|
|
|
|
static void init() {
|
|
|
|
SPI_SS::SetDirWrite();
|
|
|
|
SPI_SS::Set();
|
|
|
|
SPI.begin();
|
2014-03-07 02:19:34 +01:00
|
|
|
SPI.setClockDivider(4); // Set speed to 84MHz/4=21MHz - the MAX3421E can handle up to 26MHz
|
2014-02-25 11:47:59 +01:00
|
|
|
}
|
2013-11-24 14:56:34 +01:00
|
|
|
#else
|
2013-03-28 08:26:02 +01:00
|
|
|
static void init() {
|
|
|
|
//uint8_t tmp;
|
2013-12-15 22:19:50 +01:00
|
|
|
SPI_CLK::SetDirWrite();
|
|
|
|
SPI_MOSI::SetDirWrite();
|
|
|
|
SPI_MISO::SetDirRead();
|
2013-03-28 08:26:02 +01:00
|
|
|
SPI_SS::SetDirWrite();
|
|
|
|
/* mode 00 (CPOL=0, CPHA=0) master, fclk/2. Mode 11 (CPOL=11, CPHA=11) is also supported by MAX3421E */
|
|
|
|
SPCR = 0x50;
|
2013-05-14 02:54:12 +02:00
|
|
|
SPSR = 0x01; // 0x01
|
2013-03-28 08:26:02 +01:00
|
|
|
/**/
|
|
|
|
//tmp = SPSR;
|
|
|
|
//tmp = SPDR;
|
|
|
|
}
|
2013-11-24 14:56:34 +01:00
|
|
|
#endif
|
2013-03-28 08:26:02 +01:00
|
|
|
};
|
2011-01-19 07:27:20 +01:00
|
|
|
|
|
|
|
/* SPI pin definitions. see avrpins.h */
|
2011-06-27 05:27:44 +02:00
|
|
|
#if defined(__AVR_ATmega1280__) || (__AVR_ATmega2560__) || defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
|
2011-01-19 07:27:20 +01:00
|
|
|
typedef SPi< Pb1, Pb2, Pb3, Pb0 > spi;
|
2013-10-21 01:59:29 +02:00
|
|
|
#elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
|
2011-01-19 07:27:20 +01:00
|
|
|
typedef SPi< Pb5, Pb3, Pb4, Pb2 > spi;
|
2013-10-21 01:59:29 +02:00
|
|
|
#elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__)
|
2011-06-27 05:27:44 +02:00
|
|
|
typedef SPi< Pb7, Pb5, Pb6, Pb4 > spi;
|
2014-02-25 11:47:59 +01:00
|
|
|
#elif defined(CORE_TEENSY) && (defined(__MK20DX128__) || defined(__MK20DX256__))
|
2013-05-29 19:32:51 +02:00
|
|
|
typedef SPi< P13, P11, P12, P10 > spi;
|
2014-02-25 11:47:59 +01:00
|
|
|
#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
|
|
|
|
typedef SPi< P76, P75, P74, P10 > spi;
|
2013-10-21 01:59:29 +02:00
|
|
|
#else
|
|
|
|
#error "No SPI entry in usbhost.h"
|
2013-05-29 19:32:51 +02:00
|
|
|
#endif
|
2011-01-19 07:27:20 +01:00
|
|
|
|
2013-09-30 07:13:11 +02:00
|
|
|
typedef enum {
|
2013-09-08 19:11:22 +02:00
|
|
|
vbus_on = 0,
|
|
|
|
vbus_off = GPX_VBDET
|
2013-09-30 07:13:11 +02:00
|
|
|
} VBUS_t;
|
2013-08-09 08:17:01 +02:00
|
|
|
|
2013-12-15 22:19:50 +01:00
|
|
|
template< typename SPI_SS, typename INTR > class MAX3421e /* : public spi */ {
|
2013-03-28 08:26:02 +01:00
|
|
|
static uint8_t vbusState;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MAX3421e();
|
|
|
|
void regWr(uint8_t reg, uint8_t data);
|
|
|
|
uint8_t* bytesWr(uint8_t reg, uint8_t nbytes, uint8_t* data_p);
|
|
|
|
void gpioWr(uint8_t data);
|
|
|
|
uint8_t regRd(uint8_t reg);
|
|
|
|
uint8_t* bytesRd(uint8_t reg, uint8_t nbytes, uint8_t* data_p);
|
|
|
|
uint8_t gpioRd();
|
|
|
|
uint16_t reset();
|
|
|
|
int8_t Init();
|
2013-08-09 08:17:01 +02:00
|
|
|
int8_t Init(int mseconds);
|
|
|
|
|
|
|
|
void vbusPower(VBUS_t state) {
|
|
|
|
regWr(rPINCTL, (bmFDUPSPI | bmINTLEVEL | state));
|
|
|
|
}
|
2011-01-19 07:27:20 +01:00
|
|
|
|
2013-03-28 08:26:02 +01:00
|
|
|
uint8_t getVbusState(void) {
|
|
|
|
return vbusState;
|
|
|
|
};
|
|
|
|
void busprobe();
|
|
|
|
uint8_t GpxHandler();
|
|
|
|
uint8_t IntHandler();
|
|
|
|
uint8_t Task();
|
2011-01-19 07:27:20 +01:00
|
|
|
};
|
|
|
|
|
2013-12-15 22:19:50 +01:00
|
|
|
template< typename SPI_SS, typename INTR >
|
|
|
|
uint8_t MAX3421e< SPI_SS, INTR >::vbusState = 0;
|
2011-01-19 07:27:20 +01:00
|
|
|
|
|
|
|
/* constructor */
|
2013-12-15 22:19:50 +01:00
|
|
|
template< typename SPI_SS, typename INTR >
|
|
|
|
MAX3421e< SPI_SS, INTR >::MAX3421e() {
|
2013-11-24 14:56:34 +01:00
|
|
|
// Leaving ADK hardware setup in here, for now. This really belongs with the other parts.
|
2013-03-30 17:05:30 +01:00
|
|
|
#ifdef BOARD_MEGA_ADK
|
2013-10-21 02:17:47 +02:00
|
|
|
// For Mega ADK, which has a Max3421e on-board, set MAX_RESET to output mode, and then set it to HIGH
|
|
|
|
P55::SetDirWrite();
|
|
|
|
P55::Set();
|
2013-03-30 17:05:30 +01:00
|
|
|
#endif
|
2011-01-19 07:27:20 +01:00
|
|
|
};
|
2013-03-28 08:26:02 +01:00
|
|
|
|
2011-01-19 07:27:20 +01:00
|
|
|
/* write single byte into MAX3421 register */
|
2013-12-15 22:19:50 +01:00
|
|
|
template< typename SPI_SS, typename INTR >
|
|
|
|
void MAX3421e< SPI_SS, INTR >::regWr(uint8_t reg, uint8_t data) {
|
2013-10-08 21:52:24 +02:00
|
|
|
XMEM_ACQUIRE_SPI();
|
2014-08-03 00:06:46 +02:00
|
|
|
#if SPI_HAS_TRANSACTION
|
|
|
|
SPI.beginTransaction(SPISettings(26000000, MSBFIRST, SPI_MODE0)); // The MAX3421E can handle up to 26MHz, use MSB First and SPI mode 0
|
|
|
|
#endif
|
2013-12-15 22:19:50 +01:00
|
|
|
SPI_SS::Clear();
|
2014-08-03 00:06:46 +02:00
|
|
|
|
|
|
|
#if SPI_HAS_TRANSACTION
|
|
|
|
uint8_t c[2];
|
|
|
|
c[0] = reg | 0x02;
|
|
|
|
c[1] = data;
|
|
|
|
SPI.transfer(c, 2);
|
|
|
|
#elif USING_SPI4TEENSY3
|
2013-11-24 14:56:34 +01:00
|
|
|
uint8_t c[2];
|
|
|
|
c[0] = reg | 0x02;
|
|
|
|
c[1] = data;
|
|
|
|
spi4teensy3::send(c, 2);
|
2014-02-25 11:47:59 +01:00
|
|
|
#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
|
|
|
|
SPI.transfer(reg | 0x02);
|
|
|
|
SPI.transfer(data);
|
2013-11-24 14:56:34 +01:00
|
|
|
#else
|
2013-03-28 08:26:02 +01:00
|
|
|
SPDR = (reg | 0x02);
|
2013-03-28 09:46:43 +01:00
|
|
|
while(!(SPSR & (1 << SPIF)));
|
2013-03-28 08:26:02 +01:00
|
|
|
SPDR = data;
|
2013-03-28 09:46:43 +01:00
|
|
|
while(!(SPSR & (1 << SPIF)));
|
2013-11-24 14:56:34 +01:00
|
|
|
#endif
|
2014-08-03 00:06:46 +02:00
|
|
|
|
2013-12-15 22:19:50 +01:00
|
|
|
SPI_SS::Set();
|
2014-08-03 00:06:46 +02:00
|
|
|
#if SPI_HAS_TRANSACTION
|
|
|
|
SPI.endTransaction();
|
|
|
|
#endif
|
2013-10-08 21:52:24 +02:00
|
|
|
XMEM_RELEASE_SPI();
|
2013-03-28 08:26:02 +01:00
|
|
|
return;
|
2011-01-19 07:27:20 +01:00
|
|
|
};
|
|
|
|
/* multiple-byte write */
|
2013-03-28 08:26:02 +01:00
|
|
|
|
2011-01-19 07:27:20 +01:00
|
|
|
/* returns a pointer to memory position after last written */
|
2013-12-15 22:19:50 +01:00
|
|
|
template< typename SPI_SS, typename INTR >
|
|
|
|
uint8_t* MAX3421e< SPI_SS, INTR >::bytesWr(uint8_t reg, uint8_t nbytes, uint8_t* data_p) {
|
2013-10-08 21:52:24 +02:00
|
|
|
XMEM_ACQUIRE_SPI();
|
2014-08-03 00:06:46 +02:00
|
|
|
#if SPI_HAS_TRANSACTION
|
|
|
|
SPI.beginTransaction(SPISettings(26000000, MSBFIRST, SPI_MODE0)); // The MAX3421E can handle up to 26MHz, use MSB First and SPI mode 0
|
|
|
|
#endif
|
2013-12-15 22:19:50 +01:00
|
|
|
SPI_SS::Clear();
|
2014-08-03 00:06:46 +02:00
|
|
|
|
|
|
|
#if SPI_HAS_TRANSACTION
|
|
|
|
SPI.transfer(reg | 0x02);
|
|
|
|
SPI.transfer(data_p, nbytes);
|
|
|
|
data_p += nbytes;
|
|
|
|
#elif USING_SPI4TEENSY3
|
2013-11-24 14:56:34 +01:00
|
|
|
spi4teensy3::send(reg | 0x02);
|
|
|
|
spi4teensy3::send(data_p, nbytes);
|
|
|
|
data_p += nbytes;
|
2014-02-25 11:47:59 +01:00
|
|
|
#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
|
|
|
|
SPI.transfer(reg | 0x02);
|
|
|
|
while(nbytes) {
|
|
|
|
SPI.transfer(*data_p);
|
|
|
|
nbytes--;
|
|
|
|
data_p++; // advance data pointer
|
|
|
|
}
|
2013-11-24 14:56:34 +01:00
|
|
|
#else
|
2013-03-28 08:26:02 +01:00
|
|
|
SPDR = (reg | 0x02); //set WR bit and send register number
|
2013-12-28 20:43:21 +01:00
|
|
|
while(nbytes) {
|
2013-03-28 09:46:43 +01:00
|
|
|
while(!(SPSR & (1 << SPIF))); //check if previous byte was sent
|
2013-03-28 08:26:02 +01:00
|
|
|
SPDR = (*data_p); // send next data byte
|
2013-12-28 20:43:21 +01:00
|
|
|
nbytes--;
|
2013-03-28 08:26:02 +01:00
|
|
|
data_p++; // advance data pointer
|
|
|
|
}
|
2013-03-28 09:46:43 +01:00
|
|
|
while(!(SPSR & (1 << SPIF)));
|
2013-11-24 14:56:34 +01:00
|
|
|
#endif
|
2014-08-03 00:06:46 +02:00
|
|
|
|
2013-12-15 22:19:50 +01:00
|
|
|
SPI_SS::Set();
|
2014-08-03 00:06:46 +02:00
|
|
|
#if SPI_HAS_TRANSACTION
|
|
|
|
SPI.endTransaction();
|
|
|
|
#endif
|
2013-10-08 21:52:24 +02:00
|
|
|
XMEM_RELEASE_SPI();
|
2013-12-25 11:09:57 +01:00
|
|
|
return ( data_p);
|
2011-01-19 07:27:20 +01:00
|
|
|
}
|
|
|
|
/* GPIO write */
|
|
|
|
/*GPIO byte is split between 2 registers, so two writes are needed to write one byte */
|
2013-03-28 08:26:02 +01:00
|
|
|
|
2011-01-19 07:27:20 +01:00
|
|
|
/* GPOUT bits are in the low nibble. 0-3 in IOPINS1, 4-7 in IOPINS2 */
|
2013-12-15 22:19:50 +01:00
|
|
|
template< typename SPI_SS, typename INTR >
|
|
|
|
void MAX3421e< SPI_SS, INTR >::gpioWr(uint8_t data) {
|
2013-03-28 08:26:02 +01:00
|
|
|
regWr(rIOPINS1, data);
|
|
|
|
data >>= 4;
|
|
|
|
regWr(rIOPINS2, data);
|
|
|
|
return;
|
2011-01-19 07:27:20 +01:00
|
|
|
}
|
2013-03-28 08:26:02 +01:00
|
|
|
|
2011-01-19 07:27:20 +01:00
|
|
|
/* single host register read */
|
2013-12-15 22:19:50 +01:00
|
|
|
template< typename SPI_SS, typename INTR >
|
|
|
|
uint8_t MAX3421e< SPI_SS, INTR >::regRd(uint8_t reg) {
|
2013-10-08 21:52:24 +02:00
|
|
|
XMEM_ACQUIRE_SPI();
|
2014-08-03 00:06:46 +02:00
|
|
|
#if SPI_HAS_TRANSACTION
|
|
|
|
SPI.beginTransaction(SPISettings(26000000, MSBFIRST, SPI_MODE0)); // The MAX3421E can handle up to 26MHz, use MSB First and SPI mode 0
|
|
|
|
#endif
|
2013-12-15 22:19:50 +01:00
|
|
|
SPI_SS::Clear();
|
2014-08-03 00:06:46 +02:00
|
|
|
|
|
|
|
#if (defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)) || SPI_HAS_TRANSACTION
|
2014-02-25 11:47:59 +01:00
|
|
|
SPI.transfer(reg);
|
2014-08-03 00:06:46 +02:00
|
|
|
uint8_t rv = SPI.transfer(0); // Send empty byte
|
|
|
|
SPI_SS::Set();
|
|
|
|
#elif USING_SPI4TEENSY3
|
|
|
|
spi4teensy3::send(reg);
|
2014-08-03 18:01:55 +02:00
|
|
|
uint8_t rv = spi4teensy3::receive();
|
2014-02-25 11:47:59 +01:00
|
|
|
SPI_SS::Set();
|
2013-11-24 14:56:34 +01:00
|
|
|
#else
|
2013-03-28 08:26:02 +01:00
|
|
|
SPDR = reg;
|
2013-03-28 09:46:43 +01:00
|
|
|
while(!(SPSR & (1 << SPIF)));
|
2014-08-03 00:06:46 +02:00
|
|
|
SPDR = 0; // Send empty byte
|
2013-03-28 09:46:43 +01:00
|
|
|
while(!(SPSR & (1 << SPIF)));
|
2013-12-15 22:19:50 +01:00
|
|
|
SPI_SS::Set();
|
2013-10-08 21:52:24 +02:00
|
|
|
uint8_t rv = SPDR;
|
2013-11-24 14:56:34 +01:00
|
|
|
#endif
|
2014-08-03 00:06:46 +02:00
|
|
|
|
|
|
|
#if SPI_HAS_TRANSACTION
|
|
|
|
SPI.endTransaction();
|
|
|
|
#endif
|
2013-10-08 21:52:24 +02:00
|
|
|
XMEM_RELEASE_SPI();
|
2013-12-25 11:09:57 +01:00
|
|
|
return (rv);
|
2011-01-19 07:27:20 +01:00
|
|
|
}
|
|
|
|
/* multiple-byte register read */
|
2013-03-28 08:26:02 +01:00
|
|
|
|
2011-01-19 07:27:20 +01:00
|
|
|
/* returns a pointer to a memory position after last read */
|
2013-12-15 22:19:50 +01:00
|
|
|
template< typename SPI_SS, typename INTR >
|
|
|
|
uint8_t* MAX3421e< SPI_SS, INTR >::bytesRd(uint8_t reg, uint8_t nbytes, uint8_t* data_p) {
|
2013-10-08 21:52:24 +02:00
|
|
|
XMEM_ACQUIRE_SPI();
|
2014-08-03 00:06:46 +02:00
|
|
|
#if SPI_HAS_TRANSACTION
|
|
|
|
SPI.beginTransaction(SPISettings(26000000, MSBFIRST, SPI_MODE0)); // The MAX3421E can handle up to 26MHz, use MSB First and SPI mode 0
|
|
|
|
#endif
|
2013-12-15 22:19:50 +01:00
|
|
|
SPI_SS::Clear();
|
2014-08-03 00:06:46 +02:00
|
|
|
|
|
|
|
#if SPI_HAS_TRANSACTION
|
|
|
|
SPI.transfer(reg);
|
2014-08-03 18:01:55 +02:00
|
|
|
memset(data_p, 0, nbytes); // Make sure we send out empty bytes
|
2014-08-03 00:06:46 +02:00
|
|
|
SPI.transfer(data_p, nbytes);
|
|
|
|
data_p += nbytes;
|
|
|
|
#elif USING_SPI4TEENSY3
|
2013-11-24 14:56:34 +01:00
|
|
|
spi4teensy3::send(reg);
|
|
|
|
spi4teensy3::receive(data_p, nbytes);
|
|
|
|
data_p += nbytes;
|
2014-02-25 11:47:59 +01:00
|
|
|
#elif defined(ARDUINO_SAM_DUE) && defined(__SAM3X8E__)
|
|
|
|
SPI.transfer(reg);
|
|
|
|
while(nbytes) {
|
|
|
|
*data_p++ = SPI.transfer(0);
|
|
|
|
nbytes--;
|
|
|
|
}
|
2013-11-24 14:56:34 +01:00
|
|
|
#else
|
2013-03-28 08:26:02 +01:00
|
|
|
SPDR = reg;
|
2013-03-28 09:46:43 +01:00
|
|
|
while(!(SPSR & (1 << SPIF))); //wait
|
|
|
|
while(nbytes) {
|
2014-08-03 18:01:55 +02:00
|
|
|
SPDR = 0; // Send empty byte
|
2013-03-28 08:26:02 +01:00
|
|
|
nbytes--;
|
2013-03-28 09:46:43 +01:00
|
|
|
while(!(SPSR & (1 << SPIF)));
|
2013-05-14 02:54:12 +02:00
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
*data_p = SPDR;
|
|
|
|
printf("%2.2x ", *data_p);
|
|
|
|
}
|
2013-03-28 08:26:02 +01:00
|
|
|
data_p++;
|
|
|
|
}
|
2013-05-14 02:54:12 +02:00
|
|
|
printf("\r\n");
|
|
|
|
#else
|
|
|
|
*data_p++ = SPDR;
|
|
|
|
}
|
2013-11-24 14:56:34 +01:00
|
|
|
#endif
|
2013-05-14 02:54:12 +02:00
|
|
|
#endif
|
2014-08-03 00:06:46 +02:00
|
|
|
|
2013-12-15 22:19:50 +01:00
|
|
|
SPI_SS::Set();
|
2014-08-03 00:06:46 +02:00
|
|
|
#if SPI_HAS_TRANSACTION
|
|
|
|
SPI.endTransaction();
|
|
|
|
#endif
|
2013-10-08 21:52:24 +02:00
|
|
|
XMEM_RELEASE_SPI();
|
2013-12-25 11:09:57 +01:00
|
|
|
return ( data_p);
|
2011-01-19 07:27:20 +01:00
|
|
|
}
|
|
|
|
/* GPIO read. See gpioWr for explanation */
|
2013-03-28 08:26:02 +01:00
|
|
|
|
2011-01-19 07:27:20 +01:00
|
|
|
/* GPIN pins are in high nibbles of IOPINS1, IOPINS2 */
|
2013-12-15 22:19:50 +01:00
|
|
|
template< typename SPI_SS, typename INTR >
|
|
|
|
uint8_t MAX3421e< SPI_SS, INTR >::gpioRd() {
|
2013-03-28 08:26:02 +01:00
|
|
|
uint8_t gpin = 0;
|
|
|
|
gpin = regRd(rIOPINS2); //pins 4-7
|
|
|
|
gpin &= 0xf0; //clean lower nibble
|
|
|
|
gpin |= (regRd(rIOPINS1) >> 4); //shift low bits and OR with upper from previous operation.
|
2013-12-25 11:09:57 +01:00
|
|
|
return ( gpin);
|
2011-01-19 07:27:20 +01:00
|
|
|
}
|
2013-03-28 08:26:02 +01:00
|
|
|
|
2011-01-19 07:27:20 +01:00
|
|
|
/* reset MAX3421E. Returns number of cycles it took for PLL to stabilize after reset
|
|
|
|
or zero if PLL haven't stabilized in 65535 cycles */
|
2013-12-15 22:19:50 +01:00
|
|
|
template< typename SPI_SS, typename INTR >
|
|
|
|
uint16_t MAX3421e< SPI_SS, INTR >::reset() {
|
2013-03-28 08:26:02 +01:00
|
|
|
uint16_t i = 0;
|
|
|
|
regWr(rUSBCTL, bmCHIPRES);
|
|
|
|
regWr(rUSBCTL, 0x00);
|
2013-03-28 09:46:43 +01:00
|
|
|
while(++i) {
|
|
|
|
if((regRd(rUSBIRQ) & bmOSCOKIRQ)) {
|
2013-03-28 08:26:02 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-12-25 11:09:57 +01:00
|
|
|
return ( i);
|
2011-01-19 07:27:20 +01:00
|
|
|
}
|
2013-03-28 08:26:02 +01:00
|
|
|
|
2011-01-19 07:27:20 +01:00
|
|
|
/* initialize MAX3421E. Set Host mode, pullups, and stuff. Returns 0 if success, -1 if not */
|
2013-12-15 22:19:50 +01:00
|
|
|
template< typename SPI_SS, typename INTR >
|
|
|
|
int8_t MAX3421e< SPI_SS, INTR >::Init() {
|
2013-10-08 21:52:24 +02:00
|
|
|
XMEM_ACQUIRE_SPI();
|
|
|
|
// Moved here.
|
|
|
|
// you really should not init hardware in the constructor when it involves locks.
|
|
|
|
// Also avoids the vbus flicker issue confusing some devices.
|
|
|
|
/* pin and peripheral setup */
|
2013-12-15 22:19:50 +01:00
|
|
|
SPI_SS::SetDirWrite();
|
|
|
|
SPI_SS::Set();
|
2013-10-08 21:52:24 +02:00
|
|
|
spi::init();
|
|
|
|
INTR::SetDirRead();
|
|
|
|
XMEM_RELEASE_SPI();
|
|
|
|
/* MAX3421E - full-duplex SPI, level interrupt */
|
|
|
|
// GPX pin on. Moved here, otherwise we flicker the vbus.
|
|
|
|
regWr(rPINCTL, (bmFDUPSPI | bmINTLEVEL));
|
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
if(reset() == 0) { //OSCOKIRQ hasn't asserted in time
|
2013-12-25 11:09:57 +01:00
|
|
|
return ( -1);
|
2013-03-28 08:26:02 +01:00
|
|
|
}
|
2013-08-09 02:21:05 +02:00
|
|
|
|
2013-08-09 08:17:01 +02:00
|
|
|
regWr(rMODE, bmDPPULLDN | bmDMPULLDN | bmHOST); // set pull-downs, Host
|
|
|
|
|
|
|
|
regWr(rHIEN, bmCONDETIE | bmFRAMEIE); //connection detection
|
|
|
|
|
|
|
|
/* check if device is connected */
|
|
|
|
regWr(rHCTL, bmSAMPLEBUS); // sample USB bus
|
|
|
|
while(!(regRd(rHCTL) & bmSAMPLEBUS)); //wait for sample operation to finish
|
|
|
|
|
|
|
|
busprobe(); //check if anything is connected
|
|
|
|
|
|
|
|
regWr(rHIRQ, bmCONDETIRQ); //clear connection detect interrupt
|
|
|
|
regWr(rCPUCTL, 0x01); //enable interrupt pin
|
|
|
|
|
2013-12-25 11:09:57 +01:00
|
|
|
return ( 0);
|
2013-08-09 08:17:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize MAX3421E. Set Host mode, pullups, and stuff. Returns 0 if success, -1 if not */
|
2013-12-15 22:19:50 +01:00
|
|
|
template< typename SPI_SS, typename INTR >
|
|
|
|
int8_t MAX3421e< SPI_SS, INTR >::Init(int mseconds) {
|
2013-10-08 21:52:24 +02:00
|
|
|
XMEM_ACQUIRE_SPI();
|
|
|
|
// Moved here.
|
|
|
|
// you really should not init hardware in the constructor when it involves locks.
|
|
|
|
// Also avoids the vbus flicker issue confusing some devices.
|
|
|
|
/* pin and peripheral setup */
|
2013-12-15 22:19:50 +01:00
|
|
|
SPI_SS::SetDirWrite();
|
|
|
|
SPI_SS::Set();
|
2013-10-08 21:52:24 +02:00
|
|
|
spi::init();
|
|
|
|
INTR::SetDirRead();
|
|
|
|
XMEM_RELEASE_SPI();
|
|
|
|
/* MAX3421E - full-duplex SPI, level interrupt, vbus off */
|
|
|
|
regWr(rPINCTL, (bmFDUPSPI | bmINTLEVEL | GPX_VBDET));
|
|
|
|
|
2013-08-09 08:17:01 +02:00
|
|
|
if(reset() == 0) { //OSCOKIRQ hasn't asserted in time
|
2013-12-25 11:09:57 +01:00
|
|
|
return ( -1);
|
2013-08-09 08:17:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delay a minimum of 1 second to ensure any capacitors are drained.
|
|
|
|
// 1 second is required to make sure we do not smoke a Microdrive!
|
|
|
|
if(mseconds < 1000) mseconds = 1000;
|
|
|
|
delay(mseconds);
|
2013-08-09 02:21:05 +02:00
|
|
|
|
2013-03-28 08:26:02 +01:00
|
|
|
regWr(rMODE, bmDPPULLDN | bmDMPULLDN | bmHOST); // set pull-downs, Host
|
2011-03-01 19:26:31 +01:00
|
|
|
|
2013-03-28 08:26:02 +01:00
|
|
|
regWr(rHIEN, bmCONDETIE | bmFRAMEIE); //connection detection
|
2011-01-19 07:27:20 +01:00
|
|
|
|
2013-03-28 08:26:02 +01:00
|
|
|
/* check if device is connected */
|
|
|
|
regWr(rHCTL, bmSAMPLEBUS); // sample USB bus
|
2013-03-28 09:46:43 +01:00
|
|
|
while(!(regRd(rHCTL) & bmSAMPLEBUS)); //wait for sample operation to finish
|
2011-01-19 07:27:20 +01:00
|
|
|
|
2013-03-28 08:26:02 +01:00
|
|
|
busprobe(); //check if anything is connected
|
|
|
|
|
|
|
|
regWr(rHIRQ, bmCONDETIRQ); //clear connection detect interrupt
|
|
|
|
regWr(rCPUCTL, 0x01); //enable interrupt pin
|
2013-08-09 02:21:05 +02:00
|
|
|
|
|
|
|
// GPX pin on. This is done here so that busprobe will fail if we have a switch connected.
|
|
|
|
regWr(rPINCTL, (bmFDUPSPI | bmINTLEVEL));
|
|
|
|
|
2013-12-25 11:09:57 +01:00
|
|
|
return ( 0);
|
2011-01-19 07:27:20 +01:00
|
|
|
}
|
|
|
|
|
2013-03-28 08:26:02 +01:00
|
|
|
/* probe bus to determine device presence and speed and switch host to this speed */
|
2013-12-15 22:19:50 +01:00
|
|
|
template< typename SPI_SS, typename INTR >
|
|
|
|
void MAX3421e< SPI_SS, INTR >::busprobe() {
|
2013-03-28 08:26:02 +01:00
|
|
|
uint8_t bus_sample;
|
|
|
|
bus_sample = regRd(rHRSL); //Get J,K status
|
|
|
|
bus_sample &= (bmJSTATUS | bmKSTATUS); //zero the rest of the byte
|
2013-03-28 09:46:43 +01:00
|
|
|
switch(bus_sample) { //start full-speed or low-speed host
|
2013-03-28 08:26:02 +01:00
|
|
|
case( bmJSTATUS):
|
2013-03-28 09:46:43 +01:00
|
|
|
if((regRd(rMODE) & bmLOWSPEED) == 0) {
|
2013-03-28 08:26:02 +01:00
|
|
|
regWr(rMODE, MODE_FS_HOST); //start full-speed host
|
|
|
|
vbusState = FSHOST;
|
|
|
|
} else {
|
|
|
|
regWr(rMODE, MODE_LS_HOST); //start low-speed host
|
|
|
|
vbusState = LSHOST;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case( bmKSTATUS):
|
2013-03-28 09:46:43 +01:00
|
|
|
if((regRd(rMODE) & bmLOWSPEED) == 0) {
|
2013-03-28 08:26:02 +01:00
|
|
|
regWr(rMODE, MODE_LS_HOST); //start low-speed host
|
|
|
|
vbusState = LSHOST;
|
|
|
|
} else {
|
|
|
|
regWr(rMODE, MODE_FS_HOST); //start full-speed host
|
|
|
|
vbusState = FSHOST;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case( bmSE1): //illegal state
|
|
|
|
vbusState = SE1;
|
|
|
|
break;
|
|
|
|
case( bmSE0): //disconnected state
|
|
|
|
regWr(rMODE, bmDPPULLDN | bmDMPULLDN | bmHOST | bmSEPIRQ);
|
|
|
|
vbusState = SE0;
|
|
|
|
break;
|
2011-01-19 07:27:20 +01:00
|
|
|
}//end switch( bus_sample )
|
|
|
|
}
|
2013-03-28 08:26:02 +01:00
|
|
|
|
2011-01-19 07:27:20 +01:00
|
|
|
/* MAX3421 state change task and interrupt handler */
|
2013-12-15 22:19:50 +01:00
|
|
|
template< typename SPI_SS, typename INTR >
|
|
|
|
uint8_t MAX3421e< SPI_SS, INTR >::Task(void) {
|
2013-03-28 08:26:02 +01:00
|
|
|
uint8_t rcode = 0;
|
|
|
|
uint8_t pinvalue;
|
2013-06-12 05:11:43 +02:00
|
|
|
//USB_HOST_SERIAL.print("Vbus state: ");
|
|
|
|
//USB_HOST_SERIAL.println( vbusState, HEX );
|
2013-03-28 08:26:02 +01:00
|
|
|
pinvalue = INTR::IsSet(); //Read();
|
|
|
|
//pinvalue = digitalRead( MAX_INT );
|
2013-03-28 09:46:43 +01:00
|
|
|
if(pinvalue == 0) {
|
2013-03-28 08:26:02 +01:00
|
|
|
rcode = IntHandler();
|
|
|
|
}
|
|
|
|
// pinvalue = digitalRead( MAX_GPX );
|
|
|
|
// if( pinvalue == LOW ) {
|
|
|
|
// GpxHandler();
|
|
|
|
// }
|
|
|
|
// usbSM(); //USB state machine
|
2013-12-25 11:09:57 +01:00
|
|
|
return ( rcode);
|
2013-03-28 08:26:02 +01:00
|
|
|
}
|
|
|
|
|
2013-12-15 22:19:50 +01:00
|
|
|
template< typename SPI_SS, typename INTR >
|
|
|
|
uint8_t MAX3421e< SPI_SS, INTR >::IntHandler() {
|
2013-03-28 08:26:02 +01:00
|
|
|
uint8_t HIRQ;
|
|
|
|
uint8_t HIRQ_sendback = 0x00;
|
|
|
|
HIRQ = regRd(rHIRQ); //determine interrupt source
|
|
|
|
//if( HIRQ & bmFRAMEIRQ ) { //->1ms SOF interrupt handler
|
|
|
|
// HIRQ_sendback |= bmFRAMEIRQ;
|
|
|
|
//}//end FRAMEIRQ handling
|
2013-03-28 09:46:43 +01:00
|
|
|
if(HIRQ & bmCONDETIRQ) {
|
2013-03-28 08:26:02 +01:00
|
|
|
busprobe();
|
|
|
|
HIRQ_sendback |= bmCONDETIRQ;
|
|
|
|
}
|
|
|
|
/* End HIRQ interrupts handling, clear serviced IRQs */
|
|
|
|
regWr(rHIRQ, HIRQ_sendback);
|
2013-12-25 11:09:57 +01:00
|
|
|
return ( HIRQ_sendback);
|
2011-01-19 07:27:20 +01:00
|
|
|
}
|
2013-12-15 22:19:50 +01:00
|
|
|
//template< typename SPI_SS, typename INTR >
|
|
|
|
//uint8_t MAX3421e< SPI_SS, INTR >::GpxHandler()
|
2011-03-05 08:33:02 +01:00
|
|
|
//{
|
2014-08-03 00:06:46 +02:00
|
|
|
// uint8_t GPINIRQ = regRd( rGPINIRQ ); //read GPIN IRQ register
|
2011-03-05 08:33:02 +01:00
|
|
|
//// if( GPINIRQ & bmGPINIRQ7 ) { //vbus overload
|
|
|
|
//// vbusPwr( OFF ); //attempt powercycle
|
|
|
|
//// delay( 1000 );
|
|
|
|
//// vbusPwr( ON );
|
|
|
|
//// regWr( rGPINIRQ, bmGPINIRQ7 );
|
2013-03-28 08:26:02 +01:00
|
|
|
//// }
|
2011-03-05 08:33:02 +01:00
|
|
|
// return( GPINIRQ );
|
|
|
|
//}
|
2011-01-19 07:27:20 +01:00
|
|
|
|
|
|
|
#endif //_USBHOST_H_
|