├── ArduinoFDC.cpp ├── ArduinoFDC.h ├── ArduinoFDC.ino ├── ArduinoFDCMegaShieldGerber.zip ├── ArduinoFDCMegaShieldSchematic.pdf ├── ArduinoFDCShieldGerber.zip ├── ArduinoFDCShieldSchematic.pdf ├── LICENSE ├── README.md ├── XModem.cpp ├── XModem.h ├── diskio.cpp ├── diskio.h ├── ff.c ├── ff.h ├── ffconf.h └── images ├── ArduDOS.png ├── CutTrace.jpg ├── ShieldAssembled.jpg ├── Shields.jpg └── setup.jpg /ArduinoFDC.h: -------------------------------------------------------------------------------- 1 | // ----------------------------------------------------------------------------- 2 | // 3.5"/5.25" DD/HD Disk controller for Arduino 3 | // Copyright (C) 2021 David Hansel 4 | // 5 | // This program is free software; you can redistribute it and/or modify 6 | // it under the terms of the GNU General Public License as published by 7 | // the Free Software Foundation; either version 3 of the License, or 8 | // (at your option) any later version. 9 | // 10 | // This program is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | // GNU General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU General Public License 16 | // along with this program; if not, write to the Free Software Foundation, 17 | // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | // ----------------------------------------------------------------------------- 19 | 20 | #ifndef ARDUINOFDC_H 21 | #define ARDUINOFDC_H 22 | 23 | #include "Arduino.h" 24 | 25 | // return status for readSector/writeSector and formatDisk functions 26 | #define S_OK 0 // no error 27 | #define S_NOTINIT 1 // ArduinoFDC.begin() was not called 28 | #define S_NOTREADY 2 // Drive is not ready (no disk or power) 29 | #define S_NOSYNC 3 // No sync marks found 30 | #define S_NOHEADER 4 // Sector header not found 31 | #define S_INVALIDID 5 // Sector data record has invalid id 32 | #define S_CRC 6 // Sector data checksum error 33 | #define S_NOTRACK0 7 // No track0 signal 34 | #define S_VERIFY 8 // Verify after write failed 35 | #define S_READONLY 9 // Attempt to write to a write-protected disk 36 | 37 | class ArduinoFDCClass 38 | { 39 | public: 40 | 41 | enum DriveType { 42 | DT_5_DD, // 5.25" double density (360 KB) 43 | DT_5_DDonHD, // 5.25" double density disk in high density drive (360 KB) 44 | DT_5_HD, // 5.25" high density (1.2 MB) 45 | DT_3_DD, // 3.5" double density (720 KB) 46 | DT_3_HD // 3.5" high density (1.44 MB) 47 | }; 48 | 49 | enum DensityPinMode { 50 | DP_DISCONNECT = 0, // density pin disconnected (set to INPUT mode) 51 | DP_OUTPUT_LOW_FOR_HD, // density pin goes LOW for high density disk 52 | DP_OUTPUT_LOW_FOR_DD // density pin goes LOW for double density disk 53 | }; 54 | 55 | ArduinoFDCClass(); 56 | 57 | // Initialize pins used for controlling the disk drive 58 | void begin(enum DriveType driveAType = DT_3_HD, enum DriveType driveBType = DT_3_HD); 59 | 60 | // Release pins used for controlling the disk drive 61 | void end(); 62 | 63 | // Select drive A(0) or B(1). 64 | bool selectDrive(byte drive); 65 | 66 | // Returns which drive is currently selected, A (0) or B (1) 67 | byte selectedDrive() const; 68 | 69 | // set the drive type for the currently selected drive 70 | void setDriveType(enum DriveType type); 71 | void setDriveType(byte drive, enum DriveType type); 72 | 73 | // get the type of the currently selected drive 74 | enum DriveType getDriveType() const; 75 | enum DriveType getDriveType(byte drive) const; 76 | 77 | // returns true if a disk is detected in the drive 78 | bool haveDisk() const; 79 | 80 | // returns true if a disk change was detected since the last call to diskChanged() 81 | bool diskChanged() const; 82 | 83 | // returns true if the disk is write protected 84 | bool isWriteProtected() const; 85 | 86 | // get number of heads for the currently selected drive (1 or 2) 87 | byte numHeads() const; 88 | 89 | // get number of tracks for the currently selected drive 90 | byte numTracks() const; 91 | 92 | // get number of sectors for the currently selected drive 93 | byte numSectors() const; 94 | 95 | // set the density pin mode for the currently selected drive 96 | void setDensityPinMode(enum DensityPinMode mode); 97 | 98 | // Read a sector from disk, 99 | // buffer MUST have a size of at least 516 bytes. 100 | // IMPORTANT: On successful return, the 512 bytes of sector data 101 | // read will be in buffer[1..512] (NOT: 0..511!) 102 | // See error codes above for possible return values 103 | byte readSector(byte track, byte side, byte sector, byte *buffer); 104 | 105 | // Write a sector to disk, 106 | // buffer MUST have a size of at least 516 bytes. 107 | // IMPORTANT: The 512 bytes of sector data to be written 108 | // must be in buffer[1..512] (NOT: 0..511!) 109 | // if "verify" is true then the data will be re-read after writing 110 | // and compared to the data just written. 111 | // See error codes above for possible return values 112 | byte writeSector(byte track, byte side, byte sector, byte *buffer, bool verify); 113 | 114 | // Formats a disk 115 | // buffer is needed to store temporary data while formatting and MUST have 116 | // a size of at least 144 bytes. 117 | // All sector data is initialized with 0xF6. 118 | // See error codes above for possible return values 119 | // IMPORTANT: No DOS file system is initialized, i.e. DOS or Windows 120 | // will NOT recognize this as a valid disk 121 | byte formatDisk(byte *buffer, byte fromTrack=0, byte toTrack=255, byte interleave=1); 122 | 123 | // Turn the disk drive motor on. The readSector/writeSector/formatDisk 124 | // functions will turn on the motor automatically if it is not running 125 | // yet. In that case (and ONLY then) they will also turn it off when finished. 126 | void motorOn(); 127 | 128 | // Turn the disk drive motor off 129 | void motorOff(); 130 | 131 | // Returns true if the disk drive motor is currently running 132 | bool motorRunning() const; 133 | 134 | private: 135 | void driveSelect(bool state) const; 136 | void setDensityPin(); 137 | byte getBitLength(); 138 | 139 | enum DriveType m_driveType[2]; 140 | enum DensityPinMode m_densityPinMode[2]; 141 | byte m_currentDrive, m_bitLength[2]; 142 | bool m_initialized, m_motorState[2]; 143 | }; 144 | 145 | 146 | extern ArduinoFDCClass ArduinoFDC; 147 | 148 | #endif 149 | -------------------------------------------------------------------------------- /ArduinoFDC.ino: -------------------------------------------------------------------------------- 1 | // ----------------------------------------------------------------------------- 2 | // 3.5"/5.25" DD/HD Disk controller for Arduino 3 | // Copyright (C) 2021 David Hansel 4 | // 5 | // This program is free software; you can redistribute it and/or modify 6 | // it under the terms of the GNU General Public License as published by 7 | // the Free Software Foundation; either version 3 of the License, or 8 | // (at your option) any later version. 9 | // 10 | // This program is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | // GNU General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU General Public License 16 | // along with this program; if not, write to the Free Software Foundation, 17 | // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | // ----------------------------------------------------------------------------- 19 | 20 | #include "ArduinoFDC.h" 21 | #include "ff.h" 22 | 23 | 24 | // comment this out to remove high-level ArduDOS functions 25 | #define USE_ARDUDOS 26 | 27 | // commenting this out will remove the low-level disk monitor 28 | #define USE_MONITOR 29 | 30 | // comenting this out will remove support for XModem data transfers 31 | //#define USE_XMODEM 32 | 33 | 34 | #if defined(__AVR_ATmega32U4__) && defined(USE_ARDUDOS) && (defined(USE_MONITOR) || defined(USE_XMODEM)) 35 | #warning "Arduino Leonardo/Micro program memory is too small to support ArduDOS together with other components!" 36 | #undef USE_MONITOR 37 | #undef USE_XMODEM 38 | #endif 39 | 40 | // ------------------------------------------------------------------------------------------------- 41 | // Basic helper functions 42 | // ------------------------------------------------------------------------------------------------- 43 | 44 | #define TEMPBUFFER_SIZE 80 45 | byte tempbuffer[TEMPBUFFER_SIZE]; 46 | 47 | unsigned long motor_timeout = 0; 48 | 49 | 50 | void print_hex(byte b) 51 | { 52 | if( b<16 ) Serial.write('0'); 53 | Serial.print(b, HEX); 54 | } 55 | 56 | 57 | void dump_buffer(int offset, byte *buf, int n) 58 | { 59 | int i = 0; 60 | while( i0 ) 101 | { Serial.write(8); Serial.write(' '); Serial.write(8); l--; } 102 | } 103 | else if( isprint(i) && l0 && millis() > motor_timeout ) { ArduinoFDC.motorOff(); motor_timeout = 0; } 107 | } 108 | while(true); 109 | 110 | while( l>0 && isspace(buf[l-1]) ) l--; 111 | buf[l] = 0; 112 | return buf; 113 | } 114 | 115 | 116 | bool confirm_formatting() 117 | { 118 | int c; 119 | Serial.print(F("Formatting will erase all data on the disk in drive ")); 120 | Serial.write('A' + ArduinoFDC.selectedDrive()); 121 | Serial.print(F(". Continue (y/n)?")); 122 | while( (c=Serial.read())<0 ); 123 | do { delay(1); } while( Serial.read()>=0 ); 124 | Serial.println(); 125 | return c=='y'; 126 | } 127 | 128 | 129 | void print_drive_type(byte n) 130 | { 131 | switch( n ) 132 | { 133 | case ArduinoFDCClass::DT_5_DD: Serial.print(F("5.25\" DD")); break; 134 | case ArduinoFDCClass::DT_5_DDonHD: Serial.print(F("5.25\" DD disk in HD drive")); break; 135 | case ArduinoFDCClass::DT_5_HD: Serial.print(F("5.25\" HD")); break; 136 | case ArduinoFDCClass::DT_3_DD: Serial.print(F("3.5\" DD")); break; 137 | case ArduinoFDCClass::DT_3_HD: Serial.print(F("3.5\" HD")); break; 138 | default: Serial.print(F("Unknown")); 139 | } 140 | } 141 | 142 | 143 | void print_error(byte n) 144 | { 145 | Serial.print(F("Error: ")); 146 | switch( n ) 147 | { 148 | case S_OK : Serial.print(F("No error")); break; 149 | case S_NOTINIT : Serial.print(F("ArduinoFDC.begin() was not called")); break; 150 | case S_NOTREADY : Serial.print(F("Drive not ready")); break; 151 | case S_NOSYNC : Serial.print(F("No sync marks found")); break; 152 | case S_NOHEADER : Serial.print(F("Sector header not found")); break; 153 | case S_INVALIDID : Serial.print(F("Data record has unexpected id")); break; 154 | case S_CRC : Serial.print(F("Data checksum error")); break; 155 | case S_NOTRACK0 : Serial.print(F("No track 0 signal detected")); break; 156 | case S_VERIFY : Serial.print(F("Verify after write failed")); break; 157 | case S_READONLY : Serial.print(F("Disk is write protected")); break; 158 | default : Serial.print(F("Unknonwn error")); break; 159 | } 160 | Serial.println('!'); 161 | } 162 | 163 | 164 | void set_drive_type(int n) 165 | { 166 | ArduinoFDC.setDriveType((ArduinoFDCClass::DriveType) n); 167 | Serial.print(F("Setting disk type for drive ")); Serial.write('A'+ArduinoFDC.selectedDrive()); 168 | Serial.print(F(" to ")); print_drive_type(ArduinoFDC.getDriveType()); 169 | Serial.println(); 170 | } 171 | 172 | 173 | // ------------------------------------------------------------------------------------------------- 174 | // XModem data transfer functions 175 | // ------------------------------------------------------------------------------------------------- 176 | 177 | 178 | int recvChar(int msDelay) 179 | { 180 | unsigned long start = millis(); 181 | while( (int) (millis()-start) < msDelay) 182 | { 183 | if( Serial.available() ) return (uint8_t) Serial.read(); 184 | } 185 | 186 | return -1; 187 | } 188 | 189 | 190 | void sendData(const char *data, int size) 191 | { 192 | Serial.write((const uint8_t *) data, size); 193 | } 194 | 195 | 196 | // ------------------------------------------------------------------------------------------------- 197 | // High-level ArduDOS 198 | // ------------------------------------------------------------------------------------------------- 199 | 200 | 201 | #ifdef USE_ARDUDOS 202 | 203 | static FATFS FatFs; 204 | static FIL FatFsFile; 205 | 206 | #ifdef USE_XMODEM 207 | 208 | #include "XModem.h" 209 | FRESULT xmodem_status = FR_OK; 210 | 211 | bool xmodemHandlerSend(unsigned long no, char* data, int size) 212 | { 213 | UINT br; 214 | xmodem_status = f_read(&FatFsFile, data, size, &br); 215 | 216 | // if there is an error or there is nothing more to read then return 217 | if( xmodem_status != FR_OK || br==0 ) return false; 218 | 219 | // XMODEM sends blocks of 128 bytes so if we have less than that 220 | // fill the rest of the buffer with EOF (ASCII 26) characters 221 | while( (int) br")); 285 | char *cmd = read_user_cmd(tempbuffer, TEMPBUFFER_SIZE); 286 | 287 | if( ArduinoFDC.diskChanged() ) 288 | { 289 | Serial.println("Disk change detected!"); 290 | f_mount(&FatFs, "0:", 0); 291 | } 292 | 293 | if( strcmp_PF(cmd, PSTR("a:"))==0 || strcmp_PF(cmd, PSTR("b:"))==0 ) 294 | { 295 | byte drive = cmd[0]-'a'; 296 | if( drive != ArduinoFDC.selectedDrive() ) 297 | { 298 | ArduinoFDC.motorOff(); 299 | motor_timeout = 0; 300 | ArduinoFDC.selectDrive(drive); 301 | } 302 | 303 | f_mount(&FatFs, "0:", 0); 304 | } 305 | else if( strncmp_P(cmd, PSTR("dir"), 3)==0 ) 306 | { 307 | DIR dir; 308 | FILINFO finfo; 309 | 310 | ArduinoFDC.motorOn(); 311 | fr = f_opendir(&dir, strlen(cmd)<5 ? "0:\\" : cmd+4); 312 | if (fr == FR_OK) 313 | { 314 | count = 0; 315 | while(1) 316 | { 317 | fr = f_readdir(&dir, &finfo); 318 | if( fr!=FR_OK || finfo.fname[0]==0 ) 319 | break; 320 | 321 | char *c = finfo.fname; 322 | byte col = 0; 323 | while( *c!=0 && *c!='.' ) { Serial.write(toupper(*c)); col++; c++; } 324 | while( col<9 ) { Serial.write(' '); col++; } 325 | if( *c=='.' ) 326 | { 327 | c++; 328 | while( *c!=0 ) { Serial.write(toupper(*c)); col++; c++; } 329 | } 330 | while( col<14 ) { Serial.write(' '); col++; } 331 | if( finfo.fattrib & AM_DIR ) 332 | Serial.println(F("")); 333 | else 334 | Serial.println(finfo.fsize); 335 | count++; 336 | } 337 | 338 | f_closedir(&dir); 339 | 340 | if( fr==FR_OK ) 341 | { 342 | if( count==0 ) Serial.println(F("No files.")); 343 | 344 | FATFS *fs; 345 | DWORD fre_clust; 346 | fr = f_getfree("0:", &fre_clust, &fs); 347 | 348 | if( fr==FR_OK ) 349 | { Serial.print(fre_clust * fs->csize * 512); Serial.println(F(" bytes free.")); } 350 | } 351 | 352 | if( fr!=FR_OK ) 353 | print_ff_error(fr); 354 | } 355 | else 356 | print_ff_error(fr); 357 | } 358 | else if( strncmp_P(cmd, PSTR("type "), 5)==0 ) 359 | { 360 | ArduinoFDC.motorOn(); 361 | fr = f_open(&FatFsFile, cmd+5, FA_READ); 362 | if( fr == FR_OK ) 363 | { 364 | count = 1; 365 | while( count>0 ) 366 | { 367 | fr = f_read(&FatFsFile, tempbuffer, TEMPBUFFER_SIZE, &count); 368 | if( fr == FR_OK ) 369 | Serial.write(tempbuffer, count); 370 | else 371 | print_ff_error(fr); 372 | } 373 | f_close(&FatFsFile); 374 | } 375 | else 376 | print_ff_error(fr); 377 | } 378 | else if( strncmp_P(cmd, PSTR("dump "), 5)==0 ) 379 | { 380 | ArduinoFDC.motorOn(); 381 | fr = f_open(&FatFsFile, cmd+5, FA_READ); 382 | if( fr == FR_OK ) 383 | { 384 | count = 1; 385 | int offset = 0; 386 | while( count>0 ) 387 | { 388 | fr = f_read(&FatFsFile, tempbuffer, (TEMPBUFFER_SIZE/16)*16, &count); 389 | if( fr == FR_OK ) 390 | { dump_buffer(offset, tempbuffer, count); offset += count; } 391 | else 392 | print_ff_error(fr); 393 | } 394 | f_close(&FatFsFile); 395 | } 396 | else 397 | print_ff_error(fr); 398 | } 399 | else if( strncmp_P(cmd, PSTR("write "), 6)==0 ) 400 | { 401 | ArduinoFDC.motorOn(); 402 | fr = f_open(&FatFsFile, cmd+6, FA_WRITE | FA_CREATE_NEW); 403 | if( fr == FR_OK ) 404 | { 405 | motor_timeout = 0; 406 | while( true ) 407 | { 408 | char *s = read_user_cmd(tempbuffer, TEMPBUFFER_SIZE); 409 | if( s[0] ) 410 | { 411 | fr = f_write(&FatFsFile, s, strlen(cmd), &count); 412 | if( fr==FR_OK ) fr = f_write(&FatFsFile, "\r\n", 2, &count); 413 | if( fr!=FR_OK ) print_ff_error(fr); 414 | } 415 | else 416 | break; 417 | } 418 | 419 | f_close(&FatFsFile); 420 | } 421 | else 422 | print_ff_error(fr); 423 | } 424 | else if( strncmp_P(cmd, PSTR("del "), 4)==0 ) 425 | { 426 | ArduinoFDC.motorOn(); 427 | fr = f_unlink(cmd+4); 428 | if( fr != FR_OK ) 429 | print_ff_error(fr); 430 | } 431 | else if( strncmp_P(cmd, PSTR("mkdir "), 6)==0 ) 432 | { 433 | ArduinoFDC.motorOn(); 434 | fr = f_mkdir(cmd+6); 435 | if( fr != FR_OK ) 436 | print_ff_error(fr); 437 | } 438 | else if( strncmp_P(cmd, PSTR("rmdir "), 6)==0 ) 439 | { 440 | ArduinoFDC.motorOn(); 441 | fr = f_rmdir(cmd+6); 442 | if( fr != FR_OK ) 443 | print_ff_error(fr); 444 | } 445 | else if( strncmp_P(cmd, PSTR("disktype "), 9)==0 ) 446 | { 447 | set_drive_type(atoi(cmd+9)); 448 | f_mount(&FatFs, "0:", 0); 449 | } 450 | else if( strncmp_P(cmd, PSTR("format"), 6)==0 ) 451 | { 452 | MKFS_PARM param; 453 | param.fmt = FM_FAT | FM_SFD; // FAT12 type, no disk partitioning 454 | param.n_fat = 2; // number of FATs 455 | param.n_heads = 2; // number of heads 456 | param.n_sec_track = ArduinoFDC.numSectors(); 457 | param.align = 1; // block alignment (not used for FAT12) 458 | 459 | switch( ArduinoFDC.getDriveType() ) 460 | { 461 | case ArduinoFDCClass::DT_5_DD: 462 | case ArduinoFDCClass::DT_5_DDonHD: 463 | param.au_size = 1024; // bytes/cluster 464 | param.n_root = 112; // number of root directory entries 465 | param.media = 0xFD; // media descriptor 466 | break; 467 | 468 | case ArduinoFDCClass::DT_5_HD: 469 | param.au_size = 512; // bytes/cluster 470 | param.n_root = 224; // number of root directory entries 471 | param.media = 0xF9; // media descriptor 472 | break; 473 | 474 | case ArduinoFDCClass::DT_3_DD: 475 | param.au_size = 1024; // bytes/cluster 476 | param.n_root = 112; // number of root directory entries 477 | param.media = 0xF9; // media descriptor 478 | break; 479 | 480 | case ArduinoFDCClass::DT_3_HD: 481 | param.au_size = 512; // bytes/cluster 482 | param.n_root = 224; // number of root directory entries 483 | param.media = 0xF0; // media descriptor 484 | break; 485 | } 486 | 487 | if( confirm_formatting() ) 488 | { 489 | byte st; 490 | ArduinoFDC.motorOn(); 491 | f_unmount("0:"); 492 | if( strstr(cmd, "/q") || (st=ArduinoFDC.formatDisk(FatFs.win))==S_OK ) 493 | { 494 | Serial.println(F("Initializing file system...\n")); 495 | FRESULT fr = f_mkfs ("0:", ¶m, FatFs.win, 512); 496 | if( fr != FR_OK ) print_ff_error(fr); 497 | } 498 | else 499 | print_error(st); 500 | 501 | f_mount(&FatFs, "0:", 0); 502 | } 503 | } 504 | #ifdef USE_MONITOR 505 | else if( strncmp_P(cmd, PSTR("monitor"), max(3, strlen(cmd)))==0 ) 506 | { 507 | motor_timeout = 0; 508 | monitor(); 509 | f_mount(&FatFs, "0:", 0); 510 | } 511 | #endif 512 | #ifdef USE_XMODEM 513 | else if( strncmp_P(cmd, PSTR("receive "), 8)==0 ) 514 | { 515 | ArduinoFDC.motorOn(); 516 | fr = f_open(&FatFsFile, cmd+8, FA_WRITE | FA_CREATE_NEW); 517 | if( fr == FR_OK ) 518 | { 519 | Serial.println(F("Send file via XModem now...")); 520 | 521 | XModem modem(recvChar, sendData, xmodemHandlerReceive); 522 | xmodem_status = FR_OK; 523 | modem.receive(); 524 | 525 | if( xmodem_status == FR_OK ) 526 | Serial.println(F("\r\nSuccess!")); 527 | else 528 | { 529 | unsigned long t = millis() + 500; 530 | while( millis() < t ) { if( Serial.read()>=0 ) t = millis()+500; } 531 | while( Serial.read()<0 ); 532 | 533 | Serial.println('\r'); 534 | if( xmodem_status!=S_OK ) print_ff_error(xmodem_status); 535 | } 536 | 537 | f_close(&FatFsFile); 538 | } 539 | else 540 | print_ff_error(fr); 541 | } 542 | else if( strncmp_P(cmd, PSTR("send "), 5)==0 ) 543 | { 544 | ArduinoFDC.motorOn(); 545 | fr = f_open(&FatFsFile, cmd+5, FA_READ); 546 | if( fr == FR_OK ) 547 | { 548 | Serial.println(F("Receive file via XModem now...")); 549 | 550 | XModem modem(recvChar, sendData, xmodemHandlerSend); 551 | xmodem_status = FR_OK; 552 | modem.transmit(); 553 | 554 | if( xmodem_status == FR_OK ) 555 | Serial.println(F("\r\nSuccess!")); 556 | else 557 | { 558 | unsigned long t = millis() + 500; 559 | while( millis() < t ) { if( Serial.read()>=0 ) t = millis()+500; } 560 | while( Serial.read()<0 ); 561 | 562 | Serial.println('\r'); 563 | if( xmodem_status!=S_OK ) print_ff_error(xmodem_status); 564 | } 565 | 566 | f_close(&FatFsFile); 567 | } 568 | else 569 | print_ff_error(fr); 570 | } 571 | #endif 572 | #if !defined(USE_ARDUDOS) || !defined(USE_MONITOR) || !defined(USE_XMODEM) || defined(__AVR_ATmega2560__) 573 | // must save flash space if all three of ARDUDOS/MONITR/XMODEM are enabled on UNO 574 | else if( strcmp_P(cmd, PSTR("help"))==0 || strcmp_P(cmd, PSTR("h"))==0 || strcmp_P(cmd, PSTR("?"))==0 ) 575 | { 576 | Serial.print(F("Valid commands: dir, type, dump, write, del, mkdir, rmdir, disktype, format")); 577 | #ifdef USE_MONITOR 578 | Serial.print(F(", monitor")); 579 | #endif 580 | #ifdef USE_XMODEM 581 | Serial.print(F(", send, receive")); 582 | #endif 583 | Serial.println(); 584 | } 585 | #endif 586 | else if( cmd[0]!=0 ) 587 | { 588 | Serial.print(F("Unknown command: ")); 589 | Serial.print(cmd); 590 | } 591 | 592 | motor_timeout = millis() + 5000; 593 | } 594 | } 595 | 596 | #endif 597 | 598 | 599 | // ------------------------------------------------------------------------------------------------- 600 | // Low-level disk monitor 601 | // ------------------------------------------------------------------------------------------------- 602 | 603 | 604 | #ifdef USE_MONITOR 605 | 606 | // re-use the FatFs data buffer if ARDUDOS is enabled (to save RAM) 607 | #ifdef USE_ARDUDOS 608 | #define databuffer FatFs.win 609 | #else 610 | static byte databuffer[516]; 611 | #endif 612 | 613 | 614 | #ifdef USE_XMODEM 615 | 616 | #include "XModem.h" 617 | 618 | byte xmodem_status_mon = S_OK; 619 | bool xmodem_verify = false; 620 | word xmodem_sector = 0, xmodem_data_ptr = 0xFFFF; 621 | 622 | 623 | bool xmodemHandlerSendMon(unsigned long no, char* data, int size) 624 | { 625 | if( xmodem_data_ptr>=512 ) 626 | { 627 | byte numsec = ArduinoFDC.numSectors(); 628 | if( xmodem_sector >= 2*numsec*ArduinoFDC.numTracks() ) 629 | { xmodem_status_mon = S_OK; return false; } 630 | 631 | byte head = 0; 632 | byte track = xmodem_sector / (numsec*2); 633 | byte sector = xmodem_sector % (numsec*2); 634 | if( sector >= numsec ) { head = 1; sector -= numsec; } 635 | 636 | byte r = S_NOHEADER, retry = 5; 637 | while( retry>0 && r!=S_OK ) { r = ArduinoFDC.readSector(track, head, sector+1, databuffer); retry--; } 638 | if( r!=S_OK ) { xmodem_status_mon = r; return false; } 639 | 640 | xmodem_data_ptr = 0; 641 | xmodem_sector++; 642 | } 643 | 644 | // "size" is always 128 and sector length is 512, i.e. a multiple of "size" 645 | // readSector returns data in databuffer[1..512] 646 | memcpy(data, databuffer+1+xmodem_data_ptr, size); 647 | xmodem_data_ptr += size; 648 | return true; 649 | } 650 | 651 | 652 | bool xmodemHandlerReceiveMon(unsigned long no, char* data, int size) 653 | { 654 | // "size" is always 128 and sector length is 512, i.e. a multiple of "size" 655 | // writeSector expects data in databuffer[1..512] 656 | memcpy(databuffer+1+xmodem_data_ptr, data, size); 657 | xmodem_data_ptr += size; 658 | 659 | if( xmodem_data_ptr>=512 ) 660 | { 661 | byte numsec = ArduinoFDC.numSectors(); 662 | if( xmodem_sector >= 2*numsec*ArduinoFDC.numTracks() ) 663 | { xmodem_status_mon = S_OK; return false; } 664 | 665 | byte head = 0; 666 | byte track = xmodem_sector / (numsec*2); 667 | byte sector = xmodem_sector % (numsec*2); 668 | if( sector >= numsec ) { head = 1; sector -= numsec; } 669 | 670 | byte r = S_NOHEADER, retry = 5; 671 | while( retry>0 && r!=S_OK ) { r = ArduinoFDC.writeSector(track, head, sector+1, databuffer, xmodem_verify); retry--; } 672 | if( r!=S_OK ) { xmodem_status_mon = r; return false; } 673 | 674 | xmodem_data_ptr = 0; 675 | xmodem_sector++; 676 | } 677 | 678 | return true; 679 | } 680 | 681 | #endif 682 | 683 | 684 | void monitor() 685 | { 686 | char cmd; 687 | int a1, a2, a3, head, track, sector, n; 688 | 689 | ArduinoFDC.motorOn(); 690 | while( true ) 691 | { 692 | Serial.print(F("\r\n\r\nCommand: ")); 693 | n = sscanf(read_user_cmd(tempbuffer, 512), "%c%i,%i,%i", &cmd, &a1, &a2, &a3); 694 | if( n<=0 || isspace(cmd) ) continue; 695 | 696 | if( cmd=='r' && n>=3 ) 697 | { 698 | track=a1; sector=a2; head= (n==3) ? 0 : a3; 699 | if( head>=0 && head<2 && track>=0 && track=1 && sector<=ArduinoFDC.numSectors() ) 700 | { 701 | Serial.print(F("Reading track ")); Serial.print(track); 702 | Serial.print(F(" sector ")); Serial.print(sector); 703 | Serial.print(F(" side ")); Serial.println(head); 704 | Serial.flush(); 705 | 706 | byte status = ArduinoFDC.readSector(track, head, sector, databuffer); 707 | if( status==S_OK ) 708 | { 709 | dump_buffer(0, databuffer+1, 512); 710 | Serial.println(); 711 | } 712 | else 713 | print_error(status); 714 | } 715 | else 716 | Serial.println(F("Invalid sector specification")); 717 | } 718 | else if( cmd=='r' && n==1 ) 719 | { 720 | ArduinoFDC.motorOn(); 721 | for(track=0; track ok")); 738 | break; 739 | } 740 | else if( (status==S_INVALIDID || status==S_CRC) && (attempts++ < 10) ) 741 | Serial.println(F(" => CRC error, trying again")); 742 | else 743 | { 744 | Serial.print(F(" => ")); 745 | print_error(status); 746 | break; 747 | } 748 | } 749 | 750 | sector+=2; 751 | if( sector>ArduinoFDC.numSectors() ) sector = 2; 752 | } 753 | } 754 | } 755 | else if( cmd=='w' && n>=3 ) 756 | { 757 | track=a1; sector=a2; head= (n==3) ? 0 : a3; 758 | if( head>=0 && head<2 && track>=0 && track=1 && sector<=ArduinoFDC.numSectors() ) 759 | { 760 | Serial.print(F("Writing and verifying track ")); Serial.print(track); 761 | Serial.print(F(" sector ")); Serial.print(sector); 762 | Serial.print(F(" side ")); Serial.println(head); 763 | Serial.flush(); 764 | 765 | byte status = ArduinoFDC.writeSector(track, head, sector, databuffer, true); 766 | if( status==S_OK ) 767 | Serial.println(F("Ok.")); 768 | else 769 | print_error(status); 770 | } 771 | else 772 | Serial.println(F("Invalid sector specification")); 773 | } 774 | else if( cmd=='w' && n>=1 ) 775 | { 776 | bool verify = n>1 && a2>0; 777 | char c; 778 | Serial.print(F("Write current buffer to all sectors in drive ")); 779 | Serial.write('A' + ArduinoFDC.selectedDrive()); 780 | Serial.println(F(". Continue (y/n)?")); 781 | while( (c=Serial.read())<0 ); 782 | if( c=='y' ) 783 | { 784 | ArduinoFDC.motorOn(); 785 | for(track=0; track ok")); 798 | else 799 | { 800 | Serial.print(F(" => ")); 801 | print_error(status); 802 | } 803 | 804 | sector+=2; 805 | if( sector>ArduinoFDC.numSectors() ) sector = 2; 806 | } 807 | } 808 | } 809 | } 810 | else if( cmd=='b' ) 811 | { 812 | Serial.println(F("Buffer contents:")); 813 | dump_buffer(0, databuffer+1, 512); 814 | } 815 | else if( cmd=='B' ) 816 | { 817 | Serial.print(F("Filling buffer")); 818 | if( n==1 ) 819 | { 820 | for(int i=0; i<512; i++) databuffer[i+1] = i; 821 | } 822 | else 823 | { 824 | Serial.print(F(" with 0x")); 825 | Serial.print(a1, HEX); 826 | for(int i=0; i<512; i++) databuffer[i+1] = a1; 827 | } 828 | Serial.println(); 829 | } 830 | else if( cmd=='m' ) 831 | { 832 | if( n==1 ) 833 | { 834 | Serial.print(F("Drive ")); 835 | Serial.write('A' + ArduinoFDC.selectedDrive()); 836 | Serial.print(F(" motor is ")); 837 | Serial.println(ArduinoFDC.motorRunning() ? F("on") : F("off")); 838 | } 839 | else 840 | { 841 | Serial.print(F("Turning drive ")); 842 | Serial.write('A' + ArduinoFDC.selectedDrive()); 843 | Serial.print(F(" motor ")); 844 | if( n==1 || a1==0 ) 845 | { 846 | Serial.println(F("off")); 847 | ArduinoFDC.motorOff(); 848 | } 849 | else 850 | { 851 | Serial.println(F("on")); 852 | ArduinoFDC.motorOn(); 853 | } 854 | } 855 | } 856 | else if( cmd=='s' ) 857 | { 858 | if( n==1 ) 859 | { 860 | Serial.print(F("Current drive is ")); 861 | Serial.write('A' + ArduinoFDC.selectedDrive()); 862 | } 863 | else 864 | { 865 | Serial.print(F("Selecting drive ")); 866 | Serial.write(a1>0 ? 'B' : 'A'); 867 | Serial.println(); 868 | ArduinoFDC.selectDrive(n>1 && a1>0); 869 | ArduinoFDC.motorOn(); 870 | } 871 | } 872 | else if( cmd=='t' && n>1 ) 873 | { 874 | set_drive_type(a1); 875 | } 876 | else if( cmd=='f' ) 877 | { 878 | if( confirm_formatting() ) 879 | { 880 | Serial.println(F("Formatting disk...")); 881 | byte status = ArduinoFDC.formatDisk(databuffer, n>1 ? a1 : 0, n>2 ? a2 : 255); 882 | if( status!=S_OK ) print_error(status); 883 | memset(databuffer, 0, 513); 884 | } 885 | } 886 | #ifdef USE_XMODEM 887 | else if( cmd=='R' ) 888 | { 889 | Serial.println(F("Send image via XModem now...")); 890 | xmodem_status_mon = S_OK; 891 | xmodem_sector = 0; 892 | xmodem_data_ptr = 0; 893 | xmodem_verify = n>1 && (a1!=0); 894 | 895 | XModem modem(recvChar, sendData, xmodemHandlerReceiveMon); 896 | if( modem.receive() && xmodem_status_mon==S_OK ) 897 | Serial.println(F("\r\nSuccess!")); 898 | else 899 | { 900 | unsigned long t = millis() + 500; 901 | while( millis() < t ) { if( Serial.read()>=0 ) t = millis()+500; } 902 | while( Serial.read()<0 ); 903 | 904 | Serial.println('\r'); 905 | if( xmodem_status_mon!=S_OK ) print_error(xmodem_status_mon); 906 | } 907 | } 908 | else if( cmd=='S' ) 909 | { 910 | Serial.println(F("Receive image via XModem now...")); 911 | xmodem_status_mon = S_OK; 912 | xmodem_sector = 0; 913 | xmodem_data_ptr = 0xFFFF; 914 | 915 | XModem modem(recvChar, sendData, xmodemHandlerSendMon); 916 | if( modem.transmit() && xmodem_status_mon==S_OK ) 917 | Serial.println(F("\r\nSuccess!")); 918 | else 919 | { 920 | unsigned long t = millis() + 500; 921 | while( millis() < t ) { if( Serial.read()>=0 ) t = millis()+500; } 922 | while( Serial.read()<0 ); 923 | 924 | Serial.println('\r'); 925 | if( xmodem_status_mon!=S_OK ) print_error(xmodem_status_mon); 926 | } 927 | } 928 | #endif 929 | #ifdef USE_ARDUDOS 930 | else if (cmd=='x' ) 931 | return; 932 | #endif 933 | #if !defined(USE_ARDUDOS) || !defined(USE_MONITOR) || !defined(USE_XMODEM) || defined(__AVR_ATmega2560__) 934 | // must save flash space if all three of ARDUDOS/MONITR/XMODEM are enabled on UNO 935 | else if( cmd=='h' || cmd=='?' ) 936 | { 937 | Serial.println(F("Commands (t=track (0-based), s=sector (1-based), h=head (0/1)):")); 938 | Serial.println(F("r t,s,h Read sector to buffer and print buffer")); 939 | Serial.println(F("r Read ALL sectors and print pass/fail")); 940 | Serial.println(F("w t,s,h Write buffer to sector")); 941 | Serial.println(F("w [0/1] Write buffer to ALL sectors (without/with verify)")); 942 | Serial.println(F("b Print buffer")); 943 | Serial.println(F("B [n] Fill buffer with 'n' or 00..FF if n not given")); 944 | Serial.println(F("m 0/1 Turn drive motor off/on")); 945 | Serial.println(F("s 0/1 Select drive A/B")); 946 | Serial.println(F("t 0-4 Set type of current drive (5.25DD/5.25DDinHD/5.25HD/3.5DD/3.5HD)")); 947 | Serial.println(F("f Low-level format disk (tf)")); 948 | #ifdef USE_XMODEM 949 | Serial.println(F("S Read disk image and send via XModem")); 950 | Serial.println(F("R [0/1] Receive disk image via XModem and write to disk (without/with verify)")); 951 | #endif 952 | #ifdef USE_ARDUDOS 953 | Serial.println(F("x Exit monitor\n")); 954 | #endif 955 | } 956 | #endif 957 | else 958 | Serial.println(F("Invalid command")); 959 | } 960 | } 961 | 962 | #endif 963 | 964 | 965 | // ------------------------------------------------------------------------------------------------- 966 | // Main functions 967 | // ------------------------------------------------------------------------------------------------- 968 | 969 | 970 | void setup() 971 | { 972 | Serial.begin(115200); 973 | ArduinoFDC.begin(ArduinoFDCClass::DT_3_HD, ArduinoFDCClass::DT_3_HD); 974 | 975 | // must save flash space if all three of ARDUDOS/MONITOR/XMODEM are enabled on UNO 976 | #if !defined(USE_ARDUDOS) || !defined(USE_MONITOR) || !defined(USE_XMODEM) || defined(__AVR_ATmega2560__) 977 | Serial.print(F("Drive A: ")); print_drive_type(ArduinoFDC.getDriveType()); Serial.println(); 978 | if( ArduinoFDC.selectDrive(1) ) 979 | { 980 | Serial.print(F("Drive B: ")); print_drive_type(ArduinoFDC.getDriveType()); Serial.println(); 981 | ArduinoFDC.selectDrive(0); 982 | } 983 | #endif 984 | } 985 | 986 | 987 | void loop() 988 | { 989 | #if defined(USE_ARDUDOS) 990 | arduDOS(); 991 | #elif defined(USE_MONITOR) 992 | monitor(); 993 | #else 994 | #error "Need at least one of USE_ARDUDOS and USE_MONITOR" 995 | #endif 996 | } 997 | -------------------------------------------------------------------------------- /ArduinoFDCMegaShieldGerber.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhansel/ArduinoFDC/c376c44166c2abf6bcc02500afa78697afab289f/ArduinoFDCMegaShieldGerber.zip -------------------------------------------------------------------------------- /ArduinoFDCMegaShieldSchematic.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhansel/ArduinoFDC/c376c44166c2abf6bcc02500afa78697afab289f/ArduinoFDCMegaShieldSchematic.pdf -------------------------------------------------------------------------------- /ArduinoFDCShieldGerber.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhansel/ArduinoFDC/c376c44166c2abf6bcc02500afa78697afab289f/ArduinoFDCShieldGerber.zip -------------------------------------------------------------------------------- /ArduinoFDCShieldSchematic.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhansel/ArduinoFDC/c376c44166c2abf6bcc02500afa78697afab289f/ArduinoFDCShieldSchematic.pdf -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ArduinoFDC 2 | 3 | ![ArduinoFDC Setup](images/setup.jpg) 4 | 5 | ArduinoFDC is a sketch that implements a floppy disk controller. It works with Arduino Uno, Leonardo, Nano, Pro Mini, Micro and Mega. 6 | 7 | ArduinoFDC consists of three parts: 8 | 9 | 1. A [library](#library-functions) providing low-level functions to allow reading and writing disks at the 10 | sector level as well as low-level formatting disks. 11 | 2. Integration of ChaN's brilliant [FatFS](http://elm-chan.org/fsw/ff/00index_e.html) 12 | library to provide file-level functions for reading and writing files and directories 13 | in a FAT (MS-DOS) file system and a high-level format function to initialize a FAT file system. 14 | 3. An example sketch implementing [ArduDOS](#ardudos), a (very) small DOS environment for browsing 15 | a FAT file system as well as a [low-level disk monitor](#low-level-disk-monitor) to access sector data on the disk, 16 | including the ability to transfer whole disks or single files via the XModem protocol. 17 | 18 | ArduinoFDC works with double density (DD) as well as high density (HD) 19 | disk drives. It can read, write and format 5.25" DD (360KB), 5.25" HD (1.2MB), 3.5" DD (720KB) 20 | and 3.5" HD (1.44MB) disks. 21 | 22 | ## Wiring 23 | 24 | If you don't want to wire your disk drive up yourself then you can use a shield 25 | for Arduino Uno or Mega. See the [ArduinoFDC shields](#arduinofdc-shields) section below. 26 | The shield plugs into the Arduino and provides a standard 34-pin floppy disk connector. 27 | 28 | The table below shows how to wire the Arduino pins to the 34-pin IDC 29 | connector on the floppy drive cable. 30 | 31 | The pin numbers are defined at the top of the ArduinoFDC.cpp file. Some of them can 32 | easily be changed whereas others are hard-coded in the controller code. Refer to 33 | the comments at the top of the ArduinoFDC.cpp file if you want to use different 34 | pin assignments. 35 | 36 | Floppy Cable | Uno/Mini/Nano | Leonardo/Micro | Mega | Notes | Function 37 | -----------------|---------------|-----------------|--------|--------|--------------- 38 | 2 | 13 | 13/16 | 42 | 3,4,5 | Density select 39 | 8 | 7 | 8 | 47 | | Index 40 | 10 | 4 | 5 | 51 | 1,3 | Motor Enable A 41 | 12 | A1 | A1 | 40 | 1,3,4 | Drive Select B 42 | 14 | 5 | 6 | 50 | 1,3 | Drive Select A 43 | 16 | A0 | A0 | 41 | 1,3,4 | Motor Enable B 44 | 18 | 3 | 3 | 52 | 3 | Head Step Direction 45 | 20 | 2 | 2 | 53 | 3 | Head Step Pulse 46 | 22 | 9 | 9 | 46 | | Write Data 47 | 24 | 10 | 10 | 45 | | Write Gate 48 | 26 | 11 | 11/14 | 44 | 3 | Track 0 49 | 28 | 12 | 12/15 | 43 | 3,4 | Write Protect 50 | 30 | 8 | 4 | 48 | 2 | Read Data 51 | 32 | 6 | 7 | 49 | 3 | Side Select 52 | 34 | A2 | A2 | 39 | 3,4 | Disk Changed 53 | 1,3,5,...,31,33 | GND | GND | GND | 6 | Signal Ground 54 | 55 | **Note 1:** 56 | The pin numbers for the SELECT/MOTOR signals assume you are wiring to 57 | the controller end of a floppy drive cable. If you are wiring directly 58 | to the floppy drive, the A/B pins will be reversed (search the web 59 | for "Floppy drive twist" for more information). 60 | 61 | **Note 2:** 62 | It is **highly** recommended (but not entirely necessary) to add a 1k 63 | pull-up resistor to +5V to this signal. The Arduino's built-in pull-up 64 | resistors are very weak (20-50k) and may not pull the signal up quickly enough. 65 | Without the resistor you may encounter read errors (bad CRC, header not found), 66 | especially when reading HD disks. Whether it works without the resistor will 67 | depend on your specific drive, drive cable, connections and Arduino. 68 | 69 | **Note 3:** 70 | This signal can easily be moved to a different pin on the Arduino by 71 | changing the corresponding `#define PIN_XXX ...` statement at the top 72 | of ArduinoFDC.cpp 73 | 74 | **Note 4:** 75 | This signal is not essential for the functionality of the controller. 76 | The corresponding pin can be freed by commenting out the `#define PIN_XXX ...` 77 | statement at the top of ArduinoFDC.cpp 78 | 79 | **Note 5:** 80 | See section "DENSITY control signal" below. 81 | 82 | **Note 6:** 83 | You should be able to just pick one of the GND pins. However, some cables/drives 84 | do not actually connect all of these to ground. If your setup does not work 85 | it may be worth trying a different GND pin. 86 | 87 | ## Powering the drive 88 | 89 | In addition to the signal wiring, the floppy drive needs to be powered. 90 | 5.25" drives typically use a [Molex connector](https://en.wikipedia.org/wiki/Molex_connector#Disk_drive) 91 | while 3.5" drives usually use a [Berg connector](https://en.wikipedia.org/wiki/Berg_connector). 92 | 93 | In my setup I used the power supply of an external hard drive adapter ([something like this](https://www.amazon.com/SATA-Adapter-Converter-Cable-Drive/dp/B01HO07GNW)), 94 | which has a Molex connector, with an adapter to the 3.5" drive Berg connector. 95 | 96 | Since most (all?) 3.5" drives do not use the 12V power line, it is possible to power such drives directly from the Arduio. 97 | Connect the 5V and GND pins from the Arduino to the 5V and GND pins on the [floppy power connector](https://en.wikipedia.org/wiki/Berg_connector). 98 | I have done that before but did run into issues when using cheap cables to connect the Arduino to the PC. 99 | What happened was that the USB cable was unable to support the power needed for the Arduino and floppy drive. 100 | This resulted in a voltage drop over the USB cable that caused the drive to not work properly. 101 | 102 | I would generally recommend using a separate power supply for the drive. 103 | 104 | ## Supported disk/drive types 105 | 106 | To properly read/write data, the library must be configured for the drive/disk 107 | combination that is being used. The drive type can be passed into the `begin` functions 108 | or set afterwards by calling the `setDriveType` function. Supported types are: 109 | * **ArduinoFDC::DT_5_DD**: Double-density disk in a 5.25" double-density drive 110 | * **ArduinoFDC::DT_5_DDonHD**: Double-density disk in a 5.25" high-density drive 111 | * **ArduinoFDC::DT_5_HD**: High-density disk in a 5.25" high-density drive 112 | * **ArduinoFDC::DT_3_DD**: Double-density disk in a 3.5" double- or high-density drive 113 | * **ArduinoFDC::DT_3_HD**: High-density disk in a 3.5" high-density drive 114 | 115 | ## DENSITY control signal 116 | 117 | The function of the DENSITY control signal line between the controller and the 118 | floppy drive is not well defined and varies between drives. Furthermore most drives 119 | can be configured by jumpers (also called "straps"). You may want to consult the 120 | documentation for your drive for details. 121 | 122 | In their default configuration most 3.5" drives do not use this signal. The drive 123 | itself determines the type of disk (DD or HD) by the presence of a hole in the disk 124 | (on the opposite edge from the "write protect" hole). The controller must be configured 125 | separately for the correct type (DD or HD), otherwise reading/writing will fail. 126 | 127 | For 5.25" HD drives this signal is generally an input from the controller to the drive. 128 | If the signal is LOW then low density mode is selected, otherwise high density is used. 129 | However, for some drives the opposite is true. Many drives can be configured via jumpers 130 | to select the expected levals. 131 | 132 | The controller can be configured what to do with this signal by calling the "setDensityPinMode" 133 | function. The following modes are supported: 134 | * **ArduinoFDC::DP_DISCONNECT**: This configures the DENSITY pin as INPUT. It does not actually read the pin. 135 | * **ArduinoFDC::DP_OUTPUT_LOW_FOR_HD**: This configures the DENSITY pin as an OUTPUT and sets it LOW if the disk type is HD. 136 | * **ArduinoFDC::DP_OUTPUT_LOW_FOR_DD**: This configures the DENSITY pin as an OUTPUT and sets it LOW if the disk type is DD. 137 | 138 | By default, the mode is set to DP_DISCONNECT for 3.5" drives and DP_OUTPUT_LOW_FOR_DD for 5.25" drives. 139 | 140 | Another way to handle the DENSITY signal is to comment out the `#define PIN_DENSITY` line 141 | at the top of ArduinoFDC.cpp and hard-wire the DENSITY signal from the disk drive cable 142 | to the proper level (or leave it disconnected). 143 | 144 | ## Library functions: 145 | 146 | To use the low-level disk access library functions (listed below), copy ArduinoFDC.h and ArduinoFDC.cpp 147 | into your Arduino sketch directory and add `#include "ArduinoFDC.h"` to your sketch. 148 | 149 | To use the FAT file system functions, additionally copy ff.h, ff.c, ffconf.h, diskio.h and diskio.cpp. 150 | Then add `#include "ff.h"` and `#include "ArduinoFDC.h"` to your sketch. For documentation of the FatFS 151 | functions refer to the [FatFS documentation](http://elm-chan.org/fsw/ff/00index_e.html). 152 | 153 | #### `void ArduinoFDC.begin(driveAtype, driveBtype)` 154 | Initializes the Arduino pins used by the controller. For possible drive types see 155 | the "[Supported disk/drive types](#supported-diskdrive-types)" section above. If left out both types default to 156 | ArduinoFDC::DT_3_HD. 157 | 158 | #### `void ArduinoFDC.end()` 159 | Releases the pins initialized by ArduinoFDC.begin() 160 | 161 | #### `bool ArduinoFDC.selectDrive(byte drive)` 162 | Selects drive A (0) or B (1) to be used for subsequent calls to 163 | readSector/writeSector/formatDisk. Calling `begin()` selects drive A. 164 | Returns 'false' if trying to select drive 1 when the corresponding control 165 | pins are commented out in ArduinoFDC.cpp 166 | 167 | #### `byte ArduinoFDC.selectedDrive()` 168 | Returns which drive is currently selected, A (0) or B (1). 169 | 170 | #### `void ArduinoFDC.setDriveType(driveType)` 171 | Sets the disk/drive type for the currently selected drive. For possible drive types see 172 | the "[Supported disk/drive types](#supported-diskdrive-types)" section above. 173 | 174 | #### `byte ArduinoFDC.getDriveType(driveType)` 175 | Returns the drive type of the currently selected drive. 176 | 177 | #### `void ArduinoFDC.setDensityPinMode(mode)` 178 | Sets the function of the DENSITY pin for the currently selected drive. 179 | See section "[Density control signal](#density-control-signal)" above. 180 | 181 | #### `byte ArduinoFDC.numTracks()` 182 | Returns the number of tracks for the drive type of the currently selected drive. 183 | 184 | #### `byte ArduinoFDC.numSectors()` 185 | Returns the number of sectors per track for the drive type of the currently selected drive. 186 | 187 | #### `bool ArduinoFDC.haveDisk()` 188 | Returns true if a disk is in the drive. This is done by looking for the index 189 | hole. If the drive motor is not currently running this haveDisk() will temporarily 190 | turn it on. 191 | 192 | #### `bool ArduinoFDC.isWriteProtected()` 193 | Returns true if the disk is write protected. If no disk is in the drive then 194 | the result may be either true or false. If the WRITE PROTECT signal is not connected 195 | then the result is always false. 196 | 197 | #### `bool ArduinoFDC.diskChanged()` 198 | Returns true if a disk change was detected since the last call to diskChanged(). 199 | 200 | #### `void ArduinoFDC.motorOn()` 201 | Turns the disk drive motor on. 202 | 203 | The `readSector`/`writeSector`/`formatDisk` functions will turn the motor on **and** back off 204 | automatically if it is not already running. Note that turning on the motor also includes 205 | a one second delay to allow it to spin up. If you are reading/writing multiple sectors 206 | you may want to use the `motorOn` and `motorOff` functions to manually turn the motor on 207 | and off. 208 | 209 | #### `void ArduinoFDC.motorOff()` 210 | Turns the disk drive motor off. 211 | 212 | #### `bool ArduinoFDC.motorRunning()` 213 | Returns *true* if the disk drive motor is currently running and *false* if not. 214 | 215 | #### `byte ArduinoFDC.readSector(byte track, byte side, byte sector, byte *buffer)` 216 | Reads data from a sector from the flopy disk. Always reads a full sector (512 bytes). 217 | 218 | * The "track" parameter must be in range 0..(numTracks()-1) 219 | * The "side" parameter must either be 0 or 1 220 | * The "sector" paramter must be in range 1..numSectors() 221 | * The "buffer" parameter must be a pointer to a byte array of size (at least) 516 bytes. 222 | 223 | The function returns 0 if reading succeeded. Otherwise an error code is returned 224 | (see [Troubleshooting](#troubleshooting) section below) 225 | 226 | **IMPORTANT:** On successful return, the sector data that was read will be in buffer[1..512] (**NOT** buffer[0..511]) 227 | 228 | #### `byte ArduinoFDC.writeSector(byte track, byte side, byte sector, byte *buffer, bool verify)` 229 | Writes data to a sector on the floppy disk. Always writes a full sector (512 bytes). 230 | 231 | * The "track" parameter must be in range 0..(numTracks()-1) 232 | * The "side" parameter must either be 0 or 1 233 | * The "sector" paramter must be in range 1..numSectors() 234 | * The "buffer" parameter must be a pointer to a byte array of size (at least) 516 bytes. 235 | * If the "verify" parameter is *true*, the data written will be read back and compared to what was written. 236 | If a difference is detected then the function will return error code S_VERIFY. 237 | If the "verify" parameter is *false* then no verification is done. 238 | 239 | The function returns 0 if writing succeeded. Otherwise an error code is returned 240 | (see [Troubleshooting](#troubleshooting) section below) 241 | 242 | **IMPORTANT:** The sector data to be written must be in buffer[1..512] (**NOT** buffer[0..511]) 243 | 244 | #### `bool ArduinoFDC.formatDisk(byte *buffer, byte from_track=0, byte to_track=255, byte interleave = 1)` 245 | Formats a floppy disk according to the format specified by the "setDriveType()" function. 246 | A subset of tracks can be formatted by specifying the from_track and to_track parameters. 247 | 248 | A buffer of size at least 144 bytes is needed to store temporary data during formatting. 249 | The buffer is passed in as an argument to allow re-using other buffers in your sketch. 250 | If you do not have a buffer to be re-used, just declare `byte buffer[144]` before calling formatDisk(). 251 | 252 | The interleave argument specifies the disk interleave factor, i.e. interleave=7 specifies 253 | an [interleave factor](https://en.wikipedia.org/wiki/Interleaving_(disk_storage)) of 1:7 (default is 1:1). 254 | 255 | This function does **not** set up any file system on the disk. It only sets up the 256 | low-level sector structure that allows reading and writing of sectors (and fills all 257 | sector data with 0xF6 bytes). 258 | 259 | The function returns 0 if formatting succeeded. Otherwise an error code is returned 260 | (see [Troubleshooting](#troubleshooting) section below). Note that no verification of the formatted disk 261 | is performed. The only possible error conditions are missing track 0 or index hole signals. 262 | You can use the `readSector`function to verify that data can be read properly 263 | after formatting. 264 | 265 | ## ArduDOS 266 | 267 | ArduDOS is a minimal DOS that allows the user to browse the file system 268 | on the disk and read/write files. The basic functionality is modeled on MS-DOS 269 | with some exceptions: 270 | 1. All commands operate **only** on the currently selected drive. If two drives are 271 | connected then use "b:" or "a:" to switch drives. 272 | 2. The working directory is **always** the top-level directory of the disk. No "cd" 273 | command is available to change the directory. Therefore, all paths given as 274 | arguments to commands must be relative to the top-level directory. 275 | 3. Disk changes are **not** automatically detected. After changing a disk, re-select 276 | the current drive (e.g. "a:") to notify ArduDOS of the change. 277 | 278 | ArduDOS is easy to access from either Arduio's serial monitor or any other 279 | serial terminal. Set the monitor or terminal's baud rate to 115200 before connecting. 280 | The following commands are available: 281 | 282 | * `dir [directory]`
283 | Show the listing of the specified directory (default is root directory). 284 | * `type filename`
285 | Type out the specified file to the screen (best for text files). 286 | * `dump filename`
287 | Dump the specified file to the screen in hexadecimal notation (best for binary files). 288 | * `write filename`
289 | Receive text line-by-line from the user and write it to the specified file. 290 | Enter an empty line to finish. 291 | * `del filename`
292 | Delete the specified file. 293 | * `mkdir dirname`
294 | Create the specified directory. 295 | * `rmdir dirname`
296 | Remove the specified directory. 297 | * `disktype 0/1/2/3/4`
298 | Set the drive type of the current drive, where 0/1/2/3/4 stands for the drive type 299 | as listed (in the same order) in section "[Supported disk/drive types](#supported-diskdrive-types)" above. 300 | * `format [/q]`
301 | Low-level format a disk and initialize a FAT file system. If `/q` argument is given, 302 | performs a quick format, i.e. only resets the file system without low-level format. 303 | * `monitor`
304 | Enter the low-level disk monitor (see the "[Low-level disk monitor](#low-level-disk-monitor)" section below). 305 | * `send filename` (only available if `#define USE_XMODEM` is enabled at the top of ArduinoFDC.ino)
306 | Send the specified file via XModem protocol. See the "[XModem](#xmodem)" section below for more details. 307 | * `receive filename` (only available if `#define USE_XMODEM` is enabled at the top of ArduinoFDC.ino)
308 | Receive the specified file via XModem protocol. See the "[XModem](#xmodem)" section below for more details. 309 | 310 | ![ArduDOS session](images/ArduDOS.png) 311 | 312 | ## Low-level disk monitor 313 | 314 | Like ArduDOS, the low-level monitor is easy to use with the Arduino serial monitor. 315 | When using ArduDOS (i.e. `#define USE_ARDUDOS` is enabled at the top of ArduinoFDC.ino), 316 | use the "monitor" command to enter the low-level monitor. 317 | 318 | If ArduDOS is not enabled then the sketch drops directly into the disk monitor mode. 319 | Set Arduino's serial monitor to 115200 baud to connect. 320 | 321 | When running, the monitor will show a command prompt. Enter your command in the 322 | serial monitor's input line and press *Enter* to execute the command. 323 | 324 | The following commands are supported: 325 | * `r track, sector[,side]`
326 | Read the sector specified by track/sector/side, copy its contents to an internal 327 | buffer and show the buffer content. If the *side* parameter is left out it defaults 328 | to zero. 329 | * `w track, sector[,side]`
330 | Write the current buffer contents to the sector specified by track/sector/side 331 | and verify the data after writing. Shows "Ok" or "Error" status after execution. 332 | If the *side* parameter is left out it defaults to zero. 333 | * `f`
334 | Low-level format the disk. No file system is initialized, all sectors are filled 335 | with 0xF6. To format and/or add a file system use the "format" command in ArduDOS 336 | (see ArduDOS section above). 337 | * `b`
338 | Show the current buffer content 339 | * `B [n]`
340 | Fill the buffer with value *n*. If *n* is left out then fill the buffer with 341 | bytes 0,1,2,...255,0,1,2,...255. 342 | * `m [0/1]`
343 | Turn the motor of the currently selected drive off/on. If the *0/1* parameter is left out 344 | then the current motor status is shown. 345 | * `r`
346 | Read ALL sectors on the disk and show status Ok/Error for each one. 347 | * `w [0/1]`
348 | Write the current buffer content to ALL sectors on the disk. If the *0/1* parameter 349 | is 1 then verify every sector after writing it (significantly slower). 350 | If the *0/1* parameter is left out it defaults to 0. 351 | * `s [0/1]`
352 | Select drive A (0) or B (1). If the *0/1* parameter is left out then the currently 353 | selected drive is shown. 354 | * `t 0/1/2/3/4`
355 | Set the drive type of the current drive, where 0/1/2/3/4 stands for the drive type 356 | as listed (in the same order) in section "[Supported disk/drive types](#supported-diskdrive-types)" above. 357 | * `S` (only available if `#define USE_XMODEM` is enabled at the top of ArduinoFDC.ino)
358 | Read all sectors of the current disk and transmit them via XModem protocol. See 359 | the "[XModem](#xmodem)" section below for more details. 360 | * `R` (only available if `#define USE_XMODEM` is enabled at the top of ArduinoFDC.ino)
361 | Receive a disk image via XModem and write it to the current disk. See 362 | the "[XModem](#xmodem)" section below for more details. 363 | * `x` (only if the monitor is entered from ArduDOS via the "monitor" command)
364 | Exit the monitor and return to [ArduDOS](#ardudos). 365 | 366 | ## XModem 367 | 368 | Both ArduDOS and the low-level monitor can transmit and receive data via the XModem protocol. 369 | If you want to use this functionality, first un-comment the `#define USE_XMODEM` setting at 370 | the top of file ArduinoFDC.ino and re-upload the sketch. 371 | 372 | Use a terminal program that supports XModem to send/receive the data. I recommend TeraTerm. 373 | First start the transfer on the controller then initiate the XModem send/receive function in 374 | the terminal program. 375 | 376 | Note that *no error messages* can be displayed during the XModem transfers since the transfer 377 | takes place over the same serial connection as the terminal. If the transfer stops prematurely 378 | and nothing is shown in the terminal, pressing ENTER will get the command prompt back. 379 | 380 | ## Troubleshooting 381 | 382 | The following table lists the error codes returned by the `readSector`, `writeSector` 383 | and `formatDisk` functions including possible causes for each error. Pin numbers refer 384 | to pins on the Arduino UNO. 385 | 386 | \# | Code | Meaning | Possible causes 387 | --|-------------|---------|---------------- 388 | 0 | S_OK | No error, the operation succeeded | 389 | 1 | S_NOTINIT | The ArduinoFDC.begin() function has not been called | 390 | 2 | S_NOTREADY | No data at all is received from the disk drive | - no disk in drive
- drive does not have power
- pins MOTOR (4/12), SELECT (5/13), READ (8), INDEX (7) or GND not properly connected 391 | 3 | S_NOSYNC | Data is received but no sync mark can be found | - disk not formatted or not formatted for the correct density
- GND pin not properly connected 392 | 4 | S_NOHEADER | Sync marks are found but either no sector header or no header with the expected track/side/sector markings | - pins STEP (2), STEPDIR (3), SIDE (6) or GND not properly connected
- bad disk or unknown format
- misaligned disk drive
- invalid track, sector or head number given 393 | 5 | S_INVALIDID | The data record was not started by a 0xFB byte as expected | - bad disk or unknown format 394 | 6 | S_CRC | The sector data checksum is incorrect | - bad disk or unknown format
- pullup resistors too weak (see note 2 in wiring section) 395 | 7 | S_NOTRACK0 | When trying to move the read head to track 0, the TRACK0 signal was not seen, even after stepping more than 80 tracks. | - pins STEP (2), STEPDIR (3), SELECT (5/13), TRACK0 (11) or GND not properly connected
- drive does not have power 396 | 8 | S_VERIFY | When reading back data that was just written, the data did not match | - pins WRITEGATE (10) or WRITEDATA (9) not properly connected
- disk is write protected and WRITEPROTECT (12) pin is not connected
- bad disk 397 | 9 | S_READONLY | Attempting to write to a write-protected disk | - disk is write protected 398 | 399 | ## ArduinoFDC shields 400 | 401 | For a more permanent connection between the disk drive and the Arduino, I made shields for the Arduino UNO 402 | and Arduino Mega: 403 | 404 | ![ArduinoFDC Setup](images/Shields.jpg) 405 | 406 | The only components to populate are two resistors (R1 and R2, both 1 kOhm) and the 407 | [IDC34 connector](https://www.digikey.com/en/products/detail/sullins-connector-solutions/SBH11-PBPC-D17-ST-BK/1990067) 408 | for the disk drive. 409 | 410 | These shields use the default pins as shown in the wiring table above. As is, all pins including 411 | optional ones are wired up. If you need some optional pins for other functions then simply cut 412 | the small trace between the two pads next to the pin description: 413 | 414 | ![Traces](images/CutTrace.jpg) 415 | 416 | That will disconnect the signal from the disk drive connector and you can connect it to something 417 | else. If you cut the traces and install pin headers in the adjacent holes then you can use jumpers 418 | to easily connect or disconnect the signals. 419 | 420 | To get a shield, simply download the Gerber file from this repository and upload it to a PCB 421 | manufacturing site such as JLCPCB or PCBWay: 422 | - Gerber file for Arduino UNO can be downloaded [here](https://github.com/dhansel/ArduinoFDC/raw/main/ArduinoFDCShieldGerber.zip) 423 | (schematics are [here](https://github.com/dhansel/ArduinoFDC/raw/main/ArduinoFDCShieldSchematic.pdf)) 424 | - Gerber file for Arduino Mega can be downloaded [here](https://github.com/dhansel/ArduinoFDC/raw/main/ArduinoFDCMegaShieldGerber.zip) 425 | (schematics are [here](https://github.com/dhansel/ArduinoFDC/raw/main/ArduinoFDCMegaShieldSchematic.pdf)) 426 | 427 | Here is a picture of an assembled shield attached to an Arduino UNO: 428 | 429 | ![Assembled ArduinoFDC Shield](images/ShieldAssembled.jpg) 430 | 431 | ## Acknowledgements 432 | 433 | The ArduDOS functionality would not have been possible without ChaN's brilliant [FatFS](http://elm-chan.org/fsw/ff/00index_e.html) library. 434 | 435 | The XModem communication uses the arduino-xmodem code available at https://code.google.com/archive/p/arduino-xmodem. 436 | -------------------------------------------------------------------------------- /XModem.cpp: -------------------------------------------------------------------------------- 1 | // This code was taken from: https://github.com/mgk/arduino-xmodem 2 | // (https://code.google.com/archive/p/arduino-xmodem) 3 | // which was released under GPL V3: 4 | // 5 | // This program is free software; you can redistribute it and/or modify 6 | // it under the terms of the GNU General Public License as published by 7 | // the Free Software Foundation; either version 3 of the License, or 8 | // (at your option) any later version. 9 | // 10 | // This program is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | // GNU General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU General Public License 16 | // along with this program; if not, write to the Free Software Foundation, 17 | // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | // ----------------------------------------------------------------------------- 19 | 20 | #include 21 | #include 22 | 23 | #include "XModem.h" 24 | #ifdef UTEST 25 | #include "CppUTestExt/MockSupport.h" 26 | #endif 27 | const unsigned char XModem::NACK = 21; 28 | const unsigned char XModem::ACK = 6; 29 | 30 | const unsigned char XModem::SOH = 1; 31 | const unsigned char XModem::EOT = 4; 32 | const unsigned char XModem::CAN = 0x18; 33 | 34 | const int XModem::receiveDelay=7000; 35 | const int XModem::rcvRetryLimit = 10; 36 | 37 | 38 | 39 | 40 | XModem::XModem(int (*recvChar)(int msDelay), 41 | void (*sendData)(const char *data, int len)) 42 | { 43 | this->sendData = sendData; 44 | this->recvChar = recvChar; 45 | this->dataHandler = NULL; 46 | 47 | } 48 | XModem::XModem(int (*recvChar)(int msDelay), 49 | void (*sendData)(const char *data, int len), 50 | bool (*dataHandler)(unsigned long number, char *buffer, int len)) 51 | { 52 | this->sendData = sendData; 53 | this->recvChar = recvChar; 54 | this->dataHandler = dataHandler; 55 | 56 | } 57 | 58 | bool XModem::dataAvail(int delay) 59 | { 60 | if (this->byte != -1) 61 | return true; 62 | if ((this->byte = this->recvChar(delay)) != -1) 63 | return true; 64 | else 65 | return false; 66 | 67 | } 68 | int XModem::dataRead(int delay) 69 | { 70 | int b; 71 | if(this->byte != -1) 72 | { 73 | b = this->byte; 74 | this->byte = -1; 75 | return b; 76 | } 77 | return this->recvChar(delay); 78 | } 79 | void XModem::dataWrite(char symbol) 80 | { 81 | this->sendData(&symbol, 1); 82 | } 83 | bool XModem::receiveFrameNo() 84 | { 85 | unsigned char num = 86 | (unsigned char)this->dataRead(XModem::receiveDelay); 87 | unsigned char invnum = 88 | (unsigned char)this->dataRead(XModem::receiveDelay); 89 | this->repeatedBlock = false; 90 | //check for repeated block 91 | if (invnum == (255-num) && num == this->blockNo-1) { 92 | this->repeatedBlock = true; 93 | return true; 94 | } 95 | 96 | if(num != this-> blockNo || invnum != (255-num)) 97 | return false; 98 | else 99 | return true; 100 | } 101 | bool XModem::receiveData() 102 | { 103 | for(int i = 0; i < 128; i++) { 104 | int byte = this->dataRead(XModem::receiveDelay); 105 | if(byte != -1) 106 | this->buffer[i] = (unsigned char)byte; 107 | else 108 | return false; 109 | } 110 | return true; 111 | } 112 | bool XModem::checkCrc() 113 | { 114 | unsigned short frame_crc = ((unsigned char)this-> 115 | dataRead(XModem::receiveDelay)) << 8; 116 | 117 | frame_crc |= (unsigned char)this->dataRead(XModem::receiveDelay); 118 | //now calculate crc on data 119 | unsigned short crc = this->crc16_ccitt(this->buffer, 128); 120 | 121 | if(frame_crc != crc) 122 | return false; 123 | else 124 | return true; 125 | 126 | } 127 | bool XModem::checkChkSum() 128 | { 129 | unsigned char frame_chksum = (unsigned char)this-> 130 | dataRead(XModem::receiveDelay); 131 | //calculate chksum 132 | unsigned char chksum = 0; 133 | for(int i = 0; i< 128; i++) { 134 | chksum += this->buffer[i]; 135 | } 136 | if(frame_chksum == chksum) 137 | return true; 138 | else 139 | return false; 140 | } 141 | bool XModem::sendNack() 142 | { 143 | this->dataWrite(XModem::NACK); 144 | this->retries++; 145 | if(this->retries < XModem::rcvRetryLimit) 146 | return true; 147 | else 148 | return false; 149 | 150 | } 151 | bool XModem::receiveFrames(transfer_t transfer) 152 | { 153 | bool handlerOk = true; 154 | this->blockNo = 1; 155 | this->blockNoExt = 1; 156 | this->retries = 0; 157 | while (1) { 158 | char cmd = this->dataRead(1000); 159 | switch(cmd){ 160 | case XModem::SOH: 161 | if (!this->receiveFrameNo()) { 162 | if (this->sendNack()) 163 | break; 164 | else 165 | return false; 166 | } 167 | if (!this->receiveData()) { 168 | if (this->sendNack()) 169 | break; 170 | else 171 | return false; 172 | 173 | }; 174 | if (transfer == Crc) { 175 | if (!this->checkCrc()) { 176 | if (this->sendNack()) 177 | break; 178 | else 179 | return false; 180 | } 181 | } else { 182 | if(!this->checkChkSum()) { 183 | if (this->sendNack()) 184 | break; 185 | else 186 | return false; 187 | } 188 | } 189 | //callback 190 | if(handlerOk && this->dataHandler != NULL && this->repeatedBlock == false) 191 | handlerOk = this->dataHandler(this->blockNoExt, this->buffer, 128); 192 | //ack 193 | if( handlerOk ) 194 | { 195 | this->dataWrite(XModem::ACK); 196 | if(this->repeatedBlock == false) 197 | { 198 | this->blockNo++; 199 | this->blockNoExt++; 200 | } 201 | this->retries = 0; 202 | } 203 | else if( !this->sendNack() ) 204 | return false; 205 | 206 | break; 207 | case XModem::EOT: 208 | this->dataWrite(XModem::ACK); 209 | return true; 210 | case XModem::CAN: 211 | //wait second CAN 212 | if(this->dataRead(XModem::receiveDelay) == 213 | XModem::CAN) { 214 | this->dataWrite(XModem::ACK); 215 | //this->flushInput(); 216 | return false; 217 | } 218 | //something wrong 219 | this->dataWrite(XModem::CAN); 220 | this->dataWrite(XModem::CAN); 221 | this->dataWrite(XModem::CAN); 222 | return false; 223 | default: 224 | //something wrong 225 | this->dataWrite(XModem::CAN); 226 | this->dataWrite(XModem::CAN); 227 | this->dataWrite(XModem::CAN); 228 | return false; 229 | } 230 | 231 | } 232 | } 233 | void XModem::init() 234 | { 235 | //set preread byte 236 | this->byte = -1; 237 | } 238 | bool XModem::receive() 239 | { 240 | this->init(); 241 | 242 | for (int i =0; i < 128; i++) 243 | { 244 | this->dataWrite('C'); 245 | if (this->dataAvail(1000)) 246 | return receiveFrames(Crc); 247 | 248 | } 249 | for (int i =0; i < 128; i++) 250 | { 251 | this->dataWrite(XModem::NACK); 252 | if (this->dataAvail(1000)) 253 | return receiveFrames(ChkSum); 254 | } 255 | return false; 256 | } 257 | unsigned short XModem::crc16_ccitt(char *buf, int size) 258 | { 259 | unsigned short crc = 0; 260 | while (--size >= 0) { 261 | int i; 262 | crc ^= (unsigned short) *buf++ << 8; 263 | for (i = 0; i < 8; i++) 264 | if (crc & 0x8000) 265 | crc = crc << 1 ^ 0x1021; 266 | else 267 | crc <<= 1; 268 | } 269 | return crc; 270 | } 271 | unsigned char XModem::generateChkSum(const char *buf, int len) 272 | { 273 | //calculate chksum 274 | unsigned char chksum = 0; 275 | for(int i = 0; i< len; i++) { 276 | chksum += buf[i]; 277 | } 278 | return chksum; 279 | 280 | } 281 | 282 | bool XModem::transmitFrames(transfer_t transfer) 283 | { 284 | this->blockNo = 1; 285 | this->blockNoExt = 1; 286 | // use this only in unit tetsing 287 | //memset(this->buffer, 'A', 128); 288 | while(1) 289 | { 290 | //get data 291 | if (this->dataHandler != NULL) 292 | { 293 | if( false == 294 | this->dataHandler(this->blockNoExt, this->buffer+3, 295 | 128)) 296 | { 297 | //end of transfer 298 | this->dataWrite(XModem::EOT); 299 | //wait ACK 300 | if (this->dataRead(XModem::receiveDelay) == 301 | XModem::ACK) 302 | return true; 303 | else 304 | return false; 305 | 306 | } 307 | 308 | } 309 | else 310 | { 311 | //cancel transfer - send CAN twice 312 | this->dataWrite(XModem::CAN); 313 | this->dataWrite(XModem::CAN); 314 | //wait ACK 315 | if (this->dataRead(XModem::receiveDelay) == 316 | XModem::ACK) 317 | return true; 318 | else 319 | return false; 320 | } 321 | //SOH 322 | buffer[0] = XModem::SOH; 323 | //frame number 324 | buffer[1] = this->blockNo; 325 | //inv frame number 326 | buffer[2] = (unsigned char)(255-(this->blockNo)); 327 | //(data is already in buffer starting at byte 3) 328 | //checksum or crc 329 | if (transfer == ChkSum) { 330 | buffer[3+128] = this->generateChkSum(buffer+3, 128); 331 | this->sendData(buffer, 3+128+1); 332 | } else { 333 | unsigned short crc; 334 | crc = this->crc16_ccitt(this->buffer+3, 128); 335 | buffer[3+128+0] = (unsigned char)(crc >> 8); 336 | buffer[3+128+1] = (unsigned char)(crc);; 337 | this->sendData(buffer, 3+128+2); 338 | } 339 | 340 | //TO DO - wait NACK or CAN or ACK 341 | int ret = this->dataRead(XModem::receiveDelay); 342 | switch(ret) 343 | { 344 | case XModem::ACK: //data is ok - go to next chunk 345 | this->blockNo++; 346 | this->blockNoExt++; 347 | continue; 348 | case XModem::NACK: //resend data 349 | continue; 350 | case XModem::CAN: //abort transmision 351 | return false; 352 | 353 | } 354 | 355 | } 356 | return false; 357 | } 358 | bool XModem::transmit() 359 | { 360 | int retry = 0; 361 | int sym; 362 | this->init(); 363 | 364 | //wait for CRC transfer 365 | while(retry < 256) 366 | { 367 | if(this->dataAvail(1000)) 368 | { 369 | sym = this->dataRead(1); //data is here - no delay 370 | if(sym == 'C') 371 | return this->transmitFrames(Crc); 372 | if(sym == XModem::NACK) 373 | return this->transmitFrames(ChkSum); 374 | } 375 | retry++; 376 | } 377 | return false; 378 | } 379 | -------------------------------------------------------------------------------- /XModem.h: -------------------------------------------------------------------------------- 1 | // This code was taken from: https://code.google.com/archive/p/arduino-xmodem 2 | // (https://code.google.com/archive/p/arduino-xmodem) 3 | // which was released under GPL V3: 4 | // 5 | // This program is free software; you can redistribute it and/or modify 6 | // it under the terms of the GNU General Public License as published by 7 | // the Free Software Foundation; either version 3 of the License, or 8 | // (at your option) any later version. 9 | // 10 | // This program is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | // GNU General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU General Public License 16 | // along with this program; if not, write to the Free Software Foundation, 17 | // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | // ----------------------------------------------------------------------------- 19 | 20 | #ifndef XMODEM_H 21 | #define XMODEM_H 22 | 23 | typedef enum { 24 | Crc, 25 | ChkSum 26 | } transfer_t; 27 | 28 | 29 | class XModem { 30 | private: 31 | //delay when receive bytes in frame - 7 secs 32 | static const int receiveDelay; 33 | //retry limit when receiving 34 | static const int rcvRetryLimit; 35 | //holds readed byte (due to dataAvail()) 36 | int byte; 37 | //expected block number 38 | unsigned char blockNo; 39 | //extended block number, send to dataHandler() 40 | unsigned long blockNoExt; 41 | //retry counter for NACK 42 | int retries; 43 | //buffer 44 | char buffer[133]; 45 | //repeated block flag 46 | bool repeatedBlock; 47 | 48 | int (*recvChar)(int); 49 | void (*sendData)(const char *data, int len); 50 | bool (*dataHandler)(unsigned long number, char *buffer, int len); 51 | unsigned short crc16_ccitt(char *buf, int size); 52 | bool dataAvail(int delay); 53 | int dataRead(int delay); 54 | void dataWrite(char symbol); 55 | bool receiveFrameNo(void); 56 | bool receiveData(void); 57 | bool checkCrc(void); 58 | bool checkChkSum(void); 59 | bool receiveFrames(transfer_t transfer); 60 | bool sendNack(void); 61 | void init(void); 62 | 63 | bool transmitFrames(transfer_t); 64 | unsigned char generateChkSum(const char *buffer, int len); 65 | 66 | public: 67 | static const unsigned char NACK; 68 | static const unsigned char ACK; 69 | static const unsigned char SOH; 70 | static const unsigned char EOT; 71 | static const unsigned char CAN; 72 | 73 | XModem(int (*recvChar)(int), void (*sendData)(const char *data, int len)); 74 | XModem(int (*recvChar)(int), void (*sendData)(const char *data, int len), 75 | bool (*dataHandler)(unsigned long, char*, int)); 76 | bool receive(); 77 | bool transmit(); 78 | 79 | 80 | 81 | }; 82 | 83 | 84 | #endif 85 | -------------------------------------------------------------------------------- /diskio.cpp: -------------------------------------------------------------------------------- 1 | // ----------------------------------------------------------------------------- 2 | // 3.5"/5.25" DD/HD Disk controller for Arduino 3 | // Copyright (C) 2021 David Hansel 4 | // 5 | // This program is free software; you can redistribute it and/or modify 6 | // it under the terms of the GNU General Public License as published by 7 | // the Free Software Foundation; either version 3 of the License, or 8 | // (at your option) any later version. 9 | // 10 | // This program is distributed in the hope that it will be useful, 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | // GNU General Public License for more details. 14 | // 15 | // You should have received a copy of the GNU General Public License 16 | // along with this program; if not, write to the Free Software Foundation, 17 | // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 | // ----------------------------------------------------------------------------- 19 | 20 | #include "diskio.h" 21 | #include "ArduinoFDC.h" 22 | 23 | 24 | DSTATUS disk_status(BYTE pdrv) 25 | { 26 | // STA_NOINIT: Drive not initialized 27 | // STA_NODISK: No medium in the drive 28 | // STA_PROTECT: Write protected 29 | return ArduinoFDC.isWriteProtected() ? STA_PROTECT : 0; 30 | } 31 | 32 | 33 | DSTATUS disk_initialize(BYTE pdrv) 34 | { 35 | return ((ArduinoFDC.haveDisk() ? 0 : (STA_NODISK|STA_NOINIT)) | 36 | (ArduinoFDC.isWriteProtected() ? STA_PROTECT : 0)); 37 | } 38 | 39 | 40 | DRESULT disk_read(BYTE pdrv, BYTE *buf, DWORD sec, UINT count) 41 | { 42 | DRESULT res = RES_OK; 43 | 44 | byte numsec = ArduinoFDC.numSectors(); 45 | 46 | if( count!=1 || sec>2*numsec*ArduinoFDC.numTracks() ) 47 | res = RES_PARERR; 48 | else 49 | { 50 | byte head = 0; 51 | byte track = sec / (numsec*2); 52 | byte sector = sec % (numsec*2); 53 | if( sector >= numsec ) { head = 1; sector -= numsec; } 54 | 55 | byte r = S_NOHEADER, retry = 5; 56 | while( retry>0 && r!=S_OK ) { r = ArduinoFDC.readSector(track, head, sector+1, buf); retry--; } 57 | 58 | if( r==S_OK ) 59 | { 60 | memmove(buf, buf+1, 512); 61 | res = RES_OK; 62 | } 63 | else if( r==S_NOTREADY ) 64 | res = RES_NOTRDY; 65 | else 66 | res = RES_ERROR; 67 | } 68 | 69 | return res; 70 | } 71 | 72 | 73 | DRESULT disk_write(BYTE pdrv, BYTE *buf, DWORD sec, UINT count) 74 | { 75 | DRESULT res = RES_OK; 76 | byte numsec = ArduinoFDC.numSectors(); 77 | 78 | if( count!=1 || sec>2*numsec*ArduinoFDC.numTracks() ) 79 | res = RES_PARERR; 80 | else 81 | { 82 | byte head = 0; 83 | byte track = sec / (numsec*2); 84 | byte sector = sec % (numsec*2); 85 | 86 | if( sector >= numsec ) { head = 1; sector -= numsec; } 87 | 88 | memmove(buf+1, buf, 512); 89 | byte r = S_NOHEADER, retry = 3; 90 | while( retry>0 && r!=S_OK ) { r = ArduinoFDC.writeSector(track, head, sector+1, buf, true); retry--; } 91 | memmove(buf, buf+1, 512); 92 | 93 | if( r==S_OK ) 94 | res = RES_OK; 95 | else if( r==S_NOTREADY ) 96 | res = RES_NOTRDY; 97 | else if( r==S_READONLY ) 98 | res = RES_WRPRT; 99 | else 100 | res = RES_ERROR; 101 | } 102 | 103 | return res; 104 | } 105 | 106 | 107 | DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void* buff) 108 | { 109 | DRESULT res = RES_ERROR; 110 | 111 | switch( cmd ) 112 | { 113 | case CTRL_SYNC: 114 | res = RES_OK; 115 | break; 116 | 117 | case GET_SECTOR_COUNT: 118 | *((DWORD *) buff) = (DWORD) ArduinoFDC.numSectors() * (DWORD) ArduinoFDC.numTracks() * 2; 119 | res = RES_OK; 120 | break; 121 | 122 | case GET_SECTOR_SIZE: 123 | *((DWORD *) buff) = 512; 124 | break; 125 | 126 | case GET_BLOCK_SIZE: 127 | *((DWORD *) buff) = 1; 128 | break; 129 | } 130 | 131 | return res; 132 | } 133 | -------------------------------------------------------------------------------- /diskio.h: -------------------------------------------------------------------------------- 1 | /*-----------------------------------------------------------------------/ 2 | / Low level disk interface modlue include file (C)ChaN, 2014 / 3 | /-----------------------------------------------------------------------*/ 4 | 5 | #ifndef _DISKIO_DEFINED 6 | #define _DISKIO_DEFINED 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | #define _USE_WRITE 1 /* 1: Enable disk_write function */ 13 | #define _USE_IOCTL 1 /* 1: Enable disk_ioctl function */ 14 | 15 | #include "ff.h" 16 | 17 | 18 | /* Status of Disk Functions */ 19 | typedef BYTE DSTATUS; 20 | 21 | /* Results of Disk Functions */ 22 | typedef enum { 23 | RES_OK = 0, /* 0: Successful */ 24 | RES_ERROR, /* 1: R/W Error */ 25 | RES_WRPRT, /* 2: Write Protected */ 26 | RES_NOTRDY, /* 3: Not Ready */ 27 | RES_PARERR /* 4: Invalid Parameter */ 28 | } DRESULT; 29 | 30 | 31 | /*---------------------------------------*/ 32 | /* Prototypes for disk control functions */ 33 | 34 | 35 | DSTATUS disk_initialize (BYTE pdrv); 36 | DSTATUS disk_status (BYTE pdrv); 37 | DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count); 38 | DRESULT disk_write (BYTE pdrv, BYTE* buff, DWORD sector, UINT count); 39 | DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff); 40 | DWORD get_fattime (void); 41 | 42 | /* Disk Status Bits (DSTATUS) */ 43 | 44 | #define STA_NOINIT 0x01 /* Drive not initialized */ 45 | #define STA_NODISK 0x02 /* No medium in the drive */ 46 | #define STA_PROTECT 0x04 /* Write protected */ 47 | 48 | 49 | /* Command code for disk_ioctrl fucntion */ 50 | 51 | /* Generic command (Used by FatFs) */ 52 | #define CTRL_SYNC 0 /* Complete pending write process (needed at _FS_READONLY == 0) */ 53 | #define GET_SECTOR_COUNT 1 /* Get media size (needed at _USE_MKFS == 1) */ 54 | #define GET_SECTOR_SIZE 2 /* Get sector size (needed at _MAX_SS != _MIN_SS) */ 55 | #define GET_BLOCK_SIZE 3 /* Get erase block size (needed at _USE_MKFS == 1) */ 56 | #define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at _USE_TRIM == 1) */ 57 | 58 | /* Generic command (Not used by FatFs) */ 59 | #define CTRL_POWER 5 /* Get/Set power status */ 60 | #define CTRL_LOCK 6 /* Lock/Unlock media removal */ 61 | #define CTRL_EJECT 7 /* Eject media */ 62 | #define CTRL_FORMAT 8 /* Create physical format on the media */ 63 | 64 | /* MMC/SDC specific ioctl command */ 65 | #define MMC_GET_TYPE 10 /* Get card type */ 66 | #define MMC_GET_CSD 11 /* Get CSD */ 67 | #define MMC_GET_CID 12 /* Get CID */ 68 | #define MMC_GET_OCR 13 /* Get OCR */ 69 | #define MMC_GET_SDSTAT 14 /* Get SD status */ 70 | 71 | /* ATA/CF specific ioctl command */ 72 | #define ATA_GET_REV 20 /* Get F/W revision */ 73 | #define ATA_GET_MODEL 21 /* Get model name */ 74 | #define ATA_GET_SN 22 /* Get serial number */ 75 | 76 | #ifdef __cplusplus 77 | } 78 | #endif 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /ff.h: -------------------------------------------------------------------------------- 1 | /*----------------------------------------------------------------------------/ 2 | / FatFs - Generic FAT Filesystem module R0.14b / 3 | /-----------------------------------------------------------------------------/ 4 | / 5 | / Copyright (C) 2021, ChaN, all right reserved. 6 | / 7 | / FatFs module is an open source software. Redistribution and use of FatFs in 8 | / source and binary forms, with or without modification, are permitted provided 9 | / that the following condition is met: 10 | 11 | / 1. Redistributions of source code must retain the above copyright notice, 12 | / this condition and the following disclaimer. 13 | / 14 | / This software is provided by the copyright holder and contributors "AS IS" 15 | / and any warranties related to this software are DISCLAIMED. 16 | / The copyright owner or contributors be NOT LIABLE for any damages caused 17 | / by use of this software. 18 | / 19 | /----------------------------------------------------------------------------*/ 20 | 21 | 22 | #ifndef FF_DEFINED 23 | #define FF_DEFINED 86631 /* Revision ID */ 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | #include "ffconf.h" /* FatFs configuration options */ 30 | 31 | #if FF_DEFINED != FFCONF_DEF 32 | #error Wrong configuration file (ffconf.h). 33 | #endif 34 | 35 | 36 | /* Integer types used for FatFs API */ 37 | 38 | #if defined(_WIN32) /* Windows VC++ (for development only) */ 39 | #define FF_INTDEF 2 40 | #include 41 | typedef unsigned __int64 QWORD; 42 | #include 43 | #define isnan(v) _isnan(v) 44 | #define isinf(v) (!_finite(v)) 45 | 46 | #elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__cplusplus) /* C99 or later */ 47 | #define FF_INTDEF 2 48 | #include 49 | typedef unsigned int UINT; /* int must be 16-bit or 32-bit */ 50 | typedef unsigned char BYTE; /* char must be 8-bit */ 51 | typedef uint16_t WORD; /* 16-bit unsigned integer */ 52 | typedef uint32_t DWORD; /* 32-bit unsigned integer */ 53 | typedef uint64_t QWORD; /* 64-bit unsigned integer */ 54 | typedef WORD WCHAR; /* UTF-16 character type */ 55 | 56 | #else /* Earlier than C99 */ 57 | #define FF_INTDEF 1 58 | typedef unsigned int UINT; /* int must be 16-bit or 32-bit */ 59 | typedef unsigned char BYTE; /* char must be 8-bit */ 60 | typedef unsigned short WORD; /* 16-bit unsigned integer */ 61 | typedef unsigned long DWORD; /* 32-bit unsigned integer */ 62 | typedef WORD WCHAR; /* UTF-16 character type */ 63 | #endif 64 | 65 | 66 | /* Type of file size and LBA variables */ 67 | 68 | #if FF_FS_EXFAT 69 | #if FF_INTDEF != 2 70 | #error exFAT feature wants C99 or later 71 | #endif 72 | typedef QWORD FSIZE_t; 73 | #if FF_LBA64 74 | typedef QWORD LBA_t; 75 | #else 76 | typedef DWORD LBA_t; 77 | #endif 78 | #else 79 | #if FF_LBA64 80 | #error exFAT needs to be enabled when enable 64-bit LBA 81 | #endif 82 | typedef DWORD FSIZE_t; 83 | typedef DWORD LBA_t; 84 | #endif 85 | 86 | 87 | 88 | /* Type of path name strings on FatFs API (TCHAR) */ 89 | 90 | #if FF_USE_LFN && FF_LFN_UNICODE == 1 /* Unicode in UTF-16 encoding */ 91 | typedef WCHAR TCHAR; 92 | #define _T(x) L ## x 93 | #define _TEXT(x) L ## x 94 | #elif FF_USE_LFN && FF_LFN_UNICODE == 2 /* Unicode in UTF-8 encoding */ 95 | typedef char TCHAR; 96 | #define _T(x) u8 ## x 97 | #define _TEXT(x) u8 ## x 98 | #elif FF_USE_LFN && FF_LFN_UNICODE == 3 /* Unicode in UTF-32 encoding */ 99 | typedef DWORD TCHAR; 100 | #define _T(x) U ## x 101 | #define _TEXT(x) U ## x 102 | #elif FF_USE_LFN && (FF_LFN_UNICODE < 0 || FF_LFN_UNICODE > 3) 103 | #error Wrong FF_LFN_UNICODE setting 104 | #else /* ANSI/OEM code in SBCS/DBCS */ 105 | typedef char TCHAR; 106 | #define _T(x) x 107 | #define _TEXT(x) x 108 | #endif 109 | 110 | 111 | 112 | /* Definitions of volume management */ 113 | 114 | #if FF_MULTI_PARTITION /* Multiple partition configuration */ 115 | typedef struct { 116 | BYTE pd; /* Physical drive number */ 117 | BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */ 118 | } PARTITION; 119 | extern PARTITION VolToPart[]; /* Volume - Partition mapping table */ 120 | #endif 121 | 122 | #if FF_STR_VOLUME_ID 123 | #ifndef FF_VOLUME_STRS 124 | extern const char* VolumeStr[FF_VOLUMES]; /* User defied volume ID */ 125 | #endif 126 | #endif 127 | 128 | 129 | 130 | /* Filesystem object structure (FATFS) */ 131 | 132 | typedef struct { 133 | BYTE fs_type; /* Filesystem type (0:not mounted) */ 134 | BYTE pdrv; /* Associated physical drive */ 135 | BYTE n_fats; /* Number of FATs (1 or 2) */ 136 | BYTE wflag; /* win[] flag (b0:dirty) */ 137 | BYTE fsi_flag; /* FSINFO flags (b7:disabled, b0:dirty) */ 138 | WORD id; /* Volume mount ID */ 139 | WORD n_rootdir; /* Number of root directory entries (FAT12/16) */ 140 | WORD csize; /* Cluster size [sectors] */ 141 | #if FF_MAX_SS != FF_MIN_SS 142 | WORD ssize; /* Sector size (512, 1024, 2048 or 4096) */ 143 | #endif 144 | #if FF_USE_LFN 145 | WCHAR* lfnbuf; /* LFN working buffer */ 146 | #endif 147 | #if FF_FS_EXFAT 148 | BYTE* dirbuf; /* Directory entry block scratchpad buffer for exFAT */ 149 | #endif 150 | #if FF_FS_REENTRANT 151 | FF_SYNC_t sobj; /* Identifier of sync object */ 152 | #endif 153 | #if !FF_FS_READONLY 154 | DWORD last_clst; /* Last allocated cluster */ 155 | DWORD free_clst; /* Number of free clusters */ 156 | #endif 157 | #if FF_FS_RPATH 158 | DWORD cdir; /* Current directory start cluster (0:root) */ 159 | #if FF_FS_EXFAT 160 | DWORD cdc_scl; /* Containing directory start cluster (invalid when cdir is 0) */ 161 | DWORD cdc_size; /* b31-b8:Size of containing directory, b7-b0: Chain status */ 162 | DWORD cdc_ofs; /* Offset in the containing directory (invalid when cdir is 0) */ 163 | #endif 164 | #endif 165 | DWORD n_fatent; /* Number of FAT entries (number of clusters + 2) */ 166 | DWORD fsize; /* Size of an FAT [sectors] */ 167 | LBA_t volbase; /* Volume base sector */ 168 | LBA_t fatbase; /* FAT base sector */ 169 | LBA_t dirbase; /* Root directory base sector/cluster */ 170 | LBA_t database; /* Data base sector */ 171 | #if FF_FS_EXFAT 172 | LBA_t bitbase; /* Allocation bitmap base sector */ 173 | #endif 174 | LBA_t winsect; /* Current sector appearing in the win[] */ 175 | BYTE win[FF_MAX_SS+5]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */ 176 | } FATFS; 177 | 178 | 179 | 180 | /* Object ID and allocation information (FFOBJID) */ 181 | 182 | typedef struct { 183 | FATFS* fs; /* Pointer to the hosting volume of this object */ 184 | WORD id; /* Hosting volume mount ID */ 185 | BYTE attr; /* Object attribute */ 186 | BYTE stat; /* Object chain status (b1-0: =0:not contiguous, =2:contiguous, =3:fragmented in this session, b2:sub-directory stretched) */ 187 | DWORD sclust; /* Object data start cluster (0:no cluster or root directory) */ 188 | FSIZE_t objsize; /* Object size (valid when sclust != 0) */ 189 | #if FF_FS_EXFAT 190 | DWORD n_cont; /* Size of first fragment - 1 (valid when stat == 3) */ 191 | DWORD n_frag; /* Size of last fragment needs to be written to FAT (valid when not zero) */ 192 | DWORD c_scl; /* Containing directory start cluster (valid when sclust != 0) */ 193 | DWORD c_size; /* b31-b8:Size of containing directory, b7-b0: Chain status (valid when c_scl != 0) */ 194 | DWORD c_ofs; /* Offset in the containing directory (valid when file object and sclust != 0) */ 195 | #endif 196 | #if FF_FS_LOCK 197 | UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */ 198 | #endif 199 | } FFOBJID; 200 | 201 | 202 | 203 | /* File object structure (FIL) */ 204 | 205 | typedef struct { 206 | FFOBJID obj; /* Object identifier (must be the 1st member to detect invalid object pointer) */ 207 | BYTE flag; /* File status flags */ 208 | BYTE err; /* Abort flag (error code) */ 209 | FSIZE_t fptr; /* File read/write pointer (Zeroed on file open) */ 210 | DWORD clust; /* Current cluster of fpter (invalid when fptr is 0) */ 211 | LBA_t sect; /* Sector number appearing in buf[] (0:invalid) */ 212 | #if !FF_FS_READONLY 213 | LBA_t dir_sect; /* Sector number containing the directory entry (not used at exFAT) */ 214 | BYTE* dir_ptr; /* Pointer to the directory entry in the win[] (not used at exFAT) */ 215 | #endif 216 | #if FF_USE_FASTSEEK 217 | DWORD* cltbl; /* Pointer to the cluster link map table (nulled on open, set by application) */ 218 | #endif 219 | #if !FF_FS_TINY 220 | BYTE buf[FF_MAX_SS+5]; /* File private data read/write window */ 221 | #endif 222 | } FIL; 223 | 224 | 225 | 226 | /* Directory object structure (DIR) */ 227 | 228 | typedef struct { 229 | FFOBJID obj; /* Object identifier */ 230 | DWORD dptr; /* Current read/write offset */ 231 | DWORD clust; /* Current cluster */ 232 | LBA_t sect; /* Current sector (0:Read operation has terminated) */ 233 | BYTE* dir; /* Pointer to the directory item in the win[] */ 234 | BYTE fn[12]; /* SFN (in/out) {body[8],ext[3],status[1]} */ 235 | #if FF_USE_LFN 236 | DWORD blk_ofs; /* Offset of current entry block being processed (0xFFFFFFFF:Invalid) */ 237 | #endif 238 | #if FF_USE_FIND 239 | const TCHAR* pat; /* Pointer to the name matching pattern */ 240 | #endif 241 | } DIR; 242 | 243 | 244 | 245 | /* File information structure (FILINFO) */ 246 | 247 | typedef struct { 248 | FSIZE_t fsize; /* File size */ 249 | WORD fdate; /* Modified date */ 250 | WORD ftime; /* Modified time */ 251 | BYTE fattrib; /* File attribute */ 252 | #if FF_USE_LFN 253 | TCHAR altname[FF_SFN_BUF + 1];/* Altenative file name */ 254 | TCHAR fname[FF_LFN_BUF + 1]; /* Primary file name */ 255 | #else 256 | TCHAR fname[12 + 1]; /* File name */ 257 | #endif 258 | } FILINFO; 259 | 260 | 261 | 262 | /* Format parameter structure (MKFS_PARM) */ 263 | 264 | typedef struct { 265 | BYTE fmt; /* Format option (FM_FAT, FM_FAT32, FM_EXFAT and FM_SFD) */ 266 | BYTE n_fat; /* Number of FATs */ 267 | UINT align; /* Data area alignment (sector) */ 268 | UINT n_root; /* Number of root directory entries */ 269 | DWORD au_size; /* Cluster size (byte) */ 270 | BYTE media; /* media descriptor */ 271 | WORD n_sec_track; /* number of sectors per track */ 272 | WORD n_heads; /* number of heads */ 273 | } MKFS_PARM; 274 | 275 | 276 | 277 | /* File function return code (FRESULT) */ 278 | 279 | typedef enum { 280 | FR_OK = 0, /* (0) Succeeded */ 281 | FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */ 282 | FR_INT_ERR, /* (2) Assertion failed */ 283 | FR_NOT_READY, /* (3) The physical drive cannot work */ 284 | FR_NO_FILE, /* (4) Could not find the file */ 285 | FR_NO_PATH, /* (5) Could not find the path */ 286 | FR_INVALID_NAME, /* (6) The path name format is invalid */ 287 | FR_DENIED, /* (7) Access denied due to prohibited access or directory full */ 288 | FR_EXIST, /* (8) Access denied due to prohibited access */ 289 | FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */ 290 | FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */ 291 | FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */ 292 | FR_NOT_ENABLED, /* (12) The volume has no work area */ 293 | FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */ 294 | FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */ 295 | FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */ 296 | FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */ 297 | FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */ 298 | FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */ 299 | FR_INVALID_PARAMETER /* (19) Given parameter is invalid */ 300 | } FRESULT; 301 | 302 | 303 | 304 | /*--------------------------------------------------------------*/ 305 | /* FatFs module application interface */ 306 | 307 | FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */ 308 | FRESULT f_close (FIL* fp); /* Close an open file object */ 309 | FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from the file */ 310 | FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to the file */ 311 | FRESULT f_lseek (FIL* fp, FSIZE_t ofs); /* Move file pointer of the file object */ 312 | FRESULT f_truncate (FIL* fp); /* Truncate the file */ 313 | FRESULT f_sync (FIL* fp); /* Flush cached data of the writing file */ 314 | FRESULT f_opendir (DIR* dp, const TCHAR* path); /* Open a directory */ 315 | FRESULT f_closedir (DIR* dp); /* Close an open directory */ 316 | FRESULT f_readdir (DIR* dp, FILINFO* fno); /* Read a directory item */ 317 | FRESULT f_findfirst (DIR* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern); /* Find first file */ 318 | FRESULT f_findnext (DIR* dp, FILINFO* fno); /* Find next file */ 319 | FRESULT f_mkdir (const TCHAR* path); /* Create a sub directory */ 320 | FRESULT f_unlink (const TCHAR* path); /* Delete an existing file or directory */ 321 | FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */ 322 | FRESULT f_stat (const TCHAR* path, FILINFO* fno); /* Get file status */ 323 | FRESULT f_chmod (const TCHAR* path, BYTE attr, BYTE mask); /* Change attribute of a file/dir */ 324 | FRESULT f_utime (const TCHAR* path, const FILINFO* fno); /* Change timestamp of a file/dir */ 325 | FRESULT f_chdir (const TCHAR* path); /* Change current directory */ 326 | FRESULT f_chdrive (const TCHAR* path); /* Change current drive */ 327 | FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */ 328 | FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */ 329 | FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */ 330 | FRESULT f_setlabel (const TCHAR* label); /* Set volume label */ 331 | FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */ 332 | FRESULT f_expand (FIL* fp, FSIZE_t fsz, BYTE opt); /* Allocate a contiguous block to the file */ 333 | FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */ 334 | FRESULT f_mkfs (const TCHAR* path, const MKFS_PARM* opt, void* work, UINT len); /* Create a FAT volume */ 335 | FRESULT f_fdisk (BYTE pdrv, const LBA_t ptbl[], void* work); /* Divide a physical drive into some partitions */ 336 | FRESULT f_setcp (WORD cp); /* Set current code page */ 337 | int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */ 338 | int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */ 339 | int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */ 340 | TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */ 341 | 342 | #define f_eof(fp) ((int)((fp)->fptr == (fp)->obj.objsize)) 343 | #define f_error(fp) ((fp)->err) 344 | #define f_tell(fp) ((fp)->fptr) 345 | #define f_size(fp) ((fp)->obj.objsize) 346 | #define f_rewind(fp) f_lseek((fp), 0) 347 | #define f_rewinddir(dp) f_readdir((dp), 0) 348 | #define f_rmdir(path) f_unlink(path) 349 | #define f_unmount(path) f_mount(0, path, 0) 350 | 351 | 352 | 353 | 354 | /*--------------------------------------------------------------*/ 355 | /* Additional user defined functions */ 356 | 357 | /* RTC function */ 358 | #if !FF_FS_READONLY && !FF_FS_NORTC 359 | DWORD get_fattime (void); 360 | #endif 361 | 362 | /* LFN support functions */ 363 | #if FF_USE_LFN >= 1 /* Code conversion (defined in unicode.c) */ 364 | WCHAR ff_oem2uni (WCHAR oem, WORD cp); /* OEM code to Unicode conversion */ 365 | WCHAR ff_uni2oem (DWORD uni, WORD cp); /* Unicode to OEM code conversion */ 366 | DWORD ff_wtoupper (DWORD uni); /* Unicode upper-case conversion */ 367 | #endif 368 | #if FF_USE_LFN == 3 /* Dynamic memory allocation */ 369 | void* ff_memalloc (UINT msize); /* Allocate memory block */ 370 | void ff_memfree (void* mblock); /* Free memory block */ 371 | #endif 372 | 373 | /* Sync functions */ 374 | #if FF_FS_REENTRANT 375 | int ff_cre_syncobj (BYTE vol, FF_SYNC_t* sobj); /* Create a sync object */ 376 | int ff_req_grant (FF_SYNC_t sobj); /* Lock sync object */ 377 | void ff_rel_grant (FF_SYNC_t sobj); /* Unlock sync object */ 378 | int ff_del_syncobj (FF_SYNC_t sobj); /* Delete a sync object */ 379 | #endif 380 | 381 | 382 | 383 | 384 | /*--------------------------------------------------------------*/ 385 | /* Flags and offset address */ 386 | 387 | 388 | /* File access mode and open method flags (3rd argument of f_open) */ 389 | #define FA_READ 0x01 390 | #define FA_WRITE 0x02 391 | #define FA_OPEN_EXISTING 0x00 392 | #define FA_CREATE_NEW 0x04 393 | #define FA_CREATE_ALWAYS 0x08 394 | #define FA_OPEN_ALWAYS 0x10 395 | #define FA_OPEN_APPEND 0x30 396 | 397 | /* Fast seek controls (2nd argument of f_lseek) */ 398 | #define CREATE_LINKMAP ((FSIZE_t)0 - 1) 399 | 400 | /* Format options (2nd argument of f_mkfs) */ 401 | #define FM_FAT 0x01 402 | #define FM_FAT32 0x02 403 | #define FM_EXFAT 0x04 404 | #define FM_ANY 0x07 405 | #define FM_SFD 0x08 406 | 407 | /* Filesystem type (FATFS.fs_type) */ 408 | #define FS_FAT12 1 409 | #define FS_FAT16 2 410 | #define FS_FAT32 3 411 | #define FS_EXFAT 4 412 | 413 | /* File attribute bits for directory entry (FILINFO.fattrib) */ 414 | #define AM_RDO 0x01 /* Read only */ 415 | #define AM_HID 0x02 /* Hidden */ 416 | #define AM_SYS 0x04 /* System */ 417 | #define AM_DIR 0x10 /* Directory */ 418 | #define AM_ARC 0x20 /* Archive */ 419 | 420 | 421 | #ifdef __cplusplus 422 | } 423 | #endif 424 | 425 | #endif /* FF_DEFINED */ 426 | -------------------------------------------------------------------------------- /ffconf.h: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------/ 2 | / FatFs Functional Configurations 3 | /---------------------------------------------------------------------------*/ 4 | 5 | #define FFCONF_DEF 86631 /* Revision ID */ 6 | 7 | /*---------------------------------------------------------------------------/ 8 | / Function Configurations 9 | /---------------------------------------------------------------------------*/ 10 | 11 | #define FF_FS_READONLY 0 12 | /* This option switches read-only configuration. (0:Read/Write or 1:Read-only) 13 | / Read-only configuration removes writing API functions, f_write(), f_sync(), 14 | / f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree() 15 | / and optional writing functions as well. */ 16 | 17 | 18 | #define FF_FS_MINIMIZE 0 19 | /* This option defines minimization level to remove some basic API functions. 20 | / 21 | / 0: Basic functions are fully enabled. 22 | / 1: f_stat(), f_getfree(), f_unlink(), f_mkdir(), f_truncate() and f_rename() 23 | / are removed. 24 | / 2: f_opendir(), f_readdir() and f_closedir() are removed in addition to 1. 25 | / 3: f_lseek() function is removed in addition to 2. */ 26 | 27 | 28 | #define FF_USE_FIND 0 29 | /* This option switches filtered directory read functions, f_findfirst() and 30 | / f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */ 31 | 32 | 33 | #define FF_USE_MKFS 1 34 | /* This option switches f_mkfs() function. (0:Disable or 1:Enable) */ 35 | 36 | 37 | #define FF_USE_FASTSEEK 0 38 | /* This option switches fast seek function. (0:Disable or 1:Enable) */ 39 | 40 | 41 | #define FF_USE_EXPAND 0 42 | /* This option switches f_expand function. (0:Disable or 1:Enable) */ 43 | 44 | 45 | #define FF_USE_CHMOD 0 46 | /* This option switches attribute manipulation functions, f_chmod() and f_utime(). 47 | / (0:Disable or 1:Enable) Also FF_FS_READONLY needs to be 0 to enable this option. */ 48 | 49 | 50 | #define FF_USE_LABEL 0 51 | /* This option switches volume label functions, f_getlabel() and f_setlabel(). 52 | / (0:Disable or 1:Enable) */ 53 | 54 | 55 | #define FF_USE_FORWARD 0 56 | /* This option switches f_forward() function. (0:Disable or 1:Enable) */ 57 | 58 | 59 | #define FF_USE_STRFUNC 0 60 | #define FF_PRINT_LLI 0 61 | #define FF_PRINT_FLOAT 0 62 | #define FF_STRF_ENCODE 0 63 | /* FF_USE_STRFUNC switches string functions, f_gets(), f_putc(), f_puts() and 64 | / f_printf(). 65 | / 66 | / 0: Disable. FF_PRINT_LLI, FF_PRINT_FLOAT and FF_STRF_ENCODE have no effect. 67 | / 1: Enable without LF-CRLF conversion. 68 | / 2: Enable with LF-CRLF conversion. 69 | / 70 | / FF_PRINT_LLI = 1 makes f_printf() support long long argument and FF_PRINT_FLOAT = 1/2 71 | makes f_printf() support floating point argument. These features want C99 or later. 72 | / When FF_LFN_UNICODE >= 1 with LFN enabled, string functions convert the character 73 | / encoding in it. FF_STRF_ENCODE selects assumption of character encoding ON THE FILE 74 | / to be read/written via those functions. 75 | / 76 | / 0: ANSI/OEM in current CP 77 | / 1: Unicode in UTF-16LE 78 | / 2: Unicode in UTF-16BE 79 | / 3: Unicode in UTF-8 80 | */ 81 | 82 | 83 | /*---------------------------------------------------------------------------/ 84 | / Locale and Namespace Configurations 85 | /---------------------------------------------------------------------------*/ 86 | 87 | #define FF_CODE_PAGE -1 88 | /* This option specifies the OEM code page to be used on the target system. 89 | / Incorrect code page setting can cause a file open failure. 90 | / 91 | / -1 - do not use code pages (ASCII only) 92 | / 437 - U.S. 93 | / 720 - Arabic 94 | / 737 - Greek 95 | / 771 - KBL 96 | / 775 - Baltic 97 | / 850 - Latin 1 98 | / 852 - Latin 2 99 | / 855 - Cyrillic 100 | / 857 - Turkish 101 | / 860 - Portuguese 102 | / 861 - Icelandic 103 | / 862 - Hebrew 104 | / 863 - Canadian French 105 | / 864 - Arabic 106 | / 865 - Nordic 107 | / 866 - Russian 108 | / 869 - Greek 2 109 | / 932 - Japanese (DBCS) 110 | / 936 - Simplified Chinese (DBCS) 111 | / 949 - Korean (DBCS) 112 | / 950 - Traditional Chinese (DBCS) 113 | / 0 - Include all code pages above and configured by f_setcp() 114 | */ 115 | 116 | 117 | #define FF_USE_LFN 0 118 | #define FF_MAX_LFN 255 119 | /* The FF_USE_LFN switches the support for LFN (long file name). 120 | / 121 | / 0: Disable LFN. FF_MAX_LFN has no effect. 122 | / 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe. 123 | / 2: Enable LFN with dynamic working buffer on the STACK. 124 | / 3: Enable LFN with dynamic working buffer on the HEAP. 125 | / 126 | / To enable the LFN, ffunicode.c needs to be added to the project. The LFN function 127 | / requiers certain internal working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and 128 | / additional (FF_MAX_LFN + 44) / 15 * 32 bytes when exFAT is enabled. 129 | / The FF_MAX_LFN defines size of the working buffer in UTF-16 code unit and it can 130 | / be in range of 12 to 255. It is recommended to be set it 255 to fully support LFN 131 | / specification. 132 | / When use stack for the working buffer, take care on stack overflow. When use heap 133 | / memory for the working buffer, memory management functions, ff_memalloc() and 134 | / ff_memfree() exemplified in ffsystem.c, need to be added to the project. */ 135 | 136 | 137 | #define FF_LFN_UNICODE 0 138 | /* This option switches the character encoding on the API when LFN is enabled. 139 | / 140 | / 0: ANSI/OEM in current CP (TCHAR = char) 141 | / 1: Unicode in UTF-16 (TCHAR = WCHAR) 142 | / 2: Unicode in UTF-8 (TCHAR = char) 143 | / 3: Unicode in UTF-32 (TCHAR = DWORD) 144 | / 145 | / Also behavior of string I/O functions will be affected by this option. 146 | / When LFN is not enabled, this option has no effect. */ 147 | 148 | 149 | #define FF_LFN_BUF 255 150 | #define FF_SFN_BUF 12 151 | /* This set of options defines size of file name members in the FILINFO structure 152 | / which is used to read out directory items. These values should be suffcient for 153 | / the file names to read. The maximum possible length of the read file name depends 154 | / on character encoding. When LFN is not enabled, these options have no effect. */ 155 | 156 | 157 | #define FF_FS_RPATH 0 158 | /* This option configures support for relative path. 159 | / 160 | / 0: Disable relative path and remove related functions. 161 | / 1: Enable relative path. f_chdir() and f_chdrive() are available. 162 | / 2: f_getcwd() function is available in addition to 1. 163 | */ 164 | 165 | 166 | /*---------------------------------------------------------------------------/ 167 | / Drive/Volume Configurations 168 | /---------------------------------------------------------------------------*/ 169 | 170 | #define FF_VOLUMES 1 171 | /* Number of volumes (logical drives) to be used. (1-10) */ 172 | 173 | 174 | #define FF_STR_VOLUME_ID 0 175 | #define FF_VOLUME_STRS "RAM","NAND","CF","SD","SD2","USB","USB2","USB3" 176 | /* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings. 177 | / When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive 178 | / number in the path name. FF_VOLUME_STRS defines the volume ID strings for each 179 | / logical drives. Number of items must not be less than FF_VOLUMES. Valid 180 | / characters for the volume ID strings are A-Z, a-z and 0-9, however, they are 181 | / compared in case-insensitive. If FF_STR_VOLUME_ID >= 1 and FF_VOLUME_STRS is 182 | / not defined, a user defined volume string table needs to be defined as: 183 | / 184 | / const char* VolumeStr[FF_VOLUMES] = {"ram","flash","sd","usb",... 185 | */ 186 | 187 | 188 | #define FF_MULTI_PARTITION 0 189 | /* This option switches support for multiple volumes on the physical drive. 190 | / By default (0), each logical drive number is bound to the same physical drive 191 | / number and only an FAT volume found on the physical drive will be mounted. 192 | / When this function is enabled (1), each logical drive number can be bound to 193 | / arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk() 194 | / funciton will be available. */ 195 | 196 | 197 | #define FF_MIN_SS 512 198 | #define FF_MAX_SS 512 199 | /* This set of options configures the range of sector size to be supported. (512, 200 | / 1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and 201 | / harddisk, but a larger value may be required for on-board flash memory and some 202 | / type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is configured 203 | / for variable sector size mode and disk_ioctl() function needs to implement 204 | / GET_SECTOR_SIZE command. */ 205 | 206 | 207 | #define FF_LBA64 0 208 | /* This option switches support for 64-bit LBA. (0:Disable or 1:Enable) 209 | / To enable the 64-bit LBA, also exFAT needs to be enabled. (FF_FS_EXFAT == 1) */ 210 | 211 | 212 | #define FF_MIN_GPT 0x10000000 213 | /* Minimum number of sectors to switch GPT as partitioning format in f_mkfs and 214 | / f_fdisk function. 0x100000000 max. This option has no effect when FF_LBA64 == 0. */ 215 | 216 | 217 | #define FF_USE_TRIM 0 218 | /* This option switches support for ATA-TRIM. (0:Disable or 1:Enable) 219 | / To enable Trim function, also CTRL_TRIM command should be implemented to the 220 | / disk_ioctl() function. */ 221 | 222 | 223 | 224 | /*---------------------------------------------------------------------------/ 225 | / System Configurations 226 | /---------------------------------------------------------------------------*/ 227 | 228 | #define FF_FS_TINY 0 229 | /* This option switches tiny buffer configuration. (0:Normal or 1:Tiny) 230 | / At the tiny configuration, size of file object (FIL) is shrinked FF_MAX_SS bytes. 231 | / Instead of private sector buffer eliminated from the file object, common sector 232 | / buffer in the filesystem object (FATFS) is used for the file data transfer. */ 233 | 234 | 235 | #define FF_FS_EXFAT 0 236 | /* This option switches support for exFAT filesystem. (0:Disable or 1:Enable) 237 | / To enable exFAT, also LFN needs to be enabled. (FF_USE_LFN >= 1) 238 | / Note that enabling exFAT discards ANSI C (C89) compatibility. */ 239 | 240 | 241 | #define FF_FS_NORTC 1 242 | #define FF_NORTC_MON 1 243 | #define FF_NORTC_MDAY 1 244 | #define FF_NORTC_YEAR 2020 245 | /* The option FF_FS_NORTC switches timestamp functiton. If the system does not have 246 | / any RTC function or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable 247 | / the timestamp function. Every object modified by FatFs will have a fixed timestamp 248 | / defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR in local time. 249 | / To enable timestamp function (FF_FS_NORTC = 0), get_fattime() function need to be 250 | / added to the project to read current time form real-time clock. FF_NORTC_MON, 251 | / FF_NORTC_MDAY and FF_NORTC_YEAR have no effect. 252 | / These options have no effect in read-only configuration (FF_FS_READONLY = 1). */ 253 | 254 | 255 | #define FF_FS_NOFSINFO 0 256 | /* If you need to know correct free space on the FAT32 volume, set bit 0 of this 257 | / option, and f_getfree() function at first time after volume mount will force 258 | / a full FAT scan. Bit 1 controls the use of last allocated cluster number. 259 | / 260 | / bit0=0: Use free cluster count in the FSINFO if available. 261 | / bit0=1: Do not trust free cluster count in the FSINFO. 262 | / bit1=0: Use last allocated cluster number in the FSINFO if available. 263 | / bit1=1: Do not trust last allocated cluster number in the FSINFO. 264 | */ 265 | 266 | 267 | #define FF_FS_LOCK 0 268 | /* The option FF_FS_LOCK switches file lock function to control duplicated file open 269 | / and illegal operation to open objects. This option must be 0 when FF_FS_READONLY 270 | / is 1. 271 | / 272 | / 0: Disable file lock function. To avoid volume corruption, application program 273 | / should avoid illegal open, remove and rename to the open objects. 274 | / >0: Enable file lock function. The value defines how many files/sub-directories 275 | / can be opened simultaneously under file lock control. Note that the file 276 | / lock control is independent of re-entrancy. */ 277 | 278 | 279 | /* #include // O/S definitions */ 280 | #define FF_FS_REENTRANT 0 281 | #define FF_FS_TIMEOUT 1000 282 | #define FF_SYNC_t HANDLE 283 | /* The option FF_FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs 284 | / module itself. Note that regardless of this option, file access to different 285 | / volume is always re-entrant and volume control functions, f_mount(), f_mkfs() 286 | / and f_fdisk() function, are always not re-entrant. Only file/directory access 287 | / to the same volume is under control of this function. 288 | / 289 | / 0: Disable re-entrancy. FF_FS_TIMEOUT and FF_SYNC_t have no effect. 290 | / 1: Enable re-entrancy. Also user provided synchronization handlers, 291 | / ff_req_grant(), ff_rel_grant(), ff_del_syncobj() and ff_cre_syncobj() 292 | / function, must be added to the project. Samples are available in 293 | / option/syscall.c. 294 | / 295 | / The FF_FS_TIMEOUT defines timeout period in unit of time tick. 296 | / The FF_SYNC_t defines O/S dependent sync object type. e.g. HANDLE, ID, OS_EVENT*, 297 | / SemaphoreHandle_t and etc. A header file for O/S definitions needs to be 298 | / included somewhere in the scope of ff.h. */ 299 | 300 | 301 | 302 | /*--- End of configuration options ---*/ 303 | -------------------------------------------------------------------------------- /images/ArduDOS.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhansel/ArduinoFDC/c376c44166c2abf6bcc02500afa78697afab289f/images/ArduDOS.png -------------------------------------------------------------------------------- /images/CutTrace.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhansel/ArduinoFDC/c376c44166c2abf6bcc02500afa78697afab289f/images/CutTrace.jpg -------------------------------------------------------------------------------- /images/ShieldAssembled.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhansel/ArduinoFDC/c376c44166c2abf6bcc02500afa78697afab289f/images/ShieldAssembled.jpg -------------------------------------------------------------------------------- /images/Shields.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhansel/ArduinoFDC/c376c44166c2abf6bcc02500afa78697afab289f/images/Shields.jpg -------------------------------------------------------------------------------- /images/setup.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhansel/ArduinoFDC/c376c44166c2abf6bcc02500afa78697afab289f/images/setup.jpg --------------------------------------------------------------------------------