Sagar Shah

S12XDP512 flash, watchdog and PIT interaction

Discussion created by Sagar Shah on Jun 13, 2008
Latest reply on Jun 13, 2008 by Sagar Shah
Hi all,

First off just want to say I've learned a lot by lurking here - this is a great forum with very helpful posters - keep up the good work.

My question is about flash operations and the PIT on the S12XDP512.  I'm writing a bootloader which must periodically toggle a port connected to an external watchdog, and flash new product code to locations in the flash memory.  I've set up a timer interrupt using the PIT, and the ISR for this interrupt toggles the port (period required for watchdog is once every second).

I've set up the flash routines and the timer ISR to be allocated to RAM, so they are not run from flash, and interrupts are disabled when a flash write command is initiated.  My issue is that I would like to keep toggling the watchdog port inside the flash routines, if the timer has expired.  I set up a check for the PTF flag for the appropriate channel, and a routine which toggles the port is called if it is set (timer has expired):

#define EXTERNAL_WATCHDOG_PORT PTJ_PTJ2

FLASH_ERROR_TYPE FlashCtrlWriteWord(UINT *__far WriteAddress, UINT Data, UCHAR disableInts)
{
  if (disableInts == 1)
    DisableInterrupts;

... // set up flash registers

  FSTAT_CBEIF = 1;

  while (FSTAT_CBEIF == 0)
  {
    if ( (disableInts == 1) && (PITTF_PTF3 == 1) )
    {
      ExtWdToggle_PseudoIsr();
    }
  }

... // same while loop for FSTAT_CCIF

  if (disableInts == 1)
    EnableInterrupts;

... // return errors if they exist
}

void ExtWdToggle_PseudoIsr(void)
{
  static UINT int_count = 0;
  int_count++;
  if (int_count >= 5)
  {
      int_count = 0;
      if (EXTERNAL_WATCHDOG_PORT == 0)
      {
          EXTERNAL_WATCHDOG_PORT = 1;
      }
      else
      {
          EXTERNAL_WATCHDOG_PORT = 0;
      }
  }
  PITTF = PITTF_PTF3_MASK;
}

__interrupt void ExtWdToggleIsr(void)
{
  ExtWdToggle_PseudoIsr();
}

I have not included the erase routine, as it is very similar to the write routine.  All code and data above are allocated to RAM.

The timer ISR works correctly.  The flash write routine does not work (processor jumps to random address and halts, etc.) when interrupts are disabled and ExtWdToggle_PseudoIsr() is called.  The timings for a max-delay flash write and sector erase are ~75 us and 27 ms, respectively, so I think I may be fine if the timer expires while inside the flash routines, and it is not serviced until interrupts are re-enabled (watchdog period is 1 sec).  However, I'd still like to get this to work.  Any ideas?

Thanks,
harshsaga

Outcomes