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

📄 7xx_flash.c

📁 STR71x (ARM7TDMI) flash programming examples
💻 C
📖 第 1 页 / 共 2 页
字号:
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
* File Name          : 7xx_flash.c
* Author             : MCD Application Team
* Date First Issued  : 10/01/2006 : V1.0
* Description        : This file provides all the Flash software functions.
********************************************************************************
* History:
* 10/01/2006 : 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.
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include"7xx_flash.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Flash operation enable bit mask */
#define FLASH_SPR_MASK     0x01000000
#define FLASH_SER_MASK     0x08000000
#define FLASH_DWPG_MASK    0x10000000
#define FLASH_WPG_MASK     0x20000000
#define FLASH_SUSP_MASK    0x40000000
#define FLASH_WMS_MASK     0x80000000

/* Flash interrupt mask */
#define FLASH_INTM_MASK    0x00200000
#define FLASH_INTP_MASK    0x00100000

#ifdef STR73x
  /* STR73x Flash lock and busy bits */
  #define FLASH_FLAG_LOCKBSY     0x12
#else
  /* STR71x/STR75x Flash lock and busy bits */
  #define FLASH_FLAG_LOCKBSY     0x16
#endif

/* Flash protection mask */
#ifdef STR75x
  /* STR75x Readout protection */
  #define FLASH_PROTECTION_MASK  0x00000001
#else /* STR71x & STR73x */
  /* STR71x & STR73x Debug protection */
  #define FLASH_PROTECTION_MASK  0x00000002
#endif

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/*******************************************************************************
* Function Name  : FLASH_DeInit
* Description    : Deinitializes the Flash module registers to their default 
*                  reset values.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void FLASH_DeInit(void)
{
  /* Reset the Flash control registers */
  FLASH->CR0 = 0x00000000;
  FLASH->CR1 = 0x00000000;

  /* Reset the Flash data registers */
  FLASH->DR0 = 0xFFFFFFFF;
  FLASH->DR1 = 0xFFFFFFFF;

  /* Reset the Flash address register */
  FLASH->AR = 0x00000000;

  /* Reset the Flash error register */
  FLASH->ER  = 0x00000000;
}

/*******************************************************************************
* Function Name  : FLASH_WriteWord
* Description    : Writes a Word to a Flash address destination.
* Input          : - FLASH_Address: the Address of the destination.
*                  - FLASH_Data: the data to be programmed.
* Output         : None
* Return         : None
*******************************************************************************/
void FLASH_WriteWord(u32 FLASH_Address, u32 FLASH_Data)
{
  /* Set the word programming bit */
  FLASH->CR0 |= FLASH_WPG_MASK;
  /* Load the destination address */
  FLASH->AR   = FLASH_Address;
  /* Load DATA to be programmed */
  FLASH->DR0  = FLASH_Data;
  /* Start the operation */
  FLASH->CR0 |= FLASH_WMS_MASK;
}

/*******************************************************************************
* Function Name  : FLASH_WriteDoubleWord
* Description    : Writes Double Word to the Flash.
* Input          : - FLASH_Address: the destination address.
*                  - FLASH_Data_1: the first word to be programmed.
*                  - FLASH_Data_2: the second word to be programmed.
* Output         : None
* Return         : None
*******************************************************************************/
void FLASH_WriteDoubleWord(u32 FLASH_Address, u32 FLASH_Data_1, u32 FLASH_Data_2)
{
  /* Set the double word programming bit */
  FLASH->CR0 |= FLASH_DWPG_MASK;
  /* Load the destination address */
  FLASH->AR   = FLASH_Address;
  /* Load FLASH_Data_1 in the DR0 register */
  FLASH->DR0  = FLASH_Data_1;
  /* Load FLASH_Data_2 in the DR1 register */
  FLASH->DR1  = FLASH_Data_2;
  /* Start the operation */
  FLASH->CR0 |= FLASH_WMS_MASK;
}

/*******************************************************************************
* Function Name  : FLASH_EraseSector
* Description    : Erases one or multiple sectors on a flash bank.
* Input          : FLASH_Sectors: the sectors to be erased.
*                  This parameter can be one of the following values:
*                      - FLASH_BANK0_SECTOR0: Bank 0 sector 0.
*                      - FLASH_BANK0_SECTOR1: Bank 0 sector 1.
*                      - FLASH_BANK0_SECTOR2: Bank 0 sector 2.
*                      - FLASH_BANK0_SECTOR3: Bank 0 sector 3.
*                      - FLASH_BANK0_SECTOR4: Bank 0 sector 4.
*                      - FLASH_BANK0_SECTOR5: Bank 0 sector 5.
*                      - FLASH_BANK0_SECTOR6: Bank 0 sector 6.
*                      - FLASH_BANK0_SECTOR7: Bank 0 sector 7.
*                      - FLASH_BANK0_MODULE:  Bank 0 module.
*                      - FLASH_BANK1_SECTOR0: Bank 1 sector 0. (N.A for STR73x)
*                      - FLASH_BANK1_SECTOR1: Bank 1 sector 1. (N.A for STR73x)
*                      - FLASH_BANK1_MODULE:  Bank 1 module.   (N.A for STR73x)
* Output         : None
* Return         : None
*******************************************************************************/
void FLASH_EraseSector(u32 FLASH_Sectors)
{
  /* Set the sector erase bit */
  FLASH->CR0 |= FLASH_SER_MASK;
  /* Select the sectors to be erased */
  FLASH->CR1 |= FLASH_Sectors;
  /* Start the operation */
  FLASH->CR0 |= FLASH_WMS_MASK;
}

/*******************************************************************************
* Function Name  : FLASH_SuspendOperation
* Description    : Suspends the current operation.
* Input          : None
* Output         : None
* Return         : Flash Control register content.
*******************************************************************************/
u32 FLASH_SuspendOperation(void)
{
  u32 FLASH_CONTROL_REGISTER = 0x0;
  /* Wait until register's unlock */
  while(FLASH->CR0 & 0x10);
  /* Save the control register content */
  FLASH_CONTROL_REGISTER = FLASH->CR0;
  /* Set the suspend Bit */
  FLASH->CR0 |= FLASH_SUSP_MASK;
  /* Wait until the flash controller acknowledges the suspend of the current
     operation */
  FLASH_WaitForLastOperation();
  /* Return the suspended operation */
  return FLASH_CONTROL_REGISTER;
}
/*******************************************************************************
* Function Name  : FLASH_ResumeOperation
* Description    : Resumes the suspended operation.
* Input          : FLASH_LastOperation: the Flash control register content
*                  returned by the FLASH_SuspendOperation function.
* Output         : None
* Return         : None
*******************************************************************************/
void FLASH_ResumeOperation(u32 FLASH_LastOperation)
{
  /* Clear the suspend Bit */
  FLASH->CR0 &= ~FLASH_SUSP_MASK;
  /* Resume the last operation */
  FLASH->CR0  = FLASH_LastOperation & ~FLASH_WMS_MASK;
  /* Start the operation */
  FLASH->CR0 |= FLASH_WMS_MASK;
}

/*******************************************************************************
* Function Name  : FLASH_ITConfig
* Description    : Enables or disables the end of write interrupt.
* Input          : FLASH_NewState: the new state of end of write interrupt.
*                  This parameter can be one of the following values: 
*                      - ENABLE:  Enable the end of write interrupt.
*                      - DISABLE: Disable the end of write interrupt.
* Output         : None
* Return         : None
*******************************************************************************/
void FLASH_ITConfig(FunctionalState FLASH_NewState)
{
  if(FLASH_NewState == ENABLE)
  { 
    /* Enable the end of write interrupt */
    FLASH->CR0  |= FLASH_INTM_MASK;
  }
  else
  {
    /* Disable the end of write interrupt */
    FLASH->CR0  &= ~FLASH_INTM_MASK;
  }
}

/*******************************************************************************
* Function Name  : FLASH_GetITStatus
* Description    : Checks whether the specified Flash interrupt pending bit
*                  is set or not
* Input          : None
* Output         : None
* Return         : The new state of the interrupt (SET or RESET)
*******************************************************************************/
FlagStatus FLASH_GetITStatus(void)
{
  if((FLASH->CR0 & FLASH_INTP_MASK)!= RESET)
  {
    return SET;
  }
  else
  {
    return RESET;
  }
}

/*******************************************************************************
* Function Name  : FLASH_ClearITPendingBit
* Description    : Clears the end of write interrupt pending flag.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void FLASH_ClearITPendingBit(void)
{
  FLASH->CR0 &= ~FLASH_INTP_MASK;
}

/*******************************************************************************
* Function Name  : FLASH_GetFlagStatus
* Description    : Checks the Flash flags.
* Input          : None
* Output         : None
* Return         : The Flash flag errors. 
*                  The description of the Flash flag errors bits :  
*                      - Bit 0: Write error flag.
*                      - Bit 1: Erase error flag.
*                      - Bit 2: Program error flag.
*                      - Bit 3: 1 over 0 error flag.
*                      - Bit 6: Sequence error flag.
*                      - Bit 7: Resume error flag.
*                      - Bit 8: Write protection error flag.
*******************************************************************************/
u16 FLASH_GetFlagStatus(void)
{
  return FLASH->ER;
}

/*******************************************************************************

⌨️ 快捷键说明

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