Can.attach and can.read does not work on MbedOS6

Hello All.
I’m trying to use can.read() when using can.attach() on MbedOS6.
I’ve already known it has the bug that it doesn’t work on ISR context, so I use thread and Event Queue.
This is my code.

#include "mbed.h"

void onReceive();
void CANreceive();

CAN can(PB_5, PB_6, 500000);
Thread t(osPriorityNormal, 2000);
EventQueue queue(32 * EVENTS_EVENT_SIZE);
int main(void)
{
  can.attach(onReceive, CAN::RxIrq);
  t.start(callback(&queue, &EventQueue::dispatch_forever));
  while (1)
  {
    printf("loop\n");
  }

  return 0;
}

void onReceive()
{

  queue.call(CANreceive);
}

void CANreceive()
{
  CANMessage msg;

  if (can.read(msg))
  {
    printf("data: %s,id:%d\n", msg.data, msg.id);
  }
}

However, it doesn’t work. I can see only “loop”. I suspected that CANreceive() has a problem, so I put it on main(). Then, it starts to read messages. Does can.attach() really work? Can anyone teach me solutions?
=environments=
*nucleo-stm32f767zi
*mbedOS version 6.15
*mbedCLI2 version 7.33.0
*GCC_ARM

1 Like

Hello,

I don’t know but spamming a printf in main loop without a wait is bad practise, I think.
I do not know what or who is a sender in your case. I use a CAN loop on one board with 2 interfaces.
So I only copy paste you code, fill a sender and seems to be OK.

  • nucleo-stm32f767zi
  • mbedOS version 6.15
  • KeilStudio IDE
  • AC6

BR, Jan