Enabling NuttX CAN drivers on Pixhawk4

I’m trying to get the NuttX CAN drivers built for my Pixhawk4 (px4_fmu-v5_default) and I can’t seem to get them to build. I verified that uavcan is disabled and that the NuttX CAN interfaces and STM32 CAN1 and CAN2 peripherals are enabled via boardconfig and menuconfig. However, it looks like the CAN drivers aren’t building and there’s no sign of /dev/can0 or /dev/can1 once I build it. What configuration options should I check?

ETA: CAN drivers are building but I can’t link my application in such a way that I can link the can_devinit() function nor do I see /dev/can0 or /dev/can1 on the Pixhawk once I load the code.

1 Like

So… I got past this hurdle by modifying boards/px4/fmu-v5/src/init.c.
I added a local declaration for can_devinit():

extern int can_devinit(void);

And I added a call to it down at the bottom of board_app_initialize(). So now /dev/can0 exists and my code starts and appears to be able to open. However, I see no evidence of incoming packets, despite there being a talkative NMEA2000 device on the CAN bus.

Have you had any more success?

I’m trying to do the same. I made a new driver taking some inspiration from src/drivers/cyphal/CanardNuttXCDev.cpp. I can read and write to /dev/can0 without error, but nothing is written to the bus (connected to CAN1), nor are incoming messages read - so seems like the same result. I verified with a CAN adapter connected to my computer.

I have not, but mostly b/c I’ve had a bunch of higher priority stuff on my plate and I may have found a way to remove the PX4 software from the path for my CAN devices making the answer less relevant to me.

1 Like

I had a success with that on PX4 v1.10

  1. in boards/px4/fmu-v5/nuttx-config/nsh/defconfig
CONFIG_STM32F7_CAN1=y
CONFIG_STM32F7_CAN1_BAUD=125000
  1. I created a custom driver in src/drivers
f (!_initialized) {

		struct can_dev_s *can = stm32_caninitialize(1); // the 1 is for CAN1

		if (can == nullptr) {
			PX4_ERR("Failed to get CAN interface");
			return;

		}
		/* Register the CAN driver at "/dev/can0" */
		int ret = can_register("/dev/can0", can);
		if (ret < 0) {
			PX4_ERR("can_register failed: %d", ret);
			return;
		}

		// open /dev/can0
		_fd = ::open("/dev/can0", O_RDWR /*| O_NONBLOCK*/);
		if (_fd < 0) {
			PX4_ERR("FAILED TO OPEN /dev/can0");
			return;
		} else {
			PX4_INFO("CAN opened successfully on /dev/can0");
		}

		_initialized = true;
	}

I hope this can help

2 Likes

Hi!
What am I doing wrong?

src/can.c:45:10: fatal error: stm32_can.h: No such file or directory

45 | #include “stm32_can.h”