gd32e23x_fmc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*!
  2. \file gd32e23x_fmc.h
  3. \brief definitions for the FMC
  4. \version 2019-02-19, V1.0.0, firmware for GD32E23x
  5. \version 2020-12-12, V1.1.0, firmware for GD32E23x
  6. */
  7. /*
  8. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  9. Redistribution and use in source and binary forms, with or without modification,
  10. are permitted provided that the following conditions are met:
  11. 1. Redistributions of source code must retain the above copyright notice, this
  12. list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. 3. Neither the name of the copyright holder nor the names of its contributors
  17. may be used to endorse or promote products derived from this software without
  18. specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. OF SUCH DAMAGE.
  29. */
  30. #ifndef GD32E23X_FMC_H
  31. #define GD32E23X_FMC_H
  32. #include "gd32e23x.h"
  33. /* FMC and option byte definition */
  34. #define FMC FMC_BASE /*!< FMC register base address */
  35. #define OB OB_BASE /*!< option byte base address */
  36. /* registers definitions */
  37. #define FMC_WS REG32((FMC) + 0x00U) /*!< FMC wait state register */
  38. #define FMC_KEY REG32((FMC) + 0x04U) /*!< FMC unlock key register */
  39. #define FMC_OBKEY REG32((FMC) + 0x08U) /*!< FMC option bytes unlock key register */
  40. #define FMC_STAT REG32((FMC) + 0x0CU) /*!< FMC status register */
  41. #define FMC_CTL REG32((FMC) + 0x10U) /*!< FMC control register */
  42. #define FMC_ADDR REG32((FMC) + 0x14U) /*!< FMC address register */
  43. #define FMC_OBSTAT REG32((FMC) + 0x1CU) /*!< FMC option bytes status register */
  44. #define FMC_WP REG32((FMC) + 0x20U) /*!< FMC write protection register */
  45. #define FMC_PID REG32((FMC) + 0x100U) /*!< FMC product ID register */
  46. #define OB_SPC_USER REG32((OB) + 0x00U) /*!< option byte security protection value and user value */
  47. #define OB_DATA REG32((OB) + 0x04U) /*!< option byte data value*/
  48. #define OB_WP REG32((OB) + 0x08U) /*!< option byte write protection */
  49. /* bits definitions */
  50. /* FMC_WS */
  51. #define FMC_WS_WSCNT BITS(0,2) /*!< wait state counter */
  52. #define FMC_WS_PFEN BIT(4) /*!< pre-fetch enable */
  53. #define FMC_WS_PGW BIT(15) /*!< program width to flash memory */
  54. /* FMC_KEY */
  55. #define FMC_KEY_KEY BITS(0,31) /*!< FMC main flash unlock key bits */
  56. /* FMC_OBKEY */
  57. #define FMC_OBKEY_OBKEY BITS(0,31) /*!< option bytes unlock key bits */
  58. /* FMC_STAT */
  59. #define FMC_STAT_BUSY BIT(0) /*!< flash busy flag bit */
  60. #define FMC_STAT_PGERR BIT(2) /*!< flash program error flag bit */
  61. #define FMC_STAT_PGAERR BIT(3) /*!< program alignment error flag bit */
  62. #define FMC_STAT_WPERR BIT(4) /*!< flash write protection error flag bit */
  63. #define FMC_STAT_ENDF BIT(5) /*!< end of operation flag bit */
  64. /* FMC_CTL */
  65. #define FMC_CTL_PG BIT(0) /*!< main flash program command bit */
  66. #define FMC_CTL_PER BIT(1) /*!< main flash page erase bit */
  67. #define FMC_CTL_MER BIT(2) /*!< main flash mass erase bit */
  68. #define FMC_CTL_OBPG BIT(4) /*!< option bytes program command bit */
  69. #define FMC_CTL_OBER BIT(5) /*!< option bytes erase command bit */
  70. #define FMC_CTL_START BIT(6) /*!< send erase command to FMC bit */
  71. #define FMC_CTL_LK BIT(7) /*!< flash lock bit */
  72. #define FMC_CTL_OBWEN BIT(9) /*!< option bytes erase/program enable bit */
  73. #define FMC_CTL_ERRIE BIT(10) /*!< error interrupt enable bit */
  74. #define FMC_CTL_ENDIE BIT(12) /*!< end of operation interrupt enable bit */
  75. #define FMC_CTL_OBRLD BIT(13) /*!< option bytes reload bit */
  76. /* FMC_ADDR */
  77. #define FMC_ADDR_ADDR BITS(0,31) /*!< flash command address bits */
  78. /* FMC_OBSTAT */
  79. #define FMC_OBSTAT_OBERR BIT(0) /*!< option bytes read error bit */
  80. #define FMC_OBSTAT_PLEVEL_BIT0 BIT(1) /*!< protection level bit 0 */
  81. #define FMC_OBSTAT_PLEVEL_BIT1 BIT(2) /*!< protection level bit 1 */
  82. #define FMC_OBSTAT_USER BITS(8,15) /*!< option bytes user bits */
  83. #define FMC_OBSTAT_DATA BITS(16,31) /*!< option byte data bits */
  84. /* FMC_WP */
  85. #define FMC_WP_WP BITS(0,15) /*!< store WP[15:0] of option byte block after system reset */
  86. /* FMC_PID */
  87. #define FMC_PID_PID BITS(0,31) /*!< product ID bits */
  88. /* constants definitions */
  89. /* fmc state */
  90. typedef enum
  91. {
  92. FMC_READY, /*!< the operation has been completed */
  93. FMC_BUSY, /*!< the operation is in progress */
  94. FMC_PGERR, /*!< program error */
  95. FMC_PGAERR, /*!< program alignment error */
  96. FMC_WPERR, /*!< erase/program protection error */
  97. FMC_TOERR, /*!< timeout error */
  98. FMC_OB_HSPC /*!< option byte security protection code high */
  99. }fmc_state_enum;
  100. /* unlock key */
  101. #define UNLOCK_KEY0 ((uint32_t)0x45670123U) /*!< unlock key 0 */
  102. #define UNLOCK_KEY1 ((uint32_t)0xCDEF89ABU) /*!< unlock key 1 */
  103. /* wait state counter value */
  104. #define WS_WSCNT_0 ((uint8_t)0x00U) /*!< 0 wait state added */
  105. #define WS_WSCNT_1 ((uint8_t)0x01U) /*!< 1 wait state added */
  106. #define WS_WSCNT_2 ((uint8_t)0x02U) /*!< 2 wait state added */
  107. /* read protect configure */
  108. #define FMC_NSPC ((uint16_t)0x5AA5U) /*!< no security protection */
  109. #define FMC_LSPC ((uint16_t)0x44BBU) /*!< low security protection, any value except 0xA5 or 0xCC */
  110. #define FMC_HSPC ((uint16_t)0x33CCU) /*!< high security protection */
  111. #define LOW_16BITS_MASK ((uint32_t)0x0000FFFFU) /*!< low 16 bits mask */
  112. #define HIGH_16BITS_MASK ((uint32_t)0xFFFF0000U) /*!< high 16 bits mask */
  113. /* option byte address */
  114. #define OB_SPC_USER_ADDRESS ((uint32_t)0x1FFFF800U) /*!< address of option byte security protection and user */
  115. #define OB_DATA_ADDRESS ((uint32_t)0x1FFFF804U) /*!< address of option byte data */
  116. #define OB_WP_ADDRESS ((uint32_t)0x1FFFF808U) /*!< address of option byte write protection */
  117. /* option byte write protection */
  118. #define OB_LWP ((uint32_t)0x000000FFU) /*!< write protection low bits */
  119. #define OB_HWP ((uint32_t)0x0000FF00U) /*!< write protection high bits */
  120. /* option byte software/hardware free watchdog timer */
  121. #define OB_FWDGT_HW ((uint8_t)(~BIT(0))) /*!< hardware free watchdog timer */
  122. #define OB_FWDGT_SW ((uint8_t)BIT(0)) /*!< software free watchdog timer */
  123. /* option byte reset or not entering deep sleep mode */
  124. #define OB_DEEPSLEEP_RST ((uint8_t)(~BIT(1))) /*!< generate a reset instead of entering deepsleep mode */
  125. #define OB_DEEPSLEEP_NRST ((uint8_t)BIT(1)) /*!< no reset when entering deepsleep mode */
  126. /* option byte reset or not entering standby mode */
  127. #define OB_STDBY_RST ((uint8_t)(~BIT(2))) /*!< generate a reset instead of entering standby mode */
  128. #define OB_STDBY_NRST ((uint8_t)BIT(2)) /*!< no reset when entering deepsleep mode */
  129. /* option byte OB_BOOT1_n set */
  130. #define OB_BOOT1_SET_1 ((uint8_t)(~BIT(4))) /*!< BOOT1 bit is 1 */
  131. #define OB_BOOT1_SET_0 ((uint8_t)BIT(4)) /*!< BOOT1 bit is 0 */
  132. /* option byte VDDA monitor enable/disable */
  133. #define OB_VDDA_DISABLE ((uint8_t)(~BIT(5))) /*!< disable VDDA monitor */
  134. #define OB_VDDA_ENABLE ((uint8_t)BIT(5)) /*!< enable VDDA monitor */
  135. /* option byte SRAM parity enable/disable */
  136. #define OB_SRAM_PARITY_ENABLE ((uint8_t)(~BIT(6))) /*!< enable SRAM parity check */
  137. #define OB_SRAM_PARITY_DISABLE ((uint8_t)BIT(6)) /*!< disable SRAM parity check */
  138. /* option byte security protection level in FMC_OBSTAT register */
  139. #define OB_OBSTAT_PLEVEL_NO ((uint8_t)0x00U) /*!< no security protection */
  140. #define OB_OBSTAT_PLEVEL_LOW ((uint8_t)0x02U) /*!< low security protection */
  141. #define OB_OBSTAT_PLEVEL_HIGH ((uint8_t)0x06U) /*!< high security protection */
  142. /* option byte user mask */
  143. #define OB_USER_MASK ((uint8_t)0x88U) /*!< OB_USER reserved bit mask */
  144. /* option byte data address */
  145. #define OB_DATA_ADDR0 ((uint32_t)0x1FFFF804U) /*!< option byte data address 0 */
  146. #define OB_DATA_ADDR1 ((uint32_t)0x1FFFF806U) /*!< option byte data address 1 */
  147. /* FMC flags */
  148. #define FMC_FLAG_BUSY FMC_STAT_BUSY /*!< FMC busy flag */
  149. #define FMC_FLAG_PGERR FMC_STAT_PGERR /*!< FMC programming error flag */
  150. #define FMC_FLAG_PGAERR FMC_STAT_PGAERR /*!< FMC program alignment 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 flags */
  154. #define FMC_INT_FLAG_PGERR FMC_STAT_PGERR /*!< FMC programming error flag */
  155. #define FMC_INT_FLAG_PGAERR FMC_STAT_PGAERR /*!< FMC program alignment error flag */
  156. #define FMC_INT_FLAG_WPERR FMC_STAT_WPERR /*!< FMC write protection error flag */
  157. #define FMC_INT_FLAG_END FMC_STAT_ENDF /*!< FMC end of programming flag */
  158. /* FMC interrupt enable */
  159. #define FMC_INTEN_END FMC_CTL_ENDIE /*!< enable FMC end of operation interrupt */
  160. #define FMC_INTEN_ERR FMC_CTL_ERRIE /*!< enable FMC error interrupt */
  161. /* FMC time out */
  162. #define FMC_TIMEOUT_COUNT ((uint32_t)0x000F0000U) /*!< count to judge of FMC timeout */
  163. /* function declarations */
  164. /* FMC main memory programming functions */
  165. /* unlock the main FMC operation */
  166. void fmc_unlock(void);
  167. /* lock the main FMC operation */
  168. void fmc_lock(void);
  169. /* set the wait state counter value */
  170. void fmc_wscnt_set(uint8_t wscnt);
  171. /* pre-fetch enable */
  172. void fmc_prefetch_enable(void);
  173. /* pre-fetch disable */
  174. void fmc_prefetch_disable(void);
  175. /* FMC erase page */
  176. fmc_state_enum fmc_page_erase(uint32_t page_address);
  177. /* FMC erase whole chip */
  178. fmc_state_enum fmc_mass_erase(void);
  179. /* FMC program a double word at the corresponding address */
  180. fmc_state_enum fmc_doubleword_program(uint32_t address, uint64_t data);
  181. /* FMC program a word at the corresponding address */
  182. fmc_state_enum fmc_word_program(uint32_t address, uint32_t data);
  183. /* FMC option bytes programming functions */
  184. /* unlock the option byte operation */
  185. void ob_unlock(void);
  186. /* lock the option byte operation */
  187. void ob_lock(void);
  188. /* reload the option byte and generate a system reset */
  189. void ob_reset(void);
  190. /* get option byte value */
  191. uint32_t option_byte_value_get(uint32_t addr);
  192. /* erase option byte */
  193. fmc_state_enum ob_erase(void);
  194. /* enable option byte write protection (OB_WP) */
  195. fmc_state_enum ob_write_protection_enable(uint16_t ob_wp);
  196. /* configure read out protect */
  197. fmc_state_enum ob_security_protection_config(uint16_t ob_spc);
  198. /* write the FMC option byte user */
  199. fmc_state_enum ob_user_write(uint8_t ob_user);
  200. /* write the FMC option byte data */
  201. fmc_state_enum ob_data_program(uint16_t data);
  202. /* get the FMC option byte OB_USER */
  203. uint8_t ob_user_get(void);
  204. /* get the FMC option byte OB_DATA */
  205. uint16_t ob_data_get(void);
  206. /* get the FMC option byte write protection */
  207. uint16_t ob_write_protection_get(void);
  208. /* get the value of FMC option byte security protection level (PLEVEL) in FMC_OBSTAT register */
  209. uint32_t ob_obstat_plevel_get(void);
  210. /* FMC interrupts and flags management functions */
  211. /* enable FMC interrupt */
  212. void fmc_interrupt_enable(uint32_t interrupt);
  213. /* disable FMC interrupt */
  214. void fmc_interrupt_disable(uint32_t interrupt);
  215. /* get flag set or reset */
  216. FlagStatus fmc_flag_get(uint32_t flag);
  217. /* clear the FMC pending flag */
  218. void fmc_flag_clear(uint32_t flag);
  219. /* get intrrupt flag set or reset */
  220. FlagStatus fmc_interrupt_flag_get(uint32_t int_flag);
  221. /* clear the FMC interrupt pending flag by writing 1 */
  222. void fmc_interrupt_flag_clear(uint32_t int_flag);
  223. /* return the FMC state */
  224. fmc_state_enum fmc_state_get(void);
  225. /* check FMC ready or not */
  226. fmc_state_enum fmc_ready_wait(uint32_t timeout);
  227. #endif /* GD32E23X_FMC_H */