Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 08:54 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : PIC18F SPI and CAN Ideas....

Author Message
retepsnikrep

Senior Member

Joined: 31/12/2007
Location: United Kingdom
Posts: 134
Posted: 09:22am 08 Oct 2020
Copy link to clipboard 
Print this post

I'm building a MITM 'Man in the Middle' CAN - CAN device to manipulate some CAN data.

I'm happy working with pic CAN rxd and txd and am using 2 x pic18F2680 32mhz joined with hardware SPI Master/Slave 8mhz for prototype testing.

The hardware can be easily upgraded to a 64mhz 18F26K80 and 16mhz SPI bus later once I have a sensible software strategy.

Basically the hardware setup is as follows. (This all works)

CANBUS1 <<<>>> PIC18F2680 <<< SPI 8mhz >>> PIC18F2680 <<<>>> CANBUS2

Whatever is received on CANBUS1 needs to be sent out on CANBUS2 and vice versa.

There is even a Heath Robinson video of it working if you have a few minutes..

https://youtu.be/9oTvZ-2dbOk

As described this does operate quite well, but i'm dropping a few CAN packets and am after some ideas on the best/fastest software strategy..

I can poll flags or use Interrupts on the CAN and SPI modules.
I could add a ring buffer at both ends maybe for RX and TX.

I've read up on the CAN and SPI errata for the PIC and done a lot of reading and that all seems to be working.
The SPI is currently configured as Master & Slave...
I could add more handshaking lines between the pics as there is a lot of spare IO if that would help throughput.

Now packets on the two CAN busses might not arrive at the same time or intervals/frequency. How to deal with that?

Maybe I should join two complete ports together on the pics and clock out data a byte at a time?
I could join PIC ports A directly to each other with a 1k current limit resistor on each line to prevent direct shorts.
Would that be quicker/easier than the hardware 8mhz SPI?

Anyone have any other cunning ideas?

Basically at the moment my process is run the SPI bus as fast as possible between the PICS,
and then as data appears on the CANRX busses (poll or interrupt detected) drop it onto the SPI to clock it across to the other PIC for onward transmission.

Thanks in advance for ideas..
Gen1 Honda Insights.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 09:38am 08 Oct 2020
Copy link to clipboard 
Print this post

With 8MHz SPI on a 32MHz processor you have very few instructions available for each character (best case 32 and probably less). This seems to rule out interrupt use. Lowest instruction count is probably 8-bit parallel + clock (plus a parity bit?) but even there you are lacking much time for any sort of error detection/correction. If the transmission is discontinuous and end-to-end transit time isn't an issue then ring buffers can certainly help
 
retepsnikrep

Senior Member

Joined: 31/12/2007
Location: United Kingdom
Posts: 134
Posted: 10:32am 08 Oct 2020
Copy link to clipboard 
Print this post

Thanks for the input Peter.

The CAN bus is not fully loaded so I think I might have cracked it for now.

I added a control line from the Slave to the Master to basically say 'Hold on Boss i'm not ready'.

This keeps the SPI in sync and means CAN packets are no longer getting missed.

I'm going to load up both sides of the CAN bus with a load more packets and see if it falls over.

I realise time is tight.

I need to add a counter to see how many cpu cycles are unused at present?
Not sure how to do that...

This is it working.. for now..

https://youtu.be/KFAdIyS6QOk
Gen1 Honda Insights.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 10:44am 08 Oct 2020
Copy link to clipboard 
Print this post

  Quote  I need to add a counter to see how many cpu cycles are unused at present?
Not sure how to do that...

Put NOPs in until it stops working?
 
vegipete

Guru

Joined: 29/01/2013
Location: Canada
Posts: 1132
Posted: 04:55pm 08 Oct 2020
Copy link to clipboard 
Print this post

I would suggest moving up the PIC spectrum a bit.

There are a bunch of PIC24/dsPIC33 devices with dual CAN.
For example, the dsPIC33CH256MP505 has 2 CAN modules, DMA, runs at 100MHz and costs less than 2 18F26K80s.
Hmm, reading the data sheet, it is a dual core chip, with 90/100 MIPS performance. Might be a handful.

Something like a dsPIC33FJ64GP706A or even a dsPIC33EP128GS804 would be less of an architectural change from the PIC18s.

All of these work at 3.3 volts, not 5 if that matters. A dsPIC30F5011 can run at 5 volts, but I would avoid such a dinosaur.
Visit Vegipete's *Mite Library for cool programs.
 
retepsnikrep

Senior Member

Joined: 31/12/2007
Location: United Kingdom
Posts: 134
Posted: 05:59pm 08 Oct 2020
Copy link to clipboard 
Print this post

Thanks..

Unfortunately I can't move up very far as I use the Pic Basic Pro Compiler and have done for years. I'm limited by the devices it supports for now.

I can't face learning C or something..
Gen1 Honda Insights.
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025