Write() returns -1 on nuttx CAN bus driver

Hi everyone,

I’m trying to setup a CAN bus driver for a jetcat engine.

  • It is strongly inspired from CanardNuttXCDev.cpp.
  • I configured CONFIG_CAN=y, CONFIG_STM32F7_CAN1=y, CONFIG_STM32F7_CAN1_BAUD=20000 in defconfig
  • I don’t want to use uavcan so I commented it in boards/px4/fmu-v5/default.cmake
  • I open the CAN port as _fd = ::open("/dev/can0", O_RDWR | O_NONBLOCK);
  • the file descriptor is non null, so the port is open. I am able to read CAN data that are sent from a CAN bus analyzer. So the reading is functional.

But whenever I call write(), the function returns -1, i.e. no bytes are written. And whenever I call poll(&fds, 1, 100); // 100ms,
it returns <0.

So it seems that the CAN port is never available for transmitting/writing. Did I forget any initialization?
@dagar @PavelKirienko any idea?



Did you ever figure this out? I’m stuck one step before that. I don’t see /dev/can0 show up. This is on a Pixhawk 4.

Aha, I was missing this:

diff --git a/boards/px4/fmu-v5/src/init.c b/boards/px4/fmu-v5/src/init.c
index cfec81bae6..1f0581805c 100644
--- a/boards/px4/fmu-v5/src/init.c
+++ b/boards/px4/fmu-v5/src/init.c
@@ -95,6 +95,8 @@ extern void led_on(int led);
 extern void led_off(int led);
+extern void can_devinit(void);
  * Name: board_peripheral_reset
@@ -283,5 +285,7 @@ __EXPORT int board_app_initialize(uintptr_t arg)
+    can_devinit();
        return OK;

If I remember correctly, in my case I was calling write() from module::custom_command(), which turned out to be running on a different thread (?) than module::run() (in which the CAN port was opened).

Calling write() from module::run() solved the problem.