Page 1 of 1

Strange behavior of chMsgSend()

Posted: Wed Apr 01, 2015 1:09 am
by Chudik
I'm using sending message to one thread from two places of my project. From one function everything is fine, but similar set of commands in the other function does not work :o

The structure of the message is like this:

Code: Select all

 typedef struct
 {
    uint8_t length;
    uint8_t tag;
    uint8_t *msg;
    DisplayFont *font;
 } ui_msg;


in header file
menu.h

Code: Select all

// Global variables
  extern Thread *thMenu;
  extern ui_msg menu_message;


In menu.c

Code: Select all

Thread *thMenu;
ui_msg menu_message;
....

static WORKING_AREA (waMenu, UI_WA_SIZE);
static msg_t MenuServer(void *arg);
...

void MenuInit(void)
{
    thMenu = chThdCreateStatic(waMenu, sizeof(waMenu), NORMALPRIO, MenuServer, NULL);   // NULL here should be changed to a stream later
 ...
}

static msg_t MenuServer(void *arg)      // "Server" where message should com
{
      Thread *tpmenu;
   ui_msg *message;

   uint8_t button, displaystate;
   uint8_t data[4];


    (void)arg;
    menuitem *CurrentMenu, *item;

    chRegSetThreadName("Menu");
 ....
    CurrentMenu = MainScreen;
    ShowMenu(CurrentMenu, &CurrentReading, FULLMENU);

    while (true)
    {
         /* Waiting for a queued message then retrieving it.*/
   tpmenu = chMsgWait();
   message = (ui_msg *)chMsgGet(tpmenu);
        chMsgRelease(tpmenu, MSG_OK);

        switch(message->tag)                 // <---- Set breakpoint here
        {
            case KEYBOARD:  button = *(message->msg);
                            break;
            case MENUCHANGE: CurrentMenu = (menuitem *)(message->msg);
                             break;
            default: break;
        }

        if((message->tag) == KEYBOARD)
        { ...       }   // KEYBOARD

    }

    return 0;
}

static void RunCalibrationMenu(void *arg)
{
    (void)arg;

    chVTReset(&MenuTimer);                                     // reset timeout timer for the button
    ShowMenu(CalibrationMenu, &CurrentReading, FULLMENU);

    chSysLock();
    screenstate = CALIBMENU;
    menu_message.length = 1;                                  // fill the global menu_message with data
    menu_message.tag = MENUCHANGE;
    menu_message.msg = (uint8_t *)CalibrationMenu;
    chSysUnlock();

    chMsgSend(thMenu,(msg_t)&menu_message);    // <--- Does not send the message?!!!
}


in ui_driver.c

Code: Select all

#include "menu.h"    // for having  access to the menu_message 
...

static msg_t UI_EventsHandler(void *arg)     // VT and keyboards interrupts events handler
 {
...
    uint8_t evdata[16], evindata;

     tUI_EventsHandler = chThdSelf();
     while(true)
     {
         inevent = chEvtWaitAny((eventmask_t)(DISPLAY_VT_REQUEST | KEYB_RD_REQUEST));
         if(inevent & DISPLAY_VT_REQUEST)
         {  ....    }

         if(inevent & KEYB_RD_REQUEST)
         {
            ....
             chSysLock();
             menu_message.tag = KEYBOARD;     // fill the global menu_message with required data
             menu_message.msg = &evindata;    //
             chSysUnlock();

             chMsgSend(thMenu,(msg_t)&menu_message);   // <--- Sending message works just fine!!!
         }
     }

    return 0;
 }


So, why the similar procedure of filling structure with data and sending message to the same thread works fine in one case and does not in another? Debugging stops at the shown breakpoint when UI_EventsHandler() sends the messages and does not when RunCalibrationMenu() sends it.
Can it happen because RunCalibrationMenu() is a callback?

Re: Strange behavior of chMsgSend()

Posted: Wed Apr 01, 2015 2:38 am
by Chudik
I have found a solution without really understanding it yet.
Now my callback sends a signal by using chEvtSignal() to the Menu_EventHandler() that previously took care about VT only and this handler does the other job that previously done in the callback.

Actually it makes one extra step/call.

Hopefully Giovanni can explain this behavior.

Re: Strange behavior of chMsgSend()

Posted: Wed Apr 01, 2015 8:13 am
by Giovanni
Probably your problem is that menu_message is a static variable shared by two threads.

Giovanni

Re: Strange behavior of chMsgSend()

Posted: Wed Apr 01, 2015 8:26 am
by Chudik
menu_message is global, but both ui and menu system use them in different time.
Do you think I need to use two of them - one for communication between UI and menu and another one only for the menu system?

BTW, what else thread can wait from another modules in the project? So far I used events (for interrupts handling) and messages, but number of messages types defined by 32 bit mask. What should I use if I need more then 32 different types of messages? mailboxes?

Re: Strange behavior of chMsgSend()

Posted: Wed Apr 01, 2015 3:14 pm
by Giovanni
Messages are usually pointers to message structures, into the structure you can put the data you want without size restrictions.

Giovanni

Re: Strange behavior of chMsgSend()

Posted: Wed Apr 01, 2015 3:47 pm
by Chudik
Giovanni,
Yes, that I've understood and I'm using it in my type ui_msg type.

I wonder what kind of restrictions exist for the callbacks? Why could not I send a message from it? Good that I already had a function around, that handled my menus events, but this is strange for me. And it happened on my way back/up to the main menu. When I was going down to the second level it was straight forwards without any crutches.

Re: Strange behavior of chMsgSend()

Posted: Wed Apr 01, 2015 4:08 pm
by Giovanni
Messages are a mechanism for threads not ISRs. You cannot wait into an ISR so chMsgSend() cannot be used because it waits for a response.

Giovanni

Re: Strange behavior of chMsgSend()

Posted: Wed Apr 01, 2015 4:21 pm
by Chudik
Yes, if callback used for the ISR - I understand that. However, I'm talking about my own structure that uses pointer to a function, which I also call callback (just for not having new terms :)).

Just for reference, here is what I use

Code: Select all

/**
 * @brief   Menu callback function.
 */
typedef void (*mnfunc_t)(void *arg);

/**
 * @brief   Menu Item structure type.
 */
typedef struct
{ void *prev;
  void *next;
  char *name;
  uint8_t length;
  uint8_t x;
  uint8_t y;
  DisplayFont *font;
  uint8_t opt;
  mnfunc_t  mncb;
} menuitem;


Is it also treated as an ISR? :o

Re: Strange behavior of chMsgSend()

Posted: Wed Apr 01, 2015 5:57 pm
by Giovanni
You should ask to the author.

ChibiOS-originated callbacks are always ISRs.

Giovanni

Re: Strange behavior of chMsgSend()

Posted: Wed Apr 01, 2015 10:18 pm
by Chudik
Giovanni,
Hmm, ain't you the author? :)

Anyway, with your explanation I got the idea for the callbacks restriction. They are treated as ISR callbacks even if they are not. However, they can't use chSysLockFromIsr()/chSysUnlockFromIsr() pair - only chSysLock()/chSysUnlock(). It's been tested by me just now.

Now I made some changes in my procedures: all my menus changes are going to use only one bit in event mask and all preparations for that will be done in the callbacks.

Thanks!

And just in case if anybody will get the same problem I put here the final version of this part of my code

Code: Select all

static msg_t Menu_EventsHandler(void *arg)
{
     eventmask_t inevent;

    (void)arg;

    uint8_t evdata[16], evindata;

     tMenu_EventsHandler = chThdSelf();
     while(true)
     {
         inevent = chEvtWaitAny((eventmask_t)(MENU_CLR_CAL_REQUEST | MENU_CHANGE_REQUEST));
         if(inevent & MENU_CLR_CAL_REQUEST)
         {
             ... // not related to this topic
         }

         if(inevent & MENU_CHANGE_REQUEST)
         {
             chMsgSend(thMenu,(msg_t)&menu_message);    // menu_message filled early
         }

     }
}

static void RunCalibrationMenu(void *arg)
{
    (void)arg;

    chVTReset(&MenuTimer);

    chSysLock();
    ....                // prepare menu_message in global variable
    chSysUnlock();

    chEvtSignal(tMenu_EventsHandler, (eventmask_t)MENU_CHANGE_REQUEST);

}


static void CalibrationBack(void *arg)
{
    (void)arg;

    chSysLock();
    ....                // prepare menu_message in global variable   
    chSysUnlock();

    chEvtSignal(tMenu_EventsHandler, (eventmask_t)MENU_CHANGE_REQUEST);
}