EMAC_LPC18xx.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. /* --------------------------------------------------------------------------
  2. * Copyright (c) 2013-2016 ARM Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * $Date: 02. March 2016
  19. * $Revision: V2.6
  20. *
  21. * Driver: Driver_ETH_MAC0
  22. * Configured: via RTE_Device.h configuration file
  23. * Project: Ethernet Media Access (MAC) Driver for NXP LPC18xx
  24. * --------------------------------------------------------------------------
  25. * Use the following configuration settings in the middleware component
  26. * to connect to this driver.
  27. *
  28. * Configuration Setting Value
  29. * --------------------- -----
  30. * Connect to hardware via Driver_ETH_MAC# = 0
  31. * -------------------------------------------------------------------------- */
  32. /* History:
  33. * Version 2.6
  34. * - Corrected PowerControl function for conditional Power full (driver must be initialized)
  35. * Version 2.5
  36. * - Corrected return value of the ReadFrame function
  37. * Version 2.4
  38. * - Updated initialization, uninitialization and power procedures
  39. * Version 2.3
  40. * - Corrected return value of PHY_Read and PHY_Write functions on timeout
  41. * Version 2.2
  42. * - GetMacAddress function implemented in Ethernet driver
  43. * Version 2.1
  44. * - Added Sleep mode and Wake-up on Magic Packet
  45. * - Improved robustness and error control
  46. * - Added CLK0 pin option support
  47. * Version 2.0
  48. * - Based on API V2.00
  49. * - Added multicast MAC address filtering
  50. * Version 1.1
  51. * - Based on API V1.10 (namespace prefix ARM_ added)
  52. * Version 1.0
  53. * - Initial release
  54. */
  55. /* IEEE 1588 time stamping enable (PTP) */
  56. #ifndef EMAC_TIME_STAMP
  57. #define EMAC_TIME_STAMP 0
  58. #endif
  59. #include "EMAC_LPC18xx.h"
  60. #define ARM_ETH_MAC_DRV_VERSION ARM_DRIVER_VERSION_MAJOR_MINOR(2,6) /* driver version */
  61. /* Timeouts */
  62. #define PHY_TIMEOUT 200 /* PHY Register access timeout in us */
  63. /* EMAC Memory Buffer configuration */
  64. #define NUM_RX_BUF 4 /* 0x1800 for Rx (4*1536=6K) */
  65. #define NUM_TX_BUF 2 /* 0x0C00 for Tx (2*1536=3K) */
  66. #define ETH_BUF_SIZE 1536 /* ETH Receive/Transmit buffer size */
  67. /* EMAC core clock (system_LPC43xx.c) */
  68. extern uint32_t GetClockFreq (uint32_t clk_src);
  69. /* Ethernet Pin definitions */
  70. static const PIN_ID eth_pins[] = {
  71. { RTE_ENET_MDI_MDC_PORT, RTE_ENET_MDI_MDC_PIN, SCU_SFS_EZI | RTE_ENET_MDI_MDC_FUNC },
  72. { RTE_ENET_MDI_MDIO_PORT, RTE_ENET_MDI_MDIO_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_MDI_MDIO_FUNC },
  73. #if (RTE_ENET_RMII)
  74. { RTE_ENET_RMII_TXD0_PORT, RTE_ENET_RMII_TXD0_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_RMII_TXD0_FUNC },
  75. { RTE_ENET_RMII_TXD1_PORT, RTE_ENET_RMII_TXD1_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_RMII_TXD1_FUNC },
  76. { RTE_ENET_RMII_TX_EN_PORT, RTE_ENET_RMII_TX_EN_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_RMII_TX_EN_FUNC },
  77. { RTE_ENET_RMII_REF_CLK_PORT,RTE_ENET_RMII_REF_CLK_PIN,SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_RMII_REF_CLK_FUNC },
  78. { RTE_ENET_RMII_RXD0_PORT, RTE_ENET_RMII_RXD0_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_RMII_RXD0_FUNC },
  79. { RTE_ENET_RMII_RXD1_PORT, RTE_ENET_RMII_RXD1_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_RMII_RXD1_FUNC },
  80. { RTE_ENET_RMII_RX_DV_PORT, RTE_ENET_RMII_RX_DV_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_RMII_RX_DV_FUNC }
  81. #endif
  82. #if (RTE_ENET_MII)
  83. { RTE_ENET_MII_TXD0_PORT, RTE_ENET_MII_TXD0_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_MII_TXD0_FUNC },
  84. { RTE_ENET_MII_TXD1_PORT, RTE_ENET_MII_TXD1_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_MII_TXD1_FUNC },
  85. { RTE_ENET_MII_TXD2_PORT, RTE_ENET_MII_TXD2_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_MII_TXD2_FUNC },
  86. { RTE_ENET_MII_TXD3_PORT, RTE_ENET_MII_TXD3_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_MII_TXD3_FUNC },
  87. { RTE_ENET_MII_TX_EN_PORT, RTE_ENET_MII_TX_EN_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_MII_TX_EN_FUNC },
  88. { RTE_ENET_MII_TX_CLK_PORT, RTE_ENET_MII_TX_CLK_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_MII_TX_CLK_FUNC },
  89. #if (RTE_ENET_MII_TX_ER_PIN_EN)
  90. { RTE_ENET_MII_TX_ER_PORT, RTE_ENET_MII_TX_ER_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_MII_TX_ER_FUNC },
  91. #endif
  92. { RTE_ENET_MII_RXD0_PORT, RTE_ENET_MII_RXD0_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_MII_RXD0_FUNC },
  93. { RTE_ENET_MII_RXD1_PORT, RTE_ENET_MII_RXD1_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_MII_RXD1_FUNC },
  94. { RTE_ENET_MII_RXD2_PORT, RTE_ENET_MII_RXD2_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_MII_RXD2_FUNC },
  95. { RTE_ENET_MII_RXD3_PORT, RTE_ENET_MII_RXD3_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_MII_RXD3_FUNC },
  96. { RTE_ENET_MII_RX_DV_PORT, RTE_ENET_MII_RX_DV_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_MII_RX_DV_FUNC },
  97. { RTE_ENET_MII_RX_CLK_PORT, RTE_ENET_MII_RX_CLK_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_MII_RX_CLK_FUNC },
  98. { RTE_ENET_MII_RX_ER_PORT, RTE_ENET_MII_RX_ER_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_MII_RX_ER_FUNC },
  99. { RTE_ENET_MII_COL_PORT, RTE_ENET_MII_COL_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_MII_COL_FUNC },
  100. { RTE_ENET_MII_CRS_PORT, RTE_ENET_MII_CRS_PIN, SCU_SFS_EHS | SCU_SFS_EZI | RTE_ENET_MII_CRS_FUNC },
  101. #endif
  102. };
  103. /* Driver Version */
  104. static const ARM_DRIVER_VERSION DriverVersion = {
  105. ARM_ETH_MAC_API_VERSION,
  106. ARM_ETH_MAC_DRV_VERSION
  107. };
  108. /* Driver Capabilities */
  109. static const ARM_ETH_MAC_CAPABILITIES DriverCapabilities = {
  110. 0, /* checksum_offload_rx_ip4 */
  111. 0, /* checksum_offload_rx_ip6 */
  112. 0, /* checksum_offload_rx_udp */
  113. 0, /* checksum_offload_rx_tcp */
  114. 0, /* checksum_offload_rx_icmp */
  115. 0, /* checksum_offload_tx_ip4 */
  116. 0, /* checksum_offload_tx_ip6 */
  117. 0, /* checksum_offload_tx_udp */
  118. 0, /* checksum_offload_tx_tcp */
  119. 0, /* checksum_offload_tx_icmp */
  120. (RTE_ENET_RMII) ?
  121. ARM_ETH_INTERFACE_RMII :
  122. ARM_ETH_INTERFACE_MII, /* media_interface */
  123. 0, /* mac_address */
  124. 1, /* event_rx_frame */
  125. 1, /* event_tx_frame */
  126. 1, /* event_wakeup */
  127. (EMAC_TIME_STAMP) ? 1 : 0 /* precision_timer */
  128. };
  129. /* Local variables */
  130. static EMAC_CTRL emac_control = { 0 };
  131. #define emac (emac_control)
  132. static RX_Desc rx_desc[NUM_RX_BUF];
  133. static TX_Desc tx_desc[NUM_TX_BUF];
  134. static uint32_t rx_buf [NUM_RX_BUF][ETH_BUF_SIZE>>2];
  135. static uint32_t tx_buf [NUM_TX_BUF][ETH_BUF_SIZE>>2];
  136. /* Local functions */
  137. static void init_rx_desc (void);
  138. static void init_tx_desc (void);
  139. static uint32_t crc32_8bit_rev (uint32_t crc32, uint8_t val);
  140. static uint32_t crc32_data (const uint8_t *data, uint32_t len);
  141. /**
  142. \fn void init_rx_desc (void)
  143. \brief Initialize Rx DMA descriptors.
  144. \return none.
  145. */
  146. static void init_rx_desc (void) {
  147. uint32_t i,next;
  148. for (i = 0; i < NUM_RX_BUF; i++) {
  149. rx_desc[i].Stat = EMAC_RDES0_OWN;
  150. rx_desc[i].Ctrl = EMAC_RDES1_RCH | ETH_BUF_SIZE;
  151. rx_desc[i].Addr = (uint8_t *)&rx_buf[i];
  152. next = i + 1;
  153. if (next == NUM_RX_BUF) next = 0;
  154. rx_desc[i].Next = &rx_desc[next];
  155. }
  156. ENET->DMA_REC_DES_ADDR = (uint32_t)&rx_desc[0];
  157. emac.rx_index = 0;
  158. }
  159. /**
  160. \fn void init_tx_desc (void)
  161. \brief Initialize Tx DMA descriptors.
  162. \return none.
  163. */
  164. static void init_tx_desc (void) {
  165. uint32_t i,next;
  166. for (i = 0; i < NUM_TX_BUF; i++) {
  167. tx_desc[i].CtrlStat = EMAC_TDES0_TCH | EMAC_TDES0_LS | EMAC_TDES0_FS;
  168. tx_desc[i].Addr = (uint8_t *)&tx_buf[i];
  169. next = i + 1;
  170. if (next == NUM_TX_BUF) next = 0;
  171. tx_desc[i].Next = &tx_desc[next];
  172. }
  173. ENET->DMA_TRANS_DES_ADDR = (uint32_t)&tx_desc[0];
  174. emac.tx_index = 0;
  175. }
  176. /**
  177. \fn uint32_t crc32_8bit_rev (uint32_t crc32, uint8_t val)
  178. \brief Calculate 32-bit CRC (Polynom: 0x04C11DB7, data bit-reversed).
  179. \param[in] crc32 CRC initial value
  180. \param[in] val Input value
  181. \return Calculated CRC value
  182. */
  183. static uint32_t crc32_8bit_rev (uint32_t crc32, uint8_t val) {
  184. uint32_t n;
  185. crc32 ^= __RBIT (val);
  186. for (n = 8; n; n--) {
  187. if (crc32 & 0x80000000) {
  188. crc32 <<= 1;
  189. crc32 ^= 0x04C11DB7;
  190. } else {
  191. crc32 <<= 1;
  192. }
  193. }
  194. return (crc32);
  195. }
  196. /**
  197. \fn uint32_t crc32_data (const uint8_t *data, uint32_t len)
  198. \brief Calculate standard 32-bit Ethernet CRC.
  199. \param[in] data Pointer to buffer containing the data
  200. \param[in] len Data length in bytes
  201. \return Calculated CRC value
  202. */
  203. static uint32_t crc32_data (const uint8_t *data, uint32_t len) {
  204. uint32_t crc;
  205. for (crc = 0xFFFFFFFF; len; len--) {
  206. crc = crc32_8bit_rev (crc, *data++);
  207. }
  208. return (crc ^ 0xFFFFFFFF);
  209. }
  210. /* Ethernet Driver functions */
  211. /**
  212. \fn ARM_DRIVER_VERSION GetVersion (void)
  213. \brief Get driver version.
  214. \return \ref ARM_DRIVER_VERSION
  215. */
  216. static ARM_DRIVER_VERSION GetVersion (void) {
  217. return DriverVersion;
  218. }
  219. /**
  220. \fn ARM_ETH_MAC_CAPABILITIES GetCapabilities (void)
  221. \brief Get driver capabilities.
  222. \return \ref ARM_ETH_MAC_CAPABILITIES
  223. */
  224. static ARM_ETH_MAC_CAPABILITIES GetCapabilities (void) {
  225. return DriverCapabilities;
  226. }
  227. /**
  228. \fn int32_t Initialize (ARM_ETH_MAC_SignalEvent_t cb_event)
  229. \brief Initialize Ethernet MAC Device.
  230. \param[in] cb_event Pointer to \ref ARM_ETH_MAC_SignalEvent
  231. \return \ref execution_status
  232. */
  233. static int32_t Initialize (ARM_ETH_MAC_SignalEvent_t cb_event) {
  234. const PIN_ID *pin;
  235. if (emac.flags & EMAC_FLAG_INIT) { return ARM_DRIVER_OK; }
  236. /* Configure EMAC pins */
  237. for (pin = eth_pins; pin != &eth_pins[sizeof(eth_pins)/sizeof(PIN_ID)]; pin++) {
  238. if (pin->port == 0x10) {
  239. SCU_CLK_PinConfigure (pin->num, pin->config_val);
  240. continue;
  241. }
  242. SCU_PinConfigure(pin->port, pin->num, pin->config_val);
  243. }
  244. /* Clear control structure */
  245. memset (&emac, 0, sizeof (EMAC_CTRL));
  246. emac.cb_event = cb_event;
  247. emac.flags = EMAC_FLAG_INIT;
  248. return ARM_DRIVER_OK;
  249. }
  250. /**
  251. \fn int32_t Uninitialize (void)
  252. \brief De-initialize Ethernet MAC Device.
  253. \return \ref execution_status
  254. */
  255. static int32_t Uninitialize (void) {
  256. const PIN_ID *pin;
  257. emac.flags = 0;
  258. /* Unconfigure ethernet pins */
  259. for (pin = eth_pins; pin != &eth_pins[sizeof(eth_pins)/sizeof(PIN_ID)]; pin++) {
  260. if (pin->port == 0x10) {
  261. SCU_CLK_PinConfigure (pin->num, 0);
  262. continue;
  263. }
  264. SCU_PinConfigure(pin->port, pin->num, 0);
  265. }
  266. return ARM_DRIVER_OK;
  267. }
  268. /**
  269. \fn int32_t PowerControl (ARM_POWER_STATE state)
  270. \brief Control Ethernet MAC Device Power.
  271. \param[in] state Power state
  272. \return \ref execution_status
  273. */
  274. static int32_t PowerControl (ARM_POWER_STATE state) {
  275. uint32_t clk;
  276. switch (state) {
  277. case ARM_POWER_OFF:
  278. /* Disable EMAC interrupts */
  279. NVIC_DisableIRQ(ETHERNET_IRQn);
  280. /* Reset EMAC peripheral */
  281. LPC_RGU->RESET_CTRL0 = RGU_RESET_EMAC;
  282. while (!(LPC_RGU->RESET_ACTIVE_STATUS0 & RGU_RESET_EMAC));
  283. /* Disable EMAC peripheral clock */
  284. LPC_CCU1->CLK_M3_ETHERNET_CFG &= ~CCU_CLK_CFG_RUN;
  285. emac.flags &= ~EMAC_FLAG_POWER;
  286. break;
  287. case ARM_POWER_LOW:
  288. return ARM_DRIVER_ERROR_UNSUPPORTED;
  289. case ARM_POWER_FULL:
  290. if ((emac.flags & EMAC_FLAG_INIT) == 0) { return ARM_DRIVER_ERROR; }
  291. if ((emac.flags & EMAC_FLAG_POWER) != 0) { return ARM_DRIVER_OK; }
  292. /* Enable EMAC peripheral clock */
  293. LPC_CCU1->CLK_M3_ETHERNET_CFG |= CCU_CLK_CFG_AUTO | CCU_CLK_CFG_RUN;
  294. while (!(LPC_CCU1->CLK_M3_ETHERNET_STAT & CCU_CLK_STAT_RUN));
  295. /* Configure Ethernet PHY interface mode (MII/RMII) */
  296. /* EMAC must be reset after changing PHY interface! */
  297. #if (RTE_ENET_RMII)
  298. LPC_CREG->CREG6 = (LPC_CREG->CREG6 & ~EMAC_CREG6_ETH_MASK) | EMAC_CREG6_ETH_RMII;
  299. #else
  300. LPC_CREG->CREG6 = (LPC_CREG->CREG6 & ~EMAC_CREG6_ETH_MASK) | EMAC_CREG6_ETH_MII;
  301. #endif
  302. /* Reset EMAC peripheral */
  303. LPC_RGU->RESET_CTRL0 = RGU_RESET_EMAC;
  304. while (!(LPC_RGU->RESET_ACTIVE_STATUS0 & RGU_RESET_EMAC));
  305. /* Soft reset EMAC DMA controller */
  306. ENET->DMA_BUS_MODE |= EMAC_DBMR_SWR;
  307. while (ENET->DMA_BUS_MODE & EMAC_DBMR_SWR);
  308. /* MDC clock range selection */
  309. clk = GetClockFreq (CLK_SRC_PLL1);
  310. if (clk >= 150000000) emac.mmar_cr_val = EMAC_MMAR_CR_Div102;
  311. else if (clk >= 100000000) emac.mmar_cr_val = EMAC_MMAR_CR_Div62;
  312. else if (clk >= 60000000) emac.mmar_cr_val = EMAC_MMAR_CR_Div42;
  313. else if (clk >= 35000000) emac.mmar_cr_val = EMAC_MMAR_CR_Div26;
  314. else if (clk >= 25000000) emac.mmar_cr_val = EMAC_MMAR_CR_Div16;
  315. else return ARM_DRIVER_ERROR_UNSUPPORTED;
  316. ENET->MAC_MII_ADDR = emac.mmar_cr_val;
  317. #if (EMAC_TIME_STAMP)
  318. /* Enhanced DMA descriptor enable */
  319. ENET->DMA_BUS_MODE |= EMAC_DBMR_ATDS;
  320. /* Set clock accuracy to 20ns (50MHz) or 50ns (20MHz) */
  321. if (clk >= 51000000) {
  322. ENET->SUBSECOND_INCR = 20;
  323. ENET->ADDEND = (50000000ULL << 32) / clk;
  324. }
  325. else {
  326. ENET->SUBSECOND_INCR = 50;
  327. ENET->ADDEND = (20000000ULL << 32) / clk;
  328. }
  329. /* Enable timestamp fine update */
  330. ENET->MAC_TIMESTP_CTRL = EMAC_MTCR_TSIPV4E | EMAC_MTCR_TSIPV6E |
  331. EMAC_MTCR_TSCTRL | EMAC_MTCR_TSADDR |
  332. EMAC_MTCR_TSCFUP | EMAC_MTCR_TSENA;
  333. emac.tx_ts_index = 0;
  334. #endif
  335. /* Initialize MAC configuration */
  336. ENET->MAC_CONFIG = EMAC_MCR_DO | EMAC_MCR_PS;
  337. /* Initialize Filter registers */
  338. ENET->MAC_FRAME_FILTER = EMAC_MFFR_DBF;
  339. ENET->MAC_FLOW_CTRL = EMAC_MFCR_DZPQ;
  340. /* Initialize Address register */
  341. ENET->MAC_ADDR0_HIGH = 0x00000000;
  342. ENET->MAC_ADDR0_LOW = 0x00000000;
  343. /* Disable MAC interrupts */
  344. ENET->MAC_INTR_MASK = EMAC_MIMR_PMTIM | EMAC_MIMR_TSIM;
  345. /* Initialize DMA Descriptors */
  346. init_rx_desc ();
  347. init_tx_desc ();
  348. /* Enable DMA interrupts */
  349. ENET->DMA_STAT = 0xFFFFFFFF;
  350. ENET->DMA_INT_EN = EMAC_DIER_NIE | EMAC_DIER_RIE | EMAC_DIER_TIE;
  351. /* Enable ethernet interrupts */
  352. NVIC_ClearPendingIRQ(ETHERNET_IRQn);
  353. NVIC_EnableIRQ(ETHERNET_IRQn);
  354. emac.frame_end = NULL;
  355. emac.flags |= EMAC_FLAG_POWER;
  356. break;
  357. default:
  358. return ARM_DRIVER_ERROR_UNSUPPORTED;
  359. }
  360. return ARM_DRIVER_OK;
  361. }
  362. /**
  363. \fn int32_t GetMacAddress (ARM_ETH_MAC_ADDR *ptr_addr)
  364. \brief Get Ethernet MAC Address.
  365. \param[in] ptr_addr Pointer to address
  366. \return \ref execution_status
  367. */
  368. static int32_t GetMacAddress (ARM_ETH_MAC_ADDR *ptr_addr) {
  369. uint32_t val;
  370. if (!ptr_addr) {
  371. /* Invalid parameters */
  372. return ARM_DRIVER_ERROR_PARAMETER;
  373. }
  374. if (!(emac.flags & EMAC_FLAG_POWER)) {
  375. /* Driver not yet powered */
  376. return ARM_DRIVER_ERROR;
  377. }
  378. val = ENET->MAC_ADDR0_HIGH;
  379. ptr_addr->b[5] = (uint8_t)(val >> 8);
  380. ptr_addr->b[4] = (uint8_t)(val);
  381. val = ENET->MAC_ADDR0_LOW;
  382. ptr_addr->b[3] = (uint8_t)(val >> 24);
  383. ptr_addr->b[2] = (uint8_t)(val >> 16);
  384. ptr_addr->b[1] = (uint8_t)(val >> 8);
  385. ptr_addr->b[0] = (uint8_t)(val);
  386. return ARM_DRIVER_OK;
  387. }
  388. /**
  389. \fn int32_t SetMacAddress (const ARM_ETH_MAC_ADDR *ptr_addr)
  390. \brief Set Ethernet MAC Address.
  391. \param[in] ptr_addr Pointer to address
  392. \return \ref execution_status
  393. */
  394. static int32_t SetMacAddress (const ARM_ETH_MAC_ADDR *ptr_addr) {
  395. if (!ptr_addr) {
  396. /* Invalid parameters */
  397. return ARM_DRIVER_ERROR_PARAMETER;
  398. }
  399. if (!(emac.flags & EMAC_FLAG_POWER)) {
  400. /* Driver not yet powered */
  401. return ARM_DRIVER_ERROR;
  402. }
  403. /* Set Ethernet MAC Address registers */
  404. ENET->MAC_ADDR0_HIGH = (ptr_addr->b[5] << 8) | ptr_addr->b[4];
  405. ENET->MAC_ADDR0_LOW = (ptr_addr->b[3] << 24) | (ptr_addr->b[2] << 16) |
  406. (ptr_addr->b[1] << 8) | ptr_addr->b[0];
  407. return ARM_DRIVER_OK;
  408. }
  409. /**
  410. \fn int32_t SetAddressFilter (const ARM_ETH_MAC_ADDR *ptr_addr,
  411. uint32_t num_addr)
  412. \brief Configure Address Filter.
  413. \param[in] ptr_addr Pointer to addresses
  414. \param[in] num_addr Number of addresses to configure
  415. \return \ref execution_status
  416. */
  417. static int32_t SetAddressFilter (const ARM_ETH_MAC_ADDR *ptr_addr, uint32_t num_addr) {
  418. uint32_t crc;
  419. if (!ptr_addr && num_addr) {
  420. /* Invalid parameters */
  421. return ARM_DRIVER_ERROR_PARAMETER;
  422. }
  423. if (!(emac.flags & EMAC_FLAG_POWER)) {
  424. /* Driver not yet powered */
  425. return ARM_DRIVER_ERROR;
  426. }
  427. ENET->MAC_FRAME_FILTER &= ~(EMAC_MFFR_HPF | EMAC_MFFR_HMC);
  428. ENET->MAC_HASHTABLE_HIGH = 0x00000000;
  429. ENET->MAC_HASHTABLE_LOW = 0x00000000;
  430. if (num_addr == 0) {
  431. return ARM_DRIVER_OK;
  432. }
  433. /* Calculate 64-bit Hash table for MAC addresses */
  434. for ( ; num_addr; ptr_addr++, num_addr--) {
  435. crc = crc32_data (&ptr_addr->b[0], 6) >> 26;
  436. if (crc & 0x20) {
  437. ENET->MAC_HASHTABLE_HIGH |= (1 << (crc & 0x1F));
  438. }
  439. else {
  440. ENET->MAC_HASHTABLE_LOW |= (1 << crc);
  441. }
  442. }
  443. /* Enable both, unicast and hash address filtering */
  444. ENET->MAC_FRAME_FILTER |= EMAC_MFFR_HPF | EMAC_MFFR_HMC;
  445. return ARM_DRIVER_OK;
  446. }
  447. /**
  448. \fn int32_t SendFrame (const uint8_t *frame, uint32_t len, uint32_t flags)
  449. \brief Send Ethernet frame.
  450. \param[in] frame Pointer to frame buffer with data to send
  451. \param[in] len Frame buffer length in bytes
  452. \param[in] flags Frame transmit flags (see ARM_ETH_MAC_TX_FRAME_...)
  453. \return \ref execution_status
  454. */
  455. static int32_t SendFrame (const uint8_t *frame, uint32_t len, uint32_t flags) {
  456. uint8_t *dst;
  457. uint32_t ctrl;
  458. if (!frame || !len) {
  459. /* Invalid parameters */
  460. return ARM_DRIVER_ERROR_PARAMETER;
  461. }
  462. if (!(emac.flags & EMAC_FLAG_POWER)) {
  463. /* Driver not yet powered */
  464. return ARM_DRIVER_ERROR;
  465. }
  466. dst = emac.frame_end;
  467. if (dst == NULL) {
  468. /* Start of a new transmit frame */
  469. if (tx_desc[emac.tx_index].CtrlStat & EMAC_TDES0_OWN) {
  470. /* Transmitter is busy, wait */
  471. return ARM_DRIVER_ERROR_BUSY;
  472. }
  473. dst = tx_desc[emac.tx_index].Addr;
  474. tx_desc[emac.tx_index].Size = len;
  475. }
  476. else {
  477. /* Sending data fragments in progress */
  478. tx_desc[emac.tx_index].Size += len;
  479. }
  480. /* Fast-copy data fragments to EMAC-DMA buffer */
  481. for ( ; len > 7; dst += 8, frame += 8, len -= 8) {
  482. ((__packed uint32_t *)dst)[0] = ((__packed uint32_t *)frame)[0];
  483. ((__packed uint32_t *)dst)[1] = ((__packed uint32_t *)frame)[1];
  484. }
  485. /* Copy remaining 7 bytes */
  486. for ( ; len > 1; dst += 2, frame += 2, len -= 2) {
  487. ((__packed uint16_t *)dst)[0] = ((__packed uint16_t *)frame)[0];
  488. }
  489. if (len > 0) dst++[0] = frame++[0];
  490. if (flags & ARM_ETH_MAC_TX_FRAME_FRAGMENT) {
  491. /* More data to come, remember current write position */
  492. emac.frame_end = dst;
  493. return ARM_DRIVER_OK;
  494. }
  495. /* Frame is now ready, send it to DMA */
  496. ctrl = tx_desc[emac.tx_index].CtrlStat & ~(EMAC_TDES0_IC | EMAC_TDES0_TTSE);
  497. if (flags & ARM_ETH_MAC_TX_FRAME_EVENT) ctrl |= EMAC_TDES0_IC;
  498. #if (EMAC_TIME_STAMP)
  499. if (flags & ARM_ETH_MAC_TX_FRAME_TIMESTAMP) ctrl |= EMAC_TDES0_TTSE;
  500. emac.tx_ts_index = emac.tx_index;
  501. #endif
  502. tx_desc[emac.tx_index].CtrlStat = ctrl | EMAC_TDES0_OWN;
  503. if (++emac.tx_index == NUM_TX_BUF) emac.tx_index = 0;
  504. emac.frame_end = NULL;
  505. /* Start frame transmission */
  506. ENET->DMA_STAT = EMAC_DSR_TPS;
  507. ENET->DMA_TRANS_POLL_DEMAND = 0;
  508. return ARM_DRIVER_OK;
  509. }
  510. /**
  511. \fn int32_t ReadFrame (uint8_t *frame, uint32_t len)
  512. \brief Read data of received Ethernet frame.
  513. \param[in] frame Pointer to frame buffer for data to read into
  514. \param[in] len Frame buffer length in bytes
  515. \return number of data bytes read or execution status
  516. - value >= 0: number of data bytes read
  517. - value < 0: error occurred, value is execution status as defined with \ref execution_status
  518. */
  519. static int32_t ReadFrame (uint8_t *frame, uint32_t len) {
  520. uint8_t const *src;
  521. int32_t cnt = (int32_t)len;
  522. if (!frame && len) {
  523. /* Invalid parameters */
  524. return ARM_DRIVER_ERROR_PARAMETER;
  525. }
  526. if (!(emac.flags & EMAC_FLAG_POWER)) {
  527. /* Driver not yet powered */
  528. return ARM_DRIVER_ERROR;
  529. }
  530. /* Fast-copy data to packet buffer */
  531. src = rx_desc[emac.rx_index].Addr;
  532. for ( ; len > 7; frame += 8, src += 8, len -= 8) {
  533. ((__packed uint32_t *)frame)[0] = ((uint32_t *)src)[0];
  534. ((__packed uint32_t *)frame)[1] = ((uint32_t *)src)[1];
  535. }
  536. /* Copy remaining 7 bytes */
  537. for ( ; len > 1; frame += 2, src += 2, len -= 2) {
  538. ((__packed uint16_t *)frame)[0] = ((uint16_t *)src)[0];
  539. }
  540. if (len > 0) frame[0] = src[0];
  541. /* Return this block back to EMAC-DMA */
  542. rx_desc[emac.rx_index].Stat = EMAC_RDES0_OWN;
  543. if (++emac.rx_index == NUM_RX_BUF) emac.rx_index = 0;
  544. if (ENET->DMA_STAT & EMAC_DSR_RU) {
  545. /* Receive buffer unavailable, resume DMA */
  546. ENET->DMA_STAT = EMAC_DSR_RU;
  547. ENET->DMA_REC_POLL_DEMAND = 0;
  548. }
  549. return (cnt);
  550. }
  551. /**
  552. \fn uint32_t GetRxFrameSize (void)
  553. \brief Get size of received Ethernet frame.
  554. \return number of bytes in received frame
  555. */
  556. static uint32_t GetRxFrameSize (void) {
  557. uint32_t stat;
  558. if (!(emac.flags & EMAC_FLAG_POWER)) {
  559. /* Driver not yet powered */
  560. return (0);
  561. }
  562. stat = rx_desc[emac.rx_index].Stat;
  563. if (stat & EMAC_RDES0_OWN) {
  564. /* Owned by DMA */
  565. return (0);
  566. }
  567. if ((stat & EMAC_RDES0_ES) || !(stat & EMAC_RDES0_FS) || !(stat & EMAC_RDES0_LS)) {
  568. /* Error, this block is invalid */
  569. return (0xFFFFFFFF);
  570. }
  571. return (((stat & EMAC_RDES0_FL) >> 16) - 4);
  572. }
  573. /**
  574. \fn int32_t GetRxFrameTime (ARM_ETH_MAC_TIME *time)
  575. \brief Get time of received Ethernet frame.
  576. \param[in] time Pointer to time structure for data to read into
  577. \return \ref execution_status
  578. */
  579. static int32_t GetRxFrameTime (ARM_ETH_MAC_TIME *time) {
  580. #if (EMAC_TIME_STAMP)
  581. RX_Desc *rxd;
  582. if (!time) {
  583. /* Invalid parameters */
  584. return ARM_DRIVER_ERROR_PARAMETER;
  585. }
  586. rxd = &rx_desc[emac.rx_index];
  587. if (rxd->Stat & EMAC_RDES0_OWN) {
  588. /* Owned by DMA */
  589. return ARM_DRIVER_ERROR_BUSY;
  590. }
  591. time->ns = rxd->TimeLo;
  592. time->sec = rxd->TimeHi;
  593. return ARM_DRIVER_OK;
  594. #else
  595. return ARM_DRIVER_ERROR_UNSUPPORTED;
  596. #endif
  597. }
  598. /**
  599. \fn int32_t GetTxFrameTime (ARM_ETH_MAC_TIME *time)
  600. \brief Get time of transmitted Ethernet frame.
  601. \param[in] time Pointer to time structure for data to read into
  602. \return \ref execution_status
  603. */
  604. static int32_t GetTxFrameTime (ARM_ETH_MAC_TIME *time) {
  605. #if (EMAC_TIME_STAMP)
  606. TX_Desc *txd;
  607. if (!time) {
  608. /* Invalid parameters */
  609. return ARM_DRIVER_ERROR_PARAMETER;
  610. }
  611. txd = &tx_desc[emac.tx_ts_index];
  612. if (txd->CtrlStat & EMAC_TDES0_OWN) {
  613. /* Owned by DMA */
  614. return ARM_DRIVER_ERROR_BUSY;
  615. }
  616. if (!(txd->CtrlStat & EMAC_TDES0_TTSS)) {
  617. /* No transmit time stamp available */
  618. return ARM_DRIVER_ERROR;
  619. }
  620. time->ns = txd->TimeLo;
  621. time->sec = txd->TimeHi;
  622. return ARM_DRIVER_OK;
  623. #else
  624. return ARM_DRIVER_ERROR_UNSUPPORTED;
  625. #endif
  626. }
  627. /**
  628. \fn int32_t Control (uint32_t control, uint32_t arg)
  629. \brief Control Ethernet Interface.
  630. \param[in] control Operation
  631. \param[in] arg Argument of operation (optional)
  632. \return \ref execution_status
  633. */
  634. static int32_t Control (uint32_t control, uint32_t arg) {
  635. uint32_t maccr;
  636. uint32_t macffr;
  637. if (!(emac.flags & EMAC_FLAG_POWER)) {
  638. /* Driver not powered */
  639. return ARM_DRIVER_ERROR;
  640. }
  641. switch (control) {
  642. case ARM_ETH_MAC_CONFIGURE:
  643. maccr = ENET->MAC_CONFIG & ~(EMAC_MCR_FES | EMAC_MCR_DM | EMAC_MCR_LM);
  644. /* Configure 100MBit/10MBit mode */
  645. switch (arg & ARM_ETH_MAC_SPEED_Msk) {
  646. case ARM_ETH_MAC_SPEED_10M:
  647. #if (RTE_ENET_RMII)
  648. /* RMII Half Duplex Collision detection does not work */
  649. maccr |= EMAC_MCR_DM;
  650. #endif
  651. break;
  652. case ARM_ETH_SPEED_100M:
  653. maccr |= EMAC_MCR_FES;
  654. break;
  655. default:
  656. return ARM_DRIVER_ERROR_UNSUPPORTED;
  657. }
  658. /* Configure Half/Full duplex mode */
  659. switch (arg & ARM_ETH_MAC_DUPLEX_Msk) {
  660. case ARM_ETH_MAC_DUPLEX_FULL:
  661. maccr |= EMAC_MCR_DM;
  662. break;
  663. }
  664. /* Configure loopback mode */
  665. if (arg & ARM_ETH_MAC_LOOPBACK) {
  666. maccr |= EMAC_MCR_LM;
  667. }
  668. if ((arg & ARM_ETH_MAC_CHECKSUM_OFFLOAD_RX) ||
  669. (arg & ARM_ETH_MAC_CHECKSUM_OFFLOAD_TX)) {
  670. /* Checksum offload is disabled in the driver */
  671. return ARM_DRIVER_ERROR_UNSUPPORTED;
  672. }
  673. ENET->MAC_CONFIG = maccr;
  674. macffr = ENET->MAC_FRAME_FILTER & ~(EMAC_MFFR_PR | EMAC_MFFR_PAM | EMAC_MFFR_DBF);
  675. /* Enable broadcast frame receive */
  676. if (!(arg & ARM_ETH_MAC_ADDRESS_BROADCAST)) {
  677. macffr |= EMAC_MFFR_DBF;
  678. }
  679. /* Enable all multicast frame receive */
  680. if (arg & ARM_ETH_MAC_ADDRESS_MULTICAST) {
  681. macffr |= EMAC_MFFR_PAM;
  682. }
  683. /* Enable promiscuous mode (no filtering) */
  684. if (arg & ARM_ETH_MAC_ADDRESS_ALL) {
  685. macffr |= EMAC_MFFR_PR;
  686. }
  687. ENET->MAC_FRAME_FILTER = macffr;
  688. break;
  689. case ARM_ETH_MAC_CONTROL_TX:
  690. /* Enable/disable MAC transmitter */
  691. if (arg != 0) {
  692. ENET->MAC_CONFIG |= EMAC_MCR_TE;
  693. ENET->DMA_OP_MODE |= EMAC_DOMR_ST;
  694. }
  695. else {
  696. ENET->DMA_OP_MODE &= ~EMAC_DOMR_ST;
  697. ENET->MAC_CONFIG &= ~EMAC_MCR_TE;
  698. }
  699. break;
  700. case ARM_ETH_MAC_CONTROL_RX:
  701. /* Enable/disable MAC receiver */
  702. if (arg != 0) {
  703. ENET->MAC_CONFIG |= EMAC_MCR_RE;
  704. ENET->DMA_OP_MODE |= EMAC_DOMR_SR;
  705. }
  706. else {
  707. ENET->DMA_OP_MODE &= ~EMAC_DOMR_SR;
  708. ENET->MAC_CONFIG &= ~EMAC_MCR_RE;
  709. }
  710. break;
  711. case ARM_ETH_MAC_FLUSH:
  712. /* Flush Tx and Rx buffers */
  713. if (arg & ARM_ETH_MAC_FLUSH_RX) {
  714. /* Stop/Start DMA Receive */
  715. uint32_t domr = ENET->DMA_OP_MODE;
  716. ENET->DMA_OP_MODE &= ~EMAC_DOMR_SR;
  717. init_rx_desc ();
  718. ENET->DMA_OP_MODE = domr;
  719. }
  720. if (arg & ARM_ETH_MAC_FLUSH_TX) {
  721. /* Stop/Start DMA Transmit */
  722. uint32_t domr = ENET->DMA_OP_MODE;
  723. ENET->DMA_OP_MODE &= ~EMAC_DOMR_ST;
  724. /* Flush transmit FIFO */
  725. ENET->DMA_OP_MODE |= EMAC_DOMR_FTF;
  726. init_tx_desc ();
  727. ENET->DMA_OP_MODE = domr;
  728. }
  729. break;
  730. case ARM_ETH_MAC_SLEEP:
  731. /* Enable/disable Sleep mode */
  732. if (arg != 0) {
  733. /* Enable Power Management interrupts */
  734. ENET->MAC_INTR_MASK &= ~EMAC_MIMR_PMTIM;
  735. /* Enter Power-down, Magic packet enable */
  736. ENET->MAC_PMT_CTRL_STAT = EMAC_PMTR_MPE | EMAC_PMTR_PD;
  737. }
  738. else {
  739. /* Disable Power Management interrupts */
  740. ENET->MAC_INTR_MASK |= EMAC_MIMR_PMTIM;
  741. ENET->MAC_PMT_CTRL_STAT = 0x00000000;
  742. }
  743. break;
  744. case ARM_ETH_MAC_VLAN_FILTER:
  745. /* Configure VLAN filter */
  746. ENET->MAC_VLAN_TAG = arg;
  747. break;
  748. default:
  749. return ARM_DRIVER_ERROR_UNSUPPORTED;
  750. }
  751. return ARM_DRIVER_OK;
  752. }
  753. /**
  754. \fn int32_t ControlTimer (uint32_t control, ARM_ETH_MAC_TIME *time)
  755. \brief Control Precision Timer.
  756. \param[in] control Operation
  757. \param[in] time Pointer to time structure
  758. \return \ref execution_status
  759. */
  760. static int32_t ControlTimer (uint32_t control, ARM_ETH_MAC_TIME *time) {
  761. #if (EMAC_TIME_STAMP)
  762. if (!(emac.flags & EMAC_FLAG_POWER)) {
  763. /* Driver not powered */
  764. return ARM_DRIVER_ERROR;
  765. }
  766. switch (control) {
  767. case ARM_ETH_MAC_TIMER_GET_TIME:
  768. /* Get current time */
  769. time->sec = ENET->SECONDS;
  770. time->ns = ENET->NANOSECONDS;
  771. break;
  772. case ARM_ETH_MAC_TIMER_SET_TIME:
  773. /* Set new time */
  774. ENET->SECONDSUPDATE = time->sec;
  775. ENET->NANOSECONDSUPDATE = time->ns;
  776. /* Initialize precision timer */
  777. ENET->MAC_TIMESTP_CTRL |= EMAC_MTCR_TSINIT;
  778. break;
  779. case ARM_ETH_MAC_TIMER_INC_TIME:
  780. /* Increment current time */
  781. ENET->SECONDSUPDATE = time->sec;
  782. ENET->NANOSECONDSUPDATE = time->ns;
  783. /* Update precision timer */
  784. ENET->MAC_TIMESTP_CTRL |= EMAC_MTCR_TSUPDT;
  785. break;
  786. case ARM_ETH_MAC_TIMER_DEC_TIME:
  787. /* Decrement current time */
  788. ENET->SECONDSUPDATE = time->sec;
  789. ENET->NANOSECONDSUPDATE = time->ns | 0x80000000;
  790. /* Update precision timer */
  791. ENET->MAC_TIMESTP_CTRL |= EMAC_MTCR_TSUPDT;
  792. break;
  793. case ARM_ETH_MAC_TIMER_SET_ALARM:
  794. /* Set alarm time */
  795. ENET->TARGETSECONDS = time->sec;
  796. ENET->TARGETNANOSECONDS = time->ns;
  797. /* Enable timestamp interrupt trigger */
  798. ENET->MAC_TIMESTP_CTRL |= EMAC_MTCR_TSTRIG;
  799. if (time->sec || time->ns) {
  800. /* Enable timestamp interrupts */
  801. ENET->MAC_INTR_MASK &= ~EMAC_MIMR_TSIM;
  802. break;
  803. }
  804. /* Disable timestamp interrupts */
  805. ENET->MAC_INTR_MASK |= EMAC_MIMR_TSIM;
  806. break;
  807. case ARM_ETH_MAC_TIMER_ADJUST_CLOCK:
  808. /* Adjust current time, fine correction */
  809. /* Correction factor is Q31 (0x80000000 = 1.000000000) */
  810. ENET->ADDEND = ((uint64_t)time->ns * ENET->ADDEND) >> 31;
  811. /* Update addend register */
  812. ENET->MAC_TIMESTP_CTRL |= EMAC_MTCR_TSADDR;
  813. break;
  814. default:
  815. return ARM_DRIVER_ERROR_UNSUPPORTED;
  816. }
  817. return ARM_DRIVER_OK;
  818. #else
  819. return ARM_DRIVER_ERROR_UNSUPPORTED;
  820. #endif
  821. }
  822. /**
  823. \fn int32_t PHY_Read (uint8_t phy_addr, uint8_t reg_addr, uint16_t *data)
  824. \brief Read Ethernet PHY Register through Management Interface.
  825. \param[in] phy_addr 5-bit device address
  826. \param[in] reg_addr 5-bit register address
  827. \param[out] data Pointer where the result is written to
  828. \return \ref execution_status
  829. */
  830. static int32_t PHY_Read (uint8_t phy_addr, uint8_t reg_addr, uint16_t *data) {
  831. uint32_t tick;
  832. if (!data) {
  833. /* Invalid parameter */
  834. return ARM_DRIVER_ERROR_PARAMETER;
  835. }
  836. if (!(emac.flags & EMAC_FLAG_POWER)) {
  837. /* Driver not powered */
  838. return ARM_DRIVER_ERROR;
  839. }
  840. ENET->MAC_MII_ADDR = emac.mmar_cr_val | EMAC_MMAR_GB |
  841. (phy_addr << 11) | (reg_addr << 6);
  842. /* Wait until operation completed */
  843. tick = osKernelSysTick();
  844. do {
  845. if (!(ENET->MAC_MII_ADDR & EMAC_MMAR_GB)) {
  846. *data = ENET->MAC_MII_DATA & EMAC_MMDR_GD;
  847. return ARM_DRIVER_OK;
  848. }
  849. } while ((osKernelSysTick() - tick) < osKernelSysTickMicroSec(PHY_TIMEOUT));
  850. if (!(ENET->MAC_MII_ADDR & EMAC_MMAR_GB)) {
  851. *data = ENET->MAC_MII_DATA & EMAC_MMDR_GD;
  852. return ARM_DRIVER_OK;
  853. }
  854. return ARM_DRIVER_ERROR_TIMEOUT;
  855. }
  856. /**
  857. \fn int32_t PHY_Write (uint8_t phy_addr, uint8_t reg_addr, uint16_t data)
  858. \brief Write Ethernet PHY Register through Management Interface.
  859. \param[in] phy_addr 5-bit device address
  860. \param[in] reg_addr 5-bit register address
  861. \param[in] data 16-bit data to write
  862. \return \ref execution_status
  863. */
  864. static int32_t PHY_Write (uint8_t phy_addr, uint8_t reg_addr, uint16_t data) {
  865. uint32_t tick;
  866. if (!(emac.flags & EMAC_FLAG_POWER)) {
  867. /* Driver not powered */
  868. return ARM_DRIVER_ERROR;
  869. }
  870. ENET->MAC_MII_DATA = data;
  871. ENET->MAC_MII_ADDR = emac.mmar_cr_val | EMAC_MMAR_GB | EMAC_MMAR_W |
  872. (phy_addr << 11) | (reg_addr << 6);
  873. /* Wait until operation completed */
  874. tick = osKernelSysTick();
  875. do {
  876. if (!(ENET->MAC_MII_ADDR & EMAC_MMAR_GB)) {
  877. return ARM_DRIVER_OK;
  878. }
  879. } while ((osKernelSysTick() - tick) < osKernelSysTickMicroSec(PHY_TIMEOUT));
  880. if (!(ENET->MAC_MII_ADDR & EMAC_MMAR_GB)) {
  881. return ARM_DRIVER_OK;
  882. }
  883. return ARM_DRIVER_ERROR_TIMEOUT;
  884. }
  885. /**
  886. \fn void ETH_IRQHandler (void)
  887. \brief Ethernet Interrupt handler.
  888. */
  889. void ETH_IRQHandler (void) {
  890. uint32_t stat,event = 0;
  891. stat = ENET->DMA_STAT;
  892. ENET->DMA_STAT = stat & (EMAC_DSR_NIS | EMAC_DSR_RI | EMAC_DSR_TI);
  893. if (stat & EMAC_DSR_TI) {
  894. /* Transmit interrupt */
  895. event |= ARM_ETH_MAC_EVENT_TX_FRAME;
  896. }
  897. if (stat & EMAC_DSR_RI) {
  898. /* Receive interrupt */
  899. event |= ARM_ETH_MAC_EVENT_RX_FRAME;
  900. }
  901. stat = ENET->MAC_INTR;
  902. #if (EMAC_TIME_STAMP)
  903. if (stat & EMAC_MISR_TS) {
  904. /* Timestamp interrupt */
  905. if (ENET->TIMESTAMPSTAT & EMAC_MTSR_TSTARGT) {
  906. /* Alarm trigger interrupt */
  907. event |= ARM_ETH_MAC_EVENT_TIMER_ALARM;
  908. }
  909. }
  910. #endif
  911. if (stat & EMAC_MISR_PMT) {
  912. /* Power management interrupt */
  913. if (ENET->MAC_PMT_CTRL_STAT & EMAC_PMTR_MPR) {
  914. /* Magic packet received */
  915. event |= ARM_ETH_MAC_EVENT_WAKEUP;
  916. }
  917. }
  918. /* Callback event notification */
  919. if (event && emac.cb_event) {
  920. emac.cb_event (event);
  921. }
  922. }
  923. /* MAC Driver Control Block */
  924. ARM_DRIVER_ETH_MAC Driver_ETH_MAC0 = {
  925. GetVersion,
  926. GetCapabilities,
  927. Initialize,
  928. Uninitialize,
  929. PowerControl,
  930. GetMacAddress,
  931. SetMacAddress,
  932. SetAddressFilter,
  933. SendFrame,
  934. ReadFrame,
  935. GetRxFrameSize,
  936. GetRxFrameTime,
  937. GetTxFrameTime,
  938. ControlTimer,
  939. Control,
  940. PHY_Read,
  941. PHY_Write
  942. };