amiro-blt / Target / Modules / PowerManagement_1-2 / Boot / main.c @ 2b9a14a2
History | View | Annotate | Download (57.492 KB)
1 | a270d48f | Thomas Schöpping | /************************************************************************************//** |
---|---|---|---|
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 | 2b9a14a2 | Thomas Schöpping | static void initGpio(void); |
55 | static void initExti(void); |
||
56 | void configGpioForShutdown(void); |
||
57 | void systemPowerDown(void); |
||
58 | a270d48f | Thomas Schöpping | |
59 | 2b9a14a2 | Thomas Schöpping | ErrorStatus handleColdReset(void);
|
60 | ErrorStatus handleSoftwareReset(void);
|
||
61 | ErrorStatus handleUartDnWakeup(void);
|
||
62 | ErrorStatus handlePathDcWakeup(void);
|
||
63 | ErrorStatus handleTouchWakeup(void);
|
||
64 | ErrorStatus handleIwdgWakeup(void);
|
||
65 | a270d48f | Thomas Schöpping | |
66 | 2b9a14a2 | Thomas Schöpping | static void indicateHibernate(void); |
67 | static void AdcSingleMeasurement(void); |
||
68 | a270d48f | Thomas Schöpping | |
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 | 2b9a14a2 | Thomas Schöpping | void shutdownToTransportation(void); |
74 | void shutdownToDeepsleep(void); |
||
75 | void shutdownToHibernate(void); |
||
76 | void shutdownAndRestart(void); |
||
77 | a270d48f | Thomas Schöpping | |
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 | 2b9a14a2 | Thomas Schöpping | .vBootloader = {BL_VERSION_ID_AMiRoBLT_Beta, BL_VERSION_MAJOR, BL_VERSION_MINOR, 4},
|
92 | a270d48f | Thomas Schöpping | .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 | 2b9a14a2 | Thomas Schöpping | /*
|
170 | * Measure the current voltage of VSYS and enable the chargers if it was found to be > 9V.
|
||
171 | */
|
||
172 | AdcSingleMeasurement(); |
||
173 | if ( (((float)(ADC_GetConversionValue(ADC1)) / (float)0x0FFF) * 3.3f * 5.33f) > 9.0f ) { |
||
174 | /* VSYS was found to be > 9V */
|
||
175 | setLed(BLT_TRUE); |
||
176 | GPIO_ResetBits(CHARGE_EN1_N_GPIO, CHARGE_EN1_N_PIN); |
||
177 | GPIO_ResetBits(CHARGE_EN2_N_GPIO, CHARGE_EN2_N_PIN); |
||
178 | } |
||
179 | |||
180 | a270d48f | Thomas Schöpping | /* handle different wakeup/reset reasons */
|
181 | ErrorStatus status = ERROR; |
||
182 | if (backup_reg.wakeup_pri_reason & BL_WAKEUP_PRI_RSN_SFTRST) {
|
||
183 | /* system was reset by software */
|
||
184 | status = handleSoftwareReset(); |
||
185 | } else if (backup_reg.wakeup_pri_reason & BL_WAKEUP_PRI_RSN_WKUP) { |
||
186 | /* system was woken via WKUP pin */
|
||
187 | /* differeciate between thre wakeup types */
|
||
188 | switch (backup_reg.wakeup_sec_reason) {
|
||
189 | case BL_WAKEUP_SEC_RSN_UART:
|
||
190 | status = handleUartDnWakeup(); |
||
191 | break;
|
||
192 | case BL_WAKEUP_SEC_RSN_PWRPLUG:
|
||
193 | status = handlePathDcWakeup(); |
||
194 | break;
|
||
195 | case BL_WAKEUP_SEC_RSN_TOUCH:
|
||
196 | status = handleTouchWakeup(); |
||
197 | break;
|
||
198 | default:
|
||
199 | status = ERROR; |
||
200 | break;
|
||
201 | } |
||
202 | } else if (backup_reg.wakeup_pri_reason & BL_WAKEUP_PRI_RSN_IWDGRST) { |
||
203 | /* system was woken by IWDG */
|
||
204 | status = handleIwdgWakeup(); |
||
205 | } else if (backup_reg.wakeup_pri_reason == BL_WAKEUP_PRI_RSN_PINRST) { |
||
206 | /* system was reset via NRST pin */
|
||
207 | status = handleColdReset(); |
||
208 | } else {
|
||
209 | /* system was woken/reset for an unexpected reason.
|
||
210 | * In this case the LED blinks "SOS" (... --- ...) and the system resets.
|
||
211 | */
|
||
212 | blinkSOS(1);
|
||
213 | status = ERROR; |
||
214 | backup_reg.shutdown_pri_reason = BL_SHUTDOWN_PRI_RSN_RESTART; |
||
215 | backup_reg.shutdown_sec_reason = BL_SHUTDOWN_SEC_RSN_UNKNOWN; |
||
216 | RTC_WriteBackupRegister(BL_RTC_BACKUP_REG, backup_reg.raw); |
||
217 | NVIC_SystemReset(); |
||
218 | } |
||
219 | |||
220 | /* if something went wrong, signal this failure */
|
||
221 | if (status != SUCCESS) {
|
||
222 | blinkSOSinf(); |
||
223 | } |
||
224 | |||
225 | return;
|
||
226 | } /*** end of main ***/
|
||
227 | |||
228 | |||
229 | /************************************************************************************//** |
||
230 | ** \brief Initializes the microcontroller.
|
||
231 | ** \return none.
|
||
232 | **
|
||
233 | ****************************************************************************************/
|
||
234 | static void Init(void) |
||
235 | { |
||
236 | #if (BOOT_COM_UART_ENABLE > 0 || BOOT_GATE_UART_ENABLE > 0) |
||
237 | GPIO_InitTypeDef GPIO_InitStructure; |
||
238 | #elif (BOOT_FILE_SYS_ENABLE > 0) |
||
239 | GPIO_InitTypeDef GPIO_InitStructure; |
||
240 | USART_InitTypeDef USART_InitStructure; |
||
241 | #elif (BOOT_COM_CAN_ENABLE > 0 || BOOT_GATE_CAN_ENABLE > 0) |
||
242 | GPIO_InitTypeDef GPIO_InitStructure; |
||
243 | #endif
|
||
244 | |||
245 | /* initialize the system and its clocks */
|
||
246 | SystemInit(); |
||
247 | #if (BOOT_COM_UART_ENABLE > 0 || BOOT_GATE_UART_ENABLE > 0) |
||
248 | /* enable UART peripheral clock */
|
||
249 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); |
||
250 | /* enable GPIO peripheral clock for transmitter and receiver pins */
|
||
251 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); |
||
252 | /* connect the pin to the peripherals alternate function */
|
||
253 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); |
||
254 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); |
||
255 | /* configure USART Tx as alternate function */
|
||
256 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; |
||
257 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; |
||
258 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; |
||
259 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; |
||
260 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
||
261 | GPIO_Init(GPIOA, &GPIO_InitStructure); |
||
262 | /* configure USART Rx as alternate function */
|
||
263 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; |
||
264 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; |
||
265 | GPIO_Init(GPIOA, &GPIO_InitStructure); |
||
266 | #endif
|
||
267 | |||
268 | #if (BOOT_COM_BLUETOOTH_UART_ENABLE > 0) |
||
269 | /* enable UART peripheral clock */
|
||
270 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); |
||
271 | |||
272 | /* enable GPIO peripheral clock for transmitter and receiver pins */
|
||
273 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); |
||
274 | /* connect the pin to the peripherals alternate function */
|
||
275 | GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3); |
||
276 | GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3); |
||
277 | /* configure USART Tx as alternate function */
|
||
278 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; |
||
279 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; |
||
280 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; |
||
281 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; |
||
282 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
||
283 | GPIO_Init(GPIOC, &GPIO_InitStructure); |
||
284 | /* configure USART Rx as alternate function */
|
||
285 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; |
||
286 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; |
||
287 | GPIO_Init(GPIOC, &GPIO_InitStructure); |
||
288 | |||
289 | /* Configure Bluetooth reset pin */
|
||
290 | GPIO_InitTypeDef gpio_init; |
||
291 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); |
||
292 | gpio_init.GPIO_Pin = BT_RST_PIN; |
||
293 | gpio_init.GPIO_OType = GPIO_OType_OD; |
||
294 | gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
||
295 | gpio_init.GPIO_Mode = GPIO_Mode_OUT; |
||
296 | gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
||
297 | GPIO_Init(BT_RST_GPIO, &gpio_init); |
||
298 | /* Reset Bluetooth reset pin */
|
||
299 | GPIO_ResetBits(BT_RST_GPIO, BT_RST_PIN); |
||
300 | #endif
|
||
301 | |||
302 | |||
303 | #if (BOOT_COM_CAN_ENABLE > 0 || BOOT_GATE_CAN_ENABLE > 0) |
||
304 | /* enable clocks for CAN transmitter and receiver pins */
|
||
305 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); |
||
306 | /* select alternate function for the CAN pins */
|
||
307 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_CAN1); |
||
308 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_CAN1); |
||
309 | /* configure CAN RX and TX pins */
|
||
310 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; |
||
311 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
||
312 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; |
||
313 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; |
||
314 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; |
||
315 | GPIO_Init(GPIOA, &GPIO_InitStructure); |
||
316 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; |
||
317 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; |
||
318 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
||
319 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; |
||
320 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; |
||
321 | GPIO_Init(GPIOA, &GPIO_InitStructure); |
||
322 | #endif
|
||
323 | } /*** end of Init ***/
|
||
324 | |||
325 | /*
|
||
326 | * Initializes all GPIO used by the bootloader
|
||
327 | */
|
||
328 | 2b9a14a2 | Thomas Schöpping | static void initGpio(void) { |
329 | a270d48f | Thomas Schöpping | GPIO_InitTypeDef gpio_init; |
330 | |||
331 | /*
|
||
332 | * OUTPUTS
|
||
333 | */
|
||
334 | |||
335 | /* initialize LED and push it up (inactive) */
|
||
336 | GPIO_SetBits(LED_GPIO, LED_PIN); |
||
337 | gpio_init.GPIO_Pin = LED_PIN; |
||
338 | gpio_init.GPIO_Mode = GPIO_Mode_OUT; |
||
339 | gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
||
340 | gpio_init.GPIO_OType = GPIO_OType_PP; |
||
341 | gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
||
342 | GPIO_Init(LED_GPIO, &gpio_init); |
||
343 | |||
344 | /* initialize SYS_PD_N and push it up (inactive) */
|
||
345 | GPIO_SetBits(SYS_PD_N_GPIO, SYS_PD_N_PIN); |
||
346 | gpio_init.GPIO_Pin = SYS_PD_N_PIN; |
||
347 | gpio_init.GPIO_Mode = GPIO_Mode_OUT; |
||
348 | gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
||
349 | gpio_init.GPIO_OType = GPIO_OType_OD; |
||
350 | gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
||
351 | GPIO_Init(SYS_PD_N_GPIO, &gpio_init); |
||
352 | |||
353 | /* initialize SYS_SYNC_N and pull it down (active) */
|
||
354 | GPIO_ResetBits(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN); |
||
355 | gpio_init.GPIO_Pin = SYS_SYNC_N_PIN; |
||
356 | gpio_init.GPIO_Mode = GPIO_Mode_OUT; |
||
357 | gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
||
358 | gpio_init.GPIO_OType = GPIO_OType_OD; |
||
359 | gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
||
360 | GPIO_Init(SYS_SYNC_N_GPIO, &gpio_init); |
||
361 | |||
362 | /* initialize SYS_WARMRST_N and pull it down (active) */
|
||
363 | GPIO_ResetBits(SYS_WARMRST_N_GPIO, SYS_WARMRST_N_PIN); |
||
364 | gpio_init.GPIO_Pin = SYS_WARMRST_N_PIN; |
||
365 | gpio_init.GPIO_Mode = GPIO_Mode_OUT; |
||
366 | gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
||
367 | gpio_init.GPIO_OType = GPIO_OType_OD; |
||
368 | gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
||
369 | GPIO_Init(SYS_WARMRST_N_GPIO, &gpio_init); |
||
370 | |||
371 | /* initialize SYS_UART_DN and push it up (inactive) */
|
||
372 | GPIO_SetBits(SYS_UART_DN_GPIO, SYS_UART_DN_PIN); |
||
373 | gpio_init.GPIO_Pin = SYS_UART_DN_PIN; |
||
374 | gpio_init.GPIO_Mode = GPIO_Mode_OUT; |
||
375 | gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
||
376 | gpio_init.GPIO_OType = GPIO_OType_OD; |
||
377 | gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
||
378 | GPIO_Init(SYS_UART_DN_GPIO, &gpio_init); |
||
379 | |||
380 | /* initialize POWER_EN and pull it down (inactive) */
|
||
381 | GPIO_ResetBits(POWER_EN_GPIO, POWER_EN_PIN); |
||
382 | gpio_init.GPIO_Pin = POWER_EN_PIN; |
||
383 | gpio_init.GPIO_Mode = GPIO_Mode_OUT; |
||
384 | gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
||
385 | gpio_init.GPIO_OType = GPIO_OType_PP; |
||
386 | gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
||
387 | GPIO_Init(POWER_EN_GPIO, &gpio_init); |
||
388 | |||
389 | /* initialize SYS_REG_EN and pull it down (inactive) */
|
||
390 | GPIO_ResetBits(SYS_REG_EN_GPIO, SYS_REG_EN_PIN); |
||
391 | gpio_init.GPIO_Pin = SYS_REG_EN_PIN; |
||
392 | gpio_init.GPIO_Mode = GPIO_Mode_OUT; |
||
393 | gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
||
394 | gpio_init.GPIO_OType = GPIO_OType_PP; |
||
395 | gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
||
396 | GPIO_Init(SYS_REG_EN_GPIO, &gpio_init); |
||
397 | |||
398 | /* initialize CHARGE_EN1_N and CHARGE_EN2_N and push them up (inactive) */
|
||
399 | GPIO_SetBits(CHARGE_EN1_N_GPIO, CHARGE_EN1_N_PIN); |
||
400 | GPIO_SetBits(CHARGE_EN2_N_GPIO, CHARGE_EN2_N_PIN); |
||
401 | gpio_init.GPIO_Pin = CHARGE_EN1_N_PIN; |
||
402 | gpio_init.GPIO_Mode = GPIO_Mode_OUT; |
||
403 | gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
||
404 | gpio_init.GPIO_OType = GPIO_OType_PP; |
||
405 | gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
||
406 | GPIO_Init(CHARGE_EN1_N_GPIO, &gpio_init); |
||
407 | gpio_init.GPIO_Pin = CHARGE_EN2_N_PIN; |
||
408 | GPIO_Init(CHARGE_EN2_N_GPIO, &gpio_init); |
||
409 | |||
410 | /*
|
||
411 | * INPUTS
|
||
412 | */
|
||
413 | |||
414 | /* initialize SWITCH_STATUS_N */
|
||
415 | gpio_init.GPIO_Pin = SWITCH_STATUS_N_PIN; |
||
416 | gpio_init.GPIO_Mode = GPIO_Mode_IN; |
||
417 | gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
||
418 | gpio_init.GPIO_OType = GPIO_OType_PP; |
||
419 | gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
||
420 | GPIO_Init(SWITCH_STATUS_N_GPIO, &gpio_init); |
||
421 | |||
422 | /* initialize PATH_DC */
|
||
423 | gpio_init.GPIO_Pin = PATH_DC_PIN; |
||
424 | gpio_init.GPIO_Mode = GPIO_Mode_IN; |
||
425 | gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
||
426 | gpio_init.GPIO_OType = GPIO_OType_PP; |
||
427 | gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
||
428 | GPIO_Init(PATH_DC_GPIO, &gpio_init); |
||
429 | |||
430 | /* initialize TOUCH_INT_N */
|
||
431 | gpio_init.GPIO_Pin = TOUCH_INT_N_PIN; |
||
432 | gpio_init.GPIO_Mode = GPIO_Mode_IN; |
||
433 | gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
||
434 | gpio_init.GPIO_OType = GPIO_OType_PP; |
||
435 | gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
||
436 | GPIO_Init(TOUCH_INT_N_GPIO, &gpio_init); |
||
437 | |||
438 | /* initialize VSYS_SENSE as analog input */
|
||
439 | gpio_init.GPIO_Pin = VSYS_SENSE_PIN; |
||
440 | gpio_init.GPIO_Mode = GPIO_Mode_AN; |
||
441 | gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
||
442 | gpio_init.GPIO_OType = GPIO_OType_PP; |
||
443 | gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
||
444 | GPIO_Init(VSYS_SENSE_GPIO, &gpio_init); |
||
445 | |||
446 | /* initialize GPIOB4, since it is configured in alternate function mode on reset */
|
||
447 | gpio_init.GPIO_Pin = CHARGE_STAT2A_PIN; |
||
448 | gpio_init.GPIO_Mode = GPIO_Mode_IN; |
||
449 | gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
||
450 | gpio_init.GPIO_OType = GPIO_OType_PP; |
||
451 | gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
||
452 | GPIO_Init(CHARGE_STAT2A_GPIO, &gpio_init); |
||
453 | |||
454 | return;
|
||
455 | } /*** end of initGpio ***/
|
||
456 | |||
457 | /*
|
||
458 | * Initialize all EXTI lines
|
||
459 | */
|
||
460 | 2b9a14a2 | Thomas Schöpping | static void initExti(void) { |
461 | a270d48f | Thomas Schöpping | /* configure EXTI lines */
|
462 | SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource0); // IR_INT1_N
|
||
463 | SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource0); // CHARGE_STAT1A
|
||
464 | SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource1); // GAUGE_BATLOW1
|
||
465 | SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource2); // GAUGE_BATGD1_N
|
||
466 | SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource3); // SYS_UART_DN
|
||
467 | SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource4); // CHARGE_STAT2A
|
||
468 | SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource4); // IR_INT2_N
|
||
469 | SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource5); // TOUCH_INT_N
|
||
470 | SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource6); // GAUGE_BATLOW2
|
||
471 | SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource7); // GAUGE_BATGD2_N
|
||
472 | SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource8); // PATH_DC
|
||
473 | SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource9); // SYS_SPI_DIR
|
||
474 | SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource12); // SYS_SYNC_N
|
||
475 | SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource13); // SYS_PD_N
|
||
476 | SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource14); // SYS_WARMRST_N
|
||
477 | SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource15); // SYS_UART_UP
|
||
478 | |||
479 | return;
|
||
480 | } /*** end of initExti ***/
|
||
481 | |||
482 | /*
|
||
483 | * Signals, which type of low-power mode the system shall enter after the shutdown sequence.
|
||
484 | */
|
||
485 | ErrorStatus shutdownDisambiguationProcedure(const uint8_t type) {
|
||
486 | GPIO_SetBits(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN); |
||
487 | ErrorStatus ret_val = ERROR; |
||
488 | |||
489 | switch (type) {
|
||
490 | case BL_SHUTDOWN_PRI_RSN_UNKNOWN:
|
||
491 | case BL_SHUTDOWN_PRI_RSN_HIBERNATE:
|
||
492 | case BL_SHUTDOWN_PRI_RSN_DEEPSLEEP:
|
||
493 | case BL_SHUTDOWN_PRI_RSN_TRANSPORT:
|
||
494 | { |
||
495 | // broadcast a number of pulses, depending on the argument
|
||
496 | uint8_t pulse_counter = 0;
|
||
497 | for (pulse_counter = 0; pulse_counter < type; ++pulse_counter) { |
||
498 | msleep(1);
|
||
499 | GPIO_ResetBits(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN); |
||
500 | msleep(1);
|
||
501 | GPIO_SetBits(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN); |
||
502 | } |
||
503 | // wait for timeout
|
||
504 | msleep(10);
|
||
505 | ret_val = SUCCESS; |
||
506 | break;
|
||
507 | } |
||
508 | case BL_SHUTDOWN_PRI_RSN_RESTART:
|
||
509 | { |
||
510 | // since there is no ambiguity for restart requests, no pulses are generated
|
||
511 | msleep(10);
|
||
512 | ret_val = SUCCESS; |
||
513 | break;
|
||
514 | } |
||
515 | default:
|
||
516 | ret_val = ERROR; |
||
517 | break;
|
||
518 | } |
||
519 | |||
520 | return ret_val;
|
||
521 | } /*** end of shutdownDisambiguationProcedure ***/
|
||
522 | |||
523 | /*
|
||
524 | * Final shutdown of the system to enter transportation mode.
|
||
525 | */
|
||
526 | 2b9a14a2 | Thomas Schöpping | void shutdownToTransportation(void) { |
527 | a270d48f | Thomas Schöpping | /* configure some criticpal GPIOs as input
|
528 | * This is required, because otherwise some hardware might be powered through these signals */
|
||
529 | configGpioForShutdown(); |
||
530 | |||
531 | /* power down the system */
|
||
532 | systemPowerDown(); |
||
533 | |||
534 | /* deactivate the WKUP pin */
|
||
535 | PWR_WakeUpPinCmd(DISABLE); |
||
536 | |||
537 | /* deactivate any RTC related events */
|
||
538 | RTC_WakeUpCmd(DISABLE); |
||
539 | RTC_TamperCmd(RTC_Tamper_1, DISABLE); |
||
540 | RTC_TimeStampCmd(RTC_TimeStampEdge_Rising, DISABLE); |
||
541 | RTC_TimeStampCmd(RTC_TimeStampEdge_Falling, DISABLE); |
||
542 | RTC_ClearFlag(~0);
|
||
543 | |||
544 | /* disable the IWDG */
|
||
545 | IWDG_ReloadCounter(); |
||
546 | |||
547 | /* write some information to the backup register */
|
||
548 | blBackupRegister_t backup; |
||
549 | backup.shutdown_pri_reason = BL_SHUTDOWN_PRI_RSN_TRANSPORT; |
||
550 | backup.shutdown_sec_reason = BL_SHUTDOWN_SEC_RSN_UNKNOWN; |
||
551 | backup.wakeup_pri_reason = BL_WAKEUP_PRI_RSN_UNKNOWN; |
||
552 | backup.wakeup_sec_reason = BL_WAKEUP_SEC_RSN_UNKNOWN; |
||
553 | PWR_BackupAccessCmd(ENABLE); |
||
554 | RTC_WriteBackupRegister(BL_RTC_BACKUP_REG, backup.raw); |
||
555 | |||
556 | /* morse 'OK' via the LED to signal that shutdown was successful */
|
||
557 | blinkOK(1);
|
||
558 | |||
559 | /* enter standby mode */
|
||
560 | PWR_EnterSTANDBYMode(); |
||
561 | |||
562 | return;
|
||
563 | } /*** end of shutdownToTransportation ***/
|
||
564 | |||
565 | /*
|
||
566 | fc7151bb | Thomas Schöpping | * Final shutdown of the system to enter deepsleep mode.
|
567 | a270d48f | Thomas Schöpping | */
|
568 | 2b9a14a2 | Thomas Schöpping | void shutdownToDeepsleep(void) { |
569 | a270d48f | Thomas Schöpping | /* configure some criticpal GPIOs as input
|
570 | * This is required, because otherwise some hardware might be powered through these signals */
|
||
571 | configGpioForShutdown(); |
||
572 | |||
573 | /* power down the system */
|
||
574 | systemPowerDown(); |
||
575 | |||
576 | /* activate the WKUP pin */
|
||
577 | PWR_WakeUpPinCmd(ENABLE); |
||
578 | |||
579 | /*
|
||
580 | * Configuration of RTC and IWDG belongs to the OS.
|
||
581 | */
|
||
582 | |||
583 | /* write some information to the backup register */
|
||
584 | blBackupRegister_t backup; |
||
585 | backup.shutdown_pri_reason = BL_SHUTDOWN_PRI_RSN_DEEPSLEEP; |
||
586 | backup.shutdown_sec_reason = BL_SHUTDOWN_SEC_RSN_UNKNOWN; |
||
587 | backup.wakeup_pri_reason = BL_WAKEUP_PRI_RSN_UNKNOWN; |
||
588 | backup.wakeup_sec_reason = BL_WAKEUP_SEC_RSN_UNKNOWN; |
||
589 | PWR_BackupAccessCmd(ENABLE); |
||
590 | RTC_WriteBackupRegister(BL_RTC_BACKUP_REG, backup.raw); |
||
591 | |||
592 | /* morse 'OK' via the LED to signal that shutdown was successful */
|
||
593 | blinkOK(1);
|
||
594 | |||
595 | /* enter standby mode or restart the system in case a power plug is already present */
|
||
596 | if (GPIO_ReadInputDataBit(PATH_DC_GPIO, PATH_DC_PIN) != Bit_SET) {
|
||
597 | PWR_EnterSTANDBYMode(); |
||
598 | } else {
|
||
599 | NVIC_SystemReset(); |
||
600 | } |
||
601 | |||
602 | return;
|
||
603 | } /*** end of shutdownToDeepsleep ***/
|
||
604 | |||
605 | /*
|
||
606 | * Final shutdown of the system to enter hibernate mode.
|
||
607 | */
|
||
608 | 2b9a14a2 | Thomas Schöpping | void shutdownToHibernate(void) { |
609 | a270d48f | Thomas Schöpping | /* configure some criticpal GPIOs as input
|
610 | * This is required, because otherwise some hardware might be powered through these signals */
|
||
611 | configGpioForShutdown(); |
||
612 | |||
613 | /* power down the system */
|
||
614 | systemPowerDown(); |
||
615 | |||
616 | /* write some information to the backup register */
|
||
617 | blBackupRegister_t backup; |
||
618 | backup.shutdown_pri_reason = BL_SHUTDOWN_PRI_RSN_HIBERNATE; |
||
619 | backup.shutdown_sec_reason = BL_SHUTDOWN_SEC_RSN_UNKNOWN; |
||
620 | backup.wakeup_pri_reason = BL_WAKEUP_PRI_RSN_UNKNOWN; |
||
621 | backup.wakeup_sec_reason = BL_WAKEUP_SEC_RSN_UNKNOWN; |
||
622 | PWR_BackupAccessCmd(ENABLE); |
||
623 | RTC_WriteBackupRegister(BL_RTC_BACKUP_REG, backup.raw); |
||
624 | |||
625 | fc7151bb | Thomas Schöpping | /* morse 'OK' via the LED to signal that shutdown was successful */
|
626 | a270d48f | Thomas Schöpping | blinkOK(1);
|
627 | |||
628 | /* reset the MCU */
|
||
629 | NVIC_SystemReset(); |
||
630 | |||
631 | return;
|
||
632 | } /*** end of shutdownToHibernate ***/
|
||
633 | |||
634 | /*
|
||
635 | * Final shutdown of the system and restart.
|
||
636 | */
|
||
637 | 2b9a14a2 | Thomas Schöpping | void shutdownAndRestart(void) { |
638 | a270d48f | Thomas Schöpping | /* configure some criticpal GPIOs as input
|
639 | * This is required, because otherwise some hardware might be powered through these signals */
|
||
640 | configGpioForShutdown(); |
||
641 | |||
642 | /* power down the system */
|
||
643 | systemPowerDown(); |
||
644 | |||
645 | /* write some information to the backup register */
|
||
646 | blBackupRegister_t backup; |
||
647 | backup.shutdown_pri_reason = BL_SHUTDOWN_PRI_RSN_RESTART; |
||
648 | backup.shutdown_sec_reason = BL_SHUTDOWN_SEC_RSN_UNKNOWN; |
||
649 | backup.wakeup_pri_reason = BL_WAKEUP_PRI_RSN_UNKNOWN; |
||
650 | backup.wakeup_sec_reason = BL_WAKEUP_SEC_RSN_UNKNOWN; |
||
651 | PWR_BackupAccessCmd(ENABLE); |
||
652 | RTC_WriteBackupRegister(BL_RTC_BACKUP_REG, backup.raw); |
||
653 | |||
654 | fc7151bb | Thomas Schöpping | /* morse 'OK' via the LED to signal that shutdown was successful */
|
655 | a270d48f | Thomas Schöpping | blinkOK(1);
|
656 | |||
657 | /* reset the MCU */
|
||
658 | NVIC_SystemReset(); |
||
659 | |||
660 | return;
|
||
661 | } /*** end of shutdownAndRestart ***/
|
||
662 | |||
663 | /*
|
||
664 | * Configures some GPIO pins as inputs for safety reasons.
|
||
665 | * Under certain circumstances, these pins might power hardware that is supposed to be shut down.
|
||
666 | */
|
||
667 | 2b9a14a2 | Thomas Schöpping | void configGpioForShutdown(void) { |
668 | a270d48f | Thomas Schöpping | /* setup the configuration */
|
669 | fc7151bb | Thomas Schöpping | GPIO_InitTypeDef gpio_init; |
670 | a270d48f | Thomas Schöpping | gpio_init.GPIO_Mode = GPIO_Mode_IN; |
671 | gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
||
672 | gpio_init.GPIO_OType = GPIO_OType_PP; |
||
673 | gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL; |
||
674 | |||
675 | /* configure SYS_UART_TX */
|
||
676 | gpio_init.GPIO_Pin = SYS_UART_TX_PIN; |
||
677 | GPIO_Init(SYS_UART_TX_GPIO, &gpio_init); |
||
678 | |||
679 | /* configure all SYS_SPI signals */
|
||
680 | gpio_init.GPIO_Pin = SYS_SPI_SS0_N_PIN; |
||
681 | GPIO_Init(SYS_SPI_SS0_N_GPIO, &gpio_init); |
||
682 | gpio_init.GPIO_Pin = SYS_SPI_SCLK_PIN; |
||
683 | GPIO_Init(SYS_SPI_SCLK_GPIO, &gpio_init); |
||
684 | gpio_init.GPIO_Pin = SYS_SPI_MISO_PIN; |
||
685 | GPIO_Init(SYS_SPI_MISO_GPIO, &gpio_init); |
||
686 | gpio_init.GPIO_Pin = SYS_SPI_MOSI_PIN; |
||
687 | GPIO_Init(SYS_SPI_MOSI_GPIO, &gpio_init); |
||
688 | gpio_init.GPIO_Pin = SYS_SPI_SS1_N_PIN; |
||
689 | GPIO_Init(SYS_SPI_SS1_N_GPIO, &gpio_init); |
||
690 | gpio_init.GPIO_Pin = SYS_SPI_DIR_PIN; |
||
691 | GPIO_Init(SYS_SPI_DIR_GPIO, &gpio_init); |
||
692 | |||
693 | /* configure CAN_TX */
|
||
694 | gpio_init.GPIO_Pin = CAN_TX_PIN; |
||
695 | GPIO_Init(CAN_TX_GPIO, &gpio_init); |
||
696 | |||
697 | /* configure all Bluetooth signals */
|
||
698 | gpio_init.GPIO_Pin = BT_CTS_PIN; |
||
699 | GPIO_Init(BT_CTS_GPIO, &gpio_init); |
||
700 | gpio_init.GPIO_Pin = BT_RX_PIN; |
||
701 | GPIO_Init(BT_RX_GPIO, &gpio_init); |
||
702 | |||
703 | return;
|
||
704 | } /*** end of configGpioForShutdown ***/
|
||
705 | |||
706 | /*
|
||
707 | * Disables all regulated voltages and finally cuts power to the rest of the system.
|
||
708 | */
|
||
709 | 2b9a14a2 | Thomas Schöpping | void systemPowerDown(void) { |
710 | a270d48f | Thomas Schöpping | setLed(BLT_TRUE); |
711 | |||
712 | /* make sure that all other modules are shut down */
|
||
713 | msleep(10);
|
||
714 | |||
715 | /* reset slave modules */
|
||
716 | GPIO_ResetBits(SYS_WARMRST_N_GPIO, SYS_WARMRST_N_PIN); |
||
717 | |||
718 | /* disable voltage regulators */
|
||
719 | GPIO_ResetBits(SYS_REG_EN_GPIO, SYS_REG_EN_PIN); |
||
720 | |||
721 | /* cut power */
|
||
722 | GPIO_ResetBits(POWER_EN_GPIO, POWER_EN_PIN); |
||
723 | |||
724 | /* make sure, all capacitors are discharged */
|
||
725 | msleep(100);
|
||
726 | |||
727 | setLed(BLT_FALSE); |
||
728 | |||
729 | return;
|
||
730 | } /*** end of systemPowerDown ***/
|
||
731 | |||
732 | /*
|
||
733 | * Cofigures the independent watchdog (IWDG) to fire after the specified time when it is enabled.
|
||
734 | * The argument is the requested time in milliseconds.
|
||
735 | * The time that was actually set for the IWDG is returned by the function (again in milliseconds).
|
||
736 | * In some cases the returned value might differ from the requested one, but if so, it will alwyas be smaller.
|
||
737 | * Although the IWDG provides |