cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F3 timer3 problem

dooz
Associate II
Posted on August 11, 2015 at 09:40

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.

3 REPLIES 3
dooz
Associate II
Posted on August 11, 2015 at 11:09

I also tried to call flash saving function inside the main loop after the timer fills the buffer, but each time the saving process occurs I can no longer start my timer (using button) because then the program is catching a lag.

Flash writing function works fine, I checked it (I programmed a button to write next flash page everytime it's pushed. After that I can see the correct data written in flash registers on exact pages I wanted. Timer alone also works fine by evoking an interrupt every 10ms.

Sometimes a compiler is also giving such a result:

Open On-Chip Debugger 0.9.0-dev-00415-g2d4ae3f-dirty (2015-04-22-11:10)

Licensed under GNU GPL v2

For bug reports, read

    http://openocd.org/doc/doxygen/bugs.html

Info : auto-selecting first available session transport ''hla_swd''. To override use 'transport select <transport>'.

adapter speed: 1000 kHz

adapter_nsrst_delay: 100

Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD

none separate

srst_only separate srst_nogate srst_open_drain connect_deassert_srst

Info : Unable to match requested speed 1000 kHz, using 950 kHz

Info : Unable to match requested speed 1000 kHz, using 950 kHz

Info : clock speed 950 kHz

Info : STLINK v2 JTAG v23 API v2 SWIM v0 VID 0x0483 PID 0x3748

Info : using stlink api v2

Info : Target voltage: 2.928257

Info : stm32f3x.cpu: hardware has 6 breakpoints, 4 watchpoints

Info : accepting 'gdb' connection on tcp/3333

Info : device id = 0x10036422

Info : flash size = 256kbytes

undefined debug reason 7 - target needs reset

Info : Unable to match requested speed 1000 kHz, using 950 kHz

Info : Unable to match requested speed 1000 kHz, using 950 kHz

adapter speed: 950 kHz

target state: halted

target halted due to debug-request, current mode: Thread

xPSR: 0x01000000 pc: 0x08005cd8 msp: 0x2000a000

Info : Unable to match requested speed 1000 kHz, using 950 kHz

Info : Unable to match requested speed 1000 kHz, using 950 kHz

adapter speed: 950 kHz

target state: halted

target halted due to debug-request, current mode: Thread

xPSR: 0x01000000 pc: 0x08005cd8 msp: 0x2000a000

Info : Unable to match requested speed 8000 kHz, using 4000 kHz

Info : Unable to match requested speed 8000 kHz, using 4000 kHz

adapter speed: 4000 kHz

target state: halted

target halted due to breakpoint, current mode: Thread

xPSR: 0x61000000 pc: 0x2000003a msp: 0x2000a000

Info : Unable to match requested speed 1000 kHz, using 950 kHz

Info : Unable to match requested speed 1000 kHz, using 950 kHz

adapter speed: 950 kHz

target state: halted

target halted due to debug-request, current mode: Thread

xPSR: 0x01000000 pc: 0x08005d80 msp: 0x2000a000

Info : dropped 'gdb' connection

dooz
Associate II
Posted on August 11, 2015 at 13:12

Okey, I no longer have this problem with matching speed. I add to debug configuration startup these lines:

monitor reset

monitor halt

monitor flash protect 0 0 11 off

but it still doesnt solve the problem of halting the program when trying to start a timer after writing to flash.

dooz
Associate II
Posted on August 12, 2015 at 11:06

I have moved timer IT function into main module using  HAL_TIM_PeriodElapsedCallback()  and it works fine until 3rd page of flash is written. Then it freezes. Either way if I first save data to flash and then start the timer if freezes instantly and I have to programm the Microcontroller again.

edited:

I fount the problem. Timer is blocking the microcontroller after calling of HAL_FLASH_Unlock()  HAL_FLASH_Lock();

But I dont know the reason of such a behaviour. Any help will be appreciated.