gd32f10x_gpio.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*!
  2. \file gd32f10x_gpio.c
  3. \brief GPIO driver
  4. \version 2014-12-26, V1.0.0, firmware for GD32F10x
  5. \version 2017-06-20, V2.0.0, firmware for GD32F10x
  6. \version 2018-07-31, V2.1.0, firmware for GD32F10x
  7. \version 2020-09-30, V2.2.0, firmware for GD32F10x
  8. */
  9. /*
  10. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include "gd32f10x_gpio.h"
  33. #define AFIO_EXTI_SOURCE_MASK ((uint8_t)0x03U) /*!< AFIO exti source selection mask*/
  34. #define AFIO_EXTI_SOURCE_FIELDS ((uint8_t)0x04U) /*!< select AFIO exti source registers */
  35. #define LSB_16BIT_MASK ((uint16_t)0xFFFFU) /*!< LSB 16-bit mask */
  36. #define PCF_POSITION_MASK ((uint32_t)0x000F0000U) /*!< AFIO_PCF register position mask */
  37. #define PCF_SWJCFG_MASK ((uint32_t)0xF0FFFFFFU) /*!< AFIO_PCF register SWJCFG mask */
  38. #define PCF_LOCATION1_MASK ((uint32_t)0x00200000U) /*!< AFIO_PCF register location1 mask */
  39. #define PCF_LOCATION2_MASK ((uint32_t)0x00100000U) /*!< AFIO_PCF register location2 mask */
  40. #define AFIO_PCF1_FIELDS ((uint32_t)0x80000000U) /*!< select AFIO_PCF1 register */
  41. #define GPIO_OUTPUT_PORT_OFFSET ((uint32_t)4U) /*!< GPIO event output port offset*/
  42. /*!
  43. \brief reset GPIO port
  44. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G)
  45. \param[out] none
  46. \retval none
  47. */
  48. void gpio_deinit(uint32_t gpio_periph)
  49. {
  50. switch(gpio_periph){
  51. case GPIOA:
  52. /* reset GPIOA */
  53. rcu_periph_reset_enable(RCU_GPIOARST);
  54. rcu_periph_reset_disable(RCU_GPIOARST);
  55. break;
  56. case GPIOB:
  57. /* reset GPIOB */
  58. rcu_periph_reset_enable(RCU_GPIOBRST);
  59. rcu_periph_reset_disable(RCU_GPIOBRST);
  60. break;
  61. case GPIOC:
  62. /* reset GPIOC */
  63. rcu_periph_reset_enable(RCU_GPIOCRST);
  64. rcu_periph_reset_disable(RCU_GPIOCRST);
  65. break;
  66. case GPIOD:
  67. /* reset GPIOD */
  68. rcu_periph_reset_enable(RCU_GPIODRST);
  69. rcu_periph_reset_disable(RCU_GPIODRST);
  70. break;
  71. case GPIOE:
  72. /* reset GPIOE */
  73. rcu_periph_reset_enable(RCU_GPIOERST);
  74. rcu_periph_reset_disable(RCU_GPIOERST);
  75. break;
  76. case GPIOF:
  77. /* reset GPIOF */
  78. rcu_periph_reset_enable(RCU_GPIOFRST);
  79. rcu_periph_reset_disable(RCU_GPIOFRST);
  80. break;
  81. case GPIOG:
  82. /* reset GPIOG */
  83. rcu_periph_reset_enable(RCU_GPIOGRST);
  84. rcu_periph_reset_disable(RCU_GPIOGRST);
  85. break;
  86. default:
  87. break;
  88. }
  89. }
  90. /*!
  91. \brief reset alternate function I/O(AFIO)
  92. \param[in] none
  93. \param[out] none
  94. \retval none
  95. */
  96. void gpio_afio_deinit(void)
  97. {
  98. rcu_periph_reset_enable(RCU_AFRST);
  99. rcu_periph_reset_disable(RCU_AFRST);
  100. }
  101. /*!
  102. \brief GPIO parameter initialization
  103. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G)
  104. \param[in] mode: gpio pin mode
  105. only one parameter can be selected which is shown as below:
  106. \arg GPIO_MODE_AIN: analog input mode
  107. \arg GPIO_MODE_IN_FLOATING: floating input mode
  108. \arg GPIO_MODE_IPD: pull-down input mode
  109. \arg GPIO_MODE_IPU: pull-up input mode
  110. \arg GPIO_MODE_OUT_OD: GPIO output with open-drain
  111. \arg GPIO_MODE_OUT_PP: GPIO output with push-pull
  112. \arg GPIO_MODE_AF_OD: AFIO output with open-drain
  113. \arg GPIO_MODE_AF_PP: AFIO output with push-pull
  114. \param[in] speed: gpio output max speed value
  115. only one parameter can be selected which is shown as below:
  116. \arg GPIO_OSPEED_10MHZ: output max speed 10MHz
  117. \arg GPIO_OSPEED_2MHZ: output max speed 2MHz
  118. \arg GPIO_OSPEED_50MHZ: output max speed 50MHz
  119. \param[in] pin: GPIO pin
  120. one or more parameters can be selected which are shown as below:
  121. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  122. \param[out] none
  123. \retval none
  124. */
  125. void gpio_init(uint32_t gpio_periph, uint32_t mode, uint32_t speed, uint32_t pin)
  126. {
  127. uint16_t i;
  128. uint32_t temp_mode = 0U;
  129. uint32_t reg = 0U;
  130. /* GPIO mode configuration */
  131. temp_mode = (uint32_t)(mode & ((uint32_t)0x0FU));
  132. /* GPIO speed configuration */
  133. if(((uint32_t)0x00U) != ((uint32_t)mode & ((uint32_t)0x10U))){
  134. /* output mode max speed:10MHz,2MHz,50MHz */
  135. temp_mode |= (uint32_t)speed;
  136. }
  137. /* configure the eight low port pins with GPIO_CTL0 */
  138. for(i = 0U;i < 8U;i++){
  139. if((1U << i) & pin){
  140. reg = GPIO_CTL0(gpio_periph);
  141. /* clear the specified pin mode bits */
  142. reg &= ~GPIO_MODE_MASK(i);
  143. /* set the specified pin mode bits */
  144. reg |= GPIO_MODE_SET(i, temp_mode);
  145. /* set IPD or IPU */
  146. if(GPIO_MODE_IPD == mode){
  147. /* reset the corresponding OCTL bit */
  148. GPIO_BC(gpio_periph) = (uint32_t)((1U << i) & pin);
  149. }else{
  150. /* set the corresponding OCTL bit */
  151. if(GPIO_MODE_IPU == mode){
  152. GPIO_BOP(gpio_periph) = (uint32_t)((1U << i) & pin);
  153. }
  154. }
  155. /* set GPIO_CTL0 register */
  156. GPIO_CTL0(gpio_periph) = reg;
  157. }
  158. }
  159. /* configure the eight high port pins with GPIO_CTL1 */
  160. for(i = 8U;i < 16U;i++){
  161. if((1U << i) & pin){
  162. reg = GPIO_CTL1(gpio_periph);
  163. /* clear the specified pin mode bits */
  164. reg &= ~GPIO_MODE_MASK(i - 8U);
  165. /* set the specified pin mode bits */
  166. reg |= GPIO_MODE_SET(i - 8U, temp_mode);
  167. /* set IPD or IPU */
  168. if(GPIO_MODE_IPD == mode){
  169. /* reset the corresponding OCTL bit */
  170. GPIO_BC(gpio_periph) = (uint32_t)((1U << i) & pin);
  171. }else{
  172. /* set the corresponding OCTL bit */
  173. if(GPIO_MODE_IPU == mode){
  174. GPIO_BOP(gpio_periph) = (uint32_t)((1U << i) & pin);
  175. }
  176. }
  177. /* set GPIO_CTL1 register */
  178. GPIO_CTL1(gpio_periph) = reg;
  179. }
  180. }
  181. }
  182. /*!
  183. \brief set GPIO pin
  184. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G)
  185. \param[in] pin: GPIO pin
  186. one or more parameters can be selected which are shown as below:
  187. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  188. \param[out] none
  189. \retval none
  190. */
  191. void gpio_bit_set(uint32_t gpio_periph, uint32_t pin)
  192. {
  193. GPIO_BOP(gpio_periph) = (uint32_t)pin;
  194. }
  195. /*!
  196. \brief reset GPIO pin
  197. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G)
  198. \param[in] pin: GPIO pin
  199. one or more parameters can be selected which are shown as below:
  200. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  201. \param[out] none
  202. \retval none
  203. */
  204. void gpio_bit_reset(uint32_t gpio_periph, uint32_t pin)
  205. {
  206. GPIO_BC(gpio_periph) = (uint32_t)pin;
  207. }
  208. /*!
  209. \brief write data to the specified GPIO pin
  210. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G)
  211. \param[in] pin: GPIO pin
  212. one or more parameters can be selected which are shown as below:
  213. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  214. \param[in] bit_value: SET or RESET
  215. \arg RESET: clear the port pin
  216. \arg SET: set the port pin
  217. \param[out] none
  218. \retval none
  219. */
  220. void gpio_bit_write(uint32_t gpio_periph, uint32_t pin, bit_status bit_value)
  221. {
  222. if(RESET != bit_value){
  223. GPIO_BOP(gpio_periph) = (uint32_t)pin;
  224. }else{
  225. GPIO_BC(gpio_periph) = (uint32_t)pin;
  226. }
  227. }
  228. /*!
  229. \brief write data to the specified GPIO port
  230. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G)
  231. \param[in] data: specify the value to be written to the port output data register
  232. \param[out] none
  233. \retval none
  234. */
  235. void gpio_port_write(uint32_t gpio_periph,uint16_t data)
  236. {
  237. GPIO_OCTL(gpio_periph) = (uint32_t)data;
  238. }
  239. /*!
  240. \brief get GPIO pin input status
  241. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G)
  242. \param[in] pin: GPIO pin
  243. one or more parameters can be selected which are shown as below:
  244. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  245. \param[out] none
  246. \retval input status of gpio pin: SET or RESET
  247. */
  248. FlagStatus gpio_input_bit_get(uint32_t gpio_periph,uint32_t pin)
  249. {
  250. if((uint32_t)RESET != (GPIO_ISTAT(gpio_periph)&(pin))){
  251. return SET;
  252. }else{
  253. return RESET;
  254. }
  255. }
  256. /*!
  257. \brief get GPIO port input status
  258. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G)
  259. \param[out] none
  260. \retval input status of gpio all pins
  261. */
  262. uint16_t gpio_input_port_get(uint32_t gpio_periph)
  263. {
  264. return (uint16_t)(GPIO_ISTAT(gpio_periph));
  265. }
  266. /*!
  267. \brief get GPIO pin output status
  268. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G)
  269. \param[in] pin: GPIO pin
  270. one or more parameters can be selected which are shown as below:
  271. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  272. \param[out] none
  273. \retval output status of gpio pin: SET or RESET
  274. */
  275. FlagStatus gpio_output_bit_get(uint32_t gpio_periph, uint32_t pin)
  276. {
  277. if((uint32_t)RESET !=(GPIO_OCTL(gpio_periph)&(pin))){
  278. return SET;
  279. }else{
  280. return RESET;
  281. }
  282. }
  283. /*!
  284. \brief get GPIO port output status
  285. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G)
  286. \param[out] none
  287. \retval output status of gpio all pins
  288. */
  289. uint16_t gpio_output_port_get(uint32_t gpio_periph)
  290. {
  291. return ((uint16_t)GPIO_OCTL(gpio_periph));
  292. }
  293. /*!
  294. \brief configure GPIO pin remap
  295. \param[in] gpio_remap: select the pin to remap
  296. \arg GPIO_SPI0_REMAP: SPI0 remapping
  297. \arg GPIO_I2C0_REMAP: I2C0 remapping
  298. \arg GPIO_USART0_REMAP: USART0 remapping
  299. \arg GPIO_USART1_REMAP: USART1 remapping
  300. \arg GPIO_USART2_PARTIAL_REMAP: USART2 partial remapping
  301. \arg GPIO_USART2_FULL_REMAP: USART2 full remapping
  302. \arg GPIO_TIMER0_PARTIAL_REMAP: TIMER0 partial remapping
  303. \arg GPIO_TIMER0_FULL_REMAP: TIMER0 full remapping
  304. \arg GPIO_TIMER1_PARTIAL_REMAP1: TIMER1 partial remapping
  305. \arg GPIO_TIMER1_PARTIAL_REMAP2: TIMER1 partial remapping
  306. \arg GPIO_TIMER1_FULL_REMAP: TIMER1 full remapping
  307. \arg GPIO_TIMER2_PARTIAL_REMAP: TIMER2 partial remapping
  308. \arg GPIO_TIMER2_FULL_REMAP: TIMER2 full remapping
  309. \arg GPIO_TIMER3_REMAP: TIMER3 remapping
  310. \arg GPIO_CAN_PARTIAL_REMAP: CAN partial remapping(only for GD32F10X_MD, GD32F10X_HD devices and GD32F10X_XD devices)
  311. \arg GPIO_CAN_FULL_REMAP: CAN full remapping(only for GD32F10X_MD, GD32F10X_HD devices and GD32F10X_XD devices)
  312. \arg GPIO_CAN0_PARTIAL_REMAP: CAN0 partial remapping(only for GD32F10X_CL devices)
  313. \arg GPIO_CAN0_FULL_REMAP: CAN0 full remapping(only for GD32F10X_CL devices)
  314. \arg GPIO_PD01_REMAP: PD01 remapping
  315. \arg GPIO_TIMER4CH3_IREMAP: TIMER4 channel3 internal remapping(only for GD32F10X_CL devices and GD32F10X_HD devices)
  316. \arg GPIO_ADC0_ETRGRT_REMAP: ADC0 external trigger routine conversion remapping(only for GD32F10X_MD, GD32F10X_HD devices and GD32F10X_XD devices)
  317. \arg GPIO_ADC1_ETRGRT_REMAP: ADC1 external trigger routine conversion remapping(only for GD32F10X_MD, GD32F10X_HD devices and GD32F10X_XD devices)
  318. \arg GPIO_ENET_REMAP: ENET remapping(only for GD32F10X_CL devices)
  319. \arg GPIO_CAN1_REMAP: CAN1 remapping(only for GD32F10X_CL devices)
  320. \arg GPIO_SWJ_NONJTRST_REMAP: full SWJ(JTAG-DP + SW-DP),but without NJTRST
  321. \arg GPIO_SWJ_SWDPENABLE_REMAP: JTAG-DP disabled and SW-DP enabled
  322. \arg GPIO_SWJ_DISABLE_REMAP: JTAG-DP disabled and SW-DP disabled
  323. \arg GPIO_SPI2_REMAP: SPI2 remapping(only for GD32F10X_CL, GD32F10X_HD and GD32F10X_XD devices)
  324. \arg GPIO_TIMER1ITI1_REMAP: TIMER1 internal trigger 1 remapping(only for GD32F10X_CL devices)
  325. \arg GPIO_PTP_PPS_REMAP: ethernet PTP PPS remapping(only for GD32F10X_CL devices)
  326. \arg GPIO_TIMER8_REMAP: TIMER8 remapping
  327. \arg GPIO_TIMER9_REMAP: TIMER9 remapping
  328. \arg GPIO_TIMER10_REMAP: TIMER10 remapping
  329. \arg GPIO_TIMER12_REMAP: TIMER12 remapping
  330. \arg GPIO_TIMER13_REMAP: TIMER13 remapping
  331. \arg GPIO_EXMC_NADV_REMAP: EXMC_NADV connect/disconnect
  332. \param[in] newvalue: ENABLE or DISABLE
  333. \param[out] none
  334. \retval none
  335. */
  336. void gpio_pin_remap_config(uint32_t remap, ControlStatus newvalue)
  337. {
  338. uint32_t remap1 = 0U, remap2 = 0U, temp_reg = 0U, temp_mask = 0U;
  339. if(AFIO_PCF1_FIELDS == (remap & AFIO_PCF1_FIELDS)){
  340. /* get AFIO_PCF1 regiter value */
  341. temp_reg = AFIO_PCF1;
  342. }else{
  343. /* get AFIO_PCF0 regiter value */
  344. temp_reg = AFIO_PCF0;
  345. }
  346. temp_mask = (remap & PCF_POSITION_MASK) >> 0x10U;
  347. remap1 = remap & LSB_16BIT_MASK;
  348. /* judge pin remap type */
  349. if((PCF_LOCATION1_MASK | PCF_LOCATION2_MASK) == (remap & (PCF_LOCATION1_MASK | PCF_LOCATION2_MASK))){
  350. temp_reg &= PCF_SWJCFG_MASK;
  351. AFIO_PCF0 &= PCF_SWJCFG_MASK;
  352. }else if(PCF_LOCATION2_MASK == (remap & PCF_LOCATION2_MASK)){
  353. remap2 = ((uint32_t)0x03U) << temp_mask;
  354. temp_reg &= ~remap2;
  355. temp_reg |= ~PCF_SWJCFG_MASK;
  356. }else{
  357. temp_reg &= ~(remap1 << ((remap >> 0x15U)*0x10U));
  358. temp_reg |= ~PCF_SWJCFG_MASK;
  359. }
  360. /* set pin remap value */
  361. if(DISABLE != newvalue){
  362. temp_reg |= (remap1 << ((remap >> 0x15U)*0x10U));
  363. }
  364. if(AFIO_PCF1_FIELDS == (remap & AFIO_PCF1_FIELDS)){
  365. /* set AFIO_PCF1 regiter value */
  366. AFIO_PCF1 = temp_reg;
  367. }else{
  368. /* set AFIO_PCF0 regiter value */
  369. AFIO_PCF0 = temp_reg;
  370. }
  371. }
  372. /*!
  373. \brief select GPIO pin exti sources
  374. \param[in] gpio_outputport: gpio event output port
  375. \arg GPIO_PORT_SOURCE_GPIOA: output port source A
  376. \arg GPIO_PORT_SOURCE_GPIOB: output port source B
  377. \arg GPIO_PORT_SOURCE_GPIOC: output port source C
  378. \arg GPIO_PORT_SOURCE_GPIOD: output port source D
  379. \arg GPIO_PORT_SOURCE_GPIOE: output port source E
  380. \arg GPIO_PORT_SOURCE_GPIOF: output port source F
  381. \arg GPIO_PORT_SOURCE_GPIOG: output port source G
  382. \param[in] gpio_outputpin: GPIO_PIN_SOURCE_x(x=0..15)
  383. \param[out] none
  384. \retval none
  385. */
  386. void gpio_exti_source_select(uint8_t output_port, uint8_t output_pin)
  387. {
  388. uint32_t source = 0U;
  389. source = ((uint32_t)0x0FU) << (AFIO_EXTI_SOURCE_FIELDS * (output_pin & AFIO_EXTI_SOURCE_MASK));
  390. /* select EXTI sources */
  391. if(GPIO_PIN_SOURCE_4 > output_pin){
  392. /* select EXTI0/EXTI1/EXTI2/EXTI3 */
  393. AFIO_EXTISS0 &= ~source;
  394. AFIO_EXTISS0 |= (((uint32_t)output_port) << (AFIO_EXTI_SOURCE_FIELDS * (output_pin & AFIO_EXTI_SOURCE_MASK)));
  395. }else if(GPIO_PIN_SOURCE_8 > output_pin){
  396. /* select EXTI4/EXTI5/EXTI6/EXTI7 */
  397. AFIO_EXTISS1 &= ~source;
  398. AFIO_EXTISS1 |= (((uint32_t)output_port) << (AFIO_EXTI_SOURCE_FIELDS * (output_pin & AFIO_EXTI_SOURCE_MASK)));
  399. }else if(GPIO_PIN_SOURCE_12 > output_pin){
  400. /* select EXTI8/EXTI9/EXTI10/EXTI11 */
  401. AFIO_EXTISS2 &= ~source;
  402. AFIO_EXTISS2 |= (((uint32_t)output_port) << (AFIO_EXTI_SOURCE_FIELDS * (output_pin & AFIO_EXTI_SOURCE_MASK)));
  403. }else{
  404. /* select EXTI12/EXTI13/EXTI14/EXTI15 */
  405. AFIO_EXTISS3 &= ~source;
  406. AFIO_EXTISS3 |= (((uint32_t)output_port) << (AFIO_EXTI_SOURCE_FIELDS * (output_pin & AFIO_EXTI_SOURCE_MASK)));
  407. }
  408. }
  409. /*!
  410. \brief configure GPIO pin event output
  411. \param[in] output_port: gpio event output port
  412. only one parameter can be selected which are shown as below:
  413. \arg GPIO_EVENT_PORT_GPIOA: event output port A
  414. \arg GPIO_EVENT_PORT_GPIOB: event output port B
  415. \arg GPIO_EVENT_PORT_GPIOC: event output port C
  416. \arg GPIO_EVENT_PORT_GPIOD: event output port D
  417. \arg GPIO_EVENT_PORT_GPIOE: event output port E
  418. \arg GPIO_EVENT_PORT_GPIOE: event output port F
  419. \arg GPIO_EVENT_PORT_GPIOE: event output port G
  420. \param[in] output_pin:
  421. only one parameter can be selected which are shown as below:
  422. \arg GPIO_EVENT_PIN_x(x=0..15)
  423. \param[out] none
  424. \retval none
  425. */
  426. void gpio_event_output_config(uint8_t output_port, uint8_t output_pin)
  427. {
  428. uint32_t reg = 0U;
  429. reg = AFIO_EC;
  430. /* clear AFIO_EC_PORT and AFIO_EC_PIN bits */
  431. reg &= (uint32_t)(~(AFIO_EC_PORT|AFIO_EC_PIN));
  432. reg |= (uint32_t)((uint32_t)output_port << GPIO_OUTPUT_PORT_OFFSET);
  433. reg |= (uint32_t)output_pin;
  434. AFIO_EC = reg;
  435. }
  436. /*!
  437. \brief enable GPIO pin event output
  438. \param[in] none
  439. \param[out] none
  440. \retval none
  441. */
  442. void gpio_event_output_enable(void)
  443. {
  444. AFIO_EC |= AFIO_EC_EOE;
  445. }
  446. /*!
  447. \brief disable GPIO pin event output
  448. \param[in] none
  449. \param[out] none
  450. \retval none
  451. */
  452. void gpio_event_output_disable(void)
  453. {
  454. AFIO_EC &= (uint32_t)(~AFIO_EC_EOE);
  455. }
  456. /*!
  457. \brief lock GPIO pin
  458. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G)
  459. \param[in] pin: GPIO pin
  460. one or more parameters can be selected which are shown as below:
  461. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  462. \param[out] none
  463. \retval none
  464. */
  465. void gpio_pin_lock(uint32_t gpio_periph, uint32_t pin)
  466. {
  467. uint32_t lock = 0x00010000U;
  468. lock |= pin;
  469. /* lock key writing sequence: write 1 -> write 0 -> write 1 -> read 0 -> read 1 */
  470. GPIO_LOCK(gpio_periph) = (uint32_t)lock;
  471. GPIO_LOCK(gpio_periph) = (uint32_t)pin;
  472. GPIO_LOCK(gpio_periph) = (uint32_t)lock;
  473. lock = GPIO_LOCK(gpio_periph);
  474. lock = GPIO_LOCK(gpio_periph);
  475. }
  476. #ifdef GD32F10X_CL
  477. /*!
  478. \brief select ethernet MII or RMII PHY
  479. \param[in] gpio_enetsel: ethernet MII or RMII PHY selection
  480. \arg GPIO_ENET_PHY_MII: configure ethernet MAC for connection with an MII PHY
  481. \arg GPIO_ENET_PHY_RMII: configure ethernet MAC for connection with an RMII PHY
  482. \param[out] none
  483. \retval none
  484. */
  485. void gpio_ethernet_phy_select(uint32_t gpio_enetsel)
  486. {
  487. /* clear AFIO_PCF0_ENET_PHY_SEL bit */
  488. AFIO_PCF0 &= (uint32_t)(~AFIO_PCF0_ENET_PHY_SEL);
  489. /* select MII or RMII PHY */
  490. AFIO_PCF0 |= (uint32_t)gpio_enetsel;
  491. }
  492. #endif