⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flash.c

📁 STR71x Flash IAP Example This project is an RV-MDK adaptation of the ST Application Note: "STR71
💻 C
字号:
/******************** (C) COPYRIGHT 2006 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:
*  02/01/2006 : IAP Version 2.0
*  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"

/*******************************************************************************
* Function Name  : FLASH_Init
* Description    : Initialise the Flash registers to their default values.
* 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 in the Flash.
* Input 1        : Address of the Destination.
* Input 2        : Word to program at Address.
* Return         : None.
*******************************************************************************/
void FLASH_WordWrite(u32 XtargetAdd, u32 Xdata)
{
  /* Wait until another operation going on is completed */
  FLASH_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;
  /* Wait until the write operation is completed */
  FLASH_WaitForLastTask();
}

/*******************************************************************************
* Function Name  : FLASH_SectorErase
* Description    : Erases a Flash sector.
* Input 1        : Sectors to be Erased.
* Return         : None.
*******************************************************************************/
void FLASH_SectorErase(u32 Xsectors)
{
  /* Wait until another operation going on is completed */  
  FLASH_WaitForLastTask();
  /* Set the Sector Erase flag 'SER' in the CRO reg */
  FLASHR->CR0 |= FLASH_SER_Mask;
  /* Select in the CR1 register the Sectors to be erased  */
  FLASHR->CR1 |= Xsectors;
  /* Set the Write Mode Start bit 'WMS' in the CR0 Reg to Start Erase Operation */
  FLASHR->CR0 |= FLASH_WMS_Mask;
  /* Wait until the erase operation is completed */
  FLASH_WaitForLastTask();
}

/*******************************************************************************
* Function Name  : FLASH_WritePrConfig
* Description    : Enable Write protection or Disable temporarily Write 
*                  protection of a flash sector.
* Input 1        : Flash Sector.
* Input 2        : Enable or disable Flash sector Write Protection.
* Return         : None.
*******************************************************************************/
void FLASH_WritePrConfig(u32 Xsectors, FunctionalState NewState)
{
  /* Wait until another operation going on is completed */  
  FLASH_WaitForLastTask();  
  /* Enable Xsectors write protection */
  if (NewState == ENABLE)
  {
    /* 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  = ~Xsectors;
    /* Set the WMS bit to Start the Sequence */
    FLASHR->CR0 |= FLASH_WMS_Mask;
  }
  /* Unprotect temporarily Flash sector */ 
  else
  {
    /* 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  = FLASHPR->NVWPAR|Xsectors;
    /* Set the WMS bit to Start the Sequence */
    FLASHR->CR0 |= FLASH_WMS_Mask;
  }
}

/*******************************************************************************
* Function Name  : FLASH_WaitForLastTask
* Description    : Waits for the end of last task on a Flash Bank.
* Input 1        : None.
* Return         : None.
*******************************************************************************/
void FLASH_WaitForLastTask(void)
{
  while (FLASHR->CR0&0x16);
}

/*******************(C)COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -