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

Committer:
frank26080115
Date:
Sun Mar 20 05:38:56 2011 +0000
Revision:
0:bf7b9fba3924

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
frank26080115 0:bf7b9fba3924 1 /******************************************************************
frank26080115 0:bf7b9fba3924 2 ***** *****
frank26080115 0:bf7b9fba3924 3 ***** Name: cs8900.c *****
frank26080115 0:bf7b9fba3924 4 ***** Ver.: 1.0 *****
frank26080115 0:bf7b9fba3924 5 ***** Date: 07/05/2001 *****
frank26080115 0:bf7b9fba3924 6 ***** Auth: Andreas Dannenberg *****
frank26080115 0:bf7b9fba3924 7 ***** HTWK Leipzig *****
frank26080115 0:bf7b9fba3924 8 ***** university of applied sciences *****
frank26080115 0:bf7b9fba3924 9 ***** Germany *****
frank26080115 0:bf7b9fba3924 10 ***** Func: ethernet packet-driver for use with LAN- *****
frank26080115 0:bf7b9fba3924 11 ***** controller CS8900 from Crystal/Cirrus Logic *****
frank26080115 0:bf7b9fba3924 12 ***** *****
frank26080115 0:bf7b9fba3924 13 ***** NXP: Module modified for use with NXP *****
frank26080115 0:bf7b9fba3924 14 ***** LPC1768 EMAC Ethernet controller *****
frank26080115 0:bf7b9fba3924 15 ***** *****
frank26080115 0:bf7b9fba3924 16 ******************************************************************/
frank26080115 0:bf7b9fba3924 17
frank26080115 0:bf7b9fba3924 18 #include "EMAC.h"
frank26080115 0:bf7b9fba3924 19 #include "tcpip.h"
frank26080115 0:bf7b9fba3924 20 #include "lpc17xx_emac.h"
frank26080115 0:bf7b9fba3924 21 #include "lpc17xx_pinsel.h"
frank26080115 0:bf7b9fba3924 22
frank26080115 0:bf7b9fba3924 23 /* For debugging... */
frank26080115 0:bf7b9fba3924 24 #include "debug_frmwrk.h"
frank26080115 0:bf7b9fba3924 25 #include <stdio.h>
frank26080115 0:bf7b9fba3924 26 #define DB _DBG((uint8_t *)db)
frank26080115 0:bf7b9fba3924 27 char db[64];
frank26080115 0:bf7b9fba3924 28
frank26080115 0:bf7b9fba3924 29 static unsigned short *rptr;
frank26080115 0:bf7b9fba3924 30 static unsigned short *tptr;
frank26080115 0:bf7b9fba3924 31
frank26080115 0:bf7b9fba3924 32 /*
frank26080115 0:bf7b9fba3924 33 * NXP: Here AHBRAM1 section still not be used, so a mount of this section
frank26080115 0:bf7b9fba3924 34 * will be used to store buffer data get from receive packet buffer of EMAC
frank26080115 0:bf7b9fba3924 35 */
frank26080115 0:bf7b9fba3924 36 static unsigned short *pgBuf = (unsigned short *)LPC_AHBRAM1_BASE;
frank26080115 0:bf7b9fba3924 37
frank26080115 0:bf7b9fba3924 38 // configure port-pins for use with LAN-controller,
frank26080115 0:bf7b9fba3924 39 // reset it and send the configuration-sequence
frank26080115 0:bf7b9fba3924 40 void Init_EMAC(void)
frank26080115 0:bf7b9fba3924 41 {
frank26080115 0:bf7b9fba3924 42 uint32_t delay;
frank26080115 0:bf7b9fba3924 43
frank26080115 0:bf7b9fba3924 44 /* EMAC configuration type */
frank26080115 0:bf7b9fba3924 45 EMAC_CFG_Type Emac_Config;
frank26080115 0:bf7b9fba3924 46 /* pin configuration */
frank26080115 0:bf7b9fba3924 47 PINSEL_CFG_Type PinCfg;
frank26080115 0:bf7b9fba3924 48
frank26080115 0:bf7b9fba3924 49 /* EMAC address */
frank26080115 0:bf7b9fba3924 50 uint8_t EMACAddr[] = {MYMAC_1, MYMAC_2, MYMAC_3, MYMAC_4, MYMAC_5, MYMAC_6};
frank26080115 0:bf7b9fba3924 51
frank26080115 0:bf7b9fba3924 52 /*
frank26080115 0:bf7b9fba3924 53 * Enable P1 Ethernet Pins:
frank26080115 0:bf7b9fba3924 54 * P1.0 - ENET_TXD0
frank26080115 0:bf7b9fba3924 55 * P1.1 - ENET_TXD1
frank26080115 0:bf7b9fba3924 56 * P1.4 - ENET_TX_EN
frank26080115 0:bf7b9fba3924 57 * P1.8 - ENET_CRS
frank26080115 0:bf7b9fba3924 58 * P1.9 - ENET_RXD0
frank26080115 0:bf7b9fba3924 59 * P1.10 - ENET_RXD1
frank26080115 0:bf7b9fba3924 60 * P1.14 - ENET_RX_ER
frank26080115 0:bf7b9fba3924 61 * P1.15 - ENET_REF_CLK
frank26080115 0:bf7b9fba3924 62 * P1.16 - ENET_MDC
frank26080115 0:bf7b9fba3924 63 * P1.17 - ENET_MDIO
frank26080115 0:bf7b9fba3924 64 */
frank26080115 0:bf7b9fba3924 65 PinCfg.Funcnum = 1;
frank26080115 0:bf7b9fba3924 66 PinCfg.OpenDrain = 0;
frank26080115 0:bf7b9fba3924 67 PinCfg.Pinmode = 0;
frank26080115 0:bf7b9fba3924 68 PinCfg.Portnum = 1;
frank26080115 0:bf7b9fba3924 69
frank26080115 0:bf7b9fba3924 70 PinCfg.Pinnum = 0;
frank26080115 0:bf7b9fba3924 71 PINSEL_ConfigPin(&PinCfg);
frank26080115 0:bf7b9fba3924 72 PinCfg.Pinnum = 1;
frank26080115 0:bf7b9fba3924 73 PINSEL_ConfigPin(&PinCfg);
frank26080115 0:bf7b9fba3924 74 PinCfg.Pinnum = 4;
frank26080115 0:bf7b9fba3924 75 PINSEL_ConfigPin(&PinCfg);
frank26080115 0:bf7b9fba3924 76 PinCfg.Pinnum = 8;
frank26080115 0:bf7b9fba3924 77 PINSEL_ConfigPin(&PinCfg);
frank26080115 0:bf7b9fba3924 78 PinCfg.Pinnum = 9;
frank26080115 0:bf7b9fba3924 79 PINSEL_ConfigPin(&PinCfg);
frank26080115 0:bf7b9fba3924 80 PinCfg.Pinnum = 10;
frank26080115 0:bf7b9fba3924 81 PINSEL_ConfigPin(&PinCfg);
frank26080115 0:bf7b9fba3924 82 PinCfg.Pinnum = 14;
frank26080115 0:bf7b9fba3924 83 PINSEL_ConfigPin(&PinCfg);
frank26080115 0:bf7b9fba3924 84 PinCfg.Pinnum = 15;
frank26080115 0:bf7b9fba3924 85 PINSEL_ConfigPin(&PinCfg);
frank26080115 0:bf7b9fba3924 86 PinCfg.Pinnum = 16;
frank26080115 0:bf7b9fba3924 87 PINSEL_ConfigPin(&PinCfg);
frank26080115 0:bf7b9fba3924 88 PinCfg.Pinnum = 17;
frank26080115 0:bf7b9fba3924 89 PINSEL_ConfigPin(&PinCfg);
frank26080115 0:bf7b9fba3924 90
frank26080115 0:bf7b9fba3924 91 _DBG_("Init EMAC module");
frank26080115 0:bf7b9fba3924 92 sprintf(db,"MAC addr: %X-%X-%X-%X-%X-%X \n\r", \
frank26080115 0:bf7b9fba3924 93 EMACAddr[0], EMACAddr[1], EMACAddr[2], \
frank26080115 0:bf7b9fba3924 94 EMACAddr[3], EMACAddr[4], EMACAddr[5]);
frank26080115 0:bf7b9fba3924 95 DB;
frank26080115 0:bf7b9fba3924 96
frank26080115 0:bf7b9fba3924 97 Emac_Config.Mode = EMAC_MODE_AUTO;
frank26080115 0:bf7b9fba3924 98 Emac_Config.pbEMAC_Addr = EMACAddr;
frank26080115 0:bf7b9fba3924 99 // Initialize EMAC module with given parameter
frank26080115 0:bf7b9fba3924 100 while (EMAC_Init(&Emac_Config) == ERROR){
frank26080115 0:bf7b9fba3924 101 // Delay for a while then continue initializing EMAC module
frank26080115 0:bf7b9fba3924 102 _DBG_("Error during initializing EMAC, restart after a while");
frank26080115 0:bf7b9fba3924 103 for (delay = 0x100000; delay; delay--);
frank26080115 0:bf7b9fba3924 104 }
frank26080115 0:bf7b9fba3924 105 _DBG_("Init EMAC complete");
frank26080115 0:bf7b9fba3924 106 }
frank26080115 0:bf7b9fba3924 107
frank26080115 0:bf7b9fba3924 108
frank26080115 0:bf7b9fba3924 109 // reads a word in little-endian byte order from RX_BUFFER
frank26080115 0:bf7b9fba3924 110
frank26080115 0:bf7b9fba3924 111 unsigned short ReadFrame_EMAC(void)
frank26080115 0:bf7b9fba3924 112 {
frank26080115 0:bf7b9fba3924 113 return (*rptr++);
frank26080115 0:bf7b9fba3924 114 }
frank26080115 0:bf7b9fba3924 115
frank26080115 0:bf7b9fba3924 116 // reads a word in big-endian byte order from RX_FRAME_PORT
frank26080115 0:bf7b9fba3924 117 // (useful to avoid permanent byte-swapping while reading
frank26080115 0:bf7b9fba3924 118 // TCP/IP-data)
frank26080115 0:bf7b9fba3924 119
frank26080115 0:bf7b9fba3924 120 unsigned short ReadFrameBE_EMAC(void)
frank26080115 0:bf7b9fba3924 121 {
frank26080115 0:bf7b9fba3924 122 unsigned short ReturnValue;
frank26080115 0:bf7b9fba3924 123
frank26080115 0:bf7b9fba3924 124 ReturnValue = SwapBytes (*rptr++);
frank26080115 0:bf7b9fba3924 125 return (ReturnValue);
frank26080115 0:bf7b9fba3924 126 }
frank26080115 0:bf7b9fba3924 127
frank26080115 0:bf7b9fba3924 128
frank26080115 0:bf7b9fba3924 129 // copies bytes from frame port to MCU-memory
frank26080115 0:bf7b9fba3924 130 // NOTES: * an odd number of byte may only be transfered
frank26080115 0:bf7b9fba3924 131 // if the frame is read to the end!
frank26080115 0:bf7b9fba3924 132 // * MCU-memory MUST start at word-boundary
frank26080115 0:bf7b9fba3924 133
frank26080115 0:bf7b9fba3924 134 void CopyFromFrame_EMAC(void *Dest, unsigned short Size)
frank26080115 0:bf7b9fba3924 135 {
frank26080115 0:bf7b9fba3924 136 unsigned short * piDest; // Keil: Pointer added to correct expression
frank26080115 0:bf7b9fba3924 137
frank26080115 0:bf7b9fba3924 138 piDest = Dest; // Keil: Line added
frank26080115 0:bf7b9fba3924 139 while (Size > 1) {
frank26080115 0:bf7b9fba3924 140 *piDest++ = ReadFrame_EMAC();
frank26080115 0:bf7b9fba3924 141 Size -= 2;
frank26080115 0:bf7b9fba3924 142 }
frank26080115 0:bf7b9fba3924 143
frank26080115 0:bf7b9fba3924 144 if (Size) { // check for leftover byte...
frank26080115 0:bf7b9fba3924 145 *(unsigned char *)piDest = (char)ReadFrame_EMAC();// the LAN-Controller will return 0
frank26080115 0:bf7b9fba3924 146 } // for the highbyte
frank26080115 0:bf7b9fba3924 147 }
frank26080115 0:bf7b9fba3924 148
frank26080115 0:bf7b9fba3924 149 // does a dummy read on frame-I/O-port
frank26080115 0:bf7b9fba3924 150 // NOTE: only an even number of bytes is read!
frank26080115 0:bf7b9fba3924 151
frank26080115 0:bf7b9fba3924 152 void DummyReadFrame_EMAC(unsigned short Size) // discards an EVEN number of bytes
frank26080115 0:bf7b9fba3924 153 { // from RX-fifo
frank26080115 0:bf7b9fba3924 154 while (Size > 1) {
frank26080115 0:bf7b9fba3924 155 ReadFrame_EMAC();
frank26080115 0:bf7b9fba3924 156 Size -= 2;
frank26080115 0:bf7b9fba3924 157 }
frank26080115 0:bf7b9fba3924 158 }
frank26080115 0:bf7b9fba3924 159
frank26080115 0:bf7b9fba3924 160 // Reads the length of the received ethernet frame and checks if the
frank26080115 0:bf7b9fba3924 161 // destination address is a broadcast message or not
frank26080115 0:bf7b9fba3924 162 // returns the frame length
frank26080115 0:bf7b9fba3924 163 unsigned short StartReadFrame(void) {
frank26080115 0:bf7b9fba3924 164 unsigned short RxLen;
frank26080115 0:bf7b9fba3924 165 EMAC_PACKETBUF_Type RxPack;
frank26080115 0:bf7b9fba3924 166
frank26080115 0:bf7b9fba3924 167 RxLen = EMAC_GetReceiveDataSize() - 3;
frank26080115 0:bf7b9fba3924 168 // Copy packet to data buffer
frank26080115 0:bf7b9fba3924 169 RxPack.pbDataBuf = (uint32_t *)pgBuf;
frank26080115 0:bf7b9fba3924 170 RxPack.ulDataLen = RxLen;
frank26080115 0:bf7b9fba3924 171 EMAC_ReadPacketBuffer(&RxPack);
frank26080115 0:bf7b9fba3924 172 // Point to the data buffer
frank26080115 0:bf7b9fba3924 173 rptr = (unsigned short *)pgBuf;
frank26080115 0:bf7b9fba3924 174 return(RxLen);
frank26080115 0:bf7b9fba3924 175 }
frank26080115 0:bf7b9fba3924 176
frank26080115 0:bf7b9fba3924 177 // Release the buffer after reading all the content inside
frank26080115 0:bf7b9fba3924 178 void EndReadFrame(void) {
frank26080115 0:bf7b9fba3924 179 // just call EMAC_UpdateConsumeIndex() in EMAC driver
frank26080115 0:bf7b9fba3924 180 EMAC_UpdateRxConsumeIndex();
frank26080115 0:bf7b9fba3924 181 }
frank26080115 0:bf7b9fba3924 182
frank26080115 0:bf7b9fba3924 183 // Check whether if there is a receive packet coming
frank26080115 0:bf7b9fba3924 184 unsigned int CheckFrameReceived(void) { // Packet received ?
frank26080115 0:bf7b9fba3924 185 // Just call EMAC_CheckReceiveIndex() in EMAC driver
frank26080115 0:bf7b9fba3924 186 if (EMAC_CheckReceiveIndex() == TRUE){
frank26080115 0:bf7b9fba3924 187 return (1);
frank26080115 0:bf7b9fba3924 188 } else {
frank26080115 0:bf7b9fba3924 189 return (0);
frank26080115 0:bf7b9fba3924 190 }
frank26080115 0:bf7b9fba3924 191 }
frank26080115 0:bf7b9fba3924 192
frank26080115 0:bf7b9fba3924 193 // requests space in EMAC memory for storing an outgoing frame
frank26080115 0:bf7b9fba3924 194 void RequestSend(unsigned short FrameSize)
frank26080115 0:bf7b9fba3924 195 {
frank26080115 0:bf7b9fba3924 196 // Nothing to do here, just implemented in CopyToFrame_EMAC()
frank26080115 0:bf7b9fba3924 197 }
frank26080115 0:bf7b9fba3924 198
frank26080115 0:bf7b9fba3924 199 // check if ethernet controller is ready to accept the
frank26080115 0:bf7b9fba3924 200 // frame we want to send
frank26080115 0:bf7b9fba3924 201
frank26080115 0:bf7b9fba3924 202 unsigned int Rdy4Tx(void)
frank26080115 0:bf7b9fba3924 203 {
frank26080115 0:bf7b9fba3924 204 return (1); // the ethernet controller transmits much faster
frank26080115 0:bf7b9fba3924 205 } // than the CPU can load its buffers
frank26080115 0:bf7b9fba3924 206
frank26080115 0:bf7b9fba3924 207
frank26080115 0:bf7b9fba3924 208 // writes a word in little-endian byte order to TX_BUFFER
frank26080115 0:bf7b9fba3924 209 void WriteFrame_EMAC(unsigned short Data)
frank26080115 0:bf7b9fba3924 210 {
frank26080115 0:bf7b9fba3924 211 *tptr++ = Data;
frank26080115 0:bf7b9fba3924 212 }
frank26080115 0:bf7b9fba3924 213
frank26080115 0:bf7b9fba3924 214 // copies bytes from MCU-memory to frame port
frank26080115 0:bf7b9fba3924 215 // NOTES: * an odd number of byte may only be transfered
frank26080115 0:bf7b9fba3924 216 // if the frame is written to the end!
frank26080115 0:bf7b9fba3924 217 // * MCU-memory MUST start at word-boundary
frank26080115 0:bf7b9fba3924 218
frank26080115 0:bf7b9fba3924 219 void CopyToFrame_EMAC(void *Source, unsigned int Size)
frank26080115 0:bf7b9fba3924 220 {
frank26080115 0:bf7b9fba3924 221 EMAC_PACKETBUF_Type TxPack;
frank26080115 0:bf7b9fba3924 222
frank26080115 0:bf7b9fba3924 223 // Setup Tx Packet buffer
frank26080115 0:bf7b9fba3924 224 // NXP: Added for compatibility with old style
frank26080115 0:bf7b9fba3924 225 TxPack.ulDataLen = Size;
frank26080115 0:bf7b9fba3924 226 TxPack.pbDataBuf = (uint32_t *)Source;
frank26080115 0:bf7b9fba3924 227 EMAC_WritePacketBuffer(&TxPack);
frank26080115 0:bf7b9fba3924 228 EMAC_UpdateTxProduceIndex();
frank26080115 0:bf7b9fba3924 229 }
frank26080115 0:bf7b9fba3924 230