nrf_drv_common.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #ifndef NRF_DRV_COMMON_H__
  13. #define NRF_DRV_COMMON_H__
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include "nrf.h"
  17. #include "sdk_errors.h"
  18. #include "nrf_drv_config.h"
  19. /**
  20. * @brief Offset of event registers in every peripheral instance
  21. *
  22. * This is the offset where event registers start in the every peripheral.
  23. */
  24. #define NRF_DRV_COMMON_EVREGS_OFFSET 0x100U
  25. /**
  26. * @brief Driver state.
  27. */
  28. typedef enum
  29. {
  30. NRF_DRV_STATE_UNINITIALIZED, /**< Uninitialized. */
  31. NRF_DRV_STATE_INITIALIZED, /**< Initialized but powered off. */
  32. NRF_DRV_STATE_POWERED_ON
  33. } nrf_drv_state_t;
  34. /**
  35. * @brief Driver power state selection.
  36. */
  37. typedef enum
  38. {
  39. NRF_DRV_PWR_CTRL_ON, /**< Power on request. */
  40. NRF_DRV_PWR_CTRL_OFF /**< Power off request. */
  41. } nrf_drv_pwr_ctrl_t;
  42. /**
  43. * @brief IRQ handler type.
  44. */
  45. typedef void (*nrf_drv_irq_handler_t)(void);
  46. #if PERIPHERAL_RESOURCE_SHARING_ENABLED
  47. /**
  48. * @brief Function for acquiring shared peripheral resources associated with
  49. * the specified peripheral.
  50. *
  51. * Certain resources and registers are shared among peripherals that have
  52. * the same ID (for example: SPI0, SPIM0, SPIS0, TWI0, TWIM0, and TWIS0).
  53. * Only one of them can be utilized at a given time. This function reserves
  54. * proper resources to be used by the specified peripheral.
  55. * If PERIPHERAL_RESOURCE_SHARING_ENABLED is set to a non-zero value, IRQ
  56. * handlers for peripherals that are sharing resources with others are
  57. * implemented by the nrf_drv_common module instead of individual drivers.
  58. * The drivers must then specify their interrupt handling routines and
  59. * register them by using this function.
  60. *
  61. * @param[in] p_per_base Requested peripheral base pointer.
  62. * @param[in] handler Interrupt handler to register. May be NULL
  63. * if interrupts are not used for the peripheral.
  64. *
  65. * @retval NRF_SUCCESS If resources were acquired successfully.
  66. * @retval NRF_ERROR_BUSY If resources were already acquired.
  67. * @retval NRF_ERROR_INVALID_PARAM If the specified peripheral is not enabled
  68. * or the peripheral does not share resources
  69. * with other peripherals.
  70. */
  71. ret_code_t nrf_drv_common_per_res_acquire(void const * p_per_base,
  72. nrf_drv_irq_handler_t handler);
  73. /**
  74. * @brief Function for releasing shared resources reserved previously by
  75. * @ref nrf_drv_common_per_res_acquire() for the specified peripheral.
  76. *
  77. * @param[in] p_per_base Requested peripheral base pointer.
  78. */
  79. void nrf_drv_common_per_res_release(void const * p_per_base);
  80. #endif // PERIPHERAL_RESOURCE_SHARING_ENABLED
  81. /**
  82. * @brief Function sets priority and enables NVIC interrupt
  83. *
  84. * @note Function checks if correct priority is used when softdevice is present
  85. *
  86. * @param[in] IRQn Interrupt id
  87. * @param[in] priority Interrupt priority
  88. */
  89. void nrf_drv_common_irq_enable(IRQn_Type IRQn, uint8_t priority);
  90. /**
  91. * @brief Function disables NVIC interrupt
  92. *
  93. * @param[in] IRQn Interrupt id
  94. */
  95. __STATIC_INLINE void nrf_drv_common_irq_disable(IRQn_Type IRQn);
  96. /**
  97. * @brief Convert bit position to event code
  98. *
  99. * Function for converting the bit position in INTEN register to event code
  100. * that is equivalent to the offset of the event register from the beginning
  101. * of peripheral instance.
  102. *
  103. * For example the result of this function can be casted directly to
  104. * the types like @ref nrf_twis_event_t or @ref nrf_rng_events_t...
  105. *
  106. * @param bit Bit position in INTEN register
  107. * @return Event code to be casted to the right enum type or to be used in functions like
  108. * @ref nrf_rng_event_get
  109. *
  110. * @sa nrf_drv_event_to_bitpos
  111. */
  112. __STATIC_INLINE uint32_t nrf_drv_bitpos_to_event(uint32_t bit);
  113. /**
  114. * @brief Convert event code to bit position
  115. *
  116. * This function can be used to get bit position in INTEN register from event code.
  117. *
  118. * @param event Event code that may be casted from enum values from types like
  119. * @ref nrf_twis_event_t or @ref nrf_rng_events_t
  120. * @return Bit position in INTEN register that corresponds to the given code.
  121. *
  122. * @sa nrf_drv_bitpos_to_event
  123. */
  124. __STATIC_INLINE uint32_t nrf_drv_event_to_bitpos(uint32_t event);
  125. /**
  126. * @brief Get interrupt number connected with given instance
  127. *
  128. * Function returns interrupt number for a given instance of any peripheral.
  129. * @param[in] pinst Pointer to peripheral registry
  130. * @return Interrupt number
  131. */
  132. __STATIC_INLINE IRQn_Type nrf_drv_get_IRQn(void const * const pinst);
  133. /**
  134. * @brief Check if given object is in RAM
  135. *
  136. * Function for analyzing if given location is placed in RAM.
  137. * This function is used to determine if we have address that can be supported by EasyDMA.
  138. * @param[in] ptr Pointer to the object
  139. * @retval true Object is located in RAM
  140. * @retval false Object is not located in RAM
  141. */
  142. __STATIC_INLINE bool nrf_drv_is_in_RAM(void const * const ptr);
  143. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  144. __STATIC_INLINE void nrf_drv_common_irq_disable(IRQn_Type IRQn)
  145. {
  146. NVIC_DisableIRQ(IRQn);
  147. }
  148. __STATIC_INLINE uint32_t nrf_drv_bitpos_to_event(uint32_t bit)
  149. {
  150. return NRF_DRV_COMMON_EVREGS_OFFSET + bit * sizeof(uint32_t);
  151. }
  152. __STATIC_INLINE uint32_t nrf_drv_event_to_bitpos(uint32_t event)
  153. {
  154. return (event - NRF_DRV_COMMON_EVREGS_OFFSET) / sizeof(uint32_t);
  155. }
  156. __STATIC_INLINE IRQn_Type nrf_drv_get_IRQn(void const * const pinst)
  157. {
  158. uint8_t ret = (uint8_t)((uint32_t)pinst>>12U);
  159. return (IRQn_Type) ret;
  160. }
  161. __STATIC_INLINE bool nrf_drv_is_in_RAM(void const * const ptr)
  162. {
  163. return ((((uintptr_t)ptr) & 0xE0000000u) == 0x20000000u);
  164. }
  165. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  166. #endif // NRF_DRV_COMMON_H__