AVR client connecting and reading ID correctly!
authortravisutk <travisutk@12e2690d-a6be-4b82-a7b7-67c4a43b65c8>
Wed, 7 Oct 2009 10:16:03 +0000 (10:16 +0000)
committertravisutk <travisutk@12e2690d-a6be-4b82-a7b7-67c4a43b65c8>
Wed, 7 Oct 2009 10:16:03 +0000 (10:16 +0000)
git-svn-id: https://svn.code.sf.net/p/goodfet/code/trunk@190 12e2690d-a6be-4b82-a7b7-67c4a43b65c8

firmware/apps/avr/avr.c
firmware/lib/command.c

index 47ae68e..c86711d 100644 (file)
@@ -23,32 +23,60 @@ void avrconnect(){
   avrsetup();//set I/O pins
   
   //Pulse !RST (SS) at least twice while CLK is low.
+  CLRCLK;
   CLRSS;
+
+  SETSS;
   CLRCLK;
-  
-  for(i=0;i<5;i++){
-    SETSS;
-    CLRSS;
-  }
+  delay(500);
+  CLRSS;
+  delay(500);
   
   //Enable programming
   avr_prgen();
 }
 
+//! Read and write an SPI byte.
+unsigned char avrtrans8(unsigned char byte){
+  register unsigned int bit;
+  //This function came from the SPI Wikipedia article.
+  //Minor alterations.
+  
+  for (bit = 0; bit < 8; bit++) {
+    /* write MOSI on trailing edge of previous clock */
+    if (byte & 0x80)
+      SETMOSI;
+    else
+      CLRMOSI;
+    byte <<= 1;
+    
+    delay(2);
+    SETCLK;
+  
+    /* read MISO on trailing edge */
+    byte |= READMISO;
+    delay(2);
+    CLRCLK;
+  }
+  
+  return byte;
+}
+
 //! Perform a 4-byte exchange.
 u8 avrexchange(u8 a, u8 b, u8 c, u8 d){
-  spitrans8(a);
-  spitrans8(b);
-  if(spitrans8(c)!=b){
+  avrtrans8(a);
+  avrtrans8(b);
+  if(avrtrans8(c)!=b){
     debugstr("AVR sync error, b not returned as c.");
+  }else{
+    debugstr("Synced properly.");
   }
-  spitrans8(c);
-  return spitrans8(d);
+  return avrtrans8(d);
 }
 
 //! Enable AVR programming mode.
 void avr_prgen(){
-  avrexchange(0xac, 0x53, 0, 0);
+  avrexchange(0xAC, 0x53, 0, 0);
 }
 
 //! Read AVR device code.
@@ -78,9 +106,12 @@ void avrhandle(unsigned char app,
     avrsetup();
     txdata(app,verb,0);
     break;
-  case START:
+  case START://returns device code
     avrconnect();
-    txdata(app,verb,0);
+    //no break here
+  case AVR_PEEKSIG:
+    cmddata[0]=avr_devicecode();
+    txdata(app,verb,1);
     break;
   case PEEK:
   case POKE:
index 282d98a..a826571 100644 (file)
@@ -30,6 +30,17 @@ void debugstr(const char *str){
   txstring(0xFF,0xFF,str);
 }
 
+/*! \brief Transmit debug bytes.
+  
+  Transmits bytes for debugging.
+*/
+void debugbytes(const char *bytes, unsigned int len){
+  u16 i;
+  txhead(0xFF,0xFE,len);
+  for(i=0;i<len;i++)
+    serial_tx(bytes[i]);
+}
+
 
 //! Transmit a header.
 void txhead(unsigned char app,
@@ -37,7 +48,7 @@ void txhead(unsigned char app,
            unsigned long len){
   serial_tx(app);
   serial_tx(verb);
-  //serial_tx(len);
+  //serial_tx(len); //old protocol
   txword(len);
 }