You are on page 1of 31

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:
• Control signals
• Status signals
• Handshaking signals - bắt tay
P&I-Ch2: Protocols 3
• 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:

PC SLCT SLTC_in
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 Modem
or
Comm PC 2
Port Comm
DTR DSR
Port

H×nh 2.1.b. CommPort Handshake Signals


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

P&I-Ch2: Protocols 6
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
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 7
P&I-Ch2: Protocols 8
S¬ ®å c¸ch ly quang häc ®/v tÝn hiÖu In/Out:

Hình 2.2. Opto Coupler


P&I-Ch2: Protocols 9
Cable & Connectors:
• Connectors:
– D shell: DB9, DB25,...
– DIN,
• Cable:
• Flat,
• Coaxial,
• Shield,
• Twisted Pair
• Normal
• Optical Fiber...

H×nh 2.3.a. Connectors


P&I-Ch2: Protocols 10
H×nh 2.3.b. Optical Connector & S¬ ®å:

P&I-Ch2: Protocols 11
H×nh 2.3.c. Sîi c¸p quang:

P&I-Ch2: Protocols 12
2.1. Interface Protocols: 2.1.2. Format sè liÖu:
®èi víi file/text: sè liÖu nhiÒu => khi trao ®æi (với DAS,
PLC, Digi-Oscilloscope, GPS, TelSat...) Î ‘®ã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.
P&I-Ch2: Protocols 13
H×nh 2.3.d. USB data packet format

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).

P&I-Ch2: Protocols 14
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:
ƒ Bottle-neck?
ƒ Phô thuéc Kho¶ng c¸ch - tÝch sè (k/c vµ tèc ®é)
ƒ 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)
ƒ M«i tr−êng, ®−êng truyÒn (c¸p ®ång, quang, wireless: radio,
infrared)
ƒ Synchronous/Asynchronous?
ƒ Modulation/Demodulation ...
=> tèc ®é bao nhiªu kbps/kBps, tèc ®é chuÈn?
Ví dụ: LPT: SPP mode: 50..100kBps - software, ECP: 2..4
MBps - DMA
LAN Ethernet – IEEE 802.3: 10/100 Mbps dual speed
RS232: 2400/ 4800/P&I-Ch2:
9600/Protocols
19200... bps 15
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∙
vµ kiÓm tra lçi

P&I-Ch2: Protocols 16
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
• CODE 128, Zebra, check sum modulo 103
P&I-Ch2: Protocols 17
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.
C¸c c©u lÖnh vµ tr¶ lêi cã syntax riªng (cÊu tróc vµ
ng÷ ph¸p).

P&I-Ch2: Protocols 18
2.1. Interface Protocols:
2.1.5. Command & Response (Result) set:

Case Studies: lÖnh AT vµ Response Hayes MODEMs:


LÖnh: ATDT 1260 ↵ ' Víi nhiÒu Options
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)...

P&I-Ch2: Protocols 19
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,
o tham sè lÖnh, 1... n bytes,
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ù
®iÒu khiÓn cña ASCII nh−: ENQ, ACK, NACK, Bell, OK,
ERR, BUSY ...

P&I-Ch2: Protocols 20
2.1. Interface Protocols:
2.1.6. KÞch b¶n ®èi tho¹i – Scenario:
LiÖt kª c¸c tr−êng
hîp cã thÓ råi ¸p Master ENQ Slave
c¸c phÐp xö lý
ACK
t−¬ng øng ®Ó
®¶m b¶o viÖc NAK
ghÐp nèi: kh«ng
nothing
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
H×nh 2.5. USB Interlayer Interconnection Model
P&I-Ch2: Protocols 22
2.2. Ph©n miÒn vµ lÖnh I/O: 2.2.1. IO Mapping:

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


P&I-Ch2: Protocols 23
• 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
VÝ dô: in al,60h ; Read KB port
out 23Eh, ax ;
lÖnh sai, IOaddr>255
out 61h, al ; beep, set/reset key flag
o Indirect: for IO space 0..0FFFFh, via dx register
VÝ dô: mov dx, 378h ; PLT port Addr
mov al, ‘A’ ;41h/ 65d
out dx, al ; 'A' ==> Printer
mov dx,3F8h ;Comm 1 port
in al,dx ;
P&I-Ch2: Protocols 25
Phụ lục PC’s I/O Port Map
Port (hex) Port Assignments
000-00F DMA Controller, đkh truy
nhập trực tiếp 1, 4 kênh 8 bit
020-02F Master PIC 8259A, ưu tiên
ngắt 1, IRQ0 – IRQ7
040-05F PIT, Programmable Interval Timẻ
060-06F Keyboard Port,
070-071 Real Time Clock và 64 byte
CMOS-RAM
080-083 DMA Page Register, giữ địa chỉ
cao

P&I-Ch2: Protocols 26
0A0-0AF PIC #2, Bộ đkh ưu tiên ngắt
số 2, IRQ8 – IRQ15
0C0-0CF DMAC #2, 3 kênh DRQ 16
bit
0E0-0EF reserved, để dành
110-16F AVAILABLE, để dành
170-17F Hard Drive 1 (AT)
180-1EF AVAILABLE
1F0-1FF Hard Drive 0 (AT)
200-20F Game Adapter
P&I-Ch2: Protocols 27
210-217 Expansion Card Ports
220-26F AVAILABLE
278-27F Parallel Port 2/ LPT2
280-2A1 AVAILABLE
2A2-2A3 clock
2B0-2DF EGA/Video
2E2-2E3 Data Acquisition Adapter (AT)
2E8-2EF Serial Port COM4
2F0-2F7 Reserved
2F8-2FF Serial Port COM2
300-31F Prototype Adapter, cho người dùng
phát triển
320-32F AVAILABLE

P&I-Ch2: Protocols 28
340-35F AVAILABLE
360-36F Network, LAN adaptor
370-377 Floppy Disk Controller
378-37F Parallel Port 1, LPT1
380-38F SDLC Adapter
390-39F Cluster Adapter
3A0-3AF reserved
3B0-3BF Monochrome Adapter
3BC-3BF Parallel Port 3

P&I-Ch2: Protocols 29
3C0-3CF EGA/VGA
3D0-3DF Color Graphics Adapter
3E8-3EF Serial Port COM3
3F0-3F7 Floppy Disk Controller
3F8-3FF 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)


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)
2.2. USB Protocol
2.3. ThiÕt kÕ giao thøc truyÒn file gi÷a 2 m¸y tÝnh (LPT/
Comm).

P&I-Ch2: Protocols 31

You might also like