📄 net_nic.c
字号:
/*
*********************************************************************************************************
* uC/TCP-IP
* The Embedded TCP/IP Suite
*
* (c) Copyright 2003-2006; Micrium, Inc.; Weston, FL
*
* All rights reserved. Protected by international copyright laws.
*
* uC/TCP-IP is provided in source form for FREE evaluation, for educational
* use or peaceful research. If you plan on using uC/TCP-IP in a commercial
* product you need to contact Micrium to properly license its use in your
* product. We provide ALL the source code for your convenience and to help
* you experience uC/TCP-IP. The fact that the source code is provided does
* NOT mean that you can use it without paying a licensing fee.
*
* Network Interface Card (NIC) port files provided, as is, for FREE and do
* NOT require any additional licensing or licensing fee.
*
* Knowledge of the source code may NOT be used to develop a similar product.
*
* Please help us continue to provide the Embedded community with the finest
* software available. Your honesty is greatly appreciated.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*
* NETWORK INTERFACE CARD
*
* CS8900A
*
* Filename : net_nic.c
* Version : V1.87
* Programmer(s) : ES
*********************************************************************************************************
* Note(s) : (1) Supports the Cirrus CS8900A 10 MBPS Ethernet Controller (MAC + PHY)
*
* Cirrus Corporation (Cirrus; http://www.cirrus.com)
*
* (2) REQUIREs Ethernet Network Interface Layer located in the following network directory :
*
* \<Network Protocol Suite>\IF\Ether\
*
* where
* <Network Protocol Suite> directory path for network protocol suite
*********************************************************************************************************
*/
#define NET_NIC_MODULE
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#include <net.h>
/*
*********************************************************************************************************
* LOCAL DEFINES
*********************************************************************************************************
*/
#define CS8900_DBG_PRINT_EN DEF_DISABLED
/* ---------------- CS8900 BUFFERS ------------- */
/* Buffer size MUST be divisible by 16 and MUST */
#define CS8900_RX_BUF_SIZE 1536 /* ... be greater than 1518 or 1522 (VLAN) bytes */
#define CS8900_TX_BUF_SIZE 1536
#define CS8900_NUM_ETH_FRAME_BUF 5
#define CS8900_Phy_Reg_Link_Mask (1 << 7) /* LinkOK Bit in CS8900A LineStatus Register */
/*
*********************************************************************************************************
* MACROS
*********************************************************************************************************
*/
#if (CPU_CFG_ENDIAN_TYPE == CPU_ENDIAN_TYPE_BIG)
#define CS8900_NIC_TO_HOST_16(val) ((((CPU_INT16U)(val) & 0xFF00) >> 8) | \
(((CPU_INT16U)(val) & 0x00FF) << 8))
#else
#define CS8900_NIC_TO_HOST_16(val) (val)
#endif
#define CS8900_HOST_TO_NIC_16(val) CS8900_NIC_TO_HOST_16(val)
#if (CS8900_DBG_PRINT_EN == DEF_ENABLED)
extern void Uart_Printf(char *ptr);
#define CS8900_DBG_PRINT(a) Uart_Printf(a)
#else
#define CS8900_DBG_PRINT(a)
#endif
/*
*********************************************************************************************************
* DATA TYPES
*********************************************************************************************************
*/
typedef struct {
CPU_INT16U size;
CPU_INT08U Frame[CS8900_RX_BUF_SIZE];
} CS8900_ETH_FRAME;
/*
*********************************************************************************************************
* CONSTANTS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/
NET_LOCAL CS8900_ETH_FRAME frame_bff[CS8900_NUM_ETH_FRAME_BUF];
NET_LOCAL CPU_INT08U frame_bff_head;
NET_LOCAL CPU_INT08U frame_bff_tail;
NET_LOCAL CPU_INT08U frame_bff_num;
/*
*********************************************************************************************************
* EXTERNAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
NET_EXT void NET_BSP_CS8900A_Reset_Delay(void);
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
NET_LOCAL void NetNIC_RxISR_Handler (void); /* ISR for RX interrupts. */
NET_LOCAL void NetNIC_TxISR_Handler (void); /* ISR for TX interrupts. */
NET_LOCAL void NetNIC_TxPktDiscard (NET_ERR *perr);
/* ---------- CS8900 REG FNCTS ----------------- */
NET_LOCAL CPU_INT16U CS8900_RegRd (CPU_INT16U reg_offset);
NET_LOCAL void CS8900_RegWr (CPU_INT16U reg_offset,
CPU_INT16U val);
NET_LOCAL CPU_INT16U CS8900_PPRegRd (CPU_INT16U regOffset);
NET_LOCAL void CS8900_PPRegWr (CPU_INT16U regOffset,
CPU_INT16U val);
NET_LOCAL CPU_INT32S CS8900_Reset (void);
/* ---------- CS8900 FNCTS --------------------- */
NET_LOCAL void CS8900_Init (void);
/* ---------- CS8900 RX FNCTS ------------------ */
NET_LOCAL void CS8900_RxPkt (void *ppkt,
CPU_INT16U size,
NET_ERR *perr);
NET_LOCAL void CS8900_RxPktDiscard (CPU_INT16U size);
/* ---------- CS8900 TX FNCTS ------------------ */
NET_LOCAL void CS8900_TxPkt (void *ppkt,
CPU_INT16U size,
NET_ERR *perr);
NET_LOCAL CPU_INT32S CS8900_Int_Receive (CS8900_ETH_FRAME *pframe,
CPU_INT16U RxEventStatus);
/*$PAGE*/
/*
*********************************************************************************************************
* CS8900_RegRd()
*
* Description : Read 16 bit data from addressable CS8900A registers
*********************************************************************************************************
*/
NET_LOCAL CPU_INT16U CS8900_RegRd (CPU_INT16U reg_offset)
{
CPU_INT16U reg_val;
reg_val = NetNIC_Rd16(reg_offset);
reg_val = CS8900_NIC_TO_HOST_16(reg_val);
return (reg_val);
}
/*
*********************************************************************************************************
* CS8900_RegWr()
*
* Description : Write 16 bit data to addressable CS8900A registers
*********************************************************************************************************
*/
NET_LOCAL void CS8900_RegWr (CPU_INT16U reg_offset, CPU_INT16U value)
{
value = CS8900_HOST_TO_NIC_16(value);
NetNIC_Wr16(reg_offset, value);
}
/*
*********************************************************************************************************
* CS8900_PPRegRd()
*
* Description : Read 16 bit data from indirectly accessible CS8900A registers though the packet page port
*********************************************************************************************************
*/
NET_LOCAL CPU_INT16U CS8900_PPRegRd (CPU_INT16U regOffset)
{
CPU_INT16U data;
CS8900_RegWr(CS8900_PPTR_OFFSET, regOffset);
data = CS8900_RegRd(CS8900_PDATA_OFFSET);
return (data);
}
/*
*********************************************************************************************************
* CS8900_PPRegWr()
*
* Description : Write 16 bit data to indirectly accessible CS8900A registers though the packet page port
*********************************************************************************************************
*/
NET_LOCAL void CS8900_PPRegWr (CPU_INT16U regOffset, CPU_INT16U val)
{
CS8900_RegWr(CS8900_PPTR_OFFSET, regOffset);
CS8900_RegWr(CS8900_PDATA_OFFSET, val);
}
/*$PAGE*/
/*
*********************************************************************************************************
* NetNIC_Init()
*
* Description : (1) Initialize Network Interface Card :
*
* (a) Perform NIC Layer OS initialization
* (b) Initialize NIC status
* (c) Initialize NIC statistics & error counters
* (d) Initialize CS8900
*
*
* Argument(s) : perr Pointer to variable that will hold the return error code from this function :
*
* NET_NIC_ERR_NONE Network interface card successfully initialized.
*
* -------- RETURNED BY NetOS_NIC_Init() : --------
* NET_OS_ERR_INIT_NIC_TX_RDY NIC transmit ready signal NOT successfully
* initialized.
* NET_OS_ERR_INIT_NIC_TX_RDY_NAME NIC transmit ready name NOT successfully
* configured.
* Return(s) : none.
*
* Caller(s) : Net_Init().
*
* Note(s) : none.
*********************************************************************************************************
*/
void NetNIC_Init (NET_ERR *perr)
{
/* --------------- PERFORM NIC/OS INIT ---------------- */
NetOS_NIC_Init(perr); /* Create NIC objs. */
if (*perr != NET_OS_ERR_NONE) {
return;
}
/* ----------------- INIT NIC STATUS ------------------ */
NetNIC_ConnStatus = DEF_ON;
/* ------------- INIT NIC STAT & ERR CTRS ------------- */
#if (NET_CTR_CFG_STAT_EN == DEF_ENABLED)
NetNIC_StatRxPktCtr = 0;
NetNIC_StatTxPktCtr = 0;
#endif
#if (NET_CTR_CFG_ERR_EN == DEF_ENABLED)
NetNIC_ErrRxPktDiscardedCtr = 0;
NetNIC_ErrTxPktDiscardedCtr = 0;
#endif
/* ----------------- INIT CS8900 ------------------- */
CS8900_Init();
*perr = NET_NIC_ERR_NONE;
}
/*
*********************************************************************************************************
* NetNIC_IntEn()
*
* Description : Enable NIC interrupts.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -