n32g45x_flash.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file n32g45x_flash.h
  29. * @author Nations
  30. * @version v1.0.2
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #ifndef __N32G45X_FLASH_H__
  35. #define __N32G45X_FLASH_H__
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. #include "n32g45x.h"
  40. /** @addtogroup N32G45X_StdPeriph_Driver
  41. * @{
  42. */
  43. /** @addtogroup FLASH
  44. * @{
  45. */
  46. /** @addtogroup FLASH_Exported_Types
  47. * @{
  48. */
  49. /**
  50. * @brief FLASH Status
  51. */
  52. typedef enum
  53. {
  54. FLASH_BUSY = 1,
  55. FLASH_RESERVED,
  56. FLASH_ERR_PG,
  57. FLASH_ERR_PV,
  58. FLASH_ERR_WRP,
  59. FLASH_COMPL,
  60. FLASH_ERR_EV,
  61. FLASH_ERR_RDP2,
  62. FLASH_ERR_ADD,
  63. FLASH_TIMEOUT
  64. } FLASH_STS;
  65. typedef enum
  66. {
  67. FLASH_SMP1 = 0,
  68. FLASH_SMP2
  69. } FLASH_SMPSEL;
  70. /**
  71. * @}
  72. */
  73. /** @addtogroup FLASH_Exported_Constants
  74. * @{
  75. */
  76. /** @addtogroup Flash_Latency
  77. * @{
  78. */
  79. #define FLASH_LATENCY_0 ((uint32_t)0x00000000) /*!< FLASH Zero Latency cycle */
  80. #define FLASH_LATENCY_1 ((uint32_t)0x00000001) /*!< FLASH One Latency cycle */
  81. #define FLASH_LATENCY_2 ((uint32_t)0x00000002) /*!< FLASH Two Latency cycles */
  82. #define FLASH_LATENCY_3 ((uint32_t)0x00000003) /*!< FLASH Three Latency cycles */
  83. #define FLASH_LATENCY_4 ((uint32_t)0x00000004) /*!< FLASH Four Latency cycles */
  84. #define IS_FLASH_LATENCY(LATENCY) \
  85. (((LATENCY) == FLASH_LATENCY_0) || ((LATENCY) == FLASH_LATENCY_1) || ((LATENCY) == FLASH_LATENCY_2) \
  86. || ((LATENCY) == FLASH_LATENCY_3) || ((LATENCY) == FLASH_LATENCY_4))
  87. /**
  88. * @}
  89. */
  90. /** @addtogroup Prefetch_Buffer_Enable_Disable
  91. * @{
  92. */
  93. #define FLASH_PrefetchBuf_EN ((uint32_t)0x00000010) /*!< FLASH Prefetch Buffer Enable */
  94. #define FLASH_PrefetchBuf_DIS ((uint32_t)0x00000000) /*!< FLASH Prefetch Buffer Disable */
  95. #define IS_FLASH_PREFETCHBUF_STATE(STATE) (((STATE) == FLASH_PrefetchBuf_EN) || ((STATE) == FLASH_PrefetchBuf_DIS))
  96. /**
  97. * @}
  98. */
  99. /** @addtogroup iCache_Enable_Disable
  100. * @{
  101. */
  102. #define FLASH_iCache_EN ((uint32_t)0x00000080) /*!< FLASH iCache Enable */
  103. #define FLASH_iCache_DIS ((uint32_t)0x00000000) /*!< FLASH iCache Disable */
  104. #define IS_FLASH_ICACHE_STATE(STATE) (((STATE) == FLASH_iCache_EN) || ((STATE) == FLASH_iCache_DIS))
  105. /**
  106. * @}
  107. */
  108. /** @addtogroup SMPSEL_SMP1_SMP2
  109. * @{
  110. */
  111. #define FLASH_SMPSEL_SMP1 ((uint32_t)0x00000000) /*!< FLASH SMPSEL SMP1 */
  112. #define FLASH_SMPSEL_SMP2 ((uint32_t)0x00000100) /*!< FLASH SMPSEL SMP2 */
  113. #define IS_FLASH_SMPSEL_STATE(STATE) (((STATE) == FLASH_SMPSEL_SMP1) || ((STATE) == FLASH_SMPSEL_SMP2))
  114. /**
  115. * @}
  116. */
  117. /* Values to be used with N32G45X devices */
  118. #define FLASH_WRP_Pages0to1 \
  119. ((uint32_t)0x00000001) /*!< N32G45X devices: \
  120. Write protection of page 0 to 1 */
  121. #define FLASH_WRP_Pages2to3 \
  122. ((uint32_t)0x00000002) /*!< N32G45X devices: \
  123. Write protection of page 2 to 3 */
  124. #define FLASH_WRP_Pages4to5 \
  125. ((uint32_t)0x00000004) /*!< N32G45X devices: \
  126. Write protection of page 4 to 5 */
  127. #define FLASH_WRP_Pages6to7 \
  128. ((uint32_t)0x00000008) /*!< N32G45X devices: \
  129. Write protection of page 6 to 7 */
  130. #define FLASH_WRP_Pages8to9 \
  131. ((uint32_t)0x00000010) /*!< N32G45X devices: \
  132. Write protection of page 8 to 9 */
  133. #define FLASH_WRP_Pages10to11 \
  134. ((uint32_t)0x00000020) /*!< N32G45X devices: \
  135. Write protection of page 10 to 11 */
  136. #define FLASH_WRP_Pages12to13 \
  137. ((uint32_t)0x00000040) /*!< N32G45X devices: \
  138. Write protection of page 12 to 13 */
  139. #define FLASH_WRP_Pages14to15 \
  140. ((uint32_t)0x00000080) /*!< N32G45X devices: \
  141. Write protection of page 14 to 15 */
  142. #define FLASH_WRP_Pages16to17 \
  143. ((uint32_t)0x00000100) /*!< N32G45X devices: \
  144. Write protection of page 16 to 17 */
  145. #define FLASH_WRP_Pages18to19 \
  146. ((uint32_t)0x00000200) /*!< N32G45X devices: \
  147. Write protection of page 18 to 19 */
  148. #define FLASH_WRP_Pages20to21 \
  149. ((uint32_t)0x00000400) /*!< N32G45X devices: \
  150. Write protection of page 20 to 21 */
  151. #define FLASH_WRP_Pages22to23 \
  152. ((uint32_t)0x00000800) /*!< N32G45X devices: \
  153. Write protection of page 22 to 23 */
  154. #define FLASH_WRP_Pages24to25 \
  155. ((uint32_t)0x00001000) /*!< N32G45X devices: \
  156. Write protection of page 24 to 25 */
  157. #define FLASH_WRP_Pages26to27 \
  158. ((uint32_t)0x00002000) /*!< N32G45X devices: \
  159. Write protection of page 26 to 27 */
  160. #define FLASH_WRP_Pages28to29 \
  161. ((uint32_t)0x00004000) /*!< N32G45X devices: \
  162. Write protection of page 28 to 29 */
  163. #define FLASH_WRP_Pages30to31 \
  164. ((uint32_t)0x00008000) /*!< N32G45X devices: \
  165. Write protection of page 30 to 31 */
  166. #define FLASH_WRP_Pages32to33 \
  167. ((uint32_t)0x00010000) /*!< N32G45X devices: \
  168. Write protection of page 32 to 33 */
  169. #define FLASH_WRP_Pages34to35 \
  170. ((uint32_t)0x00020000) /*!< N32G45X devices: \
  171. Write protection of page 34 to 35 */
  172. #define FLASH_WRP_Pages36to37 \
  173. ((uint32_t)0x00040000) /*!< N32G45X devices: \
  174. Write protection of page 36 to 37 */
  175. #define FLASH_WRP_Pages38to39 \
  176. ((uint32_t)0x00080000) /*!< N32G45X devices: \
  177. Write protection of page 38 to 39 */
  178. #define FLASH_WRP_Pages40to41 \
  179. ((uint32_t)0x00100000) /*!< N32G45X devices: \
  180. Write protection of page 40 to 41 */
  181. #define FLASH_WRP_Pages42to43 \
  182. ((uint32_t)0x00200000) /*!< N32G45X devices: \
  183. Write protection of page 42 to 43 */
  184. #define FLASH_WRP_Pages44to45 \
  185. ((uint32_t)0x00400000) /*!< N32G45X devices: \
  186. Write protection of page 44 to 45 */
  187. #define FLASH_WRP_Pages46to47 \
  188. ((uint32_t)0x00800000) /*!< N32G45X devices: \
  189. Write protection of page 46 to 47 */
  190. #define FLASH_WRP_Pages48to49 \
  191. ((uint32_t)0x01000000) /*!< N32G45X devices: \
  192. Write protection of page 48 to 49 */
  193. #define FLASH_WRP_Pages50to51 \
  194. ((uint32_t)0x02000000) /*!< N32G45X devices: \
  195. Write protection of page 50 to 51 */
  196. #define FLASH_WRP_Pages52to53 \
  197. ((uint32_t)0x04000000) /*!< N32G45X devices: \
  198. Write protection of page 52 to 53 */
  199. #define FLASH_WRP_Pages54to55 \
  200. ((uint32_t)0x08000000) /*!< N32G45X devices: \
  201. Write protection of page 54 to 55 */
  202. #define FLASH_WRP_Pages56to57 \
  203. ((uint32_t)0x10000000) /*!< N32G45X devices: \
  204. Write protection of page 56 to 57 */
  205. #define FLASH_WRP_Pages58to59 \
  206. ((uint32_t)0x20000000) /*!< N32G45X devices: \
  207. Write protection of page 58 to 59 */
  208. #define FLASH_WRP_Pages60to61 \
  209. ((uint32_t)0x40000000) /*!< N32G45X devices: \
  210. Write protection of page 60 to 61 */
  211. #define FLASH_WRP_Pages62to127 \
  212. ((uint32_t)0x80000000) /*!< N32G45X - 256KB devices: Write protection of page 62 to 127 */
  213. #define FLASH_WRP_Pages62to255 \
  214. ((uint32_t)0x80000000) /*!< N32G45X - 512KB devices: Write protection of page 62 to 255 */
  215. #define FLASH_WRP_AllPages ((uint32_t)0xFFFFFFFF) /*!< Write protection of all Pages */
  216. #define IS_FLASH_WRP_PAGE(PAGE) (((PAGE) != 0x00000000))
  217. #define IS_FLASH_ADDRESS(ADDRESS) (((ADDRESS) >= 0x08000000) && ((ADDRESS) < 0x0807FFFF))
  218. #define IS_OB_DATA_ADDRESS(ADDRESS) ((ADDRESS) == 0x1FFFF804)
  219. /**
  220. * @}
  221. */
  222. /** @addtogroup Option_Bytes_IWatchdog
  223. * @{
  224. */
  225. #define OB_IWDG_SW ((uint16_t)0x0001) /*!< Software IWDG selected */
  226. #define OB_IWDG_HW ((uint16_t)0x0000) /*!< Hardware IWDG selected */
  227. #define IS_OB_IWDG_SOURCE(SOURCE) (((SOURCE) == OB_IWDG_SW) || ((SOURCE) == OB_IWDG_HW))
  228. /**
  229. * @}
  230. */
  231. /** @addtogroup Option_Bytes_nRST_STOP
  232. * @{
  233. */
  234. #define OB_STOP0_NORST ((uint16_t)0x0002) /*!< No reset generated when entering in STOP */
  235. #define OB_STOP0_RST ((uint16_t)0x0000) /*!< Reset generated when entering in STOP */
  236. #define IS_OB_STOP0_SOURCE(SOURCE) (((SOURCE) == OB_STOP0_NORST) || ((SOURCE) == OB_STOP0_RST))
  237. /**
  238. * @}
  239. */
  240. /** @addtogroup Option_Bytes_nRST_STDBY
  241. * @{
  242. */
  243. #define OB_STDBY_NORST ((uint16_t)0x0004) /*!< No reset generated when entering in STANDBY */
  244. #define OB_STDBY_RST ((uint16_t)0x0000) /*!< Reset generated when entering in STANDBY */
  245. #define IS_OB_STDBY_SOURCE(SOURCE) (((SOURCE) == OB_STDBY_NORST) || ((SOURCE) == OB_STDBY_RST))
  246. /**
  247. * @}
  248. */
  249. /** @addtogroup FLASH_Interrupts
  250. * @{
  251. */
  252. #define FLASH_INT_ERRIE ((uint32_t)0x00000400) /*!< PGERR WRPERR ERROR error interrupt source */
  253. #define FLASH_INT_FERR ((uint32_t)0x00000800) /*!< EVERR PVERR interrupt source */
  254. #define FLASH_INT_EOP ((uint32_t)0x00001000) /*!< End of FLASH Operation Interrupt source */
  255. #define IS_FLASH_INT(IT) ((((IT) & (uint32_t)0xFFFFE3FF) == 0x00000000) && (((IT) != 0x00000000)))
  256. /**
  257. * @}
  258. */
  259. /** @addtogroup FLASH_Flags
  260. * @{
  261. */
  262. #define FLASH_FLAG_BUSY ((uint32_t)0x00000001) /*!< FLASH Busy flag */
  263. #define FLASH_FLAG_PGERR ((uint32_t)0x00000004) /*!< FLASH Program error flag */
  264. #define FLASH_FLAG_PVERR ((uint32_t)0x00000008) /*!< FLASH Program Verify ERROR flag after program */
  265. #define FLASH_FLAG_WRPERR ((uint32_t)0x00000010) /*!< FLASH Write protected error flag */
  266. #define FLASH_FLAG_EOP ((uint32_t)0x00000020) /*!< FLASH End of Operation flag */
  267. #define FLASH_FLAG_EVERR ((uint32_t)0x00000040) /*!< FLASH Erase Verify ERROR flag after page erase */
  268. #define FLASH_FLAG_OBERR ((uint32_t)0x00000001) /*!< FLASH Option Byte error flag */
  269. #define IS_FLASH_CLEAR_FLAG(FLAG) ((((FLAG) & 0xFFFFFF83) == 0x00) && (FLAG != 0x00))
  270. #define IS_FLASH_GET_FLAG(FLAG) \
  271. (((FLAG) == FLASH_FLAG_BUSY) || ((FLAG) == FLASH_FLAG_PGERR) || ((FLAG) == FLASH_FLAG_PVERR) \
  272. || ((FLAG) == FLASH_FLAG_WRPERR) || ((FLAG) == FLASH_FLAG_EOP) || ((FLAG) == FLASH_FLAG_EVERR) \
  273. || ((FLAG) == FLASH_FLAG_OBERR))
  274. /**
  275. * @}
  276. */
  277. /** @addtogroup FLASH_STS_CLRFLAG
  278. * @{
  279. */
  280. #define FLASH_STS_CLRFLAG (FLASH_FLAG_PGERR | FLASH_FLAG_PVERR | FLASH_FLAG_WRPERR | FLASH_FLAG_EOP |FLASH_FLAG_EVERR)
  281. /**
  282. * @}
  283. */
  284. /** @addtogroup FLASH_Exported_Functions
  285. * @{
  286. */
  287. /*------------ Functions used for N32G45X devices -----*/
  288. void FLASH_SetLatency(uint32_t FLASH_Latency);
  289. void FLASH_PrefetchBufSet(uint32_t FLASH_PrefetchBuf);
  290. void FLASH_iCacheRST(void);
  291. void FLASH_iCacheCmd(uint32_t FLASH_iCache);
  292. void FLASH_Unlock(void);
  293. void FLASH_Lock(void);
  294. FLASH_STS FLASH_EraseOnePage(uint32_t Page_Address);
  295. FLASH_STS FLASH_MassErase(void);
  296. FLASH_STS FLASH_EraseOB(void);
  297. FLASH_STS FLASH_ProgramWord(uint32_t Address, uint32_t Data);
  298. FLASH_STS FLASH_ProgramOBData(uint32_t Address, uint32_t Data);
  299. FLASH_STS FLASH_EnWriteProtection(uint32_t FLASH_Pages);
  300. FLASH_STS FLASH_ReadOutProtectionL1(FunctionalState Cmd);
  301. FLASH_STS FLASH_ReadOutProtectionL2_ENABLE(void);
  302. FLASH_STS FLASH_ConfigUserOB(uint16_t OB_IWDG, uint16_t OB_STOP, uint16_t OB_STDBY);
  303. uint32_t FLASH_GetUserOB(void);
  304. uint32_t FLASH_GetWriteProtectionOB(void);
  305. FlagStatus FLASH_GetReadOutProtectionSTS(void);
  306. FlagStatus FLASH_GetReadOutProtectionL2STS(void);
  307. FlagStatus FLASH_GetPrefetchBufSTS(void);
  308. void FLASH_SetSMPSELStatus(uint32_t FLASH_smpsel);
  309. FLASH_SMPSEL FLASH_GetSMPSELStatus(void);
  310. void FLASH_INTConfig(uint32_t FLASH_INT, FunctionalState Cmd);
  311. FlagStatus FLASH_GetFlagSTS(uint32_t FLASH_FLAG);
  312. void FLASH_ClearFlag(uint32_t FLASH_FLAG);
  313. FLASH_STS FLASH_GetSTS(void);
  314. FLASH_STS FLASH_WaitForLastOpt(uint32_t Timeout);
  315. #ifdef __cplusplus
  316. }
  317. #endif
  318. #endif /* __N32G45X_FLASH_H__ */
  319. /**
  320. * @}
  321. */
  322. /**
  323. * @}
  324. */
  325. /**
  326. * @}
  327. */