drv_eth.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. /*
  2. * File : drv_eth.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009-2013 RT-Thread Develop Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2014-07-25 ArdaFu Port to TM4C129X
  13. */
  14. /**
  15. * @file - tivaif.c
  16. * lwIP Ethernet interface for Stellaris LM4F Devices
  17. *
  18. */
  19. /**
  20. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  21. * All rights reserved.
  22. *
  23. * Redistribution and use in source and binary forms, with or without modification,
  24. * are permitted provided that the following conditions are met:
  25. *
  26. * 1. Redistributions of source code must retain the above copyright notice,
  27. * this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright notice,
  29. * this list of conditions and the following disclaimer in the documentation
  30. * and/or other materials provided with the distribution.
  31. * 3. The name of the author may not be used to endorse or promote products
  32. * derived from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  35. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  36. * MERCHANTABILITY AND FITNESS FOR A PARTICui32AR PURPOSE ARE DISCLAIMED. IN NO EVENT
  37. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  38. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  39. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  40. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  41. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  42. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  43. * OF SUCH DAMAGE.
  44. *
  45. * This file is part of the lwIP TCP/IP stack.
  46. *
  47. * Author: Adam Dunkels <adam@sics.se>
  48. *
  49. */
  50. /**
  51. * Copyright (c) 2008-2012 Texas Instruments Incorporated
  52. *
  53. * This file is derived from the ``ethernetif.c'' skeleton Ethernet network
  54. * interface driver for lwIP.
  55. *
  56. */
  57. #include "lwip/opt.h"
  58. #include "lwip/def.h"
  59. #include "lwip/mem.h"
  60. #include "lwip/pbuf.h"
  61. #include "lwip/sys.h"
  62. #include <lwip/stats.h>
  63. #include <lwip/snmp.h>
  64. #include "lwip/tcpip.h"
  65. #include "netif/etharp.h"
  66. #include "netif/ppp_oe.h"
  67. /**
  68. * Sanity Check: This interface driver will NOT work if the following defines
  69. * are incorrect.
  70. *
  71. */
  72. #if (PBUF_LINK_HLEN != 16)
  73. #error "PBUF_LINK_HLEN must be 16 for this interface driver!"
  74. #endif
  75. #if (ETH_PAD_SIZE != 0)
  76. #error "ETH_PAD_SIZE must be 0 for this interface driver!"
  77. #endif
  78. #if (!SYS_LIGHTWEIGHT_PROT)
  79. #error "SYS_LIGHTWEIGHT_PROT must be enabled for this interface driver!"
  80. #endif
  81. /**
  82. * Set the physical address of the PHY we will be using if this is not
  83. * specified in lwipopts.h. We assume 0 for the internal PHY.
  84. */
  85. #ifndef PHY_PHYS_ADDR
  86. #define PHY_PHYS_ADDR 0
  87. #endif
  88. #ifndef EMAC_PHY_CONFIG
  89. #define EMAC_PHY_CONFIG (EMAC_PHY_TYPE_INTERNAL | EMAC_PHY_INT_MDIX_EN | \
  90. EMAC_PHY_AN_100B_T_FULL_DUPLEX)
  91. #endif
  92. /**
  93. * If necessary, set the defaui32t number of transmit and receive DMA descriptors
  94. * used by the Ethernet MAC.
  95. *
  96. */
  97. #ifndef NUM_RX_DESCRIPTORS
  98. #define NUM_RX_DESCRIPTORS 4
  99. #endif
  100. #ifndef NUM_TX_DESCRIPTORS
  101. #define NUM_TX_DESCRIPTORS 8
  102. #endif
  103. /**
  104. * Setup processing for PTP (IEEE-1588).
  105. *
  106. */
  107. #if LWIP_PTPD
  108. extern uint32_t g_ui32SysClk;
  109. extern uint32_t g_ui32PTPTickRate;
  110. extern void lwIPHostGetTime(u32_t *time_s, u32_t *time_ns);
  111. #endif
  112. /**
  113. * Stellaris DriverLib Header Files required for this interface driver.
  114. *
  115. */
  116. #include <stdint.h>
  117. #include <stdbool.h>
  118. #include "inc/hw_emac.h"
  119. #include "inc/hw_ints.h"
  120. #include "inc/hw_memmap.h"
  121. #include "inc/hw_types.h"
  122. #include "driverlib/emac.h"
  123. #include "driverlib/interrupt.h"
  124. #include "driverlib/sysctl.h"
  125. #include "driverlib/flash.h"
  126. #include "driverlib/interrupt.h"
  127. #include "driverlib/pin_map.h"
  128. #include "driverlib/rom_map.h"
  129. #include "driverlib/gpio.h"
  130. #include <netif/ethernetif.h>
  131. #include "lwipopts.h"
  132. #include "drv_eth.h"
  133. #include <components.h>
  134. /**
  135. * A structure used to keep track of driver state and error counts.
  136. */
  137. typedef struct {
  138. uint32_t ui32TXCount;
  139. uint32_t ui32TXCopyCount;
  140. uint32_t ui32TXCopyFailCount;
  141. uint32_t ui32TXNoDescCount;
  142. uint32_t ui32TXBufQueuedCount;
  143. uint32_t ui32TXBufFreedCount;
  144. uint32_t ui32RXBufReadCount;
  145. uint32_t ui32RXPacketReadCount;
  146. uint32_t ui32RXPacketErrCount;
  147. uint32_t ui32RXPacketCBErrCount;
  148. uint32_t ui32RXNoBufCount;
  149. }
  150. tDriverStats;
  151. tDriverStats g_sDriverStats;
  152. #ifdef DEBUG
  153. /**
  154. * Note: This rather weird construction where we invoke the macro with the
  155. * name of the field minus its Hungarian prefix is a workaround for a problem
  156. * experienced with GCC which does not like concatenating tokens after an
  157. * operator, specifically '.' or '->', in a macro.
  158. */
  159. #define DRIVER_STATS_INC(x) do{ g_sDriverStats.ui32##x++; } while(0)
  160. #define DRIVER_STATS_DEC(x) do{ g_sDriverStats.ui32##x--; } while(0)
  161. #define DRIVER_STATS_ADD(x, inc) do{ g_sDriverStats.ui32##x += (inc); } while(0)
  162. #define DRIVER_STATS_SUB(x, dec) do{ g_sDriverStats.ui32##x -= (dec); } while(0)
  163. #else
  164. #define DRIVER_STATS_INC(x)
  165. #define DRIVER_STATS_DEC(x)
  166. #define DRIVER_STATS_ADD(x, inc)
  167. #define DRIVER_STATS_SUB(x, dec)
  168. #endif
  169. /**
  170. * Helper struct holding a DMA descriptor and the pbuf it currently refers
  171. * to.
  172. */
  173. typedef struct {
  174. tEMACDMADescriptor Desc;
  175. struct pbuf *pBuf;
  176. } tDescriptor;
  177. typedef struct {
  178. tDescriptor *pDescriptors;
  179. uint32_t ui32NumDescs;
  180. uint32_t ui32Read;
  181. uint32_t ui32Write;
  182. } tDescriptorList;
  183. /**
  184. * Helper struct to hold private data used to operate your ethernet interface.
  185. * Keeping the ethernet address of the MAC in this struct is not necessary
  186. * as it is already kept in the struct netif.
  187. * But this is only an example, anyway...
  188. */
  189. typedef struct {
  190. struct eth_addr *ethaddr;
  191. /* Add whatever per-interface state that is needed here. */
  192. tDescriptorList *pTxDescList;
  193. tDescriptorList *pRxDescList;
  194. } tStellarisIF;
  195. /**
  196. * Global variable for this interface's private data. Needed to allow
  197. * the interrupt handlers access to this information outside of the
  198. * context of the lwIP netif.
  199. *
  200. */
  201. tDescriptor g_pTxDescriptors[NUM_TX_DESCRIPTORS];
  202. tDescriptor g_pRxDescriptors[NUM_RX_DESCRIPTORS];
  203. tDescriptorList g_TxDescList = {
  204. g_pTxDescriptors, NUM_TX_DESCRIPTORS, 0, 0
  205. };
  206. tDescriptorList g_RxDescList = {
  207. g_pRxDescriptors, NUM_RX_DESCRIPTORS, 0, 0
  208. };
  209. static tStellarisIF g_StellarisIFData = {
  210. 0, &g_TxDescList, &g_RxDescList
  211. };
  212. /**
  213. * Interrupt counters (for debug purposes).
  214. */
  215. volatile uint32_t g_ui32NormalInts;
  216. volatile uint32_t g_ui32AbnormalInts;
  217. /**
  218. * A macro which determines whether a pointer is within the SRAM address
  219. * space and, hence, points to a buffer that the Ethernet MAC can directly
  220. * DMA from.
  221. */
  222. #define PTR_SAFE_FOR_EMAC_DMA(ptr) (((uint32_t)(ptr) >= 0x2000000) && \
  223. ((uint32_t)(ptr) < 0x20070000))
  224. typedef struct
  225. {
  226. /* inherit from ethernet device */
  227. struct eth_device parent;
  228. tStellarisIF* dma_if;
  229. /* for rx_thread async get pbuf */
  230. rt_mailbox_t rx_pbuf_mb;
  231. } net_device;
  232. typedef net_device* net_device_t;
  233. static char rx_pbuf_mb_pool[8*4];
  234. static struct rt_mailbox eth_rx_pbuf_mb;
  235. static net_device eth_dev_entry;
  236. static net_device_t eth_dev = &eth_dev_entry;
  237. /**
  238. * Initialize the transmit and receive DMA descriptor lists.
  239. */
  240. void
  241. InitDMADescriptors(void)
  242. {
  243. uint32_t ui32Loop;
  244. /* Transmit list - mark all descriptors as not owned by the hardware */
  245. for(ui32Loop = 0; ui32Loop < NUM_TX_DESCRIPTORS; ui32Loop++)
  246. {
  247. g_pTxDescriptors[ui32Loop].pBuf = (struct pbuf *)0;
  248. g_pTxDescriptors[ui32Loop].Desc.ui32Count = 0;
  249. g_pTxDescriptors[ui32Loop].Desc.pvBuffer1 = 0;
  250. g_pTxDescriptors[ui32Loop].Desc.DES3.pLink =
  251. ((ui32Loop == (NUM_TX_DESCRIPTORS - 1)) ?
  252. &g_pTxDescriptors[0].Desc : &g_pTxDescriptors[ui32Loop + 1].Desc);
  253. g_pTxDescriptors[ui32Loop].Desc.ui32CtrlStatus = DES0_TX_CTRL_INTERRUPT |
  254. DES0_TX_CTRL_CHAINED | DES0_TX_CTRL_IP_ALL_CKHSUMS;
  255. }
  256. g_TxDescList.ui32Read = 0;
  257. g_TxDescList.ui32Write = 0;
  258. /* Receive list - tag each descriptor with a pbuf and set all fields to
  259. * allow packets to be received.
  260. */
  261. for(ui32Loop = 0; ui32Loop < NUM_RX_DESCRIPTORS; ui32Loop++)
  262. {
  263. g_pRxDescriptors[ui32Loop].pBuf = pbuf_alloc(PBUF_RAW, PBUF_POOL_BUFSIZE,
  264. PBUF_POOL);
  265. g_pRxDescriptors[ui32Loop].Desc.ui32Count = DES1_RX_CTRL_CHAINED;
  266. if(g_pRxDescriptors[ui32Loop].pBuf)
  267. {
  268. /* Set the DMA to write directly into the pbuf payload. */
  269. g_pRxDescriptors[ui32Loop].Desc.pvBuffer1 =
  270. g_pRxDescriptors[ui32Loop].pBuf->payload;
  271. g_pRxDescriptors[ui32Loop].Desc.ui32Count |=
  272. (g_pRxDescriptors[ui32Loop].pBuf->len << DES1_RX_CTRL_BUFF1_SIZE_S);
  273. g_pRxDescriptors[ui32Loop].Desc.ui32CtrlStatus = DES0_RX_CTRL_OWN;
  274. }
  275. else
  276. {
  277. LWIP_DEBUGF(NETIF_DEBUG, ("tivaif_init: pbuf_alloc error\n"));
  278. /* No pbuf available so leave the buffer pointer empty. */
  279. g_pRxDescriptors[ui32Loop].Desc.pvBuffer1 = 0;
  280. g_pRxDescriptors[ui32Loop].Desc.ui32CtrlStatus = 0;
  281. }
  282. g_pRxDescriptors[ui32Loop].Desc.DES3.pLink =
  283. ((ui32Loop == (NUM_RX_DESCRIPTORS - 1)) ?
  284. &g_pRxDescriptors[0].Desc : &g_pRxDescriptors[ui32Loop + 1].Desc);
  285. }
  286. g_TxDescList.ui32Read = 0;
  287. g_TxDescList.ui32Write = 0;
  288. //
  289. // Set the descriptor pointers in the hardware.
  290. //
  291. EMACRxDMADescriptorListSet(EMAC0_BASE, &g_pRxDescriptors[0].Desc);
  292. EMACTxDMADescriptorListSet(EMAC0_BASE, &g_pTxDescriptors[0].Desc);
  293. }
  294. /**
  295. * In this function, the hardware should be initialized.
  296. * Called from tivaif_init().
  297. *
  298. * @param netif the already initialized lwip network interface structure
  299. * for this ethernetif
  300. */
  301. static void
  302. tivaif_hwinit(struct netif *psNetif)
  303. {
  304. uint16_t ui16Val;
  305. /* Initialize the DMA descriptors. */
  306. InitDMADescriptors();
  307. /* Clear any stray PHY interrupts that may be set. */
  308. ui16Val = EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_MISR1);
  309. ui16Val = EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_MISR2);
  310. /* Configure and enable the link status change interrupt in the PHY. */
  311. ui16Val = EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_SCR);
  312. ui16Val |= (EPHY_SCR_INTEN_EXT | EPHY_SCR_INTOE_EXT);
  313. EMACPHYWrite(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_SCR, ui16Val);
  314. EMACPHYWrite(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_MISR1, (EPHY_MISR1_LINKSTATEN |
  315. EPHY_MISR1_SPEEDEN | EPHY_MISR1_DUPLEXMEN | EPHY_MISR1_ANCEN));
  316. /* Read the PHY interrupt status to clear any stray events. */
  317. ui16Val = EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_MISR1);
  318. /**
  319. * Set MAC filtering options. We receive all broadcast and mui32ticast
  320. * packets along with those addressed specifically for us.
  321. */
  322. EMACFrameFilterSet(EMAC0_BASE, (EMAC_FRMFILTER_HASH_AND_PERFECT |
  323. EMAC_FRMFILTER_PASS_MULTICAST));
  324. #if LWIP_PTPD
  325. //
  326. // Enable timestamping on all received packets.
  327. //
  328. // We set the fine clock adjustment mode and configure the subsecond
  329. // increment to half the 25MHz PTPD clock. This will give us maximum control
  330. // over the clock rate adjustment and keep the arithmetic easy later. It
  331. // should be possible to synchronize with higher accuracy than this with
  332. // appropriate juggling of the subsecond increment count and the addend
  333. // register value, though.
  334. //
  335. EMACTimestampConfigSet(EMAC0_BASE, (EMAC_TS_ALL_RX_FRAMES |
  336. EMAC_TS_DIGITAL_ROLLOVER |
  337. EMAC_TS_PROCESS_IPV4_UDP | EMAC_TS_ALL |
  338. EMAC_TS_PTP_VERSION_1 | EMAC_TS_UPDATE_FINE),
  339. (1000000000 / (25000000 / 2)));
  340. EMACTimestampAddendSet(EMAC0_BASE, 0x80000000);
  341. EMACTimestampEnable(EMAC0_BASE);
  342. #endif
  343. /* Clear any pending MAC interrupts. */
  344. EMACIntClear(EMAC0_BASE, EMACIntStatus(EMAC0_BASE, false));
  345. /* Enable the Ethernet MAC transmitter and receiver. */
  346. EMACTxEnable(EMAC0_BASE);
  347. EMACRxEnable(EMAC0_BASE);
  348. /* Enable the Ethernet RX and TX interrupt source. */
  349. EMACIntEnable(EMAC0_BASE, (EMAC_INT_RECEIVE | EMAC_INT_TRANSMIT |
  350. EMAC_INT_TX_STOPPED | EMAC_INT_RX_NO_BUFFER |
  351. EMAC_INT_RX_STOPPED | EMAC_INT_PHY));
  352. /* Enable the Ethernet interrupt. */
  353. IntEnable(INT_EMAC0);
  354. /* Enable all processor interrupts. */
  355. IntMasterEnable();
  356. /* Tell the PHY to start an auto-negotiation cycle. */
  357. EMACPHYWrite(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_BMCR, (EPHY_BMCR_ANEN |
  358. EPHY_BMCR_RESTARTAN));
  359. }
  360. #ifdef DEBUG
  361. /**
  362. * Dump the chain of pbuf pointers to the debug output.
  363. */
  364. void
  365. tivaif_trace_pbuf(const char *pcTitle, struct pbuf *p)
  366. {
  367. LWIP_DEBUGF(NETIF_DEBUG, ("%s %08x (%d, %d)", pcTitle, p, p->tot_len,
  368. p->len));
  369. do
  370. {
  371. p = p->next;
  372. if(p)
  373. {
  374. LWIP_DEBUGF(NETIF_DEBUG, ("->%08x(%d)", p, p->len));
  375. }
  376. else
  377. {
  378. LWIP_DEBUGF(NETIF_DEBUG, ("->%08x", p));
  379. }
  380. } while((p != NULL) && (p->tot_len != p->len));
  381. LWIP_DEBUGF(NETIF_DEBUG, ("\n"));
  382. }
  383. #endif
  384. /**
  385. * This function is used to check whether a passed pbuf contains only buffers
  386. * resident in regions of memory that the Ethernet MAC can access. If any
  387. * buffers in the chain are outside a directly-DMAable section of memory,
  388. * the pbuf is copied to SRAM and a different pointer returned. If all
  389. * buffers are safe, the pbuf reference count is incremented and the original
  390. * pointer returned.
  391. */
  392. static struct pbuf *
  393. tivaif_check_pbuf(struct pbuf *p)
  394. {
  395. struct pbuf *pBuf;
  396. rt_err_t Err;
  397. pBuf = p;
  398. #ifdef DEBUG
  399. tivaif_trace_pbuf("Original:", p);
  400. #endif
  401. /* Walk the list of buffers in the pbuf checking each. */
  402. do
  403. {
  404. /* Does this pbuf's payload reside in memory that the Ethernet DMA
  405. * can access?
  406. */
  407. if(!PTR_SAFE_FOR_EMAC_DMA(pBuf->payload))
  408. {
  409. /* This buffer is outside the DMA-able memory space so we need
  410. * to copy the pbuf.
  411. */
  412. pBuf = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_POOL);
  413. /* If we got a new pbuf... */
  414. if(pBuf)
  415. {
  416. /* ...copy the old pbuf into the new one. */
  417. Err = pbuf_copy(pBuf, p);
  418. /* If we failed to copy the pbuf, free the newly allocated one
  419. * and make sure we return a NULL to show a problem.
  420. */
  421. if(Err != RT_EOK)
  422. {
  423. DRIVER_STATS_INC(TXCopyFailCount);
  424. pbuf_free(pBuf);
  425. pBuf = NULL;
  426. }
  427. else
  428. {
  429. #ifdef DEBUG
  430. tivaif_trace_pbuf("Copied:", pBuf);
  431. #endif
  432. DRIVER_STATS_INC(TXCopyCount);
  433. /* Reduce the reference count on the original pbuf since
  434. * we're not going to hold on to it after returning from
  435. * tivaif_transmit. Note that we already bumped
  436. * the reference count at the top of tivaif_transmit.
  437. */
  438. pbuf_free(p);
  439. }
  440. }
  441. /* Send back the new pbuf pointer or NULL if an error occurred. */
  442. return(pBuf);
  443. }
  444. /* Move on to the next buffer in the queue */
  445. pBuf = pBuf->next;
  446. }
  447. while(pBuf);
  448. /**
  449. * If we get here, the passed pbuf can be safely used without needing to
  450. * be copied.
  451. */
  452. return(p);
  453. }
  454. /**
  455. * This function should do the actual transmission of the packet. The packet is
  456. * contained in the pbuf that is passed to the function. This pbuf might be
  457. * chained.
  458. *
  459. * @param psNetif the lwip network interface structure for this ethernetif
  460. * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
  461. * @return RT_EOK if the packet coui32d be sent
  462. * an err_t value if the packet coui32dn't be sent
  463. */
  464. static rt_err_t
  465. tivaif_transmit(net_device_t dev, struct pbuf *p)
  466. {
  467. tStellarisIF *pIF;
  468. tDescriptor *pDesc;
  469. struct pbuf *pBuf;
  470. uint32_t ui32NumChained, ui32NumDescs;
  471. bool bFirst;
  472. SYS_ARCH_DECL_PROTECT(lev);
  473. LWIP_DEBUGF(NETIF_DEBUG, ("tivaif_transmit 0x%08x, len %d\n", p,
  474. p->tot_len));
  475. /**
  476. * This entire function must run within a "critical section" to preserve
  477. * the integrity of the transmit pbuf queue.
  478. */
  479. SYS_ARCH_PROTECT(lev);
  480. /* Update our transmit attempt counter. */
  481. DRIVER_STATS_INC(TXCount);
  482. /**
  483. * Increase the reference count on the packet provided so that we can
  484. * hold on to it until we are finished transmitting its content.
  485. */
  486. pbuf_ref(p);
  487. /**
  488. * Determine whether all buffers passed are within SRAM and, if not, copy
  489. * the pbuf into SRAM-resident buffers so that the Ethernet DMA can access
  490. * the data.
  491. */
  492. p = tivaif_check_pbuf(p);
  493. /* Make sure we still have a valid buffer (it may have been copied) */
  494. if(!p)
  495. {
  496. LINK_STATS_INC(link.memerr);
  497. SYS_ARCH_UNPROTECT(lev);
  498. return(-RT_ENOMEM);
  499. }
  500. /* Get our state data from the netif structure we were passed. */
  501. //pIF = (tStellarisIF *)psNetif->state;
  502. pIF = dev->dma_if;
  503. /* Make sure that the transmit descriptors are not all in use */
  504. pDesc = &(pIF->pTxDescList->pDescriptors[pIF->pTxDescList->ui32Write]);
  505. if(pDesc->pBuf)
  506. {
  507. /**
  508. * The current write descriptor has a pbuf attached to it so this
  509. * implies that the ring is fui32l. Reject this transmit request with a
  510. * memory error since we can't satisfy it just now.
  511. */
  512. pbuf_free(p);
  513. LINK_STATS_INC(link.memerr);
  514. DRIVER_STATS_INC(TXNoDescCount);
  515. SYS_ARCH_UNPROTECT(lev);
  516. return (-RT_ENOMEM);
  517. }
  518. /* How many pbufs are in the chain passed? */
  519. ui32NumChained = (uint32_t)pbuf_clen(p);
  520. /* How many free transmit descriptors do we have? */
  521. ui32NumDescs = (pIF->pTxDescList->ui32Read > pIF->pTxDescList->ui32Write) ?
  522. (pIF->pTxDescList->ui32Read - pIF->pTxDescList->ui32Write) :
  523. ((NUM_TX_DESCRIPTORS - pIF->pTxDescList->ui32Write) +
  524. pIF->pTxDescList->ui32Read);
  525. /* Do we have enough free descriptors to send the whole packet? */
  526. if(ui32NumDescs < ui32NumChained)
  527. {
  528. /* No - we can't transmit this whole packet so return an error. */
  529. pbuf_free(p);
  530. LINK_STATS_INC(link.memerr);
  531. DRIVER_STATS_INC(TXNoDescCount);
  532. SYS_ARCH_UNPROTECT(lev);
  533. return (-RT_ENOMEM);
  534. }
  535. /* Tag the first descriptor as the start of the packet. */
  536. bFirst = true;
  537. pDesc->Desc.ui32CtrlStatus = DES0_TX_CTRL_FIRST_SEG;
  538. /* Here, we know we can send the packet so write it to the descriptors */
  539. pBuf = p;
  540. while(ui32NumChained)
  541. {
  542. /* Get a pointer to the descriptor we will write next. */
  543. pDesc = &(pIF->pTxDescList->pDescriptors[pIF->pTxDescList->ui32Write]);
  544. /* Fill in the buffer pointer and length */
  545. pDesc->Desc.ui32Count = (uint32_t)pBuf->len;
  546. pDesc->Desc.pvBuffer1 = pBuf->payload;
  547. /* Tag the first descriptor as the start of the packet. */
  548. if(bFirst)
  549. {
  550. bFirst = false;
  551. pDesc->Desc.ui32CtrlStatus = DES0_TX_CTRL_FIRST_SEG;
  552. }
  553. else
  554. {
  555. pDesc->Desc.ui32CtrlStatus = 0;
  556. }
  557. pDesc->Desc.ui32CtrlStatus |= (DES0_TX_CTRL_IP_ALL_CKHSUMS |
  558. DES0_TX_CTRL_CHAINED);
  559. /* Decrement our descriptor counter, move on to the next buffer in the
  560. * pbuf chain. */
  561. ui32NumChained--;
  562. pBuf = pBuf->next;
  563. /* Update the descriptor list write index. */
  564. pIF->pTxDescList->ui32Write++;
  565. if(pIF->pTxDescList->ui32Write == NUM_TX_DESCRIPTORS)
  566. {
  567. pIF->pTxDescList->ui32Write = 0;
  568. }
  569. /* If this is the last descriptor, mark it as the end of the packet. */
  570. if(!ui32NumChained)
  571. {
  572. pDesc->Desc.ui32CtrlStatus |= (DES0_TX_CTRL_LAST_SEG |
  573. DES0_TX_CTRL_INTERRUPT);
  574. /* Tag the descriptor with the original pbuf pointer. */
  575. pDesc->pBuf = p;
  576. }
  577. else
  578. {
  579. /* Set the lsb of the pbuf pointer. We use this as a signal that
  580. * we should not free the pbuf when we are walking the descriptor
  581. * list while processing the transmit interrupt. We only free the
  582. * pbuf when processing the last descriptor used to transmit its
  583. * chain.
  584. */
  585. pDesc->pBuf = (struct pbuf *)((uint32_t)p + 1);
  586. }
  587. DRIVER_STATS_INC(TXBufQueuedCount);
  588. /* Hand the descriptor over to the hardware. */
  589. pDesc->Desc.ui32CtrlStatus |= DES0_TX_CTRL_OWN;
  590. }
  591. /* Tell the transmitter to start (in case it had stopped). */
  592. EMACTxDMAPollDemand(EMAC0_BASE);
  593. /* Update lwIP statistics */
  594. LINK_STATS_INC(link.xmit);
  595. SYS_ARCH_UNPROTECT(lev);
  596. return(RT_EOK);
  597. }
  598. /**
  599. * This function will process all transmit descriptors and free pbufs attached
  600. * to any that have been transmitted since we last checked.
  601. *
  602. * This function is called only from the Ethernet interrupt handler.
  603. *
  604. * @param netif the lwip network interface structure for this ethernetif
  605. * @return None.
  606. */
  607. static void
  608. tivaif_process_transmit(tStellarisIF *pIF)
  609. {
  610. tDescriptorList *pDescList;
  611. uint32_t ui32NumDescs;
  612. /* Get a pointer to the transmit descriptor list. */
  613. pDescList = pIF->pTxDescList;
  614. /* Walk the list until we have checked all descriptors or we reach the
  615. * write pointer or find a descriptor that the hardware is still working
  616. * on.
  617. */
  618. for(ui32NumDescs = 0; ui32NumDescs < pDescList->ui32NumDescs; ui32NumDescs++)
  619. {
  620. /* Has the buffer attached to this descriptor been transmitted? */
  621. if(pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32CtrlStatus &
  622. DES0_TX_CTRL_OWN)
  623. {
  624. /* No - we're finished. */
  625. break;
  626. }
  627. /* Does this descriptor have a buffer attached to it? */
  628. if(pDescList->pDescriptors[pDescList->ui32Read].pBuf)
  629. {
  630. /* Yes - free it if it's not marked as an intermediate pbuf */
  631. if(!((uint32_t)(pDescList->pDescriptors[pDescList->ui32Read].pBuf) & 1))
  632. {
  633. pbuf_free(pDescList->pDescriptors[pDescList->ui32Read].pBuf);
  634. DRIVER_STATS_INC(TXBufFreedCount);
  635. }
  636. pDescList->pDescriptors[pDescList->ui32Read].pBuf = NULL;
  637. }
  638. else
  639. {
  640. /* If the descriptor has no buffer, we are finished. */
  641. break;
  642. }
  643. /* Move on to the next descriptor. */
  644. pDescList->ui32Read++;
  645. if(pDescList->ui32Read == pDescList->ui32NumDescs)
  646. {
  647. pDescList->ui32Read = 0;
  648. }
  649. }
  650. }
  651. /**
  652. * This function will process all receive descriptors that contain newly read
  653. * data and pass complete frames up the lwIP stack as they are found. The
  654. * timestamp of the packet will be placed into the pbuf structure if PTPD is
  655. * enabled.
  656. *
  657. * This function is called only from the Ethernet interrupt handler.
  658. *
  659. * @param psNetif the lwip network interface structure for this ethernetif
  660. * @return None.
  661. */
  662. static void
  663. tivaif_receive(net_device_t dev)
  664. {
  665. tDescriptorList *pDescList;
  666. tStellarisIF *pIF;
  667. struct pbuf *pBuf;
  668. uint32_t ui32DescEnd;
  669. /* Get a pointer to our state data */
  670. pIF = dev->dma_if;
  671. /* Get a pointer to the receive descriptor list. */
  672. pDescList = pIF->pRxDescList;
  673. /* Start with a NULL pbuf so that we don't try to link chain the first
  674. * time round.
  675. */
  676. pBuf = NULL;
  677. /* Determine where we start and end our walk of the descriptor list */
  678. ui32DescEnd = pDescList->ui32Read ? (pDescList->ui32Read - 1) : (pDescList->ui32NumDescs - 1);
  679. /* Step through the descriptors that are marked for CPU attention. */
  680. while(pDescList->ui32Read != ui32DescEnd)
  681. {
  682. /* Does the current descriptor have a buffer attached to it? */
  683. if(pDescList->pDescriptors[pDescList->ui32Read].pBuf)
  684. {
  685. /* Yes - determine if the host has filled it yet. */
  686. if(pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32CtrlStatus &
  687. DES0_RX_CTRL_OWN)
  688. {
  689. /* The DMA engine still owns the descriptor so we are finished */
  690. break;
  691. }
  692. DRIVER_STATS_INC(RXBufReadCount);
  693. /* If this descriptor contains the end of the packet, fix up the
  694. * buffer size accordingly.
  695. */
  696. if(pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32CtrlStatus &
  697. DES0_RX_STAT_LAST_DESC)
  698. {
  699. /* This is the last descriptor for the frame so fix up the
  700. * length. It is safe for us to modify the internal fields
  701. * directly here (rather than calling pbuf_realloc) since we
  702. * know each of these pbufs is never chained.
  703. */
  704. pDescList->pDescriptors[pDescList->ui32Read].pBuf->len =
  705. (pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32CtrlStatus &
  706. DES0_RX_STAT_FRAME_LENGTH_M) >>
  707. DES0_RX_STAT_FRAME_LENGTH_S;
  708. pDescList->pDescriptors[pDescList->ui32Read].pBuf->tot_len =
  709. pDescList->pDescriptors[pDescList->ui32Read].pBuf->len;
  710. }
  711. if(pBuf)
  712. {
  713. /* Link this pbuf to the last one we looked at since this buffer
  714. * is a continuation of an existing frame (split across mui32tiple
  715. * pbufs). Note that we use pbuf_cat() here rather than
  716. * pbuf_chain() since we don't want to increase the reference
  717. * count of either pbuf - we only want to link them together.
  718. */
  719. pbuf_cat(pBuf, pDescList->pDescriptors[pDescList->ui32Read].pBuf);
  720. pDescList->pDescriptors[pDescList->ui32Read].pBuf = pBuf;
  721. }
  722. /* Remember the buffer associated with this descriptor. */
  723. pBuf = pDescList->pDescriptors[pDescList->ui32Read].pBuf;
  724. /* Is this the last descriptor for the current frame? */
  725. if(pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32CtrlStatus &
  726. DES0_RX_STAT_LAST_DESC)
  727. {
  728. /* Yes - does the frame contain errors? */
  729. if(pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32CtrlStatus &
  730. DES0_RX_STAT_ERR)
  731. {
  732. /* This is a bad frame so discard it and update the relevant
  733. * statistics.
  734. */
  735. LWIP_DEBUGF(NETIF_DEBUG, ("tivaif_receive: packet error\n"));
  736. pbuf_free(pBuf);
  737. LINK_STATS_INC(link.drop);
  738. DRIVER_STATS_INC(RXPacketErrCount);
  739. }
  740. else
  741. {
  742. /* This is a good frame so pass it up the stack. */
  743. LINK_STATS_INC(link.recv);
  744. DRIVER_STATS_INC(RXPacketReadCount);
  745. #if LWIP_PTPD
  746. /* Place the timestamp in the PBUF if PTPD is enabled */
  747. pBuf->time_s =
  748. pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32IEEE1588TimeHi;
  749. pBuf->time_ns =
  750. pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32IEEE1588TimeLo;
  751. #endif
  752. #if NO_SYS
  753. if(ethernet_input(pBuf, psNetif) != RT_EOK)
  754. {
  755. #else
  756. //if(tcpip_input(pBuf, psNetif) != RT_EOK)
  757. if((rt_mb_send(dev->rx_pbuf_mb, (rt_uint32_t)pBuf) != RT_EOK) ||
  758. (eth_device_ready(&(dev->parent)) != RT_EOK))
  759. {
  760. #endif
  761. /* drop the packet */
  762. LWIP_DEBUGF(NETIF_DEBUG, ("tivaif_input: input error\n"));
  763. pbuf_free(pBuf);
  764. /* Adjust the link statistics */
  765. LINK_STATS_INC(link.memerr);
  766. LINK_STATS_INC(link.drop);
  767. DRIVER_STATS_INC(RXPacketCBErrCount);
  768. }
  769. /* We're finished with this packet so make sure we don't try
  770. * to link the next buffer to it.
  771. */
  772. pBuf = NULL;
  773. }
  774. }
  775. }
  776. /* Allocate a new buffer for this descriptor */
  777. pDescList->pDescriptors[pDescList->ui32Read].pBuf = pbuf_alloc(PBUF_RAW,
  778. PBUF_POOL_BUFSIZE,
  779. PBUF_POOL);
  780. pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32Count =
  781. DES1_RX_CTRL_CHAINED;
  782. if(pDescList->pDescriptors[pDescList->ui32Read].pBuf)
  783. {
  784. /* We got a buffer so fill in the payload pointer and size. */
  785. pDescList->pDescriptors[pDescList->ui32Read].Desc.pvBuffer1 =
  786. pDescList->pDescriptors[pDescList->ui32Read].pBuf->payload;
  787. pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32Count |=
  788. (pDescList->pDescriptors[pDescList->ui32Read].pBuf->len <<
  789. DES1_RX_CTRL_BUFF1_SIZE_S);
  790. /* Give this descriptor back to the hardware */
  791. pDescList->pDescriptors[pDescList->ui32Read].Desc.ui32CtrlStatus =
  792. DES0_RX_CTRL_OWN;
  793. }
  794. else
  795. {
  796. LWIP_DEBUGF(NETIF_DEBUG, ("tivaif_receive: pbuf_alloc error\n"));
  797. pDescList->pDescriptors[pDescList->ui32Read].Desc.pvBuffer1 = 0;
  798. /* Update the stats to show we coui32dn't allocate a pbuf. */
  799. DRIVER_STATS_INC(RXNoBufCount);
  800. LINK_STATS_INC(link.memerr);
  801. /* Stop parsing here since we can't leave a broken descriptor in
  802. * the chain.
  803. */
  804. break;
  805. }
  806. /* Move on to the next descriptor in the chain, taking care to wrap. */
  807. pDescList->ui32Read++;
  808. if(pDescList->ui32Read == pDescList->ui32NumDescs)
  809. {
  810. pDescList->ui32Read = 0;
  811. }
  812. }
  813. }
  814. /**
  815. * Process interrupts from the PHY.
  816. *
  817. * should be called from the Stellaris Ethernet Interrupt Handler. This
  818. * function will read packets from the Stellaris Ethernet fifo and place them
  819. * into a pbuf queue. If the transmitter is idle and there is at least one packet
  820. * on the transmit queue, it will place it in the transmit fifo and start the
  821. * transmitter.
  822. *
  823. */
  824. void
  825. tivaif_process_phy_interrupt(net_device_t dev)
  826. {
  827. uint16_t ui16Val, ui16Status;
  828. uint32_t ui32Config, ui32Mode, ui32RxMaxFrameSize;
  829. /* Read the PHY interrupt status. This clears all interrupt sources.
  830. * Note that we are only enabling sources in EPHY_MISR1 so we don't
  831. * read EPHY_MISR2.
  832. */
  833. ui16Val = EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_MISR1);
  834. /*
  835. * Dummy read PHY REG EPHY_BMSR, it will force update the EPHY_STS register
  836. */
  837. EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_BMSR);
  838. /* Read the current PHY status. */
  839. ui16Status = EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, EPHY_STS);
  840. /* Has the link status changed? */
  841. if(ui16Val & EPHY_MISR1_LINKSTAT)
  842. {
  843. /* Is link up or down now? */
  844. if(ui16Status & EPHY_STS_LINK)
  845. {
  846. /* Tell lwIP the link is up. */
  847. #if NO_SYS
  848. netif_set_link_up(psNetif);
  849. #else
  850. //tcpip_callback((tcpip_callback_fn)netif_set_link_up, psNetif);
  851. eth_device_linkchange(&(dev->parent), RT_TRUE);
  852. #endif
  853. /* In this case we drop through since we may need to reconfigure
  854. * the MAC depending upon the speed and half/fui32l-duplex settings.
  855. */
  856. }
  857. else
  858. {
  859. /* Tell lwIP the link is down */
  860. #if NO_SYS
  861. netif_set_link_down(psNetif);
  862. #else
  863. //tcpip_callback((tcpip_callback_fn)netif_set_link_down, psNetif);
  864. eth_device_linkchange(&(dev->parent), RT_FALSE);
  865. #endif
  866. }
  867. }
  868. /* Has the speed or duplex status changed? */
  869. if(ui16Val & (EPHY_MISR1_SPEED | EPHY_MISR1_SPEED | EPHY_MISR1_ANC))
  870. {
  871. /* Get the current MAC configuration. */
  872. EMACConfigGet(EMAC0_BASE, &ui32Config, &ui32Mode,
  873. &ui32RxMaxFrameSize);
  874. /* What speed is the interface running at now?
  875. */
  876. if(ui16Status & EPHY_STS_SPEED)
  877. {
  878. /* 10Mbps is selected */
  879. ui32Config &= ~EMAC_CONFIG_100MBPS;
  880. }
  881. else
  882. {
  883. /* 100Mbps is selected */
  884. ui32Config |= EMAC_CONFIG_100MBPS;
  885. }
  886. /* Are we in fui32l- or half-duplex mode? */
  887. if(ui16Status & EPHY_STS_DUPLEX)
  888. {
  889. /* Fui32l duplex. */
  890. ui32Config |= EMAC_CONFIG_FULL_DUPLEX;
  891. }
  892. else
  893. {
  894. /* Half duplex. */
  895. ui32Config &= ~EMAC_CONFIG_FULL_DUPLEX;
  896. }
  897. /* Reconfigure the MAC */
  898. EMACConfigSet(EMAC0_BASE, ui32Config, ui32Mode, ui32RxMaxFrameSize);
  899. }
  900. }
  901. /**
  902. * Process tx and rx packets at the low-level interrupt.
  903. *
  904. * should be called from the Stellaris Ethernet Interrupt Handler. This
  905. * function will read packets from the Stellaris Ethernet fifo and place them
  906. * into a pbuf queue. If the transmitter is idle and there is at least one packet
  907. * on the transmit queue, it will place it in the transmit fifo and start the
  908. * transmitter.
  909. *
  910. */
  911. void
  912. tivaif_interrupt(net_device_t dev, uint32_t ui32Status)
  913. {
  914. /* Update our debug interrupt counters. */
  915. if(ui32Status & EMAC_INT_NORMAL_INT)
  916. {
  917. g_ui32NormalInts++;
  918. }
  919. if(ui32Status & EMAC_INT_ABNORMAL_INT)
  920. {
  921. g_ui32AbnormalInts++;
  922. }
  923. /* Is this an interrupt from the PHY? */
  924. if(ui32Status & EMAC_INT_PHY)
  925. {
  926. tivaif_process_phy_interrupt(dev);
  927. }
  928. /* Process the transmit DMA list, freeing any buffers that have been
  929. * transmitted since our last interrupt.
  930. */
  931. if(ui32Status & EMAC_INT_TRANSMIT)
  932. {
  933. tivaif_process_transmit(dev->dma_if);
  934. }
  935. /**
  936. * Process the receive DMA list and pass all successfui32ly received packets
  937. * up the stack. We also call this function in cases where the receiver has
  938. * stalled due to missing buffers since the receive function will attempt to
  939. * allocate new pbufs for descriptor entries which have none.
  940. */
  941. if(ui32Status & (EMAC_INT_RECEIVE | EMAC_INT_RX_NO_BUFFER |
  942. EMAC_INT_RX_STOPPED))
  943. {
  944. tivaif_receive(dev);
  945. }
  946. }
  947. #if NETIF_DEBUG
  948. /* Print an IP header by using LWIP_DEBUGF
  949. * @param p an IP packet, p->payload pointing to the IP header
  950. */
  951. void
  952. tivaif_debug_print(struct pbuf *p)
  953. {
  954. struct eth_hdr *ethhdr = (struct eth_hdr *)p->payload;
  955. u16_t *plen = (u16_t *)p->payload;
  956. LWIP_DEBUGF(NETIF_DEBUG, ("ETH header:\n"));
  957. LWIP_DEBUGF(NETIF_DEBUG, ("Packet Length:%5"U16_F" \n",*plen));
  958. LWIP_DEBUGF(NETIF_DEBUG, ("Destination: %02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"\n",
  959. ethhdr->dest.addr[0],
  960. ethhdr->dest.addr[1],
  961. ethhdr->dest.addr[2],
  962. ethhdr->dest.addr[3],
  963. ethhdr->dest.addr[4],
  964. ethhdr->dest.addr[5]));
  965. LWIP_DEBUGF(NETIF_DEBUG, ("Source: %02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"-%02"X8_F"\n",
  966. ethhdr->src.addr[0],
  967. ethhdr->src.addr[1],
  968. ethhdr->src.addr[2],
  969. ethhdr->src.addr[3],
  970. ethhdr->src.addr[4],
  971. ethhdr->src.addr[5]));
  972. LWIP_DEBUGF(NETIF_DEBUG, ("Packet Type:0x%04"U16_F" \n", ethhdr->type));
  973. }
  974. #endif /* NETIF_DEBUG */
  975. void lwIPEthernetIntHandler(void)
  976. {
  977. uint32_t ui32Status;
  978. #ifdef DEF_INT_TEMPSTAMP
  979. uint32_t ui32TimerStatus;
  980. #endif
  981. //
  982. // Read and Clear the interrupt.
  983. //
  984. ui32Status = MAP_EMACIntStatus(EMAC0_BASE, true);
  985. //
  986. // If the interrupt really came from the Ethernet and not our
  987. // timer, clear it.
  988. //
  989. if(ui32Status)
  990. {
  991. MAP_EMACIntClear(EMAC0_BASE, ui32Status);
  992. }
  993. #ifdef DEF_INT_TEMPSTAMP
  994. //
  995. // Check to see whether a hardware timer interrupt has been reported.
  996. //
  997. if(ui32Status & EMAC_INT_TIMESTAMP)
  998. {
  999. //
  1000. // Yes - read and clear the timestamp interrupt status.
  1001. //
  1002. ui32TimerStatus = EMACTimestampIntStatus(EMAC0_BASE);
  1003. //
  1004. // If a timer interrupt handler has been registered, call it.
  1005. //
  1006. if(g_pfnTimerHandler)
  1007. {
  1008. g_pfnTimerHandler(EMAC0_BASE, ui32TimerStatus);
  1009. }
  1010. }
  1011. #endif
  1012. //
  1013. // The handling of the interrupt is different based on the use of a RTOS.
  1014. //
  1015. //
  1016. // No RTOS is being used. If a transmit/receive interrupt was active,
  1017. // run the low-level interrupt handler.
  1018. //
  1019. if(ui32Status)
  1020. {
  1021. tivaif_interrupt(eth_dev, ui32Status);
  1022. }
  1023. //
  1024. // Service the lwIP timers.
  1025. //
  1026. //lwIPServiceTimers();
  1027. }
  1028. // OUI:00-12-37 (hex) Texas Instruments, only for test
  1029. static int tiva_eth_mac_addr_init(void)
  1030. {
  1031. int retVal =0;
  1032. uint32_t ulUser[2];
  1033. uint8_t mac_addr[6];
  1034. MAP_FlashUserGet(&ulUser[0], &ulUser[1]);
  1035. if((ulUser[0] == 0xffffffff) || (ulUser[1] == 0xffffffff))
  1036. {
  1037. rt_kprintf("Fail to get mac address from eeprom.\n");
  1038. rt_kprintf("Using default mac address\n");
  1039. // OUI:00-12-37 (hex) Texas Instruments, only for test
  1040. // Configure the hardware MAC address
  1041. ulUser[0] = 0x00371200;
  1042. ulUser[1] = 0x00563412;
  1043. //FlashUserSet(ulUser0, ulUser1);
  1044. retVal =-1;
  1045. }
  1046. //Convert the 24/24 split MAC address from NV ram into a 32/16 split MAC
  1047. //address needed to program the hardware registers, then program the MAC
  1048. //address into the Ethernet Controller registers.
  1049. mac_addr[0] = ((ulUser[0] >> 0) & 0xff);
  1050. mac_addr[1] = ((ulUser[0] >> 8) & 0xff);
  1051. mac_addr[2] = ((ulUser[0] >> 16) & 0xff);
  1052. mac_addr[3] = ((ulUser[1] >> 0) & 0xff);
  1053. mac_addr[4] = ((ulUser[1] >> 8) & 0xff);
  1054. mac_addr[5] = ((ulUser[1] >> 16) & 0xff);
  1055. //
  1056. // Program the hardware with its MAC address (for filtering).
  1057. //
  1058. MAP_EMACAddrSet(EMAC0_BASE, 0, mac_addr);
  1059. return retVal;
  1060. }
  1061. void tiva_eth_lowlevel_init(void)
  1062. {
  1063. MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
  1064. //
  1065. // PF1/PK4/PK6 are used for Ethernet LEDs.
  1066. //
  1067. MAP_GPIOPinConfigure(GPIO_PF0_EN0LED0);
  1068. MAP_GPIOPinConfigure(GPIO_PF4_EN0LED1);
  1069. GPIOPinTypeEthernetLED(GPIO_PORTF_BASE, GPIO_PIN_0);
  1070. GPIOPinTypeEthernetLED(GPIO_PORTF_BASE, GPIO_PIN_4);
  1071. //
  1072. // Enable the ethernet peripheral.
  1073. //
  1074. MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_EMAC0);
  1075. MAP_SysCtlPeripheralReset(SYSCTL_PERIPH_EMAC0);
  1076. //
  1077. // Enable the internal PHY if it's present and we're being
  1078. // asked to use it.
  1079. //
  1080. if((EMAC_PHY_CONFIG & EMAC_PHY_TYPE_MASK) == EMAC_PHY_TYPE_INTERNAL)
  1081. {
  1082. //
  1083. // We've been asked to configure for use with the internal
  1084. // PHY. Is it present?
  1085. //
  1086. if(SysCtlPeripheralPresent(SYSCTL_PERIPH_EPHY0))
  1087. {
  1088. //
  1089. // Yes - enable and reset it.
  1090. //
  1091. MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_EPHY0);
  1092. MAP_SysCtlPeripheralReset(SYSCTL_PERIPH_EPHY0);
  1093. }
  1094. else
  1095. {
  1096. //
  1097. // Internal PHY is not present on this part so hang here.
  1098. //
  1099. rt_kprintf("Internal PHY is not present on this part.\n");
  1100. while(1)
  1101. {
  1102. }
  1103. }
  1104. }
  1105. //
  1106. // Wait for the MAC to come out of reset.
  1107. //
  1108. while(!MAP_SysCtlPeripheralReady(SYSCTL_PERIPH_EMAC0))
  1109. {
  1110. }
  1111. //
  1112. // Configure for use with whichever PHY the user requires.
  1113. //
  1114. MAP_EMACPHYConfigSet(EMAC0_BASE, EMAC_PHY_CONFIG);
  1115. //
  1116. // Initialize the MAC and set the DMA mode.
  1117. //
  1118. MAP_EMACInit(EMAC0_BASE, 120000000, //system clock = 120MHz
  1119. EMAC_BCONFIG_MIXED_BURST | EMAC_BCONFIG_PRIORITY_FIXED,
  1120. 4, 4, 0);
  1121. //
  1122. // Set MAC configuration options.
  1123. //
  1124. MAP_EMACConfigSet(EMAC0_BASE, (EMAC_CONFIG_FULL_DUPLEX |
  1125. EMAC_CONFIG_CHECKSUM_OFFLOAD |
  1126. EMAC_CONFIG_7BYTE_PREAMBLE |
  1127. EMAC_CONFIG_IF_GAP_96BITS |
  1128. EMAC_CONFIG_USE_MACADDR0 |
  1129. EMAC_CONFIG_SA_FROM_DESCRIPTOR |
  1130. EMAC_CONFIG_BO_LIMIT_1024),
  1131. (EMAC_MODE_RX_STORE_FORWARD |
  1132. EMAC_MODE_TX_STORE_FORWARD |
  1133. EMAC_MODE_TX_THRESHOLD_64_BYTES |
  1134. EMAC_MODE_RX_THRESHOLD_64_BYTES), 0);
  1135. EMACIntRegister(EMAC0_BASE, lwIPEthernetIntHandler);
  1136. }
  1137. static rt_err_t eth_dev_init(rt_device_t device)
  1138. {
  1139. net_device_t net_dev = (net_device_t)device;
  1140. struct netif *psNetif = (net_dev->parent.netif);
  1141. LWIP_ASSERT("psNetif != NULL", (psNetif != NULL));
  1142. #if LWIP_NETIF_HOSTNAME
  1143. /* Initialize interface hostname */
  1144. psNetif->hostname = "t4mc";
  1145. #endif /* LWIP_NETIF_HOSTNAME */
  1146. /*
  1147. * Initialize the snmp variables and counters inside the struct netif.
  1148. * The last argument should be replaced with your link speed, in units
  1149. * of bits per second.
  1150. */
  1151. //NETIF_INIT_SNMP(psNetif, snmp_ifType_ethernet_csmacd, 1000000);
  1152. net_dev->dma_if = &g_StellarisIFData;
  1153. /* Remember our MAC address. */
  1154. g_StellarisIFData.ethaddr = (struct eth_addr *)&(psNetif->hwaddr[0]);
  1155. /* Initialize the hardware */
  1156. tivaif_hwinit(psNetif);
  1157. return RT_EOK;
  1158. }
  1159. /* control the interface */
  1160. static rt_err_t eth_dev_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  1161. {
  1162. switch(cmd)
  1163. {
  1164. case NIOCTL_GADDR:
  1165. /* get mac address */
  1166. if(args)
  1167. MAP_EMACAddrGet(EMAC0_BASE, 0, (uint8_t*)args);
  1168. else
  1169. return -RT_ERROR;
  1170. break;
  1171. default :
  1172. break;
  1173. }
  1174. return RT_EOK;
  1175. }
  1176. /* Open the interface */
  1177. static rt_err_t eth_dev_open(rt_device_t dev, rt_uint16_t oflag)
  1178. {
  1179. return RT_EOK;
  1180. }
  1181. /* Close the interface */
  1182. static rt_err_t eth_dev_close(rt_device_t dev)
  1183. {
  1184. return RT_EOK;
  1185. }
  1186. /* Read */
  1187. static rt_size_t eth_dev_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  1188. {
  1189. rt_set_errno(-RT_ENOSYS);
  1190. return 0;
  1191. }
  1192. /* Write */
  1193. static rt_size_t eth_dev_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  1194. {
  1195. rt_set_errno(-RT_ENOSYS);
  1196. return 0;
  1197. }
  1198. static rt_err_t eth_dev_tx(rt_device_t dev, struct pbuf *p)
  1199. {
  1200. return tivaif_transmit((net_device_t)dev, p);
  1201. }
  1202. static struct pbuf* eth_dev_rx(rt_device_t dev)
  1203. {
  1204. rt_err_t result;
  1205. rt_uint32_t temp =0;
  1206. net_device_t net_dev = (net_device_t)dev;
  1207. result = rt_mb_recv(net_dev->rx_pbuf_mb, &temp, RT_WAITING_NO);
  1208. return (result == RT_EOK)? (struct pbuf*)temp : RT_NULL;
  1209. }
  1210. int rt_hw_tiva_eth_init(void)
  1211. {
  1212. rt_err_t result;
  1213. /* Clock GPIO and etc */
  1214. tiva_eth_lowlevel_init();
  1215. tiva_eth_mac_addr_init();
  1216. /* init rt-thread device interface */
  1217. eth_dev->parent.parent.init = eth_dev_init;
  1218. eth_dev->parent.parent.open = eth_dev_open;
  1219. eth_dev->parent.parent.close = eth_dev_close;
  1220. eth_dev->parent.parent.read = eth_dev_read;
  1221. eth_dev->parent.parent.write = eth_dev_write;
  1222. eth_dev->parent.parent.control = eth_dev_control;
  1223. eth_dev->parent.eth_rx = eth_dev_rx;
  1224. eth_dev->parent.eth_tx = eth_dev_tx;
  1225. result = rt_mb_init(&eth_rx_pbuf_mb, "epbuf",
  1226. &rx_pbuf_mb_pool[0], sizeof(rx_pbuf_mb_pool)/4,
  1227. RT_IPC_FLAG_FIFO);
  1228. RT_ASSERT(result == RT_EOK);
  1229. eth_dev->rx_pbuf_mb = &eth_rx_pbuf_mb;
  1230. result = eth_device_init(&(eth_dev->parent), "e0");
  1231. return result;
  1232. }
  1233. // eth_device_init using malloc
  1234. // We use INIT_COMPONENT_EXPORT insted of INIT_BOARD_EXPORT
  1235. INIT_COMPONENT_EXPORT(rt_hw_tiva_eth_init);
  1236. #if 0
  1237. #ifdef RT_USING_FINSH
  1238. #include "finsh.h"
  1239. void PHY_Read(uint8_t addr)
  1240. {
  1241. uint16_t data = EMACPHYRead(EMAC0_BASE, PHY_PHYS_ADDR, addr);
  1242. rt_kprintf("R PHY_REG[0x%02X] = 0x%04X\n", addr, data);
  1243. }
  1244. FINSH_FUNCTION_EXPORT(PHY_Read, (add));
  1245. void PHY_Write(uint8_t addr , uint16_t data)
  1246. {
  1247. EMACPHYWrite(EMAC0_BASE, PHY_PHYS_ADDR, addr, data);
  1248. rt_kprintf("W PHY_REG[0x%02X] = 0x%04X\n", addr, data);
  1249. }
  1250. FINSH_FUNCTION_EXPORT(PHY_Write, (add, data));
  1251. void PHY_SetAdd(uint8_t addr0, uint8_t addr1, uint8_t addr2,
  1252. uint8_t addr3, uint8_t addr4, uint8_t addr5)
  1253. {
  1254. uint32_t ulUser[2];
  1255. ulUser[0] = (((addr2<<8)|addr1)<<8)|addr0;
  1256. ulUser[1] = (((addr5<<8)|addr4)<<8)|addr3;
  1257. MAP_FlashUserSet(ulUser[0], ulUser[1]);
  1258. MAP_FlashUserSave();
  1259. rt_kprintf("Save to EEPROM. please reboot.");
  1260. }
  1261. FINSH_FUNCTION_EXPORT(PHY_SetAdd, (add0-add5));
  1262. #endif //RT_USING_FINSH
  1263. #endif