📄 flashapi.c
字号:
/*=====================================================================================
File name: FlashAPI.C (28x version)
Originator: Seed Group
Dsscription: Flash API
=====================================================================================
History:
-------------------------------------------------------------------------------------
07-27-2005 Release Rev 1.0
-------------------------------------------------------------------------------------*/
#include "DSP281x_Device.h"
/********************************************/
/*---- Flash API include file -------------------------------------------------*/
#include "Flash281x_API_Library.h"
/*---- example include file -------------------------------------------------*/
#include "Example_Flash281x_API.h"
/*
Executing Your Code from Flash after a DSP Reset:
F281x devices contain a ROM bootloader that can transfer code execution to the
flash after a device reset. The ROM bootloader is detailed in "TMS320F28x Boot
ROM Peripheral Reference Guide (SPRU095)". When the boot mode selection pins are
configured for "Jump to Flash" mode, the ROM bootloader will branch to the
instruction located at address 0x3F7FF6 in the flash. The user should place an
instruction that branches to the beginning of their code at this address. Recall
that the CSM passwords begin at address 0x3F7FF8, such that exactly 2 words are
available to hold this branch instruction. Not coincidentally, a long branch
instruction (LB in assembly code) occupies exactly 2 words.In general, the
branch instruction will branch to the start of the C-environment initialization
routine located in the C-compiler runtime support library. The entry symbol for
this routine is _c_int00. No C code can be executed until this setup routine is
run. Alternately, there is sometimes a need to execute a small amount of
assembly code prior to starting your C application (for example, to disable the
watchdog timer peripheral). In this case, the branch instruction should branch
to the start of your assembly code. Regardless, there is a need to properly
locate this branch instruction in the flash. The easiest way to do this is with
assembly code. The following example creates a named initialized section called
codestart that contains a long branch to the C-environment setup routine. The
codestart section should be placed in memory with the user linker command file.
More executing Your Code from Flash after a DSP Reset is detailed in "Running an
Application from Internal Flash Memory on the TMS320F281x DSP(SPRA958D)" .
*/
/*--- Global variables used to interface to the flash routines */
FLASH_ST EraseStatus;
FLASH_ST ProgStatus;
FLASH_ST VerifyStatus;
extern Uint32 Flash_CPUScaleFactor;
/********************************************/
const long Tp = 7500; //PeriodCounter=ControlPeriod*Freq(us*M);HISPCP
const long CPUTime_T0 = 100; //CPU定时时间
//当使用Flash.CMD时应将程序copy到RAM中运行,搬移地址定义
extern Uint16 RamfuncsLoadStart;
extern Uint16 RamfuncsLoadEnd;
extern Uint16 RamfuncsRunStart;
interrupt void cpu_timer0_isr(void); //定时器中断
long a=0,b=0,i=0;
void main(void)
{/****************************/
Uint16 i;
Uint16 Status;
/*------------------------------------------------------------------
Initalize the PLLCR value before calling 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;
}
Status = Example_CsmUnlock();
if(Status != STATUS_SUCCESS)
{
Example_Error(Status);
}
// 如果将程序完全烧进Flash需要进行如下操作
//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);
//InitFlash();
Flash_CPUScaleFactor = SCALE_FACTOR;
// Jump to SARAM and call the Flash API functions
Example_CallFlashAPI();
/*********************************/
//以上是关于Flash的操作,以下初始化系统执行相应期待操作
InitSysCtrl();
DINT;
InitPieCtrl();
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable();
EALLOW;
PieVectTable.TINT0 = &cpu_timer0_isr;
EDIS;
InitPeripherals();
InitCpuTimers();
ConfigCpuTimer(&CpuTimer0, 150, CPUTime_T0);
StartCpuTimer0();
IER |= M_INT1;
PieCtrlRegs.PIEIER1.bit.INTx7 = 1;
EINT;
while(1)
{ for(i=0;i<80000;i++){};
if(i>=80000)i=0;
}
}
interrupt void cpu_timer0_isr(void)
{
a++;
b=a+i;
if(a>=80000)a=0;
if(b>=80000)b=0;
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
}
//#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
/*------------------------------------------------------------------
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);
/*------------------------------------------------------------------
Before programming make sure the sectors are Erased.
------------------------------------------------------------------*/
// Example: Erase Sector F
// SECTORX are defined in Flash281x_API_Library.h
Status = Flash_Erase((SECTORJ),&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);
}
//Status = Flash_Erase((SECTORJ),&EraseStatus);
//if(Status != STATUS_SUCCESS)
//{
// Example_Error(Status);
//}
}
/*************************************************************/
//以上程序先擦写段F,然后写进一个数组,然后再将这个段擦除
//以下是一些子程序
/*************************************************************/
void Example_MemCopy(Uint16 *SourceAddr, Uint16* SourceEndAddr, Uint16* DestAddr)
{
while(SourceAddr < SourceEndAddr)
{
*DestAddr++ = *SourceAddr++;
}
return;
}
Uint16 Example_CsmUnlock()
{
volatile Uint16 temp;
// Load the key registers with the current password
// These are defined in Example_Flash281x_CsmKeys.asm
EALLOW;
*KEY0 = 0xffff;
*KEY1 = 0xffff;
*KEY2 = 0xffff;
*KEY3 = 0xffff;
*KEY4 = 0xffff;
*KEY5 = 0xffff;
*KEY6 = 0xffff;
*KEY7 = 0xffff;
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;
}
void Example_Error(Uint16 Status)
{
int i;
i=Status;
Status=i;
//Error code will be in the AL register.
asm(" ESTOP0");
asm(" SB 0, UNC");
}
//===========================================================================
// No more.
//===========================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -