/** ****************************************************************************** * @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 * *

© COPYRIGHT(c) 2016 STMicroelectronics

* * 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" #include "hwsetup.h" /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ volatile DL_FLASHCTL_COMMAND_STATUS gCmdStatus; volatile uint8_t gErrorType=0; uint32_t adderr=0; // inline void BSL_CI_disableCache(void); // inline void BSL_CI_enableCache(void); /** * @brief Unlocks Flash for write access * @param None * @retval None */ /** * @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 NbrOfSectors = 0; // flash_unlock(); __disable_irq(); for(NbrOfSectors = 0; start + NbrOfSectors * USER_FLASH_SECTOR_SIZE < (USER_FLASH_BANK1_END_ADDRESS + 1);NbrOfSectors++) { DL_FlashCTL_unprotectSector( FLASHCTL, start, DL_FLASHCTL_REGION_SELECT_MAIN); // flash_sector_erase(start + NbrOfSectors * USER_FLASH_SECTOR_SIZE); gCmdStatus= DL_FlashCTL_eraseMemoryFromRAM( FLASHCTL, (start + NbrOfSectors * USER_FLASH_SECTOR_SIZE), DL_FLASHCTL_COMMAND_SIZE_SECTOR); adderr=(start + NbrOfSectors * USER_FLASH_SECTOR_SIZE); if (gCmdStatus != DL_FLASHCTL_COMMAND_STATUS_PASSED) { /* If command did not pass, set error flag. */ gErrorType = 15; } } // flash_lock(); __enable_irq(); 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 *p_source_test; uint32_t gDataArray64[] = {0, 0}; uint32_t gDataArraytest[] = {0, 0}; uint32_t FLASH_If_Write(uint32_t destination, uint32_t *p_source, uint32_t length) { uint32_t i = 0; __disable_irq(); // flash_unlock(); // DL_FlashCTL_unprotectSector( // FLASHCTL, destination, DL_FLASHCTL_REGION_SELECT_MAIN); p_source_test=p_source; for (i = 0; (i < length) && (destination <= (USER_FLASH_BANK1_END_ADDRESS-8)); i++) { /* Device voltage range supposed to be [2.7V to 3.6V], the operation will be done by word */ gDataArray64[0]= *(uint32_t*)(p_source+2*i) ; gDataArray64[1]= *(uint32_t*)(p_source+2*i+1) ; DL_FlashCTL_unprotectSector( FLASHCTL, destination, DL_FLASHCTL_REGION_SELECT_MAIN); gCmdStatus = DL_FlashCTL_programMemoryFromRAM64WithECCGenerated( FLASHCTL, destination , &gDataArray64[0]); // if (gCmdStatus == DL_FLASHCTL_COMMAND_STATUS_FAILED) if (gCmdStatus != DL_FLASHCTL_COMMAND_STATUS_PASSED) { /* If command did not pass, set error flag. */ gErrorType = 12; } if(gErrorType !=11 ) //TODO // DL_FLASHCTL_COMMAND_STATUS_PASSED = DL_FlashCTL_programMemoryFromRAM16WithECCGenerated( // FLASHCTL, (MAIN_BASE_ADDRESS + 8), &gData16); { /* Check the written value */ // gDataArraytest[0]=*(uint32_t*)destination; // gDataArraytest[1]=*(uint32_t*)(destination+1); // if ((*(uint32_t*)destination != *(uint32_t*)(p_source+i)) // &&(*(uint32_t*)(destination+4) != *(uint32_t*)(p_source+i+1))) // { // /* Flash content doesn't match SRAM content */ // return(FLASHIF_WRITINGCTRL_ERROR); // } /* Increment FLASH destination address */ destination += 8; } else { gDataArraytest[0]=11; gDataArraytest[1]=22; /* Error occurred while writing data in Flash memory */ return (FLASHIF_WRITING_ERROR); } } __enable_irq(); // flash_lock(); return (FLASHIF_OK); } //addr_end =1024整数倍-1 uint8_t CMD_USER_API_Flash_Range_Erase(uint32_t addrStart,uint32_t addr_end) { uint8_t ret = 0; uint8_t erase_status = (uint8_t) 1; uint32_t main_flash_start_address = addrStart; uint32_t main_flash_end_address =addr_end;; uint32_t data_pointer; data_pointer = addrStart; /* Check if BSL is unlocked */ /* If the address range is valid, then erase the memory range */ if ((addrStart >= main_flash_start_address) && (addr_end <= main_flash_end_address) && (addrStart <= addr_end)) { // BSL_CI_disableCache(); while (data_pointer <= addr_end) { DL_FlashCTL_unprotectSector( FLASHCTL, data_pointer, DL_FLASHCTL_REGION_SELECT_MAIN); DL_FlashCTL_eraseMemoryFromRAM( FLASHCTL, data_pointer, DL_FLASHCTL_COMMAND_SIZE_SECTOR); erase_status = erase_status & (uint8_t) DL_FlashCTL_waitForCmdDone(FLASHCTL); data_pointer = data_pointer + MAIN_SECTOR_SIZE; } DL_FlashCTL_unprotectSector( FLASHCTL, addr_end, DL_FLASHCTL_REGION_SELECT_MAIN); DL_FlashCTL_eraseMemoryFromRAM( FLASHCTL, addr_end, DL_FLASHCTL_COMMAND_SIZE_SECTOR); erase_status = erase_status & (uint8_t) DL_FlashCTL_waitForCmdDone(FLASHCTL); // BSL_CI_enableCache(); /* Check if all flash sector erase got passed */ if (erase_status == (uint8_t) 0) { ret = 2; } } else { ret = 3; } return ret; } //inline void BSL_CI_disableCache(void) //{ // DL_CORE_configInstruction(CPUSS_CTL_ICACHE_DISABLE, // CPUSS_CTL_PREFETCH_DISABLE, CPUSS_CTL_LITEN_DISABLE); //} // //inline void BSL_CI_enableCache(void) //{ // DL_CORE_configInstruction(CPUSS_CTL_ICACHE_ENABLE, // CPUSS_CTL_PREFETCH_ENABLE, CPUSS_CTL_LITEN_ENABLE); //} /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/