2009年10月2日 星期五

PING 8051 with DM9000 Ethernet

http://class.ruten.com.tw/user/index00.php?s=indiana_jones
I like to share with you how to ping 8051 by ethernet protocol,
It's basic ethernet protocol implementation, and it's not necessory to spend much money to practice it.
If you know How it works in ethernet and ping and TCP/IP protocol, then it 's much helpful to implement
others Internet protocol like UDP , TCP , telent, FTP and http ..
1. Base on my previous article, you should know how to move out from DM9000 a packet , and move in 
   a packt to DM9000 and transmimt it.
   FIFO move in/output is basic function.

2. We can know is IP or ARP from Ethernet header protocol, Then we can implement two function two
   branch this two different protocol.

3. We can easy to use Windows Commander, and type pin 192.168.0.200, then PC will send a broadcast command to etherent , our 8051 will get this packet from DM9000, then know PC is ask DM9000 MAC address. So DM9000 will reply and MAC address to PC.

4. PC will use new MAC address , and send ICMP packet to 8051.

5. When 8051 get new packet from DM9000, and know is IP packet not ARP packet.

6. 8051 will check the IP packet protocol, is ICMP or UDP or TCP and pass to ICMP protocol..

7. 8051 will know hte packe the ICMP request. then prepare another packet to transmit.

8. 8051 well prepare ICMP packet , then Pass back the IP layer .

9. 8051 will prepare IP header and add in front of ICMP Packet , then pass back to Ethernet layer.

10. 8051 move all of IP + ICMP packet and add ethernet header , then move into DM9000, and transmit.

11. PC will get this packet and show delay time.

You can see this, We using DOS windows ping command, to send ICMP packet to 8051+DM9000, Due to 8051 have eto calcute checksum, so it took some times delay ..


We show the meeasge in debug window when 8051+dm9000 got packet or send packet.


you also be able to use wireshark to capture packet, to know your transmit packet are correct or not .

http://class.ruten.com.tw/user/index00.php?s=indiana_jones
if you intersting this toy , you can buy it from this , or contact with me.
jones.hsu@gmail.com ..

2009年10月1日 星期四

PING 8051 Step 0 ( Keil Compiler Bug )

Sometimes you will never think about the bug is there , and hard to image that.
Yester I want to Implement PING function to 8051+DM9000B solution.
I can get a  packet from Ethernet with Broadcast command, but I can't get ICMP packet
Then I started to debug , why  ????

I try to look at the packet read pointer of DM9000, When I got first ARP packet and reply RARP to
Sender, and Sender re-send ICMP packet to me, but I will lost this packet.and there is two possibility:
1. The packet is not correct.
2. my MAC address has problem ..
3. Buffer overflow.

So I try to list down all of input buffer reader pointer, the packet pointer is work fine..
then I try to print it the value of My MAC address, and I found the problem is from My wrong
MAC address..

This is MAC address wirte function, it looks no any problem , is it ?

void dm9k_hash_table(void)
{
UC i;
        for(i = 0; i < 6; i++)
                 iow(DM9000_PAR + i, DEF_MAC_ADDR[i]);

        for(i = 0; i < 8; i++)
                  iow(DM9000_MAR + i, 0x00);
         iow(DM9000_MAR + 7, 0x80);
}

But I try to look at and assemeby CODE, then I got this :

; FUNCTION dm9k_hash_table (BEGIN)

; SOURCE LINE # 81
; SOURCE LINE # 82
; SOURCE LINE # 85

0000 E4              CLR A
0001 F500 R      MOV i,A
0003 ?C0016:
0003 E500 R      MOV A,i
0005 C3             CLR C
0006 9406          SUBB A,#06H
0008 500C         JNC ?C0017

; SOURCE LINE # 86

000A E500 R     MOV A,i
000C 2410         ADD A,#010H
000E 908000     MOV DPTR,#DM9000_Index
0011 F0             MOVX @DPTR,A
0012 0500 R      INC i
0014 80ED         SJMP ?C0016
0016 ?C0017:
0016 7400 R      MOV A,#LOW DEF_MAC_ADDR
0018 2500 R      ADD A,i
001A F8             MOV R0,A
001B E6             MOV A,@R0
001C 908001     MOV DPTR,#DM9000_Data
001F F0             MOVX @DPTR,A

now you should see the problem , Keil C optimize the function, and let separate two block
and only write a data .
So I modify the code as it ..

void dm9k_hash_table(void)

{
UC i;
      for(i = 0; i < 6; i++)
     {
           iow(DM9000_PAR + i, DEF_MAC_ADDR[i]);
      }
      for(i = 0; i < 8; i++)
    {
           iow(DM9000_MAR + i, 0x00);
     }
     iow(DM9000_MAR + 7, 0x80);
}

and I got assemebly as below :

; FUNCTION dm9k_hash_table (BEGIN)

; SOURCE LINE # 81
; SOURCE LINE # 82
; SOURCE LINE # 85
0000 E4              CLR A
0001 F500 R       MOV i,A
0003 ?C0016:
0003 E500 R       MOV A,i
0005 C3              CLR C
0006 9406           SUBB A,#06H
0008 5014           JNC ?C0017
; SOURCE LINE # 86
; SOURCE LINE # 87
000A E500 R      MOV A,i
000C 2410          ADD A,#010H
000E 908000      MOV DPTR,#DM9000_Index
0011 F0              MOVX @DPTR,A
0012 7400 R       MOV A,#LOW DEF_MAC_ADDR
0014 2500 R       ADD A,i
0016 F8              MOV R0,A
0017 E6              MOV A,@R0
0018 A3             INC DPTR
0019 F0             MOVX @DPTR,A

; SOURCE LINE # 88
001A 0500 R      INC i
001C 80E5        SJMP ?C0016

So be careful for coding ...

BR,Jones  joens.hsu@gmail.com

2009年9月27日 星期日

8051 and DM9000 First ARP Packet




Packet in = 0040
Packet Pointer = 3110
Packet Length = 0040
0000 FF FF FF FF FF FF 00 1F C6 37 5B 64 08 06 00 01
0010 08 00 06 04 00 01 00 1F C6 37 5B 64 C0 A8 00 67
0020 00 00 00 00 00 00 C0 A8 00 14 00 00 00 00 00 00
0030 00 00 00 00 00 00 00 00 00 00 00 00 84 7F F9 44

This is first packet which we get from DM9000, When Sender didn’t know target MAC address, sender will send this packet first to ask target MAC address who own this IP address.

a. Ethernet header: Dest MAC + Src MAC + Protocol
First 14 bytes were be define ethernet header, and  0806 is ARM protocol, 0800 is IP protocol.

b. Hardware type:  0001 is Ethernet, 0006 is Token Ring.
c. Protocol Type: 0800 is IP protocol.

d. Hard Address Length:  06 means it has 6 bytes address.

e. Protocol Address length: 04 means it has 4 bytes address.

f. Command: 0001 is ARP request command.

g. Source MAC address:
h. Source IP address:
i. Destination MAC address:
j. Destination IP address:

2009年9月12日 星期六

My New Toy

It's time to share with you my new toy.
Many years ago, I designed TCP/IP protocol in 8051 with AX88796 platform.
It let me upderstood how TCP/IP work, but it only my habit project ...

Now i like introduce it to you, if you interesting in this deisng , then you can follow me to learn
how to design it for your application ..

The Board is base on Megawin MPC82G516 1T 8051 and Davicom DM9000B .
I also put a microSD card connector on board, it can let you store your web page ,
or acqusition data.
If you interesting in it, you can e-mail to me or visit here , i will try my best to upload relative 
information in here ... 

2009年5月25日 星期一

網路通訊協定

1. Ethernet :
乙太網路, 將信號用NRZ 來編碼,並傳至遠方。在通訊的時候主要靠MAC address 來認定傳送封包的發送者或接收者,每一個封包最大1518 個Bytes.

2. MAC addrss :
  為六個BYTES 的數字,每一個乙太網路都需要有這個MAC address, 這六個Bytes 一般表示為 00:21:70:DD:D1:FA 每一Bytes 用冒號來區隔,並為16進制, 廠商可以跟IEEE 來購買此MAC address.

3.IP address:
在網際網路是用IP adress 來定址,此位址為四個Bytes 數字,一般表示為192.168.0.1,每個Bytes 用點號來區隔, 使用0~255 十進制來表示。IP address 在內網可以自己去設定,但是如果要連接WAN網際網路就必須跟ISP (Internet Service Provider)要IP.
我們平常用 ADSL撥號連接,中華電信就會發給我們一個IP address.

3. ARP 通訊協定
網際網路指只認定IP adrress , 但是假如我們是透過Ethernet來傳送IP封包 就必須先知道其MAC address, 此時我們可以透過ARP 封包來查詢此IP address的MAC address.

================== ================================================
在 Ethernet 的實作中當我們收到一個Ethernet 封包時,我們可以透過protocol type 可以得知
此ethernet 封包攜帶著IP 或ARP 或 RARP 的封包。
===================================================================

4. ICMP 通訊協定
我們常用ping ipaddress 來檢查遠方PC 是否有連線, ICMP 則定義了此功能,我們也可以利用此工具得知遠端電腦的反應速度與連線的狀況.

5. TCP 通訊協定
 在TCP通訊協定中定義了port 概念, 兩個bytes 合計65536 個ports, 這些ports 可以把它想像成水管的分支,並傳送給不同的通訊協定。列如port 80 就是HTTP service, Port20 就是FTP service. 但是大部分都是空的,只要兩端指定好就可以相互傳輸資料。TCP 通訊協定有一個重要的特性就是握手式傳輸機制。每一個TCP 封包會有一個Sequence Number 每經過一次傳輸就會將Sequence Number 累加, 系統可以檢查此number 來決定封包是否需要重傳。所以一般我們會說TCP 是必較穩定的傳輸協定,封包也比較不會不見。

6. UDP 通訊協定
 UDP 跟TCP 很類似,唯一的差異就是UDP 不具有握手式的傳輸機制,也就是傳完並不會檢查是否對方已收到所以可靠度就不高但是傳輸效率較高

===================================================================
在TCP/IP實作時,當我們收到一個IP封包時,可以透過IP 封包的欄位知道此IP封包所攜帶的是UDP、TCP還是ICMP的封包!
很多書把ICMP 的方包放在UDP與TCP 的上面, 但實際上已封包的角度來看她其實與 UDP/TCP 同一層...
===================================================================

2009年4月8日 星期三

TTL 的High 與Low

在數位電路中,我們常會用 Logic Gate 來設計,但是由於一般的Logic Gate 都是Push Pull 的結構,所以在設計上要特別注意:
1. 最好使用低電位輸出:在Push Pull 的結構因為high side 電晶體通常考量IC 設計的特性,並不會做大電流的輸出以避免增加IC layout 的面積,但是Low Side 電晶體卻可以承受比較大的電流流入。所以一邊在設計時通常我們會使用Active low 以保證Logic 可以有足夠的能力將輸出拉到low.換句話說,當輸出High 時,由於電流流出,如果此時負載很大,因負載效應他會把輸出倒拉下來此時我們就無法保證書出示不是真正的High.

2. Open Dran or Open collect 有線結及閘的特性,但因為沒有High Side 電晶體所以必須外加提升電阻,而此提升電阻的阻值必須依據實際連接狀況適當加以修改。

3. 雖然TTL input 在open 或沒用到的接腳可以floating ,但在實際電路設計時,強烈建議接到high 或low ,以避免雜訊產生。

4. 承第一點,除了ouput 設計成Low 動作以外我們也建議,在一般的情況,我們也是建議將input 設計成Active Low.

5. 有時序特性的元件,例如D-FF, JK-FF 可以善用RC 充放電的特性,沒用到的接腳加上RC ,以確保開機的啟始狀態。

6. 不同Power source 在互相連接時,最好加一個串聯電組,以避免使用IC 內部的電阻。

2009年4月7日 星期二

幾個電晶體的認知

1. 電晶體是放大器
以前剛開始學電子學的時後,老師總是告訴我們,二極體是PN 元件,只能單存做開關,而電晶體是雙極元件,所以可以做放大。以前也不會覺得有神麼奇怪,總認反正電晶體就是可以做放大器。但是你真正知道瞭解這個放大的意義嗎?在我們的認知中,我們會任為放大就是由小變大,但是各位是否曾想過,如果以物理的質能不滅定律,難道這個小變大的能量是無中生有的嗎?仔細看一下共射極放大電路就可以知道,這個能量是來自於Power Source,電晶體在這裡充其量只能說是一個輸出控制者爾以。

2. 電晶體的工作區
以前老師會教電晶體有三個工作區:飽和區、截止區與工作區,高職時候我們常計算放大率與增益,並總避免讓電晶體工作在飽和區與截止區。但是其實飽和區與截止區卻成為數位電路的基礎,因為電晶體的飽和區與截止區明顯的分別剛好可以用來表是數位電路的0 與1,也因為這樣我們在數位電路中,反而要避免電晶體工作在工作區。

3. 電晶體的VBE
如果電晶體工作在工作區,或應用在放大器電路中,檢查電晶體B,E 兩端是否為0.6V~0.8V,就可以知道電晶體是否有正常工作,所以如果你的擴大機不工作了,檢查一下電晶體的VBE 就可以知道電晶體是否已經燒壞。

4. 電晶體的電壓與電流放大
電晶體可以設計成共射極或共集極放大電路,共射級為電壓放大電路,共集極為電流放大電路,但是在一般根據我們前面所提,電流放大的source 是從power 所提供,所以若當電流放大時,必需選用能承受足夠大電流的電晶體。