flash_if.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. ******************************************************************************
  3. * @file IAP_Main/Src/flash_if.c
  4. * @author MCD Application Team
  5. * @version V1.6.0
  6. * @date 12-May-2017
  7. * @brief This file provides all the memory related operation functions.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  12. *
  13. * Redistribution and use in source and binary forms, with or without modification,
  14. * are permitted provided that the following conditions are met:
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ******************************************************************************
  36. */
  37. /* Includes ------------------------------------------------------------------*/
  38. #include "flash_if.h"
  39. /* Private typedef -----------------------------------------------------------*/
  40. /* Private define ------------------------------------------------------------*/
  41. /* Private macro -------------------------------------------------------------*/
  42. /* Private variables ---------------------------------------------------------*/
  43. /* Private function prototypes -----------------------------------------------*/
  44. /* Private functions ---------------------------------------------------------*/
  45. /**
  46. * @brief Unlocks Flash for write access
  47. * @param None
  48. * @retval None
  49. */
  50. void FLASH_If_Init(void)
  51. {
  52. /* Unlock the Program memory */
  53. fmc_unlock();
  54. /* clear all pending flags */
  55. fmc_flag_clear(FMC_FLAG_BANK0_PGERR | FMC_FLAG_BANK0_WPERR | FMC_FLAG_BANK0_END);
  56. /* Unlock the Program memory */
  57. fmc_lock();
  58. }
  59. /**
  60. * @brief This function does an erase of all user flash area
  61. * @param start: start of user flash area
  62. * @retval FLASHIF_OK : user flash area successfully erased
  63. * FLASHIF_ERASEKO : error occurred
  64. */
  65. uint32_t FLASH_If_Erase(uint32_t start)
  66. {
  67. uint32_t NbrOfPages = 0;
  68. fmc_state_enum fmcstatus;
  69. uint32_t wperror = 0;
  70. /* Unlock the Flash to enable the flash control register access *************/
  71. fmc_unlock();
  72. /* Get the sector where start the user flash area */
  73. for(NbrOfPages = 0; start + NbrOfPages * USER_FLASH_PAGE_SIZE < (USER_FLASH_BANK1_END_ADDRESS + 1);NbrOfPages++)
  74. {
  75. fmcstatus = fmc_page_erase(start + NbrOfPages * USER_FLASH_PAGE_SIZE);
  76. wperror += (fmcstatus == FMC_WPERR);
  77. fmc_flag_clear(FMC_FLAG_BANK0_PGERR | FMC_FLAG_BANK0_WPERR | FMC_FLAG_BANK0_END);
  78. }
  79. /* Lock the Flash to disable the flash control register access (recommended
  80. to protect the FLASH memory against possible unwanted operation) *********/
  81. fmc_lock();
  82. if (wperror != 0)
  83. {
  84. /* Error occurred while page erase */
  85. return FLASHIF_ERASEKO;
  86. }
  87. return FLASHIF_OK;
  88. }
  89. /* Public functions ---------------------------------------------------------*/
  90. /**
  91. * @brief This function writes a data buffer in flash (data are 32-bit aligned).
  92. * @note After writing data buffer, the flash content is checked.
  93. * @param destination: start address for target location
  94. * @param p_source: pointer on buffer with data to write
  95. * @param length: length of data buffer (unit is 32-bit word)
  96. * @retval uint32_t 0: Data successfully written to Flash memory
  97. * 1: Error occurred while writing data in Flash memory
  98. * 2: Written Data in flash memory is different from expected one
  99. */
  100. uint32_t FLASH_If_Write(uint32_t destination, uint32_t *p_source, uint32_t length)
  101. {
  102. uint32_t i = 0;
  103. /* Unlock the Flash to enable the flash control register access *************/
  104. fmc_unlock();
  105. for (i = 0; (i < length) && (destination <= (USER_FLASH_BANK1_END_ADDRESS-4)); i++)
  106. {
  107. /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
  108. be done by word */
  109. if (fmc_word_program(destination, *(uint32_t*)(p_source+i)) == FMC_READY)
  110. {
  111. /* Check the written value */
  112. if (*(uint32_t*)destination != *(uint32_t*)(p_source+i))
  113. {
  114. /* Flash content doesn't match SRAM content */
  115. return(FLASHIF_WRITINGCTRL_ERROR);
  116. }
  117. /* Increment FLASH destination address */
  118. destination += 4;
  119. }
  120. else
  121. {
  122. /* Error occurred while writing data in Flash memory */
  123. return (FLASHIF_WRITING_ERROR);
  124. }
  125. }
  126. /* Lock the Flash to disable the flash control register access (recommended
  127. to protect the FLASH memory against possible unwanted operation) *********/
  128. fmc_lock();
  129. return (FLASHIF_OK);
  130. }
  131. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/