bool    WF_FirmwareUpdate_Uart_24G(void)
{
    enum SM_FIRMWARE_UPDATE
    {
        SM_FIRMWARE_UPDATE_SOH,
        SM_FIRMWARE_UPDATE_BLOCK,
        SM_FIRMWARE_UPDATE_BLOCK_CMP,
        SM_FIRMWARE_UPDATE_DATA,
        SM_FIRMWARE_UPDATE_CHECKSUM,
        SM_FIRMWARE_UPDATE_FINISH,
    } state;

    BYTE c;
   // MPFS_HANDLE handle;
    BOOL lbDone;
    BYTE blockLen=0;
    BOOL lResult = FALSE;
    BYTE BlockNumber=0, preBlockNum=0;
    BYTE checksum=0;
    
    SYS_TICK lastTick;
    SYS_TICK currentTick;
    state = SM_FIRMWARE_UPDATE_SOH;
    lbDone = FALSE;

    TCPIP_NET_HANDLE netH;
    netH = TCPIP_STACK_NetHandle("MRF24W");
    
    if( BUTTON3_IO == 1u) return FALSE;    
    printf("\n\rPress S2 (on Explorer16) to start the update.\n\r");
    while(BUTTON2_IO == 1u);
	printf("1\n");
    WF_Init(netH);//MACInit();
    printf("2\n");
    SYS_TICK_MsDelay(100);
	printf("3\n");
    AutoUpdate_Initialize();
    printf("I am ready, Please transfer firmware patch by XMODEM.\r\n"); 
    printf("If you press S3(On Explorwe16), I will stop update, and restore back to previous firmware.\r\n"); 
    lastTick = SYS_TICK_Get();
    do
    {
        currentTick = SYS_TICK_Get();
        if ( currentTick - lastTick >= (SYS_TICK_TicksPerSecondGet()*2) )
        {
            lastTick = SYS_TICK_Get();
            //todo jw  //while(BusyUART());
            _SYS_CONSOLE_PUTC(XMODEM_NAK); //WriteUART(XMODEM_NAK);
        }

    } while(!_SYS_CONSOLE_DATA_RDY()); //(!DataRdyUART());

    
    while(!lbDone)
    {
        if (BUTTON3_IO == 0u)   // If you want to cancel AutoUpdate, please press S3
        {
            printf("You press S3 button, revert begin...\r\n");
            AutoUpdate_Restore();
            printf("revert done\r\n");
            return FALSE;
        }
        if(_SYS_CONSOLE_DATA_RDY()) //(DataRdyUART())
        {
            c = _SYS_CONSOLE_GETC();//ReadUART();
            lastTick = SYS_TICK_Get();
        }
        else
        {
            // put some timeout to make sure  that we do not wait forever.
             currentTick = SYS_TICK_Get();
            if ( currentTick - lastTick >= (SYS_TICK_TicksPerSecondGet()*10) )
            {
                //if time out, copy old patch image from bank2 to bank1
                printf("timeout, revert begin...\r\n");
                AutoUpdate_Restore();
                printf("revert done\r\n");
                return FALSE;
            }
            continue;
        }
        //dbgPrintf("(%02x) ",c); 
        switch(state)
        {
        case SM_FIRMWARE_UPDATE_SOH:
            if(c == XMODEM_SOH)
            {
                state = SM_FIRMWARE_UPDATE_BLOCK;
                dbgPrintf("\r\n! ");
                checksum = c;
                lResult = TRUE;
            }
            else if ( c == XMODEM_EOT )
            {
                state = SM_FIRMWARE_UPDATE_FINISH;
                
                // todo jw://while(BusyUART());
                _SYS_CONSOLE_PUTC(XMODEM_ACK); //WriteUART(XMODEM_ACK);
                lbDone = TRUE;
            }  
            else
            {
                dbgPrintf("\n!error\n");
                while(1);
            }
            break;
        case SM_FIRMWARE_UPDATE_BLOCK:
            BlockNumber = c;
            dbgPrintf("BLK=%d ",BlockNumber);         
            checksum += c;
            state = SM_FIRMWARE_UPDATE_BLOCK_CMP;
            break;

        case SM_FIRMWARE_UPDATE_BLOCK_CMP:
            dbgPrintf("%d ",c);
            dbgPrintf("@:");
            //Judge: Is it correct ?
            if(c != (BlockNumber ^ 0xFF))
            {
                lResult = FALSE;
                dbgPrintf("\nBLOCK_CMP err: %x,%x\n", c, BlockNumber ^ 0xFF );
            }
            else 
            {
                if((BYTE)(preBlockNum+1) != BlockNumber)
                {
                    lResult = FALSE;
                    dbgPrintf("\nBLOCK  err %x %x\n",preBlockNum+1,BlockNumber);
                }
            }
            checksum += c;
            blockLen = 0;
            state = SM_FIRMWARE_UPDATE_DATA;
            break;
        case SM_FIRMWARE_UPDATE_DATA:
            // Buffer block data until it is over.
            tempData[blockLen++] = c;
            if ( blockLen == XMODEM_BLOCK_LEN )
            {
                state = SM_FIRMWARE_UPDATE_CHECKSUM;
            }
            checksum += c;
            
            break;
        case SM_FIRMWARE_UPDATE_CHECKSUM:
            dbgPrintf("Checksum=%x=%x ",checksum,c);
            if(checksum != c)
            {
                lResult = FALSE;
                dbgPrintf("\nchecksum  err\n");
            }
            XMODEM_SendToModule(tempData);
            // todo jw://while(BusyUART());
            if(lResult == TRUE)
            {
                _SYS_CONSOLE_PUTC(XMODEM_ACK);//WriteUART(XMODEM_ACK);
                preBlockNum++;
            }
            else
            {
                _SYS_CONSOLE_PUTC(XMODEM_NAK);//WriteUART(XMODEM_NAK);
            }
            state = SM_FIRMWARE_UPDATE_SOH;
            break;

        default:
            dbgPrintf("\n!error\n");
            while(1);
            break;
        }

    }
    
    AutoUpdate_Completed();

    return TRUE;
}
bool    AutoUpdate_UartXMODEM_24G(void)
{
    enum SM_FIRMWARE_UPDATE
    {
        SM_FIRMWARE_UPDATE_SOH,
        SM_FIRMWARE_UPDATE_BLOCK,
        SM_FIRMWARE_UPDATE_BLOCK_CMP,
        SM_FIRMWARE_UPDATE_DATA,
        SM_FIRMWARE_UPDATE_CHECKSUM,
        SM_FIRMWARE_UPDATE_FINISH,
    } state;

    uint8_t c;
    // MPFS_HANDLE handle;
    bool lbDone;
    uint8_t blockLen=0;
    bool lResult = false;
    uint8_t BlockNumber=0, preBlockNum=0;
    uint8_t checksum=0;

    uint32_t lastTick;
    uint32_t currentTick;
    state = SM_FIRMWARE_UPDATE_SOH;
    lbDone = false;

#if 0 //use button to begin update
    if( BUTTON3_IO == 1u) return false;
    putsUART("\n\rPress S2 (on Explorer16) to start the update.\n\r");
    while(BUTTON2_IO == 1u);
#else //use console command to begin update
    if(false == wf_update_begin_uart) return false;
    wf_update_begin_uart = false;
#endif

    MACInit();
    DelayMs(100);
    AutoUpdate_Initialize();
    putsUART("Please initiate file transfer of firmware patch by XMODEM.\r\n");
    putsUART("If S3 pressed (on Explorer16), update will stop and previous image will be restored.\r\n");
    lastTick = TickGet();
    do
    {
        currentTick = TickGet();
        if ( currentTick - lastTick >= (TICK_SECOND*2) )
        {
            lastTick = TickGet();
            while(BusyUART());
            WriteUART(XMODEM_NAK);
        }

    } while(!DataRdyUART());


    while(!lbDone)
    {
        if (BUTTON3_IO == 0u)   // If you want to cancel AutoUpdate, please press S3
        {
            putsUART("You press S3 button, revert begin...\r\n");
            AutoUpdate_Restore();
            putsUART("revert done\r\n");
            return false;
        }
        if(DataRdyUART())
        {
            c = ReadUART();
            lastTick = TickGet();
        }
        else
        {
            // put some timeout to make sure  that we do not wait forever.
            currentTick = TickGet();
            if ( currentTick - lastTick >= (TICK_SECOND*10) )
            {
                //if time out, copy old patch image from bank2 to bank1
                putsUART("timeout, revert begin...\r\n");
                AutoUpdate_Restore();
                putsUART("revert done\r\n");
                return false;
            }
            continue;
        }
        //dbgPrintf("(%02x) ",c);
        switch(state)
        {
        case SM_FIRMWARE_UPDATE_SOH:
            if(c == XMODEM_SOH)
            {
                state = SM_FIRMWARE_UPDATE_BLOCK;
                dbgPrintf("\r\n! ");
                checksum = c;
                lResult = true;
            }
            else if ( c == XMODEM_EOT )
            {
                state = SM_FIRMWARE_UPDATE_FINISH;

                while(BusyUART());
                WriteUART(XMODEM_ACK);
                lbDone = true;
            }
            else
            {
                dbgPrintf("\n!error\n");
                while(1);
            }
            break;
        case SM_FIRMWARE_UPDATE_BLOCK:
            BlockNumber = c;
            dbgPrintf("BLK=%d ",BlockNumber);
            checksum += c;
            state = SM_FIRMWARE_UPDATE_BLOCK_CMP;
            break;

        case SM_FIRMWARE_UPDATE_BLOCK_CMP:
            dbgPrintf("%d ",c);
            dbgPrintf("@:");
            //Judge: Is it correct ?
            if(c != (BlockNumber ^ 0xFF))
            {
                lResult = false;
                dbgPrintf("\nBLOCK_CMP err: %x,%x\n", c, BlockNumber ^ 0xFF );
            }
            else
            {
                if((uint8_t)(preBlockNum+1) != BlockNumber)
                {
                    lResult = false;
                    dbgPrintf("\nBLOCK  err %x %x\n",preBlockNum+1,BlockNumber);
                }
            }
            checksum += c;
            blockLen = 0;
            state = SM_FIRMWARE_UPDATE_DATA;
            break;
        case SM_FIRMWARE_UPDATE_DATA:
            // Buffer block data until it is over.
            tempData[blockLen++] = c;
            if ( blockLen == XMODEM_BLOCK_LEN )
            {
                state = SM_FIRMWARE_UPDATE_CHECKSUM;
            }
            checksum += c;

            break;
        case SM_FIRMWARE_UPDATE_CHECKSUM:
            dbgPrintf("Checksum=%x=%x ",checksum,c);
            if(checksum != c)
            {
                lResult = false;
                dbgPrintf("\nchecksum  err\n");
            }
            XMODEM_SendToModule(tempData);
            while(BusyUART());
            if(lResult == true)
            {
                WriteUART(XMODEM_ACK);
                preBlockNum++;
            }
            else
            {
                WriteUART(XMODEM_NAK);
            }
            state = SM_FIRMWARE_UPDATE_SOH;
            break;

        default:
            dbgPrintf("\n!error\n");
            while(1);
            break;
        }

    }

    AutoUpdate_Completed();

    return true;
}