Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAN communication support #259

Open
Vicky-S opened this issue Jun 2, 2018 · 47 comments
Open

CAN communication support #259

Vicky-S opened this issue Jun 2, 2018 · 47 comments
Labels
enhancement New feature or request

Comments

@Vicky-S
Copy link

Vicky-S commented Jun 2, 2018

I could not find any example for CAN communication in STM32 official core. Is there any?

@fpistm fpistm added the question ❓ Usually converted as a discussion label Jun 2, 2018
@fpistm
Copy link
Member

fpistm commented Jun 2, 2018

Currently, there is no CAN support

@Vicky-S
Copy link
Author

Vicky-S commented Jun 2, 2018

Ok. will it be available in future?

@fpistm
Copy link
Member

fpistm commented Jun 2, 2018

Yes but I can't tell when...

@Vicky-S
Copy link
Author

Vicky-S commented Jun 2, 2018

Ok... Please try to make support ASAP..

@BennehBoy
Copy link
Contributor

@fpistm, by no support you mean there is no library right? So people could program directly against HAL CAN modules?

@fpistm
Copy link
Member

fpistm commented Feb 11, 2019

@BennehBoy
Yes of course. All STM32 features are available thanks HAL/LL (if user enable the right HAL module).
All stuff which can be done thanks the cube are available. By no support I told about Arduino point of view, there is no dedicated library to support STM32 CAN for this core with specific Arduino API style

@fpistm fpistm changed the title CAN communication support not available for STM32F746 CAN communication support Feb 19, 2019
@fpistm fpistm added New feature and removed question ❓ Usually converted as a discussion labels Feb 19, 2019
@jacky4566
Copy link

Bump. Would love to see some CAN bus support. Seems silly to buy a standalone SPI chip when all you need is a transceiver.

@fpistm
Copy link
Member

fpistm commented Nov 12, 2019

Hi @jacky4566
I'd love too.
I know @geosmall made some work around CAN:
https://github.com/geosmall/UAVCAN-for-STM32-Arduino

@seeers
Copy link
Contributor

seeers commented Jan 8, 2020

Unfortunately the UAVCAN example only works with the STM32Duino version 1.4.
With version 1.8 I get the following errors:
`
sketch\can.c: In function 'MX_CAN_Init':
can.c:12: error: 'CAN_InitTypeDef' {aka 'struct '} has no member named 'SJW'
12 | hcan.Init.SJW = CAN_SJW_1TQ;
| ^
can.c:13: error: 'CAN_InitTypeDef' {aka 'struct '} has no member named 'BS1'
13 | hcan.Init.BS1 = CAN_BS1_1TQ;
| ^
can.c:14: error: 'CAN_InitTypeDef' {aka 'struct '} has no member named 'BS2'
14 | hcan.Init.BS2 = CAN_BS2_1TQ;
| ^
can.c:15: error: 'CAN_InitTypeDef' {aka 'struct '} has no member named 'TTCM'
15 | hcan.Init.TTCM = DISABLE;
| ^
can.c:16: error: 'CAN_InitTypeDef' {aka 'struct '} has no member named 'ABOM'
16 | hcan.Init.ABOM = DISABLE;
| ^
can.c:17: error: 'CAN_InitTypeDef' {aka 'struct '} has no member named 'AWUM'
17 | hcan.Init.AWUM = DISABLE;
| ^
can.c:18: error: 'CAN_InitTypeDef' {aka 'struct '} has no member named 'NART'
18 | hcan.Init.NART = DISABLE;
| ^
can.c:19: error: 'CAN_InitTypeDef' {aka 'struct '} has no member named 'RFLM'
19 | hcan.Init.RFLM = DISABLE;
| ^
can.c:20: error: 'CAN_InitTypeDef' {aka 'struct '} has no member named 'TXFP'
20 | hcan.Init.TXFP = DISABLE;
| ^
exit status 1
'CAN_InitTypeDef' {aka 'struct '} has no member named 'SJW'

`

Is there maybe an easy way to get it running with the current version?

@fpistm
Copy link
Member

fpistm commented Jan 8, 2020

@seeers
I think this is due to STM32F1 HAL update.
looking at the Release_Notes.html:
https://htmlpreview.github.io/?https://github.com/stm32duino/Arduino_Core_STM32/blob/master/system/Drivers/STM32F1xx_HAL_Driver/Release_Notes.html

From version V1.1.3 / 09-October-2018:

HAL CAN update
Fields of CAN_InitTypeDef structure are reworked:
SJW to SyncJumpWidth, BS1 to TimeSeg1, BS2 to TimeSeg2, TTCM to TimeTriggeredMode, ABOM to AutoBusOff, AWUM to AutoWakeUp, NART to AutoRetransmission (inversed), RFLM to ReceiveFifoLocked and TXFP to TransmitFifoPriority
HAL_CAN_Init() is split into both HAL_CAN_Init() and HAL_CAN_Start() API’s
HAL_CAN_Transmit() is replaced by HAL_CAN_AddTxMessage() to place Tx Request, then HAL_CAN_GetTxMailboxesFreeLevel() for polling until completion.
HAL_CAN_Transmit_IT() is replaced by HAL_CAN_ActivateNotification() to enable transmit IT, then HAL_CAN_AddTxMessage() for place Tx request.
HAL_CAN_Receive() is replaced by HAL_CAN_GetRxFifoFillLevel() for polling until reception, then HAL_CAN_GetRxMessage()
to get Rx message.
HAL_CAN_Receive_IT() is replaced by HAL_CAN_ActivateNotification() to enable receive IT, then HAL_CAN_GetRxMessage()
in the receivecallback to get Rx message
HAL_CAN_Slepp() is renamed as HAL_CAN_RequestSleep()
HAL_CAN_TxCpltCallback() is split into HAL_CAN_TxMailbox0CompleteCallback(), HAL_CAN_TxMailbox1CompleteCallback() and HAL_CAN_TxMailbox2CompleteCallback().
HAL_CAN_RxCpltCallback is split into HAL_CAN_RxFifo0MsgPendingCallback() and HAL_CAN_RxFifo1MsgPendingCallback().
More complete “How to use the new driver” is detailed in the driver header section itself.

It seems there is an HAL_CAN_LEGACY_MODULE_ENABLED which can be enabled to use the legacy one but HAL_CAN_MODULE_ENABLED have to be disabled.

@jacky4566
Copy link

If you look at the code in this project the Canbus registers are really not all that hard to use. Maybe 3 or 4 key regs.
I got it working after working out some of the defined values. I am working on my own implementation for STM32L4 as part of a custom library.

https://github.com/UBC-Solar/Firmware-v2/tree/master/Peripherals/CAN

@geosmall
Copy link
Contributor

I have the Libcanard UAVCAN example mentioned above now up and running / verified on V1.8.
https://github.com/geosmall/UAVCAN-for-STM32-Arduino

As this library (https://github.com/UAVCAN/libuavcan) uses it's own low level access and does not use HAL/LL I only needed to fix the rename deltas so not too bad.

UAVCAN based CAN is gaining some traction in both Ardupilot (https://github.com/ArduPilot/ardupilot) and PX4 (https://github.com/PX4/Firmware) so I'm planning to stay on that path.

I will update my example project over the weekend and document on https://www.stm32duino.com/

@fpistm
Copy link
Member

fpistm commented Jan 10, 2020

Thanks @geosmall for the feedback.

You can also document on the wiki if you want.

@Vicky-S
Copy link
Author

Vicky-S commented Jan 10, 2020

Will it work for STM32F4 Series too? @geosmall

@seeers
Copy link
Contributor

seeers commented Jan 10, 2020

If you look at the code in this project the Canbus registers are really not all that hard to use. Maybe 3 or 4 key regs.
I got it working after working out some of the defined values. I am working on my own implementation for STM32L4 as part of a custom library.

https://github.com/UBC-Solar/Firmware-v2/tree/master/Peripherals/CAN

I got it running with parts of your code on an stm32f103c8t6. Thank you very much
If someone needs a very simple example: https://github.com/seeers/CAN-Bus-Arduino_Core_STM32

@geosmall
Copy link
Contributor

geosmall commented Jan 11, 2020

@Vicky-S

Will it work for STM32F4 Series too? @geosmall

I have examples for F103C8 (Bluepill) AND F303RE-NUCLEO I am updating.

The CAN periperals are all pretty close but the devil is in the datasheets :).

It would of course need an F4 variant with CAN peripheral (e.g. F405/7, F446, etc and not F401, F411...).

@geosmall
Copy link
Contributor

geosmall commented Jan 11, 2020

Thanks @geosmall for the feedback.

You can also document on the wiki if you want.

Ok I'll capture on the forum and then figure out how to contribute to wiki (another first for me so please forgive stumbles :)

@geosmall
Copy link
Contributor

UAVCAN libcanard based CAN example updated for v1.8 here https://github.com/geosmall/UAVCAN-for-STM32-Arduino.

F1 preliminary coms testing completed so far.

@Vicky-S
Copy link
Author

Vicky-S commented Jan 14, 2020

@geosmall

The CAN periperals are all pretty close but the devil is in the datasheets :).

It would of course need an F4 variant with CAN peripheral (e.g. F405/7, F446, etc and not F401, F411...).

I have F446 Board. Please tell me about the required changes. Let me try.

@geosmall
Copy link
Contributor

geosmall commented Jan 15, 2020

F103 Bluepill end-to-end CAN testing has now been completed. ESC values input with UAVCAN GUI are sent over CAN bus to Bluepill node, which receives and re-transmits those same ESC values out over Serial.

I will test the NUCLEO-F303RE next in a similar fashion.

@Vicky-S F446 should work fine, it has 2 CAN peripherals. I believe changes should be confined to can.c.

Getting started with CAN bus development can be tedious, if anything in the chain is off you get nothing out, so I would suggest starting with a confirmed working target first to wring out the hardware setup as a start. But if you have known good hardware then go for it. I had a bunch of early problems with counterfeit china sourced FTDI and CAN hardware I had to sort through before my setup worked.

I suggest further discussion move to the forum here.: https://www.stm32duino.com/viewtopic.php?f=10&t=99

@geosmall
Copy link
Contributor

F103C8 and F303CC UAVCAN functional testing now completed for both examples.

@fpistm fpistm mentioned this issue Feb 22, 2020
@pazi88
Copy link

pazi88 commented Mar 3, 2020

Is there any progress on CAN support for STM32? It would be really useful for many projects. The example code from @seeers for example would be enough for me currently, but I can seem to figure out how to use the PA11 and PA12 in stm32f103c8t6 instead of the PB8 and PB9 that the code does remapping of the pins. https://github.com/seeers/CAN-Bus-Arduino_Core_STM32

@fpistm
Copy link
Member

fpistm commented Mar 4, 2020

@pazi88
currently, there is no dedicated task for this on my side.

@xerdink
Copy link

xerdink commented Mar 16, 2020

@pazi88 how did you edit the code in order to use PA11 and PA12?

@pazi88
Copy link

pazi88 commented Mar 16, 2020

@orhaneee See the code in here: https://github.com/pazi88/8Ch-EGT I have included CAN code to mine, but you should be able to make that out. The register settings are slightly different depending what pins you want to use. That all is verified to work.

@nopnop2002
Copy link

@orhaneee
How to remap CAN port for stm32f103:
https://github.com/nopnop2002/Arduino-STM32-CAN/tree/master/stm32f103

@pazi88
Copy link

pazi88 commented Mar 25, 2020

@nopnop2002 Thanks for that! I have tried to figure out how to make CAN work on F4 too, but that will save lot of time.

@nopnop2002
Copy link

nopnop2002 commented Mar 25, 2020

@pazi88
Currently, i am trying to catch the end of transmission / reception by interruption, but it has not been successful yet.

@Elektrik1
Copy link

Any progress on implementing Hardware CAN library?

@J-f-Jensen
Copy link

I have made a working CAN library based on libraries I have found. It should work both with can0 and can1 on devices with two CAN devices, but I have only tested it with CAN0. USB serial cannot be used together with the CAN library on stm32f103 bluepill devices. Link: https://github.com/J-f-Jensen/libraries

@fpistm
Copy link
Member

fpistm commented Oct 8, 2020

Thanks for sharing @J-f-Jensen
Maybe it could be a good candidate to add it as a built-in library or other.
Probably some update would be required to support all series but I guess 90% of the work is done. 👏

@lutfisan
Copy link

any CAN FD support for NUCLEO-G474RE?

@pazi88
Copy link

pazi88 commented Nov 26, 2020

I have made a working CAN library based on libraries I have found. It should work both with can0 and can1 on devices with two CAN devices, but I have only tested it with CAN0. USB serial cannot be used together with the CAN library on stm32f103 bluepill devices. Link: https://github.com/J-f-Jensen/libraries

Nice library, but what I can see it only works on F1. It fails to build on F4.

@J-f-Jensen
Copy link

J-f-Jensen commented Nov 26, 2020 via email

@pazi88
Copy link

pazi88 commented Nov 30, 2020

What error do you get? I can be a issue with the hal config fileBr Jens  -------- Oprindelig besked --------Fra: Pasi Kemppainen [email protected] Dato: 26.11.2020 14.13 (GMT+01:00) Til: stm32duino/Arduino_Core_STM32 [email protected] Cc: J-f-Jensen [email protected], Mention [email protected] Emne: Re: [stm32duino/Arduino_Core_STM32] CAN communication support (#259) I have made a working CAN library based on libraries I have found. It should work both with can0 and can1 on devices with two CAN devices, but I have only tested it with CAN0. USB serial cannot be used together with the CAN library on stm32f103 bluepill devices. Link: https://github.com/J-f-Jensen/libraries Nice library, but what I can see it only works on F1. It fails to build on F4. —You are receiving this because you were mentioned.Reply to this email directly, view it on GitHub, or unsubscribe.

For example there is no AFIO registers in F4. And there was some other errors too.

@fpistm
Copy link
Member

fpistm commented Nov 30, 2020

Hi @pazi88
Which F4 boards you used ? did you defined the HAL_CAN_MODULE_ENABLED ?

https://github.com/stm32duino/wiki/wiki/HAL-configuration#hal-modules-configuration

@shaneyake
Copy link

Does anybody know of a library that will work with the HAL_FDCAN_MODULE_ENABLED?
@J-f-Jensen library is great but I can't get it to compile for my Nucleo-H743IZ2.
or is there a way to use HAL_CAN_MODULE_ENABLED with the Nucleo-H743IZ2?
Thanks

@pazi88
Copy link

pazi88 commented Dec 3, 2020

@shaneyake The library only works for F1 series as far as I know, because that's the only one using AFIO for pin mapping. F4 andvothets have different way to map pins, so that's why it wont compile (as I mentioned above).

@fpistm fpistm mentioned this issue Dec 13, 2021
@phoenixs3
Copy link

any progress on this? would be very useful to have can support...

@pazi88
Copy link

pazi88 commented May 8, 2022

any progress on this? would be very useful to have can support...

You can use this library I created. Uses HAL drivers, hadware interrupts and it's easy to use. Should work with every stm32 chip that is supported by stm32duino: https://github.com/pazi88/STM32_CAN

@pazi88
Copy link

pazi88 commented May 8, 2022

I have been meaning to do some last modifications to that and maybe make a PR for that to be added in stm32core. I have been personally used that with many projects communicating busy vehicle CAN busses and it works great (other ones I have found, have many problems).

@phoenixs3
Copy link

Wow thanks @pazi88 will test tomorrow! Was previously using MBed but it is failing with high bus loads....

@fpistm
Copy link
Member

fpistm commented May 9, 2022

e last modifications to that and maybe make a PR for that to be added in stm32core. I have been personally used that with many projects communicating busy vehicle CAan busses and it works great (other ones I have found, have many problems).

Hi @pazi88, do not hesitate to male a PR or you can also share it as an Arduino library.

@pazi88
Copy link

pazi88 commented May 9, 2022

Wow thanks @pazi88 will test tomorrow! Was previously using MBed but it is failing with high bus loads....

Yes with other libraries available I also had problems with high bus loads, but with this one all seems fine, because both TX and RX side are interrupt based and the packets are gathered to buffers that have configurable size.

@pazi88
Copy link

pazi88 commented May 9, 2022

e last modifications to that and maybe make a PR for that to be added in stm32core. I have been personally used that with many projects communicating busy vehicle CAan busses and it works great (other ones I have found, have many problems).

Hi @pazi88, do not hesitate to male a PR or you can also share it as an Arduino library.

It works already as Arduino library. But I'll do those additions I have been meaning to do and do a PR then.

@ChinaBurn
Copy link

ChinaBurn commented May 25, 2022

There is a working Can instance here, but this is not a library, I have tested it to work on stm32f103c8t6 and use it in production.
https://github.com/nopnop2002/Arduino-STM32-CAN
Recently tried to use stm32f103zet6, but failed. No problem has been found yet, the phenomenon is that Can initialization failed

@wltue
Copy link

wltue commented Jun 19, 2024

https://github.com/nopnop2002/Arduino-STM32-CAN
On a Nucleo F303 works great, only if the RX and/or TX get some noise on the board, CAN initialization fails sometimes (e.g. connecting only a scope and no CAN transceiver connected)
Thanks for posting, helped me out big time!

@fpistm fpistm added enhancement New feature or request and removed New feature labels Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests