mirror of https://github.com/rusefi/openblt.git
323 lines
12 KiB
C
323 lines
12 KiB
C
/************************************************************************************//**
|
|
* \file Demo/ARMCM4_STM32F3_Olimexino_STM32F3_GCC/Boot/main.c
|
|
* \brief Bootloader application source file.
|
|
* \ingroup Boot_ARMCM4_STM32F3_Olimexino_STM32F3_GCC
|
|
* \internal
|
|
*----------------------------------------------------------------------------------------
|
|
* C O P Y R I G H T
|
|
*----------------------------------------------------------------------------------------
|
|
* Copyright (c) 2023 by Feaser http://www.feaser.com All rights reserved
|
|
*
|
|
*----------------------------------------------------------------------------------------
|
|
* L I C E N S E
|
|
*----------------------------------------------------------------------------------------
|
|
* This file is part of OpenBLT. OpenBLT is free software: you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License as published by the Free
|
|
* Software Foundation, either version 3 of the License, or (at your option) any later
|
|
* version.
|
|
*
|
|
* OpenBLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
*
|
|
* You have received a copy of the GNU General Public License along with OpenBLT. It
|
|
* should be located in ".\Doc\license.html". If not, contact Feaser to obtain a copy.
|
|
*
|
|
* \endinternal
|
|
****************************************************************************************/
|
|
|
|
/****************************************************************************************
|
|
* Include files
|
|
****************************************************************************************/
|
|
#include "boot.h" /* bootloader generic header */
|
|
#include "stm32f3xx.h" /* STM32 registers and drivers */
|
|
#include "stm32f3xx_ll_rcc.h" /* STM32 LL RCC header */
|
|
#include "stm32f3xx_ll_bus.h" /* STM32 LL BUS header */
|
|
#include "stm32f3xx_ll_system.h" /* STM32 LL SYSTEM header */
|
|
#include "stm32f3xx_ll_utils.h" /* STM32 LL UTILS header */
|
|
#include "stm32f3xx_ll_gpio.h" /* STM32 LL GPIO header */
|
|
|
|
|
|
/****************************************************************************************
|
|
* Function prototypes
|
|
****************************************************************************************/
|
|
static void Init(void);
|
|
static void SystemClock_Config(void);
|
|
|
|
|
|
/************************************************************************************//**
|
|
** \brief This is the entry point for the bootloader application and is called
|
|
** by the reset interrupt vector after the C-startup routines executed.
|
|
** \return Program return code.
|
|
**
|
|
****************************************************************************************/
|
|
int main(void)
|
|
{
|
|
/* Initialize the microcontroller. */
|
|
Init();
|
|
/* Initialize the bootloader. */
|
|
BootInit();
|
|
|
|
/* Start the infinite program loop. */
|
|
while (1)
|
|
{
|
|
/* Run the bootloader task. */
|
|
BootTask();
|
|
}
|
|
|
|
/* Program should never get here. */
|
|
return 0;
|
|
} /*** end of main ***/
|
|
|
|
|
|
/************************************************************************************//**
|
|
** \brief Initializes the microcontroller.
|
|
** \return none.
|
|
**
|
|
****************************************************************************************/
|
|
static void Init(void)
|
|
{
|
|
/* HAL library initialization. */
|
|
HAL_Init();
|
|
/* Configure system clock. */
|
|
SystemClock_Config();
|
|
} /*** end of Init ***/
|
|
|
|
/************************************************************************************//**
|
|
** \brief System Clock Configuration. This code was created by CubeMX and configures
|
|
** the system clock to match the configuration in the bootloader's
|
|
** configuration (blt_conf.h), specifically the macros:
|
|
** BOOT_CPU_SYSTEM_SPEED_KHZ and BOOT_CPU_XTAL_SPEED_KHZ.
|
|
** Note that the Lower Layer drivers were selected in CubeMX for the RCC
|
|
** subsystem.
|
|
** \return none.
|
|
**
|
|
****************************************************************************************/
|
|
static void SystemClock_Config(void)
|
|
{
|
|
/* Set flash latency. */
|
|
LL_FLASH_SetLatency(LL_FLASH_LATENCY_2);
|
|
while(LL_FLASH_GetLatency()!= LL_FLASH_LATENCY_2)
|
|
{
|
|
;
|
|
}
|
|
|
|
/* Enable the HSE clock. */
|
|
LL_RCC_HSE_Enable();
|
|
/* Wait till HSE is ready. */
|
|
while(LL_RCC_HSE_IsReady() != 1)
|
|
{
|
|
;
|
|
}
|
|
|
|
/* Configure and enable the PLL. */
|
|
LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE_DIV_1, LL_RCC_PLL_MUL_9);
|
|
LL_RCC_PLL_Enable();
|
|
/* Wait till PLL is ready. */
|
|
while(LL_RCC_PLL_IsReady() != 1)
|
|
{
|
|
;
|
|
}
|
|
|
|
/* Configure bus prescalers. */
|
|
LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
|
|
LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_2);
|
|
LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
|
|
|
|
/* Select the system clock. */
|
|
LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
|
|
/* Wait till system clock is ready. */
|
|
while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
|
|
{
|
|
;
|
|
}
|
|
/* Update the system clock speed setting. */
|
|
LL_SetSystemCoreClock(BOOT_CPU_SYSTEM_SPEED_KHZ * 1000u);
|
|
|
|
/* Select peripheral clock sources. */
|
|
#if (BOOT_COM_RS232_ENABLE > 0)
|
|
LL_RCC_SetUSARTClockSource(LL_RCC_USART3_CLKSOURCE_PCLK1);
|
|
#endif
|
|
#if (BOOT_COM_USB_ENABLE > 0)
|
|
LL_RCC_SetUSBClockSource(LL_RCC_USB_CLKSOURCE_PLL_DIV_1_5);
|
|
#endif
|
|
} /*** end of SystemClock_Config ***/
|
|
|
|
|
|
/************************************************************************************//**
|
|
** \brief Initializes the Global MSP. This function is called from HAL_Init()
|
|
** function to perform system level initialization (GPIOs, clock, DMA,
|
|
** interrupt).
|
|
** \return none.
|
|
**
|
|
****************************************************************************************/
|
|
void HAL_MspInit(void)
|
|
{
|
|
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
/* SYSCFG and PWR clock enable. */
|
|
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
|
|
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
|
|
|
|
/* GPIO ports clock enable. */
|
|
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
|
|
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
|
|
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC);
|
|
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOD);
|
|
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOF);
|
|
|
|
#if (BOOT_COM_RS232_ENABLE > 0)
|
|
/* UART clock enable. */
|
|
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART3);
|
|
#endif
|
|
|
|
#if (BOOT_COM_CAN_ENABLE > 0)
|
|
/* CAN clock enable. */
|
|
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_CAN);
|
|
#endif
|
|
|
|
#if (BOOT_COM_USB_ENABLE > 0)
|
|
/* USB clock enable. */
|
|
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USB);
|
|
#endif
|
|
|
|
/* Configure GPIO pin for (optional) backdoor entry input. */
|
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
|
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
|
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
|
|
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
/* Configure GPIO pin for the LED. */
|
|
LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_5);
|
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_5;
|
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
|
|
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
|
|
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
|
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
/* Configure GPIO pin for the USB connect. */
|
|
LL_GPIO_SetOutputPin(GPIOC, LL_GPIO_PIN_12);
|
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_12;
|
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
|
|
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
|
|
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
|
LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
|
|
|
#if (BOOT_COM_RS232_ENABLE > 0)
|
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_10 | LL_GPIO_PIN_11;
|
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
|
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
|
|
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
|
GPIO_InitStruct.Alternate = LL_GPIO_AF_7;
|
|
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
#endif
|
|
|
|
#if (BOOT_COM_CAN_ENABLE > 0)
|
|
/* CAN TX and RX GPIO pin configuration. */
|
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_8 | LL_GPIO_PIN_9;
|
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
|
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
|
|
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
|
GPIO_InitStruct.Alternate = LL_GPIO_AF_9;
|
|
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
#endif
|
|
|
|
#if (BOOT_COM_USB_ENABLE > 0)
|
|
/* USB D+ and USB D- pin configuration. */
|
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_11 | LL_GPIO_PIN_12;
|
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
|
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
|
|
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
|
GPIO_InitStruct.Alternate = LL_GPIO_AF_14;
|
|
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
#endif
|
|
} /*** end of HAL_MspInit ***/
|
|
|
|
|
|
/************************************************************************************//**
|
|
** \brief DeInitializes the Global MSP. This function is called from HAL_DeInit()
|
|
** function to perform system level de-initialization (GPIOs, clock, DMA,
|
|
** interrupt).
|
|
** \return none.
|
|
**
|
|
****************************************************************************************/
|
|
void HAL_MspDeInit(void)
|
|
{
|
|
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
/* Make sure USB DISC (PC12) stays high to disable the pull-up on the USB D+ line,
|
|
* needed to place the USB device in the disconnected state. Otherwise a possible
|
|
* connected USB host will attempt to enumerate, which will fail because the USB
|
|
* device is not active at this time.
|
|
*/
|
|
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC);
|
|
LL_GPIO_SetOutputPin(GPIOC, LL_GPIO_PIN_12);
|
|
GPIO_InitStruct.Pin = LL_GPIO_PIN_12;
|
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
|
|
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
|
|
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
|
LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
|
|
|
/* Reset the RCC clock configuration to the default reset state. */
|
|
LL_RCC_DeInit();
|
|
|
|
/* Deinit used GPIOs, except GPIOC to make sure USB DISC (PC12) stays high. */
|
|
LL_GPIO_DeInit(GPIOD);
|
|
LL_GPIO_DeInit(GPIOB);
|
|
LL_GPIO_DeInit(GPIOA);
|
|
|
|
#if (BOOT_COM_RS232_ENABLE > 0)
|
|
/* UART clock disable. */
|
|
LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_USART3);
|
|
#endif
|
|
|
|
#if (BOOT_COM_CAN_ENABLE > 0)
|
|
/* CAN clock disable. */
|
|
LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_CAN);
|
|
#endif
|
|
|
|
#if (BOOT_COM_USB_ENABLE > 0)
|
|
/* USB clock disable. */
|
|
LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_USB);
|
|
#endif
|
|
|
|
#if (BOOT_FILE_SYS_ENABLE > 0)
|
|
/* SPI clock disable. */
|
|
LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_SPI2);
|
|
#endif
|
|
|
|
/* GPIO ports clock disable, except GPIOC to make sure USB DISC (PC12) stays high. */
|
|
LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOD);
|
|
LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
|
|
LL_AHB1_GRP1_DisableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
|
|
|
|
/* PWR and SYSCFG clock disable. */
|
|
LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_PWR);
|
|
LL_APB2_GRP1_DisableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
|
|
} /*** end of HAL_MspDeInit ***/
|
|
|
|
|
|
/************************************************************************************//**
|
|
** \brief This function handles the SysTick interrupt. The HAL driver is initialized
|
|
** before the bootloader disables the global interrupts and reconfigures the
|
|
** SysTick. It is theoretically possible that the SysTick interrupt still
|
|
** fires before the timer driver disables it. Therefore the handler is
|
|
** implemented here. If not, then the default handler from the C startup
|
|
** code is used, which hangs the system.
|
|
** \return none.
|
|
**
|
|
****************************************************************************************/
|
|
void SysTick_Handler(void)
|
|
{
|
|
/* Nothing to do here. */
|
|
} /*** end of SysTick_Handler ***/
|
|
|
|
|
|
/*********************************** end of main.c *************************************/
|