e093024b9456604dcda1ff220ff58f136345a267
[goodfet] / firmware / apps / usb / maxusb.c
1 /*! \file max3421.c
2   \author Travis Goodspeed
3   \brief SPI Driver for MAX342x USB Controllers
4 */
5
6
7 #include "command.h"
8
9 #ifdef __MSPGCC__
10 #include <msp430.h>
11 #else
12 #include <signal.h>
13 #include <msp430.h>
14 #include <iomacros.h>
15 #endif
16
17 #include "maxusb.h"
18
19 #define MAXUSBAPPLICATION
20
21 #include "platform.h"
22
23
24 // define the spi app's app_t
25 app_t const maxusb_app = {
26
27         /* app number */
28         MAXUSB,
29
30         /* handle fn */
31         maxusb_handle_fn,
32
33         /* name */
34         "MAXUSB",
35
36         /* desc */
37         "\tThis allows you to write USB Host or USB Device drivers for\n"
38         "\t the MAX3421 and MAX3420 chips.\n"
39 };
40
41 //! Set up the pins for SPI mode.
42 void maxusb_setup(){
43   SETSS;
44   SPIDIR|=MOSI+SCK+BIT0; //BIT0 might be SS
45   SPIDIR&=~MISO;
46   P4DIR&=~TST; //TST line becomes interrupt input.
47   P2DIR|=RST;
48   DIRSS;
49   
50   
51   //Setup the configuration pins.
52   //This might need some delays.
53   CLRRST; //Put the chip into RESET.
54   //debugstr("MAXUSB is off.");
55   SETSS;  //Deselect the chip, end any existing transation.
56   SETRST; //Bring the chip out of RESET.
57   //debugstr("MAXUSB is on.");
58 }
59
60
61
62 //! Handles a MAXUSB monitor command.
63 void maxusb_handle_fn( uint8_t const app,
64                        uint8_t const verb,
65                        uint32_t const len){
66   unsigned long i;
67
68   //Raise !SS to end transaction, just in case we forgot.
69   SETSS;
70
71   switch(verb){
72   case READ:
73   case WRITE:
74     CLRSS; //Drop !SS to begin transaction.
75     for(i=0;i<len;i++)
76       cmddata[i]=spitrans8(cmddata[i]);
77     SETSS;  //Raise !SS to end transaction.
78     txdata(app,verb,len);
79     break;
80
81   case PEEK://TODO peek a register.
82     debugstr("PEEK isn't implemented in MAXUSB");
83     txdata(app,verb,0);
84     break;
85
86   case POKE://TODO poke a register.
87     debugstr("POKE isn't implemented in MAXUSB");
88     txdata(app,verb,0);
89     break;
90     
91   case SETUP:
92     maxusb_setup();
93     txdata(app,verb,0);
94     break;
95   }
96         
97   //Raise !SS to end transaction, in case we forgot.
98   SETSS;
99 }