+//! Handle an EM260 exchange.
+void spi_rw_em260(u8 app, u8 verb, u32 len){
+ static int state=0;
+ unsigned long i;
+ u8 lastin;
+
+ P4DIR=0; //TODO ASAP remove P4 references.
+ P4OUT=0xFF;
+ P4REN=0xFF;
+
+ //See GoodFETEM260.py for details.
+ //The EM260 requires that the host wait for the client.
+
+ /*
+ if((~P4IN)&1)
+ debugstr("Detected HOST_INT.");
+ */
+
+ //if(!state++)
+ //if(state++&1)
+ em260_wake();
+
+ em260woken:
+
+ SETMOSI; //Autodetected SPI mode.
+ CLRSS; //Drop !SS to begin transaction.
+ //Host to slave. Ignore data.
+ for(i=0;i<len;i++){
+ lastin=spitrans8(cmddata[i]);
+ if(lastin!=0xFF){
+ debugstr("Got a byte during transmission. (Shouldn't happen with EM260.)");
+ debughex(lastin);
+ }
+ }
+ //debugstr("Finished transmission to EM260.");
+
+ //Wait for nHOST_INT to drop.
+ i=0xffff;
+
+ /*
+ while(P4IN&1
+ && --i
+ )
+ spitrans8(0xFF);
+ */
+ while((cmddata[0]=spitrans8(0xFF))==0xFF
+ && --i);
+
+ if(!i)
+ debugstr("Gave up on host interrupt.");
+
+ debugstr("Reading response.");
+ len=1;
+ while(
+ (cmddata[len++]=spitrans8(0xFF))!=0xA7
+ );
+ SETSS; //Raise !SS to end transaction.
+
+
+ if(cmddata[0]==0x02){
+ debugstr("Aborted transaction. :(");
+ //goto em260woken;
+ }
+
+ txdata(app,verb,len);
+ return;
+}