SHARE
TWEET

Untitled

a guest Nov 19th, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Test #10: TNFS Read/Write, let's see if this works...
  3.  */
  4.  
  5. #include <ESP8266WiFi.h>
  6. #include <WiFiUdp.h>
  7.  
  8. #define TNFS_SERVER "192.168.1.114"
  9. #define TNFS_PORT 16384
  10.  
  11. enum {ID, COMMAND, AUX1, AUX2, CHECKSUM, ACK, NAK, PROCESS, WAIT} cmdState;
  12.  
  13. // Uncomment for Debug on 2nd UART (GPIO 2)
  14. #define DEBUG_S
  15.  
  16. #define PIN_LED         2
  17. #define PIN_INT         5
  18. #define PIN_PROC        4
  19. #define PIN_MTR        16
  20. #define PIN_CMD        12
  21.  
  22. #define DELAY_T5          1500
  23. #define READ_CMD_TIMEOUT  12
  24. #define CMD_TIMEOUT       50
  25.  
  26. #define STATUS_SKIP       8
  27.  
  28. unsigned long cmdTimer = 0;
  29. byte statusSkipCount = 0;
  30.  
  31. WiFiUDP UDP;
  32. byte tnfs_fd;
  33.  
  34. byte sector[128];
  35.  
  36. volatile bool cmdFlag=false;
  37.  
  38. union
  39. {
  40.   struct
  41.   {
  42.     byte session_idl;
  43.     byte session_idh;
  44.     byte retryCount;
  45.     byte command;
  46.     byte data[508];
  47.   };
  48.   byte rawData[512];
  49. } tnfsPacket;
  50.  
  51. union
  52. {
  53.   struct
  54.   {
  55.     unsigned char devic;
  56.     unsigned char comnd;
  57.     unsigned char aux1;
  58.     unsigned char aux2;
  59.     unsigned char cksum;
  60.   };
  61.   byte cmdFrameData[5];
  62. } cmdFrame;
  63.  
  64. /**
  65.    calculate 8-bit checksum.
  66. */
  67. byte sio_checksum(byte* chunk, int length)
  68. {
  69.   int chkSum = 0;
  70.   for (int i = 0; i < length; i++) {
  71.     chkSum = ((chkSum + chunk[i]) >> 8) + ((chkSum + chunk[i]) & 0xff);
  72.   }
  73.   return (byte)chkSum;
  74. }
  75.  
  76. /**
  77.    ISR for falling COMMAND
  78. */
  79. void ICACHE_RAM_ATTR sio_isr_cmd()
  80. {
  81.   cmdFlag=true;
  82. }
  83.  
  84. /**
  85.    Get ID
  86. */
  87. void sio_get_id()
  88. {
  89.   cmdFrame.devic = Serial.read();
  90.   if (cmdFrame.devic == 0x31)
  91.     cmdState = COMMAND;
  92.   else
  93.   {
  94.     cmdState = WAIT;
  95.     cmdTimer = 0;
  96.   }
  97.  
  98. #ifdef DEBUG_S
  99.   Serial1.print("CMD DEVC: ");
  100.   Serial1.println(cmdFrame.devic, HEX);
  101. #endif
  102. }
  103.  
  104. /**
  105.    Get Command
  106. */
  107.  
  108. void sio_get_command()
  109. {
  110.   cmdFrame.comnd = Serial.read();
  111.   cmdState=AUX1;
  112.  
  113. //  if (cmdFrame.comnd == 'R' || cmdFrame.comnd == 'W' || cmdFrame.comnd == 'P' || cmdFrame.comnd == 'S' )
  114. //    cmdState = AUX1;
  115. //  else
  116. //  {
  117. //    cmdState = WAIT;
  118. //    cmdTimer = 0;
  119. //  }
  120.  
  121. #ifdef DEBUG_S
  122.   Serial1.print("CMD CMND: ");
  123.   Serial1.println(cmdFrame.comnd, HEX);
  124. #endif
  125. }
  126.  
  127. /**
  128.    Get aux1
  129. */
  130. void sio_get_aux1()
  131. {
  132.   cmdFrame.aux1 = Serial.read();
  133.   cmdState = AUX2;
  134.  
  135. #ifdef DEBUG_S
  136.   Serial1.print("CMD AUX1: ");
  137.   Serial1.println(cmdFrame.aux1, HEX);
  138. #endif
  139. }
  140.  
  141. /**
  142.    Get aux2
  143. */
  144. void sio_get_aux2()
  145. {
  146.   cmdFrame.aux2 = Serial.read();
  147.   cmdState = CHECKSUM;
  148.  
  149. #ifdef DEBUG_S
  150.   Serial1.print("CMD AUX2: ");
  151.   Serial1.println(cmdFrame.aux2, HEX);
  152. #endif
  153. }
  154.  
  155. /**
  156.    Get Checksum, and compare
  157. */
  158. void sio_get_checksum()
  159. {
  160.   byte ck;
  161.   cmdFrame.cksum = Serial.read();
  162.   ck = sio_checksum((byte *)&cmdFrame.cmdFrameData, 4);
  163.  
  164. #ifdef DEBUG_S
  165.     Serial1.print("CMD CKSM: ");
  166.     Serial1.print(cmdFrame.cksum, HEX);
  167. #endif
  168.  
  169.     if (ck == cmdFrame.cksum)
  170.     {
  171. #ifdef DEBUG_S
  172.       Serial1.println(", ACK");
  173. #endif
  174.       sio_ack();
  175.     }
  176.     else
  177.     {
  178. #ifdef DEBUG_S
  179.       Serial1.println(", NAK");
  180. #endif
  181.       sio_nak();
  182.     }
  183. }
  184.  
  185. /**
  186.    Process command
  187. */
  188.  
  189. void sio_process()
  190. {
  191.   switch (cmdFrame.comnd)
  192.   {
  193.     case 'R':
  194.       sio_read();
  195.       break;
  196.     case 'W':
  197.     case 'P':
  198.       sio_write();
  199.       break;
  200.     case 'S':
  201.       sio_status();
  202.       break;
  203.     case '!':
  204.       sio_format();
  205.       break;
  206.   }
  207.  
  208.   cmdState = WAIT;
  209.   cmdTimer = 0;
  210. }
  211.  
  212. /**
  213.    format (fake)
  214. */
  215. void sio_format()
  216. {
  217.   byte ck;
  218.  
  219.   for (int i=0;i<128;i++)
  220.     sector[i]=0;
  221.  
  222.   sector[0]=0xFF; // no bad sectors.
  223.   sector[1]=0xFF;
  224.  
  225.   ck = sio_checksum((byte *)&sector, 128);
  226.  
  227.   delayMicroseconds(DELAY_T5); // t5 delay
  228.   Serial.write('C'); // Completed command
  229.   Serial.flush();
  230.  
  231.   // Write data frame
  232.   Serial.write(sector,128);
  233.    
  234.   // Write data frame checksum
  235.   Serial.write(ck);
  236.   Serial.flush();
  237.   delayMicroseconds(200);
  238. #ifdef DEBUG_S
  239.   Serial1.printf("We faked a format.\n");
  240. #endif
  241. }
  242.  
  243. /**
  244.    Read
  245. */
  246. void sio_read()
  247. {
  248.   byte ck;
  249.   int offset =(256 * cmdFrame.aux2)+cmdFrame.aux1;
  250.   offset *= 128;
  251.   offset -= 128;
  252.   offset += 16; // skip 16 byte ATR Header
  253.   tnfs_seek(offset);
  254.   tnfs_read();
  255.  
  256.   for (int i=0;i<128;i++)
  257.     sector[i]=tnfsPacket.data[i+3];
  258.  
  259.   ck = sio_checksum((byte *)&sector, 128);
  260.  
  261.   delayMicroseconds(DELAY_T5); // t5 delay
  262.   Serial.write('C'); // Completed command
  263.   Serial.flush();
  264.  
  265.   // Write data frame
  266.   Serial.write(sector,128);
  267.    
  268.   // Write data frame checksum
  269.   Serial.write(ck);
  270.   Serial.flush();
  271.   delayMicroseconds(200);
  272. #ifdef DEBUG_S
  273.   Serial1.print("SIO READ OFFSET: ");
  274.   Serial1.print(offset);
  275.   Serial1.print(" - ");
  276.   Serial1.println((offset + 128));
  277. #endif
  278. }
  279.  
  280. /**
  281.  * Write, called for both W and P commands.
  282.  */
  283. void sio_write()
  284. {
  285.   byte ck;
  286.   int offset =(256 * cmdFrame.aux2)+cmdFrame.aux1;
  287.   offset *= 128;
  288.   offset -= 128;
  289.   offset += 16; // skip 16 byte ATR Header
  290.   tnfs_seek(offset);
  291.  
  292. #ifdef DEBUG_S
  293.   Serial1.printf("receiving 128b data frame from computer.\n");
  294. #endif
  295.  
  296.   Serial.readBytes(sector,128);
  297.   ck=Serial.read(); // Read checksum
  298.   //delayMicroseconds(350);
  299.   Serial.write('A'); // Write ACK
  300.  
  301.   if (ck==sio_checksum(sector,128))
  302.   {
  303.     delayMicroseconds(DELAY_T5);
  304.     Serial.write('C');
  305.     tnfs_write();
  306.   }
  307. }
  308.  
  309. /**
  310.    Status
  311. */
  312. void sio_status()
  313. {
  314.   byte status[4] = {0x00, 0xFF, 0xFE, 0x00};
  315.   byte ck;
  316.  
  317.   ck = sio_checksum((byte *)&status, 4);
  318.  
  319.   delayMicroseconds(DELAY_T5); // t5 delay
  320.   Serial.write('C'); // Command always completes.
  321.   Serial.flush();
  322.   delayMicroseconds(200);
  323.   //delay(1);
  324.  
  325.   // Write data frame
  326.   for (int i = 0; i < 4; i++)
  327.     Serial.write(status[i]);
  328.  
  329.   // Write checksum
  330.   Serial.write(ck);
  331.   Serial.flush();
  332.   delayMicroseconds(200);
  333. }
  334.  
  335. /**
  336.    Send an acknowledgement
  337. */
  338. void sio_ack()
  339. {
  340.   delayMicroseconds(500);
  341.   Serial.write('A');
  342.   Serial.flush();
  343.   //cmdState = PROCESS;
  344.   sio_process();
  345. }
  346.  
  347. /**
  348.    Send a non-acknowledgement
  349. */
  350. void sio_nak()
  351. {
  352.   delayMicroseconds(500);
  353.   Serial.write('N');
  354.   Serial.flush();
  355.   cmdState = WAIT;
  356.   cmdTimer = 0;
  357. }
  358.  
  359. void sio_incoming(){
  360.   switch (cmdState)
  361.   {
  362.     case ID:
  363.       sio_get_id();
  364.       break;
  365.     case COMMAND:
  366.       sio_get_command();
  367.       break;
  368.     case AUX1:
  369.       sio_get_aux1();
  370.       break;
  371.     case AUX2:
  372.       sio_get_aux2();
  373.       break;
  374.     case CHECKSUM:
  375.       sio_get_checksum();
  376.       break;
  377.     case ACK:
  378.       sio_ack();
  379.       break;
  380.     case NAK:
  381.       sio_nak();
  382.       break;
  383.     case PROCESS:
  384.       sio_process();
  385.       break;
  386.     case WAIT:
  387.       Serial.read(); // Toss it for now
  388.       cmdTimer = 0;
  389.       break;
  390.   }
  391. }
  392.  
  393. /**
  394.  * Mount the TNFS server
  395.  */
  396. void tnfs_mount()
  397. {
  398.   int start=millis();
  399.   int dur=millis()-start;
  400.  
  401.   memset(tnfsPacket.rawData, 0, sizeof(tnfsPacket.rawData));
  402.   tnfsPacket.session_idl=0;
  403.   tnfsPacket.session_idh=0;
  404.   tnfsPacket.retryCount=0;
  405.   tnfsPacket.command=0;
  406.   tnfsPacket.data[0]=0x01;   // vers
  407.   tnfsPacket.data[1]=0x00;   // "  "
  408.   tnfsPacket.data[2]=0x2F;   // /
  409.   tnfsPacket.data[3]=0x00;   // nul
  410.   tnfsPacket.data[4]=0x00;   // no username
  411.   tnfsPacket.data[5]=0x00;   // no password
  412.  
  413. #ifdef DEBUG_S
  414.   Serial1.print("Mounting / from ");
  415.   Serial1.println(TNFS_SERVER);
  416.   Serial1.print("Req Packet: ");
  417.   for (int i=0;i<10;i++)
  418.   {
  419.     Serial1.print(tnfsPacket.rawData[i], HEX);
  420.     Serial1.print(" ");
  421.   }
  422.   Serial1.println("");
  423. #endif /* DEBUG_S */
  424.  
  425.   UDP.beginPacket(TNFS_SERVER,TNFS_PORT);
  426.   UDP.write(tnfsPacket.rawData,10);
  427.   UDP.endPacket();
  428.  
  429.   while (dur < 5000)
  430.   {
  431.     if (UDP.parsePacket())
  432.     {
  433.       int l=UDP.read(tnfsPacket.rawData,512);
  434. #ifdef DEBUG_S
  435.       Serial1.print("Resp Packet: ");
  436.       for (int i=0;i<l;i++)
  437.       {
  438.         Serial1.print(tnfsPacket.rawData[i], HEX);
  439.         Serial.print(" ");
  440.       }
  441.       Serial1.println("");
  442. #endif /* DEBUG_S */
  443.       if (tnfsPacket.data[0]==0x00)
  444.       {
  445.         // Successful
  446. #ifdef DEBUG_S
  447.         Serial1.print("Successful, Session ID: ");
  448.         Serial1.print(tnfsPacket.session_idl, HEX);
  449.         Serial1.println(tnfsPacket.session_idh, HEX);
  450. #endif /* DEBUG_S */
  451.         return;  
  452.       }
  453.       else
  454.       {
  455.         // Error
  456. #ifdef DEBUG_S
  457.         Serial1.print("Error #");
  458.         Serial1.println(tnfsPacket.data[0], HEX);
  459. #endif /* DEBUG_S */
  460.         return;  
  461.       }
  462.     }
  463.   }
  464.   // Otherwise we timed out.
  465. #ifdef DEBUG_S
  466. Serial1.println("Timeout after 5000ms");
  467. #endif /* DEBUG_S */
  468. }
  469.  
  470. /**
  471.  * Open 'autorun.atr'
  472.  */
  473. void tnfs_open()
  474. {
  475.   int start=millis();
  476.   int dur=millis()-start;
  477.   tnfsPacket.retryCount++;  // increase sequence #
  478.   tnfsPacket.command=0x29;  // OPEN
  479.   tnfsPacket.data[0]=0x03;  // R/W
  480.   tnfsPacket.data[1]=0x00;  //
  481.   tnfsPacket.data[2]=0x00;  // Flags
  482.   tnfsPacket.data[3]=0x00;  //
  483.   tnfsPacket.data[4]='/';   // Filename start
  484.   tnfsPacket.data[5]='a';
  485.   tnfsPacket.data[6]='u';
  486.   tnfsPacket.data[7]='t';
  487.   tnfsPacket.data[8]='o';
  488.   tnfsPacket.data[9]='r';
  489.   tnfsPacket.data[10]='u';
  490.   tnfsPacket.data[11]='n';
  491.   tnfsPacket.data[12]='.';
  492.   tnfsPacket.data[13]='a';
  493.   tnfsPacket.data[14]='t';
  494.   tnfsPacket.data[15]='r';
  495.   tnfsPacket.data[16]=0x00; // NUL terminated
  496.   tnfsPacket.data[17]=0x00; // no username
  497.   tnfsPacket.data[18]=0x00; // no password
  498.  
  499. #ifdef DEBUG_S
  500.   Serial1.println("Opening /autorun.atr...");
  501.   Serial1.print("Req packet: ");
  502.   for (int i=0;i<23;i++)
  503.   {
  504.     Serial1.print(tnfsPacket.rawData[i], HEX);
  505.     Serial1.print(" ");
  506.   }
  507.   Serial1.println("");
  508. #endif /* DEBUG_S */
  509.  
  510.   UDP.beginPacket(TNFS_SERVER,TNFS_PORT);
  511.   UDP.write(tnfsPacket.rawData,19+4);
  512.   UDP.endPacket();
  513.  
  514.   while (dur<5000)
  515.   {
  516.     if (UDP.parsePacket())
  517.     {
  518.       int l=UDP.read(tnfsPacket.rawData,512);
  519. #ifdef DEBUG_S
  520.       Serial1.print("Resp packet: ");
  521.       for (int i=0;i<l;i++)
  522.       {
  523.         Serial1.print(tnfsPacket.rawData[i], HEX);
  524.         Serial1.print(" ");
  525.       }
  526.       Serial1.println("");
  527. #endif DEBUG_S
  528.       if (tnfsPacket.data[0]==0x00)
  529.       {
  530.         // Successful
  531.         tnfs_fd=tnfsPacket.data[1];
  532. #ifdef DEBUG_S
  533.         Serial1.print("Successful, file descriptor: #");
  534.         Serial1.println(tnfs_fd, HEX);
  535. #endif /* DEBUG_S */
  536.         return;
  537.       }
  538.       else
  539.       {
  540.         // unsuccessful
  541. #ifdef DEBUG_S
  542.         Serial1.print("Error code #");
  543.         Serial1.println(tnfsPacket.data[0], HEX);
  544. #endif /* DEBUG_S*/
  545.         return;  
  546.       }
  547.     }
  548.   }
  549.   // Otherwise, we timed out.
  550. #ifdef DEBUG_S
  551.   Serial1.println("Timeout after 5000ms.");
  552. #endif /* DEBUG_S */
  553. }
  554.  
  555. /**
  556.  * TNFS close
  557.  */
  558. void tnfs_close()
  559. {
  560.   int start=millis();
  561.   int dur=millis()-start;
  562.   tnfsPacket.retryCount++;  // Increase sequence
  563.   tnfsPacket.command=0x23;  // CLOSE
  564.   tnfsPacket.data[0]=tnfs_fd; // returned file descriptor
  565.  
  566. #ifdef DEBUG_S
  567.   Serial1.print("closing File descriptor: ");
  568.   Serial1.println(tnfs_fd);
  569.   Serial1.print("Req Packet: ");
  570.   for (int i=0;i<7;i++)
  571.   {
  572.     Serial1.print(tnfsPacket.rawData[i], HEX);
  573.     Serial1.print(" ");
  574.   }
  575.   Serial1.println("");
  576. #endif /* DEBUG_S */
  577.  
  578.   UDP.beginPacket(TNFS_SERVER,TNFS_PORT);
  579.   UDP.write(tnfsPacket.rawData,4+1);
  580.   UDP.endPacket();
  581.  
  582.   while (dur<5000)
  583.   {
  584.     if (UDP.parsePacket())
  585.     {
  586.       int l=UDP.read(tnfsPacket.rawData,sizeof(tnfsPacket.rawData));
  587. #ifdef DEBUG_S
  588.       Serial1.print("Resp packet: ");
  589.       for (int i=0;i<l;i++)
  590.       {
  591.         Serial1.print(tnfsPacket.rawData[i], HEX);
  592.         Serial1.print(" ");
  593.       }
  594.       Serial1.println("");
  595. #endif /* DEBUG_S */
  596.       if (tnfsPacket.data[0]==0x00)
  597.       {
  598.         // Successful
  599. #ifndef DEBUG_S
  600.         Serial1.println("Successful.");
  601. #endif /* DEBUG_S */
  602.         return;
  603.       }
  604.       else
  605.       {
  606.         // Error
  607. #ifdef DEBUG_S
  608.         Serial1.print("Error code #");
  609.         Serial1.println(tnfsPacket.data[0], HEX);
  610. #endif /* DEBUG_S*/        
  611.         return;
  612.       }
  613.     }
  614.   }
  615. #ifdef DEBUG_S
  616.   Serial1.println("Timeout after 5000ms.");
  617. #endif /* DEBUG_S */
  618. }
  619.  
  620. /**
  621.  * TNFS write
  622.  */
  623. void tnfs_write()
  624. {
  625.   int start=millis();
  626.   int dur=millis()-start;
  627.   tnfsPacket.retryCount++;  // Increase sequence
  628.   tnfsPacket.command=0x22;  // READ
  629.   tnfsPacket.data[0]=tnfs_fd; // returned file descriptor
  630.   tnfsPacket.data[1]=0x80;  // 128 bytes
  631.   tnfsPacket.data[2]=0x00;  //
  632.  
  633. #ifdef DEBUG_S
  634.   Serial1.print("Writing to File descriptor: ");
  635.   Serial1.println(tnfs_fd);
  636.   Serial1.print("Req Packet: ");
  637.   for (int i=0;i<7;i++)
  638.   {
  639.     Serial1.print(tnfsPacket.rawData[i], HEX);
  640.     Serial1.print(" ");
  641.   }
  642.   Serial1.println("");
  643. #endif /* DEBUG_S */
  644.  
  645.   UDP.beginPacket(TNFS_SERVER,TNFS_PORT);
  646.   UDP.write(tnfsPacket.rawData,4+3);
  647.   UDP.write(sector,128);
  648.   UDP.endPacket();
  649.  
  650.   while (dur<5000)
  651.   {
  652.     if (UDP.parsePacket())
  653.     {
  654.       int l=UDP.read(tnfsPacket.rawData,sizeof(tnfsPacket.rawData));
  655. #ifdef DEBUG_S
  656.       Serial1.print("Resp packet: ");
  657.       for (int i=0;i<l;i++)
  658.       {
  659.         Serial1.print(tnfsPacket.rawData[i], HEX);
  660.         Serial1.print(" ");
  661.       }
  662.       Serial1.println("");
  663. #endif /* DEBUG_S */
  664.       if (tnfsPacket.data[0]==0x00)
  665.       {
  666.         // Successful
  667. #ifndef DEBUG_S
  668.         Serial1.println("Successful.");
  669. #endif /* DEBUG_S */
  670.         return;
  671.       }
  672.       else
  673.       {
  674.         // Error
  675. #ifdef DEBUG_S
  676.         Serial1.print("Error code #");
  677.         Serial1.println(tnfsPacket.data[0], HEX);
  678. #endif /* DEBUG_S*/        
  679.         return;
  680.       }
  681.     }
  682.   }
  683. #ifdef DEBUG_S
  684.   Serial1.println("Timeout after 5000ms.");
  685. #endif /* DEBUG_S */
  686. }
  687.  
  688. /**
  689.  * TNFS read
  690.  */
  691. void tnfs_read()
  692. {
  693.   int start=millis();
  694.   int dur=millis()-start;
  695.   tnfsPacket.retryCount++;  // Increase sequence
  696.   tnfsPacket.command=0x21;  // READ
  697.   tnfsPacket.data[0]=tnfs_fd; // returned file descriptor
  698.   tnfsPacket.data[1]=0x80;  // 128 bytes
  699.   tnfsPacket.data[2]=0x00;  //
  700.  
  701. #ifdef DEBUG_S
  702.   Serial1.print("Reading from File descriptor: ");
  703.   Serial1.println(tnfs_fd);
  704.   Serial1.print("Req Packet: ");
  705.   for (int i=0;i<7;i++)
  706.   {
  707.     Serial1.print(tnfsPacket.rawData[i], HEX);
  708.     Serial1.print(" ");
  709.   }
  710.   Serial1.println("");
  711. #endif /* DEBUG_S */
  712.  
  713.   UDP.beginPacket(TNFS_SERVER,TNFS_PORT);
  714.   UDP.write(tnfsPacket.rawData,4+3);
  715.   UDP.endPacket();
  716.  
  717.   while (dur<5000)
  718.   {
  719.     if (UDP.parsePacket())
  720.     {
  721.       int l=UDP.read(tnfsPacket.rawData,sizeof(tnfsPacket.rawData));
  722. #ifdef DEBUG_S
  723.       Serial1.print("Resp packet: ");
  724.       for (int i=0;i<l;i++)
  725.       {
  726.         Serial1.print(tnfsPacket.rawData[i], HEX);
  727.         Serial1.print(" ");
  728.       }
  729.       Serial1.println("");
  730. #endif /* DEBUG_S */
  731.       if (tnfsPacket.data[0]==0x00)
  732.       {
  733.         // Successful
  734. #ifndef DEBUG_S
  735.         Serial1.println("Successful.");
  736. #endif /* DEBUG_S */
  737.         return;
  738.       }
  739.       else
  740.       {
  741.         // Error
  742. #ifdef DEBUG_S
  743.         Serial1.print("Error code #");
  744.         Serial1.println(tnfsPacket.data[0], HEX);
  745. #endif /* DEBUG_S*/        
  746.         return;
  747.       }
  748.     }
  749.   }
  750. #ifdef DEBUG_S
  751.   Serial1.println("Timeout after 5000ms.");
  752. #endif /* DEBUG_S */
  753. }
  754.  
  755. /**
  756.  * TNFS seek
  757.  */
  758. void tnfs_seek(long offset)
  759. {
  760.   int start=millis();
  761.   int dur=millis()-start;
  762.   byte offsetVal[4];
  763.  
  764.   // This may be sending the bytes in the wrong endian, pls check. Easiest way is to flip the indices.
  765.   offsetVal[0] = (int)((offset & 0xFF000000) >> 24 );
  766.   offsetVal[1] = (int)((offset & 0x00FF0000) >> 16 );
  767.   offsetVal[2] = (int)((offset & 0x0000FF00) >> 8 );
  768.   offsetVal[3] = (int)((offset & 0X000000FF));  
  769.  
  770.   tnfsPacket.retryCount++;
  771.   tnfsPacket.command=0x25; // LSEEK
  772.   tnfsPacket.data[0]=tnfs_fd;
  773.   tnfsPacket.data[1]=0x00; // SEEK_SET
  774.   tnfsPacket.data[2]=offsetVal[3];
  775.   tnfsPacket.data[3]=offsetVal[2];
  776.   tnfsPacket.data[4]=offsetVal[1];
  777.   tnfsPacket.data[5]=offsetVal[0];
  778.  
  779. #ifdef DEBUG_S
  780.   Serial1.print("Seek requested to offset: ");
  781.   Serial1.println(offset);
  782.   Serial1.print("Req packet: ");
  783.   for (int i=0;i<10;i++)
  784.   {
  785.     Serial1.print(tnfsPacket.rawData[i], HEX);
  786.     Serial1.print(" ");
  787.   }
  788.   Serial1.println("");
  789. #endif /* DEBUG_S*/
  790.  
  791.   UDP.beginPacket(TNFS_SERVER,TNFS_PORT);
  792.   UDP.write(tnfsPacket.rawData,6+4);
  793.   UDP.endPacket();
  794.  
  795.   while (dur<5000)
  796.   {
  797.     if (UDP.parsePacket())
  798.     {
  799.       int l=UDP.read(tnfsPacket.rawData,sizeof(tnfsPacket.rawData));
  800. #ifdef DEBUG_S
  801.       Serial1.print("Resp packet: ");
  802.       for (int i=0;i<l;i++)
  803.       {
  804.         Serial1.print(tnfsPacket.rawData[i], HEX);
  805.         Serial1.print(" ");
  806.       }
  807.       Serial1.println("");
  808. #endif /* DEBUG_S */
  809.  
  810.       if (tnfsPacket.data[0]==0)
  811.       {
  812.         // Success.
  813. #ifdef DEBUG_S
  814.         Serial1.println("Successful.");
  815. #endif /* DEBUG_S */
  816.         return;  
  817.       }
  818.       else
  819.       {
  820.         // Error.
  821. #ifdef DEBUG_S
  822.         Serial1.print("Error code #");
  823.         Serial1.println(tnfsPacket.data[0], HEX);
  824. #endif /* DEBUG_S*/        
  825.         return;  
  826.       }
  827.     }
  828.   }
  829. #ifdef DEBUG_S
  830.   Serial1.println("Timeout after 5000ms.");
  831. #endif /* DEBUG_S */
  832. }
  833.  
  834. void setup()
  835. {
  836.   // Set up pins
  837. #ifdef DEBUG_S
  838.   Serial1.begin(19200);
  839.   Serial1.println();
  840.   Serial1.println("#AtariWifi Test Program #10 started");
  841. #else
  842.   pinMode(PIN_LED, OUTPUT);
  843.   digitalWrite(PIN_LED, HIGH);
  844. #endif
  845.   pinMode(PIN_INT, INPUT);
  846.   pinMode(PIN_PROC, INPUT);
  847.   pinMode(PIN_MTR, INPUT);
  848.   pinMode(PIN_CMD, INPUT);
  849.  
  850.   WiFi.begin("SSID", "PASSWORD");
  851.   while (WiFi.status() != WL_CONNECTED)
  852.   {
  853.     delay(10);
  854.   }
  855.  
  856.   UDP.begin(16384);
  857.  
  858.   tnfs_mount();
  859.   tnfs_open();
  860.  
  861.   // Set up serial
  862.   Serial.begin(19200);
  863.   Serial.swap();
  864.  
  865.   // Attach COMMAND interrupt.
  866.   attachInterrupt(digitalPinToInterrupt(PIN_CMD), sio_isr_cmd, FALLING);
  867.   cmdState = WAIT; // Start in wait state
  868. }
  869.  
  870. void loop()
  871. {
  872.   if (cmdFlag)
  873.   {
  874.     if (digitalRead(PIN_CMD) == LOW)
  875.     {
  876.       cmdState = ID;
  877.       cmdTimer = millis();
  878.       cmdFlag=false;
  879.     }
  880.   }
  881.  
  882.   if (Serial.available() > 0)
  883.   {
  884.     sio_incoming();
  885.   }
  886.  
  887.   if (millis() - cmdTimer > CMD_TIMEOUT && cmdState != WAIT)
  888.   {
  889.     Serial1.print("SIO CMD TIMEOUT: ");
  890.     Serial1.println(cmdState);
  891.     cmdState = WAIT;
  892.     cmdTimer = 0;
  893.   }
  894. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top