Interprocessor communication using SPI

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have two Atmega168's in my system and I want to transfer data between them both ways. I am using an SPI master on one sending commands and data to the other (SPI slave). I cannot get the SPI slave to work right. I have tried both interrupt driven and polling methods in the Slave, but can't get the state machine to work right.

I've even tried using the AVRJTAGICE MKII in DebugWire mode, but it doesn't seem to make sense either. In any case the IPC has to work in real time and as soon as you try to debug it, things go haywire.

Has anyone ever successfully achieved IPC using SPI?

I am sucessfully using the SPI master to also talk to an external FLASH memory chip and an SPI DAC.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've done IPC using TWI. I had to use two computers, two Dragons, and disable any kind of timeout detection on the two systems. It's not easy, and what you really need is a different version of AVR studio (not written!) that allows multiple processors to be debugged. Even then, you'd need two Dragons to make this run.

You need (IIRC) on the slave, SS to be active, because it's used as a chip select on the slave. It's generally driven as an output from the master (if so, then its state does not hold the SPI transmission). IIRC, the SS pin on the slave has to be active low, and if not, the slave will not run since it's not selected.

The same thing holds on the flash and dac, of course. Guess you know that one already.

IIRC, the only way to have this work might be to ignore any 0xFF back unless it's part of data. You might go so far as to send a 2 byte message, with the first byte (sent under active control) as anything other than 0xFF for a valid transmission, then the second byte can be anything.

That might solve the debugging one at a time problem.

Harvey

PS: not sure how spi implements slave not ready. If it were TWI, I could tell you.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Has anyone ever successfully achieved IPC using SPI?

I did ever smoothly implements SPI communication between ATMega8 and ATMega16.
Quote:
I cannot get the SPI slave to work right. I have tried both interrupt driven and polling methods in the Slave

The examples in the datasheet compiles and works great. I'm doubt you're using the datasheet's example.
Please note that example in the datasheet (slave receive) is written for non interrupt usage. If you're using interrupt then you just need to read the SPDR without waiting for SPIF bit to set.
Read this might help:
https://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=56549&highlight=
Usually when doing communication with only one SPI slave device, people just bond up the SS pin to ground so it is always active all the time. For 2 way master slave communication the SS pin between master and slave must be connected each other to accomplish "slaving" each other.

KISS - Keep It Simple Stupid!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I had to use two computers,....AVR studio (not written!) that allows multiple processors to be debugged.
Why not run 2 (or more) AVRstudio windows?

I know, I know some people have troubles just getting 1 to run.... :?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

it certainly is possible. Perhaps some more detail, or even code would help us help you a little better.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is a shot in the dark about what your really trying to do, but should serve as a basic setup idea.

1. You have control over code running in both AVR's( your programming them both).
2. One is a slave, and will responed to commands from the master.

The slave can provide a status byte back when a command is sent, followed by the number of bytes of data, a byte will need to be sent from the master for each byte of data to be received from the slave.

On the slave each time a byte is recived a byte is sent to the master, also another byte is loaded on the slave side to be sent on the following recived byte from master.

Master sends:

0xAE, 0xFF, 0xFF,

Slave sends:
0xAA, 0xBB, 0xEE // status and value from memory.

Your slave state machine should load a byte into the SPI shift register at the end of each byte recived from the master. Also there should be a status byte loaded each time the SS line is set inactive to active. Some of this will depend on your communication protocol.

Time:
-------,slot 1, slot 2, slot 3, slot 4
Master: , 0xAE, 0xFF, 0xFF
Slave: 0xAA, 0xBB, 0xEE

Each time the master sends a byte, a byte is transfered back to master from the slave.

The use of a status byte is for reading if some command is complete, the processor is ready, etc.

Tell us a bit more about your application.

Caleb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JS:

AVR studio oinks up the screen so well that one laptop can't hack it. I only have two computers with two screens each and they're both dedicated to something quite different.

It's actually easier to take an older laptop and run a Dragon off it than to run two instances of AVR studio on the same laptop.

Still a pain to deal with changing sources at both locations, though.

Is running two instances of Studio easier than running one on separate computers? I've generally been rather annoyed at the problems at debugging two systems.

If this is interesting enough, we might start a new thread. Not to hijack the old one, although it is somewhat related.

Harvey