1 /*
2  * Copyright (c) 2019 - 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 NRFX_TEMP_H__
33 #define NRFX_TEMP_H__
34 
35 #include <nrfx.h>
36 #include <hal/nrf_temp.h>
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 /**
43  * @defgroup nrfx_temp TEMP driver
44  * @{
45  * @ingroup nrf_temp
46  * @brief   Temperature sensor (TEMP) driver.
47  */
48 
49 /** @brief Structure for TEMP configuration. */
50 typedef struct
51 {
52     uint8_t interrupt_priority;    /**< Interrupt priority. */
53 } nrfx_temp_config_t;
54 
55 /** @brief TEMP default configuration. */
56 #define NRFX_TEMP_DEFAULT_CONFIG                                        \
57     {                                                                   \
58         .interrupt_priority = NRFX_TEMP_DEFAULT_CONFIG_IRQ_PRIORITY,    \
59     }
60 
61 /**
62  * @brief TEMP driver data ready handler type.
63  *
64  * @param temperature  Raw temperature in a 2's complement signed value
65  *                     representation. This value can be converted to Celsius
66  *                     scale using the @ref nrfx_temp_calculate() function.
67  */
68 typedef void (* nrfx_temp_data_handler_t)(int32_t raw_temperature);
69 
70 /**
71  * @brief Function for initializing the TEMP driver.
72  *
73  * @param[in] p_config  Pointer to the structure with initial configuration.
74  * @param[in] handler   Data handler provided by the user. If not provided,
75  *                      the driver is initialized in blocking mode.
76  *
77  * @retval NRFX_SUCCESS                    Driver was successfully initialized.
78  * @retval NRFX_ERROR_ALREADY_INITIALIZED  Driver was already initialized.
79  */
80 nrfx_err_t nrfx_temp_init(nrfx_temp_config_t const * p_config, nrfx_temp_data_handler_t handler);
81 
82 /** @brief Function for uninitializing the TEMP driver. */
83 void nrfx_temp_uninit(void);
84 
85 /**
86  * @brief Function for getting the temperature measurement in a 2's complement
87  *        signed value representation.
88  *
89  * This function returns the last value prepared by the TEMP peripheral.
90  * In blocking mode, it should be used after calling the @ref nrfx_temp_measure()
91  * function. In non-blocking mode, it is called internally by the driver,
92  * and the value it returns is passed to the data handler.
93  *
94  * @return Temperature measurement result in a 2's complement signed value
95  *         representation.
96  */
97 NRFX_STATIC_INLINE int32_t nrfx_temp_result_get(void);
98 
99 /**
100  * @brief Function for calculating the temperature value in Celsius scale from raw data.
101  *
102  * The returned temperature value is in Celsius scale, multiplied by 100
103  * For example, the actual temperature of 25.75[C] will be returned as a 2575 signed integer.
104  * Measurement accuracy is 0.25[C].
105  *
106  * @param[in] raw_measurement Temperature value in a 2's complement signed
107  *                            value representation.
108  *
109  * @return Temperature measurement result.
110  */
111 int32_t nrfx_temp_calculate(int32_t raw_measurement);
112 
113 /**
114  * @brief Function for starting the temperature measurement.
115  *
116  * Non-blocking mode:
117  * This function returns immediately. After a measurement, the handler specified
118  * during initialization is called, with measurement result as the parameter.
119  *
120  * Blocking mode:
121  * This function waits until the measurement is finished. The value should be read
122  * using the @ref nrfx_temp_result_get() function.
123  *
124  * @retval NRFX_SUCCESS        In non-blocking mode: Measurement was started.
125  *                             An interrupt will be generated soon. <br>
126  *                             In blocking mode:
127  *                             Measurement was started and finished. Data can
128  *                             be read using the @ref nrfx_temp_result_get() function.
129  * @retval NRFX_ERROR_INTERNAL In non-blocking mode:
130  *                             Not applicable. <br>
131  *                             In blocking mode:
132  *                             Measurement data ready event did not occur.
133  */
134 nrfx_err_t nrfx_temp_measure(void);
135 
136 #ifndef NRFX_DECLARE_ONLY
nrfx_temp_result_get(void)137 NRFX_STATIC_INLINE int32_t nrfx_temp_result_get(void)
138 {
139     return nrf_temp_result_get(NRF_TEMP);
140 }
141 #endif // NRFX_DECLARE_ONLY
142 
143 /** @} */
144 
145 void nrfx_temp_irq_handler(void);
146 
147 #ifdef __cplusplus
148 }
149 #endif
150 
151 #endif // NRFX_TEMP_H__
152