Strange behavior of mailboxes

Discussions and support about ChibiOS/RT, the free embedded RTOS.
Alexte
Posts: 27
Joined: Sun Jul 01, 2012 1:24 pm

Strange behavior of mailboxes

Postby Alexte » Fri Apr 20, 2018 4:45 pm

simple example, compiles without errors and warnings
chibios 16.1.9

Code: Select all

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

#define MODULE_ID 222
#define CAN_BUFFER_SIZE 32

mailbox_t can_send_mailbox;
msg_t can_send_buffer[CAN_BUFFER_SIZE];

static const CANConfig cancfg = {CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
                                 CAN_BTR_SJW(1) | CAN_BTR_TS2(1)
                                     | CAN_BTR_TS1(6) | CAN_BTR_BRP(179)};

/*
 * Receiver thread.
 */
static THD_WORKING_AREA(can_rx_wa, 256);
static THD_FUNCTION(can_rx, p) {
  event_listener_t el;
  CANRxFrame rxmsg;

  (void)p;
  chRegSetThreadName("receiver");
  chEvtRegister(&CAND1.rxfull_event, &el, 0);
  while (!chThdShouldTerminateX()) {
    if (chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(100)) == 0)
      continue;
    while (canReceive(&CAND1, CAN_ANY_MAILBOX, &rxmsg, TIME_IMMEDIATE) == MSG_OK ) {
      palTogglePad(IOPORT3, GPIOC_LED);
    }
  }
  chEvtUnregister(&CAND1.rxfull_event, &el);
}

/*
 * Transmitter thread.
 */
static THD_WORKING_AREA(can_tx_wa, 256);
static THD_FUNCTION(can_tx, p) {
  CANTxFrame txmsg;
  uint8_t *pbuf; // буфер сообщений

  (void)p;
  chRegSetThreadName("transmitter");
  txmsg.IDE = CAN_IDE_EXT;
  txmsg.EID = 0x01234567;
  txmsg.RTR = CAN_RTR_DATA;
  txmsg.DLC = 8;
  txmsg.data32[0] = 0x55AA55AA;
  txmsg.data32[1] = 0x00FF00FF;

  while (!chThdShouldTerminateX()) {
    // вот здесь тред останавливается и ждет пока не появятся в майлбоксе сообщения
    chMBFetch(&can_send_mailbox, (msg_t *)&pbuf, TIME_INFINITE);
    txmsg.EID = (uint32_t)(1 << 24 | 2 << 16 | 0 << 8 | pbuf[0]);
    canTransmit(&CAND1, CAN_ANY_MAILBOX, &txmsg, MS2ST(100));
    chThdSleepMilliseconds(30);  // calculate = 1 / can_speed * 600
  }
}

int main(void) {

  halInit();
  chSysInit();

  // create CAN send mailbox
  chMBObjectInit(&can_send_mailbox, can_send_buffer, CAN_BUFFER_SIZE);

  canStart(&CAND1, &cancfg);

  chThdCreateStatic(can_rx_wa, sizeof(can_rx_wa), NORMALPRIO + 7, can_rx, NULL);
  chThdCreateStatic(can_tx_wa, sizeof(can_tx_wa), NORMALPRIO + 7, can_tx, NULL);

  uint8_t buff[1];
  uint8_t i = 0;
  while (true) {
    if(i > 99) i = 0;
    buff[0] = i;
    chMBPost(&can_send_mailbox, (msg_t)buff, TIME_INFINITE);
    i++;
    palTogglePad(IOPORT3, GPIOC_LED);
  }
  return 0;
}


Generate a message on the CAN bus.
The first message passes, the remaining 32 disappear in the mailbox.
The number of missing messages is equal to the size of the mailbox buffer (32)

Can log:
02064.0.0041 | 170.085.170.085.255.000.255.000 17:59:21.945
02064.0.0040 | 170.085.170.085.255.000.255.000 17:59:21.915
02064.0.0039 | 170.085.170.085.255.000.255.000 17:59:21.883
02064.0.0038 | 170.085.170.085.255.000.255.000 17:59:21.853
02064.0.0037 | 170.085.170.085.255.000.255.000 17:59:21.824
02064.0.0036 | 170.085.170.085.255.000.255.000 17:59:21.793
02064.0.0035 | 170.085.170.085.255.000.255.000 17:59:21.765
02064.0.0034 | 170.085.170.085.255.000.255.000 17:59:21.734
02064.0.0033 | 170.085.170.085.255.000.255.000 17:59:21.704
02064.0.0000 | 170.085.170.085.255.000.255.000 17:59:21.677

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

Re: Strange behavior of mailboxes

Postby Giovanni » Fri Apr 20, 2018 5:38 pm

I believe the problem is that you are always sending the same value through the mailbox: the same pointer to buf. Try passing directly the message as msg_t, there is no need to pass a pointer.

Giovanni

Alexte
Posts: 27
Joined: Sun Jul 01, 2012 1:24 pm

Re: Strange behavior of mailboxes

Postby Alexte » Fri Apr 20, 2018 8:02 pm

Thanks, I tried your advice, it did not help.

The problem disappears only if you remove the delay of 30 ms after sending CAN messages. Then messages from the mailbox are not lost.
But the delay is needed not to overload the CANbus

canTransmit(&CAND1, CAN_ANY_MAILBOX, &txmsg, MS2ST(100));
chThdSleepMilliseconds(30);

Alexte
Posts: 27
Joined: Sun Jul 01, 2012 1:24 pm

Re: Strange behavior of mailboxes

Postby Alexte » Mon Apr 23, 2018 6:36 am

Everything works on a different board.
Apparently, there was a hardware error.
For a long time I've been using the system and mailboxes, I've never had any problems before.
Giovanni, thanks for the excellent OS!

Alexte
Posts: 27
Joined: Sun Jul 01, 2012 1:24 pm

Re: Strange behavior of mailboxes

Postby Alexte » Mon Apr 23, 2018 8:48 am

Problem still exists. If we pass a variable to the mailbox, then everything is fine.

c1.png

Debug terminal

Code: Select all

post -> 0
fetch ->   0
post -> 1
post -> 2
post -> 3
fetch ->   1
post -> 4
post -> 5
post -> 6
fetch ->   2
post -> 7
post -> 8
post -> 9
fetch ->   3
post -> 10
post -> 11
post -> 12
fetch ->   4
post -> 13
post -> 14
post -> 15
fetch ->   5
post -> 16
post -> 17
post -> 18
fetch ->   6
post -> 19
post -> 20
post -> 21
fetch ->   7
post -> 22
post -> 23
post -> 24
fetch ->   8
post -> 25
post -> 26
post -> 27
fetch ->   9


If we transfer the array to a mailbox, then messages disappear.

c2.png

Code: Select all

post -> 0
fetch ->   0
post -> 1
post -> 2
post -> 3
fetch ->   3
post -> 4
post -> 5
post -> 6
fetch ->   6
post -> 7
post -> 8
post -> 9
fetch ->   9
post -> 10
post -> 11
post -> 12
fetch ->   12
post -> 13
post -> 14
post -> 15
fetch ->   15
post -> 16
post -> 17
post -> 18
fetch ->   18
post -> 19
post -> 20
post -> 21
fetch ->   21
post -> 22
post -> 23
post -> 24
fetch ->   24
post -> 25
post -> 26
post -> 27
fetch ->   27
post -> 28
post -> 29
post -> 30
fetch ->   30


If in the second example, remove the delay in the thread
chThdSleepMilliseconds (300);
Then everything works correctly

Code: Select all

post -> 0
fetch ->   0
post -> 1
fetch ->   1
post -> 2
fetch ->   2
post -> 3
fetch ->   3
post -> 4
fetch ->   4
post -> 5
fetch ->   5
post -> 6
fetch ->   6
post -> 7
fetch ->   7
post -> 8
fetch ->   8
post -> 9
fetch ->   9
post -> 10
fetch ->   10
post -> 11
fetch ->   11
post -> 12
fetch ->   12
post -> 13
fetch ->   13
post -> 14

faisal
Posts: 121
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 16 times
Been thanked: 14 times

Re: Strange behavior of mailboxes

Postby faisal » Mon Apr 23, 2018 1:42 pm

Can you try increasing the relative priority of the transmitter or receiver thread?

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

Re: Strange behavior of mailboxes

Postby Giovanni » Mon Apr 23, 2018 1:58 pm

Relative priority should not matter, I suggest removing all sleeps except where you do chMBPost(), the threads would wait anyway, no need to add sleep.

Giovanni


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: No registered users and 2 guests