<commit_before>// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*******************************************************************************
 * NOTICE
 * The hal is not public api, don't use in application code.
 * See readme.md in soc/include/hal/readme.md
 ******************************************************************************/

// The LL layer for ESP32 MCPWM register operations

#pragma once

#include <soc/mcpwm_periph.h>
#include "soc/mcpwm_periph.h"
#include "hal/mcpwm_types.h"
#include "soc/mcpwm_caps.h"
#include "hal/hal_defs.h"

#include "esp_types.h"

#ifdef __cplusplus
extern "C" {
#endif

/// Get the address of peripheral registers
#define MCPWM_LL_GET_HW(ID) (((ID)==0)? &MCPWM0: &MCPWM1)


/********************* Global *******************/
/**
 * Initialize common registers.
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 */
static inline void mcpwm_ll_init(mcpwm_dev_t *mcpwm)
{
    mcpwm->update_cfg.global_up_en = 1;
    mcpwm->update_cfg.global_force_up = 1;
    mcpwm->update_cfg.global_force_up = 0;
}

/**
 * Set the prescale of the PWM main clock to the input clock.
 *
 * Input clock is 160MHz, PWM main clock cycle = 6.25ns*(prescale + 1).
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 * @param prescale Prescale factor, 0-255.
 */
static inline void mcpwm_ll_set_clock_prescale(mcpwm_dev_t *mcpwm, int prescale)
{
    mcpwm->clk_cfg.prescale = prescale;
}

STATIC_HAL_REG_CHECK(MCPWM, MCPWM_LL_INTR_CAP0, MCPWM_CAP0_INT_RAW);
STATIC_HAL_REG_CHECK(MCPWM, MCPWM_LL_INTR_CAP1, MCPWM_CAP1_INT_RAW);
STATIC_HAL_REG_CHECK(MCPWM, MCPWM_LL_INTR_CAP2, MCPWM_CAP2_INT_RAW);

/**
 * Get raw interrupt status.
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 * @return The triggered interrupts, ORed by active interrupts.
 */
static inline mcpwm_intr_t mcpwm_ll_get_intr(mcpwm_dev_t *mcpwm)
{
    return mcpwm->int_raw.val;
}

/**
 * Clear the interrupts.
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 * @param intr Bitwise ORed interrupts to clear.
 */
static inline void mcpwm_ll_clear_intr(mcpwm_dev_t* mcpwm, mcpwm_intr_t intr)
{
    mcpwm->int_clr.val = intr;
}

/********************* Timer *******************/
/**
 * Set the prescale of the Timer_x clock to the PWM main clock.
 *
 * Timer clock frequency = PWM main clock frequency/(prescale + 1).
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 * @param timer The timer to set the prescale, 0-2.
 * @param prescale Prescale factor, 0-255.
 */
static inline void mcpwm_ll_timer_set_prescale(mcpwm_dev_t* mcpwm, int timer, uint32_t prescale)
{
    mcpwm->timer[timer].period.prescale = prescale;
}


STATIC_HAL_REG_CHECK(MCPWM, MCPWM_UP_COUNTER,        1);
STATIC_HAL_REG_CHECK(MCPWM, MCPWM_DOWN_COUNTER,      2);
STATIC_HAL_REG_CHECK(MCPWM, MCPWM_UP_DOWN_COUNTER,   3);

/**
 * Set the counting mode for the PWM timer.
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 * @param timer The timer to change counting mode, 0-2.
 * @param mode Counting mode to use.
 */
static inline void mcpwm_ll_timer_set_count_mode(mcpwm_dev_t *mcpwm, int timer, mcpwm_counter_type_t mode)
{
    mcpwm->timer[timer].mode.mode = mode;
}

/**
 * Start a timer.
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 * @param timer The timer to start, 0-2.
 */
static inline void mcpwm_ll_timer_start(mcpwm_dev_t *mcpwm, int timer)
{
    mcpwm->timer[timer].mode.start = 2;
}

/**
 * Stop a timer.
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 * @param timer The timer to stop, 0-2.
 */
static inline void mcpwm_ll_timer_stop(mcpwm_dev_t *mcpwm, int timer)
{
    mcpwm->timer[timer].mode.start = 0;
}

/**
 * Set the overflow period of a timer.
 *
 * The overflow rate will be Frequency of timer / period.
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 * @param timer The timer to set period, 0-2.
 * @param period Total timer count of each period, 0-65535.
 */
static inline void mcpwm_ll_timer_set_period(mcpwm_dev_t *mcpwm, int timer, uint32_t period)
{

    mcpwm->timer[timer].period.period = period;
    mcpwm->timer[timer].period.upmethod = 0;
}

/**
 * Get the period setting of a timer.
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 * @param timer The timer to get the period, 0-2.
 * @return Period setting value.
 */
static inline uint32_t mcpwm_ll_timer_get_period(mcpwm_dev_t *mcpwm, int timer)
{
    return mcpwm->timer[timer].period.period;
}

/********************* Sync *******************/
/**
 * Enable the synchronization feature for a timer.
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 * @param timer Timer to set, 0-2.
 * @param enable true to enable, otherwise false.
 */
static inline void mcpwm_ll_sync_enable(mcpwm_dev_t *mcpwm, int timer, bool enable)
{
    if (enable) {
        mcpwm->timer[timer].sync.out_sel = 0;
        mcpwm->timer[timer].sync.in_en = 1;
    } else {
        mcpwm->timer[timer].sync.in_en = 0;
    }
}

/**
 * Set the phase (counter value) to reload when the sync signal triggers.
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 * @param timer Timer to set, 0-2.
 * @param reload_val The reloaded value.
 */
static inline void mcpwm_ll_sync_set_phase(mcpwm_dev_t *mcpwm, int timer, uint32_t reload_val)
{<commit_msg>mcpwm: fix the issue of wrong period<commit_after>// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*******************************************************************************
 * NOTICE
 * The hal is not public api, don't use in application code.
 * See readme.md in soc/include/hal/readme.md
 ******************************************************************************/

// The LL layer for ESP32 MCPWM register operations

#pragma once

#include <soc/mcpwm_periph.h>
#include "soc/mcpwm_periph.h"
#include "hal/mcpwm_types.h"
#include "soc/mcpwm_caps.h"
#include "hal/hal_defs.h"

#include "esp_types.h"

#ifdef __cplusplus
extern "C" {
#endif

/// Get the address of peripheral registers
#define MCPWM_LL_GET_HW(ID) (((ID)==0)? &MCPWM0: &MCPWM1)


/********************* Global *******************/
/**
 * Initialize common registers.
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 */
static inline void mcpwm_ll_init(mcpwm_dev_t *mcpwm)
{
    mcpwm->update_cfg.global_up_en = 1;
    mcpwm->update_cfg.global_force_up = 1;
    mcpwm->update_cfg.global_force_up = 0;
}

/**
 * Set the prescale of the PWM main clock to the input clock.
 *
 * Input clock is 160MHz, PWM main clock cycle = 6.25ns*(prescale + 1).
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 * @param prescale Prescale factor, 0-255.
 */
static inline void mcpwm_ll_set_clock_prescale(mcpwm_dev_t *mcpwm, int prescale)
{
    mcpwm->clk_cfg.prescale = prescale;
}

STATIC_HAL_REG_CHECK(MCPWM, MCPWM_LL_INTR_CAP0, MCPWM_CAP0_INT_RAW);
STATIC_HAL_REG_CHECK(MCPWM, MCPWM_LL_INTR_CAP1, MCPWM_CAP1_INT_RAW);
STATIC_HAL_REG_CHECK(MCPWM, MCPWM_LL_INTR_CAP2, MCPWM_CAP2_INT_RAW);

/**
 * Get raw interrupt status.
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 * @return The triggered interrupts, ORed by active interrupts.
 */
static inline mcpwm_intr_t mcpwm_ll_get_intr(mcpwm_dev_t *mcpwm)
{
    return mcpwm->int_raw.val;
}

/**
 * Clear the interrupts.
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 * @param intr Bitwise ORed interrupts to clear.
 */
static inline void mcpwm_ll_clear_intr(mcpwm_dev_t* mcpwm, mcpwm_intr_t intr)
{
    mcpwm->int_clr.val = intr;
}

/********************* Timer *******************/
/**
 * Set the prescale of the Timer_x clock to the PWM main clock.
 *
 * Timer clock frequency = PWM main clock frequency/(prescale + 1).
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 * @param timer The timer to set the prescale, 0-2.
 * @param prescale Prescale factor, 0-255.
 */
static inline void mcpwm_ll_timer_set_prescale(mcpwm_dev_t* mcpwm, int timer, uint32_t prescale)
{
    mcpwm->timer[timer].period.prescale = prescale;
}


STATIC_HAL_REG_CHECK(MCPWM, MCPWM_UP_COUNTER,        1);
STATIC_HAL_REG_CHECK(MCPWM, MCPWM_DOWN_COUNTER,      2);
STATIC_HAL_REG_CHECK(MCPWM, MCPWM_UP_DOWN_COUNTER,   3);

/**
 * Set the counting mode for the PWM timer.
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 * @param timer The timer to change counting mode, 0-2.
 * @param mode Counting mode to use.
 */
static inline void mcpwm_ll_timer_set_count_mode(mcpwm_dev_t *mcpwm, int timer, mcpwm_counter_type_t mode)
{
    mcpwm->timer[timer].mode.mode = mode;
}

/**
 * Start a timer.
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 * @param timer The timer to start, 0-2.
 */
static inline void mcpwm_ll_timer_start(mcpwm_dev_t *mcpwm, int timer)
{
    mcpwm->timer[timer].mode.start = 2;
}

/**
 * Stop a timer.
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 * @param timer The timer to stop, 0-2.
 */
static inline void mcpwm_ll_timer_stop(mcpwm_dev_t *mcpwm, int timer)
{
    mcpwm->timer[timer].mode.start = 0;
}

/**
 * Set the overflow period of a timer.
 *
 * The overflow rate will be Frequency of timer / period.
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 * @param timer The timer to set period, 0-2.
 * @param period Total timer count of each period, 0-65535.
 */
static inline void mcpwm_ll_timer_set_period(mcpwm_dev_t *mcpwm, int timer, uint32_t period)
{

    mcpwm->timer[timer].period.period = period - 1;
    mcpwm->timer[timer].period.upmethod = 0;
}

/**
 * Get the period setting of a timer.
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 * @param timer The timer to get the period, 0-2.
 * @return Period setting value.
 */
static inline uint32_t mcpwm_ll_timer_get_period(mcpwm_dev_t *mcpwm, int timer)
{
    return mcpwm->timer[timer].period.period + 1;
}

/********************* Sync *******************/
/**
 * Enable the synchronization feature for a timer.
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 * @param timer Timer to set, 0-2.
 * @param enable true to enable, otherwise false.
 */
static inline void mcpwm_ll_sync_enable(mcpwm_dev_t *mcpwm, int timer, bool enable)
{
    if (enable) {
        mcpwm->timer[timer].sync.out_sel = 0;
        mcpwm->timer[timer].sync.in_en = 1;
    } else {
        mcpwm->timer[timer].sync.in_en = 0;
    }
}

/**
 * Set the phase (counter value) to reload when the sync signal triggers.
 *
 * @param mcpwm Address of the MCPWM peripheral registers.
 * @param timer Timer to set, 0-2.
 * @param reload_val The reloaded value.
 */
static inline void mcpwm_ll_sync_set_phase(mcpwm_dev_t *mcpwm, int timer, uint32_t reload_val)
{
{'fraction_non_alphanumeric': 0.1448159119763013, 'fraction_numerical': 0.01388066017774016, 'mean_word_length': 4.0799656061908856, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '926649', 'n_tokens_mistral': 4254, 'n_tokens_neox': 3991, 'n_words': 1222}