/********************************** (C) COPYRIGHT *******************************
* File Name          : main.c
* Author             : WCH
* Version            : V1.0.0
* Date               : 2021/08/08
* Description        : Main program body.
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/

/*
 *@Note
   FSMC紱釬NANDFLASH瞰最ㄩ
      掛瞰最栳尨 籵徹FSMC諉諳紱釬 FS33ND01GS108TF倰瘍NANFLASH 笠-黍-迡-黍ㄛ甜婓黍迡徹最笢
  ECC苺桄﹝
  PINㄩ
    FSMC_NOE〞〞PD4  (RE)
    FSMC_NWE〞〞PD5  (WE)
    FSMC_NE1_NCE2〞〞PD7  (CE)
    FSMC_NWAIT〞〞PD6  (R/B)
    FSMC_D0 〞〞PD14
    FSMC_D1 〞〞PD15
    FSMC_D2 〞〞PD0
    FSMC_D3 〞〞PD1
    FSMC_D4 〞〞PE7
    FSMC_D5 〞〞PE8
    FSMC_D6 〞〞PE9
    FSMC_D7 〞〞PE10
    FSMC_A16〞〞PD11 (CLE)
    FSMC_A17〞〞PD12 (ALE)
*/

#include "debug.h"
#include "NAND_Flash.h"
#include "string.h"


u8 buf[NAND_ECC_SECTOR_SIZE];
u8 pbuf[NAND_ECC_SECTOR_SIZE];

/*********************************************************************
 * @fn      main
 *
 * @brief   Main program.
 *
 * @return  none
 */
int main( void )
{
    u32 i = 0;
    u8 t = 0;

    NVIC_PriorityGroupConfig( NVIC_PriorityGroup_2 );
    Delay_Init();
    USART_Printf_Init( 115200 );
    printf( "SystemClk:%d\r\n", SystemCoreClock );

    memset( buf, 0, NAND_ECC_SECTOR_SIZE );
    printf( "clear buf \n" );
    for( i = 0; i < NAND_ECC_SECTOR_SIZE; i++ ) {
        printf( "%02x ", buf[i] );

    } printf( "\n" );

    while( NAND_Init() )
    {
        printf( "NAND Error!\n" );
        Delay_Ms( 500 );

        printf( "Please Check\n" );
        Delay_Ms( 500 );
    }
    printf( "NAND Size:%dMB\n", ( nand_dev.block_totalnum / 1024 ) * ( nand_dev.page_mainsize / 1024 )*nand_dev.block_pagenum );

    /* Erase */
    t = NAND_EraseBlock( 0 );

    if( t )
    {
        printf( "Erase fail\n" );
    }
    else
    {
        printf( "Erase suc\n" );
    }

    /* Read */
    t = NAND_ReadPage( 0, 0, buf, NAND_ECC_SECTOR_SIZE );

    if( t )
    {
        printf( "read fail\n" );
        printf( "Err %02x\n", t );
    }
    else
    {
        printf( "1***read data:\n" );

        for( i = 0; i < NAND_ECC_SECTOR_SIZE; i++ ){
            printf( "%02x ", buf[i] );

        } printf( "\n" );
    }

    /* Write */
    printf( "Write data:\n" );
    pbuf[0] = 0X10;
    printf( "%02x ", pbuf[0] );
    for( i = 1;  i < NAND_ECC_SECTOR_SIZE; i++ )
    {
        pbuf[i] = 0X11;
        printf( "%02x ", pbuf[i] );
    }

    printf( "\r\n" );

    t = NAND_WritePage( 0, 0, pbuf, NAND_ECC_SECTOR_SIZE );

    if( t )
    {
        printf( "write fail\n" );
    }
    else
    {
        printf( "write suc\n" );
    }

    /* Read */
    t = NAND_ReadPage( 0, 0, buf, NAND_ECC_SECTOR_SIZE );

    if( t )
    {
        printf( "read fail\n" );
        printf( "Err %02x\n", t );
    }
    else
    {
        printf( "2***read data:\n" );
        for( i = 0; i < NAND_ECC_SECTOR_SIZE; i++ ){
            printf( "%02x ", buf[i] );
        } printf( "\r\n" );
    }


    while( 1 );

}



