Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 ·...

26
Serial Communication -SPI Raspberry Pi Python SPI Library Electronics Serial Communication-SPI Terry Sturtevant Wilfrid Laurier University October 3, 2018 Terry Sturtevant Electronics Serial Communication-SPI

Transcript of Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 ·...

Page 1: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

ElectronicsSerial Communication-SPI

Terry Sturtevant

Wilfrid Laurier University

October 3, 2018

Terry Sturtevant Electronics Serial Communication-SPI

Page 2: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

Serial Communication -SPI

Serial Peripheral InterfaceMaster/slave communicationUses 3 signals (and Ground),MISO, MOSI, SCLKand chip selects for each slave deviceSynchronous, so master controls clock rate

Terry Sturtevant Electronics Serial Communication-SPI

Page 3: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

Serial Communication -SPI

Serial Peripheral Interface

Master/slave communicationUses 3 signals (and Ground),MISO, MOSI, SCLKand chip selects for each slave deviceSynchronous, so master controls clock rate

Terry Sturtevant Electronics Serial Communication-SPI

Page 4: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

Serial Communication -SPI

Serial Peripheral InterfaceMaster/slave communication

Uses 3 signals (and Ground),MISO, MOSI, SCLKand chip selects for each slave deviceSynchronous, so master controls clock rate

Terry Sturtevant Electronics Serial Communication-SPI

Page 5: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

Serial Communication -SPI

Serial Peripheral InterfaceMaster/slave communicationUses 3 signals (and Ground),MISO, MOSI, SCLKand chip selects for each slave device

Synchronous, so master controls clock rate

Terry Sturtevant Electronics Serial Communication-SPI

Page 6: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

Serial Communication -SPI

Serial Peripheral InterfaceMaster/slave communicationUses 3 signals (and Ground),MISO, MOSI, SCLKand chip selects for each slave deviceSynchronous, so master controls clock rate

Terry Sturtevant Electronics Serial Communication-SPI

Page 7: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

CS

SCLK

MOSI

MISO

0 1 2 3 4 5 6 76 6 6 6 6 6 6 6

SPI transfers can happen in both directions simultaneously.

Terry Sturtevant Electronics Serial Communication-SPI

Page 8: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

CS

SCLK

MOSI

MISO

0 1 2 3 4 5 6 76 6 6 6 6 6 6 6

SPI transfers can happen in both directions simultaneously.

Terry Sturtevant Electronics Serial Communication-SPI

Page 9: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

PySpidev

spi = spidev.SpiDev()create objectspi.open(0,0)open port, deviceThere is normally one SPI port on the Raspberry Pi.It is designated as port 0SPI0 has two associated devices, selected by chip selectsCE0 and CE1So, spi.open(0,0) means toconnect to the device using CE0 on SPI0

Terry Sturtevant Electronics Serial Communication-SPI

Page 10: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

PySpidev

spi = spidev.SpiDev()create object

spi.open(0,0)open port, deviceThere is normally one SPI port on the Raspberry Pi.It is designated as port 0SPI0 has two associated devices, selected by chip selectsCE0 and CE1So, spi.open(0,0) means toconnect to the device using CE0 on SPI0

Terry Sturtevant Electronics Serial Communication-SPI

Page 11: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

PySpidev

spi = spidev.SpiDev()create objectspi.open(0,0)open port, device

There is normally one SPI port on the Raspberry Pi.It is designated as port 0SPI0 has two associated devices, selected by chip selectsCE0 and CE1So, spi.open(0,0) means toconnect to the device using CE0 on SPI0

Terry Sturtevant Electronics Serial Communication-SPI

Page 12: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

PySpidev

spi = spidev.SpiDev()create objectspi.open(0,0)open port, deviceThere is normally one SPI port on the Raspberry Pi.

It is designated as port 0SPI0 has two associated devices, selected by chip selectsCE0 and CE1So, spi.open(0,0) means toconnect to the device using CE0 on SPI0

Terry Sturtevant Electronics Serial Communication-SPI

Page 13: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

PySpidev

spi = spidev.SpiDev()create objectspi.open(0,0)open port, deviceThere is normally one SPI port on the Raspberry Pi.It is designated as port 0

SPI0 has two associated devices, selected by chip selectsCE0 and CE1So, spi.open(0,0) means toconnect to the device using CE0 on SPI0

Terry Sturtevant Electronics Serial Communication-SPI

Page 14: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

PySpidev

spi = spidev.SpiDev()create objectspi.open(0,0)open port, deviceThere is normally one SPI port on the Raspberry Pi.It is designated as port 0SPI0 has two associated devices, selected by chip selectsCE0 and CE1

So, spi.open(0,0) means toconnect to the device using CE0 on SPI0

Terry Sturtevant Electronics Serial Communication-SPI

Page 15: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

PySpidev

spi = spidev.SpiDev()create objectspi.open(0,0)open port, deviceThere is normally one SPI port on the Raspberry Pi.It is designated as port 0SPI0 has two associated devices, selected by chip selectsCE0 and CE1So, spi.open(0,0) means toconnect to the device using CE0 on SPI0

Terry Sturtevant Electronics Serial Communication-SPI

Page 16: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

PySpidev

(continued)

So, spi.max speed hz = 5000 means tolimit the SPI clock speed to about 5kHzresponse = spi.xfer2([0xAA])transfer one byteCS held active between blocksresponse = spi.xfer([values])transfer bytesCS realeased and reactivated between blocks

Terry Sturtevant Electronics Serial Communication-SPI

Page 17: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

PySpidev

(continued)So, spi.max speed hz = 5000 means tolimit the SPI clock speed to about 5kHz

response = spi.xfer2([0xAA])transfer one byteCS held active between blocksresponse = spi.xfer([values])transfer bytesCS realeased and reactivated between blocks

Terry Sturtevant Electronics Serial Communication-SPI

Page 18: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

PySpidev

(continued)So, spi.max speed hz = 5000 means tolimit the SPI clock speed to about 5kHzresponse = spi.xfer2([0xAA])transfer one byteCS held active between blocks

response = spi.xfer([values])transfer bytesCS realeased and reactivated between blocks

Terry Sturtevant Electronics Serial Communication-SPI

Page 19: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

PySpidev

(continued)So, spi.max speed hz = 5000 means tolimit the SPI clock speed to about 5kHzresponse = spi.xfer2([0xAA])transfer one byteCS held active between blocksresponse = spi.xfer([values])transfer bytesCS realeased and reactivated between blocks

Terry Sturtevant Electronics Serial Communication-SPI

Page 20: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

PySpidev (continued)

spi.writebytes([values])write bytesspi.readbytes(len)read len bytesspi.cshighget or set active state of CSspi.close()close port

Terry Sturtevant Electronics Serial Communication-SPI

Page 21: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

PySpidev (continued)

spi.writebytes([values])write bytes

spi.readbytes(len)read len bytesspi.cshighget or set active state of CSspi.close()close port

Terry Sturtevant Electronics Serial Communication-SPI

Page 22: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

PySpidev (continued)

spi.writebytes([values])write bytesspi.readbytes(len)read len bytes

spi.cshighget or set active state of CSspi.close()close port

Terry Sturtevant Electronics Serial Communication-SPI

Page 23: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

PySpidev (continued)

spi.writebytes([values])write bytesspi.readbytes(len)read len bytesspi.cshighget or set active state of CS

spi.close()close port

Terry Sturtevant Electronics Serial Communication-SPI

Page 24: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

PySpidev (continued)

spi.writebytes([values])write bytesspi.readbytes(len)read len bytesspi.cshighget or set active state of CSspi.close()close port

Terry Sturtevant Electronics Serial Communication-SPI

Page 25: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

PySpidev sample code

impor t s p i d e vs p i = s p i d e v . SpiDev ( )s p i . open (0 , 0 ) #port , d e v i c e

# use po r t 0 , ch i p s e l e c t CE0s p i . max speed hz = 5000w h i l e True :

s t r v a l = i n p u t (” v a l ( 0 . . . 2 5 5 , q=q u i t ) : ” )i f s t r v a l == ’q ’ :

b reake l s e :

v a l u e = i n t ( s t r v a l )dummy = s p i . x f e r 2 ( [ 4 9 , v a l u e ] )

s p i . c l o s e ( )

Terry Sturtevant Electronics Serial Communication-SPI

Page 26: Electronics- Serial Communication-SPIdenethor.wlu.ca/pc320/lectures/spibeam.pdf · 2018-10-03 · Serial Communication -SPI Raspberry Pi Python SPI Library PySpidev spi = spidev.SpiDev()

Serial Communication -SPIRaspberry Pi Python SPI Library

PySpidev sample code

impor t s p i d e vs p i = s p i d e v . SpiDev ( )s p i . open (0 , 0 ) #port , d e v i c e

# use po r t 0 , ch i p s e l e c t CE0s p i . max speed hz = 5000w h i l e True :

s t r v a l = i n p u t (” v a l ( 0 . . . 2 5 5 , q=q u i t ) : ” )i f s t r v a l == ’q ’ :

b reake l s e :

v a l u e = i n t ( s t r v a l )dummy = s p i . x f e r 2 ( [ 4 9 , v a l u e ] )

s p i . c l o s e ( )

Terry Sturtevant Electronics Serial Communication-SPI