ch. 2 giao thøc ghÐp nèi (Interfacing Protocols)

Bao gåm:

- Giao thøc ghÐp nèi - §Æc ®iÓm lËp tr×nh I/O

P&I-Ch2: Protocols

1

2.1. Interface Protocols:

• K/n: Lµ c¸c qui ®Þnh:

– Signals – Data format – Rate – Error detection & correction – Command & Response set – Scenario (kÞch b¶n) ISO 7 layer Model (Ref. Computer networks) Cã thÓ ph©n chia thµnh 2 nhãm chÝnh:

• Transport-Oriented Protocols (1..4) (!!!)

– PhysicAL (wire, cable, connector, port), DataLink (CRC, CS, Token), NetWork (Comm. 2 networks ), Transport (Err-protected raw infor),

• Application-Oriented Protocols (5..7) :

– Session (Opening, End), Presentation (Common Language) vµ Application (Read/Write, Start/Stop, FileTransfer)

P&I-Ch2: Protocols

2

2.1. Interface Protocols: 2.1.1. Signals:

Khi thiÕt kÕ, x©y dùng ghÐp nèi m¸y tÝnh, cÇn chó ý ®Æc biÖt tíi tÝn hiÖu theo c¸c yªu cÇu:

• H¬n 1 thiÕt bÞ? => bus/m¹ng hay kh«ng ? => dïng bit

(tr−êng) ®Þa chØ.

• NÕu dïng bus => Standard bus hay kh«ng? IDE vμ LPT-mode 0 cables lμ bus? t¹i sao?

• Data: Xa /gÇn, nhanh/chËm => Serial vs Parallel • Xa: Daisy chain cho t/h hoÆc nguån cÊp... • C¸c tÝn hiÖu ®iÒu khiÓn vµ tr¹ng th¸i:

P&I-Ch2: Protocols

3

• Control signals • Status signals • Handshaking signals - bắt tay

• Daisy chain:

– Nối các thiết bị có khoảng cách từ vài đến vài chục m, đầu ra của port (module) thứ n nối với đầu vào của n+1

– Dùng cho tín hiệu (// & nt), nguồn cung cấp,

handshake...

P&I-Ch2: Protocols

4

2.1. Interface Protocols: 2.1.1. Signals:

• VÝ dô 1: PC-LPT handshaking:

SLCT SLTC_in

PC

Line Printer

LPT port

H×nh 2.1.a. LPT handshake Signal

P&I-Ch2: Protocols

5

2.1. Interface Protocols: 2.1.1. Signals:

• VÝ dô 2: PC Comm-Modem handshaking:

RTS

CTS

PC 1

Comm Port

DTR DSR

Modem or PC 2 Comm Port

H×nh 2.1.b. CommPort Handshake Signals

P&I-Ch2: Protocols

6

•Ph−¬ng ph¸p biÕn ®æi tÝn hiÖu: biªn, tÇn, pha, dßng, quang (c¸p quang, Ir)

2.1. Interface Protocols: 2.1.1. Signals:

• T/c vật lý của tín hiệu:

- Direction: In/Out, Input voltage: -0.5V to Vcc +0.5V - Mức điện áp: Mức áp ? (TTL (transistor transistor logic- [0..0.8V/ 2..5V]) , 12V/ 24V/48V...) - Single End (đơn cực) vs Differential (vi sai) signals - Khả năng tải - dòng điện ra: (Fan Out, Loading):

+ Number of Standard/LS TTL loads, + Sink: dòng vào – LowLevel, (cỡ mA), + Source: dòng ra – HighLevel, mA/uA.(H. 2.1.c) • Nối chung/ghép nối bus đơn giản: 3 state (Hi Z), open

P&I-Ch2: Protocols

7

collector (Open Drain), Mux – dồn kênh, Switch - khóa. • Hot swap – hot plugible: Y/c Vcc và t/h ( www.atmel.com ) • Cách ly (isolation): Relay, Opto Coupler, IrLED... • Bus Slot, Connector, chuẩn, số chân (pin)

P&I-Ch2: Protocols

8

S¬ ®å c¸ch ly quang häc ®/v tÝn hiÖu In/Out:

P&I-Ch2: Protocols

9

Hình 2.2. Opto Coupler

Cable & Connectors:

• Connectors:

– D shell: DB9, DB25,... – DIN, • Cable: • Flat, • Coaxial, • Shield, • Twisted Pair • Normal • Optical Fiber...

P&I-Ch2: Protocols

10

H×nh 2.3.a. Connectors

P&I-Ch2: Protocols

11

H×nh 2.3.b. Optical Connector & S¬ ®å:

P&I-Ch2: Protocols

12

H×nh 2.3.c. Sîi c¸p quang:

2.1. Interface Protocols: 2.1.2. Format sè liÖu:

P&I-Ch2: Protocols

13

®èi víi file/text: sè liÖu nhiÒu => khi trao ®æi (với DAS, PLC, Digi-Oscilloscope, GPS, TelSat...) (cid:206) ‘®ãng gãi’ sè liÖu (packaging). Mçi gãi tin (packet) gåm 3 phÇn: • Header: [cã thÓ cã: tªn b¶n tin, tªn gãi, sè thø tù, ký tù b¾t tay, ký tù ®ång bé, sè ký tù/ byte trong gãi tin...], kh«ng mang tin. • Content: néi dung tin - mang th«ng tin. • Tailer: M∙ b¾t tay kÕt thóc, [m∙ kiÓm tra lçi] - kh«ng mang tin. VÝ dô: HDC, FDC: Full Sector: gap - 5 byte ID field - 2 byte ID CRC - gap - data field: 512 byte - 2 byte CRC. FTP, Kermit, X-Modem.. Protocols: 128[256] B/pack.

H×nh 2.3.d. USB data packet format

P&I-Ch2: Protocols

14

Byte sè liÖu/character/frame: (truyÒn kh«ng ®ång bé, RS-232, RS-485, RS-422...): ký tù hay byte ®−îc ®Þnh d¹ng thµnh 1 frame: • 1 start bit = 0, • 5/6/7/8 data bit, D0 first, • [parity: Even/ Odd], • 1 / [1.5 / 2] stop bit = 1(s).

2.1. Interface Protocols: 2.1.3. Tèc ®é trao ®æi th«ng tin:

XuÊt ph¸t tõ: Nhu cÇu trao ®æi th«ng tin cña T/b ngo¹i vi => chän kiÓu truyÒn thÝch hîp, cã liªn quan tíi t/h: (cid:131) Bottle-neck? (cid:131) Phô thuéc Kho¶ng c¸ch - tÝch sè (k/c vµ tèc ®é) (cid:131) NhiÔu: theo c«ng thøc Shannon: bps = BW log2 (1+P/N). Víi BW: bandwidth, P/N tû sè c«ng suÊt tÝn hiÖu/ nhiÔu (not a dB ratio) (cid:131) M«i tr−êng, ®−êng truyÒn (c¸p ®ång, quang, wireless: radio,

infrared)

(cid:131) Synchronous/Asynchronous? (cid:131) Modulation/Demodulation ... => tèc ®é bao nhiªu kbps/kBps, tèc ®é chuÈn? Ví dụ: LPT: SPP mode: 50..100kBps - software, ECP: 2..4

P&I-Ch2: Protocols

15

MBps - DMA LAN Ethernet – IEEE 802.3: 10/100 Mbps dual speed RS232: 2400/ 4800/ 9600/ 19200... bps

2.1. Interface Protocols:

2.1.4. KiÓm tra, söa lçi, n©ng cao ®é tin cËy:

Khi trao ®æi th«ng tin th−êng hay g©y ra lçi, ®Æc biÖt truyÒn xa/ chuyÓn ®æi t/h. NhiÒu ph−¬ng ph¸p (Hardware, Software) hç trî ®Ó kiÓm tra: • [Block] check sum - BCC, phÇn mÒm, • CRC, ECC,... vi m¹ch/ software - subroutine • Parity, 1 hoÆc 2 chiÒu • Redundancy (RAID), thõa d−: trao ®æi néi dung sè

liÖu h¬n 1 lÇn vµ so s¸nh.

• Case study: Barcode Ph−¬ng ph¸p m∙ hãa, gi¶i m∙

P&I-Ch2: Protocols

16

vµ kiÓm tra lçi

2.1.4. KiÓm tra, söa lçi, n©ng cao ®é tin cËy: Bar Code: EAN 13, CODE 39 (Intermec), CODE 128 (Zebra), UPC ...

• EAN 13 (European article numbering)

• Encoding: AAA BBBBB CCCC D; 4 ®é dµy v¹ch, 6

v¹ch/digit (b&w)

• A(National): VN 893, CN 690-692, JP 45-49, GE

400-440, RU 460-469

• B: com/ org • C: Product • D - Check sum, right most: (right to left): 10 - [(D2*3 + D3*1 + D4*3 + D5*1...+ D13*1)mod10] VÝ dô: 893 12345 1234 7

P&I-Ch2: Protocols

17

• CODE 128, Zebra, check sum modulo 103

2.1. Interface Protocols: 2.1.5. Command & Response (Result/Reaction) set:

GhÐp Intelligent Devices (Computerized devices - mouse, KB, Printer, modem, FDC, HDC, RTU...) cã nhiÒu tham sè, chÕ ®é ho¹t ®éng => x©y dùng bé lÖnh (command set) vµ th«ng tin tr¶ vÒ (response set). Dïng phÇn mÒm ®Ó xö lý => bít tÝn hiÖu. • •

TËp hîp c¸c yªu cÇu tõ CS - command set, TËp hîp c¸c tr¶ lêi, tr¹ng th¸i - result/response/ reaction set.

P&I-Ch2: Protocols

18

C¸c c©u lÖnh vµ tr¶ lêi cã syntax riªng (cÊu tróc vµ ng÷ ph¸p).

2.1. Interface Protocols:

2.1.5. Command & Response (Result) set:

P&I-Ch2: Protocols

19

Case Studies: lÖnh AT vµ Response Hayes MODEMs: ATDT 1260 ↵' Víi nhiÒu Options LÖnh: Tr¶ lêi (reaction) OK [Error] . . . Connect @19200 (result) LÖnh FX Printer: Esc * m, n1, n2; Sel Graph Mode Tham kh¶o c¸c bé lÖnh cña c¸c thiÕt bÞ chuyªn dïng: GPS, Gyrocompass, Digital Oscilloscope, SAGEM, TelSat, Programmer (Hi-Lo System All-11P2)...

2.1. Interface Protocols:

2.1.5. Command & Response (Result) set:

1 command/response th−êng cã cÊu tróc:

o m∙ b¾t ®Çu ký tù riªng nh− @ / # / $/ AT... o m∙ lÖnh, 1..3 bytes/ char, tham sè lÖnh, 1... n bytes, o o m∙ kiÓm tra lçi check sum, CRC (dÔ xö lý) o m∙ kÕt thóc, ký tù riªng.

Cã thªm c¸c m∙ (ký tù) ®èi tho¹i/ reaction, [dïng] ký tù

P&I-Ch2: Protocols

20

®iÒu khiÓn cña ASCII nh−: ENQ, ACK, NACK, Bell, OK, ERR, BUSY ...

2.1. Interface Protocols: 2.1.6. KÞch b¶n ®èi tho¹i – Scenario:

LiÖt kª c¸c tr−êng

Master Slave ENQ

ACK

NAK

nothing

hîp cã thÓ råi ¸p c¸c phÐp xö lý t−¬ng øng ®Ó ®¶m b¶o viÖc ghÐp nèi: kh«ng mÊt tin, thõa tin, quÈn, treo...

Th−êng x©y dùng: Step List hoÆc Chart: Time Out !

Hinh 2.4. Scenario Chart

P&I-Ch2: Protocols

21

22

H×nh 2.5. USB Interlayer Interconnection Model P&I-Ch2: Protocols

2.2. Ph©n miÒn vµ lÖnh I/O: 2.2.1. IO Mapping:

P&I-Ch2: Protocols

23

H×nh 2.6. Ph©n miÒn c¸c cæng I/O

• Memory Mapped IOs:

o IOs chung víi Mem trong MemSpace => chiÕm vïng

nhí, tốn vùng nhớ

o CPU xö lý c¸c cæng IO b»ng c¸c lÖnh nh− ®/v mem.

• IO Mapped IOs: (Z80, x86...):

o Kh«ng chiÕm kh«ng gian nhí, o CPU chØ thùc hiÖn 2 lÖnh: IN vµ OUT

• x86 CPU’s IO map: IO mapped IOs, 16 Addr bit IO

=> 64Kilo IO locations.

• PC’s IO map:

o IO Mapped IOs, o ChØ dïng 10 lowest addr bit, A0..A9 => 1 kilo IO locations Soi gương – 1st kilo Mirrored víi 63 kilo cßn l¹i, Mçi IO port chiÕm nhiÒu ®Þa chØ (nh− PIC, PIT, PPI..) => thiÕu IO space. SÏ dïng thªm kiÓu Mem

Mapped IOs.

P&I-Ch2: Protocols

24

2.2.2. LÖnh In/Out: (x86) :

LÖnh IN vµ OUT: chØ dïng c¸c thanh ghi Accumulator: 8 bit: AL, 16 bit: AX vµ 32 bit: EAX.

ChÕ ®é ®Þa chØ: o Direct: for IO space: 0..0FFh

(cid:0) VÝ dô:

; Read KB port

in al,60h out 23Eh, ax ; lÖnh sai, IOaddr>255 out 61h, al

; beep, set/reset key flag

VÝ dô:

;41h/ 65d ; 'A' ==> Printer

o Indirect: for IO space 0..0FFFFh, via dx register mov dx, 378h ; PLT port Addr mov al, ‘A’ out dx, al mov dx,3F8h ;Comm 1 port ; in al,dx P&I-Ch2: Protocols

25

Port (hex) 000-00F

020-02F

040-05F 060-06F 070-071

080-083

Phụ lục PC’s I/O Port Map Port Assignments DMA Controller, đkh truy nhập trực tiếp 1, 4 kênh 8 bit Master PIC 8259A, ưu tiên ngắt 1, IRQ0 – IRQ7 PIT, Programmable Interval Timẻ Keyboard Port, Real Time Clock và 64 byte CMOS-RAM DMA Page Register, giữ địa chỉ cao

P&I-Ch2: Protocols

26

0A0-0AF

0C0-0CF

0E0-0EF 110-16F 170-17F 180-1EF 1F0-1FF 200-20F

PIC #2, Bộ đkh ưu tiên ngắt số 2, IRQ8 – IRQ15 DMAC #2, 3 kênh DRQ 16 bit reserved, để dành AVAILABLE, để dành Hard Drive 1 (AT) AVAILABLE Hard Drive 0 (AT) Game Adapter

P&I-Ch2: Protocols

27

210-217 220-26F 278-27F 280-2A1 2A2-2A3 2B0-2DF 2E2-2E3 2E8-2EF 2F0-2F7 2F8-2FF 300-31F

320-32F

Expansion Card Ports AVAILABLE Parallel Port 2/ LPT2 AVAILABLE clock EGA/Video Data Acquisition Adapter (AT) Serial Port COM4 Reserved Serial Port COM2 Prototype Adapter, cho người dùng phát triển AVAILABLE

P&I-Ch2: Protocols

28

340-35F 360-36F 370-377 378-37F 380-38F 390-39F 3A0-3AF 3B0-3BF 3BC-3BF

AVAILABLE Network, LAN adaptor Floppy Disk Controller Parallel Port 1, LPT1 SDLC Adapter Cluster Adapter reserved Monochrome Adapter Parallel Port 3

P&I-Ch2: Protocols

29

3C0-3CF 3D0-3DF 3E8-3EF 3F0-3F7 3F8-3FF

EGA/VGA Color Graphics Adapter Serial Port COM3 Floppy Disk Controller Serial Port COM1

• Soundblaster cards usually use I/O ports 220-22F • Data acquisition cards frequently use 300-31F.

P&I-Ch2: Protocols

30

Bµi tËp ch−¬ng 2

2.1. Tham kh¶o mét sè giao thøc: MODBUS (AEG)

2.2.

2.3.

modbus.org, ProfiBus, ASI (Siemens), RTU ASCII (ABB), I2C... tõ ®ã thiÕt kÕ ®å ¸n m«n häc: hÖ thu thËp - ®iÒu khiÓn ph©n t¸n (Mini SCADA) USB Protocol ThiÕt kÕ giao thøc truyÒn file gi÷a 2 m¸y tÝnh (LPT/ Comm).

P&I-Ch2: Protocols

31