AnsweredAssumed Answered

STM32F3 timer3 problem

Question asked by oziom.dominik on Aug 11, 2015
Latest reply on Aug 12, 2015 by oziom.dominik
Hello,
I'm using timer3 to generate an interrupt every 10ms (it saves one value everytime to a buffer). Everything works fine until I try to save buffer data to 1 page of flash. It happens of course inside the interrupt function after a buffer is fully wrtten. The saving into flash function works fine but the problem is that when the saving happens in an interrupt my μC is just catching a huge lag and even reset button can't help about it.

variables and interrupt function in timer module:

extern uint32_t DataPage[512]; // flash page buffer
extern unsigned long TablicaStron[128]; //table of flash pages
extern uint32_t dana;  // data to be saved into buffer
extern int DataIndex;       // pointer to another place in page buffer
extern int PageNumber; 
extern uint32_t Address;
extern TIM_HandleTypeDef htim2;

/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/
extern TIM_HandleTypeDef htim3;



void TIM3_IRQHandler(void)
{
  /* USER CODE BEGIN TIM3_IRQn 0 */

  /* USER CODE END TIM3_IRQn 0 */
  HAL_TIM_IRQHandler(&htim3);
  /* USER CODE BEGIN TIM3_IRQn 1 */

  dana =  htim2.Instance->CNT; // current encoder value
  write1BrickFlash( DataIndex, dana); // saves one data into buffer
  DataIndex++;
  if(DataIndex==512)
  {
      WriteFlashPage();
      EraseDataPage();    //clears the buffer
      IncreasePageNumber(PageNumber);
      DataIndex=0;
  }
  /* USER CODE END TIM3_IRQn 1 */
}


my flash saving function situated in main.c file module looks like that:

static FLASH_EraseInitTypeDef EraseInitStruct; //Variable used for Erase procedure


void WriteFlashPage(){
    HAL_FLASH_Unlock();

      int index=0;
      __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP|FLASH_FLAG_PGERR|FLASH_FLAG_WRPERR|FLASH_FLAG_BSY);
    EraseInitStruct.TypeErase = TYPEERASE_PAGES;
    EraseInitStruct.PageAddress = TablicaStron[PageNumber];
    EraseInitStruct.NbPages = (TablicaStron[PageNumber+1] -   TablicaStron[PageNumber])/FLASH_PAGE_SIZE;
    if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK)
    {
      while (1)
      {
        HAL_GPIO_WritePin(GPIOE, GPIO_PIN_13, GPIO_PIN_SET); //Page erase error
      }
    }

    Address = TablicaStron[PageNumber];
    while (Address < TablicaStron[PageNumber+1])
    {
        if (HAL_FLASH_Program(TYPEPROGRAM_WORD, Address, DataPage[index]) == HAL_OK)
        {
            index++;
            Address = Address + 4;
      }
    }
    HAL_FLASH_Lock();
}

I have no idea where the problem lies. Any help will be appreciated.
Regards
Dominik O.

Outcomes