Page 2 of 2

Re: Mailboxes and queues examples?

Posted: Mon Aug 19, 2019 11:04 am
by sntnjrg
Hi,

Following the IRQ_STORM example, I have written the following code:

Code: Select all

#define MAILBOX_MSG_SIZE                    64

static mailbox_t mb;
static msg_t mb_msg[MAILBOX_MSG_SIZE];

static THD_WORKING_AREA(waThdBlinker, 128);
static THD_FUNCTION(ThdBlinker, arg) {
  (void)arg;
  chRegSetThreadName("Blinker");

  static msg_t msgp[MAILBOX_MSG_SIZE];
  while (true) {
    switch (chMBFetchTimeout(&mb, msgp, TIME_INFINITE)) {
    case MSG_OK :
      palSetLine(LINE_LED_BLUE);
      break;
    case MSG_RESET :
    case MSG_TIMEOUT :
      palSetLine(LINE_LED_RED);
      break;
    }
  }
}

int main(void) {
  halInit();
  chSysInit();

  static msg_t result;
  static uint8_t counter = 0;
  static uint8_t info[5][MAILBOX_MSG_SIZE] = {"Lorem ipsum dolor sit amet, ",
                                              "consectetur adipiscing elit. ",
                                              "Nam eu mollis velit. ",
                                              "Nulla facilisi. ",
                                              "Integer ut viverra leo."};

  chMBObjectInit(&mb, mb_msg, MAILBOX_MSG_SIZE);

  chThdCreateStatic(waThdBlinker, sizeof(waThdBlinker), NORMALPRIO + 1,
                    ThdBlinker, NULL);

  while (true) {
    if (counter < 5) {
      if (chMBPostTimeout(&mb, (msg_t)info[counter], TIME_IMMEDIATE) != MSG_OK) {
        palSetLine(LINE_LED_RED);
      }
      counter++;
    }
    else {
      counter = 0;
    }
    palToggleLine(LINE_LED_GREEN);
    chThdSleepMilliseconds(200);
  } 
}

When it is executed, an exception is generated.

Code: Select all

Vector3FC:
#endif
        bl          _unhandled_exception


When I debugged the program, I verified that after creating the thread the chMBFetchTimeout() function returns the MSG_TIMEOUT value. Then, after sending three messages with the chMBPostTimeout() function, the chMBFetchTimeout() function gets the MSG_RESET value but never receives the MSG_OK value.

What am I doing wrong?

EDIT: The buffer that I used with chMBFetchTimeout() function was not defined. However, it still doesn't work.

Thank you.

Re: Mailboxes and queues examples?

Posted: Mon Aug 19, 2019 12:37 pm
by Giovanni
Hi,

1) You are getting a stack overflow in the thread.
2) The mailbox exchanges a single msg_t, not buffers.

Giovanni

Re: Mailboxes and queues examples?

Posted: Mon Aug 19, 2019 12:58 pm
by sntnjrg
Thank you Giovanni.

How can I send a buffer through mailbox?

Cheers.

Re: Mailboxes and queues examples?

Posted: Mon Aug 19, 2019 1:09 pm
by Giovanni
Use "object FIFO", those exchange fixed-size buffers kept into a memory pool. It uses a mailbox internally so you can look at the code.

Giovanni

Re: Mailboxes and queues examples?

Posted: Mon Aug 19, 2019 2:43 pm
by sntnjrg
Hi Giovanni,

To initialize a FIFO object, I have to create a buffer to store all the elements of the fifo and another for the mailbox, right?

The code will be as follows:

Code: Select all

#define FIFO_OBJ_SIZE                       64
#define FIFO_OBJ_AVAILABLE                  6

static objects_fifo_t fifo_buffer;
static char data_buffer[FIFO_OBJ_AVAILABLE][FIFO_OBJ_SIZE];
static msg_t msg_buffer[FIFO_OBJ_SIZE];

static void init_fifo_object(void) {
   chFifoObjectInit( &fifo_buffer, FIFO_OBJ_SIZE, FIFO_OBJ_AVAILABLE, data_buffer, msg_buffer);
}

Cheers.

Re: Mailboxes and queues examples?

Posted: Mon Aug 19, 2019 4:22 pm
by Giovanni
Correct, make it two arrays of the same size, usually the exchanged data are structures/unions.

Internally this object includes a mailbox and a pool, so it is also an example for those.

Giovanni

Re: Mailboxes and queues examples?

Posted: Tue Aug 20, 2019 9:32 am
by sntnjrg
Thank you Giovanni.

I have an example using a memory pool with mailbox and another using a FIFO object.

Based on your experience, What kind of solution would you recommend? a FIFO object directly or memory pool with mailbox?

Cheers.

Re: Mailboxes and queues examples?

Posted: Tue Aug 20, 2019 9:35 am
by Giovanni
It is the same, the FIFO IS a pool with a mailbox.

All things in OSLIB are other things aggregated in some way, the point is to not reinvent the wheel each time.

Giovanni