kinetis入门之——FLASH
0赞kinetis可以支持在程序运行时改写FLASH。下面详述具体步骤:
1、点“文件->新建->bareboard project”出现下面对话框:
为工程起个名字(该示例为k10_flash_pe),点“下一步”,出现:
选择你所用的芯片型号(这里选“PK10X256”)即可。点“下一步”,出现:
“Connetion to be used”中先选我们先前建好的“USBDM K10X256”,点“下一步”,一直点“下一步”直到出现:
选“Processor Export”,点“下一步”,出现:
选实际用到的芯片(我用的是144-pins LQFP),点“完成”,出现:
等待上面对话框消失,一个工程就建好了。
双击“Memory”下的“FLASH_LDD”添加一个“FLASH Bean”,如下图:
配置如下图:
Methods”里
点“项目”->“Generate Processor Export Code”如下图:
下面是“ProcessorExpert.c”的“main()”函数代码:
//ProcessorExpert.c
/* Including needed modules to compile this module/procedure */
#include "Cpu.h"
#include "Events.h"
#include "FLASH1.h"
/* Including shared modules, which are used for whole project */
#include "PE_Types.h"
#include "PE_Error.h"
#include "PE_Const.h"
#include "IO_Map.h"
/* User includes (#include below this line is not maintained by Processor Expert) */
#define MY_FLASH_LOCATION 0x10000000 /* Flash memory location the data will be written to */
volatile bool DataWrittenFlg = FALSE;
char Data[] = "Hello world";
LDD_TError Error;
LDD_TDeviceData *MyFLASH_Ptr;
void main(void)
{
/* Write your local variable definition here */
/*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
PE_low_level_init();
/*** End of Processor Expert internal initialization. ***/
/* Write your code here */
/* For example: for(;;) { } */
MyFLASH_Ptr = FLASH1_Init(NULL); /* Initialization of FLASH1 component */
Error = FLASH1_Write(MyFLASH_Ptr, Data, MY_FLASH_LOCATION, sizeof(Data)); /* Start writing to the flash memory */
/* Here some other application code can be placed */
while (!DataWrittenFlg) { /* Wait until the data are written */
}
DataWrittenFlg = FALSE;
if (FLASH1_GetOperationStatus(MyFLASH_Ptr) == LDD_FLASH_FAILED) { /* Check if the operation has successfully ended */
/* Error state solution */
}
/*** Don't write any code pass this line, or it will be deleted during code generation. ***/
/*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
for(;;){}
/*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/
//Events.c
#include "Cpu.h"
#include "Events.h"
/* User includes (#include below this line is not maintained by Processor Expert) */
extern volatile bool DataWrittenFlg;
/*
** ===================================================================
** Event : Cpu_OnNMIINT (module Events)
**
** Component : Cpu [MK10N512LQ100]
** Description :
** This event is called when the Non maskable interrupt had
** occurred. This event is automatically enabled when the <NMI
** interrrupt> property is set to 'Enabled'.
** Parameters : None
** Returns : Nothing
** ===================================================================
*/
void Cpu_OnNMIINT(void)
{
/* Write your code here ... */
}
/*
** ===================================================================
** Event : FLASH1_OnOperationComplete (module Events)
**
** Component : FLASH1 [FLASH_LDD]
** Description :
** Called at the end of the whole write / erase operation. if
** the event is enabled. See SetEventMask() and GetEventMask()
** methods.
** Parameters :
** NAME - DESCRIPTION
** * UserDataPtr - Pointer to the user or
** RTOS specific data. This pointer is passed
** as the parameter of Init method.
** Returns : Nothing
** ===================================================================
*/
void FLASH1_OnOperationComplete(LDD_TUserData *UserDataPtr)
{
/* Write your code here ... */
DataWrittenFlg = TRUE; /* Set DataWrittenFlg flag */
}
/* END Events */
运行程序,观察Memory的“0x10000000”位置被写成“Hello word”。如下图所示: