Relaying SPI data and changing one databit?

Dear all, I am experimenting with an ATmega32U4 and I am trying to change one databit in a SPI data transmission, if a certain variable has been set.

The data is being transmitted from a master chip to a slave receiver, using a "CS" to select the destination chip. I would like to ideally cut this dataline and install my ATmega32U4 in between to change the state of the databit, when I want, when it's being transmitted. The data can be slightly delayed without problems.

On each transmisison, it's the last databit that I need to set high.

What I have tried so far in brief, I first experimented using an interrupt to count datapulses (4 microseconds long pulses), and after the second-last pulse, activate an output high to inject a pulse until the CS goes low. "Simple, quick and dirty", but nope, it doesn't seem to work.

Then I found out that the ATmega32U4 actually has SPI capability with the "MISO", "MOSI" and "SCLK" ports. Ahaa. Could I somehow use this to achieve what I'm looking for? Can the data be read in, changed like I want (if the variable is set) and sent on to the receiver?

I have been reading all kinds of examples but so far I have not found what I'm looking for. If someone can be kind enough to give me some hints and suggestions on where to look, what to read, or just what to try, I would really appreciate it.

Thanks,
MarmotteVS

use a microcontroller with two SPI interfaces ?

1 Like

I'm not sure that you can do this using 2 SPI interfaces in the way you suggest - i.e. intercepting bits. If you were to be a "man in the middle" device, and transfer the received byte from the master (you're the slave SPI device on this side) to your master SPI for onward transmission to the actual slave device, then I don't think you can get the answer back from the real slave device across your microcontroller and back out to the real master keeping the timing correct. (That makes sense in my head!)

However, if you were to use a device with 2 SPI busses, then on one side you could be the master to the actual slave device, continually asking it for data. On the other side you appear to the real master device to be the real slave device, responding with the latest data that you've acquired.

Why are you hacking the interface in this way ?

Thanks for the kind suggestion.

@horace Yes, that could indeed be interesting. I was hoping that the ATmega32U4 would be ok, because space is kind of limited. And, the communication is only one-way. To be confirmed...

@markd833
The communication from the "real master" to the "real slave" is only one-way, so (luckily) I don't need to confirm that the data has arrived. (No flow control).

Could I use the input to the ATmega as a middle-man slave receiver, examine the data and send it over the output as a middle-man master to the real slave?

If at all possible, I would like to use the ATmega32U4 that I already have, it has plenty of other code for other functions and the small size is very practical.

@ srnet
The reason I'm doing it this way is that both the "real master" and the "real slave" are proprietary chips where I can't change the hardcoded contents.

Thanks,
MarmotteVS

Are you able to tell us in general terms what the real slave device is/does?

You may be able to use a software SPI implementation on the real slave, saving the SPI hardware interface for the real master.

@markd833 the real slave is a digital volume controller with a selection function on the last bit and the master is a Pioneer proprietary controller chip.

Ok, so nothing particularly fast. You could set your mega32 up as an SPI slave device using the real SPI hardware interface and receive the data from the master. Store that data momentarily and change the bit you want. Then use a software SPI interface (or you may be able to use the shiftOut function) to send the edited message on to the volume controller.

EDIT: you need to check if the phase of clock/data on shiftOut is compatible with your volume controller.

@markd833 , oh yes, that sounds like a very interesting solution. :smiley: :+1: Thanks a lot, I really appreciate it. I will have to give that a try as soon as possible. I will report back how it works.

Cheers,
MarmotteVS

If you find you can't make a software SPI work, here's another option. Arduino based on samd21 chips (Zero, M0 & compatibles) can be configured for 2 hardware SPI. Here is an article describing how to do this (I have not done this myself).

Interesting, thanks a lot. If I can't get the other solution working, this might just (also) be what I need. :slightly_smiling_face:

Cheers,
MarmotteVS

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.