📄 example_flash281x_api.c
字号:
if(Status != STATUS_SUCCESS)
{
Example_Error(Status);
}
// Verify the values programmed. The Program step itself does a verify
// as it goes. This verify is a 2nd verification that can be done.
Status = Flash_Verify(Flash_ptr,Buffer,Length,&VerifyStatus);
if(Status != STATUS_SUCCESS)
{
Example_Error(Status);
}
// --------------
// You can program a single bit in a memory location and then go back to
// program another bit in the same memory location.
// Example: Program bit 0 in location in location 0x3F2000
// which is in Flash Sector C. That is program the value 0xFFFE
Flash_ptr = (Uint16 *)0x003F2000;
i = 0xFFFE;
Length = 1;
Status = Flash_Program(Flash_ptr,&i,Length,&ProgStatus);
if(Status != STATUS_SUCCESS)
{
Example_Error(Status);
}
// Example: Program bit 1 in the same location 0x3F2000. Remember
// that bit 0 was already programmed so the value will be 0xFFFC
// (bit 0 and bit 1 will both be 0)
i = 0xFFFC;
Length = 1;
Status = Flash_Program(Flash_ptr,&i,Length,&ProgStatus);
// This should return a STATUS_FAIL_ZERO_BIT_ERROR
if(Status != STATUS_SUCCESS)
{
Example_Error(Status);
}
// Verify the value in 0x3F2000. This first verify should fail.
i = 0xFFFE;
Status = Flash_Verify(Flash_ptr,&i,Length,&VerifyStatus);
if(Status != STATUS_FAIL_VERIFY)
{
Example_Error(Status);
}
// This is the correct value and will pass.
i = 0xFFFC;
Status = Flash_Verify(Flash_ptr,&i,Length,&VerifyStatus);
if(Status != STATUS_SUCCESS)
{
Example_Error(Status);
}
// --------------
// If a bit has already been programmed, it cannot be brought back to a 1 by
// the program function. The only way to bring a bit back to a 1 is by erasing
// the entire sector that the bit belongs to. This example shows the error
// that program will return if a bit is specified as a 1 when it has already
// been programmed to 0.
// Example: Program a single 16-bit value, 0x0002, into location 0x3F2001
// which is in Flash Sector C
Flash_ptr = (Uint16 *)0x003F2001;
i = 0x0000;
Length = 1;
Status = Flash_Program(Flash_ptr,&i,Length,&ProgStatus);
if(Status != STATUS_SUCCESS)
{
Example_Error(Status);
}
// Example: This will return an error!! Can't program 0x0001
// because bit 0 in the the location was previously programmed
// to zero!
i = 0x0001;
Length = 1;
Status = Flash_Program(Flash_ptr,&i,Length,&ProgStatus);
// This should return a STATUS_FAIL_ZERO_BIT_ERROR
if(Status != STATUS_FAIL_ZERO_BIT_ERROR)
{
Example_Error(Status);
}
// --------------
// Example: This will return an error!! The location specified
// 0x3F8000 is outside of the Flash and OTP!
Flash_ptr = (Uint16 *)0x003F8000;
i = 0x0001;
Length = 1;
Status = Flash_Program(Flash_ptr,&i,Length,&ProgStatus);
// This should return a STATUS_FAIL_ADDR_INVALID error
if(Status != STATUS_FAIL_ADDR_INVALID)
{
Example_Error(Status);
}
// --------------
// Example: This will return an error. The end of the buffer falls
// outside of the flash bank. No values will be programmed
for(i=0;i<13;i++)
{
Buffer[i] = 0xFFFF;
}
Flash_ptr = (Uint16 *)0x003F7FF8;
Length = 13;
Status = Flash_Program(Flash_ptr,Buffer,Length,&ProgStatus);
if(Status != STATUS_FAIL_ADDR_INVALID)
{
Example_Error(Status);
}
/*------------------------------------------------------------------
More Erase Sectors Examples - Clean up the sectors we wrote to:
------------------------------------------------------------------*/
// Example: Erase Sector B
// SECTORB is defined in Flash281x_API_Library.h
Status = Flash_Erase(SECTORB,&EraseStatus);
if(Status != STATUS_SUCCESS)
{
Example_Error(Status);
}
// Example: Erase Sector C and Sector E
// SECTORC and SECTORE are defined in Flash281x_API_Library.h
Status = Flash_Erase((SECTORC|SECTORE),&EraseStatus);
if(Status != STATUS_SUCCESS)
{
Example_Error(Status);
}
// Example: This will return an error. No valid sector is specified.
Status = Flash_Erase(0,&EraseStatus);
// Should return STATUS_FAIL_NO_SECTOR_SPECIFIED
if(Status != STATUS_FAIL_NO_SECTOR_SPECIFIED)
{
Example_Error(Status);
}
Example_Done();
}
/*------------------------------------------------------------------
Example_CsmUnlock
Unlock the code security module (CSM)
Parameters:
Return Value:
STATUS_SUCCESS CSM is unlocked
STATUS_FAIL_UNLOCK CSM did not unlock
Notes:
-----------------------------------------------------------------*/
Uint16 Example_CsmUnlock()
{
volatile Uint16 temp;
// Load the key registers with the current password
// These are defined in Example_Flash281x_CsmKeys.asm
EALLOW;
*KEY0 = PRG_key0;
*KEY1 = PRG_key1;
*KEY2 = PRG_key2;
*KEY3 = PRG_key3;
*KEY4 = PRG_key4;
*KEY5 = PRG_key5;
*KEY6 = PRG_key6;
*KEY7 = PRG_key7;
EDIS;
// Perform a dummy read of the password locations
// if they match the key values, the CSM will unlock
temp = *PWL0;
temp = *PWL1;
temp = *PWL2;
temp = *PWL3;
temp = *PWL4;
temp = *PWL5;
temp = *PWL6;
temp = *PWL7;
// If the CSM unlocked, return succes, otherwise return
// failure.
if ( (*CSMSCR & 0x0001) == 0) return STATUS_SUCCESS;
else return STATUS_FAIL_CSM_LOCKED;
}
/*------------------------------------------------------------------
Example_ToggleTest
This function shows example calls to the ToggleTest.
This test is used to Toggle a GPIO pin at a known rate and thus
confirm the frequency configuration of the API functions.
Common output pins are supported here, however the user could
call the function with any GPIO mux register and pin mask.
A pin should be selected based on the hardware being used.
Parameters: Uint16 PinNumber
Return Value: The toggle test does not return. It will loop
forever and is used only for testing purposes.
Notes:
----------------------------------------------------------------*/
void Example_ToggleTest(Uint16 PinNumber)
{
switch(PinNumber) {
case 0:
break;
case 1:
Flash_ToggleTest(GPFMUX,GPFTOGGLE,GPIOF14_XF_MASK);
break;
case 2:
Flash_ToggleTest(GPAMUX,GPATOGGLE,GPIOA0_PWM1_MASK);
break;
case 3:
Flash_ToggleTest(GPFMUX,GPFTOGGLE,GPIOF4_SCITXDA_MASK);
break;
case 4:
Flash_ToggleTest(GPGMUX,GPGTOGGLE,GPIOG4_SCITXDB_MASK);
break;
case 5:
Flash_ToggleTest(GPFMUX,GPFTOGGLE,GPIOF12_MDXA_MASK);
break;
default:
break;
}
}
/*------------------------------------------------------------------
Callback function - must be executed from outside flash/OTP
-----------------------------------------------------------------*/
#pragma CODE_SECTION(MyCallbackFunction,"ramfuncs");
void MyCallbackFunction(void)
{
// Toggle pin, service external watchdog etc
MyCallbackCounter++;
asm(" NOP");
}
/*------------------------------------------------------------------
Simple memory copy routine to move code out of flash into SARAM
-----------------------------------------------------------------*/
void Example_MemCopy(Uint16 *SourceAddr, Uint16* SourceEndAddr, Uint16* DestAddr)
{
while(SourceAddr < SourceEndAddr)
{
*DestAddr++ = *SourceAddr++;
}
return;
}
/*------------------------------------------------------------------
For this example, if an error is found just stop here
-----------------------------------------------------------------*/
#pragma CODE_SECTION(Example_Error,"ramfuncs");
void Example_Error(Uint16 Status)
{
// Error code will be in the AL register.
asm(" ESTOP0");
asm(" SB 0, UNC");
}
/*------------------------------------------------------------------
For this example, once we are done just stop here
-----------------------------------------------------------------*/
#pragma CODE_SECTION(Example_Done,"ramfuncs");
void Example_Done(void)
{
asm(" ESTOP0");
asm(" SB 0, UNC");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -