nrf_svci_async_function.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * Copyright (c) 2017 - 2020, 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_svci_async_function Asynchronous Supervisor function interface
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief Macros to create Asynchronous Supervisor interface functions.
  47. */
  48. #ifndef NRF_SVC_ASYNC_FUNCTION_H__
  49. #define NRF_SVC_ASYNC_FUNCTION_H__
  50. #include "nrf_svci.h"
  51. #include "nrf_svci_async_handler.h"
  52. #ifdef __cplusplus
  53. extern "C" {
  54. #endif
  55. /**@brief Macro for creating type definition for SVCI interface init function.
  56. *
  57. * @warning Do not call this macro directly! Use @ref NRF_SVCI_ASYNC_FUNC_DECLARE instead.
  58. *
  59. * @param[in] name Name of async request function. Will be appended with _async_fn_t.
  60. * @param[in] param_type Parameter type.
  61. * @param[in] state_type State type.
  62. *
  63. * @retval Type definition a named SVCI async init function.
  64. */
  65. #define NRF_SVCI_ACYNC_FUNC_TYPEDEF(name, param_type, state_type) \
  66. typedef uint32_t (* name ## _async_fn_t)(param_type * p_param, state_type * p_state)
  67. /**@brief Macro for creating type definition for SVCI interface event function.
  68. *
  69. * @warning Do not call this macro directly! Use @ref NRF_SVCI_ASYNC_FUNC_DECLARE instead.
  70. *
  71. * @details Calling this function with sys-events will report @ref NRF_ERROR_BUSY until
  72. * the asynchronous calls is finished, at which time it will report either
  73. * NRF_SUCCESS or any another error-message.
  74. *
  75. * @param[in] name Name of the event function. Will be appended with _event_fn_t
  76. * @param[in] state_type Type parameter for the state.
  77. *
  78. * @retval Type definition for a named SVCI async event function.
  79. */
  80. #define NRF_SVCI_ASYNC_EVENT_FUNC_TYPEDEF(name, state_type) \
  81. typedef uint32_t (* name ## _event_fn_t)(uint32_t sys_evt, state_type * p_state)
  82. /**@brief Macro for creating a declaration of a named async function for the SVCI interface.
  83. *
  84. * @details The async interface provides a method to call into a external application
  85. * through the SVCI interface without relying on allocated or reserved memory inside the
  86. * external application.
  87. *
  88. * This macro declares variables and function types in use by the async SVCI interface.
  89. *
  90. * @note This is intended to be invoked in a header file shared by both the
  91. * caller and the recipient (handler).
  92. *
  93. * @param[in] svci_num SVC indirect number.
  94. * @param[in] name Name of the async function.
  95. * @param[in] param_type Type of the param used for the async interface.
  96. * @param[in] state_type Type of the state used for the async interface.
  97. *
  98. * @retval A type definition of NAME_svc_async_t to be used for async access
  99. * through the SVCI interface.
  100. */
  101. #define NRF_SVCI_ASYNC_FUNC_DECLARE(svci_num, \
  102. name, \
  103. param_type, \
  104. state_type) \
  105. /*lint --e{19} */ \
  106. NRF_SVCI_ACYNC_FUNC_TYPEDEF(name, param_type, state_type); \
  107. NRF_SVCI_ASYNC_EVENT_FUNC_TYPEDEF(name, state_type); \
  108. \
  109. typedef struct \
  110. { \
  111. name ## _async_fn_t async_func; \
  112. name ## _event_fn_t sys_evt_handler; \
  113. state_type state; \
  114. } name ## _svci_async_t;
  115. //lint -save --e{10, 19, 40, 102} -esym(526, *_init) -esym(628, *_init)
  116. /**@brief Macro for defining a named SVCI async interface.
  117. *
  118. * @details The async interface provides a method to call into an external application
  119. * through the SVCI interface without relying on allocated or reserved memory inside the
  120. * external application.
  121. *
  122. * Running this macro creates a defintion of the structure that holds the
  123. * information about the async function, the event handler, and the state.
  124. *
  125. * Running this macro also defines convenience functions to the SVCI interface.
  126. *
  127. * The available functions are:
  128. * -NAME_init - Function to call to set up the async SVCI interface.
  129. * -NAME - Function to call the async SVCI interface.
  130. * -NAME_on_sys_event - Function to report sys events to the async
  131. * SVCI interface.
  132. * -NAME_is_initialized - Function to check if the async SVCI interface is
  133. * initialized and ready to use.
  134. *
  135. * @note Invoking this macro is only possible in a source file as the macro creates
  136. * a static variable for the async interface as well as static functions to call
  137. * into the async interface.
  138. *
  139. * @param[in] svci_num SVC indirect number.
  140. * @param[in] name Name of the async function.
  141. * @param[in] param_type Type of the param used for the async interface.
  142. *
  143. * @retval Instance of the async SVCI interface and convenience functions for using it.
  144. */
  145. #define NRF_SVCI_ASYNC_FUNC_DEFINE(svci_num, name, param_type) \
  146. \
  147. SVCI(svci_num, uint32_t, name ## _svci_async_init, name ## _svci_async_t *, p_async); \
  148. static name ## _svci_async_t name ## _svci_async_def = {0}; \
  149. \
  150. static __INLINE uint32_t name ## _init (void) \
  151. { \
  152. return name ## _svci_async_init(&name ## _svci_async_def); \
  153. } \
  154. \
  155. static __INLINE uint32_t name(param_type * p_param) \
  156. { \
  157. return name ## _svci_async_def.async_func(p_param, &name ## _svci_async_def.state); \
  158. } \
  159. \
  160. static __INLINE uint32_t name ## _on_sys_evt(uint32_t sys_evt) \
  161. { \
  162. return name ## _svci_async_def.sys_evt_handler(sys_evt, &name ## _svci_async_def.state); \
  163. } \
  164. \
  165. static __INLINE uint32_t name ## _is_initialized(void) \
  166. { \
  167. return (name ## _svci_async_def.async_func != NULL && \
  168. name ## _svci_async_def.sys_evt_handler != NULL ); \
  169. }
  170. //lint -restore
  171. #ifdef __cplusplus
  172. }
  173. #endif
  174. #endif // NRF_SVC_ASYNC_FUNCTION_H__
  175. /** @} */