Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:22:59

0001 /*
0002  * Copyright (c) 2015, Freescale Semiconductor, Inc.
0003  * Copyright 2016-2017, 2020 NXP
0004  * All rights reserved.
0005  *
0006  * SPDX-License-Identifier: BSD-3-Clause
0007  */
0008 
0009 #include "fsl_ewm.h"
0010 
0011 /* Component ID definition, used by tools. */
0012 #ifndef FSL_COMPONENT_ID
0013 #define FSL_COMPONENT_ID "platform.drivers.ewm"
0014 #endif
0015 
0016 /*******************************************************************************
0017  * Code
0018  ******************************************************************************/
0019 
0020 /*!
0021  * brief Initializes the EWM peripheral.
0022  *
0023  * This function is used to initialize the EWM. After calling, the EWM
0024  * runs immediately according to the configuration.
0025  * Note that, except for the interrupt enable control bit, other control bits and registers are write once after a
0026  * CPU reset. Modifying them more than once generates a bus transfer error.
0027  *
0028  * This is an example.
0029  * code
0030  *   ewm_config_t config;
0031  *   EWM_GetDefaultConfig(&config);
0032  *   config.compareHighValue = 0xAAU;
0033  *   EWM_Init(ewm_base,&config);
0034  * endcode
0035  *
0036  * param base EWM peripheral base address
0037  * param config The configuration of the EWM
0038  */
0039 void EWM_Init(EWM_Type *base, const ewm_config_t *config)
0040 {
0041     assert(NULL != config);
0042 
0043     uint8_t value = 0U;
0044 
0045 #if !((defined(FSL_FEATURE_SOC_PCC_COUNT) && FSL_FEATURE_SOC_PCC_COUNT) && \
0046       (defined(FSL_FEATURE_PCC_SUPPORT_EWM_CLOCK_REMOVE) && FSL_FEATURE_PCC_SUPPORT_EWM_CLOCK_REMOVE))
0047 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
0048     CLOCK_EnableClock(kCLOCK_Ewm0);
0049 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
0050 #endif
0051     value = EWM_CTRL_EWMEN(config->enableEwm) | EWM_CTRL_ASSIN(config->setInputAssertLogic) |
0052             EWM_CTRL_INEN(config->enableEwmInput) | EWM_CTRL_INTEN(config->enableInterrupt);
0053 #if defined(FSL_FEATURE_EWM_HAS_PRESCALER) && FSL_FEATURE_EWM_HAS_PRESCALER
0054     base->CLKPRESCALER = config->prescaler;
0055 #endif /* FSL_FEATURE_EWM_HAS_PRESCALER */
0056 
0057 #if defined(FSL_FEATURE_EWM_HAS_CLOCK_SELECT) && FSL_FEATURE_EWM_HAS_CLOCK_SELECT
0058     base->CLKCTRL = (uint8_t)config->clockSource;
0059 #endif /* FSL_FEATURE_EWM_HAS_CLOCK_SELECT*/
0060 
0061     base->CMPL = config->compareLowValue;
0062     base->CMPH = config->compareHighValue;
0063     base->CTRL = value;
0064 }
0065 
0066 /*!
0067  * brief Deinitializes the EWM peripheral.
0068  *
0069  * This function is used to shut down the EWM.
0070  *
0071  * param base EWM peripheral base address
0072  */
0073 void EWM_Deinit(EWM_Type *base)
0074 {
0075     EWM_DisableInterrupts(base, (uint32_t)kEWM_InterruptEnable);
0076 #if !((defined(FSL_FEATURE_SOC_PCC_COUNT) && FSL_FEATURE_SOC_PCC_COUNT) && \
0077       (defined(FSL_FEATURE_PCC_SUPPORT_EWM_CLOCK_REMOVE) && FSL_FEATURE_PCC_SUPPORT_EWM_CLOCK_REMOVE))
0078 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
0079     CLOCK_DisableClock(kCLOCK_Ewm0);
0080 #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
0081 #endif /* FSL_FEATURE_PCC_SUPPORT_EWM_CLOCK_REMOVE */
0082 }
0083 
0084 /*!
0085  * brief Initializes the EWM configuration structure.
0086  *
0087  * This function initializes the EWM configuration structure to default values. The default
0088  * values are as follows.
0089  * code
0090  *   ewmConfig->enableEwm = true;
0091  *   ewmConfig->enableEwmInput = false;
0092  *   ewmConfig->setInputAssertLogic = false;
0093  *   ewmConfig->enableInterrupt = false;
0094  *   ewmConfig->ewm_lpo_clock_source_t = kEWM_LpoClockSource0;
0095  *   ewmConfig->prescaler = 0;
0096  *   ewmConfig->compareLowValue = 0;
0097  *   ewmConfig->compareHighValue = 0xFEU;
0098  * endcode
0099  *
0100  * param config Pointer to the EWM configuration structure.
0101  * see ewm_config_t
0102  */
0103 void EWM_GetDefaultConfig(ewm_config_t *config)
0104 {
0105     assert(NULL != config);
0106 
0107     /* Initializes the configure structure to zero. */
0108     (void)memset(config, 0, sizeof(*config));
0109 
0110     config->enableEwm           = true;
0111     config->enableEwmInput      = false;
0112     config->setInputAssertLogic = false;
0113     config->enableInterrupt     = false;
0114 #if defined(FSL_FEATURE_EWM_HAS_CLOCK_SELECT) && FSL_FEATURE_EWM_HAS_CLOCK_SELECT
0115     config->clockSource = kEWM_LpoClockSource0;
0116 #endif /* FSL_FEATURE_EWM_HAS_CLOCK_SELECT*/
0117 #if defined(FSL_FEATURE_EWM_HAS_PRESCALER) && FSL_FEATURE_EWM_HAS_PRESCALER
0118     config->prescaler = 0U;
0119 #endif /* FSL_FEATURE_EWM_HAS_PRESCALER */
0120     config->compareLowValue  = 0U;
0121     config->compareHighValue = 0xFEU;
0122 }
0123 
0124 /*!
0125  * brief Services the EWM.
0126  *
0127  * This function resets the EWM counter to zero.
0128  *
0129  * param base EWM peripheral base address
0130  */
0131 void EWM_Refresh(EWM_Type *base)
0132 {
0133     uint32_t primaskValue = 0U;
0134 
0135     /* Disable the global interrupt to protect refresh sequence */
0136     primaskValue = DisableGlobalIRQ();
0137     base->SERV   = (uint8_t)0xB4U;
0138     base->SERV   = (uint8_t)0x2CU;
0139     EnableGlobalIRQ(primaskValue);
0140 }