You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

364 lines
13 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_flash.h
  4. * @author MCD Application Team
  5. * @version V1.1.0
  6. * @date 19-June-2014
  7. * @brief Header file of FLASH HAL module.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT(c) 2014 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. /* Define to prevent recursive inclusion -------------------------------------*/
  38. #ifndef __STM32F4xx_HAL_FLASH_H
  39. #define __STM32F4xx_HAL_FLASH_H
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /* Includes ------------------------------------------------------------------*/
  44. #include "stm32f4xx_hal_def.h"
  45. /** @addtogroup STM32F4xx_HAL_Driver
  46. * @{
  47. */
  48. /** @addtogroup FLASH
  49. * @{
  50. */
  51. /* Exported types ------------------------------------------------------------*/
  52. /**
  53. * @brief FLASH Error structure definition
  54. */
  55. typedef enum
  56. {
  57. FLASH_ERROR_RD = 0x01,
  58. FLASH_ERROR_PGS = 0x02,
  59. FLASH_ERROR_PGP = 0x04,
  60. FLASH_ERROR_PGA = 0x08,
  61. FLASH_ERROR_WRP = 0x10,
  62. FLASH_ERROR_OPERATION = 0x20
  63. }FLASH_ErrorTypeDef;
  64. /**
  65. * @brief FLASH Procedure structure definition
  66. */
  67. typedef enum
  68. {
  69. FLASH_PROC_NONE = 0,
  70. FLASH_PROC_SECTERASE,
  71. FLASH_PROC_MASSERASE,
  72. FLASH_PROC_PROGRAM
  73. } FLASH_ProcedureTypeDef;
  74. /**
  75. * @brief FLASH handle Structure definition
  76. */
  77. typedef struct
  78. {
  79. __IO FLASH_ProcedureTypeDef ProcedureOnGoing; /*Internal variable to indicate which procedure is ongoing or not in IT context*/
  80. __IO uint32_t NbSectorsToErase; /*Internal variable to save the remaining sectors to erase in IT context*/
  81. __IO uint8_t VoltageForErase; /*Internal variable to provide voltange range selected by user in IT context*/
  82. __IO uint32_t Sector; /*Internal variable to define the current sector which is erasing*/
  83. __IO uint32_t Bank; /*Internal variable to save current bank selected during mass erase*/
  84. __IO uint32_t Address; /*Internal variable to save address selected for program*/
  85. HAL_LockTypeDef Lock; /* FLASH locking object */
  86. __IO FLASH_ErrorTypeDef ErrorCode; /* FLASH error code */
  87. }FLASH_ProcessTypeDef;
  88. /**
  89. * @brief FLASH Error source
  90. */
  91. /* Exported constants --------------------------------------------------------*/
  92. /** @defgroup FLASH_Exported_Constants FLASH Exported Constants
  93. * @{
  94. */
  95. /** @defgroup FLASH_Type_Program FLASH Type Program
  96. * @{
  97. */
  98. #define TYPEPROGRAM_BYTE ((uint32_t)0x00) /*!< Program byte (8-bit) at a specified address */
  99. #define TYPEPROGRAM_HALFWORD ((uint32_t)0x01) /*!< Program a half-word (16-bit) at a specified address */
  100. #define TYPEPROGRAM_WORD ((uint32_t)0x02) /*!< Program a word (32-bit) at a specified address */
  101. #define TYPEPROGRAM_DOUBLEWORD ((uint32_t)0x03) /*!< Program a double word (64-bit) at a specified address */
  102. #define IS_TYPEPROGRAM(VALUE)(((VALUE) == TYPEPROGRAM_BYTE) || \
  103. ((VALUE) == TYPEPROGRAM_HALFWORD) || \
  104. ((VALUE) == TYPEPROGRAM_WORD) || \
  105. ((VALUE) == TYPEPROGRAM_DOUBLEWORD))
  106. /**
  107. * @}
  108. */
  109. /** @defgroup FLASH_Flag_definition FLASH Flag definition
  110. * @brief Flag definition
  111. * @{
  112. */
  113. #define FLASH_FLAG_EOP FLASH_SR_EOP /*!< FLASH End of Operation flag */
  114. #define FLASH_FLAG_OPERR FLASH_SR_SOP /*!< FLASH operation Error flag */
  115. #define FLASH_FLAG_WRPERR FLASH_SR_WRPERR /*!< FLASH Write protected error flag */
  116. #define FLASH_FLAG_PGAERR FLASH_SR_PGAERR /*!< FLASH Programming Alignment error flag */
  117. #define FLASH_FLAG_PGPERR FLASH_SR_PGPERR /*!< FLASH Programming Parallelism error flag */
  118. #define FLASH_FLAG_PGSERR FLASH_SR_PGSERR /*!< FLASH Programming Sequence error flag */
  119. #define FLASH_FLAG_RDERR ((uint32_t)0x00000100) /*!< Read Protection error flag (PCROP) */
  120. #define FLASH_FLAG_BSY FLASH_SR_BSY /*!< FLASH Busy flag */
  121. /**
  122. * @}
  123. */
  124. /** @defgroup FLASH_Interrupt_definition FLASH Interrupt definition
  125. * @brief FLASH Interrupt definition
  126. * @{
  127. */
  128. #define FLASH_IT_EOP FLASH_CR_EOPIE /*!< End of FLASH Operation Interrupt source */
  129. #define FLASH_IT_ERR ((uint32_t)0x02000000) /*!< Error Interrupt source */
  130. /**
  131. * @}
  132. */
  133. /** @defgroup FLASH_Program_Parallelism FLASH Program Parallelism
  134. * @{
  135. */
  136. #define FLASH_PSIZE_BYTE ((uint32_t)0x00000000)
  137. #define FLASH_PSIZE_HALF_WORD ((uint32_t)0x00000100)
  138. #define FLASH_PSIZE_WORD ((uint32_t)0x00000200)
  139. #define FLASH_PSIZE_DOUBLE_WORD ((uint32_t)0x00000300)
  140. #define CR_PSIZE_MASK ((uint32_t)0xFFFFFCFF)
  141. /**
  142. * @}
  143. */
  144. /** @defgroup FLASH_Keys FLASH Keys
  145. * @{
  146. */
  147. #define RDP_KEY ((uint16_t)0x00A5)
  148. #define FLASH_KEY1 ((uint32_t)0x45670123)
  149. #define FLASH_KEY2 ((uint32_t)0xCDEF89AB)
  150. #define FLASH_OPT_KEY1 ((uint32_t)0x08192A3B)
  151. #define FLASH_OPT_KEY2 ((uint32_t)0x4C5D6E7F)
  152. /**
  153. * @}
  154. */
  155. /**
  156. * @brief ACR register byte 0 (Bits[7:0]) base address
  157. */
  158. #define ACR_BYTE0_ADDRESS ((uint32_t)0x40023C00)
  159. /**
  160. * @brief OPTCR register byte 0 (Bits[7:0]) base address
  161. */
  162. #define OPTCR_BYTE0_ADDRESS ((uint32_t)0x40023C14)
  163. /**
  164. * @brief OPTCR register byte 1 (Bits[15:8]) base address
  165. */
  166. #define OPTCR_BYTE1_ADDRESS ((uint32_t)0x40023C15)
  167. /**
  168. * @brief OPTCR register byte 2 (Bits[23:16]) base address
  169. */
  170. #define OPTCR_BYTE2_ADDRESS ((uint32_t)0x40023C16)
  171. /**
  172. * @brief OPTCR register byte 3 (Bits[31:24]) base address
  173. */
  174. #define OPTCR_BYTE3_ADDRESS ((uint32_t)0x40023C17)
  175. /**
  176. * @}
  177. */
  178. /* Exported macro ------------------------------------------------------------*/
  179. /**
  180. * @brief Set the FLASH Latency.
  181. * @param __LATENCY__: FLASH Latency
  182. * The value of this parameter depend on device used within the same series
  183. * @retval none
  184. */
  185. #define __HAL_FLASH_SET_LATENCY(__LATENCY__) (*(__IO uint8_t *)ACR_BYTE0_ADDRESS = (uint8_t)(__LATENCY__))
  186. /**
  187. * @brief Enable the FLASH prefetch buffer.
  188. * @retval none
  189. */
  190. #define __HAL_FLASH_PREFETCH_BUFFER_ENABLE() (FLASH->ACR |= FLASH_ACR_PRFTEN)
  191. /**
  192. * @brief Disable the FLASH prefetch buffer.
  193. * @retval none
  194. */
  195. #define __HAL_FLASH_PREFETCH_BUFFER_DISABLE() (FLASH->ACR &= (~FLASH_ACR_PRFTEN))
  196. /**
  197. * @brief Enable the FLASH instruction cache.
  198. * @retval none
  199. */
  200. #define __HAL_FLASH_INSTRUCTION_CACHE_ENABLE() (FLASH->ACR |= FLASH_ACR_ICEN)
  201. /**
  202. * @brief Disable the FLASH instruction cache.
  203. * @retval none
  204. */
  205. #define __HAL_FLASH_INSTRUCTION_CACHE_DISABLE() (FLASH->ACR &= (~FLASH_ACR_ICEN))
  206. /**
  207. * @brief Enable the FLASH data cache.
  208. * @retval none
  209. */
  210. #define __HAL_FLASH_DATA_CACHE_ENABLE() (FLASH->ACR |= FLASH_ACR_DCEN)
  211. /**
  212. * @brief Disable the FLASH data cache.
  213. * @retval none
  214. */
  215. #define __HAL_FLASH_DATA_CACHE_DISABLE() (FLASH->ACR &= (~FLASH_ACR_DCEN))
  216. /**
  217. * @brief Resets the FLASH instruction Cache.
  218. * @note This function must be used only when the Instruction Cache is disabled.
  219. * @retval None
  220. */
  221. #define __HAL_FLASH_INSTRUCTION_CACHE_RESET() (FLASH->ACR |= FLASH_ACR_ICRST)
  222. /**
  223. * @brief Resets the FLASH data Cache.
  224. * @note This function must be used only when the data Cache is disabled.
  225. * @retval None
  226. */
  227. #define __HAL_FLASH_DATA_CACHE_RESET() (FLASH->ACR |= FLASH_ACR_DCRST)
  228. /**
  229. * @brief Enable the specified FLASH interrupt.
  230. * @param __INTERRUPT__ : FLASH interrupt
  231. * This parameter can be any combination of the following values:
  232. * @arg FLASH_IT_EOP: End of FLASH Operation Interrupt
  233. * @arg FLASH_IT_ERR: Error Interrupt
  234. * @retval none
  235. */
  236. #define __HAL_FLASH_ENABLE_IT(__INTERRUPT__) (FLASH->CR |= (__INTERRUPT__))
  237. /**
  238. * @brief Disable the specified FLASH interrupt.
  239. * @param __INTERRUPT__ : FLASH interrupt
  240. * This parameter can be any combination of the following values:
  241. * @arg FLASH_IT_EOP: End of FLASH Operation Interrupt
  242. * @arg FLASH_IT_ERR: Error Interrupt
  243. * @retval none
  244. */
  245. #define __HAL_FLASH_DISABLE_IT(__INTERRUPT__) (FLASH->CR &= ~(uint32_t)(__INTERRUPT__))
  246. /**
  247. * @brief Get the specified FLASH flag status.
  248. * @param __FLAG__: specifies the FLASH flag to check.
  249. * This parameter can be one of the following values:
  250. * @arg FLASH_FLAG_EOP : FLASH End of Operation flag
  251. * @arg FLASH_FLAG_OPERR : FLASH operation Error flag
  252. * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
  253. * @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
  254. * @arg FLASH_FLAG_PGPERR: FLASH Programming Parallelism error flag
  255. * @arg FLASH_FLAG_PGSERR: FLASH Programming Sequence error flag
  256. * @arg FLASH_FLAG_RDERR : FLASH Read Protection error flag (PCROP)
  257. * @arg FLASH_FLAG_BSY : FLASH Busy flag
  258. * @retval The new state of __FLAG__ (SET or RESET).
  259. */
  260. #define __HAL_FLASH_GET_FLAG(__FLAG__) ((FLASH->SR & (__FLAG__)))
  261. /**
  262. * @brief Clear the specified FLASH flag.
  263. * @param __FLAG__: specifies the FLASH flags to clear.
  264. * This parameter can be any combination of the following values:
  265. * @arg FLASH_FLAG_EOP : FLASH End of Operation flag
  266. * @arg FLASH_FLAG_OPERR : FLASH operation Error flag
  267. * @arg FLASH_FLAG_WRPERR: FLASH Write protected error flag
  268. * @arg FLASH_FLAG_PGAERR: FLASH Programming Alignment error flag
  269. * @arg FLASH_FLAG_PGPERR: FLASH Programming Parallelism error flag
  270. * @arg FLASH_FLAG_PGSERR: FLASH Programming Sequence error flag
  271. * @arg FLASH_FLAG_RDERR : FLASH Read Protection error flag (PCROP)
  272. * @retval none
  273. */
  274. #define __HAL_FLASH_CLEAR_FLAG(__FLAG__) (FLASH->SR = (__FLAG__))
  275. /* Include FLASH HAL Extension module */
  276. #include "stm32f4xx_hal_flash_ex.h"
  277. #include "stm32f4xx_hal_flash_ramfunc.h"
  278. /* Exported functions --------------------------------------------------------*/
  279. /* Program operation functions ***********************************************/
  280. HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
  281. HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
  282. /* FLASH IRQ handler method */
  283. void HAL_FLASH_IRQHandler(void);
  284. /* Callbacks in non blocking modes */
  285. void HAL_FLASH_EndOfOperationCallback(uint32_t ReturnValue);
  286. void HAL_FLASH_OperationErrorCallback(uint32_t ReturnValue);
  287. /* Peripheral Control functions **********************************************/
  288. HAL_StatusTypeDef HAL_FLASH_Unlock(void);
  289. HAL_StatusTypeDef HAL_FLASH_Lock(void);
  290. HAL_StatusTypeDef HAL_FLASH_OB_Unlock(void);
  291. HAL_StatusTypeDef HAL_FLASH_OB_Lock(void);
  292. /* Option bytes control */
  293. HAL_StatusTypeDef HAL_FLASH_OB_Launch(void);
  294. /* Peripheral State functions ************************************************/
  295. FLASH_ErrorTypeDef HAL_FLASH_GetError(void);
  296. HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout);
  297. /**
  298. * @}
  299. */
  300. /**
  301. * @}
  302. */
  303. #ifdef __cplusplus
  304. }
  305. #endif
  306. #endif /* __STM32F4xx_HAL_FLASH_H */
  307. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/