luminaryif.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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) rt_set_errno(-RT_ERROR);
  108. //
  109. // Disable Ethernet RX Interrupt.
  110. //
  111. EthernetIntDisable(ETH_BASE, ETH_INT_RX);
  112. }
  113. if(ulTemp & ETH_INT_TX)
  114. {
  115. /* A frame has been transmitted. */
  116. rt_sem_release(&tx_sem);
  117. }
  118. }
  119. /* control the interface */
  120. rt_err_t luminaryif_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  121. {
  122. switch(cmd)
  123. {
  124. case NIOCTL_GADDR:
  125. /* get mac address */
  126. if(args) rt_memcpy(args, luminaryif_dev_entry.dev_addr, 6);
  127. else return -RT_ERROR;
  128. break;
  129. default :
  130. break;
  131. }
  132. return RT_EOK;
  133. }
  134. /* Open the ethernet interface */
  135. rt_err_t luminaryif_open(rt_device_t dev, rt_uint16_t oflag)
  136. {
  137. return RT_EOK;
  138. }
  139. /* Close the interface */
  140. rt_err_t luminaryif_close(rt_device_t dev)
  141. {
  142. return RT_EOK;
  143. }
  144. /* Read */
  145. rt_size_t luminaryif_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  146. {
  147. rt_set_errno(-RT_ENOSYS);
  148. return 0;
  149. }
  150. /* Write */
  151. rt_size_t luminaryif_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  152. {
  153. rt_set_errno(-RT_ENOSYS);
  154. return 0;
  155. }
  156. //****************************************************************************
  157. //
  158. // Low-Level transmit routine. Should do the actual transmission of the
  159. // packet. The packet is contained in the pbuf that is passed to the function.
  160. // This pbuf might be chained.
  161. //
  162. //****************************************************************************
  163. rt_err_t luminaryif_tx(rt_device_t dev, struct pbuf *p)
  164. {
  165. int iBuf;
  166. unsigned char *pucBuf;
  167. unsigned long *pulBuf;
  168. struct pbuf *q;
  169. int iGather;
  170. unsigned long ulGather;
  171. unsigned char *pucGather;
  172. unsigned long ulTemp;
  173. /* lock tx operation */
  174. rt_sem_take(&tx_sem, RT_WAITING_FOREVER);
  175. //
  176. // Wait for space available in the TX FIFO.
  177. //
  178. while(!EthernetSpaceAvail(ETH_BASE))
  179. {
  180. }
  181. //
  182. // Fill in the first two bytes of the payload data (configured as padding
  183. // with ETH_PAD_SIZE = 2) with the total length of the payload data
  184. // (minus the Ethernet MAC layer header).
  185. //
  186. *((unsigned short *)(p->payload)) = p->tot_len - 16;
  187. //
  188. // Initialize the gather register.
  189. //
  190. iGather = 0;
  191. pucGather = (unsigned char *)&ulGather;
  192. ulGather = 0;
  193. //
  194. // Copy data from the pbuf(s) into the TX Fifo.
  195. //
  196. for(q = p; q != NULL; q = q->next)
  197. {
  198. //
  199. // Intialize a char pointer and index to the pbuf payload data.
  200. //
  201. pucBuf = (unsigned char *)q->payload;
  202. iBuf = 0;
  203. //
  204. // If the gather buffer has leftover data from a previous pbuf
  205. // in the chain, fill it up and write it to the Tx FIFO.
  206. //
  207. while((iBuf < q->len) && (iGather != 0))
  208. {
  209. //
  210. // Copy a byte from the pbuf into the gather buffer.
  211. //
  212. pucGather[iGather] = pucBuf[iBuf++];
  213. //
  214. // Increment the gather buffer index modulo 4.
  215. //
  216. iGather = ((iGather + 1) % 4);
  217. }
  218. //
  219. // If the gather index is 0 and the pbuf index is non-zero,
  220. // we have a gather buffer to write into the Tx FIFO.
  221. //
  222. if((iGather == 0) && (iBuf != 0))
  223. {
  224. HWREG(ETH_BASE + MAC_O_DATA) = ulGather;
  225. ulGather = 0;
  226. }
  227. //
  228. // Copy words of pbuf data into the Tx FIFO, but don't go past
  229. // the end of the pbuf.
  230. //
  231. if((iBuf % 4) != 0)
  232. {
  233. while((iBuf + 4) <= q->len)
  234. {
  235. ulTemp = (pucBuf[iBuf++] << 0);
  236. ulTemp |= (pucBuf[iBuf++] << 8);
  237. ulTemp |= (pucBuf[iBuf++] << 16);
  238. ulTemp |= (pucBuf[iBuf++] << 24);
  239. HWREG(ETH_BASE + MAC_O_DATA) = ulTemp;
  240. }
  241. }
  242. else
  243. {
  244. //
  245. // Initialze a long pointer into the pbuf for 32-bit access.
  246. //
  247. pulBuf = (unsigned long *)&pucBuf[iBuf];
  248. while((iBuf + 4) <= q->len)
  249. {
  250. HWREG(ETH_BASE + MAC_O_DATA) = *pulBuf++;
  251. iBuf += 4;
  252. }
  253. }
  254. //
  255. // Check if leftover data in the pbuf and save it in the gather
  256. // buffer for the next time.
  257. //
  258. while(iBuf < q->len)
  259. {
  260. //
  261. // Copy a byte from the pbuf into the gather buffer.
  262. //
  263. pucGather[iGather] = pucBuf[iBuf++];
  264. //
  265. // Increment the gather buffer index modulo 4.
  266. //
  267. iGather = ((iGather + 1) % 4);
  268. }
  269. }
  270. //
  271. // Send any leftover data to the FIFO.
  272. //
  273. HWREG(ETH_BASE + MAC_O_DATA) = ulGather;
  274. //
  275. // Wakeup the transmitter.
  276. //
  277. HWREG(ETH_BASE + MAC_O_TR) = MAC_TR_NEWTX;
  278. #if LINK_STATS
  279. lwip_stats.link.xmit++;
  280. #endif
  281. return(ERR_OK);
  282. }
  283. //*****************************************************************************
  284. //
  285. // Low-Level receive routine. Should allocate a pbuf and transfer the bytes
  286. // of the incoming packet from the interface into the pbuf.
  287. //
  288. //*****************************************************************************
  289. struct pbuf * luminaryif_rx(rt_device_t dev)
  290. {
  291. struct pbuf *p, *q;
  292. u16_t len;
  293. unsigned long ulTemp;
  294. int i;
  295. unsigned long *ptr;
  296. if(!EthernetPacketAvail(ETH_BASE))
  297. {
  298. //
  299. // Enable Ethernet RX Interrupt.
  300. //
  301. EthernetIntEnable(ETH_BASE, ETH_INT_RX);
  302. return(NULL);
  303. }
  304. //
  305. // Obtain the size of the packet and put it into the "len" variable.
  306. // Note: The length returned in the FIFO length position includes the
  307. // two bytes for the length + the 4 bytes for the FCS.
  308. //
  309. ulTemp = HWREG(ETH_BASE + MAC_O_DATA);
  310. len = ulTemp & 0xFFFF;
  311. //
  312. // We allocate a pbuf chain of pbufs from the pool.
  313. //
  314. p = pbuf_alloc(PBUF_LINK, len, PBUF_RAM);
  315. if(p != NULL)
  316. {
  317. //
  318. // Place the first word into the first pbuf location.
  319. //
  320. *(unsigned long *)p->payload = ulTemp;
  321. p->payload = (char *)(p->payload) + 4;
  322. p->len -= 4;
  323. //
  324. // Process all but the last buffer in the pbuf chain.
  325. //
  326. q = p;
  327. while(q != NULL)
  328. {
  329. //
  330. // Setup a byte pointer into the payload section of the pbuf.
  331. //
  332. ptr = q->payload;
  333. //
  334. // Read data from FIFO into the current pbuf
  335. // (assume pbuf length is modulo 4)
  336. //
  337. for(i = 0; i < q->len; i += 4)
  338. {
  339. *ptr++ = HWREG(ETH_BASE + MAC_O_DATA);
  340. }
  341. //
  342. // Link in the next pbuf in the chain.
  343. //
  344. q = q->next;
  345. }
  346. //
  347. // Restore the first pbuf parameters to their original values.
  348. //
  349. p->payload = (char *)(p->payload) - 4;
  350. p->len += 4;
  351. #if LINK_STATS
  352. lwip_stats.link.recv++;
  353. #endif
  354. }
  355. else
  356. {
  357. //
  358. // Just read all of the remaining data from the FIFO and dump it.
  359. //
  360. for(i = 4; i < len; i+=4)
  361. {
  362. ulTemp = HWREG(ETH_BASE + MAC_O_DATA);
  363. }
  364. #if LINK_STATS
  365. lwip_stats.link.memerr++;
  366. lwip_stats.link.drop++;
  367. #endif
  368. }
  369. return(p);
  370. }
  371. int rt_hw_luminaryif_init(void)
  372. {
  373. rt_err_t result;
  374. unsigned long ulUser0, ulUser1;
  375. /* Enable and Reset the Ethernet Controller. */
  376. SysCtlPeripheralEnable(SYSCTL_PERIPH_ETH);
  377. SysCtlPeripheralReset(SYSCTL_PERIPH_ETH);
  378. /*
  379. Enable Port F for Ethernet LEDs.
  380. LED0 Bit 3 Output
  381. LED1 Bit 2 Output
  382. */
  383. SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
  384. /* GPIODirModeSet and GPIOPadConfigSet */
  385. GPIOPinTypeEthernetLED(GPIO_PORTF_BASE, GPIO_PIN_2 | GPIO_PIN_3);
  386. GPIOPinConfigure(GPIO_PF2_LED1);
  387. GPIOPinConfigure(GPIO_PF3_LED0);
  388. FlashUserSet(0x00371200, 0x00563412); /* OUI:00-12-37 (hex) Texas Instruments, only for test */
  389. /* Configure the hardware MAC address */
  390. FlashUserGet(&ulUser0, &ulUser1);
  391. if((ulUser0 == 0xffffffff) || (ulUser1 == 0xffffffff))
  392. {
  393. rt_kprintf("Fatal error in geting MAC address\n");
  394. }
  395. /* init rt-thread device interface */
  396. luminaryif_dev_entry.parent.parent.init = luminaryif_init;
  397. luminaryif_dev_entry.parent.parent.open = luminaryif_open;
  398. luminaryif_dev_entry.parent.parent.close = luminaryif_close;
  399. luminaryif_dev_entry.parent.parent.read = luminaryif_read;
  400. luminaryif_dev_entry.parent.parent.write = luminaryif_write;
  401. luminaryif_dev_entry.parent.parent.control = luminaryif_control;
  402. luminaryif_dev_entry.parent.eth_rx = luminaryif_rx;
  403. luminaryif_dev_entry.parent.eth_tx = luminaryif_tx;
  404. /*
  405. Convert the 24/24 split MAC address from NV ram into a 32/16 split MAC
  406. address needed to program the hardware registers, then program the MAC
  407. address into the Ethernet Controller registers.
  408. */
  409. luminaryif_dev_entry.dev_addr[0] = ((ulUser0 >> 0) & 0xff);
  410. luminaryif_dev_entry.dev_addr[1] = ((ulUser0 >> 8) & 0xff);
  411. luminaryif_dev_entry.dev_addr[2] = ((ulUser0 >> 16) & 0xff);
  412. luminaryif_dev_entry.dev_addr[3] = ((ulUser1 >> 0) & 0xff);
  413. luminaryif_dev_entry.dev_addr[4] = ((ulUser1 >> 8) & 0xff);
  414. luminaryif_dev_entry.dev_addr[5] = ((ulUser1 >> 16) & 0xff);
  415. /* Program the hardware with it's MAC address (for filtering). */
  416. EthernetMACAddrSet(ETH_BASE, luminaryif_dev_entry.dev_addr);
  417. rt_sem_init(&tx_sem, "emac", 1, RT_IPC_FLAG_FIFO);
  418. result = eth_device_init(&(luminaryif_dev->parent), "E0");
  419. return result;
  420. }