gpio.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * File : gpio.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017-09-16 Haley the first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #include <rthw.h>
  27. #include "am_mcu_apollo.h"
  28. #ifdef RT_USING_PIN
  29. #define APLLO2_PIN_NUMBERS 64 //[34, 64]
  30. struct rt_pin_irq_hdr am_pin_irq_hdr_tab[64];
  31. void am_pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode)
  32. {
  33. if (mode == PIN_MODE_OUTPUT)
  34. {
  35. /* output setting */
  36. am_hal_gpio_pin_config(pin, AM_HAL_GPIO_OUTPUT);
  37. }
  38. else if (mode == PIN_MODE_INPUT)
  39. {
  40. /* input setting: not pull. */
  41. am_hal_gpio_pin_config(pin, AM_HAL_GPIO_INPUT);
  42. }
  43. else if (mode == PIN_MODE_INPUT_PULLUP)
  44. {
  45. /* input setting: pull up. */
  46. am_hal_gpio_pin_config(pin, AM_HAL_GPIO_INPUT);
  47. }
  48. else if (mode == PIN_MODE_INPUT_PULLDOWN)
  49. {
  50. /* input setting: pull down. */
  51. am_hal_gpio_pin_config(pin, AM_HAL_GPIO_OPENDRAIN);
  52. }
  53. else
  54. {
  55. /* input setting:default. */
  56. am_hal_gpio_pin_config(pin, AM_HAL_GPIO_3STATE);
  57. }
  58. }
  59. void am_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t value)
  60. {
  61. if (value == PIN_LOW)
  62. {
  63. am_hal_gpio_out_bit_clear(pin);
  64. }
  65. else if (value == PIN_HIGH)
  66. {
  67. am_hal_gpio_out_bit_set(pin);
  68. }
  69. }
  70. int am_pin_read(rt_device_t dev, rt_base_t pin)
  71. {
  72. int value = PIN_LOW;
  73. if (am_hal_gpio_pin_config_read(pin) == AM_HAL_GPIO_OUTPUT)
  74. {
  75. if (am_hal_gpio_out_bit_read(pin) == 0)
  76. {
  77. value = PIN_LOW;
  78. }
  79. else
  80. {
  81. value = PIN_HIGH;
  82. }
  83. }
  84. else
  85. {
  86. if (am_hal_gpio_input_bit_read(pin) == 0)
  87. {
  88. value = PIN_LOW;
  89. }
  90. else
  91. {
  92. value = PIN_HIGH;
  93. }
  94. }
  95. return value;
  96. }
  97. rt_err_t am_pin_attach_irq(struct rt_device *device, rt_int32_t pin,
  98. rt_uint32_t mode, void (*hdr)(void *args), void *args)
  99. {
  100. rt_base_t level;
  101. rt_int32_t irqindex = -1;
  102. irqindex = pin;
  103. level = rt_hw_interrupt_disable();
  104. if(am_pin_irq_hdr_tab[irqindex].pin == pin &&
  105. am_pin_irq_hdr_tab[irqindex].hdr == hdr &&
  106. am_pin_irq_hdr_tab[irqindex].mode == mode &&
  107. am_pin_irq_hdr_tab[irqindex].args == args
  108. )
  109. {
  110. rt_hw_interrupt_enable(level);
  111. return RT_EOK;
  112. }
  113. if(am_pin_irq_hdr_tab[irqindex].pin != -1)
  114. {
  115. rt_hw_interrupt_enable(level);
  116. return -RT_EBUSY;
  117. }
  118. am_pin_irq_hdr_tab[irqindex].pin = pin;
  119. am_pin_irq_hdr_tab[irqindex].hdr = hdr;
  120. am_pin_irq_hdr_tab[irqindex].mode = mode;
  121. am_pin_irq_hdr_tab[irqindex].args = args;
  122. rt_hw_interrupt_enable(level);
  123. return RT_EOK;
  124. }
  125. rt_err_t am_pin_dettach_irq(struct rt_device *device, rt_int32_t pin)
  126. {
  127. rt_base_t level;
  128. rt_int32_t irqindex = -1;
  129. irqindex = pin;
  130. level = rt_hw_interrupt_disable();
  131. if(am_pin_irq_hdr_tab[irqindex].pin == -1)
  132. {
  133. rt_hw_interrupt_enable(level);
  134. return RT_EOK;
  135. }
  136. am_pin_irq_hdr_tab[irqindex].pin = -1;
  137. am_pin_irq_hdr_tab[irqindex].hdr = RT_NULL;
  138. am_pin_irq_hdr_tab[irqindex].mode = 0;
  139. am_pin_irq_hdr_tab[irqindex].args = RT_NULL;
  140. rt_hw_interrupt_enable(level);
  141. return RT_EOK;
  142. }
  143. rt_err_t am_pin_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled)
  144. {
  145. rt_base_t level;
  146. rt_int32_t irqindex = -1;
  147. irqindex = pin;
  148. if (enabled == PIN_IRQ_ENABLE)
  149. {
  150. level = rt_hw_interrupt_disable();
  151. /* Configure the GPIO/button interrupt polarity */
  152. if (am_pin_irq_hdr_tab[irqindex].mode == PIN_IRQ_MODE_RISING)
  153. {
  154. am_hal_gpio_int_polarity_bit_set(am_pin_irq_hdr_tab[irqindex].pin, AM_HAL_GPIO_RISING);
  155. }
  156. else if (am_pin_irq_hdr_tab[irqindex].mode == PIN_IRQ_MODE_FALLING)
  157. {
  158. am_hal_gpio_int_polarity_bit_set(am_pin_irq_hdr_tab[irqindex].pin, AM_HAL_GPIO_FALLING);
  159. }
  160. /* Clear the GPIO Interrupt (write to clear) */
  161. am_hal_gpio_int_clear(AM_HAL_GPIO_BIT(am_pin_irq_hdr_tab[irqindex].pin));
  162. /* Enable the GPIO/button interrupt */
  163. am_hal_gpio_int_enable(AM_HAL_GPIO_BIT(am_pin_irq_hdr_tab[irqindex].pin));
  164. rt_hw_interrupt_enable(level);
  165. }
  166. else if (enabled == PIN_IRQ_DISABLE)
  167. {
  168. if (am_hal_gpio_int_enable_get() != AM_HAL_GPIO_BIT(am_pin_irq_hdr_tab[irqindex].pin))
  169. {
  170. return RT_ENOSYS;
  171. }
  172. /* Disable the GPIO/button interrupt */
  173. am_hal_gpio_int_disable(AM_HAL_GPIO_BIT(am_pin_irq_hdr_tab[irqindex].pin));
  174. }
  175. else
  176. {
  177. return RT_ENOSYS;
  178. }
  179. return RT_EOK;
  180. }
  181. const static struct rt_pin_ops am_pin_ops =
  182. {
  183. am_pin_mode,
  184. am_pin_write,
  185. am_pin_read,
  186. am_pin_attach_irq,
  187. am_pin_dettach_irq,
  188. am_pin_irq_enable,
  189. };
  190. int rt_hw_pin_init(void)
  191. {
  192. rt_device_pin_register("pin", &am_pin_ops, RT_NULL);
  193. //rt_device_pin_irq_register("pin", &am_pin_ops, RT_NULL);
  194. //rt_kprintf("pin_init!\n");
  195. return 0;
  196. }
  197. INIT_BOARD_EXPORT(rt_hw_pin_init);
  198. #endif
  199. /*@}*/