I am using Pi4J to write programs on my Raspberry Pi. The Raspi is supposed to communicate with an Arduino Uno via SPI. It works pretty good, but there are only two SpiChannels so there are also only two Chip-Select-Pins CS0 and CS1. I was wondering if I can also use Gpio-Pins as Chip-Select, but the method 'SpiFactory.getInstance' expects a SpiChannel-obejct.

Is there any way to "convert" a GpioPin into a SpiChannel or a way to create an instance of SpiDevice without choosing a SpiChannel? (then I could control the Gpio-Chip-Select manually)

I hope you can help me and sorry for my bad english :)

  • What you want to do is called "bit banging". If the library doesn't allow you to set up the SPI with an arbitrary pin, you'll have to find software that does allow it, or write your own. – stevieb Jul 20 '17 at 21:56
  • Have you found any solution? – LiTTle Jun 1 '18 at 19:18

I used a GPIO as CS pin.

val pinCS = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_29, "CS")

I am using the MCP23S17GpioProvider class but this class always needs an SpiChannel. I am setting it to SpiChannel.CS0 just to avoid any exceptions.

val provider = MCP23S17GpioProvider(MCP23S17GpioProvider.ADDRESS_0, SpiChannel.CS0)

I successfully done it by setting to low the CS pin before the job and after the job is done to set it to high.

// select the device

do the job you have to do

// deselect the device

This is not the right way to do it, because I use (for Exception reasons) the CS0 to the MCP23S17GpioProvider contructor.

I am also asking the author of Pi4J for more info here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.