nu_gpio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /**************************************************************************//**
  2. * @file gpio.c
  3. * @version V1.00
  4. * @brief N9H30 GPIO driver source file
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. * @copyright (C) 2018 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. #include "N9H30.h"
  10. #include "nu_sys.h"
  11. #include "nu_gpio.h"
  12. /** @addtogroup N9H30_Device_Driver N9H30 Device Driver
  13. @{
  14. */
  15. /** @addtogroup N9H30_GPIO_Driver GPIO Driver
  16. @{
  17. */
  18. /** @addtogroup N9H30_GPIO_EXPORTED_FUNCTIONS GPIO Exported Functions
  19. @{
  20. */
  21. /**
  22. * @brief Set GPIO Port
  23. *
  24. * @param[in] port GPIO port. It could be \ref GPIOA, \ref GPIOB, ... or \ref GPIOJ
  25. * @param[in] bitMap GPIO port. It could be \ref BIT0 \ref BIT1, ... or \ref BIT31
  26. *
  27. * @retval <0 Fail
  28. * @retval 0 Success
  29. *
  30. * @details This function is used to set GPIO port output data.
  31. */
  32. INT32 GPIO_Set(GPIO_PORT port, UINT32 bitMap)
  33. {
  34. INT32 offset;
  35. INT32 reg;
  36. offset = (INT32)port;
  37. reg = inpw(REG_GPIOA_DATAOUT + offset);
  38. reg = reg | bitMap;
  39. outpw(REG_GPIOA_DATAOUT + offset, reg);
  40. return SUCCESSFUL;
  41. }
  42. /**
  43. * @brief Clear GPIO port OUT Data
  44. *
  45. * @param[in] port GPIO port. It could be \ref GPIOA, \ref GPIOB, ... or \ref GPIOJ
  46. * @param[in] bitMap GPIO port data. It could be 0x00 ~ 0xFF.
  47. *
  48. * @retval <0 Fail
  49. * @retval 0 Success
  50. *
  51. * @details Clear GPIO port output data to 0.
  52. */
  53. INT32 GPIO_Clr(GPIO_PORT port, UINT32 bitMap)
  54. {
  55. INT32 offset;
  56. INT32 reg;
  57. offset = (INT32)port;
  58. reg = inpw(REG_GPIOA_DATAOUT + offset);
  59. reg = reg & (~bitMap);
  60. outpw(REG_GPIOA_DATAOUT + offset, reg);
  61. return SUCCESSFUL;
  62. }
  63. /**
  64. * @brief Open GPIO bit
  65. *
  66. * @param[in] port GPIO port. It could be \ref GPIOA, \ref GPIOB, ... or \ref GPIOJ
  67. * @param[in] bit GPIO pin. It could be \ref BIT0 \ref BIT1, ... or \ref BIT31
  68. * @param[in] direction GPIO direction. It could be \ref DIR_INPUT or \ref DIR_OUTPUT
  69. * @param[in] pull GPIO pull-up. It could be \ref NO_PULL_UP or \ref PULL_UP
  70. *
  71. * @retval <0 Fail
  72. * @retval 0 Success
  73. *
  74. * @details This function is used to open gpio pin.
  75. */
  76. INT32 GPIO_OpenBit(GPIO_PORT port, UINT32 bit, GPIO_DIR direction, GPIO_PULL pull)
  77. {
  78. UINT32 reg;
  79. UINT32 mask;
  80. INT32 offset;
  81. offset = (INT32)port;
  82. mask = (UINT32)bit;
  83. reg = inpw(REG_GPIOA_DIR + offset);
  84. reg = reg & (~mask);
  85. if (direction == DIR_OUTPUT)
  86. {
  87. reg = reg | mask;
  88. }
  89. outpw(REG_GPIOA_DIR + offset, reg);
  90. reg = inpw(REG_GPIOA_PUEN + offset);
  91. reg = reg & (~mask);
  92. if (pull == PULL_UP)
  93. {
  94. reg = reg | mask;
  95. outpw(REG_GPIOA_PUEN + offset, reg);
  96. }
  97. else if (pull == PULL_DOWN)
  98. {
  99. reg = reg | mask;
  100. outpw(REG_GPIOA_PDEN + offset, reg);
  101. }
  102. else
  103. {
  104. outpw(REG_GPIOA_PUEN + offset, reg);
  105. outpw(REG_GPIOA_PDEN + offset, reg);
  106. }
  107. return SUCCESSFUL;
  108. }
  109. /**
  110. * @brief Set GPIO pin OUT Data
  111. *
  112. * @param[in] port GPIO port. It could be \ref GPIOA, \ref GPIOB, ... or \ref GPIOJ
  113. * @param[in] bit GPIO pin. It could be \ref BIT0 \ref BIT1, ... or \ref BIT31
  114. *
  115. * @retval <0 Fail
  116. * @retval 0 Success
  117. *
  118. * @details Set the Data into specified GPIO pin.
  119. */
  120. INT32 GPIO_CloseBit(GPIO_PORT port, UINT32 bit)
  121. {
  122. UINT32 reg;
  123. UINT32 mask;
  124. INT32 offset;
  125. offset = (INT32)port;
  126. mask = (UINT32)bit;
  127. reg = inpw(REG_GPIOA_DIR + offset);
  128. reg = reg & (~mask);
  129. outpw(REG_GPIOA_DIR + offset, reg);
  130. reg = inpw(REG_GPIOA_PUEN + offset);
  131. reg = reg & (~mask);
  132. outpw(REG_GPIOA_PUEN + offset, reg);
  133. return SUCCESSFUL;
  134. }
  135. /**
  136. * @brief Set GPIO pin OUT Data
  137. *
  138. * @param[in] port GPIO port. It could be \ref GPIOA, \ref GPIOB, ... or \ref GPIOJ
  139. * @param[in] bit GPIO pin. It could be \ref BIT0 \ref BIT1, ... or \ref BIT31
  140. *
  141. * @retval <0 Fail
  142. * @retval 0 Success
  143. *
  144. * @details Set the Data into specified GPIO pin.
  145. */
  146. INT32 GPIO_SetBit(GPIO_PORT port, UINT32 bit)
  147. {
  148. UINT32 bitMap;
  149. INT32 offset;
  150. INT32 reg;
  151. offset = (INT32)port;
  152. bitMap = (UINT32)bit;
  153. reg = inpw(REG_GPIOA_DATAOUT + offset);
  154. reg = reg | bitMap;
  155. outpw(REG_GPIOA_DATAOUT + offset, reg);
  156. return SUCCESSFUL;
  157. }
  158. /**
  159. * @brief Clear GPIO port Interrupt Flag
  160. *
  161. * @param[in] port GPIO port. It could be \ref GPIOA, \ref GPIOB, ... or \ref GPIOJ
  162. * @param[in] bitMap GPIO port data. It could be 0x00 ~ 0xFF.
  163. *
  164. * @retval <0 Fail
  165. * @retval 0 Success
  166. *
  167. * @details Clear the interrupt status of specified GPIO port.
  168. */
  169. INT32 GPIO_ClrISR(GPIO_PORT port, UINT32 bitMap)
  170. {
  171. INT32 offset;
  172. offset = (INT32)port;
  173. outpw(REG_GPIOA_ISR + offset, bitMap);
  174. return SUCCESSFUL;
  175. }
  176. /**
  177. * @brief Clear GPIO Pin Interrupt Flag
  178. *
  179. * @param[in] port GPIO port. It could be \ref GPIOA, \ref GPIOB, ... or \ref GPIOJ
  180. * @param[in] bit GPIO pin. It could be \ref BIT0 \ref BIT1, ... or \ref BIT31
  181. *
  182. * @retval <0 Fail
  183. * @retval 0 Success
  184. *
  185. * @details Clear the interrupt status of specified GPIO pin.
  186. */
  187. INT32 GPIO_ClrISRBit(GPIO_PORT port, UINT32 bit)
  188. {
  189. UINT32 bitMap;
  190. INT32 offset;
  191. offset = (INT32)port;
  192. bitMap = (UINT32)bit;
  193. outpw(REG_GPIOA_ISR + offset, bitMap);
  194. return SUCCESSFUL;
  195. }
  196. /**
  197. * @brief Clear GPIO pin OUT Data
  198. *
  199. * @param[in] port GPIO port. It could be \ref GPIOA, \ref GPIOB, ... or \ref GPIOJ
  200. * @param[in] bit GPIO pin. It could be \ref BIT0 \ref BIT1, ... or \ref BIT31
  201. *
  202. * @retval <0 Fail
  203. * @retval 0 Success
  204. *
  205. * @details Set the Data into specified GPIO pin.
  206. */
  207. INT32 GPIO_ClrBit(GPIO_PORT port, UINT32 bit)
  208. {
  209. UINT32 bitMap;
  210. INT32 offset;
  211. INT32 reg;
  212. offset = (INT32)port;
  213. bitMap = (UINT32)bit;
  214. reg = inpw(REG_GPIOA_DATAOUT + offset);
  215. reg = reg & (~bitMap);
  216. outpw(REG_GPIOA_DATAOUT + offset, reg);
  217. return SUCCESSFUL;
  218. }
  219. /**
  220. * @brief Read GPIO pin In Data
  221. *
  222. * @param[in] port GPIO port. It could be \ref GPIOA, \ref GPIOB, ... or \ref GPIOJ
  223. * @param[in] bit GPIO pin. It could be \ref BIT0 \ref BIT1, ... or \ref BIT31
  224. *
  225. * @retval 1/0 GPIO pin input data.
  226. *
  227. * @details Read the In Data from GPIO pin.
  228. */
  229. INT32 GPIO_ReadBit(GPIO_PORT port, UINT32 bit)
  230. {
  231. UINT32 reg;
  232. UINT32 bitMap;
  233. INT32 offset;
  234. offset = (INT32)port;
  235. bitMap = (UINT32)bit;
  236. reg = inpw(REG_GPIOA_DATAIN + offset);
  237. return ((reg & bitMap) ? 1 : 0);
  238. }
  239. /**
  240. * @brief Set GPIO pin direction
  241. *
  242. * @param[in] port GPIO port. It could be \ref GPIOA, \ref GPIOB, ... or \ref GPIOJ
  243. * @param[in] bit GPIO pin. It could be \ref BIT0 \ref BIT1, ... or \ref BIT31
  244. * @param[in] direction GPIO direction. It could be \ref DIR_INPUT, \ref DIR_OUTPUT.
  245. *
  246. * @retval <0 Fail
  247. * @retval 0 Success
  248. *
  249. * @details Set the GPIO direction into specified GPIO pin.
  250. */
  251. INT32 GPIO_SetBitDir(GPIO_PORT port, UINT32 bit, GPIO_DIR direction)
  252. {
  253. UINT32 reg;
  254. UINT32 bitMap;
  255. INT32 offset;
  256. offset = (INT32)port;
  257. bitMap = (UINT32)bit;
  258. reg = inpw(REG_GPIOA_DIR + offset);
  259. reg = reg & (~bitMap);
  260. if (direction == DIR_OUTPUT)
  261. {
  262. reg = reg | bitMap;
  263. }
  264. outpw(REG_GPIOA_DIR + offset, reg);
  265. return SUCCESSFUL;
  266. }
  267. /**
  268. * @brief Enable GPIO trigger type.
  269. *
  270. * @param[in] port GPIO port. It could be \ref GPIOA, \ref GPIOB, ... or \ref GPIOJ
  271. * @param[in] bitMap GPIO port. It could be \ref BIT0 \ref BIT1, ... or \ref BIT31
  272. * @param[in] triggerType The triggerType of specified GPIO pin. It could be \n
  273. * \ref RISING, \ref FALLING, \ref BOTH_EDGE, \ref HIGH, \ref LOW.
  274. *
  275. * @retval <0 Fail
  276. * @retval 0 Success
  277. *
  278. * @details This function is used to enable trigger type.
  279. */
  280. INT32 GPIO_EnableTriggerType(GPIO_PORT port, UINT32 bitMap, GPIO_TRIGGER_TYPE triggerType)
  281. {
  282. UINT32 reg;
  283. INT32 offset;
  284. offset = (INT32)port;
  285. switch (triggerType)
  286. {
  287. case LOW:
  288. reg = inpw(REG_GPIOA_IMD + offset);
  289. outpw(REG_GPIOA_IMD + offset, reg | bitMap);
  290. reg = inpw(REG_GPIOA_IREN + offset);
  291. outpw(REG_GPIOA_IREN + offset, reg & ~bitMap);
  292. reg = inpw(REG_GPIOA_IFEN + offset);
  293. outpw(REG_GPIOA_IFEN + offset, reg | bitMap);
  294. break;
  295. case HIGH:
  296. reg = inpw(REG_GPIOA_IMD + offset);
  297. outpw(REG_GPIOA_IMD + offset, reg | bitMap);
  298. reg = inpw(REG_GPIOA_IREN + offset);
  299. outpw(REG_GPIOA_IREN + offset, reg | bitMap);
  300. reg = inpw(REG_GPIOA_IFEN + offset);
  301. outpw(REG_GPIOA_IFEN + offset, reg & ~bitMap);
  302. break;
  303. case FALLING:
  304. reg = inpw(REG_GPIOA_IMD + offset);
  305. outpw(REG_GPIOA_IMD + offset, reg & ~bitMap);
  306. reg = inpw(REG_GPIOA_IREN + offset);
  307. outpw(REG_GPIOA_IREN + offset, reg & ~bitMap);
  308. reg = inpw(REG_GPIOA_IFEN + offset);
  309. outpw(REG_GPIOA_IFEN + offset, reg | bitMap);
  310. break;
  311. case RISING:
  312. reg = inpw(REG_GPIOA_IMD + offset);
  313. outpw(REG_GPIOA_IMD + offset, reg & ~bitMap);
  314. reg = inpw(REG_GPIOA_IREN + offset);
  315. outpw(REG_GPIOA_IREN + offset, reg | bitMap);
  316. reg = inpw(REG_GPIOA_IFEN + offset);
  317. outpw(REG_GPIOA_IFEN + offset, reg & ~bitMap);
  318. break;
  319. case BOTH_EDGE:
  320. reg = inpw(REG_GPIOA_IMD + offset);
  321. outpw(REG_GPIOA_IMD + offset, reg & ~bitMap);
  322. reg = inpw(REG_GPIOA_IREN + offset);
  323. outpw(REG_GPIOA_IREN + offset, reg | bitMap);
  324. reg = inpw(REG_GPIOA_IFEN + offset);
  325. outpw(REG_GPIOA_IFEN + offset, reg | bitMap);
  326. break;
  327. }
  328. return SUCCESSFUL;
  329. }
  330. /**
  331. * @brief Disable GPIO trigger type.
  332. *
  333. * @param[in] port GPIO port. It could be \ref GPIOA, \ref GPIOB, ... or \ref GPIOJ
  334. * @param[in] bitMap GPIO port. It could be \ref BIT0 \ref BIT1, ... or \ref BIT31
  335. *
  336. * @retval <0 Fail
  337. * @retval 0 Success
  338. *
  339. * @details This function is used to disable trigger type.
  340. */
  341. INT32 GPIO_DisableTriggerType(GPIO_PORT port, UINT32 bitMap)
  342. {
  343. UINT32 reg;
  344. INT32 offset;
  345. offset = (INT32)port;
  346. reg = inpw(REG_GPIOA_IMD + offset);
  347. outpw(REG_GPIOA_IMD + offset, reg & ~bitMap);
  348. reg = inpw(REG_GPIOA_IREN + offset);
  349. outpw(REG_GPIOA_IREN + offset, reg & ~bitMap);
  350. reg = inpw(REG_GPIOA_IFEN + offset);
  351. outpw(REG_GPIOA_IFEN + offset, reg & ~bitMap);
  352. return SUCCESSFUL;
  353. }
  354. /**
  355. * @brief Enable GPIO De-bounce Function
  356. *
  357. * @param[in] debounceClkSel The de-bounce sampling cycle selection. It could be 0~0xF. \n
  358. * 0 = Sample interrupt input once per 1 clocks. \n
  359. * 1 = Sample interrupt input once per 2 clocks. \n
  360. * 2 = Sample interrupt input once per 4 clocks. \n
  361. * 3 = Sample interrupt input once per 8 clocks. \n
  362. * 4 = Sample interrupt input once per 16 clocks. \n
  363. * 5 = Sample interrupt input once per 32 clocks. \n
  364. * 6 = Sample interrupt input once per 64 clocks. \n
  365. * 7 = Sample interrupt input once per 128 clocks. \n
  366. * 8 = Sample interrupt input once per 256 clocks. \n
  367. * 9 = Sample interrupt input once per 2*256 clocks. \n
  368. * 10 = Sample interrupt input once per 4*256 clocks. \n
  369. * 11 = Sample interrupt input once per 8*256 clocks. \n
  370. * 12 = Sample interrupt input once per 16*256 clocks. \n
  371. * 13 = Sample interrupt input once per 32*256 clocks. \n
  372. * 14 = Sample interrupt input once per 64*256 clocks. \n
  373. * 15 = Sample interrupt input once per 128*256 clocks
  374. *
  375. * @retval <0 Fail
  376. * @retval 0 Success
  377. *
  378. * @details Enable the interrupt de-bounce function of specified GPIO.
  379. */
  380. INT32 GPIO_EnableDebounce(INT32 debounceClkSel)
  381. {
  382. UINT32 reg;
  383. reg = inpw(REG_GPIO_DBNCECON);
  384. /* Setting the debounce timing */
  385. reg = ((reg & ~0xf) | debounceClkSel);
  386. /* Enable the debounce function */
  387. reg = reg | 0x20;
  388. outpw(REG_GPIO_DBNCECON, reg);
  389. return SUCCESSFUL;
  390. }
  391. /**
  392. * @brief Disable GPIO De-bounce Function.
  393. *
  394. * @retval <0 Fail
  395. * @retval 0 Success
  396. *
  397. * @details Disable the interrupt de-bounce function of specified GPIO.
  398. */
  399. INT32 GPIO_DisableDebounce(void)
  400. {
  401. UINT32 reg;
  402. reg = inpw(REG_GPIO_DBNCECON);
  403. /* Setting the debounce timing */
  404. reg = ((reg & ~0xf));
  405. /* Enable the debounce function */
  406. reg = reg | 0x20;
  407. outpw(REG_GPIO_DBNCECON, reg);
  408. return SUCCESSFUL;
  409. }
  410. /*@}*/ /* end of group N9H30_GPIO_EXPORTED_FUNCTIONS */
  411. /*@}*/ /* end of group N9H30_GPIO_Driver */
  412. /*@}*/ /* end of group N9H30_Device_Driver */