Esempio n. 1
0
static boolean_t
get_sector_info(uint32_t addr, sector_info_t *info)
{
	int bank_id, sector_id;
	Fapi_StatusType status;
	Fapi_FlashBankSectorsType fbs;
	uint32_t sector_addr, asked_addr = (uint32_t)addr;
	uint32_t sector_size;

	for (bank_id = 0; bank_id < ARRAY_COUNT(bank_ids); bank_id++) {
		status = Fapi_getBankSectors(bank_ids[bank_id], &fbs);
		if (Fapi_Status_Success != status) {
			continue;
		}
		sector_addr = fbs.u32BankStartAddress;
		for (sector_id = 0; sector_id < fbs.u32NumberOfSectors; sector_id++) {
			if (Fapi_FLEE == fbs.oFlashBankTech) {
				sector_size = fbs.au16SectorSizes[0];
			} else {
				sector_size = fbs.au16SectorSizes[sector_id];
			}
			sector_size *= 1024;
			if (sector_addr <= asked_addr && asked_addr < sector_addr + sector_size) {
				info->bankTech = fbs.oFlashBankTech;
				info->bankId = bank_ids[bank_id];
				info->sectorId = sector_id;
				info->sectorStartAddress = sector_addr;
				info->sectorSize = sector_size;

				return true;
			}
			sector_addr += sector_size;
		}
	}

	return false;
}
void Example_CallFlashAPI(void)
{
    uint32 u32Index = 0;
    uint16 i = 0;

    Fapi_StatusType            oReturnCheck;
    Fapi_LibraryInfoType       oLibInfo;
    Fapi_FlashStatusType       oFlashStatus;
    Fapi_FlashBankSectorsType  oFlashBankSectors;
    Fapi_FlashStatusWordType   oFlashStatusWord;

    // Disable ECC.  ECC does not have to be disabled to do FSM operations like
    // program and erase.
    // However, on Sonata Rev. 0 silicon, due to an OTP ECC errata,
    // disable ECC to avoid ECC errors while using Flash API functions that
    // read TI-OTP
    EALLOW;
    FlashEccRegs.ECC_ENABLE.bit.ENABLE = 0x0;
    EDIS;

    EALLOW;

    // This function is required to initialize the Flash API based on System
    // frequency before any other Flash API operation can be performed
    #if CPU_FRQ_150MHZ
    oReturnCheck = Fapi_initializeAPI(F021_CPU0_BASE_ADDRESS, 150);
    #endif

    #if CPU_FRQ_100MHZ
    oReturnCheck = Fapi_initializeAPI(F021_CPU0_BASE_ADDRESS, 100);
    #endif

    #if CPU_FRQ_60MHZ
    oReturnCheck = Fapi_initializeAPI(F021_CPU0_BASE_ADDRESS, 60);
    #endif

    if(oReturnCheck |= Fapi_Status_Success)
    {
        // Check Flash API documentation for possible errors
        Example_Error(oReturnCheck);
    }
    
    // Fapi_getLibraryInfo function can be used to get the information specific
    // to the compiled version of the API library
    oLibInfo = Fapi_getLibraryInfo();

    // Before performing FSM operations, set the waitstates for FSM operations
    // calculated using RWAIT = (SYSCLK/(2*24MHz))-1
    // If RWAIT results in a fractional value, round it up to the nearest
    // integer.
    // Please note that RWAIT for read operation should be calculated
    // differently.  See Internal Memory guide section in TRM for more details.

       #if CPU_FRQ_150MHZ
       FlashCtrlRegs.FRDCNTL.bit.RWAIT = 0x3;
       #endif

       #if CPU_FRQ_100MHZ
       FlashCtrlRegs.FRDCNTL.bit.RWAIT = 0x2;
       #endif

       #if CPU_FRQ_60MHZ
       FlashCtrlRegs.FRDCNTL.bit.RWAIT = 0x1;
       #endif

    // Fapi_setActiveFlashBank function sets the Flash bank and FMC for further
    // Flash operations to be performed on the bank
    oReturnCheck = Fapi_setActiveFlashBank(Fapi_FlashBank0);
    if(oReturnCheck |= Fapi_Status_Success)
    {
        // Check Falsh API documentation for possible errors
        Example_Error(oReturnCheck);
    }

    // Fapi_getBankSectors function returns the bank starting address, number of
    // sectors, sector sizes, and bank technology type
    // Above information is returned in a structure oFlashBankSectors of type
    // Fapi_FlashBankSectorsType
    oReturnCheck = Fapi_getBankSectors(Fapi_FlashBank0,&oFlashBankSectors);
    if(oReturnCheck |= Fapi_Status_Success)
    {
        //Check Falsh API documentation for possible errors
        Example_Error(oReturnCheck);
    }

    // Erase Sector C
    // Sectors A and D have the example code so leave them programmed
    oReturnCheck = Fapi_issueAsyncCommandWithAddress(Fapi_EraseSector,
                   (uint32 *)Bzero_SectorC_start);
    
    // Wait until FSM is done with erase sector operation
    while (Fapi_checkFsmForReady() != Fapi_Status_FsmReady){}
    
    // Verify that SectorL is erased.  The Erase step itself does a
    // verify as it goes.  This verify is a 2nd verification that can be done.
    oReturnCheck = Fapi_doBlankCheck((uint32 *)Bzero_SectorC_start,
                   Bzero_16KSector_u32length,
                   &oFlashStatusWord);
    
    if(oReturnCheck |= Fapi_Status_Success)
    {
        // Check Falsh API documentation for possible errors
        // If Erase command fails, use Fapi_getFsmStatus() function to get the
        // FMSTAT register contents to see if any of the EV bit, ESUSP bit,
        // CSTAT bit or VOLTSTAT bit is set (Refer to API documentation for
        // more details)
        Example_Error(oReturnCheck);
    }
    
    // Erase Sector B
    // Sector N has the example code so leave it programmed
    oReturnCheck = Fapi_issueAsyncCommandWithAddress(Fapi_EraseSector,
                   (uint32 *)Bzero_SectorB_start);
    
    // Wait until FSM is done with erase sector operation
    while (Fapi_checkFsmForReady() != Fapi_Status_FsmReady){}
    // Verify that SectorK is erased.  The Erase step itself does a verify as
    // it goes.  This verify is a 2nd verification that can be done.
    oReturnCheck = Fapi_doBlankCheck((uint32 *)Bzero_SectorB_start,
                   Bzero_16KSector_u32length,
                   &oFlashStatusWord);
    
    if(oReturnCheck |= Fapi_Status_Success)
    {
        // Check Flash API documentation for possible errors
        // If Erase command fails, use Fapi_getFsmStatus() function
        // to get the FMSTAT register contents
        // to see if any of the EV bit, ESUSP bit, CSTAT bit or VOLTSTAT
        // bit is set (Refer to API documentation for more details)
        Example_Error(oReturnCheck);
    }		


    // A data buffer of max 8 words can be supplied to the program function.
    // Each word is programmed until the whole buffer is programmed or a
    // problem is found. However to program a buffer that has more than 8
    // words, program function can be called in a loop to program 8 words for
    // each loop iteration until the whole buffer is programmed


    // Example: Program 0xFF bytes in Flash Sector C along with auto-
    // generated ECC

    // In this case just fill a buffer with data to program into the flash.
    for(i=0;i<=WORDS_IN_FLASH_BUFFER;i++)
    {
        Buffer[i] = i;
    }


    for(i=0, u32Index = Bzero_SectorC_start;
       (u32Index < (Bzero_SectorC_start + WORDS_IN_FLASH_BUFFER))
       && (oReturnCheck == Fapi_Status_Success); i+= 8, u32Index+= 8)
    {
        oReturnCheck = Fapi_issueProgrammingCommand((uint32 *)u32Index,Buffer+i,
                       8,
                       0,
                       0,
                       Fapi_AutoEccGeneration);
        
        while(Fapi_checkFsmForReady() == Fapi_Status_FsmBusy);

        if(oReturnCheck |= Fapi_Status_Success)
        {
        // Check Flash API documentation for possible errors
        Example_Error(oReturnCheck);
        }

        // Read FMSTAT register contents to know the status of FSM after
        // program command for any debug
        oFlashStatus = Fapi_getFsmStatus();

        // Verify the values programmed.  The Program step itself does a verify
        // as it goes.  This verify is a 2nd verification that can be done.
        oReturnCheck = Fapi_doVerify((uint32 *)u32Index,
                       4,
                       Buffer32+(i/2),
                       &oFlashStatusWord);
        if(oReturnCheck |= Fapi_Status_Success)
        {
        // Check Flash API documentation for possible errors
        Example_Error(oReturnCheck);
        }
    }
    
    // Example:  Program 0xFF bytes in Flash Sector B with out ECC
    // Disable ECC so that error is not generated when reading Flash contents
    // without ECC
    FlashEccRegs.ECC_ENABLE.bit.ENABLE = 0x0;
    
    for(i=0, u32Index = Bzero_SectorB_start;
       (u32Index < (Bzero_SectorB_start + WORDS_IN_FLASH_BUFFER))
       && (oReturnCheck == Fapi_Status_Success); i+= 8, u32Index+= 8)
    {
        oReturnCheck = Fapi_issueProgrammingCommand((uint32 *)u32Index,
                       Buffer+i,
                       8,
                       0,
                       0,
                       Fapi_DataOnly);
        while(Fapi_checkFsmForReady() == Fapi_Status_FsmBusy);

        if(oReturnCheck |= Fapi_Status_Success)
        {
        // Check Flash API documentation for possible errors
        Example_Error(oReturnCheck);
        }

        // Read FMSTAT register contents to know the status of FSM
        // after program command for any debug
        oFlashStatus = Fapi_getFsmStatus();

        // Verify the values programmed.  The Program step itself does a verify
        // as it goes.  This verify is a 2nd verification that can be done.
        oReturnCheck = Fapi_doVerify((uint32 *)u32Index,
                       4,
                       Buffer32+(i/2),
                       &oFlashStatusWord);
        if(oReturnCheck |= Fapi_Status_Success)
        {
        // Check Flash API documentation for possible errors
        Example_Error(oReturnCheck);
        }
    }	
    
    // Erase the sectors that we have programmed above	
    // Erase Sector C
    // Sectors A and D have the example code so leave them programmed
    oReturnCheck = Fapi_issueAsyncCommandWithAddress(Fapi_EraseSector,
                   (uint32 *)Bzero_SectorC_start);

    // Wait until FSM is done with erase sector operation
    while (Fapi_checkFsmForReady() != Fapi_Status_FsmReady){}

    // Verify that SectorL is erased.  The Erase step itself does a verify as
    // it goes.
    // This verify is a 2nd verification that can be done.
    oReturnCheck = Fapi_doBlankCheck((uint32 *)Bzero_SectorC_start,
                   Bzero_16KSector_u32length,
                   &oFlashStatusWord);

    if(oReturnCheck |= Fapi_Status_Success)
    {
        // Check Falsh API documentation for possible errors
        // If Erase command fails, use Fapi_getFsmStatus() function to get the
        // FMSTAT register contents
        // to see if any of the EV bit, ESUSP bit, CSTAT bit or VOLTSTAT bit is
        // set (Refer to API documentation for more details)
        Example_Error(oReturnCheck);
    }
    
    // Erase Sector B
    // Sector N has the example code so leave it programmed
    oReturnCheck = Fapi_issueAsyncCommandWithAddress(Fapi_EraseSector,
                   (uint32 *)Bzero_SectorB_start);
    // Wait until FSM is done with erase sector operation
    while (Fapi_checkFsmForReady() != Fapi_Status_FsmReady){}
    // Verify that SectorK is erased.  The Erase step itself does a verify
    // as it goes.  This verify is a 2nd verification that can be done.
    oReturnCheck = Fapi_doBlankCheck((uint32 *)Bzero_SectorB_start,
                   Bzero_16KSector_u32length,
                   &oFlashStatusWord);

    if(oReturnCheck |= Fapi_Status_Success)
    {
        // Check Flash API documentation for possible errors
        // If Erase command fails, use Fapi_getFsmStatus() function to get
        // the FMSTAT register contents to see if any of the EV bit, ESUSP bit,
        // CSTAT bit or VOLTSTAT bit is set (Refer to API documentation
        // for more details)
        Example_Error(oReturnCheck);
    }		
    
    // Enable ECC
    FlashEccRegs.ECC_ENABLE.bit.ENABLE = 0xA;	
    
    EDIS;
    
    // Leave control over flash pump
    FlashLeavePump();
    
    // Example is done here
    Example_Done();

}