dm9161.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*!
  2. \file gd32f4xx_enet_eval.c
  3. \brief ethernet hardware configuration
  4. */
  5. /*
  6. Copyright (C) 2016 GigaDevice
  7. 2016-08-15, V1.0.0, firmware for GD32F4xx
  8. */
  9. #include "dm9161.h"
  10. #include "interface.h"
  11. #include "lwip/api.h"
  12. #include "lwip/sockets.h"
  13. #include "netconf.h"
  14. #include "stm32f4x7_eth.h"
  15. static __IO uint32_t enet_init_status = 0;
  16. static void dm9161_gpio_config(void);
  17. static void dm9161_mac_dma_config(void);
  18. static void nvic_configuration(void);
  19. /*!
  20. \brief setup ethernet system(GPIOs, clocks, MAC, DMA, systick)
  21. \param[in] none
  22. \param[out] none
  23. \retval none
  24. */
  25. void enet_system_setup(void)
  26. {
  27. nvic_configuration();
  28. /* configure the GPIO ports for ethernet pins */
  29. dm9161_gpio_config();
  30. /* configure the ethernet MAC/DMA */
  31. dm9161_mac_dma_config();
  32. /* Read PHY status register: Get Ethernet link status */
  33. if (ETH_ReadPHYRegister(DP83848_PHY_ADDRESS, PHY_SR) & 1)
  34. {
  35. // enet_init_status |= ETH_LINK_FLAG;
  36. }
  37. // ETH_MACITConfig(ETH_MAC_IT_PMT, ENABLE);
  38. ETH_DMAITConfig(ETH_DMA_IT_R, ENABLE);
  39. ETH_DMAITConfig(ETH_DMA_IT_NIS, ENABLE);
  40. // ETH_MACReceptionCmd(ENABLE);
  41. }
  42. /*!
  43. \brief configures the ethernet interface
  44. \param[in] none
  45. \param[out] none
  46. \retval none
  47. */
  48. static void dm9161_mac_dma_config(void)
  49. {
  50. ErrorStatus reval_state = ERROR;
  51. /* reset ethernet on AHB bus */
  52. ETH_DeInit();
  53. /* Software reset */
  54. ETH_SoftwareReset();
  55. /* Wait for software reset */
  56. reval_state = ETH_GetSoftwareResetStatus();
  57. if (reval_state == ERROR)
  58. {
  59. return;
  60. }
  61. // /* ETHERNET Configuration --------------------------------------------------*/
  62. // /* Call ETH_StructInit if you don't like to configure all ETH_InitStructure parameter */
  63. ETH_InitTypeDef ETH_InitStructure;
  64. ETH_StructInit(&ETH_InitStructure);
  65. /* Fill ETH_InitStructure parametrs */
  66. /*------------------------ MAC -----------------------------------*/
  67. ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Enable;
  68. // ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Disable;
  69. // ETH_InitStructure.ETH_Speed = ETH_Speed_10M;
  70. // ETH_InitStructure.ETH_Mode = ETH_Mode_FullDuplex;
  71. ETH_InitStructure.ETH_LoopbackMode = ETH_LoopbackMode_Disable;
  72. ETH_InitStructure.ETH_RetryTransmission = ETH_RetryTransmission_Disable;
  73. ETH_InitStructure.ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable;
  74. ETH_InitStructure.ETH_ReceiveAll = ETH_ReceiveAll_Disable;
  75. ETH_InitStructure.ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Enable;
  76. ETH_InitStructure.ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
  77. ETH_InitStructure.ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_Perfect;
  78. ETH_InitStructure.ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
  79. #ifdef CHECKSUM_BY_HARDWARE
  80. ETH_InitStructure.ETH_ChecksumOffload = ETH_ChecksumOffload_Enable;
  81. #endif
  82. /*------------------------ DMA -----------------------------------------*/
  83. /* When we use the Checksum offload feature, we need to enable the Store and Forward mode:
  84. the store and forward guarantee that a whole frame is stored in the FIFO, so the MAC can insert/verify the checksum,
  85. if the checksum is OK the DMA can handle the frame otherwise the frame is dropped */
  86. ETH_InitStructure.ETH_DropTCPIPChecksumErrorFrame = ETH_DropTCPIPChecksumErrorFrame_Enable;
  87. ETH_InitStructure.ETH_ReceiveStoreForward = ETH_ReceiveStoreForward_Enable;
  88. ETH_InitStructure.ETH_TransmitStoreForward = ETH_TransmitStoreForward_Enable;
  89. ETH_InitStructure.ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Disable;
  90. ETH_InitStructure.ETH_ForwardUndersizedGoodFrames = ETH_ForwardUndersizedGoodFrames_Disable;
  91. ETH_InitStructure.ETH_SecondFrameOperate = ETH_SecondFrameOperate_Enable;
  92. ETH_InitStructure.ETH_AddressAlignedBeats = ETH_AddressAlignedBeats_Enable;
  93. ETH_InitStructure.ETH_FixedBurst = ETH_FixedBurst_Enable;
  94. ETH_InitStructure.ETH_RxDMABurstLength = ETH_RxDMABurstLength_32Beat;
  95. ETH_InitStructure.ETH_TxDMABurstLength = ETH_TxDMABurstLength_32Beat;
  96. ETH_InitStructure.ETH_DMAArbitration = ETH_DMAArbitration_RoundRobin_RxTx_2_1;
  97. /* Configure Ethernet */
  98. enet_init_status = ETH_Init(&ETH_InitStructure, 0x01);
  99. }
  100. /*!
  101. \brief configures the nested vectored interrupt controller
  102. \param[in] none
  103. \param[out] none
  104. \retval none
  105. */
  106. static void nvic_configuration(void)
  107. {
  108. NVIC_InitTypeDef NVIC_InitStructure;
  109. NVIC_InitStructure.NVIC_IRQChannel = ETH_IRQn;
  110. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  111. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  112. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  113. NVIC_Init(&NVIC_InitStructure);
  114. }
  115. /*!
  116. \brief configures the different GPIO ports
  117. \param[in] none
  118. \param[out] none
  119. \retval none
  120. */
  121. static void dm9161_gpio_config(void)
  122. {
  123. INT16U i = 0;
  124. GPIO_InitTypeDef GPIO_StructInit;
  125. GPIO_StructInit.GPIO_Mode = GPIO_Mode_OUT;
  126. // GPIO_StructInit.GPIO_OType = interface_info[index].GPIO_OType;
  127. GPIO_StructInit.GPIO_Speed = GPIO_High_Speed;
  128. GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_DOWN;
  129. GPIO_StructInit.GPIO_Pin = ETH_RESET_PIN;
  130. GPIO_Init(ETH_RESET_PORT, &GPIO_StructInit);
  131. ETH_RESET_ON;
  132. while (i < 1000)
  133. {
  134. i++;
  135. }
  136. i = 0;
  137. ETH_RESET_OFF;
  138. while (i < 1000)
  139. {
  140. i++;
  141. }
  142. i = 0;
  143. /* Enable SYSCFG clock */
  144. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  145. #ifdef MII_MODE
  146. #ifdef PHY_CLOCK_MCO
  147. /* output HXTAL clock (25MHz) on CKOUT0 pin(PA8) to clock the PHY */
  148. RCC_MCO1Config(RCC_MCO1Source_HSE, RCC_MCO1Div_1);
  149. #endif /* PHY_CLOCK_MCO */
  150. SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_MII);
  151. #elif defined RMII_MODE
  152. SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_RMII);
  153. #endif
  154. }
  155. static err_t bms_net_process(int fd, void *data, int len)
  156. {
  157. INT8U response[2] = {0xBB, 0xAA};
  158. if ((len == 2) && (((INT8U *)data)[0] == 0xAA) && (((INT8U *)data)[1] == 0xBB))
  159. {
  160. send(fd, (void *)response, sizeof(response), 0);
  161. }
  162. return 1;
  163. }
  164. void dm9161_task(void)
  165. {
  166. INT8U buf[50];
  167. INT32S ret = 0;
  168. INT32S sockfd = -1, newfd = -1;
  169. INT32U len = 0;
  170. struct sockaddr_in svr_addr, clt_addr;
  171. svr_addr.sin_family = AF_INET;
  172. svr_addr.sin_port = htons(TCP_PORT);
  173. svr_addr.sin_addr.s_addr = htons(INADDR_ANY);
  174. while (1)
  175. {
  176. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  177. if (sockfd < 0)
  178. {
  179. continue;
  180. }
  181. ret = bind(sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr));
  182. if (ret < 0)
  183. {
  184. lwip_close(sockfd);
  185. sockfd = -1;
  186. continue;
  187. }
  188. ret = listen(sockfd, 1);
  189. if (ret < 0)
  190. {
  191. lwip_close(sockfd);
  192. continue;
  193. }
  194. len = sizeof(clt_addr);
  195. newfd = accept(sockfd, NULL, NULL);
  196. while (-1 != newfd)
  197. {
  198. ret = recv(newfd, buf, sizeof(buf) - 1, 0);
  199. if (ret <= 0)
  200. {
  201. lwip_close(newfd);
  202. newfd = -1;
  203. break;
  204. }
  205. bms_net_process(newfd, buf, ret);
  206. }
  207. lwip_close(sockfd);
  208. sockfd = -1;
  209. // OSTimeDly(10);
  210. }
  211. }