gpio.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 "am_mcu_apollo.h"
  27. #ifdef RT_USING_PIN
  28. void am_pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode)
  29. {
  30. if (mode == PIN_MODE_OUTPUT)
  31. {
  32. /* output setting */
  33. am_hal_gpio_pin_config(pin, AM_HAL_GPIO_OUTPUT);
  34. }
  35. else if (mode == PIN_MODE_INPUT)
  36. {
  37. /* input setting: not pull. */
  38. am_hal_gpio_pin_config(pin, AM_HAL_GPIO_INPUT);
  39. }
  40. else if (mode == PIN_MODE_INPUT_PULLUP)
  41. {
  42. /* input setting: pull up. */
  43. am_hal_gpio_pin_config(pin, AM_HAL_GPIO_OPENDRAIN);
  44. }
  45. else
  46. {
  47. /* input setting:default. */
  48. am_hal_gpio_pin_config(pin, AM_HAL_GPIO_INPUT);
  49. }
  50. }
  51. void am_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t value)
  52. {
  53. if (value == PIN_LOW)
  54. {
  55. am_hal_gpio_out_bit_clear(pin);
  56. }
  57. else
  58. {
  59. am_hal_gpio_out_bit_set(pin);
  60. }
  61. }
  62. int am_pin_read(rt_device_t dev, rt_base_t pin)
  63. {
  64. int value = PIN_LOW;
  65. if (am_hal_gpio_input_bit_read(pin) == 0)
  66. {
  67. value = PIN_LOW;
  68. }
  69. else
  70. {
  71. value = PIN_HIGH;
  72. }
  73. return value;
  74. }
  75. const static struct rt_pin_ops _am_pin_ops =
  76. {
  77. am_pin_mode,
  78. am_pin_write,
  79. am_pin_read,
  80. };
  81. int rt_hw_pin_init(void)
  82. {
  83. rt_device_pin_register("pin", &_am_pin_ops, RT_NULL);
  84. rt_kprintf("pin_init!\n");
  85. return 0;
  86. }
  87. INIT_BOARD_EXPORT(rt_hw_pin_init);
  88. #endif
  89. /*@}*/