1 /***************************************************************************//**
2  * @file
3  * @brief ETAMPDET (External Tamper Detection) peripheral API
4  *******************************************************************************
5  * # License
6  * <b>Copyright 2022 Silicon Laboratories Inc. www.silabs.com</b>
7  *******************************************************************************
8  *
9  * SPDX-License-Identifier: Zlib
10  *
11  * The licensor of this software is Silicon Laboratories Inc.
12  *
13  * This software is provided 'as-is', without any express or implied
14  * warranty. In no event will the authors be held liable for any damages
15  * arising from the use of this software.
16  *
17  * Permission is granted to anyone to use this software for any purpose,
18  * including commercial applications, and to alter it and redistribute it
19  * freely, subject to the following restrictions:
20  *
21  * 1. The origin of this software must not be misrepresented; you must not
22  *    claim that you wrote the original software. If you use this software
23  *    in a product, an acknowledgment in the product documentation would be
24  *    appreciated but is not required.
25  * 2. Altered source versions must be plainly marked as such, and must not be
26  *    misrepresented as being the original software.
27  * 3. This notice may not be removed or altered from any source distribution.
28  *
29  ******************************************************************************/
30 
31 #include "sl_hal_etampdet.h"
32 #if defined(ETAMPDET_PRESENT)
33 
34 /***************************************************************************//**
35  * @addtogroup etampdet ETAMPDET - External Tamper Detect
36  * @brief External Tamper Detect (ETAMPDET) Peripheral API
37  * @details
38  *  This module contains functions to control the ETAMPDET peripheral of Silicon
39  *  Labs 32-bit MCUs and SoCs. The ETAMPDET can alert the system to unauthorized
40  *  physical access with very low current consumption.
41  * @{
42  ******************************************************************************/
43 
44 /***************************************************************************//**
45  * Initialize ETAMPDET.
46  ******************************************************************************/
sl_hal_etampdet_init(const sl_hal_etampdet_config_t * config)47 void sl_hal_etampdet_init(const sl_hal_etampdet_config_t *config)
48 {
49   // Wait for synchronization to finish
50   sl_hal_etampdet_wait_sync();
51 
52   if (ETAMPDET->EN & ETAMPDET_EN_EN) {
53     // Disable ETAMDET before initialization
54     sl_hal_etampdet_disable();
55     // Wait for disablement to finish
56     sl_hal_etampdet_wait_ready();
57   }
58 
59   // Set upper and lower clock prescaler
60   ETAMPDET->CLKPRESCVAL = ((uint32_t)config->upper_clk_presc_val << _ETAMPDET_CLKPRESCVAL_UPPERPRESC_SHIFT)
61                           | ((uint32_t)config->lower_clk_presc_val << _ETAMPDET_CLKPRESCVAL_LOWERPRESC_SHIFT);
62 }
63 
64 /***************************************************************************//**
65  * Initialize ETAMPDET channel.
66  ******************************************************************************/
sl_hal_etampdet_init_channel(const sl_hal_etampdet_config_channel_t * config_channel)67 void sl_hal_etampdet_init_channel(const sl_hal_etampdet_config_channel_t *config_channel)
68 {
69   uint32_t temp = 0;
70   // Wait for synchronization to finish
71   sl_hal_etampdet_wait_sync();
72 
73   if (ETAMPDET->EN & ETAMPDET_EN_EN) {
74     // Disable ETAMDET before initialization
75     sl_hal_etampdet_disable();
76     // Wait for disablement to finish
77     sl_hal_etampdet_wait_ready();
78   }
79 
80   // Set enable channel value
81   temp |= (uint32_t)config_channel->channel_pad_en << _ETAMPDET_CFG_CHNLPADEN0_SHIFT;
82   // Set channel tamper detect filtering value
83   temp |= (uint32_t)config_channel->channel_tampdet_filt_en << _ETAMPDET_CFG_CHNLTAMPDETFILTEN0_SHIFT;
84   // Set 1 clock delay to TX value
85   temp |= (uint32_t)config_channel->channel_cmp_dly_en << _ETAMPDET_CFG_CHNLCMPDLYEN0_SHIFT;
86   // Set channel configuration register
87   ETAMPDET->CFG_CLR = (_ETAMPDET_CFG_CHNLPADEN0_MASK
88                        | _ETAMPDET_CFG_CHNLTAMPDETFILTEN0_MASK
89                        | _ETAMPDET_CFG_CHNLCMPDLYEN0_MASK) << (config_channel->channel * _ETAMPDET_CFG_CHNLCMPDLYEN1_SHIFT);
90   ETAMPDET->CFG_SET = temp << (config_channel->channel * _ETAMPDET_CFG_CHNLCMPDLYEN1_SHIFT);
91 
92   if (config_channel->channel_tampdet_filt_en) {
93     // Set filter threshold register
94     ETAMPDET->CNTMISMATCHMAX_CLR = _ETAMPDET_CNTMISMATCHMAX_CHNLCNTMISMATCHMAX0_MASK
95                                    << (config_channel->channel * _ETAMPDET_CNTMISMATCHMAX_CHNLCNTMISMATCHMAX1_SHIFT);
96     ETAMPDET->CNTMISMATCHMAX_SET = (uint32_t)config_channel->channel_cnt_mismatch
97                                    << (config_channel->channel * _ETAMPDET_CNTMISMATCHMAX_CHNLCNTMISMATCHMAX1_SHIFT);
98 
99     // Set moving window size register
100     ETAMPDET->CHNLFILTWINSIZE_CLR = _ETAMPDET_CHNLFILTWINSIZE_CHNLFILTWINSIZE0_MASK
101                                     << (config_channel->channel * _ETAMPDET_CHNLFILTWINSIZE_CHNLFILTWINSIZE1_SHIFT);
102     ETAMPDET->CHNLFILTWINSIZE_SET = (uint32_t)config_channel->channel_filt_win_size
103                                     << (config_channel->channel * _ETAMPDET_CHNLFILTWINSIZE_CHNLFILTWINSIZE1_SHIFT);
104   }
105 
106   // Set EM4 wakeup enable value
107   ETAMPDET->EM4WUEN_CLR = _ETAMPDET_EM4WUEN_CHNLEM4WUEN0_MASK
108                           << (config_channel->channel * _ETAMPDET_EM4WUEN_CHNLEM4WUEN1_SHIFT);
109   ETAMPDET->EM4WUEN_SET = (uint32_t)config_channel->em4_wakeup_en
110                           << (config_channel->channel * _ETAMPDET_EM4WUEN_CHNLEM4WUEN1_SHIFT);
111 
112   // Set Seed value
113   if (config_channel->channel == channel_0) {
114     ETAMPDET->CHNLSEEDVAL0 = config_channel->channel_seed_val;
115   } else {
116     ETAMPDET->CHNLSEEDVAL1 = config_channel->channel_seed_val;
117   }
118 }
119 
120 /** @} (end addtogroup etampdet) */
121 #endif /* defined(ETAMPDET_PRESENT) */
122