/**
 ******************************************************************************
 * @file    msp.c
 * @author  Aaron Escoboza, Github account: https://github.com/aaron-ev
 * @brief   Interrupt header file: Holds interrupt handlers.
 ******************************************************************************
 */

#include "main.h"
#include "appConfig.h"

/**
* @brief Enable peripheral clocks and set NVIC priorities
* @param void
* @retval void
*/
void HAL_MspInit(void)
{
    /* Enable clocks for some system settings */
    __HAL_RCC_SYSCFG_CLK_ENABLE();
    __HAL_RCC_PWR_CLK_ENABLE();

    /* Enable clock for all peripherals being used */
    __HAL_RCC_GPIOA_CLK_ENABLE();
    __HAL_RCC_GPIOB_CLK_ENABLE();
    __HAL_RCC_GPIOC_CLK_ENABLE();
    __HAL_RCC_TIM2_CLK_ENABLE();
    __HAL_RCC_TIM5_CLK_ENABLE();
    __HAL_RCC_USART1_CLK_ENABLE();

    /* Set priority for PWM timer */
    HAL_NVIC_SetPriority(TIM2_IRQn, 14, 0);
    HAL_NVIC_EnableIRQ(TIM2_IRQn);
}

/**
* @brief Low level initialization for console UART
* @param *uartHandler UART handler that would be used for the console
* @retval void
*/
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandler)
{
    GPIO_InitTypeDef uartGpioInit;

    if (uartHandler->Instance == CONSOLE_INSTANCE)
    {
        /* No pull and speed very high */
        uartGpioInit.Pin = CONSOLE_TX_PIN | CONSOLE_RX_PIN;
        uartGpioInit.Mode = GPIO_MODE_AF_PP;
        uartGpioInit.Pull = GPIO_NOPULL;
        uartGpioInit.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
        uartGpioInit.Alternate = GPIO_AF7_USART1;
        HAL_GPIO_Init(CONSOLE_GPIO_PORT, &uartGpioInit);

        HAL_NVIC_SetPriority(USART1_IRQn, 15 , 0);
        HAL_NVIC_EnableIRQ(USART1_IRQn);
    }
}

/**
* @brief Low level initialization for GPIO pins assigned to PWM feature.
* @param *timerHandler Timer handler assigned to PWM feature.
* @retval void
*/
void HAL_TIM_OC_MspInit(TIM_HandleTypeDef *timerHandler)
{
    GPIO_InitTypeDef pwmGpioInit = {0};

    pwmGpioInit.Pin = PWM_GPIO_PINX;
    pwmGpioInit.Mode = GPIO_MODE_AF_PP;
    pwmGpioInit.Pull = GPIO_NOPULL;
    pwmGpioInit.Speed = GPIO_SPEED_FREQ_LOW;
    pwmGpioInit.Alternate = PWM_GPIO_ALTERNATE;
    HAL_GPIO_Init(PWM_GPIO_INSTANCE, &pwmGpioInit);
}

/**
* @brief Low level initialization for RTC
* @param *rtcHandler RTC handler
* @retval void
*/
void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandler)
{
    HAL_StatusTypeDef halStatus;
    RCC_OscInitTypeDef oscConfig = {0};
    RCC_PeriphCLKInitTypeDef pclkInit;

    oscConfig.LSEState = RCC_LSE_ON;;
    oscConfig.OscillatorType = RCC_OSCILLATORTYPE_LSE;
    halStatus = HAL_RCC_OscConfig(&oscConfig);
    if (halStatus != HAL_OK)
        Error_Handler();

    pclkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
    pclkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
    halStatus = HAL_RCCEx_PeriphCLKConfig(&pclkInit);
    if (halStatus != HAL_OK)
        Error_Handler();

    __HAL_RCC_RTC_ENABLE();
}
