gd32f4xx_syscfg.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*!
  2. \file gd32f4xx_syscfg.c
  3. \brief SYSCFG driver
  4. \version 2016-08-15, V1.0.0, firmware for GD32F4xx
  5. \version 2018-12-12, V2.0.0, firmware for GD32F4xx
  6. \version 2020-09-30, V2.1.0, firmware for GD32F4xx
  7. */
  8. /*
  9. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  10. Redistribution and use in source and binary forms, with or without modification,
  11. are permitted provided that the following conditions are met:
  12. 1. Redistributions of source code must retain the above copyright notice, this
  13. list of conditions and the following disclaimer.
  14. 2. Redistributions in binary form must reproduce the above copyright notice,
  15. this list of conditions and the following disclaimer in the documentation
  16. and/or other materials provided with the distribution.
  17. 3. Neither the name of the copyright holder nor the names of its contributors
  18. may be used to endorse or promote products derived from this software without
  19. specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  24. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  27. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  29. OF SUCH DAMAGE.
  30. */
  31. #include "gd32f4xx_syscfg.h"
  32. /*!
  33. \brief reset the SYSCFG registers
  34. \param[in] none
  35. \param[out] none
  36. \retval none
  37. */
  38. void syscfg_deinit(void)
  39. {
  40. rcu_periph_reset_enable(RCU_SYSCFGRST);
  41. rcu_periph_reset_disable(RCU_SYSCFGRST);
  42. }
  43. /*!
  44. \brief configure the boot mode
  45. \param[in] syscfg_bootmode: selects the memory remapping
  46. only one parameter can be selected which is shown as below:
  47. \arg SYSCFG_BOOTMODE_FLASH: main flash memory (0x08000000~0x083BFFFF) is mapped at address 0x00000000
  48. \arg SYSCFG_BOOTMODE_BOOTLOADER: boot loader (0x1FFF0000 - 0x1FFF77FF) is mapped at address 0x00000000
  49. \arg SYSCFG_BOOTMODE_EXMC_SRAM: SRAM/NOR 0 and 1 of EXMC (0x60000000~0x67FFFFFF) is mapped at address 0x00000000
  50. \arg SYSCFG_BOOTMODE_SRAM: SRAM0 of on-chip SRAM (0x20000000~0x2001BFFF) is mapped at address 0x00000000
  51. \arg SYSCFG_BOOTMODE_EXMC_SDRAM: SDRAM bank0 of EXMC (0xC0000000~0xC7FFFFFF) is mapped at address 0x00000000
  52. \param[out] none
  53. \retval none
  54. */
  55. void syscfg_bootmode_config(uint8_t syscfg_bootmode)
  56. {
  57. /* reset the SYSCFG_CFG0_BOOT_MODE bit and set according to syscfg_bootmode */
  58. SYSCFG_CFG0 &= ~SYSCFG_CFG0_BOOT_MODE;
  59. SYSCFG_CFG0 |= (uint32_t)syscfg_bootmode;
  60. }
  61. /*!
  62. \brief FMC memory mapping swap
  63. \param[in] syscfg_fmc_swap: selects the interal flash bank swapping
  64. only one parameter can be selected which is shown as below:
  65. \arg SYSCFG_FMC_SWP_BANK0: bank 0 is mapped at address 0x08000000 and bank 1 is mapped at address 0x08100000
  66. \arg SYSCFG_FMC_SWP_BANK1: bank 1 is mapped at address 0x08000000 and bank 0 is mapped at address 0x08100000
  67. \param[out] none
  68. \retval none
  69. */
  70. void syscfg_fmc_swap_config(uint32_t syscfg_fmc_swap)
  71. {
  72. uint32_t reg;
  73. reg = SYSCFG_CFG0;
  74. /* reset the FMC_SWP bit and set according to syscfg_fmc_swap */
  75. reg &= ~SYSCFG_CFG0_FMC_SWP;
  76. SYSCFG_CFG0 = (reg | syscfg_fmc_swap);
  77. }
  78. /*!
  79. \brief EXMC memory mapping swap
  80. \param[in] syscfg_exmc_swap: selects the memories in EXMC swapping
  81. only one parameter can be selected which is shown as below:
  82. \arg SYSCFG_EXMC_SWP_ENABLE: SDRAM bank 0 and bank 1 are swapped with NAND bank 1 and PC card
  83. \arg SYSCFG_EXMC_SWP_DISABLE: no memory mapping swap
  84. \param[out] none
  85. \retval none
  86. */
  87. void syscfg_exmc_swap_config(uint32_t syscfg_exmc_swap)
  88. {
  89. uint32_t reg;
  90. reg = SYSCFG_CFG0;
  91. /* reset the SYSCFG_CFG0_EXMC_SWP bits and set according to syscfg_exmc_swap */
  92. reg &= ~SYSCFG_CFG0_EXMC_SWP;
  93. SYSCFG_CFG0 = (reg | syscfg_exmc_swap);
  94. }
  95. /*!
  96. \brief configure the GPIO pin as EXTI Line
  97. \param[in] exti_port: specify the GPIO port used in EXTI
  98. only one parameter can be selected which is shown as below:
  99. \arg EXTI_SOURCE_GPIOx(x = A,B,C,D,E,F,G,H,I): EXTI GPIO port
  100. \param[in] exti_pin: specify the EXTI line
  101. only one parameter can be selected which is shown as below:
  102. \arg EXTI_SOURCE_PINx(x = 0..15): EXTI GPIO pin
  103. \param[out] none
  104. \retval none
  105. */
  106. void syscfg_exti_line_config(uint8_t exti_port, uint8_t exti_pin)
  107. {
  108. uint32_t clear_exti_mask = ~((uint32_t)EXTI_SS_MASK << (EXTI_SS_MSTEP(exti_pin)));
  109. uint32_t config_exti_mask = ((uint32_t)exti_port) << (EXTI_SS_MSTEP(exti_pin));
  110. switch(exti_pin/EXTI_SS_JSTEP){
  111. case EXTISS0:
  112. /* clear EXTI source line(0..3) */
  113. SYSCFG_EXTISS0 &= clear_exti_mask;
  114. /* configure EXTI soure line(0..3) */
  115. SYSCFG_EXTISS0 |= config_exti_mask;
  116. break;
  117. case EXTISS1:
  118. /* clear EXTI soure line(4..7) */
  119. SYSCFG_EXTISS1 &= clear_exti_mask;
  120. /* configure EXTI soure line(4..7) */
  121. SYSCFG_EXTISS1 |= config_exti_mask;
  122. break;
  123. case EXTISS2:
  124. /* clear EXTI soure line(8..11) */
  125. SYSCFG_EXTISS2 &= clear_exti_mask;
  126. /* configure EXTI soure line(8..11) */
  127. SYSCFG_EXTISS2 |= config_exti_mask;
  128. break;
  129. case EXTISS3:
  130. /* clear EXTI soure line(12..15) */
  131. SYSCFG_EXTISS3 &= clear_exti_mask;
  132. /* configure EXTI soure line(12..15) */
  133. SYSCFG_EXTISS3 |= config_exti_mask;
  134. break;
  135. default:
  136. break;
  137. }
  138. }
  139. /*!
  140. \brief configure the PHY interface for the ethernet MAC
  141. \param[in] syscfg_enet_phy_interface: specifies the media interface mode.
  142. only one parameter can be selected which is shown as below:
  143. \arg SYSCFG_ENET_PHY_MII: MII mode is selected
  144. \arg SYSCFG_ENET_PHY_RMII: RMII mode is selected
  145. \param[out] none
  146. \retval none
  147. */
  148. void syscfg_enet_phy_interface_config(uint32_t syscfg_enet_phy_interface)
  149. {
  150. uint32_t reg;
  151. reg = SYSCFG_CFG1;
  152. /* reset the ENET_PHY_SEL bit and set according to syscfg_enet_phy_interface */
  153. reg &= ~SYSCFG_CFG1_ENET_PHY_SEL;
  154. SYSCFG_CFG1 = (reg | syscfg_enet_phy_interface);
  155. }
  156. /*!
  157. \brief configure the I/O compensation cell
  158. \param[in] syscfg_compensation: specifies the I/O compensation cell mode
  159. only one parameter can be selected which is shown as below:
  160. \arg SYSCFG_COMPENSATION_ENABLE: I/O compensation cell is enabled
  161. \arg SYSCFG_COMPENSATION_DISABLE: I/O compensation cell is disabled
  162. \param[out] none
  163. \retval none
  164. */
  165. void syscfg_compensation_config(uint32_t syscfg_compensation)
  166. {
  167. uint32_t reg;
  168. reg = SYSCFG_CPSCTL;
  169. /* reset the SYSCFG_CPSCTL_CPS_EN bit and set according to syscfg_compensation */
  170. reg &= ~SYSCFG_CPSCTL_CPS_EN;
  171. SYSCFG_CPSCTL = (reg | syscfg_compensation);
  172. }
  173. /*!
  174. \brief checks whether the I/O compensation cell ready flag is set or not
  175. \param[in] none
  176. \param[out] none
  177. \retval FlagStatus: SET or RESET
  178. */
  179. FlagStatus syscfg_flag_get(void)
  180. {
  181. if(((uint32_t)RESET) != (SYSCFG_CPSCTL & SYSCFG_CPSCTL_CPS_RDY)){
  182. return SET;
  183. }else{
  184. return RESET;
  185. }
  186. }