Guest User

Untitled

a guest
Dec 15th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.57 KB | None | 0 0
  1.  
  2. void NandFlash_WaitForReady( void )
  3. {
  4.     u32 i;
  5.  
  6.     //while( FIO2PIN & (1 << 29) );     /* from high to low once */
  7.  
  8.     while( !(FIO2PIN & (1 << 29)) );
  9.  
  10.     return;
  11. }
  12.  
  13. Bool NandFlash_PageProgram( u32 pageNum, u32 blockNum, u8 *bufPtr )
  14. {
  15.     volatile u8 *pCLE = K9F1G_CLE;
  16.     volatile u8 *pALE = K9F1G_ALE;
  17.     volatile u8 *pDATA = K9F1G_DATA;
  18.     u32 i, curAddr, curColumm;
  19.  
  20.     curAddr = NANDFLASH_BASE_ADDR + blockNum * NANDFLASH_BLOCK_FSIZE
  21.                                 + pageNum * NANDFLASH_PAGE_FSIZE;
  22.  
  23.     curColumm = curAddr % NANDFLASH_PAGE_FSIZE;
  24.     curAddr -= curColumm;
  25.  
  26.     *pCLE = K9FXX_BLOCK_PROGRAM_1;
  27.  
  28.     *pALE =  (u8)(curColumm & 0x000000FF);      /* column address low */
  29.  
  30.     *pALE = (u8)((curColumm & 0x00000F00) >> 8);    /* column address high */
  31.  
  32.     *pALE = (u8)((curAddr & 0x00FF0000) >> 16); /* row address low */
  33.  
  34.     *pALE = (u8)((curAddr & 0xFF000000) >> 24); /* row address high */
  35.  
  36.     //Not write to spare area for the NandFlash valid block checking
  37.     for ( i = 0; i < NANDFLASH_RW_PAGE_SIZE; i++ )
  38.     {
  39.         *pDATA = *bufPtr++;
  40.     }
  41.  
  42.     *pCLE = K9FXX_BLOCK_PROGRAM_2;
  43.  
  44.     NandFlash_WaitForReady();
  45.  
  46.     return( NandFlash_ReadStatus( K9FXX_BLOCK_PROGRAM_1 ) );
  47. }
  48.  
  49. Bool NandFlash_BlockErase( u32 blockNum )
  50. {
  51.     volatile u8 *pCLE = K9F1G_CLE;
  52.     volatile u8 *pALE = K9F1G_ALE;
  53.     u32 rowAddr;
  54.  
  55.     rowAddr = (NANDFLASH_BASE_ADDR + blockNum * NANDFLASH_BLOCK_FSIZE);
  56.     rowAddr = rowAddr - (rowAddr % NANDFLASH_BLOCK_FSIZE);
  57.  
  58.     *pCLE = K9FXX_BLOCK_ERASE_1;
  59.  
  60.     *pALE = (u8)(rowAddr & 0x00FF);         /* column address low */
  61.  
  62.     *pALE = (u8)((rowAddr & 0xFF00) >> 8);  /* column address high */
  63.  
  64.     *pCLE = K9FXX_BLOCK_ERASE_2;
  65.  
  66.     NandFlash_WaitForReady();
  67.  
  68.     return(NandFlash_ReadStatus(K9FXX_BLOCK_ERASE_1));
  69. }
  70.  
  71. void NandFlash_Reset( void )
  72. {
  73.     volatile u8 *pCLE;
  74.  
  75.     /* Reset NAND FLASH  through NAND FLASH command */
  76.     pCLE = K9F1G_CLE;
  77.     *pCLE = K9FXX_RESET;
  78.  
  79.     delayMs(0, 2);
  80.     return;
  81. }
  82.  
  83. int NandFlash_ReadFromAddr(u32 addrInWholeNand, u8* bufPtr, u32 size)
  84. {
  85.     volatile u8 *pCLE = K9F1G_CLE;
  86.     volatile u8 *pALE = K9F1G_ALE;
  87.     volatile u8 *pDATA = K9F1G_DATA;
  88.     u32 i = 0, curColumm, curRow;
  89.  
  90.     curColumm = addrInWholeNand % NANDFLASH_PAGE_FSIZE;
  91.     curRow = addrInWholeNand - curColumm;
  92.  
  93.     *pCLE = K9FXX_READ_1;
  94.  
  95.     *pALE = (u8)(curColumm & 0x000000FF);               /* column address low */
  96.  
  97.     *pALE = (u8)((curColumm & 0x00000F00) >> 8);  /* column address high */
  98.  
  99.     *pALE = (u8)((curRow & 0x00FF0000) >> 16);    /* row address low */
  100.  
  101.     *pALE = (u8)((curRow & 0xFF000000) >> 24);    /* row address high */
  102.  
  103.     *pCLE = K9FXX_READ_2;
  104.  
  105.     NandFlash_WaitForReady();
  106.  
  107.     //Get data from the current address in the page til the end of the page
  108.     for ( i = 0; i < (NANDFLASH_PAGE_FSIZE - curColumm); i++ )
  109.     {
  110.         *bufPtr = *pDATA;
  111.  
  112.         bufPtr++;
  113.  
  114.         if((i + 1) >= size)
  115.             break;
  116.     }
  117.  
  118.     // Ok, return
  119.     return i;
  120. }
  121.  
  122. int NandFlash_PageReadFromAddr(u32 blockNum, u32 pageNum,
  123.                                             u32 addrInPage, u8* bufPtr, u32 size)
  124. {
  125.     u32 curAddr = 0;
  126.  
  127.     curAddr += NANDFLASH_BASE_ADDR + blockNum * NANDFLASH_BLOCK_FSIZE;
  128.  
  129.     curAddr += pageNum * NANDFLASH_PAGE_FSIZE;
  130.  
  131.     curAddr += addrInPage;
  132.  
  133.     return (NandFlash_ReadFromAddr(curAddr, bufPtr, size));
  134. }
  135.  
  136. int NandFlash_PageReadFromBeginning(u32 blockNum, u32 pageNum, u8* bufPtr)
  137. {
  138.     return (NandFlash_PageReadFromAddr(blockNum, pageNum, 0, bufPtr, NANDFLASH_PAGE_FSIZE));
  139. }
  140.  
  141. u32 NandFlash_ReadId( void )
  142. {
  143.     u8 a, b, c, d;
  144.     volatile u8 *pCLE = K9F1G_CLE;
  145.     volatile u8 *pALE = K9F1G_ALE;
  146.     volatile u8 *pDATA = K9F1G_DATA;
  147.  
  148.     *pCLE = K9FXX_READ_ID;
  149.     *pALE = 0;
  150.  
  151.     a = *pDATA;
  152.     a = *pDATA;
  153.     b = *pDATA;
  154.     c = *pDATA;
  155.     d = *pDATA;
  156.    
  157.     return ((a << 24) | (b << 16) | (c << 8) | d);
  158. }
Add Comment
Please, Sign In to add comment