gd32f3x0_fmc.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*!
  2. \file gd32f3x0_fmc.h
  3. \brief definitions for the FMC
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-06-06, V1.0.0, firmware for GD32F3x0
  8. */
  9. #ifndef GD32F3X0_FMC_H
  10. #define GD32F3X0_FMC_H
  11. #include "gd32f3x0.h"
  12. /* FMC and option byte definition */
  13. #define FMC FMC_BASE /*!< FMC register base address */
  14. #define OB OB_BASE /*!< option byte base address */
  15. /* registers definitions */
  16. #define FMC_WS REG32((FMC) + 0x00U) /*!< FMC wait state register */
  17. #define FMC_KEY REG32((FMC) + 0x04U) /*!< FMC unlock key register */
  18. #define FMC_OBKEY REG32((FMC) + 0x08U) /*!< FMC option bytes unlock key register */
  19. #define FMC_STAT REG32((FMC) + 0x0CU) /*!< FMC status register */
  20. #define FMC_CTL REG32((FMC) + 0x10U) /*!< FMC control register */
  21. #define FMC_ADDR REG32((FMC) + 0x14U) /*!< FMC address register */
  22. #define FMC_OBSTAT REG32((FMC) + 0x1CU) /*!< FMC option bytes status register */
  23. #define FMC_WP REG32((FMC) + 0x20U) /*!< FMC write protection register */
  24. #define FMC_WSEN REG32((FMC) + 0xFCU) /*!< FMC wait state enable register */
  25. #define FMC_PID REG32((FMC) + 0x100U) /*!< FMC product ID register */
  26. #define OB_SPC REG16((OB) + 0x00U) /*!< option byte security protection value */
  27. #define OB_USER REG16((OB) + 0x02U) /*!< option byte user value*/
  28. #define OB_WP0 REG16((OB) + 0x08U) /*!< option byte write protection 0 */
  29. #define OB_WP1 REG16((OB) + 0x0AU) /*!< option byte write protection 1 */
  30. /* bits definitions */
  31. /* FMC_WS */
  32. #define FMC_WS_WSCNT BITS(0,2) /*!< wait state counter */
  33. /* FMC_KEY */
  34. #define FMC_KEY_KEY BITS(0,31) /*!< FMC main flash unlock key bits */
  35. /* FMC_OBKEY */
  36. #define FMC_OBKEY_OBKEY BITS(0,31) /*!< option bytes unlock key bits */
  37. /* FMC_STAT */
  38. #define FMC_STAT_BUSY BIT(0) /*!< flash busy flag bit */
  39. #define FMC_STAT_PGERR BIT(2) /*!< flash program error flag bit */
  40. #define FMC_STAT_WPERR BIT(4) /*!< flash write protection error flag bit */
  41. #define FMC_STAT_ENDF BIT(5) /*!< end of operation flag bit */
  42. /* FMC_CTL */
  43. #define FMC_CTL_PG BIT(0) /*!< main flash program command bit */
  44. #define FMC_CTL_PER BIT(1) /*!< main flash page erase bit */
  45. #define FMC_CTL_MER BIT(2) /*!< main flash mass erase bit */
  46. #define FMC_CTL_OBPG BIT(4) /*!< option bytes program command bit */
  47. #define FMC_CTL_OBER BIT(5) /*!< option bytes erase command bit */
  48. #define FMC_CTL_START BIT(6) /*!< send erase command to FMC bit */
  49. #define FMC_CTL_LK BIT(7) /*!< flash lock bit */
  50. #define FMC_CTL_OBWEN BIT(9) /*!< option bytes erase/program enable bit */
  51. #define FMC_CTL_ERRIE BIT(10) /*!< error interrupt enable bit */
  52. #define FMC_CTL_ENDIE BIT(12) /*!< end of operation interrupt enable bit */
  53. #define FMC_CTL_OBRLD BIT(13) /*!< option bytes reload bit */
  54. /* FMC_ADDR */
  55. #define FMC_ADDR_ADDR BITS(0,31) /*!< flash command address bits */
  56. /* FMC_OBSTAT */
  57. #define FMC_OBSTAT_OBERR BIT(0) /*!< option bytes read error bit */
  58. #define FMC_OBSTAT_PLVL_BIT0 BIT(1) /*!< protection level bit 0 */
  59. #define FMC_OBSTAT_PLVL_BIT1 BIT(2) /*!< protection level bit 1 */
  60. #define FMC_OBSTAT_USER BITS(8,15) /*!< option bytes user bits */
  61. #define FMC_OBSTAT_DATA BITS(16,31) /*!< option byte data bits */
  62. /* FMC_WSEN */
  63. #define FMC_WSEN_WSEN BIT(0) /*!< FMC wait state enable bit */
  64. #define FMC_WSEN_BPEN BIT(1) /*!< FMC bit program enable bit */
  65. /* FMC_PID */
  66. #define FMC_PID_PID BITS(0,31) /*!< product ID bits */
  67. /* constants definitions */
  68. /* fmc state */
  69. typedef enum
  70. {
  71. FMC_READY, /*!< the operation has been completed */
  72. FMC_BUSY, /*!< the operation is in progress */
  73. FMC_PGERR, /*!< program error */
  74. FMC_WPERR, /*!< erase/program protection error */
  75. FMC_TOERR, /*!< timeout error */
  76. FMC_OB_HSPC /*!< option byte security protection code high */
  77. }fmc_state_enum;
  78. /* unlock key */
  79. #define UNLOCK_KEY0 ((uint32_t)0x45670123U) /*!< unlock key 0 */
  80. #define UNLOCK_KEY1 ((uint32_t)0xCDEF89ABU) /*!< unlock key 1 */
  81. /* wait state counter value */
  82. #define WS_WSCNT_0 ((uint8_t)0x00U) /*!< 0 wait state added */
  83. #define WS_WSCNT_1 ((uint8_t)0x01U) /*!< 1 wait state added */
  84. #define WS_WSCNT_2 ((uint8_t)0x02U) /*!< 2 wait state added */
  85. /* read protect configure */
  86. #define FMC_NSPC ((uint8_t)0xA5U) /*!< no security protection */
  87. #define FMC_LSPC ((uint8_t)0xBBU) /*!< low security protection, any value except 0xA5 or 0xCC */
  88. #define FMC_HSPC ((uint8_t)0xCCU) /*!< high security protection */
  89. /* option byte write protection */
  90. #define OB_LWP ((uint32_t)0x000000FFU) /*!< write protection low bits */
  91. #define OB_HWP ((uint32_t)0x0000FF00U) /*!< write protection high bits */
  92. /* option byte software/hardware free watchdog timer */
  93. #define OBUSER_NWDG_HW(regval) (BIT(0) & ((uint32_t)(regval) << 0))
  94. #define OB_FWDGT_HW OBUSER_NWDG_HW(0) /*!< hardware free watchdog timer */
  95. #define OB_FWDGT_SW OBUSER_NWDG_HW(1) /*!< software free watchdog timer */
  96. /* option byte reset or not entering deep sleep mode */
  97. #define OBUSER_NRST_DPSLP(regval) (BIT(1) & ((uint32_t)(regval) << 1))
  98. #define OB_DEEPSLEEP_RST OBUSER_NRST_DPSLP(0) /*!< generate a reset instead of entering deepsleep mode */
  99. #define OB_DEEPSLEEP_NRST OBUSER_NRST_DPSLP(1) /*!< no reset when entering deepsleep mode */
  100. /* option byte reset or not entering standby mode */
  101. #define OBUSER_NRST_STDBY(regval) (BIT(2) & ((uint32_t)(regval) << 2))
  102. #define OB_STDBY_RST OBUSER_NRST_STDBY(0) /*!< generate a reset instead of entering standby mode */
  103. #define OB_STDBY_NRST OBUSER_NRST_STDBY(1) /*!< no reset when entering deepsleep mode */
  104. /* option byte OB_BOOT1_n set */
  105. #define OBUSER_BOOT1_N(regval) (BIT(4) & ((uint32_t)(regval) << 4))
  106. #define OB_BOOT1_SET_1 OBUSER_BOOT1_N(0) /*!< BOOT1 bit is 1 */
  107. #define OB_BOOT1_SET_0 OBUSER_BOOT1_N(1) /*!< BOOT1 bit is 0 */
  108. /* option byte VDDA monitor enable/disable */
  109. #define OBUSER_VDDA_VISOR(regval) (BIT(5) & ((uint32_t)(regval) << 5))
  110. #define OB_VDDA_DISABLE OBUSER_VDDA_VISOR(0) /*!< disable VDDA monitor */
  111. #define OB_VDDA_ENABLE OBUSER_VDDA_VISOR(1) /*!< enable VDDA monitor */
  112. /* option byte SRAM parity enable/disable */
  113. #define OBUSER_SRAM_PARITY(regval) (BIT(6) & ((uint32_t)(regval) << 6))
  114. #define OB_SRAM_PARITY_ENABLE OBUSER_SRAM_PARITY(0) /*!< enable SRAM parity check */
  115. #define OB_SRAM_PARITY_DISABLE OBUSER_SRAM_PARITY(1) /*!< disable SRAM parity check */
  116. /* option byte security protection level in FMC_OBSTAT register */
  117. #define OB_OBSTAT_PLEVEL_NO ((uint32_t)0x00000000U) /*!< no security protection */
  118. #define OB_OBSTAT_PLEVEL_LOW ((uint32_t)0x00000002U) /*!< low security protection */
  119. #define OB_OBSTAT_PLEVEL_HIGH ((uint32_t)0x00000006U) /*!< high security protection */
  120. /* option byte user mask */
  121. #define OB_USER_MASK ((uint8_t)0x88U) /*!< OB_USER reserved bit mask */
  122. /* option byte data address */
  123. #define OB_DATA_ADDR0 ((uint32_t)0x1FFFF804U) /*!< option byte data address 0 */
  124. #define OB_DATA_ADDR1 ((uint32_t)0x1FFFF806U) /*!< option byte data address 1 */
  125. /* FMC flags */
  126. #define FMC_FLAG_BUSY FMC_STAT_BUSY /*!< FMC busy flag */
  127. #define FMC_FLAG_PGERR FMC_STAT_PGERR /*!< FMC programming error flag */
  128. #define FMC_FLAG_WPERR FMC_STAT_WPERR /*!< FMC write protection error flag */
  129. #define FMC_FLAG_END FMC_STAT_ENDF /*!< FMC end of programming flag */
  130. /* FMC interrupt enable */
  131. #define FMC_INTEN_END FMC_CTL_ENDIE /*!< enable FMC end of operation interrupt */
  132. #define FMC_INTEN_ERR FMC_CTL_ERRIE /*!< enable FMC error interrupt */
  133. /* FMC time out */
  134. #define FMC_TIMEOUT_COUNT ((uint32_t)0x000F0000U) /*!< count to judge of FMC timeout */
  135. /* function declarations */
  136. /* FMC main memory programming functions */
  137. /* unlock the main FMC operation */
  138. void fmc_unlock(void);
  139. /* lock the main FMC operation */
  140. void fmc_lock(void);
  141. /* set the wait state counter value */
  142. void fmc_wscnt_set(uint8_t wscnt);
  143. /* fmc wait state enable */
  144. void fmc_wait_state_enable(void);
  145. /* fmc wait state disable */
  146. void fmc_wait_state_disable(void);
  147. /* FMC erase page */
  148. fmc_state_enum fmc_page_erase(uint32_t page_address);
  149. /* FMC erase whole chip */
  150. fmc_state_enum fmc_mass_erase(void);
  151. /* FMC program a word at the corresponding address */
  152. fmc_state_enum fmc_word_program(uint32_t address, uint32_t data);
  153. /* FMC program a half word at the corresponding address */
  154. fmc_state_enum fmc_halfword_program(uint32_t address, uint16_t data);
  155. /* FMC program a word at the corresponding address without erasing */
  156. fmc_state_enum fmc_word_reprogram(uint32_t address, uint32_t data);
  157. /* FMC option bytes programming functions */
  158. /* unlock the option byte operation */
  159. void ob_unlock(void);
  160. /* lock the option byte operation */
  161. void ob_lock(void);
  162. /* reload the option byte and generate a system reset */
  163. void ob_reset(void);
  164. /* erase option byte */
  165. fmc_state_enum ob_erase(void);
  166. /* enable option byte write protection (OB_WP) */
  167. fmc_state_enum ob_write_protection_enable(uint32_t ob_wp);
  168. /* configure read out protect */
  169. fmc_state_enum ob_security_protection_config(uint8_t ob_spc);
  170. /* write the FMC option byte user */
  171. fmc_state_enum ob_user_write(uint8_t ob_user);
  172. /* write the FMC option byte data */
  173. fmc_state_enum ob_data_program(uint32_t address, uint8_t data);
  174. /* get the FMC option byte OB_USER */
  175. uint8_t ob_user_get(void);
  176. /* get the FMC option byte OB_DATA */
  177. uint16_t ob_data_get(void);
  178. /* get the FMC option byte write protection */
  179. uint16_t ob_write_protection_get(void);
  180. /* get the value of FMC option byte security protection level (PLEVEL) in FMC_OBSTAT register */
  181. uint32_t ob_obstat_plevel_get(void);
  182. /* FMC interrupts and flags management functions */
  183. /* enable FMC interrupt */
  184. void fmc_interrupt_enable(uint32_t interrupt);
  185. /* disable FMC interrupt */
  186. void fmc_interrupt_disable(uint32_t interrupt);
  187. /* get flag set or reset */
  188. FlagStatus fmc_flag_get(uint32_t flag);
  189. /* clear the FMC pending flag */
  190. void fmc_flag_clear(uint32_t flag);
  191. /* return the FMC state */
  192. fmc_state_enum fmc_state_get(void);
  193. /* check FMC ready or not */
  194. fmc_state_enum fmc_ready_wait(uint32_t timeout);
  195. #endif /* GD32F3X0_FMC_H */