amiro-blt / Target / Modules / DiWheelDrive_1-1 / Boot / main.c @ 367c0652
History | View | Annotate | Download (39.265 KB)
| 1 | 69661903 | 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 | 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 "stm32f10x.h" /* microcontroller registers */ |
||
| 39 | #include "stm32f10x_conf.h" /* STM32 peripheral drivers */ |
||
| 40 | #include "timer.h" |
||
| 41 | #include "ARMCM3_STM32/types.h" |
||
| 42 | 470d0567 | Thomas Schöpping | #include "AMiRo/amiroblt.h" |
| 43 | 69661903 | Thomas Schöpping | #include "AMiRo/helper.h" |
| 44 | |||
| 45 | |||
| 46 | /****************************************************************************************
|
||
| 47 | * Defines
|
||
| 48 | ****************************************************************************************/
|
||
| 49 | #define WKUP_GPIO GPIOA
|
||
| 50 | #define WKUP_PIN GPIO_Pin_0
|
||
| 51 | #define LED_GPIO GPIOA
|
||
| 52 | #define LED_PIN GPIO_Pin_1
|
||
| 53 | #define DRIVE_PWM1A_GPIO GPIOA
|
||
| 54 | #define DRIVE_PWM1A_PIN GPIO_Pin_2
|
||
| 55 | #define DRIVE_PWM1B_GPIO GPIOA
|
||
| 56 | #define DRIVE_PWM1B_PIN GPIO_Pin_3
|
||
| 57 | #define MOTION_SCLK_GPIO GPIOA
|
||
| 58 | #define MOTION_SCLK_PIN GPIO_Pin_5
|
||
| 59 | #define MOTION_MISO_GPIO GPIOA
|
||
| 60 | #define MOTION_MISO_PIN GPIO_Pin_6
|
||
| 61 | #define MOTION_MOSI_GPIO GPIOA
|
||
| 62 | #define MOTION_MOSI_PIN GPIO_Pin_7
|
||
| 63 | #define PROG_RX_GPIO GPIOA
|
||
| 64 | #define PROG_RX_PIN GPIO_Pin_9
|
||
| 65 | #define PROG_TX_GPIO GPIOA
|
||
| 66 | #define PROG_TX_PIN GPIO_Pin_10
|
||
| 67 | #define CAN_RX_GPIO GPIOA
|
||
| 68 | #define CAN_RX_PIN GPIO_Pin_11
|
||
| 69 | #define CAN_TX_GPIO GPIOA
|
||
| 70 | #define CAN_TX_PIN GPIO_Pin_12
|
||
| 71 | #define SWDIO_GPIO GPIOA
|
||
| 72 | #define SWDIO_PIN GPIO_Pin_13
|
||
| 73 | #define SWCLK_GPIO GPIOA
|
||
| 74 | #define SWCLK_PIN GPIO_Pin_14
|
||
| 75 | #define DRIVE_PWM2B_GPIO GPIOA
|
||
| 76 | #define DRIVE_PWM2B_PIN GPIO_Pin_15
|
||
| 77 | |||
| 78 | #define DRIVE_SENSE2_GPIO GPIOB
|
||
| 79 | #define DRIVE_SENSE2_PIN GPIO_Pin_1
|
||
| 80 | #define POWER_EN_GPIO GPIOB
|
||
| 81 | #define POWER_EN_PIN GPIO_Pin_2
|
||
| 82 | #define DRIVE_PWM2A_GPIO GPIOB
|
||
| 83 | #define DRIVE_PWM2A_PIN GPIO_Pin_3
|
||
| 84 | #define COMPASS_DRDY_GPIO GPIOB
|
||
| 85 | #define COMPASS_DRDY_PIN GPIO_Pin_5
|
||
| 86 | #define DRIVE_ENC1A_GPIO GPIOB
|
||
| 87 | #define DRIVE_ENC1A_PIN GPIO_Pin_6
|
||
| 88 | #define DRIVE_ENC1B_GPIO GPIOB
|
||
| 89 | #define DRIVE_ENC1B_PIN GPIO_Pin_7
|
||
| 90 | #define COMPASS_SCL_GPIO GPIOB
|
||
| 91 | #define COMPASS_SCL_PIN GPIO_Pin_8
|
||
| 92 | #define COMPASS_SDA_GPIO GPIOB
|
||
| 93 | #define COMPASS_SDA_PIN GPIO_Pin_9
|
||
| 94 | #define IR_SCL_GPIO GPIOB
|
||
| 95 | #define IR_SCL_PIN GPIO_Pin_10
|
||
| 96 | #define IR_SDA_GPIO GPIOB
|
||
| 97 | #define IR_SDA_PIN GPIO_Pin_11
|
||
| 98 | #define IR_INT_GPIO GPIOB
|
||
| 99 | #define IR_INT_PIN GPIO_Pin_12
|
||
| 100 | #define GYRP_DRDY_GPIO GPIOB
|
||
| 101 | #define GYRO_DRDY_PIN GPIO_Pin_13
|
||
| 102 | #define SYS_UART_UP_GPIO GPIOB
|
||
| 103 | #define SYS_UART_UP_PIN GPIO_Pin_14
|
||
| 104 | #define ACCEL_INT_N_GPIO GPIOB
|
||
| 105 | #define ACCEL_INT_N_PIN GPIO_Pin_15
|
||
| 106 | |||
| 107 | #define DRIVE_SENSE1_GPIO GPIOC
|
||
| 108 | #define DRIVE_SENSE1_PIN GPIO_Pin_0
|
||
| 109 | #define SYS_SYNC_N_GPIO GPIOC
|
||
| 110 | #define SYS_SYNC_N_PIN GPIO_Pin_1
|
||
| 111 | #define PATH_DCSTAT_GPIO GPIOC
|
||
| 112 | #define PATH_DCSTAT_PIN GPIO_Pin_3
|
||
| 113 | #define PATH_DCEN_GPIO GPIOC
|
||
| 114 | #define PATH_DCEN_PIN GPIO_Pin_5
|
||
| 115 | #define DRIVE_ENC2B_GPIO GPIOC
|
||
| 116 | #define DRIVE_ENC2B_PIN GPIO_Pin_6
|
||
| 117 | #define DRIVE_ENC2A_GPIO GPIOC
|
||
| 118 | #define DRIVE_ENC2A_PIN GPIO_Pin_7
|
||
| 119 | #define SYS_PD_N_GPIO GPIOC
|
||
| 120 | #define SYS_PD_N_PIN GPIO_Pin_8
|
||
| 121 | #define SYS_REG_EN_GPIO GPIOC
|
||
| 122 | #define SYS_REG_EN_PIN GPIO_Pin_9
|
||
| 123 | #define SYS_UART_RX_GPIO GPIOC
|
||
| 124 | #define SYS_UART_RX_PIN GPIO_Pin_10
|
||
| 125 | #define SYS_UART_TX_GPIO GPIOC
|
||
| 126 | #define SYS_UART_TX_PIN GPIO_Pin_11
|
||
| 127 | #define ACCEL_SS_N_GPIO GPIOC
|
||
| 128 | #define ACCEL_SS_N_PIN GPIO_Pin_13
|
||
| 129 | #define GYRO_SS_N_GPIO GPIOC
|
||
| 130 | #define GYRO_SS_N_PIN GPIO_Pin_14
|
||
| 131 | |||
| 132 | #define OSC_IN_GPIO GPIOD
|
||
| 133 | #define OSC_IN_PIN GPIO_Pin_0
|
||
| 134 | #define OSC_OUT_GPIO GPIOD
|
||
| 135 | #define OSC_OUT_PIN GPIO_Pin_1
|
||
| 136 | #define SYS_WARMRST_N_GPIO GPIOD
|
||
| 137 | #define SYS_WARMRST_N_PIN GPIO_Pin_2
|
||
| 138 | |||
| 139 | #define RESET_TIMEOUT_MS 100 |
||
| 140 | |||
| 141 | /****************************************************************************************
|
||
| 142 | * Function prototypes
|
||
| 143 | ****************************************************************************************/
|
||
| 144 | static void Init(void); |
||
| 145 | |||
| 146 | static void initGpio(); |
||
| 147 | static void initExti(); |
||
| 148 | void configGpioForShutdown();
|
||
| 149 | |||
| 150 | ErrorStatus handleColdReset(); |
||
| 151 | ErrorStatus handleUartWakeup(); |
||
| 152 | ErrorStatus handleAccelWakeup(); |
||
| 153 | |||
| 154 | ErrorStatus shutdownDisambiguationProcedure(const uint8_t type);
|
||
| 155 | void shutdownToTransportation(const blt_bool exec_disambiguation); |
||
| 156 | void shutdownToDeepsleep(const blt_bool exec_disambiguation); |
||
| 157 | void shutdownToHibernate(const blt_bool exec_disambiguation); |
||
| 158 | void shutdownAndRestart(const blt_bool exec_disambiguation); |
||
| 159 | |||
| 160 | 470d0567 | Thomas Schöpping | volatile blBackupRegister_t backup_reg;
|
| 161 | 69661903 | Thomas Schöpping | |
| 162 | /****************************************************************************************
|
||
| 163 | * Callback configuration
|
||
| 164 | ****************************************************************************************/
|
||
| 165 | void blCallbackShutdownTransportation(void); |
||
| 166 | void blCallbackShutdownDeepsleep(void); |
||
| 167 | void blCallbackShutdownHibernate(void); |
||
| 168 | void blCallbackShutdownRestart(void); |
||
| 169 | void blCallbackHandleShutdownRequest(void); |
||
| 170 | |||
| 171 | 470d0567 | Thomas Schöpping | const blCallbackTable_t cbtable __attribute__ ((section ("_callback_table"))) = { |
| 172 | 69661903 | Thomas Schöpping | .magicNumber = BL_MAGIC_NUMBER, |
| 173 | f4758731 | Thomas Schöpping | .vBootloader = {BL_VERSION_ID_AMiRoBLT_Release, BL_VERSION_MAJOR, BL_VERSION_MINOR, 1},
|
| 174 | 470d0567 | Thomas Schöpping | .vSSSP = {BL_VERSION_ID_SSSP, SSSP_VERSION_MAJOR, SSSP_VERSION_MINOR, 0},
|
| 175 | .vCompiler = {BL_VERSION_ID_GCC, __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__}, // currently only GCC is supported
|
||
| 176 | 69661903 | Thomas Schöpping | .cbShutdownHibernate = blCallbackShutdownHibernate, |
| 177 | .cbShutdownDeepsleep = blCallbackShutdownDeepsleep, |
||
| 178 | .cbShutdownTransportation = blCallbackShutdownTransportation, |
||
| 179 | .cbShutdownRestart = blCallbackShutdownRestart, |
||
| 180 | .cbHandleShutdownRequest = blCallbackHandleShutdownRequest, |
||
| 181 | .cb5 = (void*)0, |
||
| 182 | .cb6 = (void*)0, |
||
| 183 | .cb7 = (void*)0, |
||
| 184 | .cb8 = (void*)0, |
||
| 185 | .cb9 = (void*)0, |
||
| 186 | .cb10 = (void*)0, |
||
| 187 | .cb11 = (void*)0 |
||
| 188 | }; |
||
| 189 | |||
| 190 | |||
| 191 | /************************************************************************************//** |
||
| 192 | 470d0567 | Thomas Schöpping | ** \brief This is the entry point for the bootloader application and is called
|
| 193 | 69661903 | Thomas Schöpping | ** by the reset interrupt vector after the C-startup routines executed.
|
| 194 | ** \return Program return code.
|
||
| 195 | **
|
||
| 196 | ****************************************************************************************/
|
||
| 197 | int main(void) |
||
| 198 | {
|
||
| 199 | /* initialize the microcontroller */
|
||
| 200 | Init(); |
||
| 201 | |||
| 202 | /* activate some required clocks */
|
||
| 203 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); |
||
| 204 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE); |
||
| 205 | |||
| 206 | /* initialize GPIOs and EXTI lines */
|
||
| 207 | initGpio(); |
||
| 208 | setLed(BLT_TRUE); |
||
| 209 | initExti(); |
||
| 210 | |||
| 211 | /* initialize the timer */
|
||
| 212 | TimerInit(); |
||
| 213 | |||
| 214 | /* detect the primary reason for this wakeup/restart */
|
||
| 215 | backup_reg.wakeup_pri_reason = |
||
| 216 | ((RCC_GetFlagStatus(RCC_FLAG_LPWRRST) == SET) ? BL_WAKEUP_PRI_RSN_LPWRRST : 0) |
|
||
| 217 | ((RCC_GetFlagStatus(RCC_FLAG_WWDGRST) == SET) ? BL_WAKEUP_PRI_RSN_WWDGRST : 0) |
|
||
| 218 | ((RCC_GetFlagStatus(RCC_FLAG_IWDGRST) == SET) ? BL_WAKEUP_PRI_RSN_IWDGRST : 0) |
|
||
| 219 | ((RCC_GetFlagStatus(RCC_FLAG_SFTRST) == SET) ? BL_WAKEUP_PRI_RSN_SFTRST : 0) |
|
||
| 220 | ((RCC_GetFlagStatus(RCC_FLAG_PORRST) == SET) ? BL_WAKEUP_PRI_RSN_PORRST : 0) |
|
||
| 221 | ((RCC_GetFlagStatus(RCC_FLAG_PINRST) == SET) ? BL_WAKEUP_PRI_RSN_PINRST : 0) |
|
||
| 222 | ((PWR_GetFlagStatus(PWR_FLAG_WU) == SET) ? BL_WAKEUP_PRI_RSN_WKUP : 0);
|
||
| 223 | |||
| 224 | /* when woken from standby mode, detect the secondary reason for thiswakeup/reset */
|
||
| 225 | if ( (backup_reg.wakeup_pri_reason & BL_WAKEUP_PRI_RSN_WKUP) && (PWR_GetFlagStatus(PWR_FLAG_SB) == SET) ) {
|
||
| 226 | if (GPIO_ReadInputDataBit(SYS_UART_UP_GPIO, SYS_UART_UP_PIN) == Bit_SET) {
|
||
| 227 | backup_reg.wakeup_sec_reason = BL_WAKEUP_SEC_RSN_UART; |
||
| 228 | } else {
|
||
| 229 | backup_reg.wakeup_sec_reason = BL_WAKEUP_SEC_RSN_ACCEL; |
||
| 230 | } |
||
| 231 | } else {
|
||
| 232 | backup_reg.wakeup_sec_reason = BL_WAKEUP_SEC_RSN_UNKNOWN; |
||
| 233 | } |
||
| 234 | |||
| 235 | /* clear the flags */
|
||
| 236 | RCC_ClearFlag(); |
||
| 237 | PWR_ClearFlag(PWR_FLAG_WU); |
||
| 238 | |||
| 239 | setLed(BLT_FALSE); |
||
| 240 | |||
| 241 | /* wait 1ms for all signals to become stable */
|
||
| 242 | msleep(1);
|
||
| 243 | |||
| 244 | /* handle different wakeup/reset reasons */
|
||
| 245 | ErrorStatus status = ERROR; |
||
| 246 | if (backup_reg.wakeup_pri_reason & BL_WAKEUP_PRI_RSN_WKUP) {
|
||
| 247 | /* the system was woken via WKUP pin */
|
||
| 248 | /* differenciate between two wakeup types */
|
||
| 249 | switch (backup_reg.wakeup_sec_reason) {
|
||
| 250 | case BL_WAKEUP_SEC_RSN_UART:
|
||
| 251 | status = handleUartWakeup(); |
||
| 252 | break;
|
||
| 253 | case BL_WAKEUP_SEC_RSN_ACCEL:
|
||
| 254 | status = handleAccelWakeup(); |
||
| 255 | break;
|
||
| 256 | default:
|
||
| 257 | status = ERROR; |
||
| 258 | break;
|
||
| 259 | } |
||
| 260 | } else if (backup_reg.wakeup_pri_reason & BL_WAKEUP_PRI_RSN_PINRST) { |
||
| 261 | /* system was woken via NRST pin */
|
||
| 262 | status = handleColdReset(); |
||
| 263 | } else {
|
||
| 264 | /* system was woken/reset for an unexpected reason */
|
||
| 265 | blinkSOS(1);
|
||
| 266 | status = handleColdReset(); |
||
| 267 | } |
||
| 268 | |||
| 269 | /* if something wehnt wrong, signal this failure */
|
||
| 270 | if (status != SUCCESS) {
|
||
| 271 | blinkSOSinf(); |
||
| 272 | } |
||
| 273 | |||
| 274 | return 0; |
||
| 275 | } /*** end of main ***/
|
||
| 276 | |||
| 277 | |||
| 278 | /************************************************************************************//** |
||
| 279 | 470d0567 | Thomas Schöpping | ** \brief Initializes the microcontroller.
|
| 280 | 69661903 | Thomas Schöpping | ** \return none.
|
| 281 | **
|
||
| 282 | ****************************************************************************************/
|
||
| 283 | static void Init(void) |
||
| 284 | {
|
||
| 285 | volatile blt_int32u StartUpCounter = 0, HSEStatus = 0; |
||
| 286 | blt_int32u pll_multiplier; |
||
| 287 | #if (BOOT_FILE_LOGGING_ENABLE > 0) && (BOOT_COM_UART_ENABLE == 0) |
||
| 288 | GPIO_InitTypeDef GPIO_InitStruct; |
||
| 289 | 470d0567 | Thomas Schöpping | USART_InitTypeDef USART_InitStruct; |
| 290 | #endif
|
||
| 291 | 69661903 | Thomas Schöpping | |
| 292 | /* reset the RCC clock configuration to the default reset state (for debug purpose) */
|
||
| 293 | /* set HSION bit */
|
||
| 294 | RCC->CR |= (blt_int32u)0x00000001;
|
||
| 295 | /* reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */
|
||
| 296 | RCC->CFGR &= (blt_int32u)0xF8FF0000;
|
||
| 297 | /* reset HSEON, CSSON and PLLON bits */
|
||
| 298 | RCC->CR &= (blt_int32u)0xFEF6FFFF;
|
||
| 299 | /* reset HSEBYP bit */
|
||
| 300 | RCC->CR &= (blt_int32u)0xFFFBFFFF;
|
||
| 301 | /* reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */
|
||
| 302 | RCC->CFGR &= (blt_int32u)0xFF80FFFF;
|
||
| 303 | /* disable all interrupts and clear pending bits */
|
||
| 304 | RCC->CIR = 0x009F0000;
|
||
| 305 | 470d0567 | Thomas Schöpping | /* enable HSE */
|
| 306 | 69661903 | Thomas Schöpping | RCC->CR |= ((blt_int32u)RCC_CR_HSEON); |
| 307 | /* wait till HSE is ready and if Time out is reached exit */
|
||
| 308 | do
|
||
| 309 | {
|
||
| 310 | HSEStatus = RCC->CR & RCC_CR_HSERDY; |
||
| 311 | 470d0567 | Thomas Schöpping | StartUpCounter++; |
| 312 | } |
||
| 313 | 69661903 | Thomas Schöpping | while((HSEStatus == 0) && (StartUpCounter != 1500)); |
| 314 | /* check if time out was reached */
|
||
| 315 | if ((RCC->CR & RCC_CR_HSERDY) == RESET)
|
||
| 316 | {
|
||
| 317 | /* cannot continue when HSE is not ready */
|
||
| 318 | ASSERT_RT(BLT_FALSE); |
||
| 319 | } |
||
| 320 | /* enable flash prefetch buffer */
|
||
| 321 | FLASH->ACR |= FLASH_ACR_PRFTBE; |
||
| 322 | /* reset flash wait state configuration to default 0 wait states */
|
||
| 323 | FLASH->ACR &= (blt_int32u)((blt_int32u)~FLASH_ACR_LATENCY); |
||
| 324 | #if (BOOT_CPU_SYSTEM_SPEED_KHZ > 48000) |
||
| 325 | /* configure 2 flash wait states */
|
||
| 326 | 470d0567 | Thomas Schöpping | FLASH->ACR |= (blt_int32u)FLASH_ACR_LATENCY_2; |
| 327 | #elif (BOOT_CPU_SYSTEM_SPEED_KHZ > 24000) |
||
| 328 | 69661903 | Thomas Schöpping | /* configure 1 flash wait states */
|
| 329 | 470d0567 | Thomas Schöpping | FLASH->ACR |= (blt_int32u)FLASH_ACR_LATENCY_1; |
| 330 | 69661903 | Thomas Schöpping | #endif
|
| 331 | /* HCLK = SYSCLK */
|
||
| 332 | RCC->CFGR |= (blt_int32u)RCC_CFGR_HPRE_DIV1; |
||
| 333 | /* PCLK2 = HCLK/2 */
|
||
| 334 | RCC->CFGR |= (blt_int32u)RCC_CFGR_PPRE2_DIV2; |
||
| 335 | /* PCLK1 = HCLK/2 */
|
||
| 336 | RCC->CFGR |= (blt_int32u)RCC_CFGR_PPRE1_DIV2; |
||
| 337 | /* reset PLL configuration */
|
||
| 338 | RCC->CFGR &= (blt_int32u)((blt_int32u)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | \ |
||
| 339 | RCC_CFGR_PLLMULL)); |
||
| 340 | /* assert that the pll_multiplier is between 2 and 16 */
|
||
| 341 | ASSERT_CT((BOOT_CPU_SYSTEM_SPEED_KHZ/BOOT_CPU_XTAL_SPEED_KHZ) >= 2);
|
||
| 342 | ASSERT_CT((BOOT_CPU_SYSTEM_SPEED_KHZ/BOOT_CPU_XTAL_SPEED_KHZ) <= 16);
|
||
| 343 | /* calculate multiplier value */
|
||
| 344 | pll_multiplier = BOOT_CPU_SYSTEM_SPEED_KHZ/BOOT_CPU_XTAL_SPEED_KHZ; |
||
| 345 | /* convert to register value */
|
||
| 346 | pll_multiplier = (blt_int32u)((pll_multiplier - 2) << 18); |
||
| 347 | /* set the PLL multiplier and clock source */
|
||
| 348 | RCC->CFGR |= (blt_int32u)(RCC_CFGR_PLLSRC_HSE | pll_multiplier); |
||
| 349 | /* enable PLL */
|
||
| 350 | RCC->CR |= RCC_CR_PLLON; |
||
| 351 | /* wait till PLL is ready */
|
||
| 352 | while((RCC->CR & RCC_CR_PLLRDY) == 0) |
||
| 353 | {
|
||
| 354 | } |
||
| 355 | /* select PLL as system clock source */
|
||
| 356 | RCC->CFGR &= (blt_int32u)((blt_int32u)~(RCC_CFGR_SW)); |
||
| 357 | 470d0567 | Thomas Schöpping | RCC->CFGR |= (blt_int32u)RCC_CFGR_SW_PLL; |
| 358 | 69661903 | Thomas Schöpping | /* wait till PLL is used as system clock source */
|
| 359 | while ((RCC->CFGR & (blt_int32u)RCC_CFGR_SWS) != (blt_int32u)0x08) |
||
| 360 | {
|
||
| 361 | } |
||
| 362 | |||
| 363 | /* remap JTAG pins */
|
||
| 364 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); |
||
| 365 | AFIO->MAPR &= ~(blt_int32u)((blt_int32u)0x7 << 24); |
||
| 366 | AFIO->MAPR |= (blt_int32u)((blt_int32u)0x2 << 24); |
||
| 367 | /* all input */
|
||
| 368 | |||
| 369 | #if (BOOT_COM_CAN_ENABLE > 0 || BOOT_GATE_CAN_ENABLE > 0) |
||
| 370 | /* enable clocks for CAN transmitter and receiver pins (GPIOB and AFIO) */
|
||
| 371 | RCC->APB2ENR |= (blt_int32u)(0x00000004 | 0x00000001); |
||
| 372 | /* configure CAN Rx (GPIOA11) as alternate function input */
|
||
| 373 | /* first reset the configuration */
|
||
| 374 | GPIOA->CRH &= ~(blt_int32u)((blt_int32u)0xf << 12); |
||
| 375 | /* CNF8[1:0] = %01 and MODE8[1:0] = %00 */
|
||
| 376 | GPIOA->CRH |= (blt_int32u)((blt_int32u)0x4 << 12); |
||
| 377 | /* configure CAN Tx (GPIOA12) as alternate function push-pull */
|
||
| 378 | /* first reset the configuration */
|
||
| 379 | GPIOA->CRH &= ~(blt_int32u)((blt_int32u)0xf << 16); |
||
| 380 | /* CNF9[1:0] = %11 and MODE9[1:0] = %11 */
|
||
| 381 | GPIOA->CRH |= (blt_int32u)((blt_int32u)0xb << 16); |
||
| 382 | |||
| 383 | /* remap CAN1 pins to PortA */
|
||
| 384 | AFIO->MAPR &= ~(blt_int32u)((blt_int32u)0x3 << 13); |
||
| 385 | AFIO->MAPR |= (blt_int32u)((blt_int32u)0x0 << 13); |
||
| 386 | #endif
|
||
| 387 | |||
| 388 | #if (BOOT_COM_UART_ENABLE > 0 || BOOT_GATE_UART_ENABLE > 0) |
||
| 389 | /* enable clocks for USART1 peripheral, transmitter and receiver pins (GPIOA and AFIO) */
|
||
| 390 | RCC->APB2ENR |= (blt_int32u)(0x00004000 | 0x00000004 | 0x00000001); |
||
| 391 | /* configure USART1 Tx (GPIOA9) as alternate function push-pull */
|
||
| 392 | /* first reset the configuration */
|
||
| 393 | GPIOA->CRH &= ~(blt_int32u)((blt_int32u)0xf << 4); |
||
| 394 | /* CNF2[1:0] = %10 and MODE2[1:0] = %11 */
|
||
| 395 | GPIOA->CRH |= (blt_int32u)((blt_int32u)0xb << 4); |
||
| 396 | /* configure USART1 Rx (GPIOA10) as alternate function input floating */
|
||
| 397 | /* first reset the configuration */
|
||
| 398 | GPIOA->CRH &= ~(blt_int32u)((blt_int32u)0xf << 8); |
||
| 399 | /* CNF2[1:0] = %01 and MODE2[1:0] = %00 */
|
||
| 400 | GPIOA->CRH |= (blt_int32u)((blt_int32u)0x4 << 8); |
||
| 401 | #endif
|
||
| 402 | |||
| 403 | } /*** end of Init ***/
|
||
| 404 | |||
| 405 | /*
|
||
| 406 | * Initializes all GPIO used by the bootloader
|
||
| 407 | */
|
||
| 408 | static void initGpio() { |
||
| 409 | GPIO_InitTypeDef gpio_init; |
||
| 410 | |||
| 411 | /*
|
||
| 412 | * OUTPUTS
|
||
| 413 | */
|
||
| 414 | |||
| 415 | /* initialize LED and push it up (inactive) */
|
||
| 416 | GPIO_SetBits(LED_GPIO, LED_PIN); |
||
| 417 | gpio_init.GPIO_Pin = LED_PIN; |
||
| 418 | gpio_init.GPIO_Mode = GPIO_Mode_Out_PP; |
||
| 419 | gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
||
| 420 | GPIO_Init(LED_GPIO, &gpio_init); |
||
| 421 | |||
| 422 | /* initialize SYS_PD_N and let it go (inactive) */
|
||
| 423 | GPIO_SetBits(SYS_PD_N_GPIO, SYS_PD_N_PIN); |
||
| 424 | gpio_init.GPIO_Pin = SYS_PD_N_PIN; |
||
| 425 | gpio_init.GPIO_Mode = GPIO_Mode_Out_OD; |
||
| 426 | gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
||
| 427 | GPIO_Init(SYS_PD_N_GPIO, &gpio_init); |
||
| 428 | |||
| 429 | /* initialize SYS_SYNC_N and pull it down (active) */
|
||
| 430 | GPIO_ResetBits(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN); |
||
| 431 | gpio_init.GPIO_Pin = SYS_SYNC_N_PIN; |
||
| 432 | gpio_init.GPIO_Mode = GPIO_Mode_Out_OD; |
||
| 433 | gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
||
| 434 | GPIO_Init(SYS_SYNC_N_GPIO, &gpio_init); |
||
| 435 | |||
| 436 | /* initialize SYS_WARMST_N and let it go (active) */
|
||
| 437 | GPIO_SetBits(SYS_WARMRST_N_GPIO, SYS_WARMRST_N_PIN); |
||
| 438 | gpio_init.GPIO_Pin = SYS_WARMRST_N_PIN; |
||
| 439 | gpio_init.GPIO_Mode = GPIO_Mode_Out_OD; |
||
| 440 | gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
||
| 441 | GPIO_Init(SYS_WARMRST_N_GPIO, &gpio_init); |
||
| 442 | |||
| 443 | /* initialize SYS_UART_UP and let it go (inactive) */
|
||
| 444 | GPIO_SetBits(SYS_UART_UP_GPIO, SYS_UART_UP_PIN); |
||
| 445 | gpio_init.GPIO_Pin = SYS_UART_UP_PIN; |
||
| 446 | gpio_init.GPIO_Mode = GPIO_Mode_Out_OD; |
||
| 447 | gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
||
| 448 | GPIO_Init(SYS_UART_UP_GPIO, &gpio_init); |
||
| 449 | |||
| 450 | /* initialize PATH_DCEN and pull it down (inactive) */
|
||
| 451 | GPIO_ResetBits(PATH_DCEN_GPIO, PATH_DCEN_PIN); |
||
| 452 | gpio_init.GPIO_Pin = PATH_DCEN_PIN; |
||
| 453 | gpio_init.GPIO_Mode = GPIO_Mode_Out_PP; |
||
| 454 | gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
||
| 455 | GPIO_Init(PATH_DCEN_GPIO, &gpio_init); |
||
| 456 | |||
| 457 | /*
|
||
| 458 | * INPUTS
|
||
| 459 | */
|
||
| 460 | |||
| 461 | /* initialize the input ACCEL_INT_N */
|
||
| 462 | gpio_init.GPIO_Pin = ACCEL_INT_N_PIN; |
||
| 463 | gpio_init.GPIO_Mode = GPIO_Mode_IN_FLOATING; |
||
| 464 | gpio_init.GPIO_Speed = GPIO_Speed_50MHz; |
||
| 465 | GPIO_Init(ACCEL_INT_N_GPIO, &gpio_init); |
||
| 466 | |||
| 467 | return;
|
||
| 468 | } /*** end of initGpio ***/
|
||
| 469 | |||
| 470 | /*
|
||
| 471 | * Initialize all EXTI lines
|
||
| 472 | */
|
||
| 473 | static void initExti() { |
||
| 474 | /* configure EXTI lines */
|
||
| 475 | GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource1); // SYS_SYNC_N
|
||
| 476 | GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource2); // SYS_WARMRST_N
|
||
| 477 | GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource3); // PATH_DCSTAT
|
||
| 478 | GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource5); // COMPASS_DRDY
|
||
| 479 | GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource8); // SYS_PD_N
|
||
| 480 | GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource9); // SYS_REG_EN
|
||
| 481 | GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource12); // IR_INT
|
||
| 482 | GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource13); // GYRO_DRDY
|
||
| 483 | GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource14); // SYS_UART_UP
|
||
| 484 | GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource15); // ACCEL_INT_N
|
||
| 485 | |||
| 486 | return;
|
||
| 487 | } |
||
| 488 | |||
| 489 | /*
|
||
| 490 | * Signals, which type of low-power mode the system shall enter after the shutdown sequence.
|
||
| 491 | */
|
||
| 492 | ErrorStatus shutdownDisambiguationProcedure(const uint8_t type) {
|
||
| 493 | GPIO_SetBits(SYS_SYNC_N_GPIO, SYS_SYNC_N_PIN); |
||
| 494 |