1 /*
2  * Copyright (c) 2012 - 2020, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  *    list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  *    contributors may be used to endorse or promote products derived from this
17  *    software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef NRF_TEMP_H__
33 #define NRF_TEMP_H__
34 
35 #include <nrfx.h>
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /**
42 * @defgroup nrf_temp_hal TEMP HAL
43 * @{
44 * @ingroup nrf_temp
45 * @brief   Hardware access layer for managing the Temperature sensor (TEMP).
46 */
47 
48 /** @brief TEMP tasks. */
49 typedef enum
50 {
51     NRF_TEMP_TASK_START = offsetof(NRF_TEMP_Type, TASKS_START), /**< Start temperature measurement. */
52     NRF_TEMP_TASK_STOP  = offsetof(NRF_TEMP_Type, TASKS_STOP)   /**< Stop temperature measurement. */
53 } nrf_temp_task_t;
54 
55 /** @brief TEMP events. */
56 typedef enum
57 {
58     NRF_TEMP_EVENT_DATARDY = offsetof(NRF_TEMP_Type, EVENTS_DATARDY) /**< Temperature measurement complete, data ready. */
59 } nrf_temp_event_t;
60 
61 /** @brief TEMP interrupts. */
62 typedef enum
63 {
64     NRF_TEMP_INT_DATARDY_MASK = TEMP_INTENSET_DATARDY_Msk /**< Interrupt on DATARDY event.  */
65 } nrf_temp_int_mask_t;
66 
67 /**
68  * @brief Function for enabling specified interrupts.
69  *
70  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
71  * @param[in] mask  Mask of interrupts to be enabled.
72  */
73 NRF_STATIC_INLINE void nrf_temp_int_enable(NRF_TEMP_Type * p_reg, uint32_t mask);
74 
75 /**
76  * @brief Function for disabling specified interrupts.
77  *
78  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
79  * @param[in] mask  Mask of interrupts to be disabled.
80  */
81 NRF_STATIC_INLINE void nrf_temp_int_disable(NRF_TEMP_Type * p_reg, uint32_t mask);
82 
83 /**
84  * @brief Function for checking if the specified interrupts are enabled.
85  *
86  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
87  * @param[in] mask  Mask of interrupts to be checked.
88  *
89  * @return Mask of enabled interrupts.
90  */
91 NRF_STATIC_INLINE uint32_t nrf_temp_int_enable_check(NRF_TEMP_Type const * p_reg, uint32_t mask);
92 
93 /**
94  * @brief Function for getting the address of the specified TEMP task register.
95  *
96  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
97  * @param[in] task  Requested task.
98  *
99  * @return Address of the requested task register.
100  */
101 NRF_STATIC_INLINE uint32_t nrf_temp_task_address_get(NRF_TEMP_Type const * p_reg,
102                                                      nrf_temp_task_t       task);
103 
104 /**
105  * @brief Function for activating the specified TEMP task.
106  *
107  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
108  * @param[in] task  Task to be activated.
109  */
110 NRF_STATIC_INLINE void nrf_temp_task_trigger(NRF_TEMP_Type * p_reg, nrf_temp_task_t task);
111 
112 /**
113  * @brief Function for getting the address of the specified TEMP event register.
114  *
115  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
116  * @param[in] event Requested event.
117  *
118  * @return Address of the requested event register.
119  */
120 NRF_STATIC_INLINE uint32_t nrf_temp_event_address_get(NRF_TEMP_Type const * p_reg,
121                                                       nrf_temp_event_t      event);
122 
123 /**
124  * @brief Function for clearing the specified TEMP event.
125  *
126  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
127  * @param[in] event Event to clear.
128  */
129 NRF_STATIC_INLINE void nrf_temp_event_clear(NRF_TEMP_Type *  p_reg, nrf_temp_event_t event);
130 
131 /**
132  * @brief Function for getting the state of a specific event.
133  *
134  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
135  * @param[in] event Event to be checked.
136  *
137  * @retval true  The event has been generated.
138  * @retval false The event has not been generated.
139  */
140 NRF_STATIC_INLINE bool nrf_temp_event_check(NRF_TEMP_Type const * p_reg, nrf_temp_event_t event);
141 
142 /**
143  * @brief Function for getting the result of temperature measurement.
144  *
145  * @note Returned value is in 2's complement format, 0.25 °C steps
146  *
147  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
148  *
149  * @return Temperature value register contents.
150  */
151 NRF_STATIC_INLINE int32_t nrf_temp_result_get(NRF_TEMP_Type const * p_reg);
152 
153 #ifndef NRF_DECLARE_ONLY
154 
nrf_temp_int_enable(NRF_TEMP_Type * p_reg,uint32_t mask)155 NRF_STATIC_INLINE void nrf_temp_int_enable(NRF_TEMP_Type * p_reg, uint32_t mask)
156 {
157     p_reg->INTENSET = mask;
158 }
159 
nrf_temp_int_disable(NRF_TEMP_Type * p_reg,uint32_t mask)160 NRF_STATIC_INLINE void nrf_temp_int_disable(NRF_TEMP_Type * p_reg, uint32_t mask)
161 {
162     p_reg->INTENCLR = mask;
163 }
164 
nrf_temp_int_enable_check(NRF_TEMP_Type const * p_reg,uint32_t mask)165 NRF_STATIC_INLINE uint32_t nrf_temp_int_enable_check(NRF_TEMP_Type const * p_reg, uint32_t mask)
166 {
167     return p_reg->INTENSET & mask;
168 }
169 
nrf_temp_task_address_get(NRF_TEMP_Type const * p_reg,nrf_temp_task_t task)170 NRF_STATIC_INLINE uint32_t nrf_temp_task_address_get(NRF_TEMP_Type const * p_reg,
171                                                      nrf_temp_task_t       task)
172 {
173     return (uint32_t)((uint8_t *)p_reg + (uint32_t)task);
174 }
175 
nrf_temp_task_trigger(NRF_TEMP_Type * p_reg,nrf_temp_task_t task)176 NRF_STATIC_INLINE void nrf_temp_task_trigger(NRF_TEMP_Type * p_reg, nrf_temp_task_t task)
177 {
178     *(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task) = 1;
179 }
180 
nrf_temp_event_address_get(NRF_TEMP_Type const * p_reg,nrf_temp_event_t event)181 NRF_STATIC_INLINE uint32_t nrf_temp_event_address_get(NRF_TEMP_Type const * p_reg,
182                                                       nrf_temp_event_t      event)
183 {
184     return (uint32_t)((uint8_t *)p_reg + (uint32_t)event);
185 }
186 
nrf_temp_event_clear(NRF_TEMP_Type * p_reg,nrf_temp_event_t event)187 NRF_STATIC_INLINE void nrf_temp_event_clear(NRF_TEMP_Type * p_reg, nrf_temp_event_t event)
188 {
189     *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0;
190     nrf_event_readback((uint8_t *)p_reg + (uint32_t)event);
191 }
192 
nrf_temp_event_check(NRF_TEMP_Type const * p_reg,nrf_temp_event_t event)193 NRF_STATIC_INLINE bool nrf_temp_event_check(NRF_TEMP_Type const * p_reg, nrf_temp_event_t event)
194 {
195     return (bool)*((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event));
196 }
197 
nrf_temp_result_get(NRF_TEMP_Type const * p_reg)198 NRF_STATIC_INLINE int32_t nrf_temp_result_get(NRF_TEMP_Type const * p_reg)
199 {
200     int32_t raw_measurement = p_reg->TEMP;
201 
202 #if defined(NRF51)
203     /* Apply workaround for the nRF51 series anomaly 28 - TEMP: Negative measured values are not represented correctly. */
204     if ((raw_measurement & 0x00000200) != 0)
205     {
206         raw_measurement |= 0xFFFFFC00UL;
207     }
208 #endif
209 
210     return raw_measurement;
211 }
212 
213 #endif // NRF_DECLARE_ONLY
214 
215 /** @} */
216 
217 #ifdef __cplusplus
218 }
219 #endif
220 
221 #endif // NRF_TEMP_H__
222