Prusa MINI Firmware overview
sdio.h
Go to the documentation of this file.
1 /**
2  * Marlin 3D Printer Firmware
3  *
4  * Copyright (c) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
5  * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
6  * Copyright (c) 2017 Victor Perez
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 #pragma once
23 
24 #include "../shared/Marduino.h"
25 
26 #include <libmaple/sdio.h>
27 #include <libmaple/dma.h>
28 
29 // ------------------------
30 // Defines
31 // ------------------------
32 
33 #define SDMMC_CMD_GO_IDLE_STATE ((uint8_t)0) /* Resets the SD memory card. */
34 #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. */
35 #define SDMMC_CMD_SET_REL_ADDR ((uint8_t)3) /* Asks the card to publish a new relative address (RCA). */
36 #define SDMMC_CMD_SEL_DESEL_CARD ((uint8_t)7) /* Selects the card by its own relative address and gets deselected by any other address */
37 #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. */
38 #define SDMMC_CMD_SEND_CSD ((uint8_t)9) /* Addressed card sends its card specific data (CSD) on the CMD line. */
39 #define SDMMC_CMD_SEND_STATUS ((uint8_t)13) /*!< Addressed card sends its status register. */
40 #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. */
41 #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. */
42 #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. */
43 
44 #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. */
45 #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. */
46 #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 */
47 
48 #define CMD0_GO_IDLE_STATE (uint16_t)(SDMMC_CMD_GO_IDLE_STATE | SDIO_CMD_WAIT_NO_RESP)
49 #define CMD2_ALL_SEND_CID (uint16_t)(SDMMC_CMD_ALL_SEND_CID | SDIO_CMD_WAIT_LONG_RESP)
50 #define CMD3_SET_REL_ADDR (uint16_t)(SDMMC_CMD_SET_REL_ADDR | SDIO_CMD_WAIT_SHORT_RESP)
51 #define CMD7_SEL_DESEL_CARD (uint16_t)(SDMMC_CMD_SEL_DESEL_CARD | SDIO_CMD_WAIT_SHORT_RESP)
52 #define CMD8_HS_SEND_EXT_CSD (uint16_t)(SDMMC_CMD_HS_SEND_EXT_CSD | SDIO_CMD_WAIT_SHORT_RESP)
53 #define CMD9_SEND_CSD (uint16_t)(SDMMC_CMD_SEND_CSD | SDIO_CMD_WAIT_LONG_RESP)
54 #define CMD13_SEND_STATUS (uint16_t)(SDMMC_CMD_SEND_STATUS | SDIO_CMD_WAIT_SHORT_RESP)
55 #define CMD17_READ_SINGLE_BLOCK (uint16_t)(SDMMC_CMD_READ_SINGLE_BLOCK | SDIO_CMD_WAIT_SHORT_RESP)
56 #define CMD24_WRITE_SINGLE_BLOCK (uint16_t)(SDMMC_CMD_WRITE_SINGLE_BLOCK | SDIO_CMD_WAIT_SHORT_RESP)
57 #define CMD55_APP_CMD (uint16_t)(SDMMC_CMD_APP_CMD | SDIO_CMD_WAIT_SHORT_RESP)
58 
59 #define ACMD6_APP_SD_SET_BUSWIDTH (uint16_t)(SDMMC_ACMD_APP_SD_SET_BUSWIDTH | SDIO_CMD_WAIT_SHORT_RESP)
60 #define ACMD41_SD_APP_OP_COND (uint16_t)(SDMMC_ACMD_SD_APP_OP_COND | SDIO_CMD_WAIT_SHORT_RESP)
61 #define ACMD42_SD_APP_SET_CLR_CARD_DETECT (uint16_t)(SDMMC_ACMD_SD_APP_SET_CLR_CARD_DETECT | SDIO_CMD_WAIT_SHORT_RESP)
62 
63 
64 #define SDMMC_ALLZERO 0x00000000U
65 #define SDMMC_OCR_ERRORBITS 0xFDFFE008U
66 
67 #define SDMMC_R6_GENERAL_UNKNOWN_ERROR 0x00002000U
68 #define SDMMC_R6_ILLEGAL_CMD 0x00004000U
69 #define SDMMC_R6_COM_CRC_FAILED 0x00008000U
70 
71 #define SDMMC_VOLTAGE_WINDOW_SD 0x80100000U
72 #define SDMMC_HIGH_CAPACITY 0x40000000U
73 #define SDMMC_STD_CAPACITY 0x00000000U
74 #define SDMMC_CHECK_PATTERN 0x000001AAU
75 
76 #define SDIO_TRANSFER_MODE_BLOCK 0x00000000U
77 #define SDIO_DPSM_ENABLE 0x00000001U
78 #define SDIO_TRANSFER_DIR_TO_CARD 0x00000000U
79 #define SDIO_DATABLOCK_SIZE_512B 0x00000090U
80 #define SDIO_TRANSFER_DIR_TO_SDIO 0x00000100U
81 #define SDIO_DMA_ENABLE 0x00001000U
82 
83 #define CARD_V1_X 0x00000000U
84 #define CARD_V2_X 0x00000001U
85 #define CARD_SDSC 0x00000000U
86 #define CARD_SDHC_SDXC 0x00000001U
87 
88 #define SDIO_RESP1 0
89 #define SDIO_RESP2 1
90 #define SDIO_RESP3 2
91 #define SDIO_RESP4 3
92 
93 #define SDIO_GET_FLAG(__FLAG__) !!((SDIO->STA) & (__FLAG__))
94 #define SDIO_CLEAR_FLAG(__FLAG__) (SDIO->ICR = (__FLAG__))
95 
96 #define SDMMC_MAX_VOLT_TRIAL 0x00000FFFU
97 #define SDIO_CARD_TRANSFER 0x00000004U /* Card is in transfer state */
98 #define SDIO_CARD_ERROR 0x000000FFU /* Card response Error */
99 #define SDIO_CMDTIMEOUT 200U /* Command send and response timeout */
100 #define SDIO_DATA_TIMEOUT 100U /* Read data transfer timeout */
101 #define SDIO_WRITE_TIMEOUT 200U /* Write data transfer timeout */
102 
103 #define SDIO_CLOCK 18000000 /* 18 MHz */
104 
105 // ------------------------
106 // Types
107 // ------------------------
108 
109 typedef struct {
110  uint32_t CardType; // Card Type
111  uint32_t CardVersion; // Card version
112  uint32_t Class; // Class of the card class
113  uint32_t RelCardAdd; // Relative Card Address
114  uint32_t BlockNbr; // Card Capacity in blocks
115  uint32_t BlockSize; // One block size in bytes
116  uint32_t LogBlockNbr; // Card logical Capacity in blocks
117  uint32_t LogBlockSize; // Logical block size in bytes
119 
120 // ------------------------
121 // Public functions
122 // ------------------------
123 
124 inline uint32_t SDIO_GetCardState();
125 
126 bool SDIO_CmdGoIdleState();
127 bool SDIO_CmdSendCID();
128 bool SDIO_CmdSetRelAdd(uint32_t *rca);
129 bool SDIO_CmdSelDesel(uint32_t address);
130 bool SDIO_CmdOperCond();
131 bool SDIO_CmdSendCSD(uint32_t argument);
132 bool SDIO_CmdSendStatus(uint32_t argument);
133 bool SDIO_CmdReadSingleBlock(uint32_t address);
134 bool SDIO_CmdWriteSingleBlock(uint32_t address);
135 bool SDIO_CmdAppCommand(uint32_t rsa);
136 
137 bool SDIO_CmdAppSetBusWidth(uint32_t rsa, uint32_t argument);
138 bool SDIO_CmdAppOperCommand(uint32_t sdType);
139 bool SDIO_CmdAppSetClearCardDetect(uint32_t rsa);
140 
141 void SDIO_SendCommand(uint16_t command, uint32_t argument);
143 uint32_t SDIO_GetResponse(uint32_t response);
144 bool SDIO_GetCmdError();
145 bool SDIO_GetCmdResp1(uint8_t command);
146 bool SDIO_GetCmdResp2();
147 bool SDIO_GetCmdResp3();
148 bool SDIO_GetCmdResp6(uint8_t command, uint32_t *rca);
149 bool SDIO_GetCmdResp7();
SDMMC_STD_CAPACITY
#define SDMMC_STD_CAPACITY
Definition: sdio.h:73
crc
png_uint_32 crc
Definition: png.c:2247
DMA_PRIORITY_MEDIUM
#define DMA_PRIORITY_MEDIUM
Definition: stm32f4xx_hal_dma.h:309
SDIO_CmdSendStatus
bool SDIO_CmdSendStatus(uint32_t argument)
SDIO_CardInfoTypeDef::RelCardAdd
uint32_t RelCardAdd
Definition: sdio.h:113
SDIO_CardInfoTypeDef::CardVersion
uint32_t CardVersion
Definition: sdio.h:111
SDIO_CmdSetRelAdd
bool SDIO_CmdSetRelAdd(uint32_t *rca)
SDIO_GetCmdResp1
bool SDIO_GetCmdResp1(uint8_t command)
SDMMC_R6_COM_CRC_FAILED
#define SDMMC_R6_COM_CRC_FAILED
Definition: sdio.h:69
CMD55_APP_CMD
#define CMD55_APP_CMD
Definition: sdio.h:57
CARD_SDHC_SDXC
#define CARD_SDHC_SDXC
Definition: sdio.h:86
SDMMC_OCR_ERRORBITS
#define SDMMC_OCR_ERRORBITS
Definition: sdio.h:65
PersistentStore::write_data
static bool write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc)
SDIO_CmdAppCommand
bool SDIO_CmdAppCommand(uint32_t rsa)
SDIO_CardInfoTypeDef::BlockNbr
uint32_t BlockNbr
Definition: sdio.h:114
eeprom.h
CMD9_SEND_CSD
#define CMD9_SEND_CSD
Definition: sdio.h:53
ACMD42_SD_APP_SET_CLR_CARD_DETECT
#define ACMD42_SD_APP_SET_CLR_CARD_DETECT
Definition: sdio.h:61
PersistentStore::access_finish
static bool access_finish()
SDIO_CardInfoTypeDef::Class
uint32_t Class
Definition: sdio.h:112
SDMMC_ALLZERO
#define SDMMC_ALLZERO
Definition: sdio.h:64
SDIO_CmdSendCID
bool SDIO_CmdSendCID()
SDMMC_CMD_APP_CMD
#define SDMMC_CMD_APP_CMD
Definition: sdio.h:42
SDMMC_CMD_READ_SINGLE_BLOCK
#define SDMMC_CMD_READ_SINGLE_BLOCK
Definition: sdio.h:40
data
uint8_t data[8]
Definition: masstorage.h:49
PersistentStore::access_start
static bool access_start()
i
uint8_t i
Definition: screen_test_graph.c:72
SDIO_RESP3
#define SDIO_RESP3
Definition: sdio.h:90
SDIO_GetCmdResp6
bool SDIO_GetCmdResp6(uint8_t command, uint32_t *rca)
millis
uint32_t millis(void)
Definition: wiring_time.c:29
SDIO_GET_FLAG
#define SDIO_GET_FLAG(__FLAG__)
Definition: sdio.h:93
SDIO_CardInfoTypeDef::CardType
uint32_t CardType
Definition: sdio.h:110
SDIO_GetCmdResp3
bool SDIO_GetCmdResp3()
SDIO_GetCmdError
bool SDIO_GetCmdError()
SDIO_GetCommandResponse
uint8_t SDIO_GetCommandResponse()
SDMMC_CMD_SEND_STATUS
#define SDMMC_CMD_SEND_STATUS
Definition: sdio.h:39
SDIO_CARD_TRANSFER
#define SDIO_CARD_TRANSFER
Definition: sdio.h:97
SDMMC_HIGH_CAPACITY
#define SDMMC_HIGH_CAPACITY
Definition: sdio.h:72
ACMD6_APP_SD_SET_BUSWIDTH
#define ACMD6_APP_SD_SET_BUSWIDTH
Definition: sdio.h:59
SDIO_CmdReadSingleBlock
bool SDIO_CmdReadSingleBlock(uint32_t address)
SDIO_GetCardState
uint32_t SDIO_GetCardState()
CMD8_HS_SEND_EXT_CSD
#define CMD8_HS_SEND_EXT_CSD
Definition: sdio.h:52
SDIO_CardInfoTypeDef::LogBlockNbr
uint32_t LogBlockNbr
Definition: sdio.h:116
SDIO_CmdAppSetBusWidth
bool SDIO_CmdAppSetBusWidth(uint32_t rsa, uint32_t argument)
CMD0_GO_IDLE_STATE
#define CMD0_GO_IDLE_STATE
Definition: sdio.h:48
SDMMC_MAX_VOLT_TRIAL
#define SDMMC_MAX_VOLT_TRIAL
Definition: sdio.h:96
SDIO_CmdWriteSingleBlock
bool SDIO_CmdWriteSingleBlock(uint32_t address)
E2END
#define E2END
Definition: pins_RAMPS_LINUX.h:52
SDIO_CardInfoTypeDef::LogBlockSize
uint32_t LogBlockSize
Definition: sdio.h:117
sdio.h
CARD_V1_X
#define CARD_V1_X
Definition: sdio.h:83
SDIO_CmdOperCond
bool SDIO_CmdOperCond()
SDMMC_CMD_WRITE_SINGLE_BLOCK
#define SDMMC_CMD_WRITE_SINGLE_BLOCK
Definition: sdio.h:41
CMD3_SET_REL_ADDR
#define CMD3_SET_REL_ADDR
Definition: sdio.h:50
SDIO_GetCmdResp2
bool SDIO_GetCmdResp2()
SDIO_CmdAppOperCommand
bool SDIO_CmdAppOperCommand(uint32_t sdType)
SDIO_CardInfoTypeDef
Definition: sdio.h:109
void
void
Definition: png.h:1083
SDIO_CardInfoTypeDef::BlockSize
uint32_t BlockSize
Definition: sdio.h:115
SDIO_SendCommand
void SDIO_SendCommand(uint16_t command, uint32_t argument)
SDIO_DATA_TIMEOUT
#define SDIO_DATA_TIMEOUT
Definition: sdio.h:100
F_CPU
#define F_CPU
Definition: stm32_def.h:23
CMD24_WRITE_SINGLE_BLOCK
#define CMD24_WRITE_SINGLE_BLOCK
Definition: sdio.h:56
SDIO_CmdGoIdleState
bool SDIO_CmdGoIdleState()
SDMMC_R6_ILLEGAL_CMD
#define SDMMC_R6_ILLEGAL_CMD
Definition: sdio.h:68
SDIO_GetCmdResp7
bool SDIO_GetCmdResp7()
ACMD41_SD_APP_OP_COND
#define ACMD41_SD_APP_OP_COND
Definition: sdio.h:60
CARD_SDSC
#define CARD_SDSC
Definition: sdio.h:85
uint8_t
const uint8_t[]
Definition: 404_html.c:3
SDIO_RESP1
#define SDIO_RESP1
Definition: sdio.h:88
address
UsbDeviceAddress address
Definition: address.h:202
CARD_V2_X
#define CARD_V2_X
Definition: sdio.h:84
CMD13_SEND_STATUS
#define CMD13_SEND_STATUS
Definition: sdio.h:54
SDIO_CLEAR_FLAG
#define SDIO_CLEAR_FLAG(__FLAG__)
Definition: sdio.h:94
CMD7_SEL_DESEL_CARD
#define CMD7_SEL_DESEL_CARD
Definition: sdio.h:51
PersistentStore::read_data
static bool read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing=true)
SDMMC_R6_GENERAL_UNKNOWN_ERROR
#define SDMMC_R6_GENERAL_UNKNOWN_ERROR
Definition: sdio.h:67
PersistentStore::capacity
static size_t capacity()
SDIO_CLOCK
#define SDIO_CLOCK
Definition: sdio.h:103
status
static status_t status
Definition: filament_sensor.c:37
SDIO_WRITE_TIMEOUT
#define SDIO_WRITE_TIMEOUT
Definition: sdio.h:101
SDIO_RESP2
#define SDIO_RESP2
Definition: sdio.h:89
CMD17_READ_SINGLE_BLOCK
#define CMD17_READ_SINGLE_BLOCK
Definition: sdio.h:55
SDMMC_CHECK_PATTERN
#define SDMMC_CHECK_PATTERN
Definition: sdio.h:74
SDMMC_VOLTAGE_WINDOW_SD
#define SDMMC_VOLTAGE_WINDOW_SD
Definition: sdio.h:71
SDMMC_CMD_SEL_DESEL_CARD
#define SDMMC_CMD_SEL_DESEL_CARD
Definition: sdio.h:36
crc16
void crc16(uint16_t *crc, const void *const data, uint16_t cnt)
Definition: crc16.cpp:25
SDMMC_CMD_SET_REL_ADDR
#define SDMMC_CMD_SET_REL_ADDR
Definition: sdio.h:35
CMD2_ALL_SEND_CID
#define CMD2_ALL_SEND_CID
Definition: sdio.h:49
SDIO_CmdSelDesel
bool SDIO_CmdSelDesel(uint32_t address)
SDIO_CmdAppSetClearCardDetect
bool SDIO_CmdAppSetClearCardDetect(uint32_t rsa)
SDIO_CmdSendCSD
bool SDIO_CmdSendCSD(uint32_t argument)
size
static png_bytep size_t size
Definition: pngwrite.c:2170
SDIO_CARD_ERROR
#define SDIO_CARD_ERROR
Definition: sdio.h:98
SDIO_GetResponse
uint32_t SDIO_GetResponse(uint32_t response)