This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Terminating the program execution

Other Parts Discussed in Thread: TMS320F2812

Hello,

I am programming the digital signal controller TMS320F2812. Under a fault condition, I want the controller to terminate the execution of the control program and all other activity until the device is reset.That is, the controller should exit the infinite loop in which it normally stays, and also stop everything else such as the timers.

So far I have tried the statement return 0; in the following program structure:

int main(){
  // variable declarations
  // register setups
  while (1){
    if (fault condition)
      return 0;
    // control program   
  }
}

Apparently the code execution is indeed stopped when the fault occurs. However, the timer output is still functioning; it keeps outputting a pulse sequence. The bits T1CON.FREE and SOFT are both set to 0 and thus the timer stops on emulation suspend. I therefore expect the timer to stop operating when the program is terminated as well. Setting T1PIN to zero before the return statement forces the timer output low, but I would rather have the timer (and all other peripherals) stop operating completely. How can that be achieved?

Regards,

Adrian

  • Hi,

    in case of firmware written in a languages such as C or C++, what happens if main() exits depends on the used compiler and the startup code of the included run-time-support library. It is possible that the code will enter an infinite loop when main() returns. It is even possible that the next memory address after the last instruction in main() is empty. On a microcontroller with FLASH memory, an empty instruction contains the value 0xFFFF. For many architectures this op code is interpreted as a 'no operation'. It simply does nothing. The CPU would continue executing those nops all the way down the memory to the end and wrap around back to 0x0000. The CPU happily continues executing instructions back at the beginning of the code, just as if it had been reset. Other CPUs may rely on "halting" by simply just jumping to the same address repeatedly.

    3446793 said:

    Under a fault condition, I want the controller to terminate the execution of the control program and all other activity until the device is reset.That is, the controller should exit the infinite loop in which it normally stays, and also stop everything else such as the timers.

    I guess you are writing a compiled application program that is the only thing that is running on your signal controller (no underlying OS). You're thinking too high level. The concept of a program shutdown doesn't normally exist in an embedded environment. At a low level a CPU will fetch and execute instructions from the current position pointed to by the program counter while it can; there is no such thing as a "final return statement". A CPU may stop execution if it encounters an unrecoverable fault or if explicitly halted (put into a sleep mode, a low power mode, etc). Many microprocessors have halt instructions, which stops the processor but without affecting its perhiperals.

    3446793 said:

    However, the timer output is still functioning; it keeps outputting a pulse sequence.

    You have to stop all running peripherals by yourself, if that is the requirement of your application.

    int main(){
      // variable declarations
      // register setups
      while (1){
        if (fault condition)
           break;
        // control program
      
    }

      // stop peripherals
      // enter power down mode
      // enter endless-loop for(;;)

      return 0;
    }

    If the application requires the CPU to power down, then, depending on the CPU, there will probably be various sleep modes available. If you have a legitimate need for any of that in your application you will have to do it yourself. However, CPUs have a habit of waking up again, so you'd have to make sure there was no time limit to the sleep, and no Watch Dog Timer active, etc.

    You could even organise some external circuitry that would allow the CPU to completely cut its own power when it had finished the application.

    Regards,
    Christian

  • Thanks for your response and detailed explanation, Christian.

    I will disable interrupts and the peripherals then. That should be good enough.