drv_gpio.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /******************************************************************************
  17. * @file drv_gpio.h
  18. * @brief header file for gpio driver
  19. * @version V1.0
  20. * @date 02. June 2017
  21. ******************************************************************************/
  22. #ifndef _CSI_GPIO_H_
  23. #define _CSI_GPIO_H_
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #include <stdint.h>
  28. #include <stdbool.h>
  29. #include <drv_common.h>
  30. #include <pin_name.h>
  31. /// definition for gpio port handle.
  32. typedef void *gpio_port_handle_t;
  33. /// definition for gpio pin handle.
  34. typedef void *gpio_pin_handle_t;
  35. /****** GPIO specific error codes *****/
  36. typedef enum {
  37. GPIO_ERROR_MODE = (EDRV_SPECIFIC + 1), ///< Specified Mode not suphandleed
  38. GPIO_ERROR_DIRECTION, ///< Specified direction not suphandleed
  39. GPIO_ERROR_IRQ_MODE, ///< Specified irq mode not suphandleed
  40. } drv_gpio_error_e;
  41. /*----- GPIO Control Codes: Mode -----*/
  42. typedef enum {
  43. GPIO_MODE_PULLNONE = 0, ///< pull none for input
  44. GPIO_MODE_PULLUP , ///< pull up for input
  45. GPIO_MODE_PULLDOWM , ///< pull down for input
  46. GPIO_MODE_OPEN_DRAIN , ///< open drain mode for output
  47. GPIO_MODE_PUSH_PULL ///< push-pull mode for output
  48. } gpio_mode_e;
  49. /*----- GPIO Control Codes: Mode Parameters: Data Bits -----*/
  50. typedef enum {
  51. GPIO_DIRECTION_INPUT = 0, ///< gpio as input
  52. GPIO_DIRECTION_OUTPUT , ///< gpio as output
  53. } gpio_direction_e;
  54. /*----- GPIO Control Codes: Mode Parameters: Parity -----*/
  55. typedef enum {
  56. GPIO_IRQ_MODE_RISING_EDGE = 0, ///< interrupt mode for rising edge
  57. GPIO_IRQ_MODE_FALLING_EDGE , ///< interrupt mode for falling edge
  58. GPIO_IRQ_MODE_DOUBLE_EDGE , ///< interrupt mode for double edge
  59. GPIO_IRQ_MODE_LOW_LEVEL , ///< interrupt mode for low level
  60. GPIO_IRQ_MODE_HIGH_LEVEL , ///< interrupt mode for high level
  61. } gpio_irq_mode_e;
  62. /**
  63. \brief GPIO Driver Capabilities.
  64. */
  65. typedef struct {
  66. uint32_t interrupt_mode : 1; ///< suphandles GPIO interrupt mode
  67. uint32_t pull_mode : 1;
  68. } gpio_capabilities_t;
  69. typedef void (*gpio_event_cb_t)(gpio_pin_handle_t io); ///< gpio Event call back.
  70. /**
  71. \brief Initialize GPIO module. 1. Initializes the resources needed for the GPIO handle 2.registers event callback function
  72. 3.get gpio_port_handle
  73. \param[in] port port_name.
  74. \param[in] cb_event Pointer to \ref gpio_event_cb_t
  75. \return gpio_port_handle
  76. */
  77. gpio_port_handle_t csi_gpio_port_initialize(port_name_t port, gpio_event_cb_t cb_event);
  78. /**
  79. \brief De-initialize GPIO handle. stops operation and releases the software resources used by the handle
  80. \param[in] handle gpio port handle to operate.
  81. \return error code
  82. */
  83. int32_t csi_gpio_port_uninitialize(gpio_port_handle_t handle);
  84. /**
  85. \brief Get gpio capabilities.all pins have same capabilities in one handle
  86. \param[in] handle handle instance to operate.
  87. \return \ref gpio_capabilities_t
  88. */
  89. gpio_capabilities_t csi_gpio_get_io_capabilities(gpio_port_handle_t handle);
  90. /**
  91. \brief config multiple pin within one handle
  92. \param[in] handle gpio port handle to operate.
  93. \param[in] mask the bitmask to identify which bits in the handle should be included (0 - ignore)
  94. \param[in] mode \ref gpio_mode_e
  95. \param[in] dir \ref gpio_direction_e
  96. \return error code
  97. */
  98. int32_t csi_gpio_port_config(gpio_port_handle_t handle,
  99. uint32_t mask,
  100. gpio_mode_e mode,
  101. gpio_direction_e dir);
  102. /**
  103. \brief Write value to the handle(write value to multiple pins on one handle at the same time)
  104. \param[in] handle gpio port handle to operate.
  105. \param[in] mask The bitmask to identify which bits in the handle should be included (0 - ignore)
  106. \param[in] value the value to be set
  107. \return error code
  108. */
  109. int32_t csi_gpio_port_write(gpio_port_handle_t handle, uint32_t mask, uint32_t value);
  110. /**
  111. \brief Read the current value on the handle(read value of multiple pins on one handle at the same time)
  112. \param[in] handle gpio port handle to operate.
  113. \param[in] mask The bitmask to identify which bits in the handle should be included (0 - ignore)
  114. \param[out] value an integer with each bit corresponding to an associated handle pin setting
  115. \return error code
  116. */
  117. int32_t csi_gpio_port_read(gpio_port_handle_t handle, uint32_t mask, uint32_t *value);
  118. /**
  119. \brief Initialize GPIO handle.
  120. \param[in] gpio_pin Pointer to the pin_t.
  121. \return gpio_pin_handle
  122. */
  123. gpio_pin_handle_t csi_gpio_pin_initialize(pin_t gpio_pin);
  124. /**
  125. \brief config pin
  126. \param[in] pin gpio pin handle to operate.
  127. \param[in] mode \ref gpio_mode_e
  128. \param[in] dir \ref gpio_direction_e
  129. \return error code
  130. */
  131. int32_t csi_gpio_pin_config(gpio_pin_handle_t pin,
  132. gpio_mode_e mode,
  133. gpio_direction_e dir);
  134. /**
  135. \brief Set one or zero to the selected GPIO pin.
  136. \param[in] pin gpio pin handle to operate.
  137. \param[in] value the value to be set
  138. \return error code
  139. */
  140. int32_t csi_gpio_pin_write(gpio_pin_handle_t pin, bool value);
  141. /**
  142. \brief Get the value of selected GPIO pin.
  143. \param[in] pin gpio pin handle to operate.
  144. \param[out] value buf to store the pin value
  145. \return error code
  146. */
  147. int32_t csi_gpio_pin_read(gpio_pin_handle_t pin, bool *value);
  148. /**
  149. \brief set GPIO interrupt mode.
  150. \param[in] pin gpio pin handle to operate.
  151. \param[in] mode the irq mode to be set
  152. \param[in] enable the enable flag
  153. \return error code
  154. */
  155. int32_t csi_gpio_pin_irq_set(gpio_pin_handle_t pin, gpio_irq_mode_e mode, bool enable);
  156. #ifdef __cplusplus
  157. }
  158. #endif
  159. #endif /* _CSI_GPIO_H_ */