Mailbox - How to pass a pointer correct

Discussions and support about ChibiOS/RT, the free embedded RTOS.
Methanbombe
Posts: 8
Joined: Sat Mar 26, 2016 9:07 pm
Location: Munich, Germany

Mailbox - How to pass a pointer correct

Postby Methanbombe » Wed Mar 30, 2016 11:50 am

Hello again,

Still having trouble with my own incompetence.

So I want to pass data from one thread to another. There for I have declared a Mailbox and initialized it.

Code: Select all

 
  /*
   * Initialize Mailbox
   */
  mailbox_t Output_Queue;
  msg_t Output_buf[128];
  chMBObjectInit(&(Output_Queue), Output_buf, 128);


The data is stored in a stuct, which i allocate from a MemPool. The MemPool should also be correct initialized:

Code: Select all

  memory_pool_t Output_Msg_Pool;
  OutputMessage* Pool_Buffer[128] __attribute__((aligned(sizeof(stkalign_t))));
  /*
   * Initialize Memory Pool for Output-Message Objects and add all Buffer-Elements
   * to the Memory Pool
   */
  chPoolObjectInit(&(Output_Msg_Pool), sizeof(OutputMessage), NULL);
  for(i = 0; i < 128; i++)
    chPoolFree(&(Output_Msg_Pool), &(Pool_Buffer[i]));


The struct now is passed as Pointer through the Mailbox to the receiving thread:

Code: Select all

  OutputMessage *om;
  om = (OutputMessage*)chPoolAlloc(&(Output_Msg_Pool));
  //Initialize Data.
  chMBPost(&(VCOM.Output_Queue), (msg_t)om, TIME_IMMEDIATE);


However, when I now want to get the Pointer out, I dont get the right Value....

Code: Select all

  OutputMessage *current;
  chMBFetch(&(Output_Queue), (msg_t*)current, TIME_IMMEDIATE);


The value of *current isn't the right address of the struct, which I wanted to pass.

I'm pretty sure that the mechanism should work this way somehow, but I'm apparently not bright enough to figure out by myself, how to correctly pass this struct.
Does anyone know what I'm doing wrong?

I've already tried different ways of retrieving the Mailbox-Payload:

Code: Select all

  OutputMessage current;
  chMBFetch(&(VCOM.Output_Queue), (msg_t*)&current, TIME_IMMEDIATE);


doesn't work either.

I hope someone can help me with this.
All answers are appreciated.

Methanbombe

Methanbombe
Posts: 8
Joined: Sat Mar 26, 2016 9:07 pm
Location: Munich, Germany

Re: Mailbox - How to pass a pointer correct

Postby Methanbombe » Wed Mar 30, 2016 12:06 pm

Also here is the code, if it helps.
Attachments
Why_Doesnt_It_Work.tar.gz
(5.3 KiB) Downloaded 267 times

flabbergast
Posts: 71
Joined: Sat Aug 22, 2015 1:22 pm

Re: Mailbox - How to pass a pointer correct

Postby flabbergast » Wed Mar 30, 2016 1:01 pm

I think it should be

Code: Select all

  OutputMessage *current;
  chMBFetch(&(VCOM.Output_Queue), (msg_t*)&current, TIME_IMMEDIATE);

The second parameter to chMBFetch should a pointer to the thing you want to receive. You want to receive a pointer.

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Mailbox - How to pass a pointer correct

Postby Giovanni » Wed Mar 30, 2016 1:21 pm

Correct, the message parameter must be a pointer. It is guaranteed that msg_t is large enough to contain a pointer.

Code: Select all

  OutputMessage current;
  chMBFetch(&(VCOM.Output_Queue), (msg_t*)&current, TIME_IMMEDIATE);


This is right assuming TIME_IMMEDIATE is what you want. You need to check the returned value if so.

Giovanni

Methanbombe
Posts: 8
Joined: Sat Mar 26, 2016 9:07 pm
Location: Munich, Germany

Re: Mailbox - How to pass a pointer correct

Postby Methanbombe » Wed Mar 30, 2016 4:59 pm

flabbergast wrote:The second parameter to chMBFetch should a pointer to the thing you want to receive. You want to receive a pointer.


Giovanni wrote:Correct, the message parameter must be a pointer. It is guaranteed that msg_t is large enough to contain a pointer.
Giovanni



Yes but my actual problem is, that the pointer, when I recieve it, does not correctly point to my Message-struct.
Pointer.png


You can see in the first Picture, that the Pointer to the OutputMessage *om = 0x20001624.

That does mean the Message Object is at this address, right?

Now in the second Picture, the pointer is fetched from the Mailbox. the value of *msgp is again 0x20001624, which should be the adress of the Message Object.
Also I think the Mailbox works fine (rdymsg = 0 /* MSG_OK */).

In the third Picture you can now see my problem:

Even though I think I have the right address of my OutputMessage the content isn't correct.
Compare Picture 1:
om->message = 0x08007370

Picture 3:
current->message = 0x20000808


I am sure I am doing sth essential wrong, but I have no idea what.

flabbergast
Posts: 71
Joined: Sat Aug 22, 2015 1:22 pm

Re: Mailbox - How to pass a pointer correct

Postby flabbergast » Wed Mar 30, 2016 5:58 pm

So the mailbox mechanism is working fine - you get out exactly what you post in.

BTW your pool code looks a bit fishy to me: Pool_Buffer contains 128 "pointers to OutputMessage" (sizeof each of the 128 things is the size of the pointer). But in the chPoolObjectInit call you declare that each item in the pool has sizeof(OutputMessage). Or am I reading this wrong?

Methanbombe
Posts: 8
Joined: Sat Mar 26, 2016 9:07 pm
Location: Munich, Germany

Re: Mailbox - How to pass a pointer correct

Postby Methanbombe » Wed Mar 30, 2016 6:07 pm

flabbergast wrote:BTW your pool code looks a bit fishy to me: Pool_Buffer contains 128 "pointers to OutputMessage" (sizeof each of the 128 things is the size of the pointer). But in the chPoolObjectInit call you declare that each item in the pool has sizeof(OutputMessage)


I know that the code is not aesthetic :/ But this is my first real MC project; I am open for improvement suggestions.

About the Pool Initialization, how should it be done correct?

I want to have a Pool of 128 OutputMessage structs.

Methanbombe
Posts: 8
Joined: Sat Mar 26, 2016 9:07 pm
Location: Munich, Germany

Re: Mailbox - How to pass a pointer correct

Postby Methanbombe » Wed Mar 30, 2016 6:28 pm

Could this be the problem:

Code: Select all

OutputMessage* Pool_Buffer[OUTPUT_BUFFER_LEN] __attribute__((aligned(sizeof(stkalign_t))));


Because I only have an array of Pointers to OutputMessages?

But when I try:

Code: Select all

OutputMessage Pool_Buffer[OUTPUT_BUFFER_LEN] __attribute__((aligned(sizeof(stkalign_t))));

which should be an array of OutputMessages i get a compiler Error: array type has incomplete element type

I hate my incompetence :((

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Mailbox - How to pass a pointer correct

Postby Giovanni » Wed Mar 30, 2016 6:39 pm

Hi,

I missed this:

Code: Select all

OutputMessage current;
chMBFetch(&(VCOM.Output_Queue), (msg_t*)&current, TIME_IMMEDIATE);


What you receive from chMbxFetch() is the pointer to your structure so you need to declare:

Code: Select all

OutputMessage *current;


What you pass to chMbxFetch() is a pointer to a pointer to your message.

Giovanni

Methanbombe
Posts: 8
Joined: Sat Mar 26, 2016 9:07 pm
Location: Munich, Germany

Re: Mailbox - How to pass a pointer correct

Postby Methanbombe » Wed Mar 30, 2016 6:45 pm

Giovanni wrote:What you receive from chMbxFetch() is the pointer to your structure so you need to declare:

Code: Select all

OutputMessage *current;



Yes I have already changed it to a Pointer.

flabbergast wrote:Pool_Buffer contains 128 "pointers to OutputMessage" (sizeof each of the 128 things is the size of the pointer). But in the chPoolObjectInit call you declare that each item in the pool has sizeof(OutputMessage).


I am now trying to figure out the correct way to declare an array of OutputMessages.
So this:

Code: Select all

OutputMessage Pool_Buffer[OUTPUT_BUFFER_LEN] __attribute__((aligned(sizeof(stkalign_t))));


is generating a compile Error...


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: No registered users and 2 guests