extint_callback.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /**
  2. * \file
  3. *
  4. * \brief SAM External Interrupt Driver
  5. *
  6. * Copyright (C) 2012-2015 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #include "extint.h"
  47. #include "extint_callback.h"
  48. /**
  49. * \internal
  50. * Internal driver device instance struct, declared in the main module driver.
  51. */
  52. extern struct _extint_module _extint_dev;
  53. /**
  54. * \internal
  55. * This is the number of the channel whose callback is currently running.
  56. */
  57. uint8_t _current_channel;
  58. /**
  59. * \brief Registers an asynchronous callback function with the driver.
  60. *
  61. * Registers an asynchronous callback with the EXTINT driver, fired when a
  62. * channel detects the configured channel detection criteria
  63. * (e.g. edge or level). Callbacks are fired once for each detected channel.
  64. *
  65. * \note NMI channel callbacks cannot be registered via this function; the
  66. * device's NMI interrupt should be hooked directly in the user
  67. * application and the NMI flags manually cleared via
  68. * \ref extint_nmi_clear_detected().
  69. *
  70. * \param[in] callback Pointer to the callback function to register
  71. * \param[in] channel Logical channel to register callback for
  72. * \param[in] type Type of callback function to register
  73. *
  74. * \return Status of the registration operation.
  75. * \retval STATUS_OK The callback was registered successfully
  76. * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied
  77. * \retval STATUS_ERR_ALREADY_INITIALIZED Callback function has been
  78. * registered, need unregister first
  79. */
  80. enum status_code extint_register_callback(
  81. const extint_callback_t callback,
  82. const uint8_t channel,
  83. const enum extint_callback_type type)
  84. {
  85. /* Sanity check arguments */
  86. Assert(callback);
  87. if (type != EXTINT_CALLBACK_TYPE_DETECT) {
  88. Assert(false);
  89. return STATUS_ERR_INVALID_ARG;
  90. }
  91. if (_extint_dev.callbacks[channel] == NULL) {
  92. _extint_dev.callbacks[channel] = callback;
  93. return STATUS_OK;
  94. } else if (_extint_dev.callbacks[channel] == callback) {
  95. return STATUS_OK;
  96. }
  97. return STATUS_ERR_ALREADY_INITIALIZED;
  98. }
  99. /**
  100. * \brief Unregisters an asynchronous callback function with the driver.
  101. *
  102. * Unregisters an asynchronous callback with the EXTINT driver, removing it
  103. * from the internal callback registration table.
  104. *
  105. * \param[in] callback Pointer to the callback function to unregister
  106. * \param[in] channel Logical channel to unregister callback for
  107. * \param[in] type Type of callback function to unregister
  108. *
  109. * \return Status of the de-registration operation.
  110. * \retval STATUS_OK The callback was unregistered successfully
  111. * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied
  112. * \retval STATUS_ERR_BAD_ADDRESS No matching entry was found in the
  113. * registration table
  114. */
  115. enum status_code extint_unregister_callback(
  116. const extint_callback_t callback,
  117. const uint8_t channel,
  118. const enum extint_callback_type type)
  119. {
  120. /* Sanity check arguments */
  121. Assert(callback);
  122. if (type != EXTINT_CALLBACK_TYPE_DETECT) {
  123. Assert(false);
  124. return STATUS_ERR_INVALID_ARG;
  125. }
  126. if (_extint_dev.callbacks[channel] == callback) {
  127. _extint_dev.callbacks[channel] = NULL;
  128. return STATUS_OK;
  129. }
  130. return STATUS_ERR_BAD_ADDRESS;
  131. }
  132. /**
  133. * \brief Enables asynchronous callback generation for a given channel and type.
  134. *
  135. * Enables asynchronous callbacks for a given logical external interrupt channel
  136. * and type. This must be called before an external interrupt channel will
  137. * generate callback events.
  138. *
  139. * \param[in] channel Logical channel to enable callback generation for
  140. * \param[in] type Type of callback function callbacks to enable
  141. *
  142. * \return Status of the callback enable operation.
  143. * \retval STATUS_OK The callback was enabled successfully
  144. * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied
  145. */
  146. enum status_code extint_chan_enable_callback(
  147. const uint8_t channel,
  148. const enum extint_callback_type type)
  149. {
  150. if (type == EXTINT_CALLBACK_TYPE_DETECT) {
  151. Eic *const eic = _extint_get_eic_from_channel(channel);
  152. eic->INTENSET.reg = (1UL << channel);
  153. }
  154. else {
  155. Assert(false);
  156. return STATUS_ERR_INVALID_ARG;
  157. }
  158. return STATUS_OK;
  159. }
  160. /**
  161. * \brief Disables asynchronous callback generation for a given channel and type.
  162. *
  163. * Disables asynchronous callbacks for a given logical external interrupt
  164. * channel and type.
  165. *
  166. * \param[in] channel Logical channel to disable callback generation for
  167. * \param[in] type Type of callback function callbacks to disable
  168. *
  169. * \return Status of the callback disable operation.
  170. * \retval STATUS_OK The callback was disabled successfully
  171. * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied
  172. */
  173. enum status_code extint_chan_disable_callback(
  174. const uint8_t channel,
  175. const enum extint_callback_type type)
  176. {
  177. if (type == EXTINT_CALLBACK_TYPE_DETECT) {
  178. Eic *const eic = _extint_get_eic_from_channel(channel);
  179. eic->INTENCLR.reg = (1UL << channel);
  180. }
  181. else {
  182. Assert(false);
  183. return STATUS_ERR_INVALID_ARG;
  184. }
  185. return STATUS_OK;
  186. }
  187. /**
  188. * \brief Find what channel caused the callback.
  189. *
  190. * Can be used in an EXTINT callback function to find what channel caused
  191. * the callback in case the same callback is used by multiple channels.
  192. *
  193. * \return Channel number.
  194. */
  195. uint8_t extint_get_current_channel(void)
  196. {
  197. return _current_channel;
  198. }
  199. /** Handler for the EXTINT hardware module interrupt. */
  200. void EIC_Handler(void)
  201. {
  202. /* Find any triggered channels, run associated callback handlers */
  203. for (_current_channel = 0; _current_channel < EIC_NUMBER_OF_INTERRUPTS ; _current_channel++) {
  204. if (extint_chan_is_detected(_current_channel)) {
  205. /* Clear flag */
  206. extint_chan_clear_detected(_current_channel);
  207. /* Find any associated callback entries in the callback table */
  208. if (_extint_dev.callbacks[_current_channel] != NULL) {
  209. /* Run the registered callback */
  210. _extint_dev.callbacks[_current_channel]();
  211. }
  212. }
  213. }
  214. }