Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Allow Host Access any Marlin attached SD #19939

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Marlin/Configuration_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,15 @@
*/
//#define NO_SD_HOST_DRIVE // Disable SD Card access over USB (for security).

/**
* By default the framework is responsible for the shared media I/O.
* Enable this if you need Marlin to take care of the shared media I/O.
* Useful if shared media isn't working properly on some boards.
*/
#if ENABLED(SDSUPPORT) && DISABLED(NO_SD_HOST_DRIVE)
//#define DISKIO_HOST_DRIVE
#endif

/**
* Additional options for Graphical Displays
*
Expand Down
130 changes: 130 additions & 0 deletions Marlin/src/HAL/LPC1768/diskio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2022 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

#ifdef TARGET_LPC1768

#include "../../inc/MarlinConfig.h"

#if BOTH(SDSUPPORT, DISKIO_HOST_DRIVE)

// Overwrite DiskIO functions from LPC framework to use Marlin Sd2Card API.
#include <chanfs/diskio.h>
#include "../../sd/cardreader.h"

DRESULT disk_read (
BYTE drv, // Physical drive number (0)
BYTE *buff, // Pointer to the data buffer to store read data
DWORD sector, // Start sector number (LBA)
UINT count // Number of sectors to read (1..128)
) {
auto sd2card = card.getSd2Card();
if (count == 1) {
sd2card.readBlock(sector, buff);
return RES_OK;
}

sd2card.readStart(sector);
while (count--) {
sd2card.readData(buff);
buff += 512;
}
sd2card.readStop();
return RES_OK;
}

DSTATUS disk_status(BYTE drv) { // Physical drive number (0)
return 0;
}

DSTATUS disk_initialize(BYTE drv) { // Physical drive number (0)
// If already mounted, it's already initialized!
if (!card.isMounted()) {
auto sd2card = card.getSd2Card();
if (!sd2card.init(SPI_SPEED, SDSS)
#if defined(LCD_SDSS) && (LCD_SDSS != SDSS)
&& !sd2card.init(SPI_SPEED, LCD_SDSS)
#endif
) {
return RES_ERROR;
}
}

return RES_OK;
}

#if _DISKIO_WRITE

DRESULT disk_write(
BYTE drv, // Physical drive number (0)
const BYTE *buff, // Ponter to the data to write
DWORD sector, // Start sector number (LBA)
UINT count // Number of sectors to write (1..128)
) {
auto sd2card = card.getSd2Card();
if (count == 1) {
sd2card.writeBlock(sector, buff);
return RES_OK;
}

sd2card.writeStart(sector, count);
while (count--) {
sd2card.writeData(buff);
buff += 512;
}
sd2card.writeStop();
return RES_OK;
}

#endif // _DISKIO_WRITE

#if _DISKIO_IOCTL

DRESULT disk_ioctl(
BYTE drv, // Physical drive number (0)
BYTE cmd, // Control command code
void *buff // Pointer to the conrtol data
) {
DWORD *dp, st, ed;

auto sd2card = card.getSd2Card();
switch (cmd) {
case CTRL_SYNC: // Wait for end of internal write process of the drive
break;
case GET_SECTOR_COUNT: // Get drive capacity in unit of sector (DWORD)
*(int32_t*)buff = sd2card.cardSize();
break;
case GET_BLOCK_SIZE: // Get erase block size in unit of sector (DWORD)
break;
case CTRL_TRIM: // Erase a block of sectors (used when _USE_TRIM in ffconf.h is 1)
dp = (DWORD*)buff; st = dp[0]; ed = dp[1]; // Load sector block
sd2card.erase(st, ed);
break;
}

return RES_OK;
}

#endif // _DISKIO_IOCTL

#endif // SDSUPPORT && DISKIO_HOST_DRIVE

#endif // TARGET_LPC1768
19 changes: 19 additions & 0 deletions Marlin/src/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,25 @@ static_assert(Y_MAX_LENGTH >= Y_BED_SIZE, "Movement bounds (Y_MIN_POS, Y_MAX_POS
#endif
#endif

#ifndef TARGET_LPC1768
#if ANY( \
ENDSTOPPULLDOWNS, \
ENDSTOPPULLDOWN_XMIN, ENDSTOPPULLDOWN_XMAX, \
ENDSTOPPULLDOWN_YMIN, ENDSTOPPULLDOWN_YMAX, \
ENDSTOPPULLDOWN_ZMIN, ENDSTOPPULLDOWN_ZMAX, \
ENDSTOPPULLDOWN_IMIN, ENDSTOPPULLDOWN_IMAX, \
ENDSTOPPULLDOWN_JMIN, ENDSTOPPULLDOWN_JMAX, \
ENDSTOPPULLDOWN_KMIN, ENDSTOPPULLDOWN_KMAX, \
ENDSTOPPULLDOWN_UMIN, ENDSTOPPULLDOWN_UMAX, \
ENDSTOPPULLDOWN_VMIN, ENDSTOPPULLDOWN_VMAX, \
ENDSTOPPULLDOWN_WMIN, ENDSTOPPULLDOWN_WMAX \
)
#error "PULLDOWN pin mode is not available on the selected board."
#elif BOTH(SDSUPPORT, DISKIO_HOST_DRIVE)
#error "DISKIO_HOST_DRIVE is not available on the selected board."
#endif
#endif

#if BOTH(ENDSTOPPULLUPS, ENDSTOPPULLDOWNS)
#error "Enable only one of ENDSTOPPULLUPS or ENDSTOPPULLDOWNS."
#elif BOTH(FIL_RUNOUT_PULLUP, FIL_RUNOUT_PULLDOWN)
Expand Down