• AVR Freaks

Helpful ReplyHot!function localtime() of time.h consuming much flash memory (8.6KB) of PIC32MX

Author
seccoxiru
Junior Member
  • Total Posts : 103
  • Reward points : 0
  • Joined: 2011/10/27 13:42:59
  • Location: 0
  • Status: offline
2019/10/05 16:08:57 (permalink)
0

function localtime() of time.h consuming much flash memory (8.6KB) of PIC32MX

Hi.
I use a PIC32MX270F256D interfaced with an ESP-01 Wifi module via UART/AT commands.
I am obtaining from a NTP server (pool.ntp.br) the timestamp, seconds since 1/1/1900 00:00:00, it is 4 bytes that forms a time_t value;
 
Thats the unique point of my code where I call function DataHour_UpdateViaNTP()...
If I do comment the calling of DataHour_UpdateViaNTP() below and compile, there are 8.6KB less of flash usage.

//check if received reply from NTP server 
if (ptr_wifi->ntp_req == true && req_size == 48)
{
ptr_wifi->ntp_req = false;
time_t seconds = (string[40] << 24) + (string[41] << 16) + (string[42] << 8) + string[43];
DataHour_UpdateViaNTP (DATA(), seconds);
}

 
Thats where I use function localtime()...
void DataHour_UpdateViaNTP (DATA_t *ptr_data, time_t seconds) 
{
//NTP server reply regards to seconds from 1/1/1900 00:00:00
//converts from 1/1/1900 00:00:00 to 1/1/1970 00:00:00
seconds -= 2208988800;

//GMT
int32_t GMT_seconds = fabs (ptr_data->fuso_horario);
GMT_seconds *= 3600;
if (ptr_data->fuso_horario > 0) { seconds += GMT_seconds; }
if (ptr_data->fuso_horario < 0) { seconds -= GMT_seconds; }

ptr_data->current_date = localtime(&seconds);

ptr_data->ano = ptr_data->current_date->tm_year - 100;
ptr_data->mes = ptr_data->current_date->tm_mon + 1;
ptr_data->dia = ptr_data->current_date->tm_mday;
ptr_data->hora = ptr_data->current_date->tm_hour;
ptr_data->minuto = ptr_data->current_date->tm_min;
ptr_data->segundo = ptr_data->current_date->tm_sec;
ptr_data->dia_da_semana = ptr_data->current_date->tm_wday;
}


With DataHour_UpdateViaNTP() present: "Program used = 113.456"
With DataHour_UpdateViaNTP() commented: "Program used = 104.632"
8.6KB of difference. I think thats so much.

Why my function if consuming so much flash?
What other method could I use to do the same conversion I am doing with localtime ()? That is lighter in terms of flash.
Compiler = XC32 v1.43
MPLAB X = v5.25

Regards.
post edited by seccoxiru - 2019/10/05 16:23:58

Attachment(s)

Attachments are not available: Download requirements not met
#1
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11395
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: function localtime() of time.h consuming much flash memory (8.6KB) of PIC32MX 2019/10/05 16:45:22 (permalink)
+2 (2)
I'm not sure why you expected localtime to be trivial.  It has to deal with time zones, daylight saving time, leap years, leap seconds, among other things.
 
I notice you're also using floating point (fabs); are you sure that's not the source of your issue?
#2
andersm
Super Member
  • Total Posts : 2666
  • Reward points : 0
  • Joined: 2012/10/07 14:57:44
  • Location: 0
  • Status: online
Re: function localtime() of time.h consuming much flash memory (8.6KB) of PIC32MX 2019/10/05 16:45:49 (permalink)
+3 (3)
Using fabs() will cause a whole lot of floating-point emulation code to get pulled in.
#3
seccoxiru
Junior Member
  • Total Posts : 103
  • Reward points : 0
  • Joined: 2011/10/27 13:42:59
  • Location: 0
  • Status: offline
Re: function localtime() of time.h consuming much flash memory (8.6KB) of PIC32MX 2019/10/05 17:48:22 (permalink)
0
It is not fabs() the problem.
If I remove it, the code reduce in 32 bytes (8 instructions) only.
 
Maybe... Do you guys know if there is a specific protocol of some type of servers from where I could obtain the date/hour directly in ASCII format? usinf for example the format YYYY/MM/DD HH:MM:SS... ? This would be much more insteresting for me.
 
Thanks anyway for your helps.
 
#4
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11395
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: function localtime() of time.h consuming much flash memory (8.6KB) of PIC32MX 2019/10/05 18:00:23 (permalink) ☄ Helpfulby seccoxiru 2019/11/05 08:16:08
+2 (2)
Do you really need localtime?  How are you providing the time zone to localtime?
 
Can  you use gmtime instead?  I would expect it to be much smaller.
#5
seccoxiru
Junior Member
  • Total Posts : 103
  • Reward points : 0
  • Joined: 2011/10/27 13:42:59
  • Location: 0
  • Status: offline
Re: function localtime() of time.h consuming much flash memory (8.6KB) of PIC32MX 2019/11/05 08:18:31 (permalink)
0
jtemples
Do you really need localtime?  How are you providing the time zone to localtime?
Can  you use gmtime instead?  I would expect it to be much smaller.



Timezone is a configuration of the product, I provide it, and before suply the time in seconds for localtime I subtract/add seconds to the raw value.
using gmtime instead of localtime gave me almost the same results with 2.56KB less of flash memory.
#6
Jump to:
© 2019 APG vNext Commercial Version 4.5