📄 example_flash281x_api.c
字号:
//###########################################################################
//
// FILE: Example_Flash281x_API.c
//
// TITLE: F281x Flash API Example
//
// NOTE: This example runs from Flash. First program the example
// into flash. The code will then copy the API's to RAM and
// modify the flash.
//
//
//###########################################################################
// $TI Release: Flash281x API V2.10 $
// $Release Date: August 4, 2005 $
//###########################################################################
/*---- Flash API include file -------------------------------------------------*/
#include "Flash281x_API_Library.h"
/*---- example include file -------------------------------------------------*/
#include "Example_Flash281x_API.h"
/*--- Global variables used to interface to the flash routines */
FLASH_ST EraseStatus;
FLASH_ST ProgStatus;
FLASH_ST VerifyStatus;
/*--- Callback function. Function specified by defining Flash_CallbackPtr */
void MyCallbackFunction(void);
Uint32 MyCallbackCounter; // Just increment a counter in the callback function
void main( void )
{
/*------------------------------------------------------------------
To use the F2812, F2811 or F2810 Flash API, the following steps
must be followed:
1. Modify Flash281x_API.config.h for your targets operating
conditions.
2. Include Flash281x_API_Library.h in the application.
3. Add the approparite Flash API library to the project.
The user's code is responsible for the following:
4. Initalize the PLL to the proper CPU operating frequency.
5. If required, copy the flash API functions into on-chip zero waitstate
RAM.
6. Initalize the Flash_CPUScaleFactor variable to SCALE_FACTOR
7. Initalize the flash callback function pointer.
8. Optional: Run the Toggle test to confirm proper frequency configuration
of the API.
9. Optional: Unlock the CSM.
10. Call the API functions: Flash_Erase(), Flash_Program(), Flash_Verify()
The API functions will:
Disable the watchdog
Check the device revision (REVID). This API is for Revision C silicon
Perform the desired operation and return status
------------------------------------------------------------------*/
Uint16 i;
Uint16 Status;
/*------------------------------------------------------------------
Initalize the PLLCR value before calling any of the F2810, F2811
or F281x Flash API functions.
Check to see if the PLL needs to changed
PLLCR_VALUE is defined in Example_Flash281x_API.h
1) Make the change
2) Wait for the DSP to switch to the PLL clock
This wait is performed to ensure that the flash API functions
will be executed at the correct frequency.
3) While waiting, feed the watchdog so it will not reset.
------------------------------------------------------------------*/
if(*PLLCR != PLLCR_VALUE)
{
EALLOW;
*PLLCR = PLLCR_VALUE;
// Wait for PLL to lock
// Each time through this loop takes ~14 cycles
// PLL Lock time is 131072 Cycles
for(i= 0; i< 131072/14; i++){
*WDKEY = 0x0055;
*WDKEY = 0x00AA;
}
EDIS;
}
/*------------------------------------------------------------------
Unlock the CSM.
If the API functions are going to run in unsecured RAM
then the CSM must be unlocked in order for the flash
API functions to access the flash.
If the flash API functions are executed from secure memory
(L0/L1) then this step is not required.
------------------------------------------------------------------*/
Status = Example_CsmUnlock();
if(Status != STATUS_SUCCESS)
{
Example_Error(Status);
}
/*------------------------------------------------------------------
Copy API Functions into SARAM
The flash API functions MUST be run out of internal
zero-waitstate SARAM memory. This is required for
the algos to execute at the proper CPU frequency.
If the algos are already in SARAM then this step
can be skipped.
DO NOT run the algos from Flash
DO NOT run the algos from external memory
------------------------------------------------------------------*/
// Copy the Flash API functions to SARAM
Example_MemCopy(&Flash28_API_LoadStart, &Flash28_API_LoadEnd, &Flash28_API_RunStart);
// We must also copy required user interface functions to RAM.
Example_MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart);
/*------------------------------------------------------------------
Initalize Flash_CPUScaleFactor.
Flash_CPUScaleFactor is a 32-bit global variable that the flash
API functions use to scale software delays. This scale factor
must be initalized to SCALE_FACTOR by the user's code prior
to calling any of the Flash API functions. This initalization
is VITAL to the proper operation of the flash API functions.
SCALE_FACTOR is defined in Example_Flash281x_API.h as
#define SCALE_FACTOR 1048576.0L*( (200L/CPU_RATE) )
This value is calculated during the compile based on the CPU
rate, in nanoseconds, at which the algorithums will be run.
------------------------------------------------------------------*/
Flash_CPUScaleFactor = SCALE_FACTOR;
/*------------------------------------------------------------------
Initalize Flash_CallbackPtr.
Flash_CallbackPtr is a pointer to a function. The API uses
this pointer to invoke a callback function during the API operations.
If this function is not going to be used, set the pointer to NULL
NULL is defined in <stdio.h>.
------------------------------------------------------------------*/
Flash_CallbackPtr = &MyCallbackFunction;
MyCallbackCounter = 0; // Increment this counter in the callback function
// Jump to SARAM and call the Flash API functions
Example_CallFlashAPI();
}
/*------------------------------------------------------------------
Example_CallFlashAPI
This function will interface to the flash API.
Parameters:
Return Value:
Notes: This function will be executed from SARAM
-----------------------------------------------------------------*/
#pragma CODE_SECTION(Example_CallFlashAPI,"ramfuncs");
void Example_CallFlashAPI(void)
{
Uint16 i;
Uint16 Status;
Uint16 *Flash_ptr; // Pointer to a location in flash
Uint32 Length; // Number of 16-bit values to be programmed
float32 VersionFloat;
Uint16 VersionHex;
/*------------------------------------------------------------------
Toggle Test
The toggle test is run to verify the frequency configuration of
the API functions.
The selected pin will toggle at 10kHz (100uS cycle time) if the
API is configured correctly.
Example_ToggleTest() supports common output pins. Other pins can be used
by modifying the Example_ToggleTest() function or calling the Flash_ToggleTest()
function directly.
Select a pin that makes sense for the hardware platform being used.
This test will run forever and not return, thus only run this test
to confirm frequency configuration and not during normal API use.
------------------------------------------------------------------*/
// Example: Toggle XF
// Example_ToggleTest(TOGGLE_XF);
// Example: Toggle PWM1
// Example_ToggleTest(TOGGLE_PWM1);
// Example: Toggle SCITXDA
// Example_ToggleTest(TOGGLE_SCITXDA);
/*------------------------------------------------------------------
Get the API version in hex or floating point
------------------------------------------------------------------*/
VersionFloat = Flash_APIVersion();
if(VersionFloat != (float32)2.10)
{
Example_Error(1);
}
VersionHex = Flash_APIVersionHex();
if(VersionHex != 0x0210)
{
Example_Error(1);
}
/*------------------------------------------------------------------
Before programming make sure the sectors are Erased.
------------------------------------------------------------------*/
// Example: Erase Sector B,C,E
// Sectors A and D have the example code so leave them
// programmed.
// SECTORB, SECTORC and SECTORE are defined in Flash281x_API_Library.h
Status = Flash_Erase((SECTORB|SECTORC|SECTORE),&EraseStatus);
if(Status != STATUS_SUCCESS)
{
Example_Error(Status);
}
/*------------------------------------------------------------------
Program Flash Examples
------------------------------------------------------------------*/
// A buffer can be supplied to the program function. Each word is
// programmed until the whole buffer is programmed or a problem is
// found. If the buffer goes outside of the range of OTP or Flash
// then nothing is done and an error is returned.
// Example: Program 0x400 values in Flash Sector E starting at
// 0x3E8000.
// In this case just fill a buffer with data to program into the flash.
for(i=0;i<0x400;i++)
{
Buffer[i] = 0x8000+i;
}
Flash_ptr = (Uint16 *)0x003E8000;
Length = 0x400;
Status = Flash_Program(Flash_ptr,Buffer,Length,&ProgStatus);
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);
}
// --------------
// Example: Program 0x199 values in Flash Sector B starting at
// 0x3F4500.
for(i=0;i<0x400;i++)
{
Buffer[i] = 0x4500+i;
}
Flash_ptr = (Uint16 *)0x003F4500;
Length = 0x199;
Status = Flash_Program(Flash_ptr,Buffer,Length,&ProgStatus);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -