0
\$\begingroup\$

I am new to raspberry pi (Raspbian). Any guidance in the matter will be appreciated!

I have written a C code which includes C Filehandling & wiringPi.h library for GPIO manipulations.

I have narrowed down the problem to the empty infinite loop. If I include it in the main function, my terminal freezes and not even Hello world! gets printed.

Should I do implement this infinite loop in some other way because it's a raspberry? If so, then how?

This is how the main() function looks:

// Main function
int main (void)
{
  printf("Hello world!");
  delay(1000);

  // Read updated preferences file
  readPrefs();

  // Initialize WiringPi Library
  wiringPiSetup () ;

  // Setup falling edge interrupt on INPUT pin
  wiringPiISR (INPUT, INT_EDGE_FALLING, &myInterrupt0) ;

  // Setup OUT1 & OUT2 as output
  pinMode (OUT1, OUTPUT) ;
  pinMode (OUT2, OUTPUT) ;

  // Initialize motor
  motorSTOP();

  for (;;)
  {
    // Empty loop since all the work is done in Interrupt routine

  }

  return 0 ;
}

Here is the interrupt routine:

// Interrupt Service routine
void myInterrupt0 (void) { 

    motorFWD();
    delay(FWDDelay);

    motorSTOP();
    delay(WaitDelay);

    motorBCK();
    delay(BCKDelay);

    motorSTOP();    
}
\$\endgroup\$
  • \$\begingroup\$ If you accept an answer you may also want to upvote it. It would be strange if an answer is good enough to actually solve your problem, but not "useful" as the upvote reason states. \$\endgroup\$ – pipe Sep 10 '17 at 7:36
  • 1
    \$\begingroup\$ One comment about your code: the empty loop will cause 100% CPU load. Place a delay inside to avoid this. And a while(1){} is more readable than a for(;;) {} \$\endgroup\$ – sweber Sep 10 '17 at 7:40
  • \$\begingroup\$ @sweber If there is some desktop OS like Linux, it will be pre-emptive. Not quite sure what you wish to achieve with the delay() then, spook up the context switching? \$\endgroup\$ – Lundin Sep 11 '17 at 14:43
  • \$\begingroup\$ @Ludin: To be honest, I have trouble understanding your comment. An infinite, emtpy loop causes CPU load. It would be best to let the main function sleep forever, but looping over long delays works quite well, too. An empty loop is often used on simple microcontrollers, but this is a pi, a small computer... \$\endgroup\$ – sweber Sep 11 '17 at 17:46
  • \$\begingroup\$ @sweber You let the process go down by waiting for some manner or wake-up event. Adding delays is a poor solution, your program will basically just sit there screaming "ok context switch now, zzz, ok context switch now, zzz..." over and over. A preemptive OS will be able to do that by itself, without your process manually yielding its time slice over and over. \$\endgroup\$ – Lundin Sep 12 '17 at 6:43
4
\$\begingroup\$

Streams, to which functions like printf() write, by default exhibit line buffered behavior, and do not typically actually write the buffered output until either a line ending is encountered or an explicit fflush() is called.

Therefore, either change your message to include a newline:

printf("Hello world!\n");

Or explicitly flush the stream

printf("Hello world!");
fflush(stdout);

As this is really a generic programming question having nothing in particular to do with hardware or the Raspberry Pi, it would probably have better belonged on Stackoverflow - where it has no doubt been answered numerous times in the past. Though it's quite understandable why the use of interrupts in an adjacent part of the code could be distracting, even though actually not relevant to the issue.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.