CPU halting when working with timeval, gettimeofday()

youmebangbang
Posts: 3
Joined: Thu Aug 31, 2017 2:38 am

CPU halting when working with timeval, gettimeofday()

Postby youmebangbang » Thu Aug 31, 2017 2:43 am

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);
  }

}

ESP_Sprite
Posts: 9041
Joined: Thu Nov 26, 2015 4:08 am

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

Postby ESP_Sprite » Thu Aug 31, 2017 2:47 am

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.

youmebangbang
Posts: 3
Joined: Thu Aug 31, 2017 2:38 am

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

Postby youmebangbang » Thu Aug 31, 2017 2:00 pm

Great, thank you!

Who is online

Users browsing this forum: chegewara and 128 guests