1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_cop.h"
10 
11 /* Component ID definition, used by tools. */
12 #ifndef FSL_COMPONENT_ID
13 #define FSL_COMPONENT_ID "platform.drivers.cop"
14 #endif
15 
16 /*******************************************************************************
17  * Code
18  ******************************************************************************/
19 
20 /*!
21  * brief Initializes the COP configuration structure.
22  *
23  * This function initializes the COP configuration structure to default values. The default
24  * values are:
25  * code
26  *   copConfig->enableWindowMode = false;
27  *   copConfig->timeoutMode = kCOP_LongTimeoutMode;
28  *   copConfig->enableStop = false;
29  *   copConfig->enableDebug = false;
30  *   copConfig->clockSource = kCOP_LpoClock;
31  *   copConfig->timeoutCycles = kCOP_2Power10CyclesOr2Power18Cycles;
32  * endcode
33  *
34  * param config Pointer to the COP configuration structure.
35  * see cop_config_t
36  */
COP_GetDefaultConfig(cop_config_t * config)37 void COP_GetDefaultConfig(cop_config_t *config)
38 {
39     assert(NULL != config);
40 
41     /* Initializes the configure structure to zero. */
42     (void)memset(config, 0, sizeof(*config));
43 
44     config->enableWindowMode = false;
45 #if defined(FSL_FEATURE_COP_HAS_LONGTIME_MODE) && FSL_FEATURE_COP_HAS_LONGTIME_MODE
46     config->timeoutMode = kCOP_LongTimeoutMode;
47     config->enableStop  = false;
48     config->enableDebug = false;
49 #endif /* FSL_FEATURE_COP_HAS_LONGTIME_MODE */
50     config->clockSource   = kCOP_LpoClock;
51     config->timeoutCycles = kCOP_2Power10CyclesOr2Power18Cycles;
52 }
53 
54 /*!
55  * brief Initializes the COP module.
56  *
57  * This function configures the COP. After it is called, the COP
58  * starts running according to the configuration.
59  * Because all COP control registers are write-once only, the COP_Init function
60  * and the COP_Disable function can be called only once. A second call has no effect.
61  *
62  * Example:
63  * code
64  *  cop_config_t config;
65  *  COP_GetDefaultConfig(&config);
66  *  config.timeoutCycles = kCOP_2Power8CyclesOr2Power16Cycles;
67  *  COP_Init(sim_base,&config);
68  * endcode
69  *
70  * param base   SIM peripheral base address.
71  * param config The configuration of COP.
72  */
COP_Init(SIM_Type * base,const cop_config_t * config)73 void COP_Init(SIM_Type *base, const cop_config_t *config)
74 {
75     assert(NULL != config);
76 
77     uint32_t value = 0U;
78 
79 #if defined(FSL_FEATURE_COP_HAS_LONGTIME_MODE) && FSL_FEATURE_COP_HAS_LONGTIME_MODE
80     value = SIM_COPC_COPW(config->enableWindowMode) | SIM_COPC_COPCLKS(config->timeoutMode) |
81             SIM_COPC_COPT(config->timeoutCycles) | SIM_COPC_COPSTPEN(config->enableStop) |
82             SIM_COPC_COPDBGEN(config->enableDebug) | SIM_COPC_COPCLKSEL(config->clockSource);
83 #else
84     value = SIM_COPC_COPW(config->enableWindowMode) | SIM_COPC_COPCLKS(config->clockSource) |
85             SIM_COPC_COPT(config->timeoutCycles);
86 #endif /* FSL_FEATURE_COP_HAS_LONGTIME_MODE */
87     base->COPC = value;
88 }
89 
90 /*!
91  * brief Refreshes the COP timer
92  *
93  * This function feeds the COP.
94  *
95  * param base  SIM peripheral base address.
96  */
COP_Refresh(SIM_Type * base)97 void COP_Refresh(SIM_Type *base)
98 {
99     uint32_t primaskValue = 0U;
100 
101     /* Disable the global interrupt to protect refresh sequence */
102     primaskValue = DisableGlobalIRQ();
103     base->SRVCOP = COP_FIRST_BYTE_OF_REFRESH;
104     base->SRVCOP = COP_SECOND_BYTE_OF_REFRESH;
105     EnableGlobalIRQ(primaskValue);
106 }
107