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()
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;
}
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
This is probably a bug in the driver, but BBIO is failing to report an error.