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

📄 mmc.c

📁 开发工具:keil7 主要IC:at89c51sdnd1(atmel) 说明:mp3播放器详细设计方案
💻 C
📖 第 1 页 / 共 3 页
字号:
        }
      }
      Usb_write_byte(Mmc_rd_byte());        /* read 16 bytes from FIFO */
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      gl_mem_tick = MMC_DATA_TIME;
      while (!Mmc_read_ready())             /* wait data in FIFO 1 */
      {
        if (gl_mem_tick == MMC_TIME_OUT)
        {
          gl_mem_failure = TRUE;
          return KO;
        }
      }
      Usb_write_byte(Mmc_rd_byte());        /* read 16 bytes from FIFO */
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      gl_mem_tick = MMC_DATA_TIME;
      while (!Mmc_read_ready())             /* wait data in FIFO 1 */
      {
        if (gl_mem_tick == MMC_TIME_OUT)
        {
          gl_mem_failure = TRUE;
          return KO;
        }
      }
      Usb_write_byte(Mmc_rd_byte());        /* read 16 bytes from FIFO */
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
      Usb_write_byte(Mmc_rd_byte());
  
      if (begin_ping_pong)
      {
        begin_ping_pong = FALSE;
      }
      else
      {
        while (!Usb_tx_complete());         /* wait end of transfer */
        Usb_clear_TXCMPL();                 /* ack transfer */
      }
      Usb_set_TXRDY();                      /* start usb transfer */
    }
    gl_ptr_mem += 512;
    nb_sector--;                            /* 1 more sector read */
    
  }
  while (nb_sector != 0);

  while (!Usb_tx_complete());             /* wait end of last transfer */
  Usb_clear_TXCMPL();                     /* ack transfer */

  return OK;                                /* read done */
}


/*F**************************************************************************
* NAME: mmc_write_open
*----------------------------------------------------------------------------
* PARAMS:
*   pos: address of the the next write data
*   global: gl_ptr_mem
*
* return:
*   status: OK: open done
*           KO: open not done
*----------------------------------------------------------------------------
* PURPOSE: 
*   Open memory card in write mode (write block)
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   - pos must always be synchronized on a block boundary address
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit mmc_write_open (Uint32 pos)
{
  gl_ptr_mem = pos << 9;                    /* gl_ptr_mem = pos * 512 */
  if (mmc_mem_busy)
  {
    mmc_mem_busy = FALSE;
    while (Mmc_card_busy());                /* wait end of programming */
  }
  Mmc_disable_flow_ctrl();                  /* enable clock for command */
  Mmc_set_write();                          /* dir from uc to card */
  Mmc_write_block_cmd(gl_ptr_mem);          /* send write block */
//  while (!Mmc_command_sent());              /* wait end of transmission */
  gl_mem_tick = MMC_RESP_TIME;
  while (mmc_check_response() == MMC_ERR_RESP)
  {
    if (gl_mem_tick == MMC_TIME_OUT)
    {
      return KO;
    }
  }
  if ((mmc_read_response() & MMC_TRAN_STATE_MSK) != MMC_TRAN_STATE)
  {
    return  KO;
  }
  else
  {
    Mmc_enable_flow_ctrl();                 /* stop clock when FIFO 1&2 empty */
    Mmc_enable_send();                      /* send data after response */
    return  OK;
  }
}


/*F**************************************************************************
* NAME: mmc_write_close
*----------------------------------------------------------------------------
* PARAMS:
*   global: gl_ptr_mem
*
* return:
*----------------------------------------------------------------------------
* PURPOSE: 
*   Memory card write close
*     finish programming end of block
*     release MMC card
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void mmc_write_close (void)
{
  while ((((Byte*)&gl_ptr_mem)[3] != 0x00) || ((((Byte*)&gl_ptr_mem)[2] & MMC_PAGE_MASK) != 0x00))
  {
    Mmc_wr_byte(0x00);                      /* dummy write to end of block */
    gl_ptr_mem++;
  }
  Mmc_disable_flow_ctrl();                  /* re-enable clock for CRC status */
  mmc_mem_busy = TRUE;                      /* memory is busy */
}


/*F**************************************************************************
* NAME: mmc_write_byte
*----------------------------------------------------------------------------
* PARAMS:
*   b: data to write
*   global: gl_ptr_mem
*
* return:
*   write status: OK: write done
*                 KO: write not done
*----------------------------------------------------------------------------
* PURPOSE: 
*   Card byte write function
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit mmc_write_byte (Byte b)
{
  if (mmc_mem_busy)
  {
    mmc_mem_busy = FALSE;
    while (Mmc_card_busy());                /* wait end of programming */
    Mmc_write_block_cmd(gl_ptr_mem);        /* write next block */
    Mmc_enable_flow_ctrl();                 /* stop clock when FIFO 1&2 empty */
    Mmc_enable_send();                      /* send data after response */
    Mmc_wr_byte(b);                         /* write 1 byte to card */
    gl_ptr_mem++;
    return OK;                              /* write done */
  }
  else
  {
    Mmc_wr_byte(b);                         /* write 1 byte to card */
    gl_ptr_mem++;
  
    /* check if end of block */
    if ((((Byte*)&gl_ptr_mem)[3] == 0x00) && ((((Byte*)&gl_ptr_mem)[2] & MMC_PAGE_MASK) == 0x00))
    {
      Mmc_disable_flow_ctrl();              /* re-enable clock for CRC status */
      mmc_mem_busy = TRUE;                  /* memory is busy */
    }  
    return OK;                              /* write done */
  }
}


/*F**************************************************************************
* NAME: mmc_write_sector
*----------------------------------------------------------------------------
* PARAMS:
*   nb_sector:  number of contiguous sector to write 
*   global:     gl_ptr_mem
*
* return:
*   write status: OK: write done
*                 KO: write not done
*----------------------------------------------------------------------------
* PURPOSE: 
*   This function is an optimized function that writes nb-sector * 512 bytes
*   from USB controller to MMC card
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   nb_sector always >= 1, can not be zero
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit mmc_write_sector (Uint16 nb_sector)
{
Byte i;

  do
  {
    if (mmc_mem_busy)
    {
      mmc_mem_busy = FALSE;
      while (Mmc_card_busy());              /* wait end of programming */
      Mmc_write_block_cmd(gl_ptr_mem);      /* write next block */
      Mmc_enable_flow_ctrl();               /* stop clock when FIFO 1&2 empty */
      Mmc_enable_send();                    /* send data after response */
    }
  
    for (i = 8; i != 0; i--)                /* write 8x64b = 512b */
    {
      while (!Usb_rx_complete());           /* wait end of reception */
      Mmc_wr_byte(Usb_read_byte());         /* write 16 bytes to FIFO */
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      while (Mmc_write_busy());             /* wait no more data in FIFO 1 */
      Mmc_wr_byte(Usb_read_byte());         /* write 16 bytes to FIFO */
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      while (Mmc_write_busy());             /* wait no more data in FIFO 1 */
      Mmc_wr_byte(Usb_read_byte());         /* write 16 bytes to FIFO */
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      while (Mmc_write_busy());             /* wait no more data in FIFO 1 */
      Mmc_wr_byte(Usb_read_byte());         /* write 16 bytes to FIFO */
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Mmc_wr_byte(Usb_read_byte());
      Usb_clear_RXOUT_PP();                  /* usb read acknowledgement */
    }
    /* always end of page */
    Mmc_disable_flow_ctrl();                /* re-enable clock for CRC status */
    mmc_mem_busy = TRUE;
    gl_ptr_mem += 512;
    nb_sector--;                            /* 1 more sector written */
    while (!Mmc_data_sent());               /* wait end of transmission */
  }
  while (nb_sector != 0);
  return OK;                                /* write done */
}


/*F**************************************************************************
* NAME: mmc_format
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*   Address of the format parameter structure in code
*----------------------------------------------------------------------------
* PURPOSE: 
*   This function is called by the fat_format function and returns a pointer
*   to a table containing the format parameters.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*   MMC FORMAT PARAMETERS
*   CAPACITY  LBAs      CYL   HDs   S/T   CLUSTs  S/C   S/F   FAT   HID
*   4MB       7872      123   2     32    975     8     3     12    32
*   8MB       15680     245   2     32    1950    8     6     12    32
*   16MB      31360     490   2     32    3908    8     12    12    32
*   28MB      54784     428   4     32    13652   4     54    16    32
*   32MB      62720     490   4     32    15632   4     62    16    32
*   64MB      125440    490   8     32    31282   4     123   16    32
*   128MB     250880    980   8     32    62581   4     245   16    32
*   256MB     501760    980   16    32    62720   8     245   16    32
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
s_format code * mmc_format (void)
{
code  s_format  mmc_tab_format[]=
  {
   /* nb_cylinder, nb_head,  nb_sector, nb_hidden, nb_sector_per_cluster */
    { (Uint16)123, (Byte)2,  (Byte)32,  (Byte)32,  (Byte)8 }, /* 4MB */
    { (Uint16)245, (Byte)2,  (Byte)32,  (Byte)32,  (Byte)8 }, /* 8MB */
    { (Uint16)490, (Byte)2,  (Byte)32,  (Byte)32,  (Byte)8 }, /* 16MB */
    { (Uint16)428, (Byte)4,  (Byte)32,  (Byte)32,  (Byte)4 }, /* 28MB */
    { (Uint16)490, (Byte)4,  (Byte)32,  (Byte)32,  (Byte)4 }, /* 32MB */
    { (Uint16)490, (Byte)8,  (Byte)32,  (Byte)32,  (Byte)4 }, /* 64MB */
    { (Uint16)980, (Byte)8,  (Byte)32,  (Byte)32,  (Byte)4 }, /* 128MB */
    { (Uint16)980, (Byte)16, (Byte)32,  (Byte)32,  (Byte)8 }, /* 256MB */
  };

  /* -- MMC Type Selection -- */
  if (Mmc_disk_size() >= MMC_SIZE_256MB) return &mmc_tab_format[MMC_256MB];
  if (Mmc_disk_size() >= MMC_SIZE_128MB) return &mmc_tab_format[MMC_128MB];
  if (Mmc_disk_size() >= MMC_SIZE_64MB)  return &mmc_tab_format[MMC_64MB];
  if (Mmc_disk_size() >= MMC_SIZE_32MB)  return &mmc_tab_format[MMC_32MB];
  if (Mmc_disk_size() >= MMC_SIZE_28MB)  return &mmc_tab_format[MMC_28MB];
  if (Mmc_disk_size() >= MMC_SIZE_16MB)  return &mmc_tab_format[MMC_16MB];
  if (Mmc_disk_size() >= MMC_SIZE_8MB)   return &mmc_tab_format[MMC_8MB];
  return &mmc_tab_format[MMC_4MB];
}





⌨️ 快捷键说明

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