Sfoglia il codice sorgente

SDIO support for STM32F1 (#12782)

jmz52 5 anni fa
parent
commit
d372e7e477

+ 267
- 0
Marlin/src/HAL/HAL_STM32F1/HAL_sdio_STM32F1.cpp Vedi File

@@ -0,0 +1,267 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ * Copyright (C) 2017 Victor Perez
8
+ *
9
+ * This program is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation, either version 3 of the License, or
12
+ * (at your option) any later version.
13
+ *
14
+ * This program is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
+ * GNU General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU General Public License
20
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
+ *
22
+ */
23
+
24
+#ifdef __STM32F1__
25
+
26
+#include "HAL_sdio_STM32F1.h"
27
+
28
+SDIO_CardInfoTypeDef SdCard;
29
+
30
+bool SDIO_Init(void) {
31
+  uint32_t count = 0U;
32
+  SdCard.CardType = SdCard.CardVersion = SdCard.Class = SdCard.RelCardAdd = SdCard.BlockNbr = SdCard.BlockSize = SdCard.LogBlockNbr = SdCard.LogBlockSize = 0;
33
+
34
+  sdio_begin();
35
+  sdio_set_dbus_width(SDIO_CLKCR_WIDBUS_1BIT);
36
+
37
+  dma_init(SDIO_DMA_DEV);
38
+  dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL);
39
+  dma_set_priority(SDIO_DMA_DEV, SDIO_DMA_CHANNEL, DMA_PRIORITY_VERY_HIGH);
40
+
41
+  if (!SDIO_CmdGoIdleState()) return false;
42
+  if (!SDIO_CmdGoIdleState()) return false; /* Hotplugged cards tends to miss first CMD0, so give them a second chance. */
43
+
44
+  SdCard.CardVersion = SDIO_CmdOperCond() ? CARD_V2_X : CARD_V1_X;
45
+
46
+  do {
47
+    if (count++ == SDMMC_MAX_VOLT_TRIAL) return false;
48
+    SDIO_CmdAppOperCommand(SdCard.CardVersion == CARD_V2_X ? SDMMC_HIGH_CAPACITY : SDMMC_STD_CAPACITY);
49
+  } while ((SDIO_GetResponse(SDIO_RESP1) & 0x80000000) == 0);
50
+
51
+  SdCard.CardType = (SDIO_GetResponse(SDIO_RESP1) & SDMMC_HIGH_CAPACITY) ? CARD_SDHC_SDXC : CARD_SDSC;
52
+
53
+  if (!SDIO_CmdSendCID()) return false;
54
+  if (!SDIO_CmdSetRelAdd(&SdCard.RelCardAdd)) return false; /* Send CMD3 SET_REL_ADDR with argument 0. SD Card publishes its RCA. */
55
+  if (!SDIO_CmdSendCSD(SdCard.RelCardAdd << 16U)) return false;
56
+
57
+  SdCard.Class = (SDIO_GetResponse(SDIO_RESP2) >> 20U);
58
+
59
+  if (SdCard.CardType == CARD_SDHC_SDXC) {
60
+    SdCard.LogBlockNbr = SdCard.BlockNbr = (((SDIO_GetResponse(SDIO_RESP2) & 0x0000003FU) << 26U) | ((SDIO_GetResponse(SDIO_RESP3) & 0xFFFF0000U) >> 6U)) + 1024;
61
+    SdCard.LogBlockSize = SdCard.BlockSize = 512U;
62
+  }
63
+  else {
64
+    SdCard.BlockNbr  = ((((SDIO_GetResponse(SDIO_RESP2) & 0x000003FFU) << 2U ) | ((SDIO_GetResponse(SDIO_RESP3) & 0xC0000000U) >> 30U)) + 1U) * (4U << ((SDIO_GetResponse(SDIO_RESP3) & 0x00038000U) >> 15U));
65
+    SdCard.BlockSize = 1U << ((SDIO_GetResponse(SDIO_RESP2) >> 16) & 0x0FU);
66
+    SdCard.LogBlockNbr =  (SdCard.BlockNbr) * ((SdCard.BlockSize) / 512U);
67
+    SdCard.LogBlockSize = 512U;
68
+  }
69
+
70
+  if (!SDIO_CmdSelDesel(SdCard.RelCardAdd << 16U)) return false;
71
+  if (!SDIO_CmdAppSetClearCardDetect(SdCard.RelCardAdd << 16U)) return false;
72
+  if (!SDIO_CmdAppSetBusWidth(SdCard.RelCardAdd << 16U, 2)) return false;
73
+
74
+  sdio_set_dbus_width(SDIO_CLKCR_WIDBUS_4BIT);
75
+  sdio_set_clock(SDIO_CLOCK);
76
+  return true;
77
+}
78
+
79
+bool SDIO_ReadBlock(uint32_t blockAddress, uint8_t *data) {
80
+  if (SDIO_GetCardState() != SDIO_CARD_TRANSFER) return false;
81
+  if (blockAddress >= SdCard.LogBlockNbr) return false;
82
+  if ((0x03 & (uint32_t)data)) return false; // misaligned data
83
+
84
+  if (SdCard.CardType != CARD_SDHC_SDXC) { blockAddress *= 512U; }
85
+
86
+  dma_setup_transfer(SDIO_DMA_DEV, SDIO_DMA_CHANNEL, &SDIO->FIFO, DMA_SIZE_32BITS, data, DMA_SIZE_32BITS, DMA_MINC_MODE);
87
+  dma_set_num_transfers(SDIO_DMA_DEV, SDIO_DMA_CHANNEL, 128);
88
+  dma_clear_isr_bits(SDIO_DMA_DEV, SDIO_DMA_CHANNEL);
89
+  dma_enable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL);
90
+
91
+  sdio_setup_transfer(SDIO_DATA_TIMEOUT * (F_CPU / 1000U), 512, SDIO_BLOCKSIZE_512 | SDIO_DCTRL_DMAEN | SDIO_DCTRL_DTEN | SDIO_DIR_RX);
92
+
93
+  if (!SDIO_CmdReadSingleBlock(blockAddress)) {
94
+    SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS);
95
+    dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL);
96
+    return false;
97
+  }
98
+
99
+  while (!SDIO_GET_FLAG(SDIO_STA_DATAEND | SDIO_STA_TRX_ERROR_FLAGS)) {}
100
+
101
+  dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL);
102
+
103
+  if (SDIO_GET_FLAG(SDIO_STA_TRX_ERROR_FLAGS)) {
104
+    SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS);
105
+    return false;
106
+  }
107
+  SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS);
108
+  return true;
109
+}
110
+
111
+bool SDIO_WriteBlock(uint32_t blockAddress, const uint8_t *data) {
112
+  if (SDIO_GetCardState() != SDIO_CARD_TRANSFER) return false;
113
+  if (blockAddress >= SdCard.LogBlockNbr) return false;
114
+  if ((0x03 & (uint32_t)data)) return false; // misaligned data
115
+
116
+  if (SdCard.CardType != CARD_SDHC_SDXC) { blockAddress *= 512U; }
117
+
118
+  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);
119
+  dma_set_num_transfers(SDIO_DMA_DEV, SDIO_DMA_CHANNEL, 128);
120
+  dma_clear_isr_bits(SDIO_DMA_DEV, SDIO_DMA_CHANNEL);
121
+  dma_enable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL);
122
+
123
+  if (!SDIO_CmdWriteSingleBlock(blockAddress)) {
124
+    dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL);
125
+    return false;
126
+  }
127
+
128
+  sdio_setup_transfer(SDIO_DATA_TIMEOUT * (F_CPU / 1000U), 512U, SDIO_BLOCKSIZE_512 | SDIO_DCTRL_DMAEN | SDIO_DCTRL_DTEN);
129
+
130
+  while (!SDIO_GET_FLAG(SDIO_STA_DATAEND | SDIO_STA_TRX_ERROR_FLAGS)) {}
131
+
132
+  dma_disable(SDIO_DMA_DEV, SDIO_DMA_CHANNEL);
133
+
134
+  if (SDIO_GET_FLAG(SDIO_STA_TRX_ERROR_FLAGS)) {
135
+    SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS);
136
+    return false;
137
+  }
138
+
139
+  SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS | SDIO_ICR_DATA_FLAGS);
140
+
141
+  uint32 timeout = millis() + SDIO_WRITE_TIMEOUT;
142
+  while (timeout > millis()) {
143
+    if (SDIO_GetCardState() == SDIO_CARD_TRANSFER) {
144
+      return true;
145
+    }
146
+  }
147
+  return false;
148
+}
149
+
150
+inline uint32_t SDIO_GetCardState(void) { return SDIO_CmdSendStatus(SdCard.RelCardAdd << 16U) ? (SDIO_GetResponse(SDIO_RESP1) >> 9U) & 0x0FU : SDIO_CARD_ERROR; }
151
+
152
+// --------------------------------------------------------------------------
153
+// SD Commands and Responses
154
+// --------------------------------------------------------------------------
155
+
156
+void SDIO_SendCommand(uint16_t command, uint32_t argument) { SDIO->ARG = argument; SDIO->CMD = (uint32_t)(SDIO_CMD_CPSMEN | command); }
157
+uint8_t SDIO_GetCommandResponse(void) { return (uint8_t)(SDIO->RESPCMD); }
158
+uint32_t SDIO_GetResponse(uint32_t response) { return SDIO->RESP[response]; }
159
+
160
+bool SDIO_CmdGoIdleState(void) { SDIO_SendCommand(CMD0_GO_IDLE_STATE, 0); return SDIO_GetCmdError(); }
161
+bool SDIO_CmdSendCID(void) { SDIO_SendCommand(CMD2_ALL_SEND_CID, 0); return SDIO_GetCmdResp2(); }
162
+bool SDIO_CmdSetRelAdd(uint32_t *rca) { SDIO_SendCommand(CMD3_SET_REL_ADDR, 0); return SDIO_GetCmdResp6(SDMMC_CMD_SET_REL_ADDR, rca); }
163
+bool SDIO_CmdSelDesel(uint32_t address) { SDIO_SendCommand(CMD7_SEL_DESEL_CARD, address); return SDIO_GetCmdResp1(SDMMC_CMD_SEL_DESEL_CARD); }
164
+bool SDIO_CmdOperCond(void) { SDIO_SendCommand(CMD8_HS_SEND_EXT_CSD, SDMMC_CHECK_PATTERN); return SDIO_GetCmdResp7(); }
165
+bool SDIO_CmdSendCSD(uint32_t argument) { SDIO_SendCommand(CMD9_SEND_CSD, argument); return SDIO_GetCmdResp2(); }
166
+bool SDIO_CmdSendStatus(uint32_t argument) { SDIO_SendCommand(CMD13_SEND_STATUS, argument); return SDIO_GetCmdResp1(SDMMC_CMD_SEND_STATUS); }
167
+bool SDIO_CmdReadSingleBlock(uint32_t address) { SDIO_SendCommand(CMD17_READ_SINGLE_BLOCK, address); return SDIO_GetCmdResp1(SDMMC_CMD_READ_SINGLE_BLOCK); }
168
+bool SDIO_CmdWriteSingleBlock(uint32_t address) { SDIO_SendCommand(CMD24_WRITE_SINGLE_BLOCK, address); return SDIO_GetCmdResp1(SDMMC_CMD_WRITE_SINGLE_BLOCK); }
169
+bool SDIO_CmdAppCommand(uint32_t rsa) { SDIO_SendCommand(CMD55_APP_CMD, rsa); return SDIO_GetCmdResp1(SDMMC_CMD_APP_CMD); }
170
+
171
+bool SDIO_CmdAppSetBusWidth(uint32_t rsa, uint32_t argument) {
172
+  if (!SDIO_CmdAppCommand(rsa)) return false;
173
+  SDIO_SendCommand(ACMD6_APP_SD_SET_BUSWIDTH, argument);
174
+  return SDIO_GetCmdResp2();
175
+}
176
+
177
+bool SDIO_CmdAppOperCommand(uint32_t sdType) {
178
+  if (!SDIO_CmdAppCommand(0)) return false;
179
+  SDIO_SendCommand(ACMD41_SD_APP_OP_COND , SDMMC_VOLTAGE_WINDOW_SD | sdType);
180
+  return SDIO_GetCmdResp3();
181
+}
182
+
183
+bool SDIO_CmdAppSetClearCardDetect(uint32_t rsa) {
184
+  if (!SDIO_CmdAppCommand(rsa)) return false;
185
+  SDIO_SendCommand(ACMD42_SD_APP_SET_CLR_CARD_DETECT, 0);
186
+  return SDIO_GetCmdResp2();
187
+}
188
+
189
+// Wait until given flags are unset or till timeout
190
+#define SDIO_WAIT(FLAGS) do{ \
191
+  uint32_t count = 1 + (SDIO_CMDTIMEOUT) * ((F_CPU) / 8U / 1000U); \
192
+  do { if (!--count) return false; } while (!SDIO_GET_FLAG(FLAGS)); \
193
+}while(0)
194
+
195
+bool SDIO_GetCmdError(void) {
196
+  SDIO_WAIT(SDIO_STA_CMDSENT);
197
+
198
+  SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS);
199
+  return true;
200
+}
201
+
202
+bool SDIO_GetCmdResp1(uint8_t command) {
203
+  SDIO_WAIT(SDIO_STA_CCRCFAIL | SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT);
204
+
205
+  if (SDIO_GET_FLAG(SDIO_STA_CCRCFAIL | SDIO_STA_CTIMEOUT)) {
206
+    SDIO_CLEAR_FLAG(SDIO_STA_CCRCFAIL | SDIO_STA_CTIMEOUT);
207
+    return false;
208
+  }
209
+  if (SDIO_GetCommandResponse() != command) return false;
210
+
211
+  SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS);
212
+  return (SDIO_GetResponse(SDIO_RESP1) & SDMMC_OCR_ERRORBITS) == SDMMC_ALLZERO;
213
+}
214
+
215
+bool SDIO_GetCmdResp2(void) {
216
+  SDIO_WAIT(SDIO_STA_CCRCFAIL | SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT);
217
+
218
+  if (SDIO_GET_FLAG(SDIO_STA_CCRCFAIL | SDIO_STA_CTIMEOUT)) {
219
+    SDIO_CLEAR_FLAG(SDIO_STA_CCRCFAIL | SDIO_STA_CTIMEOUT);
220
+    return false;
221
+  }
222
+
223
+  SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS);
224
+  return true;
225
+}
226
+
227
+bool SDIO_GetCmdResp3(void) {
228
+  SDIO_WAIT(SDIO_STA_CCRCFAIL | SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT);
229
+
230
+  if (SDIO_GET_FLAG(SDIO_STA_CTIMEOUT)) {
231
+    SDIO_CLEAR_FLAG(SDIO_STA_CTIMEOUT);
232
+    return false;
233
+  }
234
+
235
+  SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS);
236
+  return true;
237
+}
238
+
239
+bool SDIO_GetCmdResp6(uint8_t command, uint32_t *rca) {
240
+  SDIO_WAIT(SDIO_STA_CCRCFAIL | SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT);
241
+
242
+  if (SDIO_GET_FLAG(SDIO_STA_CCRCFAIL | SDIO_STA_CTIMEOUT)) {
243
+    SDIO_CLEAR_FLAG(SDIO_STA_CCRCFAIL | SDIO_STA_CTIMEOUT);
244
+    return false;
245
+  }
246
+  if (SDIO_GetCommandResponse() != command) return false;
247
+
248
+  SDIO_CLEAR_FLAG(SDIO_ICR_CMD_FLAGS);
249
+  if (SDIO_GetResponse(SDIO_RESP1) & (SDMMC_R6_GENERAL_UNKNOWN_ERROR | SDMMC_R6_ILLEGAL_CMD | SDMMC_R6_COM_CRC_FAILED)) return false;
250
+
251
+  *rca = SDIO_GetResponse(SDIO_RESP1) >> 16;
252
+  return true;
253
+}
254
+
255
+bool SDIO_GetCmdResp7(void) {
256
+  SDIO_WAIT(SDIO_STA_CCRCFAIL | SDIO_STA_CMDREND | SDIO_STA_CTIMEOUT);
257
+
258
+  if (SDIO_GET_FLAG(SDIO_STA_CTIMEOUT)) {
259
+    SDIO_CLEAR_FLAG(SDIO_STA_CTIMEOUT);
260
+    return false;
261
+  }
262
+
263
+  if (SDIO_GET_FLAG(SDIO_STA_CMDREND)) { SDIO_CLEAR_FLAG(SDIO_STA_CMDREND); }
264
+  return true;
265
+}
266
+
267
+#endif // __STM32F1__

+ 152
- 0
Marlin/src/HAL/HAL_STM32F1/HAL_sdio_STM32F1.h Vedi File

@@ -0,0 +1,152 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ *
4
+ * Copyright (C) 2016 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
+// --------------------------------------------------------------------------
25
+// Includes
26
+// --------------------------------------------------------------------------
27
+
28
+#include "Arduino.h"
29
+#include "libmaple/sdio.h"
30
+#include "libmaple/dma.h"
31
+
32
+// --------------------------------------------------------------------------
33
+// Defines
34
+// --------------------------------------------------------------------------
35
+
36
+#define SDMMC_CMD_GO_IDLE_STATE                       ((uint8_t)0)   /* Resets the SD memory card. */
37
+#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. */
38
+#define SDMMC_CMD_SET_REL_ADDR                        ((uint8_t)3)   /* Asks the card to publish a new relative address (RCA). */
39
+#define SDMMC_CMD_SEL_DESEL_CARD                      ((uint8_t)7)   /* Selects the card by its own relative address and gets deselected by any other address */
40
+#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. */
41
+#define SDMMC_CMD_SEND_CSD                            ((uint8_t)9)   /* Addressed card sends its card specific data (CSD) on the CMD line. */
42
+#define SDMMC_CMD_SEND_STATUS                         ((uint8_t)13)  /*!< Addressed card sends its status register. */
43
+#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. */
44
+#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. */
45
+#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. */
46
+
47
+#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. */
48
+#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. */
49
+#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  */
50
+
51
+#define CMD0_GO_IDLE_STATE                            (uint16_t)(SDMMC_CMD_GO_IDLE_STATE | SDIO_CMD_WAIT_NO_RESP)
52
+#define CMD2_ALL_SEND_CID                             (uint16_t)(SDMMC_CMD_ALL_SEND_CID | SDIO_CMD_WAIT_LONG_RESP)
53
+#define CMD3_SET_REL_ADDR                             (uint16_t)(SDMMC_CMD_SET_REL_ADDR | SDIO_CMD_WAIT_SHORT_RESP)
54
+#define CMD7_SEL_DESEL_CARD                           (uint16_t)(SDMMC_CMD_SEL_DESEL_CARD | SDIO_CMD_WAIT_SHORT_RESP)
55
+#define CMD8_HS_SEND_EXT_CSD                          (uint16_t)(SDMMC_CMD_HS_SEND_EXT_CSD | SDIO_CMD_WAIT_SHORT_RESP)
56
+#define CMD9_SEND_CSD                                 (uint16_t)(SDMMC_CMD_SEND_CSD | SDIO_CMD_WAIT_LONG_RESP)
57
+#define CMD13_SEND_STATUS                             (uint16_t)(SDMMC_CMD_SEND_STATUS | SDIO_CMD_WAIT_SHORT_RESP)
58
+#define CMD17_READ_SINGLE_BLOCK                       (uint16_t)(SDMMC_CMD_READ_SINGLE_BLOCK | SDIO_CMD_WAIT_SHORT_RESP)
59
+#define CMD24_WRITE_SINGLE_BLOCK                      (uint16_t)(SDMMC_CMD_WRITE_SINGLE_BLOCK | SDIO_CMD_WAIT_SHORT_RESP)
60
+#define CMD55_APP_CMD                                 (uint16_t)(SDMMC_CMD_APP_CMD | SDIO_CMD_WAIT_SHORT_RESP)
61
+
62
+#define ACMD6_APP_SD_SET_BUSWIDTH                     (uint16_t)(SDMMC_ACMD_APP_SD_SET_BUSWIDTH | SDIO_CMD_WAIT_SHORT_RESP)
63
+#define ACMD41_SD_APP_OP_COND                         (uint16_t)(SDMMC_ACMD_SD_APP_OP_COND | SDIO_CMD_WAIT_SHORT_RESP)
64
+#define ACMD42_SD_APP_SET_CLR_CARD_DETECT             (uint16_t)(SDMMC_ACMD_SD_APP_SET_CLR_CARD_DETECT | SDIO_CMD_WAIT_SHORT_RESP)
65
+
66
+
67
+#define SDMMC_ALLZERO                        0x00000000U
68
+#define SDMMC_OCR_ERRORBITS                  0xFDFFE008U
69
+
70
+#define SDMMC_R6_GENERAL_UNKNOWN_ERROR       0x00002000U
71
+#define SDMMC_R6_ILLEGAL_CMD                 0x00004000U
72
+#define SDMMC_R6_COM_CRC_FAILED              0x00008000U
73
+
74
+#define SDMMC_VOLTAGE_WINDOW_SD              0x80100000U
75
+#define SDMMC_HIGH_CAPACITY                  0x40000000U
76
+#define SDMMC_STD_CAPACITY                   0x00000000U
77
+#define SDMMC_CHECK_PATTERN                  0x000001AAU
78
+
79
+#define SDIO_TRANSFER_MODE_BLOCK             0x00000000U
80
+#define SDIO_DPSM_ENABLE                     0x00000001U
81
+#define SDIO_TRANSFER_DIR_TO_CARD            0x00000000U
82
+#define SDIO_DATABLOCK_SIZE_512B             0x00000090U
83
+#define SDIO_TRANSFER_DIR_TO_SDIO            0x00000100U
84
+#define SDIO_DMA_ENABLE                      0x00001000U
85
+
86
+#define CARD_V1_X                            0x00000000U
87
+#define CARD_V2_X                            0x00000001U
88
+#define CARD_SDSC                            0x00000000U
89
+#define CARD_SDHC_SDXC                       0x00000001U
90
+
91
+#define SDIO_RESP1                           0
92
+#define SDIO_RESP2                           1
93
+#define SDIO_RESP3                           2
94
+#define SDIO_RESP4                           3
95
+
96
+#define SDIO_GET_FLAG(__FLAG__)              !!((SDIO->STA) & (__FLAG__))
97
+#define SDIO_CLEAR_FLAG(__FLAG__)            (SDIO->ICR = (__FLAG__))
98
+
99
+#define SDMMC_MAX_VOLT_TRIAL                 0x00000FFFU
100
+#define SDIO_CARD_TRANSFER                   0x00000004U    /* Card is in transfer state */
101
+#define SDIO_CARD_ERROR                      0x000000FFU    /* Card response Error */
102
+#define SDIO_CMDTIMEOUT                      200U           /* Command send and response timeout */
103
+#define SDIO_DATA_TIMEOUT                    100U           /* Read data transfer timeout */
104
+#define SDIO_WRITE_TIMEOUT                   200U           /* Write data transfer timeout */
105
+
106
+#define SDIO_CLOCK                           18000000       /* 18 MHz */
107
+
108
+// --------------------------------------------------------------------------
109
+// Types
110
+// --------------------------------------------------------------------------
111
+
112
+typedef struct {
113
+  uint32_t CardType;      // Card Type
114
+  uint32_t CardVersion;   // Card version
115
+  uint32_t Class;         // Class of the card class
116
+  uint32_t RelCardAdd;    // Relative Card Address
117
+  uint32_t BlockNbr;      // Card Capacity in blocks
118
+  uint32_t BlockSize;     // One block size in bytes
119
+  uint32_t LogBlockNbr;   // Card logical Capacity in blocks
120
+  uint32_t LogBlockSize;  // Logical block size in bytes
121
+} SDIO_CardInfoTypeDef;
122
+
123
+// --------------------------------------------------------------------------
124
+// Public functions
125
+// --------------------------------------------------------------------------
126
+
127
+inline uint32_t SDIO_GetCardState(void);
128
+
129
+bool SDIO_CmdGoIdleState(void);
130
+bool SDIO_CmdSendCID(void);
131
+bool SDIO_CmdSetRelAdd(uint32_t *rca);
132
+bool SDIO_CmdSelDesel(uint32_t address);
133
+bool SDIO_CmdOperCond(void);
134
+bool SDIO_CmdSendCSD(uint32_t argument);
135
+bool SDIO_CmdSendStatus(uint32_t argument);
136
+bool SDIO_CmdReadSingleBlock(uint32_t address);
137
+bool SDIO_CmdWriteSingleBlock(uint32_t address);
138
+bool SDIO_CmdAppCommand(uint32_t rsa);
139
+
140
+bool SDIO_CmdAppSetBusWidth(uint32_t rsa, uint32_t argument);
141
+bool SDIO_CmdAppOperCommand(uint32_t sdType);
142
+bool SDIO_CmdAppSetClearCardDetect(uint32_t rsa);
143
+
144
+void SDIO_SendCommand(uint16_t command, uint32_t argument);
145
+uint8_t SDIO_GetCommandResponse(void);
146
+uint32_t SDIO_GetResponse(uint32_t response);
147
+bool SDIO_GetCmdError(void);
148
+bool SDIO_GetCmdResp1(uint8_t command);
149
+bool SDIO_GetCmdResp2(void);
150
+bool SDIO_GetCmdResp3(void);
151
+bool SDIO_GetCmdResp6(uint8_t command, uint32_t *rca);
152
+bool SDIO_GetCmdResp7(void);

+ 4
- 0
Marlin/src/HAL/HAL_STM32F1/SanityCheck.h Vedi File

@@ -70,3 +70,7 @@
70 70
 #if ENABLED(EMERGENCY_PARSER)
71 71
   #error "EMERGENCY_PARSER is not yet implemented for STM32F1. Disable EMERGENCY_PARSER to continue."
72 72
 #endif
73
+
74
+#if ENABLED(SDIO_SUPPORT) && DISABLED(SDSUPPORT)
75
+  #error "SDIO_SUPPORT requires SDSUPPORT. Enable SDSUPPORT to continue."
76
+#endif

+ 13
- 11
Marlin/src/config/examples/Mks/Robin/Configuration.h Vedi File

@@ -104,7 +104,7 @@
104 104
  *
105 105
  * :[-1, 0, 1, 2, 3, 4, 5, 6, 7]
106 106
  */
107
-#define SERIAL_PORT 0
107
+#define SERIAL_PORT 3
108 108
 
109 109
 /**
110 110
  * Select a secondary serial port on the board to use for communication with the host.
@@ -113,7 +113,8 @@
113 113
  *
114 114
  * :[-1, 0, 1, 2, 3, 4, 5, 6, 7]
115 115
  */
116
-//#define SERIAL_PORT_2 -1
116
+#define SERIAL_PORT_2 1
117
+#define NUM_SERIAL 2
117 118
 
118 119
 /**
119 120
  * This setting determines the communication speed of the printer.
@@ -132,12 +133,12 @@
132 133
 // The following define selects which electronics board you have.
133 134
 // Please choose the name from boards.h that matches your setup
134 135
 #ifndef MOTHERBOARD
135
-  #define MOTHERBOARD BOARD_RAMPS_14_EFB
136
+  #define MOTHERBOARD BOARD_MKS_ROBIN
136 137
 #endif
137 138
 
138 139
 // Optional custom name for your RepStrap or other custom machine
139 140
 // Displayed in the LCD "Ready" message
140
-//#define CUSTOM_MACHINE_NAME "3D Printer"
141
+#define CUSTOM_MACHINE_NAME "MKS Robin"
141 142
 
142 143
 // Define this to set a unique identifier for this printer, (Used by some programs to differentiate between machines)
143 144
 // You can use an online service to generate a random UUID. (eg http://www.uuidgenerator.net/version4)
@@ -147,10 +148,10 @@
147 148
 
148 149
 // This defines the number of extruders
149 150
 // :[1, 2, 3, 4, 5, 6]
150
-#define EXTRUDERS 1
151
+#define EXTRUDERS 2
151 152
 
152 153
 // Generally expected filament diameter (1.75, 2.85, 3.0, ...). Used for Volumetric, Filament Width Sensor, etc.
153
-#define DEFAULT_NOMINAL_FILAMENT_DIA 3.0
154
+#define DEFAULT_NOMINAL_FILAMENT_DIA 1.75
154 155
 
155 156
 // For Cyclops or any "multi-extruder" that shares a single nozzle.
156 157
 //#define SINGLENOZZLE
@@ -328,12 +329,12 @@
328 329
  * :{ '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" }
329 330
  */
330 331
 #define TEMP_SENSOR_0 1
331
-#define TEMP_SENSOR_1 0
332
+#define TEMP_SENSOR_1 1
332 333
 #define TEMP_SENSOR_2 0
333 334
 #define TEMP_SENSOR_3 0
334 335
 #define TEMP_SENSOR_4 0
335 336
 #define TEMP_SENSOR_5 0
336
-#define TEMP_SENSOR_BED 0
337
+#define TEMP_SENSOR_BED 1
337 338
 #define TEMP_SENSOR_CHAMBER 0
338 339
 
339 340
 // Dummy thermistor constant temperature readings, for use with 998 and 999
@@ -433,7 +434,7 @@
433 434
  * heater. If your configuration is significantly different than this and you don't understand
434 435
  * the issues involved, don't use bed PID until someone else verifies that your hardware works.
435 436
  */
436
-//#define PIDTEMPBED
437
+#define PIDTEMPBED
437 438
 
438 439
 //#define BED_LIMIT_SWITCHING
439 440
 
@@ -1507,7 +1508,8 @@
1507 1508
  * you must uncomment the following option or it won't work.
1508 1509
  *
1509 1510
  */
1510
-//#define SDSUPPORT
1511
+#define SDSUPPORT
1512
+#define SDIO_SUPPORT
1511 1513
 
1512 1514
 /**
1513 1515
  * SD CARD: SPI SPEED
@@ -1880,7 +1882,7 @@
1880 1882
 //
1881 1883
 // MKS Robin 320x240 color display
1882 1884
 //
1883
-//#define MKS_ROBIN_TFT
1885
+#define MKS_ROBIN_TFT
1884 1886
 
1885 1887
 //=============================================================================
1886 1888
 //============================  Other Controllers  ============================

+ 1
- 1
Marlin/src/config/examples/Mks/Robin/Configuration_adv.h Vedi File

@@ -646,7 +646,7 @@
646 646
   // as SD_DETECT_PIN in your board's pins definitions.
647 647
   // This setting should be disabled unless you are using a push button, pulling the pin to ground.
648 648
   // Note: This is always disabled for ULTIPANEL (except ELB_FULL_GRAPHIC_CONTROLLER).
649
-  #define SD_DETECT_INVERTED
649
+  //#define SD_DETECT_INVERTED
650 650
 
651 651
   #define SD_FINISHED_STEPPERRELEASE true          // Disable steppers when SD Print is finished
652 652
   #define SD_FINISHED_RELEASECOMMAND "M84 X Y Z E" // You might want to keep the Z enabled so your bed stays in place.

+ 1
- 0
Marlin/src/pins/pins_MKS_ROBIN.h Vedi File

@@ -121,4 +121,5 @@
121 121
 #define FSMC_CS_PIN        PG12  // NE4
122 122
 #define FSMC_RS_PIN        PF0   // A0
123 123
 
124
+#define SD_DETECT_PIN      PF12
124 125
 #define SDSS               -1

+ 1
- 1
Marlin/src/sd/Sd2Card.cpp Vedi File

@@ -30,7 +30,7 @@
30 30
 
31 31
 #include "../inc/MarlinConfig.h"
32 32
 
33
-#if ENABLED(SDSUPPORT) && DISABLED(USB_FLASH_DRIVE_SUPPORT)
33
+#if ENABLED(SDSUPPORT) && DISABLED(USB_FLASH_DRIVE_SUPPORT) && DISABLED(SDIO_SUPPORT)
34 34
 
35 35
 /* Enable FAST CRC computations - You can trade speed for FLASH space if
36 36
  * needed by disabling the following define */

+ 39
- 0
Marlin/src/sd/Sd2Card_sdio.h Vedi File

@@ -0,0 +1,39 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
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 "../inc/MarlinConfig.h"
25
+
26
+#if ENABLED(SDIO_SUPPORT)
27
+
28
+bool SDIO_Init(void);
29
+bool SDIO_ReadBlock(uint32_t block, uint8_t *dst);
30
+bool SDIO_WriteBlock(uint32_t block, const uint8_t *src);
31
+
32
+class Sd2Card {
33
+  public:
34
+    bool init(uint8_t sckRateID = 0, uint8_t chipSelectPin = 0) { return SDIO_Init(); }
35
+    bool readBlock(uint32_t block, uint8_t *dst) { return SDIO_ReadBlock(block, dst); }
36
+    bool writeBlock(uint32_t block, const uint8_t *src) { return SDIO_WriteBlock(block, src); }
37
+};
38
+
39
+#endif // SDIO_SUPPORT

+ 2
- 0
Marlin/src/sd/SdVolume.h Vedi File

@@ -35,6 +35,8 @@
35 35
 
36 36
 #if ENABLED(USB_FLASH_DRIVE_SUPPORT)
37 37
   #include "usb_flashdrive/Sd2Card_FlashDrive.h"
38
+#elif ENABLED(SDIO_SUPPORT)
39
+  #include "Sd2Card_sdio.h"
38 40
 #else
39 41
   #include "Sd2Card.h"
40 42
 #endif

Loading…
Annulla
Salva