📄 flash.c
字号:
/******************** (C) COPYRIGHT 2003 STMicroelectronics ********************
* File Name : flash.c
* Author : MCD Application Team
* Date First Issued : 28/07/2003
* Description : This file provides all the Flash software functions
********************************************************************************
* History:
** 28/07/2003 : Created
* 13/07/2004 : Rename FLASH_EraseSector,FLASH_EraseBank,FLASH_EraseModule functions
* to FLASH_SectorErase,FLASH_BankErase,FLASH_ModuleErase
*******************************************************************************/
#include "flash.h"
/*******************************************************************************
* Function Name : FLASH_Init
* Description : Initialise the Flash
* Input : None
* Return : None
*******************************************************************************/
void FLASH_Init(void)
{
// Reset Flash Control Registers
FLASHR->CR0 = 0x00000000;
FLASHR->CR1 = 0x00000000;
// Reset Flash Data Registers
FLASHR->DR0 = 0xFFFFFFFF;
FLASHR->DR1 = 0xFFFFFFFF;
// Reset Flash Error Register
FLASHR->ER = 0x00000000;
}
/*******************************************************************************
* 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(FLASH_BANK0);
WaitForLastTask(FLASH_BANK1);
// 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_DWordWrite
* Description : Writes Double Word to the Flash
* Input 1 : Address of the Destination
* Input 2 : Word 1 To program
* Input 3 : Word 2 To program
* Return : None
*******************************************************************************/
void FLASH_DWordWrite(u32 XtargetAdd, u32 Xdata0, u32 Xdata1)
{
WaitForLastTask(FLASH_BANK0);
WaitForLastTask(FLASH_BANK1);
// set the Double Word Programming bit 'DWPG' in the CR0 Reg
FLASHR->CR0 |= FLASH_DWPG_Mask;
// Load the destination address in AR
FLASHR->AR = XtargetAdd;
// Load DATA0 in DR0 Reg
FLASHR->DR0 = Xdata0;
// Load DATA1 in DR1 Reg
FLASHR->DR1 = Xdata1;
// Set the Write Mode Start bit 'WMS' in the CR0 Reg to Start Write Operation
FLASHR->CR0 |= FLASH_WMS_Mask;
}
/*******************************************************************************
* Function Name : FLASH_WriteBlock
* Description : Writes Data To the Flash
* Input 1 : Address of the Data source
* Input 2 : Address of the Destination
* Input 3 : Nbr of words to be stored
* Return : None
*******************************************************************************/
void FLASH_BlockWrite(u32 XsourceAdd, u32 XtargetAdd, u32 XdataLength)
{
u32 TmpAddrS, TmpAddrD;
u32 NbrW, NbrDW;
u32 TmpData0, TmpData1;
// Get The Source Address
TmpAddrS = XsourceAdd;
// Get The Destination Address
TmpAddrD = XtargetAdd;
// Get Number of Double Words
NbrDW = XdataLength >> 1;
// Get Number of single Words
NbrW = XdataLength & 0x00000001;
// Programming Double Words
while (NbrDW > 0)
{
// Get The First 32 bits
TmpData0 = *(u32 *)TmpAddrS;
// Increment The Source Address
TmpAddrS += 4;
// Get The Second 32 bits
TmpData1 = *(u32 *)TmpAddrS;
// Increment The Source Address
TmpAddrS += 4;
// Use The FLASH_DWordWrite function to program the 64 bits
FLASH_DWordWrite(TmpAddrD, TmpData0, TmpData1);
// Decrease number of Double word
NbrDW--;
// Increment The destination Address
TmpAddrD += 8;
// Waits for the data to be programmed
FLASH_Delay();
}
if (NbrW > 0)
{
// Get The First 32 bits
TmpData1 = *(u32 *)TmpAddrS;
// Use The FLASH_WordWrite function to program the 32 bits
FLASH_WordWrite(TmpAddrD, TmpData1);
// Waits for the data to be programmed
FLASH_Delay();
}
}
/*******************************************************************************
* Function Name : FLASH_EraseSector
* Description : Erases a Flash sector
* Input 1 : Sectors to be Erased
* Return : None
*******************************************************************************/
void FLASH_SectorErase(u32 Xsectors)
{
WaitForLastTask(FLASH_BANK0);
WaitForLastTask(FLASH_BANK1);
// 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 : FLASH_EraseModule
* Description : Erases a flash module
* Input : None
* Return : None
*******************************************************************************/
void FLASH_ModuleErase(void)
{
WaitForLastTask(FLASH_BANK0);
FLASH_BankErase(FLASH_BANK0);
WaitForLastTask(FLASH_BANK1);
FLASH_BankErase(FLASH_BANK1);
// waits for the end of the write operation on both banks
WaitForLastTask(FLASH_BANK0);
WaitForLastTask(FLASH_BANK1);
}
/*******************************************************************************
* Function Name : FLASH_Delay
* Description : Add the delay required for the Flash Write & Erase operation
* Input 1 : None
* Return : None
*******************************************************************************/
void FLASH_Delay(void)
{
u8 Xdelay;
for(Xdelay = 0; Xdelay < 0xFE; Xdelay ++);
}
/*******************************************************************************
* Function Name : FLASH_Suspend
* Description : Suspends the current program or erase operation
* Input 1 : None
* Return : None
*******************************************************************************/
void FLASH_Suspend(void)
{
// Set The suspend Bit 'SUSP' in the CR0 register
FLASHR->CR0 |= FLASH_SUSP_Mask;
}
/*******************************************************************************
* Function Name : FLASH_Resume
* Description : Resume a Suspended program or erase operation
* Input 1 : None
* Return : None
*******************************************************************************/
void FLASH_Resume(void)
{
// Reset The suspend Bit 'SUSP' in the FRC0 register
FLASHR->CR0 &= ~FLASH_SUSP_Mask;
// Set write mode bit
FLASHR->CR0 |= FLASH_WMS_Mask;
}
/*******************************************************************************
* Function Name : FLASH_ReadWord
* Description : Read a single word of the flash
* Input 1 : Source Address
* Return : Word
*******************************************************************************/
u32 FLASH_WordRead(u32 SourceAdd)
{
WaitForLastTask(FLASH_BANK0);
WaitForLastTask(FLASH_BANK1);
// Waits for the flash to be unlocked
while((FLASH_FlagStatus(FLASH_LOCK))== SET);
// Reads the data from the specified Address
return *(u32 *)(SourceAdd + 0x40000000);
}
/*******************************************************************************
* Function Name : FLASH_ReadBlock -> Block Read
* Description : Block Read from the flash
* Input 1 : Destination Address where the Data will be Stored
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -