User manual
# read bytes from the selected CBUS device
Response = cbusRd(10) # <- you specify how many bytes to read
# deselect the CBUS device
writePin(somePin, True) # assuming the chip select is active-low
CBUS writes are handled in a similar fashion.
If you are already familiar with CBUS devices, you should have no trouble using these funcons to interface to
external CBUS chips.
A detailed example of interfacing to an external CBUS voice chip may someday be the topic of an applicaon
note.
NOTE – Not all SNAP Engines support CBUS, refer to the SNAP Reference Manual.
Interfacing to external SPI slave devices
SPI is another clocked serial bus. It typically requires at least four pins:
• CLK – master ming reference for all SPI transfers
• MOSI – Master Out Slave In – data line FROM the master TO the slave devices
• MISO – Master In Slave Out – data line FROM the slaves TO the master
• CS – At least one Chip Select (CS)
SPI also exists in a three wire variant, with the MOSI pin serving double-duty.
Numerous opons complicate use of SPI:
• Clock Polarity – the clock signal may or may not need to be inverted
• Clock Phase – the edge of the clock actually used varies between SPI devices
• Data Order – some devices expect/require Most Significant Bit (MSB) first, others only work Least
Significant Bit (LSB) first
• Data Width – some SPI devices are 8-bit, some are 12, some are 16, etc.
You can find more informaon on SPI at hp://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus
The SPI support rounes in SNAPpy can deal with all these variaons, but you will have to make sure the opons
you specify in your SNAPpy scripts match the sengs required by your external devices.
As with what was done for CBUS devices, dedicated SPI support (master emulaon only) has been added to the
set of SNAPpy built-in funcons. Four funcons (callable from SNAPpy but implemented in opmized C code)
support reading and wring SPI data.
In order to support both three wire and four wire SPI, there are more spiXXX() funcons than you might first
expect.
•
spiInit(cpol, cpha, isMsbFirst, isFourWire) – setup for SPI (supporng many opons!)
•
spiWrite(byteStr, bitsInLastByte=8) – send data out SPI
•
spiRead(byteCount, bitsInLastByte=8) – receive data in from SPI (3 wire only)
•
spiXfer(byteStr, bitsInLastByte=8) – bidireconal SPI transfer (4 wire only)
Four-wire SPI interfaces transfer data in both direcons simultaneously, and should use the spiXfer() funcon.
Some SPI devices are write-only, and you can use spiWrite() to send data to them (three-wire or four-wire
hookup).
SNAP® Network Operang System 39