123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- /**
- ******************************************************************************
- * @file IAP_Main/Src/flash_if.c
- * @author MCD Application Team
- * @version V1.6.0
- * @date 12-May-2017
- * @brief This file provides all the memory related operation functions.
- ******************************************************************************
- * @attention
- *
- * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "flash_if.h"
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief Unlocks Flash for write access
- * @param None
- * @retval None
- */
- void FLASH_If_Init(void)
- {
- /* Unlock the Program memory */
- fmc_unlock();
- /* clear all pending flags */
- fmc_flag_clear(FMC_FLAG_BANK0_PGERR | FMC_FLAG_BANK0_WPERR | FMC_FLAG_BANK0_END);
-
- /* Unlock the Program memory */
- fmc_lock();
- }
- /**
- * @brief This function does an erase of all user flash area
- * @param start: start of user flash area
- * @retval FLASHIF_OK : user flash area successfully erased
- * FLASHIF_ERASEKO : error occurred
- */
- uint32_t FLASH_If_Erase(uint32_t start)
- {
- uint32_t NbrOfPages = 0;
- fmc_state_enum fmcstatus;
- uint32_t wperror = 0;
- /* Unlock the Flash to enable the flash control register access *************/
- fmc_unlock();
- /* Get the sector where start the user flash area */
- for(NbrOfPages = 0; start + NbrOfPages * USER_FLASH_PAGE_SIZE < (USER_FLASH_BANK1_END_ADDRESS + 1);NbrOfPages++)
- {
- fmcstatus = fmc_page_erase(start + NbrOfPages * USER_FLASH_PAGE_SIZE);
- wperror += (fmcstatus == FMC_WPERR);
- fmc_flag_clear(FMC_FLAG_BANK0_PGERR | FMC_FLAG_BANK0_WPERR | FMC_FLAG_BANK0_END);
- }
- /* Lock the Flash to disable the flash control register access (recommended
- to protect the FLASH memory against possible unwanted operation) *********/
- fmc_lock();
- if (wperror != 0)
- {
- /* Error occurred while page erase */
- return FLASHIF_ERASEKO;
- }
- return FLASHIF_OK;
- }
- /* Public functions ---------------------------------------------------------*/
- /**
- * @brief This function writes a data buffer in flash (data are 32-bit aligned).
- * @note After writing data buffer, the flash content is checked.
- * @param destination: start address for target location
- * @param p_source: pointer on buffer with data to write
- * @param length: length of data buffer (unit is 32-bit word)
- * @retval uint32_t 0: Data successfully written to Flash memory
- * 1: Error occurred while writing data in Flash memory
- * 2: Written Data in flash memory is different from expected one
- */
- uint32_t FLASH_If_Write(uint32_t destination, uint32_t *p_source, uint32_t length)
- {
- uint32_t i = 0;
- /* Unlock the Flash to enable the flash control register access *************/
- fmc_unlock();
- for (i = 0; (i < length) && (destination <= (USER_FLASH_BANK1_END_ADDRESS-4)); i++)
- {
- /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
- be done by word */
- if (fmc_word_program(destination, *(uint32_t*)(p_source+i)) == FMC_READY)
- {
- /* Check the written value */
- if (*(uint32_t*)destination != *(uint32_t*)(p_source+i))
- {
- /* Flash content doesn't match SRAM content */
- return(FLASHIF_WRITINGCTRL_ERROR);
- }
- /* Increment FLASH destination address */
- destination += 4;
- }
- else
- {
- /* Error occurred while writing data in Flash memory */
- return (FLASHIF_WRITING_ERROR);
- }
- }
- /* Lock the Flash to disable the flash control register access (recommended
- to protect the FLASH memory against possible unwanted operation) *********/
- fmc_lock();
- return (FLASHIF_OK);
- }
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|