Cyber-Physical Systems Communication ICEN 553/453 Fall 2018 Prof. - - PowerPoint PPT Presentation

cyber physical systems communication
SMART_READER_LITE
LIVE PREVIEW

Cyber-Physical Systems Communication ICEN 553/453 Fall 2018 Prof. - - PowerPoint PPT Presentation

Cyber-Physical Systems Communication ICEN 553/453 Fall 2018 Prof. Dola Saha 1 Why do we need Communication? Connect different systems together o Two embedded systems o A desktop and an embedded system Connect different chips together


slide-1
SLIDE 1

1

Cyber-Physical Systems Communication

ICEN 553/453– Fall 2018

  • Prof. Dola Saha
slide-2
SLIDE 2

2

Why do we need Communication?

§ Connect different systems together

  • Two embedded systems
  • A desktop and an embedded system

§ Connect different chips together in the same embedded system

  • MCU to peripheral
  • MCU to MCU
slide-3
SLIDE 3

3

What determines how much we can transmit?

Ø Shannon’s noisy channel coding theorem § Says you can achieve error-free communicate at any Ø Rate up to the channel capacity, and can’t do any better § C: channel capacity, in bits / s § W: bandwidth amount of frequency “real estate”, in Hz (cycles / s) § S: Signal power § N: Noise power

! = #$%&' ( + * *

slide-4
SLIDE 4

4

Communication Methods

Ø Different physical layers methods: wires, radio frequency (RF), optical (IR) Ø Different encoding schemes: amplitude, frequency, and pulse-width

modulation

slide-5
SLIDE 5

5

Dimensions to consider

Ø bandwidth – number of wires – serial/parallel Ø speed – bits/bytes/words per second Ø timing methodology – synchronous or asynchronous Ø number of destinations/sources Ø arbitration scheme – daisy-chain, centralized, distributed Ø protocols – provide some guarantees as to correct

communication

slide-6
SLIDE 6

6

Parallel and Serial Bus

slide-7
SLIDE 7

7

Parallel and Serial Communication

Ø Serial

§ Single wire or channel to transmit information one bit at a time § Requires synchronization between sender and receiver § Sometimes includes extra wires for clock and/or handshaking § Good for inexpensive connections (e.g.,terminals) § Good for long-distance connections (e.g.,LANs)

Ø Parallel

§ Multiple wires to transmit information

  • ne byte or word at a time

§ Good for high-bandwidth requirements (CPU to disk) § Crosstalk creates interference between multiple wires § Length of link increases crosstalk § More expensive wiring/connectors/current requirements

slide-8
SLIDE 8

8

Parallel vs. Serial Digital Interfaces

Ø

Parallel (one wire per bit)

§ ATA: Advanced Technology Attachment § PCI: Peripheral Component Interface § SCSI: Small Computer System Interface

§ Serial (one wire per direction)

§ RS-232 § SPI: Serial Peripheral Interface bus § I2C: Inter-Integrated Circuit § USB: Universal Serial Bus § SATA: Serial ATA § Ethernet, IrDA, Firewire, Bluetooth, DVI, HDMI Ø

Mixed (one or more “lanes”)

§ PCIe: PCI Express

PCI SCSI USB RS-232

slide-9
SLIDE 9

9

Parallel vs Serial Digital Interfaces

Ø Parallel connectors have been replaced by Serial § Significant crosstalk/inter-wire interference for parallel connectors § Maintaining synchrony across the multiple wires § Serial connection speeds can be increased by increasing transmission freq, but parallel crosstalk gets worse at increased freq

slide-10
SLIDE 10

10

Serial Peripheral Interface (SPI)

Ø

Synchronous full-duplex communication

Ø

Can have multiple slave devices

Ø

No flow control or acknowledgment

Ø

Slave cannot communicate with slave directly.

SCLK: serial clock SS: slave select (active low) MOSI: master out slave in MISO: master in slave out

Serial Peripheral Interface

http://upload.wikimedia.org/wikipedia/commons/thumb/e/ed/ SPI_single_slave.svg/350px-SPI_single_slave.svg.png

slide-11
SLIDE 11

11

SPI – Point-to-point and Daisy Chain

SCLK: serial clock SS: slave select (active low) MOSI: master out slave in MISO: master in slave out

Pictures: https://learn.sparkfun.com/tutorials/serial-peripheral-interface-spi/

Point-to-point Daisy Chain

slide-12
SLIDE 12

12

Data Exchange

Ø Master has to provide clock to slave Ø Synchronous exchange: for each clock pulse, a bit is shifted out and

another bit is shifted in at the same time. This process stops when all bits are swapped.

Ø Only master can start the data transfer

slide-13
SLIDE 13

13

Clock

slide-14
SLIDE 14

14

Clock Phase and Polarity

Ø CPHA (Clock PHase)

§ = 0 or =1, determines when data goes on bus relative to clock

Ø CPOL (Clock POLarity)

§ =0 clock idles low between transfers § =1 clock idles high between transfers

Ø

CPOL = 0 ⟶ SCLK is pushed to low during idle. Otherwise, pulled to high during idle.

Ø

CPHA = 0 ⟶ the first clock transition (either rising or falling) is the first data capture edge. Otherwise, the second clock transition is the first data capture edge.

Ø

Combination of CPOL and CPHA determines the clock edge for transmitting and receiving.

Mode 0 Mode 1 Mode 2 Mode 3 Clock Phase (CPHA) Clock Polarity (CPOL) CPHA = 0 CPOL = 0 CPHA = 1 CPOL = 1

Sampling Edge Sampling Edge Toggling Edge Toggling Edge

slide-15
SLIDE 15

15

Clock Phase and Polarity

SSN SCLK CPOL = 0 CPHA = 0 SCLK CPOL = 0 CPHA = 1 SCLK CPOL = 1 CPHA = 0 SCLK CPOL = 1 CPHA = 1 Sampling Edge Sampling Edge Sampling Edge Sampling Edge Sampling Edge Sampling Edge Sampling Edge Sampling Edge

bout[2] bout[0] bout[1] bout[3] bout[4] bout[5] bout[6] bout[7] Mode 0 Mode 1 Mode 2 Mode 3 Clock Phase (CPHA) Clock Polarity (CPOL) CPHA = 0 CPOL = 0 CPHA = 1 CPOL = 1

Sampling Edge Sampling Edge Toggling Edge Toggling Edge

slide-16
SLIDE 16

16

SPI: Pros and Cons

Ø Pros § Simplest way to connect 1 peripheral to a micro § Fast (10s of Mbits/s, not on MSP) because all lines actively driven, unlike I2C § Clock does not need to be precise § Nice for connecting 1 slave Ø Cons § No built-in acknowledgement of data § Not very good for multiple slaves § Requires 4 wires § 3 wire variants exist...some get rid of full duplex and share a data line, some get rid of slave select

slide-17
SLIDE 17

17

Analog to Digital Converter

Ø DGND : digital ground pin for the chip Ø CS : chip select. Ø DIN : data in from the MC itself. Ø DOUT: data out pin. Ø CLK: clock pin. Ø AGND: analog ground and obviously connects to ground. Ø VREF: analog reference voltage. You can change this if you

want to change the scale. You probably want to keep it the same so keep this as 3.3v.

Ø VDD: positive power pin for the chip.

slide-18
SLIDE 18

18

MCP 3008

slide-19
SLIDE 19

19

Configuration Bits

Ø Single or Differential Ø D2, D1, D0

slide-20
SLIDE 20

20

Communication

slide-21
SLIDE 21

21

Analog to Digital Converter

RPi 3.3V RPi 3.3V RPi GND RPi SClk RPi MISO RPi MOSI RPi CE0 RPi GND

slide-22
SLIDE 22

22

Connect a Sensor

slide-23
SLIDE 23

23

Channel Select

Ø

The device will begin to sample the analog input on the fourth rising edge of the clock after the start bit has been received. The sample period will end on the falling edge of the fifth clock following the start bit.

slide-24
SLIDE 24

24

Enable SPI in Raspberry PI

Ø sudo raspi-config Ø 5 Interfacing Options Ø P4 SPI Ø Would you like the SPI interface to be enabled? § Select Yes Ø The SPI interface is enabled § Select OK Ø Finish

slide-25
SLIDE 25

25

Has SPI been really enabled?

Ø sudo ls /dev/spi* Ø /dev/spidev0.0

/dev/spidev0.1

slide-26
SLIDE 26

26

Install Adafruit MCP 3008 Python Library

Ø sudo apt-get update Ø sudo apt-get install build-essential python-dev python-smbus git Ø cd ~ Ø git clone https://github.com/adafruit/Adafruit_Python_MCP3008.git Ø cd Adafruit_Python_MCP3008 Ø sudo python setup.py install

slide-27
SLIDE 27

27

Photocell

Ø Measure Voltage drop depending on Lux Ø Resistance range: 200KΩ (dark) to 10KΩ (10 lux brightness) Ø Sensitivity range: Respond to light between 400nm (violet)

and 600nm (orange) wavelengths, peaking at about 520nm (green).

Ø Power supply: Up to 100V § uses less than 1mA of current on average

slide-28
SLIDE 28

28

Connect Photocell

slide-29
SLIDE 29

29

Run existing code

Ø cd ~/Adafruit_Python_MCP3008/examples/ Ø Change the simpletest.py code to enable Hardware SPI

and disable Software SPI

Ø Then run the simpletest.py

slide-30
SLIDE 30

30

Output of simpletest.py

Ø Why are there values in unused channels? Ø What can be the range of values in the used channel?

slide-31
SLIDE 31

31

Connect Temperature Sensor

slide-32
SLIDE 32

32

Kernel and User Space

ioctl()

slide-33
SLIDE 33

33

SPI Bus on Linux

Ø lsmod | grep spi Ø modprobe spidev Ø modprobe spi_bcm2835 Ø dmesg | grep spi

slide-34
SLIDE 34

34

SPI Using User->Kernel Modules

Ø ioctl § /usr/include/asm-generic/ioctl.h Ø spidev § /usr/include/linux/spi/spidev.h Ø Kernel Module § https://github.com/raspberrypi/linux/blob/rpi-3.12.y/drivers/spi/spi- bcm2835.c

slide-35
SLIDE 35

35

ioctl() – Input/Output Control

Ø int ioctl(int fd, unsigned long request, ...); Ø The ioctl() system call manipulates the underlying device parameters of

special files.

Ø Input Arguments

§ fd – File Descriptor § request – Device dependent request code § Third Argument – Integer value of a pointer to data for transfer

Ø Return

§ 0 on success. § -1 on error.

slide-36
SLIDE 36

36

spi_ioc_transfer structure

slide-37
SLIDE 37

37

SPI Dev Interface

Ø https://www.kernel.org/doc/Documentation/spi/spidev Ø /dev/spidevB.C (B=bus, C=slave number). § On RPi it is /dev/spidev0.0 Ø To open the device: § fd=open("/dev/spidev0.0",O_RDWR);

slide-38
SLIDE 38

38

SPI Dev Interface

Ø To set the mode: § int mode=SPI_MODE_0; § result = ioctl(spi_fd , SPI_IOC_WR_MODE , &mode); Ø To set the bit order: § int lsb_mode =0; § result = ioctl(spi_fd, SPI_IOC_WR_LSB_FIRST, &lsb_mode);

slide-39
SLIDE 39

39

SPI Dev Interface

Ø To transfer: § ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); Ø To close: § close(fd);

slide-40
SLIDE 40

40

MCP 3008 Data Transfer