From d372e7e4779ebd233d9f0a6dc3853fc1010c8b51 Mon Sep 17 00:00:00 2001 From: jmz52 Date: Fri, 4 Jan 2019 22:38:07 +0300 Subject: [PATCH] SDIO support for STM32F1 (#12782) --- .../src/HAL/HAL_STM32F1/HAL_sdio_STM32F1.cpp | 267 ++++++++++++++++++ Marlin/src/HAL/HAL_STM32F1/HAL_sdio_STM32F1.h | 152 ++++++++++ Marlin/src/HAL/HAL_STM32F1/SanityCheck.h | 4 + .../config/examples/Mks/Robin/Configuration.h | 24 +- .../examples/Mks/Robin/Configuration_adv.h | 2 +- Marlin/src/pins/pins_MKS_ROBIN.h | 1 + Marlin/src/sd/Sd2Card.cpp | 2 +- Marlin/src/sd/Sd2Card_sdio.h | 39 +++ Marlin/src/sd/SdVolume.h | 2 + 9 files changed, 480 insertions(+), 13 deletions(-) create mode 100644 Marlin/src/HAL/HAL_STM32F1/HAL_sdio_STM32F1.cpp create mode 100644 Marlin/src/HAL/HAL_STM32F1/HAL_sdio_STM32F1.h create mode 100644 Marlin/src/sd/Sd2Card_sdio.h diff --git a/Marlin/src/HAL/HAL_STM32F1/HAL_sdio_STM32F1.cpp b/Marlin/src/HAL/HAL_STM32F1/HAL_sdio_STM32F1.cpp new file mode 100644 index 000000000..372aa26e5 --- /dev/null +++ b/Marlin/src/HAL/HAL_STM32F1/HAL_sdio_STM32F1.cpp @@ -0,0 +1,267 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * Copyright (C) 2017 Victor Perez + * + * 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 . + * + */ + +#ifdef __STM32F1__ + +#include "HAL_sdio_STM32F1.h" + +SDIO_CardInfoTypeDef SdCard; + +bool SDIO_Init(void) { + uint32_t count = 0U; + SdCard.CardType = SdCard.CardVersion = SdCard.Class = SdCard.RelCardAdd = SdCard.BlockNbr = SdCard.BlockSize = SdCard.LogBlockNbr = SdCard.LogBlockSize = 0; + + sdio_begin(); + sdio_set_dbus_width(SDIO_CLKCR_WIDBUS_1BIT); + + dma_init(SDIO_DMA_DEV); + dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); + dma_set_priority(SDIO_DMA_DEV, SDIO_DMA_CHANNEL, DMA_PRIORITY_VERY_HIGH); + + if (!SDIO_CmdGoIdleState()) return false; + if (!SDIO_CmdGoIdleState()) return false; /* Hotplugged cards tends to miss first CMD0, so give them a second chance. */ + + SdCard.CardVersion = SDIO_CmdOperCond() ? CARD_V2_X : CARD_V1_X; + + do { + if (count++ == SDMMC_MAX_VOLT_TRIAL) return false; + SDIO_CmdAppOperCommand(SdCard.CardVersion == CARD_V2_X ? SDMMC_HIGH_CAPACITY : SDMMC_STD_CAPACITY); + } while ((SDIO_GetResponse(SDIO_RESP1) & 0x80000000) == 0); + + SdCard.CardType = (SDIO_GetResponse(SDIO_RESP1) & SDMMC_HIGH_CAPACITY) ? CARD_SDHC_SDXC : CARD_SDSC; + + if (!SDIO_CmdSendCID()) return false; + if (!SDIO_CmdSetRelAdd(&SdCard.RelCardAdd)) return false; /* Send CMD3 SET_REL_ADDR with argument 0. SD Card publishes its RCA. */ + if (!SDIO_CmdSendCSD(SdCard.RelCardAdd << 16U)) return false; + + SdCard.Class = (SDIO_GetResponse(SDIO_RESP2) >> 20U); + + if (SdCard.CardType == CARD_SDHC_SDXC) { + SdCard.LogBlockNbr = SdCard.BlockNbr = (((SDIO_GetResponse(SDIO_RESP2) & 0x0000003FU) << 26U) | ((SDIO_GetResponse(SDIO_RESP3) & 0xFFFF0000U) >> 6U)) + 1024; + SdCard.LogBlockSize = SdCard.BlockSize = 512U; + } + else { + SdCard.BlockNbr = ((((SDIO_GetResponse(SDIO_RESP2) & 0x000003FFU) << 2U ) | ((SDIO_GetResponse(SDIO_RESP3) & 0xC0000000U) >> 30U)) + 1U) * (4U << ((SDIO_GetResponse(SDIO_RESP3) & 0x00038000U) >> 15U)); + SdCard.BlockSize = 1U << ((SDIO_GetResponse(SDIO_RESP2) >> 16) & 0x0FU); + SdCard.LogBlockNbr = (SdCard.BlockNbr) * ((SdCard.BlockSize) / 512U); + SdCard.LogBlockSize = 512U; + } + + if (!SDIO_CmdSelDesel(SdCard.RelCardAdd << 16U)) return false; + if (!SDIO_CmdAppSetClearCardDetect(SdCard.RelCardAdd << 16U)) return false; + if (!SDIO_CmdAppSetBusWidth(SdCard.RelCardAdd << 16U, 2)) return false; + + sdio_set_dbus_width(SDIO_CLKCR_WIDBUS_4BIT); + sdio_set_clock(SDIO_CLOCK); + return true; +} + +bool SDIO_ReadBlock(uint32_t blockAddress, uint8_t *data) { + if (SDIO_GetCardState() != SDIO_CARD_TRANSFER) return false; + if (blockAddress >= SdCard.LogBlockNbr) return false; + if ((0x03 & (uint32_t)data)) return false; // misaligned data + + if (SdCard.CardType != CARD_SDHC_SDXC) { blockAddress *= 512U; } + + dma_setup_transfer(SDIO_DMA_DEV, SDIO_DMA_CHANNEL, &SDIO->FIFO, DMA_SIZE_32BITS, data, DMA_SIZE_32BITS, DMA_MINC_MODE); + dma_set_num_transfers(SDIO_DMA_DEV, SDIO_DMA_CHANNEL, 128); + dma_clear_isr_bits(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); + dma_enable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); + + sdio_setup_transfer(SDIO_DATA_TIMEOUT * (F_CPU / 1000U), 512, SDIO_BLOCKSIZE_512 | SDIO_DCTRL_DMAEN | SDIO_DCTRL_DTEN | SDIO_DIR_RX); + + if (!SDIO_CmdReadSingleBlock(blockAddress)) { + SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS); + dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); + return false; + } + + while (!SDIO_GET_FLAG(SDIO_STA_DATAEND | SDIO_STA_TRX_ERROR_FLAGS)) {} + + dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); + + if (SDIO_GET_FLAG(SDIO_STA_TRX_ERROR_FLAGS)) { + SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS); + return false; + } + SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS); + return true; +} + +bool SDIO_WriteBlock(uint32_t blockAddress, const uint8_t *data) { + if (SDIO_GetCardState() != SDIO_CARD_TRANSFER) return false; + if (blockAddress >= SdCard.LogBlockNbr) return false; + if ((0x03 & (uint32_t)data)) return false; // misaligned data + + if (SdCard.CardType != CARD_SDHC_SDXC) { blockAddress *= 512U; } + + dma_setup_transfer(SDIO_DMA_DEV, SDIO_DMA_CHANNEL, &SDIO->FIFO, DMA_SIZE_32BITS, (volatile void *) data, DMA_SIZE_32BITS, DMA_MINC_MODE | DMA_FROM_MEM); + dma_set_num_transfers(SDIO_DMA_DEV, SDIO_DMA_CHANNEL, 128); + dma_clear_isr_bits(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); + dma_enable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); + + if (!SDIO_CmdWriteSingleBlock(blockAddress)) { + dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); + return false; + } + + sdio_setup_transfer(SDIO_DATA_TIMEOUT * (F_CPU / 1000U), 512U, SDIO_BLOCKSIZE_512 | SDIO_DCTRL_DMAEN | SDIO_DCTRL_DTEN); + + while (!SDIO_GET_FLAG(SDIO_STA_DATAEND | SDIO_STA_TRX_ERROR_FLAGS)) {} + + dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL); + + if (SDIO_GET_FLAG(SDIO_STA_TRX_ERROR_FLAGS)) { + SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS); + return false; + } + + SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS); + + uint32 timeout = millis() + SDIO_WRITE_TIMEOUT; + while (timeout > millis()) { + if (SDIO_GetCardState() == SDIO_CARD_TRANSFER) { + return true; + } + } + return false; +} + +inline uint32_t SDIO_GetCardState(void) { return SDIO_CmdSendStatus(SdCard.RelCardAdd << 16U) ? (SDIO_GetResponse(SDIO_RESP1) >> 9U) & 0x0FU : SDIO_CARD_ERROR; } + +// -------------------------------------------------------------------------- +// SD Commands and Responses +// -------------------------------------------------------------------------- + +void SDIO_SendCommand(uint16_t command, uint32_t argument) { SDIO->ARG = argument; SDIO->CMD = (uint32_t)(SDIO_CMD_CPSMEN | command); } +uint8_t SDIO_GetCommandResponse(void) { return (uint8_t)(SDIO->RESPCMD); } +uint32_t SDIO_GetResponse(uint32_t response) { return SDIO->RESP[response]; } + +bool SDIO_CmdGoIdleState(void) { SDIO_SendCommand(CMD0_GO_IDLE_STATE, 0); return SDIO_GetCmdError(); } +bool SDIO_CmdSendCID(void) { SDIO_SendCommand(CMD2_ALL_SEND_CID, 0); return SDIO_GetCmdResp2(); } +bool SDIO_CmdSetRelAdd(uint32_t *rca) { SDIO_SendCommand(CMD3_SET_REL_ADDR, 0); return SDIO_GetCmdResp6(SDMMC_CMD_SET_REL_ADDR, rca); } +bool SDIO_CmdSelDesel(uint32_t address) { SDIO_SendCommand(CMD7_SEL_DESEL_CARD, address); return SDIO_GetCmdResp1(SDMMC_CMD_SEL_DESEL_CARD); } +bool SDIO_CmdOperCond(void) { SDIO_SendCommand(CMD8_HS_SEND_EXT_CSD, SDMMC_CHECK_PATTERN); return SDIO_GetCmdResp7(); } +bool SDIO_CmdSendCSD(uint32_t argument) { SDIO_SendCommand(CMD9_SEND_CSD, argument); return SDIO_GetCmdResp2(); } +bool SDIO_CmdSendStatus(uint32_t argument) { SDIO_SendCommand(CMD13_SEND_STATUS, argument); return SDIO_GetCmdResp1(SDMMC_CMD_SEND_STATUS); } +bool SDIO_CmdReadSingleBlock(uint32_t address) { SDIO_SendCommand(CMD17_READ_SINGLE_BLOCK, address); return SDIO_GetCmdResp1(SDMMC_CMD_READ_SINGLE_BLOCK); } +bool SDIO_CmdWriteSingleBlock(uint32_t address) { SDIO_SendCommand(CMD24_WRITE_SINGLE_BLOCK, address); return SDIO_GetCmdResp1(SDMMC_CMD_WRITE_SINGLE_BLOCK); } +bool SDIO_CmdAppCommand(uint32_t rsa) { SDIO_SendCommand(CMD55_APP_CMD, rsa); return SDIO_GetCmdResp1(SDMMC_CMD_APP_CMD); } + +bool SDIO_CmdAppSetBusWidth(uint32_t rsa, uint32_t argument) { + if (!SDIO_CmdAppCommand(rsa)) return false; + SDIO_SendCommand(ACMD6_APP_SD_SET_BUSWIDTH, argument); + return SDIO_GetCmdResp2(); +} + +bool SDIO_CmdAppOperCommand(uint32_t sdType) { + if (!SDIO_CmdAppCommand(0)) return false; + SDIO_SendCommand(ACMD41_SD_APP_OP_COND , SDMMC_VOLTAGE_WINDOW_SD | sdType); + return SDIO_GetCmdResp3(); +} + +bool SDIO_CmdAppSetClearCardDetect(uint32_t rsa) { + if (!SDIO_CmdAppCommand(rsa)) return false; + SDIO_SendCommand(ACMD42_SD_APP_SET_CLR_CARD_DETECT, 0); + return SDIO_GetCmdResp2(); +} + +// Wait until given flags are unset or till timeout +#define SDIO_WAIT(FLAGS) do{ \ + uint32_t count = 1 + (SDIO_CMDTIMEOUT) * ((F_CPU) / 8U / 1000U); \ + do { if (!--count) return false; } while (!SDIO_GET_FLAG(FLAGS)); \ +}while(0) + +bool SDIO_GetCmdError(void) { + SDIO_WAIT(SDIO_STA_CMDSENT); + + SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS); + return true; +} + +bool SDIO_GetCmdResp1(uint8_t command) { + SDIO_WAIT(SDIO_STA_CCRCFAIL | SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT); + + if (SDIO_GET_FLAG(SDIO_STA_CCRCFAIL | SDIO_STA_CTIMEOUT)) { + SDIO_CLEAR_FLAG(SDIO_STA_CCRCFAIL | SDIO_STA_CTIMEOUT); + return false; + } + if (SDIO_GetCommandResponse() != command) return false; + + SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS); + return (SDIO_GetResponse(SDIO_RESP1) & SDMMC_OCR_ERRORBITS) == SDMMC_ALLZERO; +} + +bool SDIO_GetCmdResp2(void) { + SDIO_WAIT(SDIO_STA_CCRCFAIL | SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT); + + if (SDIO_GET_FLAG(SDIO_STA_CCRCFAIL | SDIO_STA_CTIMEOUT)) { + SDIO_CLEAR_FLAG(SDIO_STA_CCRCFAIL | SDIO_STA_CTIMEOUT); + return false; + } + + SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS); + return true; +} + +bool SDIO_GetCmdResp3(void) { + SDIO_WAIT(SDIO_STA_CCRCFAIL | SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT); + + if (SDIO_GET_FLAG(SDIO_STA_CTIMEOUT)) { + SDIO_CLEAR_FLAG(SDIO_STA_CTIMEOUT); + return false; + } + + SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS); + return true; +} + +bool SDIO_GetCmdResp6(uint8_t command, uint32_t *rca) { + SDIO_WAIT(SDIO_STA_CCRCFAIL | SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT); + + if (SDIO_GET_FLAG(SDIO_STA_CCRCFAIL | SDIO_STA_CTIMEOUT)) { + SDIO_CLEAR_FLAG(SDIO_STA_CCRCFAIL | SDIO_STA_CTIMEOUT); + return false; + } + if (SDIO_GetCommandResponse() != command) return false; + + SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS); + if (SDIO_GetResponse(SDIO_RESP1) & (SDMMC_R6_GENERAL_UNKNOWN_ERROR | SDMMC_R6_ILLEGAL_CMD | SDMMC_R6_COM_CRC_FAILED)) return false; + + *rca = SDIO_GetResponse(SDIO_RESP1) >> 16; + return true; +} + +bool SDIO_GetCmdResp7(void) { + SDIO_WAIT(SDIO_STA_CCRCFAIL | SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT); + + if (SDIO_GET_FLAG(SDIO_STA_CTIMEOUT)) { + SDIO_CLEAR_FLAG(SDIO_STA_CTIMEOUT); + return false; + } + + if (SDIO_GET_FLAG(SDIO_STA_CMDREND)) { SDIO_CLEAR_FLAG(SDIO_STA_CMDREND); } + return true; +} + +#endif // __STM32F1__ diff --git a/Marlin/src/HAL/HAL_STM32F1/HAL_sdio_STM32F1.h b/Marlin/src/HAL/HAL_STM32F1/HAL_sdio_STM32F1.h new file mode 100644 index 000000000..034bd6d88 --- /dev/null +++ b/Marlin/src/HAL/HAL_STM32F1/HAL_sdio_STM32F1.h @@ -0,0 +1,152 @@ +/** + * Marlin 3D Printer Firmware + * + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com + * Copyright (c) 2017 Victor Perez + * + * 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 . + * + */ +#pragma once + +// -------------------------------------------------------------------------- +// Includes +// -------------------------------------------------------------------------- + +#include "Arduino.h" +#include "libmaple/sdio.h" +#include "libmaple/dma.h" + +// -------------------------------------------------------------------------- +// Defines +// -------------------------------------------------------------------------- + +#define SDMMC_CMD_GO_IDLE_STATE ((uint8_t)0) /* Resets the SD memory card. */ +#define SDMMC_CMD_ALL_SEND_CID ((uint8_t)2) /* Asks any card connected to the host to send the CID numbers on the CMD line. */ +#define SDMMC_CMD_SET_REL_ADDR ((uint8_t)3) /* Asks the card to publish a new relative address (RCA). */ +#define SDMMC_CMD_SEL_DESEL_CARD ((uint8_t)7) /* Selects the card by its own relative address and gets deselected by any other address */ +#define SDMMC_CMD_HS_SEND_EXT_CSD ((uint8_t)8) /* Sends SD Memory Card interface condition, which includes host supply voltage information and asks the card whether card supports voltage. */ +#define SDMMC_CMD_SEND_CSD ((uint8_t)9) /* Addressed card sends its card specific data (CSD) on the CMD line. */ +#define SDMMC_CMD_SEND_STATUS ((uint8_t)13) /*!< Addressed card sends its status register. */ +#define SDMMC_CMD_READ_SINGLE_BLOCK ((uint8_t)17) /* Reads single block of size selected by SET_BLOCKLEN in case of SDSC, and a block of fixed 512 bytes in case of SDHC and SDXC. */ +#define SDMMC_CMD_WRITE_SINGLE_BLOCK ((uint8_t)24) /* Writes single block of size selected by SET_BLOCKLEN in case of SDSC, and a block of fixed 512 bytes in case of SDHC and SDXC. */ +#define SDMMC_CMD_APP_CMD ((uint8_t)55) /* Indicates to the card that the next command is an application specific command rather than a standard command. */ + +#define SDMMC_ACMD_APP_SD_SET_BUSWIDTH ((uint8_t)6) /* (ACMD6) Defines the data bus width to be used for data transfer. The allowed data bus widths are given in SCR register. */ +#define SDMMC_ACMD_SD_APP_OP_COND ((uint8_t)41) /* (ACMD41) Sends host capacity support information (HCS) and asks the accessed card to send its operating condition register (OCR) content in the response on the CMD line. */ +#define SDMMC_ACMD_SD_APP_SET_CLR_CARD_DETECT ((uint8_t)42) /* (ACMD42) Connect/Disconnect the 50 KOhm pull-up resistor on CD/DAT3 (pin 1) of the card */ + +#define CMD0_GO_IDLE_STATE (uint16_t)(SDMMC_CMD_GO_IDLE_STATE | SDIO_CMD_WAIT_NO_RESP) +#define CMD2_ALL_SEND_CID (uint16_t)(SDMMC_CMD_ALL_SEND_CID | SDIO_CMD_WAIT_LONG_RESP) +#define CMD3_SET_REL_ADDR (uint16_t)(SDMMC_CMD_SET_REL_ADDR | SDIO_CMD_WAIT_SHORT_RESP) +#define CMD7_SEL_DESEL_CARD (uint16_t)(SDMMC_CMD_SEL_DESEL_CARD | SDIO_CMD_WAIT_SHORT_RESP) +#define CMD8_HS_SEND_EXT_CSD (uint16_t)(SDMMC_CMD_HS_SEND_EXT_CSD | SDIO_CMD_WAIT_SHORT_RESP) +#define CMD9_SEND_CSD (uint16_t)(SDMMC_CMD_SEND_CSD | SDIO_CMD_WAIT_LONG_RESP) +#define CMD13_SEND_STATUS (uint16_t)(SDMMC_CMD_SEND_STATUS | SDIO_CMD_WAIT_SHORT_RESP) +#define CMD17_READ_SINGLE_BLOCK (uint16_t)(SDMMC_CMD_READ_SINGLE_BLOCK | SDIO_CMD_WAIT_SHORT_RESP) +#define CMD24_WRITE_SINGLE_BLOCK (uint16_t)(SDMMC_CMD_WRITE_SINGLE_BLOCK | SDIO_CMD_WAIT_SHORT_RESP) +#define CMD55_APP_CMD (uint16_t)(SDMMC_CMD_APP_CMD | SDIO_CMD_WAIT_SHORT_RESP) + +#define ACMD6_APP_SD_SET_BUSWIDTH (uint16_t)(SDMMC_ACMD_APP_SD_SET_BUSWIDTH | SDIO_CMD_WAIT_SHORT_RESP) +#define ACMD41_SD_APP_OP_COND (uint16_t)(SDMMC_ACMD_SD_APP_OP_COND | SDIO_CMD_WAIT_SHORT_RESP) +#define ACMD42_SD_APP_SET_CLR_CARD_DETECT (uint16_t)(SDMMC_ACMD_SD_APP_SET_CLR_CARD_DETECT | SDIO_CMD_WAIT_SHORT_RESP) + + +#define SDMMC_ALLZERO 0x00000000U +#define SDMMC_OCR_ERRORBITS 0xFDFFE008U + +#define SDMMC_R6_GENERAL_UNKNOWN_ERROR 0x00002000U +#define SDMMC_R6_ILLEGAL_CMD 0x00004000U +#define SDMMC_R6_COM_CRC_FAILED 0x00008000U + +#define SDMMC_VOLTAGE_WINDOW_SD 0x80100000U +#define SDMMC_HIGH_CAPACITY 0x40000000U +#define SDMMC_STD_CAPACITY 0x00000000U +#define SDMMC_CHECK_PATTERN 0x000001AAU + +#define SDIO_TRANSFER_MODE_BLOCK 0x00000000U +#define SDIO_DPSM_ENABLE 0x00000001U +#define SDIO_TRANSFER_DIR_TO_CARD 0x00000000U +#define SDIO_DATABLOCK_SIZE_512B 0x00000090U +#define SDIO_TRANSFER_DIR_TO_SDIO 0x00000100U +#define SDIO_DMA_ENABLE 0x00001000U + +#define CARD_V1_X 0x00000000U +#define CARD_V2_X 0x00000001U +#define CARD_SDSC 0x00000000U +#define CARD_SDHC_SDXC 0x00000001U + +#define SDIO_RESP1 0 +#define SDIO_RESP2 1 +#define SDIO_RESP3 2 +#define SDIO_RESP4 3 + +#define SDIO_GET_FLAG(__FLAG__) !!((SDIO->STA) & (__FLAG__)) +#define SDIO_CLEAR_FLAG(__FLAG__) (SDIO->ICR = (__FLAG__)) + +#define SDMMC_MAX_VOLT_TRIAL 0x00000FFFU +#define SDIO_CARD_TRANSFER 0x00000004U /* Card is in transfer state */ +#define SDIO_CARD_ERROR 0x000000FFU /* Card response Error */ +#define SDIO_CMDTIMEOUT 200U /* Command send and response timeout */ +#define SDIO_DATA_TIMEOUT 100U /* Read data transfer timeout */ +#define SDIO_WRITE_TIMEOUT 200U /* Write data transfer timeout */ + +#define SDIO_CLOCK 18000000 /* 18 MHz */ + +// -------------------------------------------------------------------------- +// Types +// -------------------------------------------------------------------------- + +typedef struct { + uint32_t CardType; // Card Type + uint32_t CardVersion; // Card version + uint32_t Class; // Class of the card class + uint32_t RelCardAdd; // Relative Card Address + uint32_t BlockNbr; // Card Capacity in blocks + uint32_t BlockSize; // One block size in bytes + uint32_t LogBlockNbr; // Card logical Capacity in blocks + uint32_t LogBlockSize; // Logical block size in bytes +} SDIO_CardInfoTypeDef; + +// -------------------------------------------------------------------------- +// Public functions +// -------------------------------------------------------------------------- + +inline uint32_t SDIO_GetCardState(void); + +bool SDIO_CmdGoIdleState(void); +bool SDIO_CmdSendCID(void); +bool SDIO_CmdSetRelAdd(uint32_t *rca); +bool SDIO_CmdSelDesel(uint32_t address); +bool SDIO_CmdOperCond(void); +bool SDIO_CmdSendCSD(uint32_t argument); +bool SDIO_CmdSendStatus(uint32_t argument); +bool SDIO_CmdReadSingleBlock(uint32_t address); +bool SDIO_CmdWriteSingleBlock(uint32_t address); +bool SDIO_CmdAppCommand(uint32_t rsa); + +bool SDIO_CmdAppSetBusWidth(uint32_t rsa, uint32_t argument); +bool SDIO_CmdAppOperCommand(uint32_t sdType); +bool SDIO_CmdAppSetClearCardDetect(uint32_t rsa); + +void SDIO_SendCommand(uint16_t command, uint32_t argument); +uint8_t SDIO_GetCommandResponse(void); +uint32_t SDIO_GetResponse(uint32_t response); +bool SDIO_GetCmdError(void); +bool SDIO_GetCmdResp1(uint8_t command); +bool SDIO_GetCmdResp2(void); +bool SDIO_GetCmdResp3(void); +bool SDIO_GetCmdResp6(uint8_t command, uint32_t *rca); +bool SDIO_GetCmdResp7(void); diff --git a/Marlin/src/HAL/HAL_STM32F1/SanityCheck.h b/Marlin/src/HAL/HAL_STM32F1/SanityCheck.h index 40917f03f..dcd72440d 100644 --- a/Marlin/src/HAL/HAL_STM32F1/SanityCheck.h +++ b/Marlin/src/HAL/HAL_STM32F1/SanityCheck.h @@ -70,3 +70,7 @@ #if ENABLED(EMERGENCY_PARSER) #error "EMERGENCY_PARSER is not yet implemented for STM32F1. Disable EMERGENCY_PARSER to continue." #endif + +#if ENABLED(SDIO_SUPPORT) && DISABLED(SDSUPPORT) + #error "SDIO_SUPPORT requires SDSUPPORT. Enable SDSUPPORT to continue." +#endif diff --git a/Marlin/src/config/examples/Mks/Robin/Configuration.h b/Marlin/src/config/examples/Mks/Robin/Configuration.h index 6f722714c..d981f821a 100644 --- a/Marlin/src/config/examples/Mks/Robin/Configuration.h +++ b/Marlin/src/config/examples/Mks/Robin/Configuration.h @@ -104,7 +104,7 @@ * * :[-1, 0, 1, 2, 3, 4, 5, 6, 7] */ -#define SERIAL_PORT 0 +#define SERIAL_PORT 3 /** * Select a secondary serial port on the board to use for communication with the host. @@ -113,7 +113,8 @@ * * :[-1, 0, 1, 2, 3, 4, 5, 6, 7] */ -//#define SERIAL_PORT_2 -1 +#define SERIAL_PORT_2 1 +#define NUM_SERIAL 2 /** * This setting determines the communication speed of the printer. @@ -132,12 +133,12 @@ // The following define selects which electronics board you have. // Please choose the name from boards.h that matches your setup #ifndef MOTHERBOARD - #define MOTHERBOARD BOARD_RAMPS_14_EFB + #define MOTHERBOARD BOARD_MKS_ROBIN #endif // Optional custom name for your RepStrap or other custom machine // Displayed in the LCD "Ready" message -//#define CUSTOM_MACHINE_NAME "3D Printer" +#define CUSTOM_MACHINE_NAME "MKS Robin" // Define this to set a unique identifier for this printer, (Used by some programs to differentiate between machines) // You can use an online service to generate a random UUID. (eg http://www.uuidgenerator.net/version4) @@ -147,10 +148,10 @@ // This defines the number of extruders // :[1, 2, 3, 4, 5, 6] -#define EXTRUDERS 1 +#define EXTRUDERS 2 // Generally expected filament diameter (1.75, 2.85, 3.0, ...). Used for Volumetric, Filament Width Sensor, etc. -#define DEFAULT_NOMINAL_FILAMENT_DIA 3.0 +#define DEFAULT_NOMINAL_FILAMENT_DIA 1.75 // For Cyclops or any "multi-extruder" that shares a single nozzle. //#define SINGLENOZZLE @@ -328,12 +329,12 @@ * :{ '0': "Not used", '1':"100k / 4.7k - EPCOS", '2':"200k / 4.7k - ATC Semitec 204GT-2", '3':"Mendel-parts / 4.7k", '4':"10k !! do not use for a hotend. Bad resolution at high temp. !!", '5':"100K / 4.7k - ATC Semitec 104GT-2 (Used in ParCan & J-Head)", '501':"100K Zonestar (Tronxy X3A)", '6':"100k / 4.7k EPCOS - Not as accurate as Table 1", '7':"100k / 4.7k Honeywell 135-104LAG-J01", '8':"100k / 4.7k 0603 SMD Vishay NTCS0603E3104FXT", '9':"100k / 4.7k GE Sensing AL03006-58.2K-97-G1", '10':"100k / 4.7k RS 198-961", '11':"100k / 4.7k beta 3950 1%", '12':"100k / 4.7k 0603 SMD Vishay NTCS0603E3104FXT (calibrated for Makibox hot bed)", '13':"100k Hisens 3950 1% up to 300°C for hotend 'Simple ONE ' & hotend 'All In ONE'", '20':"PT100 (Ultimainboard V2.x)", '51':"100k / 1k - EPCOS", '52':"200k / 1k - ATC Semitec 204GT-2", '55':"100k / 1k - ATC Semitec 104GT-2 (Used in ParCan & J-Head)", '60':"100k Maker's Tool Works Kapton Bed Thermistor beta=3950", '61':"100k Formbot / Vivedino 3950 350C thermistor 4.7k pullup", '66':"Dyze Design 4.7M High Temperature thermistor", '70':"the 100K thermistor found in the bq Hephestos 2", '71':"100k / 4.7k Honeywell 135-104LAF-J01", '147':"Pt100 / 4.7k", '1047':"Pt1000 / 4.7k", '110':"Pt100 / 1k (non-standard)", '1010':"Pt1000 / 1k (non standard)", '-4':"Thermocouple + AD8495", '-3':"Thermocouple + MAX31855 (only for sensor 0)", '-2':"Thermocouple + MAX6675 (only for sensor 0)", '-1':"Thermocouple + AD595",'998':"Dummy 1", '999':"Dummy 2" } */ #define TEMP_SENSOR_0 1 -#define TEMP_SENSOR_1 0 +#define TEMP_SENSOR_1 1 #define TEMP_SENSOR_2 0 #define TEMP_SENSOR_3 0 #define TEMP_SENSOR_4 0 #define TEMP_SENSOR_5 0 -#define TEMP_SENSOR_BED 0 +#define TEMP_SENSOR_BED 1 #define TEMP_SENSOR_CHAMBER 0 // Dummy thermistor constant temperature readings, for use with 998 and 999 @@ -433,7 +434,7 @@ * heater. If your configuration is significantly different than this and you don't understand * the issues involved, don't use bed PID until someone else verifies that your hardware works. */ -//#define PIDTEMPBED +#define PIDTEMPBED //#define BED_LIMIT_SWITCHING @@ -1507,7 +1508,8 @@ * you must uncomment the following option or it won't work. * */ -//#define SDSUPPORT +#define SDSUPPORT +#define SDIO_SUPPORT /** * SD CARD: SPI SPEED @@ -1880,7 +1882,7 @@ // // MKS Robin 320x240 color display // -//#define MKS_ROBIN_TFT +#define MKS_ROBIN_TFT //============================================================================= //============================ Other Controllers ============================ diff --git a/Marlin/src/config/examples/Mks/Robin/Configuration_adv.h b/Marlin/src/config/examples/Mks/Robin/Configuration_adv.h index d3a8b7e13..ae1b1681e 100644 --- a/Marlin/src/config/examples/Mks/Robin/Configuration_adv.h +++ b/Marlin/src/config/examples/Mks/Robin/Configuration_adv.h @@ -646,7 +646,7 @@ // as SD_DETECT_PIN in your board's pins definitions. // This setting should be disabled unless you are using a push button, pulling the pin to ground. // Note: This is always disabled for ULTIPANEL (except ELB_FULL_GRAPHIC_CONTROLLER). - #define SD_DETECT_INVERTED + //#define SD_DETECT_INVERTED #define SD_FINISHED_STEPPERRELEASE true // Disable steppers when SD Print is finished #define SD_FINISHED_RELEASECOMMAND "M84 X Y Z E" // You might want to keep the Z enabled so your bed stays in place. diff --git a/Marlin/src/pins/pins_MKS_ROBIN.h b/Marlin/src/pins/pins_MKS_ROBIN.h index 7c3d96d16..e71f37453 100644 --- a/Marlin/src/pins/pins_MKS_ROBIN.h +++ b/Marlin/src/pins/pins_MKS_ROBIN.h @@ -121,4 +121,5 @@ #define FSMC_CS_PIN PG12 // NE4 #define FSMC_RS_PIN PF0 // A0 +#define SD_DETECT_PIN PF12 #define SDSS -1 diff --git a/Marlin/src/sd/Sd2Card.cpp b/Marlin/src/sd/Sd2Card.cpp index 0f959a3cc..6b5acf6d3 100644 --- a/Marlin/src/sd/Sd2Card.cpp +++ b/Marlin/src/sd/Sd2Card.cpp @@ -30,7 +30,7 @@ #include "../inc/MarlinConfig.h" -#if ENABLED(SDSUPPORT) && DISABLED(USB_FLASH_DRIVE_SUPPORT) +#if ENABLED(SDSUPPORT) && DISABLED(USB_FLASH_DRIVE_SUPPORT) && DISABLED(SDIO_SUPPORT) /* Enable FAST CRC computations - You can trade speed for FLASH space if * needed by disabling the following define */ diff --git a/Marlin/src/sd/Sd2Card_sdio.h b/Marlin/src/sd/Sd2Card_sdio.h new file mode 100644 index 000000000..23c6796c1 --- /dev/null +++ b/Marlin/src/sd/Sd2Card_sdio.h @@ -0,0 +1,39 @@ +/** + * Marlin 3D Printer Firmware + * Copyright (C) 2016 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 . + * + */ +#pragma once + +#include "../inc/MarlinConfig.h" + +#if ENABLED(SDIO_SUPPORT) + +bool SDIO_Init(void); +bool SDIO_ReadBlock(uint32_t block, uint8_t *dst); +bool SDIO_WriteBlock(uint32_t block, const uint8_t *src); + +class Sd2Card { + public: + bool init(uint8_t sckRateID = 0, uint8_t chipSelectPin = 0) { return SDIO_Init(); } + bool readBlock(uint32_t block, uint8_t *dst) { return SDIO_ReadBlock(block, dst); } + bool writeBlock(uint32_t block, const uint8_t *src) { return SDIO_WriteBlock(block, src); } +}; + +#endif // SDIO_SUPPORT diff --git a/Marlin/src/sd/SdVolume.h b/Marlin/src/sd/SdVolume.h index 0f3839362..5a7cb31f1 100644 --- a/Marlin/src/sd/SdVolume.h +++ b/Marlin/src/sd/SdVolume.h @@ -35,6 +35,8 @@ #if ENABLED(USB_FLASH_DRIVE_SUPPORT) #include "usb_flashdrive/Sd2Card_FlashDrive.h" +#elif ENABLED(SDIO_SUPPORT) + #include "Sd2Card_sdio.h" #else #include "Sd2Card.h" #endif