📄 flash.c
字号:
/******************** (C) COPYRIGHT 2004 STMicroelectronics ********************
* File Name : flash.c
* Author : MCD Application Team
* Date First Issued : 07/28/2003
* Description : This file provides all the Flash software functions
********************************************************************************
* History:
* 07/28/2003 : 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 "flash.h"
//#include "util.h"
/* address offset of each sector in internal flash memory */
const u32 FLASH_Blk_Off[] =
{
0x000000, /* Start offset of sector 0 block 0 */
0x002000, /* Start offset of sector 1 block 0 */
0x004000, /* Start offset of sector 2 block 0 */
0x006000, /* Start offset of sector 3 block 0 */
0x008000, /* Start offset of sector 4 block 0 */
0x010000, /* Start offset of sector 5 block 0 */
0x020000, /* Start offset of sector 6 block 0 */
0x030000, /* Start offset of sector 7 block 0 */
0x0C0000, /* Start offset of sector 0 block 1 */
0x0C2000, /* Start offset of sector 1 block 1 */
};
/*******************************************************************************
* Function Name : FLASH_WordWrite
* Description : Writes a Word to the Flash
* Input 1 : Address of the Destination
* Input 2 : Word To program
* Return : None
*******************************************************************************/
void FLASH_WordWrite(u32 XtargetAdd, u32 Xdata)
{
WaitForLastTask();
// set the Word Programming bit 'WPG' in the CR0 Reg
FLASHR->CR0 |= FLASH_WPG_Mask;
// Load the destination address in AR
FLASHR->AR = XtargetAdd;
// Load DATA to be programmed in DR0
FLASHR->DR0 = Xdata;
// Set the Write Mode Start bit 'WMS' in the CR0 Reg to Start Write Operation
FLASHR->CR0 |= FLASH_WMS_Mask;
}
/*******************************************************************************
* Function Name : FLASH_EraseSector
* Description : Erases a Flash sector
* Input : Sectors to be Erased
* Return : None
*******************************************************************************/
void FLASH_SectorErase(u32 Xsectors)
{
WaitForLastTask();
// Set the Sector Erase flag 'SER' in the FCRO reg
FLASHR->CR0 |= FLASH_SER_Mask;
// Select the Sectors to be erased in the CR1 register
FLASHR->CR1 |= Xsectors;
// Set the Write Mode Start bit 'WMS' in the CR0 Reg to Start Erase Operation
FLASHR->CR0 |= FLASH_WMS_Mask;
}
/*******************************************************************************
* Function Name : Wait For Last Task
* Description : Waits for the end of last task on a Flash Bank
* Input : Bank number.
* Return : The value passed in parameter with the bit (Bitindex) reset
*******************************************************************************/
void WaitForLastTask(void)
{
while (FLASHR->CR0 & 0x16);
}
/*******************************************************************************
* Function Name : FLASH_Delay
* Description : Add the delay required for the Flash Write & Erase operation
* Input : None
* Return : None
*******************************************************************************/
void FLASH_Delay(void)
{
u8 Xdelay;
for(Xdelay = 0; Xdelay < 0xFE; Xdelay ++);
}
/*******************************************************************************
* Function Name : IntFlashProgram
* Description : Program the internal Flash
* Input 1 : Flash address offset
* Input 2 : Size in byte
* Input 3 : A pointer to data
* Return : Status of the program operation
: -1 : Failed
: 0 : Success
*******************************************************************************/
s32 IntFlashProgram (u32 AddrOff, u32 SizeInByte, u32 *pBuf)
{
s32 iRetVal = 0;
u32 i,oldpercent=0 , percent=0;
// char Number[10];
/* check the data location */
if ((AddrOff >= INT_FLASH_SIZE) || (SizeInByte > (INT_FLASH_SIZE - AddrOff)))
{
return -1;
}
/* program */
//MyPrintf("Programming ... 0%\r");
for (i=0; i<SizeInByte; i=i+4)
{
/* program the next word */
FLASH_WordWrite(AddrOff+0x40000000,*pBuf);
FLASH_Delay();
AddrOff=AddrOff+4;
*pBuf=*pBuf++;
//update percentage progress
percent = (i+4)*100 / (u32)SizeInByte;
if (percent > oldpercent)
{
oldpercent = percent;
//Int2Str(Number,percent);
//MyPrintf("Programming... ");
//MyPrintf(Number);
//MyPrintf("%\r");
}
}
//MyPrintf("\r\n");
return iRetVal;
}
/*******************************************************************************
* Function Name : IntFlashSectorMask
* Description : Calculate the sector mask
* Input : The sector number
* Return : The sector mask
*******************************************************************************/
u32 IntFlashSectorMask(u32 SectorNumber)
{
u32 ReturnValue = 0;
switch (SectorNumber)
{
case 0: ReturnValue = 0x01;
break;
case 1: ReturnValue = 0x02;
break;
case 2: ReturnValue = 0x04;
break;
case 3: ReturnValue = 0x08;
break;
case 4: ReturnValue = 0x10;
break;
case 5: ReturnValue = 0x20;
break;
case 6: ReturnValue = 0x40;
break;
case 7: ReturnValue = 0x80;
break;
case 8: ReturnValue = 0x10000;
break;
case 9: ReturnValue = 0x20000;
break;
}
return (ReturnValue);
}
/*******************************************************************************
* Function Name : FLASH_WritePrConfig
* Description : Configures The Write Protection Bits
* Input 1 : Flash Bank
* Input 2 : Enable Or disable Protection
* Return : None
*******************************************************************************/
void FLASH_WritePrConfig(u32 Xsectors, FunctionalState NewState)
{
u32 TmpProtection;
TmpProtection = FLASHPR->NVWPAR;
WaitForLastTask();
if (NewState == DISABLE) TmpProtection |= Xsectors;
else TmpProtection &= ~Xsectors;
// Set the Set protection Bit
FLASHR->CR0 |= FLASH_SPR_Mask;
// Set the Register Address
FLASHR->AR = 0x4010DFB0;
// Data To be Programmed to the Protection Register
FLASHR->DR0 = TmpProtection;
// Set the WMS bit to Start the Sequence
FLASHR->CR0 |= FLASH_WMS_Mask;
}
//=====================================================================================
//0x00:B0F0 / 0x01:B0F1.....0x10000:B1F0 / 0x20000:B1F1
//=====================================================================================
void Flash_Erase(u32 Sector) {
// Wait to FLASH registers access and last bank write or erase operation
//MyPrintf("\r\nFlash_Erase [%x] ", FLASHR->CR0);
while (FLASHR->CR0 & 0x16)
{
//MyPrintf("\r\n%x ", FLASHR->CR0);
} // Set the Sector Erase flag 'SER' in the FLASH_CRO register
FLASHR->CR0 |= 0x08000000;
// Select the Sector BOF1 to be erased in the FLASH_CR1 register
//FLASHR->CR1 |= 0x00000002;
FLASHR->CR1 |= Sector;
// Set the Write Mode Start bit 'WMS' in the FLASH_CR0 register to Start Erase Operation
FLASHR->CR0 |= 0x80000000;
while (FLASHR->CR0 & 0x16) {
//MyPrintf("*");
}
}
/*******************(C)COPYRIGHT 2004 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -