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 #include <nrfx.h>
35 
36 #if NRFX_CHECK(NRFX_TEMP_ENABLED)
37 
38 #include <nrfx_temp.h>
39 
40 #if !defined(USE_WORKAROUND_FOR_TEMP_OFFSET_ANOMALY) && defined(NRF51)
41 // Enable workaround for nRF51 series anomaly 28
42 // (TEMP: Temperature offset value has to be manually loaded to the TEMP module).
43 #define USE_WORKAROUND_FOR_TEMP_OFFSET_ANOMALY 1
44 #endif
45 
46 /** @brief Time of one check attempt.*/
47 #define NRFX_TEMP_TIME_US 4
48 
49 /** @brief Maximum attempts to check whether conversion passed.*/
50 #define NRFX_TEMP_ATTEMPTS 100
51 
52 /** @brief Internal state of TEMP driver. */
53 static nrfx_drv_state_t m_temp_state = NRFX_DRV_STATE_UNINITIALIZED;
54 
55 /** @brief Pointer to handler to be called from interrupt routine. */
56 static nrfx_temp_data_handler_t m_data_handler;
57 
nrfx_temp_init(nrfx_temp_config_t const * p_config,nrfx_temp_data_handler_t handler)58 nrfx_err_t nrfx_temp_init(nrfx_temp_config_t const * p_config, nrfx_temp_data_handler_t handler)
59 {
60     NRFX_ASSERT(p_config);
61 
62     if (m_temp_state != NRFX_DRV_STATE_UNINITIALIZED)
63     {
64         return NRFX_ERROR_ALREADY;
65     }
66 
67 #if NRFX_CHECK(USE_WORKAROUND_FOR_TEMP_OFFSET_ANOMALY)
68     *(uint32_t volatile *)0x4000C504 = 0;
69 #endif
70 
71 #if NRFY_TEMP_HAS_CALIBRATION && defined(FICR_TRIM_GLOBAL_TEMP_CALIB_VALUE_Msk)
72     nrfy_temp_calibration_coeff_set(NRF_TEMP, NRF_FICR->TRIM.GLOBAL.TEMP.CALIB);
73 #endif
74 
75     m_data_handler = handler;
76 
77     if (m_data_handler)
78     {
79         nrfy_temp_int_init(NRF_TEMP, 0, p_config->interrupt_priority, true);
80     }
81 
82     m_temp_state = NRFX_DRV_STATE_INITIALIZED;
83     return NRFX_SUCCESS;
84 }
85 
nrfx_temp_uninit(void)86 void nrfx_temp_uninit(void)
87 {
88     NRFX_ASSERT(m_temp_state == NRFX_DRV_STATE_INITIALIZED);
89 
90     nrfy_temp_task_trigger(NRF_TEMP, NRF_TEMP_TASK_STOP);
91     if (m_data_handler)
92     {
93         nrfy_temp_int_disable(NRF_TEMP, NRF_TEMP_INT_DATARDY_MASK);
94         nrfy_temp_int_uninit(NRF_TEMP);
95     }
96 
97     m_temp_state = NRFX_DRV_STATE_UNINITIALIZED;
98 }
99 
nrfx_temp_init_check(void)100 bool nrfx_temp_init_check(void)
101 {
102     return (m_temp_state != NRFX_DRV_STATE_UNINITIALIZED);
103 }
104 
nrfx_temp_calculate(int32_t raw_measurement)105 int32_t nrfx_temp_calculate(int32_t raw_measurement)
106 {
107     /* Raw temperature is a 2's complement signed value. Moreover, it is represented
108      * by 0.25[C] intervals, so division by 4 is needed. To preserve
109      * fractional part, raw value is multiplied by 100 before division.*/
110 
111     return (raw_measurement * 100) / 4;
112 }
113 
nrfx_temp_measure(void)114 nrfx_err_t nrfx_temp_measure(void)
115 {
116     NRFX_ASSERT(m_temp_state == NRFX_DRV_STATE_INITIALIZED);
117 
118     nrfx_err_t result = NRFX_SUCCESS;
119 
120     nrfy_temp_event_clear(NRF_TEMP, NRF_TEMP_EVENT_DATARDY);
121     nrfy_temp_task_trigger(NRF_TEMP, NRF_TEMP_TASK_START);
122 
123     if (!m_data_handler)
124     {
125         bool ev_result;
126         NRFX_WAIT_FOR(nrfy_temp_event_check(NRF_TEMP, NRF_TEMP_EVENT_DATARDY),
127                       NRFX_TEMP_ATTEMPTS,
128                       NRFX_TEMP_TIME_US,
129                       ev_result);
130         if (!ev_result)
131         {
132             result = NRFX_ERROR_INTERNAL;
133         }
134         else
135         {
136             nrfy_temp_event_clear(NRF_TEMP, NRF_TEMP_EVENT_DATARDY);
137         }
138         nrfy_temp_task_trigger(NRF_TEMP, NRF_TEMP_TASK_STOP);
139     }
140 
141     return result;
142 }
143 
nrfx_temp_irq_handler(void)144 void nrfx_temp_irq_handler(void)
145 {
146     NRFX_ASSERT(m_data_handler);
147 
148     nrf_temp_task_trigger(NRF_TEMP, NRF_TEMP_TASK_STOP);
149     nrf_temp_event_clear(NRF_TEMP, NRF_TEMP_EVENT_DATARDY);
150 
151     int32_t raw_temp = nrfx_temp_result_get();
152     m_data_handler(raw_temp);
153 }
154 
155 #endif // NRFX_CHECK(NRFX_TEMP_ENABLED)
156