enc28j60.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. #include "enc28j60.h"
  2. #include <netif/ethernetif.h>
  3. #include <stm32f10x.h>
  4. #include <stm32f10x_spi.h>
  5. #define MAX_ADDR_LEN 6
  6. // #define CSACTIVE GPIO_ResetBits(GPIOB, GPIO_Pin_12);
  7. // #define CSPASSIVE GPIO_SetBits(GPIOB, GPIO_Pin_12);
  8. #define CSACTIVE GPIOB->BRR = GPIO_Pin_12;
  9. #define CSPASSIVE GPIOB->BSRR = GPIO_Pin_12;
  10. struct net_device
  11. {
  12. /* inherit from ethernet device */
  13. struct eth_device parent;
  14. /* interface address info. */
  15. rt_uint8_t dev_addr[MAX_ADDR_LEN]; /* hw address */
  16. };
  17. static struct net_device enc28j60_dev_entry;
  18. static struct net_device *enc28j60_dev =&enc28j60_dev_entry;
  19. static rt_uint8_t Enc28j60Bank;
  20. static rt_uint16_t NextPacketPtr;
  21. static struct rt_semaphore tx_sem;
  22. void _delay_us(rt_uint32_t us)
  23. {
  24. rt_uint32_t len;
  25. for (;us > 0; us --)
  26. for (len = 0; len < 20; len++ );
  27. }
  28. void delay_ms(rt_uint32_t ms)
  29. {
  30. rt_uint32_t len;
  31. for (;ms > 0; ms --)
  32. for (len = 0; len < 100; len++ );
  33. }
  34. rt_uint8_t spi_read_op(rt_uint8_t op, rt_uint8_t address)
  35. {
  36. int temp=0;
  37. CSACTIVE;
  38. SPI_I2S_SendData(SPI2, (op | (address & ADDR_MASK)));
  39. while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY)==SET);
  40. SPI_I2S_ReceiveData(SPI2);
  41. SPI_I2S_SendData(SPI2, 0x00);
  42. while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY)==SET);
  43. // do dummy read if needed (for mac and mii, see datasheet page 29)
  44. if(address & 0x80)
  45. {
  46. SPI_I2S_ReceiveData(SPI2);
  47. SPI_I2S_SendData(SPI2, 0x00);
  48. while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY)==SET);
  49. }
  50. // release CS
  51. temp=SPI_I2S_ReceiveData(SPI2);
  52. // for(t=0;t<20;t++);
  53. CSPASSIVE;
  54. return (temp);
  55. }
  56. void spi_write_op(rt_uint8_t op, rt_uint8_t address, rt_uint8_t data)
  57. {
  58. rt_uint32_t level;
  59. level = rt_hw_interrupt_disable();
  60. CSACTIVE;
  61. SPI_I2S_SendData(SPI2, op | (address & ADDR_MASK));
  62. while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY)==SET);
  63. SPI_I2S_SendData(SPI2,data);
  64. while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY)==SET);
  65. CSPASSIVE;
  66. rt_hw_interrupt_enable(level);
  67. }
  68. void enc28j60_set_bank(rt_uint8_t address)
  69. {
  70. // set the bank (if needed)
  71. if((address & BANK_MASK) != Enc28j60Bank)
  72. {
  73. // set the bank
  74. spi_write_op(ENC28J60_BIT_FIELD_CLR, ECON1, (ECON1_BSEL1|ECON1_BSEL0));
  75. spi_write_op(ENC28J60_BIT_FIELD_SET, ECON1, (address & BANK_MASK)>>5);
  76. Enc28j60Bank = (address & BANK_MASK);
  77. }
  78. }
  79. rt_uint8_t spi_read(rt_uint8_t address)
  80. {
  81. // set the bank
  82. enc28j60_set_bank(address);
  83. // do the read
  84. return spi_read_op(ENC28J60_READ_CTRL_REG, address);
  85. }
  86. void spi_read_buffer(rt_uint8_t* data, rt_size_t len)
  87. {
  88. CSACTIVE;
  89. SPI_I2S_SendData(SPI2,ENC28J60_READ_BUF_MEM);
  90. while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY)==SET);
  91. SPI_I2S_ReceiveData(SPI2);
  92. while(len)
  93. {
  94. len--;
  95. SPI_I2S_SendData(SPI2,0x00) ;
  96. while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY)==SET);
  97. *data= SPI_I2S_ReceiveData(SPI2);
  98. data++;
  99. }
  100. CSPASSIVE;
  101. }
  102. void spi_write(rt_uint8_t address, rt_uint8_t data)
  103. {
  104. // set the bank
  105. enc28j60_set_bank(address);
  106. // do the write
  107. spi_write_op(ENC28J60_WRITE_CTRL_REG, address, data);
  108. }
  109. void enc28j60_phy_write(rt_uint8_t address, rt_uint16_t data)
  110. {
  111. // set the PHY register address
  112. spi_write(MIREGADR, address);
  113. // write the PHY data
  114. spi_write(MIWRL, data);
  115. spi_write(MIWRH, data>>8);
  116. // wait until the PHY write completes
  117. while(spi_read(MISTAT) & MISTAT_BUSY)
  118. {
  119. _delay_us(15);
  120. }
  121. }
  122. // read upper 8 bits
  123. rt_uint16_t enc28j60_phy_read(rt_uint8_t address)
  124. {
  125. // Set the right address and start the register read operation
  126. spi_write(MIREGADR, address);
  127. spi_write(MICMD, MICMD_MIIRD);
  128. _delay_us(15);
  129. // wait until the PHY read completes
  130. while(spi_read(MISTAT) & MISTAT_BUSY);
  131. // reset reading bit
  132. spi_write(MICMD, 0x00);
  133. return (spi_read(MIRDH));
  134. }
  135. void enc28j60_clkout(rt_uint8_t clk)
  136. {
  137. //setup clkout: 2 is 12.5MHz:
  138. spi_write(ECOCON, clk & 0x7);
  139. }
  140. /*
  141. * Access the PHY to determine link status
  142. */
  143. static rt_bool_t enc28j60_check_link_status()
  144. {
  145. rt_uint16_t reg;
  146. int duplex;
  147. reg = enc28j60_phy_read(PHSTAT2);
  148. duplex = reg & PHSTAT2_DPXSTAT;
  149. if (reg & PHSTAT2_LSTAT)
  150. {
  151. /* on */
  152. return RT_TRUE;
  153. }
  154. else
  155. {
  156. /* off */
  157. return RT_FALSE;
  158. }
  159. }
  160. #ifdef RT_USING_FINSH
  161. #include <finsh.h>
  162. /*
  163. * Debug routine to dump useful register contents
  164. */
  165. static void enc28j60(void)
  166. {
  167. rt_kprintf("-- enc28j60 registers:\n");
  168. rt_kprintf("HwRevID: 0x%02x\n", spi_read(EREVID));
  169. rt_kprintf("Cntrl: ECON1 ECON2 ESTAT EIR EIE\n");
  170. rt_kprintf(" 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",spi_read(ECON1), spi_read(ECON2), spi_read(ESTAT), spi_read(EIR), spi_read(EIE));
  171. rt_kprintf("MAC : MACON1 MACON3 MACON4\n");
  172. rt_kprintf(" 0x%02x 0x%02x 0x%02x\n", spi_read(MACON1), spi_read(MACON3), spi_read(MACON4));
  173. rt_kprintf("Rx : ERXST ERXND ERXWRPT ERXRDPT ERXFCON EPKTCNT MAMXFL\n");
  174. rt_kprintf(" 0x%04x 0x%04x 0x%04x 0x%04x ",
  175. (spi_read(ERXSTH) << 8) | spi_read(ERXSTL),
  176. (spi_read(ERXNDH) << 8) | spi_read(ERXNDL),
  177. (spi_read(ERXWRPTH) << 8) | spi_read(ERXWRPTL),
  178. (spi_read(ERXRDPTH) << 8) | spi_read(ERXRDPTL));
  179. rt_kprintf("0x%02x 0x%02x 0x%04x\n", spi_read(ERXFCON), spi_read(EPKTCNT),
  180. (spi_read(MAMXFLH) << 8) | spi_read(MAMXFLL));
  181. rt_kprintf("Tx : ETXST ETXND MACLCON1 MACLCON2 MAPHSUP\n");
  182. rt_kprintf(" 0x%04x 0x%04x 0x%02x 0x%02x 0x%02x\n",
  183. (spi_read(ETXSTH) << 8) | spi_read(ETXSTL),
  184. (spi_read(ETXNDH) << 8) | spi_read(ETXNDL),
  185. spi_read(MACLCON1), spi_read(MACLCON2), spi_read(MAPHSUP));
  186. }
  187. FINSH_FUNCTION_EXPORT(enc28j60, dump enc28j60 registers);
  188. #endif
  189. /*
  190. * RX handler
  191. * ignore PKTIF because is unreliable! (look at the errata datasheet)
  192. * check EPKTCNT is the suggested workaround.
  193. * We don't need to clear interrupt flag, automatically done when
  194. * enc28j60_hw_rx() decrements the packet counter.
  195. */
  196. void enc28j60_isr()
  197. {
  198. /* Variable definitions can be made now. */
  199. volatile rt_uint32_t eir, pk_counter;
  200. volatile rt_bool_t rx_activiated;
  201. rx_activiated = RT_FALSE;
  202. /* get EIR */
  203. eir = spi_read(EIR);
  204. // rt_kprintf("eir: 0x%08x\n", eir);
  205. do
  206. {
  207. /* errata #4, PKTIF does not reliable */
  208. pk_counter = spi_read(EPKTCNT);
  209. if (pk_counter)
  210. {
  211. rt_err_t result;
  212. /* a frame has been received */
  213. result = eth_device_ready((struct eth_device*)&(enc28j60_dev->parent));
  214. RT_ASSERT(result == RT_EOK);
  215. // switch to bank 0
  216. enc28j60_set_bank(EIE);
  217. // disable rx interrutps
  218. spi_write_op(ENC28J60_BIT_FIELD_CLR, EIE, EIE_PKTIE);
  219. }
  220. /* clear PKTIF */
  221. if (eir & EIR_PKTIF)
  222. {
  223. enc28j60_set_bank(EIR);
  224. spi_write_op(ENC28J60_BIT_FIELD_CLR, EIR, EIR_PKTIF);
  225. rx_activiated = RT_TRUE;
  226. }
  227. /* clear DMAIF */
  228. if (eir & EIR_DMAIF)
  229. {
  230. enc28j60_set_bank(EIR);
  231. spi_write_op(ENC28J60_BIT_FIELD_CLR, EIR, EIR_DMAIF);
  232. }
  233. /* LINK changed handler */
  234. if ( eir & EIR_LINKIF)
  235. {
  236. enc28j60_check_link_status();
  237. /* read PHIR to clear the flag */
  238. enc28j60_phy_read(PHIR);
  239. enc28j60_set_bank(EIR);
  240. spi_write_op(ENC28J60_BIT_FIELD_CLR, EIR, EIR_LINKIF);
  241. }
  242. if (eir & EIR_TXIF)
  243. {
  244. enc28j60_set_bank(EIR);
  245. spi_write_op(ENC28J60_BIT_FIELD_CLR, EIR, EIR_TXIF);
  246. /* A frame has been transmitted. */
  247. rt_sem_release(&tx_sem);
  248. }
  249. /* TX Error handler */
  250. if ((eir & EIR_TXERIF) != 0)
  251. {
  252. spi_write_op(ENC28J60_BIT_FIELD_CLR, EIR, EIR_TXERIF);
  253. }
  254. eir = spi_read(EIR);
  255. // rt_kprintf("inner eir: 0x%08x\n", eir);
  256. } while ((rx_activiated != RT_TRUE && eir != 0));
  257. }
  258. /* RT-Thread Device Interface */
  259. /* initialize the interface */
  260. rt_err_t enc28j60_init(rt_device_t dev)
  261. {
  262. CSPASSIVE;
  263. // perform system reset
  264. spi_write_op(ENC28J60_SOFT_RESET, 0, ENC28J60_SOFT_RESET);
  265. delay_ms(50);
  266. NextPacketPtr = RXSTART_INIT;
  267. // Rx start
  268. spi_write(ERXSTL, RXSTART_INIT&0xFF);
  269. spi_write(ERXSTH, RXSTART_INIT>>8);
  270. // set receive pointer address
  271. spi_write(ERXRDPTL, RXSTOP_INIT&0xFF);
  272. spi_write(ERXRDPTH, RXSTOP_INIT>>8);
  273. // RX end
  274. spi_write(ERXNDL, RXSTOP_INIT&0xFF);
  275. spi_write(ERXNDH, RXSTOP_INIT>>8);
  276. // TX start
  277. spi_write(ETXSTL, TXSTART_INIT&0xFF);
  278. spi_write(ETXSTH, TXSTART_INIT>>8);
  279. // set transmission pointer address
  280. spi_write(EWRPTL, TXSTART_INIT&0xFF);
  281. spi_write(EWRPTH, TXSTART_INIT>>8);
  282. // TX end
  283. spi_write(ETXNDL, TXSTOP_INIT&0xFF);
  284. spi_write(ETXNDH, TXSTOP_INIT>>8);
  285. // do bank 1 stuff, packet filter:
  286. // For broadcast packets we allow only ARP packtets
  287. // All other packets should be unicast only for our mac (MAADR)
  288. //
  289. // The pattern to match on is therefore
  290. // Type ETH.DST
  291. // ARP BROADCAST
  292. // 06 08 -- ff ff ff ff ff ff -> ip checksum for theses bytes=f7f9
  293. // in binary these poitions are:11 0000 0011 1111
  294. // This is hex 303F->EPMM0=0x3f,EPMM1=0x30
  295. spi_write(ERXFCON, ERXFCON_UCEN|ERXFCON_CRCEN|ERXFCON_BCEN);
  296. // do bank 2 stuff
  297. // enable MAC receive
  298. spi_write(MACON1, MACON1_MARXEN|MACON1_TXPAUS|MACON1_RXPAUS);
  299. // enable automatic padding to 60bytes and CRC operations
  300. // spi_write_op(ENC28J60_BIT_FIELD_SET, MACON3, MACON3_PADCFG0|MACON3_TXCRCEN|MACON3_FRMLNEN);
  301. spi_write_op(ENC28J60_BIT_FIELD_SET, MACON3, MACON3_PADCFG0 | MACON3_TXCRCEN | MACON3_FRMLNEN | MACON3_FULDPX);
  302. // bring MAC out of reset
  303. // set inter-frame gap (back-to-back)
  304. // spi_write(MABBIPG, 0x12);
  305. spi_write(MABBIPG, 0x15);
  306. spi_write(MACON4, MACON4_DEFER);
  307. spi_write(MACLCON2, 63);
  308. // set inter-frame gap (non-back-to-back)
  309. spi_write(MAIPGL, 0x12);
  310. spi_write(MAIPGH, 0x0C);
  311. // Set the maximum packet size which the controller will accept
  312. // Do not send packets longer than MAX_FRAMELEN:
  313. spi_write(MAMXFLL, MAX_FRAMELEN&0xFF);
  314. spi_write(MAMXFLH, MAX_FRAMELEN>>8);
  315. // do bank 3 stuff
  316. // write MAC address
  317. // NOTE: MAC address in ENC28J60 is byte-backward
  318. spi_write(MAADR0, enc28j60_dev->dev_addr[5]);
  319. spi_write(MAADR1, enc28j60_dev->dev_addr[4]);
  320. spi_write(MAADR2, enc28j60_dev->dev_addr[3]);
  321. spi_write(MAADR3, enc28j60_dev->dev_addr[2]);
  322. spi_write(MAADR4, enc28j60_dev->dev_addr[1]);
  323. spi_write(MAADR5, enc28j60_dev->dev_addr[0]);
  324. /* output off */
  325. spi_write(ECOCON, 0x00);
  326. // enc28j60_phy_write(PHCON1, 0x00);
  327. enc28j60_phy_write(PHCON1, PHCON1_PDPXMD); // full duplex
  328. // no loopback of transmitted frames
  329. enc28j60_phy_write(PHCON2, PHCON2_HDLDIS);
  330. enc28j60_set_bank(ECON2);
  331. spi_write_op(ENC28J60_BIT_FIELD_SET, ECON2, ECON2_AUTOINC);
  332. // switch to bank 0
  333. enc28j60_set_bank(ECON1);
  334. // enable interrutps
  335. spi_write_op(ENC28J60_BIT_FIELD_SET, EIE, EIE_INTIE|EIE_PKTIE|EIR_TXIF);
  336. // enable packet reception
  337. spi_write_op(ENC28J60_BIT_FIELD_SET, ECON1, ECON1_RXEN);
  338. /* clock out */
  339. // enc28j60_clkout(2);
  340. enc28j60_phy_write(PHLCON, 0xD76); //0x476
  341. delay_ms(20);
  342. // rt_kprintf("enc28j60 init ok!\n");
  343. return RT_EOK;
  344. }
  345. /* control the interface */
  346. rt_err_t enc28j60_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  347. {
  348. switch(cmd)
  349. {
  350. case NIOCTL_GADDR:
  351. /* get mac address */
  352. if(args) rt_memcpy(args, enc28j60_dev_entry.dev_addr, 6);
  353. else return -RT_ERROR;
  354. break;
  355. default :
  356. break;
  357. }
  358. return RT_EOK;
  359. }
  360. /* Open the ethernet interface */
  361. rt_err_t enc28j60_open(rt_device_t dev, rt_uint16_t oflag)
  362. {
  363. return RT_EOK;
  364. }
  365. /* Close the interface */
  366. rt_err_t enc28j60_close(rt_device_t dev)
  367. {
  368. return RT_EOK;
  369. }
  370. /* Read */
  371. rt_size_t enc28j60_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  372. {
  373. rt_set_errno(-RT_ENOSYS);
  374. return 0;
  375. }
  376. /* Write */
  377. rt_size_t enc28j60_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  378. {
  379. rt_set_errno(-RT_ENOSYS);
  380. return 0;
  381. }
  382. /* ethernet device interface */
  383. /*
  384. * Transmit packet.
  385. */
  386. rt_err_t enc28j60_tx( rt_device_t dev, struct pbuf* p)
  387. {
  388. struct pbuf* q;
  389. rt_uint32_t len;
  390. rt_uint8_t* ptr;
  391. // rt_kprintf("tx pbuf: 0x%08x, total len %d\n", p, p->tot_len);
  392. /* lock tx operation */
  393. rt_sem_take(&tx_sem, RT_WAITING_FOREVER);
  394. // Set the write pointer to start of transmit buffer area
  395. spi_write(EWRPTL, TXSTART_INIT&0xFF);
  396. spi_write(EWRPTH, TXSTART_INIT>>8);
  397. // Set the TXND pointer to correspond to the packet size given
  398. spi_write(ETXNDL, (TXSTART_INIT+ p->tot_len + 1)&0xFF);
  399. spi_write(ETXNDH, (TXSTART_INIT+ p->tot_len + 1)>>8);
  400. // write per-packet control byte (0x00 means use macon3 settings)
  401. spi_write_op(ENC28J60_WRITE_BUF_MEM, 0, 0x00);
  402. for (q = p; q != NULL; q = q->next)
  403. {
  404. CSACTIVE;
  405. SPI_I2S_SendData(SPI2, ENC28J60_WRITE_BUF_MEM);
  406. while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY)==SET);
  407. len = q->len;
  408. ptr = q->payload;
  409. while(len)
  410. {
  411. SPI_I2S_SendData(SPI2,*ptr) ;
  412. while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY)==SET);;
  413. ptr++;
  414. len--;
  415. }
  416. CSPASSIVE;
  417. }
  418. // send the contents of the transmit buffer onto the network
  419. spi_write_op(ENC28J60_BIT_FIELD_SET, ECON1, ECON1_TXRTS);
  420. // Reset the transmit logic problem. See Rev. B4 Silicon Errata point 12.
  421. if( (spi_read(EIR) & EIR_TXERIF) )
  422. {
  423. spi_write_op(ENC28J60_BIT_FIELD_CLR, ECON1, ECON1_TXRTS);
  424. }
  425. //rt_kprintf("tx ok\n");
  426. return RT_EOK;
  427. }
  428. struct pbuf *enc28j60_rx(rt_device_t dev)
  429. {
  430. struct pbuf* p;
  431. rt_uint32_t len;
  432. rt_uint16_t rxstat;
  433. rt_uint32_t pk_counter;
  434. p = RT_NULL;
  435. pk_counter = spi_read(EPKTCNT);
  436. if (pk_counter)
  437. {
  438. // Set the read pointer to the start of the received packet
  439. spi_write(ERDPTL, (NextPacketPtr));
  440. spi_write(ERDPTH, (NextPacketPtr)>>8);
  441. // read the next packet pointer
  442. NextPacketPtr = spi_read_op(ENC28J60_READ_BUF_MEM, 0);
  443. NextPacketPtr |= spi_read_op(ENC28J60_READ_BUF_MEM, 0)<<8;
  444. // read the packet length (see datasheet page 43)
  445. len = spi_read_op(ENC28J60_READ_BUF_MEM, 0); //0x54
  446. len |= spi_read_op(ENC28J60_READ_BUF_MEM, 0) <<8; //5554
  447. len-=4; //remove the CRC count
  448. // read the receive status (see datasheet page 43)
  449. rxstat = spi_read_op(ENC28J60_READ_BUF_MEM, 0);
  450. rxstat |= ((rt_uint16_t)spi_read_op(ENC28J60_READ_BUF_MEM, 0))<<8;
  451. // check CRC and symbol errors (see datasheet page 44, table 7-3):
  452. // The ERXFCON.CRCEN is set by default. Normally we should not
  453. // need to check this.
  454. if ((rxstat & 0x80)==0)
  455. {
  456. // invalid
  457. len=0;
  458. }
  459. else
  460. {
  461. /* allocation pbuf */
  462. p = pbuf_alloc(PBUF_LINK, len, PBUF_RAM);
  463. if (p != RT_NULL)
  464. {
  465. rt_uint8_t* data;
  466. struct pbuf* q;
  467. for (q = p; q != RT_NULL; q= q->next)
  468. {
  469. data = q->payload;
  470. len = q->len;
  471. CSACTIVE;
  472. SPI_I2S_SendData(SPI2,ENC28J60_READ_BUF_MEM);
  473. while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY)==SET);
  474. SPI_I2S_ReceiveData(SPI2);
  475. while(len)
  476. {
  477. len--;
  478. SPI_I2S_SendData(SPI2,0x00) ;
  479. while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY)==SET);
  480. *data= SPI_I2S_ReceiveData(SPI2);
  481. data++;
  482. }
  483. CSPASSIVE;
  484. }
  485. }
  486. }
  487. // Move the RX read pointer to the start of the next received packet
  488. // This frees the memory we just read out
  489. spi_write(ERXRDPTL, (NextPacketPtr));
  490. spi_write(ERXRDPTH, (NextPacketPtr)>>8);
  491. // decrement the packet counter indicate we are done with this packet
  492. spi_write_op(ENC28J60_BIT_FIELD_SET, ECON2, ECON2_PKTDEC);
  493. }
  494. else
  495. {
  496. rt_uint32_t level;
  497. /* lock enc28j60 */
  498. level = rt_hw_interrupt_disable();
  499. // switch to bank 0
  500. enc28j60_set_bank(EIE);
  501. // enable interrutps
  502. spi_write_op(ENC28J60_BIT_FIELD_SET, EIE, EIE_PKTIE);
  503. // switch to bank 0
  504. enc28j60_set_bank(ECON1);
  505. // enable packet reception
  506. spi_write_op(ENC28J60_BIT_FIELD_SET, ECON1, ECON1_RXEN);
  507. /* enable interrupt */
  508. rt_hw_interrupt_enable(level);
  509. }
  510. return p;
  511. }
  512. static void RCC_Configuration(void)
  513. {
  514. /* enable spi2 clock */
  515. RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
  516. /* enable gpiob port clock */
  517. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
  518. }
  519. static void NVIC_Configuration(void)
  520. {
  521. NVIC_InitTypeDef NVIC_InitStructure;
  522. /* Configure one bit for preemption priority */
  523. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  524. /* Enable the EXTI0 Interrupt */
  525. NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
  526. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  527. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  528. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  529. NVIC_Init(&NVIC_InitStructure);
  530. }
  531. static void GPIO_Configuration()
  532. {
  533. GPIO_InitTypeDef GPIO_InitStructure;
  534. EXTI_InitTypeDef EXTI_InitStructure;
  535. /* configure PB0 as external interrupt */
  536. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  537. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  538. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  539. GPIO_Init(GPIOB, &GPIO_InitStructure);
  540. /* Configure SPI2 pins: SCK, MISO and MOSI ----------------------------*/
  541. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  542. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  543. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  544. GPIO_Init(GPIOB, &GPIO_InitStructure);
  545. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  546. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  547. GPIO_Init(GPIOB, &GPIO_InitStructure);
  548. /* Connect ENC28J60 EXTI Line to GPIOB Pin 0 */
  549. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource0);
  550. /* Configure ENC28J60 EXTI Line to generate an interrupt on falling edge */
  551. EXTI_InitStructure.EXTI_Line = EXTI_Line0;
  552. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  553. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  554. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  555. EXTI_Init(&EXTI_InitStructure);
  556. /* Clear the Key Button EXTI line pending bit */
  557. EXTI_ClearITPendingBit(EXTI_Line0);
  558. }
  559. static void SetupSPI (void)
  560. {
  561. SPI_InitTypeDef SPI_InitStructure;
  562. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  563. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  564. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  565. SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  566. SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  567. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  568. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
  569. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  570. SPI_InitStructure.SPI_CRCPolynomial = 7;
  571. SPI_Init(SPI2, &SPI_InitStructure);
  572. SPI_Cmd(SPI2, ENABLE);
  573. }
  574. static rt_timer_t enc28j60_timer;
  575. void rt_hw_enc28j60_timeout(void* parameter)
  576. {
  577. // switch to bank 0
  578. enc28j60_set_bank(EIE);
  579. // enable interrutps
  580. spi_write_op(ENC28J60_BIT_FIELD_SET, EIE, EIE_PKTIE);
  581. // switch to bank 0
  582. enc28j60_set_bank(ECON1);
  583. // enable packet reception
  584. spi_write_op(ENC28J60_BIT_FIELD_SET, ECON1, ECON1_RXEN);
  585. enc28j60_isr();
  586. }
  587. int rt_hw_enc28j60_init()
  588. {
  589. rt_err_t result;
  590. /* configuration PB5 as INT */
  591. RCC_Configuration();
  592. NVIC_Configuration();
  593. GPIO_Configuration();
  594. SetupSPI();
  595. /* init rt-thread device interface */
  596. enc28j60_dev_entry.parent.parent.init = enc28j60_init;
  597. enc28j60_dev_entry.parent.parent.open = enc28j60_open;
  598. enc28j60_dev_entry.parent.parent.close = enc28j60_close;
  599. enc28j60_dev_entry.parent.parent.read = enc28j60_read;
  600. enc28j60_dev_entry.parent.parent.write = enc28j60_write;
  601. enc28j60_dev_entry.parent.parent.control = enc28j60_control;
  602. enc28j60_dev_entry.parent.eth_rx = enc28j60_rx;
  603. enc28j60_dev_entry.parent.eth_tx = enc28j60_tx;
  604. /* Update MAC address */
  605. enc28j60_dev_entry.dev_addr[0] = 0x1e;
  606. enc28j60_dev_entry.dev_addr[1] = 0x30;
  607. enc28j60_dev_entry.dev_addr[2] = 0x6c;
  608. enc28j60_dev_entry.dev_addr[3] = 0xa2;
  609. enc28j60_dev_entry.dev_addr[4] = 0x45;
  610. enc28j60_dev_entry.dev_addr[5] = 0x5e;
  611. rt_sem_init(&tx_sem, "emac", 1, RT_IPC_FLAG_FIFO);
  612. result = eth_device_init(&(enc28j60_dev->parent), "E0");
  613. /* workaround for enc28j60 interrupt */
  614. enc28j60_timer = rt_timer_create("etimer",
  615. rt_hw_enc28j60_timeout, RT_NULL,
  616. 50, RT_TIMER_FLAG_PERIODIC);
  617. if (enc28j60_timer != RT_NULL)
  618. rt_timer_start(enc28j60_timer);
  619. return RT_EOK;
  620. }