CircleOS  1
rtc_spe.c
Go to the documentation of this file.
1 /****************** COPYRIGHT (C) 2007-2013 KEOLABS S.A.S. ********************/
14 /******************************************************************************/
15 
16 /* Includes ------------------------------------------------------------------*/
17 #include "circle.h"
18 
20 
21 /* Private define ------------------------------------------------------------*/
22 //#define BUG_RTC_CRYSTAL // YRT20091112
23 
24 /* Private variables ---------------------------------------------------------*/
25 
26 /* Public functions for CircleOS ---------------------------------------------*/
27 
28 /*******************************************************************************
29 *
30 * RTC_Init
31 *
32 *******************************************************************************/
40 /******************************************************************************/
41 NODEBUG2 void RTC_Init( void )
42 {
43  /* CK_RTC clock selection */
44  RCC_APB1PeriphClockCmd( RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE );
45 
46  /* Allow access to BKP Domain */
47  PWR_BackupAccessCmd( ENABLE );
48 
49  /* Enable the LSE OSC */
50  RCC_LSEConfig( RCC_LSE_ON );
51 
52  /* Disable the LSI OSC */
53 // RCC_LSICmd( DISABLE ); //__YRT20091112
54 
55  /* Wait till LSE is ready */
56 #ifndef BUG_RTC_CRYSTAL
57 #if LED_INV
58  GPIO_WriteBit( GPIOx_LED, GPIO_Pin_LED0, Bit_RESET ); // Green
59 #else
60  GPIO_WriteBit( GPIOx_LED, GPIO_Pin_LED0, Bit_SET ); // Green
61 #endif
62 
63  while ( RCC_GetFlagStatus( RCC_FLAG_LSERDY ) == RESET )
64  {}
65 
66 #if LED_INV
67  GPIO_WriteBit( GPIOx_LED, GPIO_Pin_LED0, Bit_SET ); // Green
68 #else
69  GPIO_WriteBit( GPIOx_LED, GPIO_Pin_LED0, Bit_RESET ); // Green
70 #endif
71 #endif
72 
73  /* Select the RTC Clock Source */
74  RCC_RTCCLKConfig( RCC_RTCCLKSource_LSE );
75 
76  /* Enable the RTC Clock */
77  RCC_RTCCLKCmd( ENABLE );
78 
79 #ifndef BUG_RTC_CRYSTAL
80 #if LED_INV
81  GPIO_WriteBit( GPIOx_LED, GPIO_Pin_LED1, Bit_RESET ); // Red
82 #else
83  GPIO_WriteBit( GPIOx_LED, GPIO_Pin_LED1, Bit_SET ); // Red
84 #endif
85 
86  /* Wait for RTC registers synchronization */
87  RTC_WaitForSynchro();
88 
89 #if LED_INV
90  GPIO_WriteBit( GPIOx_LED, GPIO_Pin_LED1, Bit_SET ); // Red
91 #else
92  GPIO_WriteBit( GPIOx_LED, GPIO_Pin_LED1, Bit_RESET ); // Red
93 #endif
94 #endif
95 
96  /* Wait until last write operation on RTC registers has finished */
97  RTC_WaitForLastTask();
98 
99  /* Enable the RTC Second interrupt */
100  RTC_ITConfig( RTC_IT_SEC, ENABLE );
101 
102  /* Wait until last write operation on RTC registers has finished */
103  RTC_WaitForLastTask();
104 
105  /* Set RTC prescaler: set RTC period to 1sec */
106  /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
107  RTC_SetPrescaler( 32767 );
108 
109 #ifndef BUG_RTC_CRYSTAL
110 #if LED_INV
111  GPIO_WriteBit( GPIOx_LED, GPIO_Pin_LED1, Bit_RESET ); // Red
112 #else
113  GPIO_WriteBit( GPIOx_LED, GPIO_Pin_LED1, Bit_SET ); // Red
114 #endif
115 
116  /* Wait until last write operation on RTC registers has finished */
117  RTC_WaitForLastTask();
118 
119 #if LED_INV
120  GPIO_WriteBit( GPIOx_LED, GPIO_Pin_LED1, Bit_SET ); // Red
121 #else
122  GPIO_WriteBit( GPIOx_LED, GPIO_Pin_LED1, Bit_RESET ); // Red
123 #endif
124 #endif
125 }
126 
128 
129 /* Public functions ----------------------------------------------------------*/
130 
131 /*******************************************************************************
132 *
133 * RTC_GetTime
134 *
135 *******************************************************************************/
145 /******************************************************************************/
146 void RTC_GetTime( u8* THH, u8* TMM, u8* TSS )
147 {
148  counter_t Tmp;
149 
150  /* Load the Counter value */
151  Tmp = RTC_GetCounter();
152 
153  /* Compute hours */
154  *THH = ( Tmp / 3600 ) % 24;
155 
156  /* Compute minutes */
157  *TMM = ( Tmp / 60 ) % 60;
158 
159  /* Compute seconds */
160  *TSS = Tmp % 60;
161 }
162 
163 /*******************************************************************************
164 *
165 * RTC_SetTime
166 *
167 *******************************************************************************/
177 /******************************************************************************/
178 NODEBUG2 void RTC_SetTime( u8 THH, u8 TMM, u8 TSS )
179 {
180  /* Adjust the counter value */
181  RTC_SetCounter( THH * 3600 + TMM * 60 + TSS );
182 }
183 
184 /*******************************************************************************
185 *
186 * RTC_DisplayTime
187 *
188 *******************************************************************************/
196 /******************************************************************************/
197 void RTC_DisplayTime( void )
198 {
199  static counter_t last_time = 0;
200  counter_t new_time = RTC_GetCounter();
201 
202  // Time and/or Vbat
203 
204  if ( last_time != new_time )
205  {
206  last_time = new_time ;
207 
208  if ( fDisplayTime == 1 )
209  {
210  DRAW_DisplayTime( 6, 0 ) ;
211 
212  #if DISPLAY_TEMP
213  UTIL_SetTempMode(0);
214  DRAW_DisplayTemp( 90, 0 );
215  #endif
216  }
217  }
218 }