Skip to main content
xrstokes
Associate III
March 17, 2022
Solved

Trouble with custom bootloader and CubeMX generated code

  • March 17, 2022
  • 2 replies
  • 1288 views

Hi, I've got an project in cubeide that starts in one project and then jump's to another at 0x0800C000. It's been working in the past and I've somehow broken it trying to do updates. Here is the main code in the bootloader. It's very simple. BOOTLOADER v

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "fatfs.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
 
#include <stdbool.h>
 
/* USER CODE END Includes */
 
/* Private variables ---------------------------------------------------------*/
CRC_HandleTypeDef hcrc;
 
I2C_HandleTypeDef hi2c1;
 
SPI_HandleTypeDef hspi2;
 
/* USER CODE BEGIN PV */
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI2_Init(void);
static void MX_CRC_Init(void);
static void MX_I2C1_Init(void);
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
 
 
typedef void (*pFunction)(void);
void goto_APP(void);
pFunction JumpToApplication;
uint32_t JumpAddress;
 
#define APP_ADD 0x0800C000
 
extern bool MY_FLASH();
 
/* USER CODE END 0 */
 
/**
 * @brief The application entry point.
 * @retval int
 */
int bl_main(void)
{
 /* USER CODE BEGIN 1 */
 
 /* USER CODE END 1 */
 
 /* MCU Configuration--------------------------------------------------------*/
 
 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
 HAL_Init();
 
 /* USER CODE BEGIN Init */
 
// HAL_Delay(2000);
 
 /* USER CODE END Init */
 
 /* Configure the system clock */
 SystemClock_Config();
 
 /* USER CODE BEGIN SysInit */
 
 /* USER CODE END SysInit */
 
 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_SPI2_Init();
 MX_FATFS_Init();
 MX_CRC_Init();
 MX_I2C1_Init();
 /* USER CODE BEGIN 2 */
 
 for(int x = 0; x < 6; x++)
 {
	 HAL_Delay(100);
	 HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_8);
 }
 
 if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2) == GPIO_PIN_RESET)
 {
	 goto_APP();
 }
 
 int delay = 1000;
 
	// Gets the PWM driver running, turns on the green LED and powers the LCD port.
	HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, false); //OE
	pwm_click_init(0x40);
	HAL_Delay(10);
	pwm_all_state(false);
 
 if(MY_FLASH() == true)
 {
	 delay = 100;
 }
 
 /* USER CODE END 2 */
 
 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
 /* USER CODE END WHILE */
 
 /* USER CODE BEGIN 3 */
 
	 HAL_Delay(delay);
	 HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_8);
 
 }
 /* USER CODE END 3 */
}
 
/**
 * @brief System Clock Configuration
 * @retval None
 */
void SystemClock_Config(void)
{
 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
 /** 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_HSI;
 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
 RCC_OscInitStruct.PLL.PLLM = 8;
 RCC_OscInitStruct.PLL.PLLN = 168;
 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
 RCC_OscInitStruct.PLL.PLLQ = 7;
 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
 {
 Error_Handler();
 }
 /** 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_5) != HAL_OK)
 {
 Error_Handler();
 }
}
 
/**
 * @brief CRC Initialization Function
 * @param None
 * @retval None
 */
static void MX_CRC_Init(void)
{
 
 /* USER CODE BEGIN CRC_Init 0 */
 
 /* USER CODE END CRC_Init 0 */
 
 /* USER CODE BEGIN CRC_Init 1 */
 
 /* USER CODE END CRC_Init 1 */
 hcrc.Instance = CRC;
 if (HAL_CRC_Init(&hcrc) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN CRC_Init 2 */
 
 /* USER CODE END CRC_Init 2 */
 
}
 
static void MX_I2C1_Init(void)
{
######
}
 
 
static void MX_SPI2_Init(void)
{
###
}
 
 
static void MX_GPIO_Init(void)
{
#####
}
 
/* USER CODE BEGIN 4 */
 
void goto_APP(void)
{
 /* Test if user code is programmed starting from APP_ADD address */
 //if(((*(__IO uint32_t*)APP_ADD) & 0x2FFE0000 ) == 0x20000000)
 
 {
		/* Jump to user application */
		JumpAddress = *(__IO uint32_t*) (APP_ADD + 4);
		JumpToApplication = (pFunction) JumpAddress;
 
		/* Initialize user application's Stack Pointer */
		__set_MSP((*(__IO uint32_t*) APP_ADD ));
 
 
		HAL_I2C_DeInit(&hi2c1);
		HAL_SPI_DeInit(&hspi2);
		HAL_CRC_DeInit(&hcrc);
 
		__HAL_RCC_GPIOC_CLK_DISABLE();
		__HAL_RCC_GPIOH_CLK_DISABLE();
		__HAL_RCC_GPIOB_CLK_DISABLE();
		__HAL_RCC_GPIOE_CLK_DISABLE();
		__HAL_RCC_GPIOD_CLK_DISABLE();
		__HAL_RCC_GPIOA_CLK_DISABLE();
 
		SysTick->CTRL = 0;
		SysTick->LOAD = 0;
		SysTick->VAL = 0;
 
		HAL_RCC_DeInit();
		HAL_DeInit();
 
		SCB->VTOR = APP_ADD;
		#if(SET_VECTOR_TABLE)
			SCB->VTOR = APP_ADDRESS;
		#endif
 
 
		JumpToApplication();
 }
}
#######

APP v

int app_main(void)
{
 /* USER CODE BEGIN 1 */
 
 /* USER CODE END 1 */
 
 /* MCU Configuration--------------------------------------------------------*/
 
 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
 HAL_Init();
 
 /* USER CODE BEGIN Init */
 
 HAL_Delay(2000);
 
 /* USER CODE END Init */
 
 /* Configure the system clock */
 SystemClock_Config(); < FAILS HERE
 
 /* USER CODE BEGIN SysInit */

It's very strange. On the first power up it fails always in the app at "SystemClock_Config();" But if i reset the board. or power down and repower up in less than 10 seconds it gets past it. I've tried a bunch of different clock configs and a thousand hardware changes. Can't work it out.

Please Help.

This topic has been closed for replies.
Best answer by xrstokes

OK, I deleted the projects and started again. Generated new code and it worked fine.

2 replies

xrstokes
xrstokesAuthorBest answer
Associate III
March 21, 2022

OK, I deleted the projects and started again. Generated new code and it worked fine.

ST Technical Moderator
March 22, 2022

Hello @xrstokes​ ,

Really glad to know you overcame this problem :smiling_face_with_smiling_eyes: I marked your answer as solved.

Imen

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Thanks