gd32e23x_gpio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*!
  2. \file gd32e23x_gpio.c
  3. \brief GPIO driver
  4. \version 2019-02-19, V1.0.0, firmware for GD32E23x
  5. */
  6. /*
  7. Copyright (c) 2019, GigaDevice Semiconductor Inc.
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without modification,
  10. are permitted provided that the following conditions are met:
  11. 1. Redistributions of source code must retain the above copyright notice, this
  12. list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. 3. Neither the name of the copyright holder nor the names of its contributors
  17. may be used to endorse or promote products derived from this software without
  18. specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. OF SUCH DAMAGE.
  29. */
  30. #include "gd32e23x_gpio.h"
  31. /*!
  32. \brief reset GPIO port
  33. \param[in] gpio_periph: GPIOx(x = A,B,C,F)
  34. only one parameter can be selected which is shown as below:
  35. \arg GPIOx(x = A,B,C,F)
  36. \param[out] none
  37. \retval none
  38. */
  39. void gpio_deinit(uint32_t gpio_periph)
  40. {
  41. switch(gpio_periph){
  42. case GPIOA:
  43. /* reset GPIOA */
  44. rcu_periph_reset_enable(RCU_GPIOARST);
  45. rcu_periph_reset_disable(RCU_GPIOARST);
  46. break;
  47. case GPIOB:
  48. /* reset GPIOB */
  49. rcu_periph_reset_enable(RCU_GPIOBRST);
  50. rcu_periph_reset_disable(RCU_GPIOBRST);
  51. break;
  52. case GPIOC:
  53. /* reset GPIOC */
  54. rcu_periph_reset_enable(RCU_GPIOCRST);
  55. rcu_periph_reset_disable(RCU_GPIOCRST);
  56. break;
  57. case GPIOF:
  58. /* reset GPIOF */
  59. rcu_periph_reset_enable(RCU_GPIOFRST);
  60. rcu_periph_reset_disable(RCU_GPIOFRST);
  61. break;
  62. default:
  63. break;
  64. }
  65. }
  66. /*!
  67. \brief set GPIO mode
  68. \param[in] gpio_periph: GPIOx(x = A,B,C,F)
  69. only one parameter can be selected which is shown as below:
  70. \arg GPIOx(x = A,B,C,F)
  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: floating mode, no pull-up and pull-down resistors
  78. \arg GPIO_PUPD_PULLUP: with pull-up resistor
  79. \arg GPIO_PUPD_PULLDOWN:with pull-down resistor
  80. \param[in] pin: GPIO pin
  81. one or more parameters can be selected which are shown as below:
  82. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  83. \param[out] none
  84. \retval none
  85. */
  86. void gpio_mode_set(uint32_t gpio_periph, uint32_t mode, uint32_t pull_up_down, uint32_t pin)
  87. {
  88. uint16_t i;
  89. uint32_t ctl, pupd;
  90. ctl = GPIO_CTL(gpio_periph);
  91. pupd = GPIO_PUD(gpio_periph);
  92. for(i = 0U;i < 16U;i++){
  93. if((1U << i) & pin){
  94. /* clear the specified pin mode bits */
  95. ctl &= ~GPIO_MODE_MASK(i);
  96. /* set the specified pin mode bits */
  97. ctl |= GPIO_MODE_SET(i, mode);
  98. /* clear the specified pin pupd bits */
  99. pupd &= ~GPIO_PUPD_MASK(i);
  100. /* set the specified pin pupd bits */
  101. pupd |= GPIO_PUPD_SET(i, pull_up_down);
  102. }
  103. }
  104. GPIO_CTL(gpio_periph) = ctl;
  105. GPIO_PUD(gpio_periph) = pupd;
  106. }
  107. /*!
  108. \brief set GPIO output type and speed
  109. \param[in] gpio_periph: GPIOx(x = A,B,C,F)
  110. only one parameter can be selected which is shown as below:
  111. \arg GPIOx(x = A,B,C,F)
  112. \param[in] otype: gpio pin output mode
  113. \arg GPIO_OTYPE_PP: push pull mode
  114. \arg GPIO_OTYPE_OD: open drain mode
  115. \param[in] speed: gpio pin output max speed
  116. \arg GPIO_OSPEED_2MHZ: output max speed 2MHz
  117. \arg GPIO_OSPEED_10MHZ: output max speed 10MHz
  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_output_options_set(uint32_t gpio_periph, uint8_t otype, uint32_t speed, uint32_t pin)
  126. {
  127. uint16_t i;
  128. uint32_t ospeed;
  129. if(GPIO_OTYPE_OD == otype){
  130. GPIO_OMODE(gpio_periph) |= (uint32_t)pin;
  131. }else{
  132. GPIO_OMODE(gpio_periph) &= (uint32_t)(~pin);
  133. }
  134. /* get the specified pin output speed bits value */
  135. ospeed = GPIO_OSPD(gpio_periph);
  136. for(i = 0U;i < 16U;i++){
  137. if((1U << i) & pin){
  138. /* clear the specified pin output speed bits */
  139. ospeed &= ~GPIO_OSPEED_MASK(i);
  140. /* set the specified pin output speed bits */
  141. ospeed |= GPIO_OSPEED_SET(i,speed);
  142. }
  143. }
  144. GPIO_OSPD(gpio_periph) = ospeed;
  145. }
  146. /*!
  147. \brief set GPIO pin bit
  148. \param[in] gpio_periph: GPIOx(x = A,B,C,F)
  149. only one parameter can be selected which is shown as below:
  150. \arg GPIOx(x = A,B,C,F)
  151. \param[in] pin: GPIO pin
  152. one or more parameters can be selected which are shown as below:
  153. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  154. \param[out] none
  155. \retval none
  156. */
  157. void gpio_bit_set(uint32_t gpio_periph, uint32_t pin)
  158. {
  159. GPIO_BOP(gpio_periph) = (uint32_t)pin;
  160. }
  161. /*!
  162. \brief reset GPIO pin bit
  163. \param[in] gpio_periph: GPIOx(x = A,B,C,F)
  164. only one parameter can be selected which is shown as below:
  165. \arg GPIOx(x = A,B,C,F)
  166. \param[in] pin: GPIO pin
  167. one or more parameters can be selected which are shown as below:
  168. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  169. \param[out] none
  170. \retval none
  171. */
  172. void gpio_bit_reset(uint32_t gpio_periph, uint32_t pin)
  173. {
  174. GPIO_BC(gpio_periph) = (uint32_t)pin;
  175. }
  176. /*!
  177. \brief write data to the specified GPIO pin
  178. \param[in] gpio_periph: GPIOx(x = A,B,C,F)
  179. only one parameter can be selected which is shown as below:
  180. \arg GPIOx(x = A,B,C,F)
  181. \param[in] pin: GPIO pin
  182. one or more parameters can be selected which are shown as below:
  183. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  184. \param[in] bit_value: SET or RESET
  185. \arg RESET: clear the port pin
  186. \arg SET: set the port pin
  187. \param[out] none
  188. \retval none
  189. */
  190. void gpio_bit_write(uint32_t gpio_periph, uint32_t pin, bit_status bit_value)
  191. {
  192. if(RESET != bit_value){
  193. GPIO_BOP(gpio_periph) = (uint32_t)pin;
  194. }else{
  195. GPIO_BC(gpio_periph) = (uint32_t)pin;
  196. }
  197. }
  198. /*!
  199. \brief write data to the specified GPIO port
  200. \param[in] gpio_periph: GPIOx(x = A,B,C,F)
  201. only one parameter can be selected which is shown as below:
  202. \arg GPIOx(x = A,B,C,F)
  203. \param[in] data: specify the value to be written to the port output control register
  204. \param[out] none
  205. \retval none
  206. */
  207. void gpio_port_write(uint32_t gpio_periph, uint16_t data)
  208. {
  209. GPIO_OCTL(gpio_periph) = (uint32_t)data;
  210. }
  211. /*!
  212. \brief get GPIO pin input status
  213. \param[in] gpio_periph: GPIOx(x = A,B,C,F)
  214. only one parameter can be selected which is shown as below:
  215. \arg GPIOx(x = A,B,C,F)
  216. \param[in] pin: GPIO pin
  217. one or more parameters can be selected which are shown as below:
  218. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  219. \param[out] none
  220. \retval SET or RESET
  221. */
  222. FlagStatus gpio_input_bit_get(uint32_t gpio_periph, uint32_t pin)
  223. {
  224. if((uint32_t)RESET != (GPIO_ISTAT(gpio_periph)&(pin))){
  225. return SET;
  226. }else{
  227. return RESET;
  228. }
  229. }
  230. /*!
  231. \brief get GPIO all pins input status
  232. \param[in] gpio_periph: GPIOx(x = A,B,C,F)
  233. only one parameter can be selected which is shown as below:
  234. \arg GPIOx(x = A,B,C,F)
  235. \param[out] none
  236. \retval state of GPIO all pins
  237. */
  238. uint16_t gpio_input_port_get(uint32_t gpio_periph)
  239. {
  240. return (uint16_t)GPIO_ISTAT(gpio_periph);
  241. }
  242. /*!
  243. \brief get GPIO pin output status
  244. \param[in] gpio_periph: GPIOx(x = A,B,C,F)
  245. only one parameter can be selected which is shown as below:
  246. \arg GPIOx(x = A,B,C,F)
  247. \param[in] pin: GPIO pin
  248. one or more parameters can be selected which are shown as below:
  249. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  250. \param[out] none
  251. \retval SET or RESET
  252. */
  253. FlagStatus gpio_output_bit_get(uint32_t gpio_periph, uint32_t pin)
  254. {
  255. if((uint32_t)RESET != (GPIO_OCTL(gpio_periph)&(pin))){
  256. return SET;
  257. }else{
  258. return RESET;
  259. }
  260. }
  261. /*!
  262. \brief get GPIO all pins output status
  263. \param[in] gpio_periph: GPIOx(x = A,B,C,F)
  264. only one parameter can be selected which is shown as below:
  265. \arg GPIOx(x = A,B,C,F)
  266. \param[out] none
  267. \retval state of GPIO all pins
  268. */
  269. uint16_t gpio_output_port_get(uint32_t gpio_periph)
  270. {
  271. return (uint16_t)GPIO_OCTL(gpio_periph);
  272. }
  273. /*!
  274. \brief set GPIO alternate function
  275. \param[in] gpio_periph: GPIOx(x = A,B,C)
  276. only one parameter can be selected which is shown as below:
  277. \arg GPIOx(x = A,B,C)
  278. \param[in] alt_func_num: GPIO pin af function, please refer to specific device datasheet
  279. \arg GPIO_AF_0: TIMER13, TIMER14, TIMER16, SPI0, SPI1, I2S0, CK_OUT, USART0,
  280. I2C0, I2C1, SWDIO, SWCLK
  281. \arg GPIO_AF_1: USART0, USART1, TIMER2, TIMER14, I2C0, I2C1
  282. \arg GPIO_AF_2: TIMER0, TIMER1, TIMER15, TIMER16, I2S0
  283. \arg GPIO_AF_3: I2C0, TIMER14
  284. \arg GPIO_AF_4(port A,B only): USART1, I2C0, I2C1, TIMER13
  285. \arg GPIO_AF_5(port A,B only): TIMER15, TIMER16, I2S0
  286. \arg GPIO_AF_6(port A,B only): SPI1
  287. \arg GPIO_AF_7(port A,B only): CMP
  288. \param[in] pin: GPIO pin
  289. one or more parameters can be selected which are shown as below:
  290. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  291. \param[out] none
  292. \retval none
  293. */
  294. void gpio_af_set(uint32_t gpio_periph, uint32_t alt_func_num, uint32_t pin)
  295. {
  296. uint16_t i;
  297. uint32_t afrl, afrh;
  298. afrl = GPIO_AFSEL0(gpio_periph);
  299. afrh = GPIO_AFSEL1(gpio_periph);
  300. for(i = 0U;i < 8U;i++){
  301. if((1U << i) & pin){
  302. /* clear the specified pin alternate function bits */
  303. afrl &= ~GPIO_AFR_MASK(i);
  304. afrl |= GPIO_AFR_SET(i,alt_func_num);
  305. }
  306. }
  307. for(i = 8U;i < 16U;i++){
  308. if((1U << i) & pin){
  309. /* clear the specified pin alternate function bits */
  310. afrh &= ~GPIO_AFR_MASK(i - 8U);
  311. afrh |= GPIO_AFR_SET(i - 8U,alt_func_num);
  312. }
  313. }
  314. GPIO_AFSEL0(gpio_periph) = afrl;
  315. GPIO_AFSEL1(gpio_periph) = afrh;
  316. }
  317. /*!
  318. \brief lock GPIO pin bit
  319. \param[in] gpio_periph: GPIOx(x = A,B)
  320. only one parameter can be selected which is shown as below:
  321. \arg GPIOx(x = A,B)
  322. \param[in] pin: GPIO pin
  323. one or more parameters can be selected which are shown as below:
  324. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  325. \param[out] none
  326. \retval none
  327. */
  328. void gpio_pin_lock(uint32_t gpio_periph, uint32_t pin)
  329. {
  330. uint32_t lock = 0x00010000U;
  331. lock |= pin;
  332. /* lock key writing sequence: write 1->write 0->write 1->read 0->read 1 */
  333. GPIO_LOCK(gpio_periph) = (uint32_t)lock;
  334. GPIO_LOCK(gpio_periph) = (uint32_t)pin;
  335. GPIO_LOCK(gpio_periph) = (uint32_t)lock;
  336. lock = GPIO_LOCK(gpio_periph);
  337. lock = GPIO_LOCK(gpio_periph);
  338. }
  339. /*!
  340. \brief toggle GPIO pin status
  341. \param[in] gpio_periph: GPIOx(x = A,B,C,F)
  342. only one parameter can be selected which is shown as below:
  343. \arg GPIOx(x = A,B,C,F)
  344. \param[in] pin: GPIO pin
  345. one or more parameters can be selected which are shown as below:
  346. \arg GPIO_PIN_x(x=0..15), GPIO_PIN_ALL
  347. \param[out] none
  348. \retval none
  349. */
  350. void gpio_bit_toggle(uint32_t gpio_periph, uint32_t pin)
  351. {
  352. GPIO_TG(gpio_periph) = (uint32_t)pin;
  353. }
  354. /*!
  355. \brief toggle GPIO port status
  356. \param[in] gpio_periph: GPIOx(x = A,B,C,F)
  357. only one parameter can be selected which is shown as below:
  358. \arg GPIOx(x = A,B,C,F)
  359. \param[out] none
  360. \retval none
  361. */
  362. void gpio_port_toggle(uint32_t gpio_periph)
  363. {
  364. GPIO_TG(gpio_periph) = 0x0000FFFFU;
  365. }