1
0

drv_gpio.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * File : drv_gpio.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, 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-11-24 勤为本 first version
  23. * 2018-05-11 zhuangwei add gpio interrupt ops
  24. */
  25. #include <rtthread.h>
  26. #include <drivers/pin.h>
  27. #include "ls1c_gpio.h"
  28. #include "ls1c.h"
  29. #include <rthw.h>
  30. #ifdef RT_USING_PIN
  31. void ls1c_pin_mode(struct rt_device *device, rt_base_t pin, rt_base_t mode)
  32. {
  33. unsigned int gpio = pin;
  34. if (PIN_MODE_OUTPUT == mode)
  35. {
  36. gpio_init(gpio, gpio_mode_output);
  37. }
  38. else
  39. {
  40. gpio_init(gpio, gpio_mode_input);
  41. }
  42. return ;
  43. }
  44. void ls1c_pin_write(struct rt_device *device, rt_base_t pin, rt_base_t value)
  45. {
  46. unsigned int gpio = pin;
  47. if (PIN_LOW == value)
  48. {
  49. gpio_set(gpio, gpio_level_low);
  50. }
  51. else
  52. {
  53. gpio_set(gpio, gpio_level_high);
  54. }
  55. return ;
  56. }
  57. int ls1c_pin_read(struct rt_device *device, rt_base_t pin)
  58. {
  59. unsigned int gpio = pin;
  60. int value = PIN_LOW;
  61. if (0 == gpio_get(gpio))
  62. {
  63. value = PIN_LOW;
  64. }
  65. else
  66. {
  67. value = PIN_HIGH;
  68. }
  69. return value;
  70. }
  71. rt_err_t ls1c_pin_attach_irq(struct rt_device *device, rt_int32_t pin,
  72. rt_uint32_t mode, void (*hdr)(void *args), void *args)
  73. {
  74. unsigned int gpio = pin;
  75. char irq_name[10];
  76. gpio_set_irq_type(gpio, mode);
  77. rt_sprintf(irq_name, "PIN_%d", gpio);
  78. rt_hw_interrupt_install(LS1C_GPIO_TO_IRQ(gpio), (rt_isr_handler_t)hdr, args, irq_name);
  79. return RT_EOK;
  80. }
  81. rt_err_t ls1c_pin_dettach_irq(struct rt_device *device, rt_int32_t pin)
  82. {
  83. return RT_EOK;
  84. }
  85. rt_err_t ls1c_pin_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled)
  86. {
  87. unsigned int gpio = pin;
  88. if (enabled)
  89. rt_hw_interrupt_umask(LS1C_GPIO_TO_IRQ(gpio));
  90. else
  91. rt_hw_interrupt_mask(LS1C_GPIO_TO_IRQ(gpio));
  92. return RT_EOK;
  93. }
  94. const static struct rt_pin_ops _ls1c_pin_ops =
  95. {
  96. ls1c_pin_mode,
  97. ls1c_pin_write,
  98. ls1c_pin_read,
  99. ls1c_pin_attach_irq,
  100. ls1c_pin_dettach_irq,
  101. ls1c_pin_irq_enable
  102. };
  103. int hw_pin_init(void)
  104. {
  105. int ret = RT_EOK;
  106. ret = rt_device_pin_register("pin", &_ls1c_pin_ops, RT_NULL);
  107. return ret;
  108. }
  109. INIT_BOARD_EXPORT(hw_pin_init);
  110. #endif /*RT_USING_PIN */