chTimeNow() does not seem to be working

ChibiOS public support forum for topics related to the Atmel AVR family of micro-controllers.

Moderator: tfAteba

Post Reply
Thiapalm
Posts: 8
Joined: Mon Sep 08, 2014 3:48 pm

chTimeNow() does not seem to be working

Post by Thiapalm »

Hello,

I have a 4 thread program, where the "main" thread waits for a given time before starting actions on the other 3. However, only the "main" thread keeps running.


This is the thread (just the basics):

static WORKING_AREA(waThread3, 64);

static msg_t mainP(void *arg) {

systime_t time = chTimeNow();

while (1) {

#ifdef DEBUG
nss.println(chTimeNow());
nss.println(time);
nss.println(MS2ST(1000));
#endif

if (chTimeNow()>time+MS2ST(1000)){
// code
}

chThdYield();

}

}


I've made a trace and that's what I've got from teh DEBUG println's above:

5
5
976

Those values are always the same, they never change and keeps repeating.

Can someone help me?
utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: chTimeNow() does not seem to be working

Post by utzig »

What version are you running (or where have you got the sources)?
If you're running version is 3.x are you using tickless?

Fabio Utzig
utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: chTimeNow() does not seem to be working

Post by utzig »

Well, since chTimeNow was renamed on 3.x, of course you must be running 2.6... sorry!

I don't see why chTimeNow would not increment on the code you provided. What tick frequency are you using? Any chance of posting the whole file?
Thiapalm
Posts: 8
Joined: Mon Sep 08, 2014 3:48 pm

Re: chTimeNow() does not seem to be working

Post by Thiapalm »

Down below you can see the SETUP and MAIN, as for the Tick frequency, I have not changed anything on the default values, the AVR is running on 16Mhz.

There are some constants and global variables defined not shown here, but none of then related to OS or chTimeNow.

void mainThread() {

#ifdef DEBUG
nss.println(F("=====CLEAR POOL===== "));
#endif
// fill pool with mailPool array
for (int i = 0; i < MB_COUNT; i++) {
chPoolFree(&mailPool, &Mail[i]);
}


// start sensor thread
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO+2, Sensor, NULL);

// start LED thread
chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO+1, LedControl, NULL);

// start MAINP thread
chThdCreateStatic(waThread3, sizeof(waThread3), NORMALPRIO+1, mainP, NULL);

// start COMM thread
chThdCreateStatic(waThread4, sizeof(waThread4), NORMALPRIO, Communication, NULL);



}
//------------------------------------------------------------------------------
void setup() {

#ifdef DEBUG
nss.begin(COMMBAUD); //DEBUG ONLY
delay(2000);
nss.println(F("=====SETUP====="));
#endif


chBegin(mainThread);
// chBegin never returns, main thread continues with mainThread()
while(1) {}
}

//------------------------------------------------------------------------------
void loop() {
// not used
}
utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: chTimeNow() does not seem to be working

Post by utzig »

Btw, I see you're using the SoftwareSerial library. I guess you're running the ChibiOS Library for Arduino (not native ChibiOS). Is that right?
Which board are you using?
Thiapalm
Posts: 8
Joined: Mon Sep 08, 2014 3:48 pm

Re: chTimeNow() does not seem to be working

Post by Thiapalm »

Yes, I'm using the arduino version, but the board is of my design, it is based on Atmega328P-AN.

I am programming it via ICSP and de Serial is connected to a communication device. Since I don't have a debugger, I need to use the softserial to perform such verification.

I have checked the RAM (just in case) and it seems to be fine (free space available):

text data bss
18214 346 1241

The strange thing is that I disabled thread4 (communication) and now the timer is working as it should.

This is thread4:

static WORKING_AREA(waThread4, 128);

static msg_t Communication(void *arg) {

Serial.begin(57600);
XbeeHLD comm = XbeeHLD();
comm.begin(57600,Serial,COMMRST_PIN);

Mail_t *r;

while (1) {
#ifdef DEBUG
nss.print(F("=4="));
#endif
// CHECK FOR MAIL FROM MAINP
msg_t received = chMBFetch(&tocomm, (msg_t*)&r, TIME_IMMEDIATE);
if (received == RDY_OK) { //Message Received

// CODE

chPoolFree(&mailPool, r);
}
chThdYield();
}
return 0;
}

The memory after removing it:

text data bss
14322 304 1013

I see nothing on the thread that could interfere with the timer, could it be a memory issue?
Thiapalm
Posts: 8
Joined: Mon Sep 08, 2014 3:48 pm

Re: chTimeNow() does not seem to be working

Post by Thiapalm »

Now I have re-enabled thread4, but removed all things related to the hardwareserial (UART) and added some strings to keep RAM usage at the same level.

The chTimeNow() on this scenario is working as it should. So it seems to me that somehow, the HardwareSerial from Arduino is interfering with the timer....
utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: chTimeNow() does not seem to be working

Post by utzig »

I see no problem in the code snippets you posted so far. The ChibiOS library uses Timer0 for scheduling so one thing which could have effect could be if some library you are using modified the Timer0 programming or disabled it. HardwareSerial only uses the serial interrupts. That XBee library I have no idea...
Thiapalm
Posts: 8
Joined: Mon Sep 08, 2014 3:48 pm

Re: chTimeNow() does not seem to be working

Post by Thiapalm »

Thanks for the help Utzig.

The Xbee library uses the HardwareSerial for communication, I have not written the lib, but as far as I can see, it has no interaction with Timer0 whatsoever.

...the softwareserial on the other hand, is using PD4 as RX and PD5 as TX pins, from the datasheet PD4 and PD5 are Timer/Counter0 related...

I have just tried inverting the setup (PD4 as TX and PD5 as RX) and now the timer is running fine !

Funny thing is that I had to add the hardwareSerial for this problem to appear....

Thank you once more.
Post Reply