nrf_systick.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /**
  2. * Copyright (c) 2016 - 2018, 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. #ifndef NRF_SYSTICK_H__
  41. #define NRF_SYSTICK_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_systick_hal SYSTICK HAL
  48. * @{
  49. * @ingroup nrf_systick
  50. * @brief Hardware access layer for managing the SYSTICK peripheral.
  51. *
  52. * SYSTICK is ARM peripheral, not Nordic design.
  53. * It means that it has no Nordic-typical interface with Tasks and Events.
  54. *
  55. * Its usage is limited here to implement simple delays.
  56. * Also keep in mind that this timer would be stopped when CPU is sleeping
  57. * (WFE/WFI instruction is successfully executed).
  58. */
  59. /**
  60. * @brief Mask of usable bits in the SysTick value
  61. */
  62. #define NRF_SYSTICK_VAL_MASK SysTick_VAL_CURRENT_Msk
  63. /**
  64. * @brief Flags used by SysTick configuration.
  65. *
  66. * @sa nrf_systick_csr_set
  67. * @sa nrf_systick_csr_get
  68. */
  69. typedef enum {
  70. NRF_SYSTICK_CSR_COUNTFLAG_MASK = SysTick_CTRL_COUNTFLAG_Msk, /**< Status flag: Returns 1 if timer counted to 0 since the last read of this register. */
  71. NRF_SYSTICK_CSR_CLKSOURCE_MASK = SysTick_CTRL_CLKSOURCE_Msk, /**< Configuration bit: Select the SysTick clock source. */
  72. NRF_SYSTICK_CSR_CLKSOURCE_REF = 0U << SysTick_CTRL_CLKSOURCE_Pos, /**< Configuration value: Select reference clock. */
  73. NRF_SYSTICK_CSR_CLKSOURCE_CPU = 1U << SysTick_CTRL_CLKSOURCE_Pos, /**< Configuration value: Select CPU clock. */
  74. NRF_SYSTICK_CSR_TICKINT_MASK = SysTick_CTRL_TICKINT_Msk, /**< Configuration bit: Enables SysTick exception request. */
  75. NRF_SYSTICK_CSR_TICKINT_ENABLE = 1U << SysTick_CTRL_TICKINT_Pos, /**< Configuration value: Counting down to zero does not assert the SysTick exception request. */
  76. NRF_SYSTICK_CSR_TICKINT_DISABLE = 0U << SysTick_CTRL_TICKINT_Pos, /**< Configuration value: Counting down to zero to asserts the SysTick exception request. */
  77. NRF_SYSTICK_CSR_ENABLE_MASK = SysTick_CTRL_ENABLE_Msk, /**< Configuration bit: Enable the SysTick timer. */
  78. NRF_SYSTICK_CSR_ENABLE = 1U << SysTick_CTRL_ENABLE_Pos, /**< Configuration value: Counter enabled. */
  79. NRF_SYSTICK_CSR_DISABLE = 0U << SysTick_CTRL_ENABLE_Pos /**< Configuration value: Counter disabled. */
  80. } nrf_systick_csr_flags_t;
  81. /**
  82. * @brief Get Configuration and Status Register
  83. *
  84. * @return Values composed by @ref nrf_systick_csr_flags_t.
  85. * @note The @ref NRF_SYSTICK_CSR_COUNTFLAG_MASK value is cleared when CSR register is read.
  86. */
  87. __STATIC_INLINE uint32_t nrf_systick_csr_get(void);
  88. /**
  89. * @brief Set Configuration and Status Register
  90. *
  91. * @param[in] val The value composed from @ref nrf_systick_csr_flags_t.
  92. */
  93. __STATIC_INLINE void nrf_systick_csr_set(uint32_t val);
  94. /**
  95. * @brief Get the current reload value.
  96. *
  97. * @return The reload register value.
  98. */
  99. __STATIC_INLINE uint32_t nrf_systick_load_get(void);
  100. /**
  101. * @brief Configure the reload value.
  102. *
  103. * @param[in] val The value to set in the reload register.
  104. */
  105. __STATIC_INLINE void nrf_systick_load_set(uint32_t val);
  106. /**
  107. * @brief Read the SysTick current value
  108. *
  109. * @return The current SysTick value
  110. * @sa NRF_SYSTICK_VAL_MASK
  111. */
  112. __STATIC_INLINE uint32_t nrf_systick_val_get(void);
  113. /**
  114. * @brief Clear the SysTick current value
  115. *
  116. * @note The SysTick does not allow setting current value.
  117. * Any write to VAL register would clear the timer.
  118. */
  119. __STATIC_INLINE void nrf_systick_val_clear(void);
  120. /**
  121. * @brief Read the calibration register
  122. *
  123. * @return The calibration register value
  124. */
  125. __STATIC_INLINE uint32_t nrf_systick_calib_get(void);
  126. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  127. __STATIC_INLINE uint32_t nrf_systick_csr_get(void)
  128. {
  129. return SysTick->CTRL;
  130. }
  131. __STATIC_INLINE void nrf_systick_csr_set(uint32_t val)
  132. {
  133. SysTick->CTRL = val;
  134. }
  135. __STATIC_INLINE uint32_t nrf_systick_load_get(void)
  136. {
  137. return SysTick->LOAD;
  138. }
  139. __STATIC_INLINE void nrf_systick_load_set(uint32_t val)
  140. {
  141. SysTick->LOAD = val;
  142. }
  143. __STATIC_INLINE uint32_t nrf_systick_val_get(void)
  144. {
  145. return SysTick->VAL;
  146. }
  147. __STATIC_INLINE void nrf_systick_val_clear(void)
  148. {
  149. SysTick->VAL = 0;
  150. }
  151. __STATIC_INLINE uint32_t nrf_systick_calib_get(void)
  152. {
  153. return SysTick->CALIB;
  154. }
  155. #endif /* SUPPRESS_INLINE_IMPLEMENTATION */
  156. /** @} */
  157. #ifdef __cplusplus
  158. }
  159. #endif
  160. #endif /* NRF_SYSTICK_H__ */