drv_gpio.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. */
  24. #include <rtthread.h>
  25. #include <drivers/pin.h>
  26. #include "../libraries/ls1c_gpio.h"
  27. void ls1c_pin_mode(struct rt_device *device, rt_base_t pin, rt_base_t mode)
  28. {
  29. unsigned int gpio = pin;
  30. if (PIN_MODE_OUTPUT == mode)
  31. {
  32. gpio_init(gpio, gpio_mode_output);
  33. }
  34. else
  35. {
  36. gpio_init(gpio, gpio_mode_input);
  37. }
  38. return ;
  39. }
  40. void ls1c_pin_write(struct rt_device *device, rt_base_t pin, rt_base_t value)
  41. {
  42. unsigned int gpio = pin;
  43. if (PIN_LOW == value)
  44. {
  45. gpio_set(gpio, gpio_level_low);
  46. }
  47. else
  48. {
  49. gpio_set(gpio, gpio_level_high);
  50. }
  51. return ;
  52. }
  53. int ls1c_pin_read(struct rt_device *device, rt_base_t pin)
  54. {
  55. unsigned int gpio = pin;
  56. int value = PIN_LOW;
  57. if (0 == gpio_get(gpio))
  58. {
  59. value = PIN_LOW;
  60. }
  61. else
  62. {
  63. value = PIN_HIGH;
  64. }
  65. return value;
  66. }
  67. const static struct rt_pin_ops _ls1c_pin_ops =
  68. {
  69. ls1c_pin_mode,
  70. ls1c_pin_write,
  71. ls1c_pin_read,
  72. };
  73. int hw_pin_init(void)
  74. {
  75. rt_device_pin_register("pin", &_ls1c_pin_ops, RT_NULL);
  76. return 0;
  77. }
  78. INIT_BOARD_EXPORT(hw_pin_init);