gd32f30x_gpio.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*!
  2. \file gd32f30x_gpio.c
  3. \brief GPIO driver
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. \version 2020-09-30, V2.1.0, firmware for GD32F30x
  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 "gd32f30x_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)0xF8FFFFFFU) /*!< 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. \arg GPIO_OSPEED_MAX: output max speed more than 50MHz
  120. \param[in] pin: GPIO pin
  121. one or more parameters can be selected which are shown as below:
  122. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  123. \param[out] none
  124. \retval none
  125. */
  126. void gpio_init(uint32_t gpio_periph, uint32_t mode, uint32_t speed, uint32_t pin)
  127. {
  128. uint16_t i;
  129. uint32_t temp_mode = 0U;
  130. uint32_t reg = 0U;
  131. /* GPIO mode configuration */
  132. temp_mode = (uint32_t)(mode & ((uint32_t)0x0FU));
  133. /* GPIO speed configuration */
  134. if(((uint32_t)0x00U) != ((uint32_t)mode & ((uint32_t)0x10U))){
  135. /* output mode max speed */
  136. if(GPIO_OSPEED_MAX == (uint32_t)speed){
  137. temp_mode |= (uint32_t)0x03U;
  138. /* set the corresponding SPD bit */
  139. GPIOx_SPD(gpio_periph) |= (uint32_t)pin ;
  140. }else{
  141. /* output mode max speed:10MHz,2MHz,50MHz */
  142. temp_mode |= (uint32_t)speed;
  143. }
  144. }
  145. /* configure the eight low port pins with GPIO_CTL0 */
  146. for(i = 0U;i < 8U;i++){
  147. if((1U << i) & pin){
  148. reg = GPIO_CTL0(gpio_periph);
  149. /* clear the specified pin mode bits */
  150. reg &= ~GPIO_MODE_MASK(i);
  151. /* set the specified pin mode bits */
  152. reg |= GPIO_MODE_SET(i, temp_mode);
  153. /* set IPD or IPU */
  154. if(GPIO_MODE_IPD == mode){
  155. /* reset the corresponding OCTL bit */
  156. GPIO_BC(gpio_periph) = (uint32_t)((1U << i) & pin);
  157. }else{
  158. /* set the corresponding OCTL bit */
  159. if(GPIO_MODE_IPU == mode){
  160. GPIO_BOP(gpio_periph) = (uint32_t)((1U << i) & pin);
  161. }
  162. }
  163. /* set GPIO_CTL0 register */
  164. GPIO_CTL0(gpio_periph) = reg;
  165. }
  166. }
  167. /* configure the eight high port pins with GPIO_CTL1 */
  168. for(i = 8U;i < 16U;i++){
  169. if((1U << i) & pin){
  170. reg = GPIO_CTL1(gpio_periph);
  171. /* clear the specified pin mode bits */
  172. reg &= ~GPIO_MODE_MASK(i - 8U);
  173. /* set the specified pin mode bits */
  174. reg |= GPIO_MODE_SET(i - 8U, temp_mode);
  175. /* set IPD or IPU */
  176. if(GPIO_MODE_IPD == mode){
  177. /* reset the corresponding OCTL bit */
  178. GPIO_BC(gpio_periph) = (uint32_t)((1U << i) & pin);
  179. }else{
  180. /* set the corresponding OCTL bit */
  181. if(GPIO_MODE_IPU == mode){
  182. GPIO_BOP(gpio_periph) = (uint32_t)((1U << i) & pin);
  183. }
  184. }
  185. /* set GPIO_CTL1 register */
  186. GPIO_CTL1(gpio_periph) = reg;
  187. }
  188. }
  189. }
  190. /*!
  191. \brief set GPIO pin
  192. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G)
  193. \param[in] pin: GPIO pin
  194. one or more parameters can be selected which are shown as below:
  195. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  196. \param[out] none
  197. \retval none
  198. */
  199. void gpio_bit_set(uint32_t gpio_periph,uint32_t pin)
  200. {
  201. GPIO_BOP(gpio_periph) = (uint32_t)pin;
  202. }
  203. /*!
  204. \brief reset GPIO pin
  205. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G)
  206. \param[in] pin: GPIO pin
  207. one or more parameters can be selected which are shown as below:
  208. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  209. \param[out] none
  210. \retval none
  211. */
  212. void gpio_bit_reset(uint32_t gpio_periph,uint32_t pin)
  213. {
  214. GPIO_BC(gpio_periph) = (uint32_t)pin;
  215. }
  216. /*!
  217. \brief write data to the specified GPIO pin
  218. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G)
  219. \param[in] pin: GPIO pin
  220. one or more parameters can be selected which are shown as below:
  221. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  222. \param[in] bit_value: SET or RESET
  223. \arg RESET: clear the port pin
  224. \arg SET: set the port pin
  225. \param[out] none
  226. \retval none
  227. */
  228. void gpio_bit_write(uint32_t gpio_periph,uint32_t pin,bit_status bit_value)
  229. {
  230. if(RESET != bit_value){
  231. GPIO_BOP(gpio_periph) = (uint32_t)pin;
  232. }else{
  233. GPIO_BC(gpio_periph) = (uint32_t)pin;
  234. }
  235. }
  236. /*!
  237. \brief write data to the specified GPIO port
  238. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G)
  239. \param[in] data: specify the value to be written to the port output data register
  240. \param[out] none
  241. \retval none
  242. */
  243. void gpio_port_write(uint32_t gpio_periph,uint16_t data)
  244. {
  245. GPIO_OCTL(gpio_periph) = (uint32_t)data;
  246. }
  247. /*!
  248. \brief get GPIO pin input status
  249. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G)
  250. \param[in] pin: GPIO pin
  251. only one parameter can be selected which are shown as below:
  252. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  253. \param[out] none
  254. \retval input status of gpio pin: SET or RESET
  255. */
  256. FlagStatus gpio_input_bit_get(uint32_t gpio_periph,uint32_t pin)
  257. {
  258. if((uint32_t)RESET != (GPIO_ISTAT(gpio_periph)&(pin))){
  259. return SET;
  260. }else{
  261. return RESET;
  262. }
  263. }
  264. /*!
  265. \brief get GPIO port input status
  266. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G)
  267. \param[out] none
  268. \retval input status of gpio all pins
  269. */
  270. uint16_t gpio_input_port_get(uint32_t gpio_periph)
  271. {
  272. return (uint16_t)(GPIO_ISTAT(gpio_periph));
  273. }
  274. /*!
  275. \brief get GPIO pin output status
  276. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G)
  277. \param[in] pin: GPIO pin
  278. only one parameter can be selected which are shown as below:
  279. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  280. \param[out] none
  281. \retval output status of gpio pin: SET or RESET
  282. */
  283. FlagStatus gpio_output_bit_get(uint32_t gpio_periph,uint32_t pin)
  284. {
  285. if((uint32_t)RESET !=(GPIO_OCTL(gpio_periph)&(pin))){
  286. return SET;
  287. }else{
  288. return RESET;
  289. }
  290. }
  291. /*!
  292. \brief get GPIO port output status
  293. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G)
  294. \param[out] none
  295. \retval output status of gpio all pins
  296. */
  297. uint16_t gpio_output_port_get(uint32_t gpio_periph)
  298. {
  299. return ((uint16_t)GPIO_OCTL(gpio_periph));
  300. }
  301. /*!
  302. \brief configure GPIO pin remap
  303. \param[in] gpio_remap: select the pin to remap
  304. only one parameter can be selected which are shown as below:
  305. \arg GPIO_SPI0_REMAP: SPI0 remapping
  306. \arg GPIO_I2C0_REMAP: I2C0 remapping
  307. \arg GPIO_USART0_REMAP: USART0 remapping
  308. \arg GPIO_USART1_REMAP: USART1 remapping
  309. \arg GPIO_USART2_PARTIAL_REMAP: USART2 partial remapping
  310. \arg GPIO_USART2_FULL_REMAP: USART2 full remapping
  311. \arg GPIO_TIMER0_PARTIAL_REMAP: TIMER0 partial remapping
  312. \arg GPIO_TIMER0_FULL_REMAP: TIMER0 full remapping
  313. \arg GPIO_TIMER1_PARTIAL_REMAP0: TIMER1 partial remapping
  314. \arg GPIO_TIMER1_PARTIAL_REMAP1: TIMER1 partial remapping
  315. \arg GPIO_TIMER1_FULL_REMAP: TIMER1 full remapping
  316. \arg GPIO_TIMER2_PARTIAL_REMAP: TIMER2 partial remapping
  317. \arg GPIO_TIMER2_FULL_REMAP: TIMER2 full remapping
  318. \arg GPIO_TIMER3_REMAP: TIMER3 remapping
  319. \arg GPIO_CAN_PARTIAL_REMAP: CAN partial remapping(only for GD32F30X_HD devices and GD32F30X_XD devices)
  320. \arg GPIO_CAN_FULL_REMAP: CAN full remapping(only for GD32F30X_HD devices and GD32F30X_XD devices)
  321. \arg GPIO_CAN0_PARTIAL_REMAP: CAN0 partial remapping(only for GD32F30X_CL devices)
  322. \arg GPIO_CAN0_FULL_REMAP: CAN0 full remapping(only for GD32F30X_CL devices)
  323. \arg GPIO_PD01_REMAP: PD01 remapping
  324. \arg GPIO_TIMER4CH3_IREMAP: TIMER4 channel3 internal remapping
  325. \arg GPIO_ADC0_ETRGINS_REMAP: ADC0 external trigger inserted conversion remapping(only for GD32F30X_HD devices and GD32F30X_XD devices)
  326. \arg GPIO_ADC0_ETRGREG_REMAP: ADC0 external trigger regular conversion remapping(only for GD32F30X_HD devices and GD32F30X_XD devices)
  327. \arg GPIO_ADC1_ETRGINS_REMAP: ADC1 external trigger inserted conversion remapping(only for GD32F30X_HD devices and GD32F30X_XD devices)
  328. \arg GPIO_ADC1_ETRGREG_REMAP: ADC1 external trigger regular conversion remapping(only for GD32F30X_HD devices and GD32F30X_XD devices)
  329. \arg GPIO_ENET_REMAP: ENET remapping(only for GD32F30X_CL devices)
  330. \arg GPIO_CAN1_REMAP: CAN1 remapping(only for GD32F30X_CL devices)
  331. \arg GPIO_SWJ_NONJTRST_REMAP: full SWJ(JTAG-DP + SW-DP),but without NJTRST
  332. \arg GPIO_SWJ_SWDPENABLE_REMAP: JTAG-DP disabled and SW-DP enabled
  333. \arg GPIO_SWJ_DISABLE_REMAP: JTAG-DP disabled and SW-DP disabled
  334. \arg GPIO_SPI2_REMAP: SPI2 remapping
  335. \arg GPIO_TIMER1ITR0_REMAP: TIMER1 internal trigger 0 remapping(only for GD32F30X_CL devices)
  336. \arg GPIO_PTP_PPS_REMAP: ethernet PTP PPS remapping(only for GD32F30X_CL devices)
  337. \arg GPIO_TIMER8_REMAP: TIMER8 remapping
  338. \arg GPIO_TIMER9_REMAP: TIMER9 remapping
  339. \arg GPIO_TIMER10_REMAP: TIMER10 remapping
  340. \arg GPIO_TIMER12_REMAP: TIMER12 remapping
  341. \arg GPIO_TIMER13_REMAP: TIMER13 remapping
  342. \arg GPIO_EXMC_NADV_REMAP: EXMC_NADV connect/disconnect
  343. \arg GPIO_CTC_REMAP0: CTC remapping(PD15)
  344. \arg GPIO_CTC_REMAP1: CTC remapping(PF0)
  345. \param[in] newvalue: ENABLE or DISABLE
  346. \param[out] none
  347. \retval none
  348. */
  349. void gpio_pin_remap_config(uint32_t remap, ControlStatus newvalue)
  350. {
  351. uint32_t remap1 = 0U, remap2 = 0U, temp_reg = 0U, temp_mask = 0U;
  352. if(((uint32_t)0x80000000U) == (remap & 0x80000000U)){
  353. /* get AFIO_PCF1 regiter value */
  354. temp_reg = AFIO_PCF1;
  355. }else{
  356. /* get AFIO_PCF0 regiter value */
  357. temp_reg = AFIO_PCF0;
  358. }
  359. temp_mask = (remap & PCF_POSITION_MASK) >> 0x10U;
  360. remap1 = remap & LSB_16BIT_MASK;
  361. /* judge pin remap type */
  362. if((PCF_LOCATION1_MASK | PCF_LOCATION2_MASK) == (remap & (PCF_LOCATION1_MASK | PCF_LOCATION2_MASK))){
  363. temp_reg &= PCF_SWJCFG_MASK;
  364. AFIO_PCF0 &= PCF_SWJCFG_MASK;
  365. }else if(PCF_LOCATION2_MASK == (remap & PCF_LOCATION2_MASK)){
  366. remap2 = ((uint32_t)0x03U) << temp_mask;
  367. temp_reg &= ~remap2;
  368. temp_reg |= ~PCF_SWJCFG_MASK;
  369. }else{
  370. temp_reg &= ~(remap1 << ((remap >> 0x15U)*0x10U));
  371. temp_reg |= ~PCF_SWJCFG_MASK;
  372. }
  373. /* set pin remap value */
  374. if(DISABLE != newvalue){
  375. temp_reg |= (remap1 << ((remap >> 0x15U)*0x10U));
  376. }
  377. if(AFIO_PCF1_FIELDS == (remap & AFIO_PCF1_FIELDS)){
  378. /* set AFIO_PCF1 regiter value */
  379. AFIO_PCF1 = temp_reg;
  380. }else{
  381. /* set AFIO_PCF0 regiter value */
  382. AFIO_PCF0 = temp_reg;
  383. }
  384. }
  385. #ifdef GD32F30X_CL
  386. /*!
  387. \brief select ethernet MII or RMII PHY
  388. \param[in] enet_sel: ethernet MII or RMII PHY selection
  389. \arg GPIO_ENET_PHY_MII: configure ethernet MAC for connection with an MII PHY
  390. \arg GPIO_ENET_PHY_RMII: configure ethernet MAC for connection with an RMII PHY
  391. \param[out] none
  392. \retval none
  393. */
  394. void gpio_ethernet_phy_select(uint32_t enet_sel)
  395. {
  396. /* clear AFIO_PCF0_ENET_PHY_SEL bit */
  397. AFIO_PCF0 &= (uint32_t)(~AFIO_PCF0_ENET_PHY_SEL);
  398. /* select MII or RMII PHY */
  399. AFIO_PCF0 |= (uint32_t)enet_sel;
  400. }
  401. #endif /* GD32F30X_CL */
  402. /*!
  403. \brief select GPIO pin exti sources
  404. \param[in] output_port: gpio event output port
  405. only one parameter can be selected which are shown as below:
  406. \arg GPIO_PORT_SOURCE_GPIOA: output port source A
  407. \arg GPIO_PORT_SOURCE_GPIOB: output port source B
  408. \arg GPIO_PORT_SOURCE_GPIOC: output port source C
  409. \arg GPIO_PORT_SOURCE_GPIOD: output port source D
  410. \arg GPIO_PORT_SOURCE_GPIOE: output port source E
  411. \arg GPIO_PORT_SOURCE_GPIOF: output port source F
  412. \arg GPIO_PORT_SOURCE_GPIOG: output port source G
  413. \param[in] output_pin: GPIO output pin source
  414. only one parameter can be selected which are shown as below:
  415. \arg GPIO_PIN_SOURCE_x(x=0..15)
  416. \param[out] none
  417. \retval none
  418. */
  419. void gpio_exti_source_select(uint8_t output_port, uint8_t output_pin)
  420. {
  421. uint32_t source = 0U;
  422. source = ((uint32_t)0x0FU) << (AFIO_EXTI_SOURCE_FIELDS * (output_pin & AFIO_EXTI_SOURCE_MASK));
  423. /* select EXTI sources */
  424. if(GPIO_PIN_SOURCE_4 > output_pin){
  425. /* select EXTI0/EXTI1/EXTI2/EXTI3 */
  426. AFIO_EXTISS0 &= ~source;
  427. AFIO_EXTISS0 |= (((uint32_t)output_port) << (AFIO_EXTI_SOURCE_FIELDS * (output_pin & AFIO_EXTI_SOURCE_MASK)));
  428. }else if(GPIO_PIN_SOURCE_8 > output_pin){
  429. /* select EXTI4/EXTI5/EXTI6/EXTI7 */
  430. AFIO_EXTISS1 &= ~source;
  431. AFIO_EXTISS1 |= (((uint32_t)output_port) << (AFIO_EXTI_SOURCE_FIELDS * (output_pin & AFIO_EXTI_SOURCE_MASK)));
  432. }else if(GPIO_PIN_SOURCE_12 > output_pin){
  433. /* select EXTI8/EXTI9/EXTI10/EXTI11 */
  434. AFIO_EXTISS2 &= ~source;
  435. AFIO_EXTISS2 |= (((uint32_t)output_port) << (AFIO_EXTI_SOURCE_FIELDS * (output_pin & AFIO_EXTI_SOURCE_MASK)));
  436. }else{
  437. /* select EXTI12/EXTI13/EXTI14/EXTI15 */
  438. AFIO_EXTISS3 &= ~source;
  439. AFIO_EXTISS3 |= (((uint32_t)output_port) << (AFIO_EXTI_SOURCE_FIELDS * (output_pin & AFIO_EXTI_SOURCE_MASK)));
  440. }
  441. }
  442. /*!
  443. \brief configure GPIO pin event output
  444. \param[in] output_port: gpio event output port
  445. only one parameter can be selected which are shown as below:
  446. \arg GPIO_EVENT_PORT_GPIOA: event output port A
  447. \arg GPIO_EVENT_PORT_GPIOB: event output port B
  448. \arg GPIO_EVENT_PORT_GPIOC: event output port C
  449. \arg GPIO_EVENT_PORT_GPIOD: event output port D
  450. \arg GPIO_EVENT_PORT_GPIOE: event output port E
  451. \param[in] output_pin: GPIO event output pin
  452. only one parameter can be selected which are shown as below:
  453. \arg GPIO_EVENT_PIN_x(x=0..15)
  454. \param[out] none
  455. \retval none
  456. */
  457. void gpio_event_output_config(uint8_t output_port, uint8_t output_pin)
  458. {
  459. uint32_t reg = 0U;
  460. reg = AFIO_EC;
  461. /* clear AFIO_EC_PORT and AFIO_EC_PIN bits */
  462. reg &= (uint32_t)(~(AFIO_EC_PORT|AFIO_EC_PIN));
  463. reg |= (uint32_t)((uint32_t)output_port << GPIO_OUTPUT_PORT_OFFSET);
  464. reg |= (uint32_t)output_pin;
  465. AFIO_EC = reg;
  466. }
  467. /*!
  468. \brief enable GPIO pin event output
  469. \param[in] none
  470. \param[out] none
  471. \retval none
  472. */
  473. void gpio_event_output_enable(void)
  474. {
  475. AFIO_EC |= AFIO_EC_EOE;
  476. }
  477. /*!
  478. \brief disable GPIO pin event output
  479. \param[in] none
  480. \param[out] none
  481. \retval none
  482. */
  483. void gpio_event_output_disable(void)
  484. {
  485. AFIO_EC &= (uint32_t)(~AFIO_EC_EOE);
  486. }
  487. /*!
  488. \brief lock GPIO pin
  489. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G)
  490. \param[in] pin: GPIO pin
  491. one or more parameters can be selected which are shown as below:
  492. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  493. \param[out] none
  494. \retval none
  495. */
  496. void gpio_pin_lock(uint32_t gpio_periph,uint32_t pin)
  497. {
  498. uint32_t lock = 0x00010000U;
  499. lock |= pin;
  500. /* lock key writing sequence: write 1 -> write 0 -> write 1 -> read 0 -> read 1 */
  501. GPIO_LOCK(gpio_periph) = (uint32_t)lock;
  502. GPIO_LOCK(gpio_periph) = (uint32_t)pin;
  503. GPIO_LOCK(gpio_periph) = (uint32_t)lock;
  504. lock = GPIO_LOCK(gpio_periph);
  505. lock = GPIO_LOCK(gpio_periph);
  506. }
  507. /*!
  508. \brief configure the I/O compensation cell
  509. \param[in] compensation: specifies the I/O compensation cell mode
  510. only one parameter can be selected which are shown as below:
  511. \arg GPIO_COMPENSATION_ENABLE: I/O compensation cell is enabled
  512. \arg GPIO_COMPENSATION_DISABLE: I/O compensation cell is disabled
  513. \param[out] none
  514. \retval none
  515. */
  516. void gpio_compensation_config(uint32_t compensation)
  517. {
  518. uint32_t reg;
  519. reg = AFIO_CPSCTL;
  520. /* reset the AFIO_CPSCTL_CPS_EN bit and set according to gpio_compensation */
  521. reg &= ~AFIO_CPSCTL_CPS_EN;
  522. AFIO_CPSCTL = (reg | compensation);
  523. }
  524. /*!
  525. \brief check the I/O compensation cell is ready or not
  526. \param[in] none
  527. \param[out] none
  528. \retval FlagStatus: SET or RESET
  529. */
  530. FlagStatus gpio_compensation_flag_get(void)
  531. {
  532. if(((uint32_t)RESET) != (AFIO_CPSCTL & AFIO_CPSCTL_CPS_RDY)){
  533. return SET;
  534. }else{
  535. return RESET;
  536. }
  537. }