Nano100AN Series BSP  V3.02.002
The Board Support Package for Nano100AN Series
SDCard.c
Go to the documentation of this file.
1 /****************************************************************************/
12 #include <stdio.h>
13 #include "SDCard.h"
14 #include "Nano100Series.h"
15 
23 int8_t Is_Initialized=0,SDtype=0;
25 uint32_t LogicSector=0;
26 
27 // Command table for MMC. This table contains all commands available in SPI
28 // mode; Format of command entries is described above in command structure
29 // definition;
30 COMMAND __I command_list[] =
31 {
32  { 0,NO,0x95,CMD,R1,NO }, // CMD0; GO_IDLE_STATE: reset card;
33  { 1,NO,0xFF,CMD,R1,NO }, // CMD1; SEND_OP_COND: initialize card;
34  { 8,YES,0xFF,CMD,R7,NO }, // CMD8; SEND_IF_COND
35  { 9,NO,0xFF,RD,R1,NO }, // CMD9; SEND_CSD: get card specific data;
36  {10,NO,0xFF,RD,R1,NO }, // CMD10; SEND_CID: get card identifier;
37  {12,NO,0xFF,CMD,R1b,NO }, // CMD12; STOP_TRANSMISSION: end read;
38  {13,NO,0xFF,CMD,R2,NO }, // CMD13; SEND_STATUS: read card status;
39  {16,YES,0xFF,CMD,R1,NO }, // CMD16; SET_BLOCKLEN: set block size;
40  {17,YES,0xFF,RDB,R1,NO }, // CMD17; READ_SINGLE_BLOCK: read 1 block;
41  {18,YES,0xFF,RD,R1,YES}, // CMD18; READ_MULTIPLE_BLOCK: read > 1;
42  {23,NO,0xFF,CMD,R1,NO }, // CMD23; SET_BLOCK_COUNT
43  {24,YES,0xFF,WR,R1,NO }, // CMD24; WRITE_BLOCK: write 1 block;
44  {25,YES,0xFF,WR,R1,YES}, // CMD25; WRITE_MULTIPLE_BLOCK: write > 1;
45  {27,NO,0xFF,CMD,R1,NO }, // CMD27; PROGRAM_CSD: program CSD;
46  {28,YES,0xFF,CMD,R1b,NO }, // CMD28; SET_WRITE_PROT: set wp for group;
47  {29,YES,0xFF,CMD,R1b,NO }, // CMD29; CLR_WRITE_PROT: clear group wp;
48  {30,YES,0xFF,CMD,R1,NO }, // CMD30; SEND_WRITE_PROT: check wp status;
49  {32,YES,0xFF,CMD,R1,NO }, // CMD32; TAG_SECTOR_START: tag 1st erase;
50  {33,YES,0xFF,CMD,R1,NO }, // CMD33; TAG_SECTOR_END: tag end(single);
51  {34,YES,0xFF,CMD,R1,NO }, // CMD34; UNTAG_SECTOR: deselect for erase;
52  {35,YES,0xFF,CMD,R1,NO }, // CMD35; TAG_ERASE_GROUP_START;
53  {36,YES,0xFF,CMD,R1,NO }, // CMD36; TAG_ERASE_GROUP_END;
54  {37,YES,0xFF,CMD,R1,NO }, // CMD37; UNTAG_ERASE_GROUP;
55  {38,YES,0xFF,CMD,R1b,NO }, // CMD38; ERASE: erase all tagged sectors;
56  {42,YES,0xFF,CMD,R1,NO }, // CMD42; LOCK_UNLOCK;
57  {55,NO,0xFF,CMD,R1,NO }, // CMD55; APP_CMD
58  {58,NO,0xFF,CMD,R3,NO }, // CMD58; READ_OCR: read OCR register;
59  {59,YES,0xFF,CMD,R1,NO }, // CMD59; CRC_ON_OFF: toggles CRC checking;
60  {0x80+13,NO,0xFF,CMD,R2,NO }, // ACMD13; SD_SEND_STATUS: read card status;
61  {0x80+23,YES,0xFF,CMD,R1,NO }, // ACMD23;SD_SET_WR_BLK_ERASE_COUNT
62  {0x80+41,YES,0xFF,CMD,R1,NO } // ACMD41; SD_SEND_OP_COND: initialize card;
63 };
65 
74 void SD_Delay(uint32_t count)
75 {
76  uint32_t volatile loop;
77  for(loop=0; loop<count; loop++);
78 }
79 /*---------------------------------------------------------------------------------------------------------*/
80 /* SD CARD Protocol */
81 /*---------------------------------------------------------------------------------------------------------*/
82 /*
83  Transfer Length:48bit
84  BIT POSITION WIDTH(BITS) VAULE
85  [47] 1 0:Start bit
86  [46] 1 1:Transmit 0:Receive
87  [45:40] 6 CMD8:001000
88  [39:8] 32 Reserved
89  [7:1] 7 Reserved
90  [0] 1 1:End bit
91 
92 */
93 
101 static uint32_t GenerateCRC(uint32_t u32Data, uint32_t u32GenPoly, uint32_t u32Accum)
102 {
103  volatile uint8_t i;
104 
105  u32Data <<= 8;
106  for (i=8; i>0; i--)
107  {
108  if ((u32Data ^ u32Accum) & 0x8000)
109  u32Accum = (u32Accum << 1) ^ u32GenPoly;
110  else
111  u32Accum <<= 1;
112  u32Data <<= 1;
113  }
114  return u32Accum;
115 }
116 
122 static uint32_t SingleWrite(uint32_t u32Data)
123 {
124  SPI_WRITE_TX0(SPI1, u32Data);
125  SPI_TRIGGER(SPI1);
126  while(SPI_IS_BUSY(SPI1));
127 
128  return SPI_READ_RX0(SPI1);
129 }
130 
140 uint32_t MMC_Command_Exec (uint8_t nCmd, uint32_t nArg,uint8_t *pchar, uint32_t *response)
141 {
142  uint8_t loopguard;
143  COMMAND current_command; // Local space for the command table
144  UINT32 long_arg; // Local space for argument
145  static uint32_t current_blklen = 512;
146  uint32_t old_blklen = 512;
147  int32_t counter = 0; // Byte counter for multi-byte fields;
148  UINT16 card_response; // Variable for storing card response;
149  uint8_t data_resp; // Variable for storing data response;
150  UINT16 dummy_CRC; // Dummy variable for storing CRC field;
151 
152  card_response.i = 0;
153 
154  current_command = command_list[nCmd];// Retrieve desired command table entry
155  // from code space;
156  if(current_command.command_byte & 0x80) // Detect ACMD
157  {
158  if(MMC_Command_Exec(APP_CMD,EMPTY,EMPTY,response)==FALSE)//Send APP_CMD
159  return FALSE;
160  }
161 
162  SPI_SET_SS0_LOW(SPI1); // CS = 0
163 
164  SingleWrite(0xFF);
165  SingleWrite((current_command.command_byte | 0x40)&0x7f);
166  DBG_PRINTF("CMD:%d,",current_command.command_byte&0x7f);
167 
168  long_arg.l = nArg; // Make argument byte addressable;
169  // If current command changes block
170  // length, update block length variable
171  // to keep track;
172  // Command byte = 16 means that a set
173  // block length command is taking place
174  // and block length variable must be
175  // set;
176  if(current_command.command_byte == 16)
177  {
178  current_blklen = nArg;
179  }
180  // Command byte = 9 or 10 means that a
181  // 16-byte register value is being read
182  // from the card, block length must be
183  // set to 16 bytes, and restored at the
184  // end of the transfer;
185  if((current_command.command_byte == 9)||(current_command.command_byte == 10))
186  {
187  old_blklen = current_blklen; // Command is a GET_CSD or GET_CID,
188  current_blklen = 16; // set block length to 16-bytes;
189  }
190  // If an argument is required, transmit
191  // one, otherwise transmit 4 bytes of
192  // 0x00;
193  if(current_command.arg_required == YES)
194  {
195  dummy_CRC.i = GenerateCRC((current_command.command_byte | 0x40), 0x1200, 0);
196  for(counter=3; counter>=0; counter--)
197  {
198  SingleWrite(long_arg.b[counter]);
199  dummy_CRC.i = GenerateCRC(long_arg.b[counter], 0x1200, dummy_CRC.i);
200 
201  }
202  dummy_CRC.i = (dummy_CRC.i >> 8)| 0x01;
203  SingleWrite(dummy_CRC.b[0]);
204  }
205  else
206  {
207  counter = 0;
208  while(counter <= 3)
209  {
210  SingleWrite(0x00);
211  counter++;
212  }
213  SingleWrite(current_command.CRC);
214  }
215 
216  // The command table entry will indicate
217  // what type of response to expect for
218  // a given command; The following
219  // conditional handles the MMC response;
220  if(current_command.response == R1) // Read the R1 response from the card;
221  {
222  loopguard=0;
223  do
224  {
225  card_response.b[0] = SingleWrite(0xFF);
226  if(!++loopguard) break;
227  }
228  while((card_response.b[0] & BUSY_BIT));
229  DBG_PRINTF("R1:0x%x, counter:%d\n",card_response.b[0],loopguard);
230  if(!loopguard)
231  {
233  }
234  *response=card_response.b[0];
235  }
236  else if(current_command.response == R1b) // Read the R1b response;
237  {
238  loopguard = 0;
239  do
240  {
241  card_response.b[0] = SingleWrite(0xFF);
242  if(!++loopguard) break;
243  }
244  while((card_response.b[0] & BUSY_BIT));
245  while((SingleWrite(0xFF)&0xFF) == 0x00);
246  }
247  else if(current_command.response == R2)
248  {
249  loopguard=0;
250  do
251  {
252  card_response.b[0] = SingleWrite(0xFF);
253  if(!++loopguard) break;
254  }
255  while((card_response.b[0] & BUSY_BIT));
256  card_response.b[1] = SingleWrite(0xFF);
257  DBG_PRINTF("R2:0x%x, counter:%d\n",card_response.i,loopguard);
258  if(!loopguard)
259  {
261  }
262  *response=card_response.i;
263  }
264  else if(current_command.response == R3)
265  {
266  // Read R3 response;
267  loopguard=0;
268  do
269  {
270  card_response.b[0] = SingleWrite(0xFF);
271  if(!++loopguard) break;
272  }
273  while((card_response.b[0] & BUSY_BIT));
274  DBG_PRINTF("R3:0x%x, counter:%d\n",card_response.b[0],loopguard);
275  if(!loopguard)
276  {
278  }
279  counter = 0;
280  while(counter <= 3) // Read next three bytes and store them
281  {
282  // in local memory; These bytes make up
283  counter++; // the Operating Conditions Register
284  *pchar++ = SingleWrite(0xFF);
285  }
286  *response=card_response.b[0];
287  }
288  else
289  {
290  // Read R7 response;
291  loopguard=0;
292  do
293  {
294  card_response.b[0] = SingleWrite(0xFF);
295  if(!++loopguard) break;
296  }
297  while((card_response.b[0] & BUSY_BIT));
298  DBG_PRINTF("R7:0x%x, counter:%d\n",card_response.b[0],loopguard);
299  if(!loopguard)
300  {
302  }
303  counter = 0;
304  while(counter <= 3) // Read next three bytes and store them
305  {
306  // in local memory; These bytes make up
307  counter++; // the Operating Conditions Register
308  *pchar++ = SingleWrite(0xFF);
309  }
310  *response=card_response.b[0];
311  }
312 
313 
314  switch(current_command.trans_type) // This conditional handles all data
315  {
316  // operations; The command entry
317  // determines what type, if any, data
318  // operations need to occur;
319  case RDB: // Read data from the MMC;
320  loopguard = 0;
321 
322  while((SingleWrite(0xFF)&0xFF)!=START_SBR)
323  {
324  if(!++loopguard)
325  {
327  }
328  SD_Delay(1);
329  }
330  counter = 0; // Reset byte counter;
331  // Read <current_blklen> bytes;
332 
333  SPI_WRITE_TX0(SPI1, 0xFFFFFFFF);
334  if(pchar)
335  {
336  //SPI_SetBitLength(SPI1, 8);
337  //SPI_SetFIFOMode(SPI1, TRUE, 2);
338  //SPI_FIFOReadWrite8(SPI1, NULL, pchar, current_blklen);
339  //SPI_SetFIFOMode(SPI1, FALSE, 2);
340 
341  for (counter=0; counter<current_blklen; counter++)
342  {
343  SPI_WRITE_TX0(SPI1, 0xFF);
344  SPI_TRIGGER(SPI1);
345  while(SPI_IS_BUSY(SPI1));
346  *(pchar+counter)=SPI_READ_RX0(SPI1);
347  }
348  }
349  else
350  {
351  for (; counter<current_blklen; counter++)
352  {
353  SPI_TRIGGER(SPI1);
354  while(SPI_IS_BUSY(SPI1));
355  }
356  }
357  dummy_CRC.b[1] = SingleWrite(0xFF); // After all data is read, read the two
358  dummy_CRC.b[0] = SingleWrite(0xFF); // CRC bytes; These bytes are not used
359  // in this mode, but the place holders
360  // must be read anyway;
361  break;
362  case RD: // Read data from the MMC;
363  loopguard = 0;
364 
365  while((SingleWrite(0xFF)&0xFF)!=START_SBR)
366  {
367  if(!++loopguard)
368  {
370  }
371  }
372  counter = 0; // Reset byte counter;
373  // Read <current_blklen> bytes;
374  if(pchar)
375  {
376  for (counter=0; counter<current_blklen; counter++)
377  {
378  SPI_WRITE_TX0(SPI1, 0xFF);
379  SPI_TRIGGER(SPI1);
380  while(SPI_IS_BUSY(SPI1));
381  *(pchar+counter)=SPI_READ_RX0(SPI1);
382  }
383  }
384  else
385  {
386  for (counter=0; counter<current_blklen; counter++)
387  {
388  SPI_WRITE_TX0(SPI1, 0xFF);
389  SPI_TRIGGER(SPI1);
390  while(SPI_IS_BUSY(SPI1));
391  }
392  }
393  dummy_CRC.b[1] = SingleWrite(0xFF); // After all data is read, read the two
394  dummy_CRC.b[0] = SingleWrite(0xFF); // CRC bytes; These bytes are not used
395  // in this mode, but the place holders
396  // must be read anyway;
397  break;
398 
399  case WR:
400  SingleWrite(0xFF);
402 
403  for (counter=0; counter<current_blklen; counter++)
404  {
405  SPI_WRITE_TX0(SPI1, *(pchar+counter));
406  SPI_TRIGGER(SPI1);
407  dummy_CRC.i = GenerateCRC(*(pchar+counter), 0x1021, dummy_CRC.i);
408  while(SPI_IS_BUSY(SPI1));
409  }
410  SingleWrite(dummy_CRC.b[1]);
411  SingleWrite(dummy_CRC.b[0]);
412 
413  loopguard = 0;
414  do // Read Data Response from card;
415  {
416  data_resp = SingleWrite(0xFF);
417  if(!++loopguard) break;
418  }
419  while((data_resp & DATA_RESP_MASK) != 0x01); // When bit 0 of the MMC response
420  // is clear, a valid data response
421  // has been received;
422 
423  if(!loopguard)
424  {
426  }
427 
428 
429  while((SingleWrite(0xFF)&0xFF)!=0xFF);//Wait for Busy
430  SingleWrite(0xFF);
431  break;
432  default:
433  break;
434  }
435  SPI_SET_SS0_HIGH(SPI1); // CS = 1
436  if((current_command.command_byte == 9)||(current_command.command_byte == 10))
437  {
438  current_blklen = old_blklen;
439  }
440  return TRUE;
441 }
442 
447 void MMC_FLASH_Init(void)
448 {
449  uint32_t response;
450  uint16_t loopguard;
451  uint32_t i;
452  uint8_t counter = 0;
453  uint8_t pchar[16]; // Data pointer for storing MMC
454  uint32_t c_size,bl_len;
455  uint8_t c_mult;
456 
457 
458  Is_Initialized = 0;
459 
460 re_init:
461  SPI_SET_SS0_HIGH(SPI1); // CS = 1
462  SD_Delay(1000);
463  //--------------------------------------------------------
464  // Send 74 SD clcok in SD mode for Toshiba SD Card
465  //--------------------------------------------------------
466  for(counter = 0; counter < 10; counter++)
467  {
468  SingleWrite(0xFF);
469  }
470  SD_Delay(1000);
471 
472  SPI_SET_SS0_LOW(SPI1); // CS = 0
473  while(MMC_Command_Exec(GO_IDLE_STATE,EMPTY,EMPTY,&response)==FALSE)
474  SD_Delay(1000);
475  if(response!=0x01)
476  goto re_init;
477 
478  if(MMC_Command_Exec(SEND_IF_COND,0x15A,pchar,&response) && response==1)
479  {
480  /* SDC ver 2.00 */
481  if (pchar[2] == 0x01 && pchar[3] == 0x5A)
482  {
483  /* The card can work at VDD range of 2.7-3.6V */
484  loopguard=0;
485  do
486  {
487  MMC_Command_Exec(SD_SEND_OP_COND,0x40000000,EMPTY,&response);//Enable HCS(OCR[30])
488  if(!++loopguard) break;
489  SD_Delay(0x100);
490  }
491  while(response!=0);
492  if(!loopguard)
493  return;
494 
495  MMC_Command_Exec(READ_OCR,EMPTY,pchar,&response);
496  SDtype=(pchar[0]&0x40)?SDv2|SDBlock:SDv2;
497  }
498  }
499  else
500  {
501  /* SDv1 or MMCv3 */
502  MMC_Command_Exec(SD_SEND_OP_COND,0x00,EMPTY,&response);
503  if (response <= 1)
504  {
505  loopguard=0;
506  do
507  {
508  MMC_Command_Exec(SD_SEND_OP_COND,0x00,EMPTY,&response);
509  if(!++loopguard) break;
510  SD_Delay(50);
511  }
512  while(response!=0);
513  if(!loopguard)
514  return;
515  SDtype=SDv1; /* SDv1 */
516  }
517  else
518  {
519  loopguard=0;
520  do
521  {
522  MMC_Command_Exec(SEND_OP_COND,0x00,EMPTY,&response);
523  if(!++loopguard) break;
524  SD_Delay(50);
525  }
526  while(response!=0);
527  if(!loopguard)
528  return;
529  SDtype=MMCv3; /* MMCv3 */
530  }
532  }
533  if(MMC_Command_Exec(SEND_CSD,EMPTY,pchar,&response)==FALSE)
534  return;
535 
536  if(response==0)
537  {
538  DBG_PRINTF("Change speed:");
539  for(i=0; i<16; i++)
540  {
541  DBG_PRINTF("0x%X ",pchar[i]);
542  }
543 
544  }
545  else
546  {
547  DBG_PRINTF("CARD STATUS 0x%X:\n",response);
548  for(i=0; i<16; i++)
549  {
550  DBG_PRINTF("0x%X ",pchar[i]);
551  }
552  LogicSector=0;
553  return;
554  }
555 
556  if(SDtype&SDBlock) // Determine the number of MMC sectors;
557  {
558  bl_len = 1 << (pchar[5] & 0x0f) ;
559  c_size = ((pchar[7] & 0x3F) << 16) |(pchar[8] << 8) | (pchar[9]);
560  LogicSector=c_size*((512*1024)/bl_len);
561  }
562  else
563  {
564  bl_len = 1 << (pchar[5] & 0x0f) ;
565  c_size = ((pchar[6] & 0x03) << 10) |(pchar[7] << 2) | ((pchar[8] &0xc0) >> 6);
566  c_mult = (((pchar[9] & 0x03) << 1) | ((pchar[10] & 0x80) >> 7));
567  LogicSector=(c_size+1)*(1 << (c_mult+2))*(bl_len/512);
568  }
569  DBG_PRINTF("\nLogicSector:%d, PHYSICAL_SIZE:%dMB\n",LogicSector,(LogicSector/2/1024));
570 
571  loopguard = 0;
572  while((MMC_Command_Exec(READ_SINGLE_BLOCK,0,0,&response)==FALSE))
573  {
574  if(!++loopguard) break;
575  }
576  Is_Initialized = 1;
577 }
578 
584 uint32_t SDCARD_Open(void)
585 {
586  /* Configure SPI1 as a master, 8-bit transaction*/
587  SPI_Open(SPI1, SPI_MASTER, SPI_MODE_0, 8, 300000);
588 
591 
592  DBG_PRINTF("SPI is running at %d Hz\n", SPI_GetBusClock(SPI1));
593 
594  MMC_FLASH_Init();
595  SD_Delay(3000);
596 
597  if (Is_Initialized)
598  DBG_PRINTF("SDCARD INIT OK\n\n");
599  else
600  {
601  DBG_PRINTF("SDCARD INIT FAIL\n\n");
602  return SD_FAIL;
603  }
604 
605  SPI_SetBusClock(SPI1, 5000000);
606  DBG_PRINTF("Now, SPI is running at %d Hz\n", SPI_GetBusClock(SPI1));
607 
608  return SD_SUCCESS;
609 }
610 
615 void SDCARD_Close(void)
616 {
617  SPI_Close(SPI1);
618 }
619 
626 uint32_t SDCARD_GetCardSize(uint32_t *pu32TotSecCnt)
627 {
628  if (LogicSector == 0)
629  return FALSE;
630  else
631  *pu32TotSecCnt = LogicSector;
632 
633  return TRUE;
634 }
635 
640 uint32_t GetLogicSector(void)
641 {
642  return LogicSector;
643 }
644 
652 void SpiRead(uint32_t addr, uint32_t size, uint8_t* buffer)
653 {
654  /* This is low level read function of USB Mass Storage */
655  uint32_t response;
656  if(SDtype&SDBlock)
657  {
658  while(size >= PHYSICAL_BLOCK_SIZE)
659  {
660  MMC_Command_Exec(READ_SINGLE_BLOCK,addr,buffer,&response);
661  addr ++;
662  buffer += PHYSICAL_BLOCK_SIZE;
663  size -= PHYSICAL_BLOCK_SIZE;
664  }
665 
666  }
667  else
668  {
669  addr*=PHYSICAL_BLOCK_SIZE;
670  while(size >= PHYSICAL_BLOCK_SIZE)
671  {
672  MMC_Command_Exec(READ_SINGLE_BLOCK,addr,buffer,&response);
673  addr += PHYSICAL_BLOCK_SIZE;
674  buffer += PHYSICAL_BLOCK_SIZE;
675  size -= PHYSICAL_BLOCK_SIZE;
676  }
677  }
678 }
679 
687 void SpiWrite(uint32_t addr, uint32_t size, uint8_t* buffer)
688 {
689  uint32_t response;
690  if(SDtype&SDBlock)
691  {
692  while(size >= PHYSICAL_BLOCK_SIZE)
693  {
694  MMC_Command_Exec(WRITE_BLOCK,addr,buffer,&response);
695  addr ++;
696  buffer += PHYSICAL_BLOCK_SIZE;
697  size -= PHYSICAL_BLOCK_SIZE;
698  }
699  }
700  else
701  {
702  addr*=PHYSICAL_BLOCK_SIZE;
703  while(size >= PHYSICAL_BLOCK_SIZE)
704  {
705  MMC_Command_Exec(WRITE_BLOCK,addr,buffer,&response);
706  addr += (PHYSICAL_BLOCK_SIZE);
707  buffer += PHYSICAL_BLOCK_SIZE;
708  size -= PHYSICAL_BLOCK_SIZE;
709  }
710  }
711 } /* end of group NANO100_SDCARD_EXPORTED_FUNCTIONS */
713  /* end of group NANO100_SDCARD_Driver */
715  /* end of group NANO100_Library */
717 
718 /*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/
void SDCARD_Close(void)
This function is used to close SDCARD.
Definition: SDCard.c:615
void SPI_DisableAutoSS(SPI_T *spi)
Disable the automatic slave select function.
Definition: spi.c:105
#define DATA_RESP_MASK
Definition: SDCard.h:65
#define CMD
Definition: SDCard.h:45
uint32_t SPI_SetBusClock(SPI_T *spi, uint32_t u32BusClock)
Set the SPI bus clock. Only available in Master mode.
Definition: spi.c:128
#define NO
Definition: SDCard.h:44
Definition: SDCard.h:72
#define SD_SUCCESS
Definition: SDCard.h:83
uint32_t SDCARD_Open(void)
This function is used to Open GPIO function and initial SDCARD.
Definition: SDCard.c:584
#define FALSE
Boolean false, define to use in API parameters or return value.
#define SPI_IS_BUSY(spi)
Get the SPI busy state.
Definition: spi.h:283
#define SPI_TRIGGER(spi)
Set the GO_BUSY bit to trigger SPI transfer.
Definition: spi.h:291
#define SPI_READ_RX0(spi)
Get the datum read from RX0 FIFO.
Definition: spi.h:136
#define SPI1
Pointer to SPI1 register structure.
#define SPI_SET_MSB_FIRST(spi)
Set the SPI transfer sequence with MSB first.
Definition: spi.h:258
void SpiRead(uint32_t addr, uint32_t size, uint8_t *buffer)
This function is used to Get data from SD card.
Definition: SDCard.c:652
Nano100 series peripheral access layer header file. This file contains all the peripheral register's ...
#define R2
Definition: SDCard.h:52
void SD_Delay(uint32_t count)
Delay function.
Definition: SDCard.c:74
#define DBG_PRINTF(...)
Definition: SDCard.h:34
#define RD
Definition: SDCard.h:46
static __INLINE void SPI_SET_SS0_HIGH(SPI_T *spi)
Disable automatic slave select function and set SPI_SS pin to high state.
Definition: spi.h:169
Nano100 series SD Card driver header file.
#define SD_FAIL
Definition: SDCard.h:84
#define WR
Definition: SDCard.h:47
#define APP_CMD
Definition: SDCard.h:120
uint32_t SDCARD_GetCardSize(uint32_t *pu32TotSecCnt)
This function is used to get card total sector after SDCARD is opened.
Definition: SDCard.c:626
void SpiWrite(uint32_t addr, uint32_t size, uint8_t *buffer)
This function is used to store data into SD card.
Definition: SDCard.c:687
void MMC_FLASH_Init(void)
This function is used to initialize the flash card.
Definition: SDCard.c:447
static __INLINE void SPI_SET_SS0_LOW(SPI_T *spi)
Disable automatic slave select function and set SPI_SS pin to low state.
Definition: spi.h:183
#define SPI_WRITE_TX0(spi, u32TxData)
Write datum to TX0 register.
Definition: spi.h:152
uint32_t GetLogicSector(void)
This function is used to get logic sector size.
Definition: SDCard.c:640
#define MMCv3
Definition: SDCard.h:92
#define GO_IDLE_STATE
Definition: SDCard.h:95
void SPI_Close(SPI_T *spi)
Reset SPI module.
Definition: spi.c:60
#define SEND_CSD
Definition: SDCard.h:98
#define PHYSICAL_BLOCK_SIZE
Definition: SDCard.h:37
#define SEND_IF_COND
Definition: SDCard.h:97
#define R7
Definition: SDCard.h:54
uint8_t trans_type
Definition: SDCard.h:137
#define RDB
Definition: SDCard.h:48
#define SPI_MASTER
Definition: spi.h:40
uint8_t command_byte
Definition: SDCard.h:134
uint8_t arg_required
Definition: SDCard.h:135
uint32_t SPI_GetBusClock(SPI_T *spi)
Get the actual frequency of SPI bus clock. Only available in Master mode.
Definition: spi.c:176
static uint32_t SingleWrite(uint32_t u32Data)
This function is used to send data though SPI to general clock for SDCARD operation.
Definition: SDCard.c:122
#define R1b
Definition: SDCard.h:51
#define SDv1
Definition: SDCard.h:90
uint8_t b[4]
Definition: SDCard.h:75
#define R1
Definition: SDCard.h:50
#define EMPTY
Definition: SDCard.h:42
#define SDBlock
Definition: SDCard.h:93
#define READ_SINGLE_BLOCK
Definition: SDCard.h:103
static uint32_t GenerateCRC(uint32_t u32Data, uint32_t u32GenPoly, uint32_t u32Accum)
This function is used to generate CRC value.
Definition: SDCard.c:101
uint8_t CRC
Definition: SDCard.h:136
uint16_t i
Definition: SDCard.h:79
uint8_t b[2]
Definition: SDCard.h:80
#define READ_OCR
Definition: SDCard.h:121
#define BACK_FROM_ERROR
Definition: SDCard.h:70
#define SPI_MODE_0
Definition: spi.h:34
uint32_t MMC_Command_Exec(uint8_t nCmd, uint32_t nArg, uint8_t *pchar, uint32_t *response)
This function is used to Send SDCARD CMD and Receive Response.
Definition: SDCard.c:140
uint32_t SPI_Open(SPI_T *spi, uint32_t u32MasterSlave, uint32_t u32SPIMode, uint32_t u32DataWidth, uint32_t u32BusClock)
This function make SPI module be ready to transfer. By default, the SPI transfer sequence is MSB firs...
Definition: spi.c:41
uint8_t response
Definition: SDCard.h:138
#define YES
Definition: SDCard.h:43
#define R3
Definition: SDCard.h:53
#define SD_SEND_OP_COND
Definition: SDCard.h:125
#define BUSY_BIT
Definition: SDCard.h:68
#define TRUE
Boolean true, define to use in API parameters or return value.
#define WRITE_BLOCK
Definition: SDCard.h:106
#define START_SBR
Definition: SDCard.h:58
uint32_t l
Definition: SDCard.h:74
#define START_SBW
Definition: SDCard.h:60
Definition: SDCard.h:77
#define SEND_OP_COND
Definition: SDCard.h:96
#define SET_BLOCKLEN
Definition: SDCard.h:102
#define SDv2
Definition: SDCard.h:91