gpio.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. * GPIO -- General Purpose Input/Output
  7. *
  8. * Defines a simple and generic interface to access GPIO device.
  9. *
  10. */
  11. #include <assert.h>
  12. #include <errno.h>
  13. #include <drivers/gpio.h>
  14. /*
  15. * The gpio implementation
  16. */
  17. static const gpio_ops_t *ops;
  18. int gpio_get_direction(int gpio)
  19. {
  20. assert(ops);
  21. assert(ops->get_direction != 0);
  22. assert(gpio >= 0);
  23. return ops->get_direction(gpio);
  24. }
  25. void gpio_set_direction(int gpio, int direction)
  26. {
  27. assert(ops);
  28. assert(ops->set_direction != 0);
  29. assert((direction == GPIO_DIR_OUT) || (direction == GPIO_DIR_IN));
  30. assert(gpio >= 0);
  31. ops->set_direction(gpio, direction);
  32. }
  33. int gpio_get_value(int gpio)
  34. {
  35. assert(ops);
  36. assert(ops->get_value != 0);
  37. assert(gpio >= 0);
  38. return ops->get_value(gpio);
  39. }
  40. void gpio_set_value(int gpio, int value)
  41. {
  42. assert(ops);
  43. assert(ops->set_value != 0);
  44. assert((value == GPIO_LEVEL_LOW) || (value == GPIO_LEVEL_HIGH));
  45. assert(gpio >= 0);
  46. ops->set_value(gpio, value);
  47. }
  48. void gpio_set_pull(int gpio, int pull)
  49. {
  50. assert(ops);
  51. assert(ops->set_pull != 0);
  52. assert((pull == GPIO_PULL_NONE) || (pull == GPIO_PULL_UP) ||
  53. (pull == GPIO_PULL_DOWN));
  54. assert(gpio >= 0);
  55. ops->set_pull(gpio, pull);
  56. }
  57. int gpio_get_pull(int gpio)
  58. {
  59. assert(ops);
  60. assert(ops->get_pull != 0);
  61. assert(gpio >= 0);
  62. return ops->get_pull(gpio);
  63. }
  64. /*
  65. * Initialize the gpio. The fields in the provided gpio
  66. * ops pointer must be valid.
  67. */
  68. void gpio_init(const gpio_ops_t *ops_ptr)
  69. {
  70. assert(ops_ptr != 0 &&
  71. (ops_ptr->get_direction != 0) &&
  72. (ops_ptr->set_direction != 0) &&
  73. (ops_ptr->get_value != 0) &&
  74. (ops_ptr->set_value != 0));
  75. ops = ops_ptr;
  76. }