tsens_callback.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * \file
  3. *
  4. * \brief SAM Temperature Sensor Driver
  5. *
  6. * Copyright (C) 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. #ifndef TSENS_CALLBACK_H_INCLUDED
  47. #define TSENS_CALLBACK_H_INCLUDED
  48. #ifdef __cplusplus
  49. extern "C" {
  50. #endif
  51. #include <compiler.h>
  52. #include <system_interrupt.h>
  53. #include "tsens.h"
  54. /**
  55. * \addtogroup asfdoc_sam0_tsens_group
  56. *
  57. * @{
  58. */
  59. /**
  60. * \brief TSENS Callback Types.
  61. *
  62. * Callback types for TSENS callback driver.
  63. *
  64. */
  65. enum tsens_callback {
  66. /** Callback for result ready */
  67. TSENS_CALLBACK_RESULT_READY,
  68. /** Callback when result overwritten before read */
  69. TSENS_CALLBACK_OVERRUN,
  70. /** Callback when window is hit */
  71. TSENS_CALLBACK_WINDOW,
  72. /** Callback when the result overflows */
  73. TSENS_CALLBACK_OVF,
  74. # if !defined(__DOXYGEN__)
  75. /** Number of available callbacks */
  76. TSENS_CALLBACK_NUM,
  77. # endif
  78. };
  79. /** Type of the callback functions. */
  80. typedef void (*tsens_callback_t)(enum tsens_callback);
  81. /**
  82. * \brief TSENS software device instance structure.
  83. *
  84. * TSENS software instance structure, used to retain software state information
  85. * of an associated hardware module instance.
  86. *
  87. * \note The fields of this structure should not be altered by the user
  88. * application; they are reserved for module-internal use only.
  89. */
  90. struct tsens_module {
  91. #if !defined(__DOXYGEN__)
  92. /** Array to store callback functions. */
  93. tsens_callback_t callback[TSENS_CALLBACK_NUM];
  94. /** Pointer to used for TSENS results. */
  95. volatile int32_t *value;
  96. #endif
  97. };
  98. /**
  99. * \name Callback Management
  100. * @{
  101. */
  102. enum status_code tsens_register_callback(
  103. struct tsens_module *const module,
  104. tsens_callback_t callback_func,
  105. enum tsens_callback callback_type);
  106. enum status_code tsens_unregister_callback(
  107. struct tsens_module *const module,
  108. enum tsens_callback callback_type);
  109. /**
  110. * \brief Enables callback.
  111. *
  112. * Enables the callback function registered by \ref
  113. * tsens_register_callback. The callback function will be called from the
  114. * interrupt handler when the conditions for the callback type are met.
  115. *
  116. * \param[in] callback_type Callback type given by an enum
  117. *
  118. */
  119. static inline void tsens_enable_callback(enum tsens_callback callback_type)
  120. {
  121. uint32_t inenset_temp = 0;
  122. switch (callback_type) {
  123. case TSENS_CALLBACK_RESULT_READY:
  124. inenset_temp |= TSENS_INTFLAG_RESRDY;
  125. break;
  126. case TSENS_CALLBACK_OVERRUN:
  127. inenset_temp |= TSENS_INTENSET_OVERRUN;
  128. break;
  129. case TSENS_CALLBACK_WINDOW:
  130. inenset_temp |= TSENS_INTENSET_WINMON;
  131. break;
  132. case TSENS_CALLBACK_OVF:
  133. inenset_temp |= TSENS_INTENSET_OVF;
  134. break;
  135. default:
  136. break;
  137. }
  138. /* Enable the interrupt for the callback */
  139. TSENS->INTENSET.reg = inenset_temp;
  140. }
  141. /**
  142. * \brief Disables callback.
  143. *
  144. * Disables the callback function registered by the \ref
  145. * tsens_register_callback.
  146. *
  147. * \param[in] callback_type Callback type given by an enum
  148. *
  149. */
  150. static inline void tsens_disable_callback(enum tsens_callback callback_type)
  151. {
  152. uint32_t inenclr_temp = 0;
  153. switch (callback_type) {
  154. case TSENS_CALLBACK_RESULT_READY:
  155. inenclr_temp |= TSENS_INTENCLR_OVERRUN;
  156. break;
  157. case TSENS_CALLBACK_OVERRUN:
  158. inenclr_temp |= TSENS_INTENSET_OVERRUN;
  159. break;
  160. case TSENS_CALLBACK_WINDOW:
  161. inenclr_temp |= TSENS_INTENSET_WINMON;
  162. break;
  163. case TSENS_CALLBACK_OVF:
  164. inenclr_temp |= TSENS_INTENSET_OVF;
  165. break;
  166. default:
  167. break;
  168. }
  169. /* Disable the interrupt for the callback */
  170. TSENS->INTENCLR.reg = inenclr_temp;
  171. }
  172. void tsens_read_job(struct tsens_module *const module_inst, int32_t *result);
  173. /** @} */
  174. /** @} */
  175. #ifdef __cplusplus
  176. }
  177. #endif
  178. #endif /* TSENS_CALLBACK_H_INCLUDED */