Ethernet.c
资源名称:str711USB.rar [点击查看]
上传用户:yyyd609
上传日期:2022-07-18
资源大小:183k
文件大小:2k
源码类别:
微处理器开发
开发平台:
C/C++
- #include "Ethernet.h"
- u16 local_eth_addr[3] = {0x4422, 0x8866, 0xCCAA};
- inline u16 NIC_InPort(u16 reg)
- {
- ETH_Addr_Port = reg;
- return ETH_Data_Port;
- }
- inline void NIC_OutPort(u16 reg, u16 x)
- {
- ETH_Addr_Port = reg;
- ETH_Data_Port = x;
- }
- inline void RequestSend(u16 FrameSize)
- {
- ETH_TX_CMD_Port = TX_START_ALL_BYTES;
- ETH_TX_LEN_Port = FrameSize;
- }
- void NIC_Init(void)
- {
- u16 t;
- GPIO_Config(GPIO2, 0x000F, GPIO_AF_PP);
- EMI_Config(2, EMI_ENABLE | EMI_WAITSTATE(15) | EMI_SIZE_16);
- NIC_OutPort(PP_SelfCTL, POWER_ON_RESET);
- t = ETH_Port(1);
- t = ETH_Port(0);
- t = ETH_Port(1);
- while(!(NIC_InPort(PP_SelfST) & INIT_DONE));
- NIC_OutPort(PP_IA + 0, local_eth_addr[0]);
- NIC_OutPort(PP_IA + 2, local_eth_addr[1]);
- NIC_OutPort(PP_IA + 4, local_eth_addr[2]);
- NIC_OutPort(PP_RxCTL, RX_OK_ACCEPT | RX_IA_ACCEPT | RX_BROADCAST_ACCEPT);
- NIC_OutPort(PP_LineCTL, SERIAL_RX_ON | SERIAL_TX_ON);
- }
- int NIC_RecvPack(u16 *buf)
- {
- int stat = NIC_InPort(PP_RxEvent);
- if((stat & RX_OK) && (stat & (RX_IA | RX_BROADCAST)))
- {
- int len, i;
- stat = ETH_RX_Frame_Port;
- len = ETH_RX_Frame_Port;
- if(len < 60 || len > 1514)
- return -1;
- for(i = 0; i < len; i += 2)
- *buf++ = ETH_RX_Frame_Port;
- return len;
- }
- return 0;
- }
- int NIC_SendPack(u16 *buf, int len)
- {
- if(!(NIC_InPort(PP_LineST) & LINK_OK))
- return -1;
- if(len < 60)
- len = 60;
- RequestSend(len);
- while(!(NIC_InPort(PP_BusST) & READY_FOR_TX_NOW))
- NIC_OutPort(PP_RxCFG, SKIP_1 | RX_OK_ENBL);
- if(NIC_InPort(PP_BusST) & READY_FOR_TX_NOW)
- {
- int i;
- for(i = 0; i < len; i += 2)
- ETH_TX_Frame_Port = *buf++;
- }
- return 0;
- }