S12XDP512 flash, watchdog and PIT interaction

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

S12XDP512 flash, watchdog and PIT interaction

1,200 Views
harshsaga
Contributor I
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
Labels (1)
0 Kudos
1 Reply

367 Views
harshsaga
Contributor I
Ok, asked the reigning S12X / Codewarrior expert at my work, and he fixed my problem.

FYI, turns out that while I had a line, #pragma CODE_SEG __NEAR_SEG IntReqHndlr_Code_Seg, before my definition of
ExtWdToggle_PseudoIsr(), to allocate it to RAM, I also needed a similar #pragma before the function prototype to make sure the compiler uses the right function call paradigm for calling code in RAM.

-harshsaga

0 Kudos