選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

255 行
7.2 KiB

  1. /** @defgroup gpio_file GPIO peripheral API
  2. * @brief SWM050 GPIO API.
  3. * @ingroup peripheral_apis
  4. * LGPL License Terms @ref lgpl_license
  5. * @author @htmlonly © @endhtmlonly 2019
  6. * Icenowy Zheng <icenowy@aosc.io>
  7. * @author @htmlonly &copy; @endhtmlonly 2019
  8. * Caleb Szalacinski <contact@skiboy.net>
  9. */
  10. /*
  11. * This file is part of the libopencm3 project.
  12. *
  13. * Copyright (C) 2019 Icenowy Zheng <icenowy@aosc.io>
  14. * Copyright (C) 2019 Caleb Szalacinski <contact@skiboy.net>
  15. *
  16. * This library is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Lesser General Public License as published by
  18. * the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This library is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Lesser General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Lesser General Public License
  27. * along with this library. If not, see <http://www.gnu.org/licenses/>.
  28. */
  29. /**@{*/
  30. #include <libopencm3/swm050/gpio.h>
  31. /*---------------------------------------------------------------------------*/
  32. /** @brief Set a Group of Pins
  33. Set one or more pins of GPIO to 1. Please note that this chip doesn't support
  34. atomic pin setting.
  35. @param[in] gpios Pin identifiers @ref gpio_pin_id
  36. If multiple pins are to be changed, use bitwise OR '|' to separate
  37. them.
  38. */
  39. void gpio_set(uint16_t gpios)
  40. {
  41. GPIO_ADATA |= gpios;
  42. }
  43. /*---------------------------------------------------------------------------*/
  44. /** @brief Clear a Group of Pins
  45. Set one or more pins of GPIO to 0. Please note that this chip doesn't support
  46. atomic pin setting.
  47. @param[in] gpios Pin identifiers @ref gpio_pin_id
  48. If multiple pins are to be changed, use bitwise OR '|' to separate
  49. them.
  50. */
  51. void gpio_clear(uint16_t gpios)
  52. {
  53. GPIO_ADATA &= ~gpios;
  54. }
  55. /*---------------------------------------------------------------------------*/
  56. /** @brief Read a Group of Pins.
  57. @param[in] gpios Pin identifiers @ref gpio_pin_id
  58. If multiple pins are to be read, use bitwise OR '|' to separate
  59. them.
  60. @return The pin values as a bitfield. The bit position of the pin
  61. value returned corresponds to the pin number.
  62. */
  63. uint16_t gpio_get(uint16_t gpios)
  64. {
  65. return GPIO_AEXT & gpios;
  66. }
  67. /*---------------------------------------------------------------------------*/
  68. /** @brief Toggle a Group of Pins
  69. Toggle one or more pins of GPIO. The non-toggled pins are not affected.
  70. @param[in] gpios Pin identifiers @ref gpio_pin_id
  71. If multiple pins are to be changed, use bitwise OR '|' to separate
  72. them.
  73. */
  74. void gpio_toggle(uint16_t gpios)
  75. {
  76. uint32_t curr_status = GPIO_ADATA & gpios;
  77. GPIO_ADATA = (GPIO_ADATA & (~gpios)) | (~curr_status);
  78. }
  79. /*---------------------------------------------------------------------------*/
  80. /** @brief Set the direction of a Group of Pins to Input
  81. Set the direction of one or more pins of GPIO to input.
  82. @param[in] gpios Pin identifiers @ref gpio_pin_id
  83. If multiple pins are to be changed, use bitwise OR '|' to separate
  84. them.
  85. */
  86. void gpio_input(uint16_t gpios)
  87. {
  88. GPIO_ADIR &= ~gpios;
  89. }
  90. /*---------------------------------------------------------------------------*/
  91. /** @brief Set the direction of a Group of Pins to Output
  92. Set the direction of one or more pins of GPIO to output.
  93. @param[in] gpios Pin identifiers @ref gpio_pin_id
  94. If multiple pins are to be changed, use bitwise OR '|' to separate
  95. them.
  96. */
  97. void gpio_output(uint16_t gpios)
  98. {
  99. GPIO_ADIR |= gpios;
  100. }
  101. /*---------------------------------------------------------------------------*/
  102. /** @brief Sets the pins as external interrupts, rather than normal GPIO
  103. Enable interrupts on the selected pins. If you want to quickly
  104. switch on and off interrupts, use gpio_int_mask() after calling this.
  105. @param[in] gpios Pin identifiers @ref gpio_pin_id
  106. If multiple pins are to be changed, use bitwise OR '|' to separate
  107. them.
  108. @param[in] en True to enable, false to disable.
  109. */
  110. void gpio_int_enable(uint16_t gpios, bool en)
  111. {
  112. if (en) {
  113. GPIO_INTEN_A |= gpios;
  114. } else {
  115. GPIO_INTEN_A &= ~gpios;
  116. }
  117. }
  118. /*---------------------------------------------------------------------------*/
  119. /** @brief Sets bits in the interrupt mask
  120. When interrupts are masked, it prevents them from being received, which is a
  121. quicker way to turn on and off GPIO interrupts (after calling gpio_int_en()).
  122. @param[in] gpios Pin identifiers @ref gpio_pin_id
  123. If multiple pins are to be changed, use bitwise OR '|' to separate
  124. them.
  125. @param[in] masked Pin mask selection @ref gpio_int_masked
  126. Whether to mask or unmask pins.
  127. */
  128. void gpio_int_mask(uint16_t gpios, enum gpio_int_masked masked)
  129. {
  130. if (masked) {
  131. GPIO_INTMASK_A |= gpios;
  132. } else {
  133. GPIO_INTMASK_A &= ~gpios;
  134. }
  135. }
  136. /*---------------------------------------------------------------------------*/
  137. /** @brief Sets whether the pins are edge triggered or level triggered
  138. Sets whether the pins are edge triggered or level triggered. Edge-triggered
  139. interrupt bits must be cleared by software.
  140. @param[in] gpios Pin identifiers @ref gpio_pin_id
  141. If multiple pins are to be changed, use bitwise OR '|' to separate
  142. them.
  143. @param[in] type Trigger Type @ref gpio_trig_type
  144. Level or edge triggered
  145. */
  146. void gpio_int_type(uint16_t gpios, enum gpio_trig_type type)
  147. {
  148. if (type) {
  149. GPIO_INTLEVEL_A |= gpios;
  150. } else {
  151. GPIO_INTLEVEL_A &= ~gpios;
  152. }
  153. }
  154. /*---------------------------------------------------------------------------*/
  155. /** @brief Sets the interrupt trigger polarity
  156. Sets whether the interrupt is triggered by a high or low level/edge.
  157. @param[in] gpios Pin identifiers @ref gpio_pin_id
  158. If multiple pins are to be changed, use bitwise OR '|' to separate
  159. them.
  160. @param[in] pol Polarity @ref gpio_pol
  161. High or low level/edge
  162. */
  163. void gpio_int_pol(uint16_t gpios, enum gpio_pol pol)
  164. {
  165. if (pol) {
  166. GPIO_INTPOLARITY_A |= gpios;
  167. } else {
  168. GPIO_INTPOLARITY_A &= ~gpios;
  169. }
  170. }
  171. /*---------------------------------------------------------------------------*/
  172. /** @brief Gets the masked interrupt status
  173. Returns the pin interrupt status masked with the mask set
  174. in @ref gpio_int_mask().
  175. @return The masked pin interrupt status as a bitfield. The bit position of the
  176. pin value returned corresponds to the pin number.
  177. */
  178. uint16_t gpio_int_status(void)
  179. {
  180. return GPIO_INTSTAT_A;
  181. }
  182. /*---------------------------------------------------------------------------*/
  183. /** @brief Gets the raw unmasked interrupt status
  184. Returns the raw unmasked interrupt status.
  185. @return The unmasked pin interrupt status as a bitfield. The bit position of the
  186. pin value returned corresponds to the pin number.
  187. */
  188. uint16_t gpio_int_raw_status(void)
  189. {
  190. return GPIO_RAWINTSTAT_A;
  191. }
  192. /*---------------------------------------------------------------------------*/
  193. /** @brief Clear the specified pin interrupts
  194. Clears the specified pin interrupts. Edge-triggered interrupts must be cleared
  195. by software.
  196. @param[in] gpios Pin identifiers @ref gpio_pin_id
  197. If multiple pins are to be changed, use bitwise OR '|' to separate
  198. them.
  199. */
  200. void gpio_int_clear(uint16_t gpios)
  201. {
  202. GPIO_INTEOI_A |= gpios;
  203. }
  204. /**@}*/