Browse Source

🎨 Format onboard_sd.cpp

Scott Lahteine 4 years ago
parent
commit
ecb727c3ef
1 changed files with 183 additions and 176 deletions
  1. 183
    176
      Marlin/src/HAL/STM32F1/onboard_sd.cpp

+ 183
- 176
Marlin/src/HAL/STM32F1/onboard_sd.cpp View File

@@ -38,8 +38,8 @@
38 38
   #define SPI_CLOCK_MAX SPI_BAUD_PCLK_DIV_2
39 39
 #endif
40 40
 
41
-#define CS_LOW()  WRITE(ONBOARD_SD_CS_PIN, LOW)  /* Set OnboardSPI cs low */
42
-#define CS_HIGH() WRITE(ONBOARD_SD_CS_PIN, HIGH) /* Set OnboardSPI cs high */
41
+#define CS_LOW()  WRITE(ONBOARD_SD_CS_PIN, LOW)  // Set OnboardSPI cs low
42
+#define CS_HIGH() WRITE(ONBOARD_SD_CS_PIN, HIGH) // Set OnboardSPI cs high
43 43
 
44 44
 #define FCLK_FAST() ONBOARD_SD_SPI.setClockDivider(SPI_CLOCK_MAX)
45 45
 #define FCLK_SLOW() ONBOARD_SD_SPI.setClockDivider(SPI_BAUD_PCLK_DIV_256)
@@ -49,32 +49,32 @@
49 49
 ---------------------------------------------------------------------------*/
50 50
 
51 51
 /* MMC/SD command */
52
-#define CMD0  (0)     /* GO_IDLE_STATE */
53
-#define CMD1  (1)     /* SEND_OP_COND (MMC) */
54
-#define ACMD41  (0x80+41) /* SEND_OP_COND (SDC) */
55
-#define CMD8  (8)     /* SEND_IF_COND */
56
-#define CMD9  (9)     /* SEND_CSD */
57
-#define CMD10 (10)    /* SEND_CID */
58
-#define CMD12 (12)    /* STOP_TRANSMISSION */
59
-#define ACMD13  (0x80+13) /* SD_STATUS (SDC) */
60
-#define CMD16 (16)    /* SET_BLOCKLEN */
61
-#define CMD17 (17)    /* READ_SINGLE_BLOCK */
62
-#define CMD18 (18)    /* READ_MULTIPLE_BLOCK */
63
-#define CMD23 (23)    /* SET_BLOCK_COUNT (MMC) */
64
-#define ACMD23  (0x80+23) /* SET_WR_BLK_ERASE_COUNT (SDC) */
65
-#define CMD24 (24)    /* WRITE_BLOCK */
66
-#define CMD25 (25)    /* WRITE_MULTIPLE_BLOCK */
67
-#define CMD32 (32)    /* ERASE_ER_BLK_START */
68
-#define CMD33 (33)    /* ERASE_ER_BLK_END */
69
-#define CMD38 (38)    /* ERASE */
70
-#define CMD48 (48)    /* READ_EXTR_SINGLE */
71
-#define CMD49 (49)    /* WRITE_EXTR_SINGLE */
72
-#define CMD55 (55)    /* APP_CMD */
73
-#define CMD58 (58)    /* READ_OCR */
74
-
75
-static volatile DSTATUS Stat = STA_NOINIT;  /* Physical drive status */
52
+#define CMD0  (0)         // GO_IDLE_STATE
53
+#define CMD1  (1)         // SEND_OP_COND (MMC)
54
+#define ACMD41  (0x80+41) // SEND_OP_COND (SDC)
55
+#define CMD8  (8)         // SEND_IF_COND
56
+#define CMD9  (9)         // SEND_CSD
57
+#define CMD10 (10)        // SEND_CID
58
+#define CMD12 (12)        // STOP_TRANSMISSION
59
+#define ACMD13  (0x80+13) // SD_STATUS (SDC)
60
+#define CMD16 (16)        // SET_BLOCKLEN
61
+#define CMD17 (17)        // READ_SINGLE_BLOCK
62
+#define CMD18 (18)        // READ_MULTIPLE_BLOCK
63
+#define CMD23 (23)        // SET_BLOCK_COUNT (MMC)
64
+#define ACMD23  (0x80+23) // SET_WR_BLK_ERASE_COUNT (SDC)
65
+#define CMD24 (24)        // WRITE_BLOCK
66
+#define CMD25 (25)        // WRITE_MULTIPLE_BLOCK
67
+#define CMD32 (32)        // ERASE_ER_BLK_START
68
+#define CMD33 (33)        // ERASE_ER_BLK_END
69
+#define CMD38 (38)        // ERASE
70
+#define CMD48 (48)        // READ_EXTR_SINGLE
71
+#define CMD49 (49)        // WRITE_EXTR_SINGLE
72
+#define CMD55 (55)        // APP_CMD
73
+#define CMD58 (58)        // READ_OCR
74
+
75
+static volatile DSTATUS Stat = STA_NOINIT;  // Physical drive status
76 76
 static volatile UINT timeout;
77
-static BYTE CardType;      /* Card type flags */
77
+static BYTE CardType;                       // Card type flags
78 78
 
79 79
 /*-----------------------------------------------------------------------*/
80 80
 /* Send/Receive data to the MMC  (Platform dependent)                    */
@@ -82,7 +82,7 @@ static BYTE CardType;      /* Card type flags */
82 82
 
83 83
 /* Exchange a byte */
84 84
 static BYTE xchg_spi (
85
-  BYTE dat  /* Data to send */
85
+  BYTE dat  // Data to send
86 86
 ) {
87 87
   BYTE returnByte = ONBOARD_SD_SPI.transfer(dat);
88 88
   return returnByte;
@@ -90,18 +90,18 @@ static BYTE xchg_spi (
90 90
 
91 91
 /* Receive multiple byte */
92 92
 static void rcvr_spi_multi (
93
-  BYTE *buff,   /* Pointer to data buffer */
94
-  UINT btr    /* Number of bytes to receive (16, 64 or 512) */
93
+  BYTE *buff,   // Pointer to data buffer
94
+  UINT btr      // Number of bytes to receive (16, 64 or 512)
95 95
 ) {
96 96
   ONBOARD_SD_SPI.dmaTransfer(0, const_cast<uint8_t*>(buff), btr);
97 97
 }
98 98
 
99 99
 #if _DISKIO_WRITE
100 100
 
101
-  /* Send multiple bytes */
101
+  // Send multiple bytes
102 102
   static void xmit_spi_multi (
103
-    const BYTE *buff, /* Pointer to the data */
104
-    UINT btx      /* Number of bytes to send (multiple of 16) */
103
+    const BYTE *buff, // Pointer to the data
104
+    UINT btx          // Number of bytes to send (multiple of 16)
105 105
   ) {
106 106
     ONBOARD_SD_SPI.dmaSend(const_cast<uint8_t*>(buff), btx);
107 107
   }
@@ -112,16 +112,15 @@ static void rcvr_spi_multi (
112 112
 /* Wait for card ready                                                   */
113 113
 /*-----------------------------------------------------------------------*/
114 114
 
115
-static int wait_ready (  /* 1:Ready, 0:Timeout */
116
-  UINT wt     /* Timeout [ms] */
115
+static int wait_ready ( // 1:Ready, 0:Timeout
116
+  UINT wt               // Timeout [ms]
117 117
 ) {
118 118
   BYTE d;
119
-
120 119
   timeout = millis() + wt;
121 120
   do {
122 121
     d = xchg_spi(0xFF);
123
-    /* This loop takes a while. Insert rot_rdq() here for multitask environment. */
124
-  } while (d != 0xFF && (timeout > millis()));  /* Wait for card goes ready or timeout */
122
+    // This loop takes a while. Insert rot_rdq() here for multitask environment.
123
+  } while (d != 0xFF && (timeout > millis()));  // Wait for card goes ready or timeout
125 124
 
126 125
   return (d == 0xFF) ? 1 : 0;
127 126
 }
@@ -131,21 +130,21 @@ static int wait_ready (  /* 1:Ready, 0:Timeout */
131 130
 /*-----------------------------------------------------------------------*/
132 131
 
133 132
 static void deselect() {
134
-  CS_HIGH();    /* CS = H */
135
-  xchg_spi(0xFF); /* Dummy clock (force DO hi-z for multiple slave SPI) */
133
+  CS_HIGH();      // CS = H
134
+  xchg_spi(0xFF); // Dummy clock (force DO hi-z for multiple slave SPI)
136 135
 }
137 136
 
138 137
 /*-----------------------------------------------------------------------*/
139 138
 /* Select card and wait for ready                                        */
140 139
 /*-----------------------------------------------------------------------*/
141 140
 
142
-static int select() { /* 1:OK, 0:Timeout */
143
-  CS_LOW();   /* CS = L */
144
-  xchg_spi(0xFF); /* Dummy clock (force DO enabled) */
141
+static int select() {             // 1:OK, 0:Timeout
142
+  CS_LOW();                       // CS = L
143
+  xchg_spi(0xFF);                 // Dummy clock (force DO enabled)
145 144
 
146
-  if (wait_ready(500)) return 1;  /* Leading busy check: Wait for card ready */
145
+  if (wait_ready(500)) return 1;  // Leading busy check: Wait for card ready
147 146
 
148
-  deselect();   /* Timeout */
147
+  deselect();                     // Timeout
149 148
   return 0;
150 149
 }
151 150
 
@@ -153,16 +152,18 @@ static int select() { /* 1:OK, 0:Timeout */
153 152
 /* Control SPI module (Platform dependent)                               */
154 153
 /*-----------------------------------------------------------------------*/
155 154
 
156
-static void power_on() {  /* Enable SSP module and attach it to I/O pads */
155
+// Enable SSP module and attach it to I/O pads
156
+static void sd_power_on() {
157 157
   ONBOARD_SD_SPI.setModule(ONBOARD_SPI_DEVICE);
158 158
   ONBOARD_SD_SPI.begin();
159 159
   ONBOARD_SD_SPI.setBitOrder(MSBFIRST);
160 160
   ONBOARD_SD_SPI.setDataMode(SPI_MODE0);
161
-  OUT_WRITE(ONBOARD_SD_CS_PIN, HIGH); /* Set CS# high */
161
+  OUT_WRITE(ONBOARD_SD_CS_PIN, HIGH); // Set CS# high
162 162
 }
163 163
 
164
-static void power_off() {   /* Disable SPI function */
165
-  select();       /* Wait for card ready */
164
+// Disable SPI function
165
+static void sd_power_off() {
166
+  select();                           // Wait for card ready
166 167
   deselect();
167 168
 }
168 169
 
@@ -170,23 +171,23 @@ static void power_off() {   /* Disable SPI function */
170 171
 /* Receive a data packet from the MMC                                    */
171 172
 /*-----------------------------------------------------------------------*/
172 173
 
173
-static int rcvr_datablock (  /* 1:OK, 0:Error */
174
-  BYTE *buff,     /* Data buffer */
175
-  UINT btr      /* Data block length (byte) */
174
+static int rcvr_datablock (   // 1:OK, 0:Error
175
+  BYTE *buff,                 // Data buffer
176
+  UINT btr                    // Data block length (byte)
176 177
 ) {
177 178
   BYTE token;
178 179
 
179 180
   timeout = millis() + 200;
180
-  do {              /* Wait for DataStart token in timeout of 200ms */
181
+  do {                            // Wait for DataStart token in timeout of 200ms
181 182
     token = xchg_spi(0xFF);
182
-    /* This loop will take a while. Insert rot_rdq() here for multitask environment. */
183
+                                  // This loop will take a while. Insert rot_rdq() here for multitask environment.
183 184
   } while ((token == 0xFF) && (timeout > millis()));
184
-  if (token != 0xFE) return 0;   /* Function fails if invalid DataStart token or timeout */
185
+  if (token != 0xFE) return 0;    // Function fails if invalid DataStart token or timeout
185 186
 
186
-  rcvr_spi_multi(buff, btr);    /* Store trailing data to the buffer */
187
-  xchg_spi(0xFF); xchg_spi(0xFF); /* Discard CRC */
187
+  rcvr_spi_multi(buff, btr);      // Store trailing data to the buffer
188
+  xchg_spi(0xFF); xchg_spi(0xFF); // Discard CRC
188 189
 
189
-  return 1;           /* Function succeeded */
190
+  return 1;                       // Function succeeded
190 191
 }
191 192
 
192 193
 /*-----------------------------------------------------------------------*/
@@ -195,25 +196,25 @@ static int rcvr_datablock (  /* 1:OK, 0:Error */
195 196
 
196 197
 #if _DISKIO_WRITE
197 198
 
198
-  static int xmit_datablock (  /* 1:OK, 0:Failed */
199
-    const BYTE *buff, /* Ponter to 512 byte data to be sent */
200
-    BYTE token      /* Token */
199
+  static int xmit_datablock(  // 1:OK, 0:Failed
200
+    const BYTE *buff,         // Pointer to 512 byte data to be sent
201
+    BYTE token                // Token
201 202
   ) {
202 203
     BYTE resp;
203 204
 
204
-    if (!wait_ready(500)) return 0;   /* Leading busy check: Wait for card ready to accept data block */
205
+    if (!wait_ready(500)) return 0;       // Leading busy check: Wait for card ready to accept data block
205 206
 
206
-    xchg_spi(token);          /* Send token */
207
-    if (token == 0xFD) return 1;    /* Do not send data if token is StopTran */
207
+    xchg_spi(token);                      // Send token
208
+    if (token == 0xFD) return 1;          // Do not send data if token is StopTran
208 209
 
209
-    xmit_spi_multi(buff, 512);      /* Data */
210
-    xchg_spi(0xFF); xchg_spi(0xFF);   /* Dummy CRC */
210
+    xmit_spi_multi(buff, 512);            // Data
211
+    xchg_spi(0xFF); xchg_spi(0xFF);       // Dummy CRC
211 212
 
212
-    resp = xchg_spi(0xFF);        /* Receive data resp */
213
+    resp = xchg_spi(0xFF);                // Receive data resp
213 214
 
214
-    return (resp & 0x1F) == 0x05 ? 1 : 0; /* Data was accepted or not */
215
+    return (resp & 0x1F) == 0x05 ? 1 : 0; // Data was accepted or not
215 216
 
216
-    /* Busy check is done at next transmission */
217
+    // Busy check is done at next transmission
217 218
   }
218 219
 
219 220
 #endif // _DISKIO_WRITE
@@ -222,43 +223,43 @@ static int rcvr_datablock (  /* 1:OK, 0:Error */
222 223
 /* Send a command packet to the MMC                                      */
223 224
 /*-----------------------------------------------------------------------*/
224 225
 
225
-static BYTE send_cmd (   /* Return value: R1 resp (bit7==1:Failed to send) */
226
-  BYTE cmd,   /* Command index */
227
-  DWORD arg   /* Argument */
226
+static BYTE send_cmd( // Return value: R1 resp (bit7==1:Failed to send)
227
+  BYTE cmd,           // Command index
228
+  DWORD arg           // Argument
228 229
 ) {
229 230
   BYTE n, res;
230 231
 
231
-  if (cmd & 0x80) { /* Send a CMD55 prior to ACMD<n> */
232
+  if (cmd & 0x80) {   // Send a CMD55 prior to ACMD<n>
232 233
     cmd &= 0x7F;
233 234
     res = send_cmd(CMD55, 0);
234 235
     if (res > 1) return res;
235 236
   }
236 237
 
237
-  /* Select the card and wait for ready except to stop multiple block read */
238
+  // Select the card and wait for ready except to stop multiple block read
238 239
   if (cmd != CMD12) {
239 240
     deselect();
240 241
     if (!select()) return 0xFF;
241 242
   }
242 243
 
243
-  /* Send command packet */
244
-  xchg_spi(0x40 | cmd);       /* Start + command index */
245
-  xchg_spi((BYTE)(arg >> 24));    /* Argument[31..24] */
246
-  xchg_spi((BYTE)(arg >> 16));    /* Argument[23..16] */
247
-  xchg_spi((BYTE)(arg >> 8));     /* Argument[15..8] */
248
-  xchg_spi((BYTE)arg);        /* Argument[7..0] */
249
-  n = 0x01;             /* Dummy CRC + Stop */
250
-  if (cmd == CMD0) n = 0x95;      /* Valid CRC for CMD0(0) */
251
-  if (cmd == CMD8) n = 0x87;      /* Valid CRC for CMD8(0x1AA) */
244
+  // Send command packet
245
+  xchg_spi(0x40 | cmd);         // Start + command index
246
+  xchg_spi((BYTE)(arg >> 24));  // Argument[31..24]
247
+  xchg_spi((BYTE)(arg >> 16));  // Argument[23..16]
248
+  xchg_spi((BYTE)(arg >> 8));   // Argument[15..8]
249
+  xchg_spi((BYTE)arg);          // Argument[7..0]
250
+  n = 0x01;                     // Dummy CRC + Stop
251
+  if (cmd == CMD0) n = 0x95;    // Valid CRC for CMD0(0)
252
+  if (cmd == CMD8) n = 0x87;    // Valid CRC for CMD8(0x1AA)
252 253
   xchg_spi(n);
253 254
 
254
-  /* Receive command resp */
255
-  if (cmd == CMD12) xchg_spi(0xFF); /* Diacard following one byte when CMD12 */
256
-  n = 10;               /* Wait for response (10 bytes max) */
255
+  // Receive command response
256
+  if (cmd == CMD12) xchg_spi(0xFF); // Discard the following byte when CMD12
257
+  n = 10;                           // Wait for response (10 bytes max)
257 258
   do
258 259
     res = xchg_spi(0xFF);
259 260
   while ((res & 0x80) && --n);
260 261
 
261
-  return res;             /* Return received response */
262
+  return res;                   // Return received response
262 263
 }
263 264
 
264 265
 /*--------------------------------------------------------------------------
@@ -270,49 +271,52 @@ static BYTE send_cmd (   /* Return value: R1 resp (bit7==1:Failed to send) */
270 271
 /*-----------------------------------------------------------------------*/
271 272
 
272 273
 DSTATUS disk_initialize (
273
-  BYTE drv    /* Physical drive number (0) */
274
+  BYTE drv                                                            // Physical drive number (0)
274 275
 ) {
275 276
   BYTE n, cmd, ty, ocr[4];
276 277
 
277
-  if (drv) return STA_NOINIT;     /* Supports only drive 0 */
278
-  power_on();             /* Initialize SPI */
278
+  if (drv) return STA_NOINIT;                                         // Supports only drive 0
279
+  sd_power_on();                                                      // Initialize SPI
279 280
 
280
-  if (Stat & STA_NODISK) return Stat; /* Is a card existing in the soket? */
281
+  if (Stat & STA_NODISK) return Stat;                                 // Is a card existing in the soket?
281 282
 
282 283
   FCLK_SLOW();
283
-  for (n = 10; n; n--) xchg_spi(0xFF);  /* Send 80 dummy clocks */
284
+  for (n = 10; n; n--) xchg_spi(0xFF);                                // Send 80 dummy clocks
284 285
 
285 286
   ty = 0;
286
-  if (send_cmd(CMD0, 0) == 1) {     /* Put the card SPI state */
287
-    timeout = millis() + 1000;            /* Initialization timeout = 1 sec */
288
-    if (send_cmd(CMD8, 0x1AA) == 1) { /* Is the catd SDv2? */
289
-      for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF);  /* Get 32 bit return value of R7 resp */
290
-      if (ocr[2] == 0x01 && ocr[3] == 0xAA) {       /* Does the card support 2.7-3.6V? */
291
-        while ((timeout > millis()) && send_cmd(ACMD41, 1UL << 30)) ; /* Wait for end of initialization with ACMD41(HCS) */
292
-        if ((timeout > millis()) && send_cmd(CMD58, 0) == 0) {    /* Check CCS bit in the OCR */
287
+  if (send_cmd(CMD0, 0) == 1) {                                       // Put the card SPI state
288
+    timeout = millis() + 1000;                                        // Initialization timeout = 1 sec
289
+    if (send_cmd(CMD8, 0x1AA) == 1) {                                 // Is the catd SDv2?
290
+      for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF);                // Get 32 bit return value of R7 resp
291
+      if (ocr[2] == 0x01 && ocr[3] == 0xAA) {                         // Does the card support 2.7-3.6V?
292
+        while ((timeout > millis()) && send_cmd(ACMD41, 1UL << 30));  // Wait for end of initialization with ACMD41(HCS)
293
+        if ((timeout > millis()) && send_cmd(CMD58, 0) == 0) {        // Check CCS bit in the OCR
293 294
           for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF);
294
-          ty = (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2;  /* Check if the card is SDv2 */
295
+          ty = (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2;          // Check if the card is SDv2
295 296
         }
296 297
       }
297
-    } else {  /* Not an SDv2 card */
298
-      if (send_cmd(ACMD41, 0) <= 1)   { /* SDv1 or MMCv3? */
299
-        ty = CT_SD1; cmd = ACMD41;  /* SDv1 (ACMD41(0)) */
300
-      } else {
301
-        ty = CT_MMC; cmd = CMD1;  /* MMCv3 (CMD1(0)) */
298
+    }
299
+    else {                                                            // Not an SDv2 card
300
+      if (send_cmd(ACMD41, 0) <= 1)   {                               // SDv1 or MMCv3?
301
+        ty = CT_SD1; cmd = ACMD41;                                    // SDv1 (ACMD41(0))
302
+      }
303
+      else {
304
+        ty = CT_MMC; cmd = CMD1;                                      // MMCv3 (CMD1(0))
302 305
       }
303
-      while ((timeout > millis()) && send_cmd(cmd, 0)) ;    /* Wait for the card leaves idle state */
304
-      if (!(timeout > millis()) || send_cmd(CMD16, 512) != 0) /* Set block length: 512 */
306
+      while ((timeout > millis()) && send_cmd(cmd, 0));               // Wait for the card leaves idle state
307
+      if (!(timeout > millis()) || send_cmd(CMD16, 512) != 0)         // Set block length: 512
305 308
         ty = 0;
306 309
     }
307 310
   }
308
-  CardType = ty;  /* Card type */
311
+  CardType = ty;                                                      // Card type
309 312
   deselect();
310 313
 
311
-  if (ty) {   /* OK */
312
-    FCLK_FAST();      /* Set fast clock */
313
-    Stat &= ~STA_NOINIT;  /* Clear STA_NOINIT flag */
314
-  } else {    /* Failed */
315
-    power_off();
314
+  if (ty) {                                                           // OK
315
+    FCLK_FAST();                                                      // Set fast clock
316
+    Stat &= ~STA_NOINIT;                                              // Clear STA_NOINIT flag
317
+  }
318
+  else {                                                              // Failed
319
+    sd_power_off();
316 320
     Stat = STA_NOINIT;
317 321
   }
318 322
 
@@ -324,10 +328,10 @@ DSTATUS disk_initialize (
324 328
 /*-----------------------------------------------------------------------*/
325 329
 
326 330
 DSTATUS disk_status (
327
-  BYTE drv    /* Physical drive number (0) */
331
+  BYTE drv                    // Physical drive number (0)
328 332
 ) {
329
-  if (drv) return STA_NOINIT;   /* Supports only drive 0 */
330
-  return Stat;  /* Return disk status */
333
+  if (drv) return STA_NOINIT; // Supports only drive 0
334
+  return Stat;                // Return disk status
331 335
 }
332 336
 
333 337
 /*-----------------------------------------------------------------------*/
@@ -335,28 +339,28 @@ DSTATUS disk_status (
335 339
 /*-----------------------------------------------------------------------*/
336 340
 
337 341
 DRESULT disk_read (
338
-  BYTE drv,   /* Physical drive number (0) */
339
-  BYTE *buff,   /* Pointer to the data buffer to store read data */
340
-  DWORD sector, /* Start sector number (LBA) */
341
-  UINT count    /* Number of sectors to read (1..128) */
342
+  BYTE drv,     // Physical drive number (0)
343
+  BYTE *buff,   // Pointer to the data buffer to store read data
344
+  DWORD sector, // Start sector number (LBA)
345
+  UINT count    // Number of sectors to read (1..128)
342 346
 ) {
343 347
   BYTE cmd;
344 348
 
345
-  if (drv || !count) return RES_PARERR;   /* Check parameter */
346
-  if (Stat & STA_NOINIT) return RES_NOTRDY; /* Check if drive is ready */
347
-  if (!(CardType & CT_BLOCK)) sector *= 512;  /* LBA ot BA conversion (byte addressing cards) */
349
+  if (drv || !count) return RES_PARERR;       // Check parameter
350
+  if (Stat & STA_NOINIT) return RES_NOTRDY;   // Check if drive is ready
351
+  if (!(CardType & CT_BLOCK)) sector *= 512;  // LBA ot BA conversion (byte addressing cards)
348 352
   FCLK_FAST();
349
-  cmd = count > 1 ? CMD18 : CMD17;      /*  READ_MULTIPLE_BLOCK : READ_SINGLE_BLOCK */
353
+  cmd = count > 1 ? CMD18 : CMD17;            //  READ_MULTIPLE_BLOCK : READ_SINGLE_BLOCK
350 354
   if (send_cmd(cmd, sector) == 0) {
351 355
     do {
352 356
       if (!rcvr_datablock(buff, 512)) break;
353 357
       buff += 512;
354 358
     } while (--count);
355
-    if (cmd == CMD18) send_cmd(CMD12, 0); /* STOP_TRANSMISSION */
359
+    if (cmd == CMD18) send_cmd(CMD12, 0);     // STOP_TRANSMISSION
356 360
   }
357 361
   deselect();
358 362
 
359
-  return count ? RES_ERROR : RES_OK;  /* Return result */
363
+  return count ? RES_ERROR : RES_OK;          // Return result
360 364
 }
361 365
 
362 366
 /*-----------------------------------------------------------------------*/
@@ -366,36 +370,36 @@ DRESULT disk_read (
366 370
 #if _DISKIO_WRITE
367 371
 
368 372
   DRESULT disk_write(
369
-    BYTE drv,     /* Physical drive number (0) */
370
-    const BYTE *buff, /* Ponter to the data to write */
371
-    DWORD sector,   /* Start sector number (LBA) */
372
-    UINT count      /* Number of sectors to write (1..128) */
373
+    BYTE drv,                                   // Physical drive number (0)
374
+    const BYTE *buff,                           // Pointer to the data to write
375
+    DWORD sector,                               // Start sector number (LBA)
376
+    UINT count                                  // Number of sectors to write (1..128)
373 377
   ) {
374
-    if (drv || !count) return RES_PARERR;   /* Check parameter */
375
-    if (Stat & STA_NOINIT) return RES_NOTRDY; /* Check drive status */
376
-    if (Stat & STA_PROTECT) return RES_WRPRT; /* Check write protect */
378
+    if (drv || !count) return RES_PARERR;       // Check parameter
379
+    if (Stat & STA_NOINIT) return RES_NOTRDY;   // Check drive status
380
+    if (Stat & STA_PROTECT) return RES_WRPRT;   // Check write protect
377 381
     FCLK_FAST();
378
-    if (!(CardType & CT_BLOCK)) sector *= 512;  /* LBA ==> BA conversion (byte addressing cards) */
382
+    if (!(CardType & CT_BLOCK)) sector *= 512;  // LBA ==> BA conversion (byte addressing cards)
379 383
 
380
-    if (count == 1) { /* Single sector write */
381
-      if ((send_cmd(CMD24, sector) == 0)  /* WRITE_BLOCK */
384
+    if (count == 1) {                           // Single sector write
385
+      if ((send_cmd(CMD24, sector) == 0)        // WRITE_BLOCK
382 386
         && xmit_datablock(buff, 0xFE)) {
383 387
         count = 0;
384 388
       }
385 389
     }
386
-    else {        /* Multiple sector write */
387
-      if (CardType & CT_SDC) send_cmd(ACMD23, count); /* Predefine number of sectors */
388
-      if (send_cmd(CMD25, sector) == 0) { /* WRITE_MULTIPLE_BLOCK */
390
+    else {                                            // Multiple sector write
391
+      if (CardType & CT_SDC) send_cmd(ACMD23, count); // Predefine number of sectors
392
+      if (send_cmd(CMD25, sector) == 0) {             // WRITE_MULTIPLE_BLOCK
389 393
         do {
390 394
           if (!xmit_datablock(buff, 0xFC)) break;
391 395
           buff += 512;
392 396
         } while (--count);
393
-        if (!xmit_datablock(0, 0xFD)) count = 1;  /* STOP_TRAN token */
397
+        if (!xmit_datablock(0, 0xFD)) count = 1;      // STOP_TRAN token
394 398
       }
395 399
     }
396 400
     deselect();
397 401
 
398
-    return count ? RES_ERROR : RES_OK;  /* Return result */
402
+    return count ? RES_ERROR : RES_OK;                // Return result
399 403
   }
400 404
 
401 405
 #endif // _DISKIO_WRITE
@@ -407,9 +411,9 @@ DRESULT disk_read (
407 411
 #if _DISKIO_IOCTL
408 412
 
409 413
   DRESULT disk_ioctl (
410
-    BYTE drv,   /* Physical drive number (0) */
411
-    BYTE cmd,   /* Control command code */
412
-    void *buff    /* Pointer to the conrtol data */
414
+    BYTE drv,   // Physical drive number (0)
415
+    BYTE cmd,   // Control command code
416
+    void *buff  // Pointer to the conrtol data
413 417
   ) {
414 418
     DRESULT res;
415 419
     BYTE n, csd[16], *ptr = (BYTE *)buff;
@@ -420,22 +424,23 @@ DRESULT disk_read (
420 424
       UINT dc;
421 425
     #endif
422 426
 
423
-    if (drv) return RES_PARERR;         /* Check parameter */
424
-    if (Stat & STA_NOINIT) return RES_NOTRDY; /* Check if drive is ready */
427
+    if (drv) return RES_PARERR;                 // Check parameter
428
+    if (Stat & STA_NOINIT) return RES_NOTRDY;   // Check if drive is ready
425 429
 
426 430
     res = RES_ERROR;
427 431
     FCLK_FAST();
428 432
     switch (cmd) {
429
-      case CTRL_SYNC:     /* Wait for end of internal write process of the drive */
433
+      case CTRL_SYNC:                           // Wait for end of internal write process of the drive
430 434
         if (select()) res = RES_OK;
431 435
         break;
432 436
 
433
-      case GET_SECTOR_COUNT:  /* Get drive capacity in unit of sector (DWORD) */
437
+      case GET_SECTOR_COUNT:                    // Get drive capacity in unit of sector (DWORD)
434 438
         if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) {
435
-          if ((csd[0] >> 6) == 1) { /* SDC ver 2.00 */
439
+          if ((csd[0] >> 6) == 1) {             // SDC ver 2.00
436 440
             csize = csd[9] + ((WORD)csd[8] << 8) + ((DWORD)(csd[7] & 63) << 16) + 1;
437 441
             *(DWORD*)buff = csize << 10;
438
-          } else {          /* SDC ver 1.XX or MMC ver 3 */
442
+          }
443
+          else {                                // SDC ver 1.XX or MMC ver 3
439 444
             n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2;
440 445
             csize = (csd[8] >> 6) + ((WORD)csd[7] << 2) + ((WORD)(csd[6] & 3) << 10) + 1;
441 446
             *(DWORD*)buff = csize << (n - 9);
@@ -444,21 +449,23 @@ DRESULT disk_read (
444 449
         }
445 450
         break;
446 451
 
447
-      case GET_BLOCK_SIZE:  /* Get erase block size in unit of sector (DWORD) */
448
-        if (CardType & CT_SD2) {  /* SDC ver 2.00 */
449
-          if (send_cmd(ACMD13, 0) == 0) { /* Read SD status */
452
+      case GET_BLOCK_SIZE:                              // Get erase block size in unit of sector (DWORD)
453
+        if (CardType & CT_SD2) {                        // SDC ver 2.00
454
+          if (send_cmd(ACMD13, 0) == 0) {               // Read SD status
450 455
             xchg_spi(0xFF);
451
-            if (rcvr_datablock(csd, 16)) {        /* Read partial block */
452
-              for (n = 64 - 16; n; n--) xchg_spi(0xFF); /* Purge trailing data */
456
+            if (rcvr_datablock(csd, 16)) {              // Read partial block
457
+              for (n = 64 - 16; n; n--) xchg_spi(0xFF); // Purge trailing data
453 458
               *(DWORD*)buff = 16UL << (csd[10] >> 4);
454 459
               res = RES_OK;
455 460
             }
456 461
           }
457
-        } else {          /* SDC ver 1.XX or MMC */
458
-          if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) {  /* Read CSD */
459
-            if (CardType & CT_SD1) {  /* SDC ver 1.XX */
462
+        }
463
+        else {                                                        // SDC ver 1.XX or MMC
464
+          if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) {  // Read CSD
465
+            if (CardType & CT_SD1) {                                  // SDC ver 1.XX
460 466
               *(DWORD*)buff = (((csd[10] & 63) << 1) + ((WORD)(csd[11] & 128) >> 7) + 1) << ((csd[13] >> 6) - 1);
461
-            } else {          /* MMC */
467
+            }
468
+            else {                                                    // MMC
462 469
               *(DWORD*)buff = ((WORD)((csd[10] & 124) >> 2) + 1) * (((csd[11] & 3) << 3) + ((csd[11] & 224) >> 5) + 1);
463 470
             }
464 471
             res = RES_OK;
@@ -466,47 +473,47 @@ DRESULT disk_read (
466 473
         }
467 474
         break;
468 475
 
469
-      case CTRL_TRIM:   /* Erase a block of sectors (used when _USE_TRIM in ffconf.h is 1) */
470
-        if (!(CardType & CT_SDC)) break;        /* Check if the card is SDC */
471
-        if (disk_ioctl(drv, MMC_GET_CSD, csd)) break; /* Get CSD */
472
-        if (!(csd[0] >> 6) && !(csd[10] & 0x40)) break; /* Check if sector erase can be applied to the card */
473
-        dp = (DWORD *)buff; st = dp[0]; ed = dp[1];       /* Load sector block */
476
+      case CTRL_TRIM:                                   // Erase a block of sectors (used when _USE_TRIM in ffconf.h is 1)
477
+        if (!(CardType & CT_SDC)) break;                // Check if the card is SDC
478
+        if (disk_ioctl(drv, MMC_GET_CSD, csd)) break;   // Get CSD
479
+        if (!(csd[0] >> 6) && !(csd[10] & 0x40)) break; // Check if sector erase can be applied to the card
480
+        dp = (DWORD *)buff; st = dp[0]; ed = dp[1];     // Load sector block
474 481
         if (!(CardType & CT_BLOCK)) {
475 482
           st *= 512; ed *= 512;
476 483
         }
477
-        if (send_cmd(CMD32, st) == 0 && send_cmd(CMD33, ed) == 0 && send_cmd(CMD38, 0) == 0 && wait_ready(30000)) { /* Erase sector block */
478
-          res = RES_OK; /* FatFs does not check result of this command */
484
+        if (send_cmd(CMD32, st) == 0 && send_cmd(CMD33, ed) == 0 && send_cmd(CMD38, 0) == 0 && wait_ready(30000)) { // Erase sector block
485
+          res = RES_OK; // FatFs does not check result of this command
479 486
         }
480 487
         break;
481 488
 
482
-      /* Following commands are never used by FatFs module */
489
+      // The following commands are never used by FatFs module
483 490
 
484
-      case MMC_GET_TYPE:    /* Get MMC/SDC type (BYTE) */
491
+      case MMC_GET_TYPE:    // Get MMC/SDC type (BYTE)
485 492
         *ptr = CardType;
486 493
         res = RES_OK;
487 494
         break;
488 495
 
489
-      case MMC_GET_CSD:   /* Read CSD (16 bytes) */
490
-        if (send_cmd(CMD9, 0) == 0 && rcvr_datablock(ptr, 16)) {  /* READ_CSD */
496
+      case MMC_GET_CSD:     // Read CSD (16 bytes)
497
+        if (send_cmd(CMD9, 0) == 0 && rcvr_datablock(ptr, 16)) {
491 498
           res = RES_OK;
492 499
         }
493 500
         break;
494 501
 
495
-      case MMC_GET_CID:   /* Read CID (16 bytes) */
496
-        if (send_cmd(CMD10, 0) == 0 && rcvr_datablock(ptr, 16)) { /* READ_CID */
502
+      case MMC_GET_CID:     // Read CID (16 bytes)
503
+        if (send_cmd(CMD10, 0) == 0 && rcvr_datablock(ptr, 16)) {
497 504
           res = RES_OK;
498 505
         }
499 506
         break;
500 507
 
501
-      case MMC_GET_OCR:   /* Read OCR (4 bytes) */
502
-        if (send_cmd(CMD58, 0) == 0) {  /* READ_OCR */
508
+      case MMC_GET_OCR:     // Read OCR (4 bytes)
509
+        if (send_cmd(CMD58, 0) == 0) {
503 510
           for (n = 4; n; n--) *ptr++ = xchg_spi(0xFF);
504 511
           res = RES_OK;
505 512
         }
506 513
         break;
507 514
 
508
-      case MMC_GET_SDSTAT:  /* Read SD status (64 bytes) */
509
-        if (send_cmd(ACMD13, 0) == 0) { /* SD_STATUS */
515
+      case MMC_GET_SDSTAT:  // Read SD status (64 bytes)
516
+        if (send_cmd(ACMD13, 0) == 0) {
510 517
           xchg_spi(0xFF);
511 518
           if (rcvr_datablock(ptr, 64)) res = RES_OK;
512 519
         }

Loading…
Cancel
Save