/*
 * hal_RTC.c
 *
 *  Created on: Dec 19, 2011
 *      Author: RomanM
 */

#include "stm32f10x.h"
#include "hal_RTC.h"

void hal_RTC_Init(void) {
	hal_RTC_Config();
	hal_RTC_NVICConfig();
}

void hal_RTC_Config(void) {
	 /* Enable PWR and BKP clocks */
	  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

	  /* Allow access to BKP Domain */
	  PWR_BackupAccessCmd(ENABLE);

	  /* Reset Backup Domain */
	  BKP_DeInit();

	  /* Enable LSE */
	  RCC_LSEConfig(RCC_LSE_ON);
	  /* Wait till LSE is ready */
	  while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
	  {}

	  /* Select LSE as RTC Clock Source */
	  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

	  /* Enable RTC Clock */
	  RCC_RTCCLKCmd(ENABLE);

	  /* Wait for RTC registers synchronization */
	  RTC_WaitForSynchro();

	  /* Wait until last write operation on RTC registers has finished */
	  RTC_WaitForLastTask();

	  /* Enable the RTC Second */
	  RTC_ITConfig(RTC_IT_SEC, ENABLE);

	  /* Wait until last write operation on RTC registers has finished */
	  RTC_WaitForLastTask();

	  /* Set RTC prescaler: set RTC period to 1sec */
	  RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */

	  /* Wait until last write operation on RTC registers has finished */
	  RTC_WaitForLastTask();

}

void hal_RTC_NVICConfig(void) {
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Configure one bit for preemption priority */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  /* Enable the RTC Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}
