flash_if.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. ******************************************************************************
  3. * @file IAP_Main/Src/flash_if.c
  4. * @author MCD Application Team
  5. * @version 1.0.0
  6. * @date 8-April-2015
  7. * @brief This file provides all the memory related operation functions.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT(c) 2015 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. NbrOfPages = (USER_FLASH_END_ADDRESS - start)/FLASH_PAGE_SIZE;
  78. pEraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
  79. pEraseInit.PageAddress = start;
  80. pEraseInit.Banks = FLASH_BANK_1;
  81. pEraseInit.NbPages = NbrOfPages;
  82. status = HAL_FLASHEx_Erase(&pEraseInit, &PageError);
  83. /* Lock the Flash to disable the flash control register access (recommended
  84. to protect the FLASH memory against possible unwanted operation) *********/
  85. HAL_FLASH_Lock();
  86. if (status != HAL_OK)
  87. {
  88. /* Error occurred while page erase */
  89. return FLASHIF_ERASEKO;
  90. }
  91. return FLASHIF_OK;
  92. }
  93. /* Public functions ---------------------------------------------------------*/
  94. /**
  95. * @brief This function writes a data buffer in flash (data are 32-bit aligned).
  96. * @note After writing data buffer, the flash content is checked.
  97. * @param destination: start address for target location
  98. * @param p_source: pointer on buffer with data to write
  99. * @param length: length of data buffer (unit is 32-bit word)
  100. * @retval uint32_t 0: Data successfully written to Flash memory
  101. * 1: Error occurred while writing data in Flash memory
  102. * 2: Written Data in flash memory is different from expected one
  103. */
  104. uint32_t FLASH_If_Write(uint32_t destination, uint32_t *p_source, uint32_t length)
  105. {
  106. uint32_t i = 0;
  107. /* Unlock the Flash to enable the flash control register access *************/
  108. HAL_FLASH_Unlock();
  109. for (i = 0; (i < length) && (destination <= (USER_FLASH_END_ADDRESS-4)); i++)
  110. {
  111. /* Device voltage range supposed to be [2.7V to 3.6V], the operation will
  112. be done by word */
  113. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, destination, *(uint32_t*)(p_source+i)) == HAL_OK)
  114. {
  115. /* Check the written value */
  116. if (*(uint32_t*)destination != *(uint32_t*)(p_source+i))
  117. {
  118. /* Flash content doesn't match SRAM content */
  119. return(FLASHIF_WRITINGCTRL_ERROR);
  120. }
  121. /* Increment FLASH destination address */
  122. destination += 4;
  123. }
  124. else
  125. {
  126. /* Error occurred while writing data in Flash memory */
  127. return (FLASHIF_WRITING_ERROR);
  128. }
  129. }
  130. /* Lock the Flash to disable the flash control register access (recommended
  131. to protect the FLASH memory against possible unwanted operation) *********/
  132. HAL_FLASH_Lock();
  133. return (FLASHIF_OK);
  134. }
  135. /**
  136. * @brief Returns the write protection status of application flash area.
  137. * @param None
  138. * @retval If a sector in application area is write-protected returned value is a combinaison
  139. of the possible values : FLASHIF_PROTECTION_WRPENABLED, FLASHIF_PROTECTION_PCROPENABLED, ...
  140. * If no sector is write-protected FLASHIF_PROTECTION_NONE is returned.
  141. */
  142. uint32_t FLASH_If_GetWriteProtectionStatus(void)
  143. {
  144. uint32_t ProtectedPAGE = FLASHIF_PROTECTION_NONE;
  145. FLASH_OBProgramInitTypeDef OptionsBytesStruct;
  146. /* Unlock the Flash to enable the flash control register access *************/
  147. HAL_FLASH_Unlock();
  148. /* Check if there are write protected sectors inside the user flash area ****/
  149. HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
  150. /* Lock the Flash to disable the flash control register access (recommended
  151. to protect the FLASH memory against possible unwanted operation) *********/
  152. HAL_FLASH_Lock();
  153. /* Get pages already write protected ****************************************/
  154. ProtectedPAGE = ~(OptionsBytesStruct.WRPPage) & FLASH_PAGE_TO_BE_PROTECTED;
  155. /* Check if desired pages are already write protected ***********************/
  156. if(ProtectedPAGE != 0)
  157. {
  158. /* Some sectors inside the user flash area are write protected */
  159. return FLASHIF_PROTECTION_WRPENABLED;
  160. }
  161. else
  162. {
  163. /* No write protected sectors inside the user flash area */
  164. return FLASHIF_PROTECTION_NONE;
  165. }
  166. }
  167. /**
  168. * @brief Configure the write protection status of user flash area.
  169. * @param protectionstate : FLASHIF_WRP_DISABLE or FLASHIF_WRP_ENABLE the protection
  170. * @retval uint32_t FLASHIF_OK if change is applied.
  171. */
  172. uint32_t FLASH_If_WriteProtectionConfig(uint32_t protectionstate)
  173. {
  174. uint32_t ProtectedPAGE = 0x0;
  175. FLASH_OBProgramInitTypeDef config_new, config_old;
  176. HAL_StatusTypeDef result = HAL_OK;
  177. /* Get pages write protection status ****************************************/
  178. HAL_FLASHEx_OBGetConfig(&config_old);
  179. /* The parameter says whether we turn the protection on or off */
  180. config_new.WRPState = (protectionstate == FLASHIF_WRP_ENABLE ? OB_WRPSTATE_ENABLE : OB_WRPSTATE_DISABLE);
  181. /* We want to modify only the Write protection */
  182. config_new.OptionType = OPTIONBYTE_WRP;
  183. /* No read protection, keep BOR and reset settings */
  184. config_new.RDPLevel = OB_RDP_LEVEL_0;
  185. config_new.USERConfig = config_old.USERConfig;
  186. /* Get pages already write protected ****************************************/
  187. ProtectedPAGE = config_old.WRPPage | FLASH_PAGE_TO_BE_PROTECTED;
  188. /* Unlock the Flash to enable the flash control register access *************/
  189. HAL_FLASH_Unlock();
  190. /* Unlock the Options Bytes *************************************************/
  191. HAL_FLASH_OB_Unlock();
  192. /* Erase all the option Bytes ***********************************************/
  193. result = HAL_FLASHEx_OBErase();
  194. if (result == HAL_OK)
  195. {
  196. config_new.WRPPage = ProtectedPAGE;
  197. result = HAL_FLASHEx_OBProgram(&config_new);
  198. }
  199. return (result == HAL_OK ? FLASHIF_OK: FLASHIF_PROTECTION_ERRROR);
  200. }
  201. /**
  202. * @}
  203. */
  204. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/