amiro-blt / Target / Modules / PowerManagement_1-2 / Boot / main.c @ 73410535
History | View | Annotate | Download (56.968 KB)
1 |
/************************************************************************************//** |
---|---|
2 |
* \file Demo\ARMCM4_STM32_Olimex_STM32E407_GCC\Boot\main.c
|
3 |
* \brief Bootloader application source file.
|
4 |
* \ingroup Boot_ARMCM4_STM32_Olimex_STM32E407_GCC
|
5 |
* \internal
|
6 |
*----------------------------------------------------------------------------------------
|
7 |
* C O P Y R I G H T
|
8 |
*----------------------------------------------------------------------------------------
|
9 |
* Copyright (c) 2013 by Feaser http://www.feaser.com All rights reserved
|
10 |
*
|
11 |
*----------------------------------------------------------------------------------------
|
12 |
* L I C E N S E
|
13 |
*----------------------------------------------------------------------------------------
|
14 |
* This file is part of OpenBLT. OpenBLT is free software: you can redistribute it and/or
|
15 |
* modify it under the terms of the GNU General Public License as published by the Free
|
16 |
* Software Foundation, either version 3 of the License, or (at your option) any later
|
17 |
* version.
|
18 |
*
|
19 |
* OpenBLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
20 |
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
21 |
* PURPOSE. See the GNU General Public License for more details.
|
22 |
*
|
23 |
* You should have received a copy of the GNU General Public License along with OpenBLT.
|
24 |
* If not, see <http://www.gnu.org/licenses/>.
|
25 |
*
|
26 |
* A special exception to the GPL is included to allow you to distribute a combined work
|
27 |
* that includes OpenBLT without being obliged to provide the source code for any
|
28 |
* proprietary components. The exception text is included at the bottom of the license
|
29 |
* file <license.html>.
|
30 |
*
|
31 |
* \endinternal
|
32 |
****************************************************************************************/
|
33 |
|
34 |
/****************************************************************************************
|
35 |
* Include files
|
36 |
****************************************************************************************/
|
37 |
#include "boot.h" /* bootloader generic header */ |
38 |
#include "com.h" |
39 |
#include "ARMCM4_STM32/types.h" |
40 |
#include "AMiRo/amiroblt.h" |
41 |
#include "helper.h" |
42 |
#include "iodef.h" |
43 |
|
44 |
/****************************************************************************************
|
45 |
* Defines
|
46 |
****************************************************************************************/
|
47 |
#define HIBERNATE_TIME_MS 5000 |
48 |
|
49 |
/****************************************************************************************
|
50 |
* Function prototypes and static variables
|
51 |
****************************************************************************************/
|
52 |
static void Init(void); |
53 |
|
54 |
static void initGpio(); |
55 |
static void initExti(); |
56 |
void configGpioForShutdown();
|
57 |
void systemPowerDown();
|
58 |
|
59 |
ErrorStatus handleColdReset(); |
60 |
ErrorStatus handleSoftwareReset(); |
61 |
ErrorStatus handleUartDnWakeup(); |
62 |
ErrorStatus handlePathDcWakeup(); |
63 |
ErrorStatus handleTouchWakeup(); |
64 |
ErrorStatus handleIwdgWakeup(); |
65 |
|
66 |
static void indicateHibernate(); |
67 |
static void AdcSingleMeasurement(); |
68 |
|
69 |
ADC_TypeDef* setupADC(ADC_TypeDef* adc, const uint16_t low_th, const uint16_t high_th); |
70 |
uint16_t configIwdg(const uint16_t ms);
|
71 |
|
72 |
ErrorStatus shutdownDisambiguationProcedure(const uint8_t type);
|
73 |
void shutdownToTransportation();
|
74 |
void shutdownToDeepsleep();
|
75 |
void shutdownToHibernate();
|
76 |
void shutdownAndRestart();
|
77 |
|
78 |
volatile blBackupRegister_t backup_reg;
|
79 |
|
80 |
/****************************************************************************************
|
81 |
* Callback configuration
|
82 |
****************************************************************************************/
|
83 |
void blCallbackShutdownTransportation(void); |
84 |
void blCallbackShutdownDeepsleep(void); |
85 |
void blCallbackShutdownHibernate(void); |
86 |
void blCallbackShutdownRestart(void); |
87 |
void blCallbackHandleShutdownRequest(void); |
88 |
|
89 |
const blCallbackTable_t cbtable __attribute__ ((section ("_callback_table"))) = { |
90 |
.magicNumber = BL_MAGIC_NUMBER, |
91 |
.vBootloader = {BL_VERSION_ID_AMiRoBLT_Beta, BL_VERSION_MAJOR, BL_VERSION_MINOR, 3},
|
92 |
.vSSSP = {BL_VERSION_ID_SSSP, BL_SSSP_VERSION_MAJOR, BL_SSSP_VERSION_MINOR, 0},
|
93 |
.vCompiler = {BL_VERSION_ID_GCC, __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__}, // currently only GCC is supported
|
94 |
.cbShutdownHibernate = blCallbackShutdownHibernate, |
95 |
.cbShutdownDeepsleep = blCallbackShutdownDeepsleep, |
96 |
.cbShutdownTransportation = blCallbackShutdownTransportation, |
97 |
.cbShutdownRestart = blCallbackShutdownRestart, |
98 |
.cbHandleShutdownRequest = blCallbackHandleShutdownRequest, |
99 |
.cb5 = (void*)0, |
100 |
.cb6 = (void*)0, |
101 |
.cb7 = (void*)0, |
102 |
.cb8 = (void*)0, |
103 |
.cb9 = (void*)0, |
104 |
.cb10 = (void*)0, |
105 |
.cb11 = (void*)0 |
106 |
}; |
107 |
|
108 |
/************************************************************************************//** |
109 |
** \brief This is the entry point for the bootloader application and is called
|
110 |
** by the reset interrupt vector after the C-startup routines executed.
|
111 |
** \return none.
|
112 |
**
|
113 |
****************************************************************************************/
|
114 |
void main(void) |
115 |
{ |
116 |
/* initialize the microcontroller */
|
117 |
Init(); |
118 |
|
119 |
/* activate some required clocks */
|
120 |
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE); |
121 |
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); |
122 |
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); |
123 |
|
124 |
/* initialize GPIOs and EXTI lines */
|
125 |
initGpio(); |
126 |
setLed(BLT_TRUE); |
127 |
initExti(); |
128 |
|
129 |
/* initialize the timer */
|
130 |
TimerInit(); // do not use saTimerInit() in order to initialize the static variable.
|
131 |
|
132 |
/* read the backup register */
|
133 |
backup_reg.raw = RTC_ReadBackupRegister(BL_RTC_BACKUP_REG); |
134 |
|
135 |
/* detect the primary reason for this wakeup/restart */
|
136 |
backup_reg.wakeup_pri_reason = |
137 |
((RCC_GetFlagStatus(RCC_FLAG_LPWRRST) == SET) ? BL_WAKEUP_PRI_RSN_LPWRRST : 0) |
|
138 |
((RCC_GetFlagStatus(RCC_FLAG_WWDGRST) == SET) ? BL_WAKEUP_PRI_RSN_WWDGRST : 0) |
|
139 |
((RCC_GetFlagStatus(RCC_FLAG_IWDGRST) == SET) ? BL_WAKEUP_PRI_RSN_IWDGRST : 0) |
|
140 |
((RCC_GetFlagStatus(RCC_FLAG_SFTRST) == SET) ? BL_WAKEUP_PRI_RSN_SFTRST : 0) |
|
141 |
((RCC_GetFlagStatus(RCC_FLAG_PORRST) == SET) ? BL_WAKEUP_PRI_RSN_PORRST : 0) |
|
142 |
((RCC_GetFlagStatus(RCC_FLAG_PINRST) == SET) ? BL_WAKEUP_PRI_RSN_PINRST : 0) |
|
143 |
((RCC_GetFlagStatus(RCC_FLAG_BORRST) == SET) ? BL_WAKEUP_PRI_RSN_BORRST : 0) |
|
144 |
((PWR_GetFlagStatus(PWR_FLAG_WU) == SET) ? BL_WAKEUP_PRI_RSN_WKUP : 0);
|
145 |
|
146 |
/* when woken from standby mode, detect the secondary reason for this wakeup/reset */
|
147 |
if ( (backup_reg.wakeup_pri_reason & BL_WAKEUP_PRI_RSN_WKUP) && (PWR_GetFlagStatus(PWR_FLAG_SB) == SET) ) {
|
148 |
if (GPIO_ReadInputDataBit(SYS_UART_DN_GPIO, SYS_UART_DN_PIN) == Bit_RESET) {
|
149 |
backup_reg.wakeup_sec_reason = BL_WAKEUP_SEC_RSN_UART; |
150 |
} else if (GPIO_ReadInputDataBit(PATH_DC_GPIO, PATH_DC_PIN) == Bit_SET) { |
151 |
backup_reg.wakeup_sec_reason = BL_WAKEUP_SEC_RSN_PWRPLUG; |
152 |
} else {
|
153 |
backup_reg.wakeup_sec_reason = BL_WAKEUP_SEC_RSN_TOUCH; |
154 |
} |
155 |
} else {
|
156 |
backup_reg.wakeup_sec_reason = BL_WAKEUP_SEC_RSN_UNKNOWN; |
157 |
} |
158 |
|
159 |
/* store the information about this wakeup/restart in the backup register */
|
160 |
PWR_BackupAccessCmd(ENABLE); |
161 |
RTC_WriteBackupRegister(BL_RTC_BACKUP_REG, backup_reg.raw); |
162 |
|
163 |
/* clear the flags */
|
164 |
RCC_ClearFlag(); |
165 |
PWR_ClearFlag(PWR_FLAG_WU); |
166 |
|
167 |
setLed(BLT_FALSE); |
168 |
|
169 |
/* handle different wakeup/reset reasons */
|
170 |
ErrorStatus status = ERROR; |
171 |
if (backup_reg.wakeup_pri_reason & BL_WAKEUP_PRI_RSN_SFTRST) {
|
172 |
/* system was reset by software */
|
173 |
status = handleSoftwareReset(); |
174 |
} else if (backup_reg.wakeup_pri_reason & BL_WAKEUP_PRI_RSN_WKUP) { |
175 |
/* system was woken via WKUP pin */
|
176 |
/* differeciate between thre wakeup types */
|
177 |
switch (backup_reg.wakeup_sec_reason) {
|
178 |
case BL_WAKEUP_SEC_RSN_UART:
|
179 |
status = handleUartDnWakeup(); |
180 |
break;
|
181 |
case BL_WAKEUP_SEC_RSN_PWRPLUG:
|
182 |
status = handlePathDcWakeup(); |
183 |
break;
|
184 |
case BL_WAKEUP_SEC_RSN_TOUCH:
|
185 |
status = handleTouchWakeup(); |
186 |
break;
|
187 |
default:
|
188 |
status = ERROR; |
189 |
break;
|
190 |
} |
191 |
} else if (backup_reg.wakeup_pri_reason & BL_WAKEUP_PRI_RSN_IWDGRST) { |
192 |
/* system was woken by IWDG */
|
193 |
status = handleIwdgWakeup(); |
194 |
} else if (backup_reg.wakeup_pri_reason == BL_WAKEUP_PRI_RSN_PINRST) { |
195 |
/* system was reset via NRST pin */
|
196 |
status = handleColdReset(); |
197 |
} else {
|
198 |
/* system was woken/reset for an unexpected reason.
|
199 |
* In this case the LED blinks "SOS" (... --- ...) and the system resets.
|
200 |
*/
|
201 |
blinkSOS(1);
|
202 |
status = ERROR; |
203 |
backup_reg.shutdown_pri_reason = BL_SHUTDOWN_PRI_RSN_RESTART; |
204 |
backup_reg.shutdown_sec_reason = BL_SHUTDOWN_SEC_RSN_UNKNOWN; |
205 |
RTC_WriteBackupRegister(BL_RTC_BACKUP_REG, backup_reg.raw); |
206 |
NVIC_SystemReset(); |
207 |
} |
208 |
|
209 |
/* if something went wrong, signal this failure */
|
210 |
if (status != SUCCESS) {
|
211 |
blinkSOSinf(); |
212 |
} |
213 |
|
214 |
return;
|
215 |
} /*** end of main ***/
|
216 |
|
217 |
|
218 |
/************************************************************************************//** |
219 |
** \brief Initializes the microcontroller.
|
220 |
** \return none.
|
221 |
**
|
222 |
****************************************************************************************/
|
223 |
static void Init(void) |
224 |
{ |
225 |
#if (BOOT_COM_UART_ENABLE > 0 || BOOT_GATE_UART_ENABLE > 0) |
226 |
GPIO_InitTypeDef GPIO_InitStructure; |
227 |
#elif (BOOT_FILE_SYS_ENABLE > 0) |
228 |
GPIO_InitTypeDef GPIO_InitStructure; |
229 |
USART_InitTypeDef USART_InitStructure; |
230 |
#elif (BOOT_COM_CAN_ENABLE > 0 || BOOT_GATE_CAN_ENABLE > 0) |
231 |
GPIO_InitTypeDef GPIO_InitStructure; |
232 |
#endif
|
233 |
|
234 |
/* initialize the system and its clocks */
|
235 |
SystemInit(); |
236 |
#if (BOOT_COM_UART_ENABLE > 0 || BOOT_GATE_UART_ENABLE > 0) |
237 |
/* enable UART peripheral clock */
|
238 |
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); |
239 |
/* enable GPIO peripheral clock for transmitter and receiver pins */
|
240 |
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); |
241 |
/* connect the pin to the peripherals alternate function */
|
242 |
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); |
243 |
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); |
244 |
/* configure USART Tx as alternate function */
|
245 |
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; |
246 |
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; |
247 |
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; |
248 |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; |
249 |
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
250 |
GPIO_Init(GPIOA, &GPIO_InitStructure); |
251 |
/* configure USART Rx as alternate function */
|
252 |
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; |
253 |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; |
254 |
GPIO_Init(GPIOA, &GPIO_InitStructure); |
255 |
#endif
|
256 |
|
257 |
#if (BOOT_COM_BLUETOOTH_UART_ENABLE > 0) |
258 |
/* enable UART peripheral clock */
|
259 |
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); |
260 |
|
261 |
/* enable GPIO peripheral clock for transmitter and receiver pins */
|
262 |
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); |
263 |
/* connect the pin to the peripherals alternate function */
|
264 |
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3); |
265 |
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3); |
266 |
/* configure USART Tx as alternate function */
|
267 |
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; |
268 |
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; |
269 |
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; |
270 |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; |
271 |
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
272 |
GPIO_Init(GPIOC, &GPIO_InitStructure); |
273 |
/* configure USART Rx as alternate function */
|
274 |
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; |
275 |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; |
276 |
GPIO_Init(GPIOC, &GPIO_InitStructure); |
277 |
|
278 |
/* Configure Bluetooth reset pin */
|
279 |
GPIO_InitTypeDef gpio_init; |
280 |
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); |
281 |
gpio_init.GPIO_Pin = BT_RST_PIN; |
282 |
gpio_init.GPIO_OType = GPIO_OType_OD; |
283 |
gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
284 |
gpio_init.GPIO_Mode = GPIO_Mode_OUT; |
285 |
gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
286 |
GPIO_Init(BT_RST_GPIO, &gpio_init); |
287 |
/* Reset Bluetooth reset pin */
|
288 |
GPIO_ResetBits(BT_RST_GPIO, BT_RST_PIN); |
289 |
#endif
|
290 |
|
291 |
|
292 |
#if (BOOT_COM_CAN_ENABLE > 0 || BOOT_GATE_CAN_ENABLE > 0) |
293 |
/* enable clocks for CAN transmitter and receiver pins */
|
294 |
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); |
295 |
/* select alternate function for the CAN pins */
|
296 |
GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_CAN1); |
297 |
GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_CAN1); |
298 |
/* configure CAN RX and TX pins */
|
299 |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; |
300 |
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
301 |
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; |
302 |
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; |
303 |
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; |
304 |
GPIO_Init(GPIOA, &GPIO_InitStructure); |
305 |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; |
306 |
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; |
307 |
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
308 |
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; |
309 |
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; |
310 |
GPIO_Init(GPIOA, &GPIO_InitStructure); |
311 |
#endif
|
312 |
} /*** end of Init ***/
|
313 |
|
314 |
/*
|
315 |
* Initializes all GPIO used by the bootloader
|
316 |
*/
|
317 |
static void initGpio() { |
318 |
GPIO_InitTypeDef gpio_init; |
319 |
|
320 |
/*
|
321 |
* OUTPUTS
|
322 |
*/
|
323 |
|
324 |
/* initialize LED and push it up (inactive) */
|
325 |
GPIO_SetBits(LED_GPIO, LED_PIN); |
326 |
gpio_init.GPIO_Pin = LED_PIN; |
327 |
gpio_init.GPIO_Mode = GPIO_Mode_OUT; |
328 |
gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
329 |
gpio_init.GPIO_OType = GPIO_OType_PP; |
330 |
gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
331 |
GPIO_Init(LED_GPIO, &gpio_init); |
332 |
|
333 |
/* initialize SYS_PD_N and push it up (inactive) */
|
334 |
GPIO_SetBits(SYS_PD_N_GPIO, SYS_PD_N_PIN); |
335 |
gpio_init.GPIO_Pin = SYS_PD_N_PIN; |
336 |
gpio_init.GPIO_Mode = GPIO_Mode_OUT; |
337 |
gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
338 |
gpio_init.GPIO_OType = GPIO_OType_OD; |
339 |
gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
340 |
GPIO_Init(SYS_PD_N_GPIO, &gpio_init); |
341 |
|
342 |
/* initialize SYS_SYNC_N and pull it down (active) */
|
343 |
GPIO_ResetBits(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN); |
344 |
gpio_init.GPIO_Pin = SYS_SYNC_N_PIN; |
345 |
gpio_init.GPIO_Mode = GPIO_Mode_OUT; |
346 |
gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
347 |
gpio_init.GPIO_OType = GPIO_OType_OD; |
348 |
gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
349 |
GPIO_Init(SYS_SYNC_N_GPIO, &gpio_init); |
350 |
|
351 |
/* initialize SYS_WARMRST_N and pull it down (active) */
|
352 |
GPIO_ResetBits(SYS_WARMRST_N_GPIO, SYS_WARMRST_N_PIN); |
353 |
gpio_init.GPIO_Pin = SYS_WARMRST_N_PIN; |
354 |
gpio_init.GPIO_Mode = GPIO_Mode_OUT; |
355 |
gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
356 |
gpio_init.GPIO_OType = GPIO_OType_OD; |
357 |
gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
358 |
GPIO_Init(SYS_WARMRST_N_GPIO, &gpio_init); |
359 |
|
360 |
/* initialize SYS_UART_DN and push it up (inactive) */
|
361 |
GPIO_SetBits(SYS_UART_DN_GPIO, SYS_UART_DN_PIN); |
362 |
gpio_init.GPIO_Pin = SYS_UART_DN_PIN; |
363 |
gpio_init.GPIO_Mode = GPIO_Mode_OUT; |
364 |
gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
365 |
gpio_init.GPIO_OType = GPIO_OType_OD; |
366 |
gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
367 |
GPIO_Init(SYS_UART_DN_GPIO, &gpio_init); |
368 |
|
369 |
/* initialize POWER_EN and pull it down (inactive) */
|
370 |
GPIO_ResetBits(POWER_EN_GPIO, POWER_EN_PIN); |
371 |
gpio_init.GPIO_Pin = POWER_EN_PIN; |
372 |
gpio_init.GPIO_Mode = GPIO_Mode_OUT; |
373 |
gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
374 |
gpio_init.GPIO_OType = GPIO_OType_PP; |
375 |
gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
376 |
GPIO_Init(POWER_EN_GPIO, &gpio_init); |
377 |
|
378 |
/* initialize SYS_REG_EN and pull it down (inactive) */
|
379 |
GPIO_ResetBits(SYS_REG_EN_GPIO, SYS_REG_EN_PIN); |
380 |
gpio_init.GPIO_Pin = SYS_REG_EN_PIN; |
381 |
gpio_init.GPIO_Mode = GPIO_Mode_OUT; |
382 |
gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
383 |
gpio_init.GPIO_OType = GPIO_OType_PP; |
384 |
gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
385 |
GPIO_Init(SYS_REG_EN_GPIO, &gpio_init); |
386 |
|
387 |
/* initialize CHARGE_EN1_N and CHARGE_EN2_N and push them up (inactive) */
|
388 |
GPIO_SetBits(CHARGE_EN1_N_GPIO, CHARGE_EN1_N_PIN); |
389 |
GPIO_SetBits(CHARGE_EN2_N_GPIO, CHARGE_EN2_N_PIN); |
390 |
gpio_init.GPIO_Pin = CHARGE_EN1_N_PIN; |
391 |
gpio_init.GPIO_Mode = GPIO_Mode_OUT; |
392 |
gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
393 |
gpio_init.GPIO_OType = GPIO_OType_PP; |
394 |
gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
395 |
GPIO_Init(CHARGE_EN1_N_GPIO, &gpio_init); |
396 |
gpio_init.GPIO_Pin = CHARGE_EN2_N_PIN; |
397 |
GPIO_Init(CHARGE_EN2_N_GPIO, &gpio_init); |
398 |
|
399 |
/*
|
400 |
* INPUTS
|
401 |
*/
|
402 |
|
403 |
/* initialize SWITCH_STATUS_N */
|
404 |
gpio_init.GPIO_Pin = SWITCH_STATUS_N_PIN; |
405 |
gpio_init.GPIO_Mode = GPIO_Mode_IN; |
406 |
gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
407 |
gpio_init.GPIO_OType = GPIO_OType_PP; |
408 |
gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
409 |
GPIO_Init(SWITCH_STATUS_N_GPIO, &gpio_init); |
410 |
|
411 |
/* initialize PATH_DC */
|
412 |
gpio_init.GPIO_Pin = PATH_DC_PIN; |
413 |
gpio_init.GPIO_Mode = GPIO_Mode_IN; |
414 |
gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
415 |
gpio_init.GPIO_OType = GPIO_OType_PP; |
416 |
gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
417 |
GPIO_Init(PATH_DC_GPIO, &gpio_init); |
418 |
|
419 |
/* initialize TOUCH_INT_N */
|
420 |
gpio_init.GPIO_Pin = TOUCH_INT_N_PIN; |
421 |
gpio_init.GPIO_Mode = GPIO_Mode_IN; |
422 |
gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
423 |
gpio_init.GPIO_OType = GPIO_OType_PP; |
424 |
gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
425 |
GPIO_Init(TOUCH_INT_N_GPIO, &gpio_init); |
426 |
|
427 |
/* initialize VSYS_SENSE as analog input */
|
428 |
gpio_init.GPIO_Pin = VSYS_SENSE_PIN; |
429 |
gpio_init.GPIO_Mode = GPIO_Mode_AN; |
430 |
gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
431 |
gpio_init.GPIO_OType = GPIO_OType_PP; |
432 |
gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
433 |
GPIO_Init(VSYS_SENSE_GPIO, &gpio_init); |
434 |
|
435 |
/* initialize GPIOB4, since it is configured in alternate function mode on reset */
|
436 |
gpio_init.GPIO_Pin = CHARGE_STAT2A_PIN; |
437 |
gpio_init.GPIO_Mode = GPIO_Mode_IN; |
438 |
gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
439 |
gpio_init.GPIO_OType = GPIO_OType_PP; |
440 |
gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
441 |
GPIO_Init(CHARGE_STAT2A_GPIO, &gpio_init); |
442 |
|
443 |
return;
|
444 |
} /*** end of initGpio ***/
|
445 |
|
446 |
/*
|
447 |
* Initialize all EXTI lines
|
448 |
*/
|
449 |
static void initExti() { |
450 |
/* configure EXTI lines */
|
451 |
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource0); // IR_INT1_N
|
452 |
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource0); // CHARGE_STAT1A
|
453 |
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource1); // GAUGE_BATLOW1
|
454 |
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource2); // GAUGE_BATGD1_N
|
455 |
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource3); // SYS_UART_DN
|
456 |
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource4); // CHARGE_STAT2A
|
457 |
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource4); // IR_INT2_N
|
458 |
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource5); // TOUCH_INT_N
|
459 |
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource6); // GAUGE_BATLOW2
|
460 |
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource7); // GAUGE_BATGD2_N
|
461 |
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource8); // PATH_DC
|
462 |
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource9); // SYS_SPI_DIR
|
463 |
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource12); // SYS_SYNC_N
|
464 |
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource13); // SYS_PD_N
|
465 |
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource14); // SYS_WARMRST_N
|
466 |
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource15); // SYS_UART_UP
|
467 |
|
468 |
return;
|
469 |
} /*** end of initExti ***/
|
470 |
|
471 |
/*
|
472 |
* Signals, which type of low-power mode the system shall enter after the shutdown sequence.
|
473 |
*/
|
474 |
ErrorStatus shutdownDisambiguationProcedure(const uint8_t type) {
|
475 |
GPIO_SetBits(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN); |
476 |
ErrorStatus ret_val = ERROR; |
477 |
|
478 |
switch (type) {
|
479 |
case BL_SHUTDOWN_PRI_RSN_UNKNOWN:
|
480 |
case BL_SHUTDOWN_PRI_RSN_HIBERNATE:
|
481 |
case BL_SHUTDOWN_PRI_RSN_DEEPSLEEP:
|
482 |
case BL_SHUTDOWN_PRI_RSN_TRANSPORT:
|
483 |
{ |
484 |
// broadcast a number of pulses, depending on the argument
|
485 |
uint8_t pulse_counter = 0;
|
486 |
for (pulse_counter = 0; pulse_counter < type; ++pulse_counter) { |
487 |
msleep(1);
|
488 |
GPIO_ResetBits(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN); |
489 |
msleep(1);
|
490 |
GPIO_SetBits(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN); |
491 |
} |
492 |
// wait for timeout
|
493 |
msleep(10);
|
494 |
ret_val = SUCCESS; |
495 |
break;
|
496 |
} |
497 |
case BL_SHUTDOWN_PRI_RSN_RESTART:
|
498 |
{ |
499 |
// since there is no ambiguity for restart requests, no pulses are generated
|
500 |
msleep(10);
|
501 |
ret_val = SUCCESS; |
502 |
break;
|
503 |
} |
504 |
default:
|
505 |
ret_val = ERROR; |
506 |
break;
|
507 |
} |
508 |
|
509 |
return ret_val;
|
510 |
} /*** end of shutdownDisambiguationProcedure ***/
|
511 |
|
512 |
/*
|
513 |
* Final shutdown of the system to enter transportation mode.
|
514 |
*/
|
515 |
void shutdownToTransportation() {
|
516 |
/* configure some criticpal GPIOs as input
|
517 |
* This is required, because otherwise some hardware might be powered through these signals */
|
518 |
configGpioForShutdown(); |
519 |
|
520 |
/* power down the system */
|
521 |
systemPowerDown(); |
522 |
|
523 |
/* deactivate the WKUP pin */
|
524 |
PWR_WakeUpPinCmd(DISABLE); |
525 |
|
526 |
/* deactivate any RTC related events */
|
527 |
RTC_WakeUpCmd(DISABLE); |
528 |
RTC_TamperCmd(RTC_Tamper_1, DISABLE); |
529 |
RTC_TimeStampCmd(RTC_TimeStampEdge_Rising, DISABLE); |
530 |
RTC_TimeStampCmd(RTC_TimeStampEdge_Falling, DISABLE); |
531 |
RTC_ClearFlag(~0);
|
532 |
|
533 |
/* disable the IWDG */
|
534 |
IWDG_ReloadCounter(); |
535 |
|
536 |
/* write some information to the backup register */
|
537 |
blBackupRegister_t backup; |
538 |
backup.shutdown_pri_reason = BL_SHUTDOWN_PRI_RSN_TRANSPORT; |
539 |
backup.shutdown_sec_reason = BL_SHUTDOWN_SEC_RSN_UNKNOWN; |
540 |
backup.wakeup_pri_reason = BL_WAKEUP_PRI_RSN_UNKNOWN; |
541 |
backup.wakeup_sec_reason = BL_WAKEUP_SEC_RSN_UNKNOWN; |
542 |
PWR_BackupAccessCmd(ENABLE); |
543 |
RTC_WriteBackupRegister(BL_RTC_BACKUP_REG, backup.raw); |
544 |
|
545 |
/* morse 'OK' via the LED to signal that shutdown was successful */
|
546 |
blinkOK(1);
|
547 |
|
548 |
/* enter standby mode */
|
549 |
PWR_EnterSTANDBYMode(); |
550 |
|
551 |
return;
|
552 |
} /*** end of shutdownToTransportation ***/
|
553 |
|
554 |
/*
|
555 |
* Final shutdown of the system to enter deepsleep mode.
|
556 |
*/
|
557 |
void shutdownToDeepsleep() {
|
558 |
/* configure some criticpal GPIOs as input
|
559 |
* This is required, because otherwise some hardware might be powered through these signals */
|
560 |
configGpioForShutdown(); |
561 |
|
562 |
/* power down the system */
|
563 |
systemPowerDown(); |
564 |
|
565 |
/* activate the WKUP pin */
|
566 |
PWR_WakeUpPinCmd(ENABLE); |
567 |
|
568 |
/*
|
569 |
* Configuration of RTC and IWDG belongs to the OS.
|
570 |
*/
|
571 |
|
572 |
/* write some information to the backup register */
|
573 |
blBackupRegister_t backup; |
574 |
backup.shutdown_pri_reason = BL_SHUTDOWN_PRI_RSN_DEEPSLEEP; |
575 |
backup.shutdown_sec_reason = BL_SHUTDOWN_SEC_RSN_UNKNOWN; |
576 |
backup.wakeup_pri_reason = BL_WAKEUP_PRI_RSN_UNKNOWN; |
577 |
backup.wakeup_sec_reason = BL_WAKEUP_SEC_RSN_UNKNOWN; |
578 |
PWR_BackupAccessCmd(ENABLE); |
579 |
RTC_WriteBackupRegister(BL_RTC_BACKUP_REG, backup.raw); |
580 |
|
581 |
/* morse 'OK' via the LED to signal that shutdown was successful */
|
582 |
blinkOK(1);
|
583 |
|
584 |
/* enter standby mode or restart the system in case a power plug is already present */
|
585 |
if (GPIO_ReadInputDataBit(PATH_DC_GPIO, PATH_DC_PIN) != Bit_SET) {
|
586 |
PWR_EnterSTANDBYMode(); |
587 |
} else {
|
588 |
NVIC_SystemReset(); |
589 |
} |
590 |
|
591 |
return;
|
592 |
} /*** end of shutdownToDeepsleep ***/
|
593 |
|
594 |
/*
|
595 |
* Final shutdown of the system to enter hibernate mode.
|
596 |
*/
|
597 |
void shutdownToHibernate() {
|
598 |
/* configure some criticpal GPIOs as input
|
599 |
* This is required, because otherwise some hardware might be powered through these signals */
|
600 |
configGpioForShutdown(); |
601 |
|
602 |
/* power down the system */
|
603 |
systemPowerDown(); |
604 |
|
605 |
/* write some information to the backup register */
|
606 |
blBackupRegister_t backup; |
607 |
backup.shutdown_pri_reason = BL_SHUTDOWN_PRI_RSN_HIBERNATE; |
608 |
backup.shutdown_sec_reason = BL_SHUTDOWN_SEC_RSN_UNKNOWN; |
609 |
backup.wakeup_pri_reason = BL_WAKEUP_PRI_RSN_UNKNOWN; |
610 |
backup.wakeup_sec_reason = BL_WAKEUP_SEC_RSN_UNKNOWN; |
611 |
PWR_BackupAccessCmd(ENABLE); |
612 |
RTC_WriteBackupRegister(BL_RTC_BACKUP_REG, backup.raw); |
613 |
|
614 |
/* morse 'OK' via the LED to signal that shutdown was successful */
|
615 |
blinkOK(1);
|
616 |
|
617 |
/* reset the MCU */
|
618 |
NVIC_SystemReset(); |
619 |
|
620 |
return;
|
621 |
} /*** end of shutdownToHibernate ***/
|
622 |
|
623 |
/*
|
624 |
* Final shutdown of the system and restart.
|
625 |
*/
|
626 |
void shutdownAndRestart() {
|
627 |
/* configure some criticpal GPIOs as input
|
628 |
* This is required, because otherwise some hardware might be powered through these signals */
|
629 |
configGpioForShutdown(); |
630 |
|
631 |
/* power down the system */
|
632 |
systemPowerDown(); |
633 |
|
634 |
/* write some information to the backup register */
|
635 |
blBackupRegister_t backup; |
636 |
backup.shutdown_pri_reason = BL_SHUTDOWN_PRI_RSN_RESTART; |
637 |
backup.shutdown_sec_reason = BL_SHUTDOWN_SEC_RSN_UNKNOWN; |
638 |
backup.wakeup_pri_reason = BL_WAKEUP_PRI_RSN_UNKNOWN; |
639 |
backup.wakeup_sec_reason = BL_WAKEUP_SEC_RSN_UNKNOWN; |
640 |
PWR_BackupAccessCmd(ENABLE); |
641 |
RTC_WriteBackupRegister(BL_RTC_BACKUP_REG, backup.raw); |
642 |
|
643 |
/* morse 'OK' via the LED to signal that shutdown was successful */
|
644 |
blinkOK(1);
|
645 |
|
646 |
/* reset the MCU */
|
647 |
NVIC_SystemReset(); |
648 |
|
649 |
return;
|
650 |
} /*** end of shutdownAndRestart ***/
|
651 |
|
652 |
/*
|
653 |
* Configures some GPIO pins as inputs for safety reasons.
|
654 |
* Under certain circumstances, these pins might power hardware that is supposed to be shut down.
|
655 |
*/
|
656 |
void configGpioForShutdown() {
|
657 |
/* setup the configuration */
|
658 |
GPIO_InitTypeDef gpio_init; |
659 |
gpio_init.GPIO_Mode = GPIO_Mode_IN; |
660 |
gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
661 |
gpio_init.GPIO_OType = GPIO_OType_PP; |
662 |
gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
663 |
|
664 |
/* configure SYS_UART_TX */
|
665 |
gpio_init.GPIO_Pin = SYS_UART_TX_PIN; |
666 |
GPIO_Init(SYS_UART_TX_GPIO, &gpio_init); |
667 |
|
668 |
/* configure all SYS_SPI signals */
|
669 |
gpio_init.GPIO_Pin = SYS_SPI_SS0_N_PIN; |
670 |
GPIO_Init(SYS_SPI_SS0_N_GPIO, &gpio_init); |
671 |
gpio_init.GPIO_Pin = SYS_SPI_SCLK_PIN; |
672 |
GPIO_Init(SYS_SPI_SCLK_GPIO, &gpio_init); |
673 |
gpio_init.GPIO_Pin = SYS_SPI_MISO_PIN; |
674 |
GPIO_Init(SYS_SPI_MISO_GPIO, &gpio_init); |
675 |
gpio_init.GPIO_Pin = SYS_SPI_MOSI_PIN; |
676 |
GPIO_Init(SYS_SPI_MOSI_GPIO, &gpio_init); |
677 |
gpio_init.GPIO_Pin = SYS_SPI_SS1_N_PIN; |
678 |
GPIO_Init(SYS_SPI_SS1_N_GPIO, &gpio_init); |
679 |
gpio_init.GPIO_Pin = SYS_SPI_DIR_PIN; |
680 |
GPIO_Init(SYS_SPI_DIR_GPIO, &gpio_init); |
681 |
|
682 |
/* configure CAN_TX */
|
683 |
gpio_init.GPIO_Pin = CAN_TX_PIN; |
684 |
GPIO_Init(CAN_TX_GPIO, &gpio_init); |
685 |
|
686 |
/* configure all Bluetooth signals */
|
687 |
gpio_init.GPIO_Pin = BT_CTS_PIN; |
688 |
GPIO_Init(BT_CTS_GPIO, &gpio_init); |
689 |
gpio_init.GPIO_Pin = BT_RX_PIN; |
690 |
GPIO_Init(BT_RX_GPIO, &gpio_init); |
691 |
|
692 |
return;
|
693 |
} /*** end of configGpioForShutdown ***/
|
694 |
|
695 |
/*
|
696 |
* Disables all regulated voltages and finally cuts power to the rest of the system.
|
697 |
*/
|
698 |
void systemPowerDown() {
|
699 |
setLed(BLT_TRUE); |
700 |
|
701 |
/* make sure that all other modules are shut down */
|
702 |
msleep(10);
|
703 |
|
704 |
/* reset slave modules */
|
705 |
GPIO_ResetBits(SYS_WARMRST_N_GPIO, SYS_WARMRST_N_PIN); |
706 |
|
707 |
/* disable voltage regulators */
|
708 |
GPIO_ResetBits(SYS_REG_EN_GPIO, SYS_REG_EN_PIN); |
709 |
|
710 |
/* cut power */
|
711 |
GPIO_ResetBits(POWER_EN_GPIO, POWER_EN_PIN); |
712 |
|
713 |
/* make sure, all capacitors are discharged */
|
714 |
msleep(100);
|
715 |
|
716 |
setLed(BLT_FALSE); |
717 |
|
718 |
return;
|
719 |
} /*** end of systemPowerDown ***/
|
720 |
|
721 |
/*
|
722 |
* Cofigures the independent watchdog (IWDG) to fire after the specified time when it is enabled.
|
723 |
* The argument is the requested time in milliseconds.
|
724 |
* The time that was actually set for the IWDG is returned by the function (again in milliseconds).
|
725 |
* In some cases the returned value might differ from the requested one, but if so, it will alwyas be smaller.
|
726 |
* Although the IWDG provides higher resolutions than milliseconds, these are not supported by this function.
|
727 |
*/
|
728 |
uint16_t configIwdg(const uint16_t ms) {
|
729 |
/* apply an upper bound to the ms argument */
|
730 |
uint16_t ms_capped = (ms >= 0x8000) ? 0x7FFF : ms; |
731 |
|
732 |
/* detect the best fitting prescaler and compute the according reload value */
|
733 |
uint8_t prescaler = 0;
|
734 |
uint16_t reload_val = 0;
|
735 |
if (ms_capped >= 0x4000) { |
736 |
prescaler = IWDG_Prescaler_256; |
737 |
reload_val = ms_capped >> 3; // note: this corresponds to a floor function |
738 |
ms_capped = reload_val << 3; // this applies the floor function to ms_capped |
739 |
} else if (ms_capped >= 0x2000) { |
740 |
prescaler = IWDG_Prescaler_128; |
741 |
reload_val = ms_capped >> 2; // note: this corresponds to a floor function |
742 |
ms_capped = reload_val << 2; // this applies the floor function to ms_capped |
743 |
} else if (ms_capped >= 0x1000) { |
744 |
ms_capped &= ~(0x0001);
|
745 |
prescaler = IWDG_Prescaler_64; |
746 |
reload_val = ms_capped >> 1; // note: this corresponds to a floor function |
747 |
ms_capped = reload_val << 1; // this applies the floor function to ms_capped |
748 |
} else {
|
749 |
prescaler = IWDG_Prescaler_32; |
750 |
reload_val = ms_capped; |
751 |
} |
752 |
|
753 |
/* configure the IWDG */
|
754 |
if (reload_val > 0) { |
755 |
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); |
756 |
IWDG_SetPrescaler(prescaler); |
757 |
IWDG_SetReload(reload_val); |
758 |
IWDG_WriteAccessCmd(IWDG_WriteAccess_Disable); |
759 |
} |
760 |
|
761 |
return ms_capped;
|
762 |
} /*** end of configIWDG ***/
|
763 |
|
764 |
/*
|
765 |
* System was reset via the NRST pin or the reason could not be detected.
|
766 |
* In this case, everything is started up.
|
767 |
* If an attempt for an OS update is detected, flashing mode is entered.
|
768 |
* Otherwise, the system will boot the OS.
|
769 |
*/
|
770 |
ErrorStatus handleColdReset() { |
771 |
/* activate system power and wait some time to ensure stable voltages */
|
772 |
setLed(BLT_TRUE); |
773 |
GPIO_SetBits(POWER_EN_GPIO, POWER_EN_PIN); |
774 |
msleep(10);
|
775 |
GPIO_SetBits(SYS_REG_EN_GPIO, SYS_REG_EN_PIN); |
776 |
msleep(10);
|
777 |
setLed(BLT_FALSE); |
778 |
|
779 |
/* drive SYS_WARMRST_N high (inactive) */
|
780 |
GPIO_SetBits(SYS_WARMRST_N_GPIO, SYS_WARMRST_N_PIN); |
781 |
|
782 |
/* enable CAN clock
|
783 |
* Note that CAN1 shares reception filters with CAN1 so for CAN2 the CAN1 peripheral also needs to be enabled. */
|
784 |
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2 | RCC_APB1Periph_CAN1, ENABLE); |
785 |
|
786 |
/* wait 1ms to make sure that all modules are running and started the bootloader */
|
787 |
msleep(1);
|
788 |
|
789 |
/* initialize the bootloader */
|
790 |
BootInit(); |
791 |
|
792 |
/* start the infinite program loop */
|
793 |
uint32_t loopStartTime = 0;
|
794 |
saTimerUpdate(&loopStartTime); |
795 |
uint32_t currentTime = loopStartTime; |
796 |
while (1) |
797 |
{ |
798 |
// /* make the LED "double-blink" */
|
799 |
// saTimerUpdate(¤tTime);
|
800 |
// if (currentTime < loopStartTime + 50) {
|
801 |
// setLed(BLT_TRUE);
|
802 |
// } else if (currentTime < loopStartTime + 50+100) {
|
803 |
// setLed(BLT_FALSE);
|
804 |
// } else if (currentTime < loopStartTime + 50+100+50) {
|
805 |
// setLed(BLT_TRUE);
|
806 |
// } else if (currentTime < loopStartTime + 50+100+50+300) {
|
807 |
// setLed(BLT_FALSE);
|
808 |
// } else {
|
809 |
// loopStartTime = currentTime;
|
810 |
// }
|
811 |
|
812 |
/* run the bootloader task */
|
813 |
BootTask(); |
814 |
|
815 |
/* check the SYS_PD_N signal */
|
816 |
if (GPIO_ReadInputDataBit(SYS_PD_N_GPIO, SYS_PD_N_PIN) == Bit_RESET) {
|
817 |
blCallbackHandleShutdownRequest(); |
818 |
return SUCCESS;
|
819 |
} |
820 |
} |
821 |
|
822 |
return ERROR;
|
823 |
} /*** end of handleColdReset ***/
|
824 |
|
825 |
/*
|
826 |
* System was reset by software.
|
827 |
* Depending on the argument, which was read from the 1st backup register (see main function) the effect of this function differs.
|
828 |
* There are three cases that can occur:
|
829 |
* - The system was reset to enter hibernate mode.
|
830 |
* In this case the system will enter a medium power saving mode (hibernate mode), but can be charged via the charging pins.
|
831 |
* The system can be woken up in the same way as in deepsleep mode (cf. blCallbackShutdownDeepsleep() function).
|
832 |
* - The system was reset to reboot.
|
833 |
* In this case the system will restart in the same way as after a cold reset.
|
834 |
* - The reason is unknown.
|
835 |
* This case will cause an error.
|
836 |
*/
|
837 |
ErrorStatus handleSoftwareReset() { |
838 |
/* action depends on original shutdown reason */
|
839 |
switch (backup_reg.shutdown_pri_reason) {
|
840 |
case BL_SHUTDOWN_PRI_RSN_HIBERNATE:
|
841 |
{ |
842 |
/* activate the WKUP pin */
|
843 |
PWR_WakeUpPinCmd(ENABLE); |
844 |
|
845 |
/* deactivate any RTC related events */
|
846 |
RTC_WakeUpCmd(DISABLE); |
847 |
RTC_TamperCmd(RTC_Tamper_1, DISABLE); |
848 |
RTC_TimeStampCmd(RTC_TimeStampEdge_Rising, DISABLE); |
849 |
RTC_TimeStampCmd(RTC_TimeStampEdge_Falling, DISABLE); |
850 |
|
851 |
/* configure the IWDG to wake the system from standby mode */
|
852 |
uint16_t iwdg_ms = 1;
|
853 |
if (GPIO_ReadInputDataBit(PATH_DC_GPIO, PATH_DC_PIN) != Bit_SET) {
|
854 |
/* if a power plug is detected, fire immediately (1ms), else fire after the defined hibernate time */
|
855 |
iwdg_ms = HIBERNATE_TIME_MS; |
856 |
} |
857 |
configIwdg(iwdg_ms); |
858 |
IWDG_Enable(); |
859 |
|
860 |
/* enter standby mode */
|
861 |
PWR_EnterSTANDBYMode(); |
862 |
|
863 |
return SUCCESS;
|
864 |
break;
|
865 |
} |
866 |
case BL_SHUTDOWN_PRI_RSN_RESTART:
|
867 |
{ |
868 |
return handleColdReset();
|
869 |
break;
|
870 |
} |
871 |
case BL_SHUTDOWN_PRI_RSN_DEEPSLEEP:
|
872 |
{ |
873 |
if (GPIO_ReadInputDataBit(PATH_DC_GPIO, PATH_DC_PIN) == Bit_SET) {
|
874 |
return handlePathDcWakeup();
|
875 |
} else {
|
876 |
blCallbackShutdownDeepsleep(); |
877 |
} |
878 |
break;
|
879 |
} |
880 |
default:
|
881 |
return ERROR;
|
882 |
} |
883 |
return ERROR;
|
884 |
} /*** end of handleSoftwareReset ***/
|
885 |
|
886 |
/*
|
887 |
* System was woken up via the WKUP pin and the SYS_UART_DN signal was found to be responsible.
|
888 |
* In this case, the system starts as after a cold reset.
|
889 |
* this function is identical to handleTouchWakeup().
|
890 |
*/
|
891 |
ErrorStatus handleUartDnWakeup() { |
892 |
return handleColdReset();
|
893 |
} /*** end of hanldeUartDnWakeup ***/
|
894 |
|
895 |
/*
|
896 |
* System was woken up via the WKUP pin and the PATH_DC signal was found to be responsible.
|
897 |
* If the system was woken from deepsleep mode, it will enter hibernate mode to enable charging as long as the power plug is present.
|
898 |
* In any other case, the system will just enter the previous low-power mode again.
|
899 |
*/
|
900 |
ErrorStatus handlePathDcWakeup() { |
901 |
/* reenter the previous low-power mode */
|
902 |
switch (backup_reg.shutdown_pri_reason) {
|
903 |
case BL_SHUTDOWN_PRI_RSN_HIBERNATE:
|
904 |
blCallbackShutdownHibernate(); |
905 |
return SUCCESS;
|
906 |
break;
|
907 |
case BL_SHUTDOWN_PRI_RSN_DEEPSLEEP:
|
908 |
/* visualize that the power plug was detected
|
909 |
* This is helpful for feedback, and required for the follwing reason:
|
910 |
* When the power plug is detected, it takes some additional time for the ADC to detect a high voltage.
|
911 |
* If the ADC detects a low voltage at the first attempt, the system will enter hibernate mode.
|
912 |
* Thus, the ADC will measure the voltage again after several seconds and charging will start.
|
913 |
* However, this behaviour does not meet the user expection.
|
914 |
* Hence, the voltage has some to adapt at this point
|
915 |
*/
|
916 |
setLed(BLT_TRUE); |
917 |
msleep(500);
|
918 |
setLed(BLT_FALSE); |
919 |
|
920 |
return handleIwdgWakeup();
|
921 |
break;
|
922 |
case BL_SHUTDOWN_PRI_RSN_TRANSPORT:
|
923 |
blCallbackShutdownTransportation(); |
924 |
return SUCCESS;
|
925 |
break;
|
926 |
default:
|
927 |
return ERROR;
|
928 |
break;
|
929 |
} |
930 |
|
931 |
return ERROR;
|
932 |
} /*** end of handlePathDcWakeup ***/
|
933 |
|
934 |
/*
|
935 |
* System was woken up via the WKUP pin and the touch sensors were found to be responsible.
|
936 |
* In this case the system starts as after an cold reset.
|
937 |
* This function is identical to handleUartDnWakeup().
|
938 |
*/
|
939 |
ErrorStatus handleTouchWakeup() { |
940 |
return handleColdReset();
|
941 |
} /*** end of handleTouchWakeup ***/
|
942 |
|
943 |
/*
|
944 |
* System was woken up via the IWDG.
|
945 |
* In this case the ADC is configured and VSYS is measured once.
|
946 |
* If VSYS is found to be high enough to charge the batteries, the system will stay active until VSYS drops or an EXTI event occurs.
|
947 |
* Otherwise, the system will configure the IWDG to wake the system again after five seconds and enter standby mode.
|
948 |
*/
|
949 |
ErrorStatus handleIwdgWakeup() { |
950 |
/* handle different situations, depending on the backup data */
|
951 |
if ((backup_reg.shutdown_pri_reason == BL_SHUTDOWN_PRI_RSN_HIBERNATE) ||
|
952 |
(backup_reg.shutdown_pri_reason == BL_SHUTDOWN_PRI_RSN_DEEPSLEEP)) { |
953 |
/* handle periodic wakeup in hibernate mode and in deepsleep mode when a power plug was detetced */
|
954 |
|
955 |
/* if in hibernate mode, indicate the DiWheelDrive to enter hibernate mode as well, so it will activate the charging pins */
|
956 |
if (backup_reg.shutdown_pri_reason == BL_SHUTDOWN_PRI_RSN_HIBERNATE) {
|
957 |
indicateHibernate(); |
958 |
} |
959 |
|
960 |
/* measure the current voltage of VSYS */
|
961 |
AdcSingleMeasurement(); |
962 |
|
963 |
/* evaluate the value
|
964 |
* The ADC value represents the analog voltage between Vref- (= GND = 0.0V) and Vref+ (= VDD = 3.3V) as 12-bit value.
|
965 |
* Hence, the value read from the register is first scaled to [0V .. 3.3V].
|
966 |
* Then, an additional factor 5.33 is applied to account the downscaling on the board.
|
967 |
* Actually, the factor should be 5.0, but due to too large resistors it was corrected to 5.33.
|
968 |
*/
|
969 |
if ( (((float)(ADC_GetConversionValue(ADC1)) / (float)(0x0FFF)) * 3.3f * 5.33f) < 9.0f ) { |
970 |
/* VSYS was found to be < 9V */
|
971 |
|
972 |
/* re-enter power saving mode
|
973 |
* If the system was shut down to deepsleep mode and the power plug was removed, re-enter deepsleep mode.
|
974 |
* (This could be done earlier in this function, but since charging via the pins of the DeWheelDrive may be
|
975 |
* supported in the future, this is done after measuring VSYS)
|
976 |
*/
|
977 |
if (backup_reg.shutdown_pri_reason == BL_SHUTDOWN_PRI_RSN_DEEPSLEEP &&
|
978 |
GPIO_ReadInputDataBit(PATH_DC_GPIO, PATH_DC_PIN) == Bit_RESET) { |
979 |
blCallbackShutdownDeepsleep(); |
980 |
} else {
|
981 |
/* reconfigure the IWDG and power down for five seconds */
|
982 |
configIwdg(HIBERNATE_TIME_MS); |
983 |
IWDG_Enable(); |
984 |
|
985 |
/* enter standby mode */
|
986 |
PWR_EnterSTANDBYMode(); |
987 |
} |
988 |
|
989 |
return SUCCESS;
|
990 |
} else {
|
991 |
/* VSYS was found to be >= 9V */
|
992 |
setLed(BLT_TRUE); |
993 |
|
994 |
/* charge the battieries */
|
995 |
GPIO_ResetBits(CHARGE_EN1_N_GPIO, CHARGE_EN1_N_PIN); |
996 |
GPIO_ResetBits(CHARGE_EN2_N_GPIO, CHARGE_EN2_N_PIN); |
997 |
|
998 |
/* configure analog watchdoch to fire as soon as the voltage drops below 9V */
|
999 |
ADC_DeInit(); |
1000 |
setupADC(ADC1, (uint16_t)(9.0f / 5.33f / 3.3f * (float)0x0FFF), 0x0FFF); |
1001 |
|
1002 |
EXTI_InitTypeDef exti; |
1003 |
/* configure UART_DN EXTI */
|
1004 |
exti.EXTI_Line = EXTI_Line3; |
1005 |
exti.EXTI_Mode = EXTI_Mode_Interrupt; |
1006 |
exti.EXTI_Trigger = EXTI_Trigger_Falling; |
1007 |
exti.EXTI_LineCmd = ENABLE; |
1008 |
EXTI_Init(&exti); |
1009 |
|
1010 |
/* configure TOUCH_INT_N EXTI */
|
1011 |
exti.EXTI_Line = EXTI_Line5; |
1012 |
exti.EXTI_Mode = EXTI_Mode_Interrupt; |
1013 |
exti.EXTI_Trigger = EXTI_Trigger_Falling; |
1014 |
exti.EXTI_LineCmd = ENABLE; |
1015 |
EXTI_Init(&exti); |
1016 |
|
1017 |
/* configure PATH_DC EXTI */
|
1018 |
if (backup_reg.shutdown_pri_reason == BL_SHUTDOWN_PRI_RSN_DEEPSLEEP) {
|
1019 |
exti.EXTI_Line = EXTI_Line8; |
1020 |
exti.EXTI_Mode = EXTI_Mode_Interrupt; |
1021 |
exti.EXTI_Trigger = EXTI_Trigger_Falling; |
1022 |
exti.EXTI_LineCmd = ENABLE; |
1023 |
EXTI_Init(&exti); |
1024 |
} |
1025 |
|
1026 |
/* configure the NVIC so ADC and EXTI will be handled */
|
1027 |
NVIC_InitTypeDef nvic; |
1028 |
nvic.NVIC_IRQChannel = ADC_IRQn; |
1029 |
nvic.NVIC_IRQChannelPreemptionPriority = 6;
|
1030 |
nvic.NVIC_IRQChannelSubPriority = 6;
|
1031 |
nvic.NVIC_IRQChannelCmd = ENABLE; |
1032 |
NVIC_Init(&nvic); |
1033 |
nvic.NVIC_IRQChannel = EXTI3_IRQn; |
1034 |
nvic.NVIC_IRQChannelPreemptionPriority = 6;
|
1035 |
nvic.NVIC_IRQChannelSubPriority = 6;
|
1036 |
nvic.NVIC_IRQChannelCmd = ENABLE; |
1037 |
NVIC_Init(&nvic); |
1038 |
NVIC_EnableIRQ(EXTI3_IRQn); |
1039 |
nvic.NVIC_IRQChannel = EXTI9_5_IRQn; |
1040 |
nvic.NVIC_IRQChannelPreemptionPriority = 6;
|
1041 |
nvic.NVIC_IRQChannelSubPriority = 6;
|
1042 |
nvic.NVIC_IRQChannelCmd = ENABLE; |
1043 |
NVIC_Init(&nvic); |
1044 |
NVIC_EnableIRQ(EXTI9_5_IRQn); |
1045 |
|
1046 |
/* activate the ADC */
|
1047 |
ADC_SoftwareStartConv(ADC1); |
1048 |
|
1049 |
/* sleep until something happens */
|
1050 |
__WFI(); |
1051 |
|
1052 |
/* disable the chargers */
|
1053 |
GPIO_SetBits(CHARGE_EN1_N_GPIO, CHARGE_EN1_N_PIN); |
1054 |
GPIO_SetBits(CHARGE_EN2_N_GPIO, CHARGE_EN2_N_PIN); |
1055 |
setLed(BLT_FALSE); |
1056 |
|
1057 |
/* evaluate wakeup reason */
|
1058 |
// note: since I (tschoepp) don't know the difference between 'pending' and 'active' IRQs, both flags are ORed.
|
1059 |
uint8_t wkup_rsn = BL_WAKEUP_SEC_RSN_UNKNOWN; |
1060 |
if ((NVIC_GetActive(ADC_IRQn) != 0 || NVIC_GetPendingIRQ(ADC_IRQn) != 0) && |
1061 |
ADC_GetITStatus(ADC1, ADC_IT_AWD) == SET && |
1062 |
ADC_GetFlagStatus(ADC1, ADC_FLAG_AWD) == SET) { |
1063 |
wkup_rsn |= BL_WAKEUP_SEC_RSN_VSYSLOW; |
1064 |
} |
1065 |
if ((NVIC_GetActive(EXTI3_IRQn) != 0 || NVIC_GetPendingIRQ(EXTI3_IRQn) != 0) && |
1066 |
EXTI_GetFlagStatus(EXTI_Line3) == SET) { |
1067 |
wkup_rsn |= BL_WAKEUP_SEC_RSN_UART; |
1068 |
} |
1069 |
if ((NVIC_GetActive(EXTI9_5_IRQn) != 0 || NVIC_GetPendingIRQ(EXTI9_5_IRQn) != 0) && |
1070 |
EXTI_GetFlagStatus(EXTI_Line5) == SET) { |
1071 |
wkup_rsn |= BL_WAKEUP_SEC_RSN_TOUCH; |
1072 |
} |
1073 |
if ((NVIC_GetActive(EXTI9_5_IRQn) != 0 || NVIC_GetPendingIRQ(EXTI9_5_IRQn) != 0) && |
1074 |
EXTI_GetFlagStatus(EXTI_Line8) == SET) { |
1075 |
wkup_rsn |= BL_WAKEUP_SEC_RSN_PWRPLUG; |
1076 |
} |
1077 |
|
1078 |
/* since only the first interrupt will be handles, clear any pending ones */
|
1079 |
NVIC_DisableIRQ(ADC_IRQn); |
1080 |
NVIC_DisableIRQ(EXTI3_IRQn); |
1081 |
NVIC_DisableIRQ(EXTI9_5_IRQn); |
1082 |
NVIC_ClearPendingIRQ(ADC_IRQn); |
1083 |
NVIC_ClearPendingIRQ(EXTI3_IRQn); |
1084 |
NVIC_ClearPendingIRQ(EXTI9_5_IRQn); |
1085 |
|
1086 |
/* clear all pending EXTI events */
|
1087 |
EXTI_DeInit(); |
1088 |
EXTI_ClearFlag(EXTI_Line3); |
1089 |
EXTI_ClearFlag(EXTI_Line5); |
1090 |
EXTI_ClearFlag(EXTI_Line8); |
1091 |
|
1092 |
/* make sure the LED was visibly turned off */
|
1093 |
msleep(100);
|
1094 |
|
1095 |
/* depending on the wakup reason, handle accordingly */
|
1096 |
if (wkup_rsn & BL_WAKEUP_SEC_RSN_TOUCH) {
|
1097 |
/* the system was interrupted via the TOUCH_INT_N signal */
|
1098 |
|
1099 |
/* act as if this was a normal touch wakeup */
|
1100 |
backup_reg.wakeup_pri_reason = BL_WAKEUP_PRI_RSN_WKUP; |
1101 |
backup_reg.wakeup_sec_reason = BL_WAKEUP_SEC_RSN_TOUCH; |
1102 |
RTC_WriteBackupRegister(BL_RTC_BACKUP_REG, backup_reg.raw); |
1103 |
return handleTouchWakeup();
|
1104 |
} else if (wkup_rsn & BL_WAKEUP_SEC_RSN_UART) { |
1105 |
/* the system was interrupted via the SYS_UARTDN signal */
|
1106 |
|
1107 |
/* act as if this was a normal UART wakeup */
|
1108 |
backup_reg.wakeup_pri_reason = BL_WAKEUP_PRI_RSN_WKUP; |
1109 |
backup_reg.wakeup_sec_reason = BL_WAKEUP_SEC_RSN_UART; |
1110 |
RTC_WriteBackupRegister(BL_RTC_BACKUP_REG, backup_reg.raw); |
1111 |
return handleUartDnWakeup();
|
1112 |
} else if (wkup_rsn & BL_WAKEUP_SEC_RSN_VSYSLOW) { |
1113 |
/* VSYS has dropped below 9V */
|
1114 |
|
1115 |
/* depending on the original reason for shutdown, act differenty */
|
1116 |
switch (backup_reg.shutdown_pri_reason) {
|
1117 |
case BL_SHUTDOWN_PRI_RSN_HIBERNATE:
|
1118 |
{ |
1119 |
blCallbackShutdownHibernate(); |
1120 |
return SUCCESS;
|
1121 |
} |
1122 |
case BL_SHUTDOWN_PRI_RSN_DEEPSLEEP:
|
1123 |
{ |
1124 |
NVIC_SystemReset(); |
1125 |
return SUCCESS;
|
1126 |
} |
1127 |
default:
|
1128 |
return ERROR;
|
1129 |
} |
1130 |
} else if (wkup_rsn & BL_WAKEUP_SEC_RSN_PWRPLUG) { |
1131 |
/* system was interrupted because the power plug was removed
|
1132 |
* note: when a power cord is plugged in, this will not trigger an interrupt because the NVIC is configured for a falling edge only */
|
1133 |
if (backup_reg.shutdown_pri_reason == BL_SHUTDOWN_PRI_RSN_DEEPSLEEP) {
|
1134 |
blCallbackShutdownDeepsleep(); |
1135 |
return SUCCESS;
|
1136 |
} else {
|
1137 |
/* this state is undefined, because the PATH_DC inerrupt is only configured when the primary shutdown reason was to enter deepsleep mode */
|
1138 |
return ERROR;
|
1139 |
} |
1140 |
} else {
|
1141 |
/* the system was interrupted for an unknown reason */
|
1142 |
return ERROR;
|
1143 |
} |
1144 |
} // end of ADC evaluation
|
1145 |
} else {
|
1146 |
/* since it is unknown why the IWDG was configured, act as after a cold reset */
|
1147 |
return handleColdReset();
|
1148 |
} |
1149 |
|
1150 |
return ERROR;
|
1151 |
} /*** end of handleIwdgWakeup ***/
|
1152 |
|
1153 |
/*
|
1154 |
* Indicates the DiWheelDrive module to enter hibernate mode at wakeup.
|
1155 |
* This function should be called quite at the beginning of the according handleXXXReset/Wakeup() methods.
|
1156 |
*/
|
1157 |
static void indicateHibernate() { |
1158 |
/* signal the DiWheelDrive to enter hibernate mode as well, so it will activate the charging pins */
|
1159 |
GPIO_ResetBits(SYS_UART_DN_GPIO, SYS_UART_DN_PIN); |
1160 |
msleep(10); // this must be that long, because the DiWheelDrive sleeps some time before evaluating any signals |
1161 |
GPIO_SetBits(SYS_UART_DN_GPIO, SYS_UART_DN_PIN); |
1162 |
|
1163 |
/* if the DiWheeDrive needs some time for setup it may pull down the signal */
|
1164 |
waitForSignal(SYS_UART_DN_GPIO, SYS_UART_DN_PIN, Bit_SET); |
1165 |
|
1166 |
return;
|
1167 |
} /*** end of indicateHibernate ***/
|
1168 |
|
1169 |
/*
|
1170 |
*Performs a one-shot measurement of the VSYS voltage.
|
1171 |
*/
|
1172 |
static void AdcSingleMeasurement() { |
1173 |
/* reset and initialize ADC for single-shot measurement */
|
1174 |
// ADC_DeInit();
|
1175 |
setupADC(ADC1, 0, 0); |
1176 |
|
1177 |
/* initialize the NVIC so ADC interrupts are handled */
|
1178 |
NVIC_InitTypeDef nvic; |
1179 |
nvic.NVIC_IRQChannel = ADC_IRQn; |
1180 |
nvic.NVIC_IRQChannelPreemptionPriority = 6;
|
1181 |
nvic.NVIC_IRQChannelSubPriority = 6;
|
1182 |
nvic.NVIC_IRQChannelCmd = ENABLE; |
1183 |
NVIC_Init(&nvic); |
1184 |
|
1185 |
/* measure the voltage once */
|
1186 |
setLed(BLT_TRUE); |
1187 |
ADC_ClearITPendingBit(ADC1, ADC_IT_EOC); |
1188 |
ADC_ClearFlag(ADC1, ADC_FLAG_EOC); |
1189 |
NVIC_EnableIRQ(ADC_IRQn); |
1190 |
ADC_SoftwareStartConv(ADC1); |
1191 |
while (ADC_GetITStatus(ADC1, ADC_IT_EOC) != SET && ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) != SET) {
|
1192 |
__WFI(); |
1193 |
} |
1194 |
NVIC_DisableIRQ(ADC_IRQn); |
1195 |
ADC_ClearITPendingBit(ADC1, ADC_IT_EOC); |
1196 |
ADC_ClearFlag(ADC1, ADC_FLAG_EOC); |
1197 |
NVIC_ClearPendingIRQ(ADC_IRQn); |
1198 |
setLed(BLT_FALSE); |
1199 |
|
1200 |
return;
|
1201 |
} /*** end of AdcSingleMeasurement ***/
|
1202 |
|
1203 |
/*
|
1204 |
* Configures the ADC for measuring VSYS.
|
1205 |
* ADCx is the ADC object to initialize.
|
1206 |
* low_th and high_th are the threshold values for the analog watchdor (must be 12-bit!).
|
1207 |
* If low_th >= high_th, the ADC is configured for single-shot measurements.
|
1208 |
* Otherwise, the watchdog is configured with the corresponding thresholds.
|
1209 |
*/
|
1210 |
ADC_TypeDef* setupADC(ADC_TypeDef* adc, const uint16_t low_th, const uint16_t high_th) { |
1211 |
/* evaluate the arguments */
|
1212 |
blt_bool awd_enable = BLT_FALSE; |
1213 |
if (low_th < high_th) {
|
1214 |
awd_enable = BLT_TRUE; |
1215 |
} |
1216 |
|
1217 |
/* enable the clock */
|
1218 |
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); |
1219 |
|
1220 |
/* enable the ADC (wakes it from low-power mode) */
|
1221 |
ADC_Cmd(adc, ENABLE); |
1222 |
|
1223 |
/* initialize the common registers */
|
1224 |
ADC_CommonInitTypeDef adc_cinit; |
1225 |
ADC_CommonStructInit(&adc_cinit); |
1226 |
adc_cinit.ADC_Prescaler = ADC_Prescaler_Div8; // clock as slow as possible
|
1227 |
ADC_CommonInit(&adc_cinit); |
1228 |
|
1229 |
/* initialize the ADC */
|
1230 |
ADC_InitTypeDef adc_init; |
1231 |
ADC_StructInit(&adc_init); |
1232 |
adc_init.ADC_ContinuousConvMode = (awd_enable == BLT_TRUE) ? ENABLE : DISABLE; |
1233 |
ADC_Init(adc, &adc_init); |
1234 |
|
1235 |
/* disable internal sensors */
|
1236 |
ADC_TempSensorVrefintCmd(DISABLE); |
1237 |
ADC_VBATCmd(DISABLE); |
1238 |
|
1239 |
/* configure ADC channel and speed */
|
1240 |
ADC_RegularChannelConfig(adc, ADC_Channel_9, 1, ADC_SampleTime_480Cycles);
|
1241 |
ADC_EOCOnEachRegularChannelCmd(adc, (awd_enable == BLT_TRUE) ? DISABLE : ENABLE); |
1242 |
ADC_DiscModeCmd(adc, DISABLE); |
1243 |
|
1244 |
/* disable DMA */
|
1245 |
ADC_DMACmd(adc, DISABLE); |
1246 |
|
1247 |
/* disable injected mode */
|
1248 |
ADC_AutoInjectedConvCmd(adc, DISABLE); |
1249 |
ADC_InjectedDiscModeCmd(adc, DISABLE); |
1250 |
|
1251 |
/* configure the analog watchdog */
|
1252 |
if (awd_enable == BLT_TRUE) {
|
1253 |
ADC_AnalogWatchdogSingleChannelConfig(adc, ADC_Channel_9); |
1254 |
ADC_AnalogWatchdogThresholdsConfig(adc, high_th, low_th); |
1255 |
ADC_AnalogWatchdogCmd(adc, ADC_AnalogWatchdog_SingleRegEnable); |
1256 |
} else {
|
1257 |
ADC_AnalogWatchdogCmd(adc, ADC_AnalogWatchdog_None); |
1258 |
} |
1259 |
|
1260 |
/* configure the interrupts to be generated by the ADC */
|
1261 |
ADC_ITConfig(adc, ADC_IT_EOC, (awd_enable == BLT_TRUE) ? DISABLE : ENABLE); |
1262 |
ADC_ITConfig(adc, ADC_IT_AWD, (awd_enable == BLT_TRUE) ? ENABLE : DISABLE); |
1263 |
ADC_ITConfig(adc, ADC_IT_JEOC, DISABLE); |
1264 |
ADC_ITConfig(adc, ADC_IT_OVR, DISABLE); |
1265 |
|
1266 |
return adc;
|
1267 |
} |
1268 |
|
1269 |
/*
|
1270 |
* Callback function that handles the system shutdown and enters transportation mode.
|
1271 |
* When called from a multithreaded environment, it must be ensured that no other thread will preempt this function.
|
1272 |
* In transportation low-power mode the system can only be woken up by pulling down the NRST signal.
|
1273 |
* Furthermore, the system can not be charged when in transportation mode.
|
1274 |
*/
|
1275 |
void blCallbackShutdownTransportation(void) { |
1276 |
/* make sure that the required clocks are activated */
|
1277 |
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE); |
1278 |
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); |
1279 |
|
1280 |
/* set/keep the SYS_SYNC and SYS_PD signals active */
|
1281 |
GPIO_ResetBits(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN); |
1282 |
GPIO_ResetBits(SYS_PD_N_GPIO, SYS_PD_N_PIN); |
1283 |
|
1284 |
/* initialized the standalone timer */
|
1285 |
saTimerInit(); |
1286 |
|
1287 |
setLed(BLT_TRUE); |
1288 |
|
1289 |
/* wait for all boards to be ready for shutdown */
|
1290 |
GPIO_SetBits(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN); |
1291 |
if (GPIO_ReadOutputDataBit(SYS_REG_EN_GPIO, SYS_REG_EN_PIN) == Bit_SET) {
|
1292 |
// this must skipped if the pullup voltage (VIO3.3) is not active
|
1293 |
setLed(BLT_TRUE); |
1294 |
waitForSignal(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN, Bit_SET); |
1295 |
setLed(BLT_FALSE); |
1296 |
} |
1297 |
|
1298 |
/* execute disambiguation procedure and signal all modules to enter transportation mode */
|
1299 |
if (shutdownDisambiguationProcedure(BL_SHUTDOWN_PRI_RSN_TRANSPORT) != SUCCESS) {
|
1300 |
blinkSOS(1);
|
1301 |
msleep(10);
|
1302 |
} |
1303 |
|
1304 |
shutdownToTransportation(); |
1305 |
|
1306 |
return;
|
1307 |
} /*** end of blCallbackTransportation ***/
|
1308 |
|
1309 |
/*
|
1310 |
* Callback function that handles the system shutdown and enters deepsleep mode.
|
1311 |
* When called from a multithreaded environment, it must be ensured that no other thread will preempt this function.
|
1312 |
* In deepsleep low-power mode the system can only be woken up via the NRST or the WKUP signal, or the RTC or IWDG, if configured.
|
1313 |
* When a power plug is detected, the system will switch to hibernate mode, to provide charging capabilities (cf. handlePathDcWakeup()).
|
1314 |
* As soon as the plug is removed again, however, the system will return to deppsleep mode (cf. handleIwdgWakeup()).
|
1315 |
*/
|
1316 |
void blCallbackShutdownDeepsleep(void) { |
1317 |
/* make sure that the required clocks are activated */
|
1318 |
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE); |
1319 |
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); |
1320 |
|
1321 |
/* set/keep the SYS_SYNC and SYS_PD signals active */
|
1322 |
GPIO_ResetBits(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN); |
1323 |
GPIO_ResetBits(SYS_PD_N_GPIO, SYS_PD_N_PIN); |
1324 |
|
1325 |
/* initialized the standalone timer */
|
1326 |
saTimerInit(); |
1327 |
|
1328 |
setLed(BLT_TRUE); |
1329 |
|
1330 |
/* wait for all boards to be ready for shutdown */
|
1331 |
GPIO_SetBits(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN); |
1332 |
if (GPIO_ReadOutputDataBit(SYS_REG_EN_GPIO, SYS_REG_EN_PIN) == Bit_SET) {
|
1333 |
// this must skipped if the pullup voltage (VIO3.3) is not active
|
1334 |
setLed(BLT_TRUE); |
1335 |
waitForSignal(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN, Bit_SET); |
1336 |
setLed(BLT_FALSE); |
1337 |
} |
1338 |
|
1339 |
/* execute disambiguation procedure and signal all modules to enter deepsleep mode */
|
1340 |
if (shutdownDisambiguationProcedure(BL_SHUTDOWN_PRI_RSN_DEEPSLEEP) != SUCCESS) {
|
1341 |
blinkSOS(1);
|
1342 |
msleep(10);
|
1343 |
} |
1344 |
|
1345 |
shutdownToDeepsleep(); |
1346 |
|
1347 |
return;
|
1348 |
} /*** end of blCallbackDeepsleep ***/
|
1349 |
|
1350 |
/*
|
1351 |
* Callback function that handles the system shutdown and enters hibernate mode.
|
1352 |
* When called from a multithreaded environment, it must be ensured that no other thread will preempt this function.
|
1353 |
* Since this function actually just configures the system in a way, that it will enter hibernate mode after the next reset and rests it,
|
1354 |
* see the handleSoftwareReset() function for more details about the hibernate low-power mode.
|
1355 |
*/
|
1356 |
void blCallbackShutdownHibernate(void) { |
1357 |
/* make sure that the required clocks are activated */
|
1358 |
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE); |
1359 |
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); |
1360 |
|
1361 |
/* set/keep the SYS_SYNC and SYS_PD signals active */
|
1362 |
GPIO_ResetBits(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN); |
1363 |
GPIO_ResetBits(SYS_PD_N_GPIO, SYS_PD_N_PIN); |
1364 |
|
1365 |
/* initialized the standalone timer */
|
1366 |
saTimerInit(); |
1367 |
|
1368 |
setLed(BLT_TRUE); |
1369 |
|
1370 |
/* wait for all boards to be ready for shutdown */
|
1371 |
GPIO_SetBits(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN); |
1372 |
if (GPIO_ReadOutputDataBit(SYS_REG_EN_GPIO, SYS_REG_EN_PIN) == Bit_SET) {
|
1373 |
// this must skipped if the pullup voltage (VIO3.3) is not active
|
1374 |
setLed(BLT_TRUE); |
1375 |
waitForSignal(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN, Bit_SET); |
1376 |
setLed(BLT_FALSE); |
1377 |
} |
1378 |
|
1379 |
/* execute disambiguation procedure and signal all modules to enter hibernate mode */
|
1380 |
if (shutdownDisambiguationProcedure(BL_SHUTDOWN_PRI_RSN_DEEPSLEEP) != SUCCESS) {
|
1381 |
blinkSOS(1);
|
1382 |
msleep(10);
|
1383 |
} |
1384 |
|
1385 |
shutdownToHibernate(); |
1386 |
|
1387 |
return;
|
1388 |
} /*** end of blCallbackShutdownHibernate ***/
|
1389 |
|
1390 |
/*
|
1391 |
* Callback function that handles the system shutdown and initializes a restart.
|
1392 |
* When called from a multithreaded environment, it must be ensured that no other thread will preempt this function.
|
1393 |
* By configuration it is ensured, that the system will end up executing the handleSoftwareReset() function after reset.
|
1394 |
*/
|
1395 |
void blCallbackShutdownRestart(void) { |
1396 |
/* make sure that the required clocks are activated */
|
1397 |
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE); |
1398 |
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); |
1399 |
|
1400 |
/* set/keep the SYS_SYNC and SYS_PD signals active */
|
1401 |
GPIO_ResetBits(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN); |
1402 |
GPIO_ResetBits(SYS_PD_N_GPIO, SYS_PD_N_PIN); |
1403 |
|
1404 |
/* initialized the standalone timer */
|
1405 |
saTimerInit(); |
1406 |
|
1407 |
setLed(BLT_TRUE); |
1408 |
|
1409 |
/* deactivate SYS_PD_N and ensure that all modules had a chance to detect the falling edge */
|
1410 |
msleep(1);
|
1411 |
GPIO_SetBits(SYS_PD_N_GPIO, SYS_PD_N_PIN); |
1412 |
msleep(1);
|
1413 |
|
1414 |
/* wait for all boards to be ready for shutdown */
|
1415 |
GPIO_SetBits(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN); |
1416 |
if (GPIO_ReadOutputDataBit(SYS_REG_EN_GPIO, SYS_REG_EN_PIN) == Bit_SET) {
|
1417 |
// this must skipped if the pullup voltage (VIO3.3) is not active
|
1418 |
setLed(BLT_TRUE); |
1419 |
waitForSignal(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN, Bit_SET); |
1420 |
setLed(BLT_FALSE); |
1421 |
} |
1422 |
|
1423 |
/* execute disambiguation procedure and signal all modules to restart normally */
|
1424 |
if (shutdownDisambiguationProcedure(BL_SHUTDOWN_PRI_RSN_RESTART) != SUCCESS) {
|
1425 |
blinkSOS(1);
|
1426 |
msleep(10);
|
1427 |
} |
1428 |
|
1429 |
/* restart the system */
|
1430 |
shutdownAndRestart(); |
1431 |
|
1432 |
return;
|
1433 |
} /*** end of blCallbackRestart ***/
|
1434 |
|
1435 |
/*
|
1436 |
* Callback function that handles a system shutdown/restart request from another module.
|
1437 |
* Depending on the result of the disambiguation procedure, the module will enter the according low-power mode or restart.
|
1438 |
* When called from a multithreaded environment, it must be ensured that no other thread will preempt this function.
|
1439 |
*/
|
1440 |
void blCallbackHandleShutdownRequest(void) { |
1441 |
/* make sure that the required clocks are activated */
|
1442 |
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE); |
1443 |
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); |
1444 |
|
1445 |
/* set/keep the SYS_SYNC and SYS_PD signals active */
|
1446 |
GPIO_ResetBits(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN); |
1447 |
GPIO_ResetBits(SYS_PD_N_GPIO, SYS_PD_N_PIN); |
1448 |
|
1449 |
/* initialized the standalone timer */
|
1450 |
saTimerInit(); |
1451 |
|
1452 |
setLed(BLT_TRUE); |
1453 |
|
1454 |
/* deactivate SYS_PD_N and ensure that all modules had a chance to detect the falling edge */
|
1455 |
msleep(1);
|
1456 |
GPIO_SetBits(SYS_PD_N_GPIO, SYS_PD_N_PIN); |
1457 |
msleep(1);
|
1458 |
|
1459 |
/* wait for all boards to be ready for shutdown */
|
1460 |
GPIO_SetBits(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN); |
1461 |
if (GPIO_ReadOutputDataBit(SYS_REG_EN_GPIO, SYS_REG_EN_PIN) == Bit_SET) {
|
1462 |
// this must be skipped if the pullup voltage (VIO3.3) is not active
|
1463 |
setLed(BLT_TRUE); |
1464 |
waitForSignal(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN, Bit_SET); |
1465 |
setLed(BLT_FALSE); |
1466 |
} |
1467 |
|
1468 |
/* check ths SYS_PD_N signal, whether the system shall shutdown or restart */
|
1469 |
blt_bool shutdown_nrestart = (GPIO_ReadInputDataBit(SYS_PD_N_GPIO, SYS_PD_N_PIN) == Bit_RESET) ? BLT_TRUE : BLT_FALSE; |
1470 |
|
1471 |
/* disambiguation procedure (passive) */
|
1472 |
uint32_t pulse_counter = 0;
|
1473 |
while (waitForSignalTimeout(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN, Bit_RESET, 10)) { |
1474 |
waitForSignal(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN, Bit_SET); |
1475 |
++pulse_counter; |
1476 |
} |
1477 |
|
1478 |
/* evaluate and hanlde disambiguation result */
|
1479 |
if (shutdown_nrestart == BLT_TRUE) {
|
1480 |
/* shutdown request */
|
1481 |
|
1482 |
/* handle special cases */
|
1483 |
if (pulse_counter == BL_SHUTDOWN_PRI_RSN_UNKNOWN) {
|
1484 |
/* no pulse at all was received */
|
1485 |
pulse_counter = BL_SHUTDOWN_PRI_RSN_DEFAULT; |
1486 |
} else if (pulse_counter != BL_SHUTDOWN_PRI_RSN_HIBERNATE && |
1487 |
pulse_counter != BL_SHUTDOWN_PRI_RSN_DEEPSLEEP && |
1488 |
pulse_counter != BL_SHUTDOWN_PRI_RSN_TRANSPORT) { |
1489 |
/* invalid number of pulses received */
|
1490 |
blinkSOS(1);
|
1491 |
pulse_counter = BL_SHUTDOWN_PRI_RSN_DEFAULT; |
1492 |
} |
1493 |
|
1494 |
switch (pulse_counter) {
|
1495 |
case BL_SHUTDOWN_PRI_RSN_HIBERNATE:
|
1496 |
shutdownToHibernate(); |
1497 |
break;
|
1498 |
case BL_SHUTDOWN_PRI_RSN_DEEPSLEEP:
|
1499 |
shutdownToDeepsleep(); |
1500 |
break;
|
1501 |
case BL_SHUTDOWN_PRI_RSN_TRANSPORT:
|
1502 |
shutdownToTransportation(); |
1503 |
break;
|
1504 |
} |
1505 |
} else {
|
1506 |
/* restart request */
|
1507 |
|
1508 |
/* there is no ambiguity for restart, so it is ignored */
|
1509 |
shutdownAndRestart(); |
1510 |
} |
1511 |
|
1512 |
/* if this code is reached, the system did neither shut down, nor restart.
|
1513 |
* This must never be the case!
|
1514 |
*/
|
1515 |
blinkSOSinf(); |
1516 |
return;
|
1517 |
} /*** end of blCallbackHandleShutdownRequest ***/
|
1518 |
|
1519 |
/*********************************** end of main.c *************************************/
|