events_hooks.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * \file
  3. *
  4. * \brief SAM Event System Controller Driver
  5. *
  6. * Copyright (C) 2014-2016 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 "events.h"
  47. #include "events_hooks.h"
  48. #include "system_interrupt.h"
  49. #if (SAML21) || (SAML22) || (SAMC20) || (SAMC21) || (SAMR30)
  50. # define _EVENTS_INTFLAGS_DETECT 0x0fff0000
  51. # define _EVENTS_INTFLAGS_OVERRUN 0x00000fff
  52. #else
  53. # define _EVENTS_INTFLAGS_DETECT 0x0f00ff00
  54. # define _EVENTS_INTFLAGS_OVERRUN 0x000f00ff
  55. #endif
  56. #define _EVENTS_INTFLAGS_MASK (_EVENTS_INTFLAGS_DETECT | _EVENTS_INTFLAGS_OVERRUN)
  57. extern struct _events_module _events_inst;
  58. enum status_code events_create_hook(struct events_hook *hook, events_interrupt_hook func)
  59. {
  60. /* Initialize the hook struct members */
  61. hook->next = NULL;
  62. hook->resource = NULL;
  63. hook->hook_func = func;
  64. return STATUS_OK;
  65. }
  66. enum status_code events_add_hook(struct events_resource *resource, struct events_hook *hook)
  67. {
  68. struct events_hook *tmp_hook = NULL;
  69. /* Associate the hook with the resource */
  70. hook->resource = resource;
  71. /* Check if this is the first hook in the list */
  72. if (_events_inst.hook_list == NULL) {
  73. _events_inst.hook_list = hook;
  74. } else {
  75. tmp_hook = _events_inst.hook_list;
  76. /* Find the first free place in the list */
  77. while (tmp_hook->next != NULL) {
  78. tmp_hook = tmp_hook->next;
  79. }
  80. /* Put the hook into the next free place in the list */
  81. tmp_hook->next = hook;
  82. }
  83. /* Check if interrupts from the EVSYS module is enabled in the interrupt controller */
  84. if (!system_interrupt_is_enabled(SYSTEM_INTERRUPT_MODULE_EVSYS)) {
  85. system_interrupt_enable(SYSTEM_INTERRUPT_MODULE_EVSYS);
  86. }
  87. return STATUS_OK;
  88. }
  89. enum status_code events_del_hook(struct events_resource *resource, struct events_hook *hook)
  90. {
  91. struct events_hook *tmp_hook = _events_inst.hook_list;
  92. struct events_hook *last_hook = NULL;
  93. if (tmp_hook != NULL) {
  94. /* Check if the first hook in the list is the one we are looking for */
  95. if (tmp_hook != hook) {
  96. /* Don't double check the first hook */
  97. tmp_hook = tmp_hook->next;
  98. /* Check if the current hook is the one we are looking for */
  99. while (tmp_hook != hook) {
  100. /* If the current hook pointer is NULL the hook is not found in the list */
  101. if(tmp_hook == NULL) {
  102. return STATUS_ERR_NOT_FOUND;
  103. }
  104. last_hook = tmp_hook;
  105. tmp_hook = tmp_hook->next;
  106. }
  107. /* Remove the current hook from the list */
  108. last_hook->next = tmp_hook->next;
  109. } else {
  110. _events_inst.hook_list = tmp_hook->next;
  111. }
  112. } else {
  113. /* No hooks where found in the list */
  114. return STATUS_ERR_NO_MEMORY;
  115. }
  116. return STATUS_OK;
  117. }
  118. enum status_code events_enable_interrupt_source(struct events_resource *resource, enum events_interrupt_source source)
  119. {
  120. Assert((source == EVENTS_INTERRUPT_DETECT) || (source == EVENTS_INTERRUPT_OVERRUN));
  121. if (source == EVENTS_INTERRUPT_DETECT) {
  122. EVSYS->INTENSET.reg = _events_find_bit_position(resource->channel,
  123. _EVENTS_START_OFFSET_DETECTION_BIT);
  124. } else if (source == EVENTS_INTERRUPT_OVERRUN) {
  125. EVSYS->INTENSET.reg = _events_find_bit_position(resource->channel,
  126. _EVENTS_START_OFFSET_OVERRUN_BIT);
  127. } else {
  128. return STATUS_ERR_INVALID_ARG;
  129. }
  130. return STATUS_OK;
  131. }
  132. enum status_code events_disable_interrupt_source(struct events_resource *resource, enum events_interrupt_source source)
  133. {
  134. Assert((source == EVENTS_INTERRUPT_DETECT) || (source == EVENTS_INTERRUPT_OVERRUN));
  135. if (source == EVENTS_INTERRUPT_DETECT) {
  136. EVSYS->INTENCLR.reg = _events_find_bit_position(resource->channel,
  137. _EVENTS_START_OFFSET_DETECTION_BIT);
  138. } else if (source == EVENTS_INTERRUPT_OVERRUN) {
  139. EVSYS->INTENCLR.reg = _events_find_bit_position(resource->channel,
  140. _EVENTS_START_OFFSET_OVERRUN_BIT);
  141. } else {
  142. return STATUS_ERR_INVALID_ARG;
  143. }
  144. return STATUS_OK;
  145. }
  146. bool events_is_interrupt_set(struct events_resource *resource, enum events_interrupt_source source)
  147. {
  148. Assert((source == EVENTS_INTERRUPT_DETECT) || (source == EVENTS_INTERRUPT_OVERRUN));
  149. uint32_t bitpos;
  150. if (source == EVENTS_INTERRUPT_DETECT) {
  151. bitpos = _events_find_bit_position(resource->channel,
  152. _EVENTS_START_OFFSET_DETECTION_BIT);
  153. } else if (source == EVENTS_INTERRUPT_OVERRUN) {
  154. bitpos = _events_find_bit_position(resource->channel,
  155. _EVENTS_START_OFFSET_OVERRUN_BIT);
  156. } else {
  157. return false;
  158. }
  159. return (bool)(_events_inst.interrupt_flag_buffer & bitpos);
  160. }
  161. enum status_code events_ack_interrupt(struct events_resource *resource, enum events_interrupt_source source)
  162. {
  163. Assert((source == EVENTS_INTERRUPT_DETECT) || (source == EVENTS_INTERRUPT_OVERRUN));
  164. uint32_t bitpos;
  165. if (source == EVENTS_INTERRUPT_DETECT) {
  166. bitpos = _events_find_bit_position(resource->channel,
  167. _EVENTS_START_OFFSET_DETECTION_BIT);
  168. } else if (source == EVENTS_INTERRUPT_OVERRUN) {
  169. bitpos = _events_find_bit_position(resource->channel,
  170. _EVENTS_START_OFFSET_OVERRUN_BIT);
  171. } else {
  172. return STATUS_ERR_INVALID_ARG;
  173. }
  174. _events_inst.interrupt_flag_ack_buffer |= bitpos;
  175. return STATUS_OK;
  176. }
  177. void EVSYS_Handler(void)
  178. {
  179. struct events_hook *current_hook = _events_inst.hook_list;
  180. uint32_t flag;
  181. /* Synch the interrupt flag buffer with the hardware register */
  182. flag = EVSYS->INTFLAG.reg;
  183. _events_inst.interrupt_flag_buffer |= flag;
  184. /* Clear all hardware interrupt flags */
  185. EVSYS->INTFLAG.reg = _EVENTS_INTFLAGS_MASK;
  186. /* Traverse the linked list */
  187. while (current_hook != NULL) {
  188. current_hook->hook_func(current_hook->resource);
  189. current_hook = current_hook->next;
  190. }
  191. /* Clear acknowledged interrupt sources from the interrupt flag buffer */
  192. flag = _events_inst.interrupt_flag_ack_buffer;
  193. _events_inst.interrupt_flag_buffer &= ~flag;
  194. }