problem with frequent call of chIOGetTimeout( )

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: tfAteba, barthess, RoccoMarco, lbednarz, utzig

User avatar
levrik
Posts: 26
Joined: Wed Dec 21, 2011 7:35 am

problem with frequent call of chIOGetTimeout( )

Postby levrik » Mon May 07, 2012 7:51 pm

Hello,
I'm a bit embarrassed asking so many questions. But there another phenomenon bothering me for which i cannot find the answer alone:

I've implemented a serial driver which puts data comming from SPI-based RF-module into an input-queue via chIQPutI() and makes it available via the serialDriver interface:

Code: Select all

static msg_t gett( void *ip, systime_t timeout )
{

  return chIQGetTimeout( &( ( SerialDriver * ) ip )->iqueue, timeout );
}


In my code the following line gets very frequently called:

Code: Select all

uint8_t pocketbus_recv(struct PocketBusCtx *ctx, struct PocketMsg *msg)
{
   int data;
   uint8_t c;


   while( (data = chIOGetTimeout( ctx->fd, MS2ST(50) )) >= Q_OK )
             {

              /* Process incoming characters while buffer is not empty */
              // returns 1 if valid telegram
             // if errors occur (crc-error, length error etc.) the loop continues
            }
             return 0;
}

Calling thread:

Code: Select all

static WORKING_AREA(waThread2, 512);
static msg_t Thread2(void *arg)
{
  (void)arg;
  uint8_t rec;
  chRegSetThreadName("Pocket-RX");

  for(;;)
  {
    rec = pocketbus_recv(&busCtrlRfm, &msg);

    palSetPad(GPIOD, LED_RED);

    if( rec == 1 )
    {

      chprintf((BaseChannel *)&SD2, "payload: %s lenght: %d \n\r", msg.payload, msg.len);
    }

    palClearPad(GPIOD, LED_RED);
    //chThdSleepMilliseconds(200); // uncomment to delay the described phenomenon
  }
  return 1;
}

If the first telegram passes, the function pocketbus_recv returns with 1, but after calling it again it delivers garbage without any real incoming data! The input-queue is empty at this point, but chIOGetTimeout delivers something and doesn't stop. If i pause the program via debugger the input-queue shows a q_counter of zero. After resuming the program, this thread receives one telegram correctly and after that garbage again. If i put a chThdSleepMilliseconds(xxx) into the calling thread the phenomenon takes much longer to appear (10 - 15 min).

Is chIOGetTimeout( ) not meant to be called frequently?
Thanks for hints.

P.S.
Hardware STM32F4-Discovery, ChibiOS latest trunk.

User avatar
Giovanni
Site Admin
Posts: 14704
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1146 times
Been thanked: 960 times

Re: problem with frequent call of chIOGetTimeout( )

Postby Giovanni » Mon May 07, 2012 8:04 pm

Is chIQPutI() invoked within a lock? in general make sure to enable the debug options in chconf.h, the state checker can catch most or this kind of errors. Be careful with I-Class functions.

That "often" also hints to a race condition somewhere :-)

Also, probably you should not use the Serial interface but extend/implement the BaseAsynchronousChannel interface.

Giovanni

User avatar
levrik
Posts: 26
Joined: Wed Dec 21, 2011 7:35 am

Re: problem with frequent call of chIOGetTimeout( )

Postby levrik » Mon May 07, 2012 8:37 pm

Is chIQPutI() invoked within a lock? in general make sure to enable the debug options in chconf.h, the state checker can catch most or this kind of errors. Be careful with I-Class functions.

chIQPutI() is invoked within a locked section, the debug-options are enabled and the state checker does not halt the system.

That "often" also hints to a race condition somewhere

That is my thought also. But due lack of extended ChibiOS experience i do not know where to begin the search.

Also, probably you should not use the Serial interface but extend/implement the BaseAsynchronousChannel interface.

Why do you recommend that?

User avatar
Giovanni
Site Admin
Posts: 14704
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1146 times
Been thanked: 960 times

Re: problem with frequent call of chIOGetTimeout( )

Postby Giovanni » Mon May 07, 2012 9:40 pm

Could you try using -O0 while compiling? Is the result the same?

Giovanni

User avatar
levrik
Posts: 26
Joined: Wed Dec 21, 2011 7:35 am

Re: problem with frequent call of chIOGetTimeout( )

Postby levrik » Tue May 08, 2012 7:10 am

Sorry for not mentioning:
During debugging i use always -O0. So the described phenomenon was already compiled with -O0.

User avatar
Giovanni
Site Admin
Posts: 14704
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1146 times
Been thanked: 960 times

Re: problem with frequent call of chIOGetTimeout( )

Postby Giovanni » Tue May 08, 2012 7:31 am

I should look at the whole code, "remote debug" didn't work. Alternatively you could create a test case that I can run for debug.

Giovanni

User avatar
levrik
Posts: 26
Joined: Wed Dec 21, 2011 7:35 am

Re: problem with frequent call of chIOGetTimeout( )

Postby levrik » Tue May 08, 2012 8:58 am

I've attached the project (without ChibiOS).

Thank you very much for your help.

User avatar
Giovanni
Site Admin
Posts: 14704
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1146 times
Been thanked: 960 times

Re: problem with frequent call of chIOGetTimeout( )

Postby Giovanni » Fri May 11, 2012 10:45 am

Hi,

The only problems I found so far: In prvRFM12XChangeI() a normal API is called inside a lock (Iclass functions can only call Iclass). Not a problem except that you do polling with interrupts disabled from an ISR.

I don't have an explanation yet for the apparent queue corruption.

Giovanni

User avatar
levrik
Posts: 26
Joined: Wed Dec 21, 2011 7:35 am

Re: problem with frequent call of chIOGetTimeout( )

Postby levrik » Mon May 14, 2012 6:59 am

Hi,
i've managed to solve the issue: The reason for this phenomenon was defective hardware. The RF-Module was continously throwing rx-interrupts even if no data was incomming via RF and the MCU put the delivered garbage into the queue. Due lack of Oscilloscope it took some time to debug this by LED-output. Sorry for the noise and thanks for your help.

mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Location: Karlsruhe, Germany
Been thanked: 1 time

Re: problem with frequent call of chIOGetTimeout( )

Postby mabl » Mon May 14, 2012 7:59 am

levrik wrote:Due lack of Oscilloscope it took some time to debug this by LED-output.

You know that Logic Analyzer are super cheap these days? The Open Workbench Logic Sniffer is only 50$ and there are also other options: viewtopic.php?f=8&t=228
:D :mrgreen:


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 105 guests