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

📄 nf.c

📁 ATMEL MP3 源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
      Nf_send_address( ((Byte*)&nf_current_physical_sector_addr)[2]);
      if (NF_5_CYCLE_ADDRESS_BIT)                                         /* Size of nf >= 128Mbytes ?  */
        Nf_send_address ( ((Byte*)&nf_current_physical_sector_addr)[1] ); /* Row address Byte 2         */
      Nf_send_command(NF_READ_CMD2);
      nf_busy = FALSE;
      Nf_wait_busy();
    }

    for (i = 8; i != 0; i--)                                /* 8 * 64 bytes = 512 bytes */
    {
      Usb_write_byte(Nf_rd_byte());                         /* read 64 bytes from card */
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_rd_byte());
      Usb_write_byte(Nf_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                 */    
    }

    ((Byte*)&gl_cpt_page)[0] += 2;
    if ( ((Byte*)&gl_cpt_page)[0] == 0x08)      /* Detection of the end of data page  */
    {
      gl_ptr_mem++;                             /* new page                           */
      if ( !(((Byte*)&gl_ptr_mem)[3] & 0x3F) )  /* New block ?                        */
      {
        nf_read_open(gl_ptr_mem << 2);
      }
      else
      {
        ((Byte*)&nf_current_physical_sector_addr)[3]++;
        gl_cpt_page = 0;
        nf_busy = TRUE;
      }
    }
    ((Byte*)&nb_sector)[1]--;
    Nf_CS_OFF();
  }
  while (((Byte*)&nb_sector)[1] != 0);
  while (!Usb_tx_complete());                   /* wait end of last transfer        */
  Usb_clear_TXCMPL();                           /* ack transfer                     */
  return OK;
}

       
/*F**************************************************************************
* NAME: nf_write_open
*----------------------------------------------------------------------------
*----------------------------------------------------------------------------
* PARAMS:
*   pos: address of the the next write data
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Low level memory write update
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit nf_write_open (Uint32 pos)
{
Byte i;
bit change_zone = FALSE;

  if (nf_write_advanced)        /* Previous operation was a write mass storage session */
  {
    nf_write_advanced = FALSE;  /* nf_write_advanced will be set only if there is a mass storage session */
    if (pos == save_gl_ptr_mem) /* if this sector is the previous "next sector" */
    {
      nf_current_physical_sector_addr = save_physical_sector;   /* Update sector information */
      gl_ptr_mem = pos >> 2;
      gl_cpt_page = (pos & 0x03) << 9;
      nf_busy = FALSE;                                /* Clear flag busy        */
      nf_wr_open = TRUE;
      return OK;
    }
    else
    {
      nf_force_write_close();
    }
  }
  else
  {
    if (nf_close_write_session)
    {
      nf_close_write_session = FALSE;
      nf_force_write_close();
    }
  }

  gl_ptr_mem = pos >> 2;
  gl_cpt_page = (pos & 0x03) << 9;


  /* Determine the logical block value and logical zone value */
  nf_logical_block = (gl_ptr_mem >> NF_SHIFT_SECTOR_BLOCK);
  nf_zone = 0;
  while (nf_logical_block > 999)
  {
    nf_logical_block -= 1000;
    nf_zone++;
  }
  Nf_CS_ON();

  if (nf_zone != nf_old_zone)
  {
    change_zone = TRUE;
    if (nf_lut_modified)
    { /* save old lut */
      i = nf_old_zone;
      nf_old_zone = nf_zone;
      nf_zone = i;
      nf_reassign_block();
      nf_lut_modified = FALSE;
      nf_zone = nf_old_zone;
    }
    else
    { /* update old zone value */
      nf_old_zone = nf_zone;
    }
    nf_block_min = 0xFFFF;
    gl_buf_free_idx = 0;
  }

  if ( (nf_logical_block >= nf_block_min) && (nf_logical_block < (nf_block_min + NF_BUFFER_SIZE - 5)))
  { /* we don't have to update the buffer */
    nf_calc_logical_block();
    nf_gl_buf_idx = nf_logical_block - nf_block_min;
  }
  else 
  { /* we have to update the buffer */
    nf_look_up_table_block =  nf_lut_block[nf_zone];
    if (nf_lut_modified)                              /* if lut have been modified */
    {
      nf_reassign_block();                            /* update lut */
    }
    gl_address      = ((Uint32)(nf_look_up_table_block)<<NF_SHIFT_SECTOR_BLOCK) 
                      + nf_lut_index[nf_zone];                /* Calculate the address for LUT access */
    nf_spare_block  = nf_spare_block_number[nf_zone];         /* For the current zone, initialize the number of spare block */
    nf_block_min    = nf_logical_block;                       /* starting block value */
    nf_block_max    = ((nf_logical_block + NF_BUFFER_SIZE) > 999) ? (1000 - nf_logical_block) : NF_BUFFER_SIZE;
    nf_calc_logical_block();                                  /* Calculate the redundant block address value */

    Nf_wait_busy();     
    Nf_send_command (NF_READ_CMD);                 /* Open the look-up table */
    Nf_send_address (nf_logical_block << 1);       /* Column address Byte 0  */
    Nf_send_address (nf_logical_block >> 7);       /* Column address Byte 1  */
    Nf_send_address ( ((Byte*)&gl_address)[3] );   /* Row address Byte 0     */
    Nf_send_address ( ((Byte*)&gl_address)[2] );   /* Row address Byte 1     */
    if (NF_5_CYCLE_ADDRESS_BIT)                        /* Size of card >= 128Mbytes ?  */
      Nf_send_address ( ((Byte*)&gl_address)[1] ); /* Row address Byte 2           */

    Nf_send_command (NF_READ_CMD2);
    Nf_wait_busy();
  
    for (i = 0; i < nf_block_max ; i++)
    {
      nf_buf[i].b[0] = Nf_rd_byte();      /* Read lut */
      nf_buf[i].b[1] = Nf_rd_byte();
    }
  
    if (change_zone)                            /* If it is a new zone, then load the spare buffer */
    {
      Nf_send_command(NF_RANDOM_READ_CMD_C1);   /* Read LUT at column address */
      Nf_send_address(0xD0);                    /* 2000 -> 07D0               */
      Nf_send_address(0x07);
      Nf_send_command(NF_RANDOM_READ_CMD_C2);
      Nf_wait_busy();
      for (i = 0; i < (nf_spare_block); i++)
      {
        nf_buf_free[i]  =   (Uint16)(Nf_rd_byte()<<8);
        nf_buf_free[i] +=   Nf_rd_byte();
      }
    }
    nf_gl_buf_idx     = 0;                    /* initialize index for main buffer         */
    nf_gl_buf_idx_max = 0;                    /* initialize the max index for the buffer  */

  }

  /* if block is already assigned, then invert with a spare block */
  nf_block_used = ( !(nf_buf[nf_gl_buf_idx].w & 0x8000) ) ? TRUE : FALSE;
  if (nf_block_used)
  {
    if ((nf_buf_free[gl_buf_free_idx] & 0x7FFF) == nf_look_up_table_block)
    {
      gl_buf_free_idx++;
      if (gl_buf_free_idx >= nf_spare_block  )
      {
        gl_buf_free_idx = 0;
      }      
    }
    nf_block_to_be_deleted          = nf_buf[nf_gl_buf_idx].w;            /* assign block to be deleted */
    nf_buf[nf_gl_buf_idx].w         = nf_buf_free[gl_buf_free_idx];
    nf_buf_free[gl_buf_free_idx++]  = nf_block_to_be_deleted | 0x8000;
    if (gl_buf_free_idx >= nf_spare_block  )
    {
      gl_buf_free_idx = 0;
    }

  }

  nf_buf[nf_gl_buf_idx].w &= 0x7FFF;                                      /* Mark block as assigned */

  if (nf_gl_buf_idx > nf_gl_buf_idx_max)
    nf_gl_buf_idx_max = nf_gl_buf_idx;
  
  nf_current_physical_sector_addr = ((Uint32)(nf_buf[nf_gl_buf_idx].w) << NF_SHIFT_SECTOR_BLOCK);/* Update the current physical sector address */

  if ( nf_block_used )                    /* block already used?                      */
  { 
    nf_copy_block_head();                 /* then copy the first part of block        */
  }
  else                                 
  { 
    nf_init_spare();                      /* else init spare data for new logical block  */
  }

  gl_address = ((Uint32)(nf_look_up_table_block)<<NF_SHIFT_SECTOR_BLOCK) + (Uint32)(nf_lut_index[nf_zone]);
  Nf_wait_busy();
  Nf_send_command(NF_SEQUENTIAL_DATA_INPUT_CMD);  /* write lut as modified                */
  Nf_send_address(0x00);                          /* Column address byte 0                */
  Nf_send_address(0x08);                          /* Column address byte 1                */
  Nf_send_address ( ((Byte*)&gl_address)[3] );    /* Row address Byte 0                   */
  Nf_send_address ( ((Byte*)&gl_address)[2] );    /* Row address Byte 1                   */
  if (NF_5_CYCLE_ADDRESS_BIT)                     /* Size of card >= 128Mbytes ?          */
    Nf_send_address ( ((Byte*)&gl_address)[1] );  /* Row address Byte 2                   */

  Nf_wr_byte(0x00);                               /* Reset first byte                     */
  Nf_send_command(NF_PAGE_PROGRAM_CMD);           /* Send program command to the device   */
  nf_busy = FALSE;                                /* Clear flag busy                      */
  nf_lut_modified = TRUE;                         /* set lut flag modified                */
  nf_wr_open = TRUE;                              /* Set this flag to send write command  */
  Nf_wait_busy();
  return OK; 
}


/*F**************************************************************************
* NAME: nf_write_close
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Low level memory write close: release NF 
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit nf_write_close (void)
{
  Nf_CS_ON();

  if ( (gl_cpt_page & 0x1FF) != 0)                      /* uncomplete write sector (for nf_write_byte only) */
  {
    for ( ; ((gl_cpt_page & 0x1FF) != 0); gl_cpt_page++)
      Nf_wr_byte(0x00);

    Nf_send_command(NF_RANDOM_DATA_INPUT_CMD);          /* Open at the beginning of spare data */
    Nf_send_address(0x00);                              
    Nf_send_address(0x08);
    nf_update_spare_data();
    Nf_send_command(NF_PAGE_PROGRAM_CMD);               /* Send program command to the device */

    if ( ((Byte*)&gl_cpt_page)[0] == 0x08)              /* end of sector */
    {
      gl_cpt_page = 0;
      gl_ptr_mem++;
      nf_current_physical_sector_addr++;
    }
  }
  else
  {
    if (gl_cpt_page != 0)                               /* a 512b page have been written */
    {
      Nf_send_command (NF_RANDOM_DATA_INPUT_CMD);
      Nf_send_address ( 0x00 );
      Nf_send_address ( 0x08 );
      nf_update_spare_data();
      Nf_send_command (NF_PAGE_PROGRAM_CMD);
    }
    /* if gl_cpt_page = 0, that means that spare data are already update */
  }

  nf_close_write_session = TRUE;          /* At next read open : finish write session (block tail copy) */
  save_gl_ptr_mem = (gl_ptr_mem << 2) + (gl_cpt_page >> 9);
  save_physical_sector = nf_current_physical_sector_addr;

  Nf_wait_busy();

  if (nf_block_used)                      /* If block was previously affected and have to be deleted */
  {                                       /* then mark it for recovery case */
    gl_address = ((Uint32)(nf_block_to_be_deleted) << NF_SHIFT_SECTOR_BLOCK);
    Nf_send_command (NF_SEQUENTIAL_DATA_INPUT_CMD);
    Nf_send_address ( 0x11 ); 
    Nf_send_address ( NF_SPARE_PAGE );
    Nf_send_address ( ((Byte*)&gl_address)[3] );
    Nf_send_address ( ((Byte*)&gl_address)[2] );

⌨️ 快捷键说明

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