at32f425_gpio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /**
  2. **************************************************************************
  3. * @file at32f425_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 "at32f425_conf.h"
  25. /** @addtogroup AT32F425_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, GPIOD, 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 == GPIOD)
  61. {
  62. crm_periph_reset(CRM_GPIOD_PERIPH_RESET, TRUE);
  63. crm_periph_reset(CRM_GPIOD_PERIPH_RESET, FALSE);
  64. }
  65. else if(gpio_x == GPIOF)
  66. {
  67. crm_periph_reset(CRM_GPIOF_PERIPH_RESET, TRUE);
  68. crm_periph_reset(CRM_GPIOF_PERIPH_RESET, FALSE);
  69. }
  70. }
  71. /**
  72. * @brief initialize the gpio peripheral.
  73. * @param gpio_x: to select the gpio peripheral.
  74. * this parameter can be one of the following values:
  75. * GPIOA, GPIOB, GPIOC, GPIOD, GPIOF.
  76. * @param gpio_init_struct: pointer to gpio init structure.
  77. * @retval none
  78. */
  79. void gpio_init(gpio_type *gpio_x, gpio_init_type *gpio_init_struct)
  80. {
  81. uint16_t pinx_value, pin_index = 0;
  82. pinx_value = (uint16_t)gpio_init_struct->gpio_pins;
  83. while(pinx_value > 0)
  84. {
  85. if(pinx_value & 0x01)
  86. {
  87. gpio_x->cfgr &= (uint32_t)~(0x03 << (pin_index * 2));
  88. gpio_x->cfgr |= (uint32_t)(gpio_init_struct->gpio_mode << (pin_index * 2));
  89. gpio_x->omode &= (uint32_t)~(0x01 << (pin_index));
  90. gpio_x->omode |= (uint32_t)(gpio_init_struct->gpio_out_type << (pin_index));
  91. gpio_x->odrvr &= (uint32_t)~(0x03 << (pin_index * 2));
  92. gpio_x->odrvr |= (uint32_t)(gpio_init_struct->gpio_drive_strength << (pin_index * 2));
  93. gpio_x->pull &= (uint32_t)~(0x03 << (pin_index * 2));
  94. gpio_x->pull |= (uint32_t)(gpio_init_struct->gpio_pull << (pin_index * 2));
  95. }
  96. pinx_value >>= 1;
  97. pin_index++;
  98. }
  99. }
  100. /**
  101. * @brief fill each gpio_init_type member with its default value.
  102. * @param gpio_init_struct : pointer to a gpio_init_type structure which will be initialized.
  103. * @retval none
  104. */
  105. void gpio_default_para_init(gpio_init_type *gpio_init_struct)
  106. {
  107. /* reset gpio init structure parameters values */
  108. gpio_init_struct->gpio_pins = GPIO_PINS_ALL;
  109. gpio_init_struct->gpio_mode = GPIO_MODE_INPUT;
  110. gpio_init_struct->gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  111. gpio_init_struct->gpio_pull = GPIO_PULL_NONE;
  112. gpio_init_struct->gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  113. }
  114. /**
  115. * @brief read the specified input port pin.
  116. * @param gpio_x: to select the gpio peripheral.
  117. * this parameter can be one of the following values:
  118. * GPIOA, GPIOB, GPIOC, GPIOD, GPIOF.
  119. * @param pins: gpio pin number
  120. * this parameter can be one of the following values:
  121. * - GPIO_PINS_0
  122. * - GPIO_PINS_1
  123. * - GPIO_PINS_2
  124. * - GPIO_PINS_3
  125. * - GPIO_PINS_4
  126. * - GPIO_PINS_5
  127. * - GPIO_PINS_6
  128. * - GPIO_PINS_7
  129. * - GPIO_PINS_8
  130. * - GPIO_PINS_9
  131. * - GPIO_PINS_10
  132. * - GPIO_PINS_11
  133. * - GPIO_PINS_12
  134. * - GPIO_PINS_13
  135. * - GPIO_PINS_14
  136. * - GPIO_PINS_15
  137. * @retval flag_status (SET or RESET)
  138. */
  139. flag_status gpio_input_data_bit_read(gpio_type *gpio_x, uint16_t pins)
  140. {
  141. flag_status status = RESET;
  142. if(pins != (pins & gpio_x->idt))
  143. {
  144. status = RESET;
  145. }
  146. else
  147. {
  148. status = SET;
  149. }
  150. return status;
  151. }
  152. /**
  153. * @brief read the specified gpio input data port.
  154. * @param gpio_x: to select the gpio peripheral.
  155. * this parameter can be one of the following values:
  156. * GPIOA, GPIOB, GPIOC, GPIOD, GPIOF.
  157. * @retval gpio input data port value.
  158. */
  159. uint16_t gpio_input_data_read(gpio_type *gpio_x)
  160. {
  161. return ((uint16_t)(gpio_x->idt));
  162. }
  163. /**
  164. * @brief read the specified output port pin.
  165. * @param gpio_x: to select the gpio peripheral.
  166. * this parameter can be one of the following values:
  167. * GPIOA, GPIOB, GPIOC, GPIOD, GPIOF.
  168. * @param pins: gpio pin number
  169. * this parameter can be one of the following values:
  170. * - GPIO_PINS_0
  171. * - GPIO_PINS_1
  172. * - GPIO_PINS_2
  173. * - GPIO_PINS_3
  174. * - GPIO_PINS_4
  175. * - GPIO_PINS_5
  176. * - GPIO_PINS_6
  177. * - GPIO_PINS_7
  178. * - GPIO_PINS_8
  179. * - GPIO_PINS_9
  180. * - GPIO_PINS_10
  181. * - GPIO_PINS_11
  182. * - GPIO_PINS_12
  183. * - GPIO_PINS_13
  184. * - GPIO_PINS_14
  185. * - GPIO_PINS_15
  186. * @retval flag_status (SET or RESET)
  187. */
  188. flag_status gpio_output_data_bit_read(gpio_type *gpio_x, uint16_t pins)
  189. {
  190. flag_status status = RESET;
  191. if((gpio_x->odt & pins) != RESET)
  192. {
  193. status = SET;
  194. }
  195. else
  196. {
  197. status = RESET;
  198. }
  199. return status;
  200. }
  201. /**
  202. * @brief read the specified gpio ouput data port.
  203. * @param gpio_x: to select the gpio peripheral.
  204. * this parameter can be one of the following values:
  205. * GPIOA, GPIOB, GPIOC, GPIOD, GPIOF.
  206. * @retval gpio input data port value.
  207. */
  208. uint16_t gpio_output_data_read(gpio_type *gpio_x)
  209. {
  210. return ((uint16_t)(gpio_x->odt));
  211. }
  212. /**
  213. * @brief set the selected data port bits.
  214. * @param gpio_x: to select the gpio peripheral.
  215. * this parameter can be one of the following values:
  216. * GPIOA, GPIOB, GPIOC, GPIOD, GPIOF.
  217. * @param pins: gpio pin number
  218. * parameter can be any combination of gpio_pin_x, gpio_pin_x as following values:
  219. * - GPIO_PINS_0
  220. * - GPIO_PINS_1
  221. * - GPIO_PINS_2
  222. * - GPIO_PINS_3
  223. * - GPIO_PINS_4
  224. * - GPIO_PINS_5
  225. * - GPIO_PINS_6
  226. * - GPIO_PINS_7
  227. * - GPIO_PINS_8
  228. * - GPIO_PINS_9
  229. * - GPIO_PINS_10
  230. * - GPIO_PINS_11
  231. * - GPIO_PINS_12
  232. * - GPIO_PINS_13
  233. * - GPIO_PINS_14
  234. * - GPIO_PINS_15
  235. * - GPIO_PINS_ALL
  236. * @retval none
  237. */
  238. void gpio_bits_set(gpio_type *gpio_x, uint16_t pins)
  239. {
  240. gpio_x->scr = pins;
  241. }
  242. /**
  243. * @brief clear the selected data port bits.
  244. * @param gpio_x: to select the gpio peripheral.
  245. * this parameter can be one of the following values:
  246. * GPIOA, GPIOB, GPIOC, GPIOD, GPIOF.
  247. * @param pins: gpio pin number
  248. * parameter can be any combination of gpio_pin_x, gpio_pin_x as following values:
  249. * - GPIO_PINS_0
  250. * - GPIO_PINS_1
  251. * - GPIO_PINS_2
  252. * - GPIO_PINS_3
  253. * - GPIO_PINS_4
  254. * - GPIO_PINS_5
  255. * - GPIO_PINS_6
  256. * - GPIO_PINS_7
  257. * - GPIO_PINS_8
  258. * - GPIO_PINS_9
  259. * - GPIO_PINS_10
  260. * - GPIO_PINS_11
  261. * - GPIO_PINS_12
  262. * - GPIO_PINS_13
  263. * - GPIO_PINS_14
  264. * - GPIO_PINS_15
  265. * - GPIO_PINS_ALL
  266. * @retval none
  267. */
  268. void gpio_bits_reset(gpio_type *gpio_x, uint16_t pins)
  269. {
  270. gpio_x->clr = pins;
  271. }
  272. /**
  273. * @brief set or clear the selected data port bit.
  274. * @param gpio_x: to select the gpio peripheral.
  275. * this parameter can be one of the following values:
  276. * GPIOA, GPIOB, GPIOC, GPIOD, GPIOF.
  277. * @param pins: gpio pin number
  278. * parameter can be any combination of gpio_pin_x, gpio_pin_x as following values:
  279. * - GPIO_PINS_0
  280. * - GPIO_PINS_1
  281. * - GPIO_PINS_2
  282. * - GPIO_PINS_3
  283. * - GPIO_PINS_4
  284. * - GPIO_PINS_5
  285. * - GPIO_PINS_6
  286. * - GPIO_PINS_7
  287. * - GPIO_PINS_8
  288. * - GPIO_PINS_9
  289. * - GPIO_PINS_10
  290. * - GPIO_PINS_11
  291. * - GPIO_PINS_12
  292. * - GPIO_PINS_13
  293. * - GPIO_PINS_14
  294. * - GPIO_PINS_15
  295. * - GPIO_PINS_ALL
  296. * @param bit_state: specifies the value to be written to the selected bit (TRUE or FALSE).
  297. * @retval none
  298. */
  299. void gpio_bits_write(gpio_type *gpio_x, uint16_t pins, confirm_state bit_state)
  300. {
  301. if(bit_state != FALSE)
  302. {
  303. gpio_x->scr = pins;
  304. }
  305. else
  306. {
  307. gpio_x->clr = pins;
  308. }
  309. }
  310. /**
  311. * @brief write data to the specified gpio data port.
  312. * @param gpio_x: to select the gpio peripheral.
  313. * this parameter can be one of the following values:
  314. * GPIOA, GPIOB, GPIOC, GPIOD, GPIOF.
  315. * @param port_value: specifies the value to be written to the port output data register.
  316. * @retval none
  317. */
  318. void gpio_port_write(gpio_type *gpio_x, uint16_t port_value)
  319. {
  320. gpio_x->odt = port_value;
  321. }
  322. /**
  323. * @brief write protect gpio pins configuration registers.
  324. * @param gpio_x: to select the gpio peripheral.
  325. * this parameter can be one of the following values:
  326. * GPIOA, GPIOB, GPIOC, GPIOD, GPIOF.
  327. * @param pins: gpio pin number
  328. * this parameter can be any combination of the following:
  329. * - GPIO_PINS_0
  330. * - GPIO_PINS_1
  331. * - GPIO_PINS_2
  332. * - GPIO_PINS_3
  333. * - GPIO_PINS_4
  334. * - GPIO_PINS_5
  335. * - GPIO_PINS_6
  336. * - GPIO_PINS_7
  337. * - GPIO_PINS_8
  338. * - GPIO_PINS_9
  339. * - GPIO_PINS_10
  340. * - GPIO_PINS_11
  341. * - GPIO_PINS_12
  342. * - GPIO_PINS_13
  343. * - GPIO_PINS_14
  344. * - GPIO_PINS_15
  345. * - GPIO_PINS_ALL
  346. * @retval none
  347. */
  348. void gpio_pin_wp_config(gpio_type *gpio_x, uint16_t pins)
  349. {
  350. uint32_t temp = 0x00010000;
  351. temp |= pins;
  352. /* set wpen bit */
  353. gpio_x->wpr = temp;
  354. /* reset wpen bit */
  355. gpio_x->wpr = pins;
  356. /* set wpen bit */
  357. gpio_x->wpr = temp;
  358. /* read wpen bit*/
  359. temp = gpio_x->wpr;
  360. /* read wpen bit*/
  361. temp = gpio_x->wpr;
  362. }
  363. /**
  364. * @brief enable or disable gpio pins huge driven.
  365. * @param gpio_x: to select the gpio peripheral.
  366. * this parameter can be one of the following values:
  367. * GPIOA, GPIOB, GPIOC, GPIOD, GPIOF.
  368. * @param pins: gpio pin number
  369. * parameter can be any combination of gpio_pin_x, gpio_pin_x as following values:
  370. * - GPIO_PINS_0
  371. * - GPIO_PINS_1
  372. * - GPIO_PINS_2
  373. * - GPIO_PINS_3
  374. * - GPIO_PINS_4
  375. * - GPIO_PINS_5
  376. * - GPIO_PINS_6
  377. * - GPIO_PINS_7
  378. * - GPIO_PINS_8
  379. * - GPIO_PINS_9
  380. * - GPIO_PINS_10
  381. * - GPIO_PINS_11
  382. * - GPIO_PINS_12
  383. * - GPIO_PINS_13
  384. * - GPIO_PINS_14
  385. * - GPIO_PINS_15
  386. * - GPIO_PINS_ALL
  387. * @param new_state: new state of the slew rate.
  388. * this parameter can be: true or false.
  389. * @retval none
  390. */
  391. void gpio_pins_huge_driven_config(gpio_type *gpio_x, uint16_t pins, confirm_state new_state)
  392. {
  393. if(new_state != FALSE)
  394. {
  395. gpio_x->hdrv |= pins;
  396. }
  397. else
  398. {
  399. gpio_x->hdrv &= ~pins;
  400. }
  401. }
  402. /**
  403. * @brief configure the pin's muxing function.
  404. * @param gpio_x: to select the gpio peripheral.
  405. * this parameter can be one of the following values:
  406. * GPIOA, GPIOB, GPIOC, GPIOD, GPIOF.
  407. * @param gpio_pin_source: specifies the pin for the muxing function.
  408. * this parameter can be one of the following values:
  409. * - GPIO_PINS_SOURCE0
  410. * - GPIO_PINS_SOURCE1
  411. * - GPIO_PINS_SOURCE2
  412. * - GPIO_PINS_SOURCE3
  413. * - GPIO_PINS_SOURCE4
  414. * - GPIO_PINS_SOURCE5
  415. * - GPIO_PINS_SOURCE6
  416. * - GPIO_PINS_SOURCE7
  417. * - GPIO_PINS_SOURCE8
  418. * - GPIO_PINS_SOURCE9
  419. * - GPIO_PINS_SOURCE10
  420. * - GPIO_PINS_SOURCE11
  421. * - GPIO_PINS_SOURCE12
  422. * - GPIO_PINS_SOURCE13
  423. * - GPIO_PINS_SOURCE14
  424. * - GPIO_PINS_SOURCE15
  425. * @param gpio_mux: select the pin to used as muxing function.
  426. * this parameter can be one of the following values:
  427. * - GPIO_MUX_0
  428. * - GPIO_MUX_1
  429. * - GPIO_MUX_2
  430. * - GPIO_MUX_3
  431. * - GPIO_MUX_4
  432. * - GPIO_MUX_5
  433. * - GPIO_MUX_6
  434. * - GPIO_MUX_7
  435. * @retval none
  436. */
  437. void gpio_pin_mux_config(gpio_type *gpio_x, gpio_pins_source_type gpio_pin_source, gpio_mux_sel_type gpio_mux)
  438. {
  439. uint32_t temp = 0x00;
  440. uint32_t temp_2 = 0x00;
  441. temp = ((uint32_t)(gpio_mux) << ((uint32_t)((uint32_t)gpio_pin_source & (uint32_t)0x07) * 4));
  442. if(gpio_pin_source >> 0x03)
  443. {
  444. gpio_x->muxh &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)gpio_pin_source & (uint32_t)0x07) * 4));
  445. temp_2 = gpio_x->muxh | temp;
  446. gpio_x->muxh = temp_2;
  447. }
  448. else
  449. {
  450. gpio_x->muxl &= ~((uint32_t)0xF << ((uint32_t)((uint32_t)gpio_pin_source & (uint32_t)0x07) * 4));
  451. temp_2 = gpio_x->muxl | temp;
  452. gpio_x->muxl = temp_2;
  453. }
  454. }
  455. /**
  456. * @}
  457. */
  458. #endif
  459. /**
  460. * @}
  461. */
  462. /**
  463. * @}
  464. */