Yeni Wireless&Wired Ethernetli Projemin Prototip Çalışması

Başlatan haydarpasamikro, 10 Şubat 2013, 22:46:11

Burak B

ENC28J60' ı nasıl kullandığınızı gösteren bir kod verebilir misiniz ?
"... a healthy dose of paranoia leads to better systems." Jack Ganssle

bymrz

@haydarpasamikro hocam,

Yanlış anlamadıysam TCP-IP, vs lerin "stack" ini kendinizin yazdığını söylemişsiniz doğru mu bu?

haydarpasamikro

Evet. Örnek kodları kendi projeme uygun olarak geliştiriyorum. Böylece istediğim gibi düzenleyebiliyorum.

mesaj birleştirme:: 13 Şubat 2013, 09:46:28

Alıntı yapılan: ByteMaster - 13 Şubat 2013, 00:12:57
ENC28J60' ı nasıl kullandığınızı gösteren bir kod verebilir misiniz ?

Elbette.Bibirimize faydamız olsun. Ülkemiz kazansın.

mesaj birleştirme:: 13 Şubat 2013, 09:51:08


//-------------------------------------------------------------------------------------------------
void MAC_Init(void){
		
		MAC_SoftReset();   		

		MAC_Banksel(0);
		MAC_WriteCtrlReg(ERXSTL,(unsigned char)( RXSTART & 0x00FF));
		MAC_WriteCtrlReg(ERXSTH,(unsigned char)((RXSTART & 0xFF00) >> 8));
  		MAC_WriteCtrlReg(ERXNDL,(unsigned char)( RXEND   & 0x00FF));
  		MAC_WriteCtrlReg(ERXNDH,(unsigned char)((RXEND   & 0xFF00) >> 8));

		//MAC_WriteCtrlReg(ETXSTL,(unsigned char)( TXSTART & 0x00FF));
		//MAC_WriteCtrlReg(ETXSTH,(unsigned char)((TXSTART & 0xFF00) >> 8));
  		//MAC_WriteCtrlReg(ETXNDL,(unsigned char)( TXEND   & 0x00FF));
  		//MAC_WriteCtrlReg(ETXNDH,(unsigned char)((TXEND   & 0xFF00) >> 8));

		// Make sure Rx Read ptr is at the start of Rx segment
  		MAC_WriteCtrlReg(ERXRDPTL, (unsigned char)( RXSTART & 0x00FF));
  		MAC_WriteCtrlReg(ERXRDPTH, (unsigned char)((RXSTART & 0xFF00) >> 8));

		// Make sure Rx Write ptr is at the start of Rx segment
  		MAC_WriteCtrlReg(ERXWRPTL, (unsigned char)( RXSTART & 0x00FF));
  		MAC_WriteCtrlReg(ERXWRPTH, (unsigned char)((RXSTART & 0xFF00) >> 8));  
		
		MAC_Banksel(1);
		//MAC_WriteCtrlReg(ERXFCON,( ERXFCON_UCEN + ERXFCON_CRCEN + ERXFCON_BCEN));
		MAC_WriteCtrlReg(ERXFCON,(ERXFCON_UCEN  + ERXFCON_BCEN));

		// Initialise the MAC registers
  		MAC_Banksel(2);                             
  		MAC_SetBitField(MACON1, MACON1_MARXEN);     	// Enable reception of frames
		MAC_WriteCtrlReg(MACLCON1, 0x0F);				//increase for long cables max = 0x15
  		MAC_WriteCtrlReg(MACLCON2, 0x3F);				//increase for long cables max = 0x3F
  		
		//MAC_WriteCtrlReg(MACON3, 0x10);   
		MAC_WriteCtrlReg(MACON3, MACON3_FRMLNEN + MACON3_TXCRCEN + MACON3_PADCFG0);   
         												// Type / len field will be checked + 
														// MAC will append valid CRC + 
														// All small packets will be padded       

		//MAC_SetBitField(MACON4, MACON4_DEFER + MACON4_BPEN + MACON4_NOBKOFF); 		// applies to half-duplex     
  		MAC_SetBitField(MACON4, MACON4_DEFER); 									// applies to half-duplex     
  		MAC_WriteCtrlReg(MAMXFLL, (unsigned char)( MAXFRAMELEN & 0x00FF));     // set max frame len
  		MAC_WriteCtrlReg(MAMXFLH, (unsigned char)((MAXFRAMELEN & 0xFF00) >> 8));
  		MAC_WriteCtrlReg(MABBIPG, 0x12);             	// back to back interpacket gap. set as per data sheet
  		MAC_WriteCtrlReg(MAIPGL , 0x12);             	// non back to back interpacket gap. set as per data sheet
  		MAC_WriteCtrlReg(MAIPGH , 0x0C);

  
		//Program our MAC address
  		MAC_Banksel(3);              
  		MAC_WriteCtrlReg(MAADR1, property.ModuleMACAddr[0]);   
  		MAC_WriteCtrlReg(MAADR2, property.ModuleMACAddr[1]);  
  		MAC_WriteCtrlReg(MAADR3, property.ModuleMACAddr[2]);
  		MAC_WriteCtrlReg(MAADR4, property.ModuleMACAddr[3]);
 	 	MAC_WriteCtrlReg(MAADR5, property.ModuleMACAddr[4]);
  		MAC_WriteCtrlReg(MAADR6, property.ModuleMACAddr[5]);

		//EECON Clockout disable
		MAC_WriteCtrlReg(ECOCON, 0);
		// Get the Rev ID so that we can implement the correct errata workarounds
		ENCRevID = MAC_ReadETHCtrlReg(EREVID); 

  		// Initialise the PHY registes PHY Register controlled in Bank-2. PHY write and Read functions do it inside
		MAC_WritePHYReg(PHIE, PHIE_PLNKIE | PHIE_PGEIE);	//Link Interrupt PHY Enable
  		MAC_WritePHYReg(PHCON1, 0x0000);
  		MAC_WritePHYReg(PHCON2, PHCON2_HDLDIS);
		//MAC_WritePHYReg(PHLCON, 0x3C12);//LedA-Link&Receive  LedB-Transmit status
		MAC_WritePHYReg(PHLCON, 0x3000 | PHLCON_LACFG3 | PHLCON_LACFG2 | PHLCON_LBCFG0 | PHLCON_STRCH);//LedA-Link&Receive  LedB-Transmit status

		MAC_SetBitField(EIE, EIE_LINKIE | EIE_INTIE);		//Link Interrupt Enable
		//MAC_SetBitField(EIE, EIE_PKTIE | EIE_INTIE);		//Receive Interrupt Enable
  		MAC_WriteCtrlReg(ECON1,  ECON1_RXEN);     	//Enable the chip for reception of packets

		MAC_NextPtrL = ((unsigned char) RXSTART & 0x00FF);
		MAC_NextPtrH = (unsigned char) ((RXSTART & 0xFF00) >> 8);

		DelayMs(100);	
}
//-------------------------------------------------------------------------------------------------
unsigned int MAC_Write(unsigned int bytLen, unsigned char *ptrdata){
	
	volatile unsigned int bytaddress = TXSTART;		
	unsigned char bytControl = 0x00;
	unsigned int timeout = 0;
   	unsigned char tryout = 0;
	unsigned int cacheEReadPtr = 0;

		MAC_Banksel(0);		
		MAC_WriteCtrlReg(ETXSTL,(unsigned char)( TXSTART & 0x00FF));	// write ptr to start of Tx packet
		MAC_WriteCtrlReg(ETXSTH,(unsigned char)((TXSTART & 0xFF00) >> 8));

		MAC_WriteCtrlReg(EWRPTL,(unsigned char)( TXSTART & 0x00FF));	// Set write buffer to point to start of Tx Buffer
		MAC_WriteCtrlReg(EWRPTH,(unsigned char)((TXSTART & 0xFF00) >> 8));

		MAC_WriteMACBuffer(&bytControl, 1);
		//bytaddress++;

		MAC_WriteMACBuffer(ptrdata, bytLen);	// write packet. Assume correct formating dst, src, type, data 			

		bytaddress += bytLen; 		
		
		
		MAC_WriteCtrlReg(ETXNDL,(unsigned char)( bytaddress  & 0x00FF));	// Tell MAC when the end of the packet is
  		MAC_WriteCtrlReg(ETXNDH,(unsigned char)((bytaddress  & 0xFF00) >> 8));

		 		
		MAC_ClearBitField(EIR,EIR_TXIF);	
		//MAC_SetBitField(EIE, EIE_TXIE |EIE_INTIE);

	   	MAC_ErrataFix();
		MAC_SetBitField(ECON1, ECON1_TXRTS);				// begin transmitting;

		
		while ( !(MAC_ReadETHCtrlReg(EIR) & (EIR_TXIF | EIR_TXERIF)) && (++timeout < 1000) );
		

		MAC_ClearBitField(ECON1, ECON1_TXRTS);

		MAC_Banksel(0);
		bytaddress++;
		cacheEReadPtr = bytaddress; 
		MAC_WriteCtrlReg(ERDPTL,(unsigned char)( bytaddress  & 0x00FF));	
  		MAC_WriteCtrlReg(ERDPTH,(unsigned char)((bytaddress  & 0xFF00) >> 8));
		MAC_ReadMACBuffer(&TxStatus.v[0], 7);


		if(MAC_ReadETHCtrlReg(EIR) & (EIR_TXERIF) || timeout >= 1000){
			if( MAC_ReadETHCtrlReg(ESTAT) & (ESTAT_TXABRT) ){
				for(tryout = 0; tryout < 16; tryout++){
					//if( MAC_ReadETHCtrlReg(ESTAT) & (ESTAT_LATECOL) ){	//Late Collision	ERRATA READ STATUS VECTOR LATECOL BIT
					if (TxStatus.bits.LateCollision){
						MAC_ErrataFix();							
						MAC_SetBitField(ECON1, ECON1_TXRTS);
						timeout = 0;
						while ( !(MAC_ReadETHCtrlReg(EIR) & (EIR_TXIF | EIR_TXERIF)) && (++timeout < 1000));
						MAC_ClearBitField(ECON1, ECON1_TXRTS);
						MAC_WriteCtrlReg(ERDPTL,(unsigned char)( cacheEReadPtr  & 0x00FF));	
  						MAC_WriteCtrlReg(ERDPTH,(unsigned char)((cacheEReadPtr  & 0xFF00) >> 8));
						MAC_ReadMACBuffer(&TxStatus.v[0], 7);
					}else{
						break;
					}
				}		   			

				MAC_ClearBitField(ECON1, ECON1_TXRTS);
				MAC_ClearBitField(EIR, EIR_TXERIF | EIR_TXIF);
				MAC_ClearBitField(ESTAT, ESTAT_TXABRT | ESTAT_LATECOL);

				if(tryout >=16){
					Mac_Reset_Type = MAC_WRITE_ERROR_TRYOUT_RESET;
					MAC_HardReset();					
					MAC_Init();	 								
					return 0;
				}else{
					return 1;
				}
			}else{
				Mac_Reset_Type = MAC_WRITE_ERROR_RESET;
				MAC_HardReset(); 				
				MAC_Init();	  				
				return 0;
			} 		
		
		}else{
			MAC_ClearBitField(EIR,EIR_TXIF);
			return 1;
		} 
		
}
//------------------------------------------------------------------------------------------------- 
void MAC_Read(void){	//returns received byte count low byte 	 	
			  
		MAC_Banksel(0);
		
		MAC_WriteCtrlReg(ERDPTL, MAC_NextPtrL);		//write this value to read buffer ptr
		MAC_WriteCtrlReg(ERDPTH, MAC_NextPtrH);  
		    
		MAC_ReadMACBuffer(&MACRxStatus.nextPtrL, sizeof(struct stMACRxStatus));		//read next packet pointer(2) + bytecount(2) + receive status(2) 				
		
		MACRxDataLen = (MACRxStatus.bytCountL);
		MACRxDataLen |= (unsigned int)(MACRxStatus.bytCountH << 8);  

		if(MACRxDataLen > MAX_RXDATA_LEN){	//datalen is longer
			MACRxDataLen = MAX_RXDATA_LEN;
		} 
								
		MAC_ReadMACBuffer(&MACRxBuffer[0], MACRxDataLen);//max data count MAX_RXDATA_LEN			
				  																			
		MAC_Banksel(0);
		MAC_WriteCtrlReg(ERXRDPTL, MACRxStatus.nextPtrL);		// free up ENC memory my adjusting the Rx Read ptr
		MAC_WriteCtrlReg(ERXRDPTH, MACRxStatus.nextPtrH);
		 
		// decrement packet counter
		MAC_SetBitField(ECON2, ECON2_PKTDEC);
	
		MAC_NextPtrL = MACRxStatus.nextPtrL;
		MAC_NextPtrH = MACRxStatus.nextPtrH; 	
}

//#################################################################################################
//ETHERNET FUNCTIONS
//#################################################################################################
unsigned int MAC_CalculateCRC(unsigned int len_ip_header, unsigned char *buf){
	unsigned int word16 = 0;
	unsigned long sum = 0;
	unsigned int i;

		// make 16 bit words out of every two adjacent 8 bit words in the packet
		// and add them up
		for (i=0;i<len_ip_header;i=i+2){
			word16 =(((unsigned int)*buf<<8)& 0xFF00)+((unsigned int)*(buf+1)& 0xFF);
			sum = sum + (unsigned long) word16;
			buf += 2;	
		}
		
		// take only 16 bits out of the 32 bit sum and add up the carries
		while (sum>>16)
		  sum = (sum & 0xFFFF)+(sum >> 16);
	
		// one's complement the result
		sum = ~sum;
	
	return ((unsigned int) sum);
}
//------------------------------------------------------------------------------------------------- 
unsigned long MAC_CalculateFCS(unsigned int glen, unsigned char *ptrdata){
               
         unsigned long CRCVal = 0xFFFFFFFFL;
		 unsigned long CRCBuf;
         int i;

		 	for (i = 0; i < glen; i++){
        		CRCVal = (CRCVal >> 8) ^ CRCTable[(CRCVal & 0xff) ^ (*ptrdata++)];
    		}
    	//Toggle yapmadan elde et
		CRCVal ^= 0xffffffff; // Toggle operation
		
		CRCBuf = (CRCVal >> 24) & 0x000000FF;
		CRCBuf |= (CRCVal >> 8) & 0x0000FF00;
		CRCBuf |= (CRCVal << 8) & 0x00FF0000;
		CRCBuf |= (CRCVal << 24) & 0xFF000000;    
 
    return CRCBuf;
}
//------------------------------------------------------------------------------------------------- 
void MAC_Restart(unsigned char gresetreason){

		Mac_Reset_Type = gresetreason;
		MAC_HardReset();					
		MAC_Init();	  
}
//------------------------------------------------------------------------------------------------- 
void MAC_HardReset(void){
			
	PCF2WriteBuffer = PCF2WriteBuffer & 0x0E8;	//MAC Reset 4.bit PCF	0 active for ON
	PCF2_Write(PCF2WriteBuffer);
	DelayMs(10);	
	PCF2WriteBuffer = PCF2WriteBuffer | 0x10;	//0x17 MAC Reset 4.bit PCF	1 pasive for OFF 	
	PCF2_Write(PCF2WriteBuffer);	 	
	DelayMs(10);

}
//-------------------------------------------------------------------------------------------------
void MAC_Select(bit bitstate){ 
	if(bitstate){
		MAC_CS = 0;//active low chip slave select
	}else{
   		MAC_CS = 1;
	}
}
//-------------------------------------------------------------------------------------------------
unsigned char MAC_Transfer(unsigned char byte){
	unsigned char counter = 0;

		  MAC_SCLK = 0; 

		  for(counter = 8; counter; counter--){
		    
			if (byte & 0x80){
		      MOSI = 1;
		    }else{
		      MOSI = 0;
		    }

			//maybe delay

			byte <<= 1;
		    
			MAC_SCLK = 1;    
		    
			if (MISO){
		      byte |= 0x01;
			}

			//maybe delay

		    MAC_SCLK = 0;     
		  }
		  return(byte);
}
//-------------------------------------------------------------------------------------------------
void MAC_WriteCtrlReg(unsigned char bytAddress,unsigned char bytData){	  
	
		bytAddress |= WCR_OP;       
	  	MAC_Select(TRUE);
	  	MAC_Transfer(bytAddress);
	  	MAC_Transfer(bytData);
	  	MAC_Select(FALSE);  
}
//-------------------------------------------------------------------------------------------------
unsigned char MAC_ReadETHCtrlReg(unsigned char bytAddress){	  
	unsigned char bytData = 0;
		
		bytAddress |= RCR_OP;     
		MAC_Select(TRUE);
		MAC_Transfer(bytAddress);
		bytData = MAC_Transfer(0xFF);
		MAC_Select(FALSE);

	return bytData;   
}
//-------------------------------------------------------------------------------------------------
unsigned char MAC_ReadMACCtrlReg(unsigned char bytAddress){	  
	unsigned char bytData = 0;
		
		bytAddress |= RCR_OP;     
		MAC_Select(TRUE);
		MAC_Transfer(bytAddress);
		bytData = MAC_Transfer(0xFF);	//dummy read byte
		bytData = MAC_Transfer(0xFF);	
		MAC_Select(FALSE);

	return bytData;   
} 
//-------------------------------------------------------------------------------------------------
void MAC_SetBitField(unsigned char bytAddress, unsigned char bytData){

		bytAddress |= BFS_OP;	
		MAC_Select(TRUE);
		MAC_Transfer(bytAddress);
		MAC_Transfer(bytData);
		MAC_Select(FALSE);
}
//-------------------------------------------------------------------------------------------------
void MAC_ClearBitField(unsigned char bytAddress, unsigned char bytData){

		bytAddress |= BFC_OP;	
		MAC_Select(TRUE);
		MAC_Transfer(bytAddress);
		MAC_Transfer(bytData);
		MAC_Select(FALSE);
}
//-------------------------------------------------------------------------------------------------
void MAC_WriteMACBuffer(unsigned char *bytBuffer, unsigned int bytLen){
	unsigned int i = 0;

		MAC_Select(TRUE);
		MAC_Transfer(WBM_OP);
		for(i = 0; i < bytLen; i++){			
	   		MAC_Transfer(*bytBuffer++);					
		}
		MAC_Select(FALSE);		
}
//-------------------------------------------------------------------------------------------------
void MAC_ReadMACBuffer(unsigned char *bytBuffer, unsigned int bytLen){
	unsigned int i = 0;
		
		MAC_Select(TRUE);
		MAC_Transfer(RBM_OP);
		for(i = 0; i < bytLen; i++){ 		
	   		*bytBuffer++ = MAC_Transfer(0xFF);						
		}
		MAC_Select(FALSE);	
}
//-------------------------------------------------------------------------------------------------
void MAC_Banksel(unsigned char gbank){
	unsigned char gtemp = 0;

		if(gbank > 3)
			return;
		
		gtemp = MAC_ReadETHCtrlReg(ECON1);		// Read ECON1 register
		gtemp &= ~ECON1_BSEL;					// mask off the BSEL bits
		gtemp |= gbank;						   	// set BSEL bits
		MAC_WriteCtrlReg(ECON1, gtemp);		   	// write new values back to ENC28J60
}
//-------------------------------------------------------------------------------------------------
void MAC_WritePHYReg(unsigned char bytAddress, unsigned int bytWord){
		
		MAC_Banksel(2);	
		MAC_WriteCtrlReg(MIREGADR, bytAddress);
		MAC_WriteCtrlReg(MIWRL, (unsigned char)bytWord);
		MAC_WriteCtrlReg(MIWRH, (unsigned char)(bytWord >> 8));	//Read Busy or waiy 10.24us	
}
//-------------------------------------------------------------------------------------------------
unsigned int MAC_ReadPHYReg(unsigned char bytAddress){	  	
	unsigned int bytWord;	
	unsigned char bytStat;
	unsigned int timeout = 1000;

		MAC_Banksel(2);
		MAC_WriteCtrlReg(MIREGADR, bytAddress);
		MAC_SetBitField(MICMD, MICMD_MIIRD);	//10.24us delay may be required	   	
		
		MAC_ENC_Error = 0;//must be clear before loop
		do{
			bytStat = MAC_ReadMACCtrlReg(MISTAT);
			timeout--; 
			if(timeout == 0){
			  	MAC_ENC_Error = 1;//global flag for error
				break;
			}
		}while(bytStat & MISTAT_BUSY);

		MAC_ClearBitField(MICMD, MICMD_MIIRD);
		bytWord = (unsigned int) MAC_ReadMACCtrlReg(MIRDL);
		bytWord |= ((unsigned int) MAC_ReadMACCtrlReg(MIRDH) << 8);	
	
	return bytWord;
}
//-------------------------------------------------------------------------------------------------
void MAC_SoftReset(void){

		MAC_Select(TRUE);
		MAC_Transfer(RESET_OP);	
		MAC_Select(FALSE); 
		DelayMs(10);	//errata wait 1ms et least
} 
//-------------------------------------------------------------------------------------------------
void MAC_ErrataFix(void){
		
		MAC_SetBitField(ECON1, ECON1_TXRST);
		MAC_ClearBitField(ECON1, ECON1_TXRST);
	   	MAC_ClearBitField(EIR, EIR_TXERIF | EIR_TXIF);
}
/*
//-------------------------------------------------------------------------------------------------
void MAC_Initial_Connect_Status(void){	 		
			
		MAC_PHYReadBuffer = MAC_ReadPHYReg(PHSTAT1); 			

		if(MAC_PHYReadBuffer & PHSTAT1_LLSTAT){	   								
			MACLinkStatus = ON;
		}else{
			MACLinkStatus = OFF;								
		}
		LCD_PutMACLinkStatus();										
}
*/
//-------------------------------------------------------------------------------------------------
void MAC_GetLinkStatus(void){

		MAC_PHYReadBuffer = MAC_ReadPHYReg(PHIR); 
		if( MAC_PHYReadBuffer & PHIR_PLNKIF){
			MAC_PHYReadBuffer = MAC_ReadPHYReg(PHSTAT2);
			if(MAC_PHYReadBuffer & PHSTAT2_LSTAT){
				MACLinkStatus = ON;
				RCCToPC_DebugStatus(DEBUGSTAT_LINKUP);
			}else{
				MACLinkStatus = OFF;
			}
			LCD_PutMACLinkStatus();	
		} 
}
//-------------------------------------------------------------------------------------------------
void MAC_TestChipHardwareStatus(void){
		
		MAC_PHYReadBuffer = MAC_ReadPHYReg(PHID1); 
		if( MAC_PHYReadBuffer != 0x0083){ // ENC28J60 Microchip OUI number hex code
			LCD_Clear();
			LCD_MessageWriteXY(0,0, "Donanim Hatasi!     ");	
			LCD_MessageWriteXY(1,0, "--------------------");
			LCD_MessageWriteXY(2,0, "ENC28J60 okunamiyor ");
			LCD_MessageWriteXY(3,0, "                    ");	
			DelayMs(3000);
		}
}	



Başlık Bilgisi

//Ethernet Definitions ============================================================================
#define MOSI		P1_2
#define MISO		P1_3
#define MAC_SCLK	P1_1
#define MAC_CS		P1_0
//-------------------------------------------------------------------------------------------------
#define RBM_OP		0x3A
#define RCR_OP      0x00
#define WCR_OP      0x40
#define WBM_OP      0x7A
#define BFS_OP      0x80
#define BFC_OP      0xA0
#define RESET_OP    0xFF

//Bank 0 registers -----
#define ERDPTL		0x00
#define ERDPTH		0x01
#define EWRPTL		0x02
#define EWRPTH		0x03
#define ETXSTL		0x04
#define ETXSTH		0x05
#define ETXNDL		0x06
#define ETXNDH		0x07
#define ERXSTL		0x08
#define ERXSTH		0x09
#define ERXNDL		0x0A
#define ERXNDH		0x0B
#define ERXRDPTL	0x0C
#define ERXRDPTH	0x0D
#define ERXWRPTL	0x0E
#define ERXWRPTH	0x0F
#define EDMASTL		0x10
#define EDMASTH		0x11
#define EDMANDL		0x12
#define EDMANDH		0x13
#define EDMADSTL	0x14
#define EDMADSTH	0x15
#define EDMACSL		0x16
#define EDMACSH		0x17
#define EIE         0x1B
#define EIR         0x1C
#define ESTAT		0x1D
#define ECON2		0x1E
#define ECON1		0x1F

//Bank 1 registers -----
#define EHT0		0x00
#define EHT1		0x01
#define EHT2		0x02
#define EHT3		0x03
#define EHT4		0x04
#define EHT5		0x05
#define EHT6		0x06
#define EHT7		0x07
#define EPMM0		0x08
#define EPMM1		0x09
#define EPMM2		0x0A
#define EPMM3		0x0B
#define EPMM4		0x0C
#define EPMM5		0x0D
#define EPMM6		0x0E
#define EPMM7		0x0F
#define EPMCSL		0x10
#define EPMCSH		0x11
#define EPMOL		0x14
#define EPMOH		0x15
#define ERXFCON		0x18
#define EPKTCNT		0x19

//Bank 2 registers -----
#define MACON1		0x00
#define MACON3		0x02
#define MACON4		0x03
#define MABBIPG		0x04
#define MAIPGL		0x06
#define MAIPGH		0x07
#define MACLCON1	0x08
#define MACLCON2	0x09
#define MAMXFLL		0x0A
#define MAMXFLH		0x0B
#define MICMD		0x12
#define MIREGADR	0x14
#define MIWRL		0x16
#define MIWRH		0x17
#define MIRDL		0x18
#define MIRDH		0x19

//Bank 3 registers -----
#define MAADR5		0x00
#define MAADR6		0x01
#define MAADR3		0x02
#define MAADR4		0x03
#define MAADR1		0x04
#define MAADR2		0x05
#define EBSTSD		0x06
#define EBSTCON		0x307
#define EBSTCSL		0x08
#define EBSTCSH		0x09
#define MISTAT		0x0A
#define EREVID		0x12
#define ECOCON		0x15
#define EFLOCON		0x17
#define EPAUSL		0x18
#define EPAUSH		0x19

// EIE bits -----------------
#define	EIE_INTIE		(1<<7)
#define	EIE_PKTIE		(1<<6)
#define	EIE_DMAIE		(1<<5)
#define	EIE_LINKIE		(1<<4)
#define	EIE_TXIE		(1<<3)
#define	EIE_TXERIE		(1<<1)
#define	EIE_RXERIE		(1)

// EIR bits -----------------
#define	EIR_PKTIF		(1<<6)
#define	EIR_DMAIF		(1<<5)
#define	EIR_LINKIF		(1<<4)
#define	EIR_TXIF		(1<<3)
#define	EIR_TXERIF		(1<<1)
#define	EIR_RXERIF		(1)
	
// ESTAT bits ---------------
#define	ESTAT_INT		(1<<7)
#define ESTAT_BUFER		(1<<6)
#define	ESTAT_LATECOL	(1<<4)
#define	ESTAT_RXBUSY	(1<<2)
#define	ESTAT_TXABRT	(1<<1)
#define	ESTAT_CLKRDY	(1)
	
// ECON2 bits ---------------
#define	ECON2_AUTOINC	(1<<7)
#define	ECON2_PKTDEC	(1<<6)
#define	ECON2_PWRSV		(1<<5)
#define	ECON2_VRPS		(1<<3)

// ECON1 bits ---------------
#define	ECON1_TXRST		(1<<7)
#define	ECON1_RXRST		(1<<6)
#define	ECON1_DMAST		(1<<5)
#define	ECON1_CSUMEN	(1<<4)
#define	ECON1_TXRTS		(1<<3)
#define	ECON1_RXEN		(1<<2)
#define	ECON1_BSEL1		(1<<1)
#define	ECON1_BSEL0		(1)
#define ECON1_BSEL      (3)

// PHY Registers ---------------
#define PHCON1	0x00
#define PHSTAT1	0x01
#define PHID1	0x02
#define PHID2	0x03
#define PHCON2	0x10
#define PHSTAT2	0x11
#define PHIE	0x12
#define PHIR	0x13
#define PHLCON	0x14

// PHCON1 bits -----------------
#define	PHCON1_PRST		(1ul<<15)
#define	PHCON1_PLOOPBK	(1ul<<14)
#define	PHCON1_PPWRSV	(1ul<<11)
#define	PHCON1_PDPXMD	(1ul<<8)


// PHSTAT1 bits ----------------
#define	PHSTAT1_PFDPX	(1ul<<12)
#define	PHSTAT1_PHDPX	(1ul<<11)
#define	PHSTAT1_LLSTAT	(1ul<<2)
#define	PHSTAT1_JBSTAT	(1ul<<1)

// PHID2 bits ------------------
#define	PHID2_PID24		(1ul<<15)
#define	PHID2_PID23		(1ul<<14)
#define	PHID2_PID22		(1ul<<13)
#define	PHID2_PID21		(1ul<<12)
#define	PHID2_PID20		(1ul<<11)
#define	PHID2_PID19		(1ul<<10)
#define	PHID2_PPN5		(1ul<<9)
#define	PHID2_PPN4		(1ul<<8)
#define	PHID2_PPN3		(1ul<<7)
#define	PHID2_PPN2		(1ul<<6)
#define	PHID2_PPN1		(1ul<<5)
#define	PHID2_PPN0		(1ul<<4)
#define	PHID2_PREV3		(1ul<<3)
#define	PHID2_PREV2		(1ul<<2)
#define	PHID2_PREV1		(1ul<<1)
#define	PHID2_PREV0		(1ul)

// PHCON2 bits -----------------
#define	PHCON2_FRCLNK	(1ul<<14)
#define	PHCON2_TXDIS	(1ul<<13)
#define	PHCON2_JABBER	(1ul<<10)
#define	PHCON2_HDLDIS	(1ul<<8)

// PHSTAT2 bits ----------------
#define	PHSTAT2_TXSTAT	(1ul<<13)
#define	PHSTAT2_RXSTAT	(1ul<<12)
#define	PHSTAT2_COLSTAT	(1ul<<11)
#define	PHSTAT2_LSTAT	(1ul<<10)
#define	PHSTAT2_DPXSTAT	(1ul<<9)
#define	PHSTAT2_PLRITY	(1ul<<5)

// PHIE bits ------------------
#define	PHIE_PLNKIE		(1ul<<4)
#define	PHIE_PGEIE		(1ul<<1)

// PHIR bits -------------------
#define	PHIR_PLNKIF		(1ul<<4)
#define	PHIR_PGIF		(1ul<<2)

// PHLCON bits -----------------
#define	PHLCON_LACFG3	(1ul<<11)
#define	PHLCON_LACFG2	(1ul<<10)
#define	PHLCON_LACFG1	(1ul<<9)
#define	PHLCON_LACFG0	(1ul<<8)
#define	PHLCON_LBCFG3	(1ul<<7)
#define	PHLCON_LBCFG2	(1ul<<6)
#define	PHLCON_LBCFG1	(1ul<<5)
#define	PHLCON_LBCFG0	(1ul<<4)
#define	PHLCON_LFRQ1	(1ul<<3)
#define	PHLCON_LFRQ0	(1ul<<2)
#define	PHLCON_STRCH	(1ul<<1)

// ERXFCON bits ------------------
#define	ERXFCON_UCEN	(1<<7)
#define	ERXFCON_ANDOR	(1<<6)
#define	ERXFCON_CRCEN	(1<<5)
#define	ERXFCON_PMEN	(1<<4)
#define	ERXFCON_MPEN	(1<<3)
#define	ERXFCON_HTEN	(1<<2)
#define	ERXFCON_MCEN	(1<<1)
#define	ERXFCON_BCEN	(1)

// MACON1 bits ---------------
#define	MACON1_TXPAUS	(1<<3)
#define	MACON1_RXPAUS	(1<<2)
#define	MACON1_PASSALL	(1<<1)
#define	MACON1_MARXEN	(1)
	
// MACON3 bits ---------------
#define	MACON3_PADCFG2	(1<<7)
#define	MACON3_PADCFG1	(1<<6)
#define	MACON3_PADCFG0	(1<<5)
#define	MACON3_TXCRCEN	(1<<4)
#define	MACON3_PHDREN	(1<<3)
#define	MACON3_HFRMEN	(1<<2)
#define	MACON3_FRMLNEN	(1<<1)
#define	MACON3_FULDPX	(1)
	
// MACON4 bits ---------------
#define	MACON4_DEFER	(1<<6)
#define	MACON4_BPEN		(1<<5)
#define	MACON4_NOBKOFF	(1<<4)

// MICMD bits ----------------
#define	MICMD_MIISCAN	(1<<1)
#define	MICMD_MIIRD		(1)

// MISTAT bits ---------------
#define	MISTAT_NVALID	(1<<2)
#define	MISTAT_SCAN		(1<<1)
#define	MISTAT_BUSY		(1)

// TXStatus structure --------
typedef union {
	unsigned char v[7];
	struct {
		unsigned int	ByteCount;
		unsigned char	CollisionCount:4;
		unsigned char	CRCError:1;
		unsigned char	LengthCheckError:1;
		unsigned char	LengthOutOfRange:1;
		unsigned char	Done:1;
		unsigned char	Multicast:1;
		unsigned char	Broadcast:1;
		unsigned char	PacketDefer:1;
		unsigned char	ExcessiveDefer:1;
		unsigned char	MaximumCollisions:1;
		unsigned char	LateCollision:1;
		unsigned char	Giant:1;
		unsigned char	Underrun:1;
		unsigned char	BytesTransmittedOnWire;
		unsigned char	ControlFrame:1;
		unsigned char	PAUSEControlFrame:1;
		unsigned char	BackpressureApplied:1;
		unsigned char	VLANTaggedFrame:1;
		unsigned char	Zeros:4;
	} bits;
} TXSTATUS;
TXSTATUS xdata TxStatus;

// RXStatus Structure -------
typedef union {
	unsigned char v[6];
	struct {
		unsigned int    NextPacket;
        unsigned int	ByteCount;
		unsigned char	LongEvent:1;
		unsigned char	Reserved:1;
		unsigned char	CarrierEvent:1;
		unsigned char	Reserved2:1;
		unsigned char	CRCError:1;
		unsigned char	LenChkError:1;
		unsigned char	LenOutofRange:1;
		unsigned char	RxOk:1;
		unsigned char	RxMultiCast:1;
		unsigned char	RxBroadCast:1;
		unsigned char	DribbleNibble:1;
		unsigned char	RxCntrlFrame:1;
		unsigned char	RxPauseFrame:1;
		unsigned char	RxUkwnOpcode:1;
        unsigned char   RxVlan:1;
		unsigned char	Zeros:1;
	} bits;
} RXSTATUS;
RXSTATUS xdata RxStatus;

// Static Definitions --------------------
#define RXSTART			0x0000//0x0000
#define RXEND			0x1EFD//0x1EFF
#define TXSTART        	0x1F00//0x1F00
#define TXEND          	0x1FFF//0x1FFF
#define RXMAXBUFLEN    	RXEND - RXSTART
#define TXMAXBUFLEN    	TXEND - TXSTART	
#define MAXFRAMELEN     80	  

unsigned char xdata MAC_NextPtrL = ((unsigned char) RXSTART & 0x00FF);
unsigned char xdata MAC_NextPtrH = (unsigned char) ((RXSTART & 0xFF00) >> 8);	

unsigned int xdata MAC_PHYReadBuffer = 0;
unsigned int xdata MAC_PHYWriteBuffer = 0;

unsigned char xdata MAC_ENC_Error = 0;

struct stproperty{
		unsigned char 	ModuleMACAddr[6];
		unsigned char 	ServerMACAddr[6];
		unsigned char 	ModuleIPAddr[4];
		unsigned char 	ServerIPAddr[4];
		unsigned int	ModulePortNo;	
		unsigned int	ServerPortNo;

};
struct stproperty xdata property;
Mikro Dünyası.Haydarpaşa Teknik ve Endüstri Meslek Lisesi Mikrodenetleyici Atl. Şefi

Burak B

"... a healthy dose of paranoia leads to better systems." Jack Ganssle

OG

@haydarpasamikro
Hocam kodları code tag içine aldım, smiley ler çıkmasın diye.
FORUMU İLGİLENDİREN KONULARA ÖM İLE CEVAP VERİLMEZ.

haydarpasamikro

Tşk. Umarım benzer projelerle uğraşanlara fikir verir.
Mikro Dünyası.Haydarpaşa Teknik ve Endüstri Meslek Lisesi Mikrodenetleyici Atl. Şefi

magnetron

haydarpaşa hocam sizin verdiğiniz örneği

STM32F4 'e uyarlamaya çalışıyorum anca vakit bulabildim

bu ARP ICMP ve UDP send receive fonksiyonlarını çağıran ana döngüyü
acaba konuya ekleyebilir misiniz ?

yani ne zaman ICMP , ARP Send ne zaman Request yapıyor onu yöneten fonksiyon

teşekkür

haydarpasamikro

Öncelikle gelen paket olup olmadığını "EPKTCNT" registeri ile anlıyoruz. Buffer da okunmayı bekleyen gelen paketlerin sayısını burdan anlıyoruz.

void MAC_RxPacketPending(void) fonksiyonum ile bunu öğreniyorum. Kendimce özel bir bayrağı set edip ana programa bir sinyal göndermiş oluyorum.

void MAC_Interrupt_Process(void) fonks. ile bufferdaki ilk paketi alıp ne tip bir protokole ait paket olduğuna bakıyorum. Buradan gelen paket bir ICMP, ARP, UDP, TCP vs.. ayırt edebiliriz.

Benzer şekilde zincirleme etkili fonksiyonları çağırarak işlem yapıyorum.

ARP isteği karşıdan gelebilir. Bu durumda siz gelen isteğe yanıt verip MAC adresinizi gönderirsiniz. Siz belli bir IP ye sahip alıcının MAC adresini öğrenmek için ARP isteğinde bulunmanız gerekebilir.

ICMP ile karşı taraf size ping(request) atabilir. Siz karşıdaki bir IP ye ping atabilirsiniz. Her zaman karşı tarafa ping reply verilmelidir. Böylece karşı tarafın varlığı,vs.. öğrenilebilir.

Windows, ağdaki haberleştiği aygıtların MAC ve IP adreslerini bilmek için ARP ve ICMP gönderip cevap ister. Bu sebeple yazacağınız uygulama eğer pc haberleşmesi yapacaksa bu protokolleri gerçekleştirmek gerek.

Programınızda ana döngüde çok öncelikli olarak ENC28J60 ın buffer ında bekleyen paket olup olmadığını hemen kontrol edip bu fonksiyonları yürütmeniz gerek.

ARM LPC13434 için yeniden düzenleyip çalıştırdım. Onu ekleyeyim.
Mikro Dünyası.Haydarpaşa Teknik ve Endüstri Meslek Lisesi Mikrodenetleyici Atl. Şefi

Burak B

Birleştirme esnasında kodun bir kısmı kesilmiş sanırsam. Tekrar verebilir misiniz @haydarpasamikro hocam. Yada biryerlere upload edebilirseniz herkese faydası dokunur.

Teşekkürler.
"... a healthy dose of paranoia leads to better systems." Jack Ganssle

haydarpasamikro

Mikro Dünyası.Haydarpaşa Teknik ve Endüstri Meslek Lisesi Mikrodenetleyici Atl. Şefi

ground

⌒╮'╭⌒╮⌒╮.
╱◥██◣                  
| 田︱田田|              
╬╬╬╬╬╬╬╬╬

Picproje - Sözde değil özde paylaşım..

haydarpasamikro

#26
http://www.2shared.com/file/F676cjpq/ethProject.html

-ENC28J60 ve LPC1343 bağlantı şeması
-Ağ da ARP, ICMP, UDP haberleşme protokolleri vs..
Mikro Dünyası.Haydarpaşa Teknik ve Endüstri Meslek Lisesi Mikrodenetleyici Atl. Şefi

Burak B

"... a healthy dose of paranoia leads to better systems." Jack Ganssle

haydarpasamikro

Kodlarımda ve şemada ethernet ile ilgili olmayan yerleri kırptığım için azda olsa birşeyler kalmışsa önemli değil. Sizi ilgilendiren yerleri inceleyiniz.
Mikro Dünyası.Haydarpaşa Teknik ve Endüstri Meslek Lisesi Mikrodenetleyici Atl. Şefi

magnetron

haydarpaşa hocam merhaba ,

picprojeden size email attım ama ulaşmadı herhalde

bu konuyu açarken bahsettiğiniz veri toplama terminali hakkında
sizinle görüşmek istiyorum

tahmin ediyorum bunu textil sektörü için geliştirdiniz

bana da benzeri plastik sektörü için lazım

sıfırdan bir ürün geliştirmek yerine sizden terminali temin etmek bana zaman kazandırır
ben zamandan kazanırım siz de ürününüzü satmış olursunuz

acaba email adresinizi yazar mısınız ? sizinle görüşmek istiyorum