luminaryif.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. //*****************************************************************************
  2. //
  3. // luminaryif.c - Ethernet Interface File for lwIP TCP/IP Stack
  4. //
  5. //*****************************************************************************
  6. #include <inc/hw_memmap.h>
  7. #include <inc/hw_types.h>
  8. #include <inc/hw_ints.h>
  9. #include <inc/hw_ethernet.h>
  10. #include <driverlib/ethernet.h>
  11. #include <driverlib/interrupt.h>
  12. #include <driverlib/sysctl.h>
  13. #include <driverlib/gpio.h>
  14. #include <driverlib/flash.h>
  15. #include <netif/ethernetif.h>
  16. #include "lwipopts.h"
  17. #include "luminaryif.h"
  18. #define MAX_ADDR_LEN 6
  19. struct net_device
  20. {
  21. /* inherit from ethernet device */
  22. struct eth_device parent;
  23. /* interface address info. */
  24. rt_uint8_t dev_addr[MAX_ADDR_LEN]; /* hw address */
  25. };
  26. static struct net_device luminaryif_dev_entry;
  27. static struct net_device *luminaryif_dev = &luminaryif_dev_entry;
  28. static struct rt_semaphore tx_sem;
  29. //*****************************************************************************
  30. //
  31. // Sanity Check: This module will NOT work if the following defines
  32. // are incorrect.
  33. //
  34. //*****************************************************************************
  35. #if (PBUF_LINK_HLEN != 16)
  36. #error "Incorrect PBUF_LINK_HLEN specified!"
  37. #endif
  38. #if (ETH_PAD_SIZE != 2)
  39. #error "Incorrect ETH_PAD_SIZE specified!"
  40. #endif
  41. #if (PBUF_POOL_BUFSIZE % 4)
  42. #error "PBUF_POOL_BUFSIZE must be modulo 4!"
  43. #endif
  44. /* RT-Thread Device Interface */
  45. /* initialize the interface */
  46. //*****************************************************************************
  47. //
  48. // Low-Level initialization function for the Ethernet Controller.
  49. //
  50. //*****************************************************************************
  51. rt_err_t luminaryif_init(rt_device_t dev)
  52. {
  53. unsigned long ulTemp;
  54. //
  55. // Disable all Ethernet Interrupts.
  56. //
  57. EthernetIntDisable(ETH_BASE, (ETH_INT_PHY | ETH_INT_MDIO | ETH_INT_RXER |
  58. ETH_INT_RXOF | ETH_INT_TX | ETH_INT_TXER |
  59. ETH_INT_RX));
  60. ulTemp = EthernetIntStatus(ETH_BASE, false);
  61. EthernetIntClear(ETH_BASE, ulTemp);
  62. //
  63. // Initialize the Ethernet Controller.
  64. //
  65. EthernetInitExpClk(ETH_BASE, SysCtlClockGet());
  66. //
  67. // Configure the Ethernet Controller for normal operation.
  68. // - Enable TX Duplex Mode
  69. // - Enable TX Padding
  70. // - Enable TX CRC Generation
  71. //
  72. EthernetConfigSet(ETH_BASE, (ETH_CFG_TX_DPLXEN |
  73. ETH_CFG_TX_CRCEN | ETH_CFG_TX_PADEN));
  74. //
  75. // Enable the Ethernet Controller transmitter and receiver.
  76. //
  77. EthernetEnable(ETH_BASE);
  78. //
  79. // Enable the Ethernet Interrupt handler.
  80. //
  81. IntEnable(INT_ETH);
  82. //
  83. // Enable Ethernet TX and RX Packet Interrupts.
  84. //
  85. EthernetIntEnable(ETH_BASE, ETH_INT_RX | ETH_INT_TX);
  86. return RT_EOK;
  87. }
  88. void luminaryif_isr(void)
  89. {
  90. unsigned long ulTemp;
  91. //
  92. // Read and Clear the interrupt.
  93. //
  94. ulTemp = EthernetIntStatus(ETH_BASE, false);
  95. EthernetIntClear(ETH_BASE, ulTemp);
  96. //
  97. // Check to see if an RX Interrupt has occured.
  98. //
  99. if (ulTemp & ETH_INT_RX)
  100. {
  101. //
  102. // Indicate that a packet has been received.
  103. //
  104. rt_err_t result;
  105. /* a frame has been received */
  106. result = eth_device_ready((struct eth_device *)&(luminaryif_dev->parent));
  107. if (result != RT_EOK)
  108. rt_set_errno(-RT_ERROR);
  109. //
  110. // Disable Ethernet RX Interrupt.
  111. //
  112. EthernetIntDisable(ETH_BASE, ETH_INT_RX);
  113. }
  114. if (ulTemp & ETH_INT_TX)
  115. {
  116. /* A frame has been transmitted. */
  117. rt_sem_release(&tx_sem);
  118. }
  119. }
  120. /* control the interface */
  121. rt_err_t luminaryif_control(rt_device_t dev, int cmd, void *args)
  122. {
  123. switch (cmd)
  124. {
  125. case NIOCTL_GADDR:
  126. /* get mac address */
  127. if (args)
  128. rt_memcpy(args, luminaryif_dev_entry.dev_addr, 6);
  129. else
  130. return -RT_ERROR;
  131. break;
  132. default:
  133. break;
  134. }
  135. return RT_EOK;
  136. }
  137. /* Open the ethernet interface */
  138. rt_err_t luminaryif_open(rt_device_t dev, rt_uint16_t oflag)
  139. {
  140. return RT_EOK;
  141. }
  142. /* Close the interface */
  143. rt_err_t luminaryif_close(rt_device_t dev)
  144. {
  145. return RT_EOK;
  146. }
  147. /* Read */
  148. rt_size_t luminaryif_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  149. {
  150. rt_set_errno(-RT_ENOSYS);
  151. return 0;
  152. }
  153. /* Write */
  154. rt_size_t luminaryif_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  155. {
  156. rt_set_errno(-RT_ENOSYS);
  157. return 0;
  158. }
  159. //****************************************************************************
  160. //
  161. // Low-Level transmit routine. Should do the actual transmission of the
  162. // packet. The packet is contained in the pbuf that is passed to the function.
  163. // This pbuf might be chained.
  164. //
  165. //****************************************************************************
  166. rt_err_t luminaryif_tx(rt_device_t dev, struct pbuf *p)
  167. {
  168. int iBuf;
  169. unsigned char *pucBuf;
  170. unsigned long *pulBuf;
  171. struct pbuf *q;
  172. int iGather;
  173. unsigned long ulGather;
  174. unsigned char *pucGather;
  175. unsigned long ulTemp;
  176. /* lock tx operation */
  177. rt_sem_take(&tx_sem, RT_WAITING_FOREVER);
  178. //
  179. // Wait for space available in the TX FIFO.
  180. //
  181. while (!EthernetSpaceAvail(ETH_BASE))
  182. {
  183. }
  184. //
  185. // Fill in the first two bytes of the payload data (configured as padding
  186. // with ETH_PAD_SIZE = 2) with the total length of the payload data
  187. // (minus the Ethernet MAC layer header).
  188. //
  189. *((unsigned short *)(p->payload)) = p->tot_len - 16;
  190. //
  191. // Initialize the gather register.
  192. //
  193. iGather = 0;
  194. pucGather = (unsigned char *)&ulGather;
  195. ulGather = 0;
  196. //
  197. // Copy data from the pbuf(s) into the TX Fifo.
  198. //
  199. for (q = p; q != NULL; q = q->next)
  200. {
  201. //
  202. // Intialize a char pointer and index to the pbuf payload data.
  203. //
  204. pucBuf = (unsigned char *)q->payload;
  205. iBuf = 0;
  206. //
  207. // If the gather buffer has leftover data from a previous pbuf
  208. // in the chain, fill it up and write it to the Tx FIFO.
  209. //
  210. while ((iBuf < q->len) && (iGather != 0))
  211. {
  212. //
  213. // Copy a byte from the pbuf into the gather buffer.
  214. //
  215. pucGather[iGather] = pucBuf[iBuf++];
  216. //
  217. // Increment the gather buffer index modulo 4.
  218. //
  219. iGather = ((iGather + 1) % 4);
  220. }
  221. //
  222. // If the gather index is 0 and the pbuf index is non-zero,
  223. // we have a gather buffer to write into the Tx FIFO.
  224. //
  225. if ((iGather == 0) && (iBuf != 0))
  226. {
  227. HWREG(ETH_BASE + MAC_O_DATA) = ulGather;
  228. ulGather = 0;
  229. }
  230. //
  231. // Copy words of pbuf data into the Tx FIFO, but don't go past
  232. // the end of the pbuf.
  233. //
  234. if ((iBuf % 4) != 0)
  235. {
  236. while ((iBuf + 4) <= q->len)
  237. {
  238. ulTemp = (pucBuf[iBuf++] << 0);
  239. ulTemp |= (pucBuf[iBuf++] << 8);
  240. ulTemp |= (pucBuf[iBuf++] << 16);
  241. ulTemp |= (pucBuf[iBuf++] << 24);
  242. HWREG(ETH_BASE + MAC_O_DATA) = ulTemp;
  243. }
  244. }
  245. else
  246. {
  247. //
  248. // Initialze a long pointer into the pbuf for 32-bit access.
  249. //
  250. pulBuf = (unsigned long *)&pucBuf[iBuf];
  251. while ((iBuf + 4) <= q->len)
  252. {
  253. HWREG(ETH_BASE + MAC_O_DATA) = *pulBuf++;
  254. iBuf += 4;
  255. }
  256. }
  257. //
  258. // Check if leftover data in the pbuf and save it in the gather
  259. // buffer for the next time.
  260. //
  261. while (iBuf < q->len)
  262. {
  263. //
  264. // Copy a byte from the pbuf into the gather buffer.
  265. //
  266. pucGather[iGather] = pucBuf[iBuf++];
  267. //
  268. // Increment the gather buffer index modulo 4.
  269. //
  270. iGather = ((iGather + 1) % 4);
  271. }
  272. }
  273. //
  274. // Send any leftover data to the FIFO.
  275. //
  276. HWREG(ETH_BASE + MAC_O_DATA) = ulGather;
  277. //
  278. // Wakeup the transmitter.
  279. //
  280. HWREG(ETH_BASE + MAC_O_TR) = MAC_TR_NEWTX;
  281. #if LINK_STATS
  282. lwip_stats.link.xmit++;
  283. #endif
  284. return (ERR_OK);
  285. }
  286. //*****************************************************************************
  287. //
  288. // Low-Level receive routine. Should allocate a pbuf and transfer the bytes
  289. // of the incoming packet from the interface into the pbuf.
  290. //
  291. //*****************************************************************************
  292. struct pbuf *luminaryif_rx(rt_device_t dev)
  293. {
  294. struct pbuf *p, *q;
  295. u16_t len;
  296. unsigned long ulTemp;
  297. int i;
  298. unsigned long *ptr;
  299. if (!EthernetPacketAvail(ETH_BASE))
  300. {
  301. //
  302. // Enable Ethernet RX Interrupt.
  303. //
  304. EthernetIntEnable(ETH_BASE, ETH_INT_RX);
  305. return (NULL);
  306. }
  307. //
  308. // Obtain the size of the packet and put it into the "len" variable.
  309. // Note: The length returned in the FIFO length position includes the
  310. // two bytes for the length + the 4 bytes for the FCS.
  311. //
  312. ulTemp = HWREG(ETH_BASE + MAC_O_DATA);
  313. len = ulTemp & 0xFFFF;
  314. //
  315. // We allocate a pbuf chain of pbufs from the pool.
  316. //
  317. p = pbuf_alloc(PBUF_LINK, len, PBUF_RAM);
  318. if (p != NULL)
  319. {
  320. //
  321. // Place the first word into the first pbuf location.
  322. //
  323. *(unsigned long *)p->payload = ulTemp;
  324. p->payload = (char *)(p->payload) + 4;
  325. p->len -= 4;
  326. //
  327. // Process all but the last buffer in the pbuf chain.
  328. //
  329. q = p;
  330. while (q != NULL)
  331. {
  332. //
  333. // Setup a byte pointer into the payload section of the pbuf.
  334. //
  335. ptr = q->payload;
  336. //
  337. // Read data from FIFO into the current pbuf
  338. // (assume pbuf length is modulo 4)
  339. //
  340. for (i = 0; i < q->len; i += 4)
  341. {
  342. *ptr++ = HWREG(ETH_BASE + MAC_O_DATA);
  343. }
  344. //
  345. // Link in the next pbuf in the chain.
  346. //
  347. q = q->next;
  348. }
  349. //
  350. // Restore the first pbuf parameters to their original values.
  351. //
  352. p->payload = (char *)(p->payload) - 4;
  353. p->len += 4;
  354. #if LINK_STATS
  355. lwip_stats.link.recv++;
  356. #endif
  357. }
  358. else
  359. {
  360. //
  361. // Just read all of the remaining data from the FIFO and dump it.
  362. //
  363. for (i = 4; i < len; i += 4)
  364. {
  365. ulTemp = HWREG(ETH_BASE + MAC_O_DATA);
  366. }
  367. #if LINK_STATS
  368. lwip_stats.link.memerr++;
  369. lwip_stats.link.drop++;
  370. #endif
  371. //
  372. // Enable Ethernet RX Interrupt.
  373. //
  374. EthernetIntEnable(ETH_BASE, ETH_INT_RX);
  375. }
  376. return (p);
  377. }
  378. int rt_hw_luminaryif_init(void)
  379. {
  380. rt_err_t result;
  381. unsigned long ulUser0, ulUser1;
  382. /* Enable and Reset the Ethernet Controller. */
  383. SysCtlPeripheralEnable(SYSCTL_PERIPH_ETH);
  384. SysCtlPeripheralReset(SYSCTL_PERIPH_ETH);
  385. /*
  386. Enable Port F for Ethernet LEDs.
  387. LED0 Bit 3 Output
  388. LED1 Bit 2 Output
  389. */
  390. SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
  391. /* GPIODirModeSet and GPIOPadConfigSet */
  392. GPIOPinTypeEthernetLED(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3);
  393. GPIOPinConfigure(GPIO_PF2_LED1);
  394. GPIOPinConfigure(GPIO_PF3_LED0);
  395. FlashUserSet(0x00371200, 0x00563412); /* OUI:00-12-37 (hex) Texas Instruments, only for test */
  396. /* Configure the hardware MAC address */
  397. FlashUserGet(&ulUser0, &ulUser1);
  398. if ((ulUser0 == 0xffffffff) || (ulUser1 == 0xffffffff))
  399. {
  400. rt_kprintf("Fatal error in geting MAC address\n");
  401. }
  402. /* init rt-thread device interface */
  403. luminaryif_dev_entry.parent.parent.init = luminaryif_init;
  404. luminaryif_dev_entry.parent.parent.open = luminaryif_open;
  405. luminaryif_dev_entry.parent.parent.close = luminaryif_close;
  406. luminaryif_dev_entry.parent.parent.read = luminaryif_read;
  407. luminaryif_dev_entry.parent.parent.write = luminaryif_write;
  408. luminaryif_dev_entry.parent.parent.control = luminaryif_control;
  409. luminaryif_dev_entry.parent.eth_rx = luminaryif_rx;
  410. luminaryif_dev_entry.parent.eth_tx = luminaryif_tx;
  411. /*
  412. Convert the 24/24 split MAC address from NV ram into a 32/16 split MAC
  413. address needed to program the hardware registers, then program the MAC
  414. address into the Ethernet Controller registers.
  415. */
  416. luminaryif_dev_entry.dev_addr[0] = ((ulUser0 >> 0) & 0xff);
  417. luminaryif_dev_entry.dev_addr[1] = ((ulUser0 >> 8) & 0xff);
  418. luminaryif_dev_entry.dev_addr[2] = ((ulUser0 >> 16) & 0xff);
  419. luminaryif_dev_entry.dev_addr[3] = ((ulUser1 >> 0) & 0xff);
  420. luminaryif_dev_entry.dev_addr[4] = ((ulUser1 >> 8) & 0xff);
  421. luminaryif_dev_entry.dev_addr[5] = ((ulUser1 >> 16) & 0xff);
  422. /* Program the hardware with it's MAC address (for filtering). */
  423. EthernetMACAddrSet(ETH_BASE, luminaryif_dev_entry.dev_addr);
  424. rt_sem_init(&tx_sem, "emac", 1, RT_IPC_FLAG_FIFO);
  425. result = eth_device_init(&(luminaryif_dev->parent), "E0");
  426. return result;
  427. }