Page 1 of 1

Problems with "localtime()" while using Salvo and PIC18

PostPosted: Thu Feb 16, 2006 3:15 am
by Sanchez
Hi!
I have following problem. I'm using Hi-Tech PICC18 8.35 and Salvo (last version). I had 3 tasks, and after adding the fourth some problems begun. The values of local variables changed while switching to other tasks, I've redeclared them as 'static', but some problems still remain. After some experiments I understood that changing of values and other 'bugs' started to appear when I used function 'localtime' in my task. It looks like:
void Test(void) {
for(; {
...
struct * tm tp;
time_t clock = (time_t)12345678; // for example
tp = localtime(&clock); // during this instructions some bugs appear
...
// after 'localtime', program's behavior becomes very strange and not understandable
...
OS_Delay(10,...);
}
}

If I comment call of 'localtime'. task runs ok. Where can be a problem?


Re: Problems with "localtime()" while using Salvo and PIC18

PostPosted: Thu Feb 16, 2006 3:47 am
by zoomcityzoom
Using localtime in a multithreaded system may cause problems. Use localtime_r instead if you have it. If not, copy the values (you need) from the localtime struct to static local storage just after calling localtime().

Here's a description of localtime_r()


Function: struct tm * localtime_r (const time_t *time, struct tm *resultp)
The localtime_r function works just like the localtime function. It takes a pointer to a variable containing a simple time and converts it to the broken-down time format.

But the result is not placed in a static buffer. Instead it is placed in the object of type struct tm to which the parameter resultp points.

If the conversion is successful the function returns a pointer to the object the result was written into, i.e., it returns resultp.


Re: Problems with "localtime()" while using Salvo and PIC18

PostPosted: Thu Feb 16, 2006 6:22 am
by aek
Hmmm ... I can't comment directly re probems with localtime, but I can comment on how to help us (Pumpkin) solve a problem like this.

Build a Salvo project using the MPLAB-SIM, which calls localtime and exhibits the problem. Then we can run it here and figure out what's going on.

The only thing off the top of my head is the mention of three tasks .. The Salvo Lite libraries support only up to three tasks -- beyond that you must use an LE/Pro library or do a source-code build. Be sure your Salvo configuration is correct.

------------------


Re: Problems with "localtime()" while using Salvo and PIC18

PostPosted: Fri Feb 17, 2006 4:38 am
by aek
Also keep in mind that the situation you're describing can easily happen if the OSTASKS and OSEVENTS entries in your salvocfg.h are "less than" the numbers of tasks and events you're actually using, or if your task and event handles (OSTCBP(i), OSECBP(i), etc.) are outside the bounds of what has been set by OSTASKS and OSEVENTS in your salvocfg.h.

------------------


Re: Problems with "localtime()" while using Salvo and PIC18

PostPosted: Fri Feb 17, 2006 5:08 am
by Sanchez
Thank you for help, I remember that I tried to disable optimizations, but only for the file which contains this task, I will try to disable them all, but now only after holidays.
And as for numbers of tasks - all ok, I set these numbers correctly.

Re: Problems with "localtime()" while using Salvo and PIC18

PostPosted: Fri Feb 17, 2006 7:54 am
by aek
Sounds like PICC-18 may be playing with some optimizations -- have you tried your code with optimizations disabled?

PICC-18 is quite different from the other compilers we support, as it is not stack-based (it uses a static overlay). For this reason, problems like the one you describe are even less liekly to have something to do with Salvo, as Salvo's context switcher is very simple and non-stack-based for PICC-18.

------------------


Re: Problems with "localtime()" while using Salvo and PIC18

PostPosted: Fri Feb 17, 2006 12:08 pm
by Sanchez
zoomcityzoom, thx, i will try this function, but it's strange that i didn't found any description about it in ht-picc manual.
aek, no, i'm building projects on source files. I can describe another similiar problem: i have function LCDOutString(const char * str), that prints string 'str' on LCD. And something like this code in task:

for (; {
static char * str2;
// some operations with localtime
LCDOutString("Wassuup");
str2 = "qwerty";
...
OS_Delay
}

In first cycle, LCD printed "Wassuup", but at second - "qwerty" ! As you see i didn't send 'str2' as an argument to LCDOutString(), but...

I will try localtime_r and write here results.

And i have one question, if i define in main.c some variables and export them in main.h (extern ...), and then i can use them in another tasks (in another c-files) without any problems or there are some problems in such way of using global variables?

UPDATE: function localtime_r() does not exist in HT-PICC18.

[This message has been edited by Sanchez (edited February 17, 2006).]