Mouse code for the MacroRat
mbed-dev/targets/TARGET_NXP/TARGET_LPC23XX/ethernet_api.c@18:6a4db94011d3, 2017-05-14 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sun May 14 23:18:57 2017 +0000
- Revision:
- 18:6a4db94011d3
Publishing again
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /* mbed Microcontroller Library |
sahilmgandhi | 18:6a4db94011d3 | 2 | * Copyright (c) 2006-2013 ARM Limited |
sahilmgandhi | 18:6a4db94011d3 | 3 | * |
sahilmgandhi | 18:6a4db94011d3 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sahilmgandhi | 18:6a4db94011d3 | 5 | * you may not use this file except in compliance with the License. |
sahilmgandhi | 18:6a4db94011d3 | 6 | * You may obtain a copy of the License at |
sahilmgandhi | 18:6a4db94011d3 | 7 | * |
sahilmgandhi | 18:6a4db94011d3 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sahilmgandhi | 18:6a4db94011d3 | 9 | * |
sahilmgandhi | 18:6a4db94011d3 | 10 | * Unless required by applicable law or agreed to in writing, software |
sahilmgandhi | 18:6a4db94011d3 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sahilmgandhi | 18:6a4db94011d3 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sahilmgandhi | 18:6a4db94011d3 | 13 | * See the License for the specific language governing permissions and |
sahilmgandhi | 18:6a4db94011d3 | 14 | * limitations under the License. |
sahilmgandhi | 18:6a4db94011d3 | 15 | */ |
sahilmgandhi | 18:6a4db94011d3 | 16 | #include <string.h> |
sahilmgandhi | 18:6a4db94011d3 | 17 | |
sahilmgandhi | 18:6a4db94011d3 | 18 | #include "ethernet_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 19 | #include "cmsis.h" |
sahilmgandhi | 18:6a4db94011d3 | 20 | #include "mbed_interface.h" |
sahilmgandhi | 18:6a4db94011d3 | 21 | #include "mbed_toolchain.h" |
sahilmgandhi | 18:6a4db94011d3 | 22 | #include "mbed_error.h" |
sahilmgandhi | 18:6a4db94011d3 | 23 | |
sahilmgandhi | 18:6a4db94011d3 | 24 | #define NEW_LOGIC 0 |
sahilmgandhi | 18:6a4db94011d3 | 25 | #define NEW_ETH_BUFFER 0 |
sahilmgandhi | 18:6a4db94011d3 | 26 | |
sahilmgandhi | 18:6a4db94011d3 | 27 | #if NEW_ETH_BUFFER |
sahilmgandhi | 18:6a4db94011d3 | 28 | |
sahilmgandhi | 18:6a4db94011d3 | 29 | #define NUM_RX_FRAG 4 // Number of Rx Fragments (== packets) |
sahilmgandhi | 18:6a4db94011d3 | 30 | #define NUM_TX_FRAG 3 // Number of Tx Fragments (== packets) |
sahilmgandhi | 18:6a4db94011d3 | 31 | |
sahilmgandhi | 18:6a4db94011d3 | 32 | #define ETH_MAX_FLEN 1536 // Maximum Ethernet Frame Size |
sahilmgandhi | 18:6a4db94011d3 | 33 | #define ETH_FRAG_SIZE ETH_MAX_FLEN // Packet Fragment size (same as packet length) |
sahilmgandhi | 18:6a4db94011d3 | 34 | |
sahilmgandhi | 18:6a4db94011d3 | 35 | #else |
sahilmgandhi | 18:6a4db94011d3 | 36 | |
sahilmgandhi | 18:6a4db94011d3 | 37 | // Memfree calculation: |
sahilmgandhi | 18:6a4db94011d3 | 38 | // (16 * 1024) - ((2 * 4 * NUM_RX) + (2 * 4 * NUM_RX) + (0x300 * NUM_RX) + |
sahilmgandhi | 18:6a4db94011d3 | 39 | // (2 * 4 * NUM_TX) + (1 * 4 * NUM_TX) + (0x300 * NUM_TX)) = 8556 |
sahilmgandhi | 18:6a4db94011d3 | 40 | /* EMAC Memory Buffer configuration for 16K Ethernet RAM. */ |
sahilmgandhi | 18:6a4db94011d3 | 41 | #define NUM_RX_FRAG 4 /* Num.of RX Fragments 4*1536= 6.0kB */ |
sahilmgandhi | 18:6a4db94011d3 | 42 | #define NUM_TX_FRAG 3 /* Num.of TX Fragments 3*1536= 4.6kB */ |
sahilmgandhi | 18:6a4db94011d3 | 43 | //#define ETH_FRAG_SIZE 1536 /* Packet Fragment size 1536 Bytes */ |
sahilmgandhi | 18:6a4db94011d3 | 44 | |
sahilmgandhi | 18:6a4db94011d3 | 45 | //#define ETH_MAX_FLEN 1536 /* Max. Ethernet Frame Size */ |
sahilmgandhi | 18:6a4db94011d3 | 46 | #define ETH_FRAG_SIZE 0x300 /* Packet Fragment size 1536/2 Bytes */ |
sahilmgandhi | 18:6a4db94011d3 | 47 | #define ETH_MAX_FLEN 0x300 /* Max. Ethernet Frame Size */ |
sahilmgandhi | 18:6a4db94011d3 | 48 | |
sahilmgandhi | 18:6a4db94011d3 | 49 | const int ethernet_MTU_SIZE = 0x300; |
sahilmgandhi | 18:6a4db94011d3 | 50 | |
sahilmgandhi | 18:6a4db94011d3 | 51 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 52 | |
sahilmgandhi | 18:6a4db94011d3 | 53 | #define ETHERNET_ADDR_SIZE 6 |
sahilmgandhi | 18:6a4db94011d3 | 54 | |
sahilmgandhi | 18:6a4db94011d3 | 55 | PACKED struct RX_DESC_TypeDef { /* RX Descriptor struct */ |
sahilmgandhi | 18:6a4db94011d3 | 56 | unsigned int Packet; |
sahilmgandhi | 18:6a4db94011d3 | 57 | unsigned int Ctrl; |
sahilmgandhi | 18:6a4db94011d3 | 58 | }; |
sahilmgandhi | 18:6a4db94011d3 | 59 | typedef struct RX_DESC_TypeDef RX_DESC_TypeDef; |
sahilmgandhi | 18:6a4db94011d3 | 60 | |
sahilmgandhi | 18:6a4db94011d3 | 61 | PACKED struct RX_STAT_TypeDef { /* RX Status struct */ |
sahilmgandhi | 18:6a4db94011d3 | 62 | unsigned int Info; |
sahilmgandhi | 18:6a4db94011d3 | 63 | unsigned int HashCRC; |
sahilmgandhi | 18:6a4db94011d3 | 64 | }; |
sahilmgandhi | 18:6a4db94011d3 | 65 | typedef struct RX_STAT_TypeDef RX_STAT_TypeDef; |
sahilmgandhi | 18:6a4db94011d3 | 66 | |
sahilmgandhi | 18:6a4db94011d3 | 67 | PACKED struct TX_DESC_TypeDef { /* TX Descriptor struct */ |
sahilmgandhi | 18:6a4db94011d3 | 68 | unsigned int Packet; |
sahilmgandhi | 18:6a4db94011d3 | 69 | unsigned int Ctrl; |
sahilmgandhi | 18:6a4db94011d3 | 70 | }; |
sahilmgandhi | 18:6a4db94011d3 | 71 | typedef struct TX_DESC_TypeDef TX_DESC_TypeDef; |
sahilmgandhi | 18:6a4db94011d3 | 72 | |
sahilmgandhi | 18:6a4db94011d3 | 73 | PACKED struct TX_STAT_TypeDef { /* TX Status struct */ |
sahilmgandhi | 18:6a4db94011d3 | 74 | unsigned int Info; |
sahilmgandhi | 18:6a4db94011d3 | 75 | }; |
sahilmgandhi | 18:6a4db94011d3 | 76 | typedef struct TX_STAT_TypeDef TX_STAT_TypeDef; |
sahilmgandhi | 18:6a4db94011d3 | 77 | |
sahilmgandhi | 18:6a4db94011d3 | 78 | /* MAC Configuration Register 1 */ |
sahilmgandhi | 18:6a4db94011d3 | 79 | #define MAC1_REC_EN 0x00000001 /* Receive Enable */ |
sahilmgandhi | 18:6a4db94011d3 | 80 | #define MAC1_PASS_ALL 0x00000002 /* Pass All Receive Frames */ |
sahilmgandhi | 18:6a4db94011d3 | 81 | #define MAC1_RX_FLOWC 0x00000004 /* RX Flow Control */ |
sahilmgandhi | 18:6a4db94011d3 | 82 | #define MAC1_TX_FLOWC 0x00000008 /* TX Flow Control */ |
sahilmgandhi | 18:6a4db94011d3 | 83 | #define MAC1_LOOPB 0x00000010 /* Loop Back Mode */ |
sahilmgandhi | 18:6a4db94011d3 | 84 | #define MAC1_RES_TX 0x00000100 /* Reset TX Logic */ |
sahilmgandhi | 18:6a4db94011d3 | 85 | #define MAC1_RES_MCS_TX 0x00000200 /* Reset MAC TX Control Sublayer */ |
sahilmgandhi | 18:6a4db94011d3 | 86 | #define MAC1_RES_RX 0x00000400 /* Reset RX Logic */ |
sahilmgandhi | 18:6a4db94011d3 | 87 | #define MAC1_RES_MCS_RX 0x00000800 /* Reset MAC RX Control Sublayer */ |
sahilmgandhi | 18:6a4db94011d3 | 88 | #define MAC1_SIM_RES 0x00004000 /* Simulation Reset */ |
sahilmgandhi | 18:6a4db94011d3 | 89 | #define MAC1_SOFT_RES 0x00008000 /* Soft Reset MAC */ |
sahilmgandhi | 18:6a4db94011d3 | 90 | |
sahilmgandhi | 18:6a4db94011d3 | 91 | /* MAC Configuration Register 2 */ |
sahilmgandhi | 18:6a4db94011d3 | 92 | #define MAC2_FULL_DUP 0x00000001 /* Full Duplex Mode */ |
sahilmgandhi | 18:6a4db94011d3 | 93 | #define MAC2_FRM_LEN_CHK 0x00000002 /* Frame Length Checking */ |
sahilmgandhi | 18:6a4db94011d3 | 94 | #define MAC2_HUGE_FRM_EN 0x00000004 /* Huge Frame Enable */ |
sahilmgandhi | 18:6a4db94011d3 | 95 | #define MAC2_DLY_CRC 0x00000008 /* Delayed CRC Mode */ |
sahilmgandhi | 18:6a4db94011d3 | 96 | #define MAC2_CRC_EN 0x00000010 /* Append CRC to every Frame */ |
sahilmgandhi | 18:6a4db94011d3 | 97 | #define MAC2_PAD_EN 0x00000020 /* Pad all Short Frames */ |
sahilmgandhi | 18:6a4db94011d3 | 98 | #define MAC2_VLAN_PAD_EN 0x00000040 /* VLAN Pad Enable */ |
sahilmgandhi | 18:6a4db94011d3 | 99 | #define MAC2_ADET_PAD_EN 0x00000080 /* Auto Detect Pad Enable */ |
sahilmgandhi | 18:6a4db94011d3 | 100 | #define MAC2_PPREAM_ENF 0x00000100 /* Pure Preamble Enforcement */ |
sahilmgandhi | 18:6a4db94011d3 | 101 | #define MAC2_LPREAM_ENF 0x00000200 /* Long Preamble Enforcement */ |
sahilmgandhi | 18:6a4db94011d3 | 102 | #define MAC2_NO_BACKOFF 0x00001000 /* No Backoff Algorithm */ |
sahilmgandhi | 18:6a4db94011d3 | 103 | #define MAC2_BACK_PRESSURE 0x00002000 /* Backoff Presurre / No Backoff */ |
sahilmgandhi | 18:6a4db94011d3 | 104 | #define MAC2_EXCESS_DEF 0x00004000 /* Excess Defer */ |
sahilmgandhi | 18:6a4db94011d3 | 105 | |
sahilmgandhi | 18:6a4db94011d3 | 106 | /* Back-to-Back Inter-Packet-Gap Register */ |
sahilmgandhi | 18:6a4db94011d3 | 107 | #define IPGT_FULL_DUP 0x00000015 /* Recommended value for Full Duplex */ |
sahilmgandhi | 18:6a4db94011d3 | 108 | #define IPGT_HALF_DUP 0x00000012 /* Recommended value for Half Duplex */ |
sahilmgandhi | 18:6a4db94011d3 | 109 | |
sahilmgandhi | 18:6a4db94011d3 | 110 | /* Non Back-to-Back Inter-Packet-Gap Register */ |
sahilmgandhi | 18:6a4db94011d3 | 111 | #define IPGR_DEF 0x00000012 /* Recommended value */ |
sahilmgandhi | 18:6a4db94011d3 | 112 | |
sahilmgandhi | 18:6a4db94011d3 | 113 | /* Collision Window/Retry Register */ |
sahilmgandhi | 18:6a4db94011d3 | 114 | #define CLRT_DEF 0x0000370F /* Default value */ |
sahilmgandhi | 18:6a4db94011d3 | 115 | |
sahilmgandhi | 18:6a4db94011d3 | 116 | /* PHY Support Register */ |
sahilmgandhi | 18:6a4db94011d3 | 117 | #define SUPP_SPEED 0x00000100 /* Reduced MII Logic Current Speed */ |
sahilmgandhi | 18:6a4db94011d3 | 118 | //#define SUPP_RES_RMII 0x00000800 /* Reset Reduced MII Logic */ |
sahilmgandhi | 18:6a4db94011d3 | 119 | #define SUPP_RES_RMII 0x00000000 /* Reset Reduced MII Logic */ |
sahilmgandhi | 18:6a4db94011d3 | 120 | |
sahilmgandhi | 18:6a4db94011d3 | 121 | /* Test Register */ |
sahilmgandhi | 18:6a4db94011d3 | 122 | #define TEST_SHCUT_PQUANTA 0x00000001 /* Shortcut Pause Quanta */ |
sahilmgandhi | 18:6a4db94011d3 | 123 | #define TEST_TST_PAUSE 0x00000002 /* Test Pause */ |
sahilmgandhi | 18:6a4db94011d3 | 124 | #define TEST_TST_BACKP 0x00000004 /* Test Back Pressure */ |
sahilmgandhi | 18:6a4db94011d3 | 125 | |
sahilmgandhi | 18:6a4db94011d3 | 126 | /* MII Management Configuration Register */ |
sahilmgandhi | 18:6a4db94011d3 | 127 | #define MCFG_SCAN_INC 0x00000001 /* Scan Increment PHY Address */ |
sahilmgandhi | 18:6a4db94011d3 | 128 | #define MCFG_SUPP_PREAM 0x00000002 /* Suppress Preamble */ |
sahilmgandhi | 18:6a4db94011d3 | 129 | #define MCFG_CLK_SEL 0x0000003C /* Clock Select Mask */ |
sahilmgandhi | 18:6a4db94011d3 | 130 | #define MCFG_RES_MII 0x00008000 /* Reset MII Management Hardware */ |
sahilmgandhi | 18:6a4db94011d3 | 131 | |
sahilmgandhi | 18:6a4db94011d3 | 132 | /* MII Management Command Register */ |
sahilmgandhi | 18:6a4db94011d3 | 133 | #define MCMD_READ 0x00000001 /* MII Read */ |
sahilmgandhi | 18:6a4db94011d3 | 134 | #define MCMD_SCAN 0x00000002 /* MII Scan continuously */ |
sahilmgandhi | 18:6a4db94011d3 | 135 | |
sahilmgandhi | 18:6a4db94011d3 | 136 | #define MII_WR_TOUT 0x00050000 /* MII Write timeout count */ |
sahilmgandhi | 18:6a4db94011d3 | 137 | #define MII_RD_TOUT 0x00050000 /* MII Read timeout count */ |
sahilmgandhi | 18:6a4db94011d3 | 138 | |
sahilmgandhi | 18:6a4db94011d3 | 139 | /* MII Management Address Register */ |
sahilmgandhi | 18:6a4db94011d3 | 140 | #define MADR_REG_ADR 0x0000001F /* MII Register Address Mask */ |
sahilmgandhi | 18:6a4db94011d3 | 141 | #define MADR_PHY_ADR 0x00001F00 /* PHY Address Mask */ |
sahilmgandhi | 18:6a4db94011d3 | 142 | |
sahilmgandhi | 18:6a4db94011d3 | 143 | /* MII Management Indicators Register */ |
sahilmgandhi | 18:6a4db94011d3 | 144 | #define MIND_BUSY 0x00000001 /* MII is Busy */ |
sahilmgandhi | 18:6a4db94011d3 | 145 | #define MIND_SCAN 0x00000002 /* MII Scanning in Progress */ |
sahilmgandhi | 18:6a4db94011d3 | 146 | #define MIND_NOT_VAL 0x00000004 /* MII Read Data not valid */ |
sahilmgandhi | 18:6a4db94011d3 | 147 | #define MIND_MII_LINK_FAIL 0x00000008 /* MII Link Failed */ |
sahilmgandhi | 18:6a4db94011d3 | 148 | |
sahilmgandhi | 18:6a4db94011d3 | 149 | /* Command Register */ |
sahilmgandhi | 18:6a4db94011d3 | 150 | #define CR_RX_EN 0x00000001 /* Enable Receive */ |
sahilmgandhi | 18:6a4db94011d3 | 151 | #define CR_TX_EN 0x00000002 /* Enable Transmit */ |
sahilmgandhi | 18:6a4db94011d3 | 152 | #define CR_REG_RES 0x00000008 /* Reset Host Registers */ |
sahilmgandhi | 18:6a4db94011d3 | 153 | #define CR_TX_RES 0x00000010 /* Reset Transmit Datapath */ |
sahilmgandhi | 18:6a4db94011d3 | 154 | #define CR_RX_RES 0x00000020 /* Reset Receive Datapath */ |
sahilmgandhi | 18:6a4db94011d3 | 155 | #define CR_PASS_RUNT_FRM 0x00000040 /* Pass Runt Frames */ |
sahilmgandhi | 18:6a4db94011d3 | 156 | #define CR_PASS_RX_FILT 0x00000080 /* Pass RX Filter */ |
sahilmgandhi | 18:6a4db94011d3 | 157 | #define CR_TX_FLOW_CTRL 0x00000100 /* TX Flow Control */ |
sahilmgandhi | 18:6a4db94011d3 | 158 | #define CR_RMII 0x00000200 /* Reduced MII Interface */ |
sahilmgandhi | 18:6a4db94011d3 | 159 | #define CR_FULL_DUP 0x00000400 /* Full Duplex */ |
sahilmgandhi | 18:6a4db94011d3 | 160 | |
sahilmgandhi | 18:6a4db94011d3 | 161 | /* Status Register */ |
sahilmgandhi | 18:6a4db94011d3 | 162 | #define SR_RX_EN 0x00000001 /* Enable Receive */ |
sahilmgandhi | 18:6a4db94011d3 | 163 | #define SR_TX_EN 0x00000002 /* Enable Transmit */ |
sahilmgandhi | 18:6a4db94011d3 | 164 | |
sahilmgandhi | 18:6a4db94011d3 | 165 | /* Transmit Status Vector 0 Register */ |
sahilmgandhi | 18:6a4db94011d3 | 166 | #define TSV0_CRC_ERR 0x00000001 /* CRC error */ |
sahilmgandhi | 18:6a4db94011d3 | 167 | #define TSV0_LEN_CHKERR 0x00000002 /* Length Check Error */ |
sahilmgandhi | 18:6a4db94011d3 | 168 | #define TSV0_LEN_OUTRNG 0x00000004 /* Length Out of Range */ |
sahilmgandhi | 18:6a4db94011d3 | 169 | #define TSV0_DONE 0x00000008 /* Tramsmission Completed */ |
sahilmgandhi | 18:6a4db94011d3 | 170 | #define TSV0_MCAST 0x00000010 /* Multicast Destination */ |
sahilmgandhi | 18:6a4db94011d3 | 171 | #define TSV0_BCAST 0x00000020 /* Broadcast Destination */ |
sahilmgandhi | 18:6a4db94011d3 | 172 | #define TSV0_PKT_DEFER 0x00000040 /* Packet Deferred */ |
sahilmgandhi | 18:6a4db94011d3 | 173 | #define TSV0_EXC_DEFER 0x00000080 /* Excessive Packet Deferral */ |
sahilmgandhi | 18:6a4db94011d3 | 174 | #define TSV0_EXC_COLL 0x00000100 /* Excessive Collision */ |
sahilmgandhi | 18:6a4db94011d3 | 175 | #define TSV0_LATE_COLL 0x00000200 /* Late Collision Occured */ |
sahilmgandhi | 18:6a4db94011d3 | 176 | #define TSV0_GIANT 0x00000400 /* Giant Frame */ |
sahilmgandhi | 18:6a4db94011d3 | 177 | #define TSV0_UNDERRUN 0x00000800 /* Buffer Underrun */ |
sahilmgandhi | 18:6a4db94011d3 | 178 | #define TSV0_BYTES 0x0FFFF000 /* Total Bytes Transferred */ |
sahilmgandhi | 18:6a4db94011d3 | 179 | #define TSV0_CTRL_FRAME 0x10000000 /* Control Frame */ |
sahilmgandhi | 18:6a4db94011d3 | 180 | #define TSV0_PAUSE 0x20000000 /* Pause Frame */ |
sahilmgandhi | 18:6a4db94011d3 | 181 | #define TSV0_BACK_PRESS 0x40000000 /* Backpressure Method Applied */ |
sahilmgandhi | 18:6a4db94011d3 | 182 | #define TSV0_VLAN 0x80000000 /* VLAN Frame */ |
sahilmgandhi | 18:6a4db94011d3 | 183 | |
sahilmgandhi | 18:6a4db94011d3 | 184 | /* Transmit Status Vector 1 Register */ |
sahilmgandhi | 18:6a4db94011d3 | 185 | #define TSV1_BYTE_CNT 0x0000FFFF /* Transmit Byte Count */ |
sahilmgandhi | 18:6a4db94011d3 | 186 | #define TSV1_COLL_CNT 0x000F0000 /* Transmit Collision Count */ |
sahilmgandhi | 18:6a4db94011d3 | 187 | |
sahilmgandhi | 18:6a4db94011d3 | 188 | /* Receive Status Vector Register */ |
sahilmgandhi | 18:6a4db94011d3 | 189 | #define RSV_BYTE_CNT 0x0000FFFF /* Receive Byte Count */ |
sahilmgandhi | 18:6a4db94011d3 | 190 | #define RSV_PKT_IGNORED 0x00010000 /* Packet Previously Ignored */ |
sahilmgandhi | 18:6a4db94011d3 | 191 | #define RSV_RXDV_SEEN 0x00020000 /* RXDV Event Previously Seen */ |
sahilmgandhi | 18:6a4db94011d3 | 192 | #define RSV_CARR_SEEN 0x00040000 /* Carrier Event Previously Seen */ |
sahilmgandhi | 18:6a4db94011d3 | 193 | #define RSV_REC_CODEV 0x00080000 /* Receive Code Violation */ |
sahilmgandhi | 18:6a4db94011d3 | 194 | #define RSV_CRC_ERR 0x00100000 /* CRC Error */ |
sahilmgandhi | 18:6a4db94011d3 | 195 | #define RSV_LEN_CHKERR 0x00200000 /* Length Check Error */ |
sahilmgandhi | 18:6a4db94011d3 | 196 | #define RSV_LEN_OUTRNG 0x00400000 /* Length Out of Range */ |
sahilmgandhi | 18:6a4db94011d3 | 197 | #define RSV_REC_OK 0x00800000 /* Frame Received OK */ |
sahilmgandhi | 18:6a4db94011d3 | 198 | #define RSV_MCAST 0x01000000 /* Multicast Frame */ |
sahilmgandhi | 18:6a4db94011d3 | 199 | #define RSV_BCAST 0x02000000 /* Broadcast Frame */ |
sahilmgandhi | 18:6a4db94011d3 | 200 | #define RSV_DRIB_NIBB 0x04000000 /* Dribble Nibble */ |
sahilmgandhi | 18:6a4db94011d3 | 201 | #define RSV_CTRL_FRAME 0x08000000 /* Control Frame */ |
sahilmgandhi | 18:6a4db94011d3 | 202 | #define RSV_PAUSE 0x10000000 /* Pause Frame */ |
sahilmgandhi | 18:6a4db94011d3 | 203 | #define RSV_UNSUPP_OPC 0x20000000 /* Unsupported Opcode */ |
sahilmgandhi | 18:6a4db94011d3 | 204 | #define RSV_VLAN 0x40000000 /* VLAN Frame */ |
sahilmgandhi | 18:6a4db94011d3 | 205 | |
sahilmgandhi | 18:6a4db94011d3 | 206 | /* Flow Control Counter Register */ |
sahilmgandhi | 18:6a4db94011d3 | 207 | #define FCC_MIRR_CNT 0x0000FFFF /* Mirror Counter */ |
sahilmgandhi | 18:6a4db94011d3 | 208 | #define FCC_PAUSE_TIM 0xFFFF0000 /* Pause Timer */ |
sahilmgandhi | 18:6a4db94011d3 | 209 | |
sahilmgandhi | 18:6a4db94011d3 | 210 | /* Flow Control Status Register */ |
sahilmgandhi | 18:6a4db94011d3 | 211 | #define FCS_MIRR_CNT 0x0000FFFF /* Mirror Counter Current */ |
sahilmgandhi | 18:6a4db94011d3 | 212 | |
sahilmgandhi | 18:6a4db94011d3 | 213 | /* Receive Filter Control Register */ |
sahilmgandhi | 18:6a4db94011d3 | 214 | #define RFC_UCAST_EN 0x00000001 /* Accept Unicast Frames Enable */ |
sahilmgandhi | 18:6a4db94011d3 | 215 | #define RFC_BCAST_EN 0x00000002 /* Accept Broadcast Frames Enable */ |
sahilmgandhi | 18:6a4db94011d3 | 216 | #define RFC_MCAST_EN 0x00000004 /* Accept Multicast Frames Enable */ |
sahilmgandhi | 18:6a4db94011d3 | 217 | #define RFC_UCAST_HASH_EN 0x00000008 /* Accept Unicast Hash Filter Frames */ |
sahilmgandhi | 18:6a4db94011d3 | 218 | #define RFC_MCAST_HASH_EN 0x00000010 /* Accept Multicast Hash Filter Fram.*/ |
sahilmgandhi | 18:6a4db94011d3 | 219 | #define RFC_PERFECT_EN 0x00000020 /* Accept Perfect Match Enable */ |
sahilmgandhi | 18:6a4db94011d3 | 220 | #define RFC_MAGP_WOL_EN 0x00001000 /* Magic Packet Filter WoL Enable */ |
sahilmgandhi | 18:6a4db94011d3 | 221 | #define RFC_PFILT_WOL_EN 0x00002000 /* Perfect Filter WoL Enable */ |
sahilmgandhi | 18:6a4db94011d3 | 222 | |
sahilmgandhi | 18:6a4db94011d3 | 223 | /* Receive Filter WoL Status/Clear Registers */ |
sahilmgandhi | 18:6a4db94011d3 | 224 | #define WOL_UCAST 0x00000001 /* Unicast Frame caused WoL */ |
sahilmgandhi | 18:6a4db94011d3 | 225 | #define WOL_BCAST 0x00000002 /* Broadcast Frame caused WoL */ |
sahilmgandhi | 18:6a4db94011d3 | 226 | #define WOL_MCAST 0x00000004 /* Multicast Frame caused WoL */ |
sahilmgandhi | 18:6a4db94011d3 | 227 | #define WOL_UCAST_HASH 0x00000008 /* Unicast Hash Filter Frame WoL */ |
sahilmgandhi | 18:6a4db94011d3 | 228 | #define WOL_MCAST_HASH 0x00000010 /* Multicast Hash Filter Frame WoL */ |
sahilmgandhi | 18:6a4db94011d3 | 229 | #define WOL_PERFECT 0x00000020 /* Perfect Filter WoL */ |
sahilmgandhi | 18:6a4db94011d3 | 230 | #define WOL_RX_FILTER 0x00000080 /* RX Filter caused WoL */ |
sahilmgandhi | 18:6a4db94011d3 | 231 | #define WOL_MAG_PACKET 0x00000100 /* Magic Packet Filter caused WoL */ |
sahilmgandhi | 18:6a4db94011d3 | 232 | |
sahilmgandhi | 18:6a4db94011d3 | 233 | /* Interrupt Status/Enable/Clear/Set Registers */ |
sahilmgandhi | 18:6a4db94011d3 | 234 | #define INT_RX_OVERRUN 0x00000001 /* Overrun Error in RX Queue */ |
sahilmgandhi | 18:6a4db94011d3 | 235 | #define INT_RX_ERR 0x00000002 /* Receive Error */ |
sahilmgandhi | 18:6a4db94011d3 | 236 | #define INT_RX_FIN 0x00000004 /* RX Finished Process Descriptors */ |
sahilmgandhi | 18:6a4db94011d3 | 237 | #define INT_RX_DONE 0x00000008 /* Receive Done */ |
sahilmgandhi | 18:6a4db94011d3 | 238 | #define INT_TX_UNDERRUN 0x00000010 /* Transmit Underrun */ |
sahilmgandhi | 18:6a4db94011d3 | 239 | #define INT_TX_ERR 0x00000020 /* Transmit Error */ |
sahilmgandhi | 18:6a4db94011d3 | 240 | #define INT_TX_FIN 0x00000040 /* TX Finished Process Descriptors */ |
sahilmgandhi | 18:6a4db94011d3 | 241 | #define INT_TX_DONE 0x00000080 /* Transmit Done */ |
sahilmgandhi | 18:6a4db94011d3 | 242 | #define INT_SOFT_INT 0x00001000 /* Software Triggered Interrupt */ |
sahilmgandhi | 18:6a4db94011d3 | 243 | #define INT_WAKEUP 0x00002000 /* Wakeup Event Interrupt */ |
sahilmgandhi | 18:6a4db94011d3 | 244 | |
sahilmgandhi | 18:6a4db94011d3 | 245 | /* Power Down Register */ |
sahilmgandhi | 18:6a4db94011d3 | 246 | #define PD_POWER_DOWN 0x80000000 /* Power Down MAC */ |
sahilmgandhi | 18:6a4db94011d3 | 247 | |
sahilmgandhi | 18:6a4db94011d3 | 248 | /* RX Descriptor Control Word */ |
sahilmgandhi | 18:6a4db94011d3 | 249 | #define RCTRL_SIZE 0x000007FF /* Buffer size mask */ |
sahilmgandhi | 18:6a4db94011d3 | 250 | #define RCTRL_INT 0x80000000 /* Generate RxDone Interrupt */ |
sahilmgandhi | 18:6a4db94011d3 | 251 | |
sahilmgandhi | 18:6a4db94011d3 | 252 | /* RX Status Hash CRC Word */ |
sahilmgandhi | 18:6a4db94011d3 | 253 | #define RHASH_SA 0x000001FF /* Hash CRC for Source Address */ |
sahilmgandhi | 18:6a4db94011d3 | 254 | #define RHASH_DA 0x001FF000 /* Hash CRC for Destination Address */ |
sahilmgandhi | 18:6a4db94011d3 | 255 | |
sahilmgandhi | 18:6a4db94011d3 | 256 | /* RX Status Information Word */ |
sahilmgandhi | 18:6a4db94011d3 | 257 | #define RINFO_SIZE 0x000007FF /* Data size in bytes */ |
sahilmgandhi | 18:6a4db94011d3 | 258 | #define RINFO_CTRL_FRAME 0x00040000 /* Control Frame */ |
sahilmgandhi | 18:6a4db94011d3 | 259 | #define RINFO_VLAN 0x00080000 /* VLAN Frame */ |
sahilmgandhi | 18:6a4db94011d3 | 260 | #define RINFO_FAIL_FILT 0x00100000 /* RX Filter Failed */ |
sahilmgandhi | 18:6a4db94011d3 | 261 | #define RINFO_MCAST 0x00200000 /* Multicast Frame */ |
sahilmgandhi | 18:6a4db94011d3 | 262 | #define RINFO_BCAST 0x00400000 /* Broadcast Frame */ |
sahilmgandhi | 18:6a4db94011d3 | 263 | #define RINFO_CRC_ERR 0x00800000 /* CRC Error in Frame */ |
sahilmgandhi | 18:6a4db94011d3 | 264 | #define RINFO_SYM_ERR 0x01000000 /* Symbol Error from PHY */ |
sahilmgandhi | 18:6a4db94011d3 | 265 | #define RINFO_LEN_ERR 0x02000000 /* Length Error */ |
sahilmgandhi | 18:6a4db94011d3 | 266 | #define RINFO_RANGE_ERR 0x04000000 /* Range Error (exceeded max. size) */ |
sahilmgandhi | 18:6a4db94011d3 | 267 | #define RINFO_ALIGN_ERR 0x08000000 /* Alignment Error */ |
sahilmgandhi | 18:6a4db94011d3 | 268 | #define RINFO_OVERRUN 0x10000000 /* Receive overrun */ |
sahilmgandhi | 18:6a4db94011d3 | 269 | #define RINFO_NO_DESCR 0x20000000 /* No new Descriptor available */ |
sahilmgandhi | 18:6a4db94011d3 | 270 | #define RINFO_LAST_FLAG 0x40000000 /* Last Fragment in Frame */ |
sahilmgandhi | 18:6a4db94011d3 | 271 | #define RINFO_ERR 0x80000000 /* Error Occured (OR of all errors) */ |
sahilmgandhi | 18:6a4db94011d3 | 272 | |
sahilmgandhi | 18:6a4db94011d3 | 273 | //#define RINFO_ERR_MASK (RINFO_FAIL_FILT | RINFO_CRC_ERR | RINFO_SYM_ERR | RINFO_LEN_ERR | RINFO_ALIGN_ERR | RINFO_OVERRUN) |
sahilmgandhi | 18:6a4db94011d3 | 274 | #define RINFO_ERR_MASK (RINFO_FAIL_FILT | RINFO_SYM_ERR | \ |
sahilmgandhi | 18:6a4db94011d3 | 275 | RINFO_LEN_ERR | RINFO_ALIGN_ERR | RINFO_OVERRUN) |
sahilmgandhi | 18:6a4db94011d3 | 276 | |
sahilmgandhi | 18:6a4db94011d3 | 277 | |
sahilmgandhi | 18:6a4db94011d3 | 278 | /* TX Descriptor Control Word */ |
sahilmgandhi | 18:6a4db94011d3 | 279 | #define TCTRL_SIZE 0x000007FF /* Size of data buffer in bytes */ |
sahilmgandhi | 18:6a4db94011d3 | 280 | #define TCTRL_OVERRIDE 0x04000000 /* Override Default MAC Registers */ |
sahilmgandhi | 18:6a4db94011d3 | 281 | #define TCTRL_HUGE 0x08000000 /* Enable Huge Frame */ |
sahilmgandhi | 18:6a4db94011d3 | 282 | #define TCTRL_PAD 0x10000000 /* Pad short Frames to 64 bytes */ |
sahilmgandhi | 18:6a4db94011d3 | 283 | #define TCTRL_CRC 0x20000000 /* Append a hardware CRC to Frame */ |
sahilmgandhi | 18:6a4db94011d3 | 284 | #define TCTRL_LAST 0x40000000 /* Last Descriptor for TX Frame */ |
sahilmgandhi | 18:6a4db94011d3 | 285 | #define TCTRL_INT 0x80000000 /* Generate TxDone Interrupt */ |
sahilmgandhi | 18:6a4db94011d3 | 286 | |
sahilmgandhi | 18:6a4db94011d3 | 287 | /* TX Status Information Word */ |
sahilmgandhi | 18:6a4db94011d3 | 288 | #define TINFO_COL_CNT 0x01E00000 /* Collision Count */ |
sahilmgandhi | 18:6a4db94011d3 | 289 | #define TINFO_DEFER 0x02000000 /* Packet Deferred (not an error) */ |
sahilmgandhi | 18:6a4db94011d3 | 290 | #define TINFO_EXCESS_DEF 0x04000000 /* Excessive Deferral */ |
sahilmgandhi | 18:6a4db94011d3 | 291 | #define TINFO_EXCESS_COL 0x08000000 /* Excessive Collision */ |
sahilmgandhi | 18:6a4db94011d3 | 292 | #define TINFO_LATE_COL 0x10000000 /* Late Collision Occured */ |
sahilmgandhi | 18:6a4db94011d3 | 293 | #define TINFO_UNDERRUN 0x20000000 /* Transmit Underrun */ |
sahilmgandhi | 18:6a4db94011d3 | 294 | #define TINFO_NO_DESCR 0x40000000 /* No new Descriptor available */ |
sahilmgandhi | 18:6a4db94011d3 | 295 | #define TINFO_ERR 0x80000000 /* Error Occured (OR of all errors) */ |
sahilmgandhi | 18:6a4db94011d3 | 296 | |
sahilmgandhi | 18:6a4db94011d3 | 297 | /* ENET Device Revision ID */ |
sahilmgandhi | 18:6a4db94011d3 | 298 | #define OLD_EMAC_MODULE_ID 0x39022000 /* Rev. ID for first rev '-' */ |
sahilmgandhi | 18:6a4db94011d3 | 299 | |
sahilmgandhi | 18:6a4db94011d3 | 300 | /* DP83848C PHY Registers */ |
sahilmgandhi | 18:6a4db94011d3 | 301 | #define PHY_REG_BMCR 0x00 /* Basic Mode Control Register */ |
sahilmgandhi | 18:6a4db94011d3 | 302 | #define PHY_REG_BMSR 0x01 /* Basic Mode Status Register */ |
sahilmgandhi | 18:6a4db94011d3 | 303 | #define PHY_REG_IDR1 0x02 /* PHY Identifier 1 */ |
sahilmgandhi | 18:6a4db94011d3 | 304 | #define PHY_REG_IDR2 0x03 /* PHY Identifier 2 */ |
sahilmgandhi | 18:6a4db94011d3 | 305 | #define PHY_REG_ANAR 0x04 /* Auto-Negotiation Advertisement */ |
sahilmgandhi | 18:6a4db94011d3 | 306 | #define PHY_REG_ANLPAR 0x05 /* Auto-Neg. Link Partner Abitily */ |
sahilmgandhi | 18:6a4db94011d3 | 307 | #define PHY_REG_ANER 0x06 /* Auto-Neg. Expansion Register */ |
sahilmgandhi | 18:6a4db94011d3 | 308 | #define PHY_REG_ANNPTR 0x07 /* Auto-Neg. Next Page TX */ |
sahilmgandhi | 18:6a4db94011d3 | 309 | |
sahilmgandhi | 18:6a4db94011d3 | 310 | /* PHY Extended Registers */ |
sahilmgandhi | 18:6a4db94011d3 | 311 | #define PHY_REG_STS 0x10 /* Status Register */ |
sahilmgandhi | 18:6a4db94011d3 | 312 | #define PHY_REG_MICR 0x11 /* MII Interrupt Control Register */ |
sahilmgandhi | 18:6a4db94011d3 | 313 | #define PHY_REG_MISR 0x12 /* MII Interrupt Status Register */ |
sahilmgandhi | 18:6a4db94011d3 | 314 | #define PHY_REG_FCSCR 0x14 /* False Carrier Sense Counter */ |
sahilmgandhi | 18:6a4db94011d3 | 315 | #define PHY_REG_RECR 0x15 /* Receive Error Counter */ |
sahilmgandhi | 18:6a4db94011d3 | 316 | #define PHY_REG_PCSR 0x16 /* PCS Sublayer Config. and Status */ |
sahilmgandhi | 18:6a4db94011d3 | 317 | #define PHY_REG_RBR 0x17 /* RMII and Bypass Register */ |
sahilmgandhi | 18:6a4db94011d3 | 318 | #define PHY_REG_LEDCR 0x18 /* LED Direct Control Register */ |
sahilmgandhi | 18:6a4db94011d3 | 319 | #define PHY_REG_PHYCR 0x19 /* PHY Control Register */ |
sahilmgandhi | 18:6a4db94011d3 | 320 | #define PHY_REG_10BTSCR 0x1A /* 10Base-T Status/Control Register */ |
sahilmgandhi | 18:6a4db94011d3 | 321 | #define PHY_REG_CDCTRL1 0x1B /* CD Test Control and BIST Extens. */ |
sahilmgandhi | 18:6a4db94011d3 | 322 | #define PHY_REG_EDCR 0x1D /* Energy Detect Control Register */ |
sahilmgandhi | 18:6a4db94011d3 | 323 | |
sahilmgandhi | 18:6a4db94011d3 | 324 | #define PHY_REG_SCSR 0x1F /* PHY Special Control/Status Register */ |
sahilmgandhi | 18:6a4db94011d3 | 325 | |
sahilmgandhi | 18:6a4db94011d3 | 326 | #define PHY_FULLD_100M 0x2100 /* Full Duplex 100Mbit */ |
sahilmgandhi | 18:6a4db94011d3 | 327 | #define PHY_HALFD_100M 0x2000 /* Half Duplex 100Mbit */ |
sahilmgandhi | 18:6a4db94011d3 | 328 | #define PHY_FULLD_10M 0x0100 /* Full Duplex 10Mbit */ |
sahilmgandhi | 18:6a4db94011d3 | 329 | #define PHY_HALFD_10M 0x0000 /* Half Duplex 10MBit */ |
sahilmgandhi | 18:6a4db94011d3 | 330 | #define PHY_AUTO_NEG 0x3000 /* Select Auto Negotiation */ |
sahilmgandhi | 18:6a4db94011d3 | 331 | |
sahilmgandhi | 18:6a4db94011d3 | 332 | #define DP83848C_DEF_ADR 0x0100 /* Default PHY device address */ |
sahilmgandhi | 18:6a4db94011d3 | 333 | #define DP83848C_ID 0x20005C90 /* PHY Identifier - DP83848C */ |
sahilmgandhi | 18:6a4db94011d3 | 334 | |
sahilmgandhi | 18:6a4db94011d3 | 335 | #define LAN8720_ID 0x0007C0F0 /* PHY Identifier - LAN8720 */ |
sahilmgandhi | 18:6a4db94011d3 | 336 | |
sahilmgandhi | 18:6a4db94011d3 | 337 | #define PHY_STS_LINK 0x0001 /* PHY Status Link Mask */ |
sahilmgandhi | 18:6a4db94011d3 | 338 | #define PHY_STS_SPEED 0x0002 /* PHY Status Speed Mask */ |
sahilmgandhi | 18:6a4db94011d3 | 339 | #define PHY_STS_DUPLEX 0x0004 /* PHY Status Duplex Mask */ |
sahilmgandhi | 18:6a4db94011d3 | 340 | |
sahilmgandhi | 18:6a4db94011d3 | 341 | #define PHY_BMCR_RESET 0x8000 /* PHY Reset */ |
sahilmgandhi | 18:6a4db94011d3 | 342 | |
sahilmgandhi | 18:6a4db94011d3 | 343 | #define PHY_BMSR_LINK 0x0004 /* PHY BMSR Link valid */ |
sahilmgandhi | 18:6a4db94011d3 | 344 | |
sahilmgandhi | 18:6a4db94011d3 | 345 | #define PHY_SCSR_100MBIT 0x0008 /* Speed: 1=100 MBit, 0=10Mbit */ |
sahilmgandhi | 18:6a4db94011d3 | 346 | #define PHY_SCSR_DUPLEX 0x0010 /* PHY Duplex Mask */ |
sahilmgandhi | 18:6a4db94011d3 | 347 | |
sahilmgandhi | 18:6a4db94011d3 | 348 | |
sahilmgandhi | 18:6a4db94011d3 | 349 | static int phy_read(unsigned int PhyReg); |
sahilmgandhi | 18:6a4db94011d3 | 350 | static int phy_write(unsigned int PhyReg, unsigned short Data); |
sahilmgandhi | 18:6a4db94011d3 | 351 | |
sahilmgandhi | 18:6a4db94011d3 | 352 | static void txdscr_init(void); |
sahilmgandhi | 18:6a4db94011d3 | 353 | static void rxdscr_init(void); |
sahilmgandhi | 18:6a4db94011d3 | 354 | |
sahilmgandhi | 18:6a4db94011d3 | 355 | #if defined (__ICCARM__) |
sahilmgandhi | 18:6a4db94011d3 | 356 | # define AHBSRAM1 |
sahilmgandhi | 18:6a4db94011d3 | 357 | #elif defined(TOOLCHAIN_GCC_CR) |
sahilmgandhi | 18:6a4db94011d3 | 358 | # define AHBSRAM1 __attribute__((section(".data.$RamPeriph32"))) |
sahilmgandhi | 18:6a4db94011d3 | 359 | #else |
sahilmgandhi | 18:6a4db94011d3 | 360 | # define AHBSRAM1 __attribute__((section("AHBSRAM1"),aligned)) |
sahilmgandhi | 18:6a4db94011d3 | 361 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 362 | |
sahilmgandhi | 18:6a4db94011d3 | 363 | AHBSRAM1 volatile uint8_t rxbuf[NUM_RX_FRAG][ETH_FRAG_SIZE]; |
sahilmgandhi | 18:6a4db94011d3 | 364 | AHBSRAM1 volatile uint8_t txbuf[NUM_TX_FRAG][ETH_FRAG_SIZE]; |
sahilmgandhi | 18:6a4db94011d3 | 365 | AHBSRAM1 volatile RX_DESC_TypeDef rxdesc[NUM_RX_FRAG]; |
sahilmgandhi | 18:6a4db94011d3 | 366 | AHBSRAM1 volatile RX_STAT_TypeDef rxstat[NUM_RX_FRAG]; |
sahilmgandhi | 18:6a4db94011d3 | 367 | AHBSRAM1 volatile TX_DESC_TypeDef txdesc[NUM_TX_FRAG]; |
sahilmgandhi | 18:6a4db94011d3 | 368 | AHBSRAM1 volatile TX_STAT_TypeDef txstat[NUM_TX_FRAG]; |
sahilmgandhi | 18:6a4db94011d3 | 369 | |
sahilmgandhi | 18:6a4db94011d3 | 370 | |
sahilmgandhi | 18:6a4db94011d3 | 371 | #if NEW_LOGIC |
sahilmgandhi | 18:6a4db94011d3 | 372 | static int rx_consume_offset = -1; |
sahilmgandhi | 18:6a4db94011d3 | 373 | static int tx_produce_offset = -1; |
sahilmgandhi | 18:6a4db94011d3 | 374 | #else |
sahilmgandhi | 18:6a4db94011d3 | 375 | static int send_doff = 0; |
sahilmgandhi | 18:6a4db94011d3 | 376 | static int send_idx = -1; |
sahilmgandhi | 18:6a4db94011d3 | 377 | static int send_size = 0; |
sahilmgandhi | 18:6a4db94011d3 | 378 | |
sahilmgandhi | 18:6a4db94011d3 | 379 | static int receive_soff = 0; |
sahilmgandhi | 18:6a4db94011d3 | 380 | static int receive_idx = -1; |
sahilmgandhi | 18:6a4db94011d3 | 381 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 382 | |
sahilmgandhi | 18:6a4db94011d3 | 383 | static uint32_t phy_id = 0; |
sahilmgandhi | 18:6a4db94011d3 | 384 | |
sahilmgandhi | 18:6a4db94011d3 | 385 | static inline int rinc(int idx, int mod) { |
sahilmgandhi | 18:6a4db94011d3 | 386 | ++idx; |
sahilmgandhi | 18:6a4db94011d3 | 387 | idx %= mod; |
sahilmgandhi | 18:6a4db94011d3 | 388 | return idx; |
sahilmgandhi | 18:6a4db94011d3 | 389 | } |
sahilmgandhi | 18:6a4db94011d3 | 390 | |
sahilmgandhi | 18:6a4db94011d3 | 391 | //extern unsigned int SystemFrequency; |
sahilmgandhi | 18:6a4db94011d3 | 392 | static inline unsigned int clockselect() { |
sahilmgandhi | 18:6a4db94011d3 | 393 | if(SystemCoreClock < 10000000) { |
sahilmgandhi | 18:6a4db94011d3 | 394 | return 1; |
sahilmgandhi | 18:6a4db94011d3 | 395 | } else if(SystemCoreClock < 15000000) { |
sahilmgandhi | 18:6a4db94011d3 | 396 | return 2; |
sahilmgandhi | 18:6a4db94011d3 | 397 | } else if(SystemCoreClock < 20000000) { |
sahilmgandhi | 18:6a4db94011d3 | 398 | return 3; |
sahilmgandhi | 18:6a4db94011d3 | 399 | } else if(SystemCoreClock < 25000000) { |
sahilmgandhi | 18:6a4db94011d3 | 400 | return 4; |
sahilmgandhi | 18:6a4db94011d3 | 401 | } else if(SystemCoreClock < 35000000) { |
sahilmgandhi | 18:6a4db94011d3 | 402 | return 5; |
sahilmgandhi | 18:6a4db94011d3 | 403 | } else if(SystemCoreClock < 50000000) { |
sahilmgandhi | 18:6a4db94011d3 | 404 | return 6; |
sahilmgandhi | 18:6a4db94011d3 | 405 | } else if(SystemCoreClock < 70000000) { |
sahilmgandhi | 18:6a4db94011d3 | 406 | return 7; |
sahilmgandhi | 18:6a4db94011d3 | 407 | } else if(SystemCoreClock < 80000000) { |
sahilmgandhi | 18:6a4db94011d3 | 408 | return 8; |
sahilmgandhi | 18:6a4db94011d3 | 409 | } else if(SystemCoreClock < 90000000) { |
sahilmgandhi | 18:6a4db94011d3 | 410 | return 9; |
sahilmgandhi | 18:6a4db94011d3 | 411 | } else if(SystemCoreClock < 100000000) { |
sahilmgandhi | 18:6a4db94011d3 | 412 | return 10; |
sahilmgandhi | 18:6a4db94011d3 | 413 | } else if(SystemCoreClock < 120000000) { |
sahilmgandhi | 18:6a4db94011d3 | 414 | return 11; |
sahilmgandhi | 18:6a4db94011d3 | 415 | } else if(SystemCoreClock < 130000000) { |
sahilmgandhi | 18:6a4db94011d3 | 416 | return 12; |
sahilmgandhi | 18:6a4db94011d3 | 417 | } else if(SystemCoreClock < 140000000) { |
sahilmgandhi | 18:6a4db94011d3 | 418 | return 13; |
sahilmgandhi | 18:6a4db94011d3 | 419 | } else if(SystemCoreClock < 150000000) { |
sahilmgandhi | 18:6a4db94011d3 | 420 | return 15; |
sahilmgandhi | 18:6a4db94011d3 | 421 | } else if(SystemCoreClock < 160000000) { |
sahilmgandhi | 18:6a4db94011d3 | 422 | return 16; |
sahilmgandhi | 18:6a4db94011d3 | 423 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 424 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 425 | } |
sahilmgandhi | 18:6a4db94011d3 | 426 | } |
sahilmgandhi | 18:6a4db94011d3 | 427 | |
sahilmgandhi | 18:6a4db94011d3 | 428 | #ifndef min |
sahilmgandhi | 18:6a4db94011d3 | 429 | #define min(x, y) (((x)<(y))?(x):(y)) |
sahilmgandhi | 18:6a4db94011d3 | 430 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 431 | |
sahilmgandhi | 18:6a4db94011d3 | 432 | /*---------------------------------------------------------------------------- |
sahilmgandhi | 18:6a4db94011d3 | 433 | Ethernet Device initialize |
sahilmgandhi | 18:6a4db94011d3 | 434 | *----------------------------------------------------------------------------*/ |
sahilmgandhi | 18:6a4db94011d3 | 435 | int ethernet_init() { |
sahilmgandhi | 18:6a4db94011d3 | 436 | int regv, tout; |
sahilmgandhi | 18:6a4db94011d3 | 437 | char mac[ETHERNET_ADDR_SIZE]; |
sahilmgandhi | 18:6a4db94011d3 | 438 | unsigned int clock = clockselect(); |
sahilmgandhi | 18:6a4db94011d3 | 439 | |
sahilmgandhi | 18:6a4db94011d3 | 440 | LPC_SC->PCONP |= 0x40000000; /* Power Up the EMAC controller. */ |
sahilmgandhi | 18:6a4db94011d3 | 441 | |
sahilmgandhi | 18:6a4db94011d3 | 442 | LPC_PINCON->PINSEL2 = 0x50150105; /* Enable P1 Ethernet Pins. */ |
sahilmgandhi | 18:6a4db94011d3 | 443 | LPC_PINCON->PINSEL3 = (LPC_PINCON->PINSEL3 & ~0x0000000F) | 0x00000005; |
sahilmgandhi | 18:6a4db94011d3 | 444 | |
sahilmgandhi | 18:6a4db94011d3 | 445 | /* Reset all EMAC internal modules. */ |
sahilmgandhi | 18:6a4db94011d3 | 446 | LPC_EMAC->MAC1 = MAC1_RES_TX | MAC1_RES_MCS_TX | MAC1_RES_RX | |
sahilmgandhi | 18:6a4db94011d3 | 447 | MAC1_RES_MCS_RX | MAC1_SIM_RES | MAC1_SOFT_RES; |
sahilmgandhi | 18:6a4db94011d3 | 448 | LPC_EMAC->Command = CR_REG_RES | CR_TX_RES | CR_RX_RES | CR_PASS_RUNT_FRM; |
sahilmgandhi | 18:6a4db94011d3 | 449 | |
sahilmgandhi | 18:6a4db94011d3 | 450 | for(tout = 100; tout; tout--) __NOP(); /* A short delay after reset. */ |
sahilmgandhi | 18:6a4db94011d3 | 451 | |
sahilmgandhi | 18:6a4db94011d3 | 452 | LPC_EMAC->MAC1 = MAC1_PASS_ALL; /* Initialize MAC control registers. */ |
sahilmgandhi | 18:6a4db94011d3 | 453 | LPC_EMAC->MAC2 = MAC2_CRC_EN | MAC2_PAD_EN; |
sahilmgandhi | 18:6a4db94011d3 | 454 | LPC_EMAC->MAXF = ETH_MAX_FLEN; |
sahilmgandhi | 18:6a4db94011d3 | 455 | LPC_EMAC->CLRT = CLRT_DEF; |
sahilmgandhi | 18:6a4db94011d3 | 456 | LPC_EMAC->IPGR = IPGR_DEF; |
sahilmgandhi | 18:6a4db94011d3 | 457 | |
sahilmgandhi | 18:6a4db94011d3 | 458 | LPC_EMAC->Command = CR_RMII | CR_PASS_RUNT_FRM; /* Enable Reduced MII interface. */ |
sahilmgandhi | 18:6a4db94011d3 | 459 | |
sahilmgandhi | 18:6a4db94011d3 | 460 | LPC_EMAC->MCFG = (clock << 0x2) & MCFG_CLK_SEL; /* Set clock */ |
sahilmgandhi | 18:6a4db94011d3 | 461 | LPC_EMAC->MCFG |= MCFG_RES_MII; /* and reset */ |
sahilmgandhi | 18:6a4db94011d3 | 462 | |
sahilmgandhi | 18:6a4db94011d3 | 463 | for(tout = 100; tout; tout--) __NOP(); /* A short delay */ |
sahilmgandhi | 18:6a4db94011d3 | 464 | |
sahilmgandhi | 18:6a4db94011d3 | 465 | LPC_EMAC->MCFG = (clock << 0x2) & MCFG_CLK_SEL; |
sahilmgandhi | 18:6a4db94011d3 | 466 | LPC_EMAC->MCMD = 0; |
sahilmgandhi | 18:6a4db94011d3 | 467 | |
sahilmgandhi | 18:6a4db94011d3 | 468 | LPC_EMAC->SUPP = SUPP_RES_RMII; /* Reset Reduced MII Logic. */ |
sahilmgandhi | 18:6a4db94011d3 | 469 | |
sahilmgandhi | 18:6a4db94011d3 | 470 | for (tout = 100; tout; tout--) __NOP(); /* A short delay */ |
sahilmgandhi | 18:6a4db94011d3 | 471 | |
sahilmgandhi | 18:6a4db94011d3 | 472 | LPC_EMAC->SUPP = 0; |
sahilmgandhi | 18:6a4db94011d3 | 473 | |
sahilmgandhi | 18:6a4db94011d3 | 474 | phy_write(PHY_REG_BMCR, PHY_BMCR_RESET); /* perform PHY reset */ |
sahilmgandhi | 18:6a4db94011d3 | 475 | for(tout = 0x20000; ; tout--) { /* Wait for hardware reset to end. */ |
sahilmgandhi | 18:6a4db94011d3 | 476 | regv = phy_read(PHY_REG_BMCR); |
sahilmgandhi | 18:6a4db94011d3 | 477 | if(regv < 0 || tout == 0) { |
sahilmgandhi | 18:6a4db94011d3 | 478 | return -1; /* Error */ |
sahilmgandhi | 18:6a4db94011d3 | 479 | } |
sahilmgandhi | 18:6a4db94011d3 | 480 | if(!(regv & PHY_BMCR_RESET)) { |
sahilmgandhi | 18:6a4db94011d3 | 481 | break; /* Reset complete. */ |
sahilmgandhi | 18:6a4db94011d3 | 482 | } |
sahilmgandhi | 18:6a4db94011d3 | 483 | } |
sahilmgandhi | 18:6a4db94011d3 | 484 | |
sahilmgandhi | 18:6a4db94011d3 | 485 | phy_id = (phy_read(PHY_REG_IDR1) << 16); |
sahilmgandhi | 18:6a4db94011d3 | 486 | phy_id |= (phy_read(PHY_REG_IDR2) & 0XFFF0); |
sahilmgandhi | 18:6a4db94011d3 | 487 | |
sahilmgandhi | 18:6a4db94011d3 | 488 | if (phy_id != DP83848C_ID && phy_id != LAN8720_ID) { |
sahilmgandhi | 18:6a4db94011d3 | 489 | error("Unknown Ethernet PHY (%x)", (unsigned int)phy_id); |
sahilmgandhi | 18:6a4db94011d3 | 490 | } |
sahilmgandhi | 18:6a4db94011d3 | 491 | |
sahilmgandhi | 18:6a4db94011d3 | 492 | ethernet_set_link(-1, 0); |
sahilmgandhi | 18:6a4db94011d3 | 493 | |
sahilmgandhi | 18:6a4db94011d3 | 494 | /* Set the Ethernet MAC Address registers */ |
sahilmgandhi | 18:6a4db94011d3 | 495 | ethernet_address(mac); |
sahilmgandhi | 18:6a4db94011d3 | 496 | LPC_EMAC->SA0 = ((uint32_t)mac[5] << 8) | (uint32_t)mac[4]; |
sahilmgandhi | 18:6a4db94011d3 | 497 | LPC_EMAC->SA1 = ((uint32_t)mac[3] << 8) | (uint32_t)mac[2]; |
sahilmgandhi | 18:6a4db94011d3 | 498 | LPC_EMAC->SA2 = ((uint32_t)mac[1] << 8) | (uint32_t)mac[0]; |
sahilmgandhi | 18:6a4db94011d3 | 499 | |
sahilmgandhi | 18:6a4db94011d3 | 500 | txdscr_init(); /* initialize DMA TX Descriptor */ |
sahilmgandhi | 18:6a4db94011d3 | 501 | rxdscr_init(); /* initialize DMA RX Descriptor */ |
sahilmgandhi | 18:6a4db94011d3 | 502 | |
sahilmgandhi | 18:6a4db94011d3 | 503 | LPC_EMAC->RxFilterCtrl = RFC_UCAST_EN | RFC_MCAST_EN | RFC_BCAST_EN | RFC_PERFECT_EN; |
sahilmgandhi | 18:6a4db94011d3 | 504 | /* Receive Broadcast, Perfect Match Packets */ |
sahilmgandhi | 18:6a4db94011d3 | 505 | |
sahilmgandhi | 18:6a4db94011d3 | 506 | LPC_EMAC->IntEnable = INT_RX_DONE | INT_TX_DONE; /* Enable EMAC interrupts. */ |
sahilmgandhi | 18:6a4db94011d3 | 507 | LPC_EMAC->IntClear = 0xFFFF; /* Reset all interrupts */ |
sahilmgandhi | 18:6a4db94011d3 | 508 | |
sahilmgandhi | 18:6a4db94011d3 | 509 | |
sahilmgandhi | 18:6a4db94011d3 | 510 | LPC_EMAC->Command |= (CR_RX_EN | CR_TX_EN); /* Enable receive and transmit mode of MAC Ethernet core */ |
sahilmgandhi | 18:6a4db94011d3 | 511 | LPC_EMAC->MAC1 |= MAC1_REC_EN; |
sahilmgandhi | 18:6a4db94011d3 | 512 | |
sahilmgandhi | 18:6a4db94011d3 | 513 | #if NEW_LOGIC |
sahilmgandhi | 18:6a4db94011d3 | 514 | rx_consume_offset = -1; |
sahilmgandhi | 18:6a4db94011d3 | 515 | tx_produce_offset = -1; |
sahilmgandhi | 18:6a4db94011d3 | 516 | #else |
sahilmgandhi | 18:6a4db94011d3 | 517 | send_doff = 0; |
sahilmgandhi | 18:6a4db94011d3 | 518 | send_idx = -1; |
sahilmgandhi | 18:6a4db94011d3 | 519 | send_size = 0; |
sahilmgandhi | 18:6a4db94011d3 | 520 | |
sahilmgandhi | 18:6a4db94011d3 | 521 | receive_soff = 0; |
sahilmgandhi | 18:6a4db94011d3 | 522 | receive_idx = -1; |
sahilmgandhi | 18:6a4db94011d3 | 523 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 524 | |
sahilmgandhi | 18:6a4db94011d3 | 525 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 526 | } |
sahilmgandhi | 18:6a4db94011d3 | 527 | |
sahilmgandhi | 18:6a4db94011d3 | 528 | /*---------------------------------------------------------------------------- |
sahilmgandhi | 18:6a4db94011d3 | 529 | Ethernet Device Uninitialize |
sahilmgandhi | 18:6a4db94011d3 | 530 | *----------------------------------------------------------------------------*/ |
sahilmgandhi | 18:6a4db94011d3 | 531 | void ethernet_free() { |
sahilmgandhi | 18:6a4db94011d3 | 532 | LPC_EMAC->IntEnable &= ~(INT_RX_DONE | INT_TX_DONE); |
sahilmgandhi | 18:6a4db94011d3 | 533 | LPC_EMAC->IntClear = 0xFFFF; |
sahilmgandhi | 18:6a4db94011d3 | 534 | |
sahilmgandhi | 18:6a4db94011d3 | 535 | LPC_SC->PCONP &= ~0x40000000; /* Power down the EMAC controller. */ |
sahilmgandhi | 18:6a4db94011d3 | 536 | |
sahilmgandhi | 18:6a4db94011d3 | 537 | LPC_PINCON->PINSEL2 &= ~0x50150105; /* Disable P1 ethernet pins. */ |
sahilmgandhi | 18:6a4db94011d3 | 538 | LPC_PINCON->PINSEL3 = (LPC_PINCON->PINSEL3 & ~0x0000000F) | 0x00000000; |
sahilmgandhi | 18:6a4db94011d3 | 539 | } |
sahilmgandhi | 18:6a4db94011d3 | 540 | |
sahilmgandhi | 18:6a4db94011d3 | 541 | // if(TxProduceIndex == TxConsumeIndex) buffer array is empty |
sahilmgandhi | 18:6a4db94011d3 | 542 | // if(TxProduceIndex == TxConsumeIndex - 1) buffer is full, should not fill |
sahilmgandhi | 18:6a4db94011d3 | 543 | // TxProduceIndex - The buffer that will/is being fileld by driver, s/w increment |
sahilmgandhi | 18:6a4db94011d3 | 544 | // TxConsumeIndex - The buffer that will/is beign sent by hardware |
sahilmgandhi | 18:6a4db94011d3 | 545 | |
sahilmgandhi | 18:6a4db94011d3 | 546 | int ethernet_write(const char *data, int slen) { |
sahilmgandhi | 18:6a4db94011d3 | 547 | |
sahilmgandhi | 18:6a4db94011d3 | 548 | #if NEW_LOGIC |
sahilmgandhi | 18:6a4db94011d3 | 549 | |
sahilmgandhi | 18:6a4db94011d3 | 550 | if(tx_produce_offset < 0) { // mark as active if not already |
sahilmgandhi | 18:6a4db94011d3 | 551 | tx_produce_offset = 0; |
sahilmgandhi | 18:6a4db94011d3 | 552 | } |
sahilmgandhi | 18:6a4db94011d3 | 553 | |
sahilmgandhi | 18:6a4db94011d3 | 554 | int index = LPC_EMAC->TxProduceIndex; |
sahilmgandhi | 18:6a4db94011d3 | 555 | |
sahilmgandhi | 18:6a4db94011d3 | 556 | int remaining = ETH_MAX_FLEN - tx_produce_offset - 4; // bytes written plus checksum |
sahilmgandhi | 18:6a4db94011d3 | 557 | int requested = slen; |
sahilmgandhi | 18:6a4db94011d3 | 558 | int ncopy = min(remaining, requested); |
sahilmgandhi | 18:6a4db94011d3 | 559 | |
sahilmgandhi | 18:6a4db94011d3 | 560 | void *pdst = (void *)(txdesc[index].Packet + tx_produce_offset); |
sahilmgandhi | 18:6a4db94011d3 | 561 | void *psrc = (void *)(data); |
sahilmgandhi | 18:6a4db94011d3 | 562 | |
sahilmgandhi | 18:6a4db94011d3 | 563 | if(ncopy > 0 ){ |
sahilmgandhi | 18:6a4db94011d3 | 564 | if(data != NULL) { |
sahilmgandhi | 18:6a4db94011d3 | 565 | memcpy(pdst, psrc, ncopy); |
sahilmgandhi | 18:6a4db94011d3 | 566 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 567 | memset(pdst, 0, ncopy); |
sahilmgandhi | 18:6a4db94011d3 | 568 | } |
sahilmgandhi | 18:6a4db94011d3 | 569 | } |
sahilmgandhi | 18:6a4db94011d3 | 570 | |
sahilmgandhi | 18:6a4db94011d3 | 571 | tx_produce_offset += ncopy; |
sahilmgandhi | 18:6a4db94011d3 | 572 | |
sahilmgandhi | 18:6a4db94011d3 | 573 | return ncopy; |
sahilmgandhi | 18:6a4db94011d3 | 574 | |
sahilmgandhi | 18:6a4db94011d3 | 575 | #else |
sahilmgandhi | 18:6a4db94011d3 | 576 | void *pdst, *psrc; |
sahilmgandhi | 18:6a4db94011d3 | 577 | const int dlen = ETH_FRAG_SIZE; |
sahilmgandhi | 18:6a4db94011d3 | 578 | int copy = 0; |
sahilmgandhi | 18:6a4db94011d3 | 579 | int soff = 0; |
sahilmgandhi | 18:6a4db94011d3 | 580 | |
sahilmgandhi | 18:6a4db94011d3 | 581 | if(send_idx == -1) { |
sahilmgandhi | 18:6a4db94011d3 | 582 | send_idx = LPC_EMAC->TxProduceIndex; |
sahilmgandhi | 18:6a4db94011d3 | 583 | } |
sahilmgandhi | 18:6a4db94011d3 | 584 | |
sahilmgandhi | 18:6a4db94011d3 | 585 | if(slen + send_doff > ethernet_MTU_SIZE) { |
sahilmgandhi | 18:6a4db94011d3 | 586 | return -1; |
sahilmgandhi | 18:6a4db94011d3 | 587 | } |
sahilmgandhi | 18:6a4db94011d3 | 588 | |
sahilmgandhi | 18:6a4db94011d3 | 589 | do { |
sahilmgandhi | 18:6a4db94011d3 | 590 | copy = min(slen - soff, dlen - send_doff); |
sahilmgandhi | 18:6a4db94011d3 | 591 | pdst = (void *)(txdesc[send_idx].Packet + send_doff); |
sahilmgandhi | 18:6a4db94011d3 | 592 | psrc = (void *)(data + soff); |
sahilmgandhi | 18:6a4db94011d3 | 593 | if(send_doff + copy > ETH_FRAG_SIZE) { |
sahilmgandhi | 18:6a4db94011d3 | 594 | txdesc[send_idx].Ctrl = (send_doff-1) | (TCTRL_INT); |
sahilmgandhi | 18:6a4db94011d3 | 595 | send_idx = rinc(send_idx, NUM_TX_FRAG); |
sahilmgandhi | 18:6a4db94011d3 | 596 | send_doff = 0; |
sahilmgandhi | 18:6a4db94011d3 | 597 | } |
sahilmgandhi | 18:6a4db94011d3 | 598 | |
sahilmgandhi | 18:6a4db94011d3 | 599 | if(data != NULL) { |
sahilmgandhi | 18:6a4db94011d3 | 600 | memcpy(pdst, psrc, copy); |
sahilmgandhi | 18:6a4db94011d3 | 601 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 602 | memset(pdst, 0, copy); |
sahilmgandhi | 18:6a4db94011d3 | 603 | } |
sahilmgandhi | 18:6a4db94011d3 | 604 | |
sahilmgandhi | 18:6a4db94011d3 | 605 | soff += copy; |
sahilmgandhi | 18:6a4db94011d3 | 606 | send_doff += copy; |
sahilmgandhi | 18:6a4db94011d3 | 607 | send_size += copy; |
sahilmgandhi | 18:6a4db94011d3 | 608 | } while(soff != slen); |
sahilmgandhi | 18:6a4db94011d3 | 609 | |
sahilmgandhi | 18:6a4db94011d3 | 610 | return soff; |
sahilmgandhi | 18:6a4db94011d3 | 611 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 612 | } |
sahilmgandhi | 18:6a4db94011d3 | 613 | |
sahilmgandhi | 18:6a4db94011d3 | 614 | int ethernet_send() { |
sahilmgandhi | 18:6a4db94011d3 | 615 | |
sahilmgandhi | 18:6a4db94011d3 | 616 | #if NEW_LOGIC |
sahilmgandhi | 18:6a4db94011d3 | 617 | if(tx_produce_offset < 0) { // no buffer active |
sahilmgandhi | 18:6a4db94011d3 | 618 | return -1; |
sahilmgandhi | 18:6a4db94011d3 | 619 | } |
sahilmgandhi | 18:6a4db94011d3 | 620 | |
sahilmgandhi | 18:6a4db94011d3 | 621 | // ensure there is a link |
sahilmgandhi | 18:6a4db94011d3 | 622 | if(!ethernet_link()) { |
sahilmgandhi | 18:6a4db94011d3 | 623 | return -2; |
sahilmgandhi | 18:6a4db94011d3 | 624 | } |
sahilmgandhi | 18:6a4db94011d3 | 625 | |
sahilmgandhi | 18:6a4db94011d3 | 626 | // we have been writing in to a buffer, so finalise it |
sahilmgandhi | 18:6a4db94011d3 | 627 | int size = tx_produce_offset; |
sahilmgandhi | 18:6a4db94011d3 | 628 | int index = LPC_EMAC->TxProduceIndex; |
sahilmgandhi | 18:6a4db94011d3 | 629 | txdesc[index].Ctrl = (tx_produce_offset-1) | (TCTRL_INT | TCTRL_LAST); |
sahilmgandhi | 18:6a4db94011d3 | 630 | |
sahilmgandhi | 18:6a4db94011d3 | 631 | // Increment ProduceIndex to allow it to be sent |
sahilmgandhi | 18:6a4db94011d3 | 632 | // We can only do this if the next slot is free |
sahilmgandhi | 18:6a4db94011d3 | 633 | int next = rinc(index, NUM_TX_FRAG); |
sahilmgandhi | 18:6a4db94011d3 | 634 | while(next == LPC_EMAC->TxConsumeIndex) { |
sahilmgandhi | 18:6a4db94011d3 | 635 | for(int i=0; i<1000; i++) { __NOP(); } |
sahilmgandhi | 18:6a4db94011d3 | 636 | } |
sahilmgandhi | 18:6a4db94011d3 | 637 | |
sahilmgandhi | 18:6a4db94011d3 | 638 | LPC_EMAC->TxProduceIndex = next; |
sahilmgandhi | 18:6a4db94011d3 | 639 | tx_produce_offset = -1; |
sahilmgandhi | 18:6a4db94011d3 | 640 | return size; |
sahilmgandhi | 18:6a4db94011d3 | 641 | |
sahilmgandhi | 18:6a4db94011d3 | 642 | #else |
sahilmgandhi | 18:6a4db94011d3 | 643 | int s = send_size; |
sahilmgandhi | 18:6a4db94011d3 | 644 | txdesc[send_idx].Ctrl = (send_doff-1) | (TCTRL_INT | TCTRL_LAST); |
sahilmgandhi | 18:6a4db94011d3 | 645 | send_idx = rinc(send_idx, NUM_TX_FRAG); |
sahilmgandhi | 18:6a4db94011d3 | 646 | LPC_EMAC->TxProduceIndex = send_idx; |
sahilmgandhi | 18:6a4db94011d3 | 647 | send_doff = 0; |
sahilmgandhi | 18:6a4db94011d3 | 648 | send_idx = -1; |
sahilmgandhi | 18:6a4db94011d3 | 649 | send_size = 0; |
sahilmgandhi | 18:6a4db94011d3 | 650 | return s; |
sahilmgandhi | 18:6a4db94011d3 | 651 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 652 | } |
sahilmgandhi | 18:6a4db94011d3 | 653 | |
sahilmgandhi | 18:6a4db94011d3 | 654 | // RxConsmeIndex - The index of buffer the driver will/is reading from. Driver should inc once read |
sahilmgandhi | 18:6a4db94011d3 | 655 | // RxProduceIndex - The index of buffer that will/is being filled by MAC. H/w will inc once rxd |
sahilmgandhi | 18:6a4db94011d3 | 656 | // |
sahilmgandhi | 18:6a4db94011d3 | 657 | // if(RxConsumeIndex == RxProduceIndex) buffer array is empty |
sahilmgandhi | 18:6a4db94011d3 | 658 | // if(RxConsumeIndex == RxProduceIndex + 1) buffer array is full |
sahilmgandhi | 18:6a4db94011d3 | 659 | |
sahilmgandhi | 18:6a4db94011d3 | 660 | // Recevies an arrived ethernet packet. |
sahilmgandhi | 18:6a4db94011d3 | 661 | // Receiving an ethernet packet will drop the last received ethernet packet |
sahilmgandhi | 18:6a4db94011d3 | 662 | // and make a new ethernet packet ready to read. |
sahilmgandhi | 18:6a4db94011d3 | 663 | // Returns size of packet, else 0 if nothing to receive |
sahilmgandhi | 18:6a4db94011d3 | 664 | |
sahilmgandhi | 18:6a4db94011d3 | 665 | // We read from RxConsumeIndex from position rx_consume_offset |
sahilmgandhi | 18:6a4db94011d3 | 666 | // if rx_consume_offset < 0, then we have not recieved the RxConsumeIndex packet for reading |
sahilmgandhi | 18:6a4db94011d3 | 667 | // rx_consume_offset = -1 // no frame |
sahilmgandhi | 18:6a4db94011d3 | 668 | // rx_consume_offset = 0 // start of frame |
sahilmgandhi | 18:6a4db94011d3 | 669 | // Assumption: A fragment should alway be a whole frame |
sahilmgandhi | 18:6a4db94011d3 | 670 | |
sahilmgandhi | 18:6a4db94011d3 | 671 | int ethernet_receive() { |
sahilmgandhi | 18:6a4db94011d3 | 672 | #if NEW_LOGIC |
sahilmgandhi | 18:6a4db94011d3 | 673 | |
sahilmgandhi | 18:6a4db94011d3 | 674 | // if we are currently reading a valid RxConsume buffer, increment to the next one |
sahilmgandhi | 18:6a4db94011d3 | 675 | if(rx_consume_offset >= 0) { |
sahilmgandhi | 18:6a4db94011d3 | 676 | LPC_EMAC->RxConsumeIndex = rinc(LPC_EMAC->RxConsumeIndex, NUM_RX_FRAG); |
sahilmgandhi | 18:6a4db94011d3 | 677 | } |
sahilmgandhi | 18:6a4db94011d3 | 678 | |
sahilmgandhi | 18:6a4db94011d3 | 679 | // if the buffer is empty, mark it as no valid buffer |
sahilmgandhi | 18:6a4db94011d3 | 680 | if(LPC_EMAC->RxConsumeIndex == LPC_EMAC->RxProduceIndex) { |
sahilmgandhi | 18:6a4db94011d3 | 681 | rx_consume_offset = -1; |
sahilmgandhi | 18:6a4db94011d3 | 682 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 683 | } |
sahilmgandhi | 18:6a4db94011d3 | 684 | |
sahilmgandhi | 18:6a4db94011d3 | 685 | uint32_t info = rxstat[LPC_EMAC->RxConsumeIndex].Info; |
sahilmgandhi | 18:6a4db94011d3 | 686 | rx_consume_offset = 0; |
sahilmgandhi | 18:6a4db94011d3 | 687 | |
sahilmgandhi | 18:6a4db94011d3 | 688 | // check if it is not marked as last or for errors |
sahilmgandhi | 18:6a4db94011d3 | 689 | if(!(info & RINFO_LAST_FLAG) || (info & RINFO_ERR_MASK)) { |
sahilmgandhi | 18:6a4db94011d3 | 690 | return -1; |
sahilmgandhi | 18:6a4db94011d3 | 691 | } |
sahilmgandhi | 18:6a4db94011d3 | 692 | |
sahilmgandhi | 18:6a4db94011d3 | 693 | int size = (info & RINFO_SIZE) + 1; |
sahilmgandhi | 18:6a4db94011d3 | 694 | return size - 4; // don't include checksum bytes |
sahilmgandhi | 18:6a4db94011d3 | 695 | |
sahilmgandhi | 18:6a4db94011d3 | 696 | #else |
sahilmgandhi | 18:6a4db94011d3 | 697 | if(receive_idx == -1) { |
sahilmgandhi | 18:6a4db94011d3 | 698 | receive_idx = LPC_EMAC->RxConsumeIndex; |
sahilmgandhi | 18:6a4db94011d3 | 699 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 700 | while(!(rxstat[receive_idx].Info & RINFO_LAST_FLAG) && ((uint32_t)receive_idx != LPC_EMAC->RxProduceIndex)) { |
sahilmgandhi | 18:6a4db94011d3 | 701 | receive_idx = rinc(receive_idx, NUM_RX_FRAG); |
sahilmgandhi | 18:6a4db94011d3 | 702 | } |
sahilmgandhi | 18:6a4db94011d3 | 703 | unsigned int info = rxstat[receive_idx].Info; |
sahilmgandhi | 18:6a4db94011d3 | 704 | int slen = (info & RINFO_SIZE) + 1; |
sahilmgandhi | 18:6a4db94011d3 | 705 | |
sahilmgandhi | 18:6a4db94011d3 | 706 | if(slen > ethernet_MTU_SIZE || (info & RINFO_ERR_MASK)) { |
sahilmgandhi | 18:6a4db94011d3 | 707 | /* Invalid frame, ignore it and free buffer. */ |
sahilmgandhi | 18:6a4db94011d3 | 708 | receive_idx = rinc(receive_idx, NUM_RX_FRAG); |
sahilmgandhi | 18:6a4db94011d3 | 709 | } |
sahilmgandhi | 18:6a4db94011d3 | 710 | receive_idx = rinc(receive_idx, NUM_RX_FRAG); |
sahilmgandhi | 18:6a4db94011d3 | 711 | receive_soff = 0; |
sahilmgandhi | 18:6a4db94011d3 | 712 | |
sahilmgandhi | 18:6a4db94011d3 | 713 | LPC_EMAC->RxConsumeIndex = receive_idx; |
sahilmgandhi | 18:6a4db94011d3 | 714 | } |
sahilmgandhi | 18:6a4db94011d3 | 715 | |
sahilmgandhi | 18:6a4db94011d3 | 716 | if((uint32_t)receive_idx == LPC_EMAC->RxProduceIndex) { |
sahilmgandhi | 18:6a4db94011d3 | 717 | receive_idx = -1; |
sahilmgandhi | 18:6a4db94011d3 | 718 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 719 | } |
sahilmgandhi | 18:6a4db94011d3 | 720 | |
sahilmgandhi | 18:6a4db94011d3 | 721 | return (rxstat[receive_idx].Info & RINFO_SIZE) - 3; |
sahilmgandhi | 18:6a4db94011d3 | 722 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 723 | } |
sahilmgandhi | 18:6a4db94011d3 | 724 | |
sahilmgandhi | 18:6a4db94011d3 | 725 | // Read from an recevied ethernet packet. |
sahilmgandhi | 18:6a4db94011d3 | 726 | // After receive returnd a number bigger than 0 it is |
sahilmgandhi | 18:6a4db94011d3 | 727 | // possible to read bytes from this packet. |
sahilmgandhi | 18:6a4db94011d3 | 728 | // Read will write up to size bytes into data. |
sahilmgandhi | 18:6a4db94011d3 | 729 | // It is possible to use read multible times. |
sahilmgandhi | 18:6a4db94011d3 | 730 | // Each time read will start reading after the last read byte before. |
sahilmgandhi | 18:6a4db94011d3 | 731 | |
sahilmgandhi | 18:6a4db94011d3 | 732 | int ethernet_read(char *data, int dlen) { |
sahilmgandhi | 18:6a4db94011d3 | 733 | #if NEW_LOGIC |
sahilmgandhi | 18:6a4db94011d3 | 734 | // Check we have a valid buffer to read |
sahilmgandhi | 18:6a4db94011d3 | 735 | if(rx_consume_offset < 0) { |
sahilmgandhi | 18:6a4db94011d3 | 736 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 737 | } |
sahilmgandhi | 18:6a4db94011d3 | 738 | |
sahilmgandhi | 18:6a4db94011d3 | 739 | // Assume 1 fragment block |
sahilmgandhi | 18:6a4db94011d3 | 740 | uint32_t info = rxstat[LPC_EMAC->RxConsumeIndex].Info; |
sahilmgandhi | 18:6a4db94011d3 | 741 | int size = (info & RINFO_SIZE) + 1 - 4; // exclude checksum |
sahilmgandhi | 18:6a4db94011d3 | 742 | |
sahilmgandhi | 18:6a4db94011d3 | 743 | int remaining = size - rx_consume_offset; |
sahilmgandhi | 18:6a4db94011d3 | 744 | int requested = dlen; |
sahilmgandhi | 18:6a4db94011d3 | 745 | int ncopy = min(remaining, requested); |
sahilmgandhi | 18:6a4db94011d3 | 746 | |
sahilmgandhi | 18:6a4db94011d3 | 747 | void *psrc = (void *)(rxdesc[LPC_EMAC->RxConsumeIndex].Packet + rx_consume_offset); |
sahilmgandhi | 18:6a4db94011d3 | 748 | void *pdst = (void *)(data); |
sahilmgandhi | 18:6a4db94011d3 | 749 | |
sahilmgandhi | 18:6a4db94011d3 | 750 | if(data != NULL && ncopy > 0) { |
sahilmgandhi | 18:6a4db94011d3 | 751 | memcpy(pdst, psrc, ncopy); |
sahilmgandhi | 18:6a4db94011d3 | 752 | } |
sahilmgandhi | 18:6a4db94011d3 | 753 | |
sahilmgandhi | 18:6a4db94011d3 | 754 | rx_consume_offset += ncopy; |
sahilmgandhi | 18:6a4db94011d3 | 755 | |
sahilmgandhi | 18:6a4db94011d3 | 756 | return ncopy; |
sahilmgandhi | 18:6a4db94011d3 | 757 | #else |
sahilmgandhi | 18:6a4db94011d3 | 758 | int slen; |
sahilmgandhi | 18:6a4db94011d3 | 759 | int copy = 0; |
sahilmgandhi | 18:6a4db94011d3 | 760 | unsigned int more; |
sahilmgandhi | 18:6a4db94011d3 | 761 | unsigned int info; |
sahilmgandhi | 18:6a4db94011d3 | 762 | void *pdst, *psrc; |
sahilmgandhi | 18:6a4db94011d3 | 763 | int doff = 0; |
sahilmgandhi | 18:6a4db94011d3 | 764 | |
sahilmgandhi | 18:6a4db94011d3 | 765 | if((uint32_t)receive_idx == LPC_EMAC->RxProduceIndex || receive_idx == -1) { |
sahilmgandhi | 18:6a4db94011d3 | 766 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 767 | } |
sahilmgandhi | 18:6a4db94011d3 | 768 | |
sahilmgandhi | 18:6a4db94011d3 | 769 | do { |
sahilmgandhi | 18:6a4db94011d3 | 770 | info = rxstat[receive_idx].Info; |
sahilmgandhi | 18:6a4db94011d3 | 771 | more = !(info & RINFO_LAST_FLAG); |
sahilmgandhi | 18:6a4db94011d3 | 772 | slen = (info & RINFO_SIZE) + 1; |
sahilmgandhi | 18:6a4db94011d3 | 773 | |
sahilmgandhi | 18:6a4db94011d3 | 774 | if(slen > ethernet_MTU_SIZE || (info & RINFO_ERR_MASK)) { |
sahilmgandhi | 18:6a4db94011d3 | 775 | /* Invalid frame, ignore it and free buffer. */ |
sahilmgandhi | 18:6a4db94011d3 | 776 | receive_idx = rinc(receive_idx, NUM_RX_FRAG); |
sahilmgandhi | 18:6a4db94011d3 | 777 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 778 | |
sahilmgandhi | 18:6a4db94011d3 | 779 | copy = min(slen - receive_soff, dlen - doff); |
sahilmgandhi | 18:6a4db94011d3 | 780 | psrc = (void *)(rxdesc[receive_idx].Packet + receive_soff); |
sahilmgandhi | 18:6a4db94011d3 | 781 | pdst = (void *)(data + doff); |
sahilmgandhi | 18:6a4db94011d3 | 782 | |
sahilmgandhi | 18:6a4db94011d3 | 783 | if(data != NULL) { |
sahilmgandhi | 18:6a4db94011d3 | 784 | /* check if Buffer available */ |
sahilmgandhi | 18:6a4db94011d3 | 785 | memcpy(pdst, psrc, copy); |
sahilmgandhi | 18:6a4db94011d3 | 786 | } |
sahilmgandhi | 18:6a4db94011d3 | 787 | |
sahilmgandhi | 18:6a4db94011d3 | 788 | receive_soff += copy; |
sahilmgandhi | 18:6a4db94011d3 | 789 | doff += copy; |
sahilmgandhi | 18:6a4db94011d3 | 790 | |
sahilmgandhi | 18:6a4db94011d3 | 791 | if((more && (receive_soff == slen))) { |
sahilmgandhi | 18:6a4db94011d3 | 792 | receive_idx = rinc(receive_idx, NUM_RX_FRAG); |
sahilmgandhi | 18:6a4db94011d3 | 793 | receive_soff = 0; |
sahilmgandhi | 18:6a4db94011d3 | 794 | } |
sahilmgandhi | 18:6a4db94011d3 | 795 | } |
sahilmgandhi | 18:6a4db94011d3 | 796 | } while(more && !(doff == dlen) && !receive_soff); |
sahilmgandhi | 18:6a4db94011d3 | 797 | |
sahilmgandhi | 18:6a4db94011d3 | 798 | return doff; |
sahilmgandhi | 18:6a4db94011d3 | 799 | #endif |
sahilmgandhi | 18:6a4db94011d3 | 800 | } |
sahilmgandhi | 18:6a4db94011d3 | 801 | |
sahilmgandhi | 18:6a4db94011d3 | 802 | int ethernet_link(void) { |
sahilmgandhi | 18:6a4db94011d3 | 803 | if (phy_id == DP83848C_ID) { |
sahilmgandhi | 18:6a4db94011d3 | 804 | return (phy_read(PHY_REG_STS) & PHY_STS_LINK); |
sahilmgandhi | 18:6a4db94011d3 | 805 | } |
sahilmgandhi | 18:6a4db94011d3 | 806 | else { // LAN8720_ID |
sahilmgandhi | 18:6a4db94011d3 | 807 | return (phy_read(PHY_REG_BMSR) & PHY_BMSR_LINK); |
sahilmgandhi | 18:6a4db94011d3 | 808 | } |
sahilmgandhi | 18:6a4db94011d3 | 809 | } |
sahilmgandhi | 18:6a4db94011d3 | 810 | |
sahilmgandhi | 18:6a4db94011d3 | 811 | static int phy_write(unsigned int PhyReg, unsigned short Data) { |
sahilmgandhi | 18:6a4db94011d3 | 812 | unsigned int timeOut; |
sahilmgandhi | 18:6a4db94011d3 | 813 | |
sahilmgandhi | 18:6a4db94011d3 | 814 | LPC_EMAC->MADR = DP83848C_DEF_ADR | PhyReg; |
sahilmgandhi | 18:6a4db94011d3 | 815 | LPC_EMAC->MWTD = Data; |
sahilmgandhi | 18:6a4db94011d3 | 816 | |
sahilmgandhi | 18:6a4db94011d3 | 817 | for(timeOut = 0; timeOut < MII_WR_TOUT; timeOut++) { /* Wait until operation completed */ |
sahilmgandhi | 18:6a4db94011d3 | 818 | if((LPC_EMAC->MIND & MIND_BUSY) == 0) { |
sahilmgandhi | 18:6a4db94011d3 | 819 | return 0; |
sahilmgandhi | 18:6a4db94011d3 | 820 | } |
sahilmgandhi | 18:6a4db94011d3 | 821 | } |
sahilmgandhi | 18:6a4db94011d3 | 822 | |
sahilmgandhi | 18:6a4db94011d3 | 823 | return -1; |
sahilmgandhi | 18:6a4db94011d3 | 824 | } |
sahilmgandhi | 18:6a4db94011d3 | 825 | |
sahilmgandhi | 18:6a4db94011d3 | 826 | static int phy_read(unsigned int PhyReg) { |
sahilmgandhi | 18:6a4db94011d3 | 827 | unsigned int timeOut; |
sahilmgandhi | 18:6a4db94011d3 | 828 | |
sahilmgandhi | 18:6a4db94011d3 | 829 | LPC_EMAC->MADR = DP83848C_DEF_ADR | PhyReg; |
sahilmgandhi | 18:6a4db94011d3 | 830 | LPC_EMAC->MCMD = MCMD_READ; |
sahilmgandhi | 18:6a4db94011d3 | 831 | |
sahilmgandhi | 18:6a4db94011d3 | 832 | for(timeOut = 0; timeOut < MII_RD_TOUT; timeOut++) { /* Wait until operation completed */ |
sahilmgandhi | 18:6a4db94011d3 | 833 | if((LPC_EMAC->MIND & MIND_BUSY) == 0) { |
sahilmgandhi | 18:6a4db94011d3 | 834 | LPC_EMAC->MCMD = 0; |
sahilmgandhi | 18:6a4db94011d3 | 835 | return LPC_EMAC->MRDD; /* Return a 16-bit value. */ |
sahilmgandhi | 18:6a4db94011d3 | 836 | } |
sahilmgandhi | 18:6a4db94011d3 | 837 | } |
sahilmgandhi | 18:6a4db94011d3 | 838 | |
sahilmgandhi | 18:6a4db94011d3 | 839 | return -1; |
sahilmgandhi | 18:6a4db94011d3 | 840 | } |
sahilmgandhi | 18:6a4db94011d3 | 841 | |
sahilmgandhi | 18:6a4db94011d3 | 842 | |
sahilmgandhi | 18:6a4db94011d3 | 843 | static void txdscr_init() { |
sahilmgandhi | 18:6a4db94011d3 | 844 | int i; |
sahilmgandhi | 18:6a4db94011d3 | 845 | |
sahilmgandhi | 18:6a4db94011d3 | 846 | for(i = 0; i < NUM_TX_FRAG; i++) { |
sahilmgandhi | 18:6a4db94011d3 | 847 | txdesc[i].Packet = (uint32_t)&txbuf[i]; |
sahilmgandhi | 18:6a4db94011d3 | 848 | txdesc[i].Ctrl = 0; |
sahilmgandhi | 18:6a4db94011d3 | 849 | txstat[i].Info = 0; |
sahilmgandhi | 18:6a4db94011d3 | 850 | } |
sahilmgandhi | 18:6a4db94011d3 | 851 | |
sahilmgandhi | 18:6a4db94011d3 | 852 | LPC_EMAC->TxDescriptor = (uint32_t)txdesc; /* Set EMAC Transmit Descriptor Registers. */ |
sahilmgandhi | 18:6a4db94011d3 | 853 | LPC_EMAC->TxStatus = (uint32_t)txstat; |
sahilmgandhi | 18:6a4db94011d3 | 854 | LPC_EMAC->TxDescriptorNumber = NUM_TX_FRAG-1; |
sahilmgandhi | 18:6a4db94011d3 | 855 | |
sahilmgandhi | 18:6a4db94011d3 | 856 | LPC_EMAC->TxProduceIndex = 0; /* Tx Descriptors Point to 0 */ |
sahilmgandhi | 18:6a4db94011d3 | 857 | } |
sahilmgandhi | 18:6a4db94011d3 | 858 | |
sahilmgandhi | 18:6a4db94011d3 | 859 | static void rxdscr_init() { |
sahilmgandhi | 18:6a4db94011d3 | 860 | int i; |
sahilmgandhi | 18:6a4db94011d3 | 861 | |
sahilmgandhi | 18:6a4db94011d3 | 862 | for(i = 0; i < NUM_RX_FRAG; i++) { |
sahilmgandhi | 18:6a4db94011d3 | 863 | rxdesc[i].Packet = (uint32_t)&rxbuf[i]; |
sahilmgandhi | 18:6a4db94011d3 | 864 | rxdesc[i].Ctrl = RCTRL_INT | (ETH_FRAG_SIZE-1); |
sahilmgandhi | 18:6a4db94011d3 | 865 | rxstat[i].Info = 0; |
sahilmgandhi | 18:6a4db94011d3 | 866 | rxstat[i].HashCRC = 0; |
sahilmgandhi | 18:6a4db94011d3 | 867 | } |
sahilmgandhi | 18:6a4db94011d3 | 868 | |
sahilmgandhi | 18:6a4db94011d3 | 869 | LPC_EMAC->RxDescriptor = (uint32_t)rxdesc; /* Set EMAC Receive Descriptor Registers. */ |
sahilmgandhi | 18:6a4db94011d3 | 870 | LPC_EMAC->RxStatus = (uint32_t)rxstat; |
sahilmgandhi | 18:6a4db94011d3 | 871 | LPC_EMAC->RxDescriptorNumber = NUM_RX_FRAG-1; |
sahilmgandhi | 18:6a4db94011d3 | 872 | |
sahilmgandhi | 18:6a4db94011d3 | 873 | LPC_EMAC->RxConsumeIndex = 0; /* Rx Descriptors Point to 0 */ |
sahilmgandhi | 18:6a4db94011d3 | 874 | } |
sahilmgandhi | 18:6a4db94011d3 | 875 | |
sahilmgandhi | 18:6a4db94011d3 | 876 | void ethernet_address(char *mac) { |
sahilmgandhi | 18:6a4db94011d3 | 877 | mbed_mac_address(mac); |
sahilmgandhi | 18:6a4db94011d3 | 878 | } |
sahilmgandhi | 18:6a4db94011d3 | 879 | |
sahilmgandhi | 18:6a4db94011d3 | 880 | void ethernet_set_link(int speed, int duplex) { |
sahilmgandhi | 18:6a4db94011d3 | 881 | unsigned short phy_data; |
sahilmgandhi | 18:6a4db94011d3 | 882 | int tout; |
sahilmgandhi | 18:6a4db94011d3 | 883 | |
sahilmgandhi | 18:6a4db94011d3 | 884 | if((speed < 0) || (speed > 1)) { |
sahilmgandhi | 18:6a4db94011d3 | 885 | phy_data = PHY_AUTO_NEG; |
sahilmgandhi | 18:6a4db94011d3 | 886 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 887 | phy_data = (((unsigned short) speed << 13) | |
sahilmgandhi | 18:6a4db94011d3 | 888 | ((unsigned short) duplex << 8)); |
sahilmgandhi | 18:6a4db94011d3 | 889 | } |
sahilmgandhi | 18:6a4db94011d3 | 890 | |
sahilmgandhi | 18:6a4db94011d3 | 891 | phy_write(PHY_REG_BMCR, phy_data); |
sahilmgandhi | 18:6a4db94011d3 | 892 | |
sahilmgandhi | 18:6a4db94011d3 | 893 | for(tout = 100; tout; tout--) { __NOP(); } /* A short delay */ |
sahilmgandhi | 18:6a4db94011d3 | 894 | |
sahilmgandhi | 18:6a4db94011d3 | 895 | switch(phy_id) { |
sahilmgandhi | 18:6a4db94011d3 | 896 | case DP83848C_ID: |
sahilmgandhi | 18:6a4db94011d3 | 897 | phy_data = phy_read(PHY_REG_STS); |
sahilmgandhi | 18:6a4db94011d3 | 898 | |
sahilmgandhi | 18:6a4db94011d3 | 899 | if(phy_data & PHY_STS_DUPLEX) { |
sahilmgandhi | 18:6a4db94011d3 | 900 | LPC_EMAC->MAC2 |= MAC2_FULL_DUP; |
sahilmgandhi | 18:6a4db94011d3 | 901 | LPC_EMAC->Command |= CR_FULL_DUP; |
sahilmgandhi | 18:6a4db94011d3 | 902 | LPC_EMAC->IPGT = IPGT_FULL_DUP; |
sahilmgandhi | 18:6a4db94011d3 | 903 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 904 | LPC_EMAC->MAC2 &= ~MAC2_FULL_DUP; |
sahilmgandhi | 18:6a4db94011d3 | 905 | LPC_EMAC->Command &= ~CR_FULL_DUP; |
sahilmgandhi | 18:6a4db94011d3 | 906 | LPC_EMAC->IPGT = IPGT_HALF_DUP; |
sahilmgandhi | 18:6a4db94011d3 | 907 | } |
sahilmgandhi | 18:6a4db94011d3 | 908 | |
sahilmgandhi | 18:6a4db94011d3 | 909 | if(phy_data & PHY_STS_SPEED) { |
sahilmgandhi | 18:6a4db94011d3 | 910 | LPC_EMAC->SUPP &= ~SUPP_SPEED; |
sahilmgandhi | 18:6a4db94011d3 | 911 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 912 | LPC_EMAC->SUPP |= SUPP_SPEED; |
sahilmgandhi | 18:6a4db94011d3 | 913 | } |
sahilmgandhi | 18:6a4db94011d3 | 914 | break; |
sahilmgandhi | 18:6a4db94011d3 | 915 | |
sahilmgandhi | 18:6a4db94011d3 | 916 | case LAN8720_ID: |
sahilmgandhi | 18:6a4db94011d3 | 917 | phy_data = phy_read(PHY_REG_SCSR); |
sahilmgandhi | 18:6a4db94011d3 | 918 | |
sahilmgandhi | 18:6a4db94011d3 | 919 | if (phy_data & PHY_SCSR_DUPLEX) { |
sahilmgandhi | 18:6a4db94011d3 | 920 | LPC_EMAC->MAC2 |= MAC2_FULL_DUP; |
sahilmgandhi | 18:6a4db94011d3 | 921 | LPC_EMAC->Command |= CR_FULL_DUP; |
sahilmgandhi | 18:6a4db94011d3 | 922 | LPC_EMAC->IPGT = IPGT_FULL_DUP; |
sahilmgandhi | 18:6a4db94011d3 | 923 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 924 | LPC_EMAC->Command &= ~CR_FULL_DUP; |
sahilmgandhi | 18:6a4db94011d3 | 925 | LPC_EMAC->IPGT = IPGT_HALF_DUP; |
sahilmgandhi | 18:6a4db94011d3 | 926 | } |
sahilmgandhi | 18:6a4db94011d3 | 927 | |
sahilmgandhi | 18:6a4db94011d3 | 928 | if(phy_data & PHY_SCSR_100MBIT) { |
sahilmgandhi | 18:6a4db94011d3 | 929 | LPC_EMAC->SUPP |= SUPP_SPEED; |
sahilmgandhi | 18:6a4db94011d3 | 930 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 931 | LPC_EMAC->SUPP &= ~SUPP_SPEED; |
sahilmgandhi | 18:6a4db94011d3 | 932 | } |
sahilmgandhi | 18:6a4db94011d3 | 933 | break; |
sahilmgandhi | 18:6a4db94011d3 | 934 | } |
sahilmgandhi | 18:6a4db94011d3 | 935 | } |