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

📄 smc.c

📁 ATMEL全套MP3设计资料
💻 C
📖 第 1 页 / 共 5 页
字号:
    smc_busy = TRUE;    

  }
  else
    if (gl_cpt_page == SMC_DATA_SIZE)        /* check if end of data page      */
    {
      /* Open in write mode in B Area */
      Smc_wait_busy();
      Smc_send_command (SMC_READ_B_AREA_CMD);
      Smc_send_command (SMC_SEQUENTIAL_DATA_INPUT_CMD); /* Sequential data input Type Command */
      Smc_send_address ( 0x00 );                                       /* 1st address cycle*/
      Smc_send_address ( ((Byte*)&current_physical_sector_addr)[3] );  /* 2nd address cycle*/
      Smc_send_address ( ((Byte*)&current_physical_sector_addr)[2] );  /* 3rd address cycle*/
      if (smc_64)                                       /* Size of card >= 64Mbytes ?*/
        Smc_send_address ( ((Byte*)&current_physical_sector_addr)[1] );/* 4th address cycle*/
      /* Copy the buffer */
      smc_download_buffer();
      /* Update spare data */
      smc_update_spare_data();
      Smc_send_command(SMC_PAGE_PROGRAM_CMD);
      smc_busy = TRUE;
      gl_ptr_mem++;
      gl_cpt_page = 0;      
    }

  return OK;

}


/*F**************************************************************************
* NAME: smc_write_sector
*----------------------------------------------------------------------------
* PARAMS:
*   global: gl_ptr_mem
*
* return:
*   write status: OK: write done
*                 KO: write not done
*----------------------------------------------------------------------------
* PURPOSE: 
*   This function is an optimized function that writes 512 bytes from USB
*   controller to SMC card
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit smc_write_sector (void)
{
  Byte j;                     /* general purpose counter                            */
  Byte count;                 /* number of usb frame received during a block erase  */
  Byte buffer_addr;           /* gl_buffer address                                  */
  bdata bit erase;
  char pdata *ptr = gl_buffer;

  if ( Smc_card_detect() == KO )
  {
    gl_mem_failure = TRUE;
    Smc_CS_OFF();
    return KO;
  }

  erase = FALSE; 
  if (smc_busy)
  {
    smc_busy = FALSE;
    /* if there is a block change */
    if ( !(gl_ptr_mem & 0x1F) )                            
    { 
      /* If previous block have to de deleted */
      if (block_used)       
      {
        smc_block_erase((Uint32)(block_to_be_deleted) << 5);
        erase = TRUE;
      }
      /* increase the main buffer index */
      gl_buf_idx++;
      /* if zone change */
      if (gl_buf_idx >= block_max)
      {
        smc_write_open(gl_ptr_mem);
      }
      else
      {
        /* if the block in the buffer is already assign, then invert it with a spare block */
        if ( !(buf[gl_buf_idx].w & 0x8000))                 
        {                                                 
          if ((buf_free[gl_buf_free_idx] & 0x7FFF) == lut_block[smc_zone])
          {
            gl_buf_free_idx++;
            if (gl_buf_free_idx >= spare_block)
            {
              gl_buf_free_idx = 0;
            }      
          }        
          /* invert the block and assign the next block to be deleted */
          block_to_be_deleted = buf[gl_buf_idx].w;
          buf[gl_buf_idx].w = buf_free[gl_buf_free_idx] & 0x7FFF;
          buf_free[gl_buf_free_idx] = block_to_be_deleted | 0x8000;
          /* block have to be deleted */
          block_used = TRUE;
          /* increase the spare buffer index */
          if ((gl_buf_free_idx++) >= spare_block )
          {
            gl_buf_free_idx = 0;
          }
  
        }
        else /* The block is not assigned. Nothing to do */
        {
          buf[gl_buf_idx].w &= 0x7FFF;
          block_used = FALSE;
        }
        /* update the max index buffer */
        if (gl_buf_idx > gl_buf_idx_max)
          gl_buf_idx_max = gl_buf_idx;
  
        /* Update current physical sector */
        current_physical_sector_addr = (Uint32)(buf[gl_buf_idx].w) << 5;
        /* increase the logical block */
        logical_block++;
        /* calculate the redundant block address */
        smc_calc_logical_block();
      }
    }
    else  /* if there is no block change, we just need to continue write operation */
    {     /* on the next physical sector                                           */
      current_physical_sector_addr++;
    }
                
  }


#ifndef SMC_ECC
  count = 0;
  if (erase)
  { 
    while (!Usb_rx_complete()); /* Wait USB reception complete */
    if (SMC_RD_BUSY == 0)              /* If card is busy */
    {
      smc_usb_to_buff_64(0);    /* Download 64 usb datas to gl_buffer */
      Usb_clear_RXOUT();
      count++;
      while (!Usb_rx_complete()); /* Wait USB reception complete */
      if (SMC_RD_BUSY == 0)              /* If card is busy */
      {
        smc_usb_to_buff_64(64);    /* Download 64 usb datas to gl_buffer */        
        Usb_clear_RXOUT();
        count++;
        while (!Usb_rx_complete()); /* Wait USB reception complete */
        if (SMC_RD_BUSY == 0)              /* If card is busy */
        {
          smc_usb_to_buff_64(128);    /* Download 64 usb datas to gl_buffer */       
          Usb_clear_RXOUT();
          count++;
          while (!Usb_rx_complete()); /* Wait USB reception complete */
          if (SMC_RD_BUSY == 0)              /* If card is busy */
          {
            smc_usb_to_buff_64(192);    /* Download 64 usb datas to gl_buffer */       
            Usb_clear_RXOUT();
            count++;
          }
        }
      }
    }
  }
  /* Open media in write operation */
  Smc_wait_busy();
  Smc_CS_ON();
  Smc_send_command (SMC_READ_A_AREA_CMD);
  Smc_send_command (SMC_SEQUENTIAL_DATA_INPUT_CMD);                 /* Sequential data input Type Command */
  Smc_send_address ( 0x00 );                                       /* 1st address cycle*/
  Smc_send_address ( ((Byte*)&current_physical_sector_addr)[3] );  /* 2nd address cycle*/
  Smc_send_address ( ((Byte*)&current_physical_sector_addr)[2] );  /* 3rd address cycle*/
  if (smc_64)                                                      /* Size of card >= 64Mbytes ?*/
    Smc_send_address ( ((Byte*)&current_physical_sector_addr)[1] );/* 4th address cycle*/

  /* If a block has been erased, then download first the data stored in gl_buffer */
  if (erase )
  { 
    buffer_addr = 0;
    for (j = count; j != 0; j--)
    {
      smc_download_buff_64(buffer_addr); /* Write 64 bytes from the buffer to SMC */
      buffer_addr += 0x40;              /* Next 64 bytes                        */
    }
  }

  /* If no ECC */
  for (j = 8 - count; j != 0; j--)
  {
    while (!Usb_rx_complete());             /* wait end of reception */
    Smc_wr_byte(Usb_read_byte());            /* write 64 bytes to card */
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Smc_wr_byte(Usb_read_byte());
    Usb_clear_RXOUT();                      /* usb read acknowledgement */
  }

#else
  /* If ECC */
  for (j = 4 ; j != 0; j--)                 /* Load 256b = 4 * 64    */
  {
    while (!Usb_rx_complete());             /* wait end of reception */
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    (*ptr++) = Usb_read_byte();
    Usb_clear_RXOUT();
  }

  smc_calculate_ecc();
 

⌨️ 快捷键说明

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