1 /*
2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
3 * Copyright 2016-2017 NXP
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * o Redistributions of source code must retain the above copyright notice, this list
9 * of conditions and the following disclaimer.
10 *
11 * o Redistributions in binary form must reproduce the above copyright notice, this
12 * list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
14 *
15 * o 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" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "fsl_ewm.h"
32
33 /*******************************************************************************
34 * Code
35 ******************************************************************************/
36
EWM_Init(EWM_Type * base,const ewm_config_t * config)37 void EWM_Init(EWM_Type *base, const ewm_config_t *config)
38 {
39 assert(config);
40
41 uint32_t value = 0U;
42
43 #if !((defined(FSL_FEATURE_SOC_PCC_COUNT) && FSL_FEATURE_SOC_PCC_COUNT) && \
44 (defined(FSL_FEATURE_PCC_SUPPORT_EWM_CLOCK_REMOVE) && FSL_FEATURE_PCC_SUPPORT_EWM_CLOCK_REMOVE))
45 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
46 CLOCK_EnableClock(kCLOCK_Ewm0);
47 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
48 #endif
49 value = EWM_CTRL_EWMEN(config->enableEwm) | EWM_CTRL_ASSIN(config->setInputAssertLogic) |
50 EWM_CTRL_INEN(config->enableEwmInput) | EWM_CTRL_INTEN(config->enableInterrupt);
51 #if defined(FSL_FEATURE_EWM_HAS_PRESCALER) && FSL_FEATURE_EWM_HAS_PRESCALER
52 base->CLKPRESCALER = config->prescaler;
53 #endif /* FSL_FEATURE_EWM_HAS_PRESCALER */
54
55 #if defined(FSL_FEATURE_EWM_HAS_CLOCK_SELECT) && FSL_FEATURE_EWM_HAS_CLOCK_SELECT
56 base->CLKCTRL = config->clockSource;
57 #endif /* FSL_FEATURE_EWM_HAS_CLOCK_SELECT*/
58
59 base->CMPL = config->compareLowValue;
60 base->CMPH = config->compareHighValue;
61 base->CTRL = value;
62 }
63
EWM_Deinit(EWM_Type * base)64 void EWM_Deinit(EWM_Type *base)
65 {
66 EWM_DisableInterrupts(base, kEWM_InterruptEnable);
67 #if !((defined(FSL_FEATURE_SOC_PCC_COUNT) && FSL_FEATURE_SOC_PCC_COUNT) && \
68 (defined(FSL_FEATURE_PCC_SUPPORT_EWM_CLOCK_REMOVE) && FSL_FEATURE_PCC_SUPPORT_EWM_CLOCK_REMOVE))
69 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
70 CLOCK_DisableClock(kCLOCK_Ewm0);
71 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
72 #endif /* FSL_FEATURE_PCC_SUPPORT_EWM_CLOCK_REMOVE */
73 }
74
EWM_GetDefaultConfig(ewm_config_t * config)75 void EWM_GetDefaultConfig(ewm_config_t *config)
76 {
77 assert(config);
78
79 config->enableEwm = true;
80 config->enableEwmInput = false;
81 config->setInputAssertLogic = false;
82 config->enableInterrupt = false;
83 #if defined(FSL_FEATURE_EWM_HAS_CLOCK_SELECT) && FSL_FEATURE_EWM_HAS_CLOCK_SELECT
84 config->clockSource = kEWM_LpoClockSource0;
85 #endif /* FSL_FEATURE_EWM_HAS_CLOCK_SELECT*/
86 #if defined(FSL_FEATURE_EWM_HAS_PRESCALER) && FSL_FEATURE_EWM_HAS_PRESCALER
87 config->prescaler = 0U;
88 #endif /* FSL_FEATURE_EWM_HAS_PRESCALER */
89 config->compareLowValue = 0U;
90 config->compareHighValue = 0xFEU;
91 }
92
EWM_Refresh(EWM_Type * base)93 void EWM_Refresh(EWM_Type *base)
94 {
95 uint32_t primaskValue = 0U;
96
97 /* Disable the global interrupt to protect refresh sequence */
98 primaskValue = DisableGlobalIRQ();
99 base->SERV = (uint8_t)0xB4U;
100 base->SERV = (uint8_t)0x2CU;
101 EnableGlobalIRQ(primaskValue);
102 }
103