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

📄 net_nic.c

📁 uC/OS-II下的LPC2378集成以太网控制器的驱动
💻 C
📖 第 1 页 / 共 5 页
字号:
#define  RX_DESC_STATUS_SYMERR                  0x01000000
#define  RX_DESC_STATUS_CRCERR                  0x00800000
#define  RX_DESC_STATUS_BCAST                   0x00400000
#define  RX_DESC_STATUS_MCAST                   0x00200000
#define  RX_DESC_STATUS_FAILFLT                 0x00100000
#define  RX_DESC_STATUS_VLAN                    0x00080000
#define  RX_DESC_STATUS_CTLFRAM	                0x00040000
#define  RX_DESC_STATUS_SIZE_MASK               0x000007FF
                                                                        /* Determine addresses of descriptor lists                  */
#define  EMAC_RX_DESC_BASE_ADDR                (EMAC_RAM_BASE_ADDR)
#define  EMAC_RX_STATUS_BASE_ADDR              (EMAC_RX_DESC_BASE_ADDR   + (EMAC_NUM_RX_DESC * sizeof(EMAC_DESCRIPTOR)))
#define  EMAC_TX_DESC_BASE_ADDR                (EMAC_RX_STATUS_BASE_ADDR + (EMAC_NUM_RX_DESC * sizeof(RX_STATUS)))
#define  EMAC_TX_STATUS_BASE_ADDR              (EMAC_TX_DESC_BASE_ADDR   + (EMAC_NUM_TX_DESC * sizeof(EMAC_DESCRIPTOR)))
#define  EMAC_RX_BUFF_BASE_ADDR                (EMAC_TX_STATUS_BASE_ADDR + (EMAC_NUM_TX_DESC * sizeof(TX_STATUS)))
#define  EMAC_TX_BUFF_BASE_ADDR                (EMAC_RX_BUFF_BASE_ADDR   + (EMAC_NUM_RX_DESC * EMAC_RX_BUF_SIZE))

/*
*********************************************************************************************************
*                                          DATA TYPES
*********************************************************************************************************
*/

typedef  struct  emac_descriptor {                                      /* EMAC Descriptor                                          */
    CPU_INT32U  PacketAddr;	                                            /* DMA Buffer Address                                       */
    CPU_INT32U  Control;                                                /* DMA Control bits                                         */
}   EMAC_DESCRIPTOR;

typedef  struct  rx_status {                                            /* Rx Status data type                                      */
    CPU_INT32U  StatusInfo;                                             /* Status information                                       */
    CPU_INT32U  StatusHashCRC;                                          /* Status Hash CRC                                          */
}   RX_STATUS;

typedef  struct  tx_status {                                            /* Rx Status data type                                      */
    CPU_INT32U  StatusInfo;                                             /* Status information                                       */
}   TX_STATUS;

/*
*********************************************************************************************************
*                                      GLOBAL VARIABLES
*********************************************************************************************************
*/

static  CPU_INT08U        MII_Dividers [7][2] =  {{4,  MCFG_CLKSEL_DIV4},
                                                  {6,  MCFG_CLKSEL_DIV6},
                                                  {8,  MCFG_CLKSEL_DIV8},
                                                  {10, MCFG_CLKSEL_DIV10},
                                                  {14, MCFG_CLKSEL_DIV14},
                                                  {20, MCFG_CLKSEL_DIV20},
                                                  {28, MCFG_CLKSEL_DIV28}};

static  EMAC_DESCRIPTOR  *RxDesc;
static  EMAC_DESCRIPTOR  *TxDesc;
static  RX_STATUS        *RxStatus;
static  TX_STATUS        *TxStatus;
static  CPU_INT08U       *RxBufBaseAddr;
static  CPU_INT08U       *TxBufBaseAddr;

static  CPU_INT32U        NIC_RxNRdyCtr;

/*$PAGE*/
/*
*********************************************************************************************************
*                                 LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/

static  void        NetNIC_TxPktDiscard      (NET_ERR      *perr);


                                                                        /* ----------- LPC23XX/LPC24XX EMAC FNCTS ------------- */

static  void        EMAC_Init                (NET_ERR      *perr);


                                                                        /* ---------- LPC23XX/LPC24XX EMAC RX FNCTS ----------- */

static  void        EMAC_RxEn                (void);
static  void        EMAC_RxDis               (void);


static  void        EMAC_RxIntEn             (void);

static  void        EMAC_RxPkt               (void         *ppkt,
                                              CPU_INT16U    size,
                                              NET_ERR      *perr);

static  void        EMAC_RxPktDiscard        (CPU_INT16U    size);

                                                                        /* ---------- LPC23XX/LPC24XX EMAC TX FNCTS ----------- */

static  void        EMAC_TxEn                (void);
static  void        EMAC_TxDis               (void);


static  void        EMAC_TxPkt               (void         *ppkt,
                                              CPU_INT16U    size,
                                              NET_ERR      *perr);

static  CPU_INT16U  NIC_RxGetNRdy            (void);

/*
*********************************************************************************************************
*********************************************************************************************************
*                                            GLOBAL FUNCTIONS
*********************************************************************************************************
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                            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 EMAC
*
*
* 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)     : (1) This function calls EMAC_Init() which initializes the LPC23XX/LPC24XX hardware.
*********************************************************************************************************
*/

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_OFF;
                                                                        /* ------------- 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 LPC2378 EMAC -------------------- */
    EMAC_Init(perr);
}

/*
*********************************************************************************************************
*                                            NetNIC_IntEn()
*
* Description : Enable NIC interrupts.
*
* Argument(s) : none.
*
* Return(s)   : none.
*
* Caller(s)   : Net_Init().
*********************************************************************************************************
*/

void  NetNIC_IntEn (NET_ERR  *perr)
{
    EMAC_RxIntEn();                                                     /* Enable Rx interrupts                                     */

    *perr = NET_NIC_ERR_NONE;
}

/*
*********************************************************************************************************
*                                        NetNIC_ConnStatusGet()
*
* Description : Get NIC's network connection status.
*
*               (1) Obtaining the NIC's network connection status is encapsulated in this function for the
*                   possibility that obtaining a NIC's connection status requires a non-trivial procedure.
*
*
* Argument(s) : none.
*
* Return(s)   : NIC network connection status :
*
*                   DEF_OFF     Network connection DOWN.
*                   DEF_ON      Network connection UP.
*
* Caller(s)   : NetIF_Pkt_Tx().
*********************************************************************************************************
*/

CPU_BOOLEAN  NetNIC_ConnStatusGet (void)
{
    return (NetNIC_ConnStatus);
}

/*
*********************************************************************************************************
*                                        EMAC Link Settings Update
*
* Description : This function is called by NetNIC_Init and the PHY ISR in order to update the
*             : speed and duplex settings for the EMAC.
*
* Arguments   : link_speed      Indicates link speed.  This can be one of
*                                   NET_PHY_SPD_0
*                                   NET_PHY_SPD_10
*                                   NET_PHY_SPD_100
*                                   NET_PHY_SPD_1000
*
*               link_duplex     Indicates link duplex.  This can be one of
*                                   NET_PHY_DUPLEX_UNKNOWN
*                                   NET_PHY_DUPLEX_HALF
*                                   NET_PHY_DUPLEX_FULL
*
* Return(s)   : none.
*********************************************************************************************************
*/

void  NetNIC_LinkChange (CPU_INT32U link_speed, CPU_INT32U link_duplex)
{
    switch (link_speed) {
        case NET_PHY_SPD_0:                                             /* Assume 10Mbps operation until linked                     */
        case NET_PHY_SPD_10:
             SUPP      &=  ~SUPP_SPEED;                                 /* Configure the RMII logic (if used) for 10MBps operation  */
             break;

        case NET_PHY_SPD_100:
             SUPP      |=   SUPP_SPEED;                                 /* Configure the RMII logic (if uses) for 100MBps operation */
             break;
    }

    switch (link_duplex) {
        case NET_PHY_DUPLEX_UNKNOWN:                                    /* Assume half duplex until link duplex is known            */
        case NET_PHY_DUPLEX_HALF:

⌨️ 快捷键说明

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