luminaryif.c 13 KB

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