sparkyPi
Posts: 33
Joined: Thu May 21, 2015 8:51 pm

wiringPi: Request support for additional SPI mode bits

Sat Oct 10, 2015 6:54 pm

Dear Gordon (wiringPi developer) and community,

I hope it's okay to post a mini "enhancement request" to wiringPi here --- I see Gordon found my other post related to wiringPi so I thought posting here might be helpful to all. :)

I started using SPI library of wiringPi recently and it's working well. Thanks Gordon! As I worked through initial troubles I found Raspberry Pi supports a couple of extra mode configuration bits that can't be set with (undocumented) wiringPiSPISetupMode() function in wiringPi. I hope it is possible to support the extra couple of bits :)

Here are the details:

Raspberry Pi supported SPI mode bits:

Code: Select all

SPI_CPOL    - Clock polarity
SPI_CPHA    - Clock phase
SPI_CS_HIGH - Chip Select active high
SPI_NO_CS   - 1 device per bus, no Chip Select
Definition of mode bits in /usr/include/linux/spi/spidev.h

Code: Select all

#define SPI_CPHA                0x01
#define SPI_CPOL                0x02

#define SPI_MODE_0              (0|0)
#define SPI_MODE_1              (0|SPI_CPHA)
#define SPI_MODE_2              (SPI_CPOL|0)
#define SPI_MODE_3              (SPI_CPOL|SPI_CPHA)

#define SPI_CS_HIGH             0x04
#define SPI_LSB_FIRST           0x08
#define SPI_3WIRE               0x10
#define SPI_LOOP                0x20
#define SPI_NO_CS               0x40
#define SPI_READY               0x80
Raspberry Pi supports mode bit positions: 0 (SPI_CPHA), 1 (SPI_CPOL), 2 (SPI_CS_HIGH) and 6 (SPI_NO_CS). However in function wiringPiSetupMode (wiringPiSPI.c):

Code: Select all

int wiringPiSPISetupMode (int channel, int speed, int mode)
{
<snip>
  mode    &= 3 ;        // Mode is 0, 1, 2 or 3
<snip>
The bit masking will ensure only bits 0 and 1 (i.e. SPI_CPHA and SPI_CPOL) can be set with the 'mode' function argument. This is sufficient for supporting SPI modes 0 to 3 which I think is intent of wiringPi SPI library.

I would like to request minor "enhancement" to allow configuration of mode bits 2 and 6 (SPI_CS_HIGH and SPI_NO_CS, respectively).

I believe replacing above line with the following would do it:

Code: Select all

  mode    &= 0x47 ;
Thanks for your consideration, Gordon! :D

Best,
sparkyPi

Return to “C/C++”