Can I use strptime and mktime on Atmel studio for AVR mega ?

Go To Last Post
24 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Guys,
Can I use strptime and mktime on Atmel studio for AVR mega ?
Reference :
http://stackoverflow.com/questions/1002542/how-to-convert-datetime-to-unix-timestamp-in-c

Or may be there's other function replacing them?

Thank you

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Which C compiler are you talking about? If avr-gcc then the current issue that you are probably using (4.7.2 with AVR-LibC 1.8.0) does not have those functions but the development tree for AVR-LibC now has time.h functions added so they will be available in the next major build/release of the tools.

In the meantime you could take a peek and "borrow" some of the files from here:

http://svn.savannah.nongnu.org/v...

BTW what RTC chip are you using?

(the reason AVR-LibC has not had time.h up to now is that AVRs don't inherently have RTCs)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm using DS1307, how can I check which compiler I used ? I have atmel studio 6 here....
it's avr-gcc, but I don't know which version, can't find it...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well if you use AS6 (latest) it is avr-gcc 4.7.2.

Anyway the last tagged release of AVR-LibC was 1.8.0 which is the one that recentAS6 has and those time.h functions hadn't made it into HEAD at that release.

So you have to do as I say, either wait for the next AVR-LibC or pre-empt that and take your own snapshot of the time functions. As the libc.a you have installed won't contain the functions you need to pick up and add any relevant .c files to your own project too.

For mktime.c it looks like you would need (at least) mktime.c, localtime.c, localtime_r.c, gmtime_r.c.

In fact you might find that gmtime_r() offers most of what you are trying to achieve on its own.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you very much for a detail explaination of yours clawson, let me understand it bit by bit...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Typically there was no operating system used on AVR projects.
So there are no timer functions available inside the standard libs.

But you can write your own RTC routines or use external libs.
At first you must decide, if you use an internal timer or an external connected timer chip.

Often T2 was used as RTC with a watch crystal connected.
This has the advantage, that no data transfer and converting crazy formats was needed.
All timing variables are available inside the AVR.

Peter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's AVR-GCC 4.72

I saw it :
Atmel Toolchain\AVR8 GCC\Native\3.4.2.939\avr8-gnu-toolchain\lib\gcc\avr\4.7.2\include

and I'm using DS1307 as my RTC so I need to convert from my RTC to 32bits Unix stamp, vice versa and save it to EEPROM, in this case I need 4 bytes everytime I saved.....please correct if I'm wrong,
is long integer enough for it ?

thank you

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi bianchi77

 

Did you manage to use gmtime_r() without the unpublished time.h ?

 

I use Atmel Studio 6 (Version: 6.2.993 - Beta) and AVR8_Toolchain_Version:3.4.4.1162 GCC_VERSION:4.8.1

 

I didn't update to 6.2 1502 yet.

 

Anyone know when the avr libc is updated to contain time.h ?

 

 

Regards

 

msskov

Last Edited: Thu. Nov 13, 2014 - 12:49 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Anyone know when the avr libc is updated to contain time.h ?

Well AVR-LibC itself has already been updated (at 1.8.1) but I guess you mean "an Atmel issued copy of AVR-LibC"? One can only guess what their release schedule is. In the meantime it's all accessible to you. The files are here:

 

http://svn.savannah.nongnu.org/v...

 

with the time.h header here:

 

http://svn.savannah.nongnu.org/v...

 

As those time .c files are not built into the copies of libc.a in your installation you will need to cherry pick the source files that provide the functions you want (and all the files they then rely on and so on). It might be easiest to simply add ALL the .c files and just rely on -function-sections and -gc-sections (on by default in Studio 6) to simply throw away anything you aren't actually calling.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi clawson.
It it excactly what I mean.
Im just trying to avoid coding something allready invented.
Specifically I need to make a timestamp from the ds1307 for data logging purpose and to increase logging time I would like a 4byte timestamp instead of a 6 byte bcd timestamp.

Regards

 

msskov

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Specifically I need to make a timestamp from the ds1307 for data logging purpose and to increase logging time I would like a 4byte timestamp instead of a 6 byte bcd timestamp.

Well, what format/timebase do you prefer?

 

How would any time.h functions help?

 

See this post and following code fragments.  (and the whole thread is applicable)

https://www.avrfreaks.net/comment...

As a diversion, I have a current application for a series of controllers that has an RTC, as well as logging to SD card as an integral part of the application. That uses FATFS -- which needs a "DOS" timestamp!

So, twice a second I get the time from the RTC, in a typical BCD format. Because I know further manipulations are needed, I make a "bin[ary] time" in the same routine. Then, I convert to Unix timestamp.

When needed (creating a file on the SD card, the "bintime" is transformed to a DOS timestamp. Code below...

As a final kick in the pants, on the PC side when the log files are processed, one of the options is to create comma-delimited files suitable for Excel import. So there is another routine to turn Unix time into Excel time.

 

DS1307 might be slightly different format than DS1305/6 but not much.

 

Also:

https://www.avrfreaks.net/comment...

https://www.avrfreaks.net/comment...

"borrowed" here: ;) https://www.avrfreaks.net/comment...

https://www.avrfreaks.net/comment...

 

 

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi theusch

Well, what format/timebase do you prefer?

I didn't know there is different format or timebases to use ?

I allways use our national winter time allso during the summer. The pc that will collect the data will also use winter timer. So for me there is no doubt which timezone.

 

How would any time.h functions help?

I would have used mktime to create a binary timestamp. But when you ask I might think I didn't have read the full description or has misunderstand something ?

 

I have searched the forum and google for a (for me) easy solution. The 1st link of yours seems to help me a lot.

I'm not that used to c and the macro used in many other solution I found.

 

Thanks

 

 

Regards

 

msskov

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Im just trying to avoid coding something allready invented.

I did a bit of reading about time.h facilities in the C standard, and I understand your aim.

 

In practice, though, on an AVR8 app, I think I'd rarely if ever make use of time.h facilities "as is".  For sure, they aren't sufficient to do your job...

 

1.  A routine is needed to communicate "get time" with an arbitrary RTC chip.

2.  That timestamp (in BCD for DS130n and many others) needs to be converted to load a tm structure binary components.

3.  tm has all "int" components -- that sucks up SRAM on an e.g. Mega48 app, vs. the unsigned char I use in my implementation.

4.  When setting the clock, a routine is needed to convert the new clock settings to the needed BCD format...

5. ... to be given to the routine that does "set time" to the RTC chip.

 

No help from time.h so far...

 

6.  As you mentioned, for logging you don't want to store 9x int, 18 bytes.  So a more compact representation is needed.  I chose unixtime; you might choose a similar 4-byte representation.  If you choose time_t -- that is implementation-defined, isn't it?

6a.  With unixtime or similar representation, simple arithmetic is easy.  For example if I have a "start cycle" and "end cycle" timestamp, simple subtraction gives elapsed time.

6b.  While time.h has clock arithmetic, it perhaps isn't going to be as small or efficient as 32-bit subtract or similar.

 

7.  As far as display goes, I need to cram the clock/date onto the controller's LCD display.  Different on 2x8 and 4x20.  Different for "short" and "long" forms.  time.h only helps if the standard format is good enough.

 

So,

How would any time.h functions help?

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not sure I'd agree with Lee. <time.h> would be my "go to" solution for anything involving translations to/from the "unixtime" format. Sure you might have to "massage" the BCD a bit to fit into a tm struct but that's just "glue".

If you choose time_t -- that is implementation-defined, isn't it?

Is it? Do you actually know of an implementation of <time.h> where it is NOT just seconds since 1st Jan 1970 in a uint32? In my world that's what I call "unixtime".

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Did I just start an argument ? :)

Regards

 

msskov

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"discussion" :-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is it? Do you actually know of an implementation of <time.h> where it is NOT just seconds since 1st Jan 1970 in a uint32?

"Google is your friend."

 

Yes, sizeof(time_t) is 8 bytes on some systems, and indeed 4 bytes on many eunuchs.  Note that floating-point representation is also allowed.  (Excel-style?)

 

Signedness varies.  Not a problem for my logging app but doesn't the lost bit cut back the end of the range?

 

I did see a mention that at least one operating system used a different epoch.

 

I'll stand behind my post, for AVR8 work.  I described my app in the links.  RTC w/ BCD; so conversion to/from needed.  "tm"-equivalent for time-setting routine (binary pieces).  unixtime for logging.  Constructed DOS time for SD card.  Excel time for PC processing.  Several short and long display formats.

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In avr-libc time_t is the number of seconds since 2000-01-01 00:00:00. See the manual in the projects section:

 

https://www.avrfreaks.net/projects/avr-libc-time-functions-manual

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Clawson: You got it :-)

Regards

 

msskov

Last Edited: Fri. Nov 14, 2014 - 08:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch

 

I'm looking into your code you sugested from this link: https://www.avrfreaks.net/comment/1039436#comment-1039436

 

I copy and pasted it into a .c file and made a header file as well (DSTIME.c, DSTIME.h)

But I think you didn't posted it in the right order. I took the last part with the defines and put it in the top of my DSTIME.c

 

.....I get lots of errors and warnings.

 

And I think some of them is because I havn't included the needed libraries.

 

eg: where do I find the flash type ?

 

could you please tell me which libraries do I need ?

 

I have included the files I made in DSTIME.zip and also the output.txt with the errors I got.

 

My intension was to adapt your code to the DS1307. If I'm not allowed to do that, I'll respect that and don't use it.

 

 

 

Attachment(s): 

Regards

 

msskov

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And I think some of them is because I havn't included the needed libraries.

 

eg: where do I find the flash type ?

That code is for CodeVision. (and isn't for a '1307 anyway)

 

But any steps are very straightforward, aren't they?

 

(I thought the GCC gurus had now convinced you to follow the One True Way. devil )

 

Lessee--where did I use "flash" in that?  Names of months?  ...nope...days-per-month table.

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

CodeVision... OK

 

steps is easy if you know where to put those feet of mine !

 

I'm not aware of which religion people have regarding cv or gcc :-). I still have to mess with something not adapted to Atmel Studio.

Regards

 

msskov

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi again

 

I followed this one by theusch, see #7: https://www.avrfreaks.net/forum/timeh-0?name=PNphpBB2&file=viewtopic&t=122268

 

And for others that need a bcd2timestamp function, I added my file made in Atmel Studio 6.2 Sp1. (gcc)

If you are a slow learner like me, I hope this will help you.

 

And again, theusch, thanks for helping me the right way.

 

I will close this tread again. Sorry bianchi77 for hijackin your tread :-)

 

ps: Forget the DSTIME.* files I made in #21

Attachment(s): 

Regards

 

msskov