⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mci_lpc23xx.c

📁 LPC23XX USB读写SD卡 例程 来自MCU123开发板配套光盘
💻 C
📖 第 1 页 / 共 2 页
字号:
   }

   for (i = 0; i < 20; i++) {
      rstat = mci_command (SELECT_CARD, arg, RESP_SHORT, &rval);
      if (rstat == 0 && (rval & 0x0F00) == 0x0700) {
         /* Should be in STBY state now and ready. */
         return (__TRUE);
      }
   }
   return (__FALSE);
}


/*--------------------------- mci_set_bus_4bit ------------------------------*/

static BOOL mci_set_bus_4bit (void) {
   /* Select 4-bit bus width for SD Card. */
   U32 i,rstat,rval;

   for (i = 0; i < 20; i++) {
      if (mci_send_acmd () == __FALSE) {
         continue;
      }
      /* Send ACMD6 command to set the bus width. */
      rstat = mci_command (SET_ACMD_BUS_WIDTH, BUS_WIDTH_4BITS, RESP_SHORT, &rval);
      if (rstat == 0 && (rval & 0x0F00) == 0x0900) {
         /* Response is back and correct. */
         return (__TRUE);
      }
   }
   return (__FALSE);
}


/*--------------------------- mci_set_block_len -----------------------------*/

static BOOL mci_set_block_len (void) {
   /* Set block length to 512 bytes. */
   U32 i,rstat,rval;

   for (i = 0; i < 20; i++) {
      /* Send ACMD6 command to set the bus width. */
      rstat = mci_command (SET_BLOCK_LEN, 512, RESP_SHORT, &rval);
      if (rstat == 0 && (rval & 0x0F00) == 0x0900) {
         /* Response is back and correct. */
         return (__TRUE);
      }
   }
   return (__FALSE);
}


/*--------------------------- mci_cmd_read_block ----------------------------*/

static BOOL mci_cmd_read_block (U32 block) {
   /* Send a command to Read block. */
   U32 i,rstat,rval;

   block *= 512;
   for (i = 0; i < 20; i++) {
      rstat = mci_command (READ_SINGLE_BLOCK, block, RESP_SHORT, &rval);
      if (rstat == 0 && (rval & 0x0F00) == 0x0900) {
         /* Ready and in TRAN state. */
         return (__TRUE);
      }
   }
   return (__FALSE);
}


/*--------------------------- mci_cmd_write_block ---------------------------*/

static BOOL mci_cmd_write_block (U32 block) {
   /* Send a command to Write block. */
   U32 i,rstat,rval;

   block *= 512;
   for (i = 0; i < 20; i++) {
      rstat = mci_command (WRITE_BLOCK, block, RESP_SHORT, &rval);
      if (rstat == 0 && (rval & 0x0F00) == 0x0900) {
         /* Ready and in TRAN state. */
         return (__TRUE);
      }
   }
   return (__FALSE);
}


/*--------------------------- mci_read_status -------------------------------*/

static U32 mci_read_status (void) {
   /* Read the status of Flash Card. */
   U32 i,arg,rstat,rval;

   arg = 0x00010000;
   if (CardType == CARD_SD) {
      /* Use address from SET_RELATIVE_ADDR. */
      arg = CardRCA << 16;
   }

   for (i = 0; i < 200; i++) {
      rstat = mci_command (SEND_STATUS, arg, RESP_SHORT, &rval);
      if (rstat == 0 && (rval & 0x0100)) {
         /* The Ready bit should be set, state TRAN or RCV. */
         return (rval);
      }
   }
   return (MCI_RESP_INVALID);
}


/*--------------------------- mci_send_stop ---------------------------------*/

static BOOL mci_send_stop (void) {
   /* Stop transmission, Flash Card is in wrong state. */
   U32 i,rstat,rval;

   for (i = 0; i < 20; i++) {
      rstat = mci_command (STOP_TRANSMISSION, 0, RESP_SHORT, &rval);
      if (rstat == 0 && (rval & 0x0100)) {
         /* The Ready bit should be set. */
         return (__TRUE);
      }
   }
   return (__FALSE);
}


/*--------------------------- mmc_command -----------------------------------*/

static U32 mci_command (U8 cmd, U32 arg, U32 resp_type, U32 *rp) {
   /* Send a Command to Flash card and get a Response. */
   U32 cmdval,stat;

   cmd   &= 0x3F;
   cmdval = 0x400 | cmd;
   switch (resp_type) {
      case RESP_SHORT:
         cmdval |= 0x40;
         break;
      case RESP_LONG:
         cmdval |= 0xC0;
         break;
   }
   /* Send the command. */
   MCI_ARGUMENT = arg;
   MCI_COMMAND  = cmdval;

   if (resp_type == RESP_NONE) {
      /* Wait until command finished. */
      while (MCI_STATUS & MCI_CMD_ACTIVE);
      MCI_CLEAR = 0x7FF;
      return (0);
   }

   for (;;) {
      stat = MCI_STATUS;
      if (stat & MCI_CMD_TIMEOUT) {
         MCI_CLEAR = stat & MCI_CLEAR_MASK;
         return (stat);
      }
      if (stat & MCI_CMD_CRC_FAIL) {
         MCI_CLEAR = stat & MCI_CLEAR_MASK;
         if ((cmd == SEND_OP_COND)      ||
             (cmd == SEND_APP_OP_COND)  ||
             (cmd == STOP_TRANSMISSION)) {
            MCI_COMMAND = 0;
            break;
         }
         return (stat);
      }
      if (stat & MCI_CMD_RESP_END) {
         MCI_CLEAR = stat & MCI_CLEAR_MASK;
         break;
      }
   }
   if ((MCI_RESP_CMD & 0x3F) != cmd) {
      if ((cmd != SEND_OP_COND)     &&
          (cmd != SEND_APP_OP_COND) &&
          (cmd != ALL_SEND_CID)     &&
          (cmd != SEND_CSD))         {
         return (MCI_RESP_INVALID);
      }
   }
   if (rp == NULL) {
      /* Response pointer undefined. */
      return (MCI_RESP_INVALID);
   }
   /* Read MCI response registers */
   rp[0] = MCI_RESP0;
   if (resp_type == RESP_LONG) {
      rp[1] = MCI_RESP1;
      rp[2] = MCI_RESP2;
      rp[3] = MCI_RESP3;
   }
   return (0);
}


/*--------------------------- mci_dma_start ---------------------------------*/

static void mci_dma_start (U32 mode, U8 *buf) {
   /* Configure DMA controller Ch0 for read or write. */

   if (mode == DMA_READ) {
      /* Transfer from MCI-FIFO to memory. */
      GPDMA_CH0_SRC  = (U32)&MCI_FIFO;
      GPDMA_CH0_DEST = (U32)buf;
      /* The burst size set to 8, transfer size 512 bytes. */
      GPDMA_CH0_CTRL = (512 >> 2)   | (0x02 << 12) | (0x02 << 15) | 
                       (0x02 << 18) | (0x02 << 21) | (1 << 27)    | (1u << 31);
      GPDMA_CH0_CFG  = 0x10001 | (0x04 << 1) | (0x00 << 6) | (0x06 << 11);
   }
   else {
      /* Transfer from memory to MCI-FIFO. */
      GPDMA_CH0_SRC  = (U32)buf;
      GPDMA_CH0_DEST = (U32)&MCI_FIFO;
      /* The burst size set to 8, transfer size 512 bytes. */
      GPDMA_CH0_CTRL = (512 >> 2)   | (0x02 << 12) | (0x02 << 15) |
                       (0x02 << 18) | (0x02 << 21) | (1 << 26)    | (1u << 31);
      GPDMA_CH0_CFG  = 0x10001 | (0x00 << 1) | (0x04 << 6) | (0x05 << 11);
   }
   /* Enable DMA channels, little endian */
   GPDMA_INT_TCCLR = 0x01;
   GPDMA_CONFIG    = 0x01;
}


/*--------------------------- mci_read_sect ---------------------------------*/

BOOL mci_read_sect (U32 sect, U8 *buf) {
   /* Read a 512 byte sector from Flash Card. */
   U32 i;

   if (mci_cmd_read_block (sect) == __FALSE) {
      /* Command Failed. */
      return (__FALSE);
   }

   /* Set MCI Transfer registers. */
   MCI_DATA_TMR  = DATA_TOUT_VALUE;
   MCI_DATA_LEN  = 512;

   /* Start DMA Peripheral to Memory transfer. */
   mci_dma_start (DMA_READ, buf);
   MCI_DATA_CTRL = 0x9B;

   for (i = 10000; i; i--) {
      if (GPDMA_RAW_INT_TCSTAT & 0x01) {
         /* Data transfer finished. */
         break;
      }
   }
   MCI_DATA_CTRL = 0x00;

   if ((i == 0) || (mci_read_status () & 0x0F00) != 0x0900) {
      /* Timeout or Wrong state, reset the card. */
      mci_send_stop ();
      return (__FALSE);
   }
   return (__TRUE);
}


/*--------------------------- mci_write_sect --------------------------------*/

BOOL mci_write_sect (U32 sect, U8 *buf) {
   /* Write a 512 byte sector to Flash Card. */
   U32 i;

   if (mci_cmd_write_block (sect) == __FALSE) {
      /* Command Failed. */
      return (__FALSE);
   }

   /* Set MCI Transfer registers. */
   MCI_DATA_TMR  = DATA_TOUT_VALUE;
   MCI_DATA_LEN  = 512;

   /* Start DMA Memory to Peripheral transfer. */
   mci_dma_start (DMA_WRITE, buf);
   MCI_DATA_CTRL = 0x99;

   for (i = 10000; i; i--) {
      if (GPDMA_RAW_INT_TCSTAT & 0x01) {
         /* Data transfer finished. */
         break;
      }
   }
   if (i == 0) {
      /* DMA Data Transfer timeout. */
      goto err;
   }
   /* Wait until Card Programming finished. */
   for (i = 0; i < 1000; i++) {
      if ((mci_read_status () & 0x0F00) == 0x0900) {
         /* Programming finished, card state TRAN. */
         MCI_DATA_CTRL = 0x00;
         return (__TRUE);
      }
   }
err:
   /* Timeout or Wrong state, reset the card. */
   MCI_DATA_CTRL = 0x00;
   mci_send_stop ();
   return (__FALSE);
}


/*--------------------------- mci_read_config -------------------------------*/

BOOL mci_read_config (MMCFG *cfg) {
   /* Read MMC/SD Card device configuration. */

   /* Already buffered. */
   *cfg = mmc_cfg;
   return (__TRUE);
}


/*----------------------------------------------------------------------------
 * end of file
 *---------------------------------------------------------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -