1 /*
2  * Copyright (c) 2023 - 2024, 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 NRF_GLITCHDET_H__
35 #define NRF_GLITCHDET_H__
36 
37 #include <nrfx.h>
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /**
44  * @defgroup nrf_glitchdet_hal GLITCHDET HAL
45  * @{
46  * @ingroup nrf_glitchdet
47  * @brief   Hardware access layer for managing the Voltage Glitch Detectors (GLITCHDET) peripheral.
48  */
49 
50 /** @brief Glitch detector mode. */
51 typedef enum
52 {
53     NRF_GLITCHDET_MODE_HIGH_PASS = GLITCHDET_CONFIG_MODE_HighPassFilter, ///< High pass filter mode.
54     NRF_GLITCHDET_MODE_CAP_DIV   = GLITCHDET_CONFIG_MODE_CapDiv,         ///< Cap divider mode.
55 } nrf_glitchdet_mode_t;
56 
57 /**
58  * @brief Function for checking whether glitch detector is enabled.
59  *
60  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
61  *
62  * @retval true  Glitch detector is enabled.
63  * @retval false Glitch detector is disabled.
64  */
65 NRF_STATIC_INLINE bool nrf_glitchdet_enable_check(NRF_GLITCHDET_Type const * p_reg);
66 
67 /**
68  * @brief Function for enabling or disabling glitch detector.
69  *
70  * @param[in] p_reg  Pointer to the structure of registers of the peripheral.
71  * @param[in] enable True if glitch detector is to be enabled, false otherwise.
72  */
73 NRF_STATIC_INLINE void nrf_glitchdet_enable_set(NRF_GLITCHDET_Type * p_reg, bool enable);
74 
75 /**
76  * @brief Function for getting glitch detector mode.
77  *
78  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
79  *
80  * @return Glitch detector mode.
81  */
82 NRF_STATIC_INLINE nrf_glitchdet_mode_t nrf_glitchdet_mode_get(NRF_GLITCHDET_Type const * p_reg);
83 
84 /**
85  * @brief Function for setting glitch detector mode.
86  *
87  * @param[in] p_reg Pointer to the structure of registers of the peripheral.
88  * @param[in] mode  Glitch detector mode to be set.
89  */
90 NRF_STATIC_INLINE void nrf_glitchdet_mode_set(NRF_GLITCHDET_Type * p_reg,
91                                               nrf_glitchdet_mode_t mode);
92 
93 #ifndef NRF_DECLARE_ONLY
94 
nrf_glitchdet_enable_check(NRF_GLITCHDET_Type const * p_reg)95 NRF_STATIC_INLINE bool nrf_glitchdet_enable_check(NRF_GLITCHDET_Type const * p_reg)
96 {
97     return ((p_reg->CONFIG & GLITCHDET_CONFIG_ENABLE_Msk)
98             >> GLITCHDET_CONFIG_ENABLE_Pos) == GLITCHDET_CONFIG_ENABLE_Enable;
99 }
100 
nrf_glitchdet_enable_set(NRF_GLITCHDET_Type * p_reg,bool enable)101 NRF_STATIC_INLINE void nrf_glitchdet_enable_set(NRF_GLITCHDET_Type * p_reg, bool enable)
102 {
103     p_reg->CONFIG = (p_reg->CONFIG & ~GLITCHDET_CONFIG_ENABLE_Msk) |
104                     ((enable ? GLITCHDET_CONFIG_ENABLE_Enable : GLITCHDET_CONFIG_ENABLE_Disable) <<
105                      GLITCHDET_CONFIG_ENABLE_Pos);
106 }
107 
nrf_glitchdet_mode_get(NRF_GLITCHDET_Type const * p_reg)108 NRF_STATIC_INLINE nrf_glitchdet_mode_t nrf_glitchdet_mode_get(NRF_GLITCHDET_Type const * p_reg)
109 {
110     return (nrf_glitchdet_mode_t)((p_reg->CONFIG & GLITCHDET_CONFIG_MODE_Msk) >>
111                                   GLITCHDET_CONFIG_MODE_Pos);
112 }
113 
nrf_glitchdet_mode_set(NRF_GLITCHDET_Type * p_reg,nrf_glitchdet_mode_t mode)114 NRF_STATIC_INLINE void nrf_glitchdet_mode_set(NRF_GLITCHDET_Type * p_reg,
115                                               nrf_glitchdet_mode_t mode)
116 {
117     p_reg->CONFIG = (p_reg->CONFIG & ~GLITCHDET_CONFIG_MODE_Msk) |
118                     (mode << GLITCHDET_CONFIG_MODE_Pos);
119 }
120 #endif // NRF_DECLARE_ONLY
121 
122 /** @} */
123 
124 #ifdef __cplusplus
125 }
126 #endif
127 
128 #endif // NRF_GLITCHDET_H__
129