
//---Includes ----------------------------------------------------------------


#include <HALWrapper/stm32_rcc.h>
#include <HALWrapper/stm32_rtc.h>
#include <HALWrapper/stm32_pwr.h>          // HAL_PWR_EnableBkUpAccess
#include <HALWrapper/stm32_cortex.h>       // NVIC

#include <HALWrapper/stm32xx_rtc_sup.h>    // Myself


//---Implementation-----------------------------------------------------------

/*
bool rtcUseLse=false;

void setRtcUseLse(bool useLse)
{
   rtcUseLse=useLse;
}
*/
#if 0
/**
  * @brief RTC MSP Initialization
  *        This function configures the hardware resources used in this example
  * @param hrtc: RTC handle pointer
  *
  * @note  Care must be taken when HAL_RCCEx_PeriphCLKConfig() is used to select
  *        the RTC clock source; in this case the Backup domain will be reset in
  *        order to modify the RTC Clock source, as consequence RTC registers (including
  *        the backup registers) and RCC_BDCR register are set to their reset values.
  *
  * @retval None
  */
void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
{
   RCC_OscInitTypeDef        RCC_OscInitStruct;
   RCC_PeriphCLKInitTypeDef  PeriphClkInitStruct;

  /*##-1- Enables the PWR Clock and Enables access to the backup domain ###################################*/
  /* To change the source clock of the RTC feature (LSE, LSI), You have to:
     - Enable the power clock using __PWR_CLK_ENABLE()
     - Enable write access using HAL_PWR_EnableBkUpAccess() function before to
       configure the RTC clock source (to be done once after reset).
     - Reset the Back up Domain using __HAL_RCC_BACKUPRESET_FORCE() and
       __HAL_RCC_BACKUPRESET_RELEASE().
     - Configure the needed RTC clock source */

   #pragma GCC diagnostic push
   #pragma GCC diagnostic ignored "-Wvolatile"

   __HAL_RCC_PWR_CLK_ENABLE();
   HAL_PWR_EnableBkUpAccess();

   #pragma GCC diagnostic pop

  /*##-2- Configue LSE/LSI as RTC clock soucre ###############################*/
  RCC_OscInitStruct.OscillatorType =  rtcUseLse ?  RCC_OSCILLATORTYPE_LSE : (RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE);
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  RCC_OscInitStruct.LSEState = rtcUseLse ? RCC_LSE_ON : RCC_LSE_OFF;
  RCC_OscInitStruct.LSIState = rtcUseLse ? RCC_LSI_OFF : RCC_LSE_ON;
  if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  PeriphClkInitStruct.RTCClockSelection = rtcUseLse ? RCC_RTCCLKSOURCE_LSE :
                                                        RCC_RTCCLKSOURCE_LSI;
  if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /*##-2- Enable RTC peripheral Clocks #######################################*/
  /* Enable RTC Clock */

   #pragma GCC diagnostic push
   #pragma GCC diagnostic ignored "-Wvolatile"

  __HAL_RCC_RTC_ENABLE();

   #pragma GCC diagnostic pop

  /*##-4- Configure the NVIC for RTC Alarm ###################################*/
  // Do we really need the IRQ of the RTC? For What? Updating Display..
  //HAL_NVIC_SetPriority(RTC_IRQn, 0x0F, 0);
  //HAL_NVIC_EnableIRQ(RTC_IRQn);
}


/**
  * @brief  This function is executed in case of error occurrence.
  * @param  None
  * @retval None
  */
static void Error_Handler()
{
  /* User may add here some code to deal with this error */
  while(1)
  {
  }
}

#endif

//---fin----------------------------------------------------------------------
