You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.4 KiB
53 lines
1.4 KiB
7 years ago
|
import gex
|
||
|
|
||
|
class SPI(gex.Unit):
|
||
|
"""
|
||
|
SPI master direct access
|
||
|
"""
|
||
|
|
||
|
def _type(self):
|
||
|
return 'SPI'
|
||
|
|
||
7 years ago
|
def query(self, slave:int, tbytes, rlen:int, rskip:int=-1, confirm=True):
|
||
7 years ago
|
"""
|
||
|
Query a slave.
|
||
|
|
||
|
If rskip is -1 (default), the tbytes length will be used.
|
||
|
Set it to 0 to skip nothing.
|
||
7 years ago
|
|
||
|
slave is 0-based index
|
||
7 years ago
|
"""
|
||
|
if rskip == -1:
|
||
|
rskip = len(tbytes)
|
||
|
|
||
|
pb = gex.PayloadBuilder()
|
||
|
pb.u8(slave)
|
||
|
pb.u16(rskip)
|
||
|
pb.u16(rlen)
|
||
|
pb.blob(tbytes)
|
||
7 years ago
|
|
||
7 years ago
|
# SPI does not respond if rlen is 0, but can be enforced using 'confirm'
|
||
7 years ago
|
if rlen > 0:
|
||
7 years ago
|
resp = self._query(0x00, pb.close())
|
||
7 years ago
|
return resp.data
|
||
|
else:
|
||
|
# write only
|
||
7 years ago
|
self._send(0x00, pb.close(), confirm=confirm)
|
||
7 years ago
|
return []
|
||
|
|
||
7 years ago
|
def write(self, slave:int, tbytes, confirm=True):
|
||
7 years ago
|
"""
|
||
|
Write with no response received
|
||
|
"""
|
||
7 years ago
|
self.query(slave, tbytes, rlen=0, rskip=0, confirm=confirm)
|
||
7 years ago
|
|
||
7 years ago
|
def multicast(self, slaves, tbytes, confirm=True):
|
||
7 years ago
|
"""
|
||
|
Write with multiple slaves at once.
|
||
7 years ago
|
Slaves is a right-aligned bitmap (eg. pins 0,2,3 would be 0b1101), or a list of active positions
|
||
7 years ago
|
"""
|
||
|
pb = gex.PayloadBuilder()
|
||
7 years ago
|
pb.u16(self.pins2int(slaves))
|
||
7 years ago
|
pb.blob(tbytes)
|
||
7 years ago
|
self._send(0x01, pb.close(), confirm=confirm)
|