flash_if.c 9.7 KB

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