amiro-blt / Target / Modules / LightRing_1-2 / Boot / main.c @ 43464dd5
History | View | Annotate | Download (33.445 KB)
| 1 | fc7151bb | Thomas Schöpping | /************************************************************************************//** |
|---|---|---|---|
| 2 | * \file Demo\ARMCM3_STM32_Olimex_STM32P103_GCC\Boot\main.c
|
||
| 3 | * \brief Bootloader application source file.
|
||
| 4 | * \ingroup Boot_ARMCM3_STM32_Olimex_STM32P103_GCC
|
||
| 5 | * \internal
|
||
| 6 | *----------------------------------------------------------------------------------------
|
||
| 7 | * C O P Y R I G H T
|
||
| 8 | *----------------------------------------------------------------------------------------
|
||
| 9 | * Copyright (c) 2012 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 "timer.h" |
||
| 39 | #include "ARMCM3_STM32/types.h" |
||
| 40 | #include "AMiRo/amiroblt.h" |
||
| 41 | #include "helper.h" |
||
| 42 | #include "iodef.h" |
||
| 43 | |||
| 44 | /****************************************************************************************
|
||
| 45 | * Defines
|
||
| 46 | ****************************************************************************************/
|
||
| 47 | |||
| 48 | /****************************************************************************************
|
||
| 49 | * Function prototypes and static variables
|
||
| 50 | ****************************************************************************************/
|
||
| 51 | static void Init(void); |
||
| 52 | |||
| 53 | static void initGpio(); |
||
| 54 | static void initExti(); |
||
| 55 | void configGpioForShutdown();
|
||
| 56 | |||
| 57 | ErrorStatus handleWarmReset(); |
||
| 58 | |||
| 59 | ErrorStatus shutdownDisambiguationProcedure(const uint8_t type);
|
||
| 60 | void shutdownToTransportation(const blt_bool exec_disambiguation); |
||
| 61 | void shutdownToDeepsleep(const blt_bool exec_disambiguation); |
||
| 62 | void shutdownToHibernate(const blt_bool exec_disambiguation); |
||
| 63 | void shutdownAndRestart(const blt_bool exec_disambiguation); |
||
| 64 | |||
| 65 | volatile blBackupRegister_t backup_reg;
|
||
| 66 | |||
| 67 | /****************************************************************************************
|
||
| 68 | * Callback configuration
|
||
| 69 | ****************************************************************************************/
|
||
| 70 | void blCallbackShutdownTransportation(void); |
||
| 71 | void blCallbackShutdownDeepsleep(void); |
||
| 72 | void blCallbackShutdownHibernate(void); |
||
| 73 | void blCallbackShutdownRestart(void); |
||
| 74 | void blCallbackHandleShutdownRequest(void); |
||
| 75 | |||
| 76 | const blCallbackTable_t cbtable __attribute__ ((section ("_callback_table"))) = { |
||
| 77 | .magicNumber = BL_MAGIC_NUMBER, |
||
| 78 | 4745cc4c | Thomas Schöpping | .vBootloader = {BL_VERSION_ID_AMiRoBLT_Release, BL_VERSION_MAJOR, BL_VERSION_MINOR, 0},
|
| 79 | fc7151bb | Thomas Schöpping | .vSSSP = {BL_VERSION_ID_SSSP, BL_SSSP_VERSION_MAJOR, BL_SSSP_VERSION_MINOR, 0},
|
| 80 | .vCompiler = {BL_VERSION_ID_GCC, __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__}, // currently only GCC is supported
|
||
| 81 | .cbShutdownHibernate = blCallbackShutdownHibernate, |
||
| 82 | .cbShutdownDeepsleep = blCallbackShutdownDeepsleep, |
||
| 83 | .cbShutdownTransportation = blCallbackShutdownTransportation, |
||
| 84 | .cbShutdownRestart = blCallbackShutdownRestart, |
||
| 85 | .cbHandleShutdownRequest = blCallbackHandleShutdownRequest, |
||
| 86 | .cb5 = (void*)0, |
||
| 87 | .cb6 = (void*)0, |
||
| 88 | .cb7 = (void*)0, |
||
| 89 | .cb8 = (void*)0, |
||
| 90 | .cb9 = (void*)0, |
||
| 91 | .cb10 = (void*)0, |
||
| 92 | .cb11 = (void*)0 |
||
| 93 | }; |
||
| 94 | |||
| 95 | /************************************************************************************//** |
||
| 96 | ** \brief This is the entry point for the bootloader application and is called
|
||
| 97 | ** by the reset interrupt vector after the C-startup routines executed.
|
||
| 98 | ** \return Program return code.
|
||
| 99 | **
|
||
| 100 | ****************************************************************************************/
|
||
| 101 | int main(void) |
||
| 102 | {
|
||
| 103 | /* initialize the microcontroller */
|
||
| 104 | Init(); |
||
| 105 | |||
| 106 | /* activate some required clocks */
|
||
| 107 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); |
||
| 108 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE); |
||
| 109 | |||
| 110 | /* initialize GPIOs and EXTI lines */
|
||
| 111 | initGpio(); |
||
| 112 | setLed(BLT_TRUE); |
||
| 113 | initExti(); |
||
| 114 | |||
| 115 | /* initialize the timer */
|
||
| 116 | TimerInit(); |
||
| 117 | |||
| 118 | /* detect the primary reason for this wakeup/restart */
|
||
| 119 | backup_reg.wakeup_pri_reason = |
||
| 120 | ((RCC_GetFlagStatus(RCC_FLAG_LPWRRST) == SET) ? BL_WAKEUP_PRI_RSN_LPWRRST : 0) |
|
||
| 121 | ((RCC_GetFlagStatus(RCC_FLAG_WWDGRST) == SET) ? BL_WAKEUP_PRI_RSN_WWDGRST : 0) |
|
||
| 122 | ((RCC_GetFlagStatus(RCC_FLAG_IWDGRST) == SET) ? BL_WAKEUP_PRI_RSN_IWDGRST : 0) |
|
||
| 123 | ((RCC_GetFlagStatus(RCC_FLAG_SFTRST) == SET) ? BL_WAKEUP_PRI_RSN_SFTRST : 0) |
|
||
| 124 | ((RCC_GetFlagStatus(RCC_FLAG_PORRST) == SET) ? BL_WAKEUP_PRI_RSN_PORRST : 0) |
|
||
| 125 | ((RCC_GetFlagStatus(RCC_FLAG_PINRST) == SET) ? BL_WAKEUP_PRI_RSN_PINRST : 0) |
|
||
| 126 | ((PWR_GetFlagStatus(PWR_FLAG_WU) == SET) ? BL_WAKEUP_PRI_RSN_WKUP : 0);
|
||
| 127 | /* for this module there is no secondary wakeup reason */
|
||
| 128 | backup_reg.wakeup_sec_reason = BL_WAKEUP_SEC_RSN_UNKNOWN; |
||
| 129 | |||
| 130 | /* clear the flags */
|
||
| 131 | RCC_ClearFlag(); |
||
| 132 | PWR_ClearFlag(PWR_FLAG_WU); |
||
| 133 | |||
| 134 | setLed(BLT_FALSE); |
||
| 135 | |||
| 136 | /* handle different wakeup/reset reasons */
|
||
| 137 | ErrorStatus status = ERROR; |
||
| 138 | if (backup_reg.wakeup_pri_reason & BL_WAKEUP_PRI_RSN_PINRST) {
|
||
| 139 | /* system was woken via NRST pin */
|
||
| 140 | status = handleWarmReset(); |
||
| 141 | } else {
|
||
| 142 | /* system was woken/reset for an unexpected reason */
|
||
| 143 | blinkSOS(1);
|
||
| 144 | status = handleWarmReset(); |
||
| 145 | } |
||
| 146 | |||
| 147 | /* if something went wrong, signal this failure */
|
||
| 148 | if (status != SUCCESS) {
|
||
| 149 | blinkSOSinf(); |
||
| 150 | } |
||
| 151 | |||
| 152 | return 0; |
||
| 153 | } /*** end of main ***/
|
||
| 154 | |||
| 155 | |||
| 156 | /************************************************************************************//** |
||
| 157 | ** \brief Initializes the microcontroller.
|
||
| 158 | ** \return none.
|
||
| 159 | **
|
||
| 160 | ****************************************************************************************/
|
||
| 161 | static void Init(void) |
||
| 162 | {
|
||
| 163 | volatile blt_int32u StartUpCounter = 0, HSEStatus = 0; |
||
| 164 | blt_int32u pll_multiplier; |
||
| 165 | #if (BOOT_FILE_LOGGING_ENABLE > 0) && (BOOT_COM_UART_ENABLE == 0) && (BOOT_GATE_UART_ENABLE == 0) |
||
| 166 | GPIO_InitTypeDef GPIO_InitStruct; |
||
| 167 | USART_InitTypeDef USART_InitStruct; |
||
| 168 | #endif
|
||
| 169 | |||
| 170 | /* reset the RCC clock configuration to the default reset state (for debug purpose) */
|
||
| 171 | /* set HSION bit */
|
||
| 172 | RCC->CR |= (blt_int32u)0x00000001;
|
||
| 173 | /* reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
|
||
| 174 | RCC->CFGR &= (blt_int32u)0xF8FF0000;
|
||
| 175 | /* reset HSEON, CSSON and PLLON bits */
|
||
| 176 | RCC->CR &= (blt_int32u)0xFEF6FFFF;
|
||
| 177 | /* reset HSEBYP bit */
|
||
| 178 | RCC->CR &= (blt_int32u)0xFFFBFFFF;
|
||
| 179 | /* reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
|
||
| 180 | RCC->CFGR &= (blt_int32u)0xFF80FFFF;
|
||
| 181 | /* disable all interrupts and clear pending bits */
|
||
| 182 | RCC->CIR = 0x009F0000;
|
||
| 183 | /* enable HSE */
|
||
| 184 | RCC->CR |= ((blt_int32u)RCC_CR_HSEON); |
||
| 185 | /* wait till HSE is ready and if Time out is reached exit */
|
||
| 186 | do
|
||
| 187 | {
|
||
| 188 | HSEStatus = RCC->CR & RCC_CR_HSERDY; |
||
| 189 | StartUpCounter++; |
||
| 190 | } |
||
| 191 | while((HSEStatus == 0) && (StartUpCounter != 1500)); |
||
| 192 | /* check if time out was reached */
|
||
| 193 | if ((RCC->CR & RCC_CR_HSERDY) == RESET)
|
||
| 194 | {
|
||
| 195 | /* cannot continue when HSE is not ready */
|
||
| 196 | ASSERT_RT(BLT_FALSE); |
||
| 197 | } |
||
| 198 | /* enable flash prefetch buffer */
|
||
| 199 | FLASH->ACR |= FLASH_ACR_PRFTBE; |
||
| 200 | /* reset flash wait state configuration to default 0 wait states */
|
||
| 201 | FLASH->ACR &= (blt_int32u)((blt_int32u)~FLASH_ACR_LATENCY); |
||
| 202 | #if (BOOT_CPU_SYSTEM_SPEED_KHZ > 48000) |
||
| 203 | /* configure 2 flash wait states */
|
||
| 204 | FLASH->ACR |= (blt_int32u)FLASH_ACR_LATENCY_2; |
||
| 205 | #elif (BOOT_CPU_SYSTEM_SPEED_KHZ > 24000) |
||
| 206 | /* configure 1 flash wait states */
|
||
| 207 | FLASH->ACR |= (blt_int32u)FLASH_ACR_LATENCY_1; |
||
| 208 | #endif
|
||
| 209 | /* HCLK = SYSCLK */
|
||
| 210 | RCC->CFGR |= (blt_int32u)RCC_CFGR_HPRE_DIV1; |
||
| 211 | /* PCLK2 = HCLK/2 */
|
||
| 212 | RCC->CFGR |= (blt_int32u)RCC_CFGR_PPRE2_DIV2; |
||
| 213 | /* PCLK1 = HCLK/2 */
|
||
| 214 | RCC->CFGR |= (blt_int32u)RCC_CFGR_PPRE1_DIV2; |
||
| 215 | /* reset PLL configuration */
|
||
| 216 | RCC->CFGR &= (blt_int32u)((blt_int32u)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | \ |
||
| 217 | RCC_CFGR_PLLMULL)); |
||
| 218 | /* assert that the pll_multiplier is between 2 and 16 */
|
||
| 219 | ASSERT_CT((BOOT_CPU_SYSTEM_SPEED_KHZ/BOOT_CPU_XTAL_SPEED_KHZ) >= 2);
|
||
| 220 | ASSERT_CT((BOOT_CPU_SYSTEM_SPEED_KHZ/BOOT_CPU_XTAL_SPEED_KHZ) <= 16);
|
||
| 221 | /* calculate multiplier value */
|
||
| 222 | pll_multiplier = BOOT_CPU_SYSTEM_SPEED_KHZ/BOOT_CPU_XTAL_SPEED_KHZ; |
||
| 223 | /* convert to register value */
|
||
| 224 | pll_multiplier = (blt_int32u)((pll_multiplier - 2) << 18); |
||
| 225 | /* set the PLL multiplier and clock source */
|
||
| 226 | RCC->CFGR |= (blt_int32u)(RCC_CFGR_PLLSRC_HSE | pll_multiplier); |
||
| 227 | /* enable PLL */
|
||
| 228 | RCC->CR |= RCC_CR_PLLON; |
||
| 229 | /* wait till PLL is ready */
|
||
| 230 | while((RCC->CR & RCC_CR_PLLRDY) == 0) |
||
| 231 | {
|
||
| 232 | } |
||
| 233 | /* select PLL as system clock source */
|
||
| 234 | RCC->CFGR &= (blt_int32u)((blt_int32u)~(RCC_CFGR_SW)); |
||
| 235 | RCC->CFGR |= (blt_int32u)RCC_CFGR_SW_PLL; |
||
| 236 | /* wait till PLL is used as system clock source */
|
||
| 237 | while ((RCC->CFGR & (blt_int32u)RCC_CFGR_SWS) != (blt_int32u)0x08) |
||
| 238 | {
|
||
| 239 | } |
||
| 240 | #if (BOOT_COM_CAN_ENABLE > 0 || BOOT_GATE_CAN_ENABLE > 0) |
||
| 241 | /* enable clocks for CAN transmitter and receiver pins (GPIOB and AFIO) */
|
||
| 242 | RCC->APB2ENR |= (blt_int32u)(0x00000004 | 0x00000001); |
||