CAN Bus Support

I couldn’t get it up and running on my discovery board, because of limited time. But what i saw so far are two points:

  • you have enabled the interrupt for HAL_NVIC_EnableIRQ(CAN1_RX0_IRQn);, but you havn’t implemented an interrupt handler for it.
    like:
    void CAN1_RX0_IRQHandler(void) { HAL_CAN_IRQHandler(&SimpleCan::hcan); }
    …look into the can examples comming from stm (file ‘stm32f4xx_it.c’) or from generated CubeMX code. For the STM324xG-EVAL board they offer some CAN samples with interrupts.

  • second thing is that you have to make hcan static (static CAN_HandleTypeDef hcan;) otherwise you can not access it from the interrupt handler (don’t forget the static initializer CAN_HandleTypeDef SimpleCan::hcan = { };).

Hope this brings you on the right track. Have fun!

1 Like

@JorgeMaker - I have interrupts working for HAL_CAN (stm32f405 vesc board). I’ve also sorted out CAN speeds which unlike FDCAN you have to work out prescalers and time segments for. Pain :frowning: Unfortunately the speeds are going to be dependent on your PCKL1 freq (42MHz for me) - so might need replacing.

The interface is a little different to @erwin74. Perhaps we can work towards a common abstraction?? I’ve looked at esp32 as a 3rd CAN abstraction but it’s newer API assumes you are using tasks/threads I think. Challenging!

If we had an abstraction then we could write a SimpleFOC CanCommander which uses this abstraction and is aware of SimpleFOC config and actions. This would allow can bus message such as “set Node 4’s velocity P term to 4” or “what is the velocity of Node 3”. The commander would receive these messages and know what todo with BLDCMotor to set or obtain the info.

3 Likes

Hi @Owen_Williams,

Greate news :slight_smile:

I will test your implementation as soon as I receive a new STM32F405RGT6, it’s on its way from China :slight_smile: Unfortunately, I destroyed my MCU due to an ESD event caused by allowing more current than my power supply could handle.

I really like the interface you have implemented, it is not that different from the one @erwin74 implemented. It would be great to develop a top layer that uses this interface and is able to act on SimpleFOC. If I have time this weekend and I will try to make a proposal.

Hi Can-Experts of the SimpleFOC community!

Thanks to the code of @Owen_Williams and @erwin74, I got bi-directional CAN working. For my setup I am using an Arduino Uno with a MCP2515 and the B-G431B-ESC1.

Everything works perfect with the standard code from Github. But when I want to combine my previous SimpleFOC code, I get many problems:

  • Only thing I add: Serial.print(“test”); in the loop and the codes stops working.
  • Or once I add the SimpleFOC library, it already stops working after BLDCMotor motor = BLDCMotor(11);
  • Or if I send more than two bytes to the standard code, it will also stops working.

I know that these are quite specific questions! So I am not looking for a precise answer. I want to ask advice how to solve these kind of things! Especially the first (Serial) and second point (Adding SimpleFOC code). Normally I can solve some problems but when adding one simple line of code and the whole board stops working (I always check with a led because Serial can sometimes be inconsistent with this board!), than I am quite lost!

Does someone have tips to help me further? :slight_smile:

Greetings,

Wittecactus

@Wittecactus - did you get past your CAN/Serial issues? I think on another thread you may have got more stability by using Arduino IDE. Does this mean you’re happy with your CAN situation?

Hi @Owen_Williams,

Thanks for your reply, I really appreciate that!

I forgot to update this reply! Arduino IDE did the trick for me. SimpleFOC code is already implemented and I am hoping to get the first leg movements in the coming week :slight_smile:.

The only thing I could not fix is that I can not send more than two bytes. When I send more than two bytes, the whole board crashes. For now it is fine because I convert a float into an integer and the bytes of this integer are than send via the CAN. But I am still curious why this is the case!

I think that it could maybe be related to the bit of the Data length code function.

1 Like

Looking forward to you ‘showing a bit of leg’. :slight_smile:

I know some stm32 peripherals can’t be used at the same time. Does the Serial work in a simple sketch but stop working when you do CAN?

Since switching back to arduino IDE. Serial does work in a simple sketch but does also work combined with CAN code! :slight_smile:

Analyze the content of the platformio.ini file …, most likely the differences come from there.