at32f421_gpio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /**
  2. **************************************************************************
  3. * @file at32f421_gpio.c
  4. * @brief contains all the functions for the gpio firmware library
  5. **************************************************************************
  6. * Copyright notice & Disclaimer
  7. *
  8. * The software Board Support Package (BSP) that is made available to
  9. * download from Artery official website is the copyrighted work of Artery.
  10. * Artery authorizes customers to use, copy, and distribute the BSP
  11. * software and its related documentation for the purpose of design and
  12. * development in conjunction with Artery microcontrollers. Use of the
  13. * software is governed by this copyright notice and the following disclaimer.
  14. *
  15. * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  16. * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  17. * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  18. * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  19. * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  21. *
  22. **************************************************************************
  23. */
  24. #include "at32f421_conf.h"
  25. /** @addtogroup AT32F421_periph_driver
  26. * @{
  27. */
  28. /** @defgroup GPIO
  29. * @brief GPIO driver modules
  30. * @{
  31. */
  32. #ifdef GPIO_MODULE_ENABLED
  33. /** @defgroup GPIO_private_functions
  34. * @{
  35. */
  36. /**
  37. * @brief reset the gpio register
  38. * @param gpio_x: to select the gpio peripheral.
  39. * this parameter can be one of the following values:
  40. * GPIOA, GPIOB, GPIOC, GPIOF.
  41. * @retval none
  42. */
  43. void gpio_reset(gpio_type *gpio_x)
  44. {
  45. if(gpio_x == GPIOA)
  46. {
  47. crm_periph_reset(CRM_GPIOA_PERIPH_RESET, TRUE);
  48. crm_periph_reset(CRM_GPIOA_PERIPH_RESET, FALSE);
  49. }
  50. else if(gpio_x == GPIOB)
  51. {
  52. crm_periph_reset(CRM_GPIOB_PERIPH_RESET, TRUE);
  53. crm_periph_reset(CRM_GPIOB_PERIPH_RESET, FALSE);
  54. }
  55. else if(gpio_x == GPIOC)
  56. {
  57. crm_periph_reset(CRM_GPIOC_PERIPH_RESET, TRUE);
  58. crm_periph_reset(CRM_GPIOC_PERIPH_RESET, FALSE);
  59. }
  60. else if(gpio_x == GPIOF)
  61. {
  62. crm_periph_reset(CRM_GPIOF_PERIPH_RESET, TRUE);
  63. crm_periph_reset(CRM_GPIOF_PERIPH_RESET, FALSE);
  64. }
  65. }
  66. /**
  67. * @brief initialize the gpio peripheral.
  68. * @param gpio_x: to select the gpio peripheral.
  69. * this parameter can be one of the following values:
  70. * GPIOA, GPIOB, GPIOC, GPIOF.
  71. * @param gpio_init_struct: pointer to gpio init structure.
  72. * @retval none
  73. */
  74. void gpio_init(gpio_type *gpio_x, gpio_init_type *gpio_init_struct)
  75. {
  76. uint16_t pinx_value, pin_index = 0;
  77. pinx_value = (uint16_t)gpio_init_struct->gpio_pins;
  78. while(pinx_value > 0)
  79. {
  80. if(pinx_value & 0x01)
  81. {
  82. gpio_x->cfgr &= (uint32_t)~(0x03 << (pin_index * 2));
  83. gpio_x->cfgr |= (uint32_t)(gpio_init_struct->gpio_mode << (pin_index * 2));
  84. gpio_x->omode &= (uint32_t)~(0x01 << (pin_index));
  85. gpio_x->omode |= (uint32_t)(gpio_init_struct->gpio_out_type << (pin_index));
  86. gpio_x->odrvr &= (uint32_t)~(0x03 << (pin_index * 2));
  87. gpio_x->odrvr |= (uint32_t)(gpio_init_struct->gpio_drive_strength << (pin_index * 2));
  88. gpio_x->pull &= (uint32_t)~(0x03 << (pin_index * 2));
  89. gpio_x->pull |= (uint32_t)(gpio_init_struct->gpio_pull << (pin_index * 2));
  90. }
  91. pinx_value >>= 1;
  92. pin_index++;
  93. }
  94. }
  95. /**
  96. * @brief fill each gpio_init_type member with its default value.
  97. * @param gpio_init_struct : pointer to a gpio_init_type structure which will be initialized.
  98. * @retval none
  99. */
  100. void gpio_default_para_init(gpio_init_type *gpio_init_struct)
  101. {
  102. /* reset gpio init structure parameters values */
  103. gpio_init_struct->gpio_pins = GPIO_PINS_ALL;
  104. gpio_init_struct->gpio_mode = GPIO_MODE_INPUT;
  105. gpio_init_struct->gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  106. gpio_init_struct->gpio_pull = GPIO_PULL_NONE;
  107. gpio_init_struct->gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  108. }
  109. /**
  110. * @brief read the specified input port pin.
  111. * @param gpio_x: to select the gpio peripheral.
  112. * this parameter can be one of the following values:
  113. * GPIOA, GPIOB, GPIOC, GPIOF.
  114. * @param pins: gpio pin number
  115. * this parameter can be one of the following values:
  116. * - GPIO_PINS_0
  117. * - GPIO_PINS_1
  118. * - GPIO_PINS_2
  119. * - GPIO_PINS_3
  120. * - GPIO_PINS_4
  121. * - GPIO_PINS_5
  122. * - GPIO_PINS_6
  123. * - GPIO_PINS_7
  124. * - GPIO_PINS_8
  125. * - GPIO_PINS_9
  126. * - GPIO_PINS_10
  127. * - GPIO_PINS_11
  128. * - GPIO_PINS_12
  129. * - GPIO_PINS_13
  130. * - GPIO_PINS_14
  131. * - GPIO_PINS_15
  132. * @retval flag_status (SET or RESET)
  133. */
  134. flag_status gpio_input_data_bit_read(gpio_type *gpio_x, uint16_t pins)
  135. {
  136. flag_status status = RESET;
  137. if(pins != (pins & gpio_x->idt))
  138. {
  139. status = RESET;
  140. }
  141. else
  142. {
  143. status = SET;
  144. }
  145. return status;
  146. }
  147. /**
  148. * @brief read the specified gpio input data port.
  149. * @param gpio_x: to select the gpio peripheral.
  150. * this parameter can be one of the following values:
  151. * GPIOA, GPIOB, GPIOC, GPIOF.
  152. * @retval gpio input data port value.
  153. */
  154. uint16_t gpio_input_data_read(gpio_type *gpio_x)
  155. {
  156. return ((uint16_t)(gpio_x->idt));
  157. }
  158. /**
  159. * @brief read the specified output port pin.
  160. * @param gpio_x: to select the gpio peripheral.
  161. * this parameter can be one of the following values:
  162. * GPIOA, GPIOB, GPIOC, GPIOF.
  163. * @param pins: gpio pin number
  164. * this parameter can be one of the following values:
  165. * - GPIO_PINS_0
  166. * - GPIO_PINS_1
  167. * - GPIO_PINS_2
  168. * - GPIO_PINS_3
  169. * - GPIO_PINS_4
  170. * - GPIO_PINS_5
  171. * - GPIO_PINS_6
  172. * - GPIO_PINS_7
  173. * - GPIO_PINS_8
  174. * - GPIO_PINS_9
  175. * - GPIO_PINS_10
  176. * - GPIO_PINS_11
  177. * - GPIO_PINS_12
  178. * - GPIO_PINS_13
  179. * - GPIO_PINS_14
  180. * - GPIO_PINS_15
  181. * @retval flag_status (SET or RESET)
  182. */
  183. flag_status gpio_output_data_bit_read(gpio_type *gpio_x, uint16_t pins)
  184. {
  185. flag_status status = RESET;
  186. if((gpio_x->odt & pins) != RESET)
  187. {
  188. status = SET;
  189. }
  190. else
  191. {
  192. status = RESET;
  193. }
  194. return status;
  195. }
  196. /**
  197. * @brief read the specified gpio ouput data port.
  198. * @param gpio_x: to select the gpio peripheral.
  199. * this parameter can be one of the following values:
  200. * GPIOA, GPIOB, GPIOC, GPIOF.
  201. * @retval gpio input data port value.
  202. */
  203. uint16_t gpio_output_data_read(gpio_type *gpio_x)
  204. {
  205. return ((uint16_t)(gpio_x->odt));
  206. }
  207. /**
  208. * @brief set the selected data port bits.
  209. * @param gpio_x: to select the gpio peripheral.
  210. * this parameter can be one of the following values:
  211. * GPIOA, GPIOB, GPIOC, GPIOF.
  212. * @param pins: gpio pin number
  213. * parameter can be any combination of gpio_pin_x, gpio_pin_x as following values:
  214. * - GPIO_PINS_0
  215. * - GPIO_PINS_1
  216. * - GPIO_PINS_2
  217. * - GPIO_PINS_3
  218. * - GPIO_PINS_4
  219. * - GPIO_PINS_5
  220. * - GPIO_PINS_6
  221. * - GPIO_PINS_7
  222. * - GPIO_PINS_8
  223. * - GPIO_PINS_9
  224. * - GPIO_PINS_10
  225. * - GPIO_PINS_11
  226. * - GPIO_PINS_12
  227. * - GPIO_PINS_13
  228. * - GPIO_PINS_14
  229. * - GPIO_PINS_15
  230. * - GPIO_PINS_ALL
  231. * @retval none
  232. */
  233. void gpio_bits_set(gpio_type *gpio_x, uint16_t pins)
  234. {
  235. gpio_x->scr = pins;
  236. }
  237. /**
  238. * @brief clear the selected data port bits.
  239. * @param gpio_x: to select the gpio peripheral.
  240. * this parameter can be one of the following values:
  241. * GPIOA, GPIOB, GPIOC, GPIOF.
  242. * @param pins: gpio pin number
  243. * parameter can be any combination of gpio_pin_x, gpio_pin_x as following values:
  244. * - GPIO_PINS_0
  245. * - GPIO_PINS_1
  246. * - GPIO_PINS_2
  247. * - GPIO_PINS_3
  248. * - GPIO_PINS_4
  249. * - GPIO_PINS_5
  250. * - GPIO_PINS_6
  251. * - GPIO_PINS_7
  252. * - GPIO_PINS_8
  253. * - GPIO_PINS_9
  254. * - GPIO_PINS_10
  255. * - GPIO_PINS_11
  256. * - GPIO_PINS_12
  257. * - GPIO_PINS_13
  258. * - GPIO_PINS_14
  259. * - GPIO_PINS_15
  260. * - GPIO_PINS_ALL
  261. * @retval none
  262. */
  263. void gpio_bits_reset(gpio_type *gpio_x, uint16_t pins)
  264. {
  265. gpio_x->clr = pins;
  266. }
  267. /**
  268. * @brief set or clear the selected data port bit.
  269. * @param gpio_x: to select the gpio peripheral.
  270. * this parameter can be one of the following values:
  271. * GPIOA, GPIOB, GPIOC, GPIOF.
  272. * @param pins: gpio pin number
  273. * parameter can be any combination of gpio_pin_x, gpio_pin_x as following values:
  274. * - GPIO_PINS_0
  275. * - GPIO_PINS_1
  276. * - GPIO_PINS_2
  277. * - GPIO_PINS_3
  278. * - GPIO_PINS_4
  279. * - GPIO_PINS_5
  280. * - GPIO_PINS_6
  281. * - GPIO_PINS_7
  282. * - GPIO_PINS_8
  283. * - GPIO_PINS_9
  284. * - GPIO_PINS_10
  285. * - GPIO_PINS_11
  286. * - GPIO_PINS_12
  287. * - GPIO_PINS_13
  288. * - GPIO_PINS_14
  289. * - GPIO_PINS_15
  290. * - GPIO_PINS_ALL
  291. * @param bit_state: specifies the value to be written to the selected bit (TRUE or FALSE).
  292. * @retval none
  293. */
  294. void gpio_bits_write(gpio_type *gpio_x, uint16_t pins, confirm_state bit_state)
  295. {
  296. if(bit_state != FALSE)
  297. {
  298. gpio_x->scr = pins;
  299. }
  300. else
  301. {
  302. gpio_x->clr = pins;
  303. }
  304. }
  305. /**
  306. * @brief write data to the specified gpio data port.
  307. * @param gpio_x: to select the gpio peripheral.
  308. * this parameter can be one of the following values:
  309. * GPIOA, GPIOB, GPIOC, GPIOF.
  310. * @param port_value: specifies the value to be written to the port output data register.
  311. * @retval none
  312. */
  313. void gpio_port_write(gpio_type *gpio_x, uint16_t port_value)
  314. {
  315. gpio_x->odt = port_value;
  316. }
  317. /**
  318. * @brief write protect gpio pins configuration registers.
  319. * @param gpio_x: to select the gpio peripheral.
  320. * this parameter can be one of the following values:
  321. * GPIOA, GPIOB, GPIOC, GPIOF.
  322. * @param pins: gpio pin number
  323. * this parameter can be any combination of the following:
  324. * - GPIO_PINS_0
  325. * - GPIO_PINS_1
  326. * - GPIO_PINS_2
  327. * - GPIO_PINS_3
  328. * - GPIO_PINS_4
  329. * - GPIO_PINS_5
  330. * - GPIO_PINS_6
  331. * - GPIO_PINS_7
  332. * - GPIO_PINS_8
  333. * - GPIO_PINS_9
  334. * - GPIO_PINS_10
  335. * - GPIO_PINS_11
  336. * - GPIO_PINS_12
  337. * - GPIO_PINS_13
  338. * - GPIO_PINS_14
  339. * - GPIO_PINS_15
  340. * - GPIO_PINS_ALL
  341. * @retval none
  342. */
  343. void gpio_pin_wp_config(gpio_type *gpio_x, uint16_t pins)
  344. {
  345. uint32_t temp = 0x00010000;
  346. temp |= pins;
  347. /* set wpen bit */
  348. gpio_x->wpr = temp;
  349. /* reset wpen bit */
  350. gpio_x->wpr = pins;
  351. /* set wpen bit */
  352. gpio_x->wpr = temp;
  353. /* read wpen bit*/
  354. temp = gpio_x->wpr;
  355. /* read wpen bit*/
  356. temp = gpio_x->wpr;
  357. }
  358. /**
  359. * @brief enable or disable gpio pins huge driven.
  360. * @param gpio_x: to select the gpio peripheral.
  361. * this parameter can be one of the following values:
  362. * GPIOA, GPIOB, GPIOC, GPIOF.
  363. * @param pins: gpio pin number
  364. * parameter can be any combination of gpio_pin_x, gpio_pin_x as following values:
  365. * - GPIO_PINS_0
  366. * - GPIO_PINS_1
  367. * - GPIO_PINS_2
  368. * - GPIO_PINS_3
  369. * - GPIO_PINS_4
  370. * - GPIO_PINS_5
  371. * - GPIO_PINS_6
  372. * - GPIO_PINS_7
  373. * - GPIO_PINS_8
  374. * - GPIO_PINS_9
  375. * - GPIO_PINS_10
  376. * - GPIO_PINS_11
  377. * - GPIO_PINS_12
  378. * - GPIO_PINS_13
  379. * - GPIO_PINS_14
  380. * - GPIO_PINS_15
  381. * - GPIO_PINS_ALL
  382. * @param new_state: new state of the slew rate.
  383. * this parameter can be: true or false.
  384. * @retval none
  385. */
  386. void gpio_pins_huge_driven_config(gpio_type *gpio_x, uint16_t pins, confirm_state new_state)
  387. {
  388. if(new_state != FALSE)
  389. {
  390. gpio_x->hdrv |= pins;
  391. }
  392. else
  393. {
  394. gpio_x->hdrv &= ~pins;
  395. }
  396. }
  397. /**
  398. * @brief configure the pin's muxing function.
  399. * @param gpio_x: to select the gpio peripheral.
  400. * this parameter can be one of the following values:
  401. * GPIOA, GPIOB, GPIOC, GPIOF.
  402. * @param gpio_pin_source: specifies the pin for the muxing function.
  403. * this parameter can be one of the following values:
  404. * - GPIO_PINS_SOURCE0
  405. * - GPIO_PINS_SOURCE1
  406. * - GPIO_PINS_SOURCE2
  407. * - GPIO_PINS_SOURCE3
  408. * - GPIO_PINS_SOURCE4
  409. * - GPIO_PINS_SOURCE5
  410. * - GPIO_PINS_SOURCE6
  411. * - GPIO_PINS_SOURCE7
  412. * - GPIO_PINS_SOURCE8
  413. * - GPIO_PINS_SOURCE9
  414. * - GPIO_PINS_SOURCE10
  415. * - GPIO_PINS_SOURCE11
  416. * - GPIO_PINS_SOURCE12
  417. * - GPIO_PINS_SOURCE13
  418. * - GPIO_PINS_SOURCE14
  419. * - GPIO_PINS_SOURCE15
  420. * @param gpio_mux: select the pin to used as muxing function.
  421. * this parameter can be one of the following values:
  422. * - GPIO_MUX_0
  423. * - GPIO_MUX_1
  424. * - GPIO_MUX_2
  425. * - GPIO_MUX_3
  426. * - GPIO_MUX_4
  427. * - GPIO_MUX_5
  428. * - GPIO_MUX_6
  429. * - GPIO_MUX_7
  430. * @retval none
  431. */
  432. void gpio_pin_mux_config(gpio_type *gpio_x, gpio_pins_source_type gpio_pin_source, gpio_mux_sel_type gpio_mux)
  433. {
  434. uint32_t temp = 0x00;
  435. uint32_t temp_2 = 0x00;
  436. temp = ((uint32_t)(gpio_mux) << ((uint32_t)((uint32_t)gpio_pin_source & (uint32_t)0x07) * 4));
  437. if(gpio_pin_source >> 0x03)
  438. {
  439. gpio_x->muxh &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)gpio_pin_source & (uint32_t)0x07) * 4));
  440. temp_2 = gpio_x->muxh | temp;
  441. gpio_x->muxh = temp_2;
  442. }
  443. else
  444. {
  445. gpio_x->muxl &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)gpio_pin_source & (uint32_t)0x07) * 4));
  446. temp_2 = gpio_x->muxl | temp;
  447. gpio_x->muxl = temp_2;
  448. }
  449. }
  450. /**
  451. * @}
  452. */
  453. #endif
  454. /**
  455. * @}
  456. */
  457. /**
  458. * @}
  459. */