title: Can't get the RTC to work
body: <p>I am trying to create a simple program that reads RTC clock value and prints it to serial monitor. USART is working fine, but i can't figure out what's wrong with RTC. It is giving me all the time same value that was set in setuRTC(). Also the second interrupt is not working either. Edit: I am using STM32f1 development board, same as <a href="http://www.aliexpress.com/item/Free-shipping-STM32F103VET6-ARM-Cortex-M3-development-Board-2-8-TFT-LCD-Touch-Panel/587697740.html" rel="nofollow">this</a></p>
  
  <p>Here is my RTC setup:</p>
  
  <pre><code>void setupRTC(void){
      RCC-&gt;APB1ENR |= (RCC_APB1ENR_PWREN | RCC_APB1ENR_BKPEN); //Enable the power and backup interface clocks by setting the PWREN and BKPEN bitsin the RCC_APB1ENR register
      PWR-&gt;CR |= PWR_CR_DBP;                     //Enable access to the backup registers and the RTC.
      RCC-&gt;BDCR |= RCC_BDCR_LSEON;               //External Low Speed oscillator enable 
      while((RCC-&gt;BDCR &amp; RCC_BDCR_LSERDY) == 0); //Wait until external oscillisator is stabilised
      RCC-&gt;BDCR |= RCC_BDCR_RTCSEL_LSE;
      sendstr(textlse);
      RCC-&gt;BDCR |= RCC_BDCR_RTCSEL_LSE;          //Select Source (LSE oscillator clock used as RTC clock )
      RCC-&gt;BDCR &amp;= ~((1 &lt;&lt; 8) | (1 &lt;&lt; 9));       //Unusable clock source, here to prevent warnings, turn off clock sources to RTC.
      RCC-&gt;BDCR = RCC_BDCR_RTCEN;                //RTC clock enable
      RTC-&gt;CRL &amp;= ~RTC_CRL_RSF;                  //Clear registers Synchronized Flag
      while(!(RTC-&gt;CRL &amp; RTC_CRL_RSF));          //Wait for the RSF bit in RTC_CRL to be set by hardware
  
      while((RTC-&gt;CRL &amp; RTC_CRL_RTOFF) == 0);    //Wait for RTOFF It is not possible to write to the RTC_CR register while the peripheral is completing a previous write operation
      RTC-&gt;CRL |= RTC_CRL_CNF;                   //Set the CNF bit to enter configuration mode
  
  
      /* Set RTC COUNTER MSB word */
      RTC-&gt;CNTH = (12*3600 + 40*60 + 00) &gt;&gt; 16;  //Random time
      /* Set RTC COUNTER LSB word */
      RTC-&gt;CNTL = ((12*3600 + 40*60 + 00)&amp; 0x0000FFFF);
      RTC-&gt;CRH |= RTC_CRH_SECIE;                 //Second Interrupt Enable
      RTC-&gt;CRL &amp;= ~RTC_CRL_CNF;                  //Exit configuration mode
      while((RTC-&gt;CRL &amp; RTC_CRL_RTOFF) == 0);    //Wait for RTOFF 
      sendstr(textconf);
  }
  </code></pre>
  
  <p>Here is the whole program:</p>
  
  <pre><code>#include "stm32f10x.h"
  #include &lt;stdio.h&gt;
  
  uint8_t text[] = {"\nSTM32 USART test ä ö \n"};
  uint8_t textlse[] = {"LSE ready\n"};
  uint8_t textconf[] = {"Configuration complete\n"};
  char str[32]; // buffer for text
  
  uint8_t RxBuffer[1024];
  int rxcount = 0;
  // Private function prototypes
  void sendstr(uint8_t * str);
  void sendChar(uint8_t ch);
  void sendTime(uint32_t ch);
  void setupRTC(void);
  void setupUSART(void);
  u32 rtc_get_counter_val(void);
  
  
  
  void setupRTC(void){
      RCC-&gt;APB1ENR |= (RCC_APB1ENR_PWREN | RCC_APB1ENR_BKPEN); //Enable the power and backup interface clocks by setting the PWREN and BKPEN bitsin the RCC_APB1ENR register
      PWR-&gt;CR |= PWR_CR_DBP;                     //Enable access to the backup registers and the RTC.
    RCC-&gt;BDCR |= RCC_BDCR_LSEON;               //External Low Speed oscillator enable 
      while((RCC-&gt;BDCR &amp; RCC_BDCR_LSERDY) == 0); //Wait until external oscillisator is stabilised
      RCC-&gt;BDCR |= RCC_BDCR_RTCSEL_LSE;
      sendstr(textlse);
      RCC-&gt;BDCR |= RCC_BDCR_RTCSEL_LSE;          //Select Source (LSE oscillator clock used as RTC clock )
    RCC-&gt;BDCR &amp;= ~((1 &lt;&lt; 8) | (1 &lt;&lt; 9));       //Unusable clock source, here to prevent warnings, turn off clock sources to RTC.
      RCC-&gt;BDCR = RCC_BDCR_RTCEN;                //RTC clock enable
      RTC-&gt;CRL &amp;= ~RTC_CRL_RSF;                  //Clear registers Synchronized Flag
      while(!(RTC-&gt;CRL &amp; RTC_CRL_RSF));          //Wait for the RSF bit in RTC_CRL to be set by hardware
  
      while((RTC-&gt;CRL &amp; RTC_CRL_RTOFF) == 0);    //Wait for RTOFF It is not possible to write to the RTC_CR register while the peripheral is completing a previous write operation
      RTC-&gt;CRL |= RTC_CRL_CNF;                   //Set the CNF bit to enter configuration mode
  
  
      /* Set RTC COUNTER MSB word */
    RTC-&gt;CNTH = (12*3600 + 40*60 + 00) &gt;&gt; 16;  //Random time
    /* Set RTC COUNTER LSB word */
    RTC-&gt;CNTL = ((12*3600 + 40*60 + 00)&amp; 0x0000FFFF);
      RTC-&gt;CRH |= RTC_CRH_SECIE;                 //Second Interrupt Enable
      RTC-&gt;CRL &amp;= ~RTC_CRL_CNF;                  //Exit configuration mode
      while((RTC-&gt;CRL &amp; RTC_CRL_RTOFF) == 0);    //Wait for RTOFF 
      sendstr(textconf);
  }
  
  void setupUSART(void){
      RCC-&gt;APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_USART1EN;
      USART1-&gt;CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE;
      USART1-&gt;BRR = (SystemCoreClock / 115200);
      GPIOA-&gt;CRH &amp;= 0xFFFFF00F;                                 //Set RX(PA10) and TX(PA9) pins
      GPIOA-&gt;CRH |= 0x000008A0;                                 //In TTL communication, TX pin must be conficured as pushpull
      NVIC_EnableIRQ(USART1_IRQn);
      USART1-&gt;CR1 |= USART_CR1_RXNEIE;                          //Enable USART interrupt
  }
  
  int main(void)
  {
      vu32 dly;
      uint32_t time;
      RCC-&gt;APB2ENR |= RCC_APB2ENR_IOPCEN;                       //Enable clock for PC pins
    GPIOC-&gt;CRL &amp;= 0x00FFFFFF;                                 //Setup PC6 and PC6 leds
      GPIOC-&gt;CRL |= 0x33000000;
      setupUSART();
      setupRTC();
      sendstr(text);
  
  
      while (1)
    {
          time = rtc_get_counter_val();
          sprintf(str, "%10d\n", time);
          sendstr(str);
          for(dly = 0; dly &lt; 1000000; dly++);
    }
  }
  
  void sendChar(uint8_t ch){
      while(!(USART1-&gt;SR &amp; USART_SR_TXE));
          USART1-&gt;DR = ch;
  
  }
  
  void sendTime(uint32_t ch){
      while(!(USART1-&gt;SR &amp; USART_SR_TXE));
          USART1-&gt;DR = ch;
  
  }
  
  
  void sendstr(uint8_t * str){
      while(*str != 0){
          sendChar(*str);
          str++;
      }
  }
  
  
  void USART1_IRQHandler(void){
      if(USART1-&gt;SR &amp; 0x0020){ //content of the shift register is transferred to the RDR RXNE bit.
          GPIOC-&gt;BSRR = (1 &lt;&lt; 7);
          RxBuffer[rxcount++] = (uint8_t)(USART1-&gt;DR &amp; (uint8_t)USART_DR_DR);
          USART1-&gt;SR = (uint16_t)~0x0020;
          sendstr(RxBuffer);
    }
  }
  
  
  void RTC_IRQHandler(void){
      if(RTC-&gt;CRL &amp; RTC_CRL_SECF){
          GPIOC-&gt;BSRR = (1 &lt;&lt; 7);
          RTC-&gt;CRL &amp;= ~RTC_CRL_SECF;
      }
  }
  
  
  
  u32 rtc_get_counter_val(void)
  {
      return (RTC-&gt;CNTH &lt;&lt; 16) | RTC-&gt;CNTL;
  }
  </code></pre>