HardFault on "HAL_FLASHEx_Erase" & "HAL_FLASH_Program" while USART is on
Hello,
I have made several tries but non of them could possible solve my problem.
In an STM32F030K Series, so Single Bank Flash memory, I am trying to have Flash Operation and UART module works together under the same build.
I know that Interrupts during Flash Erase-Write operation are creating troubles to the stability and creating HardFault Error for sure.
In order to avoid this behavior I have tried all the following methods in order to prevent HardFault Error BUT non of them worked.
Method 1:
HAL_UART_DeInit(&huart1);
__disable_irq(); /* Disable Global Interrupts */
HAL_FLASH_Unlock(); /* Unlock the Flash to enable the flash control register access */
Flash_ErasePage(FLASH_USER_START_ADDR); /* Erase 1 complete memory page */
Flash_ProgramPage(FLASH_USER_START_ADDR,EE_CacheTable); /* Program 1 complete memory page */
HAL_FLASH_Lock(); /* Lock the Flash to disable the flash control register access */
__enable_irq(); /* Disable Global Interrupts */
MX_USART1_UART_Init();
Method 2: USART Interrupt runs in RAM
#define RAMFUNC __attribute__ ((section (".ramfunctions")))
RAMFUNC void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */
/* USER CODE END USART1_IRQn 0 */
HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART1_IRQn 1 */
/* USER CODE END USART1_IRQn 1 */
}
Important information
- USART Init is the following:
void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 9600;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
HAL_UART_Receive_IT(&huart1,&data_rx,1);
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
- USART Callback is the following:
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
RX_DATA[j++]=data_rx;
if(j>255)j=0;
uart_receive_input(data_rx);
HAL_UART_Receive_IT(&huart1,&data_rx,1);
}
- Program make use the following interrupts:
SYSTICK Interrupt - HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
USART - HAL_NVIC_SetPriority(USART1_IRQn, 1, 0);
TIM1 Interrupt - HAL_NVIC_SetPriority(TIM1_BRK_UP_TRG_COM_IRQn, 2, 0); - Flash Operation is not executed into any kind of Interrupt.
It is just called during normal operation time in While Loop. - The problem insist to appear only when USART module is never called during initial Call during initialization.
