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

📄 smc.c

📁 ATMEL 89c51sndc mp3外接硬盘源码
💻 C
📖 第 1 页 / 共 5 页
字号:
      (*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_PP();*/
//    }
  
    smc_calculate_ecc();
    ecc[0] = ecc1;
    ecc[1] = ecc2;
    ecc[2] = ecc3;
  

//    smc_download_buffer();    /* Write 256 bytes from the buffer */
//    ptr = gl_buffer;          /* Restart for the last 256 bytes */
  
//    for (j = 4 ; j != 0; j--)                 /* Load 256b = 4 * 64    */
//    {
      while (!Usb_rx_complete());             /* wait end of reception */
      smc_usb_to_buff_64(0);                  /* Download usb datas to gl_buffer b0 b63*/
      Usb_clear_RXOUT_PP();
      while (!Usb_rx_complete());             /* wait end of reception */
      smc_usb_to_buff_64(64);                 /* Download usb datas to gl_buffer b64 b127*/
      Usb_clear_RXOUT_PP();
      while (!Usb_rx_complete());             /* wait end of reception */
      smc_usb_to_buff_64(128);                /* Download usb datas to gl_buffer b128 b191*/
      Usb_clear_RXOUT_PP();
      while (!Usb_rx_complete());             /* wait end of reception */
      smc_usb_to_buff_64(192);                /* Download usb datas to gl_buffer b192 b255*/
      Usb_clear_RXOUT_PP();

/*      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_PP();*/
//    }
  
    smc_calculate_ecc();
    ecc[3] = ecc1;
    ecc[4] = ecc2;
    ecc[5] = ecc3;
  
    /* Write 256 bytes from the buffer */
//    smc_download_buffer();
  #endif
  
    smc_update_spare_data();        /* Update spare data */
    Smc_send_command (SMC_PAGE_PROGRAM_CMD);
    gl_ptr_mem++;                   /* increase gl_ptr_mem : next page */
    smc_busy = TRUE;                /* set flag busy */
    Smc_wait_busy();
    nb_sector--;
  }
  while (nb_sector != 0);
  return OK;
}


#if 0
/*
Transfer result
LP14,12,10,... & LP15,13,11,... -> LP15,14,13,... & LP7,6,5,..
*/
void trans_result (Byte reg2,Byte reg3)
{
  Byte a; /* Working for reg2,reg3 */
  Byte b; /* Working for ecc1,ecc2 */
  Byte i; /* For counting */

  a = BIT7; b = BIT7; /* 80h=10000000b */
  ecc1 = ecc2 = 0; /* Clear ecc1,ecc2 */

  for(i = 0; i < 4; ++i) 
  {
    if ( (reg3 & a) != 0 ) ecc1 |= b; /* LP15,13,11,9 -> ecc1 */
    b = b >> 1; /* Right shift */
    if ( (reg2 & a) != 0 ) ecc1 |= b; /* LP14,12,10,8 -> ecc1 */
    b = b >> 1; /* Right shift */
    a = a >> 1; /* Right shift */
  }
  b = BIT7; /* 80h=10000000b */

  for (i = 0; i < 4; ++i) 
  {
    if ( (reg3 & a) != 0) ecc2 |= b; /* LP7,5,3,1 -> ecc2 */
    b = b >> 1; /* Right shift */
    if ( (reg2 & a) != 0) ecc2 |= b; /* LP6,4,2,0 -> ecc2 */
    b = b >> 1; /* Right shift */
    a = a >> 1; /* Right shift */
  }
}

#endif


/*F**************************************************************************
* NAME: smc_calculate_ecc
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   This function calculate ecc value
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/ 
void smc_calculate_ecc(void)
{
  Uint16 i; /* For counting */
  Byte   a; /* Working for table, reg2 and reg3 */
  Byte   b; /* Working for ecc1,ecc2 */
  Byte   reg1; /* D-all,CP5,CP4,CP3,... */
  Byte   reg2; /* LP14,LP12,L10,... */
  Byte   reg3; /* LP15,LP13,L11,... */

  reg1 = reg2 = reg3 = 0; /* Clear parameter */

  for(i = 0; i < 256; ++i) 
  {
    a = smc_ecc_table[gl_buffer[i]]; /* Get CP0-CP5 code from table */
    reg1 ^= (a & MASK_CPS); /* XOR with a */
    if ((a & BIT6)!=0) 
    { /* If D_all(all bit XOR) = 1 */
      reg3 ^= (Byte)i; /* XOR with counter */
      reg2 ^= ~((Byte)i); /* XOR with inv. of counter */
    }
  }
  /* Trans LP14,12,10,... & LP15,13,11,... -> LP15,14,13,... & LP7,6,5,.. */


  a = BIT7; b = BIT7; /* 80h=10000000b */
  ecc1 = ecc2 = 0; /* Clear ecc1,ecc2 */

  /* 1 */
  if ( (reg3 & a) != 0 ) ecc1 |= b; /* LP15,13,11,9 -> ecc1 */
  b = b >> 1; /* Right shift */
  if ( (reg2 & a) != 0 ) ecc1 |= b; /* LP14,12,10,8 -> ecc1 */
  b = b >> 1; /* Right shift */
  a = a >> 1; /* Right shift */

  /* 2 */
  if ( (reg3 & a) != 0 ) ecc1 |= b; /* LP15,13,11,9 -> ecc1 */
  b = b >> 1; /* Right shift */
  if ( (reg2 & a) != 0 ) ecc1 |= b; /* LP14,12,10,8 -> ecc1 */
  b = b >> 1; /* Right shift */
  a = a >> 1; /* Right shift */

  /* 3 */
  if ( (reg3 & a) != 0 ) ecc1 |= b; /* LP15,13,11,9 -> ecc1 */
  b = b >> 1; /* Right shift */
  if ( (reg2 & a) != 0 ) ecc1 |= b; /* LP14,12,10,8 -> ecc1 */
  b = b >> 1; /* Right shift */
  a = a >> 1; /* Right shift */

  /* 4 */
  if ( (reg3 & a) != 0 ) ecc1 |= b; /* LP15,13,11,9 -> ecc1 */
  b = b >> 1; /* Right shift */
  if ( (reg2 & a) != 0 ) ecc1 |= b; /* LP14,12,10,8 -> ecc1 */
  b = b >> 1; /* Right shift */
  a = a >> 1; /* Right shift */

  b = BIT7; /* 80h=10000000b */

  /* 1 */
  if ( (reg3 & a) != 0) ecc2 |= b; /* LP7,5,3,1 -> ecc2 */
  b = b >> 1; /* Right shift */
  if ( (reg2 & a) != 0) ecc2 |= b; /* LP6,4,2,0 -> ecc2 */
  b = b >> 1; /* Right shift */
  a = a >> 1; /* Right shift */

  /* 2 */
  if ( (reg3 & a) != 0) ecc2 |= b; /* LP7,5,3,1 -> ecc2 */
  b = b >> 1; /* Right shift */
  if ( (reg2 & a) != 0) ecc2 |= b; /* LP6,4,2,0 -> ecc2 */
  b = b >> 1; /* Right shift */
  a = a >> 1; /* Right shift */

  /* 3 */
  if ( (reg3 & a) != 0) ecc2 |= b; /* LP7,5,3,1 -> ecc2 */
  b = b >> 1; /* Right shift */
  if ( (reg2 & a) != 0) ecc2 |= b; /* LP6,4,2,0 -> ecc2 */
  b = b >> 1; /* Right shift */
  a = a >> 1; /* Right shift */

  /* 4 */
  if ( (reg3 & a) != 0) ecc2 |= b; /* LP7,5,3,1 -> ecc2 */
  b = b >> 1; /* Right shift */
  if ( (reg2 & a) != 0) ecc2 |= b; /* LP6,4,2,0 -> ecc2 */
  b = b >> 1; /* Right shift */
  a = a >> 1; /* Right shift */

  ecc1 = ~(ecc1); 
  ecc2 = ~(ecc2); /* Inv. ecc2 & ecc3 */
  ecc3 = ((~reg1)<<2)|BIT1BIT0; /* Make TEL format */
}


/*F**************************************************************************
* NAME: smc_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 after erasing the SMC.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
s_format code * smc_format (void)
{

code  s_format  smc_tab_format[]=
  {
   /* nb_cylinder, nb_head,  nb_sector, nb_hidden, nb_sector_per_cluster */
    { (Uint16)500, (Byte)4,  (Byte)16,  (Byte)57,  (Byte)32 }, /* 16MB */
    { (Uint16)500, (Byte)8,  (Byte)16,  (Byte)51,  (Byte)32 }, /* 32MB */
    { (Uint16)500, (Byte)8,  (Byte)32,  (Byte)39,  (Byte)32 }, /* 64MB */
    { (Uint16)500, (Byte)16, (Byte)32,  (Byte)63,  (Byte)32 }, /* 128MB */
  };

  /* Erase all block */
  smc_erase_all_block();

  /* -- SMC Type Selection -- */
  if (smc_device_type == SMC_SIZE_128MB) return &smc_tab_format[SMC_128MB];
  if (smc_device_type == SMC_SIZE_64MB)  return &smc_tab_format[SMC_64MB];
  if (smc_device_type == SMC_SIZE_32MB)  return &smc_tab_format[SMC_32MB];
  if (smc_device_type == SMC_SIZE_16MB)  return &smc_tab_format[SMC_16MB];
}



/*F**************************************************************************
* NAME: smc_check_presence
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
* return:
*   OK: card present
*   KO: card not present
*----------------------------------------------------------------------------
* PURPOSE: 
*   Return a status on card presence or not
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
********************

⌨️ 快捷键说明

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