xfer2 SPI AdafruitBBIO and 16 bpw functionality

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
cachro2
 
Posts: 2
Joined: Fri Jan 12, 2018 2:13 pm

xfer2 SPI AdafruitBBIO and 16 bpw functionality

Post by cachro2 »

Hello,

My question is regarding SPI for the Adafruit's BBIO library: https://github.com/adafruit/adafruit-be ... fruit_BBIO

We upgraded to Adafruits BBIO since we upgraded to Debian 9.X where we cannot use PyBBIO. Overall, the library is fantastic, there is just one point of confusion regarding the xfer2 function for SPI and setting bpw to 16. Our PyBBIO read/write/multi-read/multi-write functions were based on 16bits per word instructions to the device. Examples of a read and write to register below:

Code: Select all

def read_adxl_register(reg_number):
    spi_read_data = bbio.SPI0.transfer(0, [(((reg_number << 1) | 0x01) << 8)])
    return spi_read_data[0] & 0x00FF

def write_adxl_register(reg_number, reg_value):
    spi_read_data = bbio.SPI0.transfer(0, [(reg_number << 9) | reg_value])
    return spi_read_data[0]
The equivalent of this piece of code using 8bits/word using the Adafruit BBIO library (verified the functionality) was:

Code: Select all

def read_adxl_register(reg_number):
    spi_read_data = spi.xfer2([((reg_number << 1) | 0x01), 0x00]) #R/W set to 1 for read
    return spi_read_data[1]

def write_adxl_register(reg_number, reg_value):
    spi_read_data = spi.xfer2([(reg_number << 1), reg_value]) #R/W set to 0 for write
    return spi_read_data[1]
However, we'd like to back to using 16 bits per word since we have an extensive FIFO multi-byte driver written in this format, which is easy enough to set using the library spi.bpw = 16. However we cannot get a simple read to function using 16bpw as it does using 8bpw.

Code: Select all

def read_adxl_register(reg_number):
    spi_read_data = spi.xfer2(0, [(((reg_number << 1) | 0x01) << 8)])
    return spi_read_data
Can someone please provide an explanation of how to use the 16bpw setting and xfer2 function? I would assume setting bpw to 16 would then cause xfer2 to accept 16 bit hex values as well as return 16bit results in a list, if I am not mistaken..

As an example, with bpw set to 8, reading register 0x01 as shown below

Code: Select all

print(spi.xfer2([ ((0x01 << 1) | 0x01), 0x00]))
Yields, [0,0x1D] as response. I cannot get the same simple example to work with 16 bpw.

The other SPI settings are set as follows:
spi = SPI(1,0)
spi.mode = 1
spi.msh = 5000000 # default is 16000000 (16Mhz)


Thanks,
Robert

User avatar
cachro2
 
Posts: 2
Joined: Fri Jan 12, 2018 2:13 pm

Re: xfer2 SPI AdafruitBBIO and 16 bpw functionality

Post by cachro2 »

For those interested, this is a known issue highlighted here:
https://groups.google.com/forum/#!topic ... -PUIARH8Pk

Reviewed the code and it seems it is indeed casting output list object of the xfer2 function incorrectly.
I have decided to go ahead and pull 8 byte values and manually reconstruct the 16 bit words to maintain compatibility with our current sensor driver.

Regards,
Robert

User avatar
drewfustini
 
Posts: 944
Joined: Sat Dec 26, 2015 1:19 pm

Re: xfer2 SPI AdafruitBBIO and 16 bpw functionality

Post by drewfustini »

Thanks for following up. I maintain the Adafruit_BBIO library. Would you be able to raise a pull request with your fix? I'd like to make sure that other people are able to avoid this problem.

Thanks,
Drew

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

Return to “Beagle Bone & Adafruit Beagle Bone products”