/*! \file gd32f4xx_enet_eval.c \brief ethernet hardware configuration */ /* Copyright (C) 2016 GigaDevice 2016-08-15, V1.0.0, firmware for GD32F4xx */ #include "dm9161.h" #include "interface.h" #include "lwip/api.h" #include "lwip/sockets.h" #include "netconf.h" #include "stm32f4x7_eth.h" static __IO uint32_t enet_init_status = 0; static void dm9161_gpio_config(void); static void dm9161_mac_dma_config(void); static void nvic_configuration(void); /*! \brief setup ethernet system(GPIOs, clocks, MAC, DMA, systick) \param[in] none \param[out] none \retval none */ void enet_system_setup(void) { nvic_configuration(); /* configure the GPIO ports for ethernet pins */ dm9161_gpio_config(); /* configure the ethernet MAC/DMA */ dm9161_mac_dma_config(); /* Read PHY status register: Get Ethernet link status */ if (ETH_ReadPHYRegister(DP83848_PHY_ADDRESS, PHY_SR) & 1) { // enet_init_status |= ETH_LINK_FLAG; } // ETH_MACITConfig(ETH_MAC_IT_PMT, ENABLE); ETH_DMAITConfig(ETH_DMA_IT_R, ENABLE); ETH_DMAITConfig(ETH_DMA_IT_NIS, ENABLE); // ETH_MACReceptionCmd(ENABLE); } /*! \brief configures the ethernet interface \param[in] none \param[out] none \retval none */ static void dm9161_mac_dma_config(void) { ErrorStatus reval_state = ERROR; /* reset ethernet on AHB bus */ ETH_DeInit(); /* Software reset */ ETH_SoftwareReset(); /* Wait for software reset */ reval_state = ETH_GetSoftwareResetStatus(); if (reval_state == ERROR) { return; } // /* ETHERNET Configuration --------------------------------------------------*/ // /* Call ETH_StructInit if you don't like to configure all ETH_InitStructure parameter */ ETH_InitTypeDef ETH_InitStructure; ETH_StructInit(Ð_InitStructure); /* Fill ETH_InitStructure parametrs */ /*------------------------ MAC -----------------------------------*/ ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Enable; // ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Disable; // ETH_InitStructure.ETH_Speed = ETH_Speed_10M; // ETH_InitStructure.ETH_Mode = ETH_Mode_FullDuplex; ETH_InitStructure.ETH_LoopbackMode = ETH_LoopbackMode_Disable; ETH_InitStructure.ETH_RetryTransmission = ETH_RetryTransmission_Disable; ETH_InitStructure.ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable; ETH_InitStructure.ETH_ReceiveAll = ETH_ReceiveAll_Disable; ETH_InitStructure.ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Enable; ETH_InitStructure.ETH_PromiscuousMode = ETH_PromiscuousMode_Disable; ETH_InitStructure.ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_Perfect; ETH_InitStructure.ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect; #ifdef CHECKSUM_BY_HARDWARE ETH_InitStructure.ETH_ChecksumOffload = ETH_ChecksumOffload_Enable; #endif /*------------------------ DMA -----------------------------------------*/ /* When we use the Checksum offload feature, we need to enable the Store and Forward mode: the store and forward guarantee that a whole frame is stored in the FIFO, so the MAC can insert/verify the checksum, if the checksum is OK the DMA can handle the frame otherwise the frame is dropped */ ETH_InitStructure.ETH_DropTCPIPChecksumErrorFrame = ETH_DropTCPIPChecksumErrorFrame_Enable; ETH_InitStructure.ETH_ReceiveStoreForward = ETH_ReceiveStoreForward_Enable; ETH_InitStructure.ETH_TransmitStoreForward = ETH_TransmitStoreForward_Enable; ETH_InitStructure.ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Disable; ETH_InitStructure.ETH_ForwardUndersizedGoodFrames = ETH_ForwardUndersizedGoodFrames_Disable; ETH_InitStructure.ETH_SecondFrameOperate = ETH_SecondFrameOperate_Enable; ETH_InitStructure.ETH_AddressAlignedBeats = ETH_AddressAlignedBeats_Enable; ETH_InitStructure.ETH_FixedBurst = ETH_FixedBurst_Enable; ETH_InitStructure.ETH_RxDMABurstLength = ETH_RxDMABurstLength_32Beat; ETH_InitStructure.ETH_TxDMABurstLength = ETH_TxDMABurstLength_32Beat; ETH_InitStructure.ETH_DMAArbitration = ETH_DMAArbitration_RoundRobin_RxTx_2_1; /* Configure Ethernet */ enet_init_status = ETH_Init(Ð_InitStructure, 0x01); } /*! \brief configures the nested vectored interrupt controller \param[in] none \param[out] none \retval none */ static void nvic_configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = ETH_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } /*! \brief configures the different GPIO ports \param[in] none \param[out] none \retval none */ static void dm9161_gpio_config(void) { INT16U i = 0; GPIO_InitTypeDef GPIO_StructInit; GPIO_StructInit.GPIO_Mode = GPIO_Mode_OUT; // GPIO_StructInit.GPIO_OType = interface_info[index].GPIO_OType; GPIO_StructInit.GPIO_Speed = GPIO_High_Speed; GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_StructInit.GPIO_Pin = ETH_RESET_PIN; GPIO_Init(ETH_RESET_PORT, &GPIO_StructInit); ETH_RESET_ON; while (i < 1000) { i++; } i = 0; ETH_RESET_OFF; while (i < 1000) { i++; } i = 0; /* Enable SYSCFG clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); #ifdef MII_MODE #ifdef PHY_CLOCK_MCO /* output HXTAL clock (25MHz) on CKOUT0 pin(PA8) to clock the PHY */ RCC_MCO1Config(RCC_MCO1Source_HSE, RCC_MCO1Div_1); #endif /* PHY_CLOCK_MCO */ SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_MII); #elif defined RMII_MODE SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_RMII); #endif } static err_t bms_net_process(int fd, void *data, int len) { INT8U response[2] = {0xBB, 0xAA}; if ((len == 2) && (((INT8U *)data)[0] == 0xAA) && (((INT8U *)data)[1] == 0xBB)) { send(fd, (void *)response, sizeof(response), 0); } return 1; } void dm9161_task(void) { INT8U buf[50]; INT32S ret = 0; INT32S sockfd = -1, newfd = -1; INT32U len = 0; struct sockaddr_in svr_addr, clt_addr; svr_addr.sin_family = AF_INET; svr_addr.sin_port = htons(TCP_PORT); svr_addr.sin_addr.s_addr = htons(INADDR_ANY); while (1) { sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { continue; } ret = bind(sockfd, (struct sockaddr *)&svr_addr, sizeof(svr_addr)); if (ret < 0) { lwip_close(sockfd); sockfd = -1; continue; } ret = listen(sockfd, 1); if (ret < 0) { lwip_close(sockfd); continue; } len = sizeof(clt_addr); newfd = accept(sockfd, NULL, NULL); while (-1 != newfd) { ret = recv(newfd, buf, sizeof(buf) - 1, 0); if (ret <= 0) { lwip_close(newfd); newfd = -1; break; } bms_net_process(newfd, buf, ret); } lwip_close(sockfd); sockfd = -1; // OSTimeDly(10); } }