Browse Source

Initial implementation of the Toshiba FlashAir support:
Get the status of the Toshiba FlashAir, namely its IP address.
The IP address is currently reported through an M46, but this
is subject to change and the IP address shall be shown on the display.

bubnikv 8 years ago
parent
commit
562c089e17
6 changed files with 135 additions and 0 deletions
  1. 21 0
      Firmware/Marlin_main.cpp
  2. 84 0
      Firmware/Sd2Card.cpp
  3. 15 0
      Firmware/Sd2Card.h
  4. 6 0
      Firmware/SdInfo.h
  5. 7 0
      Firmware/cardreader.cpp
  6. 2 0
      Firmware/cardreader.h

+ 21 - 0
Firmware/Marlin_main.cpp

@@ -2792,6 +2792,27 @@ void process_commands()
         break;
     }
 
+    case 46:
+    {
+        // M46: Prusa3D: Show the assigned IP address.
+        uint8_t ip[4];
+        bool hasIP = card.ToshibaFlashAir_GetIP(ip);
+        if (hasIP) {
+            SERIAL_ECHOPGM("Toshiba FlashAir current IP: ");
+            SERIAL_ECHO(int(ip[0]));
+            SERIAL_ECHOPGM(".");
+            SERIAL_ECHO(int(ip[1]));
+            SERIAL_ECHOPGM(".");
+            SERIAL_ECHO(int(ip[2]));
+            SERIAL_ECHOPGM(".");
+            SERIAL_ECHO(int(ip[3]));
+            SERIAL_ECHOLNPGM("");
+        } else {
+            SERIAL_ECHOLNPGM("Toshiba FlashAir GetIP failed");          
+        }
+        break;
+    }
+
     case 47:
         // M47: Prusa3D: Show end stops dialog on the display.
         lcd_diag_show_end_stops();

+ 84 - 0
Firmware/Sd2Card.cpp

@@ -724,4 +724,88 @@ bool Sd2Card::writeStop() {
   return false;
 }
 
+//------------------------------------------------------------------------------
+/** Wait for start block token */
+//FIXME Vojtech: Copied from a current version of Sd2Card Arduino code.
+// We shall likely upgrade the rest of the Sd2Card.
+uint8_t Sd2Card::waitStartBlock(void) {
+  uint16_t t0 = millis();
+  while ((status_ = spiRec()) == 0XFF) {
+    if (((uint16_t)millis() - t0) > SD_READ_TIMEOUT) {
+      error(SD_CARD_ERROR_READ_TIMEOUT);
+      goto fail;
+    }
+  }
+  if (status_ != DATA_START_BLOCK) {
+    error(SD_CARD_ERROR_READ);
+    goto fail;
+  }
+  return true;
+
+ fail:
+  chipSelectHigh();
+  return false;
+}
+
+// Toshiba FlashAir support, copied from 
+// https://flashair-developers.com/en/documents/tutorials/arduino/
+
+//------------------------------------------------------------------------------
+/** Perform Extention Read. */
+uint8_t Sd2Card::readExt(uint32_t arg, uint8_t* dst, uint16_t count) {
+  uint16_t i;
+
+  // send command and argument.
+  if (cardCommand(CMD48, arg)) {
+    error(SD_CARD_ERROR_CMD48);
+    goto fail;
+  }
+  
+  // wait for start block token.
+  if (!waitStartBlock()) {
+    goto fail;
+  }
+
+  // receive data
+  for (i = 0; i < count; ++i) {
+    dst[i] = spiRec();
+  }
+  
+  // skip dummy bytes and 16-bit crc.
+  for (; i < 514; ++i) {
+    spiRec();
+  }
+
+  chipSelectHigh();
+  spiSend(0xFF); // dummy clock to force FlashAir finish the command.
+  return true;
+
+ fail:
+  chipSelectHigh();
+  return false;
+}
+
+//------------------------------------------------------------------------------
+/**
+ * Read an extension register space.
+ *
+ * \return The value one, true, is returned for success and
+ * the value zero, false, is returned for failure.
+ */
+uint8_t Sd2Card::readExtMemory(uint8_t mio, uint8_t func, 
+    uint32_t addr, uint16_t count, uint8_t* dst) {
+  uint32_t offset = addr & 0x1FF;
+  if (offset + count > 512) count = 512 - offset;
+  
+  if (count == 0) return true;
+  
+  uint32_t arg = 
+      (((uint32_t)mio & 0x1) << 31) | 
+    (mio ? (((uint32_t)func & 0x7) << 28) : (((uint32_t)func & 0xF) << 27)) |
+    ((addr & 0x1FFFF) << 9) |
+    ((count - 1) & 0x1FF);
+  
+  return readExt(arg, dst, count);
+}
+
 #endif

+ 15 - 0
Firmware/Sd2Card.h

@@ -105,6 +105,12 @@ uint8_t const SD_CARD_ERROR_SCK_RATE = 0X18;
 uint8_t const SD_CARD_ERROR_INIT_NOT_CALLED = 0X19;
 /** crc check error */
 uint8_t const SD_CARD_ERROR_CRC = 0X20;
+
+/** Toshiba FlashAir: iSDIO */
+uint8_t const SD_CARD_ERROR_CMD48 = 0x80;
+/** Toshiba FlashAir: iSDIO */
+uint8_t const SD_CARD_ERROR_CMD49 = 0x81;
+
 //------------------------------------------------------------------------------
 // card types
 /** Standard capacity V1 SD card */
@@ -215,6 +221,10 @@ class Sd2Card {
   bool writeData(const uint8_t* src);
   bool writeStart(uint32_t blockNumber, uint32_t eraseCount);
   bool writeStop();
+
+  // Toshiba FlashAir support
+  uint8_t readExtMemory(uint8_t mio, uint8_t func, uint32_t addr, uint16_t count, uint8_t* dst);
+
  private:
   //----------------------------------------------------------------------------
   uint8_t chipSelectPin_;
@@ -236,6 +246,11 @@ class Sd2Card {
   void type(uint8_t value) {type_ = value;}
   bool waitNotBusy(uint16_t timeoutMillis);
   bool writeData(uint8_t token, const uint8_t* src);
+
+
+  // Toshiba FlashAir support
+  uint8_t waitStartBlock(void);
+  uint8_t readExt(uint32_t arg, uint8_t* dst, uint16_t count);
 };
 #endif  // Sd2Card_h
 

+ 6 - 0
Firmware/SdInfo.h

@@ -62,6 +62,12 @@ uint8_t const CMD32 = 0X20;
 uint8_t const CMD33 = 0X21;
 /** ERASE - erase all previously selected blocks */
 uint8_t const CMD38 = 0X26;
+
+/** Toshiba FlashAir: iSDIO */
+uint8_t const CMD48 = 0x30;
+/** Toshiba FlashAir: iSDIO */
+uint8_t const CMD49 = 0x31;
+
 /** APP_CMD - escape for application specific command */
 uint8_t const CMD55 = 0X37;
 /** READ_OCR - read the OCR register of a card */

+ 7 - 0
Firmware/cardreader.cpp

@@ -653,4 +653,11 @@ void CardReader::printingHasFinished()
       autotempShutdown();
     }
 }
+
+bool CardReader::ToshibaFlashAir_GetIP(uint8_t *ip)
+{
+    memset(ip, 0, 4);
+    return card.readExtMemory(1, 1, 0x400+0x150, 4, ip);
+}
+
 #endif //SDSUPPORT

+ 2 - 0
Firmware/cardreader.h

@@ -47,6 +47,8 @@ public:
   FORCE_INLINE uint8_t percentDone(){if(!isFileOpen()) return 0; if(filesize) return sdpos/((filesize+99)/100); else return 0;};
   FORCE_INLINE char* getWorkDirName(){workDir.getFilename(filename);return filename;};
 
+  bool ToshibaFlashAir_GetIP(uint8_t *ip);
+
 public:
   bool saving;
   bool logging;