flash_if.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /** @addtogroup STM32F1xx_IAP
  38. * @{
  39. */
  40. /* Includes ------------------------------------------------------------------*/
  41. #include "flash_if.h"
  42. /* Private typedef -----------------------------------------------------------*/
  43. /* Private define ------------------------------------------------------------*/
  44. /* Private macro -------------------------------------------------------------*/
  45. /* Private variables ---------------------------------------------------------*/
  46. /* Private function prototypes -----------------------------------------------*/
  47. /* Private functions ---------------------------------------------------------*/
  48. /**
  49. * @brief Unlocks Flash for write access
  50. * @param None
  51. * @retval None
  52. */
  53. void FLASH_If_Init(void)
  54. {
  55. /* Unlock the Program memory */
  56. HAL_FLASH_Unlock();
  57. /* Clear all FLASH flags */
  58. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
  59. /* Unlock the Program memory */
  60. HAL_FLASH_Lock();
  61. }
  62. /**
  63. * @brief This function does an erase of all user flash area
  64. * @param start: start of user flash area
  65. * @retval FLASHIF_OK : user flash area successfully erased
  66. * FLASHIF_ERASEKO : error occurred
  67. */
  68. uint32_t FLASH_If_Erase(uint32_t start)
  69. {
  70. uint32_t NbrOfPages = 0;
  71. uint32_t PageError = 0;
  72. FLASH_EraseInitTypeDef pEraseInit;
  73. HAL_StatusTypeDef status = HAL_OK;
  74. /* Unlock the Flash to enable the flash control register access *************/
  75. HAL_FLASH_Unlock();
  76. /* Get the sector where start the user flash area */
  77. if (start < USER_FLASH_BANK1_END_ADDRESS)
  78. {
  79. NbrOfPages = ((USER_FLASH_BANK1_END_ADDRESS + 1) - start)/FLASH_PAGE_SIZE;
  80. pEraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
  81. pEraseInit.PageAddress = start;
  82. // pEraseInit.Banks = FLASH_BANK_1;
  83. pEraseInit.NbPages = NbrOfPages;
  84. status = HAL_FLASHEx_Erase(&pEraseInit, &PageError);
  85. }
  86. /* Lock the Flash to disable the flash control register access (recommended
  87. to protect the FLASH memory against possible unwanted operation) *********/
  88. HAL_FLASH_Lock();
  89. if (status != HAL_OK)
  90. {
  91. /* Error occurred while page erase */
  92. return FLASHIF_ERASEKO;
  93. }
  94. return FLASHIF_OK;
  95. }
  96. /* Public functions ---------------------------------------------------------*/
  97. /**
  98. * @brief This function writes a data buffer in flash (data are 32-bit aligned).
  99. * @note After writing data buffer, the flash content is checked.
  100. * @param destination: start address for target location
  101. * @param p_source: pointer on buffer with data to write
  102. * @param length: length of data buffer (unit is 32-bit word)
  103. * @retval uint32_t 0: Data successfully written to Flash memory
  104. * 1: Error occurred while writing data in Flash memory
  105. * 2: Written Data in flash memory is different from expected one
  106. */
  107. uint32_t FLASH_If_Write(uint32_t destination, uint32_t *p_source, uint32_t length)
  108. {
  109. uint32_t i = 0;
  110. /* Unlock the Flash to enable the flash control register access *************/
  111. HAL_FLASH_Unlock();
  112. for (i = 0; (i < length) && (destination <= (USER_FLASH_BANK1_END_ADDRESS-4)); i++)
  113. {
  114. /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
  115. be done by word */
  116. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, destination, *(uint32_t*)(p_source+i)) == HAL_OK)
  117. {
  118. /* Check the written value */
  119. if (*(uint32_t*)destination != *(uint32_t*)(p_source+i))
  120. {
  121. /* Flash content doesn't match SRAM content */
  122. return(FLASHIF_WRITINGCTRL_ERROR);
  123. }
  124. /* Increment FLASH destination address */
  125. destination += 4;
  126. }
  127. else
  128. {
  129. /* Error occurred while writing data in Flash memory */
  130. return (FLASHIF_WRITING_ERROR);
  131. }
  132. }
  133. /* Lock the Flash to disable the flash control register access (recommended
  134. to protect the FLASH memory against possible unwanted operation) *********/
  135. HAL_FLASH_Lock();
  136. return (FLASHIF_OK);
  137. }
  138. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/