Pumpkin, Inc.

Pumpkin User Forums

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

For issues specific to Microchip's PICmicro® MCUs, including compilers (e.g. HI-TECH PICC & PICC-18, Microchip MPLAB®-C18) and IDEs (e.g. Microchip MPLAB®).

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

Postby Sanchez » Thu Feb 16, 2006 3:15 am

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?

Sanchez
 
Posts: 3
Joined: Thu Feb 16, 2006 12:00 am

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

Postby zoomcityzoom » Thu Feb 16, 2006 3:47 am

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.

Tom Deutschman
Wizbang Designs, Inc.
zoomcityzoom
 
Posts: 13
Joined: Sat Dec 24, 2005 12:00 am
Location: Spokane, WA, USA

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

Postby aek » Thu Feb 16, 2006 6:22 am

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.

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

-------
aek
aek
 
Posts: 1888
Joined: Sat Aug 26, 2000 11:00 pm

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

Postby aek » Fri Feb 17, 2006 4:38 am

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.

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

-------
aek
aek
 
Posts: 1888
Joined: Sat Aug 26, 2000 11:00 pm

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

Postby Sanchez » Fri Feb 17, 2006 5:08 am

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.
Sanchez
 
Posts: 3
Joined: Thu Feb 16, 2006 12:00 am

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

Postby aek » Fri Feb 17, 2006 7:54 am

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.

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

-------
aek
aek
 
Posts: 1888
Joined: Sat Aug 26, 2000 11:00 pm

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

Postby Sanchez » Fri Feb 17, 2006 12:08 pm

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).]

Sanchez
 
Posts: 3
Joined: Thu Feb 16, 2006 12:00 am


Return to PICmicro MCUs

Who is online

Users browsing this forum: No registered users and 0 guests

cron