Communication with Raspberry Pi over A5/6/7

Is there some way I can communicate between my Raspberry Pi Zero and my Photon over analog pins 5, 6 and 7? I need to use these pins since I have a PCB made which limits my options for using the proper pins for the built in I2c/Serial/SPI functions of the Photon.

How well can any of these communication protocols be done in software on the Photon? Is there a better way that I’m not thinking of? I do not have vast amounts of data to transfer between the two devices, most of the time no update needs to be made on the photon and I could just send a ‘no_update’ packet. Other than that I shouldn’t need to send more than 10 -20 bytes at a time.

@Steven4755, using software SPI works very well actually. Of course not as fast as hardware SPI but quite fast nonetheless. If the Photon will be a dedicate slave (?) then three pins will be fine (MOSI, MISO and CLK) since the Slave Select line is not needed. If you need some code snippets, let me know. :wink:

3 Likes

Great news that you can do SPI without the 4th line. If you want to post some snippets that’d be great but I’m sure I can google up some other implementations if not.

1 Like

There are some code snippets for using SPI in the docs, here:

https://docs.particle.io/reference/firmware/electron/#spi

Thanks for the link, but the built in SPI functions only allow for certain pins to be used and I already am using them. Unless I’m missing a section on using non-default ports then I cant use this.

@Steven4755, here is a Software SPI snippet that fully emulates the SPI.transfer() call, writing AND reading from the SPI port simultaneously:

void begin(void) 
{
	/* Configure Software SPI */
	pinMode(_cs, OUTPUT);
	digitalWrite(_cs, HIGH);

	pinMode(_clk, OUTPUT);
	pinMode(_mosi, OUTPUT);
	pinMode(_miso, INPUT);
}

uint8_t SPItransfer(uint8_t x) {
    uint8_t reply = 0;
    for (int i=7; i>=0; i--) {
      reply <<= 1;
      pinResetFast(_clk);
      digitalWriteFast(_mosi, x & (1<<i));
      pinSetFast(_clk);
      if (pinReadFast(_miso)) 
		reply |= 1;
    }
    return reply;
}

:wink:

1 Like

@Steven4755, the snippet assumes that the Particle device is a Master device. If the rPi is the Master, then the code will not work. Just a reminder that the extra CS pin was omitted assuming the Photon and the rPi have a dedicated bus (no other SPI devices).