h7 CAN not available

Discussion related to "under the hood" OpenMV topics.
inno
Posts: 6
Joined: Thu May 09, 2019 9:18 am

h7 CAN not available

Postby inno » Thu May 09, 2019 9:38 am

Hello everyone,

I am currently in the process of switching from the M7 to the H7. However it seems there
is no CAN module builtin to the H7 firmware. CAN however is crucial for my current project.

from pyb import CAN
-> ImportError: cannot import name CAN

Note that this works flawlessly on the M7

Also help(pyb) shows no can module whatsoever.
I tested the same with both v3.41 as well as v3.2 but no difference.
Looking at the firmware revealed, that there is no stm32h7xx_hal_can.h available in the sthal.

So I wondered if CAN is available for the H7 as described on the product page?
If yes, how can I get a firmware which supports CAN?
Since it's important to me, minor changes and compiling the firmware myself if necessary is an option.

Thanks,
Stefan
User avatar
iabdalkader
Posts: 937
Joined: Sun May 24, 2015 3:53 pm

Re: h7 CAN not available

Postby iabdalkader » Thu May 09, 2019 10:30 am

Unfortunately CAN has changed significantly from F7 to H7 and there's No driver in MicroPython. I started the support for H7 in upstream MicroPython but no one else has contributed a CAN driver yet. I could look into implementing a CAN driver for H7 soon.
User avatar
kwagyeman
Posts: 3264
Joined: Sun May 24, 2015 2:10 pm

Re: h7 CAN not available

Postby kwagyeman » Thu May 09, 2019 1:54 pm

We will have a CAN shield on sale soon so we can get this fixed. Please create a bug report on the github for this. The peripheral hasn't changed much so it's just some clock stuff that has to be dealt with.
Nyamekye,
inno
Posts: 6
Joined: Thu May 09, 2019 9:18 am

Re: h7 CAN not available

Postby inno » Fri May 10, 2019 6:18 am

Since we require CAN functionality as soon as possible (think next week) depending on what you mean by "soon" we would be willing to contribute.
iabdalkader - can you maybe pinpoint me to the location of the work you have already done?

Thanks for the quick replies
User avatar
iabdalkader
Posts: 937
Joined: Sun May 24, 2015 3:53 pm

Re: h7 CAN not available

Postby iabdalkader » Fri May 10, 2019 8:37 am

inno wrote:
Fri May 10, 2019 6:18 am
Since we require CAN functionality as soon as possible (think next week) depending on what you mean by "soon" we would be willing to contribute.
iabdalkader - can you maybe pinpoint me to the location of the work you have already done?

Thanks for the quick replies
That would be great! Note no work has been done on the new CAN driver at all, anyway, here's the STHAL FDCAN driver (it's called FDCAN on H7):

https://github.com/openmv/openmv/blob/m ... al_fdcan.h
https://github.com/openmv/openmv/blob/m ... al_fdcan.c

This is our fork of MicroPython (all work is done in the openmv branch):

https://github.com/openmv/micropython

This is the old CAN driver you can base your work on this:

https://github.com/openmv/micropython/b ... tm32/can.h
https://github.com/openmv/micropython/b ... tm32/can.c


You can either fork our MicroPython and send a PR or send it upstream. If you send it to us it will get merged a lot faster, and I'll send it upstream.
User avatar
travist
Posts: 2
Joined: Fri May 10, 2019 5:49 pm

Re: h7 CAN not available

Postby travist » Fri May 10, 2019 5:57 pm

Hello,

I am also very interested in CAN/CANFD on the H7. I have spoken with Damien at Micropython several times about this, and his time and focus has been on short supply to work on the new peripheral. As it has been pointed out the H7 is much more advanced and supports the CAN Flexible Data Rate, which many systems and devices to not utilize. My hope is there can be two stages where standard CAN 2.0A/B is supported with the new peripheral before full CAN FD functionality is worried about.

I personally do not have the time to work on the libraries, but willing to contribute to funding at least some of the efforts. CAN is used in almost all my products and projects and need it on a device to consider it.

While waiting to hear back from Damien on the next step with this I created an issue in their repo to help show support for the efforts on CAN for the H7.
https://github.com/micropython/micropython/issues/4774

If there are things I can do to help speed this up I would like to know. The price on the H7 chip is sometimes cheaper than the F7, and the CAN peripheral is the main thing in the way of me using it and working in the OpenMV H7 into some of my vision work.

Thanks
___________
Travis
User avatar
iabdalkader
Posts: 937
Joined: Sun May 24, 2015 3:53 pm

Re: h7 CAN not available

Postby iabdalkader » Fri May 10, 2019 8:03 pm

Hi, there's not much to do right now (unless you want start writing the driver) I've ordered some CAN transceivers and I'm going to work on adding basic CAN support for the H7 over the next weekend.
User avatar
kwagyeman
Posts: 3264
Joined: Sun May 24, 2015 2:10 pm

Re: h7 CAN not available

Postby kwagyeman » Sat May 11, 2019 3:42 pm

Hi, I can work on getting the CAN operational. I already have the new CAN shield prototypes at home we are about to release. I'll just make the default normal CAN stuff work.

(Looks like Ibrahim might get to this before me).
Nyamekye,
User avatar
travist
Posts: 2
Joined: Fri May 10, 2019 5:49 pm

Re: h7 CAN not available

Postby travist » Sun May 12, 2019 8:50 pm

Hello thanks for he help with supporting this.

Whether it is buying some boards or donating a little bit of funds to make this a higher priority I am happy to do one or both.

I hope the goal is that we can mimic the CAN capability of the F4/F7 on with Micropython, that would be ideal. FD functionality is probably needed by few at the moment, and where I believe most of the new complexity would come in.

Right now I am swamped with little projects I am already committed to, and for now the F7 and F4 chips work, so I do not have to move to the H7... but I want to :-)

I also want to use the new H7 OpenMV with some basic automation tools I am working on, and knowing the performance gain (latest silicon goes up to 480MHz now) This would be many times better than the current platforms that have CAN support.

Which transceivers are you planning to use/test with?

Thanks again and If we want/need to move a conversation off the forums just message me your email.

Thanks
___________
Travis
User avatar
iabdalkader
Posts: 937
Joined: Sun May 24, 2015 3:53 pm

Re: h7 CAN not available

Postby iabdalkader » Sun May 12, 2019 10:24 pm

I ordered MCP2551 but I think we're using something else in our CAN shields.
User avatar
kwagyeman
Posts: 3264
Joined: Sun May 24, 2015 2:10 pm

Re: h7 CAN not available

Postby kwagyeman » Sun May 12, 2019 10:43 pm

Here's the design. It's tested and works.

https://github.com/openmv/openmv-boards ... an/can.pdf
Nyamekye,
inno
Posts: 6
Joined: Thu May 09, 2019 9:18 am

Re: h7 CAN not available

Postby inno » Tue May 14, 2019 5:41 am

iabdalkader wrote:
Fri May 10, 2019 8:03 pm
Hi, there's not much to do right now (unless you want start writing the driver) I've ordered some CAN transceivers and I'm going to work on adding basic CAN support for the H7 over the next weekend.
Sounds great :-)
Can you keep us posted on how things are turning out?
Especially if you don't find the time to work on it, so we know if we have to find a plan B to finish our project on time.

Best
Stefan
User avatar
kwagyeman
Posts: 3264
Joined: Sun May 24, 2015 2:10 pm

Re: h7 CAN not available

Postby kwagyeman » Wed May 15, 2019 12:37 am

The production run of the CAN shields are done. I will get samples soon. I'll send some to Ibrahim so he can write his driver.
Nyamekye,
User avatar
iabdalkader
Posts: 937
Joined: Sun May 24, 2015 3:53 pm

Re: h7 CAN not available

Postby iabdalkader » Sat May 18, 2019 7:06 pm

Hi can anyone help me test the FDCAN driver ? If you have 2x H7 and 2x CAN transceivers let me know and I'll send you a firmware image for testing.
inno
Posts: 6
Joined: Thu May 09, 2019 9:18 am

Re: h7 CAN not available

Postby inno » Tue May 21, 2019 4:54 am

Great :-)
I have two h7 and two can transceiver which were working with the M7 here.

Best
Stefan
User avatar
iabdalkader
Posts: 937
Joined: Sun May 24, 2015 3:53 pm

Re: h7 CAN not available

Postby iabdalkader » Tue May 21, 2019 10:05 am

Great, attached a firmware image that enables FDCAN. You just need to test with something like this:

Code: Select all

from pyb import CAN
can = CAN(2, CAN.LOOPBACK)
can.send('Hello', 123)# send message with id 123

Code: Select all

from pyb import CAN
can = CAN(2, CAN.LOOPBACK)
print(can.recv(0, timeout=10000))  # receive message on FIFO 0
Attachments
firmware.zip
(935.83 KiB) Downloaded 459 times
inno
Posts: 6
Joined: Thu May 09, 2019 9:18 am

Re: h7 CAN not available

Postby inno » Fri May 24, 2019 3:42 am

Hi,

I'm afraid the image does not work yet. At least there is no signal appearing on neither the GPIOs nor the transceiver output, when sending data.
Could you maybe push your changes to a branch on your fork, so we can debug what's happening?

Best
Stefan
User avatar
iabdalkader
Posts: 937
Joined: Sun May 24, 2015 3:53 pm

Re: h7 CAN not available

Postby iabdalkader » Fri May 24, 2019 10:09 am

Hi Stefan,

There's an open PR here with all my changes:

https://github.com/micropython/micropython/pull/4793
User avatar
garivetm
Posts: 12
Joined: Mon Sep 18, 2017 8:51 am

Re: h7 CAN not available

Postby garivetm » Thu Jun 20, 2019 11:21 am

Hello there,

I'm currently using a M7 cam and I am planning to switch to the new H7. However CAN is mandatory in my application.
Could you give me an update about the work you are doing on implementing CAN bus on the new H7 ?

I see this comment in the PR : "This driver has been tested in loopback mode and with a two node transceiver-less network. However, it could use a lot of improvements especially with filters, interrupts and error handling, rxcallback etc..." Does it mean that filter is not yet implemented at all ? Because, in my opinion, filtering is a necessary feature when dealing with CAN.

Thank you for your answer.
User avatar
iabdalkader
Posts: 937
Joined: Sun May 24, 2015 3:53 pm

Re: h7 CAN not available

Postby iabdalkader » Thu Jun 20, 2019 11:25 am

garivetm wrote:
Thu Jun 20, 2019 11:21 am
Does it mean that filter is not yet implemented at all ? Because, in my opinion, filtering is a necessary feature when dealing with CAN.
No basic filtering has been implemented, however the driver still needs some more work. I'll continue working on the driver this month, should be ready with the next release. Also MicroPython developers will help with the driver hopefully after they finish up their release.
User avatar
garivetm
Posts: 12
Joined: Mon Sep 18, 2017 8:51 am

Re: h7 CAN not available

Postby garivetm » Thu Jun 20, 2019 11:38 am

Ok, then which repo should I follow to be kept update on the upcoming improvements ?
User avatar
iabdalkader
Posts: 937
Joined: Sun May 24, 2015 3:53 pm

Re: h7 CAN not available

Postby iabdalkader » Thu Jun 20, 2019 12:15 pm

I'll update this thread as soon as we have any news about this.
inno
Posts: 6
Joined: Thu May 09, 2019 9:18 am

Re: h7 CAN not available

Postby inno » Tue Jul 02, 2019 6:24 am

Hi,

We got everything working for our project, so it is possible to use H7 with CAN, although it is not "smooth" as of yet.
Basically we are using the stock micropython form openmv with the changes from the PR from iabdalkader with a few changes.
1. the filters seemed not to work for me. After some troubleshooting, I found out, that first all packages have to be rejected to then allow only those specified by the filter
-> we added this line to ports/stm32/fdcan.c:154 after HAL_FDCAN_Init
HAL_FDCAN_ConfigGlobalFilter(&can_obj->fdcan, 3, 3, 0, 1);
2. can.any always returns true, this is because ports/stm32/fdcan.c:540 both options return true.
You can however work around it by using something like
while True:
try:
can.recv(0, lst, timeout=1)
except OsError:
break

can.recv only works with the list argument. not providing one results in an empty list being returned. Filling the list however works fine. Its not too bad, but weird.
-> do this
buf=bytearray(8)
lst = [0, 0, 0, memoryview(buf)]
and give lst as second argument to can.recv


Filtering with range filters is totally sufficient for us because you can set multiple filters with them.
E.g.
can.setfilter(0, 0, (0xe1, 0xe2)
can.setfilter(2, 0, (0xf1, 0xf2)
to get both 0xe1 and 0xf1 in FIFO 0.

When synchronizing to our can bus we found another issue when reading:
We had to set prescaler=16, sjw=1, bs1=8, bs2=3 in can.init although we expected prescaler=18 according to stm32 cube mx.
According to pyb.freq we would have calculated a frequency of 41, sjw=4, bs1=12, bs2=5
I am not too familiar with this issue, so I can't give any more details.

oh and don't forget to add fdcan.c to the Makefile in openmv/src.

Hope this is helpful

Oh and big shout out to iabdalkader for making it possible, that we completed our project on time. Thanks :-)
User avatar
iabdalkader
Posts: 937
Joined: Sun May 24, 2015 3:53 pm

Re: h7 CAN not available

Postby iabdalkader » Tue Jul 02, 2019 8:28 am

Wow thank you, this is very useful indeed. I just got our CAN shields and will be working on this next weekend and I'll add these changes the code.
We had to set prescaler=16, sjw=1, bs1=8, bs2=3 in can.init although we expected prescaler=18 according to stm32 cube mx.
I'm guessing maybe the project is set to 400MHz (the H7 runs at 480MHz now).
User avatar
iabdalkader
Posts: 937
Joined: Sun May 24, 2015 3:53 pm

Re: h7 CAN not available

Postby iabdalkader » Mon Sep 16, 2019 11:34 am

Hi, CAN is now supported on the H7, and I fixed the timing, setfilter() and any() bugs. The next release will be out in a day or two.

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 5 guests