BBIO SPI:

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
mikeduffy
 
Posts: 16
Joined: Mon May 19, 2014 4:51 pm

BBIO SPI:

Post by mikeduffy »

Here is my test program (spibug.py)

Code: Select all

#!/usr/bin/python

from Adafruit_BBIO.SPI import SPI

bus = 0
device = 0

# open the specified SPI bus/device
print "Opening SPI(%d, %d)..." % (bus, device)
spi = SPI(bus, device)

print "OK"

# configure the SPI interface
print "Set mode=0"
spi.mode = 0                # base value of clock is 0 (CPOL = 0), data captured on rising edge (CPHA = 0)
print "Set cshigh=False"
spi.cshigh = False          # the AS1116 LD signal is active low
print "Set threewire=False"
spi.threewire = False       # SI/SO are not shared
print "Set lsbfirst=True"
spi.lsbfirst = True         # AS11156 data sheet
print "Set loop=False"
spi.loop = False            # not looped back
print "Set bpw=16"
spi.bpw = 16                # driver hold SS low for this many bits (we don't actually care)
print "Set msh=4"
spi.msh = 4                 # AS1116 supports a 10 MHz SPI
print "Close"
spi.close()
I modified the source code of spimodule.c to add some debugging printfs to __SPI_set_mode() (code is slightly modified, e.g. use of else-if to id failure and set test=0):

Code: Select all

static int __SPI_set_mode( int fd, __u8 mode) {
    __u8 test = 0;
    int status = 0;
    char * msg = "OK";
    printf("__SPI_set_mode(%d, 0x%02X)...", fd, mode); fflush(NULL);
    if (ioctl(fd, SPI_IOC_WR_MODE, &mode) == -1) {
        PyErr_SetFromErrno(PyExc_IOError);
        msg = "SPI_IOC_WR_MODE";
        status = -1;
    }
    else
    if (ioctl(fd, SPI_IOC_RD_MODE, &test) == -1) {
        PyErr_SetFromErrno(PyExc_IOError);
        msg = "SPI_IOC_WR_MODE";
        status = -1;
    }
    else
    if (test != mode) {
        msg = "mode not set";
        status = -1;
    }
    printf("__SPI_set_mode returns %d (%s 0x%02x)\n", status, msg, test);
    return status;
}
Here is the output:

Code: Select all

# ./spibug.py
Opening SPI(0, 0)...
OK
Set mode=0
__SPI_set_mode(3, 0x00)...__SPI_set_mode returns 0 (OK 0x00)
Set cshigh=False
__SPI_set_mode(3, 0x00)...__SPI_set_mode returns 0 (OK 0x00)
Set threewire=False
__SPI_set_mode(3, 0x00)...__SPI_set_mode returns 0 (OK 0x00)
Set lsbfirst=True
__SPI_set_mode(3, 0x08)...__SPI_set_mode returns -1 (SPI_IOC_WR_MODE 0x00)
Set loop=False
__SPI_set_mode(3, 0x08)...__SPI_set_mode returns -1 (SPI_IOC_WR_MODE 0x00)
Set bpw=16
Set msh=4
Close
It appears that the ioctl is not honoring a request to set either SPI_LSBFIRST or SPI_LOOP.

This is probably a bug in the driver, but BBIO is failing to report an error.

mikeduffy
 
Posts: 16
Joined: Mon May 19, 2014 4:51 pm

Re: BBIO SPI:

Post by mikeduffy »

Here's a better test (checks all mode bits)

Code: Select all

#!/usr/bin/python

from Adafruit_BBIO.SPI import SPI

bus = 0
device = 0

# open the specified SPI bus/device
print "Opening SPI(%d, %d)..." % (bus, device)
spi = SPI(bus, device)

print "OK"

# configure the SPI interface
print "Set mode=0"
spi.mode = 0           

print "Set mode=1"
spi.mode = 1       

print "Set mode=2"
spi.mode = 2     

print "Set mode=3"
spi.mode = 3      

print "Set cshigh=True"
spi.cshigh = True

print "Set threewire=True"
spi.threewire = True 

print "Set lsbfirst=True"
spi.lsbfirst = True  

print "Set loop=True"
spi.loop = True    

print "Set bpw=16"
spi.bpw = 16 

print "Set msh=4"
spi.msh = 4  

print "Close"
spi.close()
And the output

Code: Select all

# ./spibug2.py 
Opening SPI(0, 0)...
OK
Set mode=0
__SPI_set_mode(3, 0x00)...__SPI_set_mode returns 0 (OK 0x00)
Set mode=1
__SPI_set_mode(3, 0x01)...__SPI_set_mode returns 0 (OK 0x01)
Set mode=2
__SPI_set_mode(3, 0x02)...__SPI_set_mode returns 0 (OK 0x02)
Set mode=3
__SPI_set_mode(3, 0x03)...__SPI_set_mode returns 0 (OK 0x03)
Set cshigh=True
__SPI_set_mode(3, 0x07)...__SPI_set_mode returns 0 (OK 0x07)
Set threewire=True
__SPI_set_mode(3, 0x17)...__SPI_set_mode returns -1 (SPI_IOC_WR_MODE 0x00)
Set lsbfirst=True
__SPI_set_mode(3, 0x1F)...__SPI_set_mode returns -1 (SPI_IOC_WR_MODE 0x00)
Set loop=True
__SPI_set_mode(3, 0x3F)...__SPI_set_mode returns -1 (SPI_IOC_WR_MODE 0x00)
Set bpw=16
Set msh=4
Close
Looking at the driver source (spi.c and spi-omap2-mcspi.c), this appears to be correct (at present).

mikeduffy
 
Posts: 16
Joined: Mon May 19, 2014 4:51 pm

Re: BBIO SPI:

Post by mikeduffy »

It also appears that attempting to set these SPI modes causes random failures elsewhere. For example, I got "impossible" errors from the serial module which disappeared after commenting out the mode setting code.

Locked
Please be positive and constructive with your questions and comments.

Return to “Beagle Bone & Adafruit Beagle Bone products”