Arduino_Core_STM32 icon indicating copy to clipboard operation
Arduino_Core_STM32 copied to clipboard

CAN communication support

Open Vicky-S opened this issue 7 years ago • 47 comments

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

Vicky-S avatar Jun 02 '18 04:06 Vicky-S

Currently, there is no CAN support

fpistm avatar Jun 02 '18 06:06 fpistm

Ok. will it be available in future?

Vicky-S avatar Jun 02 '18 12:06 Vicky-S

Yes but I can't tell when...

fpistm avatar Jun 02 '18 13:06 fpistm

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

Vicky-S avatar Jun 02 '18 14:06 Vicky-S

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

BennehBoy avatar Feb 11 '19 09:02 BennehBoy

@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 avatar Feb 11 '19 09:02 fpistm

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

jacky4566 avatar Nov 11 '19 18:11 jacky4566

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

fpistm avatar Nov 12 '19 08:11 fpistm

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?

seeers avatar Jan 08 '20 13:01 seeers

@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.

fpistm avatar Jan 08 '20 13:01 fpistm

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

jacky4566 avatar Jan 08 '20 16:01 jacky4566

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/

geosmall avatar Jan 10 '20 10:01 geosmall

Thanks @geosmall for the feedback.

You can also document on the wiki if you want.

fpistm avatar Jan 10 '20 13:01 fpistm

Will it work for STM32F4 Series too? @geosmall

Vicky-S avatar Jan 10 '20 14:01 Vicky-S

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

seeers avatar Jan 10 '20 20:01 seeers

@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 avatar Jan 11 '20 23:01 geosmall

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 avatar Jan 11 '20 23:01 geosmall

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.

geosmall avatar Jan 13 '20 10:01 geosmall

@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.

Vicky-S avatar Jan 14 '20 07:01 Vicky-S

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 avatar Jan 15 '20 10:01 geosmall

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

geosmall avatar Jan 19 '20 16:01 geosmall

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

pazi88 avatar Mar 03 '20 17:03 pazi88

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

fpistm avatar Mar 04 '20 06:03 fpistm

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

xerdink avatar Mar 16 '20 12:03 xerdink

@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.

pazi88 avatar Mar 16 '20 13:03 pazi88

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

nopnop2002 avatar Mar 25 '20 01:03 nopnop2002

@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.

pazi88 avatar Mar 25 '20 06:03 pazi88

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

nopnop2002 avatar Mar 25 '20 07:03 nopnop2002

Any progress on implementing Hardware CAN library?

Elektrik1 avatar Sep 26 '20 21:09 Elektrik1

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

J-f-Jensen avatar Oct 08 '20 16:10 J-f-Jensen