[AVR] programming two AVR's that are connected together on the SPI bus - Page 1
Forum section: electronics / general

author: Simon
author_rank: Unknown
timestamp: December 19, 2015, 08:36:03 am
content: I am making myself a development board to play with the spi bus and later CAN. To start with I want to experiment with SPI between 2 AVR's, but that means connecting their SPI pins together, the same pins that are used to program the devices.I think this will work providing I only hold in reset the one that I am programming. Is this correct ?

author: Psi
author_rank: Super Contributor
timestamp: December 19, 2015, 08:39:48 am
content: If you use 3x ~1k resistors to link clk/miso/mosi between mcus it will be fine. The programmer can connect direct and pull the pins either way. Even if the other mcu is active and trying to push data.If you want to get fancy you can always use a tristate buffer in between them.

author: Simon
author_rank: Unknown
timestamp: December 19, 2015, 08:43:55 am
content: so long as I only reset the chip I am programming presumably or I'll be trying to program both at once.

author: rs20
author_rank: Super Contributor
timestamp: December 19, 2015, 08:51:17 am
content: Actually an interesting question -- you think, those 1k resistors wouldn't be necessary if I could disable the drivers on the non-being-programmed MCU. How do you disable the drivers? One way is to hold the chip in reset -- oh wait..It's also annoying that the SPI pins switch direction based on master vs slave. If they had SDO and SDI pins, there again wouldn't be a need for 1k resistors.Gah, there must be a better way!Quote from: Simon on December 19, 2015, 08:43:55 amYes, that's implied

author: Psi
author_rank: Super Contributor
timestamp: December 19, 2015, 08:51:54 am
content: If you have the resistors in series you cant have one programming interface for both chips, since the programmer will need to go through the resistors in one of the directions.Is your idea to short MOSI MISO CLK to each mcu and also to the programmer then put a two way switch on the reset pin?The issue with this is that the mcu not in reset may try to output data during programming.You could have two "enabled" line between them (active low) and enable pullups.This way if a mcu is in reset the other mcu can detect it and set its spi pins tristate.Edit,thinking about it some more, i think you will have problems with that.Since MOSI / MISO will be around the wrong way for one of the mcus to be programmed.

author: Simon
author_rank: Unknown
timestamp: December 19, 2015, 08:56:18 am
content: Actually don't think the whole idea will work, if I connect the two AVR's together I will have a problem when I also connect the CAN interfaces that will enable the 2 AVR's to talk to each other on CAN as then both AVR's need to be masters to drive their own slave CAN controllers and there will be collisions on the SPI bus. I need to take this one step at a time. Maybe have a programming header per chip and put the 1K resistors in the bus lines so that the programmer can over ride the data from the other chip, not that there should be any.

author: Simon
author_rank: Unknown
timestamp: December 19, 2015, 09:02:00 am
content: Quote from: Psi on December 19, 2015, 08:51:54 amwhy ? I thought on SPI all the MOSI are connected together and all the MISO are connected together, as the system can be multi master, although my problem will come later when I want 2 masters at once, I think I'll just put a jumper between themso that I can break the SPI link and then each AVR can become a master with it's own bus to drive the CAN interfaces.

author: Psi
author_rank: Super Contributor
timestamp: December 19, 2015, 09:05:45 am
content: I have a solutionUse one of the AVRs that doesnt have the programming interface on the SPI lines.Some have it on other pins, i forget whichSolves all the issues.Quote from: Simon on December 19, 2015, 09:02:00 amTrue, yeah if your system will be multi master it wont matter.

author: AndreasF
author_rank: Frequent Contributor
timestamp: December 19, 2015, 09:13:08 am
content: I would use "jumpers" to bridge the three SPI lines. During programming you can take them off and only connect to the side you're programming.Multi-master SPI? I'm sure it's possible but seems complicated to do regarding arbitration.

author: Psi
author_rank: Super Contributor
timestamp: December 19, 2015, 09:17:00 am
content: You could just slap a 3rd mcu in there as a SPI hub (GPIO passthrough)Have it as the hub for all SPI nodes, A B and the programmer. Give it control of the reset lines for A and B, then just have switches on it for what you want to do/program.You could include reprogramming MCU-C itself as well, just use its own mosi/miso pins to connect to the programmer. It can use them as GPIO when doing passthrough and reprogramming A or B.If you connect up its reset line to the programmer you will reprogram the hub.Gives you flexibility if you want to add more SPI stuff later.Only disadvantage (other than extra mcu) is you probably wont be able to run the MCUA - MCUB SPI coms at max clock rate, since the pass through will add some delay if its done in software.

author: miceuz
author_rank: Frequent Contributor
timestamp: December 19, 2015, 09:44:02 am
content: I have used 4053 in this exact case to control the direction of the clock signal.I have two AVRs - slave and master communicating on SPI bus. During normal operation SCK lines of both are connected via 3k6 resistor, but when in reset, another line controlls which SCK line gets connected to programming SCK and which one grounded.

author: Simon
author_rank: Unknown
timestamp: December 19, 2015, 12:15:22 pm
content: Quote from: AndreasF on December 19, 2015, 09:13:08 amI will do this, because then I can split the bus in two for when I want each AVR to be the master controlling it's own CAN chip (MCP2515) as well. This is just for experimenting so no need to go to much length. I need to learn to use both SPI ans CAN so am trying to put both on one vero board that I can carry around with me (yes relatives, here I come at christmas with something to do when I'm suppose to be visiting you, just need my board, laptop and programmer).What sort of frequency does SPI cope with over shoddy vero board wiring, if I had time I'd order PCB's

author: Simon
author_rank: Unknown
timestamp: December 19, 2015, 07:40:10 pm
content: My efforts so far attached. Idea is to just have something to use as a test bed for SPI and CAN coms with regular reporgramming so that I can just have a few control options on buttons and feedback from an LED.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: December 19, 2015, 09:44:01 pm
content: I'd suggest one common pin which is used as open collector. One or both MCUs assert passive pull-up ('H') (or an external resistor is used), and each one checks pin state before asserting hard low ('0'). If it's already low, the other one must be talking, so hold on.Tim

author: Simon
author_rank: Unknown
timestamp: December 19, 2015, 09:47:00 pm
content: I may have run out of pinsIt's just for some initial test. once i know what I'm doiung I'll probably get a few PCB's spun.

author: commie
author_rank: Frequent Contributor
timestamp: December 19, 2015, 09:59:04 pm
content: Quote from: Simon on December 19, 2015, 09:47:00 pmHi Simon,Quick question, given you reside in the U,K. like me, who manufactures your pcb's for you? The reason I ask is I'm looking for a viable company to make a few pcb's for me. Also, is there any chance you could give me a very average price for 160x100mm board ss and ds. Thanks ever so much.CheersCommie

author: dgtl
author_rank: Regular Contributor
timestamp: December 19, 2015, 10:09:16 pm
content: Been there, done that, in 2007. My solution had a single programming connector with an extra pin.1) The board had a jumper, that was used to select the reset line going to first or second AVR. Rest of the lines (MISO, MOSI, SCK) were connected to both.2) The extra pin went to each AVR. The FW of both AVRs checked these pins on boot and if they were pulled low, the mcu FWs stayed in infinite loop and did not initialize the SPI pins as outputs. On the programming plug, this pin was shorted to GND. So when the programmer was plugged in, neither of the uc's started to boot. This way, nothing messed with the programming operation.

author: obiwanjacobi
author_rank: Super Contributor
timestamp: December 20, 2015, 08:07:28 am
content: Quote from: Simon on December 19, 2015, 08:36:03 amDoes it have to be SPI? A synchronous USART connection should work too...Otherwise have each AVR with its own ISP 6pin connector and connect them via a flatcable, you have to take off to do the programming...?

author: Simon
author_rank: Unknown
timestamp: December 20, 2015, 08:21:20 am
content: Quote from: obiwanjacobi on December 20, 2015, 08:07:28 amYes because the goal is to learn to control the MCP2515 via SPI having mastered the SPI bus, so I need to first learn and verify SPI function between two AVR's and then break the bus in two and start trying to control the CAN interfaces that will then connect the two AVR's

author: Simon
author_rank: Unknown
timestamp: December 20, 2015, 08:23:59 am
content: Quote from: commie on December 19, 2015, 09:59:04 pmPCB cart, I've also used techbridge circuits who are pricier but excellent and very very helpful.