📄 73x_flash.c
字号:
/******************** (C) COPYRIGHT 2005 STMicroelectronics ********************
* File Name : 73x_flash.c
* Author : MCD Application Team
* Date First Issued : 09/27/2005 : V1.0
* Description : This file provides all the Flash software functions.
**********************************************************************************
* History:
* 09/27/2005 : V1.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.
*********************************************************************************/
/* Standard include ----------------------------------------------------------*/
#include "73x_flash.h"
/* Include of other module interface headers ---------------------------------*/
/* Local includes ------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void FLASH_WaitForLastTask(void);
/* Interface functions -------------------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : FLASH_DeInit
* Description : Deinitializes Flash module registers to their default reset
* values.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void FLASH_DeInit(void)
{
/* reset Flash control registers */
FLASHR->CR0 = 0x00000000;
FLASHR->CR1 = 0x00000000;
/* reset Flash data registers */
FLASHR->DR0 = 0xFFFFFFFF;
FLASHR->DR1 = 0xFFFFFFFF;
/* reset Flash address register */
FLASHR->AR = 0x00000000;
/* reset Flash error register */
FLASHR->ER = 0x00000000;
}
/*******************************************************************************
* Function Name : FLASH_ITConfig
* Description : Enables or disables the end of write interrupt(INTM).
* Input : NewState: new state of end of write interrupt.
* This parameter can be: ENABLE or DISABLE.
* Output : None
* Return : None
*******************************************************************************/
void FLASH_ITConfig(FunctionalState NewState)
{
if(NewState == ENABLE)
{ /* enable INTM interrupt */
FLASHR->CR0 |= FLASH_IT_INTM;
}
else
{ /* disable INTM interrupt */
FLASHR->CR0 &= ~FLASH_IT_INTM;
}
}
/*******************************************************************************
* Function Name : FLASH_WordWrite
* Description : Writes a word at the specified address in the Flash.
* Input : - DestAddr: address of the Flash to be programmed.
* - Data: word to be written.
* Output : None
* Return : None
*******************************************************************************/
void FLASH_WordWrite(u32 DestAddr, u32 Data)
{
/* wait for the end of last task */
FLASH_WaitForLastTask();
/* set the WPG bit in CR0 register */
FLASHR->CR0 |= FLASH_WPG;
/* load the destination address in AR register */
FLASHR->AR = DestAddr;
/* load the data to be programmed in DR0 register */
FLASHR->DR0 = Data;
/* set the start bit WMS in CR0 register to start Write operation */
FLASHR->CR0 |= FLASH_WMS;
}
/*******************************************************************************
* Function Name : FLASH_DWordWrite
* Description : Writes a double word at the specified address in the Flash.
* Input : - DestAddr: address of the Flash to be programmed, should be
* aligned on a double word boundary.
* - Data0: first word to be written.
* - Data1: second word to be written.
* Output : None
* Return : None
*******************************************************************************/
void FLASH_DWordWrite(u32 DestAddr, u32 Data0, u32 Data1)
{
/* wait for the end of last task */
FLASH_WaitForLastTask();
/* set the DWPG bit in CR0 register */
FLASHR->CR0 |= FLASH_DWPG;
/* load the destination address in AR register */
FLASHR->AR = DestAddr;
/* load the first word to be programmed in DR0 register */
FLASHR->DR0 = Data0;
/* load the second word to be programmed in DR1 register */
FLASHR->DR1 = Data1;
/* set the start bit WMS in CR0 register to start Write operation */
FLASHR->CR0 |= FLASH_WMS;
}
/*******************************************************************************
* Function Name : FLASH_BlockWrite
* Description : Writes a block of data from the RAM to the Flash.
* Input : - SourceAddr: specifies the RAM address of the block of data
* to be programmed in the Flash.
* - DestAddr: specifies the address in the Flash where the
* block of data will be programmed.
* - NbrWordToWrite: specifies the number of data to be
* programmed expressed in terms of words.
* Output : None
* Return : None
*******************************************************************************/
void FLASH_BlockWrite(u32 SourceAddr, u32 DestAddr, u32 NbrWordToWrite)
{
u32 NbrWord = 0, NbrDWord = 0;
u32 TmpData0 = 0, TmpData1 = 0;
/* get the number of word that can be programmed using Double Word Program operation */
NbrDWord = NbrWordToWrite >> 1;
/* get the number of word that can be programmed using Word Program operation */
NbrWord = NbrWordToWrite & 0x00000001;
if((DestAddr % 8) == 0) /* Flash destination address is Double Word aligned */
{
if(NbrWord == 0) /* 'NbrWordToWrite' is an even number */
{ /* all data will be programmed using Double Word Program operation */
while(NbrDWord--)
{
TmpData0 = *(u32 *)SourceAddr; /* get the first word to be programmed */
SourceAddr += 4; /* increment the RAM source address by 4 */
TmpData1 = *(u32 *)SourceAddr; /* get the second word to be programmed */
SourceAddr += 4; /* increment the RAM source address by 4 */
FLASH_DWordWrite(DestAddr, TmpData0, TmpData1);
DestAddr += 8; /* increment the Flash destination address by 8 */
}
}
else /* 'NbrWordToWrite' is an odd number */
{ /* 'NbrWordToWrite-1' words will be programmed using Double Word Program operation */
while(NbrDWord--)
{
TmpData0 = *(u32 *)SourceAddr; /* get the first word to be programmed */
SourceAddr += 4; /* increment the RAM source address by 4 */
TmpData1 = *(u32 *)SourceAddr; /* get the second word to be programmed */
SourceAddr += 4; /* increment the RAM source address by 4 */
FLASH_DWordWrite(DestAddr, TmpData0, TmpData1);
DestAddr += 8; /* increment the Flash destination address by 8 */
}
/* the last word is programmed using Word Program operation */
TmpData0 = *(u32 *)SourceAddr;
FLASH_WordWrite(DestAddr, TmpData0);
}
}
else /* Flash destination address is Word aligned */
{
if(NbrWord == 0) /* 'NbrWordToWrite' is an even number */
{ /* the first word is programmed using Word Program operation */
TmpData0 = *(u32 *)SourceAddr; /* get the first word to be programmed */
SourceAddr += 4; /* increment the RAM source address by 4 */
FLASH_WordWrite(DestAddr, TmpData0);
DestAddr += 4; /* increment the Flash destination address by 4 */
NbrDWord -= 1; /* decrease 'NbrDWord' by 1 */
/* 'NbrWordToWrite-2' words will be programmed using Double Word Program operation */
while(NbrDWord--)
{
TmpData0 = *(u32 *)SourceAddr; /* get the first word to be programmed */
SourceAddr += 4; /* increment the RAM source address by 4 */
TmpData1 = *(u32 *)SourceAddr; /* get the second word to be programmed */
SourceAddr += 4; /* increment the RAM source address by 4 */
FLASH_DWordWrite(DestAddr, TmpData0, TmpData1);
DestAddr += 8; /* increment the Flash destination address by 8 */
}
/* the last word is programmed using Word Program operation */
TmpData0 = *(u32 *)SourceAddr; /* get the first word to be programmed */
FLASH_WordWrite(DestAddr, TmpData0);
}
else /* 'NbrWordToWrite' is an odd number */
{
/* the first word is programmed using Word Program operation */
TmpData0 = *(u32 *)SourceAddr; /* get the first word to be programmed */
SourceAddr += 4; /* increment the RAM source address by 4 */
FLASH_WordWrite(DestAddr, TmpData0);
DestAddr += 4; /* increment the Flash destination address by 4 */
/* the remaining words will be programmed using Double Word Program operation */
while(NbrDWord--)
{
TmpData0 = *(u32 *)SourceAddr; /* get the first word to be programmed */
SourceAddr += 4; /* increment the RAM source address by 4 */
TmpData1 = *(u32 *)SourceAddr; /* get the second word to be programmed */
SourceAddr += 4; /* increment the RAM source address by 4 */
FLASH_DWordWrite(DestAddr, TmpData0, TmpData1);
DestAddr += 8; /* increment the Flash destination address by 8 */
}
}
}
}
/*******************************************************************************
* Function Name : FLASH_WordRead
* Description : Reads a word from the specified address in the Flash.
* Input : SourceAddr: address of the Flash to be read.
* Output : None
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -