lan8742a.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. /*
  2. * File : drv_mpu.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2015, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017-08-25 LongfeiMa transplantation driver of lan8742a
  23. */
  24. #include <rtthread.h>
  25. #include "lan8742a.h"
  26. #include "etharp.h"
  27. #include "ethernetif.h"
  28. #include "stm32h7xx_hal.h"
  29. #include "lwip/opt.h"
  30. #include "lwip/ip_frag.h"
  31. #include "lwip/def.h"
  32. #include "lwip/inet_chksum.h"
  33. #include "lwip/netif.h"
  34. #include "lwip/snmp.h"
  35. #include "lwip/stats.h"
  36. #include "lwip/pbuf.h"
  37. #include "lwipopts.h"
  38. /** @defgroup LAN8742 LAN8742
  39. * @{
  40. */
  41. /* Private typedef -----------------------------------------------------------*/
  42. /* Private define ------------------------------------------------------------*/
  43. /** @defgroup LAN8742_Private_Defines LAN8742 Private Defines
  44. * @{
  45. */
  46. #define LAN8742_SW_RESET_TO ((uint32_t)500U) //software reset timeout deadline
  47. #define LAN8742_INIT_TO ((uint32_t)2000U)//Wait for 2000ms to perform initialization
  48. #define LAN8742_MAX_DEV_ADDR ((uint32_t)31U) //用于初始化时,搜索IC挂载的有效地址
  49. #if defined ( __ICCARM__ ) /*!< IAR Compiler */
  50. #pragma location=0x30040000
  51. ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */
  52. #pragma location=0x30040060
  53. ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */
  54. #pragma location=0x30040200
  55. uint8_t Rx_Buff[ETH_RX_DESC_CNT][ETH_MAX_PACKET_SIZE]; /* Ethernet Receive Buffers */
  56. #elif defined ( __CC_ARM ) /* MDK ARM Compiler */
  57. __attribute__((at(0x30040000))) ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */
  58. __attribute__((at(0x30040060))) ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */
  59. __attribute__((at(0x30040200))) uint8_t Rx_Buff[ETH_RX_DESC_CNT][ETH_MAX_PACKET_SIZE]; /* Ethernet Receive Buffer */
  60. #elif defined ( __GNUC__ ) /* GNU Compiler */
  61. ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT] __attribute__((section(".RxDecripSection"))); /* Ethernet Rx DMA Descriptors */
  62. ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT] __attribute__((section(".TxDecripSection"))); /* Ethernet Tx DMA Descriptors */
  63. uint8_t Rx_Buff[ETH_RX_DESC_CNT][ETH_MAX_PACKET_SIZE] __attribute__((section(".RxArraySection"))); /* Ethernet Receive Buffers */
  64. #endif
  65. struct pbuf_custom rx_pbuf[ETH_RX_DESC_CNT];
  66. uint32_t current_pbuf_idx =0;
  67. ETH_HandleTypeDef EthHandle;
  68. ETH_TxPacketConfig TxConfig;
  69. #define MAX_ADDR_LEN 6
  70. struct rt_lan8742_eth
  71. {
  72. /* inherit from ethernet device */
  73. struct eth_device parent;
  74. /* interface address info. */
  75. rt_uint8_t dev_addr[MAX_ADDR_LEN]; /* hw address */
  76. };
  77. static struct rt_lan8742_eth lan8742_device;
  78. static struct rt_semaphore sem_lock;
  79. /*******************************************************************************
  80. PHI IO Functions
  81. *******************************************************************************/
  82. /**
  83. * @brief Initializes the MDIO interface GPIO and clocks.
  84. * @param None
  85. * @retval 0 if OK, -1 if ERROR
  86. */
  87. int32_t ETH_PHY_IO_Init(void)
  88. {
  89. /* We assume that MDIO GPIO configuration is already done
  90. in the ETH_MspInit() else it should be done here
  91. */
  92. /* Configure the MDIO Clock */
  93. HAL_ETH_SetMDIOClockRange(&EthHandle);
  94. return 0;
  95. }
  96. /**
  97. * @brief De-Initializes the MDIO interface .
  98. * @param None
  99. * @retval 0 if OK, -1 if ERROR
  100. */
  101. int32_t ETH_PHY_IO_DeInit (void)
  102. {
  103. return 0;
  104. }
  105. /**
  106. * @brief Read a PHY register through the MDIO interface.
  107. * @param DevAddr: PHY port address
  108. * @param RegAddr: PHY register address
  109. * @param pRegVal: pointer to hold the register value
  110. * @retval 0 if OK -1 if Error
  111. */
  112. int32_t ETH_PHY_IO_ReadReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t *pRegVal)
  113. {
  114. if(HAL_ETH_ReadPHYRegister(&EthHandle, DevAddr, RegAddr, pRegVal) != HAL_OK)
  115. {
  116. return -1;
  117. }
  118. return 0;
  119. }
  120. /**
  121. * @brief Write a value to a PHY register through the MDIO interface.
  122. * @param DevAddr: PHY port address
  123. * @param RegAddr: PHY register address
  124. * @param RegVal: Value to be written
  125. * @retval 0 if OK -1 if Error
  126. */
  127. int32_t ETH_PHY_IO_WriteReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t RegVal)
  128. {
  129. if(HAL_ETH_WritePHYRegister(&EthHandle, DevAddr, RegAddr, RegVal) != HAL_OK)
  130. {
  131. return -1;
  132. }
  133. return 0;
  134. }
  135. /**
  136. * @brief Get the time in millisecons used for internal PHY driver process.
  137. * @retval Time value
  138. */
  139. int32_t ETH_PHY_IO_GetTick(void)
  140. {
  141. return HAL_GetTick();
  142. }
  143. //IC相关结构体
  144. lan8742_Object_t LAN8742 =
  145. {
  146. 0, //LAN8742.DevAddr
  147. 0, //LAN8742.Is_Initialized
  148. {
  149. ETH_PHY_IO_Init, //LAN8742.IO.Init
  150. ETH_PHY_IO_DeInit, //LAN8742.IO.DeInit
  151. ETH_PHY_IO_WriteReg,//LAN8742.IO.WriteReg
  152. ETH_PHY_IO_ReadReg, //LAN8742.IO.ReadReg
  153. ETH_PHY_IO_GetTick, //LAN8742.IO.GetTick
  154. },
  155. NULL,
  156. };
  157. /*******************************************************************************
  158. Ethernet MSP Routines
  159. *******************************************************************************/
  160. /**
  161. * @brief Initializes the ETH MSP.
  162. * @param heth: ETH handle
  163. * @retval None
  164. */
  165. void HAL_ETH_MspInit(ETH_HandleTypeDef *heth)
  166. {
  167. GPIO_InitTypeDef GPIO_InitStructure;
  168. /* Ethernett MSP init: RMII Mode */
  169. /* Enable GPIOs clocks */
  170. __HAL_RCC_GPIOA_CLK_ENABLE();
  171. __HAL_RCC_GPIOB_CLK_ENABLE();
  172. __HAL_RCC_GPIOC_CLK_ENABLE();
  173. __HAL_RCC_GPIOG_CLK_ENABLE();
  174. /* Ethernet pins configuration ************************************************/
  175. /*
  176. RMII_REF_CLK ----------------------> PA1
  177. RMII_MDIO -------------------------> PA2
  178. RMII_MDC --------------------------> PC1
  179. RMII_MII_CRS_DV -------------------> PA7
  180. RMII_MII_RXD0 ---------------------> PC4
  181. RMII_MII_RXD1 ---------------------> PC5
  182. RMII_MII_RXER ---------------------> PG2
  183. RMII_MII_TX_EN --------------------> PG11
  184. RMII_MII_TXD0 ---------------------> PG13
  185. RMII_MII_TXD1 ---------------------> PB13
  186. */
  187. /* Configure PA1, PA2 and PA7 */
  188. GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
  189. GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
  190. GPIO_InitStructure.Pull = GPIO_NOPULL;
  191. GPIO_InitStructure.Alternate = GPIO_AF11_ETH;
  192. GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7;
  193. HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
  194. /* Configure PB13 */
  195. GPIO_InitStructure.Pin = GPIO_PIN_13;
  196. HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
  197. /* Configure PC1, PC4 and PC5 */
  198. GPIO_InitStructure.Pin = GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5;
  199. HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
  200. /* Configure PG2, PG11, PG13 and PG14 */
  201. GPIO_InitStructure.Pin = GPIO_PIN_2 | GPIO_PIN_11 | GPIO_PIN_13;
  202. HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
  203. /* Enable the Ethernet global Interrupt */
  204. HAL_NVIC_SetPriority(ETH_IRQn, 0x7, 0);
  205. HAL_NVIC_EnableIRQ(ETH_IRQn);
  206. /* Enable Ethernet clocks */
  207. __HAL_RCC_ETH1MAC_CLK_ENABLE();
  208. __HAL_RCC_ETH1TX_CLK_ENABLE();
  209. __HAL_RCC_ETH1RX_CLK_ENABLE();
  210. }
  211. /**
  212. * @brief Initialize the lan8742 and configure the needed hardware resources
  213. * @param void.
  214. * @retval LAN8742_STATUS_OK if OK
  215. * LAN8742_STATUS_ADDRESS_ERROR if cannot find device address
  216. * LAN8742_STATUS_READ_ERROR if connot read register
  217. * LAN8742_STATUS_WRITE_ERROR if connot write to register
  218. * LAN8742_STATUS_RESET_TIMEOUT if cannot perform a software reset
  219. */
  220. static int32_t LAN8742_Init(lan8742_Object_t *pObj)
  221. {
  222. uint32_t tickstart = 0, regvalue = 0, addr = 0;
  223. int32_t status = LAN8742_STATUS_OK;
  224. if(pObj->Is_Initialized == 0)
  225. {
  226. if(pObj->IO.Init != 0)
  227. {
  228. /* GPIO and Clocks initialization */
  229. pObj->IO.Init();
  230. }
  231. /* for later check */
  232. pObj->DevAddr = LAN8742_MAX_DEV_ADDR + 1;
  233. /* Get the device address from special mode register */
  234. for(addr = 0; addr <= LAN8742_MAX_DEV_ADDR; addr ++)
  235. {
  236. if(pObj->IO.ReadReg(addr, LAN8742_REG_SMR, &regvalue) < 0)
  237. {
  238. status = LAN8742_STATUS_READ_ERROR;
  239. /* Can't read from this device address
  240. continue with next address */
  241. continue;
  242. }
  243. if((regvalue & LAN8742_SMR_PHY_ADDR) == addr)
  244. {
  245. pObj->DevAddr = addr;
  246. status = LAN8742_STATUS_OK;
  247. break;
  248. }
  249. }
  250. if(pObj->DevAddr > LAN8742_MAX_DEV_ADDR)
  251. {
  252. status = LAN8742_STATUS_ADDRESS_ERROR;
  253. }
  254. /* if device address is matched */
  255. if(status == LAN8742_STATUS_OK)
  256. {
  257. /* set a software reset */
  258. if(pObj->IO.WriteReg(pObj->DevAddr, LAN8742_REG_BCR, LAN8742_BCR_RESET) >= 0)
  259. {
  260. /* get software reset status */
  261. if(pObj->IO.ReadReg(pObj->DevAddr, LAN8742_REG_BCR, &regvalue) >= 0)
  262. {
  263. tickstart = pObj->IO.GetTick();
  264. /* wait until software reset is done or timeout occured */
  265. while(regvalue & LAN8742_BCR_RESET)
  266. {
  267. if((pObj->IO.GetTick() - tickstart) <= LAN8742_SW_RESET_TO)
  268. {
  269. if(pObj->IO.ReadReg(pObj->DevAddr, LAN8742_REG_BCR, &regvalue) < 0)
  270. {
  271. status = LAN8742_STATUS_READ_ERROR;
  272. break;
  273. }
  274. }
  275. else
  276. {
  277. status = LAN8742_STATUS_RESET_TIMEOUT;
  278. }
  279. }
  280. }
  281. else
  282. {
  283. status = LAN8742_STATUS_READ_ERROR;
  284. }
  285. }
  286. else
  287. {
  288. status = LAN8742_STATUS_WRITE_ERROR;
  289. }
  290. }
  291. }
  292. if(status == LAN8742_STATUS_OK)
  293. {
  294. tickstart = pObj->IO.GetTick();
  295. /* Wait for 2s to perform initialization */
  296. while((pObj->IO.GetTick() - tickstart) <= LAN8742_INIT_TO)
  297. {
  298. }
  299. pObj->Is_Initialized = 1;
  300. }
  301. return status;
  302. }
  303. /**
  304. * @brief De-Initialize the lan8742 and it's hardware resources
  305. * @param void.
  306. * @retval LAN8742_STATUS_ERROR / LAN8742_STATUS_OK
  307. */
  308. static int32_t LAN8742_DeInit(lan8742_Object_t *pObj)
  309. {
  310. if(pObj->Is_Initialized)
  311. {
  312. if(pObj->IO.DeInit != 0)
  313. {
  314. if(pObj->IO.DeInit() < 0)
  315. {
  316. return LAN8742_STATUS_ERROR;
  317. }
  318. }
  319. pObj->Is_Initialized = 0;
  320. }
  321. return LAN8742_STATUS_OK;
  322. }
  323. /**
  324. * @brief Disable the LAN8742 power down mode.
  325. * @param pObj: device object LAN8742_Object_t.
  326. * @retval LAN8742_STATUS_OK if OK
  327. * LAN8742_STATUS_READ_ERROR if connot read register
  328. * LAN8742_STATUS_WRITE_ERROR if connot write to register
  329. */
  330. static int32_t LAN8742_DisablePowerDownMode(lan8742_Object_t *pObj)
  331. {
  332. uint32_t readval = 0;
  333. int32_t status = LAN8742_STATUS_OK;
  334. if(pObj->IO.ReadReg(pObj->DevAddr, LAN8742_REG_BCR, &readval) >= 0)
  335. {
  336. readval &= ~LAN8742_BCR_POWER_DOWN;
  337. /* Apply configuration */
  338. if(pObj->IO.WriteReg(pObj->DevAddr, LAN8742_REG_BCR, readval) < 0)
  339. {
  340. status = LAN8742_STATUS_WRITE_ERROR;
  341. }
  342. }
  343. else
  344. {
  345. status = LAN8742_STATUS_READ_ERROR;
  346. }
  347. return status;
  348. }
  349. /**
  350. * @brief Enable the LAN8742 power down mode.
  351. * @param pObj: device object LAN8742_Object_t.
  352. * @retval LAN8742_STATUS_OK if OK
  353. * LAN8742_STATUS_READ_ERROR if connot read register
  354. * LAN8742_STATUS_WRITE_ERROR if connot write to register
  355. */
  356. static int32_t LAN8742_EnablePowerDownMode(lan8742_Object_t *pObj)
  357. {
  358. uint32_t readval = 0;
  359. int32_t status = LAN8742_STATUS_OK;
  360. if(pObj->IO.ReadReg(pObj->DevAddr, LAN8742_REG_BCR, &readval) >= 0)
  361. {
  362. readval |= LAN8742_BCR_POWER_DOWN;
  363. /* Apply configuration */
  364. if(pObj->IO.WriteReg(pObj->DevAddr, LAN8742_REG_BCR, readval) < 0)
  365. {
  366. status = LAN8742_STATUS_WRITE_ERROR;
  367. }
  368. }
  369. else
  370. {
  371. status = LAN8742_STATUS_READ_ERROR;
  372. }
  373. return status;
  374. }
  375. /**
  376. * @brief Start the auto negotiation process.
  377. * @param pObj: device object LAN8742_Object_t.
  378. * @retval LAN8742_STATUS_OK if OK
  379. * LAN8742_STATUS_READ_ERROR if connot read register
  380. * LAN8742_STATUS_WRITE_ERROR if connot write to register
  381. */
  382. static int32_t LAN8742_StartAutoNego(lan8742_Object_t *pObj)
  383. {
  384. uint32_t readval = 0;
  385. int32_t status = LAN8742_STATUS_OK;
  386. if(pObj->IO.ReadReg(pObj->DevAddr, LAN8742_REG_BCR, &readval) >= 0)
  387. {
  388. readval |= LAN8742_BCR_ANEG_EN;
  389. /* Apply configuration */
  390. if(pObj->IO.WriteReg(pObj->DevAddr, LAN8742_REG_BCR, readval) < 0)
  391. {
  392. status = LAN8742_STATUS_WRITE_ERROR;
  393. }
  394. }
  395. else
  396. {
  397. status = LAN8742_STATUS_READ_ERROR;
  398. }
  399. return status;
  400. }
  401. /**
  402. * @brief Get the link state of LAN8742 device.
  403. * @param pObj: Pointer to device object.
  404. * @param pLinkState: Pointer to link state
  405. * @retval LAN8742_STATUS_LINK_DOWN if link is down
  406. * LAN8742_STATUS_AUTONEGO_NOTDONE if Auto nego not completed
  407. * LAN8742_STATUS_100MBITS_FULLDUPLEX if 100Mb/s FD
  408. * LAN8742_STATUS_100MBITS_HALFDUPLEX if 100Mb/s HD
  409. * LAN8742_STATUS_10MBITS_FULLDUPLEX if 10Mb/s FD
  410. * LAN8742_STATUS_10MBITS_HALFDUPLEX if 10Mb/s HD
  411. * LAN8742_STATUS_READ_ERROR if connot read register
  412. * LAN8742_STATUS_WRITE_ERROR if connot write to register
  413. */
  414. static int32_t LAN8742_GetLinkState(lan8742_Object_t *pObj)
  415. {
  416. uint32_t readval = 0;
  417. /* Read Status register */
  418. if(pObj->IO.ReadReg(pObj->DevAddr, LAN8742_REG_BSR, &readval) < 0)
  419. {
  420. return LAN8742_STATUS_READ_ERROR;
  421. }
  422. /* Read Status register again */
  423. if(pObj->IO.ReadReg(pObj->DevAddr, LAN8742_REG_BSR, &readval) < 0)
  424. {
  425. return LAN8742_STATUS_READ_ERROR;
  426. }
  427. if((readval & LAN8742_BSR_LINK_STAT) == 0)
  428. {
  429. /* Return Link Down status */
  430. return LAN8742_STATUS_LINK_DOWN;
  431. }
  432. /* Check Auto negotiaition */
  433. if(pObj->IO.ReadReg(pObj->DevAddr, LAN8742_REG_BCR, &readval) < 0)
  434. {
  435. return LAN8742_STATUS_READ_ERROR;
  436. }
  437. if((readval & LAN8742_BCR_ANEG_EN) != LAN8742_BCR_ANEG_EN)
  438. {
  439. if(((readval & LAN8742_BCR_SPEED_SEL) == LAN8742_BCR_SPEED_SEL) && ((readval & LAN8742_BCR_DUPLEX) == LAN8742_BCR_DUPLEX))
  440. {
  441. return LAN8742_STATUS_100MBITS_FULLDUPLEX;
  442. }
  443. else if ((readval & LAN8742_BCR_SPEED_SEL) == LAN8742_BCR_SPEED_SEL)
  444. {
  445. return LAN8742_STATUS_100MBITS_HALFDUPLEX;
  446. }
  447. else if ((readval & LAN8742_BCR_DUPLEX) == LAN8742_BCR_DUPLEX)
  448. {
  449. return LAN8742_STATUS_10MBITS_FULLDUPLEX;
  450. }
  451. else
  452. {
  453. return LAN8742_STATUS_10MBITS_HALFDUPLEX;
  454. }
  455. }
  456. else /* Auto Nego enabled */
  457. {
  458. if(pObj->IO.ReadReg(pObj->DevAddr, LAN8742_REG_PSCS, &readval) < 0)
  459. {
  460. return LAN8742_STATUS_READ_ERROR;
  461. }
  462. /* Check if auto nego not done */
  463. if((readval & LAN8742_PHYSCSR_AUTONEGO_DONE) == 0)
  464. {
  465. return LAN8742_STATUS_AUTONEGO_NOTDONE;
  466. }
  467. if((readval & LAN8742_PHYSCSR_HCDSPEEDMASK) == LAN8742_PHYSCSR_100BTX_FD)
  468. {
  469. return LAN8742_STATUS_100MBITS_FULLDUPLEX;
  470. }
  471. else if ((readval & LAN8742_PHYSCSR_HCDSPEEDMASK) == LAN8742_PHYSCSR_100BTX_HD)
  472. {
  473. return LAN8742_STATUS_100MBITS_HALFDUPLEX;
  474. }
  475. else if ((readval & LAN8742_PHYSCSR_HCDSPEEDMASK) == LAN8742_PHYSCSR_10BT_FD)
  476. {
  477. return LAN8742_STATUS_10MBITS_FULLDUPLEX;
  478. }
  479. else
  480. {
  481. return LAN8742_STATUS_10MBITS_HALFDUPLEX;
  482. }
  483. }
  484. }
  485. /**
  486. * @brief Set the link state of LAN8742 device.
  487. * @param pObj: Pointer to device object.
  488. * @param pLinkState: link state can be one of the following
  489. * LAN8742_STATUS_100MBITS_FULLDUPLEX if 100Mb/s FD
  490. * LAN8742_STATUS_100MBITS_HALFDUPLEX if 100Mb/s HD
  491. * LAN8742_STATUS_10MBITS_FULLDUPLEX if 10Mb/s FD
  492. * LAN8742_STATUS_10MBITS_HALFDUPLEX if 10Mb/s HD
  493. * @retval LAN8742_STATUS_OK if OK
  494. * LAN8742_STATUS_ERROR if parameter error
  495. * LAN8742_STATUS_READ_ERROR if connot read register
  496. * LAN8742_STATUS_WRITE_ERROR if connot write to register
  497. */
  498. static int32_t LAN8742_SetLinkState(lan8742_Object_t *pObj, uint32_t LinkState)
  499. {
  500. uint32_t bcrvalue = 0;
  501. int32_t status = LAN8742_STATUS_OK;
  502. if(pObj->IO.ReadReg(pObj->DevAddr, LAN8742_REG_BCR, &bcrvalue) >= 0)
  503. {
  504. /* Disable link config (Auto nego, speed and duplex) */
  505. bcrvalue &= ~(LAN8742_BCR_ANEG_EN | LAN8742_BCR_SPEED_SEL | LAN8742_BCR_DUPLEX);
  506. if(LinkState == LAN8742_STATUS_100MBITS_FULLDUPLEX)
  507. {
  508. bcrvalue |= (LAN8742_BCR_SPEED_SEL | LAN8742_BCR_DUPLEX);
  509. }
  510. else if (LinkState == LAN8742_STATUS_100MBITS_HALFDUPLEX)
  511. {
  512. bcrvalue |= LAN8742_BCR_SPEED_SEL;
  513. }
  514. else if (LinkState == LAN8742_STATUS_10MBITS_FULLDUPLEX)
  515. {
  516. bcrvalue |= LAN8742_BCR_DUPLEX;
  517. }
  518. else
  519. {
  520. /* Wrong link status parameter */
  521. status = LAN8742_STATUS_ERROR;
  522. }
  523. }
  524. else
  525. {
  526. status = LAN8742_STATUS_READ_ERROR;
  527. }
  528. if(status == LAN8742_STATUS_OK)
  529. {
  530. /* Apply configuration */
  531. if(pObj->IO.WriteReg(pObj->DevAddr, LAN8742_REG_BCR, bcrvalue) < 0)
  532. {
  533. status = LAN8742_STATUS_WRITE_ERROR;
  534. }
  535. }
  536. return status;
  537. }
  538. /**
  539. * @brief Enable loopback mode.
  540. * @param pObj: Pointer to device object.
  541. * @retval LAN8742_STATUS_OK if OK
  542. * LAN8742_STATUS_READ_ERROR if connot read register
  543. * LAN8742_STATUS_WRITE_ERROR if connot write to register
  544. */
  545. static int32_t LAN8742_EnableLoopbackMode(lan8742_Object_t *pObj)
  546. {
  547. uint32_t readval = 0;
  548. int32_t status = LAN8742_STATUS_OK;
  549. if(pObj->IO.ReadReg(pObj->DevAddr, LAN8742_REG_BCR, &readval) >= 0)
  550. {
  551. readval |= LAN8742_BCR_LOOPBACK;
  552. /* Apply configuration */
  553. if(pObj->IO.WriteReg(pObj->DevAddr, LAN8742_REG_BCR, readval) < 0)
  554. {
  555. status = LAN8742_STATUS_WRITE_ERROR;
  556. }
  557. }
  558. else
  559. {
  560. status = LAN8742_STATUS_READ_ERROR;
  561. }
  562. return status;
  563. }
  564. /**
  565. * @brief Disable loopback mode.
  566. * @param pObj: Pointer to device object.
  567. * @retval LAN8742_STATUS_OK if OK
  568. * LAN8742_STATUS_READ_ERROR if connot read register
  569. * LAN8742_STATUS_WRITE_ERROR if connot write to register
  570. */
  571. static int32_t LAN8742_DisableLoopbackMode(lan8742_Object_t *pObj)
  572. {
  573. uint32_t readval = 0;
  574. int32_t status = LAN8742_STATUS_OK;
  575. if(pObj->IO.ReadReg(pObj->DevAddr, LAN8742_REG_BCR, &readval) >= 0)
  576. {
  577. readval &= ~LAN8742_BCR_LOOPBACK;
  578. /* Apply configuration */
  579. if(pObj->IO.WriteReg(pObj->DevAddr, LAN8742_REG_BCR, readval) < 0)
  580. {
  581. status = LAN8742_STATUS_WRITE_ERROR;
  582. }
  583. }
  584. else
  585. {
  586. status = LAN8742_STATUS_READ_ERROR;
  587. }
  588. return status;
  589. }
  590. /**
  591. * @brief Enable IT source.
  592. * @param pObj: Pointer to device object.
  593. * @param Interrupt: IT source to be enabled
  594. * should be a value or a combination of the following:
  595. * LAN8742_WOL_IT
  596. * LAN8742_ENERGYON_IT
  597. * LAN8742_AUTONEGO_COMPLETE_IT
  598. * LAN8742_REMOTE_FAULT_IT
  599. * LAN8742_LINK_DOWN_IT
  600. * LAN8742_AUTONEGO_LP_ACK_IT
  601. * LAN8742_PARALLEL_DETECTION_FAULT_IT
  602. * LAN8742_AUTONEGO_PAGE_RECEIVED_IT
  603. * @retval LAN8742_STATUS_OK if OK
  604. * LAN8742_STATUS_READ_ERROR if connot read register
  605. * LAN8742_STATUS_WRITE_ERROR if connot write to register
  606. */
  607. static int32_t LAN8742_EnableIT(lan8742_Object_t *pObj, uint32_t Interrupt)
  608. {
  609. uint32_t readval = 0;
  610. int32_t status = LAN8742_STATUS_OK;
  611. if(pObj->IO.ReadReg(pObj->DevAddr, LAN8742_REG_IM, &readval) >= 0)
  612. {
  613. readval |= Interrupt;
  614. /* Apply configuration */
  615. if(pObj->IO.WriteReg(pObj->DevAddr, LAN8742_REG_IM, readval) < 0)
  616. {
  617. status = LAN8742_STATUS_WRITE_ERROR;
  618. }
  619. }
  620. else
  621. {
  622. status = LAN8742_STATUS_READ_ERROR;
  623. }
  624. return status;
  625. }
  626. /**
  627. * @brief Disable IT source.
  628. * @param pObj: Pointer to device object.
  629. * @param Interrupt: IT source to be disabled
  630. * should be a value or a combination of the following:
  631. * LAN8742_WOL_IT
  632. * LAN8742_ENERGYON_IT
  633. * LAN8742_AUTONEGO_COMPLETE_IT
  634. * LAN8742_REMOTE_FAULT_IT
  635. * LAN8742_LINK_DOWN_IT
  636. * LAN8742_AUTONEGO_LP_ACK_IT
  637. * LAN8742_PARALLEL_DETECTION_FAULT_IT
  638. * LAN8742_AUTONEGO_PAGE_RECEIVED_IT
  639. * @retval LAN8742_STATUS_OK if OK
  640. * LAN8742_STATUS_READ_ERROR if connot read register
  641. * LAN8742_STATUS_WRITE_ERROR if connot write to register
  642. */
  643. static int32_t LAN8742_DisableIT(lan8742_Object_t *pObj, uint32_t Interrupt)
  644. {
  645. uint32_t readval = 0;
  646. int32_t status = LAN8742_STATUS_OK;
  647. if(pObj->IO.ReadReg(pObj->DevAddr, LAN8742_REG_IM, &readval) >= 0)
  648. {
  649. readval &= ~Interrupt;
  650. /* Apply configuration */
  651. if(pObj->IO.WriteReg(pObj->DevAddr, LAN8742_REG_IM, readval) < 0)
  652. {
  653. status = LAN8742_STATUS_WRITE_ERROR;
  654. }
  655. }
  656. else
  657. {
  658. status = LAN8742_STATUS_READ_ERROR;
  659. }
  660. return status;
  661. }
  662. /**
  663. * @brief Clear IT flag.
  664. * @param pObj: Pointer to device object.
  665. * @param Interrupt: IT flag to be cleared
  666. * should be a value or a combination of the following:
  667. * LAN8742_WOL_IT
  668. * LAN8742_ENERGYON_IT
  669. * LAN8742_AUTONEGO_COMPLETE_IT
  670. * LAN8742_REMOTE_FAULT_IT
  671. * LAN8742_LINK_DOWN_IT
  672. * LAN8742_AUTONEGO_LP_ACK_IT
  673. * LAN8742_PARALLEL_DETECTION_FAULT_IT
  674. * LAN8742_AUTONEGO_PAGE_RECEIVED_IT
  675. * @retval LAN8742_STATUS_OK if OK
  676. * LAN8742_STATUS_READ_ERROR if connot read register
  677. */
  678. static int32_t LAN8742_ClearIT(lan8742_Object_t *pObj, uint32_t Interrupt)
  679. {
  680. uint32_t readval = 0;
  681. int32_t status = LAN8742_STATUS_OK;
  682. if(pObj->IO.ReadReg(pObj->DevAddr, LAN8742_REG_ISF, &readval) < 0)
  683. {
  684. status = LAN8742_STATUS_READ_ERROR;
  685. }
  686. return status;
  687. }
  688. /**
  689. * @brief Get IT Flag status.
  690. * @param pObj: Pointer to device object.
  691. * @param Interrupt: IT Flag to be checked,
  692. * should be a value or a combination of the following:
  693. * LAN8742_WOL_IT
  694. * LAN8742_ENERGYON_IT
  695. * LAN8742_AUTONEGO_COMPLETE_IT
  696. * LAN8742_REMOTE_FAULT_IT
  697. * LAN8742_LINK_DOWN_IT
  698. * LAN8742_AUTONEGO_LP_ACK_IT
  699. * LAN8742_PARALLEL_DETECTION_FAULT_IT
  700. * LAN8742_AUTONEGO_PAGE_RECEIVED_IT
  701. * @retval 1 IT flag is SET
  702. * 0 IT flag is RESET
  703. * LAN8742_STATUS_READ_ERROR if connot read register
  704. */
  705. static int32_t LAN8742_GetITStatus(lan8742_Object_t *pObj, uint32_t Interrupt)
  706. {
  707. uint32_t readval = 0;
  708. int32_t status = 0;
  709. if(pObj->IO.ReadReg(pObj->DevAddr, LAN8742_REG_ISF, &readval) >= 0)
  710. {
  711. status = ((readval & Interrupt) == Interrupt);
  712. }
  713. else
  714. {
  715. status = LAN8742_STATUS_READ_ERROR;
  716. }
  717. return status;
  718. }
  719. static rt_err_t rt_lan8742_init(rt_device_t dev)
  720. {
  721. return RT_EOK;
  722. }
  723. static rt_err_t rt_lan8742_open(rt_device_t dev, rt_uint16_t oflag)
  724. {
  725. return RT_EOK;
  726. }
  727. static rt_err_t rt_lan8742_close(rt_device_t dev)
  728. {
  729. LAN8742_DeInit(&LAN8742);
  730. LAN8742_DisableIT(&LAN8742, LAN8742_INT_ALL);
  731. LAN8742_EnablePowerDownMode(&LAN8742);
  732. return RT_EOK;
  733. }
  734. static rt_size_t rt_lan8742_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  735. {
  736. rt_set_errno(-RT_ENOSYS);
  737. return 0;
  738. }
  739. static rt_size_t rt_lan8742_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  740. {
  741. rt_set_errno(-RT_ENOSYS);
  742. return 0;
  743. }
  744. static rt_err_t rt_lan8742_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  745. {
  746. switch (cmd)
  747. {
  748. case NIOCTL_GADDR:
  749. /* get mac address */
  750. if (args)
  751. rt_memcpy(args, lan8742_device.dev_addr, 6);
  752. else
  753. return -RT_ERROR;
  754. break;
  755. default :
  756. break;
  757. }
  758. return RT_EOK;
  759. }
  760. /* ethernet device interface */
  761. /* transmit packet. */
  762. rt_err_t rt_lan8742_tx( rt_device_t dev, struct pbuf* p)
  763. {
  764. uint32_t i=0, framelen = 0;
  765. struct pbuf *q;
  766. err_t errval = ERR_OK;
  767. ETH_BufferTypeDef Txbuffer[ETH_TX_DESC_CNT];
  768. /* lock LAN8742 device */
  769. rt_sem_take(&sem_lock, RT_WAITING_FOREVER);
  770. memset(Txbuffer, 0 , 4*sizeof(ETH_BufferTypeDef));
  771. for(q = p; q != NULL; q = q->next)
  772. {
  773. if(i >= ETH_TX_DESC_CNT)
  774. return ERR_IF;
  775. Txbuffer[i].buffer = q->payload;
  776. Txbuffer[i].len = q->len;
  777. framelen += q->len;
  778. if(i>0)
  779. {
  780. Txbuffer[i-1].next = &Txbuffer[i];
  781. }
  782. i++;
  783. }
  784. TxConfig.Length = framelen;
  785. TxConfig.TxBuffer = Txbuffer;
  786. /* Clean and Invalidate data cache */
  787. SCB_CleanInvalidateDCache();
  788. HAL_ETH_Transmit(&EthHandle, &TxConfig, 0);//Transmit an ETH frame in blocking mode
  789. /* unlock LAN8742 device */
  790. rt_sem_release(&sem_lock);
  791. return errval;
  792. }
  793. /* reception packet. */
  794. struct pbuf *rt_lan8742_rx(rt_device_t dev)
  795. {
  796. struct pbuf* p = RT_NULL;
  797. ETH_BufferTypeDef RxBuff;
  798. uint32_t framelength = 0;
  799. /* lock LAN8742 device */
  800. rt_sem_take(&sem_lock, RT_WAITING_FOREVER);
  801. /* Clean and Invalidate data cache */
  802. SCB_CleanInvalidateDCache();
  803. if(HAL_ETH_GetRxDataBuffer(&EthHandle, &RxBuff) == HAL_OK)
  804. {
  805. HAL_ETH_GetRxDataLength(&EthHandle, &framelength);
  806. p = pbuf_alloced_custom(PBUF_RAW, framelength, PBUF_POOL, &rx_pbuf[current_pbuf_idx], RxBuff.buffer, framelength);
  807. if(current_pbuf_idx < (ETH_RX_DESC_CNT -1))
  808. {
  809. current_pbuf_idx++;
  810. }
  811. else
  812. {
  813. current_pbuf_idx = 0;
  814. }
  815. }
  816. HAL_ETH_BuildRxDescriptors(&EthHandle);
  817. /* unlock LAN8742 device */
  818. rt_sem_release(&sem_lock);
  819. return p;
  820. }
  821. /**
  822. * @brief Custom Rx pbuf free callback
  823. * @param pbuf: pbuf to be freed
  824. * @retval None
  825. */
  826. static void pbuf_free_custom(struct pbuf *p)
  827. {
  828. if(p != NULL)
  829. {
  830. p->flags = 0;
  831. p->next = NULL;
  832. p->len = p->tot_len = 0;
  833. p->ref = 0;
  834. p->payload = NULL;
  835. }
  836. }
  837. int rt_hw_lan8742a_init(void)
  838. {
  839. uint32_t idx, duplex, speed = 0;
  840. int32_t PHYLinkState;
  841. ETH_MACConfigTypeDef MACConf;
  842. uint8_t macaddress[6]= {ETH_MAC_ADDR0, ETH_MAC_ADDR1, ETH_MAC_ADDR2,
  843. ETH_MAC_ADDR3, ETH_MAC_ADDR4, ETH_MAC_ADDR5};
  844. EthHandle.Instance = ETH;
  845. EthHandle.Init.MACAddr = macaddress;
  846. EthHandle.Init.MediaInterface = HAL_ETH_RMII_MODE;
  847. EthHandle.Init.RxDesc = DMARxDscrTab;
  848. EthHandle.Init.TxDesc = DMATxDscrTab;
  849. EthHandle.Init.RxBuffLen = ETH_MAX_PACKET_SIZE;
  850. /* configure ethernet peripheral (GPIOs, clocks, MAC, DMA) */
  851. HAL_ETH_Init(&EthHandle);
  852. for(idx = 0; idx < ETH_RX_DESC_CNT; idx ++)
  853. {
  854. HAL_ETH_DescAssignMemory(&EthHandle, idx, Rx_Buff[idx], NULL);
  855. /* Set Custom pbuf free function */
  856. rx_pbuf[idx].custom_free_function = pbuf_free_custom;
  857. }
  858. memset(&TxConfig, 0 , sizeof(ETH_TxPacketConfig));
  859. TxConfig.Attributes = ETH_TX_PACKETS_FEATURES_CSUM | ETH_TX_PACKETS_FEATURES_CRCPAD;
  860. TxConfig.ChecksumCtrl = ETH_CHECKSUM_IPHDR_PAYLOAD_INSERT_PHDR_CALC;
  861. TxConfig.CRCPadCtrl = ETH_CRC_PAD_INSERT;
  862. /* Initialize the LAN8742 ETH PHY */
  863. LAN8742_Init(&LAN8742);
  864. /* Get link state */
  865. PHYLinkState = LAN8742_GetLinkState(&LAN8742);
  866. /* Get link state */
  867. if(PHYLinkState <= LAN8742_STATUS_LINK_DOWN)
  868. {
  869. //
  870. }
  871. else
  872. {
  873. switch (PHYLinkState)
  874. {
  875. case LAN8742_STATUS_100MBITS_FULLDUPLEX:
  876. duplex = ETH_FULLDUPLEX_MODE;
  877. speed = ETH_SPEED_100M;
  878. break;
  879. case LAN8742_STATUS_100MBITS_HALFDUPLEX:
  880. duplex = ETH_HALFDUPLEX_MODE;
  881. speed = ETH_SPEED_100M;
  882. break;
  883. case LAN8742_STATUS_10MBITS_FULLDUPLEX:
  884. duplex = ETH_FULLDUPLEX_MODE;
  885. speed = ETH_SPEED_10M;
  886. break;
  887. case LAN8742_STATUS_10MBITS_HALFDUPLEX:
  888. duplex = ETH_HALFDUPLEX_MODE;
  889. speed = ETH_SPEED_10M;
  890. break;
  891. default:
  892. duplex = ETH_FULLDUPLEX_MODE;
  893. speed = ETH_SPEED_100M;
  894. break;
  895. }
  896. /* Get MAC Config MAC */
  897. HAL_ETH_GetMACConfig(&EthHandle, &MACConf);
  898. MACConf.DuplexMode = duplex;
  899. MACConf.Speed = speed;
  900. HAL_ETH_SetMACConfig(&EthHandle, &MACConf);
  901. HAL_ETH_Start_IT(&EthHandle);
  902. }
  903. rt_sem_init(&sem_lock, "eth_lock", 1, RT_IPC_FLAG_FIFO);
  904. rt_memcpy(lan8742_device.dev_addr, macaddress, 6);
  905. lan8742_device.parent.parent.init = rt_lan8742_init;
  906. lan8742_device.parent.parent.open = rt_lan8742_open;
  907. lan8742_device.parent.parent.close = rt_lan8742_close;
  908. lan8742_device.parent.parent.read = rt_lan8742_read;
  909. lan8742_device.parent.parent.write = rt_lan8742_write;
  910. lan8742_device.parent.parent.control = rt_lan8742_control;
  911. lan8742_device.parent.parent.user_data = RT_NULL;
  912. lan8742_device.parent.eth_rx = rt_lan8742_rx;
  913. lan8742_device.parent.eth_tx = rt_lan8742_tx;
  914. eth_device_init(&(lan8742_device.parent), "e0");
  915. return 0;
  916. }
  917. INIT_DEVICE_EXPORT(rt_hw_lan8742a_init);
  918. #ifdef RT_USING_LWIP
  919. /**
  920. * @brief This function handles Ethernet interrupt request.
  921. * @param None
  922. * @retval None
  923. */
  924. void ETH_IRQHandler(void)
  925. {
  926. HAL_ETH_IRQHandler(&EthHandle);
  927. }
  928. #endif /* RT_USING_LWIP */
  929. /**
  930. * @brief Ethernet Rx Transfer completed callback
  931. * @param heth: ETH handle
  932. * @retval None
  933. */
  934. void HAL_ETH_RxCpltCallback(ETH_HandleTypeDef *heth)
  935. {
  936. /* a frame has been received */
  937. eth_device_ready(&(lan8742_device.parent));
  938. }