hal_flash.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. ////////////////////////////////////////////////////////////////////////////////
  2. /// @file hal_flash.h
  3. /// @author AE TEAM
  4. /// @brief THIS FILE CONTAINS ALL THE FUNCTIONS PROTOTYPES FOR THE FLASH
  5. /// FIRMWARE LIBRARY.
  6. ////////////////////////////////////////////////////////////////////////////////
  7. /// @attention
  8. ///
  9. /// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
  10. /// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
  11. /// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
  12. /// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
  13. /// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
  14. /// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
  15. ///
  16. /// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
  17. ////////////////////////////////////////////////////////////////////////////////
  18. // Define to prevent recursive inclusion
  19. #ifndef __HAL_FLASH_H
  20. #define __HAL_FLASH_H
  21. // Files includes
  22. #include "types.h"
  23. #include "reg_common.h"
  24. #include "reg_flash.h"
  25. ////////////////////////////////////////////////////////////////////////////////
  26. /// @addtogroup MM32_Hardware_Abstract_Layer
  27. /// @{
  28. ////////////////////////////////////////////////////////////////////////////////
  29. /// @defgroup FLASH_HAL
  30. /// @brief FLASH HAL modules
  31. /// @{
  32. ////////////////////////////////////////////////////////////////////////////////
  33. /// @defgroup FLASH_Exported_Types
  34. /// @{
  35. ////////////////////////////////////////////////////////////////////////////////
  36. /// @brief FLASH Status
  37. ////////////////////////////////////////////////////////////////////////////////
  38. typedef enum {
  39. FLASH_BUSY = 1, ///< FLASH busy status
  40. FLASH_ERROR_PG, ///< FLASH programming error status
  41. FLASH_ERROR_WRP, ///< FLASH write protection error status
  42. FLASH_COMPLETE, ///< FLASH end of operation status
  43. FLASH_TIMEOUT ///< FLASH Last operation timed out status
  44. } FLASH_Status;
  45. ////////////////////////////////////////////////////////////////////////////////
  46. /// @brief FLASH Latency
  47. ////////////////////////////////////////////////////////////////////////////////
  48. typedef enum {
  49. FLASH_Latency_0 = FLASH_ACR_LATENCY_0, ///< FLASH Zero Latency cycle
  50. FLASH_Latency_1 = FLASH_ACR_LATENCY_1, ///< FLASH One Latency cycle
  51. FLASH_Latency_2 = FLASH_ACR_LATENCY_2, ///< FLASH Two Latency cycles
  52. FLASH_Latency_3 = FLASH_ACR_LATENCY_3 ///< FLASH Three Latency cycles
  53. } FLASH_Latency_TypeDef;
  54. ////////////////////////////////////////////////////////////////////////////////
  55. /// @brief Half_Cycle_Enable_Disable
  56. ////////////////////////////////////////////////////////////////////////////////
  57. typedef enum {
  58. FLASH_HalfCycleAccess_Enable = FLASH_ACR_HLFCYA, ///< FLASH Half Cycle Enable
  59. FLASH_HalfCycleAccess_Disable = (s32)~FLASH_ACR_HLFCYA ///< FLASH Half Cycle Disable
  60. } FLASH_HalfCycleAccess_TypeDef;
  61. ////////////////////////////////////////////////////////////////////////////////
  62. /// @brief Prefetch_Buffer_Enable_Disable
  63. ////////////////////////////////////////////////////////////////////////////////
  64. typedef enum {
  65. FLASH_PrefetchBuffer_Enable = FLASH_ACR_PRFTBE, ///< FLASH Prefetch Buffer Enable
  66. FLASH_PrefetchBuffer_Disable = (s32)~FLASH_ACR_PRFTBE ///< FLASH Prefetch Buffer Disable
  67. } FLASH_PrefetchBuffer_TypeDef;
  68. ////////////////////////////////////////////////////////////////////////////////
  69. /// @brief Option_Bytes_IWatchdog
  70. ////////////////////////////////////////////////////////////////////////////////
  71. typedef enum {
  72. OB_IWDG_SW = 0x0001, ///< Software IWDG selected
  73. OB_IWDG_HW = 0x0000 ///< Hardware IWDG selected
  74. } OB_IWDG_TypeDef;
  75. ////////////////////////////////////////////////////////////////////////////////
  76. /// @brief Option_Bytes_nRST_STOP
  77. ////////////////////////////////////////////////////////////////////////////////
  78. typedef enum {
  79. OB_STOP_NoRST = 0x0002, ///< No reset generated when entering in STOP
  80. OB_STOP_RST = 0x0000 ///< Reset generated when entering in STOP
  81. } OB_STOP_TypeDef;
  82. ////////////////////////////////////////////////////////////////////////////////
  83. /// @brief Option_Bytes_nRST_STDBY
  84. ////////////////////////////////////////////////////////////////////////////////
  85. typedef enum {
  86. OB_STDBY_NoRST = 0x0004, ///< No reset generated when entering in STANDBY
  87. OB_STDBY_RST = 0x0000 ///< Reset generated when entering in STANDBY
  88. } OB_STDBY_TypeDef;
  89. ////////////////////////////////////////////////////////////////////////////////
  90. /// @brief FLASH_Interrupts
  91. ////////////////////////////////////////////////////////////////////////////////
  92. typedef enum {
  93. FLASH_IT_ERROR = FLASH_CR_ERRIE, ///< FPEC error interrupt source
  94. FLASH_IT_EOP = FLASH_CR_EOPIE ///< End of FLASH Operation Interrupt source
  95. } FLASH_IT_TypeDef;
  96. ////////////////////////////////////////////////////////////////////////////////
  97. /// @brief FLASH_Flags
  98. ////////////////////////////////////////////////////////////////////////////////
  99. typedef enum {
  100. FLASH_FLAG_EOP = FLASH_SR_EOP, ///< FLASH End of Operation flag
  101. FLASH_FLAG_PGERR = FLASH_SR_PGERR, ///< FLASH Program error flag
  102. FLASH_FLAG_WRPRTERR = FLASH_SR_WRPRTERR, ///< FLASH Write protected error flag
  103. FLASH_FLAG_BSY = FLASH_SR_BUSY, ///< FLASH Busy flag
  104. FLASH_FLAG_OPTERR = FLASH_OBR_OPTERR ///< FLASH Option Byte error flag
  105. } FLASH_FLAG_TypeDef;
  106. /// @}
  107. ////////////////////////////////////////////////////////////////////////////////
  108. /// @defgroup FLASH_Exported_Constants
  109. /// @{
  110. #define RDP_Key ((u16)0x00A5)
  111. #define FLASH_KEY1 ((u32)0x45670123)
  112. #define FLASH_KEY2 ((u32)0xCDEF89AB)
  113. #define EraseTimeout ((u32)0x00000FFF)
  114. #define ProgramTimeout ((u32)0x0000000F)
  115. #define FLASH_WRProt_Pages0to3 ((u32)0x00000001) ///< Write protection of page 0 to 3
  116. #define FLASH_WRProt_Pages4to7 ((u32)0x00000002) ///< Write protection of page 4 to 7
  117. #define FLASH_WRProt_Pages8to11 ((u32)0x00000004) ///< Write protection of page 8 to 11
  118. #define FLASH_WRProt_Pages12to15 ((u32)0x00000008) ///< Write protection of page 12 to 15
  119. #define FLASH_WRProt_Pages16to19 ((u32)0x00000010) ///< Write protection of page 16 to 19
  120. #define FLASH_WRProt_Pages20to23 ((u32)0x00000020) ///< Write protection of page 20 to 23
  121. #define FLASH_WRProt_Pages24to27 ((u32)0x00000040) ///< Write protection of page 24 to 27
  122. #define FLASH_WRProt_Pages28to31 ((u32)0x00000080) ///< Write protection of page 28 to 31
  123. #define FLASH_WRProt_Pages32to35 ((u32)0x00000100) ///< Write protection of page 32 to 35
  124. #define FLASH_WRProt_Pages36to39 ((u32)0x00000200) ///< Write protection of page 36 to 39
  125. #define FLASH_WRProt_Pages40to43 ((u32)0x00000400) ///< Write protection of page 40 to 43
  126. #define FLASH_WRProt_Pages44to47 ((u32)0x00000800) ///< Write protection of page 44 to 47
  127. #define FLASH_WRProt_Pages48to51 ((u32)0x00001000) ///< Write protection of page 48 to 51
  128. #define FLASH_WRProt_Pages52to55 ((u32)0x00002000) ///< Write protection of page 52 to 55
  129. #define FLASH_WRProt_Pages56to59 ((u32)0x00004000) ///< Write protection of page 56 to 59
  130. #define FLASH_WRProt_Pages60to63 ((u32)0x00008000) ///< Write protection of page 60 to 63
  131. #define FLASH_WRProt_Pages64to67 ((u32)0x00010000) ///< Write protection of page 64 to 67
  132. #define FLASH_WRProt_Pages68to71 ((u32)0x00020000) ///< Write protection of page 68 to 71
  133. #define FLASH_WRProt_Pages72to75 ((u32)0x00040000) ///< Write protection of page 72 to 75
  134. #define FLASH_WRProt_Pages76to79 ((u32)0x00080000) ///< Write protection of page 76 to 79
  135. #define FLASH_WRProt_Pages80to83 ((u32)0x00100000) ///< Write protection of page 80 to 83
  136. #define FLASH_WRProt_Pages84to87 ((u32)0x00200000) ///< Write protection of page 84 to 87
  137. #define FLASH_WRProt_Pages88to91 ((u32)0x00400000) ///< Write protection of page 88 to 91
  138. #define FLASH_WRProt_Pages92to95 ((u32)0x00800000) ///< Write protection of page 92 to 95
  139. #define FLASH_WRProt_Pages96to99 ((u32)0x01000000) ///< Write protection of page 96 to 99
  140. #define FLASH_WRProt_Pages100to103 ((u32)0x02000000) ///< Write protection of page 100 to 103
  141. #define FLASH_WRProt_Pages104to107 ((u32)0x04000000) ///< Write protection of page 104 to 107
  142. #define FLASH_WRProt_Pages108to111 ((u32)0x08000000) ///< Write protection of page 108 to 111
  143. #define FLASH_WRProt_Pages112to115 ((u32)0x10000000) ///< Write protection of page 112 to 115
  144. #define FLASH_WRProt_Pages116to119 ((u32)0x20000000) ///< Write protection of page 115 to 119
  145. #define FLASH_WRProt_Pages120to123 ((u32)0x40000000) ///< Write protection of page 120 to 123
  146. #define FLASH_WRProt_Pages124to127 ((u32)0x80000000) ///< Write protection of page 124 to 127
  147. /// @}
  148. ////////////////////////////////////////////////////////////////////////////////
  149. /// @defgroup FLASH_Exported_Variables
  150. /// @{
  151. #ifdef _HAL_FLASH_C_
  152. #define GLOBAL
  153. #else
  154. #define GLOBAL extern
  155. #endif
  156. #undef GLOBAL
  157. /// @}
  158. ////////////////////////////////////////////////////////////////////////////////
  159. /// @defgroup FLASH_Exported_Functions
  160. /// @{
  161. void FLASH_SetLatency(FLASH_Latency_TypeDef latency);
  162. void FLASH_HalfCycleAccessCmd(FLASH_HalfCycleAccess_TypeDef half_cycle_access);
  163. void FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_TypeDef prefetch_buffer);
  164. void FLASH_Unlock(void);
  165. void FLASH_Lock(void);
  166. void FLASH_OPTB_Enable(void);
  167. void FLASH_ITConfig(FLASH_IT_TypeDef interrupt, FunctionalState state);
  168. void FLASH_ClearFlag(u16 flag);
  169. void exFLASH_EraseEE(u32 page_address);
  170. void exFLASH_ProgramEE(u16* buf, u32 address, u16 len);
  171. void exFLASH_WriteEE(u16* buf, u32 page_address, u16 len);
  172. void* exFLASH_Locate(u32 page_address, u16 len);
  173. void* exFLASH_ReadEE(u32 page_address, u16 len);
  174. u8 exFLASH_FindEmpty(u16* ptr, u16 len);
  175. u32 FLASH_GetUserOptionByte(void);
  176. u32 FLASH_GetWriteProtectionOptionByte(void);
  177. FLASH_Status FLASH_ErasePage(u32 page_address);
  178. FLASH_Status FLASH_EraseAllPages(void);
  179. FLASH_Status FLASH_EraseOptionBytes(void);
  180. FLASH_Status FLASH_EraseProtect(void);
  181. FLASH_Status FLASH_ProgramHalfWord(u32 address, u16 data);
  182. FLASH_Status FLASH_ProgramWord(u32 address, u32 data);
  183. FLASH_Status FLASH_ProgramOptionHalfWord(u32 address, u16 data);
  184. FLASH_Status FLASH_ProgramOptionByteData(u32 address, u8 data);
  185. FLASH_Status FLASH_ProgramProtect(u32 address, u16 data);
  186. FLASH_Status FLASH_EnableWriteProtection(u32 page);
  187. FLASH_Status FLASH_UserOptionByteConfig(OB_IWDG_TypeDef ob_iwdg, OB_STOP_TypeDef ob_stop, OB_STDBY_TypeDef ob_standby);
  188. FLASH_Status FLASH_GetStatus(void);
  189. FLASH_Status FLASH_WaitForLastOperation(u32 time_out);
  190. FLASH_Status FLASH_ReadOutProtection(FunctionalState state);
  191. FlagStatus FLASH_GetPrefetchBufferStatus(void);
  192. FlagStatus FLASH_GetFlagStatus(u16 flag);
  193. /// @}
  194. /// @}
  195. /// @}
  196. ////////////////////////////////////////////////////////////////////////////////
  197. #endif //__HAL_FLASH_H
  198. ////////////////////////////////////////////////////////////////////////////////