nrf_svc_function.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * Copyright (c) 2016 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file
  41. *
  42. * @defgroup sdk_nrf_svc_function Supervisor function
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief Macros to create Supervisor functions.
  47. */
  48. #ifndef NRF_SVC_FUNCTION_H__
  49. #define NRF_SVC_FUNCTION_H__
  50. #include <stdint.h>
  51. #include "nrf_section.h"
  52. #include "app_util.h"
  53. #include "nrf_svci.h"
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. /** @brief Function to be called from an SVC handler.
  58. *
  59. * @warning This function prototype has no arguments. It will be cast to a function prototype
  60. * that has 0 to 4 arguments. 4 arguments is the highest number of allowed arguments in
  61. * a Supervisor call.
  62. *
  63. * @warning The nrf_svc_func_t function prototype should not have void as parameter list as there
  64. * will be 0 to 4 arguments after casting.
  65. */
  66. typedef uint32_t (*nrf_svc_func_t)();
  67. /** @brief Type holding the SVC number, SVCI number, and the pointer to the corresponding handler
  68. * function.
  69. *
  70. * @note The function that is pointed to must not change version.
  71. */
  72. typedef struct
  73. {
  74. uint32_t svc_num; /**< Supervisor call number (actually 8-bit, padded for alignment). */
  75. uint32_t svci_num; /**< Supervisor call indirect number. */
  76. nrf_svc_func_t func_ptr;
  77. } nrf_svc_func_reg_t;
  78. // Verify that the size of nrf_svc_func_t is aligned to make sure it can be used in nrf_section.
  79. STATIC_ASSERT(sizeof(nrf_svc_func_reg_t) % 4 == 0);
  80. /** @brief Macro for registering a structure holding SVC number and SVC handler
  81. * function pointer.
  82. *
  83. * @details This macro places a variable in a section named "svc_data" that
  84. * the SVC handler uses during regular operation.
  85. *
  86. * @note This macro must be invoked from a source file. There should only be one
  87. * registration by a given SVC number. SVC number 0 (zero) is invalid input
  88. * and will cause a compile time assertion.
  89. *
  90. * @param[in] name Name of the structure. Logically accessible from the source file.
  91. * @param[in] svc_number SVC number to register.
  92. * @param[in] func Function to call for a given SVC number.
  93. *
  94. * @retval Variable registration in @ref lib_section_vars named svc_data.
  95. */
  96. #define NRF_SVC_FUNCTION_REGISTER(svc_number, name, func) \
  97. STATIC_ASSERT(svc_number != 0); \
  98. NRF_SECTION_ITEM_REGISTER(svc_data, nrf_svc_func_reg_t const name) = \
  99. { \
  100. .svc_num = svc_number, \
  101. .svci_num = NRF_SVCI_SVC_NUM_INVALID, \
  102. .func_ptr = (nrf_svc_func_t)func \
  103. }
  104. /** @brief Macro for registering a structure holding SVC number, SVCI number, and SVCI handler
  105. * function pointer.
  106. *
  107. * @details This macro places a variable in a section named "svc_data" that
  108. * the SVC handler uses during regular operation.
  109. *
  110. * @note This macro must be invoked from a source file. There should only be one registration
  111. * for a given SVC indirect number.
  112. *
  113. * @param[in] name Name of the structure. Logically accessible from the source file.
  114. * @param[in] svci_number SVC indirect number to register.
  115. * @param[in] func Function to call for a given SVC indirect number.
  116. *
  117. * @retval Variable registration in @ref lib_section_vars named svc_data.
  118. */
  119. #define NRF_SVCI_FUNCTION_REGISTER(svci_number, name, func) \
  120. NRF_SECTION_ITEM_REGISTER(svc_data, nrf_svc_func_reg_t const name) = \
  121. { \
  122. .svc_num = NRF_SVCI_SVC_NUM, \
  123. .svci_num = svci_number, \
  124. .func_ptr = (nrf_svc_func_t)func \
  125. }
  126. #ifdef __cplusplus
  127. }
  128. #endif
  129. #endif // NRF_SVC_FUNCTION_H__
  130. /** @} */