chEvtBroadcast Issue Topic is solved

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

Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess

hypergolic
Posts: 2
Joined: Sun Dec 04, 2016 5:12 pm
Has thanked: 1 time

chEvtBroadcast Issue

Postby hypergolic » Sun Dec 04, 2016 5:23 pm

With the following code I am trying to put an item in a mailbox and broadcast an event to a thread so it can fetch the item from the mailbox. The program will not run with the chEvtBroadcast function, if I comment it out the program runs fine. Any suggestions? Thanks!

Code: Select all

#include "ch.h"
#include "hal.h"
#include "test.h"

#define NUM_BUFFERS 16

static mailbox_t serial2mb;
static msg_t serial2mb_queue[NUM_BUFFERS];

event_source_t mb_event_source;

static THD_WORKING_AREA(waSerial2Thread, 128);
static THD_FUNCTION(Serial2Thread, arg) {
  event_listener_t mb_listener;

  (void) arg;

  /* Mailbox event */
  chEvtRegisterMask(
        &mb_event_source,
        &mb_listener,
        EVENT_MASK(0));

  while (true) {
    /* Waiting for any of the events we're registered on.*/
    eventmask_t evt = chEvtWaitAny(ALL_EVENTS);

    /* Mailbox event */
    if (evt & EVENT_MASK(0)) {
      msg_t tmp;
      chMBFetch(&serial2mb, &tmp, TIME_INFINITE);
    }
  }
}

/*
 * Application entry point.
 */
int main(void) {

  halInit();
  chSysInit();

  chMBObjectInit(&serial2mb, serial2mb_queue, NUM_BUFFERS);

  /*
   * Activates the serial driver 2 using the driver default configuration.
   */
  sdStart(&SD2, NULL);

  chThdCreateStatic(waSerial2Thread, sizeof(waSerial2Thread), NORMALPRIO, Serial2Thread, NULL);


  chMBPost(&serial2mb, (msg_t)'T', TIME_INFINITE);
  /* This is causing issues */
  chEvtBroadcast(&mb_event_source);

  while (true) {
    palTogglePad(GPIOA, GPIOA_LED_GREEN);
    chThdSleepMilliseconds(500);
  }
}


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

Re: chEvtBroadcast Issue  Topic is solved

Postby Giovanni » Sun Dec 04, 2016 5:43 pm

Hi,

1) The event source object is not initialized.
2) The thread is created at the same priority level, when the broadcast is reached the thread has not yet started and is not yet registered.
3) Post and broadcast are not executed atomically, this could create problems later.
4) If you wait for an event then you should fetch using TIME_IMMEDIATE and consume messages until the mailbox is empty. Events are not counted.

Giovanni

hypergolic
Posts: 2
Joined: Sun Dec 04, 2016 5:12 pm
Has thanked: 1 time

Re: chEvtBroadcast Issue

Postby hypergolic » Sun Dec 04, 2016 11:07 pm

Thanks Giovani!


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 43 guests