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

📄 nfc.c

📁 uart.tar, 龙珠i.MX21上的NAND Flash驱动程序
💻 C
📖 第 1 页 / 共 3 页
字号:
/*                      buffer to write                                      */
/*      bEcc            flag of using hardware/software ecc or not           */
/* RETURN VALUES                                                             */
/*      This function returns NFC_NO_ERR when it does successfully.          */
/*      If previous write error occurs, this function returns NFC_WRITE_ERR. */
/*      Because of interleaving write method, this function doen't check     */
/*      comletion of current write operation                                 */
/*                                                                           */
/*****************************************************************************/
uint32_t 
nfc_write_spare(uint16_t iBlockNum, uint16_t iPageNum, uint32_t* pSBuf, uint8_t bEcc)
{
  if (bEcc == ECC_WRITE)
      return(nfc_write(iBlockNum, iPageNum, (uint32_t*)0, (uint32_t*)pSBuf, NFC_CMD_WR_SB_ECC)); 
  else
      return(nfc_write(iBlockNum, iPageNum, (uint32_t*)0, (uint32_t*)pSBuf, NFC_CMD_WR_SB_NOECC));
}

/*****************************************************************************/
/*                                                                           */
/* NAME                                                                      */
/*      nfc_write_main (interface function)                                  */
/* DESCRIPTION                                                               */
/*      This function writes into NAND page area (512Bytes size)             */
/* PARAMETERS                                                                */
/*      iBlockNum       wanted block number of the NAND Flash to write       */
/*      iPageNum        wanted page number of the upper blk_num block        */
/*                      of the NAND Flash to write                           */
/*      pMBuf           unsigned char type pointer of main area sized        */
/*                      buffer to write                                      */
/*      bEcc            flag of using hardware/software ecc or not           */
/* RETURN VALUES                                                             */
/*      This function returns NFC_NO_ERR when it does successfully.          */
/*      If previous write error occurs, this function returns NFC_WRITE_ERR. */
/*      Because of interleaving write method, this function doen't check     */
/*      comletion of current write operation                                 */
/*                                                                           */
/*****************************************************************************/
uint32_t 
nfc_write_main(uint16_t iBlockNum, uint16_t iPageNum, uint32_t* pMBuf, uint8_t bEcc)
{
    if (bEcc == ECC_WRITE)
        return (nfc_write(iBlockNum, iPageNum, (uint32_t*)pMBuf, (uint32_t*)0, NFC_CMD_WR_PB_ECC));
    else
        return (nfc_write(iBlockNum, iPageNum, (uint32_t*)pMBuf, (uint32_t*)0, NFC_CMD_WR_PB_NOECC));
}

/*****************************************************************************/
/*                                                                           */
/* NAME                                                                      */
/*      nfc_write_page                                                       */
/* DESCRIPTION                                                               */
/*      This function writes into NAND page area (528Bytes size)             */
/* PARAMETERS                                                                */
/*      iBlockNum       wanted block number of the NAND Flash to write       */
/*      iPageNum        wanted page number of the upper blk_num block        */
/*                      of the NAND Flash to write                           */
/*      pMBuf           unsigned char type pointer of main area sized        */
/*                      buffer to write                                      */
/*      pSBuf           unsigned char type pointer of spare area sized       */
/*                      buffer to write                                      */
/*      bEcc            flag of using hardware/software ecc or not           */
/* RETURN VALUES                                                             */
/*      This function returns NFC_NO_ERR when it does successfully.          */
/*      If previous write error occurs, this function returns NFC_WRITE_ERR. */
/*      Because of interleaving write method, this function doen't check     */
/*      comletion of current write operation                                 */
/*                                                                           */
/*****************************************************************************/
uint32_t 
nfc_write_page(uint16_t iBlockNum, uint16_t iPageNum, uint32_t* pMBuf, uint32_t* pSBuf, uint8_t bEcc)
{
  if (bEcc == ECC_WRITE)
    return (nfc_write(iBlockNum, iPageNum, (uint32_t*)pMBuf, (uint32_t*)pSBuf, NFC_CMD_WR_PB_ECC));
  else
    return (nfc_write(iBlockNum, iPageNum, (uint32_t*)pMBuf, (uint32_t*)pSBuf, NFC_CMD_WR_PB_NOECC));
}

/*****************************************************************************/
/*                                                                           */
/* NAME                                                                      */
/*      nfc_erase                                                            */
/* DESCRIPTION                                                               */
/*      This function erases a block of NAND flash                           */
/* PARAMETERS                                                                */
/*      iBlockNum           block number of the NAND Flash to be erased      */
/* RETURN VALUES                                                             */
/*      This function returns NFC_NO_ERR when it does successfully.          */
/*      If previous write error occurs, this function returns NFC_WRITE_ERR. */
/*      Because of interleaving write method, this function doen't check     */
/*      comletion of current write operation                                 */
/*                                                                           */
/*****************************************************************************/
uint32_t 
nfc_erase(uint16_t iBlockNum)
{

  *(volatile p_uint16_t)NFC_NF_CONFIG1 = NFC_CONFIG1_READ; 

  *(volatile p_uint16_t)NFC_BLK_ADD_LOCK = iBlockNum;

  nfc_command_input(NAND_CMD_ERASE);

#if NAND_PAGE_SIZE == 512
  nfc_address_input(iBlockNum<<NAND_PAGE_ADDRESS_WIDTH );
  nfc_address_input(iBlockNum>>(8-NAND_PAGE_ADDRESS_WIDTH));
  nfc_address_input(iBlockNum>>(16-NAND_PAGE_ADDRESS_WIDTH));
#if NAND_ADDRESS_CYCLE == 5
  nfc_address_input(iBlockNum>>(24-NAND_PAGE_ADDRESS_WIDTH));
#endif
#endif

#if NAND_PAGE_SIZE == 2048
  nfc_address_input(iBlockNum<<NAND_PAGE_ADDRESS_WIDTH );
  nfc_address_input(iBlockNum>>(8-NAND_PAGE_ADDRESS_WIDTH));
#if NAND_ADDRESS_CYCLE == 5
  nfc_address_input(iBlockNum>>(16-NAND_PAGE_ADDRESS_WIDTH));
#endif
#endif    

  *(volatile p_uint16_t)NFC_NF_CONFIG1 = NFC_CONFIG1_WRITE; 

  nfc_command_input(NAND_CMD_ERASE_CONFIRM);
  
  return NFC_NO_ERR;
}

void 
nfc_block_lock(void)
{
  *(volatile p_uint16_t)NFC_NF_WR_PROT = 0x0002;
}

void 
nfc_block_unlock(uint16_t iStartBlockNum, uint16_t iEndBlockNum)
{
  *(volatile p_uint16_t)NFC_ULOCK_START_BLK = iStartBlockNum;
  *(volatile p_uint16_t)NFC_ULOCK_END_BLK   = iEndBlockNum;
  *(volatile p_uint16_t)NFC_NF_WR_PROT      = 0x0004;
}

/*****************************************************************************/
/*                             EAGLE READ ID                                 */
/*****************************************************************************/
/*****************************************************************************/
/*                                                                           */
/* NAME                                                                      */
/*      cmd_flash_readid (interface function)                                */
/* DESCRIPTION                                                               */
/*      This function reads ManufavturerID and DeviceID                      */
/* PARAMETERS                                                                */
/*      iManufacturer       NAND Flash iManufacturer ID                      */
/*                          NOW, only detect SAMSUNG (0xEC)                  */
/*      iDevice             NAND Flash device ID                             */
/* RETURN VALUES                                                             */
/*      This function returns NFC_NO_ERR except previous write error         */
/*                                                                           */
/*****************************************************************************/
uint32_t 
nfc_readid(uint8_t* iManufacturer, uint8_t* iDevice)
{
  uint32_t tmp;

  if(!nfc_status_ready())
    return NFC_WRITE_ERR;

  *(volatile p_uint16_t)NFC_NF_CONFIG1 = NFC_CONFIG1_READ; // ECC Bypass, Main & Spare Area, Read Operation

  nfc_RAM_buffer_select(0);
  nfc_command_input(NAND_CMD_READID);
  nfc_address_input(0);

  *(volatile p_uint16_t)NFC_NF_CONFIG2 = 0x0010; // FDO = ID Read

  while(NFC_BUSY);

  tmp  = *(volatile p_uint32_t)(NFC_MAB0_BASE);

  *iManufacturer  = (uint8_t)tmp;
  *iDevice        = (uint8_t)(tmp>>8);
   
  return (NFC_NO_ERR);
}

/*****************************************************************************/

uint32_t
nfc_first_block_write(uint32_t* pMBuf, uint32_t* pSBuf)
{
  uint32_t Status;
  uint32_t i;

  nfc_erase(0); // erase first block
  while(! nfc_status_ready());
  
  for(i=0; i<32 ; i++) {
    Status=nfc_write_page(0, i, (uint32_t*)((uint32_t)pMBuf+i*NAND_PAGE_SIZE), 
                                (uint32_t*)((uint32_t)pSBuf+i*NAND_PAGE_SIZE), NFC_CMD_WR_PB_ECC);
    while( ! nfc_status_ready() );
  }
  
  return Status;
}

uint32_t
nfc_first_block_read(uint32_t* pMBuf, uint32_t* pSBuf)
{
  uint32_t Status;
  uint32_t i;
  
  for(i=0; i<32 ; i++) {
    Status=nfc_read_page(0, i, (uint32_t*)((uint32_t)pMBuf+i*NAND_PAGE_SIZE), 
                               (uint32_t*)((uint32_t)pSBuf+i*NAND_PAGE_SIZE), NFC_CMD_RD_PB_ECC);
    while( ! nfc_status_ready() );
  }

  return Status;
}

void 
nfc_try(void)
{
   uint16_t address = 0x1000;

   *(volatile p_uint16_t)NFC_NAND_FLASH_ADDR = address;

   *(volatile p_uint16_t)NFC_NF_CONFIG2 = 0x0002;

   while(NFC_BUSY);    // Wait for Basic Operation Complete 
}

⌨️ 快捷键说明

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