/**
 * @file        apm32f4xx_tmr_cfg.c
 *
 * @brief       This file provides configuration support for TMR
 *
 * @version     V1.0.0
 *
 * @date        2024-08-01
 *
 * @attention
 *
 *  Copyright (C) 2024-2025 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

/* Includes ***************************************************************/
#include "apm32f4xx_tmr_cfg.h"

/* Private includes *******************************************************/

/* Private macro **********************************************************/

/* Private typedef ********************************************************/

/* Private variables ******************************************************/
TMR_HandleTypeDef htmr1;
TMR_HandleTypeDef htmr2;
TMR_HandleTypeDef htmr3;

/* Private function prototypes ********************************************/

/* External variables *****************************************************/

/* External functions *****************************************************/

/**
 * @brief  Initializes the TMR Output Compare MSP
 *
 * @param  htmr TMR Output Compare handle
 *
 * @retval None
 */
void DAL_TMR_OC_MspInit(TMR_HandleTypeDef *htmr)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    if (htmr->Instance == TMR1)
    {
        /* Enable peripheral clock */
        __DAL_RCM_TMR1_CLK_ENABLE();

        /* Enable peripheral GPIO clock */
        __DAL_RCM_GPIOA_CLK_ENABLE();

        /* TMR channel pin configuration */
        GPIO_InitStruct.Mode        = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull        = GPIO_PULLUP;
        GPIO_InitStruct.Speed       = GPIO_SPEED_FREQ_HIGH;

        /* TMR channel 1 */
        GPIO_InitStruct.Pin         = GPIO_PIN_8;
        DAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

        /* TMR channel 2 */
        GPIO_InitStruct.Pin         = GPIO_PIN_9;
        DAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    }

    if (htmr->Instance == TMR2)
    {
        /* Enable peripheral clock */
        __DAL_RCM_TMR2_CLK_ENABLE();

        /* Enable peripheral GPIO clock */
        __DAL_RCM_GPIOA_CLK_ENABLE();

        /* TMR channel pin configuration */
        GPIO_InitStruct.Mode        = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull        = GPIO_PULLUP;
        GPIO_InitStruct.Speed       = GPIO_SPEED_FREQ_HIGH;

        /* TMR Channel 1 */
        GPIO_InitStruct.Pin         = GPIO_PIN_0;
        DAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    }

    if (htmr->Instance == TMR3)
    {
        /* Enable peripheral clock */
        __DAL_RCM_TMR3_CLK_ENABLE();

        /* Enable peripheral GPIO clock */
        __DAL_RCM_GPIOA_CLK_ENABLE();

        /* TMR channel pin configuration */
        GPIO_InitStruct.Mode        = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull        = GPIO_PULLUP;
        GPIO_InitStruct.Speed       = GPIO_SPEED_FREQ_HIGH;

        /* TMR Channel 1 */
        GPIO_InitStruct.Pin         = GPIO_PIN_6;
        DAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    }
}

/**
 * @brief  DeInitializes TMR Output Compare MSP
 *
 * @param  htmr TMR Output Compare handle
 *
 * @retval None
 */
void DAL_TMR_OC_MspDeInit(TMR_HandleTypeDef *htmr)
{
    if (htmr->Instance == TMR1)
    {
        /* Enable peripheral clock */
        __DAL_RCM_TMR1_CLK_DISABLE();

        /* De-init peripheral GPIO */
        DAL_GPIO_DeInit(GPIOA, GPIO_PIN_8);
        DAL_GPIO_DeInit(GPIOA, GPIO_PIN_9);
    }

    if (htmr->Instance == TMR2)
    {
        /* Enable peripheral clock */
        __DAL_RCM_TMR2_CLK_DISABLE();

        /* De-init peripheral GPIO */
        DAL_GPIO_DeInit(GPIOA, GPIO_PIN_0);
    }

    if (htmr->Instance == TMR3)
    {
        /* Enable peripheral clock */
        __DAL_RCM_TMR3_CLK_DISABLE();

        /* De-init peripheral GPIO */
        DAL_GPIO_DeInit(GPIOA, GPIO_PIN_6);
    }
}

/**
 * @brief   TMR1 configuration
 *
 * @param   None
 *
 * @retval  None
 */
void DAL_TMR1_Config(void)
{
    TMR_OC_InitTypeDef OC_ConfigStruct = {0};
    TMR_IC_InitTypeDef IC_ConfigStruct = {0};
    TMR_MasterConfigTypeDef Master_ConfigStruct = {0};
    TMR_SlaveConfigTypeDef Slave_ConfigStruct = {0};

    htmr1.Instance                  = TMR1;
    htmr1.Init.Prescaler            = 0U;
    htmr1.Init.CounterMode          = TMR_COUNTERMODE_UP;
    htmr1.Init.Period               = 59U;
    htmr1.Init.ClockDivision        = TMR_CLOCKDIVISION_DIV1;
    htmr1.Init.RepetitionCounter    = 0U;
    htmr1.Init.AutoReloadPreload    = TMR_AUTORELOAD_PRELOAD_DISABLE;
    if (DAL_TMR_OC_Init(&htmr1) != DAL_OK)
    {
        Error_Handler();
    }

    /* Configure output channel */
    OC_ConfigStruct.OCMode      = TMR_OCMODE_TOGGLE;
    OC_ConfigStruct.OCPolarity  = TMR_OCPOLARITY_HIGH;
    if (DAL_TMR_OC_ConfigChannel(&htmr1, &OC_ConfigStruct, TMR_CHANNEL_1) != DAL_OK)
    {
        Error_Handler();
    }
    
    /* Configure input channel */
    IC_ConfigStruct.ICPolarity  = TMR_ICPOLARITY_RISING;
    IC_ConfigStruct.ICSelection = TMR_ICSELECTION_DIRECTTI;
    IC_ConfigStruct.ICPrescaler = TMR_ICPSC_DIV1;
    IC_ConfigStruct.ICFilter    = 0U;
    if (DAL_TMR_IC_ConfigChannel(&htmr1, &IC_ConfigStruct, TMR_CHANNEL_2) != DAL_OK)
    {
        Error_Handler();
    }

    /* Configure slave TMR */
    Slave_ConfigStruct.SlaveMode     = TMR_SLAVEMODE_GATED;
    Slave_ConfigStruct.InputTrigger  = TMR_TS_TI2FP2;
    if (DAL_TMR_SlaveConfigSynchro(&htmr1, &Slave_ConfigStruct) != DAL_OK)
    {
        Error_Handler();
    }

    /* Configure master TMR */
    Master_ConfigStruct.MasterOutputTrigger = TMR_TRGO_ENABLE;
    Master_ConfigStruct.MasterSlaveMode     = TMR_MASTERSLAVEMODE_ENABLE;
    if (DAL_TMREx_MasterConfigSynchronization(&htmr1, &Master_ConfigStruct) != DAL_OK)
    {
        Error_Handler();
    }
}

/**
 * @brief   TMR2 configuration
 *
 * @param   None
 *
 * @retval  None
 */
void DAL_TMR2_Config(void)
{
    TMR_OC_InitTypeDef OC_ConfigStruct = {0};
    TMR_MasterConfigTypeDef Master_ConfigStruct = {0};
    TMR_SlaveConfigTypeDef Slave_ConfigStruct = {0};

    htmr2.Instance                  = TMR2;
    htmr2.Init.Prescaler            = 0U;
    htmr2.Init.CounterMode          = TMR_COUNTERMODE_UP;
    htmr2.Init.Period               = 119U;
    htmr2.Init.ClockDivision        = TMR_CLOCKDIVISION_DIV1;
    htmr2.Init.RepetitionCounter    = 0U;
    htmr2.Init.AutoReloadPreload    = TMR_AUTORELOAD_PRELOAD_DISABLE;
    if (DAL_TMR_OC_Init(&htmr2) != DAL_OK)
    {
        Error_Handler();
    }

    /* Configure output channel */
    OC_ConfigStruct.OCMode      = TMR_OCMODE_TOGGLE;
    OC_ConfigStruct.OCPolarity  = TMR_OCPOLARITY_HIGH;
    if (DAL_TMR_OC_ConfigChannel(&htmr2, &OC_ConfigStruct, TMR_CHANNEL_1) != DAL_OK)
    {
        Error_Handler();
    }

    /* Configure slave TMR */
    Slave_ConfigStruct.SlaveMode     = TMR_SLAVEMODE_GATED;
    Slave_ConfigStruct.InputTrigger  = TMR_TS_ITR0;
    if (DAL_TMR_SlaveConfigSynchro(&htmr2, &Slave_ConfigStruct) != DAL_OK)
    {
        Error_Handler();
    }

    /* Configure master TMR */
    Master_ConfigStruct.MasterOutputTrigger = TMR_TRGO_ENABLE;
    Master_ConfigStruct.MasterSlaveMode     = TMR_MASTERSLAVEMODE_ENABLE;
    if (DAL_TMREx_MasterConfigSynchronization(&htmr2, &Master_ConfigStruct) != DAL_OK)
    {
        Error_Handler();
    }
}

/**
 * @brief   TMR3 configuration
 *
 * @param   None
 *
 * @retval  None
 */
void DAL_TMR3_Config(void)
{
    TMR_OC_InitTypeDef OC_ConfigStruct = {0};
    TMR_SlaveConfigTypeDef Slave_ConfigStruct = {0};

    htmr3.Instance                  = TMR3;
    htmr3.Init.Prescaler            = 0U;
    htmr3.Init.CounterMode          = TMR_COUNTERMODE_UP;
    htmr3.Init.Period               = 239U;
    htmr3.Init.ClockDivision        = TMR_CLOCKDIVISION_DIV1;
    htmr3.Init.RepetitionCounter    = 0U;
    htmr3.Init.AutoReloadPreload    = TMR_AUTORELOAD_PRELOAD_DISABLE;
    if (DAL_TMR_OC_Init(&htmr3) != DAL_OK)
    {
        Error_Handler();
    }

    /* Configure output channel */
    OC_ConfigStruct.OCMode      = TMR_OCMODE_TOGGLE;
    OC_ConfigStruct.OCPolarity  = TMR_OCPOLARITY_HIGH;
    if (DAL_TMR_OC_ConfigChannel(&htmr3, &OC_ConfigStruct, TMR_CHANNEL_1) != DAL_OK)
    {
        Error_Handler();
    }

    /* Configure slave TMR */
    Slave_ConfigStruct.SlaveMode     = TMR_SLAVEMODE_GATED;
    Slave_ConfigStruct.InputTrigger  = TMR_TS_ITR0;
    if (DAL_TMR_SlaveConfigSynchro(&htmr3, &Slave_ConfigStruct) != DAL_OK)
    {
        Error_Handler();
    }
}
