gd32f4xx_gpio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*!
  2. \file gd32f4xx_gpio.c
  3. \brief GPIO driver
  4. */
  5. /*
  6. Copyright (C) 2016 GigaDevice
  7. 2016-08-15, V1.0.0, firmware for GD32F4xx
  8. */
  9. #include "gd32f4xx_gpio.h"
  10. /*!
  11. \brief reset GPIO port
  12. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G,H,I)
  13. \param[out] none
  14. \retval none
  15. */
  16. void gpio_deinit(uint32_t gpio_periph)
  17. {
  18. switch(gpio_periph){
  19. case GPIOA:
  20. /* reset GPIOA */
  21. rcu_periph_reset_enable(RCU_GPIOARST);
  22. rcu_periph_reset_disable(RCU_GPIOARST);
  23. break;
  24. case GPIOB:
  25. /* reset GPIOB */
  26. rcu_periph_reset_enable(RCU_GPIOBRST);
  27. rcu_periph_reset_disable(RCU_GPIOBRST);
  28. break;
  29. case GPIOC:
  30. /* reset GPIOC */
  31. rcu_periph_reset_enable(RCU_GPIOCRST);
  32. rcu_periph_reset_disable(RCU_GPIOCRST);
  33. break;
  34. case GPIOD:
  35. /* reset GPIOD */
  36. rcu_periph_reset_enable(RCU_GPIODRST);
  37. rcu_periph_reset_disable(RCU_GPIODRST);
  38. break;
  39. case GPIOE:
  40. /* reset GPIOE */
  41. rcu_periph_reset_enable(RCU_GPIOERST);
  42. rcu_periph_reset_disable(RCU_GPIOERST);
  43. break;
  44. case GPIOF:
  45. /* reset GPIOF */
  46. rcu_periph_reset_enable(RCU_GPIOFRST);
  47. rcu_periph_reset_disable(RCU_GPIOFRST);
  48. break;
  49. case GPIOG:
  50. /* reset GPIOG */
  51. rcu_periph_reset_enable(RCU_GPIOGRST);
  52. rcu_periph_reset_disable(RCU_GPIOGRST);
  53. break;
  54. case GPIOH:
  55. /* reset GPIOH */
  56. rcu_periph_reset_enable(RCU_GPIOHRST);
  57. rcu_periph_reset_disable(RCU_GPIOHRST);
  58. break;
  59. case GPIOI:
  60. /* reset GPIOI */
  61. rcu_periph_reset_enable(RCU_GPIOIRST);
  62. rcu_periph_reset_disable(RCU_GPIOIRST);
  63. break;
  64. default:
  65. break;
  66. }
  67. }
  68. /*!
  69. \brief set GPIO output mode
  70. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G,H,I)
  71. \param[in] mode: gpio pin mode
  72. \arg GPIO_MODE_INPUT: input mode
  73. \arg GPIO_MODE_OUTPUT: output mode
  74. \arg GPIO_MODE_AF: alternate function mode
  75. \arg GPIO_MODE_ANALOG: analog mode
  76. \param[in] pull_up_down: gpio pin with pull-up or pull-down resistor
  77. \arg GPIO_PUPD_NONE: without weak pull-up and pull-down resistors
  78. \arg GPIO_PUPD_PULLUP: with weak pull-up resistor
  79. \arg GPIO_PUPD_PULLDOWN:with weak pull-down resistor
  80. \param[in] pin: GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  81. \param[out] none
  82. \retval none
  83. */
  84. void gpio_mode_set(uint32_t gpio_periph,uint32_t mode,uint32_t pull_up_down,uint32_t pin)
  85. {
  86. uint16_t i;
  87. uint32_t ctl, pupd;
  88. ctl = GPIO_CTL(gpio_periph);
  89. pupd = GPIO_PUD(gpio_periph);
  90. for(i = 0U;i < 16U;i++){
  91. if((1U << i) & pin){
  92. /* clear the specified pin mode bits */
  93. ctl &= ~GPIO_MODE_MASK(i);
  94. /* set the specified pin mode bits */
  95. ctl |= GPIO_MODE_SET(i, mode);
  96. /* clear the specified pin pupd bits */
  97. pupd &= ~GPIO_PUPD_MASK(i);
  98. /* set the specified pin pupd bits */
  99. pupd |= GPIO_PUPD_SET(i, pull_up_down);
  100. }
  101. }
  102. GPIO_CTL(gpio_periph) = ctl;
  103. GPIO_PUD(gpio_periph) = pupd;
  104. }
  105. /*!
  106. \brief set GPIO output type and speed
  107. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G,H,I)
  108. \param[in] otype: gpio pin output mode
  109. \arg GPIO_OTYPE_PP: push pull mode
  110. \arg GPIO_OTYPE_OD: open drain mode
  111. \param[in] speed: gpio pin output max speed
  112. \arg GPIO_OSPEED_2MHZ: output max speed 2M
  113. \arg GPIO_OSPEED_25MHZ: output max speed 25M
  114. \arg GPIO_OSPEED_50MHZ: output max speed 50M
  115. \arg GPIO_OSPEED_200MHZ: output max speed 200M
  116. \param[in] pin: GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  117. \param[out] none
  118. \retval none
  119. */
  120. void gpio_output_options_set(uint32_t gpio_periph,uint8_t otype,uint32_t speed,uint32_t pin)
  121. {
  122. uint16_t i;
  123. uint32_t ospeedr;
  124. if(0x1U == otype){
  125. GPIO_OMODE(gpio_periph) |= (uint32_t)pin;
  126. }else{
  127. GPIO_OMODE(gpio_periph) &= (uint32_t)(~pin);
  128. }
  129. /* get the specified pin output speed bits value */
  130. ospeedr = GPIO_OSPD(gpio_periph);
  131. for(i = 0U;i < 16U;i++){
  132. if((1U << i) & pin){
  133. /* clear the specified pin output speed bits */
  134. ospeedr &= ~GPIO_OSPEED_MASK(i);
  135. /* set the specified pin output speed bits */
  136. ospeedr |= GPIO_OSPEED_SET(i,speed);
  137. }
  138. }
  139. GPIO_OSPD(gpio_periph) = ospeedr;
  140. }
  141. /*!
  142. \brief set GPIO pin
  143. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G,H,I)
  144. \param[in] pin: GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  145. \param[out] none
  146. \retval none
  147. */
  148. void gpio_bit_set(uint32_t gpio_periph,uint32_t pin)
  149. {
  150. GPIO_BOP(gpio_periph) = (uint32_t)pin;
  151. }
  152. /*!
  153. \brief reset GPIO pin
  154. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G,H,I)
  155. \param[in] pin: GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  156. \param[out] none
  157. \retval none
  158. */
  159. void gpio_bit_reset(uint32_t gpio_periph,uint32_t pin)
  160. {
  161. GPIO_BC(gpio_periph) = (uint32_t)pin;
  162. }
  163. /*!
  164. \brief write data to the specified GPIO pin
  165. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G,H,I)
  166. \param[in] pin: GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  167. \param[in] bitvalue: SET or RESET
  168. \arg RESET: clear the port pin
  169. \arg SET: set the port pin
  170. \param[out] none
  171. \retval none
  172. */
  173. void gpio_bit_write(uint32_t gpio_periph,uint32_t pin,bit_status bit_value)
  174. {
  175. if(RESET != bit_value){
  176. GPIO_BOP(gpio_periph) = (uint32_t)pin;
  177. }else{
  178. GPIO_BC(gpio_periph) = (uint32_t)pin;
  179. }
  180. }
  181. /*!
  182. \brief write data to the specified GPIO port
  183. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G,H,I)
  184. \param[in] data: specify the value to be written to the port output data register
  185. \param[out] none
  186. \retval none
  187. */
  188. void gpio_port_write(uint32_t gpio_periph,uint16_t data)
  189. {
  190. GPIO_OCTL(gpio_periph) = (uint32_t)data;
  191. }
  192. /*!
  193. \brief get GPIO pin input status
  194. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G,H,I)
  195. \param[in] pin: GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  196. \param[out] none
  197. \retval input state of gpio pin: SET or RESET
  198. */
  199. FlagStatus gpio_input_bit_get(uint32_t gpio_periph,uint32_t pin)
  200. {
  201. if((uint32_t)RESET != (GPIO_ISTAT(gpio_periph)&(pin))){
  202. return SET;
  203. }else{
  204. return RESET;
  205. }
  206. }
  207. /*!
  208. \brief get GPIO all pins input status
  209. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G,H,I)
  210. \param[out] none
  211. \retval input state of gpio all pins
  212. */
  213. uint16_t gpio_input_port_get(uint32_t gpio_periph)
  214. {
  215. return (uint16_t)(GPIO_ISTAT(gpio_periph));
  216. }
  217. /*!
  218. \brief get GPIO pin output status
  219. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G,H,I)
  220. \param[in] pin: GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  221. \param[out] none
  222. \retval output state of gpio pin: SET or RESET
  223. */
  224. FlagStatus gpio_output_bit_get(uint32_t gpio_periph,uint32_t pin)
  225. {
  226. if((uint32_t)RESET !=(GPIO_OCTL(gpio_periph)&(pin))){
  227. return SET;
  228. }else{
  229. return RESET;
  230. }
  231. }
  232. /*!
  233. \brief get GPIO all pins output status
  234. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G,H,I)
  235. \param[out] none
  236. \retval output state of gpio all pins
  237. */
  238. uint16_t gpio_output_port_get(uint32_t gpio_periph)
  239. {
  240. return ((uint16_t)GPIO_OCTL(gpio_periph));
  241. }
  242. /*!
  243. \brief set GPIO alternate function
  244. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G,H,I)
  245. \param[in] alt_func_num: gpio pin af function
  246. \arg GPIO_AF_0: SYSTEM
  247. \arg GPIO_AF_1: TIMER0, TIMER1
  248. \arg GPIO_AF_2: TIMER2, TIMER3, TIMER4
  249. \arg GPIO_AF_3: TIMER7, TIMER8, TIMER9, TIMER10
  250. \arg GPIO_AF_4: I2C0, I2C1, I2C2
  251. \arg GPIO_AF_5: SPI0, SPI1, SPI2, SPI3, SPI4, SPI5
  252. \arg GPIO_AF_6: SPI1, SPI2, SAI0
  253. \arg GPIO_AF_7: USART0, USART1, USART2
  254. \arg GPIO_AF_8: UART3, UART4, USART5, UART6, UART7
  255. \arg GPIO_AF_9: CAN0,CAN1, TLI, TIMER11, TIMER12, TIMER13
  256. \arg GPIO_AF_10: USB_FS, USB_HS
  257. \arg GPIO_AF_11: ENET
  258. \arg GPIO_AF_12: FMC, SDIO, USB_HS
  259. \arg GPIO_AF_13: DCI
  260. \arg GPIO_AF_14: TLI
  261. \arg GPIO_AF_15: EVENTOUT
  262. \param[in] pin: GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  263. \param[out] none
  264. \retval none
  265. */
  266. void gpio_af_set(uint32_t gpio_periph,uint32_t alt_func_num,uint32_t pin)
  267. {
  268. uint16_t i;
  269. uint32_t afrl, afrh;
  270. afrl = GPIO_AFSEL0(gpio_periph);
  271. afrh = GPIO_AFSEL1(gpio_periph);
  272. for(i = 0U;i < 8U;i++){
  273. if((1U << i) & pin){
  274. /* clear the specified pin alternate function bits */
  275. afrl &= ~GPIO_AFR_MASK(i);
  276. afrl |= GPIO_AFR_SET(i,alt_func_num);
  277. }
  278. }
  279. for(i = 8U;i < 16U;i++){
  280. if((1U << i) & pin){
  281. /* clear the specified pin alternate function bits */
  282. afrh &= ~GPIO_AFR_MASK(i - 8U);
  283. afrh |= GPIO_AFR_SET(i - 8U,alt_func_num);
  284. }
  285. }
  286. GPIO_AFSEL0(gpio_periph) = afrl;
  287. GPIO_AFSEL1(gpio_periph) = afrh;
  288. }
  289. /*!
  290. \brief lock GPIO pin
  291. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G,H,I)
  292. \param[in] pin: GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  293. \param[out] none
  294. \retval none
  295. */
  296. void gpio_pin_lock(uint32_t gpio_periph,uint32_t pin)
  297. {
  298. uint32_t lock = 0x00010000U;
  299. lock |= pin;
  300. /* lock key writing sequence: write 1->write 0->write 1-> read 0-> read 1 */
  301. GPIO_LOCK(gpio_periph) = (uint32_t)lock;
  302. GPIO_LOCK(gpio_periph) = (uint32_t)pin;
  303. GPIO_LOCK(gpio_periph) = (uint32_t)lock;
  304. lock = GPIO_LOCK(gpio_periph);
  305. lock = GPIO_LOCK(gpio_periph);
  306. }
  307. /*!
  308. \brief toggle GPIO pin
  309. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G,H,I)
  310. \param[in] pin: GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  311. \param[out] none
  312. \retval none
  313. */
  314. void gpio_bit_toggle(uint32_t gpio_periph,uint32_t pin)
  315. {
  316. GPIO_TG(gpio_periph) = (uint32_t)pin;
  317. }
  318. /*!
  319. \brief toggle GPIO port
  320. \param[in] gpio_periph: GPIOx(x = A,B,C,D,E,F,G,H,I)
  321. \param[out] none
  322. \retval none
  323. */
  324. void gpio_port_toggle(uint32_t gpio_periph)
  325. {
  326. GPIO_TG(gpio_periph) = 0x0000FFFFU;
  327. }