Page 1 of 1

CPU halting when working with timeval, gettimeofday()

Posted: Thu Aug 31, 2017 2:43 am
by youmebangbang
Hi, I am trying to run a simple timer program to print to COM ever so many milliseconds. Code compiles just fine but when i run i get a CPU halted error , " Guru Meditation Error of type LoadProhibited occurred on core 1. Exception was unhandled." I know it has to be a simple problem (im learning).

Code: Select all

#include <sys/time.h>

timeval *past_time;
timeval *current_time;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
 gettimeofday(past_time, NULL);
}

void loop() {
  // put your main code here, to run repeatedly:
  gettimeofday(current_time, NULL);
  if((current_time->tv_usec - past_time->tv_usec) > 2000)
  {
    Serial.println(analogRead(32));
    gettimeofday(past_time, NULL);
  }

}

Re: CPU halting when working with timeval, gettimeofday()

Posted: Thu Aug 31, 2017 2:47 am
by ESP_Sprite
It's a C thing. You have declared current_time and past_time as pointers (presumably to a location that will contain the time). You do not initialize them, so they point at... nothing. Then you ask the gettimeofday function to put the current time at whereever the pointer is pointing at... which is essentially nowhere. The gettimeofday function call tries to do that, the CPU detects it's trying to do something that does not make sense (Write bytes to nowhere? Huh?) and panics.

I can tell you how to resolve this, but it may be more useful if you read up on pointers and see if you can fix this yourself with the above information.

Re: CPU halting when working with timeval, gettimeofday()

Posted: Thu Aug 31, 2017 2:00 pm
by youmebangbang
Great, thank you!