freqm_callback.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. *
  3. * \file
  4. *
  5. * \brief SAM Frequency Meter driver.
  6. *
  7. * This file defines a useful set of functions for the FREQM on SAM devices.
  8. *
  9. * Copyright (c) 2015-2016 Atmel Corporation. All rights reserved.
  10. *
  11. * \asf_license_start
  12. *
  13. * \page License
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. *
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. *
  25. * 3. The name of Atmel may not be used to endorse or promote products derived
  26. * from this software without specific prior written permission.
  27. *
  28. * 4. This software may only be redistributed and used in connection with an
  29. * Atmel microcontroller product.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  32. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  33. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  34. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  35. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  37. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  40. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41. * POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. * \asf_license_stop
  44. *
  45. */
  46. /*
  47. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  48. */
  49. #include "freqm.h"
  50. #include "freqm_callback.h"
  51. /** \internal Max number of callback type. */
  52. #define FREQM_CALLBACK_TYPE_NUM 1
  53. struct freqm_module *_freqm_instance;
  54. /**
  55. * \brief Registers a callback
  56. *
  57. * Registers a callback function which is implemented by the user.
  58. *
  59. * \note The callback must be enabled by \ref freqm_enable_callback,
  60. * in order for the interrupt handler to call it when the conditions for the
  61. * callback type is met.
  62. *
  63. * \param[in] module Pointer to FREQM software instance struct
  64. * \param[in] callback_func Pointer to callback function
  65. * \param[in] callback_type Callback type given by an enum
  66. *
  67. * \retval STATUS_OK The function exited successfully
  68. */
  69. enum status_code freqm_register_callback(
  70. struct freqm_module *const module,
  71. freqm_callback_t callback_func,
  72. enum freqm_callback callback_type)
  73. {
  74. /* Sanity check arguments */
  75. Assert(module);
  76. Assert(callback_func);
  77. if (callback_type >= FREQM_CALLBACK_TYPE_NUM) {
  78. Assert(false);
  79. return STATUS_ERR_INVALID_ARG;
  80. }
  81. /* Register callback function */
  82. module->callback[callback_type] = callback_func;
  83. return STATUS_OK;
  84. }
  85. /**
  86. * \brief Unregisters a callback
  87. *
  88. * Unregisters a callback function implemented by the user. The callback should be
  89. * disabled before it is unregistered.
  90. *
  91. * \param[in] module Pointer to FREQM software instance struct
  92. * \param[in] callback_type Callback type given by an enum
  93. *
  94. * \retval STATUS_OK The function exited successfully
  95. */
  96. enum status_code freqm_unregister_callback(
  97. struct freqm_module *module,
  98. enum freqm_callback callback_type)
  99. {
  100. /* Sanity check arguments */
  101. Assert(module);
  102. if (callback_type >= FREQM_CALLBACK_TYPE_NUM) {
  103. Assert(false);
  104. return STATUS_ERR_INVALID_ARG;
  105. }
  106. /* Unregister callback function */
  107. module->callback[callback_type] = NULL;
  108. return STATUS_OK;
  109. }
  110. /**
  111. * \internal The FREQM interrupt handler.
  112. */
  113. void FREQM_Handler(void)
  114. {
  115. /* Get device instance from the look-up table */
  116. struct freqm_module *module = _freqm_instance;
  117. /* Read and mask interrupt flag register */
  118. uint32_t status = FREQM->INTFLAG.reg;
  119. /* Check if data ready needs to be serviced */
  120. if (status & FREQM_INTFLAG_DONE) {
  121. if (module->callback[FREQM_CALLBACK_MEASURE_DONE]) {
  122. FREQM->INTFLAG.reg = FREQM_INTFLAG_DONE;
  123. module->callback[FREQM_CALLBACK_MEASURE_DONE]();
  124. }
  125. }
  126. }