slcd_callback.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. *
  3. * \file
  4. *
  5. * \brief SAM Segment Liquid Crystal Display(SLCD) Controller.
  6. *
  7. * Copyright (c) 2015 Atmel Corporation. All rights reserved.
  8. *
  9. * \asf_license_start
  10. *
  11. * \page License
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. *
  23. * 3. The name of Atmel may not be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * 4. This software may only be redistributed and used in connection with an
  27. * Atmel microcontroller product.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  30. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  31. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  32. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  33. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  35. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  37. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  38. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. * POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. * \asf_license_stop
  42. *
  43. */
  44. /*
  45. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  46. */
  47. #include "slcd.h"
  48. #include "slcd_callback.h"
  49. /**
  50. * \internal
  51. * \brief slcd callback function pointer
  52. */
  53. slcd_callback_t slcd_callback_pointer[SLCD_CALLBACK_TYPE_NUM];
  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 slcd_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] callback_func Pointer to callback function
  64. * \param[in] callback_type Callback type given by an enum
  65. *
  66. * \retval STATUS_OK The function exited successfully
  67. * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied
  68. */
  69. enum status_code slcd_register_callback(
  70. const slcd_callback_t callback,
  71. const enum slcd_callback_type type)
  72. {
  73. if (type >= SLCD_CALLBACK_TYPE_NUM){
  74. return STATUS_ERR_INVALID_ARG;
  75. }
  76. slcd_callback_pointer[type] = callback;
  77. return STATUS_OK;
  78. }
  79. /**
  80. * \brief Unregisters a callback
  81. *
  82. * Unregisters a callback function implemented by the user.
  83. *
  84. * \param[in] callback_type Callback type given by an enum
  85. *
  86. * \retval STATUS_OK The function exited successfully
  87. * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied
  88. */
  89. enum status_code slcd_unregister_callback(
  90. const slcd_callback_t callback,
  91. const enum slcd_callback_type type)
  92. {
  93. if (type >= SLCD_CALLBACK_TYPE_NUM){
  94. return STATUS_ERR_INVALID_ARG;
  95. }
  96. slcd_callback_pointer[type] = NULL;
  97. return STATUS_OK;
  98. }
  99. /**
  100. * \internal The SLCD interrupt handler.
  101. */
  102. void SLCD_Handler(void)
  103. {
  104. uint32_t status = SLCD->INTFLAG.reg;
  105. for (uint8_t i = 0; i < SLCD_CALLBACK_TYPE_NUM; i++) {
  106. if (status & (1 << i)) {
  107. /* Clear the INTFLAG anyway */
  108. SLCD->INTFLAG.reg = 1 << i;
  109. if (slcd_callback_pointer[i]) {
  110. slcd_callback_pointer[i]((enum slcd_callback_type)i);
  111. }
  112. }
  113. }
  114. }