gd32f1x0_fmc.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*!
  2. \file gd32f1x0_fmc.h
  3. \brief definitions for the FMC
  4. \version 2014-12-26, V1.0.0, platform GD32F1x0(x=3,5)
  5. \version 2016-01-15, V2.0.0, platform GD32F1x0(x=3,5,7,9)
  6. \version 2016-04-30, V3.0.0, firmware update for GD32F1x0(x=3,5,7,9)
  7. \version 2017-06-19, V3.1.0, firmware update for GD32F1x0(x=3,5,7,9)
  8. \version 2019-11-20, V3.2.0, firmware update for GD32F1x0(x=3,5,7,9)
  9. */
  10. /*
  11. Copyright (c) 2019, GigaDevice Semiconductor Inc.
  12. Redistribution and use in source and binary forms, with or without modification,
  13. are permitted provided that the following conditions are met:
  14. 1. Redistributions of source code must retain the above copyright notice, this
  15. list of conditions and the following disclaimer.
  16. 2. Redistributions in binary form must reproduce the above copyright notice,
  17. this list of conditions and the following disclaimer in the documentation
  18. and/or other materials provided with the distribution.
  19. 3. Neither the name of the copyright holder nor the names of its contributors
  20. may be used to endorse or promote products derived from this software without
  21. specific prior written permission.
  22. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  25. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  26. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  27. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  31. OF SUCH DAMAGE.
  32. */
  33. #ifndef GD32F1X0_FMC_H
  34. #define GD32F1X0_FMC_H
  35. #include "gd32f1x0.h"
  36. /* FMC and option byte definition */
  37. #define FMC FMC_BASE /*!< FMC register base address */
  38. #define OB OB_BASE /*!< option byte base address */
  39. /* registers definitions */
  40. #define FMC_WS REG32((FMC) + 0x00U) /*!< FMC wait state register */
  41. #define FMC_KEY REG32((FMC) + 0x04U) /*!< FMC unlock key register */
  42. #define FMC_OBKEY REG32((FMC) + 0x08U) /*!< FMC option bytes unlock key register */
  43. #define FMC_STAT REG32((FMC) + 0x0CU) /*!< FMC status register */
  44. #define FMC_CTL REG32((FMC) + 0x10U) /*!< FMC control register */
  45. #define FMC_ADDR REG32((FMC) + 0x14U) /*!< FMC address register */
  46. #define FMC_OBSTAT REG32((FMC) + 0x1CU) /*!< FMC option bytes status register */
  47. #define FMC_WP REG32((FMC) + 0x20U) /*!< FMC write protection register */
  48. #define FMC_WSEN REG32((FMC) + 0xFCU) /*!< FMC wait state enable register */
  49. #define FMC_PID REG32((FMC) + 0x100U) /*!< FMC product ID register */
  50. #define OP_BYTE(x) REG8((OB) + ((uint32_t)((uint32_t)0x02U * (x)))) /*!< option byte value */
  51. #define OB_SPC REG8((OB) + 0x00U) /*!< option byte security protection value */
  52. #define OB_USER REG8((OB) + 0x02U) /*!< option byte user value */
  53. #define OB_DATA0 REG8((OB) + 0x04U) /*!< option byte data0 value */
  54. #define OB_DATA1 REG8((OB) + 0x06U) /*!< option byte data1 value */
  55. #define OB_WP0 REG8((OB) + 0x08U) /*!< option byte write protection 0 value */
  56. #define OB_WP1 REG8((OB) + 0x0AU) /*!< option byte write protection 1 value */
  57. /* bits definitions */
  58. /* FMC_WS */
  59. #define FMC_WS_WSCNT BITS(0,2) /*!< wait state counter */
  60. /* FMC_KEY */
  61. #define FMC_KEY_KEY BITS(0,31) /*!< FMC main flash unlock key bits */
  62. /* FMC_OBKEY */
  63. #define FMC_OBKEY_OBKEY BITS(0,31) /*!< option bytes unlock key bits */
  64. /* FMC_STAT */
  65. #define FMC_STAT_BUSY BIT(0) /*!< flash busy flag bit */
  66. #define FMC_STAT_PGERR BIT(2) /*!< flash program error flag bit */
  67. #define FMC_STAT_WPERR BIT(4) /*!< flash write protection error flag bit */
  68. #define FMC_STAT_ENDF BIT(5) /*!< flash end of operation flag bit */
  69. /* FMC_CTL */
  70. #define FMC_CTL_PG BIT(0) /*!< main flash program command bit */
  71. #define FMC_CTL_PER BIT(1) /*!< main flash page erase bit */
  72. #define FMC_CTL_MER BIT(2) /*!< main flash mass erase bit */
  73. #define FMC_CTL_OBPG BIT(4) /*!< option bytes program command bit */
  74. #define FMC_CTL_OBER BIT(5) /*!< option bytes erase command bit */
  75. #define FMC_CTL_START BIT(6) /*!< send erase command to FMC bit */
  76. #define FMC_CTL_LK BIT(7) /*!< flash lock bit */
  77. #define FMC_CTL_OBWEN BIT(9) /*!< option bytes erase/program enable bit */
  78. #define FMC_CTL_ERRIE BIT(10) /*!< error interrupt enable bit */
  79. #define FMC_CTL_ENDIE BIT(12) /*!< end of operation interrupt enable bit */
  80. #define FMC_CTL_OBRLD BIT(13) /*!< option bytes reload bit */
  81. /* FMC_ADDR */
  82. #define FMC_ADDR_ADDR BITS(0,31) /*!< flash command address bits */
  83. /* FMC_OBSTAT */
  84. #define FMC_OBSTAT_OBERR BIT(0) /*!< option bytes read error bit */
  85. #define FMC_OBSTAT_PLVL_BIT0 BIT(1) /*!< protection level bit 0 */
  86. #define FMC_OBSTAT_PLVL_BIT1 BIT(2) /*!< protection level bit 1 */
  87. #define FMC_OBSTAT_USER BITS(8,15) /*!< option bytes user bits */
  88. #define FMC_OBSTAT_DATA BITS(16,31) /*!< option byte data bits */
  89. /* FMC_WSEN */
  90. #define FMC_WSEN_WSEN BIT(0) /*!< FMC wait state enable bit */
  91. #ifdef GD32F170_190
  92. #define FMC_WSEN_BPEN BIT(1) /*!< FMC bit program enable bit */
  93. #endif /* GD32F170_190 */
  94. /* FMC_PID */
  95. #define FMC_PID_PID BITS(0,31) /*!< product ID bits */
  96. /* constants definitions */
  97. /* fmc state */
  98. typedef enum
  99. {
  100. FMC_READY, /*!< the operation has been completed */
  101. FMC_BUSY, /*!< the operation is in progress */
  102. FMC_PGERR, /*!< program error */
  103. FMC_WPERR, /*!< erase/program protection error */
  104. FMC_TOERR, /*!< timeout error */
  105. FMC_OB_HSPC /*!< option byte security protection code high */
  106. }fmc_state_enum;
  107. /* unlock key */
  108. #define UNLOCK_KEY0 ((uint32_t)0x45670123U) /*!< unlock key 0 */
  109. #define UNLOCK_KEY1 ((uint32_t)0xCDEF89ABU) /*!< unlock key 1 */
  110. /* wait state counter value */
  111. #define WS_WSCNT_0 ((uint8_t)0x00U) /*!< 0 wait state added */
  112. #define WS_WSCNT_1 ((uint8_t)0x01U) /*!< 1 wait state added */
  113. #define WS_WSCNT_2 ((uint8_t)0x02U) /*!< 2 wait state added */
  114. /* read protect configure */
  115. #define FMC_NSPC ((uint8_t)0xA5U) /*!< no security protection */
  116. #define FMC_LSPC ((uint8_t)0xBBU) /*!< low security protection, any value except 0xA5 or 0xCC */
  117. #define FMC_HSPC ((uint8_t)0xCCU) /*!< high security protection */
  118. /* option byte write protection mask */
  119. #define OB_LWP ((uint16_t)0x00FFU) /*!< write protection low bits */
  120. #define OB_HWP ((uint16_t)0xFF00U) /*!< write protection high bits */
  121. /* option byte software/hardware free watchdog timer */
  122. #define OB_FWDGT_HW ((uint8_t)(~BIT(0))) /*!< hardware free watchdog timer */
  123. #define OB_FWDGT_SW ((uint8_t)BIT(0)) /*!< software free watchdog timer */
  124. /* option byte reset or not entering deep sleep mode */
  125. #define OB_DEEPSLEEP_RST ((uint8_t)(~BIT(1))) /*!< generate a reset instead of entering deepsleep mode */
  126. #define OB_DEEPSLEEP_NRST ((uint8_t)BIT(1)) /*!< no reset when entering deepsleep mode */
  127. /* option byte reset or not entering standby mode */
  128. #define OB_STDBY_RST ((uint8_t)(~BIT(2))) /*!< generate a reset instead of entering standby mode */
  129. #define OB_STDBY_NRST ((uint8_t)BIT(2)) /*!< no reset when entering standby mode */
  130. /* option byte OB_BOOT1_n set */
  131. #define OB_BOOT1_SET_1 ((uint8_t)(~BIT(4))) /*!< BOOT1 bit is 1 */
  132. #define OB_BOOT1_SET_0 ((uint8_t)BIT(4)) /*!< BOOT1 bit is 0 */
  133. /* option byte VDDA monitor enable/disable */
  134. #define OB_VDDA_DISABLE ((uint8_t)(~BIT(5))) /*!< disable VDDA monitor */
  135. #define OB_VDDA_ENABLE ((uint8_t)BIT(5)) /*!< enable VDDA monitor */
  136. /* option byte SRAM parity enable/disable */
  137. #define OB_SRAM_PARITY_ENABLE ((uint8_t)(~BIT(6))) /*!< enable SRAM parity check */
  138. #define OB_SRAM_PARITY_DISABLE ((uint8_t)BIT(6)) /*!< disable SRAM parity check */
  139. /* option byte security protection level in FMC_OBSTAT register */
  140. #define OB_OBSTAT_PLEVEL_NO ((uint32_t)0x00000000U) /*!< no security protection */
  141. #define OB_OBSTAT_PLEVEL_LOW ((uint32_t)0x00000002U) /*!< low security protection */
  142. #define OB_OBSTAT_PLEVEL_HIGH ((uint32_t)0x00000006U) /*!< high security protection */
  143. /* option byte user mask */
  144. #define OB_USER_MASK ((uint8_t)0x88U) /*!< OB_USER reserved bit mask */
  145. /* option byte data mask */
  146. #define OB_LDATA ((uint16_t)0x00FFU) /*!< option byte data address 0 */
  147. #define OB_HDATA ((uint16_t)0xFF00U) /*!< option byte data address 1 */
  148. /* FMC flags */
  149. #define FMC_FLAG_BUSY FMC_STAT_BUSY /*!< FMC busy flag */
  150. #define FMC_FLAG_PGERR FMC_STAT_PGERR /*!< FMC programming error flag */
  151. #define FMC_FLAG_WPERR FMC_STAT_WPERR /*!< FMC write protection error flag */
  152. #define FMC_FLAG_END FMC_STAT_ENDF /*!< FMC end of programming flag */
  153. /* FMC interrupt enable */
  154. #define FMC_INT_END FMC_CTL_ENDIE /*!< enable FMC end of operation interrupt */
  155. #define FMC_INT_ERR FMC_CTL_ERRIE /*!< enable FMC error interrupt */
  156. /* FMC interrupt flags */
  157. #define FMC_INT_FLAG_PGERR FMC_STAT_PGERR /*!< FMC programming error interrupt flag */
  158. #define FMC_INT_FLAG_WPERR FMC_STAT_WPERR /*!< FMC write protection error interrupt flag */
  159. #define FMC_INT_FLAG_END FMC_STAT_ENDF /*!< FMC end of programming interrupt flag */
  160. /* option bytes write protection */
  161. #define OB_WP_NONE ((uint16_t)0x0000U) /*!< disable all erase/program protection */
  162. #define OB_WP_0 ((uint16_t)0x0001U) /*!< erase/program protection of sector 0 */
  163. #define OB_WP_1 ((uint16_t)0x0002U) /*!< erase/program protection of sector 1 */
  164. #define OB_WP_2 ((uint16_t)0x0004U) /*!< erase/program protection of sector 2 */
  165. #define OB_WP_3 ((uint16_t)0x0008U) /*!< erase/program protection of sector 3 */
  166. #define OB_WP_4 ((uint16_t)0x0010U) /*!< erase/program protection of sector 4 */
  167. #define OB_WP_5 ((uint16_t)0x0020U) /*!< erase/program protection of sector 5 */
  168. #define OB_WP_6 ((uint16_t)0x0040U) /*!< erase/program protection of sector 6 */
  169. #define OB_WP_7 ((uint16_t)0x0080U) /*!< erase/program protection of sector 7 */
  170. #define OB_WP_8 ((uint16_t)0x0100U) /*!< erase/program protection of sector 8 */
  171. #define OB_WP_9 ((uint16_t)0x0200U) /*!< erase/program protection of sector 9 */
  172. #define OB_WP_10 ((uint16_t)0x0400U) /*!< erase/program protection of sector 10 */
  173. #define OB_WP_11 ((uint16_t)0x0800U) /*!< erase/program protection of sector 11 */
  174. #define OB_WP_12 ((uint16_t)0x1000U) /*!< erase/program protection of sector 12 */
  175. #define OB_WP_13 ((uint16_t)0x2000U) /*!< erase/program protection of sector 13 */
  176. #define OB_WP_14 ((uint16_t)0x4000U) /*!< erase/program protection of sector 14 */
  177. #define OB_WP_15 ((uint16_t)0x8000U) /*!< erase/program protection of sector 15 */
  178. #define OB_WP_ALL ((uint16_t)0xFFFFU) /*!< erase/program protection of all sectors */
  179. /* FMC time out */
  180. #define FMC_TIMEOUT_COUNT ((uint32_t)0x000F0000U) /*!< count to judge of FMC timeout */
  181. /* function declarations */
  182. /* FMC main memory programming functions */
  183. /* unlock the main FMC operation */
  184. void fmc_unlock(void);
  185. /* lock the main FMC operation */
  186. void fmc_lock(void);
  187. /* set the wait state counter value */
  188. void fmc_wscnt_set(uint8_t wscnt);
  189. /* fmc wait state enable */
  190. void fmc_wait_state_enable(void);
  191. /* fmc wait state disable */
  192. void fmc_wait_state_disable(void);
  193. /* FMC erase page */
  194. fmc_state_enum fmc_page_erase(uint32_t page_address);
  195. /* FMC erase whole chip */
  196. fmc_state_enum fmc_mass_erase(void);
  197. /* FMC program a word at the corresponding address */
  198. fmc_state_enum fmc_word_program(uint32_t address, uint32_t data);
  199. /* FMC program a half word at the corresponding address */
  200. fmc_state_enum fmc_halfword_program(uint32_t address, uint16_t data);
  201. #ifdef GD32F170_190
  202. /* FMC program a word at the corresponding address without erasing */
  203. fmc_state_enum fmc_word_reprogram(uint32_t address, uint32_t data);
  204. #endif /* GD32F170_190 */
  205. /* FMC option bytes programming functions */
  206. /* unlock the option byte operation */
  207. void ob_unlock(void);
  208. /* lock the option byte operation */
  209. void ob_lock(void);
  210. /* reload the option byte and generate a system reset */
  211. void ob_reset(void);
  212. /* erase option byte */
  213. fmc_state_enum ob_erase(void);
  214. /* enable option byte write protection (OB_WP) */
  215. fmc_state_enum ob_write_protection_enable(uint16_t ob_wp);
  216. /* configure read out protect */
  217. fmc_state_enum ob_security_protection_config(uint8_t ob_spc);
  218. /* write the FMC option byte user */
  219. fmc_state_enum ob_user_write(uint8_t ob_user);
  220. /* write the FMC option byte data */
  221. fmc_state_enum ob_data_program(uint16_t ob_data);
  222. /* get the FMC option byte OB_USER */
  223. uint8_t ob_user_get(void);
  224. /* get the FMC option byte OB_DATA */
  225. uint16_t ob_data_get(void);
  226. /* get the FMC option byte write protection */
  227. uint16_t ob_write_protection_get(void);
  228. /* get the value of FMC option byte security protection level (PLEVEL) in FMC_OBSTAT register */
  229. uint32_t ob_obstat_plevel_get(void);
  230. /* FMC interrupts and flags management functions */
  231. /* enable FMC interrupt */
  232. void fmc_interrupt_enable(uint32_t interrupt);
  233. /* disable FMC interrupt */
  234. void fmc_interrupt_disable(uint32_t interrupt);
  235. /* get flag set or reset */
  236. FlagStatus fmc_flag_get(uint32_t flag);
  237. /* clear the FMC pending flag */
  238. void fmc_flag_clear(uint32_t flag);
  239. /* get FMC interrupt flag state */
  240. FlagStatus fmc_interrupt_flag_get(uint32_t flag);
  241. /* clear FMC interrupt flag state */
  242. void fmc_interrupt_flag_clear(uint32_t flag);
  243. #endif /* GD32F1X0_FMC_H */