📄 mac_main.c
字号:
//*----------------------------------------------------------------------------
//* ATMEL Microcontroller Software Support - ROUSSET -
//*----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*----------------------------------------------------------------------------
//* File Name : main.c
//* Object : main application written in C
//* Creation : Hi 11/18/2002
//* Modification : JGe 07/28/2005
//*----------------------------------------------------------------------------
#include "board.h"
#include "Emac.h"
#include <stdio.h>
#include <string.h>
#include "mii.h"
// function in appli.c
extern void Init_LED_FOR_APPLI (void);
extern void APPLI(void);
extern void LED_Toggle(unsigned int led);
extern void LED_TurnOn(unsigned int led);
extern void LED_TurnOff(unsigned int led);
// function in dbgu.c
extern void AT91F_DBGU_Printk(char *buffer);
extern void Init_DBGU(void);
// functions in Emac.c
extern void AT91F_EMAC_HANDLER (void);
extern int AT91F_EmacEntry(void);
extern int AT91F_ProcessEmacPacket(AT91PS_IPheader pHeader);
extern void write_phy(AT91PS_EMAC pEmac, unsigned char phy_addr, unsigned char address, unsigned int value);
extern void read_phy(AT91PS_EMAC pEmac, unsigned char phy_addr, unsigned char address, unsigned int *value);
extern void AT91F_Enable_Mdi(AT91PS_EMAC pEmac);
extern void AT91F_Disable_Mdi(AT91PS_EMAC pEmac);
// Message buffer
char MsgBuffer[256];
// system timer counter
extern AT91PS_RxTdDescriptor RxtdList;
extern AT91PS_TxTdDescriptor TxtdList;
#define AT91C_ID_EMAC_B 16
//#define RMII
//#define EMAC_IRQ
#define DISPLAY_IP_PACKET
//*----------------------------------------------------------------------------
//* \fn AT91F_DisplayIpPacket
//* \brief This function is invoked by main
//* This function is for demonstration purpose only
//*----------------------------------------------------------------------------
void AT91F_DisplayIpPacket(AT91PS_IPheader pIpHeader)
{
char address[6];
#ifdef DISPLAY_IP_PACKET
AT91F_DBGU_Printk("\n\n\r-I- ================ IP HEADER ======================\n\r");
sprintf(MsgBuffer, "\n\r IP Version = v.%d",(pIpHeader->ip_hl_v & 0xF0) >> 4);
AT91F_DBGU_Printk(MsgBuffer);
sprintf(MsgBuffer, "\n\r Header Length = %d",pIpHeader->ip_hl_v & 0x0F);
AT91F_DBGU_Printk(MsgBuffer);
sprintf(MsgBuffer, "\n\r Type of service = 0x%x",pIpHeader->ip_tos);
AT91F_DBGU_Printk(MsgBuffer);
sprintf(MsgBuffer, "\n\r Total IP Length = 0x%X", (((pIpHeader->ip_len)>>8)&0xff) + (((pIpHeader->ip_len)<<8)&0xff00) );
AT91F_DBGU_Printk(MsgBuffer);
sprintf(MsgBuffer, "\n\r ID = 0x%X", (((pIpHeader->ip_id)>>8)&0xff) + (((pIpHeader->ip_id)<<8)&0xff00) );
AT91F_DBGU_Printk(MsgBuffer);
sprintf(MsgBuffer, "\n\r Header Checksum = 0x%X", (((pIpHeader->ip_sum)>>8)&0xff) + (((pIpHeader->ip_sum)<<8)&0xff00) );
AT91F_DBGU_Printk(MsgBuffer);
switch(pIpHeader->ip_p) {
case PROT_IP:
sprintf(MsgBuffer, "\n\r Protocol = %s","IP");
break;
case PROT_ICMP:
sprintf(MsgBuffer, "\n\r Protocol = %s","ICMP");
break;
default:
sprintf(MsgBuffer, "\n\r Protocol = 0x%X",pIpHeader->ip_p);
break;
}
AT91F_DBGU_Printk(MsgBuffer);
strncpy((char *)address, (const char *)(pIpHeader->ip_src), 4);
address[4] = 0;
sprintf(MsgBuffer, "\n\r IP Src Address = %d:%d:%d:%d",
pIpHeader->ip_src[0],
pIpHeader->ip_src[1],
pIpHeader->ip_src[2],
pIpHeader->ip_src[3]
);
AT91F_DBGU_Printk(MsgBuffer);
strncpy((char *)address, (const char *)pIpHeader->ip_dst, 4);
address[4] = 0;
sprintf(MsgBuffer, "\n\r IP Dest Address = %d:%d:%d:%d",
pIpHeader->ip_dst[0],
pIpHeader->ip_dst[1],
pIpHeader->ip_dst[2],
pIpHeader->ip_dst[3]
);
AT91F_DBGU_Printk(MsgBuffer);
#endif
}
//*----------------------------------------------------------------------------
//* \fn main
//* \brief
//*
//*----------------------------------------------------------------------------
int main()
{
int status=0,i;
#ifndef RMII
int control=0;
#endif
AT91S_IPheader IpHeader;
AT91PS_PIO pPIOB = AT91C_BASE_PIOB;
AT91PS_EMAC pEMAC = AT91C_BASE_EMAC;
AT91PS_RSTC pRSTC = AT91C_BASE_RSTC;
AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1 << AT91C_ID_PIOA ) ;
AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1 << AT91C_ID_PIOB ) ;
AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1 << AT91C_ID_EMAC ) ;
Init_LED_FOR_APPLI ();
APPLI();
//DBGU init for ethernet spy
Init_DBGU();
//disable pull up on RXDV => PHY normal mode (not in test mode),
//PHY has internal pull down
pPIOB->PIO_PPUDR=1<<15;
#ifndef RMII
//PHY has internal pull down : set MII mode
pPIOB->PIO_PPUDR=1<<16;
#endif
//clear PB18 <=> PHY powerdown
AT91F_PIO_CfgOutput( AT91C_BASE_PIOB, 1<<18 ) ;
AT91F_PIO_ClearOutput( AT91C_BASE_PIOB, 1<<18) ;
//After PHY power up, hardware reset
pRSTC->RSTC_RMR = 0xA5000000 | AT91C_RSTC_ERSTL&(0x01<<8);
pRSTC->RSTC_RCR = 0xA5000000 | AT91C_RSTC_EXTRST;
//Display general purpose infos
AT91F_DBGU_Printk("\n\n\r-I- ======================================\n\r");
AT91F_DBGU_Printk("-I- AT91SAM7X256 EMAC Test\n\r");
AT91F_DBGU_Printk("-I- --------------------------------------\n\r");
//Wait for hardware reset end
while( !(pRSTC->RSTC_RSR & AT91C_RSTC_NRSTL) ) {;}
//EMAC IO init for EMAC-PHY com
// Remove EF100 config
AT91F_PIO_CfgPeriph(
AT91C_BASE_PIOB, // PIO controller base address
((unsigned int) AT91C_PB2_ETX0 ) |
((unsigned int) AT91C_PB12_ETXER ) |
((unsigned int) AT91C_PB16_ECOL ) |
((unsigned int) AT91C_PB11_ETX3 ) |
((unsigned int) AT91C_PB6_ERX1 ) |
((unsigned int) AT91C_PB15_ERXDV_ECRSDV ) |
((unsigned int) AT91C_PB13_ERX2 ) |
((unsigned int) AT91C_PB3_ETX1 ) |
((unsigned int) AT91C_PB8_EMDC ) |
((unsigned int) AT91C_PB5_ERX0 ) |
((unsigned int) AT91C_PB14_ERX3 ) |
((unsigned int) AT91C_PB4_ECRS) |
((unsigned int) AT91C_PB1_ETXEN ) |
((unsigned int) AT91C_PB10_ETX2 ) |
((unsigned int) AT91C_PB0_ETXCK_EREFCK) |
((unsigned int) AT91C_PB9_EMDIO ) |
((unsigned int) AT91C_PB7_ERXER ) |
((unsigned int) AT91C_PB17_ERXCK ),
0);
//Enable com between EMAC PHY
AT91F_Enable_Mdi(pEMAC);
//PHY configuration
#ifndef RMII
//PHY has internal pull down : disable MII isolate
read_phy(pEMAC, AT91C_PHY_ADDR, MII_BMCR, (unsigned int *)&control);
read_phy(pEMAC, AT91C_PHY_ADDR, MII_BMCR, (unsigned int *)&control);
control&=~BMCR_ISOLATE;
write_phy(pEMAC, AT91C_PHY_ADDR, MII_BMCR, control);
#endif
// Wait for PHY auto negotiation completed
do {
read_phy(pEMAC, AT91C_PHY_ADDR, MII_BMSR, (unsigned int *)&status);
read_phy(pEMAC, AT91C_PHY_ADDR, MII_BMSR, (unsigned int *)&status);
}
while (!(status & BMSR_ANEGCOMPLETE));
AT91F_Disable_Mdi(pEMAC);
#ifdef RMII
//Enable EMAC in RMII mode, enable RMII clock (50MHz from oscillator on ERFCK)
pEMAC->EMAC_USRIO= AT91C_EMAC_RMII | AT91C_EMAC_CLKEN ;
#else
//Enable EMAC in MII mode, enable clock ERXCK and ETXCK
pEMAC->EMAC_USRIO= AT91C_EMAC_CLKEN ;
#endif
//EMAC configuration
if ( AT91F_EmacEntry())
{
sprintf(MsgBuffer, "Error EMAC init: 0x%x", status);
AT91F_DBGU_Printk(MsgBuffer);
while(1);
}
#ifdef EMAC_IRQ
AT91F_AIC_ConfigureIt (AT91C_BASE_AIC, AT91C_ID_EMAC_B, AT91C_AIC_PRIOR_HIGHEST, AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, AT91F_EMAC_HANDLER);
pEMAC->EMAC_IDR = 0x3fff;
AT91F_AIC_EnableIt(AT91C_BASE_AIC, AT91C_ID_EMAC_B); //AT91C_ID_EMACB
pEMAC->EMAC_IER = AT91C_EMAC_RCOMP | AT91C_EMAC_TCOMP | AT91C_EMAC_TXUBR;
#endif
//Packet processing : dipslay on DBGU, ping answer
while(1)
{
#ifndef EMAC_IRQ
if (AT91F_ProcessEmacPacket(&IpHeader) == AT91C_IPPACKET)
{
LED_TurnOn(LED1);
AT91F_DisplayIpPacket(&IpHeader);
LED_TurnOff(LED1);
}
if( (pEMAC->EMAC_TSR & AT91C_EMAC_COMP) == AT91C_EMAC_COMP)
{
for (i = 0; i <NB_TX_BUFFERS; i++)
{
pEMAC->EMAC_TSR |= AT91C_EMAC_COMP;
if(TxtdList[i].U_Status.S_Status.BuffUsed == 1)
{
TxtdList[i].U_Status.S_Status.Length = 0;
TxtdList[i].U_Status.S_Status.BuffUsed = 0;
}
}
}
if( (pEMAC->EMAC_TSR & AT91C_EMAC_UBR) == AT91C_EMAC_UBR )
pEMAC->EMAC_TSR |= AT91C_EMAC_UBR;
#endif
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -