amiro-blt / Target / Modules / PowerManagement_1-1 / Boot / main.c @ 768babc5
History | View | Annotate | Download (57.195 KB)
| 1 | 69661903 | 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 | 470d0567 | Thomas Schöpping | * 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 | 69661903 | Thomas Schöpping | * proprietary components. The exception text is included at the bottom of the license
|
| 29 | * file <license.html>.
|
||
| 30 | 470d0567 | Thomas Schöpping | *
|
| 31 | 69661903 | Thomas Schöpping | * \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 | 470d0567 | Thomas Schöpping | #include "AMiRo/amiroblt.h" |
| 41 | 2d379838 | Thomas Schöpping | #include "helper.h" |
| 42 | 09ad5212 | Thomas Schöpping | #include "iodef.h" |
| 43 | 69661903 | Thomas Schöpping | |
| 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 | 69661903 | 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 | 69661903 | Thomas Schöpping | |
| 66 | 2b9a14a2 | Thomas Schöpping | static void indicateHibernate(void); |
| 67 | static void AdcSingleMeasurement(void); |
||
| 68 | 69661903 | 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 | 69661903 | Thomas Schöpping | |
| 78 | 470d0567 | Thomas Schöpping | volatile blBackupRegister_t backup_reg;
|
| 79 | 69661903 | Thomas Schöpping | |
| 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 | 470d0567 | Thomas Schöpping | const blCallbackTable_t cbtable __attribute__ ((section ("_callback_table"))) = { |
| 90 | 69661903 | Thomas Schöpping | .magicNumber = BL_MAGIC_NUMBER, |
| 91 | 4745cc4c | Thomas Schöpping | .vBootloader = {BL_VERSION_ID_AMiRoBLT_Release, BL_VERSION_MAJOR, BL_VERSION_MINOR, 0},
|
| 92 | f3866909 | Thomas Schöpping | .vSSSP = {BL_VERSION_ID_SSSP, BL_SSSP_VERSION_MAJOR, BL_SSSP_VERSION_MINOR, 0},
|
| 93 | 470d0567 | Thomas Schöpping | .vCompiler = {BL_VERSION_ID_GCC, __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__}, // currently only GCC is supported
|
| 94 | 69661903 | Thomas Schöpping | .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 | 470d0567 | Thomas Schöpping | ** \brief This is the entry point for the bootloader application and is called
|
| 110 | 69661903 | Thomas Schöpping | ** 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 | 69661903 | 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;
|
||