cancel
Showing results for 
Search instead for 
Did you mean: 

pow function cause hardfault in bootloader

shayanghn
Associate

hi every one so i got a problem with bootloader in stm32 when i switch from bootloader to application everything is fine but at the middle of the user application at one function i face with hardfault. this is my initialization at main function:

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "BootLoader.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */


/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
    Process_Recieved_Information(&analyze_Recieved_Packet);
    Send_Information_InBootLoader_Lib(&Send_Data);

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* 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_USART2_UART_Init();
  MX_TIM4_Init();
  /* USER CODE BEGIN 2 */
    HAL_TIM_Base_Start_IT(&htim4);
    HAL_TIM_Base_Start(&htim4);
    USART2->CR1 |= USART_CR1_RXNEIE ;
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
    NVIC_EnableIRQ(USART2_IRQn);
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */

      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET);
      Send_RxData_To_BootLoader_Lib();

  }
  /* USER CODE END 3 */
}

this is my function to jump to user application : 

 

static void Jump_To_User_Application(void)
{
    /* Set the clock to the default state */
        __disable_irq();

        NVIC_DisableIRQ(USART2_IRQn);
        HAL_NVIC_DisableIRQ(TIM4_IRQn);

        HAL_GPIO_DeInit(GPIOB, GPIO_PIN_3);
        __HAL_RCC_GPIOA_CLK_DISABLE();
        __HAL_RCC_GPIOB_CLK_DISABLE();
        __HAL_RCC_GPIOC_CLK_DISABLE();
        __HAL_RCC_GPIOF_CLK_DISABLE();

        HAL_DeInit();
        HAL_RCC_DeInit();
        HAL_TIM_Base_DeInit(&htim4);
        HAL_UART_DeInit(&huart2);
        
    /* Disable Systick timer */
    SysTick->CTRL = 0;
    SysTick->LOAD = 0;
    SysTick->VAL = 0;
    /* Clear Interrupt Enable Register & Interrupt Pending Register */
    for (uint16_t i = 0; i < sizeof(NVIC->ICER) / sizeof(NVIC->ICER[0]); i++)
        {
          NVIC->ICER[i] = 0xFFFFFFFF;
          NVIC->ICPR[i] = 0xFFFFFFFF;
        }
    /* Re-enable all interrupts */
    __enable_irq();
 void(* user_app_1)(void);
uint32_t temp_msp = *(__IO uint32_t *)PAGE_16_ADDRESS;
__set_MSP(temp_msp);
uint32_t user_app_1_rst_handler = *(__IO uint32_t *)(PAGE_16_ADDRESS+4);
user_app_1 = (void *)user_app_1_rst_handler;
user_app_1(); }
 

 this is the function that cause hardfault : 

void SomeFunction() 
{
    for (l = 1; l <= m; l++)
    {
        le = pow(2, l);
        le2 = le / 2;
        ur = 1;
        ui = 0;
        // Calculate sine and cosine values
        sr = cos(PI/le2);
        si = -sin(PI/le2);
        // Loop for each sub FFT
        for (j = 1; j <= le2; j++)
        {
            jm1 = j - 1;
            // Loop for each butterfly
            for (i = jm1; i <= nm1; i += le)
            {
                ip = i + le2;
                tr = REX[ip]*ur - IMX[ip]*ui;
                ti = REX[ip]*ui + IMX[ip]*ur;
                REX[ip] = REX[i] - tr;
                IMX[ip] = IMX[i] - ti;
                REX[i] = REX[i] + tr;
                IMX[i] = IMX[i] + ti;
            }
            tr = ur;
            ur = tr*sr - ui*si;
            ui = tr*si + ui*sr;
        }
}

after 3 times looping i get hardfault at this line which i bold it  :

le = pow(2, l); 

i created my own pow() function and every thing works fine. but pow() function in math.h cause hardfault error. other math.h functions like sin() and cos() wont make any problem 

can any one help me ? 

 

2 REPLIES 2

Stack size?

FPU Enabled?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

>>can any one help me ? 

Help yourself, output actionable data in the Hard Fault Handler so you can look at the code, instructions and registers faulting.

https://github.com/cturvey/RandomNinjaChef/blob/main/KeilHardFault.c

Does it pay well?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..