This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

About TMS570LC4357 Fapi Flash erase Error

Other Parts Discussed in Thread: TMS570LC4357

I use TMS570LC4357 and Fapi library test for boot loader.

I have a question about using Fapi_BlockErase in Fapi library.

The test sequence is as follows,

1. Erase First : oReturnCheck = Fapi_BlockErase( 0, APP_VERSIONUP_START_ADDR, MAXIMUM_FIRMWARE_SIZE);

2.  Write Something :  oReturnCheck = Fapi_BlockProgram( 0, APP_VERSIONUP_ADDRESS + size, (unsigned long)&data[0], length);

3. Re Erase Test : oReturnCheck = Fapi_BlockErase( 0, APP_VERSIONUP_START_ADDR, MAXIMUM_FIRMWARE_SIZE); 

In number 3,  the firmware is stop in "while( FAPI_CHECK_FSM_READY_BUSY == Fapi_Status_FsmBusy );"  in Fapi_BlockErase function.

My questions are,

1. why the fimrware is stopped in Fapi_BlockErase function?

2. How I can re erase flash memory after write flash? 

Regards,

Minwoo

  • Hi Minwoo,

    Does step 1 erase the flash correctly? What is the value of APP_VERSIONUP_START_ADDR and MAXIMUM_FIRMWARE_SIZE?

  • Hi QJ Wang,

    Does step 1 erase the flash correctly? -> Yes, Step 1 erase act well. and Step 2 Write act well.

    What is the value of APP_VERSIONUP_START_ADDR and MAXIMUM_FIRMWARE_SIZE ->

    The code act in 0x00200100 ~ 0x00300000, APP_VERSIONUP_START_ADDR : 0x00100000, MAXIMUM_FIRMWARE_SIZE  : 0x00080000

     

    Regards,

    Minwoo

  • Thanks

    You erased sector 0~7 of flash bank 1, and you wrote data to sector 12 and sector 13 of bank 0. Were sector 12 and sector 13 erased before writing data to those two sectors? If not, the data may not be written properly. 

    After #2, is any error flag in FMSTAT set?

  • Thanks for your Answer,

    the Code set,

     

    1. 0x00000000 ~ 0x000FFFFF (Bank 0) : boot loader

    2. 0x00100000 ~ 0x001FFFFF (Bank 0) : Version Up code

    3. 0x00200000 ~ 0x002FFFFF (Bank 1) : Active code (Normal act)

    sector 0~7 of flash bank 1 mean 0x00200000 ~ 0x00300000?

    But, In this section, the Real code is act in this address. So, It is impossible to erase this section.

    And In sector 0~7 of flash bank 0(0x00000000 ~ 0x000FFFFF),  Boot loader Code is written.

    I wonder after the system start (power on/off). I can erase/write in only sector 12 and sector 13 of bank 0.

    Then, is there no way to re-erase flash memory?

    Regards,

    Minwoo

  • Hi Minwoo,

    Can you share the flash.c with me? I'd like to check if bank 1 is enabled or not.

  • Hi QJ Wang,


    I attach the files and these code are based on boot loader sample code.

    bl_flash.h

     

    //*****************************************************************************
    //
    // bl_flash.c     : The file holds the main control loop of the boot loader.
    // Author         : QJ Wang. qjwang@ti.com
    // Date           : 9-19-2012
    //
    // Copyright (c) 2006-2011 Texas Instruments Incorporated.  All rights reserved.
    // Software License Agreement
    //
    // Texas Instruments (TI) is supplying this software for use solely and
    // exclusively on TI's microcontroller products. The software is owned by
    // TI and/or its suppliers, and is protected under applicable copyright
    // laws. You may not combine this software with "viral" open-source
    // software in order to form a larger program.
    //
    // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
    // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
    // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
    // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
    // DAMAGES, FOR ANY REASON WHATSOEVER.
    //
    //*****************************************************************************
    
    #define _L2FMC
    #include "bl_config.h"
    #include "bl_flash.h"
    #include "F021.h"
    #include "flash_defines.h"
    
    //#define Freq_In_MHz = SYS_CLK_FREQ;
    
    //*****************************************************************************
    //
    // Returns the size of the ist sector size of the flash in bytes.
    //
    //*****************************************************************************
    uint32_t
    BLInternalFlashFirstSectorSizeGet(void)
    {
    	uint32_t firstSectorSize;
    	firstSectorSize = (uint32_t)(flash_sector[0].start) + flash_sector[0].length;
        return (firstSectorSize);
    }
    //*****************************************************************************
    //
    // Returns the size of the internal flash in bytes.
    //
    // This function returns the total number of bytes of internal flash in the
    // current part.  No adjustment is made for any sections reserved via
    // options defined in bl_config.h.
    //
    // \return Returns the total number of bytes of internal flash.
    //
    //*****************************************************************************
    uint32_t
    BLInternalFlashSizeGet(void)
    {
    	uint32_t flashSize;
    	flashSize = (uint32_t)flash_sector[NUMBEROFSECTORS-1].start + flash_sector[NUMBEROFSECTORS-1].length;
        return (flashSize);
    }
    
    //*****************************************************************************
    //
    //! Checks whether a given start address is valid for a download.
    //!
    //! This function checks to determine whether the given address is a valid
    //! download image start address given the options defined in bl_config.h.
    //!
    //! \return Returns non-zero if the address is valid or 0 otherwise.
    //
    //*****************************************************************************
    uint32_t
    BLInternalFlashStartAddrCheck(uint32_t ulAddr, uint32_t ulImgSize)
    {
        uint32_t count=0, i;
    
    	uint32_t ulWholeFlashSize;
    
        //
        // Determine the size of the flash available on the part in use.
        //
        ulWholeFlashSize = (uint32_t)flash_sector[NUMBEROFSECTORS-1].start + flash_sector[NUMBEROFSECTORS-1].length;  /* 3MB */
    
    	/* The start address must be at the begining of the sector */
        for (i = 0; i < NUMBEROFSECTORS; i++){
    		if ((ulAddr >= (uint32_t)(flash_sector[i].start)) && (ulAddr < ((uint32_t)flash_sector[i].start + flash_sector[i].length)))
    		{
    			count++;
    		}
    	}
        if (count == 0){
        	return(0);
        }
    
        //
        // Is the address we were passed a valid start address?  We allow:
        //
        // 1. Address 0 if configured to update the boot loader.
        // 2. The start of the reserved block if parameter space is reserved (to
        //    allow a download of the parameter block contents).
        // 3. The application start address specified in bl_config.h.
        //
        // The function fails if the address is not one of these, if the image
        // size is larger than the available space or if the address is not word
        // aligned.
        //
        if((
    #ifdef ENABLE_BL_UPDATE
                           (ulAddr != 0) &&
    #endif
                            (ulAddr != APP_START_ADDRESS)) ||
                           ((ulAddr + ulImgSize) > ulWholeFlashSize) ||
                           ((ulAddr & 3) != 0))
        {
        	return(0);
        }
        else  {
            return(1);
        }
    }
    
    
    //#pragma CODE_SECTION (Fapi_BlockErase, ".myTest")
    uint32_t Fapi_BlockErase( uint32_t Bank, uint32_t ulAddr, uint32_t Size)
    {
    	uint8_t  i=0, ucStartBank, ucEndBank, ucStartSector, ucEndSector;
        uint32_t EndAddr, status;
    
    	EndAddr = ulAddr + Size;
    	for (i = 0; i < NUMBEROFSECTORS; i++){
    		if ((ulAddr >= (uint32_t)(flash_sector[i].start)) && (ulAddr < ((uint32_t)flash_sector[i].start + flash_sector[i].length)))
    		{
    			ucStartBank     = flash_sector[i].bankNumber;
    		    ucStartSector   = i;
    		    break;
    		}
    	}
    
    	for (i = ucStartSector; i < NUMBEROFSECTORS; i++){
    		if (EndAddr <= (((uint32_t)flash_sector[i].start) + flash_sector[i].length))
    		{
    			ucEndBank   = flash_sector[i].bankNumber;
    			ucEndSector = i;
    		    break;
    		}
    	}
    
    	status=Fapi_initializeFlashBanks((uint32_t)SYS_CLK_FREQ); /* used for API Rev2.01 */
    	Fapi_enableAutoEccCalculation();
    
        for (i = ucStartBank; i < (ucEndBank + 1); i++){
            Fapi_setActiveFlashBank((Fapi_FlashBankType)i);
            Fapi_enableMainBankSectors(0xFFFF);                 /* used for API 2.01*/
            while( FAPI_CHECK_FSM_READY_BUSY != Fapi_Status_FsmReady );
        }
    
        for (i=ucStartSector; i<(ucEndSector+1); i++){
    		Fapi_issueAsyncCommandWithAddress(Fapi_EraseSector, flash_sector[i].start);
        	while( FAPI_CHECK_FSM_READY_BUSY == Fapi_Status_FsmBusy );
        	while(FAPI_GET_FSM_STATUS != Fapi_Status_Success);
        }
    
    //    status =  Flash_Erase_Check((uint32_t)ulAddr, Size);
    
    	return (status);
    }
    
    //Bank here is not used. We calculate the bank in the function based on the Flash-Start-addr
    uint32_t Fapi_BlockProgram( uint32_t Bank, uint32_t Flash_Address, uint32_t Data_Address, uint32_t SizeInBytes)
    {
    	register uint32_t src = Data_Address;
    	register uint32_t dst = Flash_Address;
    	uint32_t bytes;
    
    	if (SizeInBytes < 32)
    		bytes = SizeInBytes;
    	else
    		bytes = 32;
    
    	if ((Fapi_initializeFlashBanks((uint32_t)SYS_CLK_FREQ)) == Fapi_Status_Success){
    		 (void)Fapi_enableAutoEccCalculation();
    		 (void)Fapi_setActiveFlashBank((Fapi_FlashBankType)Bank);
    	     (void)Fapi_enableMainBankSectors(0xFFFF);                    /* used for API 2.01*/
    	}else {
             return (1);
    	}
    
    	while( FAPI_CHECK_FSM_READY_BUSY != Fapi_Status_FsmReady );
    	while( FAPI_GET_FSM_STATUS != Fapi_Status_Success );
    
        while( SizeInBytes > 0)
    	{
    		Fapi_issueProgrammingCommand((uint32_t *)dst,
    									 (uint8_t *)src,
    									 (uint32_t) bytes,
    									 0,
    									 0,
    									 Fapi_AutoEccGeneration);   //Fapi_AutoEccGeneration);
    
     		while( FAPI_CHECK_FSM_READY_BUSY == Fapi_Status_FsmBusy );
    //        while(FAPI_GET_FSM_STATUS != Fapi_Status_Success);
    
    		src += bytes;
    		dst += bytes;
    		SizeInBytes -= bytes;
            if ( SizeInBytes < 32){
               bytes = SizeInBytes;
            }
        }
    	return (0);
    }
    
    
    uint32_t Fapi_UpdateStatusProgram( uint32_t Bank, uint32_t Flash_Start_Address, uint32_t Data_Start_Address, uint32_t Size_In_Bytes)
    {
    	register uint32_t src = Data_Start_Address;
    	register uint32_t dst = Flash_Start_Address;
    	unsigned int bytes, status;
    
    	if (Size_In_Bytes < 16)
    		bytes = Size_In_Bytes;
    	else
    		bytes = 16;
    
    	Fapi_initializeAPI((Fapi_FmcRegistersType *)F021_CPU0_REGISTER_ADDRESS, (uint32_t)SYS_CLK_FREQ);
    	Fapi_setActiveFlashBank((Fapi_FlashBankType)Bank);
    	Fapi_issueProgrammingCommand((uint32_t *)dst,
    									 (uint8_t *)src,
    									 (uint32_t) bytes,   //8,
    									 0,
    									 0,
    									 Fapi_AutoEccGeneration);
    
     	while( Fapi_checkFsmForReady() == Fapi_Status_FsmBusy );
    	status =  Flash_Program_Check(Flash_Start_Address, Data_Start_Address, Size_In_Bytes);
    	return (status);
    }
    
    
    
    uint32_t Flash_Program_Check(uint32_t Program_Start_Address, uint32_t Source_Start_Address, uint32_t No_Of_Bytes)
    {
    	register uint32_t *src1 = (uint32_t *) Source_Start_Address;
    	register uint32_t *dst1 = (uint32_t *) Program_Start_Address;
    	register uint32_t bytes = No_Of_Bytes;
    
    	while(bytes > 0)
    	{	
    		if(*dst1++ != *src1++)
    			return (1);   //error
    
    		bytes -= 0x4;
    	}
    	return(0);
    }	
    
    
    uint32_t Flash_Erase_Check(uint32_t Start_Address, uint32_t Bytes)
    {
    	uint32_t error=0;
    	register uint32_t *dst1 = (uint32_t *) Start_Address;
    	register uint32_t bytes = Bytes;
    
    	while(bytes > 0)
    	{	
    		if(*dst1++ != 0xFFFFFFFF){
    			error = 2;
    		}
    		bytes -= 0x4;
    	}
    	return(error);
    }
    
    
    
    uint32_t Fapi_BlockRead( uint32_t Bank, uint32_t Flash_Start_Address, uint32_t Data_Start_Address, uint32_t Size_In_Bytes)
    {
    	register uint32_t src = Data_Start_Address;
    	register uint32_t dst = Flash_Start_Address;
    	register uint32_t bytes_remain = Size_In_Bytes;
    	int bytes;
    
    	if (Size_In_Bytes < 16)
    		bytes = Size_In_Bytes;
    	else
    		bytes = 16;
    	Fapi_initializeAPI((Fapi_FmcRegistersType *)F021_CPU0_REGISTER_ADDRESS, (uint32_t)SYS_CLK_FREQ);
    
     	while( bytes_remain > 0)
    	{
    		Fapi_doMarginReadByByte((uint8_t *)src,
    								(uint8_t *)dst,
    								(uint32_t) bytes,                //16
    								Fapi_NormalRead);
    		src += bytes;
    		dst += bytes;
            bytes_remain -= bytes;
        }
    	return (0);
    }
    

    /**********************************************************************************************************************
     *  COPYRIGHT
     *  -------------------------------------------------------------------------------------------------------------------
     *  \verbatim
     *
    %%Copyright%%
     *
     *  \endverbatim
     *  -------------------------------------------------------------------------------------------------------------------
     *  FILE DESCRIPTION
     *  -------------------------------------------------------------------------------------------------------------------
     *
     *      Project:  Hercules� ARM� Safety MCUs - F021 Flash API 
     *      Version:  v2.01.01 Build(000830)                                                                                
     *   Build Date:  2014-10-21                                                                                            
     *
    %%File%%
     *
     *  Description:  Contains all user defined callback functions used by the F021 Flash API.
     *---------------------------------------------------------------------------------------------------------------------
     * Author:  John R Hall
     *---------------------------------------------------------------------------------------------------------------------
     *
     *********************************************************************************************************************/
    
    /**********************************************************************************************************************
     * INCLUDES
     *********************************************************************************************************************/
    #include "F021.h"
    
    /*LDRA_HEADER_END*/
    
    /**********************************************************************************************************************
     *  Fapi_serviceWatchdogTimer
     *********************************************************************************************************************/
    /*! Callback function to service watchdog timer.  Used by the Blank, Read, and Verify fuctions. 
     *   
     *  \param [in]  none
     *  \param [out] none
     *  \return     Fapi_StatusType
     *  \retval     Fapi_Status_Success
     *  \note       TI FEE API.
     *********************************************************************************************************************/
    Fapi_StatusType Fapi_serviceWatchdogTimer(void)
    {
       /* User to add their own watchdog servicing code here */
    
       return(Fapi_Status_Success);
    }
    
    /**********************************************************************************************************************
     *  END OF FILE: Fapi_UserDefinedFunctions.c
     *********************************************************************************************************************/
    
    

    Regards,

    Minwoo

  • I just run a test on LC4357 Launchpad, there is no issue to erase the flash couple times. Here is my test case. 

    TMS570LC43x_FlashAPI_Test.zip

  • I check again. Then I find erase repeatly. Then the erase block stopped in BUSY state.

    I also check there is no error flash erase after flash program.

    Thanks for your guide.

    Regards,

    Minwoo