Page 1 of 1

Messages or something else?

Posted: Sun Jul 08, 2012 3:12 pm
by king2
I have two threads in my app, one thread communicates with PC, another with device.

When I have received some command from PC, I should ask device about it's firmware version and return this to PC.
I think that message will be more suitable for this, unfortunately, I have not found any examples about messages.

May be, I should use something else for this?
If messages, how to do it right?
Which variables use as "tp" in different functions?
chMsgSend return pointer to data from another thread, so I cannot use it safely (data can be modified by responding thread after it answered)?
Which lock has to be used?

Thanks!

Re: Messages or something else?

Posted: Sun Jul 08, 2012 3:29 pm
by Giovanni
You could use a pool of "message objects" of any size and exchange them using messages or mailboxes then return them to the pool when no more needed.

Giovanni

Re: Messages or something else?

Posted: Sun Jul 08, 2012 3:31 pm
by king2
Not simple for me.. :(
So, I have one message to send it to thread, and another message to get answer?

Re: Messages or something else?

Posted: Sun Jul 08, 2012 3:36 pm
by Giovanni
No, messages are always bidirectional, there is a message followed by a response, in the test suite there are several examples of use of messages.

Giovanni

Re: Messages or something else?

Posted: Sun Jul 08, 2012 4:25 pm
by king2
Thanks for pointing to right place!
But I have a question, I have written test code:

Code: Select all

            //chSysLockFromIsr();
            if (chMsgIsPendingI(currp)) {
                tp = chMsgWait();
                message = chMsgGet(tp);
                *(unsigned char *)message = 0x55;
                chMsgRelease(tp, message);
            }
            //chSysUnlockFromIsr();


Should I use chSysLockFromIsr() because I using I-class function?
If I uncommect 'lock' lines, it falls with debug_msg = 'SV#6, misplaced @p chSysLockFromIsr()'.

Re: Messages or something else?

Posted: Sun Jul 08, 2012 4:40 pm
by Giovanni
Messages are not usable in ISRs, I assumed you needed to communicate between 2 threads, if this is the case the use the chSysLock() and chSysUnlock(), why did you use the "FromIsr" variant? "SV#6...." is telling you exactly that.

Note that you need to unlock before calling chMsgWait().

Giovanni

Re: Messages or something else?

Posted: Sun Jul 08, 2012 7:58 pm
by king2
I have used LockFromIsr because 'I' and the end of called function; I think that I should use chSysLock when calling S-functions and LockFromIsr when calling I-functions.
I was not right :)