/***************************************************************************************************
* Filename : MAIN.C
* Last Modified : 23 Jan 2002.
* Version : 1.3
* Originator : Texas Instruments, DSP Digital Control Systems Group.
***************************************************************************************************
* Description
* This file supplies an example of the application of embedded flash
* programming algorithms.
*
* Revision history :
* Version 1.0 (25 May 2001):
* New implementation for the embeddable flash algos.
*
* Version 1.1 (10 Aug 2001):
* No functional changes.
* Version 1.2 (16 Oct 2001):
* 1.Modified the header to fix the filename/ version/ date.
* 2.Fixed the positioning of the while(1); loop to end the program. Moved this
* while loop to the kernel since there is nothing to return to in flash.
* Version 1.3 (23 Jan 2002)
* 1. Added more elements to the int bufferArray array.
* 2. Changed the parameter setup for the call to the program function to
* program 10 words.
* 3. Added code to generate an error when compiled with DEV_TYPE set to
* non LF2407 device in VAR.H. See VAR.H for setting DEV_TYPE.
*
*************************************************************************************************/
/************************************************************************************************
This example is intended to demonstrate how the algos can co-exist in RAM. However the size of
all the routines together in RAM exceeds the RAM available on the LF2401A device, whereas it is
within the available RAM space on the LF2407 and LF2407A.
So the following assember statements generate an error when the routines are compiled with the
DEV_TYPE set to other than LF2407 (e.g. Lf2401A).
*************************************************************************************************/
asm(" .include ..\\flashalgos\\include\\var.h ");
asm(" .if (DEV_TYPE != LF2407) ");
asm(" .emsg \"ERROR: DEV TYPE is not LF2407\" ");
asm(" .endif ");
/*************************************************************************************************
* The following is the sequence to re-program a section of the flash:
*
* 1. Copy the flash programming algorithms to RAM.
* This is because NO code can run from flash at-all while reprogramming
* the flash.
*
* 2. Copy the sequencer / kernel to the RAM. This would contain the
* implementation of all control code / communications routines etc.
*
* 3. Before running the flash algorithms, the watchdog is shut down. The
* flash programming algorithms must not be interrupted. So before
* calling any algos, all interrupts must be shut off as well.
************************************************************************************************/
/* Compiler directives to place all the kernel functions in the KER_text section.
* This section is handled by the linker command file to have a load address in the
* flash memory but a run addres in the SARAM block. This is copied at run time
* to the run address by the copyKernel function.
*/
#pragma CODE_SECTION(kernelControlLoop,"KER_text")
#pragma CODE_SECTION(handleError,"KER_text")
/* Function prototypes and constants, registers etc.
*/
void lf240xInitSys(void);
int handleError(void);
int kernelControlLoop(void);
/* Import the function prototypes and variable information from the
* flash algorithms
*/
#include "..\\flashalgos\\include\\CFlash.h"
/* Import the function prototypes and variable information from the
* flash algorithms
*/
#include "regs240x.h"
/* Buffer to hold data for programming into flash
*/
int bufferArray[]={0x1245,0x7436,0x8436,0x8733,0x4398,
0x4943,0x2953,0x9282,0x7838,0x3273 };
main()
{
/*
* Call the system initialization function.
* For flash programming at a minimum the Watchdog must be shut down
* and the PLL initialized
*/
lf240xInitSys();
/* Copy the algos to RAM and run them from there since flash cant
* run code while being programmed.
*/
copyClearAlgo();
copyEraseAlgo();
copyProgramAlgo();
copyKernel();
kernelControlLoop();
}
int kernelControlLoop(void)
{
/* Setup the sector mask */
flashAlgoVars.SECTOR_CMD=(SECTOR0+SECTOR1+SECTOR2+SECTOR3);
/* Call algo */
clearFlash(&flashAlgoVars);
/* Error checking */
if(0!=flashAlgoVars.ALGO_STATUS)
{
handleError();
}
/* Setup the sector mask */
flashAlgoVars.SECTOR_CMD=(SECTOR0+SECTOR1+SECTOR2+SECTOR3);
/* Call algo */
eraseFlash(&flashAlgoVars);
/* Error checking */
if(0!=flashAlgoVars.ALGO_STATUS)
{
handleError();
}
/* Setup the sector permissions */
flashAlgoVars.SECTOR_CMD=(SECTOR0+SECTOR1+SECTOR2+SECTOR3);
/* Setup ptr to data buffer. */
flashAlgoVars.DATA_PTR=(int)(&bufferArray);
/* Number of words to program */
flashAlgoVars.FL_SECEND=10;
/* Block address */
flashAlgoVars.ADDR=0x0023;
/* Call the program routine */
programFlash(&flashAlgoVars);
/* Error checking */
if(0!=flashAlgoVars.ALGO_STATUS)
{
handleError();
}
/* Since we cleared and erase all the sectors there is nothing in flash to
* return to. So we stay here.
*/
while(1);
}
int handleError(void)
{
while(1);
}
void lf240xInitSys(void)
{
WDCR=0x6f; /* Dis-able the watchdog */
SCSR1=0x0; /* PLL = x4 */
}
评论0