CircleOS  1
power_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 /*******************************************************************************
22 *
23 * POWER_Init
24 *
25 *******************************************************************************/
33 /******************************************************************************/
34 void POWER_Init( void )
35 {
36  GPIO_InitTypeDef GPIO_InitStructure;
37 
38  /* Enable Status GPIO clock */
39  RCC_PERIPH_GPIO_CLOCK_CMD( GPIO_PWR_PERIPH_STAT, ENABLE );
40 
41  /* Configure status pins as floating input */
42  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
43  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
44  GPIO_InitStructure.GPIO_Pin = GPIO_PWR_LOADING_PIN | GPIO_PWR_DONE_PIN;
45  GPIO_Init( GPIOx_PWR_STAT, &GPIO_InitStructure );
46 
47  POWER_Set_Time();
48 }
49 
50 /*******************************************************************************
51 *
52 * POWER_ReadStatus
53 *
54 *******************************************************************************/
65 /******************************************************************************/
66 s32 POWER_ReadStatus( void )
67 {
68 #define DEBUG_DISPLAY 0
69 
70  // First read the charger status
71  s32 status1 = ( GPIO_ReadInputDataBit( GPIOx_PWR_STAT, GPIO_PWR_LOADING_PIN ) == Bit_SET );
72  s32 status2 = ( GPIO_ReadInputDataBit( GPIOx_PWR_STAT, GPIO_PWR_DONE_PIN ) == Bit_SET );
73  s32 flagsta = status1 + 2 * status2; // Rq: status = 1 when led off
74 
75  //FL120727
76  //The real state of the pins is not properly read when the USB cable is
77  //plugged off. In this situation, the input are floating and the values
78  //read are more or less random.
79  //To fix this issue, we modify the voltage (by swithing on/off the internal
80  //pullup-pulldown resistors), then we check is the read value is modified.
81  //If the read value is still the level applied by the internal resistor, we
82  //are floating (therefore, not charging...). If the values returned to the
83  //initial state, it means that the charger circuit forces the state and
84  //we can trust the state we found.
85 
86 #if defined(DEBUG_DISPLAY) && DEBUG_DISPLAY
87  /* debug*/char buffer [40];
88  /* debug*/strcpy( buffer, "StF:0" );
89  /* debug*/buffer[4] += flagsta;
90  /* debug*/DRAW_DisplayStringWithMode( 0, 90, buffer, ALL_SCREEN, INVERTED_TEXT, CENTER );
91  /* debug*/UTIL_uint2str( buffer, PWR_CurrentVBat, 4, 0 );
92  /* debug*/DRAW_DisplayStringWithMode( 0, 70, buffer, ALL_SCREEN, INVERTED_TEXT, CENTER );
93 #endif
94 
95  if ( flagsta == 2 ) //being charged
96  {
97  s32 flagsta_IPU, flagsta_IPD;
98 
99  //Configure for a short time the status pins input as output...
100  GPIO_InitTypeDef GPIO_InitStructure;
101 
102  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
103  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
104  GPIO_InitStructure.GPIO_Pin = GPIO_PWR_LOADING_PIN | GPIO_PWR_DONE_PIN;
105  GPIO_Init( GPIOx_PWR_STAT, &GPIO_InitStructure );
106 
107  //and restore normal settings (i.e. floating input)
108  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
109  GPIO_Init( GPIOx_PWR_STAT, &GPIO_InitStructure );
110 
111  status1 = ( GPIO_ReadInputDataBit( GPIOx_PWR_STAT, GPIO_PWR_LOADING_PIN ) == Bit_SET );
112  status2 = ( GPIO_ReadInputDataBit( GPIOx_PWR_STAT, GPIO_PWR_DONE_PIN ) == Bit_SET );
113  flagsta_IPU = status1 + 2 * status2; // Rq: status = 1 when led off
114 
115 #if defined(DEBUG_DISPLAY) && DEBUG_DISPLAY
116  /* debug*/strcpy( buffer, "StU:0" );
117  /* debug*/buffer[4] += flagsta_IPU;
118  /* debug*/DRAW_DisplayStringWithMode( 0, 50, buffer, ALL_SCREEN, INVERTED_TEXT, CENTER );
119 #endif
120 
121  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
122  GPIO_Init( GPIOx_PWR_STAT, &GPIO_InitStructure );
123 
124  //and restore normal settings (i.e. floating input)
125  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
126  GPIO_Init( GPIOx_PWR_STAT, &GPIO_InitStructure );
127 
128  status1 = ( GPIO_ReadInputDataBit( GPIOx_PWR_STAT, GPIO_PWR_LOADING_PIN ) == Bit_SET );
129  status2 = ( GPIO_ReadInputDataBit( GPIOx_PWR_STAT, GPIO_PWR_DONE_PIN ) == Bit_SET );
130  flagsta_IPD = status1 + 2 * status2; // Rq: status = 1 when led off
131 
132 #if defined(DEBUG_DISPLAY) && DEBUG_DISPLAY
133  /* debug*/strcpy( buffer, "StD:0" );
134  /* debug*/buffer[4] += flagsta_IPD;
135  /* debug*/DRAW_DisplayStringWithMode( 0, 30, buffer, ALL_SCREEN, INVERTED_TEXT, CENTER );
136 #endif
137 
138  //and we conclude about the validity of the values we found:
139  if ( ( flagsta_IPD != flagsta ) || ( flagsta_IPU != flagsta ) )
140  {
141  //the values are not reliable: USB is plugged off!
142  flagsta = 3; // we change the status by "Power supplied by battery" (3)
143  }
144  }
145 
146  return flagsta;
147 }
149 
150 /*******************************************************************************
151 *
152 * SHUTDOWN_Action
153 *
154 *******************************************************************************/
161 /******************************************************************************/
162 void SHUTDOWN_Action( void )
163 {
164  u16 baksys2;
165 
166  //-- Backup system values --
167 
168  // CPU frequency
169  baksys2 = UTIL_GetPll() & BKPMASK_S2_PLL; // 3 bits
170 
171  // Option autorun
172  baksys2 |= ( AutorunOn ? ( BKPMASK_S2_AUTORUN ) : 0 );
173 
174  // Option loudspeaker
175  baksys2 |= ( AUDIO_SpeakerOn ? BKPMASK_S2_SPEAKER : 0 );
176 
177  // Is it currently mute?
178  baksys2 |= ( AUDIO_Mute ? BKPMASK_S2_MUTE : 0 );
179 
180  // Option buzzer
181  baksys2 |= ( AUDIO_BuzzerOn ? ( BKPMASK_S2_BUZZER ) : 0 );
182 
183  // Option joystick
184  baksys2 |= ( JoystickAsInput ? BKPMASK_S2_JOYSTICK : 0 );
185 
186  // Option Mems
187  baksys2 |= ( MemsAsInput ? BKPMASK_S2_MEMS : 0 );
188 
189  // Option Touchscreen
190  baksys2 |= ( TchscrAsInput ? ( BKPMASK_S2_TCHSCR ) : 0 );
191 
193 
194  // Audio volume
195  UTIL_WriteBackupRegister( BKP_SYS5, AUDIO_Volume & BKPMASK_S5_VOLUME );
196 
197  // Backlight
198  UTIL_WriteBackupRegister( BKP_BKLIGHT, LCD_GetBackLight() );
199 
200  // Font menus
201  UTIL_WriteBackupRegister( BKP_SYS4, Menu_Font );
202 
203  //-- Stops peripherals --
204 
205  // Disable TIM2 Update interrupt (mems)
206  TIM_ITConfig( TIM2, TIM_IT_Update, DISABLE );
207 
208  // Stop the audio codec
209  AUDIO_Shutdown();
210 
211  //-- Power Off --
212 
213  /* Enable Shutdown GPIO clock */
214  RCC_PERIPH_GPIO_CLOCK_CMD( GPIO_PWR_PERIPH, ENABLE );
215 
216  /* Configure pin shutdown as Push-Pull output */
217  GPIO_InitTypeDef GPIO_InitStructure;
218  GPIO_InitStructure.GPIO_Pin = GPIO_PWR_SHUTDOWN_PIN;
219  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
220  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
221  GPIO_Init( GPIOx_PWR, &GPIO_InitStructure );
222 
223  GPIO_WriteBit( GPIOx_PWR, GPIO_PWR_SHUTDOWN_PIN, SET );
224 
225  // Wait for the end
226  while ( 1 ) {;}
227 }