ethernetif.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * File Name : ethernetif.c
  5. * Description : This file provides code for the configuration
  6. * of the ethernetif.c MiddleWare.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2024 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "lwip/opt.h"
  23. #include "lwip/timeouts.h"
  24. #include "netif/ethernet.h"
  25. #include "netif/etharp.h"
  26. #include "lwip/ethip6.h"
  27. #include "ethernetif.h"
  28. #include "dp83848.h"
  29. #include <string.h>
  30. #include "cmsis_os.h"
  31. #include "lwip/tcpip.h"
  32. /* Within 'USER CODE' section, code will be kept by default at each generation */
  33. /* USER CODE BEGIN 0 */
  34. /* USER CODE END 0 */
  35. /* Private define ------------------------------------------------------------*/
  36. /* The time to block waiting for input. */
  37. #define TIME_WAITING_FOR_INPUT ( portMAX_DELAY )
  38. /* USER CODE BEGIN OS_THREAD_STACK_SIZE_WITH_RTOS */
  39. /* ETH_CODE: increase stack size, otherwise there
  40. * might be overflow in more advanced applications.
  41. * Lower optimization can increase the stack usage
  42. * and cause stack overflows in some cases.
  43. */
  44. /* Stack size of the interface thread */
  45. #define INTERFACE_THREAD_STACK_SIZE ( 1024 )
  46. /* USER CODE END OS_THREAD_STACK_SIZE_WITH_RTOS */
  47. /* Network interface name */
  48. #define IFNAME0 's'
  49. #define IFNAME1 't'
  50. /* ETH Setting */
  51. #define ETH_DMA_TRANSMIT_TIMEOUT ( 20U )
  52. #define ETH_TX_BUFFER_MAX ((ETH_TX_DESC_CNT) * 2U)
  53. /* ETH_RX_BUFFER_SIZE parameter is defined in lwipopts.h */
  54. /* USER CODE BEGIN 1 */
  55. #if USE_DHCP
  56. #define MAX_DHCP_TRIES 4
  57. enum dhcp_states {
  58. DHCP_OFF = 0,
  59. DHCP_START,
  60. DHCP_WAIT_ADDRESS,
  61. DHCP_ADDRESS_ASSIGNED,
  62. DHCP_TIMEOUT,
  63. DHCP_LINKD_DOWN,
  64. DHCP_LAST
  65. } ;
  66. #endif
  67. /* USER CODE END 1 */
  68. /* Private variables ---------------------------------------------------------*/
  69. /*
  70. @Note: This interface is implemented to operate in zero-copy mode only:
  71. - Rx buffers will be allocated from LwIP stack memory heap,
  72. then passed to ETH HAL driver.
  73. - Tx buffers will be allocated from LwIP stack memory heap,
  74. then passed to ETH HAL driver.
  75. @Notes:
  76. 1.a. ETH DMA Rx descriptors must be contiguous, the default count is 4,
  77. to customize it please redefine ETH_RX_DESC_CNT in ETH GUI (Rx Descriptor Length)
  78. so that updated value will be generated in stm32xxxx_hal_conf.h
  79. 1.b. ETH DMA Tx descriptors must be contiguous, the default count is 4,
  80. to customize it please redefine ETH_TX_DESC_CNT in ETH GUI (Tx Descriptor Length)
  81. so that updated value will be generated in stm32xxxx_hal_conf.h
  82. 2.a. Rx Buffers number must be between ETH_RX_DESC_CNT and 2*ETH_RX_DESC_CNT
  83. 2.b. Rx Buffers must have the same size: ETH_RX_BUFFER_SIZE, this value must
  84. passed to ETH DMA in the init field (heth.Init.RxBuffLen)
  85. 2.c The RX Ruffers addresses and sizes must be properly defined to be aligned
  86. to L1-CACHE line size (32 bytes).
  87. */
  88. /* Data Type Definitions */
  89. typedef enum
  90. {
  91. RX_ALLOC_OK = 0x00,
  92. RX_ALLOC_ERROR = 0x01
  93. } RxAllocStatusTypeDef;
  94. typedef struct
  95. {
  96. struct pbuf_custom pbuf_custom;
  97. uint8_t buff[(ETH_RX_BUFFER_SIZE + 31) & ~31] __ALIGNED(32);
  98. } RxBuff_t;
  99. /* Memory Pool Declaration */
  100. #define ETH_RX_BUFFER_CNT 12U
  101. LWIP_MEMPOOL_DECLARE(RX_POOL, ETH_RX_BUFFER_CNT, sizeof(RxBuff_t), "Zero-copy RX PBUF pool");
  102. /* Variable Definitions */
  103. static uint8_t RxAllocStatus;
  104. __IO uint32_t TxPkt = 0;
  105. __IO uint32_t RxPkt = 0;
  106. #if defined ( __ICCARM__ ) /*!< IAR Compiler */
  107. #pragma location=0x30000000
  108. ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */
  109. #pragma location=0x30000200
  110. ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */
  111. #elif defined ( __CC_ARM ) /* MDK ARM Compiler */
  112. __attribute__((at(0x30000000))) ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */
  113. __attribute__((at(0x30000200))) ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */
  114. #elif defined ( __GNUC__ ) /* GNU Compiler */
  115. ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT] __attribute__((section(".RxDecripSection"))); /* Ethernet Rx DMA Descriptors */
  116. ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT] __attribute__((section(".TxDecripSection"))); /* Ethernet Tx DMA Descriptors */
  117. #endif
  118. /* USER CODE BEGIN 2 */
  119. /* ETH_CODE: placement of RX_POOL
  120. * Please note this was tested only for GCC compiler.
  121. * Additional code needed in linkerscript for GCC.
  122. *
  123. * Also this buffer can be placed in D1 SRAM
  124. * if there is not sufficient space in D2.
  125. * This can be case of STM32H72x/H73x devices.
  126. * However the 32-byte alignment should be forced.
  127. * Below is example of placement into BSS section
  128. *
  129. * . = ALIGN(32);
  130. * *(.Rx_PoolSection)
  131. * . = ALIGN(4);
  132. * _ebss = .;
  133. * __bss_end__ = _ebss;
  134. * } >RAM_D1
  135. */
  136. #if defined ( __ICCARM__ ) /*!< IAR Compiler */
  137. #pragma location = 0x30040200
  138. extern u8_t memp_memory_RX_POOL_base[];
  139. #elif defined ( __CC_ARM ) /* MDK ARM Compiler */
  140. __attribute__((at(0x30040200)) extern u8_t memp_memory_RX_POOL_base[];
  141. #elif defined ( __GNUC__ ) /* GNU Compiler */
  142. __attribute__((section(".Rx_PoolSection"))) extern u8_t memp_memory_RX_POOL_base[];
  143. #endif
  144. /* USER CODE END 2 */
  145. osSemaphoreId RxPktSemaphore = NULL; /* Semaphore to signal incoming packets */
  146. osSemaphoreId TxPktSemaphore = NULL; /* Semaphore to signal transmit packet complete */
  147. /* Global Ethernet handle */
  148. ETH_HandleTypeDef heth;
  149. ETH_TxPacketConfig TxConfig;
  150. /* Private function prototypes -----------------------------------------------*/
  151. int32_t ETH_PHY_IO_Init(void);
  152. int32_t ETH_PHY_IO_DeInit (void);
  153. int32_t ETH_PHY_IO_ReadReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t *pRegVal);
  154. int32_t ETH_PHY_IO_WriteReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t RegVal);
  155. int32_t ETH_PHY_IO_GetTick(void);
  156. dp83848_Object_t DP83848;
  157. dp83848_IOCtx_t DP83848_IOCtx = {ETH_PHY_IO_Init,
  158. ETH_PHY_IO_DeInit,
  159. ETH_PHY_IO_WriteReg,
  160. ETH_PHY_IO_ReadReg,
  161. ETH_PHY_IO_GetTick};
  162. /* USER CODE BEGIN 3 */
  163. #if USE_DHCP
  164. void dhcp_sm(struct netif *netif, enum dhcp_states *state);
  165. #endif
  166. /* USER CODE END 3 */
  167. /* Private functions ---------------------------------------------------------*/
  168. void pbuf_free_custom(struct pbuf *p);
  169. /**
  170. * @brief Ethernet Rx Transfer completed callback
  171. * @param handlerEth: ETH handler
  172. * @retval None
  173. */
  174. void HAL_ETH_RxCpltCallback(ETH_HandleTypeDef *handlerEth)
  175. {
  176. osSemaphoreRelease(RxPktSemaphore);
  177. }
  178. /**
  179. * @brief Ethernet Tx Transfer completed callback
  180. * @param handlerEth: ETH handler
  181. * @retval None
  182. */
  183. void HAL_ETH_TxCpltCallback(ETH_HandleTypeDef *handlerEth)
  184. {
  185. osSemaphoreRelease(TxPktSemaphore);
  186. }
  187. /**
  188. * @brief Ethernet DMA transfer error callback
  189. * @param handlerEth: ETH handler
  190. * @retval None
  191. */
  192. void HAL_ETH_ErrorCallback(ETH_HandleTypeDef *handlerEth)
  193. {
  194. if((HAL_ETH_GetDMAError(handlerEth) & ETH_DMACSR_RBU) == ETH_DMACSR_RBU)
  195. {
  196. osSemaphoreRelease(RxPktSemaphore);
  197. }
  198. }
  199. /* USER CODE BEGIN 4 */
  200. /* USER CODE END 4 */
  201. /*******************************************************************************
  202. LL Driver Interface ( LwIP stack --> ETH)
  203. *******************************************************************************/
  204. /**
  205. * @brief In this function, the hardware should be initialized.
  206. * Called from ethernetif_init().
  207. *
  208. * @param netif the already initialized lwip network interface structure
  209. * for this ethernetif
  210. */
  211. static void low_level_init(struct netif *netif)
  212. {
  213. HAL_StatusTypeDef hal_eth_init_status = HAL_OK;
  214. /* USER CODE BEGIN OS_THREAD_ATTR_CMSIS_RTOS_V2 */
  215. osThreadAttr_t attributes;
  216. /* USER CODE END OS_THREAD_ATTR_CMSIS_RTOS_V2 */
  217. uint32_t duplex, speed = 0;
  218. int32_t PHYLinkState = 0;
  219. ETH_MACConfigTypeDef MACConf = {0};
  220. /* Start ETH HAL Init */
  221. uint8_t MACAddr[6] ;
  222. heth.Instance = ETH;
  223. // MACAddr[0] = 0x00;
  224. // MACAddr[1] = 0x80;
  225. // MACAddr[2] = 0xE1;
  226. // MACAddr[3] = 0x00;
  227. // MACAddr[4] = 0x00;
  228. // MACAddr[5] = 0x00;
  229. MACAddr[0] = 0x7C;
  230. MACAddr[1] = 0xF6;
  231. MACAddr[2] = 0x66;
  232. MACAddr[3] = 0xE4;
  233. MACAddr[4] = 0xB5;
  234. MACAddr[5] = 0x41;
  235. heth.Init.MACAddr = &MACAddr[0];
  236. heth.Init.MediaInterface = HAL_ETH_MII_MODE;
  237. heth.Init.TxDesc = DMATxDscrTab;
  238. heth.Init.RxDesc = DMARxDscrTab;
  239. heth.Init.RxBuffLen = 1536;
  240. /* USER CODE BEGIN MACADDRESS */
  241. /* USER CODE END MACADDRESS */
  242. hal_eth_init_status = HAL_ETH_Init(&heth);
  243. memset(&TxConfig, 0 , sizeof(ETH_TxPacketConfig));
  244. TxConfig.Attributes = ETH_TX_PACKETS_FEATURES_CSUM | ETH_TX_PACKETS_FEATURES_CRCPAD;
  245. TxConfig.ChecksumCtrl = ETH_CHECKSUM_IPHDR_PAYLOAD_INSERT_PHDR_CALC;
  246. TxConfig.CRCPadCtrl = ETH_CRC_PAD_INSERT;
  247. /* End ETH HAL Init */
  248. /* Initialize the RX POOL */
  249. LWIP_MEMPOOL_INIT(RX_POOL);
  250. #if LWIP_ARP || LWIP_ETHERNET
  251. /* set MAC hardware address length */
  252. netif->hwaddr_len = ETH_HWADDR_LEN;
  253. /* set MAC hardware address */
  254. netif->hwaddr[0] = heth.Init.MACAddr[0];
  255. netif->hwaddr[1] = heth.Init.MACAddr[1];
  256. netif->hwaddr[2] = heth.Init.MACAddr[2];
  257. netif->hwaddr[3] = heth.Init.MACAddr[3];
  258. netif->hwaddr[4] = heth.Init.MACAddr[4];
  259. netif->hwaddr[5] = heth.Init.MACAddr[5];
  260. /* maximum transfer unit */
  261. netif->mtu = ETH_MAX_PAYLOAD;
  262. /* Accept broadcast address and ARP traffic */
  263. /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
  264. #if LWIP_ARP
  265. netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
  266. #else
  267. netif->flags |= NETIF_FLAG_BROADCAST;
  268. #endif /* LWIP_ARP */
  269. /* create a binary semaphore used for informing ethernetif of frame reception */
  270. RxPktSemaphore = osSemaphoreNew(1, 1, NULL);
  271. /* create a binary semaphore used for informing ethernetif of frame transmission */
  272. TxPktSemaphore = osSemaphoreNew(1, 1, NULL);
  273. /* create the task that handles the ETH_MAC */
  274. /* USER CODE BEGIN OS_THREAD_NEW_CMSIS_RTOS_V2 */
  275. memset(&attributes, 0x0, sizeof(osThreadAttr_t));
  276. attributes.name = "EthIf";
  277. attributes.stack_size = INTERFACE_THREAD_STACK_SIZE;
  278. attributes.priority = osPriorityRealtime;
  279. osThreadNew(ethernetif_input, netif, &attributes);
  280. /* USER CODE END OS_THREAD_NEW_CMSIS_RTOS_V2 */
  281. /* USER CODE BEGIN PHY_PRE_CONFIG */
  282. /* USER CODE END PHY_PRE_CONFIG */
  283. /* Set PHY IO functions */
  284. DP83848_RegisterBusIO(&DP83848, &DP83848_IOCtx);
  285. /* Initialize the DP83848 ETH PHY */
  286. DP83848_Init(&DP83848);
  287. if (hal_eth_init_status == HAL_OK)
  288. {
  289. PHYLinkState = DP83848_GetLinkState(&DP83848);
  290. /* Get link state */
  291. if(PHYLinkState <= DP83848_STATUS_LINK_DOWN)
  292. {
  293. netif_set_link_down(netif);
  294. netif_set_down(netif);
  295. }
  296. else
  297. {
  298. switch (PHYLinkState)
  299. {
  300. case DP83848_STATUS_100MBITS_FULLDUPLEX:
  301. duplex = ETH_FULLDUPLEX_MODE;
  302. speed = ETH_SPEED_100M;
  303. break;
  304. case DP83848_STATUS_100MBITS_HALFDUPLEX:
  305. duplex = ETH_HALFDUPLEX_MODE;
  306. speed = ETH_SPEED_100M;
  307. break;
  308. case DP83848_STATUS_10MBITS_FULLDUPLEX:
  309. duplex = ETH_FULLDUPLEX_MODE;
  310. speed = ETH_SPEED_10M;
  311. break;
  312. case DP83848_STATUS_10MBITS_HALFDUPLEX:
  313. duplex = ETH_HALFDUPLEX_MODE;
  314. speed = ETH_SPEED_10M;
  315. break;
  316. default:
  317. duplex = ETH_FULLDUPLEX_MODE;
  318. speed = ETH_SPEED_100M;
  319. break;
  320. }
  321. /* Get MAC Config MAC */
  322. HAL_ETH_GetMACConfig(&heth, &MACConf);
  323. MACConf.DuplexMode = duplex;
  324. MACConf.Speed = speed;
  325. HAL_ETH_SetMACConfig(&heth, &MACConf);
  326. // HAL_ETH_Start_IT(&heth);
  327. HAL_ETH_Start_IT(&heth);
  328. netif_set_up(netif);
  329. netif_set_link_up(netif);
  330. /* USER CODE BEGIN PHY_POST_CONFIG */
  331. ethernetif_notify_conn_changed(netif);
  332. /* USER CODE END PHY_POST_CONFIG */
  333. }
  334. }
  335. else
  336. {
  337. Error_Handler();
  338. }
  339. #endif /* LWIP_ARP || LWIP_ETHERNET */
  340. /* USER CODE BEGIN LOW_LEVEL_INIT */
  341. /* USER CODE END LOW_LEVEL_INIT */
  342. }
  343. /**
  344. * @brief This function should do the actual transmission of the packet. The packet is
  345. * contained in the pbuf that is passed to the function. This pbuf
  346. * might be chained.
  347. *
  348. * @param netif the lwip network interface structure for this ethernetif
  349. * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
  350. * @return ERR_OK if the packet could be sent
  351. * an err_t value if the packet couldn't be sent
  352. *
  353. * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
  354. * strange results. You might consider waiting for space in the DMA queue
  355. * to become available since the stack doesn't retry to send a packet
  356. * dropped because of memory failure (except for the TCP timers).
  357. */
  358. static err_t low_level_output(struct netif *netif, struct pbuf *p)
  359. {
  360. uint32_t i = 0U;
  361. struct pbuf *q = NULL;
  362. err_t errval = ERR_OK;
  363. ETH_BufferTypeDef Txbuffer[ETH_TX_DESC_CNT] = {0};
  364. memset(Txbuffer, 0 , ETH_TX_DESC_CNT*sizeof(ETH_BufferTypeDef));
  365. for(q = p; q != NULL; q = q->next)
  366. {
  367. if(i >= ETH_TX_DESC_CNT)
  368. return ERR_IF;
  369. Txbuffer[i].buffer = q->payload;
  370. Txbuffer[i].len = q->len;
  371. if(i>0)
  372. {
  373. Txbuffer[i-1].next = &Txbuffer[i];
  374. }
  375. if(q->next == NULL)
  376. {
  377. Txbuffer[i].next = NULL;
  378. }
  379. i++;
  380. }
  381. TxConfig.Length = p->tot_len;
  382. TxConfig.TxBuffer = Txbuffer;
  383. TxConfig.pData = p;
  384. pbuf_ref(p);
  385. #if 1
  386. if (HAL_ETH_Transmit_IT(&heth, &TxConfig) == HAL_OK) {
  387. while(osSemaphoreAcquire(TxPktSemaphore, TIME_WAITING_FOR_INPUT)!=osOK)
  388. {
  389. }
  390. // osSemaphoreAcquire(TxPktSemaphore, pdMS_TO_TICKS(1000));
  391. HAL_ETH_ReleaseTxPacket(&heth);
  392. } else {
  393. pbuf_free(p);
  394. }
  395. #else
  396. HAL_ETH_Transmit_IT(&heth, &TxConfig);
  397. osSemaphoreAcquire(TxPktSemaphore, pdMS_TO_TICKS(100));
  398. HAL_ETH_ReleaseTxPacket(&heth);
  399. #endif
  400. return errval;
  401. }
  402. /**
  403. * @brief Should allocate a pbuf and transfer the bytes of the incoming
  404. * packet from the interface into the pbuf.
  405. *
  406. * @param netif the lwip network interface structure for this ethernetif
  407. * @return a pbuf filled with the received packet (including MAC header)
  408. * NULL on memory error
  409. */
  410. static struct pbuf * low_level_input(struct netif *netif)
  411. {
  412. struct pbuf *p = NULL;
  413. if(RxAllocStatus == RX_ALLOC_OK)
  414. {
  415. HAL_ETH_ReadData(&heth, (void **)&p);
  416. }
  417. return p;
  418. }
  419. /**
  420. * @brief This function should be called when a packet is ready to be read
  421. * from the interface. It uses the function low_level_input() that
  422. * should handle the actual reception of bytes from the network
  423. * interface. Then the type of the received packet is determined and
  424. * the appropriate input function is called.
  425. *
  426. * @param netif the lwip network interface structure for this ethernetif
  427. */
  428. void ethernetif_input(void* argument)
  429. {
  430. struct pbuf *p = NULL;
  431. struct netif *netif = (struct netif *) argument;
  432. for( ;; )
  433. {
  434. if (osSemaphoreAcquire(RxPktSemaphore, TIME_WAITING_FOR_INPUT) == osOK)
  435. {
  436. do
  437. {
  438. p = low_level_input( netif );
  439. if (p != NULL)
  440. {
  441. if (netif->input( p, netif) != ERR_OK )
  442. {
  443. pbuf_free(p);
  444. }
  445. }
  446. } while(p!=NULL);
  447. }
  448. }
  449. }
  450. #if !LWIP_ARP
  451. /**
  452. * This function has to be completed by user in case of ARP OFF.
  453. *
  454. * @param netif the lwip network interface structure for this ethernetif
  455. * @return ERR_OK if ...
  456. */
  457. static err_t low_level_output_arp_off(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr)
  458. {
  459. err_t errval;
  460. errval = ERR_OK;
  461. /* USER CODE BEGIN 5 */
  462. /* USER CODE END 5 */
  463. return errval;
  464. }
  465. #endif /* LWIP_ARP */
  466. /**
  467. * @brief Should be called at the beginning of the program to set up the
  468. * network interface. It calls the function low_level_init() to do the
  469. * actual setup of the hardware.
  470. *
  471. * This function should be passed as a parameter to netif_add().
  472. *
  473. * @param netif the lwip network interface structure for this ethernetif
  474. * @return ERR_OK if the loopif is initialized
  475. * ERR_MEM if private data couldn't be allocated
  476. * any other err_t on error
  477. */
  478. err_t ethernetif_init(struct netif *netif)
  479. {
  480. LWIP_ASSERT("netif != NULL", (netif != NULL));
  481. #if LWIP_NETIF_HOSTNAME
  482. /* Initialize interface hostname */
  483. netif->hostname = "lwip";
  484. #endif /* LWIP_NETIF_HOSTNAME */
  485. /*
  486. * Initialize the snmp variables and counters inside the struct netif.
  487. * The last argument should be replaced with your link speed, in units
  488. * of bits per second.
  489. */
  490. // MIB2_INIT_NETIF(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS);
  491. netif->name[0] = IFNAME0;
  492. netif->name[1] = IFNAME1;
  493. /* We directly use etharp_output() here to save a function call.
  494. * You can instead declare your own function an call etharp_output()
  495. * from it if you have to do some checks before sending (e.g. if link
  496. * is available...) */
  497. #if LWIP_IPV4
  498. #if LWIP_ARP || LWIP_ETHERNET
  499. #if LWIP_ARP
  500. netif->output = etharp_output;
  501. #else
  502. /* The user should write its own code in low_level_output_arp_off function */
  503. netif->output = low_level_output_arp_off;
  504. #endif /* LWIP_ARP */
  505. #endif /* LWIP_ARP || LWIP_ETHERNET */
  506. #endif /* LWIP_IPV4 */
  507. #if LWIP_IPV6
  508. netif->output_ip6 = ethip6_output;
  509. #endif /* LWIP_IPV6 */
  510. netif->linkoutput = low_level_output;
  511. /* initialize the hardware */
  512. low_level_init(netif);
  513. return ERR_OK;
  514. }
  515. /**
  516. * @brief Custom Rx pbuf free callback
  517. * @param pbuf: pbuf to be freed
  518. * @retval None
  519. */
  520. void pbuf_free_custom(struct pbuf *p)
  521. {
  522. struct pbuf_custom* custom_pbuf = (struct pbuf_custom*)p;
  523. LWIP_MEMPOOL_FREE(RX_POOL, custom_pbuf);
  524. /* If the Rx Buffer Pool was exhausted, signal the ethernetif_input task to
  525. * call HAL_ETH_GetRxDataBuffer to rebuild the Rx descriptors. */
  526. if (RxAllocStatus == RX_ALLOC_ERROR)
  527. {
  528. RxAllocStatus = RX_ALLOC_OK;
  529. osSemaphoreRelease(RxPktSemaphore);
  530. }
  531. }
  532. /* USER CODE BEGIN 6 */
  533. /**
  534. * @brief Returns the current time in milliseconds
  535. * when LWIP_TIMERS == 1 and NO_SYS == 1
  536. * @param None
  537. * @retval Current Time value
  538. */
  539. u32_t sys_now(void)
  540. {
  541. return HAL_GetTick();
  542. }
  543. /* USER CODE END 6 */
  544. /**
  545. * @brief Initializes the ETH MSP.
  546. * @param ethHandle: ETH handle
  547. * @retval None
  548. */
  549. void HAL_ETH_MspInit(ETH_HandleTypeDef* ethHandle)
  550. {
  551. GPIO_InitTypeDef GPIO_InitStruct = {0};
  552. if(ethHandle->Instance==ETH)
  553. {
  554. /* USER CODE BEGIN ETH_MspInit 0 */
  555. /* USER CODE END ETH_MspInit 0 */
  556. /* Enable Peripheral clock */
  557. __HAL_RCC_ETH1MAC_CLK_ENABLE();
  558. __HAL_RCC_ETH1TX_CLK_ENABLE();
  559. __HAL_RCC_ETH1RX_CLK_ENABLE();
  560. __HAL_RCC_GPIOC_CLK_ENABLE();
  561. __HAL_RCC_GPIOA_CLK_ENABLE();
  562. __HAL_RCC_GPIOB_CLK_ENABLE();
  563. /**ETH GPIO Configuration
  564. PC1 ------> ETH_MDC
  565. PC2_C ------> ETH_TXD2
  566. PC3_C ------> ETH_TX_CLK
  567. PA0 ------> ETH_CRS
  568. PA1 ------> ETH_RX_CLK
  569. PA2 ------> ETH_MDIO
  570. PA3 ------> ETH_COL
  571. PA7 ------> ETH_RX_DV
  572. PC4 ------> ETH_RXD0
  573. PC5 ------> ETH_RXD1
  574. PB0 ------> ETH_RXD2
  575. PB1 ------> ETH_RXD3
  576. PB11 ------> ETH_TX_EN
  577. PB12 ------> ETH_TXD0
  578. PB13 ------> ETH_TXD1
  579. PB8 ------> ETH_TXD3
  580. */
  581. GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4
  582. |GPIO_PIN_5;
  583. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  584. GPIO_InitStruct.Pull = GPIO_NOPULL;
  585. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  586. GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
  587. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  588. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
  589. |GPIO_PIN_7;
  590. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  591. GPIO_InitStruct.Pull = GPIO_NOPULL;
  592. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  593. GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
  594. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  595. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_11|GPIO_PIN_12
  596. |GPIO_PIN_13|GPIO_PIN_8;
  597. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  598. GPIO_InitStruct.Pull = GPIO_NOPULL;
  599. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  600. GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
  601. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  602. /* Peripheral interrupt init */
  603. HAL_NVIC_SetPriority(ETH_IRQn, 5, 0);
  604. HAL_NVIC_EnableIRQ(ETH_IRQn);
  605. /* USER CODE BEGIN ETH_MspInit 1 */
  606. /* USER CODE END ETH_MspInit 1 */
  607. }
  608. }
  609. void HAL_ETH_MspDeInit(ETH_HandleTypeDef* ethHandle)
  610. {
  611. if(ethHandle->Instance==ETH)
  612. {
  613. /* USER CODE BEGIN ETH_MspDeInit 0 */
  614. /* USER CODE END ETH_MspDeInit 0 */
  615. /* Disable Peripheral clock */
  616. __HAL_RCC_ETH1MAC_CLK_DISABLE();
  617. __HAL_RCC_ETH1TX_CLK_DISABLE();
  618. __HAL_RCC_ETH1RX_CLK_DISABLE();
  619. /**ETH GPIO Configuration
  620. PC1 ------> ETH_MDC
  621. PC2_C ------> ETH_TXD2
  622. PC3_C ------> ETH_TX_CLK
  623. PA0 ------> ETH_CRS
  624. PA1 ------> ETH_RX_CLK
  625. PA2 ------> ETH_MDIO
  626. PA3 ------> ETH_COL
  627. PA7 ------> ETH_RX_DV
  628. PC4 ------> ETH_RXD0
  629. PC5 ------> ETH_RXD1
  630. PB0 ------> ETH_RXD2
  631. PB1 ------> ETH_RXD3
  632. PB11 ------> ETH_TX_EN
  633. PB12 ------> ETH_TXD0
  634. PB13 ------> ETH_TXD1
  635. PB8 ------> ETH_TXD3
  636. */
  637. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4
  638. |GPIO_PIN_5);
  639. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
  640. |GPIO_PIN_7);
  641. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_11|GPIO_PIN_12
  642. |GPIO_PIN_13|GPIO_PIN_8);
  643. /* Peripheral interrupt Deinit*/
  644. HAL_NVIC_DisableIRQ(ETH_IRQn);
  645. /* USER CODE BEGIN ETH_MspDeInit 1 */
  646. /* USER CODE END ETH_MspDeInit 1 */
  647. }
  648. }
  649. /*******************************************************************************
  650. PHI IO Functions
  651. *******************************************************************************/
  652. /**
  653. * @brief Initializes the MDIO interface GPIO and clocks.
  654. * @param None
  655. * @retval 0 if OK, -1 if ERROR
  656. */
  657. int32_t ETH_PHY_IO_Init(void)
  658. {
  659. /* We assume that MDIO GPIO configuration is already done
  660. in the ETH_MspInit() else it should be done here
  661. */
  662. /* Configure the MDIO Clock */
  663. HAL_ETH_SetMDIOClockRange(&heth);
  664. return 0;
  665. }
  666. /**
  667. * @brief De-Initializes the MDIO interface .
  668. * @param None
  669. * @retval 0 if OK, -1 if ERROR
  670. */
  671. int32_t ETH_PHY_IO_DeInit (void)
  672. {
  673. return 0;
  674. }
  675. /**
  676. * @brief Read a PHY register through the MDIO interface.
  677. * @param DevAddr: PHY port address
  678. * @param RegAddr: PHY register address
  679. * @param pRegVal: pointer to hold the register value
  680. * @retval 0 if OK -1 if Error
  681. */
  682. int32_t ETH_PHY_IO_ReadReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t *pRegVal)
  683. {
  684. if(HAL_ETH_ReadPHYRegister(&heth, DevAddr, RegAddr, pRegVal) != HAL_OK)
  685. {
  686. return -1;
  687. }
  688. return 0;
  689. }
  690. /**
  691. * @brief Write a value to a PHY register through the MDIO interface.
  692. * @param DevAddr: PHY port address
  693. * @param RegAddr: PHY register address
  694. * @param RegVal: Value to be written
  695. * @retval 0 if OK -1 if Error
  696. */
  697. int32_t ETH_PHY_IO_WriteReg(uint32_t DevAddr, uint32_t RegAddr, uint32_t RegVal)
  698. {
  699. if(HAL_ETH_WritePHYRegister(&heth, DevAddr, RegAddr, RegVal) != HAL_OK)
  700. {
  701. return -1;
  702. }
  703. return 0;
  704. }
  705. /**
  706. * @brief Get the time in millisecons used for internal PHY driver process.
  707. * @retval Time value
  708. */
  709. int32_t ETH_PHY_IO_GetTick(void)
  710. {
  711. return HAL_GetTick();
  712. }
  713. /**
  714. * @brief Check the ETH link state then update ETH driver and netif link accordingly.
  715. * @retval None
  716. */
  717. void ethernet_link_thread(void* argument)
  718. {
  719. ETH_MACConfigTypeDef MACConf = {0};
  720. int32_t PHYLinkState = 0;
  721. uint32_t linkchanged = 0U, speed = 0U, duplex = 0U;
  722. struct netif *netif = (struct netif *) argument;
  723. /* USER CODE BEGIN ETH link init */
  724. #if USE_DHCP
  725. enum dhcp_states DHCP_state = DHCP_START;
  726. #endif
  727. /* ETH_CODE: call HAL_ETH_Start_IT instead of HAL_ETH_Start
  728. * This is required for operation with RTOS.
  729. * This trick allows to keep this change through
  730. * code re-generation by STM32CubeMX
  731. */
  732. #define HAL_ETH_Start HAL_ETH_Start_IT
  733. /* ETH_CODE: workaround to call LOCK_TCPIP_CORE when accessing netif link functions*/
  734. // LOCK_TCPIP_CORE();
  735. /* USER CODE END ETH link init */
  736. for(;;)
  737. {
  738. PHYLinkState = DP83848_GetLinkState(&DP83848);
  739. if(netif_is_link_up(netif) && (PHYLinkState <= DP83848_STATUS_LINK_DOWN))
  740. {
  741. HAL_ETH_Stop_IT(&heth);
  742. LOCK_TCPIP_CORE();
  743. netif_set_down(netif);
  744. netif_set_link_down(netif);
  745. UNLOCK_TCPIP_CORE();
  746. printf("Link down...\r\n");
  747. }
  748. else if(!netif_is_link_up(netif) && (PHYLinkState > DP83848_STATUS_LINK_DOWN))
  749. {
  750. switch (PHYLinkState)
  751. {
  752. case DP83848_STATUS_100MBITS_FULLDUPLEX:
  753. duplex = ETH_FULLDUPLEX_MODE;
  754. speed = ETH_SPEED_100M;
  755. linkchanged = 1;
  756. break;
  757. case DP83848_STATUS_100MBITS_HALFDUPLEX:
  758. duplex = ETH_HALFDUPLEX_MODE;
  759. speed = ETH_SPEED_100M;
  760. linkchanged = 1;
  761. break;
  762. case DP83848_STATUS_10MBITS_FULLDUPLEX:
  763. duplex = ETH_FULLDUPLEX_MODE;
  764. speed = ETH_SPEED_10M;
  765. linkchanged = 1;
  766. break;
  767. case DP83848_STATUS_10MBITS_HALFDUPLEX:
  768. duplex = ETH_HALFDUPLEX_MODE;
  769. speed = ETH_SPEED_10M;
  770. linkchanged = 1;
  771. break;
  772. default:
  773. break;
  774. }
  775. if(linkchanged)
  776. {
  777. /* Get MAC Config MAC */
  778. HAL_ETH_GetMACConfig(&heth, &MACConf);
  779. MACConf.DuplexMode = duplex;
  780. MACConf.Speed = speed;
  781. HAL_ETH_SetMACConfig(&heth, &MACConf);
  782. HAL_ETH_Start_IT(&heth);
  783. LOCK_TCPIP_CORE();
  784. netif_set_up(netif);
  785. netif_set_link_up(netif);
  786. UNLOCK_TCPIP_CORE();
  787. printf("Link up...\r\n");
  788. }
  789. }
  790. /* USER CODE BEGIN ETH link Thread core code for User BSP */
  791. #if USE_DHCP
  792. dhcp_sm(netif, &DHCP_state);
  793. #endif
  794. /* ETH_CODE: workaround to call LOCK_TCPIP_CORE when accessing netif link functions*/
  795. // UNLOCK_TCPIP_CORE();
  796. osDelay(pdMS_TO_TICKS(500));
  797. // LOCK_TCPIP_CORE();
  798. continue; /* skip next osDelay */
  799. /* USER CODE END ETH link Thread core code for User BSP */
  800. osDelay(100);
  801. }
  802. }
  803. /**
  804. * @brief This function notify user about link status changement.
  805. * @param netif: the network interface
  806. * @retval None
  807. */
  808. __weak void ethernetif_notify_conn_changed(struct netif *netif)
  809. {
  810. /* NOTE : This is function could be implemented in user file
  811. when the callback is needed,
  812. */
  813. }
  814. void HAL_ETH_RxAllocateCallback(uint8_t **buff)
  815. {
  816. /* USER CODE BEGIN HAL ETH RxAllocateCallback */
  817. struct pbuf_custom *p = LWIP_MEMPOOL_ALLOC(RX_POOL);
  818. if (p)
  819. {
  820. /* Get the buff from the struct pbuf address. */
  821. *buff = (uint8_t *)p + offsetof(RxBuff_t, buff);
  822. p->custom_free_function = pbuf_free_custom;
  823. /* Initialize the struct pbuf.
  824. * This must be performed whenever a buffer's allocated because it may be
  825. * changed by lwIP or the app, e.g., pbuf_free decrements ref. */
  826. pbuf_alloced_custom(PBUF_RAW, 0, PBUF_REF, p, *buff, ETH_RX_BUFFER_SIZE);
  827. }
  828. else
  829. {
  830. RxAllocStatus = RX_ALLOC_ERROR;
  831. *buff = NULL;
  832. }
  833. /* USER CODE END HAL ETH RxAllocateCallback */
  834. }
  835. void HAL_ETH_RxLinkCallback(void **pStart, void **pEnd, uint8_t *buff, uint16_t Length)
  836. {
  837. /* USER CODE BEGIN HAL ETH RxLinkCallback */
  838. struct pbuf **ppStart = (struct pbuf **)pStart;
  839. struct pbuf **ppEnd = (struct pbuf **)pEnd;
  840. struct pbuf *p = NULL;
  841. /* Get the struct pbuf from the buff address. */
  842. p = (struct pbuf *)(buff - offsetof(RxBuff_t, buff));
  843. p->next = NULL;
  844. p->tot_len = 0;
  845. p->len = Length;
  846. /* Chain the buffer. */
  847. if (!*ppStart)
  848. {
  849. /* The first buffer of the packet. */
  850. *ppStart = p;
  851. }
  852. else
  853. {
  854. /* Chain the buffer to the end of the packet. */
  855. (*ppEnd)->next = p;
  856. }
  857. *ppEnd = p;
  858. /* Update the total length of all the buffers of the chain. Each pbuf in the chain should have its tot_len
  859. * set to its own length, plus the length of all the following pbufs in the chain. */
  860. for (p = *ppStart; p != NULL; p = p->next)
  861. {
  862. p->tot_len += Length;
  863. }
  864. /* Invalidate data cache because Rx DMA's writing to physical memory makes it stale. */
  865. SCB_InvalidateDCache_by_Addr((uint32_t *)buff, Length);
  866. /* USER CODE END HAL ETH RxLinkCallback */
  867. }
  868. void HAL_ETH_TxFreeCallback(uint32_t * buff)
  869. {
  870. /* USER CODE BEGIN HAL ETH TxFreeCallback */
  871. pbuf_free((struct pbuf *)buff);
  872. /* USER CODE END HAL ETH TxFreeCallback */
  873. }
  874. /* USER CODE BEGIN 8 */
  875. /* ETH_CODE: add functions needed for proper multithreading support and check */
  876. static osThreadId_t lwip_core_lock_holder_thread_id;
  877. static osThreadId_t lwip_tcpip_thread_id;
  878. void sys_lock_tcpip_core(void){
  879. sys_mutex_lock(&lock_tcpip_core);
  880. lwip_core_lock_holder_thread_id = osThreadGetId();
  881. }
  882. void sys_unlock_tcpip_core(void){
  883. lwip_core_lock_holder_thread_id = 0;
  884. sys_mutex_unlock(&lock_tcpip_core);
  885. }
  886. void sys_check_core_locking(void){
  887. /* Embedded systems should check we are NOT in an interrupt context here */
  888. LWIP_ASSERT("Function called from interrupt context", (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) == 0);
  889. if (lwip_tcpip_thread_id != 0) {
  890. osThreadId_t current_thread_id = osThreadGetId();
  891. #if LWIP_TCPIP_CORE_LOCKING
  892. LWIP_ASSERT("Function called without core lock", current_thread_id == lwip_core_lock_holder_thread_id);
  893. /* ETH_CODE: to easily check that example has correct handling of core lock
  894. * This will trigger breakpoint (__BKPT)
  895. */
  896. #warning Below check should be removed in production code
  897. if(current_thread_id != lwip_core_lock_holder_thread_id) __BKPT(0);
  898. #else /* LWIP_TCPIP_CORE_LOCKING */
  899. LWIP_ASSERT("Function called from wrong thread", current_thread_id == lwip_tcpip_thread_id);
  900. #endif /* LWIP_TCPIP_CORE_LOCKING */
  901. LWIP_UNUSED_ARG(current_thread_id); /* for LWIP_NOASSERT */
  902. }
  903. }
  904. void sys_mark_tcpip_thread(void){
  905. lwip_tcpip_thread_id = osThreadGetId();
  906. }
  907. #if USE_DHCP
  908. void dhcp_sm(struct netif *netif, enum dhcp_states *state)
  909. {
  910. struct dhcp *dhcp;
  911. ip_addr_t ipaddr;
  912. ip_addr_t netmask;
  913. ip_addr_t gw;
  914. #ifdef DHCP_USER_LOGS
  915. uint8_t iptxt[20];
  916. #endif
  917. switch(*state)
  918. {
  919. case DHCP_START:
  920. *state = DHCP_WAIT_ADDRESS;
  921. dhcp = (struct dhcp *)netif_get_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP);
  922. #ifdef DHCP_USER_LOGS
  923. printf(" State: Looking for DHCP server ...\n");
  924. #endif
  925. break;
  926. case DHCP_WAIT_ADDRESS:
  927. if (dhcp_supplied_address(netif))
  928. {
  929. *state = DHCP_ADDRESS_ASSIGNED;
  930. #ifdef DHCP_USER_LOGS
  931. sprintf((char *)iptxt, "%s", ip4addr_ntoa((const ip4_addr_t *)&netif->ip_addr));
  932. printf("IP address assigned by a DHCP server: %s\n", iptxt);
  933. #endif
  934. }
  935. else
  936. {
  937. dhcp = (struct dhcp *)netif_get_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP);
  938. /* DHCP timeout */
  939. if (dhcp->tries > MAX_DHCP_TRIES)
  940. {
  941. *state = DHCP_TIMEOUT;
  942. /* Stop DHCP */
  943. LOCK_TCPIP_CORE();
  944. dhcp_stop(netif);
  945. UNLOCK_TCPIP_CORE();
  946. /* Static address used */
  947. ipaddr_aton(STATIC_IP, &ipaddr);
  948. ipaddr_aton(STATIC_MASK, &netmask);
  949. ipaddr_aton(STATIC_GW, &gw);
  950. LOCK_TCPIP_CORE();
  951. netif_set_addr(netif, ip_2_ip4(&ipaddr), ip_2_ip4(&netmask), ip_2_ip4(&gw));
  952. UNLOCK_TCPIP_CORE();
  953. #ifdef DHCP_USER_LOGS
  954. sprintf((char *)iptxt, "%s", ip4addr_ntoa((const ip4_addr_t *)&netif->ip_addr));
  955. printf("DHCP Timeout !! \n");
  956. printf("Static IP address: %s\n", iptxt);
  957. #endif
  958. }
  959. }
  960. break;
  961. case DHCP_ADDRESS_ASSIGNED:
  962. dhcp = (struct dhcp *)netif_get_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP);
  963. if(dhcp->state == 3)
  964. {
  965. *state = DHCP_START;
  966. }
  967. break;
  968. default:
  969. break;
  970. }
  971. }
  972. #endif
  973. /* USER CODE END 8 */