1 /******************************************************************
3 ***** Name: cs8900.c *****
5 ***** Date: 07/05/2001 *****
6 ***** Auth: Andreas Dannenberg *****
7 ***** HTWK Leipzig *****
8 ***** university of applied sciences *****
10 ***** Func: ethernet packet-driver for use with LAN- *****
11 ***** controller CS8900 from Crystal/Cirrus Logic *****
13 ***** Keil: Module modified for use with Philips *****
14 ***** LPC2378 EMAC Ethernet controller *****
16 ******************************************************************/
18 /* Adapted from file originally written by Andreas Dannenberg. Supplied with permission. */
25 /* The semaphore used to wake the uIP task when data arives. */
26 SemaphoreHandle_t xEMACSemaphore = NULL;
28 static unsigned short *rptr;
29 static unsigned short *tptr;
31 // easyWEB internal function
32 // help function to swap the byte order of a WORD
34 static unsigned short SwapBytes(unsigned short Data)
36 return (Data >> 8) | (Data << 8);
39 // Keil: function added to write PHY
40 void write_PHY (int PhyReg, int Value)
44 MADR = DP83848C_DEF_ADR | PhyReg;
47 /* Wait utill operation completed */
49 for (tout = 0; tout < MII_WR_TOUT; tout++) {
50 if ((MIND & MIND_BUSY) == 0) {
57 // Keil: function added to read PHY
58 unsigned short read_PHY (unsigned char PhyReg)
62 MADR = DP83848C_DEF_ADR | PhyReg;
65 /* Wait until operation completed */
67 for (tout = 0; tout < MII_RD_TOUT; tout++) {
68 if ((MIND & MIND_BUSY) == 0) {
77 // Keil: function added to initialize Rx Descriptors
78 void rx_descr_init (void)
82 for (i = 0; i < NUM_RX_FRAG; i++) {
83 RX_DESC_PACKET(i) = RX_BUF(i);
84 RX_DESC_CTRL(i) = RCTRL_INT | (ETH_FRAG_SIZE-1);
86 RX_STAT_HASHCRC(i) = 0;
89 /* Set EMAC Receive Descriptor Registers. */
90 RxDescriptor = RX_DESC_BASE;
91 RxStatus = RX_STAT_BASE;
92 RxDescriptorNumber = NUM_RX_FRAG-1;
94 /* Rx Descriptors Point to 0 */
99 // Keil: function added to initialize Tx Descriptors
100 void tx_descr_init (void) {
103 for (i = 0; i < NUM_TX_FRAG; i++) {
104 TX_DESC_PACKET(i) = TX_BUF(i);
109 /* Set EMAC Transmit Descriptor Registers. */
110 TxDescriptor = TX_DESC_BASE;
111 TxStatus = TX_STAT_BASE;
112 TxDescriptorNumber = NUM_TX_FRAG-1;
114 /* Tx Descriptors Point to 0 */
119 // configure port-pins for use with LAN-controller,
120 // reset it and send the configuration-sequence
122 portBASE_TYPE Init_EMAC(void)
124 portBASE_TYPE xReturn = pdPASS;
125 static portBASE_TYPE xAttempt = 0;
126 // Keil: function modified to access the EMAC
127 // Initializes the EMAC ethernet controller
128 volatile unsigned int regv,tout,id1,id2;
130 /* Enable P1 Ethernet Pins. */
131 PINSEL2 = configPINSEL2_VALUE;
132 PINSEL3 = (PINSEL3 & ~0x0000000F) | 0x00000005;
134 /* Power Up the EMAC controller. */
138 /* Reset all EMAC internal modules. */
139 MAC1 = MAC1_RES_TX | MAC1_RES_MCS_TX | MAC1_RES_RX | MAC1_RES_MCS_RX |
140 MAC1_SIM_RES | MAC1_SOFT_RES;
141 Command = CR_REG_RES | CR_TX_RES | CR_RX_RES;
143 /* A short delay after reset. */
146 /* Initialize MAC control registers. */
147 MAC1 = MAC1_PASS_ALL;
148 MAC2 = MAC2_CRC_EN | MAC2_PAD_EN;
153 /* Enable Reduced MII interface. */
154 Command = CR_RMII | CR_PASS_RUNT_FRM;
156 /* Reset Reduced MII Logic. */
157 SUPP = SUPP_RES_RMII;
160 /* Put the DP83848C in reset mode */
161 write_PHY (PHY_REG_BMCR, 0x8000);
162 write_PHY (PHY_REG_BMCR, 0x8000);
164 /* Wait for hardware reset to end. */
165 for (tout = 0; tout < 100; tout++) {
167 regv = read_PHY (PHY_REG_BMCR);
168 if (!(regv & 0x8000)) {
174 /* Check if this is a DP83848C PHY. */
175 id1 = read_PHY (PHY_REG_IDR1);
176 id2 = read_PHY (PHY_REG_IDR2);
177 if (((id1 << 16) | (id2 & 0xFFF0)) == DP83848C_ID) {
178 /* Configure the PHY device */
180 /* Use autonegotiation about the link speed. */
181 write_PHY (PHY_REG_BMCR, PHY_AUTO_NEG);
182 /* Wait to complete Auto_Negotiation. */
183 for (tout = 0; tout < 10; tout++) {
185 regv = read_PHY (PHY_REG_BMSR);
187 /* Autonegotiation Complete. */
197 /* Check the link status. */
198 if( xReturn == pdPASS )
201 for (tout = 0; tout < 10; tout++) {
203 regv = read_PHY (PHY_REG_STS);
212 if( xReturn == pdPASS )
214 /* Configure Full/Half Duplex mode. */
216 /* Full duplex is enabled. */
217 MAC2 |= MAC2_FULL_DUP;
218 Command |= CR_FULL_DUP;
219 IPGT = IPGT_FULL_DUP;
222 /* Half duplex mode. */
223 IPGT = IPGT_HALF_DUP;
226 /* Configure 100MBit/10MBit mode. */
236 /* Set the Ethernet MAC Address registers */
237 SA0 = (emacETHADDR0 << 8) | emacETHADDR1;
238 SA1 = (emacETHADDR2 << 8) | emacETHADDR3;
239 SA2 = (emacETHADDR4 << 8) | emacETHADDR5;
241 /* Initialize Tx and Rx DMA Descriptors */
245 /* Receive Broadcast and Perfect Match Packets */
246 RxFilterCtrl = RFC_UCAST_EN | RFC_BCAST_EN | RFC_PERFECT_EN;
248 /* Create the semaphore used ot wake the uIP task. */
249 vSemaphoreCreateBinary( xEMACSemaphore );
251 /* Reset all interrupts */
254 /* Enable receive and transmit mode of MAC Ethernet core */
255 Command |= (CR_RX_EN | CR_TX_EN);
263 // reads a word in little-endian byte order from RX_BUFFER
265 unsigned short ReadFrame_EMAC(void)
270 // reads a word in big-endian byte order from RX_FRAME_PORT
271 // (useful to avoid permanent byte-swapping while reading
274 unsigned short ReadFrameBE_EMAC(void)
276 unsigned short ReturnValue;
278 ReturnValue = SwapBytes (*rptr++);
279 return (ReturnValue);
283 // copies bytes from frame port to MCU-memory
284 // NOTES: * an odd number of byte may only be transfered
285 // if the frame is read to the end!
286 // * MCU-memory MUST start at word-boundary
288 void CopyFromFrame_EMAC(void *Dest, unsigned short Size)
290 unsigned short * piDest; // Keil: Pointer added to correct expression
292 piDest = Dest; // Keil: Line added
294 *piDest++ = ReadFrame_EMAC();
298 if (Size) { // check for leftover byte...
299 *(unsigned char *)piDest = (char)ReadFrame_EMAC();// the LAN-Controller will return 0
300 } // for the highbyte
303 // does a dummy read on frame-I/O-port
304 // NOTE: only an even number of bytes is read!
306 void DummyReadFrame_EMAC(unsigned short Size) // discards an EVEN number of bytes
314 // Reads the length of the received ethernet frame and checks if the
315 // destination address is a broadcast message or not
316 // returns the frame length
317 unsigned short StartReadFrame(void) {
318 unsigned short RxLen;
321 idx = RxConsumeIndex;
322 RxLen = (RX_STAT_INFO(idx) & RINFO_SIZE) - 3;
323 rptr = (unsigned short *)RX_DESC_PACKET(idx);
327 void EndReadFrame(void) {
330 /* DMA free packet. */
331 idx = RxConsumeIndex;
333 if (++idx == NUM_RX_FRAG)
336 RxConsumeIndex = idx;
339 unsigned int CheckFrameReceived(void) { // Packet received ?
341 if (RxProduceIndex != RxConsumeIndex) // more packets received ?
347 unsigned int uiGetEMACRxData( unsigned char *ucBuffer )
349 unsigned int uiLen = 0;
351 if( RxProduceIndex != RxConsumeIndex )
353 uiLen = StartReadFrame();
354 CopyFromFrame_EMAC( ucBuffer, uiLen );
361 // requests space in EMAC memory for storing an outgoing frame
363 void RequestSend(void)
367 idx = TxProduceIndex;
368 tptr = (unsigned short *)TX_DESC_PACKET(idx);
371 // check if ethernet controller is ready to accept the
372 // frame we want to send
374 unsigned int Rdy4Tx(void)
376 return (1); // the ethernet controller transmits much faster
377 } // than the CPU can load its buffers
380 // writes a word in little-endian byte order to TX_BUFFER
381 void WriteFrame_EMAC(unsigned short Data)
386 // copies bytes from MCU-memory to frame port
387 // NOTES: * an odd number of byte may only be transfered
388 // if the frame is written to the end!
389 // * MCU-memory MUST start at word-boundary
391 void CopyToFrame_EMAC(void *Source, unsigned int Size)
393 unsigned short * piSource;
396 Size = (Size + 1) & 0xFFFE; // round Size up to next even number
398 WriteFrame_EMAC(*piSource++);
403 void DoSend_EMAC(unsigned short FrameSize)
407 idx = TxProduceIndex;
408 TX_DESC_CTRL(idx) = FrameSize | TCTRL_LAST;
409 if (++idx == NUM_TX_FRAG) idx = 0;
410 TxProduceIndex = idx;