These are the examples provided for [[/users/frank26080115/libraries/LPC1700CMSIS_Lib/]] Note, the entire "program" is not compilable!

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EMAC.c Source File

EMAC.c

00001 /******************************************************************
00002  *****                                                        *****
00003  *****  Name: cs8900.c                                        *****
00004  *****  Ver.: 1.0                                             *****
00005  *****  Date: 07/05/2001                                      *****
00006  *****  Auth: Andreas Dannenberg                              *****
00007  *****        HTWK Leipzig                                    *****
00008  *****        university of applied sciences                  *****
00009  *****        Germany                                         *****
00010  *****  Func: ethernet packet-driver for use with LAN-        *****
00011  *****        controller CS8900 from Crystal/Cirrus Logic     *****
00012  *****                                                        *****
00013  *****  NXP: Module modified for use with NXP                 *****
00014  *****        LPC1768 EMAC Ethernet controller                *****
00015  *****                                                        *****
00016  ******************************************************************/
00017 
00018 #include "EMAC.h"
00019 #include "tcpip.h"
00020 #include "lpc17xx_emac.h"
00021 #include "lpc17xx_pinsel.h"
00022 
00023 /* For debugging... */
00024 #include "debug_frmwrk.h"
00025 #include <stdio.h>
00026 #define DB  _DBG((uint8_t *)db)
00027 char db[64];
00028 
00029 static unsigned short *rptr;
00030 static unsigned short *tptr;
00031 
00032 /*
00033  * NXP: Here AHBRAM1 section still not be used, so a mount of this section
00034  * will be used to store buffer data get from receive packet buffer of EMAC
00035  */
00036 static unsigned short *pgBuf = (unsigned short *)LPC_AHBRAM1_BASE;
00037 
00038 // configure port-pins for use with LAN-controller,
00039 // reset it and send the configuration-sequence
00040 void Init_EMAC(void)
00041 {
00042     uint32_t delay;
00043 
00044     /* EMAC configuration type */
00045     EMAC_CFG_Type Emac_Config;
00046     /* pin configuration */
00047     PINSEL_CFG_Type PinCfg;
00048 
00049     /* EMAC address */
00050     uint8_t EMACAddr[] = {MYMAC_1, MYMAC_2, MYMAC_3, MYMAC_4, MYMAC_5, MYMAC_6};
00051 
00052     /*
00053      * Enable P1 Ethernet Pins:
00054      * P1.0 - ENET_TXD0
00055      * P1.1 - ENET_TXD1
00056      * P1.4 - ENET_TX_EN
00057      * P1.8 - ENET_CRS
00058      * P1.9 - ENET_RXD0
00059      * P1.10 - ENET_RXD1
00060      * P1.14 - ENET_RX_ER
00061      * P1.15 - ENET_REF_CLK
00062      * P1.16 - ENET_MDC
00063      * P1.17 - ENET_MDIO
00064      */
00065     PinCfg.Funcnum = 1;
00066     PinCfg.OpenDrain = 0;
00067     PinCfg.Pinmode = 0;
00068     PinCfg.Portnum = 1;
00069 
00070     PinCfg.Pinnum = 0;
00071     PINSEL_ConfigPin(&PinCfg);
00072     PinCfg.Pinnum = 1;
00073     PINSEL_ConfigPin(&PinCfg);
00074     PinCfg.Pinnum = 4;
00075     PINSEL_ConfigPin(&PinCfg);
00076     PinCfg.Pinnum = 8;
00077     PINSEL_ConfigPin(&PinCfg);
00078     PinCfg.Pinnum = 9;
00079     PINSEL_ConfigPin(&PinCfg);
00080     PinCfg.Pinnum = 10;
00081     PINSEL_ConfigPin(&PinCfg);
00082     PinCfg.Pinnum = 14;
00083     PINSEL_ConfigPin(&PinCfg);
00084     PinCfg.Pinnum = 15;
00085     PINSEL_ConfigPin(&PinCfg);
00086     PinCfg.Pinnum = 16;
00087     PINSEL_ConfigPin(&PinCfg);
00088     PinCfg.Pinnum = 17;
00089     PINSEL_ConfigPin(&PinCfg);
00090 
00091     _DBG_("Init EMAC module");
00092     sprintf(db,"MAC addr: %X-%X-%X-%X-%X-%X \n\r", \
00093              EMACAddr[0],  EMACAddr[1],  EMACAddr[2], \
00094               EMACAddr[3],  EMACAddr[4],  EMACAddr[5]);
00095     DB;
00096 
00097     Emac_Config.Mode = EMAC_MODE_AUTO;
00098     Emac_Config.pbEMAC_Addr = EMACAddr;
00099     // Initialize EMAC module with given parameter
00100     while (EMAC_Init(&Emac_Config) == ERROR){
00101         // Delay for a while then continue initializing EMAC module
00102         _DBG_("Error during initializing EMAC, restart after a while");
00103         for (delay = 0x100000; delay; delay--);
00104     }
00105     _DBG_("Init EMAC complete");
00106 }
00107 
00108 
00109 // reads a word in little-endian byte order from RX_BUFFER
00110 
00111 unsigned short ReadFrame_EMAC(void)
00112 {
00113   return (*rptr++);
00114 }
00115 
00116 // reads a word in big-endian byte order from RX_FRAME_PORT
00117 // (useful to avoid permanent byte-swapping while reading
00118 // TCP/IP-data)
00119 
00120 unsigned short ReadFrameBE_EMAC(void)
00121 {
00122   unsigned short ReturnValue;
00123 
00124   ReturnValue = SwapBytes (*rptr++);
00125   return (ReturnValue);
00126 }
00127 
00128 
00129 // copies bytes from frame port to MCU-memory
00130 // NOTES: * an odd number of byte may only be transfered
00131 //          if the frame is read to the end!
00132 //        * MCU-memory MUST start at word-boundary
00133 
00134 void CopyFromFrame_EMAC(void *Dest, unsigned short Size)
00135 {
00136   unsigned short * piDest;                       // Keil: Pointer added to correct expression
00137 
00138   piDest = Dest;                                 // Keil: Line added
00139   while (Size > 1) {
00140     *piDest++ = ReadFrame_EMAC();
00141     Size -= 2;
00142   }
00143 
00144   if (Size) {                                         // check for leftover byte...
00145     *(unsigned char *)piDest = (char)ReadFrame_EMAC();// the LAN-Controller will return 0
00146   }                                                   // for the highbyte
00147 }
00148 
00149 // does a dummy read on frame-I/O-port
00150 // NOTE: only an even number of bytes is read!
00151 
00152 void DummyReadFrame_EMAC(unsigned short Size)    // discards an EVEN number of bytes
00153 {                                                // from RX-fifo
00154   while (Size > 1) {
00155     ReadFrame_EMAC();
00156     Size -= 2;
00157   }
00158 }
00159 
00160 // Reads the length of the received ethernet frame and checks if the
00161 // destination address is a broadcast message or not
00162 // returns the frame length
00163 unsigned short StartReadFrame(void) {
00164     unsigned short RxLen;
00165     EMAC_PACKETBUF_Type RxPack;
00166 
00167     RxLen = EMAC_GetReceiveDataSize() - 3;
00168     // Copy packet to data buffer
00169     RxPack.pbDataBuf = (uint32_t *)pgBuf;
00170     RxPack.ulDataLen = RxLen;
00171     EMAC_ReadPacketBuffer(&RxPack);
00172     // Point to the data buffer
00173     rptr = (unsigned short *)pgBuf;
00174     return(RxLen);
00175 }
00176 
00177 // Release the buffer after reading all the content inside
00178 void EndReadFrame(void) {
00179     // just call EMAC_UpdateConsumeIndex() in EMAC driver
00180     EMAC_UpdateRxConsumeIndex();
00181 }
00182 
00183 // Check whether if there is a receive packet coming
00184 unsigned int CheckFrameReceived(void) {             // Packet received ?
00185     // Just call EMAC_CheckReceiveIndex() in EMAC driver
00186     if (EMAC_CheckReceiveIndex() == TRUE){
00187         return (1);
00188     } else {
00189         return (0);
00190     }
00191 }
00192 
00193 // requests space in EMAC memory for storing an outgoing frame
00194 void RequestSend(unsigned short FrameSize)
00195 {
00196     // Nothing to do here, just implemented in CopyToFrame_EMAC()
00197 }
00198 
00199 // check if ethernet controller is ready to accept the
00200 // frame we want to send
00201 
00202 unsigned int Rdy4Tx(void)
00203 {
00204   return (1);   // the ethernet controller transmits much faster
00205 }               // than the CPU can load its buffers
00206 
00207 
00208 // writes a word in little-endian byte order to TX_BUFFER
00209 void WriteFrame_EMAC(unsigned short Data)
00210 {
00211   *tptr++ = Data;
00212 }
00213 
00214 // copies bytes from MCU-memory to frame port
00215 // NOTES: * an odd number of byte may only be transfered
00216 //          if the frame is written to the end!
00217 //        * MCU-memory MUST start at word-boundary
00218 
00219 void CopyToFrame_EMAC(void *Source, unsigned int Size)
00220 {
00221     EMAC_PACKETBUF_Type TxPack;
00222 
00223     // Setup Tx Packet buffer
00224     // NXP: Added for compatibility with old style
00225     TxPack.ulDataLen = Size;
00226     TxPack.pbDataBuf = (uint32_t *)Source;
00227     EMAC_WritePacketBuffer(&TxPack);
00228     EMAC_UpdateTxProduceIndex();
00229 }
00230