'_gettimeofday' compiler error when try to set RTC time using GCC compiler func - Page 1
Forum section: electronics / general

author: muthukural001
author_rank: Regular Contributor
timestamp: December 11, 2017, 08:23:00 am
content: Hello all,I am using Atmel studio with GCC compiler for our project. In this,I want to set RTC time using compiler time functions instead setting them manually. The below code is that what I am using to get this.main.cCode:#include "samd11.h"#include "hal_gpio.h"#include "i2c_master.h"#include "ISL29029.h"#include "ISL29029_regs.h"#include "MCP79410.h"if(MCP79410_IsRunning()) //If clock is already running stop it{MCP79410_DisableOscillator();}MCP79410_Initialize(); //Initialize RTCC with system time and date */MCP79410.cCode:#include <time.h>#include "i2c_master.h"#include "MCP79410.h"void MCP79410_Initialize(void){MCP79410_SetHourFormat(H24); //Set hour format to military time standardMCP79410_EnableVbat(); //Enable battery backuptime_t t = time(NULL);struct tm tm1 = *localtime(&t);RTCC_Struct curr_time = {tm1.tm_sec,tm1.tm_min,tm1.tm_hour,tm1.tm_wday,tm1.tm_mday,tm1.tm_mon+1, ((tm1.tm_year+1900)-2000)};MCP79410_SetTime(&curr_time);MCP79410_EnableOscillator(); //Start clock by enabling oscillator}When I compile the above code, I am getting "undefined reference to '_gettimeofday'. Please suggest.Regards,Muthu

author: hli
author_rank: Frequent Contributor
timestamp: December 11, 2017, 01:15:34 pm
content: time()gets the current time. What do you think where this comes from? These functions are part of the C time library, and assume a regular OS environment (read: Linux or something similar).You need to get yourtmvalues from the user running the program.

author: westfw
author_rank: Super Contributor
timestamp: December 13, 2017, 07:49:55 am
content: The time library from time.h/etc has all sorts of functions for manipulating time values, but YOU must provide the core _gettimeofday() function for it to use, since that's hardware-dependent. (For example, you might choose to read the RTC chip each time, or you might read the RTC at system startup, and then increment it based on a system-level timer.)(Your code seems to be setting the RTC time based on the system time. Isn't that ... backward?)

author: technix
author_rank: Super Contributor
timestamp: December 13, 2017, 11:05:27 am
content: The libraries ended up calling _gettimeofday and did not find it. In UNIX this is a syscall the kernel would provide responding with the current time in the system RTC, but here in embedded world you need to provide your own. Beware don't create a circular reference though, as it is likely you are writing a driver for the RTC.

author: CM800
author_rank: Frequent Contributor
timestamp: December 13, 2017, 04:06:24 pm
content: I guess you could say that the compiler just isn't giving you the time of day....Sorry.. that's all I can contribute here.

author: muthukural001
author_rank: Regular Contributor
timestamp: December 14, 2017, 09:58:20 am
content: Hello,From what you said, I understood that one should set RTC time with current time values directly rather than compiler as it not support that feature.I came up with push button switch input idea. This is to match clock with real time world signal When I press push button, it will write time values to RTC and start oscillator.Please let me knowRegards,Muthu

author: shawty
author_rank: Regular Contributor
timestamp: December 14, 2017, 10:54:29 am
content: ok, so when you want to use particular functions in ANY libraries that you include in your project, the library definition you include will set these up for you.Taking the first bit of code you posed as an example:Code:#include "samd11.h"#include "hal_gpio.h"#include "i2c_master.h"#include "ISL29029.h"#include "ISL29029_regs.h"#include "MCP79410.h"if(MCP79410_IsRunning()) //If clock is already running stop it{MCP79410_DisableOscillator();}MCP79410_Initialize();See all those lines that start with "#include"? well what your doing there, is asking the compiler to go and fetch, then set up it's libraries that it has by those names.That will then make the functions available in those libraries, available for you to call in your own code, and use the tasks that they perform.Some of these libraries need help for m you to know what to do, and they need this help because the C language cannot always know the differences in the systems that it's running on.When the libraries that you include, need some help, they usually want your help using something called, call backs.A call back is similar to you, calling a function in a library, but it works the other way, and it's the library that calls YOUR CODE to get the tasks it needs doing, performed.In your case, it appears that the libraries your using, want YOU to create a function called "_gettimeofday" so that it can get the actual time from your RTC (Or in fact anywhere you decide to get it from) and then feed it into it's own library code, so that you can then further make use of it with the other functions it provides.Now the problem I/WE on the forum have with your request for help, is we do not know the exact library and/or functions that you are including that are making the request for this call back, so we are unable to provide much help.What I will say however, is you'll find the answer in the documentation for the time library your using.If the time library is the generic Linux/Gnu library, then do some googling for "The Linux Documentation Project" aka (TLDP)The LDP, contains docs for just about every GNU C/C++ standard function (Most of it derived from the man pages available)The bottom line however is this:One of the libraries your using wants you to define a function for it to use in order to read the time source, and since that source is dependent on your project on the hardware your using, it cannot know in advance what the source is, and thus requires your help to define it.Shawty

author: technix
author_rank: Super Contributor
timestamp: December 14, 2017, 01:26:58 pm
content: Quote from: muthukural001 on December 14, 2017, 09:58:20 amYou would be creating a circular reference for this. One way I have been using is to let the code default to either a fixed date, or the time of compilation of the file, if the RTC is missing contents. Then after the system is up ask the user for the accurate time.

author: mikerj
author_rank: Super Contributor
timestamp: December 15, 2017, 12:42:59 pm
content: Quote from: muthukural001 on December 14, 2017, 09:58:20 amWhere are you getting your "real time world signal" from? e.g. GPS, MSF receiver etc?