openblt/Target/Demo/ARMCM7_STM32F7_Nucleo_F767Z.../Boot/main.c

269 lines
11 KiB
C

/************************************************************************************//**
* \file Demo/ARMCM7_STM32F7_Nucleo_F767ZI_TrueStudio/Boot/main.c
* \brief Bootloader application source file.
* \ingroup Boot_ARMCM7_STM32F7_Nucleo_F767ZI_TrueStudio
* \internal
*----------------------------------------------------------------------------------------
* C O P Y R I G H T
*----------------------------------------------------------------------------------------
* Copyright (c) 2019 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 "stm32f7xx.h" /* STM32 CPU and HAL header */
#include "shared_params.h" /* Shared parameters 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)
{
blt_int8u deferredInitRequestFlag = 0;
/* initialize the microcontroller */
Init();
/* initialize the shared parameters module */
SharedParamsInit();
/* initialize the bootloader */
BootInit();
#if (BOOT_COM_DEFERRED_INIT_ENABLE == 1)
/* the bootloader is configured to NOT initialize the TCP/IP network stack by default
* to bypass unnecessary delay times before starting the user program. the TCP/IP net-
* work tack is now only initialized when: (a) no valid user program is detected, or
* (b) a forced backdoor entry occurred (CpuUserProgramStartHook() returned BLT_FALSE).
*
* these demo bootloader and user programs have one extra feature implemented for
* demonstration purposes. the demo user program can detect firmware update requests
* from the TCP/IP network in which case it activates the bootloader. But...the
* TCP/IP network stack will not be initialized in this situation. for this reason
* the shared parameter module was integrated in both the bootloader and user program.
* more information about the shared parameter module can be found here:
* https://www.feaser.com/en/blog/?p=216
*
* the shared parameter at the first index (0) contains a flag. this flag is set to
* 1, right before the user program activates this bootloader, to explicitly request
* the bootloader to initialize the TCP/IP network stack. this makes it possible for
* a firmware update to proceed. the code here reads out this flag and performs the
* TCP/IP network stack initialization when requested.
*/
SharedParamsReadByIndex(0, &deferredInitRequestFlag);
if (deferredInitRequestFlag == 1)
{
/* explicitly initialize all communication interface for which the deferred
* initialization feature was enabled.
*/
ComDeferredInit();
}
#endif
/* 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)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
/* Configure the main internal regulator output voltage. */
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/* Initializes the CPU, AHB and APB busses clocks. */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 432;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
/* Clock configuration incorrect or hardware failure. Hang the system to prevent
* damage.
*/
ASSERT_RT(BLT_FALSE);
}
/* Activate the Over-Drive mode. */
if (HAL_PWREx_EnableOverDrive() != HAL_OK)
{
/* Clock overdrive hardware failure. Hang the system to prevent damage.
*/
ASSERT_RT(BLT_FALSE);
}
/* Initializes the CPU, AHB and APB busses clocks. */
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)
{
/* Clock configuration incorrect or hardware failure. Hang the system to prevent
* damage.
*/
ASSERT_RT(BLT_FALSE);
}
} /*** 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)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* Power and SYSCFG clock enable. */
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_RCC_SYSCFG_CLK_ENABLE();
/* GPIO ports clock enable. */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOG_CLK_ENABLE();
#if (BOOT_COM_RS232_ENABLE > 0)
/* UART clock enable. */
__HAL_RCC_USART3_CLK_ENABLE();
#endif
/* Configure GPIO pin for the LED. */
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_RESET);
/* Configure GPIO pin for (optional) backdoor entry input. */
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
#if (BOOT_COM_RS232_ENABLE > 0)
/* UART TX and RX GPIO pin configuration. */
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
HAL_GPIO_Init(GPIOD, &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)
{
/* Reset the RCC clock configuration to the default reset state. */
HAL_RCC_DeInit();
/* Reset GPIO pin for the LED to turn it off. */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_RESET);
/* Deinit used GPIOs. */
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7);
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_13);
#if (BOOT_COM_RS232_ENABLE > 0)
/* Deinit used GPIOs. */
HAL_GPIO_DeInit(GPIOD, GPIO_PIN_8);
HAL_GPIO_DeInit(GPIOD, GPIO_PIN_9);
/* UART clock disable. */
__HAL_RCC_USART3_CLK_DISABLE();
#endif
/* GPIO ports clock disable. */
__HAL_RCC_GPIOG_CLK_DISABLE();
__HAL_RCC_GPIOD_CLK_DISABLE();
__HAL_RCC_GPIOC_CLK_DISABLE();
__HAL_RCC_GPIOB_CLK_DISABLE();
__HAL_RCC_GPIOA_CLK_DISABLE();
/* SYSCFG and PWR clock disable. */
__HAL_RCC_PWR_CLK_DISABLE();
__HAL_RCC_SYSCFG_CLK_DISABLE();
} /*** end of HAL_MspDeInit ***/
/*********************************** end of main.c *************************************/