nrf_temp.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**
  2. * Copyright (c) 2012 - 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. #ifndef NRF_TEMP_H__
  41. #define NRF_TEMP_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_temp_hal_deprecated TEMP HAL (deprecated)
  48. * @{
  49. * @ingroup nrf_temp
  50. * @brief Temperature module init and read functions.
  51. */
  52. /** @brief Workaround specific define - sign mask.*/
  53. #define MASK_SIGN (0x00000200UL)
  54. /** @brief Workaround specific define - sign extension mask.*/
  55. #define MASK_SIGN_EXTENSION (0xFFFFFC00UL)
  56. /**
  57. * @brief Function for preparing the TEMP module for temperature measurement.
  58. *
  59. * This function initializes the TEMP module and writes to the hidden configuration register.
  60. */
  61. static __INLINE void nrf_temp_init(void)
  62. {
  63. /**@note Workaround for PAN_028 rev2.0A anomaly 31 - TEMP: Temperature offset value has to be manually loaded to the TEMP module */
  64. *(uint32_t *) 0x4000C504 = 0;
  65. }
  66. /**
  67. * @brief Function for reading temperature measurement.
  68. *
  69. * The function reads the 10-bit 2's complement value and transforms it to a 32-bit 2's complement value.
  70. */
  71. static __INLINE int32_t nrf_temp_read(void)
  72. {
  73. /**@note Workaround for PAN_028 rev2.0A anomaly 28 - TEMP: Negative measured values are not represented correctly */
  74. return ((NRF_TEMP->TEMP & MASK_SIGN) != 0) ?
  75. (int32_t)(NRF_TEMP->TEMP | MASK_SIGN_EXTENSION) : (NRF_TEMP->TEMP);
  76. }
  77. /** @} */
  78. /**
  79. * @defgroup nrf_temp_hal TEMP HAL
  80. * @{
  81. * @ingroup nrf_temp
  82. * @brief Hardware access layer for managing the Temperature sensor (TEMP).
  83. */
  84. /** @brief TEMP tasks. */
  85. typedef enum
  86. {
  87. NRF_TEMP_TASK_START = offsetof(NRF_TEMP_Type, TASKS_START), /**< Start temperature measurement. */
  88. NRF_TEMP_TASK_STOP = offsetof(NRF_TEMP_Type, TASKS_STOP) /**< Stop temperature measurement. */
  89. } nrf_temp_task_t;
  90. /** @brief TEMP events. */
  91. typedef enum
  92. {
  93. NRF_TEMP_EVENT_DATARDY = offsetof(NRF_TEMP_Type, EVENTS_DATARDY) /**< Temperature measurement complete, data ready. */
  94. } nrf_temp_event_t;
  95. /** @brief TEMP interrupts. */
  96. typedef enum
  97. {
  98. NRF_TEMP_INT_DATARDY_MASK = TEMP_INTENSET_DATARDY_Msk /**< Interrupt on DATARDY event. */
  99. } nrf_temp_int_mask_t;
  100. /**
  101. * @brief Function for enabling specified interrupts.
  102. *
  103. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  104. * @param[in] mask Mask of interrupts to be enabled.
  105. */
  106. __STATIC_INLINE void nrf_temp_int_enable(NRF_TEMP_Type * p_reg, uint32_t mask);
  107. /**
  108. * @brief Function for disabling specified interrupts.
  109. *
  110. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  111. * @param[in] mask Mask of interrupts to be disabled.
  112. */
  113. __STATIC_INLINE void nrf_temp_int_disable(NRF_TEMP_Type * p_reg, uint32_t mask);
  114. /**
  115. * @brief Function for retrieving the state of a given interrupt.
  116. *
  117. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  118. * @param[in] temp_int Interrupt to be checked.
  119. *
  120. * @retval true The interrupt is enabled.
  121. * @retval false The interrupt is not enabled.
  122. */
  123. __STATIC_INLINE bool nrf_temp_int_enable_check(NRF_TEMP_Type const * p_reg,
  124. nrf_temp_int_mask_t temp_int);
  125. /**
  126. * @brief Function for getting the address of the specified TEMP task register.
  127. *
  128. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  129. * @param[in] task Requested task.
  130. *
  131. * @return Address of the requested task register.
  132. */
  133. __STATIC_INLINE uint32_t nrf_temp_task_address_get(NRF_TEMP_Type const * p_reg,
  134. nrf_temp_task_t task);
  135. /**
  136. * @brief Function for activating the specified TEMP task.
  137. *
  138. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  139. * @param[in] task Task to be activated.
  140. */
  141. __STATIC_INLINE void nrf_temp_task_trigger(NRF_TEMP_Type * p_reg, nrf_temp_task_t task);
  142. /**
  143. * @brief Function for getting the address of the specified TEMP event register.
  144. *
  145. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  146. * @param[in] event Requested event.
  147. *
  148. * @return Address of the requested event register.
  149. */
  150. __STATIC_INLINE uint32_t nrf_temp_event_address_get(NRF_TEMP_Type const * p_reg,
  151. nrf_temp_event_t event);
  152. /**
  153. * @brief Function for clearing the specified TEMP event.
  154. *
  155. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  156. * @param[in] event Event to clear.
  157. */
  158. __STATIC_INLINE void nrf_temp_event_clear(NRF_TEMP_Type * p_reg, nrf_temp_event_t event);
  159. /**
  160. * @brief Function for getting the state of a specific event.
  161. *
  162. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  163. * @param[in] event Event to be checked.
  164. *
  165. * @retval true The event has been generated.
  166. * @retval false The event has not been generated.
  167. */
  168. __STATIC_INLINE bool nrf_temp_event_check(NRF_TEMP_Type const * p_reg, nrf_temp_event_t event);
  169. /**
  170. * @brief Function for getting the result of temperature measurement.
  171. *
  172. * @note Returned value is in 2's complement format, 0.25 °C steps
  173. *
  174. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  175. *
  176. * @return Temperature value register contents.
  177. */
  178. __STATIC_INLINE int32_t nrf_temp_result_get(NRF_TEMP_Type const * p_reg);
  179. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  180. __STATIC_INLINE void nrf_temp_int_enable(NRF_TEMP_Type * p_reg, uint32_t mask)
  181. {
  182. p_reg->INTENSET = mask;
  183. }
  184. __STATIC_INLINE void nrf_temp_int_disable(NRF_TEMP_Type * p_reg, uint32_t mask)
  185. {
  186. p_reg->INTENCLR = mask;
  187. }
  188. __STATIC_INLINE bool nrf_temp_int_enable_check(NRF_TEMP_Type const * p_reg,
  189. nrf_temp_int_mask_t temp_int)
  190. {
  191. return (bool)(p_reg->INTENSET & temp_int);
  192. }
  193. __STATIC_INLINE uint32_t nrf_temp_task_address_get(NRF_TEMP_Type const * p_reg,
  194. nrf_temp_task_t task)
  195. {
  196. return (uint32_t)((uint8_t *)p_reg + (uint32_t)task);
  197. }
  198. __STATIC_INLINE void nrf_temp_task_trigger(NRF_TEMP_Type * p_reg, nrf_temp_task_t task)
  199. {
  200. *(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task) = 1;
  201. }
  202. __STATIC_INLINE uint32_t nrf_temp_event_address_get(NRF_TEMP_Type const * p_reg,
  203. nrf_temp_event_t event)
  204. {
  205. return (uint32_t)((uint8_t *)p_reg + (uint32_t)event);
  206. }
  207. __STATIC_INLINE void nrf_temp_event_clear(NRF_TEMP_Type * p_reg, nrf_temp_event_t event)
  208. {
  209. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0;
  210. #if __CORTEX_M == 0x04
  211. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event));
  212. (void)dummy;
  213. #endif
  214. }
  215. __STATIC_INLINE bool nrf_temp_event_check(NRF_TEMP_Type const * p_reg, nrf_temp_event_t event)
  216. {
  217. return (bool)*((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event));
  218. }
  219. __STATIC_INLINE int32_t nrf_temp_result_get(NRF_TEMP_Type const * p_reg)
  220. {
  221. int32_t raw_measurement = p_reg->TEMP;
  222. #if defined(NRF51)
  223. /* Apply workaround for the nRF51 series anomaly 28 - TEMP: Negative measured values are not represented correctly. */
  224. if ((raw_measurement & 0x00000200) != 0)
  225. {
  226. raw_measurement |= 0xFFFFFC00UL;
  227. }
  228. #endif
  229. return raw_measurement;
  230. }
  231. #endif
  232. /** @} */
  233. #ifdef __cplusplus
  234. }
  235. #endif
  236. #endif // NRF_TEMP_H__