Author Topic: mcu with 3 CAN bus modules  (Read 2664 times)

0 Members and 1 Guest are viewing this topic.

Offline ifrythingsTopic starter

  • Contributor
  • Posts: 15
  • Country: ca
mcu with 3 CAN bus modules
« on: January 27, 2022, 07:56:19 am »
So I built a project that takes 3 CAN bus networks and transfers info between the 3 of networks, I built it with atmega328p, 3 mcp2515 stand alone CAN controllers, 3 tja1050 CAN transceivers and programmed it in arduino. The unit “works” but I’m loosing CAN packets from what I believe is to much overhead servicing the spi bus and 3 CAN networks running at 500kbps.

I’m not an expert programmer, I do this for a hobby and I am self taught, I figured at this point my requirements exceed anything the arduino platform can provide. I’m looking at moving to a different mcu and maybe even a different supplier for a mcu that has at least 3 CAN bus modules.

Anyone have a good choice for which manufacture would have a suitable mcu line up, affordable programmer tools and free development software?
I understand that I’m in for a big learning curve but hoping there is a more newbie friendly chip supplier/ programming software.

ST has a mcu that I think would work for my application https://www.st.com/en/automotive-microcontrollers/spc560b50l1.html#documentation but I’ve never used anything from ST and not sure if their mcu/ide is something worth investing time into or if there’s a better option out there.

Any advice is greatly appreciated!
« Last Edit: January 28, 2022, 05:50:01 pm by ifrythings »
 

Offline fchk

  • Frequent Contributor
  • **
  • Posts: 255
  • Country: de
Re: mcu with 3 CAN bus modules
« Reply #1 on: January 31, 2022, 01:15:45 pm »
If you want to stay with AVR, then there is a more powerful solution:

1. Use an Atmga with External Bus Interface (EBI)
  * Atmega 64/128
  * Atmega 1280/1281
  * Atmega 2560/2561

2. Use NXP SJA1000 CAN Controllers
https://www.nxp.com/docs/en/data-sheet/SJA1000.pdf

3. Connect the SJA1000 to the EBI Bus on Ports A (multiplexed address/data 0-7), C (address 8..15) and G (RD, WR, ALE). You can also add a 32k SRAM to the EBI Bus. And yes, you MUST use the designated EBI pins in order to get decent performance. You must not use Ports A, C and G for anything else.

The reason why this is better is: You don't have the SPI bottleneck. Access to a register of a SJA1000 is a single memory read or write, just as you would write to the internal SRAM.

This article uses one SJA1000 on an obsolete Mega162. You can use the schematics as a starting point. Sorry, German only.

http://www.kreatives-chaos.com/artikel/sja1000-testboard

fchk
« Last Edit: January 31, 2022, 01:17:28 pm by fchk »
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4150
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: mcu with 3 CAN bus modules
« Reply #2 on: January 31, 2022, 02:28:50 pm »
How many messages per second are your looking to route?

You could look into NXP (former freescale) lineup for some CAN parts that will probably be designed for gateway applications.
And Renesas RH850 has a part with 12 CAN channels. But talk about a learning curve, the user manual is 4500 pages.
They probably also have something with less big, but entry level accessibility of those won't be the same as ST..

Anyway, I suspect that if you get a reasonably quick ARM Cortex you can keep doing it with external controllers.

Maybe the RP2040 is worth looking at?
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3523
  • Country: it
Re: mcu with 3 CAN bus modules
« Reply #3 on: January 31, 2022, 06:16:50 pm »
the PIC32MK comes with four can controllers.
do not use the GPD/MCF series (those with CAN, too many bugs across the whole chip).
use the GPK/MCM series (those with CAN FD)
I recommend using harmony for the clock and general chip setup ONLY.
then write your own code for the canbus peripheral, it's not that difficult and the code provided in the reference manual is good.
 

Offline uer166

  • Frequent Contributor
  • **
  • Posts: 965
  • Country: us
Re: mcu with 3 CAN bus modules
« Reply #4 on: January 31, 2022, 07:03:23 pm »
Don't use the SPC5 series unless you have lots of experience and you NEED to have it automotive rated. Get something like a STM32G474 and friends, which is a mainstream MCU with 3 CANs.
 

Offline ifrythingsTopic starter

  • Contributor
  • Posts: 15
  • Country: ca
Re: mcu with 3 CAN bus modules
« Reply #5 on: February 01, 2022, 04:21:47 am »
@fchk thanks for that idea, never knew they made a chip like that, I think it would be better just to use a chip that has several native CAN modules instead. I don’t have to stay with AVR either.

@Jeroen3 I just did a quick count and it looks to be around 1,700 messages per second (for all 3 busses) and that’s not including firmware/scan tool usage I believe the scan tool adds about ~300 more messages per second.

A little about my project, doing an engine swap in a truck and I want the new engine computer to talk to the rest of the truck (new engine is from a different company). What I have done is isolate the truck computer (pcm) from the rest of the truck ( bus 1 goes to pcm, bus 2 to rest of truck) and then bus 3 goes to the new engine computer. I take the pcm messages and pass most of them to the rest of the truck and the truck side passes everything to the pcm. The pcm messages that I need to change get saved to memory and updated from the new engine data and then sent out.

I was looking at a NXP part but it looks like you have to buy their programming environment and that is $1k usd, not really into wanting to spend that kind of money.

That RH850 chip looks pretty powerful, probably overkill for what I need, don’t really need 8-12 CAN buses.

I’m hoping going with a chip that has built in CAN modules will be easier to get up and running then stand alone CAN chips.

@JPortici I found some PIC32MK1024GPE064 in stock the other day and have a few on the way, I hope those aren’t plagued with the issues your mentioning, the errata didn’t show anything majorly wrong with the CAN part besides the wake interrupt flag being set when the module is off.

I was going to get the harmony software to get the chip up and running and use one of there examples to make sure the hardware part is working then go through and modify the example for my needs. Is there something wrong with the harmony generated code that someone new should be worried about?

@uer166 the spc5 chip was just one I found while trying to see what options where out there, if definitely looks like a lot of chip and a waste for what I want to do. Being as this is just a hobby thing I’m not needing any automotive ratings.
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3523
  • Country: it
Re: mcu with 3 CAN bus modules
« Reply #6 on: February 01, 2022, 12:06:59 pm »
@JPortici I found some PIC32MK1024GPE064 in stock the other day and have a few on the way, I hope those aren’t plagued with the issues your mentioning, the errata didn’t show anything majorly wrong with the CAN part besides the wake interrupt flag being set when the module is off.

I was going to get the harmony software to get the chip up and running and use one of there examples to make sure the hardware part is working then go through and modify the example for my needs. Is there something wrong with the harmony generated code that someone new should be worried about?

those are part of the "bad ones"
the problem is not in the can module, but in other essential parts of the chip i.e.:
you NEED to use an external CMOS clock, can't use a crystal
cache is not functional, that kills the performance
I2C is not functional

The problem with harmony it's the problem of all the free (and most non free) code generators: code quality is shit, it's severly bloated, poorly documented and confusing to use. It's good just to write all the boilerplate code required to set up the chip, that's it.
 

Offline HwAoRrDk

  • Super Contributor
  • ***
  • Posts: 1561
  • Country: gb
Re: mcu with 3 CAN bus modules
« Reply #7 on: February 01, 2022, 01:48:50 pm »
Could you give some more details about your current solution?

For example, what clock frequency are you running the ATmega328P at? What SCK frequency are you running the SPI bus at? How are you deciding which messages get relayed and modified (i.e. are you filtering exclusively in code or filtering/masking on the MCP2515s)? Are you using interrupts or polling the MCP2515s?

Also, you mention you "programmed it in arduino". Could be that there are some inefficiencies in using the Arduino framework and/or libraries that could be avoided.

Some quick back-of-the-envelope calculations indicate that at a rate of 2,000 msgs/sec, you have 500 us to deal with each one, or 8,000 CPU cycles when running it at 16 MHz. Difficult to say whether that could be enough or not without knowing more details.
 

Offline AndyC_772

  • Super Contributor
  • ***
  • Posts: 4277
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: mcu with 3 CAN bus modules
« Reply #8 on: February 01, 2022, 02:51:46 pm »
  • STM32F205 has 3 separate CAN controllers. If you need more performance or memory, STM32F405 is pin-for-pin and bit-for-bit code compatible, a rare instance where you really can just drop an upgraded CPU straight into a board designed around a cheaper one
  • Use STM32CubeMX to plan your pinout and clock tree. Create a dummy project and extract the header files that define all the register names. Ignore the rest, don't use any of its auto-generated code or HAL. Read the reference manual carefully, and write to the hardware directly.
  • Rowley CrossWorks - home (non-commercial) licence. It's not free, but it's fast, reliable, 'just works', and is ridiculously good value if you're not making products to sell. Or you can use ST's own free IDE (TrueStudio) if "free" really is non-negotiable.
  • ST 'Nucleo' boards have the debug probe (ST-Link) built in, so you can just plug in a USB cable. The stand-alone ST-Link adapters are cheap too.

Of course, right now you'll struggle to actually buy an STM32<anything>, but that's pretty much par for the course across the board. The Nucleo boards are easier to obtain than individual chips, so you might get lucky, or have to practice with a heat gun.

Offline ifrythingsTopic starter

  • Contributor
  • Posts: 15
  • Country: ca
Re: mcu with 3 CAN bus modules
« Reply #9 on: February 01, 2022, 03:24:23 pm »
@ JPortici the errata shows the primary osc should work with a crystal just finicky if I’m reading it right, second osc can’t use a crystal, if I’m understanding that right. Hopefully I got rev2 chips as they fixed the cache issue. Don’t need the I2C so should be ok there.

https://ww1.microchip.com/downloads/en/DeviceDoc/PIC32MK-GP_MC-Family-Silicon-Errata-DS80000737H.pdf#page6

I’ll have to read more on the CAN registers, didn’t look to bad when I was looking at it earlier.

@HwAoRrDk the atmega328p is running at 16Mhz, using the clock out pin to feed the 3 mcp2515 at 16Mhz, using the interrupt pin from each mcp2515 to the atmega, spi bus is running at 10Mhz. I’m doing all the filtering in software, no masks or filters set, using a switch statement to filter out required messages and passing all others through the default. I can post some code tonight if needed.

 I’m sure the arduino platform and the CAN library I’m using isn’t fully optimized, not sure even if I had optimized code if the atmega could keep up.

@AndyC_772 I can’t find any STM chips right now, I’m only looking on digikey/mouser as I don’t trust eBay or other sites like that to get legit chips.I have no issues robbing chips off other boards so that is a option, I’ll check out the chips and board you mentioned tonght.
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9931
  • Country: us
Re: mcu with 3 CAN bus modules
« Reply #10 on: February 01, 2022, 03:33:51 pm »
The Teensy 4.1 is a 600 MHz device with 3 CAN controllers if you want to use internal peripherals:

https://www.pjrc.com/store/teensy41.html

If you want to use external peripherals, the Teensy will just look like an extremely fast CPU.

It's in stock!
 
The following users thanked this post: Jeroen3

Offline HwAoRrDk

  • Super Contributor
  • ***
  • Posts: 1561
  • Country: gb
Re: mcu with 3 CAN bus modules
« Reply #11 on: February 01, 2022, 04:23:05 pm »
@HwAoRrDk the atmega328p is running at 16Mhz, using the clock out pin to feed the 3 mcp2515 at 16Mhz, using the interrupt pin from each mcp2515 to the atmega, spi bus is running at 10Mhz. I’m doing all the filtering in software, no masks or filters set, using a switch statement to filter out required messages and passing all others through the default. I can post some code tonight if needed.

Ah, you may think the SPI bus is running at 10 MHz, but it won't be. :) The ATmega328P can only run the SPI SCK at certain fractions of the main clock frequency (divide by 2, 4, 8, etc), with a maximum of half the main clock speed. If you have used the Arduino SPI library and specified SPISettings(10000000, ...), what the Arduino library does is try to achieve the nearest SCK speed to what you specify, which in this case will probably be 8 MHz (16 MHz / 2). You'd have to have the AVR running at 20 MHz to get 10 MHz SPI bus. All this will mean your communications with the MCP2515s are not as fast as you think.

I suspected you might be doing the filtering in software. If applicable, try using the message ID filtering and masking features of the MCP2515. I'm not sure whether that may help in a gateway/relay context though, where you want to pass through everything except certain messages. It would certainly be helpful the other way round; the fewer messages the AVR has to see in the first place the better. The MCP2515 can even do filtering on the first two message data/payload bytes (albeit only when using standard 11-bit message IDs).

Make sure you're also using the shortcut commands on the MCP2515 rather than generic READ and WRITE commands. For example, using the dedicated READ STATUS command saves one byte of SPI traffic, because you skip having to specify the register address. If you're using a library for the MCP2515, this may already be done, but maybe not.

Posting code can't hurt - maybe there's something obvious you've missed, or some ways you could re-architect things. :-+
 

Offline HwAoRrDk

  • Super Contributor
  • ***
  • Posts: 1561
  • Country: gb
Re: mcu with 3 CAN bus modules
« Reply #12 on: February 01, 2022, 04:28:21 pm »
The Teensy 4.1 is a 600 MHz device with 3 CAN controllers if you want to use internal peripherals:

This is probably ideal for OP, as it can still be used with the Arduino environment. :-+

There is even a library for the Teensy 4.0/4.1 CAN: https://github.com/pierremolinaro/acan-t4
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4150
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: mcu with 3 CAN bus modules
« Reply #13 on: February 01, 2022, 09:07:00 pm »
@Jeroen3 I just did a quick count and it looks to be around 1,700 messages per second (for all 3 busses) and that’s not including firmware/scan tool usage I believe the scan tool adds about ~300 more messages per second.
Oi, 1700 msg/s is a lot. Many USB CAN interfaces can't even do that... I can see why your atmega32 can't keep up now.
You definetely need something better.

I was looking at a NXP part but it looks like you have to buy their programming environment and that is $1k usd, not really into wanting to spend that kind of money.
If a chip has an ARM core and public datasheet you don't need to buy anything except maybe a Segger J-Link. (and time)

This teensy looks like a great solution, it solves the hard part of getting a feature rich high density freescale chip up and running and on a board while still delivering very high speeds.
And it's not expensive at all. And most of all, the website says "in stock", which is magnificent to see once again.

However, if you're going the STM32 route, I must warn you. bxCAN is a "functional" CAN controller that requires a lot of software intervention.
You will have to find or write your own mailboxing stack, the HAL is not going to do it for you.
 

Offline uer166

  • Frequent Contributor
  • **
  • Posts: 965
  • Country: us
Re: mcu with 3 CAN bus modules
« Reply #14 on: February 01, 2022, 09:43:41 pm »
However, if you're going the STM32 route, I must warn you. bxCAN is a "functional" CAN controller that requires a lot of software intervention.
You will have to find or write your own mailboxing stack, the HAL is not going to do it for you.


Would like to correct that the newer STM32 parts have Bosch CAN FD, not bxCAN, with (seemingly) fully functional mailboxes and FIFOs.
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 8732
  • Country: fi
Re: mcu with 3 CAN bus modules
« Reply #15 on: February 02, 2022, 09:54:02 am »
IMHO, the bxCAN isn't that complicated, I got it running in a day or two IIRC, years ago. There was one documentation error, specifically, from my notes: "contrary to what the manual says, "end initialization" throws us in sleep mode, not normal mode."

With the newer FDCAN (from Bosch indeed), there is some more work, though. Specifically, you have to decide on a memory layout and program it correctly, with all the offset addresses and lengths correctly set. And remember to zero the message RAM after enabling the CAN peripheral clock. I made an Excel spreadsheet (screenshot attached) which calculates the memory offsets and also "generates" the code. More flexibility, but more work, too.
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3523
  • Country: it
Re: mcu with 3 CAN bus modules
« Reply #16 on: February 02, 2022, 11:05:55 am »
I think it's the same used in dsPIC and PIC32 with CAN FD, definetly a lot more work to set it up, understand the memory layout and such. But after that it's nicer to use than the old ECAN module
 

Offline ifrythingsTopic starter

  • Contributor
  • Posts: 15
  • Country: ca
Re: mcu with 3 CAN bus modules
« Reply #17 on: February 02, 2022, 03:26:02 pm »
I have a teensy 4.1 on the way, didn’t really need the 4.1 but it was only 1 week shipping vs 1 month for the 4.0.

@HwAoRrDk That’s good to know about the spi bus, I’ll try a 20Mhz oscillator and see how much it helps mostly for experimenting now. I’m using someone else library so I’ll have to see what they are using for read/write commands. I don’t think filters would work for me in this application as I need to let the entire range of ids through, I will enable it for the new engine to filter out any message >0x700 as that’s all scan tool data that isn’t needed to be seen.

@Jeroen3 From what I have noticed with the atmega328p and one mcp2515 is it appears to keep up with one unfiltered CAN bus as I use that for reading and decoding full vehicle CAN networks. It is possible it misses messages that are sent directly after another message and I never see them but I do have time stamping and time between the same packet ID and never see missed packets (example if ID 0x200 is every 5ms, I never see any packet interval that’s double or triple that time, I do see that with my 3 CAN bus board missing 2 or 3 of the same packet).

Also I couldn’t find a good description/example of the difference between DMA and FIFO, the pic32mk data sheet says the CAN module uses FIFO and doesn’t require DMA, does a FIFO automatically push data into memory without cpu intervention?
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf