1 /*
2  * Copyright (c) 2019 - 2025, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  *    list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  *    contributors may be used to endorse or promote products derived from this
19  *    software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef NRFX_TEMP_H__
35 #define NRFX_TEMP_H__
36 
37 #include <nrfx.h>
38 #include <haly/nrfy_temp.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 /**
45  * @defgroup nrfx_temp TEMP driver
46  * @{
47  * @ingroup nrf_temp
48  * @brief   Temperature sensor (TEMP) driver.
49  */
50 
51 /** @brief Structure for TEMP configuration. */
52 typedef struct
53 {
54     uint8_t interrupt_priority;    /**< Interrupt priority. */
55 } nrfx_temp_config_t;
56 
57 /** @brief TEMP default configuration. */
58 #define NRFX_TEMP_DEFAULT_CONFIG                                        \
59     {                                                                   \
60         .interrupt_priority = NRFX_TEMP_DEFAULT_CONFIG_IRQ_PRIORITY,    \
61     }
62 
63 /**
64  * @brief TEMP driver data ready handler type.
65  *
66  * @param temperature  Raw temperature in a 2's complement signed value
67  *                     representation. This value can be converted to Celsius
68  *                     scale using the @ref nrfx_temp_calculate() function.
69  */
70 typedef void (* nrfx_temp_data_handler_t)(int32_t temperature);
71 
72 /**
73  * @brief Function for initializing the TEMP driver.
74  *
75  * @param[in] p_config  Pointer to the structure with initial configuration.
76  * @param[in] handler   Data handler provided by the user. If not provided,
77  *                      the driver is initialized in blocking mode.
78  *
79  * @retval NRFX_SUCCESS       Driver was successfully initialized.
80  * @retval NRFX_ERROR_ALREADY Driver was already initialized.
81  */
82 nrfx_err_t nrfx_temp_init(nrfx_temp_config_t const * p_config, nrfx_temp_data_handler_t handler);
83 
84 /** @brief Function for uninitializing the TEMP driver. */
85 void nrfx_temp_uninit(void);
86 
87 /**
88  * @brief Function for checking if the TEMP driver is initialized.
89  *
90  * @retval true  Driver is already initialized.
91  * @retval false Driver is not initialized.
92  */
93 bool nrfx_temp_init_check(void);
94 
95 /**
96  * @brief Function for getting the temperature measurement in a 2's complement
97  *        signed value representation.
98  *
99  * This function returns the last value prepared by the TEMP peripheral.
100  * In blocking mode, it should be used after calling the @ref nrfx_temp_measure()
101  * function. In non-blocking mode, it is called internally by the driver,
102  * and the value it returns is passed to the data handler.
103  *
104  * @return Temperature measurement result in a 2's complement signed value
105  *         representation.
106  */
107 NRFX_STATIC_INLINE int32_t nrfx_temp_result_get(void);
108 
109 /**
110  * @brief Function for calculating the temperature value in Celsius scale from raw data.
111  *
112  * The returned temperature value is in Celsius scale, multiplied by 100
113  * For example, the actual temperature of 25.75[C] will be returned as a 2575 signed integer.
114  * Measurement accuracy is 0.25[C].
115  *
116  * @param[in] raw_measurement Temperature value in a 2's complement signed
117  *                            value representation.
118  *
119  * @return Temperature measurement result.
120  */
121 int32_t nrfx_temp_calculate(int32_t raw_measurement);
122 
123 /**
124  * @brief Function for starting the temperature measurement.
125  *
126  * Non-blocking mode:
127  * This function returns immediately. After a measurement, the handler specified
128  * during initialization is called, with measurement result as the parameter.
129  *
130  * Blocking mode:
131  * This function waits until the measurement is finished. The value should be read
132  * using the @ref nrfx_temp_result_get() function.
133  *
134  * @retval NRFX_SUCCESS        In non-blocking mode: Measurement was started.
135  *                             An interrupt will be generated soon. <br>
136  *                             In blocking mode:
137  *                             Measurement was started and finished. Data can
138  *                             be read using the @ref nrfx_temp_result_get() function.
139  * @retval NRFX_ERROR_INTERNAL In non-blocking mode:
140  *                             Not applicable. <br>
141  *                             In blocking mode:
142  *                             Measurement data ready event did not occur.
143  */
144 nrfx_err_t nrfx_temp_measure(void);
145 
146 #ifndef NRFX_DECLARE_ONLY
nrfx_temp_result_get(void)147 NRFX_STATIC_INLINE int32_t nrfx_temp_result_get(void)
148 {
149     return nrfy_temp_result_get(NRF_TEMP);
150 }
151 #endif // NRFX_DECLARE_ONLY
152 
153 /** @} */
154 
155 void nrfx_temp_irq_handler(void);
156 
157 #ifdef __cplusplus
158 }
159 #endif
160 
161 #endif // NRFX_TEMP_H__
162