⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ax88796.c

📁 avr cpu 库源代码 对avr单片机编程很有帮助
💻 C
📖 第 1 页 / 共 2 页
字号:
/*! \file ax88796.c \brief ASIX AX88796 Ethernet Interface Driver. */
//*****************************************************************************
//
// File Name	: 'ax88796.c'
// Title		: ASIX AX88796 Ethernet Interface Driver
// Author		: Pascal Stang
// Created		: 10/22/2002
// Revised		: 8/21/2005
// Version		: 0.1
// Target MCU	: Atmel AVR series
// Editor Tabs	: 4
//
// Description	: This driver provides initialization and transmit/receive
//		functions for the ASIX AX88796 10/100Mb Ethernet Controller and PHY.
//
// Based in part on code by Louis Beaudoin (www.embedded-creations.com).
// Thanks to Adam Dunkels and Louis Beaudoin for providing the initial
// structure in which to write this driver.
//
//*****************************************************************************

#include "global.h"
#include "timer.h"
#include "rprintf.h"

#include "ax88796.h"

// include configuration
#include "ax88796conf.h"

// pointers to locations in the ax88796 receive buffer
static unsigned char NextPage;				// page pointer to next Rx packet
static unsigned int CurrentRetreiveAddress;	// DMA address for read Rx packet location


void nicInit(void)
{
	ax88796Init();
}

void nicSend(unsigned int len, unsigned char* packet)
{
	ax88796BeginPacketSend(len);
	ax88796SendPacketData(packet, len);
	ax88796EndPacketSend();
}

unsigned int nicPoll(unsigned int maxlen, unsigned char* packet)
{
	unsigned int packetLength;
	
	packetLength = ax88796BeginPacketRetreive();

	// if there's no packet or an error - exit without ending the operation
	if( !packetLength )
		return 0;

	// drop anything too big for the buffer
	if( packetLength > maxlen )
	{
		ax88796EndPacketRetreive();
		return 0;
	}
	
	// copy the packet data into the uIP packet buffer
	ax88796RetreivePacketData( packet, packetLength );
	ax88796EndPacketRetreive();
		
	return packetLength;
}

void nicGetMacAddress(u08* macaddr)
{
	u08 tempCR;
	// switch register pages
	tempCR = ax88796Read(CR);
	ax88796Write(CR,tempCR|PS0);
	// read MAC address registers
	*macaddr++ = ax88796Read(PAR0);
	*macaddr++ = ax88796Read(PAR1);
	*macaddr++ = ax88796Read(PAR2);
	*macaddr++ = ax88796Read(PAR3);
	*macaddr++ = ax88796Read(PAR4);
	*macaddr++ = ax88796Read(PAR5);
	// switch register pages back
	ax88796Write(CR,tempCR);
}

void nicSetMacAddress(u08* macaddr)
{
	u08 tempCR;
	// switch register pages
	tempCR = ax88796Read(CR);
	ax88796Write(CR,tempCR|PS0);
	// write MAC address registers
	ax88796Write(PAR0, *macaddr++);
	ax88796Write(PAR1, *macaddr++);
	ax88796Write(PAR2, *macaddr++);
	ax88796Write(PAR3, *macaddr++);
	ax88796Write(PAR4, *macaddr++);
	ax88796Write(PAR5, *macaddr++);
	// switch register pages back
	ax88796Write(CR,tempCR);
}

void nicRegDump(void)
{
	ax88796RegDump();
}


void ax88796SetupPorts(void)
{
#if NIC_CONNECTION == MEMORY_MAPPED
	// enable external SRAM interface - no wait states
	sbi(MCUCR, SRE);
//	sbi(MCUCR, SRW10);
//	sbi(XMCRA, SRW00);
//	sbi(XMCRA, SRW01);
//	sbi(XMCRA, SRW11);
#else
	// set address port to output
	AX88796_ADDRESS_DDR = AX88796_ADDRESS_MASK;
    
	// set data port to input with pull-ups
	AX88796_DATA_DDR = 0x00;
	AX88796_DATA_PORT = 0xFF;

	// initialize the control port read and write pins to de-asserted
	sbi( AX88796_CONTROL_PORT, AX88796_CONTROL_READPIN );
	sbi( AX88796_CONTROL_PORT, AX88796_CONTROL_WRITEPIN );
	// set the read and write pins to output
	sbi( AX88796_CONTROL_DDR, AX88796_CONTROL_READPIN );
	sbi( AX88796_CONTROL_DDR, AX88796_CONTROL_WRITEPIN );
#endif
	// set reset pin to output
	sbi( AX88796_RESET_DDR, AX88796_RESET_PIN );
}


#if NIC_CONNECTION == MEMORY_MAPPED
inline void ax88796Write(u08 address, u08 data)
{
	*(volatile u08*)(AX88796_MEMORY_MAPPED_OFFSET + address) = data;
}
#else
void ax88796Write(u08 address, u08 data)
{
	// assert the address
    AX88796_ADDRESS_PORT = address | (AX88796_ADDRESS_PORT&~AX88796_ADDRESS_MASK);

	// set data bus as output and place data on bus
	AX88796_DATA_DDR = 0xFF;
	AX88796_DATA_PORT = data;
    
	// clock write pin
	cbi(AX88796_CONTROL_PORT, AX88796_CONTROL_WRITEPIN);
	nop();
	nop();
	sbi(AX88796_CONTROL_PORT, AX88796_CONTROL_WRITEPIN);
    
	// set data bus back to input with pullups enabled
	AX88796_DATA_DDR = 0x00;
	AX88796_DATA_PORT = 0xFF;
}
#endif


#if NIC_CONNECTION == MEMORY_MAPPED
inline u08 ax88796Read(u08 address)
{
	return *(volatile u08*)(AX88796_MEMORY_MAPPED_OFFSET + address);
}
#else
u08 ax88796Read(u08 address)
{
	u08 data;
   
	// assert the address
    AX88796_ADDRESS_PORT = address | (AX88796_ADDRESS_PORT&~AX88796_ADDRESS_MASK);

	// assert read
	cbi(AX88796_CONTROL_PORT, AX88796_CONTROL_READPIN);
	nop();
	nop();
	// read in the data
	data = AX88796_DATA_PIN;

	// negate read
	sbi(AX88796_CONTROL_PORT, AX88796_CONTROL_READPIN);

	return data;
}
#endif                       


void ax88796Init(void)
{
	unsigned char tcrFduFlag;
	
	// initialize I/O ports
	ax88796SetupPorts();

	// do a hard reset
	sbi(AX88796_RESET_PORT, AX88796_RESET_PIN);
	delay_ms(100);
	cbi(AX88796_RESET_PORT, AX88796_RESET_PIN);

	// do soft reset
	ax88796Write(ISR, ax88796Read(ISR));
	delay_ms(50);

	// wait for PHY to come out of reset
	ax88796Read(RSTPORT);
	while(ax88796Read(TR) & RST_B);
  
	ax88796WriteMii(0x10,0x00,0x0800);
	delay_ms(255);
	ax88796WriteMii(0x10,0x00,0x1200);

	ax88796Write(CR,(RD2|STOP));		// stop the NIC, abort DMA, page 0
	delay_ms(5);						// make sure nothing is coming in or going out
	ax88796Write(DCR,DCR_INIT);    
	ax88796Write(RBCR0,0x00);
	ax88796Write(RBCR1,0x00);
	ax88796Write(IMR,0x00);
	ax88796Write(ISR,0xFF);
	ax88796Write(RCR,0x20);
	ax88796Write(BNRY,RXSTART_INIT);
	ax88796Write(PSTART,RXSTART_INIT);
	ax88796Write(PSTOP,RXSTOP_INIT);
	
	// switch to page 1
	ax88796Write(CR,(PS0|RD2|STOP));
	// write mac address
	ax88796Write(PAR0+0, AX88796_MAC0);
	ax88796Write(PAR0+1, AX88796_MAC1);
	ax88796Write(PAR0+2, AX88796_MAC2);
	ax88796Write(PAR0+3, AX88796_MAC3);
	ax88796Write(PAR0+4, AX88796_MAC4);
	ax88796Write(PAR0+5, AX88796_MAC5);
	// set start point
	ax88796Write(CURR,RXSTART_INIT+1);

	ax88796Write(CR,(RD2|START));
	ax88796Write(RCR,RCR_INIT);

	if(ax88796Read(GPI) & I_SPD)		// check PHY speed setting
		tcrFduFlag = FDU;				// if 100base, do full duplex
	else
		tcrFduFlag = 0;					// if 10base, do half duplex
		
	ax88796Write(TCR,(tcrFduFlag|TCR_INIT));

	ax88796Write(GPOC,MPSEL);			// select media interface
  
	ax88796Write(TPSR,TXSTART_INIT);

	ax88796Write(CR,(RD2|STOP));
	ax88796Write(DCR,DCR_INIT);
	ax88796Write(CR,(RD2|START));
	ax88796Write(ISR,0xFF);
	ax88796Write(IMR,IMR_INIT);
	ax88796Write(TCR,(tcrFduFlag|TCR_INIT));

	//test
/*
	while(1)
	{
		vt100SetCursorPos(18,0);
		ax88796RegDump();
	}
*/
}


void ax88796BeginPacketSend(unsigned int packetLength)
{
	unsigned int sendPacketLength;
	sendPacketLength = (packetLength>=ETHERNET_MIN_PACKET_LENGTH)?
						(packetLength):(ETHERNET_MIN_PACKET_LENGTH);
	
	//start the NIC
	ax88796Write(CR,(RD2|START));
	
	// still transmitting a packet - wait for it to finish
	while( ax88796Read(CR) & TXP );

	//load beginning page for transmit buffer
	ax88796Write(TPSR,TXSTART_INIT);
	
	//set start address for remote DMA operation
	ax88796Write(RSAR0,0x00);
	ax88796Write(RSAR1,0x40);
	
	//clear the packet stored interrupt
	ax88796Write(ISR, PTX);

	//load data byte count for remote DMA
	ax88796Write(RBCR0, (unsigned char)(packetLength));
	ax88796Write(RBCR1, (unsigned char)(packetLength>>8));

	ax88796Write(TBCR0, (unsigned char)(sendPacketLength));
	ax88796Write(TBCR1, (unsigned char)((sendPacketLength)>>8));
	
	//do remote write operation
	ax88796Write(CR,0x12);
}


void ax88796SendPacketData(unsigned char * localBuffer, unsigned int length)
{
	unsigned int i;
	
	for(i=0;i<length;i++)
		ax88796Write(RDMAPORT, localBuffer[i]);
}


void ax88796EndPacketSend(void)
{
	//send the contents of the transmit buffer onto the network
	ax88796Write(CR,(RD2|TXP));
	
	// clear the remote DMA interrupt
	ax88796Write(ISR, RDC);
}


unsigned int ax88796BeginPacketRetreive(void)
{
	unsigned char writePagePtr;
	unsigned char readPagePtr;
	unsigned char bnryPagePtr;
	unsigned char i;
	
	unsigned char pageheader[4];
	unsigned int rxlen;
	
	// check for and handle an overflow
	ax88796ProcessInterrupt();
	
	// read CURR from page 1
	ax88796Write(CR,(PS0|RD2|START));
	writePagePtr = ax88796Read(CURR);
	// read the boundary register from page 0
	ax88796Write(CR,(RD2|START));
	bnryPagePtr = ax88796Read(BNRY);

	// first packet is at page bnryPtr+1
	readPagePtr = bnryPagePtr+1;
	if(readPagePtr >= RXSTOP_INIT) readPagePtr = RXSTART_INIT;
	
	// return if there is no packet in the buffer
	if( readPagePtr == writePagePtr )
	{
		return 0;
	}
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -