Permalink
Browse files

maxpktsize check added to outTransfer

  • Loading branch information...
1 parent afa275f commit 608f3ac9497ad225241018b5e28fa07661e4b22f @felis committed Aug 10, 2010
Showing with 70 additions and 111 deletions.
  1. +34 −68 Max3421e.cpp
  2. +4 −12 Max3421e.h
  3. +6 −13 Max3421e_constants.h
  4. +7 −4 Max_LCD.cpp
  5. +3 −3 Max_LCD.h
  6. +4 −0 Usb.cpp
  7. +12 −11 ch9.h
View
@@ -5,89 +5,63 @@
static byte vbusState;
- uint8_t _ss_pin; //slave select
- uint8_t _int_pin; //interrupt
- uint8_t _reset_pin; //reset
-
/* Functions */
/* Constructor */
-MAX3421E::MAX3421E( uint8_t _ss, uint8_t _int, uint8_t _reset )
+MAX3421E::MAX3421E()
{
- /* assign pins */
- _ss_pin = _ss;
- _int_pin = _int;
- _reset_pin = _reset;
-
-// Serial.println( _ss_pin, DEC );
-// Serial.println( _int_pin, DEC );
-// Serial.println( _reset_pin, DEC );
-
- /* setup pins */
- pinMode( _int_pin, INPUT);
- pinMode( _ss_pin, OUTPUT );
- digitalWrite(_ss_pin,HIGH); //deselect MAX3421E
- pinMode( _reset_pin, OUTPUT );
- digitalWrite( _reset_pin, HIGH ); //release MAX3421E from reset
-
-
//Serial.begin( 9600 );
- //init();
+ init();
//powerOn();
}
byte MAX3421E::getVbusState( void )
{
return( vbusState );
}
-byte MAX3421E::getvar( void )
-{
- return( _int_pin );
-}
/* initialization */
void MAX3421E::init()
{
/* setup pins */
-// pinMode( MAX_INT, INPUT);
-// pinMode( MAX_SS, OUTPUT );
-// Deselect_MAX3421E;
-// pinMode( MAX_RESET, OUTPUT );
-// digitalWrite( MAX_RESET, HIGH ); //release MAX3421E from reset
+ pinMode( MAX_INT, INPUT);
+ pinMode( MAX_GPX, INPUT );
+ pinMode( MAX_SS, OUTPUT );
+ pinMode( BPNT_0, OUTPUT );
+ pinMode( BPNT_1, OUTPUT );
+ digitalWrite( BPNT_0, LOW );
+ digitalWrite( BPNT_1, LOW );
+ Deselect_MAX3421E;
+ pinMode( MAX_RESET, OUTPUT );
+ digitalWrite( MAX_RESET, HIGH ); //release MAX3421E from reset
}
-
-/* toggles breakpoint pin during debug */
+//byte MAX3421E::getVbusState( void )
+//{
+// return( vbusState );
+//}
void MAX3421E::toggle( byte pin )
{
digitalWrite( pin, HIGH );
digitalWrite( pin, LOW );
}
/* Single host register write */
-/* uncomeent all 3 lines with "interrupt" in description to disable/enable interrupts during transfer */
void MAX3421E::regWr( byte reg, byte val)
{
- // uint8_t SaveSREG = SREG; //save interrupt flag
- // cli(); //disable interrupts
- digitalWrite(_ss_pin,LOW);
+ Select_MAX3421E;
Spi.transfer( reg + 2 ); //set WR bit and send register number
Spi.transfer( val );
- digitalWrite(_ss_pin,HIGH);
- // SREG = SaveSREG; //restore interrupt flag
+ Deselect_MAX3421E;
}
/* multiple-byte write */
/* returns a pointer to a memory position after last written */
-/* uncomeent all 3 lines with "interrupt" in description to disable/enable interrupts during transfer */
-char* MAX3421E::bytesWr( byte reg, byte nbytes, char * data )
+char * MAX3421E::bytesWr( byte reg, byte nbytes, char * data )
{
- // uint8_t SaveSREG = SREG; //save interrupt flag
- // cli(); //disable interrupts
- digitalWrite(_ss_pin,LOW); //assert SS
+ Select_MAX3421E; //assert SS
Spi.transfer ( reg + 2 ); //set W/R bit and select register
while( nbytes ) {
Spi.transfer( *data ); // send the next data byte
data++; // advance the pointer
nbytes--;
}
- digitalWrite(_ss_pin,HIGH); //deassert SS
- // SREG = SaveSREG; //restore interrupt flag
+ Deselect_MAX3421E; //deassert SS
return( data );
}
/* GPIO write. GPIO byte is split between 2 registers, so two writes are needed to write one byte */
@@ -102,35 +76,27 @@ void MAX3421E::gpioWr( byte val )
return;
}
/* Single host register read */
-/* uncomeent all 3 lines with "interrupt" in description to disable/enable interrupts during transfer */
byte MAX3421E::regRd( byte reg )
{
byte tmp;
- // uint8_t SaveSREG = SREG; //save interrupt flag
- // cli(); //disable interrupts
- digitalWrite(_ss_pin,LOW);
+ Select_MAX3421E;
Spi.transfer ( reg ); //send register number
tmp = Spi.transfer ( 0x00 ); //send empty byte, read register contents
- digitalWrite(_ss_pin,HIGH);
- // SREG = SaveSREG; //restore interrupt flag
+ Deselect_MAX3421E;
return (tmp);
}
/* multiple-bytes register read */
/* returns a pointer to a memory position after last read */
-/* uncomeent all 3 lines with "interrupt" in description to disable/enable interrupts during transfer */
char * MAX3421E::bytesRd ( byte reg, byte nbytes, char * data )
{
- // uint8_t SaveSREG = SREG; //save interrupt flag
- // cli(); //disable interrupts
- digitalWrite(_ss_pin,LOW); //assert SS
+ Select_MAX3421E; //assert SS
Spi.transfer ( reg ); //send register number
while( nbytes ) {
*data = Spi.transfer ( 0x00 ); //send empty byte, read register contents
data++;
nbytes--;
}
- digitalWrite(_ss_pin,HIGH); //deassert SS
- // SREG = SaveSREG; //restore interrupt flag
+ Deselect_MAX3421E; //deassert SS
return( data );
}
/* GPIO read. See gpioWr for explanation */
@@ -233,27 +199,27 @@ void MAX3421E::powerOn()
// }
/* configure host operation */
regWr( rMODE, bmDPPULLDN|bmDMPULLDN|bmHOST|bmSEPIRQ ); // set pull-downs, Host, Separate GPIN IRQ on GPX
- regWr( rHIEN, bmCONDETIE/*|bmFRAMEIE */); //connection detection
+ regWr( rHIEN, bmCONDETIE|bmFRAMEIE ); //connection detection
regWr(rHCTL,bmSAMPLEBUS); // update the JSTATUS and KSTATUS bits
busprobe(); //check if anything is connected
regWr( rHIRQ, bmCONDETIRQ ); //clear connection detect interrupt
- //regWr( rHIRQ, 0xff );
- regWr( rCPUCTL, bmIE ); //enable interrupt pin
+ regWr( rCPUCTL, 0x01 ); //enable interrupt pin
}
/* MAX3421 state change task and interrupt handler */
byte MAX3421E::Task( void )
{
byte rcode = 0;
byte pinvalue;
-
- pinvalue = digitalRead( _int_pin );
+ //Serial.print("Vbus state: ");
+ //Serial.println( vbusState, HEX );
+ pinvalue = digitalRead( MAX_INT );
if( pinvalue == LOW ) {
rcode = IntHandler();
}
-// pinvalue = digitalRead( MAX_GPX );
-// if( pinvalue == LOW ) {
-// GpxHandler();
-// }
+ pinvalue = digitalRead( MAX_GPX );
+ if( pinvalue == LOW ) {
+ GpxHandler();
+ }
// usbSM(); //USB state machine
return( rcode );
}
View
@@ -3,16 +3,15 @@
#define _MAX3421E_H_
-#include <Spi.h>
+#include <../Spi/Spi.h>
//#include <WProgram.h>
#include "WProgram.h"
#include "Max3421e_constants.h"
class MAX3421E : public SPI {
// byte vbusState;
public:
- MAX3421E( uint8_t _ss = MAX_SS, uint8_t _int = MAX_INT, uint8_t _reset = MAX_RESET );
- byte getvar( void );
+ MAX3421E( void );
byte getVbusState( void );
void toggle( byte pin );
static void regWr( byte, byte );
@@ -28,19 +27,12 @@ class MAX3421E : public SPI {
byte IntHandler();
byte GpxHandler();
byte Task();
- //void max_select();
private:
void init();
- //void max_select();
- friend class Max_LCD;
-
-
+ friend class Max_LCD;
};
-// int8_t _ss_pin; //slave select
-// uint8_t _int_pin; //interrupt
-// uint8_t _reset_pin; //reset
-//inline void MAX3421E::max_select() { digitalWrite(_ss_pin,LOW); }
+
#endif //_MAX3421E_H_
View
@@ -3,24 +3,17 @@
#ifndef _MAX3421Econstants_h_
#define _MAX3421Econstants_h_
-//#define BLACKWIDOW
-
-/* Arduino pin definitions for USB Host Shield signals. They can be changed here or while calling constructor */
+/* Arduino pin definitions */
#define MAX_SS 10
#define MAX_INT 9
-//#define MAX_GPX 8
+#define MAX_GPX 8
#define MAX_RESET 7
-#ifdef BLACKWIDOW
-#define MAX_SS 6
-#define MAX_INT 3
-#endif
-
-//#define BPNT_0 3
-//#define BPNT_1 2
+#define BPNT_0 3
+#define BPNT_1 2
-//#define Select_MAX3421E digitalWrite(_ss_pin,LOW)
-//#define Deselect_MAX3421E digitalWrite(_ss_pin,HIGH)
+#define Select_MAX3421E digitalWrite(MAX_SS,LOW)
+#define Deselect_MAX3421E digitalWrite(MAX_SS,HIGH)
/* */
View
@@ -55,6 +55,9 @@ Max_LCD::Max_LCD()
void Max_LCD::init()
{
_displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
+
+ // MAX3421E::gpioWr(0x55);
+
begin(16, 1);
}
@@ -121,18 +124,18 @@ void Max_LCD::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
void Max_LCD::clear()
{
command(LCD_CLEARDISPLAY); // clear display, set cursor position to zero
- delayMicroseconds(3000); // this command takes a long time!
+ delayMicroseconds(2000); // this command takes a long time!
}
void Max_LCD::home()
{
- command(LCD_RETURNHOME); // set cursor position to zero
- delayMicroseconds(3000); // this command takes a long time!
+ command(LCD_RETURNHOME); // set cursor position to zero
+ delayMicroseconds(2000); // this command takes a long time!
}
void Max_LCD::setCursor(uint8_t col, uint8_t row)
{
- uint8_t row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
+ int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
if ( row > _numlines ) {
row = _numlines-1; // we count rows starting w/0
}
View
@@ -1,8 +1,8 @@
//HD44780 compatible LCD display via MAX3421E GPOUT support header
-//pinout: D[4-7] -> GPOUT[4-7], RS-> GPOUT[2], E ->GPOUT[3], no LCD reads therefore R/W shall be grounded
+//pinout: D[4-7] -> GPOUT[4-7], RS-> GPOUT[2], E ->GPOUT[3]
//
-//this code is borrowing heavily from official Arduino source v.0017
-//link to original http://code.google.com/p/arduino/source/browse/trunk/hardware/libraries/LiquidCrystal/LiquidCrystal.h
+//this code is heavily borrowed from official Arduino source v.0017
+// link to original http://code.google.com/p/arduino/source/browse/trunk/hardware/libraries/LiquidCrystal/LiquidCrystal.h
//
#ifndef _Max_LCD_h_
#define _Max_LCD_h_
View
@@ -171,6 +171,10 @@ byte USB::outTransfer( byte addr, byte ep, unsigned int nbytes, char* data, unsi
byte maxpktsize = devtable[ addr ].epinfo[ ep ].MaxPktSize;
unsigned long timeout = millis() + USB_XFER_TIMEOUT;
+ if (!maxpktsize) { //todo: move this check close to epinfo init. Make it 1< pktsize <64
+ return 0xFE;
+ }
+
regWr( rHCTL, devtable[ addr ].epinfo[ ep ].sndToggle ); //set toggle value
while( bytes_left ) {
retry_count = 0;
View
23 ch9.h
@@ -96,6 +96,7 @@
/* descriptor data structures */
+
/* Device descriptor structure */
typedef struct {
byte bLength; // Length of this descriptor.
@@ -113,6 +114,7 @@ typedef struct {
byte iSerialNumber; // Index of String Descriptor with the device's serial number.
byte bNumConfigurations; // Number of possible configurations.
} USB_DEVICE_DESCRIPTOR;
+
/* Configuration descriptor structure */
typedef struct
{
@@ -125,6 +127,7 @@ typedef struct
byte bmAttributes; // Configuration characteristics.
byte bMaxPower; // Maximum power consumed by this configuration.
} USB_CONFIGURATION_DESCRIPTOR;
+
/* Interface descriptor structure */
typedef struct
{
@@ -138,6 +141,7 @@ typedef struct
byte bInterfaceProtocol; // Protocol code (assigned by the USB-IF). 0xFF-Vendor specific.
byte iInterface; // Index of String Descriptor describing the interface.
} USB_INTERFACE_DESCRIPTOR;
+
/* Endpoint descriptor structure */
typedef struct
{
@@ -148,19 +152,16 @@ typedef struct
unsigned int wMaxPacketSize; // Maximum packet size.
byte bInterval; // Polling interval in frames.
} USB_ENDPOINT_DESCRIPTOR;
+
/* HID descriptor */
typedef struct {
- uint8_t bLength;
- uint8_t bDescriptorType;
- uint16_t bcdHID;
- uint8_t bCountryCode;
- uint8_t bNumDescriptors;
- /* at least one descriptor follows, 3 bytes - descritor type, descriptor length */
+ byte bLength;
+ byte bDescriptorType;
+ unsigned int bcdHID;
+ byte bCountryCode;
+ byte bNumDescriptors;
+ byte bDescrType;
+ unsigned int wDescriptorLength;
} USB_HID_DESCRIPTOR;
-/* HID Class descriptor format in HID descriptor */
-typedef struct {
- uint8_t bDescriptorType;
- uint16_t wDescriptorLength;
-} HID_CLASS;
#endif // _ch9_h_

0 comments on commit 608f3ac

Please sign in to comment.