gpio.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* *****************************************************************************
  2. * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included
  12. * in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  17. * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
  18. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Except as contained in this notice, the name of Maxim Integrated
  23. * Products, Inc. shall not be used except as stated in the Maxim Integrated
  24. * Products, Inc. Branding Policy.
  25. *
  26. * The mere transfer of this software does not imply any licenses
  27. * of trade secrets, proprietary technology, copyrights, patents,
  28. * trademarks, maskwork rights, or any other form of intellectual
  29. * property whatsoever. Maxim Integrated Products, Inc. retains all
  30. * ownership rights.
  31. *
  32. * $Date: 2018-12-18 15:37:22 -0600 (Tue, 18 Dec 2018) $
  33. * $Revision: 40072 $
  34. *
  35. **************************************************************************** */
  36. /* **** Includes **** */
  37. #include "mxc_config.h"
  38. #include "mxc_assert.h"
  39. #include "gpio.h"
  40. #include <stddef.h>
  41. /* **** Definitions **** */
  42. /* **** Globals **** */
  43. static void (*callback[MXC_CFG_GPIO_INSTANCES][MXC_CFG_GPIO_PINS_PORT])(void *);
  44. static void *cbparam[MXC_CFG_GPIO_INSTANCES][MXC_CFG_GPIO_PINS_PORT];
  45. /* **** Functions **** */
  46. int GPIO_Init(void)
  47. {
  48. int i;
  49. int j;
  50. // Initialize call back arrays
  51. for(i = 0; i < MXC_CFG_GPIO_INSTANCES; i++) {
  52. for(j = 0; j < MXC_CFG_GPIO_PINS_PORT; j++) {
  53. callback[i][j] = NULL;
  54. }
  55. }
  56. return E_NO_ERROR;
  57. }
  58. /* ************************************************************************** */
  59. /*
  60. * GPIO_EN2 | GPIO_EN1 | GPIO_EN | Function
  61. * --------------|---------------------|---------------------|----------------------
  62. * 0 | 0 | 0 | Alternative 1
  63. * 0 | 1 | 0 | Alternative 2
  64. * 1 | 0 | 0 | Alternative 3
  65. * 1 | 1 | 0 | Alternative 4
  66. * 0 | 0 | 1 | GPIO (default)
  67. */
  68. int GPIO_Config(const gpio_cfg_t *cfg)
  69. {
  70. mxc_gpio_regs_t *gpio = MXC_GPIO_GET_GPIO(cfg->port);
  71. // Set the GPIO type
  72. switch (cfg->func) {
  73. case GPIO_FUNC_IN:
  74. gpio->out_en_clr = cfg->mask;
  75. gpio->en_set = cfg->mask;
  76. gpio->en1_clr = cfg->mask;
  77. gpio->en2_clr = cfg->mask;
  78. break;
  79. case GPIO_FUNC_OUT:
  80. gpio->out_en_set = cfg->mask;
  81. gpio->en_set = cfg->mask;
  82. gpio->en1_clr = cfg->mask;
  83. gpio->en2_clr = cfg->mask;
  84. break;
  85. case GPIO_FUNC_ALT1:
  86. gpio->en_clr = cfg->mask;
  87. gpio->en1_clr = cfg->mask;
  88. gpio->en2_clr = cfg->mask;
  89. break;
  90. case GPIO_FUNC_ALT2:
  91. gpio->en_clr = cfg->mask;
  92. gpio->en1_set = cfg->mask;
  93. gpio->en2_clr = cfg->mask;
  94. break;
  95. case GPIO_FUNC_ALT3:
  96. #if TARGET==32660
  97. gpio->en_set = cfg->mask;
  98. gpio->en1_set = cfg->mask;
  99. #else
  100. gpio->en_clr = cfg->mask;
  101. gpio->en1_clr = cfg->mask;
  102. gpio->en2_set = cfg->mask;
  103. #endif
  104. break;
  105. case GPIO_FUNC_ALT4:
  106. gpio->en_clr = cfg->mask;
  107. gpio->en1_set = cfg->mask;
  108. gpio->en2_set = cfg->mask;
  109. break;
  110. default:
  111. return E_BAD_PARAM;
  112. }
  113. // Configure the pad
  114. switch (cfg->pad) {
  115. case GPIO_PAD_NONE:
  116. gpio->pad_cfg1 &= ~cfg->mask;
  117. gpio->pad_cfg2 &= ~cfg->mask;
  118. #if TARGET==32660
  119. gpio->ps &= ~cfg->mask;
  120. #endif
  121. break;
  122. case GPIO_PAD_PULL_UP:
  123. gpio->pad_cfg1 |= cfg->mask;
  124. gpio->pad_cfg2 &= ~cfg->mask;
  125. #if TARGET==32660
  126. gpio->ps |= cfg->mask;
  127. #endif
  128. break;
  129. case GPIO_PAD_PULL_DOWN:
  130. gpio->pad_cfg1 &= ~cfg->mask;
  131. gpio->pad_cfg2 |= cfg->mask;
  132. #if TARGET==32660
  133. gpio->ps &= ~cfg->mask;
  134. #endif
  135. break;
  136. default:
  137. return E_BAD_PARAM;
  138. }
  139. return E_NO_ERROR;
  140. }
  141. /* ************************************************************************** */
  142. uint32_t GPIO_InGet(const gpio_cfg_t *cfg)
  143. {
  144. mxc_gpio_regs_t *gpio = MXC_GPIO_GET_GPIO(cfg->port);
  145. return (gpio->in & cfg->mask);
  146. }
  147. /* ************************************************************************** */
  148. void GPIO_OutSet(const gpio_cfg_t *cfg)
  149. {
  150. mxc_gpio_regs_t *gpio = MXC_GPIO_GET_GPIO(cfg->port);
  151. gpio->out_set = cfg->mask;
  152. }
  153. /* ************************************************************************** */
  154. void GPIO_OutClr(const gpio_cfg_t *cfg)
  155. {
  156. mxc_gpio_regs_t *gpio = MXC_GPIO_GET_GPIO(cfg->port);
  157. gpio->out_clr = cfg->mask;
  158. }
  159. /* ************************************************************************** */
  160. uint32_t GPIO_OutGet(const gpio_cfg_t *cfg)
  161. {
  162. mxc_gpio_regs_t *gpio = MXC_GPIO_GET_GPIO(cfg->port);
  163. return (gpio->out & cfg->mask);
  164. }
  165. /* ************************************************************************** */
  166. void GPIO_OutPut(const gpio_cfg_t *cfg, uint32_t val)
  167. {
  168. mxc_gpio_regs_t *gpio = MXC_GPIO_GET_GPIO(cfg->port);
  169. gpio->out = (gpio->out & ~cfg->mask) | (val & cfg->mask);
  170. }
  171. /* ************************************************************************** */
  172. void GPIO_OutToggle(const gpio_cfg_t *cfg)
  173. {
  174. mxc_gpio_regs_t *gpio = MXC_GPIO_GET_GPIO(cfg->port);
  175. gpio->out ^= cfg->mask;
  176. }
  177. /* ************************************************************************** */
  178. int GPIO_IntConfig(const gpio_cfg_t *cfg, gpio_int_mode_t mode, gpio_int_pol_t pol)
  179. {
  180. mxc_gpio_regs_t *gpio = MXC_GPIO_GET_GPIO(cfg->port);
  181. switch (mode) {
  182. case GPIO_INT_LEVEL:
  183. gpio->int_mod &= ~cfg->mask;
  184. break;
  185. case GPIO_INT_EDGE:
  186. gpio->int_mod |= cfg->mask;
  187. break;
  188. default:
  189. return E_BAD_PARAM;
  190. }
  191. switch (pol) {
  192. case GPIO_INT_FALLING: /* GPIO_INT_HIGH */
  193. gpio->int_pol &= ~cfg->mask;
  194. gpio->int_dual_edge &= ~cfg->mask;
  195. break;
  196. case GPIO_INT_RISING: /* GPIO_INT_LOW */
  197. gpio->int_pol |= cfg->mask;
  198. gpio->int_dual_edge &= ~cfg->mask;
  199. break;
  200. case GPIO_INT_BOTH:
  201. gpio->int_dual_edge |= cfg->mask;
  202. break;
  203. default:
  204. return E_BAD_PARAM;
  205. }
  206. return E_NO_ERROR;
  207. }
  208. /* ************************************************************************** */
  209. void GPIO_IntEnable(const gpio_cfg_t *cfg)
  210. {
  211. mxc_gpio_regs_t *gpio = MXC_GPIO_GET_GPIO(cfg->port);
  212. gpio->int_en_set = cfg->mask;
  213. }
  214. /* ************************************************************************** */
  215. void GPIO_IntDisable(const gpio_cfg_t *cfg)
  216. {
  217. mxc_gpio_regs_t *gpio = MXC_GPIO_GET_GPIO(cfg->port);
  218. gpio->int_en_clr = cfg->mask;
  219. }
  220. /* ************************************************************************** */
  221. uint32_t GPIO_IntStatus(const gpio_cfg_t *cfg)
  222. {
  223. mxc_gpio_regs_t *gpio = MXC_GPIO_GET_GPIO(cfg->port);
  224. return (gpio->int_stat & cfg->mask);
  225. }
  226. /* ************************************************************************** */
  227. void GPIO_IntClr(const gpio_cfg_t *cfg)
  228. {
  229. mxc_gpio_regs_t *gpio = MXC_GPIO_GET_GPIO(cfg->port);
  230. gpio->int_clr = cfg->mask;
  231. }
  232. /* ************************************************************************** */
  233. void GPIO_RegisterCallback(const gpio_cfg_t *cfg, gpio_callback_fn func, void *cbdata)
  234. {
  235. uint32_t mask;
  236. unsigned int pin;
  237. mask = cfg->mask;
  238. pin = 0;
  239. while (mask) {
  240. if (mask & 1) {
  241. callback[cfg->port][pin] = func;
  242. cbparam[cfg->port][pin] = cbdata;
  243. }
  244. pin++;
  245. mask >>= 1;
  246. }
  247. }
  248. /* ************************************************************************** */
  249. void GPIO_Handler(unsigned int port)
  250. {
  251. uint32_t stat;
  252. unsigned int pin;
  253. MXC_ASSERT(port < MXC_CFG_GPIO_INSTANCES);
  254. mxc_gpio_regs_t *gpio = MXC_GPIO_GET_GPIO(port);
  255. stat = gpio->int_stat;
  256. gpio->int_clr = stat;
  257. pin = 0;
  258. while (stat) {
  259. if (stat & 1) {
  260. if(callback[port][pin]) {
  261. callback[port][pin](cbparam[port][pin]);
  262. }
  263. }
  264. pin++;
  265. stat >>= 1;
  266. }
  267. }