nrf_drv_swi.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. /**@file
  13. *
  14. * @defgroup lib_driver_swi SWI driver
  15. * @{
  16. * @ingroup nrf_drivers
  17. *
  18. * @brief Driver for software interrupts (SWI).
  19. * @details The SWI driver allows the user to allocate SWIs and pass extra flags to interrupt handler functions.
  20. */
  21. #ifndef NRF_DRV_SWI_H__
  22. #define NRF_DRV_SWI_H__
  23. #include <stdbool.h>
  24. #include <stdint.h>
  25. #include "app_util.h"
  26. #include "app_util_platform.h"
  27. #include "nrf_drv_config.h"
  28. #include "sdk_errors.h"
  29. #ifndef EGU_ENABLED
  30. #define EGU_ENABLED 0
  31. #endif
  32. #if EGU_ENABLED > 0
  33. #include "nrf_egu.h"
  34. #endif
  35. typedef uint8_t nrf_swi_t; ///< @brief SWI channel (unsigned integer).
  36. /** @brief SWI user flags (unsigned integer).
  37. *
  38. * User flags are set during the SWI trigger and passed to the callback function as an argument.
  39. */
  40. typedef uint16_t nrf_swi_flags_t;
  41. /** @brief Unallocated channel value. */
  42. #define NRF_SWI_UNALLOCATED ((nrf_swi_t) 0xFFFFFFFFuL)
  43. /** @brief SWI handler function.
  44. *
  45. * Takes two arguments: SWI number (nrf_swi_t) and flags (nrf_swi_flags_t).
  46. */
  47. typedef void (* nrf_swi_handler_t)(nrf_swi_t, nrf_swi_flags_t);
  48. /**@brief Maximum numbers of SWIs. This number is fixed for a specific chip. */
  49. #if EGU_ENABLED > 0
  50. #define SWI_MAX NRF_EGU_COUNT
  51. #else
  52. #define SWI_MAX 6
  53. #endif
  54. /**@brief Number of flags per SWI (fixed number). */
  55. #if EGU_ENABLED > 0
  56. #define SWI_MAX_FLAGS NRF_EGU_CHANNEL_COUNT
  57. #else
  58. #define SWI_MAX_FLAGS 16
  59. #endif
  60. #ifndef SWI_COUNT
  61. /** @brief Number of software interrupts available.
  62. *
  63. * This number can be set in the range from 1 to SWI_MAX.
  64. */
  65. #define SWI_COUNT 4
  66. #endif
  67. #ifdef SOFTDEVICE_PRESENT
  68. #if SWI_COUNT > 2
  69. #undef SWI_COUNT
  70. #define SWI_COUNT 2
  71. #endif
  72. #else
  73. #ifdef SVCALL_AS_NORMAL_FUNCTION
  74. // Serialization is enabled.
  75. #if SWI_COUNT > 2
  76. #undef SWI_COUNT
  77. #define SWI_COUNT 2
  78. #endif
  79. #endif
  80. #endif
  81. /**@brief Default SWI priority. */
  82. #define SWI_DEFAULT_PRIORITY APP_IRQ_PRIORITY_LOW
  83. /**@brief Function for initializing the SWI module.
  84. *
  85. * @retval NRF_SUCCESS If the module was successfully initialized.
  86. * @retval MODULE_ALREADY_INITIALIZED If the module has already been initialized.
  87. */
  88. ret_code_t nrf_drv_swi_init(void);
  89. /**@brief Function for uninitializing the SWI module.
  90. *
  91. * This function also disables all SWIs.
  92. */
  93. void nrf_drv_swi_uninit(void);
  94. /**@brief Function for allocating a first unused SWI instance and setting a handler.
  95. * @details The event handler function returns void and takes one uint32_t argument (SWI number).
  96. *
  97. * @param[out] p_swi Pointer to the SWI that has been allocated.
  98. * @param[in] event_handler Event handler function (must not be NULL).
  99. * @param[in] priority Interrupt priority.
  100. *
  101. * @retval NRF_SUCCESS If the SWI was successfully allocated.
  102. * @retval NRF_ERROR_NO_MEM If there is no available SWI to be used.
  103. */
  104. ret_code_t nrf_drv_swi_alloc(nrf_swi_t * p_swi, nrf_swi_handler_t event_handler, uint32_t priority);
  105. /**@brief Function for freeing a previously allocated SWI.
  106. *
  107. * @param[in,out] p_swi SWI to free. The value is changed to NRF_SWI_UNALLOCATED on success.
  108. */
  109. void nrf_drv_swi_free(nrf_swi_t * p_swi);
  110. /**@brief Function for triggering the SWI.
  111. *
  112. * @param[in] swi SWI to trigger.
  113. * @param[in] flag_number Number of user flag to trigger.
  114. */
  115. void nrf_drv_swi_trigger(nrf_swi_t swi, uint8_t flag_number);
  116. #if EGU_ENABLED > 0
  117. /**@brief Function for returning the EGU trigger task address.
  118. *
  119. * @param[in] swi SWI instance.
  120. * @param[in] channel Number of the EGU channel.
  121. *
  122. * @returns EGU trigger task address.
  123. */
  124. uint32_t nrf_drv_swi_task_trigger_address_get(nrf_swi_t swi, uint8_t channel);
  125. /**@brief Function for returning the EGU triggered event address.
  126. *
  127. * @param[in] swi SWI instance.
  128. * @param[in] channel Number of the EGU channel.
  129. *
  130. * @returns EGU triggered event address.
  131. */
  132. uint32_t nrf_drv_swi_event_triggered_address_get(nrf_swi_t swi, uint8_t channel);
  133. #endif // EGU_ENABLED > 0
  134. #endif // NRF_DRV_SWI_H__
  135. /** @} */