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
   #define SPI_CLOCK_MAX SPI_BAUD_PCLK_DIV_2
38
   #define SPI_CLOCK_MAX SPI_BAUD_PCLK_DIV_2
39
 #endif
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
 #define FCLK_FAST() ONBOARD_SD_SPI.setClockDivider(SPI_CLOCK_MAX)
44
 #define FCLK_FAST() ONBOARD_SD_SPI.setClockDivider(SPI_CLOCK_MAX)
45
 #define FCLK_SLOW() ONBOARD_SD_SPI.setClockDivider(SPI_BAUD_PCLK_DIV_256)
45
 #define FCLK_SLOW() ONBOARD_SD_SPI.setClockDivider(SPI_BAUD_PCLK_DIV_256)
49
 ---------------------------------------------------------------------------*/
49
 ---------------------------------------------------------------------------*/
50
 
50
 
51
 /* MMC/SD command */
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
 static volatile UINT timeout;
76
 static volatile UINT timeout;
77
-static BYTE CardType;      /* Card type flags */
77
+static BYTE CardType;                       // Card type flags
78
 
78
 
79
 /*-----------------------------------------------------------------------*/
79
 /*-----------------------------------------------------------------------*/
80
 /* Send/Receive data to the MMC  (Platform dependent)                    */
80
 /* Send/Receive data to the MMC  (Platform dependent)                    */
82
 
82
 
83
 /* Exchange a byte */
83
 /* Exchange a byte */
84
 static BYTE xchg_spi (
84
 static BYTE xchg_spi (
85
-  BYTE dat  /* Data to send */
85
+  BYTE dat  // Data to send
86
 ) {
86
 ) {
87
   BYTE returnByte = ONBOARD_SD_SPI.transfer(dat);
87
   BYTE returnByte = ONBOARD_SD_SPI.transfer(dat);
88
   return returnByte;
88
   return returnByte;
90
 
90
 
91
 /* Receive multiple byte */
91
 /* Receive multiple byte */
92
 static void rcvr_spi_multi (
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
   ONBOARD_SD_SPI.dmaTransfer(0, const_cast<uint8_t*>(buff), btr);
96
   ONBOARD_SD_SPI.dmaTransfer(0, const_cast<uint8_t*>(buff), btr);
97
 }
97
 }
98
 
98
 
99
 #if _DISKIO_WRITE
99
 #if _DISKIO_WRITE
100
 
100
 
101
-  /* Send multiple bytes */
101
+  // Send multiple bytes
102
   static void xmit_spi_multi (
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
     ONBOARD_SD_SPI.dmaSend(const_cast<uint8_t*>(buff), btx);
106
     ONBOARD_SD_SPI.dmaSend(const_cast<uint8_t*>(buff), btx);
107
   }
107
   }
112
 /* Wait for card ready                                                   */
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
   BYTE d;
118
   BYTE d;
119
-
120
   timeout = millis() + wt;
119
   timeout = millis() + wt;
121
   do {
120
   do {
122
     d = xchg_spi(0xFF);
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
   return (d == 0xFF) ? 1 : 0;
125
   return (d == 0xFF) ? 1 : 0;
127
 }
126
 }
131
 /*-----------------------------------------------------------------------*/
130
 /*-----------------------------------------------------------------------*/
132
 
131
 
133
 static void deselect() {
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
 /* Select card and wait for ready                                        */
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
   return 0;
148
   return 0;
150
 }
149
 }
151
 
150
 
153
 /* Control SPI module (Platform dependent)                               */
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
   ONBOARD_SD_SPI.setModule(ONBOARD_SPI_DEVICE);
157
   ONBOARD_SD_SPI.setModule(ONBOARD_SPI_DEVICE);
158
   ONBOARD_SD_SPI.begin();
158
   ONBOARD_SD_SPI.begin();
159
   ONBOARD_SD_SPI.setBitOrder(MSBFIRST);
159
   ONBOARD_SD_SPI.setBitOrder(MSBFIRST);
160
   ONBOARD_SD_SPI.setDataMode(SPI_MODE0);
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
   deselect();
167
   deselect();
167
 }
168
 }
168
 
169
 
170
 /* Receive a data packet from the MMC                                    */
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
   BYTE token;
178
   BYTE token;
178
 
179
 
179
   timeout = millis() + 200;
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
     token = xchg_spi(0xFF);
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
   } while ((token == 0xFF) && (timeout > millis()));
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
 
196
 
196
 #if _DISKIO_WRITE
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
     BYTE resp;
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
 #endif // _DISKIO_WRITE
220
 #endif // _DISKIO_WRITE
222
 /* Send a command packet to the MMC                                      */
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
   BYTE n, res;
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
     cmd &= 0x7F;
233
     cmd &= 0x7F;
233
     res = send_cmd(CMD55, 0);
234
     res = send_cmd(CMD55, 0);
234
     if (res > 1) return res;
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
   if (cmd != CMD12) {
239
   if (cmd != CMD12) {
239
     deselect();
240
     deselect();
240
     if (!select()) return 0xFF;
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
   xchg_spi(n);
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
   do
258
   do
258
     res = xchg_spi(0xFF);
259
     res = xchg_spi(0xFF);
259
   while ((res & 0x80) && --n);
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
 /*-----------------------------------------------------------------------*/
271
 /*-----------------------------------------------------------------------*/
271
 
272
 
272
 DSTATUS disk_initialize (
273
 DSTATUS disk_initialize (
273
-  BYTE drv    /* Physical drive number (0) */
274
+  BYTE drv                                                            // Physical drive number (0)
274
 ) {
275
 ) {
275
   BYTE n, cmd, ty, ocr[4];
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
   FCLK_SLOW();
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
   ty = 0;
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
           for (n = 0; n < 4; n++) ocr[n] = xchg_spi(0xFF);
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
         ty = 0;
308
         ty = 0;
306
     }
309
     }
307
   }
310
   }
308
-  CardType = ty;  /* Card type */
311
+  CardType = ty;                                                      // Card type
309
   deselect();
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
     Stat = STA_NOINIT;
320
     Stat = STA_NOINIT;
317
   }
321
   }
318
 
322
 
324
 /*-----------------------------------------------------------------------*/
328
 /*-----------------------------------------------------------------------*/
325
 
329
 
326
 DSTATUS disk_status (
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
 /*-----------------------------------------------------------------------*/
339
 /*-----------------------------------------------------------------------*/
336
 
340
 
337
 DRESULT disk_read (
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
   BYTE cmd;
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
   FCLK_FAST();
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
   if (send_cmd(cmd, sector) == 0) {
354
   if (send_cmd(cmd, sector) == 0) {
351
     do {
355
     do {
352
       if (!rcvr_datablock(buff, 512)) break;
356
       if (!rcvr_datablock(buff, 512)) break;
353
       buff += 512;
357
       buff += 512;
354
     } while (--count);
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
   deselect();
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
 #if _DISKIO_WRITE
370
 #if _DISKIO_WRITE
367
 
371
 
368
   DRESULT disk_write(
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
     FCLK_FAST();
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
         && xmit_datablock(buff, 0xFE)) {
386
         && xmit_datablock(buff, 0xFE)) {
383
         count = 0;
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
         do {
393
         do {
390
           if (!xmit_datablock(buff, 0xFC)) break;
394
           if (!xmit_datablock(buff, 0xFC)) break;
391
           buff += 512;
395
           buff += 512;
392
         } while (--count);
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
     deselect();
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
 #endif // _DISKIO_WRITE
405
 #endif // _DISKIO_WRITE
407
 #if _DISKIO_IOCTL
411
 #if _DISKIO_IOCTL
408
 
412
 
409
   DRESULT disk_ioctl (
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
     DRESULT res;
418
     DRESULT res;
415
     BYTE n, csd[16], *ptr = (BYTE *)buff;
419
     BYTE n, csd[16], *ptr = (BYTE *)buff;
420
       UINT dc;
424
       UINT dc;
421
     #endif
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
     res = RES_ERROR;
430
     res = RES_ERROR;
427
     FCLK_FAST();
431
     FCLK_FAST();
428
     switch (cmd) {
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
         if (select()) res = RES_OK;
434
         if (select()) res = RES_OK;
431
         break;
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
         if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) {
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
             csize = csd[9] + ((WORD)csd[8] << 8) + ((DWORD)(csd[7] & 63) << 16) + 1;
440
             csize = csd[9] + ((WORD)csd[8] << 8) + ((DWORD)(csd[7] & 63) << 16) + 1;
437
             *(DWORD*)buff = csize << 10;
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
             n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2;
444
             n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2;
440
             csize = (csd[8] >> 6) + ((WORD)csd[7] << 2) + ((WORD)(csd[6] & 3) << 10) + 1;
445
             csize = (csd[8] >> 6) + ((WORD)csd[7] << 2) + ((WORD)(csd[6] & 3) << 10) + 1;
441
             *(DWORD*)buff = csize << (n - 9);
446
             *(DWORD*)buff = csize << (n - 9);
444
         }
449
         }
445
         break;
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
             xchg_spi(0xFF);
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
               *(DWORD*)buff = 16UL << (csd[10] >> 4);
458
               *(DWORD*)buff = 16UL << (csd[10] >> 4);
454
               res = RES_OK;
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
               *(DWORD*)buff = (((csd[10] & 63) << 1) + ((WORD)(csd[11] & 128) >> 7) + 1) << ((csd[13] >> 6) - 1);
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
               *(DWORD*)buff = ((WORD)((csd[10] & 124) >> 2) + 1) * (((csd[11] & 3) << 3) + ((csd[11] & 224) >> 5) + 1);
469
               *(DWORD*)buff = ((WORD)((csd[10] & 124) >> 2) + 1) * (((csd[11] & 3) << 3) + ((csd[11] & 224) >> 5) + 1);
463
             }
470
             }
464
             res = RES_OK;
471
             res = RES_OK;
466
         }
473
         }
467
         break;
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
         if (!(CardType & CT_BLOCK)) {
481
         if (!(CardType & CT_BLOCK)) {
475
           st *= 512; ed *= 512;
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
         break;
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
         *ptr = CardType;
492
         *ptr = CardType;
486
         res = RES_OK;
493
         res = RES_OK;
487
         break;
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
           res = RES_OK;
498
           res = RES_OK;
492
         }
499
         }
493
         break;
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
           res = RES_OK;
504
           res = RES_OK;
498
         }
505
         }
499
         break;
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
           for (n = 4; n; n--) *ptr++ = xchg_spi(0xFF);
510
           for (n = 4; n; n--) *ptr++ = xchg_spi(0xFF);
504
           res = RES_OK;
511
           res = RES_OK;
505
         }
512
         }
506
         break;
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
           xchg_spi(0xFF);
517
           xchg_spi(0xFF);
511
           if (rcvr_datablock(ptr, 64)) res = RES_OK;
518
           if (rcvr_datablock(ptr, 64)) res = RES_OK;
512
         }
519
         }

Loading…
Cancel
Save