stm32_emac.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. /*
  2. * Copyright (C) 2012 by Rittal GmbH & Co. KG. All rights reserved.
  3. * Copyright (C) 2006 by egnite Software GmbH. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the copyright holders nor the names of
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  22. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  25. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  26. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  28. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * For additional information see http://www.ethernut.de/
  32. *
  33. */
  34. #include <cfg/arch.h>
  35. #include <cfg/os.h>
  36. #include <cfg/clock.h>
  37. #include <cfg/phycfg.h>
  38. #include <dev/board.h>
  39. #include <string.h>
  40. #include <sys/atom.h>
  41. #include <sys/heap.h>
  42. #include <sys/thread.h>
  43. #include <sys/event.h>
  44. #include <sys/timer.h>
  45. #include <sys/confnet.h>
  46. #include <netinet/if_ether.h>
  47. #include <net/ether.h>
  48. #include <net/if_var.h>
  49. #include <dev/irqreg.h>
  50. #include <dev/gpio.h>
  51. #include <dev/phy.h>
  52. #include <dev/stm32_emac.h>
  53. #include <arch/cm3/stm/stm32xxxx.h>
  54. #include <arch/cm3/stm/stm32_gpio.h>
  55. /* WARNING: Variadic macros are C99 and may fail with C89 compilers. */
  56. #ifdef NUTDEBUG
  57. #include <stdio.h>
  58. #define EMPRINTF(args,...) printf(args,##__VA_ARGS__)
  59. #else
  60. #define EMPRINTF(args,...)
  61. #endif
  62. #if SYSCLK_FREQ < 35000001L
  63. #define ETH_MACMIIAR_CR_Div ETH_MACMIIAR_CR_Div16
  64. #elif SYSCLK_FREQ < 60000001L
  65. #define ETH_MACMIIAR_CR_Div ETH_MACMIIAR_CR_Div26
  66. #elif SYSCLK_FREQ < 100000001L
  67. #define ETH_MACMIIAR_CR_Div ETH_MACMIIAR_CR_Div42
  68. #elif SYSCLK_FREQ < 150000001L
  69. #define ETH_MACMIIAR_CR_Div ETH_MACMIIAR_CR_Div62
  70. #elif SYSCLK_FREQ < 168000001L
  71. #define ETH_MACMIIAR_CR_Div ETH_MACMIIAR_CR_Div102
  72. #endif
  73. /*
  74. * For the benefit of EMC the GPIO is run at the lowest speed
  75. * required to operate. For RMII this is 50 MHz for MII 25 MHz.
  76. */
  77. #ifdef PHY_MODE_RMII
  78. #define EMAC_GPIO_SPEED GPIO_CFG_SPEED_FAST
  79. #else
  80. #define EMAC_GPIO_SPEED GPIO_CFG_SPEED_MED
  81. #endif
  82. #ifndef NUT_THREAD_NICRXSTACK
  83. #define NUT_THREAD_NICRXSTACK 1024
  84. #endif
  85. #ifndef EMAC_RX_BUFFERS
  86. #define EMAC_RX_BUFFERS 32
  87. #endif
  88. #define EMAC_RX_BUFSIZ 128
  89. #define EMAC_TX_BUFFERS 2
  90. #ifndef EMAC_TX_BUFSIZ
  91. #define EMAC_TX_BUFSIZ 1536
  92. #endif
  93. #ifndef NIC_PHY_ADDR
  94. #define NIC_PHY_ADDR 1
  95. #endif
  96. /*
  97. * EMAC default GPIO definitions for alternate
  98. * functions which can be assigned to different ports on
  99. * STM32F2/F4 parts.
  100. */
  101. #ifndef EMAC_CRS_PORT
  102. #define EMAC_CRS_PORT NUTGPIO_PORTA
  103. #define EMAC_CRS_PIN 0
  104. #endif
  105. #ifndef EMAC_COL_PORT
  106. #define EMAC_COL_PORT NUTGPIO_PORTA
  107. #endif
  108. #ifndef EMAC_RXD2_PORT
  109. #define EMAC_RXD2_PORT NUTGPIO_PORTB
  110. #define EMAC_RXD2_PIN 0
  111. #endif
  112. #ifndef EMAC_RXD3_PORT
  113. #define EMAC_RXD3_PORT NUTGPIO_PORTB
  114. #define EMAC_RXD3_PIN 1
  115. #endif
  116. #ifndef EMAC_RX_ER_PORT
  117. #define EMAC_RX_ER_PORT NUTGPIO_PORTB
  118. #endif
  119. #ifndef EMAC_TXEN_PORT
  120. #define EMAC_TXEN_PORT NUTGPIO_PORTB
  121. #endif
  122. #ifndef EMAC_TXD0_PORT
  123. #define EMAC_TXD0_PORT NUTGPIO_PORTB
  124. #define EMAC_TXD0_PIN 12
  125. #endif
  126. #ifndef EMAC_TXD1_PORT
  127. #define EMAC_TXD1_PORT NUTGPIO_PORTB
  128. #define EMAC_TXD1_PIN 13
  129. #endif
  130. #ifndef EMAC_PPS_OUT_PORT
  131. #undef EMAC_PPS_OUT_PORT /* Default is not to use a PPS signal */
  132. #undef EMAC_PPS_OUT_PIN
  133. #endif
  134. /*!
  135. * \brief Network interface controller information structure.
  136. */
  137. struct _EMACINFO {
  138. #ifdef NUT_PERFMON
  139. uint32_t ni_rx_packets; /*!< Number of packets received. */
  140. uint32_t ni_tx_packets; /*!< Number of packets sent. */
  141. uint32_t ni_overruns; /*!< Number of packet overruns. */
  142. uint32_t ni_rx_frame_errors; /*!< Number of frame errors. */
  143. uint32_t ni_rx_crc_errors; /*!< Number of CRC errors. */
  144. uint32_t ni_rx_missed_errors; /*!< Number of missed packets. */
  145. #endif
  146. HANDLE volatile ni_rx_rdy; /*!< Receiver event queue. */
  147. HANDLE volatile ni_tx_rdy; /*!< Transmitter event queue. */
  148. HANDLE ni_mutex; /*!< Access mutex semaphore. */
  149. volatile int ni_tx_queued; /*!< Number of packets in transmission queue. */
  150. volatile int ni_tx_quelen; /*!< Number of bytes in transmission queue not sent. */
  151. volatile int ni_insane; /*!< Set by error detection. */
  152. int ni_iomode; /*!< 8 or 16 bit access. 32 bit is not supported. */
  153. };
  154. /*!
  155. * \brief Network interface controller information type.
  156. */
  157. typedef struct _EMACINFO EMACINFO;
  158. /*
  159. * TODO: Buffers and their descriptors should be part of the EMACINFO
  160. * structure. Actually there will be no dual Ethernet chip (sure?),
  161. * but just to keep the code clean.
  162. */
  163. typedef struct _TBufDescriptor {
  164. uint32_t TDES0;
  165. uint32_t TDES1;
  166. uint32_t TDES2;
  167. uint32_t TDES3;
  168. } TBufDescriptor_t;
  169. typedef struct _RBufDescriptor {
  170. uint32_t RDES0;
  171. uint32_t RDES1;
  172. uint32_t RDES2;
  173. uint32_t RDES3;
  174. } RBufDescriptor_t;
  175. static volatile TBufDescriptor_t txBufTab[EMAC_TX_BUFFERS];
  176. static volatile uint8_t txBuf[EMAC_TX_BUFFERS * EMAC_TX_BUFSIZ] __attribute__ ((aligned(8)));
  177. static unsigned int txBufIdx = 0;
  178. static volatile RBufDescriptor_t rxBufTab[EMAC_RX_BUFFERS];
  179. static volatile uint8_t rxBuf[EMAC_RX_BUFFERS * EMAC_RX_BUFSIZ] __attribute__ ((aligned(8)));
  180. static unsigned int rxBufIdx = 0;
  181. #define TDES0_OWN 0x80000000 /* Bit set: descriptor is owned by DMA */
  182. #define TDES0_IC 0x40000000 /* Set transmit interrupt after transmission */
  183. #define TDES0_LS 0x20000000 /* Indicates last segment of the frame */
  184. #define TDES0_FS 0x10000000 /* Indicates first segment of the frame */
  185. #define TDES0_DC 0x08000000 /* Disable CRC on transmit frame */
  186. #define TDES0_DP 0x04000000 /* Disable padding on short frames */
  187. #define TDES0_TTSE 0x02000000 /* Transmit time stamp enable */
  188. #define TDES0_CIC_DIS 0x00000000 /* Checksum insertion disabled */
  189. #define TDES0_CIC_IP 0x00400000 /* Checksum only for ip header */
  190. #define TDES0_CIC_FULL 0x00800000 /* Checksum for header and payload */
  191. #define TDES0_CIC_ALL 0x00C00000 /* Checksum for header and payload + pseudo header */
  192. #define TDES0_TER 0x00200000 /* Indicates final descriptor */
  193. #define TDES0_TCH 0x00100000 /* Second address points to next descriptor */
  194. #define TDES0_TTSS 0x00020000 /* Indicates time stamp captured for transmit frame */
  195. #define TDES0_IHE 0x00010000 /* MAC indicates ip header error */
  196. #define TDES0_ES 0x00008000 /* Error summary */
  197. #define TDES0_JT 0x00004000 /* MAC indicates jabber timeout */
  198. #define TDES0_FF 0x00002000 /* Indicates that DMA flushed the frame */
  199. #define TDES0_IPE 0x00001000 /* MAC indicates ip payload error */
  200. #define TDES0_LCA 0x00000800 /* Indicates loss of carrier */
  201. #define TDES0_NC 0x00000400 /* Carrier sense signal not asserted */
  202. #define TDES0_LCO 0x00000200 /* Late collision occured */
  203. #define TDES0_EC 0x00000100 /* Indicates 16 successive collisions */
  204. #define TDES0_VF 0x00000080 /* Indicates VLAN-type frame on transmission */
  205. #define TDES0_CC_MASK 0x00000078 /* Mask for collision counter */
  206. #define TDES0_CC_SHIFT 3 /* Number of right shifts to get collision number */
  207. #define TDES0_ED 0x00000004 /* Indicates excessive deferral */
  208. #define TDES0_UF 0x00000002 /* MAC indicates underflow error on DMA access */
  209. #define TDES0_DB 0x00000001 /* MAC defers because of the presence of the carrier */
  210. #define TDES1_TBS2_MASK 0x1FFF0000 /* Mask for Transmit buffer 2 size */
  211. #define TDES1_TBS2_SHIFT 16 /* Number of right shifts to get transmit buffer 2 size */
  212. #define TDES1_TBS1_MASK 0x00001FFF /* Mask for Transmit buffer 1 size */
  213. #define TDES1_TBS1_SHIFT 0 /* Number of right shifts to get transmit buffer 1 size */
  214. #define RDES0_OWN 0x80000000 /* Bit set: Descriptor is owned by DMA */
  215. #define RDES0_AFM 0x40000000 /* Indicates fail of destination address filter in MAC */
  216. #define RDES0_FL_MASK 0x3FFF0000 /* Mask for frame length */
  217. #define RDES0_FL_SHIFT 16 /* Number of right shifts to get frame length */
  218. #define RDES0_ES 0x00008000 /* Error summary */
  219. #define RDES0_DE 0x00004000 /* Descriptor error */
  220. #define RDES0_SAF 0x00002000 /* Indicates fail of source address filter in MAC */
  221. #define RDES0_LE 0x00001000 /* Indicates error in length of received frame */
  222. #define RDES0_OE 0x00000800 /* Indicates buffer overflow */
  223. #define RDES0_VLAN 0x00000400 /* Indicates a VLAN frame */
  224. #define RDES0_FS 0x00000200 /* Indicates descriptor for first buffer of the frame */
  225. #define RDES0_LS 0x00000100 /* Indicates descriptor for last buffer of the frame */
  226. #define RDES0_IPHCE 0x00000080 /* Indicates IP header checksum error */
  227. #define RDES0_LCO 0x00000040 /* Indicates a late collision */
  228. #define RDES0_FT 0x00000020 /* Indicates that the received frame is of ethernet-type */
  229. #define RDES0_RWT 0x00000010 /* Indicates receive watchdog timeout */
  230. #define RDES0_RE 0x00000008 /* Indicates receive error during reception */
  231. #define RDES0_DBE 0x00000004 /* Indicates non-integer multiple of bytes in frame */
  232. #define RDES0_CE 0x00000002 /* Indicates a CRC error on received frame */
  233. #define RDES0_PCE 0x00000001 /* Indicates payload checksum error */
  234. #define RDES1_DIC 0x80000000 /* Disable interrupt on completed reception of frame */
  235. #define RDES1_RBS2_MASK 0x1FFF0000 /* Receive buffer 2 size */
  236. #define RDES1_RBS2_SHIFT 16 /* Number of right shifts to get received buffer 2 size */
  237. #define RDES1_RER 0x00008000 /* End of ring: final descriptor of the list */
  238. #define RDES1_RCH 0x00004000 /* Second address points to next descriptor */
  239. #define RDES1_RBS1_MASK 0x00001FFF /* Receiver buffer 1 size */
  240. #define RDES1_RBS1_SHIFT 0 /* Number of right shifts to get received buffer 1 size */
  241. /*!
  242. * \brief Read contents of PHY register.
  243. *
  244. * \param reg PHY register number.
  245. *
  246. * \return Contents of the specified register.
  247. */
  248. static uint16_t phy_inw(uint8_t reg)
  249. {
  250. uint32_t tempReg;
  251. uint16_t val = 0;
  252. /* PHY read command. */
  253. tempReg = inr(&(ETH->MACMIIAR));
  254. tempReg &= 0xffff0020;
  255. tempReg |= ((NIC_PHY_ADDR) << 11) | ((reg & 0x1F) << 6) | ETH_MACMIIAR_MB| ETH_MACMIIAR_CR_Div;
  256. outr(&(ETH->MACMIIAR), tempReg);
  257. /* Wait until PHY logic completed. */
  258. while (((inr(&(ETH->MACMIIAR)) & ETH_MACMIIAR_MB) == ETH_MACMIIAR_MB) && val<10)
  259. {
  260. NutSleep(1);
  261. val++;
  262. }
  263. /* Timeout */
  264. if(val>=10)
  265. {
  266. val = 0xFFFF;
  267. EMPRINTF("phy_inw: Could not read phy register.\n");
  268. }
  269. /* Get data from PHY maintenance register. */
  270. val = (uint16_t) (inr(&(ETH->MACMIIDR)) & ETH_MACMIIDR_MD);
  271. return val;
  272. }
  273. /*!
  274. * \brief Write value to PHY register.
  275. *
  276. * \param reg PHY register number.
  277. * \param val Value to write.
  278. */
  279. static void phy_outw(uint8_t reg, uint16_t val)
  280. {
  281. uint32_t tempReg;
  282. uint16_t wait = 0;
  283. /* PHY write command. */
  284. outr(&(ETH->MACMIIDR), val);
  285. tempReg = inr(&(ETH->MACMIIAR));
  286. tempReg &= 0xffff0020;
  287. tempReg |= ((NIC_PHY_ADDR) << 11) | ((reg & 0x1F) << 6) | ETH_MACMIIAR_MW | ETH_MACMIIAR_MB| ETH_MACMIIAR_CR_Div;
  288. outr(&(ETH->MACMIIAR), tempReg);
  289. /* Wait until PHY logic completed. */
  290. while (((inr(&(ETH->MACMIIAR)) & ETH_MACMIIAR_MB) == ETH_MACMIIAR_MB) && wait<10)
  291. {
  292. NutSleep(1);
  293. wait++;
  294. }
  295. if(wait>=10)
  296. {
  297. EMPRINTF("phy_outw: Could not set phy register.\n");
  298. }
  299. }
  300. /*!
  301. * \brief Reset the Ethernet controller.
  302. *
  303. * \return 0 on success, -1 otherwise.
  304. */
  305. static int EmacReset(void)
  306. {
  307. int rc = 0;
  308. uint32_t phy = 0;
  309. int link_wait;
  310. #ifdef STM32F10X_CL
  311. /* force reset mac */
  312. RCC->AHBRSTR |= RCC_AHBRSTR_ETHMACRST;
  313. /* enable clocks for mac */
  314. RCC->AHBENR |= RCC_AHBENR_ETHMACEN | RCC_AHBENR_ETHMACTXEN |
  315. RCC_AHBENR_ETHMACRXEN;
  316. /* release reset mac */
  317. RCC->AHBRSTR &= ~RCC_AHBRSTR_ETHMACRST;
  318. #else
  319. RCC->AHB1RSTR |= RCC_AHB1RSTR_ETHMACRST;
  320. /* enable clocks for mac */
  321. RCC->AHB1ENR |= RCC_AHB1ENR_ETHMACEN | RCC_AHB1ENR_ETHMACTXEN |
  322. RCC_AHB1ENR_ETHMACRXEN;
  323. /* release reset mac */
  324. RCC->AHB1RSTR &= ~RCC_AHB1RSTR_ETHMACRST;
  325. #endif
  326. /* Register PHY to be able to reset it */
  327. rc = NutRegisterPhy( NIC_PHY_ADDR, phy_outw, phy_inw);
  328. /* Reset PHY
  329. (Note that this does not set all PHY registers to reset values!) */
  330. phy = 1;
  331. NutPhyCtl(PHY_CTL_RESET, &phy);
  332. NutSleep(250);
  333. /* Register PHY again to do all the initianlization */
  334. rc = NutRegisterPhy( NIC_PHY_ADDR, phy_outw, phy_inw);
  335. #if 0
  336. /* Clear MII isolate. */
  337. phy = 0;
  338. NutPhyCtl(PHY_CTL_ISOLATE, &phy);
  339. #endif
  340. /* Restart autonegotiation */
  341. phy = 1;
  342. NutPhyCtl(PHY_CTL_AUTONEG_RE, &phy);
  343. /* Wait for auto negotiation completed and link established. */
  344. for (link_wait = 25;; link_wait--) {
  345. NutPhyCtl(PHY_GET_STATUS, &phy);
  346. if((phy & PHY_STATUS_HAS_LINK) && (phy & PHY_STATUS_AUTONEG_OK)) {
  347. break;
  348. }
  349. if (link_wait == 0) {
  350. EMPRINTF("NO LINK!\n");
  351. return -1;
  352. }
  353. NutSleep(200);
  354. }
  355. return rc;
  356. }
  357. /*
  358. * NIC interrupt entry.
  359. */
  360. static void EmacInterrupt(void *arg)
  361. {
  362. uint32_t isr;
  363. EMACINFO *ni = (EMACINFO *) ((NUTDEVICE *) arg)->dev_dcb;
  364. /* Read interrupt status and reset interrupt flags. */
  365. isr = inr(&(ETH->DMASR));
  366. outr(&(ETH->DMASR), 0x0001E7FF);
  367. /* Receiver interrupt. */
  368. if ((isr & ETH_DMASR_RS) != 0 || (isr & ETH_DMASR_ROS) != 0) {
  369. NutEventPostFromIrq(&(ni->ni_rx_rdy));
  370. }
  371. /* Transmitter interrupt. */
  372. if ((isr & ETH_DMASR_TS) != 0) {
  373. NutEventPostFromIrq(&(ni->ni_tx_rdy));
  374. }
  375. /* Handling of corrupted rx buffers is done in reception
  376. * routines without any additional activities here */
  377. /* Also clear interrupt status bits in MAC interrupt status register
  378. * although we do not use them now, but who knows. */
  379. isr = inr(&(ETH->MACSR));
  380. }
  381. /*!
  382. * \brief Fetch the next packet out of the receive buffers.
  383. *
  384. * \return 0 on success, -1 otherwise.
  385. */
  386. static int EmacGetPacket(EMACINFO * ni, NETBUF ** nbp)
  387. {
  388. int rc = -1;
  389. uint16_t fbc = 0;
  390. unsigned int i;
  391. *nbp = NULL;
  392. /*
  393. * Search the next frame start. Release any fragment.
  394. */
  395. while ((rxBufTab[rxBufIdx].RDES0 & RDES0_OWN) == 0 && (rxBufTab[rxBufIdx].RDES0 & RDES0_FS) == 0) {
  396. rxBufTab[rxBufIdx].RDES0 = RDES0_OWN;
  397. rxBufIdx++;
  398. if (rxBufIdx >= EMAC_RX_BUFFERS) {
  399. rxBufIdx = 0;
  400. }
  401. }
  402. /*
  403. * Determine the size of the next frame.
  404. */
  405. i = rxBufIdx;
  406. while ((rxBufTab[i].RDES0 & RDES0_OWN) == 0) {
  407. if ((rxBufTab[i].RDES0 & RDES0_LS) != 0) {
  408. if((rxBufTab[i].RDES0 & RDES0_DE) == 0) {
  409. fbc = (rxBufTab[i].RDES0 & RDES0_FL_MASK) >> RDES0_FL_SHIFT;
  410. }
  411. else {
  412. /* Release buffers with corrupted frame data */
  413. while((rxBufTab[rxBufIdx].RDES0 & RDES0_OWN) == 0) {
  414. if((rxBufTab[rxBufIdx].RDES0 & RDES0_LS) != 0)
  415. {
  416. rxBufTab[rxBufIdx].RDES0 = RDES0_OWN;
  417. rxBufIdx++;
  418. if (rxBufIdx >= EMAC_RX_BUFFERS) {
  419. rxBufIdx = 0;
  420. }
  421. break;
  422. }
  423. rxBufTab[rxBufIdx].RDES0 = RDES0_OWN;
  424. rxBufIdx++;
  425. if (rxBufIdx >= EMAC_RX_BUFFERS) {
  426. rxBufIdx = 0;
  427. }
  428. }
  429. }
  430. break;
  431. }
  432. i++;
  433. if (i >= EMAC_RX_BUFFERS) {
  434. i = 0;
  435. }
  436. }
  437. if (fbc) {
  438. /*
  439. * Receiving long packets is unexpected. Let's declare the
  440. * chip insane. Short packets will be handled by the caller.
  441. */
  442. if (fbc > EMAC_TX_BUFSIZ) {
  443. ni->ni_insane = 1;
  444. } else {
  445. *nbp = NutNetBufAlloc(0, NBAF_DATALINK, fbc);
  446. if (*nbp != NULL) {
  447. uint8_t *bp = (uint8_t *) (* nbp)->nb_dl.vp;
  448. unsigned int len;
  449. while (fbc) {
  450. if (fbc > EMAC_RX_BUFSIZ) {
  451. len = EMAC_RX_BUFSIZ;
  452. } else {
  453. len = fbc;
  454. }
  455. memcpy(bp, (void *) rxBufTab[rxBufIdx].RDES2, len);
  456. rxBufTab[rxBufIdx].RDES0 = RDES0_OWN;
  457. rxBufIdx++;
  458. if (rxBufIdx >= EMAC_RX_BUFFERS) {
  459. rxBufIdx = 0;
  460. }
  461. fbc -= len;
  462. bp += len;
  463. }
  464. rc = 0;
  465. }
  466. else {
  467. EMPRINTF("NutNetBufAlloc Failed!\n");
  468. }
  469. }
  470. }
  471. return rc;
  472. }
  473. /*!
  474. * \brief Load a packet into the nic's transmit ring buffer.
  475. *
  476. * \param nb Network buffer structure containing the packet to be sent.
  477. * The structure must have been allocated by a previous
  478. * call NutNetBufAlloc(). This routine will automatically
  479. * release the buffer in case of an error.
  480. *
  481. * \return 0 on success, -1 in case of any errors. Errors
  482. * will automatically release the network buffer
  483. * structure.
  484. */
  485. static int EmacPutPacket(int bufnum, EMACINFO * ni, NETBUF * nb)
  486. {
  487. int rc = -1;
  488. unsigned int sz;
  489. uint8_t *buf;
  490. /*
  491. * Calculate the number of bytes to be send. Do not send packets
  492. * larger than the Ethernet maximum transfer unit. The MTU
  493. * consist of 1500 data bytes plus the 14 byte Ethernet header
  494. * plus 4 bytes CRC. We check the data bytes only.
  495. */
  496. if ((sz = nb->nb_nw.sz + nb->nb_tp.sz + nb->nb_ap.sz) > ETHERMTU) {
  497. return -1;
  498. }
  499. sz += nb->nb_dl.sz;
  500. /*if (sz & 1) {
  501. sz++;
  502. }*/
  503. /* Disable EMAC interrupts. */
  504. NutIrqDisable(&sig_EMAC);
  505. /* TODO: Check for link. */
  506. if (ni->ni_insane == 0) {
  507. buf = (uint8_t *) (txBuf + (bufnum * EMAC_TX_BUFSIZ));
  508. memcpy(buf, nb->nb_dl.vp, nb->nb_dl.sz);
  509. buf += nb->nb_dl.sz;
  510. memcpy(buf, nb->nb_nw.vp, nb->nb_nw.sz);
  511. buf += nb->nb_nw.sz;
  512. memcpy(buf, nb->nb_tp.vp, nb->nb_tp.sz);
  513. buf += nb->nb_tp.sz;
  514. memcpy(buf, nb->nb_ap.vp, nb->nb_ap.sz);
  515. if(bufnum < (EMAC_TX_BUFFERS - 1)) {
  516. txBufTab[bufnum].TDES0 = TDES0_LS | TDES0_FS | TDES0_IC | TDES0_TCH;
  517. txBufTab[bufnum].TDES3 = (uint32_t) &(txBufTab[bufnum + 1]);
  518. }
  519. else {
  520. txBufTab[bufnum].TDES0 = TDES0_LS | TDES0_FS | TDES0_IC | TDES0_TER;
  521. }
  522. txBufTab[bufnum].TDES1 = ((sz & TDES1_TBS1_MASK) << TDES1_TBS1_SHIFT);
  523. txBufTab[bufnum].TDES2 = (uint32_t) (txBuf + (bufnum * EMAC_TX_BUFSIZ));
  524. /* make descriptor useable for dma controller */
  525. txBufTab[bufnum].TDES0 |= TDES0_OWN;
  526. /* trigger dma polling */
  527. outr(&(ETH->DMATPDR), 0);
  528. rc = 0;
  529. #ifdef NUT_PERFMON
  530. ni->ni_tx_packets++;
  531. #endif
  532. }
  533. /* Enable EMAC interrupts. */
  534. NutIrqEnable(&sig_EMAC);
  535. return rc;
  536. }
  537. /*!
  538. * \brief Fire up the network interface.
  539. *
  540. * NIC interrupts must be disabled when calling this function.
  541. *
  542. * \param mac Six byte unique MAC address.
  543. */
  544. static int EmacStart(const uint8_t * mac)
  545. {
  546. int i;
  547. uint32_t regvalue;
  548. /* stop transmission and reception */
  549. outr(&(ETH->DMAOMR), (inr(&(ETH->DMAOMR)) & ~(ETH_DMAOMR_ST | ETH_DMAOMR_SR)) | ETH_DMAOMR_FTF);
  550. /* waiting for flush to finish */
  551. i=0;
  552. while(((inr(&(ETH->DMAOMR)) & ETH_DMAOMR_FTF) == ETH_DMAOMR_FTF) && i <100)
  553. {
  554. NutSleep(1);
  555. i++;
  556. }
  557. if (i >= 100)
  558. {
  559. EMPRINTF("EmacStart: Could not flush\n");
  560. return -1;
  561. }
  562. if (EmacReset()) {
  563. return -1;
  564. }
  565. /* Initialize transmit buffer descriptors using chained list */
  566. for (i = 0; i < EMAC_TX_BUFFERS - 1; i++) {
  567. txBufTab[i].TDES0 = TDES0_TCH;
  568. txBufTab[i].TDES1 = 0;
  569. txBufTab[i].TDES3 = (uint32_t) &(txBufTab[i+1]);
  570. }
  571. txBufTab[EMAC_TX_BUFFERS - 1].TDES0 = /*TDES0_TCH |*/ TDES0_TER;
  572. txBufTab[EMAC_TX_BUFFERS - 1].TDES1 = 0;
  573. /* Initialize receive buffer descriptors using chained list */
  574. for (i = 0; i < EMAC_RX_BUFFERS; i++) {
  575. rxBufTab[i].RDES0 = RDES0_OWN;
  576. rxBufTab[i].RDES1 = RDES1_RCH | (EMAC_RX_BUFSIZ << RDES1_RBS1_SHIFT);
  577. rxBufTab[i].RDES2 = (uint32_t) (rxBuf + (i * EMAC_RX_BUFSIZ));
  578. if (i == EMAC_RX_BUFFERS - 1) {
  579. rxBufTab[i].RDES1 = RDES1_RER | (EMAC_RX_BUFSIZ << RDES1_RBS1_SHIFT);
  580. rxBufTab[i].RDES3 = 0;
  581. }
  582. else {
  583. rxBufTab[i].RDES1 = RDES1_RCH | (EMAC_RX_BUFSIZ << RDES1_RBS1_SHIFT);
  584. rxBufTab[i].RDES3 = (uint32_t) &(rxBufTab[i+1]);
  585. }
  586. }
  587. /* reset dma and wait until reset is finished */
  588. outr(&(ETH->DMABMR), ETH_DMABMR_SR);
  589. i=0;
  590. while(((inr(&(ETH->DMABMR)) & ETH_DMABMR_SR) == ETH_DMABMR_SR)&& i <100)
  591. {
  592. NutSleep(1);
  593. i++;
  594. }
  595. if (i >= 100)
  596. {
  597. EMPRINTF("EmacStart: Could not reset dma\n");
  598. return -1;
  599. }
  600. /* Set local MAC address (used e.g. for filtering). */
  601. outr(&(ETH->MACA0HR), (mac[5] << 8) | mac[4]);
  602. outr(&(ETH->MACA0LR), (mac[3] << 24) | (mac[2] << 16) | (mac[1] << 8) | mac[0]);
  603. /* Set bus mode in register DMABMR */
  604. /* leave at reset state */
  605. /* Enable dma interrupts */
  606. outr(&(ETH->DMAIER), ETH_DMAIER_NISE | ETH_DMAIER_AISE |
  607. /*ETH_DMAIER_ERIE |*/ ETH_DMAIER_FBEIE | /*ETH_DMAIER_ETIE |
  608. ETH_DMAIER_RWTIE | ETH_DMAIER_RPSIE |*/ ETH_DMAIER_RBUIE |
  609. ETH_DMAIER_RIE | /*ETH_DMAIER_TUIE | ETH_DMAIER_ROIE |
  610. ETH_DMAIER_TJTIE | ETH_DMAIER_TBUIE | ETH_DMAIER_TPSIE |*/
  611. ETH_DMAIER_TIE);
  612. /* Set start address of descriptor lists */
  613. outr(&(ETH->DMATDLAR), (uint32_t) txBufTab);
  614. outr(&(ETH->DMARDLAR), (uint32_t) rxBufTab);
  615. /* clear management counters */
  616. outr(&(ETH->MMCCR), ETH_MMCCR_CR);
  617. /* setting speed and duplex to values from autonegotiation process */
  618. NutPhyCtl(PHY_GET_STATUS, &regvalue);
  619. if(regvalue & PHY_STATUS_100M)
  620. {
  621. /* set emac to 100Mbit/s */
  622. outr(&(ETH->MACCR), inr(&(ETH->MACCR)) | ETH_MACCR_FES);
  623. }
  624. else
  625. {
  626. /* set emac to 10Mbit/s */
  627. outr(&(ETH->MACCR), inr(&(ETH->MACCR)) & ~ETH_MACCR_FES);
  628. }
  629. if(regvalue & PHY_STATUS_FULLDUPLEX)
  630. {
  631. /* set emac to full duplex */
  632. outr(&(ETH->MACCR), inr(&(ETH->MACCR)) | ETH_MACCR_DM);
  633. }
  634. else
  635. {
  636. /* set emac to half duplex */
  637. outr(&(ETH->MACCR), inr(&(ETH->MACCR)) & ~ETH_MACCR_DM);
  638. }
  639. /* ToDo: Set up MAC frame filters. ATM we receive all frames */
  640. outr(&(ETH->MACFFR), inr(&(ETH->MACFFR)) /*| ETH_MACFFR_RA | ETH_MACFFR_PM | ETH_MACFFR_PAM*/);
  641. /* enable transmitter and receiver state machine */
  642. outr(&(ETH->MACCR), inr(&(ETH->MACCR)) | ETH_MACCR_TE | ETH_MACCR_RE);
  643. /* start transmission and reception */
  644. outr(&(ETH->DMAOMR), inr(&(ETH->DMAOMR)) | ETH_DMAOMR_ST | ETH_DMAOMR_SR
  645. /*testing:*/ | ETH_DMAOMR_FEF | ETH_DMAOMR_FUGF);
  646. /*outr(&(ETH->DMARPDR), 0);*/
  647. return 0;
  648. }
  649. /*! \fn EmacRxThread(void *arg)
  650. * \brief NIC receiver thread.
  651. *
  652. */
  653. THREAD(EmacRxThread, arg)
  654. {
  655. NUTDEVICE *dev;
  656. IFNET *ifn;
  657. EMACINFO *ni;
  658. NETBUF *nb;
  659. dev = arg;
  660. ifn = (IFNET *) dev->dev_icb;
  661. ni = (EMACINFO *) dev->dev_dcb;
  662. /*
  663. * This is a temporary hack. Due to a change in initialization,
  664. * we may not have got a MAC address yet. Wait until a valid one
  665. * has been set.
  666. */
  667. while (!ETHER_IS_UNICAST(ifn->if_mac)) {
  668. NutSleep(10);
  669. }
  670. /*
  671. * Do not continue unless we managed to start the NIC. We are
  672. * trapped here if the Ethernet link cannot be established.
  673. * This happens, for example, if no Ethernet cable is plugged
  674. * in.
  675. */
  676. while (EmacStart(ifn->if_mac)) {
  677. NutSleep(1000);
  678. }
  679. /* Run at high priority. */
  680. NutThreadSetPriority(9);
  681. /* Initialize the access mutex. */
  682. NutEventPost(&ni->ni_mutex);
  683. /* Clear pending interrupt flags and enable receive interrupts. */
  684. outr(&(ETH->DMASR), ((unsigned int)inr(&(ETH->DMASR))) | 0x0001E7FF);
  685. NutIrqEnable(&sig_EMAC);
  686. for (;;) {
  687. /*
  688. * Wait for the arrival of new packets or poll the receiver
  689. * every two seconds.
  690. */
  691. NutEventWait(&ni->ni_rx_rdy, 2000);
  692. /*
  693. * Fetch all packets from the NIC's internal buffer and pass
  694. * them to the registered handler.
  695. */
  696. while (EmacGetPacket(ni, &nb) == 0) {
  697. /* Discard short packets. */
  698. if (nb->nb_dl.sz < 60) {
  699. NutNetBufFree(nb);
  700. } else {
  701. (*ifn->if_recv) (dev, nb);
  702. }
  703. }
  704. /* We got a weird chip, try to restart it. */
  705. while (ni->ni_insane) {
  706. if (EmacStart(ifn->if_mac) == 0) {
  707. ni->ni_insane = 0;
  708. ni->ni_tx_queued = 0;
  709. ni->ni_tx_quelen = 0;
  710. NutIrqEnable(&sig_EMAC);
  711. } else {
  712. NutSleep(1000);
  713. }
  714. }
  715. }
  716. }
  717. /*!
  718. * \brief Send Ethernet packet.
  719. *
  720. * \todo This routine does not work.
  721. *
  722. * \param dev Identifies the device to use.
  723. * \param nb Network buffer structure containing the packet to be sent.
  724. * The structure must have been allocated by a previous
  725. * call NutNetBufAlloc().
  726. *
  727. * \return 0 on success, -1 in case of any errors.
  728. */
  729. int EmacOutput(NUTDEVICE * dev, NETBUF * nb)
  730. {
  731. static uint32_t mx_wait = 5000;
  732. int rc = -1;
  733. EMACINFO *ni = (EMACINFO *) dev->dev_dcb;
  734. /*
  735. * After initialization we are waiting for a long time to give
  736. * the PHY a chance to establish an Ethernet link.
  737. */
  738. while (rc) {
  739. if (ni->ni_insane) {
  740. break;
  741. }
  742. if (NutEventWait(&ni->ni_mutex, mx_wait)) {
  743. break;
  744. }
  745. /* Check for packet queue space. */
  746. if ((txBufTab[txBufIdx].TDES0 & TDES0_OWN) == TDES0_OWN) {
  747. if (NutEventWait(&ni->ni_tx_rdy, 500) && (txBufTab[txBufIdx].TDES0 & TDES0_OWN) == TDES0_OWN) {
  748. /* No queue space. Release the lock and give up. */
  749. NutEventPost(&ni->ni_mutex);
  750. break;
  751. }
  752. } else {
  753. if ((rc = EmacPutPacket(txBufIdx, ni, nb)) == 0) {
  754. txBufIdx++;
  755. if(txBufIdx >= EMAC_TX_BUFFERS)
  756. {
  757. txBufIdx = 0;
  758. }
  759. }
  760. }
  761. NutEventPost(&ni->ni_mutex);
  762. }
  763. /*
  764. * Probably no Ethernet link. Significantly reduce the waiting
  765. * time, so following transmission will soon return an error.
  766. */
  767. if (rc) {
  768. mx_wait = 500;
  769. } else {
  770. /* Ethernet works. Set a long waiting time in case we
  771. temporarly lose the link next time. */
  772. mx_wait = 5000;
  773. }
  774. return rc;
  775. }
  776. /*!
  777. * \brief Initialize Ethernet hardware.
  778. *
  779. * Applications should do not directly call this function. It is
  780. * automatically executed during during device registration by
  781. * NutRegisterDevice().
  782. *
  783. * \param dev Identifies the device to initialize.
  784. */
  785. int EmacInit(NUTDEVICE * dev)
  786. {
  787. EMACINFO *ni = (EMACINFO *) dev->dev_dcb;
  788. EMPRINTF("Using Mode %s, Remap %s, NIC_PHY_ADDR %d, \n",
  789. #ifdef PHY_MODE_RMII
  790. "RMII",
  791. #else
  792. "MII",
  793. #endif
  794. #ifdef EMAC_REMAP_ENABLE
  795. "enabled",
  796. #else
  797. "disabled",
  798. #endif
  799. NIC_PHY_ADDR
  800. );
  801. /* Clear EMACINFO structure. */
  802. memset(ni, 0, sizeof(EMACINFO));
  803. /* Register interrupt handler. */
  804. if (NutRegisterIrqHandler(&sig_EMAC, EmacInterrupt, dev)) {
  805. return -1;
  806. }
  807. #ifdef STM32F10X_CL
  808. /* disable clocks for mac */
  809. RCC->AHBENR &= ~(RCC_AHBENR_ETHMACEN | RCC_AHBENR_ETHMACTXEN |
  810. RCC_AHBENR_ETHMACRXEN);
  811. #else
  812. /* disable clocks for mac */
  813. RCC->AHB1ENR &= ~(RCC_AHB1ENR_ETHMACEN | RCC_AHB1ENR_ETHMACTXEN |
  814. RCC_AHB1ENR_ETHMACRXEN);
  815. /* Switch on SYSCFG Clock*/
  816. CM3BBSET(RCC_BASE, RCC_TypeDef, APB2ENR, _BI32(RCC_APB2ENR_SYSCFGEN));
  817. #endif
  818. /*
  819. * Alternate function remapping and GPIO set-up
  820. */
  821. #ifdef STM32F10X_CL /* STM32F1 */
  822. #ifdef EMAC_REMAP_ENABLE
  823. CM3BBSET(AFIO_BASE, AFIO_TypeDef, MAPR, _BI32(AFIO_MAPR_ETH_REMAP));
  824. #else
  825. CM3BBCLR(AFIO_BASE, AFIO_TypeDef, MAPR, _BI32(AFIO_MAPR_ETH_REMAP));
  826. #endif
  827. /* Configure (R)MII lines as alternate function */
  828. GpioPinConfigSet(NUTGPIO_PORTC, 1, GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHAL | GPIO_CFG_SPEED_FAST); // ETH_MDC (50MHz)
  829. GpioPinConfigSet(NUTGPIO_PORTA, 2, GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHAL | GPIO_CFG_SPEED_FAST); // ETH_MDIO (50MHz)
  830. GpioPinConfigSet(NUTGPIO_PORTB, 11, GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHAL | GPIO_CFG_SPEED_FAST); // ETH_(R)MII_TX_EN (50MHz)
  831. GpioPinConfigSet(NUTGPIO_PORTB, 12, GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHAL | GPIO_CFG_SPEED_FAST); // ETH_(R)MII_TXD0 (50MHz)
  832. GpioPinConfigSet(NUTGPIO_PORTB, 13, GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHAL | GPIO_CFG_SPEED_FAST); // ETH_(R)MII_TXD1 (50MHz)
  833. #if !defined(PHY_MODE_RMII)
  834. /* Configure additional MII lines as alternate function */
  835. GpioPinConfigSet(NUTGPIO_PORTC, 2, GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHAL | GPIO_CFG_SPEED_FAST); // ETH_MII_TXD2 (50MHz)
  836. GpioPinConfigSet(NUTGPIO_PORTB, 8, GPIO_CFG_OUTPUT | GPIO_CFG_PERIPHAL | GPIO_CFG_SPEED_FAST); // ETH_MII_TXD3 (50MHz)
  837. #endif
  838. #else /* STM32F2/F4 parts */
  839. #ifdef EMAC_REMAP_ENABLE
  840. #error General remapping not available for this part. Use alternate function remapping instead!
  841. #endif
  842. GpioPinConfigSet(NUTGPIO_PORTA, 1, GPIO_CFG_PERIPHAL); // ETH_RMII_REF_CLK / ETH_MII_RX_CLK
  843. GpioPinConfigSet(NUTGPIO_PORTA, 2, GPIO_CFG_PERIPHAL|GPIO_CFG_OUTPUT|EMAC_GPIO_SPEED); // ETH_MDIO
  844. GpioPinConfigSet(NUTGPIO_PORTA, 7, GPIO_CFG_PERIPHAL); // CRS_DV / ETH_MII_RX_DV
  845. GPIO_PinAFConfig((GPIO_TypeDef*) NUTGPIO_PORTA, 1, GPIO_AF_ETH); // REF_CLK / ETH_MII_RX_CLK
  846. GPIO_PinAFConfig((GPIO_TypeDef*) NUTGPIO_PORTA, 2, GPIO_AF_ETH); // ETH_MDIO
  847. GPIO_PinAFConfig((GPIO_TypeDef*) NUTGPIO_PORTA, 7, GPIO_AF_ETH); // CRS_DV / ETH_MII_RX_DV
  848. GpioPinConfigSet(NUTGPIO_PORTC, 1, GPIO_CFG_PERIPHAL|GPIO_CFG_OUTPUT|EMAC_GPIO_SPEED); // ETH_MDC
  849. GpioPinConfigSet(NUTGPIO_PORTC, 4, GPIO_CFG_PERIPHAL); // ETH_RX_D0
  850. GpioPinConfigSet(NUTGPIO_PORTC, 5, GPIO_CFG_PERIPHAL); // ETH_RX_D1
  851. GPIO_PinAFConfig((GPIO_TypeDef*) NUTGPIO_PORTC, 1, GPIO_AF_ETH); // ETH_MDC
  852. GPIO_PinAFConfig((GPIO_TypeDef*) NUTGPIO_PORTC, 4, GPIO_AF_ETH); // ETH_RX_D0
  853. GPIO_PinAFConfig((GPIO_TypeDef*) NUTGPIO_PORTC, 5, GPIO_AF_ETH); // ETH_RX_D1
  854. GpioPinConfigSet(EMAC_TXEN_PORT, 11, GPIO_CFG_PERIPHAL|GPIO_CFG_OUTPUT|EMAC_GPIO_SPEED); // TXEN
  855. GpioPinConfigSet(EMAC_TXD0_PORT, EMAC_TXD0_PIN, GPIO_CFG_PERIPHAL|GPIO_CFG_OUTPUT|EMAC_GPIO_SPEED); // TXD0
  856. GpioPinConfigSet(EMAC_TXD1_PORT, EMAC_TXD1_PIN, GPIO_CFG_PERIPHAL|GPIO_CFG_OUTPUT|EMAC_GPIO_SPEED); // TXD1
  857. GPIO_PinAFConfig((GPIO_TypeDef*) EMAC_TXEN_PORT, 11, GPIO_AF_ETH); // TXEN
  858. GPIO_PinAFConfig((GPIO_TypeDef*) EMAC_TXD0_PORT, EMAC_TXD0_PIN, GPIO_AF_ETH); // TXD0
  859. GPIO_PinAFConfig((GPIO_TypeDef*) EMAC_TXD1_PORT, EMAC_TXD1_PIN, GPIO_AF_ETH); // TXD1
  860. #ifdef EMAC_PPS_OUT_PORT
  861. GpioPinConfigSet(EMAC_PPS_OUT_PORT, EMAC_PPS_OUT_PIN, GPIO_CFG_PERIPHAL); // ETH_PPS_OUT
  862. GPIO_PinAFConfig((GPIO_TypeDef*) EMAC_PPS_OUT_PORT, EMAC_PPS_OUT_PIN, GPIO_AF_ETH); // ETH_PPS_OUT
  863. #endif /* EMAC_PPS_OUT_PORT */
  864. #ifdef EMAC_PHY_CLOCK_MCO
  865. GpioPinConfigSet(NUTGPIO_PORTA, 8, GPIO_CFG_PERIPHAL|GPIO_CFG_OUTPUT|EMAC_GPIO_SPEED); // MCO1
  866. GPIO_PinAFConfig((GPIO_TypeDef*) NUTGPIO_PORTA, 8, GPIO_AF_MCO); // MCO1
  867. /* Output HSE clock (25MHz) on MCO pin (PA8) to clock the PHY */
  868. RCC->CFGR = (RCC->CFGR & ~(RCC_CFGR_MCO1 | RCC_CFGR_MCO1PRE)) | RCC_CFGR_MCO1_1;
  869. #endif /* PHY_CLOCK_MCO */
  870. #if !defined(PHY_MODE_RMII)
  871. GpioPinConfigSet(NUTGPIO_PORTB, 8, GPIO_CFG_PERIPHAL|GPIO_CFG_OUTPUT|EMAC_GPIO_SPEED); // ETH_MII_TXD3
  872. GPIO_PinAFConfig((GPIO_TypeDef*) NUTGPIO_PORTB, 8, GPIO_AF_ETH); // ETH_MII_TXD3
  873. GpioPinConfigSet(NUTGPIO_PORTC, 2, GPIO_CFG_PERIPHAL|GPIO_CFG_OUTPUT|EMAC_GPIO_SPEED); // ETH_MII_TXD2
  874. GpioPinConfigSet(NUTGPIO_PORTC, 3, GPIO_CFG_PERIPHAL); // ETH_MII_TX_CLK
  875. GPIO_PinAFConfig((GPIO_TypeDef*) NUTGPIO_PORTC, 2, GPIO_AF_ETH); // ETH_MII_TXD2
  876. GPIO_PinAFConfig((GPIO_TypeDef*) NUTGPIO_PORTC, 3, GPIO_AF_ETH); // ETH_MII_TX_CLK
  877. GpioPinConfigSet(EMAC_CRS_PORT, EMAC_CRS_PIN, GPIO_CFG_PERIPHAL|EMAC_GPIO_SPEED); // ETH_MII_CRS
  878. GpioPinConfigSet(EMAC_COL_PORT, 3, GPIO_CFG_PERIPHAL|EMAC_GPIO_SPEED); // ETH_MII_COL
  879. GPIO_PinAFConfig((GPIO_TypeDef*) EMAC_CRS_PORT, EMAC_CRS_PIN, GPIO_AF_ETH); // ETH_MII_CRS
  880. GPIO_PinAFConfig((GPIO_TypeDef*) EMAC_COL_PORT, 3, GPIO_AF_ETH); // ETH_MII_COL
  881. GpioPinConfigSet(EMAC_RXD2_PORT, EMAC_RXD2_PIN, GPIO_CFG_PERIPHAL); // ETH_MII_RXD2
  882. GpioPinConfigSet(EMAC_RXD3_PORT, EMAC_RXD3_PIN, GPIO_CFG_PERIPHAL); // ETH_MII_RXD3
  883. GpioPinConfigSet(EMAC_RX_ER_PORT, 10, GPIO_CFG_PERIPHAL); // ETH_MII_RX_ER
  884. GPIO_PinAFConfig((GPIO_TypeDef*) EMAC_RXD2_PORT, EMAC_RXD2_PIN, GPIO_AF_ETH); // ETH_MII_RXD2
  885. GPIO_PinAFConfig((GPIO_TypeDef*) EMAC_RXD3_PORT, EMAC_RXD3_PIN, GPIO_AF_ETH); // ETH_MII_RXD3
  886. GPIO_PinAFConfig((GPIO_TypeDef*) EMAC_RX_ER_PORT, 10, GPIO_AF_ETH); // ETH_MII_RX_ER
  887. #endif /* !PHY_MODE_RMII */
  888. #endif
  889. /*
  890. * MII or RMII mode selection
  891. */
  892. #if defined (SYSCFG_PMC_MII_RMII_SEL) && !defined(SYSCFG_PMC_MII_RMII)
  893. #define SYSCFG_PMC_MII_RMII SYSCFG_PMC_MII_RMII_SEL
  894. #endif
  895. #ifdef PHY_MODE_RMII
  896. /* switch to RMII mode */
  897. #ifdef STM32F10X_CL
  898. CM3BBSET(AFIO_BASE, AFIO_TypeDef, MAPR, _BI32(AFIO_MAPR_MII_RMII_SEL));
  899. #else
  900. CM3BBSET(SYSCFG_BASE, SYSCFG_TypeDef, PMC, _BI32(SYSCFG_PMC_MII_RMII));
  901. #endif
  902. #else
  903. /* switch to MII mode */
  904. #ifdef STM32F10X_CL
  905. CM3BBCLR(AFIO_BASE, AFIO_TypeDef, MAPR, _BI32(AFIO_MAPR_MII_RMII_SEL));
  906. #else
  907. CM3BBCLR(SYSCFG_BASE, SYSCFG_TypeDef, PMC, _BI32(SYSCFG_PMC_MII_RMII));
  908. #endif
  909. #endif
  910. /* Start the receiver thread. */
  911. if (NutThreadCreate("emacrx", EmacRxThread, dev,
  912. (NUT_THREAD_NICRXSTACK * NUT_THREAD_STACK_MULT) + NUT_THREAD_STACK_ADD) == NULL) {
  913. return -1;
  914. }
  915. return 0;
  916. }
  917. static EMACINFO dcb_eth0;
  918. /*!
  919. * \brief Network interface information structure.
  920. *
  921. * Used to call.
  922. */
  923. static IFNET ifn_eth0 = {
  924. IFT_ETHER, /*!< \brief Interface type, if_type. */
  925. 0, /*!< \brief Interface flags, if_flags. */
  926. {0, 0, 0, 0, 0, 0}, /*!< \brief Hardware net address, if_mac. */
  927. 0, /*!< \brief IP address, if_local_ip. */
  928. 0, /*!< \brief Remote IP address for point to point, if_remote_ip. */
  929. 0, /*!< \brief IP network mask, if_mask. */
  930. ETHERMTU, /*!< \brief Maximum size of a transmission unit, if_mtu. */
  931. 0, /*!< \brief Packet identifier, if_pkt_id. */
  932. 0, /*!< \brief Linked list of arp entries, arpTable. */
  933. 0, /*!< \brief Linked list of multicast address entries, if_mcast. */
  934. NutEtherInput, /*!< \brief Routine to pass received data to, if_recv(). */
  935. EmacOutput, /*!< \brief Driver output routine, if_send(). */
  936. NutEtherOutput, /*!< \brief Media output routine, if_output(). */
  937. NULL /*!< \brief Interface specific control function, if_ioctl(). */
  938. #ifdef NUT_PERFMON
  939. , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  940. #endif
  941. };
  942. /*!
  943. * \brief Device information structure.
  944. *
  945. * A pointer to this structure must be passed to NutRegisterDevice()
  946. * to bind this Ethernet device driver to the Nut/OS kernel.
  947. * An application may then call NutNetIfConfig() with the name \em eth0
  948. * of this driver to initialize the network interface.
  949. *
  950. */
  951. NUTDEVICE devStm32Emac = {
  952. 0, /*!< \brief Pointer to next device. */
  953. {'e', 't', 'h', '0', 0, 0, 0, 0, 0}, /*!< \brief Unique device name. */
  954. IFTYP_NET, /*!< \brief Type of device. */
  955. 0, /*!< \brief Base address. */
  956. 0, /*!< \brief First interrupt number. */
  957. &ifn_eth0, /*!< \brief Interface control block. */
  958. &dcb_eth0, /*!< \brief Driver control block. */
  959. EmacInit, /*!< \brief Driver initialization routine. */
  960. 0, /*!< \brief Driver specific control function. */
  961. 0, /*!< \brief Read from device. */
  962. 0, /*!< \brief Write to device. */
  963. #ifdef __HARVARD_ARCH__
  964. 0, /*!< \brief Write from program space data to device. */
  965. #endif
  966. 0, /*!< \brief Open a device or file. */
  967. 0, /*!< \brief Close a device or file. */
  968. 0, /*!< \brief Request file size. */
  969. 0, /*!< \brief Select function, optional, not yet implemented */
  970. };
  971. /*@}*/