/**
 * @file        apm32f4xx_tmr_cfg.c
 *
 * @brief       This file provides configuration support for TMR
 *
 * @version     V1.0.0
 *
 * @date        2023-12-01
 *
 * @attention
 *
 *  Copyright (C) 2023 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;

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

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

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

/**
 * @brief  Initializes the TMR Encoder Interface MSP
 *
 * @param  htmr TMR Encoder Interface handle
 *
 * @retval None
 */
void DAL_TMR_Encoder_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_GPIOE_CLK_ENABLE();

        /* TMR channel pin configuration */
        GPIO_InitStruct.Mode        = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull        = GPIO_PULLUP;
        GPIO_InitStruct.Speed       = GPIO_SPEED_HIGH;
        GPIO_InitStruct.Alternate   = GPIO_AF1_TMR1;

        /* TMR channel 1 */
        GPIO_InitStruct.Pin         = GPIO_PIN_9;
        DAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

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

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

        /* De-init peripheral GPIO */
        DAL_GPIO_DeInit(GPIOE, GPIO_PIN_9 | GPIO_PIN_11);
    }
}

/**
 * @brief   TMR1 configuration
 *
 * @param   None
 *
 * @retval  None
 */
void DAL_TMR1_Config(void)
{
    TMR_Encoder_InitTypeDef Encoder_ConfigStruct = {0};

    htmr1.Instance                  = TMR1;
    htmr1.Init.Prescaler            = 0U;
    htmr1.Init.CounterMode          = TMR_COUNTERMODE_UP;
    htmr1.Init.Period               = 65535U;
    htmr1.Init.ClockDivision        = TMR_CLOCKDIVISION_DIV1;
    htmr1.Init.RepetitionCounter    = 0U;
    htmr1.Init.AutoReloadPreload    = TMR_AUTORELOAD_PRELOAD_DISABLE;

    /* Configure encoder mode */
    Encoder_ConfigStruct.EncoderMode    = TMR_ENCODERMODE_TI12;
    Encoder_ConfigStruct.IC1Polarity    = TMR_ICPOLARITY_RISING;
    Encoder_ConfigStruct.IC1Selection   = TMR_ICSELECTION_DIRECTTI;
    Encoder_ConfigStruct.IC1Prescaler   = TMR_ICPSC_DIV1;
    Encoder_ConfigStruct.IC1Filter      = 0U;

    Encoder_ConfigStruct.IC2Polarity    = TMR_ICPOLARITY_RISING;
    Encoder_ConfigStruct.IC2Selection   = TMR_ICSELECTION_DIRECTTI;
    Encoder_ConfigStruct.IC2Prescaler   = TMR_ICPSC_DIV1;
    Encoder_ConfigStruct.IC2Filter      = 0U;
    if (DAL_TMR_Encoder_Init(&htmr1, &Encoder_ConfigStruct) != DAL_OK)
    {
        Error_Handler();
    }
}
