1919 **/
2020
2121#include <fx2macros.h>
22+ #include <setupdat.h>
23+ #include <i2c.h>
24+ #include <eputils.h>
2225#include <delay.h>
26+ #include "hw6022be.inc"
27+
28+ #define SYNCDELAY SYNCDELAY7
2329
2430#ifdef DEBUG_FIRMWARE
2531#include <stdio.h>
@@ -88,12 +94,12 @@ void set_aadj() {
8894BOOL set_numchannels (BYTE n )
8995{
9096 if (n == 1 || n == 2 ) {
91- BYTE fifocfg = 0x08 + (n - 1 ); //AUTO_IN, WORD=numchannels-1
92- EP2FIFOCFG = fifocfg ;
93- EP6FIFOCFG = fifocfg ;
94- numchannels = n ;
95- set_aadj ();
96- return TRUE;
97+ BYTE fifocfg = 0x08 + (n - 1 ); //AUTO_IN, WORD=numchannels-1
98+ EP2FIFOCFG = fifocfg ;
99+ EP6FIFOCFG = fifocfg ;
100+ numchannels = n ;
101+ set_aadj ();
102+ return TRUE;
97103 }
98104 return FALSE;
99105}
@@ -104,7 +110,7 @@ void clear_fifo()
104110 GPIFABORT = 0xff ;
105111 SYNCDELAY3 ;
106112 while (!(GPIFTRIG & 0x80 )) {
107- ;
113+ ;
108114 }
109115 // see section 9.3.13 in EZ-USB trm
110116 FIFORESET = 0x80 ;
@@ -129,9 +135,9 @@ void stop_sampling()
129135 GPIFABORT = 0xff ;
130136 SYNCDELAY3 ;
131137 if (altiface == 0 ) {
132- INPKTEND = 6 ;
138+ INPKTEND = 6 ;
133139 } else {
134- INPKTEND = 2 ;
140+ INPKTEND = 2 ;
135141 }
136142}
137143
@@ -144,45 +150,48 @@ void start_sampling()
144150 SYNCDELAY3 ;
145151 GPIFTCB0 = 0 ;
146152 if (altiface == 0 )
147- GPIFTRIG = 6 ;
153+ GPIFTRIG = 6 ;
148154 else
149- GPIFTRIG = 4 ;
155+ GPIFTRIG = 4 ;
150156
151157 // set green led
152158 // don't clear led
153- ledcounter = 0 ;
154- PC0 = 1 ;
155- PC1 = 0 ;
159+ ledcounter = 1000 ;
160+ LED_RED = LED_OFF ;
161+ LED_GREEN = LED_ON ;
156162}
157163
158164extern __code BYTE highspd_dscr ;
159165extern __code BYTE fullspd_dscr ;
160166void select_interface (BYTE alt )
161167{
162168 const BYTE * pPacketSize = (USBCS & bmHSM ? & highspd_dscr : & fullspd_dscr )
163- + (9 + 16 * alt + 9 + 4 );
169+ + (9 + 16 * alt + 9 + 4 );
164170 altiface = alt ;
165171 if (alt == 0 ) {
166- // bulk on port 6
167- EP2CFG = 0x00 ;
168- EP6CFG = 0xe0 ;
169- EP6GPIFFLGSEL = 1 ;
172+ // bulk on port 6
173+ EP2CFG = 0x00 ;
174+ EP6CFG = 0xe0 ;
175+ EP6GPIFFLGSEL = 1 ;
170176
171- EP6AUTOINLENL = pPacketSize [0 ];
172- EP6AUTOINLENH = pPacketSize [1 ];
177+ EP6AUTOINLENL = pPacketSize [0 ];
178+ EP6AUTOINLENH = pPacketSize [1 ];
173179 } else {
174- // iso on port 2
175- EP2CFG = 0xd8 ;
176- EP6CFG = 0x00 ;
177- EP2GPIFFLGSEL = 1 ;
178-
179- EP2AUTOINLENL = pPacketSize [0 ];
180- EP2AUTOINLENH = pPacketSize [1 ] & 0x7 ;
181- EP2ISOINPKTS = (pPacketSize [1 ] >> 3 ) + 1 ;
182- set_aadj ();
180+ // iso on port 2
181+ EP2CFG = 0xd8 ;
182+ EP6CFG = 0x00 ;
183+ EP2GPIFFLGSEL = 1 ;
184+
185+ EP2AUTOINLENL = pPacketSize [0 ];
186+ EP2AUTOINLENH = pPacketSize [1 ] & 0x7 ;
187+ EP2ISOINPKTS = (pPacketSize [1 ] >> 3 ) + 1 ;
188+ set_aadj ();
183189 }
184190}
185191
192+ #define OUT0 0x40
193+ #define OUT1 0x44
194+
186195const struct samplerate_info {
187196 BYTE rateid ;
188197 WORD rateksps ;
@@ -195,25 +204,25 @@ const struct samplerate_info {
195204} samplerates [] = {
196205 { 48 , 48000 , 0x80 , 0 , 3 , 0 , 0x00 , 0xea },
197206 { 30 , 30000 , 0x80 , 0 , 3 , 0 , 0x00 , 0xaa },
198- { 24 , 24000 , 1 , 0 , 2 , 1 , 0x40 , 0xca },
199- { 16 , 16000 , 1 , 1 , 2 , 0 , 0x40 , 0xca },
200- { 12 , 12000 , 2 , 1 , 2 , 0 , 0x40 , 0xca },
201- { 8 , 8000 , 3 , 2 , 2 , 0 , 0x40 , 0xca },
202- { 4 , 4000 , 6 , 5 , 2 , 0 , 0x40 , 0xca },
203- { 2 , 2000 , 12 , 11 , 2 , 0 , 0x40 , 0xca },
204- { 1 , 1000 , 24 , 23 , 2 , 0 , 0x40 , 0xca },
205- { 50 , 500 , 48 , 47 , 2 , 0 , 0x40 , 0xca },
206- { 20 , 200 , 120 , 119 , 2 , 0 , 0x40 , 0xca },
207- { 10 , 100 , 240 , 239 , 2 , 0 , 0x40 , 0xca }
207+ { 24 , 24000 , 1 , 0 , 2 , 1 , OUT0 , 0xca },
208+ { 16 , 16000 , 1 , 1 , 2 , 0 , OUT0 , 0xca },
209+ { 12 , 12000 , 2 , 1 , 2 , 0 , OUT0 , 0xca },
210+ { 8 , 8000 , 3 , 2 , 2 , 0 , OUT0 , 0xca },
211+ { 4 , 4000 , 6 , 5 , 2 , 0 , OUT0 , 0xca },
212+ { 2 , 2000 , 12 , 11 , 2 , 0 , OUT0 , 0xca },
213+ { 1 , 1000 , 24 , 23 , 2 , 0 , OUT0 , 0xca },
214+ { 50 , 500 , 48 , 47 , 2 , 0 , OUT0 , 0xca },
215+ { 20 , 200 , 120 , 119 , 2 , 0 , OUT0 , 0xca },
216+ { 10 , 100 , 240 , 239 , 2 , 0 , OUT0 , 0xca }
208217};
209218
210219BOOL set_samplerate (BYTE rateid )
211220{
212221 BYTE i = 0 ;
213222 while (samplerates [i ].rateid != rateid ) {
214- i ++ ;
215- if (i == sizeof (samplerates )/sizeof (samplerates [0 ]))
216- return FALSE;
223+ i ++ ;
224+ if (i == sizeof (samplerates )/sizeof (samplerates [0 ]))
225+ return FALSE;
217226 }
218227 samplerate = samplerates [i ].rateksps ;
219228 set_aadj ();
@@ -256,8 +265,8 @@ BOOL set_samplerate(BYTE rateid)
256265 EXTAUTODAT2 = 0 ;
257266
258267 EXTAUTODAT2 = samplerates [i ].out0 ;
259- EXTAUTODAT2 = 0x44 ;
260- EXTAUTODAT2 = 0x44 ;
268+ EXTAUTODAT2 = OUT1 ;
269+ EXTAUTODAT2 = OUT1 ;
261270 EXTAUTODAT2 = 0x00 ;
262271 EXTAUTODAT2 = 0x00 ;
263272 EXTAUTODAT2 = 0x00 ;
@@ -274,7 +283,7 @@ BOOL set_samplerate(BYTE rateid)
274283 EXTAUTODAT2 = 0 ;
275284
276285 for (i = 0 ; i < 96 ; i ++ )
277- EXTAUTODAT2 = 0 ;
286+ EXTAUTODAT2 = 0 ;
278287 return TRUE;
279288}
280289
@@ -296,7 +305,7 @@ BOOL handle_get_interface(BYTE ifc, BYTE* alt_ifc) {
296305BOOL handle_set_interface (BYTE ifc ,BYTE alt_ifc ) {
297306 printf ( "Set Interface.\n" );
298307 if (ifc == 0 ) {
299- select_interface (alt_ifc );
308+ select_interface (alt_ifc );
300309 }
301310 return TRUE;
302311}
@@ -312,46 +321,93 @@ BOOL handle_set_configuration(BYTE cfg) {
312321 return TRUE;
313322}
314323
324+ // handle getting and setting EEPROM data
325+ BOOL eeprom () {
326+ WORD addr = SETUP_VALUE (), len = SETUP_LENGTH ();
327+ // wait for ep0 not busy
328+ while ( EP0CS & bmEPBUSY )
329+ ;
330+ switch ( SETUP_TYPE ) {
331+ case 0xc0 :
332+ while ( len ) { // still have bytes to read
333+ BYTE cur_read = len > 64 ? 64 : len ; // can't read more than 64 bytes at a time
334+ while ( EP0CS & bmEPBUSY ) // can't do this until EP0 is ready
335+ ;
336+ eeprom_read ( 0x51 , addr , cur_read , EP0BUF );
337+ EP0BCH = 0 ;
338+ SYNCDELAY ;
339+ EP0BCL = cur_read ;
340+ len -= cur_read ;
341+ addr += cur_read ;
342+ }
343+ break ;
344+ case 0x40 :
345+ while ( len ) {
346+ BYTE cur_write , c ;
347+ // printf ( "Len More Bytes %d\n" , len );
348+ EP0BCL = 0 ; // allow pc transfer in
349+ while ( EP0CS & bmEPBUSY ) // wait
350+ ;
351+ cur_write = EP0BCL ;
352+ // printf ( "Writing %d Bytes to %d..\n", cur_write, addr );
353+ if ( !eeprom_write ( 0x51 , addr , cur_write , EP0BUF ) )
354+ return FALSE;
355+ addr += cur_write ;
356+ len -= cur_write ;
357+ }
358+ break ;
359+ default :
360+ return FALSE; // bad type
361+ }
362+ return TRUE;
363+ }
315364
316365//******************* VENDOR COMMAND HANDLERS **************************
317366
318367BOOL handle_vendorcommand (BYTE cmd ) {
319368 stop_sampling ();
320369 // Set Red LED
321- PC0 = 0 ;
322- PC1 = 1 ;
323- ledcounter = 1000 ;
370+ LED_RED = LED_ON ;
371+ LED_GREEN = LED_OFF ;
372+ ledcounter = 1000 ; // monoflop
324373 switch (cmd ) {
374+ case 0xa2 :
375+ return eeprom ();
325376 case 0xe0 :
326377 case 0xe1 :
327- EP0BCH = 0 ;
328- EP0BCL = 0 ;
329- while (EP0CS & bmEPBUSY );
330- set_voltage (cmd - 0xe0 , EP0BUF [0 ]);
331- return TRUE;
378+ EP0BCH = 0 ;
379+ EP0BCL = 0 ;
380+ while (EP0CS & bmEPBUSY )
381+ ;
382+ set_voltage (cmd - 0xe0 , EP0BUF [0 ]);
383+ return TRUE;
332384 case 0xe2 :
333- EP0BCH = 0 ;
334- EP0BCL = 0 ;
335- while (EP0CS & bmEPBUSY );
336- set_samplerate (EP0BUF [0 ]);
337- return TRUE;
385+ EP0BCH = 0 ;
386+ EP0BCL = 0 ;
387+ while (EP0CS & bmEPBUSY )
388+ ;
389+ set_samplerate (EP0BUF [0 ]);
390+ return TRUE;
338391 case 0xe3 :
339- EP0BCH = 0 ;
340- EP0BCL = 0 ;
341- while (EP0CS & bmEPBUSY );
342- if (EP0BUF [0 ] == 1 )
343- start_sampling ();
344- return TRUE;
392+ EP0BCH = 0 ;
393+ EP0BCL = 0 ;
394+ while (EP0CS & bmEPBUSY )
395+ ;
396+ if (EP0BUF [0 ] == 1 )
397+ start_sampling ();
398+ return TRUE;
345399 case 0xe4 :
346- EP0BCH = 0 ;
347- EP0BCL = 0 ;
348- while (EP0CS & bmEPBUSY );
349- set_numchannels (EP0BUF [0 ]);
350- return TRUE;
400+ EP0BCH = 0 ;
401+ EP0BCL = 0 ;
402+ while (EP0CS & bmEPBUSY )
403+ ;
404+ set_numchannels (EP0BUF [0 ]);
405+ return TRUE;
351406 }
352407 return FALSE; // not handled by handlers
353408}
354409
410+
355411//******************** INIT ***********************
356412
357413void main_init () {
@@ -378,4 +434,3 @@ void main_init() {
378434void main_loop () {
379435}
380436
381-
0 commit comments