# NuttX CAN drivers on STM32H7x3

**URL:** <https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828>\
**Category:** PX4 Autopilot\
**Created:** [November 20, 2022, 12:05pm UTC](https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828 "2022-11-20T12:05:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlexSwitch](https://discuss.px4.io/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@AlexSwitch](https://discuss.px4.io/u/AlexSwitch)\
**Post date:** [November 20, 2022, 12:05pm UTC](https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828/1 "2022-11-20T12:05:50Z")

</div>

Hello, everyone!  
I am trying to connect to the CAN port on a board with a STM32H7 chip. I set CONFIG\_CAN=y in the defconfig file and get the error

```auto
/src/can.c:26:10: fatal error: stm32_can.h: No such file or directory
   26 | #include "stm32_can.h"
      | ^ ~~~~~~~~~~~~
compilation terminated.

```

After a little understanding I realized that STM32H7 chips do not support NuttX CAN because

In  
[PX4-Autopilot/platforms/nuttx/NuttX/nuttx/arch/arm/src/stm32h7](https://github.com/PX4/NuttX/tree/6bafcb22b7d62184fd1158c0b53c8dbd884275b8/arch/arm/src/stm32h7)  
does not exist files `stm32_can.h` `stm32_can.c`  
and  
[PX4-Autopilot/platforms/nuttx/NuttX/nuttx/arch/arm/src/stm32h7/hardware](https://github.com/PX4/NuttX/tree/6bafcb22b7d62184fd1158c0b53c8dbd884275b8/arch/arm/src/stm32h7/hardware)  
does not exist file `stm32_can.h`

In this connection I have a question:  
How does UAVCAN work without CAN bus libraries on a board with STM32H7 chip? @PavelKirienko  
Are there plans to implement CAN support on STM32H7 chips in NuttX and when? @nuttx

---

<div class="post-metadata">

**Author:** ![JulianOes](https://discuss.px4.io/user_avatar/discuss.px4.io/julianoes/32/30_2.png) [@JulianOes](https://discuss.px4.io/u/JulianOes)\
**Post date:** [November 20, 2022, 9:59pm UTC](https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828/2 "2022-11-20T21:59:49Z")

</div>

There is CAN-FD support coming on H7 with the next NuttX update, check these PRs:

I’m not sure if it’s in NuttX 10.3 or 10.4 but some PRs are coming soon here.

> <https://github.com/PX4/PX4-Autopilot/pull/20190>
>
> Co-authored-by: Peter van der Perk \<peter.vanderperk@nxp.com\>

> <https://github.com/PX4/PX4-Autopilot/pull/20026>
>
> Updates PX4 to run with NuttX 10.4
> Right now we use this PR to run CI for testi…ng
> 
> Furthermore adds support for the CANHUB-K3 board.

That being said, I tried to get a CAN device (not UAVCAN) working on top of 10.4 but I could not see my device using `candump` in the console. Maybe I’m missing something, or maybe it’s not that trivial to use a CAN device using CAN-FD although I thought it was backwards compatible.

---

<div class="post-metadata">

**Author:** ![AlexSwitch](https://discuss.px4.io/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@AlexSwitch](https://discuss.px4.io/u/AlexSwitch)\
**Post date:** [November 21, 2022, 7:06am UTC](https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828/3 "2022-11-21T07:06:55Z")

</div>

Hi!  
Thank you so much for your reply. I just saw that 5 hours ago the NuttX platform was upgraded to version 10.3+  
I too am now trying to add a CAN 2.0A device, it is a microwave radio altimeter based on the Nanoradar NRA24. I hope I succeed and share this driver with everyone, maybe you can do it now too.  
Thanks a lot to you and the dev team!

---

<div class="post-metadata">

**Author:** ![AlexSwitch](https://discuss.px4.io/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@AlexSwitch](https://discuss.px4.io/u/AlexSwitch)\
**Post date:** [November 21, 2022, 7:54am UTC](https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828/4 "2022-11-21T07:54:13Z")

</div>

Unfortunately with the NuttX update the CAN port does not start this is also due to missing files in

[PX4-Autopilot/platforms/nuttx/NuttX/nuttx/arch/arm/src/stm32h7](https://github.com/PX4/NuttX/tree/6bafcb22b7d62184fd1158c0b53c8dbd884275b8/arch/arm/src/stm32h7)  
does not exist files `stm32_can.h` `stm32_can.c`  
and  
[PX4-Autopilot/platforms/nuttx/NuttX/nuttx/arch/arm/src/stm32h7/hardware](https://github.com/PX4/NuttX/tree/6bafcb22b7d62184fd1158c0b53c8dbd884275b8/arch/arm/src/stm32h7/hardware)  
does not exist file `stm32_can.h`

---

<div class="post-metadata">

**Author:** ![JulianOes](https://discuss.px4.io/user_avatar/discuss.px4.io/julianoes/32/30_2.png) [@JulianOes](https://discuss.px4.io/u/JulianOes)\
**Post date:** [November 22, 2022, 8:06pm UTC](https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828/5 "2022-11-22T20:06:50Z")

</div>

Sorry, I should have specified. This now uses the socketcan interface.

> <https://github.com/PX4/NuttX/blob/px4_firmware_nuttx-10.4.0%2B/boards/arm/stm32h7/nucleo-h743zi2/configs/socketcan/defconfig#L42-L49>

---

<div class="post-metadata">

**Author:** ![AlexSwitch](https://discuss.px4.io/letter_avatar_proxy/v4/letter/a/4da419/32.png) [@AlexSwitch](https://discuss.px4.io/u/AlexSwitch)\
**Post date:** [November 22, 2022, 10:06pm UTC](https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828/6 "2022-11-22T22:06:10Z")

</div>

I am now immersed in studying the AUVCAN driver software code for the STM32H7. Since Pavel Kirienko answered that UAVCAN can share the same bus with other protocols based on CAN 2.0A (11-bit identifiers).  
I am planning to either write a new function in the existing UAVCAN driver which will pass the standard CAN frames I need or use the existing ones, similar to what was already [described](https://github.com/PX4/PX4-Autopilot/blob/main/src/drivers/uavcan/uavcan_drivers/socketcan/driver/src/socketcan.cpp), like this:

```auto
	/* Copy SocketCAN frame to CanardFrame */

	if (_can_fd) {
		struct canfd_frame *recv_frame = (struct canfd_frame *)&_recv_frame;
		out_frame.id = recv_frame->can_id;
		out_frame.dlc = recv_frame->len;
		memcpy(out_frame.data, &recv_frame->data, recv_frame->len);

	} else {
		struct can_frame *recv_frame = (struct can_frame *)&_recv_frame;
		out_frame.id = recv_frame->can_id;
		out_frame.dlc = recv_frame->can_dlc;
		memcpy(out_frame.data, &recv_frame->data, recv_frame->can_dlc);
	}

```

If it works, then we do not have to disable UAVCAN to receive messages from CAN 2A devices and receive and process them on the same wires as the UAVCAN devices

> **[Both use CAN (11bit) and UAVCAN at the same time on the same port?](https://forum.opencyphal.org/t/both-use-can-11bit-and-uavcan-at-the-same-time-on-the-same-port/1791)**
>
> Hi there! I am currently trying to connect the DistanceRange radar from Nanoradar model NRA24 for the PX4-Autopilot (STM32H7). This radar uses CAN 2.0A (11bit) for data transfer. Somewhere in the user manual I read: UAVCAN uses only CAN 2.0B frame...

---

<div class="post-metadata">

**Author:** ![JulianOes](https://discuss.px4.io/user_avatar/discuss.px4.io/julianoes/32/30_2.png) [@JulianOes](https://discuss.px4.io/u/JulianOes)\
**Post date:** [November 22, 2022, 10:54pm UTC](https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828/7 "2022-11-22T22:54:29Z")

</div>

I guess that would as well.

I now have writing working inspired by cansend.c and I’m just trying to get reading to work like in candump.c.

The driver I meant is here:

> <https://github.com/PX4/NuttX/blob/px4_firmware_nuttx-10.4.0%2B/arch/arm/src/stm32h7/stm32_fdcan_sock.c>

---

<div class="post-metadata">

**Author:** ![Jmailloux](https://discuss.px4.io/user_avatar/discuss.px4.io/jmailloux/32/9327_2.png) [@Jmailloux](https://discuss.px4.io/u/Jmailloux)\
**Post date:** [February 17, 2023, 8:50pm UTC](https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828/8 "2023-02-17T20:50:33Z")

</div>

Hi Julian  
I am planning on using the socketCAN driver for Pixhawk6C. I am running into NuttX compile errors using the defconfig defines you have above in the fmu-v6c defconfig file. It is because NET\_SOCK\_TYPE isn’t defined. It appears CONFIG\_NET\_IPv4 is somehow defined. That causes NET\_SOCK\_FAMILY to be AF\_INET, and NET\_SOCK\_TYPE to not be defined in netconfig.h. Have you come across this before?

---

<div class="post-metadata">

**Author:** ![Jmailloux](https://discuss.px4.io/user_avatar/discuss.px4.io/jmailloux/32/9327_2.png) [@Jmailloux](https://discuss.px4.io/u/Jmailloux)\
**Post date:** [February 21, 2023, 2:51am UTC](https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828/9 "2023-02-21T02:51:04Z")

</div>

Enabling CONFIG\_NET\_UDP fixed this issue. Not clear why CONFIG\_NET\_IPv4 is enabled…and KCONFIG doesn’t show this dependancy…

---

<div class="post-metadata">

**Author:** ![JulianOes](https://discuss.px4.io/user_avatar/discuss.px4.io/julianoes/32/30_2.png) [@JulianOes](https://discuss.px4.io/u/JulianOes)\
**Post date:** [March 10, 2023, 1:34am UTC](https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828/10 "2023-03-10T01:34:50Z")

</div>

One more thing that I just found.

I’m running my custom CAN driver on CAN2, and I have UAVCAN configured for only one interface, so CAN1.

Now, as soon as I do `uavcan start` (or set `UAVCAN_ENABLE` to `> 0`) my custom CAN driver locks up. I couldn’t yet figure out why.

These are my UAVCAN settings:

```auto
CONFIG_BOARD_UAVCAN_INTERFACES=1

```

---

<div class="post-metadata">

**Author:** ![Tink](https://discuss.px4.io/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@Tink](https://discuss.px4.io/u/Tink)\
**Post date:** [May 5, 2023, 2:19am UTC](https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828/11 "2023-05-05T02:19:58Z")

</div>

Hi Julian  
Have you achieved reading and writing classic can messages busing stm32\_fdcan\_sock.c ? Can you please give a brief introduction about how to use this. I am confronting the problem of reading/writing can messages via v6x(which uses stm32h753)

---

<div class="post-metadata">

**Author:** ![JulianOes](https://discuss.px4.io/user_avatar/discuss.px4.io/julianoes/32/30_2.png) [@JulianOes](https://discuss.px4.io/u/JulianOes)\
**Post date:** [May 5, 2023, 10:41pm UTC](https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828/12 "2023-05-05T22:41:51Z")

</div>

Let me try to summarize the steps I did to get a driver working on CAN2:

Add this to `boards/px4/fmu-v6x/init/rc.board_defaults`:

```auto
ifup can1

```

Update NuttX submodule to this hack:

> <https://github.com/julianoes/PX4-NuttX/commit/b39310baea123ee27333e1743a80bbc7bd5c74e7>

Add to `boards/px4/fmu-v6x/default.px4board`:

```auto
CONFIG_BOARD_UAVCAN_INTERFACES=1

```

Add to `boards/px4/fmu-v6x/nuttx-config/nsh/defconfig`:

```auto
CONFIG_NETDEV_CAN_BITRATE_IOCTL=y
CONFIG_NETDEV_IFINDEX=y
CONFIG_NET_CAN=y
CONFIG_NET_CAN_RAW_TX_DEADLINE=y
CONFIG_NET_CAN_SOCK_OPTS=y
CONFIG_NET_CAN_NOTIFIER=y
CONFIG_NET_TIMESTAMP=y
CONFIG_STM32H7_FDCAN1=y
CONFIG_STM32H7_FDCAN2=y
CONFIG_CANUTILS_CANDUMP=y
CONFIG_CANUTILS_CANSEND=y

```

And then basically try out `cansend` and `candump` and adapt those to what I needed.

And I think I still have to set `ENABLE_UAVCAN` to `0` to actually get it to work, even though that shouldn’t be required.

---

<div class="post-metadata">

**Author:** ![Tink](https://discuss.px4.io/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@Tink](https://discuss.px4.io/u/Tink)\
**Post date:** [May 6, 2023, 12:31am UTC](https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828/13 "2023-05-06T00:31:34Z")

</div>

Thanks for your reply!  
Do you mean by conducting these settings, I can directly call the methods in _cansend.c_ and _candump.c_ in _platforms\nuttx\NuttX\apps\canutils_ to receive/transmit messages? Are there any other settings need to be done such as modifing the original methods in _cansend.c_ ?

---

<div class="post-metadata">

**Author:** ![JulianOes](https://discuss.px4.io/user_avatar/discuss.px4.io/julianoes/32/30_2.png) [@JulianOes](https://discuss.px4.io/u/JulianOes)\
**Post date:** [May 7, 2023, 8:21pm UTC](https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828/14 "2023-05-07T20:21:55Z")

</div>

You will have to adapt cansend and candump to what you actually need to do on CAN. Or rather you copy the parts of cansend and candump that you need. But for a first test, cansend/candump can help you debug whether it works.

---

<div class="post-metadata">

**Author:** ![Tink](https://discuss.px4.io/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@Tink](https://discuss.px4.io/u/Tink)\
**Post date:** [May 8, 2023, 12:25am UTC](https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828/15 "2023-05-08T00:25:45Z")

</div>

got it. I will try it on my v6x hardware,hope it works. Thanks very much!

---

<div class="post-metadata">

**Author:** ![Tink](https://discuss.px4.io/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@Tink](https://discuss.px4.io/u/Tink)\
**Post date:** [May 16, 2023, 7:39am UTC](https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828/16 "2023-05-16T07:39:02Z")

</div>

> [@Tink](#):
>
> Hi Julian

Hi Julian, I did the configuarations as you described above, but when I type “cansend can0 1122334455667788” or “candump can0” in the nsh console, nothing happens and i can’t receive or send any can messages through my USB-CAN device.  
Are there any mistake in my working procedure? I just don’t know how to make it work…

---

<div class="post-metadata">

**Author:** ![Tink](https://discuss.px4.io/letter_avatar_proxy/v4/letter/t/58956e/32.png) [@Tink](https://discuss.px4.io/u/Tink)\
**Post date:** [May 17, 2023, 10:14am UTC](https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828/17 "2023-05-17T10:14:03Z")

</div>

> [@JulianOes](#):
>
> `stm32_fdcan_sock.c`

It actually worked out as described by @JulianOes .  
However it turns that i cannot change the configuration “CONFIG\_FDCAN1\_DATA\_BITRATE=500000” and “CONFIG\_FDCAN2\_DATA\_BITRATE=500000” ,I still have to use baudrate of 1000k to establish commonication.  
Does anyone know how can I change the baudrate to 500k? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![DaShen96](https://discuss.px4.io/user_avatar/discuss.px4.io/dashen96/32/13292_2.png) [@DaShen96](https://discuss.px4.io/u/DaShen96)\
**Post date:** [June 1, 2023, 9:36am UTC](https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828/18 "2023-06-01T09:36:54Z")

</div>

Hi,Julian. I found the following issue when using v6x CAN communication: when using standard frames, only some ID data can be received, and many IDs seem to be filtered. This issue does not occur when using extended frame packets. May I ask if there is a problem with my configuration?I configured it according to what you provided above.

---

<div class="post-metadata">

**Author:** ![JulianOes](https://discuss.px4.io/user_avatar/discuss.px4.io/julianoes/32/30_2.png) [@JulianOes](https://discuss.px4.io/u/JulianOes)\
**Post date:** [June 4, 2023, 9:03pm UTC](https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828/19 "2023-06-04T21:03:25Z")

</div>

Did you see my hack?

> <https://github.com/julianoes/PX4-NuttX/commit/b39310baea123ee27333e1743a80bbc7bd5c74e7>

And my question here?  
[https://community.st.com/s/question/0D53W00001xRGuQSAW/how-is-can-message-ram-initializedzeroed-on-stm32h7](https://community.st.com/s/question/0D53W00001xRGuQSAW/how-is-can-message-ram-initializedzeroed-on-stm32h7)

---

<div class="post-metadata">

**Author:** ![DaShen96](https://discuss.px4.io/user_avatar/discuss.px4.io/dashen96/32/13292_2.png) [@DaShen96](https://discuss.px4.io/u/DaShen96)\
**Post date:** [June 5, 2023, 2:21am UTC](https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828/20 "2023-06-05T02:21:31Z")

</div>

thank you Julian!!! It’s work.

[Next page](https://discuss.px4.io/t/nuttx-can-drivers-on-stm32h7x3/29828.md?page=2)
