📄 program.c
字号:
/******************** (C) COPYRIGHT 2004 STMicroelectronics ********************
* File Name : program.c
* Author : MCD Application Team
* Date First Issued : 10/25/2004
* Description : This file provides a software which allows to program
* the internal FLASH
********************************************************************************
* History:
* 10/25/2004 : Created
* 11/24/2004 : IAP Version 1.0
********************************************************************************
THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH
CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A
RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH
SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN
IN CONNECTION WITH THEIR PRODUCTS.
********************************************************************************/
#include "common.h"
extern const u32 FLASH_Blk_Off[];
/*******************************************************************************
* Function Name : ProgramInternalFlash
* Description : Program internal Flash
* Input 1 : Address of internal Flash
* Input 2 : Size of data to be programmed in byte
* Input 3 : A pointer to the data
* Return : 0 -- Success
* : -1 -- Block Erase Failed
* : -2 -- Flash Program Failed
* : -3 -- Data Verification Failed
*******************************************************************************/
static s32 ProgramInternalFlash (u32 FlashAddr, u32 SizeInByte, u32 *pBuf)
{
u32 LastOff,FirstOff, FirstBlk, LastBlk, CurrBlk, *pFlashPtr, *pRamPtr, ByteCounter;
/* calculate the start offset into internal flash */
FirstOff = FlashAddr - (u32)INT_FLASH_BASE;
/* check the data size */
if ((SizeInByte % 4) != 0)
{
SizeInByte += (4-(SizeInByte % 4));
}
/* calculate the end offset into internal flash */
LastOff = FirstOff + SizeInByte-1;
/* calculate the sector number where the program starts */
for (FirstBlk=0; FirstBlk<INT_FLASH_SECNUM-1; FirstBlk++)
{
if (FirstOff < FLASH_Blk_Off[FirstBlk+1])
{
break;
}
}
/* calculate the sector number where the program ends */
for (LastBlk=FirstBlk; LastBlk<INT_FLASH_SECNUM-1; LastBlk++)
{
if (LastOff < FLASH_Blk_Off[LastBlk+1])
{
break;
}
}
/* Disable the write protection for the two banks */
FLASH_WritePrConfig(FLASH_B0|FLASH_B1,DISABLE);
/* Erase sectors */
SerialPutString("Erasing Sectors...");
for (CurrBlk=FirstBlk; CurrBlk<=LastBlk; CurrBlk++)
{
FLASH_SectorErase(IntFlashSectorMask(CurrBlk));
}
SerialPutString("OK\r\n");
/* Program data */
if (IntFlashProgram(FirstOff, SizeInByte, pBuf) != 0)
{
return -2;
}
/* Verify data */
SerialPutString("Verifying...");
pFlashPtr = (u32 *)FlashAddr;
pRamPtr = (u32 *)pBuf;
for (ByteCounter=0; ByteCounter<SizeInByte; ByteCounter=ByteCounter+4)
{
if (*pFlashPtr++ != *pRamPtr++)
{
return -3;
}
}
SerialPutString("OK\r\n");
return 0;
}
/*******************************************************************************
* Function Name : ProgramIntFlash
* Description : Program internal Flash and display the status of the operation
* Input : None
* Return : None
*******************************************************************************/
void ProgramIntFlash(void)
{
s32 destflashaddr=0x40002000, srcramaddr=0x20002000, errorno = 0;
if (gimagesize == 0)
{
errorno = 2;
goto out;
}
//Now programming
if (errorno == 0)
{
errorno = ProgramInternalFlash(destflashaddr, gimagesize, (u32*)srcramaddr);
}
out:
gimagesize = 0;//reset gimagesize
//Message output
switch (errorno)
{
case 0:
SerialPutString("Successfully completed!\r\n");
break;
case 1:
SerialPutString("Invalid Command Arguments!\r\n");
break;
case 2:
SerialPutString("No File to be Programmed!\r\n");
break;
case -1:
SerialPutString("Erase Failed!\r\n");
break;
case -2:
SerialPutString("Flash Program Failed!\r\n");
break;
case -3:
SerialPutString("Data Verification Failed!\r\n");
break;
default:
SerialPutString("Unkown failure!\r\n");
break;
}
}
/*******************(C)COPYRIGHT 2004 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -