You are on page 1of 8

BO CO TIN PROJECT 2 TUN 8 Hng dn : Thy Hong Mnh Thng. Sinh vin thc hin: 1.

1. Phm Ngc Thng 20092549 DTVT01 K54 2. Nguyn Vn Ton 20092769 DTVT02 K54 3. Phan Vn Thng 20093622 DTVT06 K54

Ni dung bo co: Gii thiu v chun giao tip SPI v cu trc FAT32.

Gii thiu v chun giao tip SPI. 1. Gii thiu. SPI (Serial Peripheral Bus) l mt chun truyn thng ni tip tc cao do hng Motorola xut. y l kiu truyn thng Master-Slave, trong c 1 chip Master iu phi qu trnh truyn thng v cc chip Slaves c iu khin bi Master v th truyn thng ch xy ra gia Master v Slave. SPI l mt cch truyn song cng (full duplex) ngha l ti cng mt thi im qu trnh truyn v nhn c th xy ra ng thi. SPI i khi c gi l chun truyn thng 4 dy v c 4 ng giao tip trong chun giao tip ny. y ti ca chng ta l giao tip gia vi iu khin PIC18F4520 v th nh thng qua chun SPI. Chnh v vy vi iu khin PIC s ng vai tr l Master, cn th nh ng vai tr l Slave. Giao tip gia PIC18F4520 v th nh s thng qua 4 ng l SCK (Serial Clock) RC3/SCK/SCL, Serial Data Out (SDO) RC5/SDO, Serial Data In (SDI) RC4/SDI/SDA v mt tn hiu chn chip Slave Select (~SS) RC0 . y chn chn chip Slave Select hot ng mc thp. Trong giao tip gia vi iu khin PIC v th SD card, chn SCK ca PIC s c ni vi chn CLK ca SD. Chn ny cung cp xung nhp cho vic giao tip gia th nh v PIC. C th khi c 1 xung thay i t 0 ln 1 hoc 1 xung 0 th s c mt bit c gi i ( hoc nhn v ). SDO (Serial Data Out ): Chn ny ng vai tr truyn d liu ti th nh. N s c ni vi DI (Data Input) ca th nh. SDI (Serial Data input ): Nhn d liu t th nh gi v qua chn DO (Data output) ca th nh. SS(Slave Select ) : Chn chn chip, c kt ni vi chn CS(Chip select) ca th nh. 2. Thanh ghi h tr cho ch SPI. C 4 thanh ghi s dng trong ch SPI. l thanh ghi trng thi SSPSTAT, thanh ghi iu khin 1 (SSPCON1), thanh ghi m truyn nhn ( SSPBUF) dng lu d liu tm thi khi truyn hoc nhn, thanh ghi dch SSPSR dng dch d liu vo hoc ra. N l thanh ghi m cho vic c v ghi d liu. - Thanh ghi trng thi (SSPSTAT) :

I.

Bit 7 (SMP): SPI Master mode. SMP = 1 th d liu u vo c ly mu ti thi im cui ca d liu u ra. Bit 6(CKE) : SPI Clock Select bit, nu bit ny bng 1 th d liu s c truyn khi xung clock chuyn th 1 xung 0, cn nu bng 0 th d liu c truyn khi xung clock c chuyn t 0 ln 1. Bit 0(BF) : Buffer Full Status bit, bit bo nhn xong, tc l bit BF c set ln 1 khi qu trnh nhn hon tt , SSPBUF y. Cc bit khc khng dng cho ch SPI. - Thanh ghi iu khin (SSPCON1) : L thanh ghi thit lp cc ch iu khin cho ch SPI.

Bit 7(WCOL) : Write Collision Detect bit, bit pht hin xung t, nu thanh ghi SSPxBUF c vit khi n vn ang truyn mt t trc th bit WCOL s c set ln 1. Nu khng c xung t th n s l 0. Bit 6 (SSPOV - Receive Overflow Indicator bit): Bit bo trn trong ch Slave, n c set ln 1 khi mt byte mi c nhn trong khi thanh ghi SSPBUF vn gi d liu trc . Trong trng hp ny d liu trong SSPSR s b mt. N gi gi tr 0 nu khng c trn.

Bit 5 (SSPEN:Master Synchronous Serial Port Enable bit): Bit ny c set ln 1 th cc chn RC3,RC4,RC5 v chn SS s c s dng trong ch SPI, nu n bng 0 th s c s dng nh mt cng vo ra. Bit 4(CKP:Clock Polarity Select bit): CKP = 1 th trng thi ngh ca xung clock l mc cao, cn nu bng 0 th trng thi ngh s l mc thp. Bit 3-0: (SSPM<3:0>:Master Synchronous Serial Port Mode Select bits): Cc bit cn li thit lp tc xung clock da vo tc my. 3. Hot ng. Khi bt u SPI, mt s bt s c thit lp cho php cc ch ca SPI. Cc bit cn qua tm l SSPCON1<5:0> v SSPSTAT<7:6>. Cc bit iu khin ny s cho php cc ch hot ng sau : Master mode (SCK l xung ra) Slave mode (SCK l xung vo) Clock Polarity (Trng thi ngh ca xung clock) Data Input Sample Phase : Clock Edge (d liu c gi i ti sn ln hoc sn xung ca SCK) Clock rate, Slave select mode(); Thanh ghi dch SSPSR dch d liu vo v ra ca PIC, bt u t MSB. Thanh ghi SSPBUF cha d liu c vit t SSPSR cho n khi d liu nhn sn sng. D liu truyn nhn l 8bit, byte ny c move n thanh ghi SSPBUF. Sau b m y, bit BF,SSPIF c set. B m nhn d liu kp cho php byte tip theo bt u vic nhn trc khi c d liu va nhn. Bt k vic ghi vo thanh ghi SSPBUF trong qu trnh truyn nhn d liu u gy ra xung t v khng c chp nhn v bit bo xung t WCOL s c set. qu trnh ghi tip theo thnh cng th bit WCOL phi c set xung 0 s dng phn mm. Trong qu trnh truyn nhn c hiu lc th bit SSPBUF nn c c trc khi byte tip theo ca d liu c ghi vo SSPBUF. Bit BF ch ra SSPBUF c np vi nhn d liu (vic truyn thnh cng). Khi d liu c c th BF c xa.D liu khng hp l nu SPI ch truyn. Phng php ngt s dng xc nh khi no dch chuyn hon tt. II. Cu trc a cng s dng FAT32. 1. M t cu trc FAT 32.

Bng 2.1. M t phn vng ca FAT32. Offset Bt u ca phn vng Bt u + s sector c nh trc (reserved sector) Description Boot Sector Bng Fat

Vng d liu Bt u + s sector c nh trc + ( S sector trong (Bt u ti bng FAT32) cluster th 2) Master Boot Record: Nm sector u tin ca a cng cylinder 0, Head 0, Sector 1. N cha on code u tin m my tnh chy sau khi khi ng v bt u qun l cc chng trnh trong a cng. N cn cha bng phn vng xc nh cc phn vng khc nhau trong a cng. Nu c li xy ra 512 byte ny th nguy c thay cng mi l rt ln ! Bng 2.2. Master Boot Record Offset M t 000h M thc thi ( boots computer) 1BEh Ch mc phn vng u tin 1Ceh Ch mc phn vng th hai 1Deh Ch mc phn vng th ba 1Eeh Ch mc phn vng th t 1Feh 2 byte nh du ( 55h AAh) Kch thc 446 bytes 16 byte 16 byte 16 byte 16 byte 2 byte

M ha cylinder/Sector ly c Sector t mc trn ta thc hin php AND vi 3Fh ( ly 6 bit cui) ly c s Cylinder ta ly byte cao thc hin php OR vi byte thy c dch tri 2 ln sau khi AND vi C0h. Bng 2.3. Cu trc Cylinder/Sector. 15 14 9 8 Cylinder bit 7 -> 0 5 1 Sector bit 5->0

7 6 Cylinder bit 9+8

Ch mc phn vng (Partion) Bng 2.4. Ch mc phn vng. Offset M t 00h Trng thi hin thi ca phn vng( 00h= khng hot ng, 80h= hot ng) 01h Bt u ca phn vng( Head) 02h Bt u ca phn vng( Cylinder/ Sector) (bng di) 04h Loi phn vng(bng di) 05h Kt thc ca phn vng(Head) 06h Kt thc ca phn vng( Cylinder/ Sector) 08h S sector gia MBR v sector u tin ca phn vng 0Ch S sector trong phn vng Kch thc 1 byte 1 byte 1 word 1 byte 1 byte 1 word 1 double word 1 double word

FAT32 Boot Record Thng tin ny nm trong sector u tin ca mipartition. Bng 2.5. Fat32 Boot Record. Offset 00h 03h 0Bh 0Dh 0Eh 10h 11h 13h Description Lnh nhy ti m khi ng Tn hng sn xut S byte trong vect S sect trong 1 cluster Sect c bo v S bn copy ca bn FAT S ch mc trong phn root S sector trong phn vng nh hn 32MB Size 3 Bytes 8 Bytes 1 Word 1 Byte 1 Word 1 Byte 1 Word 1 Word

15h 16h 18h 1Ah 1Ch 20h 24h 28h

M t phng tin lu tr ( f8h cho a cng) S sector cho bng FAT S sector trong 1 track cho ngt 13h S head S sector n trong phn vng S sector trong phn vng S lun l trong phn vng

1 Byte 1 Word 1 Word 1 Word 1 Double Word 1 Double Word 1 Double Word

C (Bits 0-4 Cho php ng nhp sao chpFAT) (Bit 7 Cho bit Mirroring FAT c 1 Word bt hoc tt)Nu Mirroring FAT l c bt th thng tin FAT cho php copy bi bit 0 -4 h Phin bn FAT32 Drive (High Byte = Major 1 Word Version, Low Byte = Minor Version) S Cluster bt u ca th mc gc 1 Double Word

2Ah 2Ch

30h 32h 34h 40h 41h 42h 43h 47h 52h 5Ah 1FEh

S sect v h thng thng tin file sect(See Structure Below) (tham chiu ti t u ca 1 Word Partition) S Sector ca th Sector (tham chiu ti u 1 Word ca Partition) S byte d tr S logical drive ca Partition 1 byte cha dng n Extended Signature (29h) S Serial ca phn vng Tn phn vng Tn Fat (FAT32) Code 2 byte nh du (55h AAh) 12 Bytes 1 Byte 1 Byte 1 Byte 1 Double Word 11 Bytes 8 Bytes 420 Bytes 2 Bytes

Bng FAT - ngha ca Cluster. Mt Cluster l mt tp hp cc Sector cha thng tin trong a cng.Mt Cluster 4K cha 8 Sector (512*8=4096). Mi mt Cluster c biu din bng mt trong bng FAT. Khi nhn vo s ca cc trong bng FAT ta c th bit c cluster c d

liu hay cha, d liu kt thc hay cha, nu cha th cluster no sau n. Tt c d liu trong 1 phn vng u bt u t cluster th hai (ngay sau Root Directory). Nu c gi tr 0 th cluster ng vi n khng c d liu, nu c gi tr 0FFFFFFFh th l cui cng trong chui d liu. Mi mt file trong th SD phi c lu bt u t 1 cluster. Ngoi ra file c th tri di trn nhiu cluster khc nhau (khng nht thit lin tc) Bng FAT lp ra ch ra khi c 1 file, chng trnh cn c nhng cluster no.

Bng 2.6 . ngha ca cc m FAT M FAT ngha 00000000h Cluster trng 00000002h-0FFFFFEFh Cluster c d liu, ch n cluster tip theo ca file 0FFFFFF0h-0FFFFFF6h Cluster nh trc ( reserved cluster) 0FFFFFFF7h Cluster li 0FFFFFF8h-0FFFFFFFh Cluster c d liu, Cluster cui cng ca file

2. Cc thanh ghi trong SD Card Bng 2.7. Cc thanh ghi ca SD Card Tn rng M t CID 128 Ch s ca Card; nu c nhiu Card, mi Card s c mt CID khc nhau. RCA 16 Relative card address; a ch h thng ca mt Card. DSR 16 Driver Stage Register; cu hnh driver u ra ca Card CSD 128 Card Specific Data; thng tin v iu kin hot ng ca Card. SCR 64 SD Configuration Register; OCR 32 Thanh ghi iu kin hot ng SSR 512 SD Status; thng tin v cc tnh cht ca Card. CSR 32 Card Status;