Permalink
Browse files

SPI layer re-written internally. Some bugs fixed.

  • Loading branch information...
1 parent 4346aff commit 867e250d9f5ce0e40b4cb164ba22e02abfb6012b @felis committed Dec 6, 2010
Showing with 107 additions and 64 deletions.
  1. +66 −55 Max3421e.cpp
  2. +19 −4 Max3421e.h
  3. +21 −5 Max3421e_constants.h
  4. +1 −0 Usb.cpp
View
@@ -10,58 +10,67 @@ static byte vbusState;
/* Constructor */
MAX3421E::MAX3421E()
{
- //Serial.begin( 9600 );
- init();
- //powerOn();
-}
-byte MAX3421E::getVbusState( void )
-{
- return( vbusState );
-}
-/* initialization */
-void MAX3421E::init()
-{
- /* setup pins */
+ spi_init();
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;
+ digitalWrite(MAX_SS,HIGH);
pinMode( MAX_RESET, OUTPUT );
digitalWrite( MAX_RESET, HIGH ); //release MAX3421E from reset
}
+
+byte MAX3421E::getVbusState( void )
+{
+ return( vbusState );
+}
+/* initialization */
+//void MAX3421E::init()
+//{
+// /* setup pins */
+// 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
+//}
//byte MAX3421E::getVbusState( void )
//{
// return( vbusState );
//}
-void MAX3421E::toggle( byte pin )
-{
- digitalWrite( pin, HIGH );
- digitalWrite( pin, LOW );
-}
+//void MAX3421E::toggle( byte pin )
+//{
+// digitalWrite( pin, HIGH );
+// digitalWrite( pin, LOW );
+//}
/* Single host register write */
void MAX3421E::regWr( byte reg, byte val)
{
- Select_MAX3421E;
- Spi.transfer( reg + 2 ); //set WR bit and send register number
- Spi.transfer( val );
- Deselect_MAX3421E;
+ digitalWrite(MAX_SS,LOW);
+ SPDR = ( reg | 0x02 );
+ while(!( SPSR & ( 1 << SPIF )));
+ SPDR = val;
+ while(!( SPSR & ( 1 << SPIF )));
+ digitalWrite(MAX_SS,HIGH);
+ return;
}
/* multiple-byte write */
/* returns a pointer to a memory position after last written */
char * MAX3421E::bytesWr( byte reg, byte nbytes, char * data )
{
- 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(MAX_SS,LOW);
+ SPDR = ( reg | 0x02 );
+ while( nbytes-- ) {
+ while(!( SPSR & ( 1 << SPIF ))); //check if previous byte was sent
+ SPDR = ( *data ); // send next data byte
+ data++; // advance data pointer
}
- Deselect_MAX3421E; //deassert SS
+ while(!( SPSR & ( 1 << SPIF )));
+ digitalWrite(MAX_SS,HIGH);
return( data );
}
/* GPIO write. GPIO byte is split between 2 registers, so two writes are needed to write one byte */
@@ -79,24 +88,29 @@ void MAX3421E::gpioWr( byte val )
byte MAX3421E::regRd( byte reg )
{
byte tmp;
- Select_MAX3421E;
- Spi.transfer ( reg ); //send register number
- tmp = Spi.transfer ( 0x00 ); //send empty byte, read register contents
- Deselect_MAX3421E;
- return (tmp);
+ digitalWrite(MAX_SS,LOW);
+ SPDR = reg;
+ while(!( SPSR & ( 1 << SPIF )));
+ SPDR = 0; //send empty byte
+ while(!( SPSR & ( 1 << SPIF )));
+ digitalWrite(MAX_SS,HIGH);
+ return( SPDR );
}
/* multiple-bytes register read */
/* returns a pointer to a memory position after last read */
char * MAX3421E::bytesRd ( byte reg, byte nbytes, char * data )
{
- Select_MAX3421E; //assert SS
- Spi.transfer ( reg ); //send register number
+ digitalWrite(MAX_SS,LOW);
+ SPDR = reg;
+ while(!( SPSR & ( 1 << SPIF ))); //wait
while( nbytes ) {
- *data = Spi.transfer ( 0x00 ); //send empty byte, read register contents
- data++;
- nbytes--;
+ SPDR = 0; //send empty byte
+ nbytes--;
+ while(!( SPSR & ( 1 << SPIF )));
+ *data = SPDR;
+ data++;
}
- Deselect_MAX3421E; //deassert SS
+ digitalWrite(MAX_SS,HIGH);
return( data );
}
/* GPIO read. See gpioWr for explanation */
@@ -148,7 +162,7 @@ boolean MAX3421E::vbusPwr ( boolean action )
// }
return( true ); // power on/off successful
}
-/* probe bus to determine device presense and speed */
+/* probe bus to determine device presense and speed and switch host to this speed */
void MAX3421E::busprobe( void )
{
byte bus_sample;
@@ -191,16 +205,13 @@ void MAX3421E::powerOn()
if( reset() == false ) { //stop/start the oscillator
Serial.println("Error: OSCOKIRQ failed to assert");
}
-// /* configure power switch */
-// vbusPwr( OFF ); //turn Vbus power off
-// regWr( rGPINIEN, bmGPINIEN7 ); //enable interrupt on GPIN7 (power switch overload flag)
-// if( vbusPwr( ON ) == false ) {
-// Serial.println("Error: Vbus overload");
-// }
+
/* 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(rHCTL,bmSAMPLEBUS); // update the JSTATUS and KSTATUS bits
+ /* 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
@@ -228,9 +239,9 @@ byte MAX3421E::IntHandler()
byte HIRQ;
byte HIRQ_sendback = 0x00;
HIRQ = regRd( rHIRQ ); //determine interrupt source
- if( HIRQ & bmFRAMEIRQ ) { //->1ms SOF interrupt handler
- HIRQ_sendback |= bmFRAMEIRQ;
- }//end FRAMEIRQ handling
+ //if( HIRQ & bmFRAMEIRQ ) { //->1ms SOF interrupt handler
+ // HIRQ_sendback |= bmFRAMEIRQ;
+ //}//end FRAMEIRQ handling
if( HIRQ & bmCONDETIRQ ) {
busprobe();
HIRQ_sendback |= bmCONDETIRQ;
View
@@ -3,17 +3,17 @@
#define _MAX3421E_H_
-#include <../Spi/Spi.h>
+//#include <Spi.h>
//#include <WProgram.h>
#include "WProgram.h"
#include "Max3421e_constants.h"
-class MAX3421E : public SPI {
+class MAX3421E /* : public SPI */ {
// byte vbusState;
public:
MAX3421E( void );
byte getVbusState( void );
- void toggle( byte pin );
+// void toggle( byte pin );
static void regWr( byte, byte );
char * bytesWr( byte, byte, char * );
static void gpioWr( byte );
@@ -28,7 +28,22 @@ class MAX3421E : public SPI {
byte GpxHandler();
byte Task();
private:
- void init();
+ static void spi_init() {
+ uint8_t tmp;
+ // initialize SPI pins
+ pinMode(SCK_PIN, OUTPUT);
+ pinMode(MOSI_PIN, OUTPUT);
+ pinMode(MISO_PIN, INPUT);
+ pinMode(SS_PIN, OUTPUT);
+ digitalWrite( SS_PIN, HIGH );
+ /* mode 00 (CPOL=0, CPHA=0) master, fclk/2. Mode 11 (CPOL=11, CPHA=11) is also supported by MAX3421E */
+ SPCR = 0x50;
+ SPSR = 0x01;
+ /**/
+ tmp = SPSR;
+ tmp = SPDR;
+ }
+// void init();
friend class Max_LCD;
};
View
@@ -3,23 +3,39 @@
#ifndef _MAX3421Econstants_h_
#define _MAX3421Econstants_h_
-/* Arduino pin definitions */
+/* SPI pins for diffrent Arduinos */
+
+#if defined(__AVR_ATmega1280__) || (__AVR_ATmega2560__)
+ #define SCK_PIN 52
+ #define MISO_PIN 50
+ #define MOSI_PIN 51
+ #define SS_PIN 53
+#endif
+#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
+ #define SCK_PIN 13
+ #define MISO_PIN 12
+ #define MOSI_PIN 11
+ #define SS_PIN 10
+#endif
+
#define MAX_SS 10
#define MAX_INT 9
#define MAX_GPX 8
#define MAX_RESET 7
-#define BPNT_0 3
-#define BPNT_1 2
+/* "Breakpoint" pins for debugging */
+//#define BPNT_0 3
+//#define BPNT_1 2
-#define Select_MAX3421E digitalWrite(MAX_SS,LOW)
-#define Deselect_MAX3421E digitalWrite(MAX_SS,HIGH)
+//#define Select_MAX3421E digitalWrite(MAX_SS,LOW)
+//#define Deselect_MAX3421E digitalWrite(MAX_SS,HIGH)
/* */
#define ON true
#define OFF false
+/* VBUS states */
#define SE0 0
#define SE1 1
#define FSHOST 2
View
@@ -318,6 +318,7 @@ void USB::Task( void ) //USB state machine
if(( regRd( rHCTL ) & bmBUSRST ) == 0 ) {
tmpdata = regRd( rMODE ) | bmSOFKAENAB; //start SOF generation
regWr( rMODE, tmpdata );
+// regWr( rMODE, bmSOFKAENAB );
usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_SOF;
delay = millis() + 20; //20ms wait after reset per USB spec
}

0 comments on commit 867e250

Please sign in to comment.