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 NRF_OSCILLATORS_H__
33 #define NRF_OSCILLATORS_H__
34 
35 #include <nrfx.h>
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
41 /**
42  * @defgroup nrf_oscillators_hal OSCILLATORS HAL
43  * @{
44  * @ingroup nrf_clock
45  * @brief   Hardware access layer for managing the OSCILLATORS peripheral.
46  */
47 
48 /** @brief Capacitors configuration for LFXO. */
49 typedef enum
50 {
51     NRF_OSCILLATORS_LFXO_CAP_EXTERNAL = OSCILLATORS_XOSC32KI_INTCAP_INTCAP_External, ///< Use external capacitors.
52     NRF_OSCILLATORS_LFXO_CAP_6PF      = OSCILLATORS_XOSC32KI_INTCAP_INTCAP_C6PF,     ///< Use 6 pF internal capacitors.
53     NRF_OSCILLATORS_LFXO_CAP_7PF      = OSCILLATORS_XOSC32KI_INTCAP_INTCAP_C7PF,     ///< Use 7 pF internal capacitors.
54     NRF_OSCILLATORS_LFXO_CAP_11PF     = OSCILLATORS_XOSC32KI_INTCAP_INTCAP_C11PF,    ///< Use 11 pF internal capacitors.
55 } nrf_oscillators_lfxo_cap_t;
56 
57 /**
58  * @brief Function for enabling or disabling the bypass of LFXO with external clock source.
59  *
60  * @param[in] p_reg  Pointer to the structure of registers of the peripheral.
61  * @param[in] enable True if bypass is to be enabled (use with rail-to-rail external source).
62  *                   False if bypass is to be disabled (use with xtal or low-swing external source).
63  */
64 NRF_STATIC_INLINE void nrf_oscillators_lfxo_bypass_set(NRF_OSCILLATORS_Type * p_reg, bool enable);
65 
66 /**
67  * @brief Function for configuring the internal capacitors of LFXO.
68  *
69  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
70  * @param[in] cap   Capacitors configuration.
71  */
72 NRF_STATIC_INLINE void nrf_oscillators_lfxo_cap_set(NRF_OSCILLATORS_Type *     p_reg,
73                                                     nrf_oscillators_lfxo_cap_t cap);
74 
75 /**
76  * @brief Function for configuring the internal capacitors of HXFO.
77  *
78  * The capacitance of internal capacitors ranges from 7 pF to 20 pF in 0.5 pF steps.
79  * To calculate the correct @p cap_value, use the following equation:
80  * CAPVALUE = (1+FICR->XOSC32MTRIM.SLOPE/16) * (CAPACITANCE*2-14) + FICR->XOSC32MTRIM.OFFSET
81  *
82  * @param[in] p_reg     Pointer to the structure of registers of the peripheral.
83  * @param[in] enable    True if internal capacitors are to be enabled, false otherwise.
84  * @param[in] cap_value Value representing capacitance, calculated using provided equation.
85  *                      Ignored when internal capacitors are disabled.
86  */
87 NRF_STATIC_INLINE void nrf_oscillators_hfxo_cap_set(NRF_OSCILLATORS_Type * p_reg,
88                                                     bool                   enable,
89                                                     uint32_t               cap_value);
90 
91 #ifndef NRF_DECLARE_ONLY
nrf_oscillators_lfxo_bypass_set(NRF_OSCILLATORS_Type * p_reg,bool enable)92 NRF_STATIC_INLINE void nrf_oscillators_lfxo_bypass_set(NRF_OSCILLATORS_Type * p_reg, bool enable)
93 {
94     p_reg->XOSC32KI.BYPASS = (enable ? OSCILLATORS_XOSC32KI_BYPASS_BYPASS_Enabled :
95                                        OSCILLATORS_XOSC32KI_BYPASS_BYPASS_Disabled);
96 }
97 
nrf_oscillators_lfxo_cap_set(NRF_OSCILLATORS_Type * p_reg,nrf_oscillators_lfxo_cap_t cap)98 NRF_STATIC_INLINE void nrf_oscillators_lfxo_cap_set(NRF_OSCILLATORS_Type *     p_reg,
99                                                     nrf_oscillators_lfxo_cap_t cap)
100 {
101     p_reg->XOSC32KI.INTCAP = (uint32_t)cap;
102 }
103 
nrf_oscillators_hfxo_cap_set(NRF_OSCILLATORS_Type * p_reg,bool enable,uint32_t cap_value)104 NRF_STATIC_INLINE void nrf_oscillators_hfxo_cap_set(NRF_OSCILLATORS_Type * p_reg,
105                                                     bool                   enable,
106                                                     uint32_t               cap_value)
107 {
108     p_reg->XOSC32MCAPS =
109         (enable ? ((OSCILLATORS_XOSC32MCAPS_ENABLE_Enabled << OSCILLATORS_XOSC32MCAPS_ENABLE_Pos) |
110                    (cap_value << OSCILLATORS_XOSC32MCAPS_CAPVALUE_Pos))
111                 : (OSCILLATORS_XOSC32MCAPS_ENABLE_Disabled << OSCILLATORS_XOSC32MCAPS_ENABLE_Pos));
112 }
113 #endif // NRF_DECLARE_ONLY
114 
115 /** @} */
116 
117 #ifdef __cplusplus
118 }
119 #endif
120 
121 #endif // NRF_OSCILLATORS_H__
122