樊春春 2 anni fa
parent
commit
1163b4fc51

+ 66 - 88
MiddleWare/lwip-2.0.2/port/STM32F4xx/UCOS_II/ethernetif.c

@@ -274,77 +274,77 @@ static struct pbuf *low_level_input(struct netif *netif)
 
 // static struct pbuf *low_level_input(struct netif *netif)
 // {
-//   struct pbuf *p, *q;
-//   uint32_t len;
-//   FrameTypeDef frame;
-//   u8 *buffer;
-//   __IO ETH_DMADESCTypeDef *DMARxDesc;
-//   uint32_t bufferoffset = 0;
-//   uint32_t payloadoffset = 0;
-//   uint32_t byteslefttocopy = 0;
-//   uint32_t i = 0;
-
-//   /* get received frame */
-//   frame = ETH_Get_Received_Frame();
-
-//   /* Obtain the size of the packet and put it into the "len" variable. */
-//   len = frame.length;
-//   buffer = (u8 *)frame.buffer;
-
-//   /* We allocate a pbuf chain of pbufs from the Lwip buffer pool */
-//   p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
-
-//   if (p != NULL)
-//   {
-//     DMARxDesc = frame.descriptor;
-//     bufferoffset = 0;
-//     for (q = p; q != NULL; q = q->next)
+//     struct pbuf             *p, *q;
+//     uint32_t                 len;
+//     FrameTypeDef             frame;
+//     u8                      *buffer;
+//     __IO ETH_DMADESCTypeDef *DMARxDesc;
+//     uint32_t                 bufferoffset    = 0;
+//     uint32_t                 payloadoffset   = 0;
+//     uint32_t                 byteslefttocopy = 0;
+//     uint32_t                 i               = 0;
+
+//     /* get received frame */
+//     frame = ETH_Get_Received_Frame();
+
+//     /* Obtain the size of the packet and put it into the "len" variable. */
+//     len    = frame.length;
+//     buffer = (u8 *)frame.buffer;
+
+//     /* We allocate a pbuf chain of pbufs from the Lwip buffer pool */
+//     p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
+
+//     if (p != NULL)
 //     {
-//       byteslefttocopy = q->len;
-//       payloadoffset = 0;
+//         DMARxDesc    = frame.descriptor;
+//         bufferoffset = 0;
+//         for (q = p; q != NULL; q = q->next)
+//         {
+//             byteslefttocopy = q->len;
+//             payloadoffset   = 0;
+
+//             /* Check if the length of bytes to copy in current pbuf is bigger than Rx buffer size*/
+//             while ((byteslefttocopy + bufferoffset) > ETH_RX_BUF_SIZE)
+//             {
+//                 /* Copy data to pbuf*/
+//                 memcpy((u8_t *)((u8_t *)q->payload + payloadoffset), (u8_t *)((u8_t *)buffer + bufferoffset), (ETH_RX_BUF_SIZE - bufferoffset));
+
+//                 /* Point to next descriptor */
+//                 DMARxDesc = (ETH_DMADESCTypeDef *)(DMARxDesc->Buffer2NextDescAddr);
+//                 buffer    = (unsigned char *)(DMARxDesc->Buffer1Addr);
+
+//                 byteslefttocopy = byteslefttocopy - (ETH_RX_BUF_SIZE - bufferoffset);
+//                 payloadoffset   = payloadoffset + (ETH_RX_BUF_SIZE - bufferoffset);
+//                 bufferoffset    = 0;
+//             }
+//             /* Copy remaining data in pbuf */
+//             memcpy((u8_t *)((u8_t *)q->payload + payloadoffset), (u8_t *)((u8_t *)buffer + bufferoffset), byteslefttocopy);
+//             bufferoffset = bufferoffset + byteslefttocopy;
+//         }
+//     }
+
+//     /* Release descriptors to DMA */
+//     DMARxDesc = frame.descriptor;
 
-//       /* Check if the length of bytes to copy in current pbuf is bigger than Rx buffer size*/
-//       while ((byteslefttocopy + bufferoffset) > ETH_RX_BUF_SIZE)
-//       {
-//         /* Copy data to pbuf*/
-//         memcpy((u8_t *)((u8_t *)q->payload + payloadoffset), (u8_t *)((u8_t *)buffer + bufferoffset), (ETH_RX_BUF_SIZE - bufferoffset));
+//     /* Set Own bit in Rx descriptors: gives the buffers back to DMA */
+//     for (i = 0; i < DMA_RX_FRAME_infos->Seg_Count; i++)
+//     {
+//         DMARxDesc->Status = ETH_DMARxDesc_OWN;
+//         DMARxDesc         = (ETH_DMADESCTypeDef *)(DMARxDesc->Buffer2NextDescAddr);
+//     }
 
-//         /* Point to next descriptor */
-//         DMARxDesc = (ETH_DMADESCTypeDef *)(DMARxDesc->Buffer2NextDescAddr);
-//         buffer = (unsigned char *)(DMARxDesc->Buffer1Addr);
+//     /* Clear Segment_Count */
+//     DMA_RX_FRAME_infos->Seg_Count = 0;
 
-//         byteslefttocopy = byteslefttocopy - (ETH_RX_BUF_SIZE - bufferoffset);
-//         payloadoffset = payloadoffset + (ETH_RX_BUF_SIZE - bufferoffset);
-//         bufferoffset = 0;
-//       }
-//       /* Copy remaining data in pbuf */
-//       memcpy((u8_t *)((u8_t *)q->payload + payloadoffset), (u8_t *)((u8_t *)buffer + bufferoffset), byteslefttocopy);
-//       bufferoffset = bufferoffset + byteslefttocopy;
+//     /* When Rx Buffer unavailable flag is set: clear it and resume reception */
+//     if ((ETH->DMASR & ETH_DMASR_RBUS) != (u32)RESET)
+//     {
+//         /* Clear RBUS ETHERNET DMA flag */
+//         ETH->DMASR = ETH_DMASR_RBUS;
+//         /* Resume DMA reception */
+//         ETH->DMARPDR = 0;
 //     }
-//   }
-
-//   /* Release descriptors to DMA */
-//   DMARxDesc = frame.descriptor;
-
-//   /* Set Own bit in Rx descriptors: gives the buffers back to DMA */
-//   for (i = 0; i < DMA_RX_FRAME_infos->Seg_Count; i++)
-//   {
-//     DMARxDesc->Status = ETH_DMARxDesc_OWN;
-//     DMARxDesc = (ETH_DMADESCTypeDef *)(DMARxDesc->Buffer2NextDescAddr);
-//   }
-
-//   /* Clear Segment_Count */
-//   DMA_RX_FRAME_infos->Seg_Count = 0;
-
-//   /* When Rx Buffer unavailable flag is set: clear it and resume reception */
-//   if ((ETH->DMASR & ETH_DMASR_RBUS) != (u32)RESET)
-//   {
-//     /* Clear RBUS ETHERNET DMA flag */
-//     ETH->DMASR = ETH_DMASR_RBUS;
-//     /* Resume DMA reception */
-//     ETH->DMARPDR = 0;
-//   }
-//   return p;
+//     return p;
 // }
 
 /**
@@ -386,28 +386,6 @@ TRY_GET_NEXT_FRAME:
         }
     }
 }
-// err_t ethernetif_input(struct netif *netif)
-// {
-//   err_t err;
-//   struct pbuf *p;
-
-//   /* move received packet into a new pbuf */
-//   p = low_level_input(netif);
-
-//   /* no packet could be read, silently ignore this */
-//   if (p == NULL)
-//     return ERR_MEM;
-
-//   /* entry point to the LwIP stack */
-//   err = netif->input(p, netif);
-
-//   if (err != ERR_OK)
-//   {
-//     LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
-//     pbuf_free(p);
-//   }
-//   return err;
-// }
 
 /**
  * Should be called at the beginning of the program to set up the

+ 2 - 1
Project/GCC/scripts.py

@@ -16,6 +16,7 @@ env.AddPostAction(
 env.Append(
     LINKFLAGS=[
         "--specs=nano.specs",
-        "--specs=nosys.specs"
+        "--specs=nosys.specs",
+        "-Wl,-Map,$BUILD_DIR/${PROGNAME}.map"
     ],
 )

+ 2 - 2
User/Bsp/armfly_bsp.h

@@ -2,12 +2,12 @@
 #define _ARMFLY_H__
 
 #include "can.h"
-#include "dm9161.h"
 #include "fly_param.h"
 #include "interface.h"
 #include "netconf.h"
 #include "queue.h"
 #include "timer.h"
-#include "uart.h"
+#include "uart1.h"
+#include "uart3.h"
 
 #endif

+ 17 - 10
User/Bsp/can/can.c

@@ -1,3 +1,14 @@
+/**
+************************************************************************************************
+* @文件    : can.c
+* @作者    : 樊春春
+* @版本    : V1.0
+* @时间    : 2022/05/30 22:47:14
+* @邮箱    : [email protected]
+* @说明    : CAN初始化
+************************************************************************************************
+**/
+
 #include "can.h"
 #include "fly_param.h"
 #include <string.h>
@@ -99,20 +110,16 @@ void can_nvic_config(void)
     /* configure CAN0 NVIC */
     NVIC_InitTypeDef NVIC_InitStructure;
 
-    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
-
-    NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
-    ;
-    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
-    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0x0;
+    NVIC_InitStructure.NVIC_IRQChannel                   = CAN1_RX0_IRQn;
+    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
+    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;
     NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
     NVIC_Init(&NVIC_InitStructure);
 
     /* configure CAN1 NVIC */
-    NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
-    ;
-    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x1;
-    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0x1;
+    NVIC_InitStructure.NVIC_IRQChannel                   = CAN2_RX0_IRQn;
+    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
+    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
     NVIC_Init(&NVIC_InitStructure);
 }
 

+ 248 - 0
User/Bsp/eth/stm32f4x7_phy.c

@@ -0,0 +1,248 @@
+#include "stm32f4x7_phy.h"
+#include "interface.h"
+#include "lwip/api.h"
+#include "lwip/sockets.h"
+#include "netconf.h"
+#include "stm32f4x7_eth.h"
+
+ETH_InitTypeDef     ETH_InitStructure;
+__IO uint32_t       EthStatus = 0;
+extern struct netif gnetif;
+static void         dm9161_gpio_config(void);
+static void         dm9161_mac_dma_config(void);
+static void         nvic_configuration(void);
+
+/**
+ * @brief  Inserts a delay time.
+ * @param  nCount: number of 10ms periods to wait for.
+ * @retval None
+ */
+void ETH_Delay10ms(uint32_t nCount)
+{
+    // OSTimeDly(10 * nCount);
+}
+
+/*!
+    \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();
+
+    /* Get Ethernet link status*/
+    if (ETH_ReadPHYRegister(ETHERNET_PHY_ADDRESS, PHY_SR) & 1)
+    {
+        EthStatus |= ETH_LINK_FLAG;
+    }
+
+    /* Configure the PHY to generate an interrupt on change of link status */
+    Eth_Link_PHYITConfig(ETHERNET_PHY_ADDRESS);
+
+    ETH_DMAITConfig(ETH_DMA_IT_R, ENABLE);
+    ETH_DMAITConfig(ETH_DMA_IT_NIS, ENABLE);
+}
+
+/*!
+    \brief      configures the ethernet interface
+    \param[in]  none
+    \param[out] none
+    \retval     none
+*/
+static void dm9161_mac_dma_config(void)
+{
+
+    /* Reset ETHERNET on AHB Bus */
+    ETH_DeInit();
+    /* Software reset */
+    ETH_SoftwareReset();
+    /* Wait for software reset */
+    while (ETH_GetSoftwareResetStatus() == SET)
+        ;
+
+    /* ETHERNET Configuration ------------------------------*/
+    /* 缺省配置ETH_InitStructure */
+    ETH_StructInit(&ETH_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;
+    /* 关闭自动去除PDA/CRC功能  */
+    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
+    /* 开启ipv4和TCP/UDP/ICMP的帧校验和卸载   */
+    ETH_InitStructure.ETH_ChecksumOffload = ETH_ChecksumOffload_Enable;
+#endif
+
+    /*------------------------   DMA   -------------------------------*/
+    /*当我们使用帧校验和卸载功能的时候,一定要使能存储转发模式,存储
+    转发模式中要保证整个帧存储在FIFO中,  这样MAC能插入/识别出帧校验
+    值,当真校验正确的时候DMA就可以处理帧,否则就丢弃掉该帧*/
+    /* 开启丢弃TCP/IP错误帧 */
+    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;
+    /* 开启DMA传输的地址对齐功能 */
+    ETH_InitStructure.ETH_AddressAlignedBeats =
+        ETH_AddressAlignedBeats_Enable;
+    /* 开启固定突发功能 */
+    ETH_InitStructure.ETH_FixedBurst = ETH_FixedBurst_Enable;
+    /* DMA发送的最大突发长度为32个节拍 */
+    ETH_InitStructure.ETH_RxDMABurstLength = ETH_RxDMABurstLength_32Beat;
+    /*DMA接收的最大突发长度为32个节拍 */
+    ETH_InitStructure.ETH_TxDMABurstLength = ETH_TxDMABurstLength_32Beat;
+    ETH_InitStructure.ETH_DMAArbitration =
+        ETH_DMAArbitration_RoundRobin_RxTx_2_1;
+
+    /* 配置ETH */
+    EthStatus = ETH_Init(&ETH_InitStructure, ETHERNET_PHY_ADDRESS);
+}
+
+/*!
+    \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)
+{
+    /* 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
+}
+
+/**
+ * @brief  This function handles Ethernet link status.
+ * @param  None
+ * @retval None
+ */
+void Eth_Link_ITHandler(uint16_t PHYAddress)
+{
+    /* Check whether the link interrupt has occurred or not */
+    if (((ETH_ReadPHYRegister(PHYAddress, PHY_MISR)) & PHY_LINK_STATUS) != 0)
+    {
+        if ((ETH_ReadPHYRegister(PHYAddress, PHY_SR) & 1))
+        {
+            netif_set_link_up(&gnetif);
+        }
+        else
+        {
+            netif_set_link_down(&gnetif);
+        }
+    }
+}
+
+/**
+ * @brief  Configure the PHY to generate an interrupt on change of link status.
+ * @param PHYAddress: external PHY address
+ * @retval None
+ */
+uint32_t Eth_Link_PHYITConfig(uint16_t PHYAddress)
+{
+    uint16_t tmpreg = 0;
+
+    /* Read MICR register */
+    tmpreg = ETH_ReadPHYRegister(PHYAddress, PHY_MICR);
+
+    /* Enable output interrupt events to signal via the INT pin */
+    tmpreg |= (uint16_t)(PHY_MICR_INT_EN | PHY_MICR_INT_OE);
+    if (!(ETH_WritePHYRegister(PHYAddress, PHY_MICR, tmpreg)))
+    {
+        /* Return ERROR in case of write timeout */
+        return ETH_ERROR;
+    }
+
+    /* Read MISR register */
+    tmpreg = ETH_ReadPHYRegister(PHYAddress, PHY_MISR);
+
+    /* Enable Interrupt on change of link status */
+    tmpreg |= (uint16_t)PHY_MISR_LINK_INT_EN;
+    if (!(ETH_WritePHYRegister(PHYAddress, PHY_MISR, tmpreg)))
+    {
+        /* Return ERROR in case of write timeout */
+        return ETH_ERROR;
+    }
+    /* Return SUCCESS */
+    return ETH_SUCCESS;
+}
+
+/**
+ * @note   ETH_Get_Link_Status
+ * @brief  Get Ethernet link status
+ * @param  None
+ * @retval None
+ */
+uint8_t ETH_Get_Link_Status(void)
+{
+    return (ETH_ReadPHYRegister(ETHERNET_PHY_ADDRESS, PHY_BSR) & PHY_Linked_Status);
+}

+ 23 - 0
User/Bsp/eth/stm32f4x7_phy.h

@@ -0,0 +1,23 @@
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __STM32F4x7_PHY_H
+#define __STM32F4x7_PHY_H
+
+#include "includes.h"
+#include "lwip/netif.h"
+#include "netconf.h"
+
+/* Exported types ------------------------------------------------------------*/
+/* Exported constants --------------------------------------------------------*/
+#define ETHERNET_PHY_ADDRESS ((uint16_t)0x01)
+
+/* Ethernet Flags for EthStatus variable */
+#define ETH_INIT_FLAG 0x01
+#define ETH_LINK_FLAG 0x10
+void                 enet_system_setup(void);
+uint32_t             Eth_Link_PHYITConfig(uint16_t PHYAddress);
+void                 Eth_Link_EXTIConfig(void);
+void                 Eth_Link_ITHandler(uint16_t PHYAddress);
+void                 ETH_link_callback(struct netif *netif);
+extern __IO uint32_t EthStatus; /* 提供给主程序使用 */
+
+#endif /* __STM32F4x7_PHY_H */

+ 12 - 2
User/Bsp/interface/interface.c

@@ -1,3 +1,14 @@
+/**
+************************************************************************************************
+* @文件    : interface.c
+* @作者    : 樊春春
+* @版本    : V1.0
+* @时间    : 2022/05/31 20:47:46
+* @邮箱    : [email protected]
+* @说明    :
+************************************************************************************************
+**/
+
 #include "interface.h"
 #include "stm32f4xx_gpio.h"
 
@@ -55,8 +66,7 @@ Interface_struct interface_info[] = {
 
     // uart1
     {.type = kUart1, .GPIOx = GPIOA, .GPIO_Pin = GPIO_Pin_9, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOA, .GPIO_PinSource = GPIO_PinSource9, .GPIO_AF = GPIO_AF_USART1}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
-    {.type = kUart1, .GPIOx = GPIOA, .GPIO_Pin = GPIO_Pin_10, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOA, .GPIO_PinSource = GPIO_PinSource10, .GPIO_AF = GPIO_AF_USART1}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},
-    {.type = kUart1, .GPIOx = UART1_ENABLE_PORT, .GPIO_Pin = UART1_ENABLE_PIN, .GPIO_Mode = GPIO_Mode_OUT, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_NOPULL},
+    {.type = kUart1, .GPIOx = GPIOA, .GPIO_Pin = GPIO_Pin_10, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOA, .GPIO_PinSource = GPIO_PinSource10, .GPIO_AF = GPIO_AF_USART1}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz},
 
     // uart3
     {.type = kUart3, .GPIOx = GPIOB, .GPIO_Pin = GPIO_Pin_10, .GPIO_Mode = GPIO_Mode_AF, .AF_Info = {.GPIOx = GPIOB, .GPIO_PinSource = GPIO_PinSource10, .GPIO_AF = GPIO_AF_USART3}, .GPIO_OType = GPIO_OType_PP, .GPIO_Speed = GPIO_Speed_100MHz, .GPIO_PuPd = GPIO_PuPd_UP},

+ 1 - 25
User/Bsp/interface/interface.h

@@ -117,12 +117,8 @@ typedef struct
 // #define SI2C4_SDA_READ gpio_input_bit_get(SI2C4_SDA_PORT, SI2C4_SDA_PIN)
 
 // uart
-#define UART1_ENABLE_PORT GPIOA
-#define UART1_ENABLE_PIN  GPIO_Pin_15
 #define UART3_ENABLE_PORT GPIOB
 #define UART3_ENABLE_PIN  GPIO_Pin_2
-#define UART1_TX_ENABLE   GPIO_SetBits(UART1_ENABLE_PORT, UART1_ENABLE_PIN)
-#define UART1_RX_ENABLE   GPIO_ResetBits(UART1_ENABLE_PORT, UART1_ENABLE_PIN)
 #define UART3_TX_ENABLE   GPIO_SetBits(UART3_ENABLE_PORT, UART3_ENABLE_PIN)
 #define UART3_RX_ENABLE   GPIO_ResetBits(UART3_ENABLE_PORT, UART3_ENABLE_PIN)
 
@@ -132,27 +128,7 @@ typedef struct
 // #define SPI2_CS_HIGH GPIO_SetBits(SPI2_CS_PORT, SPI2_CS_PIN)
 // #define SPI2_CS_LOW GPIO_ResetBits(SPI2_CS_PORT, SPI2_CS_PIN)
 
-// // ethernet
-// //#define        ENET_REF_CLK                    GPIO_PIN_1
-// //#define        ENET_REF_CLK_Port           GPIOA
-// //#define        ENET_MDC                            GPIO_PIN_1
-// //#define        ENET_MDC_Port                   GPIOC
-// //#define        ENET_MDIO                           GPIO_PIN_2
-// //#define        ENET_MDIO_Port              GPIOA
-// //#define        ENET_CRSDV                      GPIO_PIN_7
-// //#define        ENET_CRSDV_Port             GPIOA
-// //#define        ENET_RXD0                           GPIO_PIN_4
-// //#define        ENET_RXD0_Port              GPIOC
-// //#define        ENET_RXD1                           GPIO_PIN_5
-// //#define        ENET_RXD1_Port              GPIOC
-// //#define        ENET_TXEN                           GPIO_PIN_11
-// //#define        ENET_TXEN_Port              GPIOG
-// //#define        ENET_RXER                           GPIO_PIN_12
-// //#define        ENET_RXER_Port              GPIOG
-// //#define        ENET_TXD0                           GPIO_PIN_13
-// //#define        ENET_TXD0_Port              GPIOG
-// //#define        ENET_TXD1                           GPIO_PIN_14
-// //#define        ENET_TXD1_Port              GPIOG
+// ethernet
 #define ETH_RESET_PORT GPIOH
 #define ETH_RESET_PIN  GPIO_Pin_6
 // #define ETH_RXER_PORT GPIOG

+ 0 - 406
User/Bsp/uart/uart.c

@@ -1,406 +0,0 @@
-#include "uart.h"
-#include "stdio.h"
-#include "uart.h"
-#include <string.h>
-
-static INT8U uart1_tx_buf[UART1_TX_LEN]  = {0};
-static INT8U uart1_rx_buf[UART1_REC_LEN] = {0};
-static INT8U uart3_tx_buf[UART3_TX_LEN]  = {0};
-static INT8U uart3_rx_buf[UART3_REC_LEN] = {0};
-
-UartFrame_TypeDef Uart1FrameStruct[MAX_MSG_NUM];
-UartFrame_TypeDef Uart3FrameStruct[MAX_MSG_NUM];
-
-extern OS_EVENT *uart1_mbox;
-extern OS_EVENT *uart3_mbox;
-
-void uart1_nvic_config(void)
-{
-    NVIC_InitTypeDef NVIC_InitStructure;
-    // Usart1 NVIC 配置
-    NVIC_InitStructure.NVIC_IRQChannel                   = USART1_IRQn; //串口1中断通道
-    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;           //抢占优先级
-    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;           //子优先级
-    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;      // IRQ通道使能
-    NVIC_Init(&NVIC_InitStructure);                                     //根据指定的参数初始化VIC寄存器
-}
-
-void uart1_dma_init(void)
-{
-    DMA_InitTypeDef DMA_InitStructure;
-    if ((u32)UART1_DMA_TXCH > (u32)DMA2) //得到当前stream是属于DMA2还是DMA1
-    {
-        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); // DMA2时钟使能
-    }
-    else
-    {
-        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); // DMA1时钟使能
-    }
-
-    DMA_DeInit(UART1_DMA_TXCH);
-    /* 配置 DMA Stream */
-    DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&(USART1->DR));        // DMA外设地址
-    DMA_InitStructure.DMA_BufferSize         = UART1_TX_LEN;                //数据传输量
-    DMA_InitStructure.DMA_PeripheralInc      = DMA_PeripheralInc_Disable;   //外设非增量模式
-    DMA_InitStructure.DMA_MemoryInc          = DMA_MemoryInc_Enable;        //存储器增量模式
-    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //外设数据长度:8位
-    DMA_InitStructure.DMA_MemoryDataSize     = DMA_MemoryDataSize_Byte;     //存储器数据长度:8位
-    DMA_InitStructure.DMA_Mode               = DMA_Mode_Normal;             // 使用普通模式
-    DMA_InitStructure.DMA_Priority           = DMA_Priority_Medium;         //中等优先级
-    DMA_InitStructure.DMA_FIFOMode           = DMA_FIFOMode_Disable;
-    DMA_InitStructure.DMA_FIFOThreshold      = DMA_FIFOThreshold_Full;
-    DMA_InitStructure.DMA_MemoryBurst        = DMA_MemoryBurst_Single;     //存储器突发单次传输
-    DMA_InitStructure.DMA_PeripheralBurst    = DMA_PeripheralBurst_Single; //外设突发单次传输
-    DMA_InitStructure.DMA_Channel            = UART1_DMA;                  //通道选择
-    DMA_InitStructure.DMA_DIR                = DMA_DIR_MemoryToPeripheral; //存储器到外设模式
-    DMA_InitStructure.DMA_Memory0BaseAddr    = (u32)uart1_tx_buf;          // DMA 存储器0地址
-    DMA_Init(UART1_DMA_TXCH, &DMA_InitStructure);                          //初始化DMA Stream
-
-    if ((u32)UART1_DMA_RXCH > (u32)DMA2) //得到当前stream是属于DMA2还是DMA1
-    {
-        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); // DMA2时钟使能
-    }
-    else
-    {
-        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); // DMA1时钟使能
-    }
-
-    DMA_DeInit(UART1_DMA_RXCH);
-    /* 配置 DMA Stream */
-    DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&(USART1->DR));        // DMA外设地址
-    DMA_InitStructure.DMA_BufferSize         = UART1_REC_LEN;               //数据传输量
-    DMA_InitStructure.DMA_PeripheralInc      = DMA_PeripheralInc_Disable;   //外设非增量模式
-    DMA_InitStructure.DMA_MemoryInc          = DMA_MemoryInc_Enable;        //存储器增量模式
-    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //外设数据长度:8位
-    DMA_InitStructure.DMA_MemoryDataSize     = DMA_MemoryDataSize_Byte;     //存储器数据长度:8位
-    DMA_InitStructure.DMA_Mode               = DMA_Mode_Circular;           // 使用循环模式
-    DMA_InitStructure.DMA_Priority           = DMA_Priority_Medium;         //中等优先级
-    DMA_InitStructure.DMA_FIFOMode           = DMA_FIFOMode_Disable;
-    DMA_InitStructure.DMA_FIFOThreshold      = DMA_FIFOThreshold_Full;
-    DMA_InitStructure.DMA_MemoryBurst        = DMA_MemoryBurst_Single;     //存储器突发单次传输
-    DMA_InitStructure.DMA_PeripheralBurst    = DMA_PeripheralBurst_Single; //外设突发单次传输
-    /* 配置 RX DMA */
-    DMA_InitStructure.DMA_Channel         = UART1_DMA;                  /* 配置接收通道 */
-    DMA_InitStructure.DMA_DIR             = DMA_DIR_PeripheralToMemory; /* 设置从外设到内存 */
-    DMA_InitStructure.DMA_Memory0BaseAddr = (u32)uart1_rx_buf;          /* 设置内存地址 */
-    DMA_Init(UART1_DMA_RXCH, &DMA_InitStructure);
-}
-
-void uart1_config(void)
-{
-    USART_InitTypeDef USART_InitStructure;
-    // USART1 初始化设置
-    USART_InitStructure.USART_BaudRate            = 9600;                           //波特率设置
-    USART_InitStructure.USART_WordLength          = USART_WordLength_8b;            //字长为8位数据格式
-    USART_InitStructure.USART_StopBits            = USART_StopBits_1;               //一个停止位
-    USART_InitStructure.USART_Parity              = USART_Parity_No;                //无奇偶校验位
-    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件数据流控制
-    USART_InitStructure.USART_Mode                = USART_Mode_Rx | USART_Mode_Tx;  //收发模式
-    USART_Init(USART1, &USART_InitStructure);                                       //初始化串口1
-
-    USART_Cmd(USART1, ENABLE); //使能串口1
-}
-
-void Uart1DMA_Enable(DMA_Stream_TypeDef *DMA_Streamx, u16 count)
-{
-    DMA_Cmd(DMA_Streamx, DISABLE); //关闭DMA传输
-    while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE)
-    {
-    } //确保DMA可以被设置
-
-    DMA_SetCurrDataCounter(DMA_Streamx, count); //数据传输量
-    DMA_Cmd(DMA_Streamx, ENABLE);               //开启DMA传输
-}
-
-void uart1_init(void)
-{
-    uart1_config();
-    uart1_dma_init();
-    uart1_nvic_config();
-    //    /* USART DMA enable*/
-    USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE); // 使能串口DMA接收数据
-    Uart1DMA_Enable(UART1_DMA_RXCH, UART3_REC_LEN);
-    /*configure DMA0 interrupt*/
-    USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); //开启空闲中断
-    USART_ITConfig(USART1, USART_IT_TC, ENABLE);
-
-    UART1_RX_ENABLE;
-}
-
-/****************************************************
- *  函 数 名:Uart1_Send_Data
- *  函数功能:串口1发送数据
- *  入口参数:buf 待发送数据  len 数据长度
- *  说    明:
- *****************************************************/
-void Uart1_Send_Data(const u8 *buf, u16 len)
-{
-    u8 i;
-
-    UART1_TX_ENABLE;          // 485 发送使能
-    for (i = 0; i < len; i++) //循环发送数据
-    {
-        while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
-            ;
-        USART_SendData(USART1, buf[i]);
-    }
-    while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
-        ;
-
-    UART1_RX_ENABLE; // 接收使能
-}
-
-void Uart1_dma_Send_Data(const INT8U *buf, INT16U len)
-{
-    //    INT16U cnt = 0;
-    if (RESET != DMA_GetFlagStatus(UART1_DMA_TXCH, DMA_FLAG_TCIF7))
-    {
-        DMA_ClearFlag(UART1_DMA_TXCH, DMA_FLAG_TCIF7);
-    }
-    UART1_TX_ENABLE;
-    if (len > UART1_TX_LEN)
-    {
-        len = UART1_TX_LEN;
-    }
-    memcpy(uart1_tx_buf, buf, len);
-
-    USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); // 使能DMA串口发送数据
-    Uart1DMA_Enable(UART1_DMA_TXCH, len);
-}
-
-void USART1_IRQHandler(void)
-{
-    static INT8U   u0_index = 0;
-    volatile INT8U clear    = 0;
-    INT8U          rec_cnt  = 0;
-    if (RESET != USART_GetITStatus(USART1, USART_IT_IDLE))
-    {
-        clear = USART1->SR;
-        clear = USART1->DR; // 先读SR, 再读DR, 就是为了消除IDLE中断
-
-        DMA_Cmd(UART1_DMA_RXCH, DISABLE);
-        DMA_ClearFlag(UART1_DMA_RXCH, DMA_FLAG_TCIF5);
-        rec_cnt = UART1_REC_LEN - DMA_GetCurrDataCounter(UART1_DMA_RXCH); // 获得接收帧帧长  特别注意: 帧长不是DMA_GetCurrDataCounter(DMA2_Stream5)
-
-        memcpy(Uart1FrameStruct[u0_index].buf, uart1_rx_buf, rec_cnt);
-        Uart1FrameStruct[u0_index].len = rec_cnt;
-        OSMboxPost(uart1_mbox, &Uart1FrameStruct[u0_index]);
-        if (u0_index < MAX_MSG_NUM - 1)
-        {
-            u0_index++;
-        }
-        else
-        {
-            u0_index = 0;
-        }
-        DMA_SetCurrDataCounter(UART1_DMA_RXCH, UART1_REC_LEN);
-        DMA_Cmd(UART1_DMA_RXCH, ENABLE);
-    }
-    else if (RESET != USART_GetITStatus(USART1, USART_IT_TC))
-    {
-        USART_ClearITPendingBit(USART1, USART_IT_TC);   // 清除发送完成标标志位
-        UART1_RX_ENABLE;                                // 485接收使能
-        Uart1DMA_Enable(UART1_DMA_RXCH, UART1_REC_LEN); // DMA接收使能
-        DMA_Cmd(UART1_DMA_TXCH, DISABLE);               // 关闭发送DMA
-        DMA_SetCurrDataCounter(UART1_DMA_TXCH, 0);      // 清除发送数据长度
-    }
-}
-
-void uart3_nvic_config(void)
-{
-    NVIC_InitTypeDef NVIC_InitStructure;
-    // Usart3 NVIC 配置
-    NVIC_InitStructure.NVIC_IRQChannel                   = USART3_IRQn; //串口3中断通道
-    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;           //抢占优先级
-    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;           //子优先级
-    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;      // IRQ通道使能
-    NVIC_Init(&NVIC_InitStructure);
-}
-
-void uart3_dma_init(void)
-{
-    DMA_InitTypeDef DMA_InitStructure;
-    if ((u32)UART3_DMA_TXCH > (u32)DMA2)
-    {
-        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); // DMA2时钟使能
-    }
-    else
-    {
-        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); // DMA1时钟使能
-    }
-    DMA_DeInit(UART3_DMA_TXCH);
-    while (DMA_GetCmdStatus(UART3_DMA_TXCH) != DISABLE)
-    {
-    } //等待DMA可配置
-    /* 配置 DMA Stream */
-    DMA_InitStructure.DMA_Channel            = UART3_DMA;                   //通道选择
-    DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART3->DR;            // DMA外设地址
-    DMA_InitStructure.DMA_Memory0BaseAddr    = (u32)uart3_tx_buf;           // DMA 存储器0地址
-    DMA_InitStructure.DMA_DIR                = DMA_DIR_MemoryToPeripheral;  //存储器到外设模式
-    DMA_InitStructure.DMA_BufferSize         = UART3_TX_LEN;                //数据传输量
-    DMA_InitStructure.DMA_PeripheralInc      = DMA_PeripheralInc_Disable;   //外设非增量模式
-    DMA_InitStructure.DMA_MemoryInc          = DMA_MemoryInc_Enable;        //存储器增量模式
-    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //外设数据长度:8位
-    DMA_InitStructure.DMA_MemoryDataSize     = DMA_MemoryDataSize_Byte;     //存储器数据长度:8位
-    DMA_InitStructure.DMA_Mode               = DMA_Mode_Normal;             // 使用普通模式
-    DMA_InitStructure.DMA_Priority           = DMA_Priority_Medium;         //中等优先级6
-    DMA_InitStructure.DMA_FIFOMode           = DMA_FIFOMode_Disable;
-    DMA_InitStructure.DMA_FIFOThreshold      = DMA_FIFOThreshold_Full;
-    DMA_InitStructure.DMA_MemoryBurst        = DMA_MemoryBurst_Single;     //存储器突发单次传输
-    DMA_InitStructure.DMA_PeripheralBurst    = DMA_PeripheralBurst_Single; //外设突发单次传输
-    DMA_Init(UART3_DMA_TXCH, &DMA_InitStructure);                          //初始化DMA Stream
-
-    if ((u32)UART3_DMA_RXCH > (u32)DMA2) //得到当前stream是属于DMA2还是DMA1
-    {
-        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); // DMA2时钟使能
-    }
-    else
-    {
-        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); // DMA1时钟使能
-    }
-
-    DMA_DeInit(UART3_DMA_RXCH);
-    while (DMA_GetCmdStatus(UART3_DMA_RXCH) != DISABLE)
-    {
-    } //等待DMA可配置
-    /* 配置 DMA Stream */
-    DMA_InitStructure.DMA_Channel            = UART3_DMA;                   //通道选择
-    DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART3->DR;            // DMA外设地址
-    DMA_InitStructure.DMA_Memory0BaseAddr    = (u32)uart3_rx_buf;           // DMA 存储器0地址
-    DMA_InitStructure.DMA_DIR                = DMA_DIR_PeripheralToMemory;  //外设到存储器模式
-    DMA_InitStructure.DMA_BufferSize         = UART3_REC_LEN;               //数据传输量
-    DMA_InitStructure.DMA_PeripheralInc      = DMA_PeripheralInc_Disable;   //外设非增量模式
-    DMA_InitStructure.DMA_MemoryInc          = DMA_MemoryInc_Enable;        //存储器增量模式
-    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //外设数据长度:8位
-    DMA_InitStructure.DMA_MemoryDataSize     = DMA_MemoryDataSize_Byte;     //存储器数据长度:8位
-    DMA_InitStructure.DMA_Mode               = DMA_Mode_Circular;           // 使用循环模式
-    DMA_InitStructure.DMA_Priority           = DMA_Priority_Medium;         //中等优先级
-    DMA_InitStructure.DMA_FIFOMode           = DMA_FIFOMode_Disable;
-    DMA_InitStructure.DMA_FIFOThreshold      = DMA_FIFOThreshold_Full;
-    DMA_InitStructure.DMA_MemoryBurst        = DMA_MemoryBurst_Single;     //存储器突发单次传输
-    DMA_InitStructure.DMA_PeripheralBurst    = DMA_PeripheralBurst_Single; //外设突发单次传输
-    DMA_Init(UART3_DMA_RXCH, &DMA_InitStructure);
-}
-
-void uart3_config(void)
-{
-    /* USART3 configure */
-    USART_InitTypeDef USART_InitStructure;
-    // USART3 初始化设置
-    USART_InitStructure.USART_BaudRate            = 9600;                           //波特率设置
-    USART_InitStructure.USART_WordLength          = USART_WordLength_8b;            //字长为8位数据格式
-    USART_InitStructure.USART_StopBits            = USART_StopBits_1;               //一个停止位
-    USART_InitStructure.USART_Parity              = USART_Parity_No;                //无奇偶校验位
-    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件数据流控制
-    USART_InitStructure.USART_Mode                = USART_Mode_Rx | USART_Mode_Tx;  //收发模式
-    USART_Init(USART3, &USART_InitStructure);                                       //初始化串口1
-
-    USART_Cmd(USART3, ENABLE); //使能串口3
-}
-
-void Uart3DMA_Enable(DMA_Stream_TypeDef *DMA_Streamx, u16 count)
-{
-    DMA_Cmd(DMA_Streamx, DISABLE); //关闭DMA传输
-    while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE)
-    {
-    } //确保DMA可以被设置
-
-    DMA_SetCurrDataCounter(DMA_Streamx, count); //数据传输量
-    DMA_Cmd(DMA_Streamx, ENABLE);               //开启DMA传输
-}
-
-void uart3_init(void)
-{
-    uart3_config();
-    uart3_dma_init();
-    uart3_nvic_config();
-    /* USART DMA enable*/
-    USART_DMACmd(USART3, USART_DMAReq_Rx, ENABLE); // 使能串口DMA接收数据
-    Uart3DMA_Enable(UART3_DMA_RXCH, UART3_REC_LEN);
-
-    /*configure DMA interrupt*/
-    USART_ITConfig(USART3, USART_IT_IDLE, ENABLE); //开启空闲中断
-    USART_ITConfig(USART3, USART_IT_TC, ENABLE);
-
-    UART3_RX_ENABLE;
-}
-
-/****************************************************
- *  函 数 名:Uart3_Send_Data
- *  函数功能:串口1发送数据
- *  入口参数:buf 待发送数据  len 数据长度
- *  说    明:
- *****************************************************/
-void Uart3_Send_Data(const INT8U *buf, INT16U len)
-{
-    INT8U i;
-
-    UART3_TX_ENABLE;          // 485 发送使能
-    for (i = 0; i < len; i++) //循环发送数据
-    {
-        while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)
-            ;
-        USART_SendData(USART3, buf[i]);
-    }
-    while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)
-        ;
-
-    UART3_RX_ENABLE; // 接收使能
-}
-
-void Uart3_dma_Send_Data(const INT8U *buf, INT16U len)
-{
-    if (DMA_GetFlagStatus(UART3_DMA_TXCH, DMA_FLAG_TCIF3) != RESET)
-    {
-        DMA_ClearFlag(UART3_DMA_TXCH, DMA_FLAG_TCIF3);
-    }
-
-    UART3_TX_ENABLE; // 485 发送使能
-    if (len > UART3_TX_LEN)
-    {
-        len = UART3_TX_LEN;
-    }
-    memcpy(uart3_tx_buf, buf, len);
-    USART_DMACmd(USART3, USART_DMAReq_Tx, len);
-
-    Uart3DMA_Enable(UART3_DMA_TXCH, len);
-}
-
-void USART3_IRQHandler(void)
-{
-    static INT8U   u3_index = 0;
-    volatile INT8U clear    = 0;
-    INT8U          rec_cnt  = 0;
-    if (RESET != USART_GetITStatus(USART3, USART_IT_IDLE))
-    {
-        clear = USART3->SR;
-        clear = USART3->DR; // 先读SR, 再读DR, 就是为了消除IDLE中断
-        DMA_Cmd(UART3_DMA_RXCH, DISABLE);
-        DMA_ClearFlag(UART3_DMA_RXCH, DMA_FLAG_TCIF1);
-        rec_cnt = UART3_REC_LEN - DMA_GetCurrDataCounter(UART3_DMA_RXCH);
-
-        memcpy(Uart3FrameStruct[u3_index].buf, uart3_rx_buf, rec_cnt);
-        Uart3FrameStruct[u3_index].len = rec_cnt;
-
-        OSMboxPost(uart3_mbox, &Uart3FrameStruct[u3_index]);
-
-        if (u3_index < MAX_MSG_NUM - 1)
-        {
-            u3_index++;
-        }
-        else
-        {
-            u3_index = 0;
-        }
-        DMA_SetCurrDataCounter(UART3_DMA_RXCH, UART3_REC_LEN);
-        DMA_Cmd(UART3_DMA_RXCH, ENABLE);
-    }
-    else if (RESET != USART_GetITStatus(USART3, USART_IT_TC))
-    {
-        USART_ClearITPendingBit(USART3, USART_IT_TC);
-        UART3_RX_ENABLE;
-        Uart3DMA_Enable(UART3_DMA_RXCH, UART3_REC_LEN); // DMA接收使能
-        DMA_Cmd(UART3_DMA_TXCH, DISABLE);               // 关闭发送DMA
-        DMA_SetCurrDataCounter(UART3_DMA_TXCH, 0);      // 清除发送数据长度
-    }
-}

+ 0 - 34
User/Bsp/uart/uart.h

@@ -1,34 +0,0 @@
-#ifndef __UART_H
-#define __UART_H
-
-#include "includes.h"
-#include "interface.h"
-#include "stm32f4xx_usart.h"
-#include "string.h"
-
-#define UART1_REC_LEN 128
-#define UART1_TX_LEN  128
-#define UART3_REC_LEN 128
-#define UART3_TX_LEN  128
-#define MAX_MSG_NUM   10
-
-#define UART1_DMA      DMA_Channel_4
-#define UART1_DMA_RXCH DMA2_Stream5
-#define UART1_DMA_TXCH DMA2_Stream7
-
-#define UART3_DMA      DMA_Channel_4
-#define UART3_DMA_RXCH DMA1_Stream1
-#define UART3_DMA_TXCH DMA1_Stream3
-
-typedef struct
-{
-    INT8U buf[128];
-    INT8U len;
-} UartFrame_TypeDef;
-
-void uart1_init(void);
-void uart3_init(void);
-void Uart1_dma_Send_Data(const INT8U *buf, INT16U len);
-void Uart3_dma_Send_Data(const INT8U *buf, INT16U len);
-
-#endif

+ 206 - 0
User/Bsp/uart/uart1.c

@@ -0,0 +1,206 @@
+/**
+************************************************************************************************
+* @文件    : uart1.c
+* @作者    : 樊春春
+* @版本    : V1.0
+* @时间    : 2022/05/31 20:20:39
+* @邮箱    : [email protected]
+* @说明    : uart1底层驱动,待修复
+************************************************************************************************
+**/
+#include "uart1.h"
+#include "queue.h"
+
+static INT8U uart1_tx_buf[UART1_TX_LEN]  = {0};
+static INT8U uart1_rx_buf[UART1_REC_LEN] = {0};
+
+UartFrame_TypeDef Uart1FrameStruct[UART1_MAX_MSG_NUM];
+
+extern OS_EVENT *uart1_mbox;
+
+void uart1_nvic_config(void)
+{
+    NVIC_InitTypeDef NVIC_InitStructure;
+    // Usart1 NVIC 配置
+    NVIC_InitStructure.NVIC_IRQChannel                   = USART1_IRQn; //串口1中断通道
+    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;           //抢占优先级
+    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;           //子优先级
+    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;      // IRQ通道使能
+    NVIC_Init(&NVIC_InitStructure);                                     //根据指定的参数初始化VIC寄存器
+}
+
+void uart1_dma_init(void)
+{
+    DMA_InitTypeDef DMA_InitStructure;
+    if ((u32)UART1_DMA_TXCH > (u32)DMA2) //得到当前stream是属于DMA2还是DMA1
+    {
+        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); // DMA2时钟使能
+    }
+    else
+    {
+        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); // DMA1时钟使能
+    }
+
+    DMA_DeInit(UART1_DMA_TXCH);
+    while (DMA_GetCmdStatus(UART1_DMA_TXCH) != DISABLE)
+    {
+    } //等待DMA可配置
+    /* 配置 DMA Stream */
+    DMA_InitStructure.DMA_Channel            = UART1_DMA;                   //通道选择
+    DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART1->DR;            // DMA外设地址
+    DMA_InitStructure.DMA_Memory0BaseAddr    = (u32)uart1_tx_buf;           // DMA 存储器0地址
+    DMA_InitStructure.DMA_DIR                = DMA_DIR_MemoryToPeripheral;  //存储器到外设模式
+    DMA_InitStructure.DMA_BufferSize         = UART1_TX_LEN;                //数据传输量
+    DMA_InitStructure.DMA_PeripheralInc      = DMA_PeripheralInc_Disable;   //外设非增量模式
+    DMA_InitStructure.DMA_MemoryInc          = DMA_MemoryInc_Enable;        //存储器增量模式
+    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //外设数据长度:8位
+    DMA_InitStructure.DMA_MemoryDataSize     = DMA_MemoryDataSize_Byte;     //存储器数据长度:8位
+    DMA_InitStructure.DMA_Mode               = DMA_Mode_Normal;             // 使用普通模式
+    DMA_InitStructure.DMA_Priority           = DMA_Priority_Medium;         //中等优先级6
+    DMA_InitStructure.DMA_FIFOMode           = DMA_FIFOMode_Disable;
+    DMA_InitStructure.DMA_FIFOThreshold      = DMA_FIFOThreshold_Full;
+    DMA_InitStructure.DMA_MemoryBurst        = DMA_MemoryBurst_Single;     //存储器突发单次传输
+    DMA_InitStructure.DMA_PeripheralBurst    = DMA_PeripheralBurst_Single; //外设突发单次传输      // DMA 存储器0地址
+    DMA_Init(UART1_DMA_TXCH, &DMA_InitStructure);                          //初始化DMA Stream
+
+    if ((u32)UART1_DMA_RXCH > (u32)DMA2) //得到当前stream是属于DMA2还是DMA1
+    {
+        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); // DMA2时钟使能
+    }
+    else
+    {
+        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); // DMA1时钟使能
+    }
+
+    DMA_DeInit(UART1_DMA_RXCH);
+    while (DMA_GetCmdStatus(UART1_DMA_RXCH) != DISABLE)
+    {
+    }
+
+    DMA_InitStructure.DMA_Channel            = UART1_DMA;                   //通道选择
+    DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART1->DR;            // DMA外设地址
+    DMA_InitStructure.DMA_Memory0BaseAddr    = (u32)uart1_rx_buf;           // DMA 存储器0地址
+    DMA_InitStructure.DMA_DIR                = DMA_DIR_PeripheralToMemory;  //外设到存储器模式
+    DMA_InitStructure.DMA_BufferSize         = UART1_REC_LEN;               //数据传输量
+    DMA_InitStructure.DMA_PeripheralInc      = DMA_PeripheralInc_Disable;   //外设非增量模式
+    DMA_InitStructure.DMA_MemoryInc          = DMA_MemoryInc_Enable;        //存储器增量模式
+    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //外设数据长度:8位
+    DMA_InitStructure.DMA_MemoryDataSize     = DMA_MemoryDataSize_Byte;     //存储器数据长度:8位
+    DMA_InitStructure.DMA_Mode               = DMA_Mode_Circular;           // 使用循环模式
+    DMA_InitStructure.DMA_Priority           = DMA_Priority_Medium;         //中等优先级
+    DMA_InitStructure.DMA_FIFOMode           = DMA_FIFOMode_Disable;
+    DMA_InitStructure.DMA_FIFOThreshold      = DMA_FIFOThreshold_Full;
+    DMA_InitStructure.DMA_MemoryBurst        = DMA_MemoryBurst_Single;     //存储器突发单次传输
+    DMA_InitStructure.DMA_PeripheralBurst    = DMA_PeripheralBurst_Single; //外设突发单次传输
+    DMA_Init(UART1_DMA_RXCH, &DMA_InitStructure);
+}
+
+void uart1_config(void)
+{
+    USART_InitTypeDef USART_InitStructure;
+    USART_InitStructure.USART_BaudRate            = 9600;                           //波特率设置
+    USART_InitStructure.USART_WordLength          = USART_WordLength_8b;            //字长为8位数据格式
+    USART_InitStructure.USART_StopBits            = USART_StopBits_1;               //一个停止位
+    USART_InitStructure.USART_Parity              = USART_Parity_No;                //无奇偶校验位
+    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件数据流控制
+    USART_InitStructure.USART_Mode                = USART_Mode_Rx | USART_Mode_Tx;  //收发模式
+    USART_Init(USART1, &USART_InitStructure);                                       //初始化串口1
+
+    USART_Cmd(USART1, ENABLE); //使能串口1
+}
+
+void Uart1DMA_Enable(DMA_Stream_TypeDef *DMA_Streamx, u16 count)
+{
+    DMA_Cmd(DMA_Streamx, DISABLE); //关闭DMA传输
+    while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE)
+    {
+    } //确保DMA可以被设置
+
+    DMA_SetCurrDataCounter(DMA_Streamx, count); //数据传输量
+    DMA_Cmd(DMA_Streamx, ENABLE);               //开启DMA传输
+}
+
+void uart1_init(void)
+{
+    uart1_config();
+    uart1_dma_init();
+    uart1_nvic_config();
+    /* USART DMA enable*/
+    USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE); // 使能串口DMA接收数据
+    Uart1DMA_Enable(UART1_DMA_RXCH, UART1_REC_LEN);
+    /*configure DMA interrupt*/
+    USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); //开启空闲中断
+    USART_ITConfig(USART1, USART_IT_TC, ENABLE);
+}
+
+/****************************************************
+ *  函 数 名:Uart1_Send_Data
+ *  函数功能:串口1发送数据
+ *  入口参数:buf 待发送数据  len 数据长度
+ *  说    明:
+ *****************************************************/
+void Uart1_Send_Data(const u8 *buf, u16 len)
+{
+    u8 i;
+    for (i = 0; i < len; i++) //循环发送数据
+    {
+        while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
+            ;
+        USART_SendData(USART1, buf[i]);
+    }
+    while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
+        ;
+}
+
+void uart1_dma_send(const INT8U *buf, INT16U len)
+{
+    if (RESET != DMA_GetFlagStatus(UART1_DMA_TXCH, DMA_FLAG_TCIF7))
+    {
+        DMA_ClearFlag(UART1_DMA_TXCH, DMA_FLAG_TCIF7);
+    }
+    if (len > UART1_TX_LEN)
+    {
+        len = UART1_TX_LEN;
+    }
+    memcpy(uart1_tx_buf, buf, len);
+    USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); // 使能DMA串口发送数据
+    Uart1DMA_Enable(UART1_DMA_TXCH, len);
+}
+
+void USART1_IRQHandler(void)
+{
+    static INT8U   u1_index = 0;
+    volatile INT8U clear    = 0;
+    INT8U          rec_cnt  = 0;
+    if (RESET != USART_GetITStatus(USART1, USART_IT_IDLE))
+    {
+        clear = USART1->SR;
+        clear = USART1->DR; // 先读SR, 再读DR, 就是为了消除IDLE中断
+
+        DMA_Cmd(UART1_DMA_RXCH, DISABLE);
+        DMA_ClearFlag(UART1_DMA_RXCH, DMA_FLAG_TCIF5);
+        rec_cnt = UART1_REC_LEN - DMA_GetCurrDataCounter(UART1_DMA_RXCH); // 获得接收帧帧长  特别注意: 帧长不是DMA_GetCurrDataCounter(DMA2_Stream5)
+
+        memcpy(Uart1FrameStruct[u1_index].buf, uart1_rx_buf, rec_cnt);
+        Uart1FrameStruct[u1_index].len = rec_cnt;
+
+        OSMboxPost(uart1_mbox, &Uart1FrameStruct[u1_index]);
+        if (u1_index < UART1_MAX_MSG_NUM - 1)
+        {
+            u1_index++;
+        }
+        else
+        {
+            u1_index = 0;
+        }
+        DMA_SetCurrDataCounter(UART1_DMA_RXCH, UART1_REC_LEN);
+        DMA_Cmd(UART1_DMA_RXCH, ENABLE);
+    }
+    else if (RESET != USART_GetITStatus(USART1, USART_IT_TC))
+    {
+        USART_ClearITPendingBit(USART1, USART_IT_TC);   // 清除发送完成标标志位
+        Uart1DMA_Enable(UART1_DMA_RXCH, UART1_REC_LEN); // DMA接收使能
+        DMA_Cmd(UART1_DMA_TXCH, DISABLE);               // 关闭发送DMA
+        DMA_SetCurrDataCounter(UART1_DMA_TXCH, 0);      // 清除发送数据长度
+    }
+}

+ 20 - 0
User/Bsp/uart/uart1.h

@@ -0,0 +1,20 @@
+#ifndef __UART1_H
+#define __UART1_H
+
+#include "includes.h"
+#include "interface.h"
+#include "stm32f4xx_usart.h"
+#include "string.h"
+
+#define UART1_REC_LEN     128
+#define UART1_TX_LEN      128
+#define UART1_MAX_MSG_NUM 10
+
+#define UART1_DMA      DMA_Channel_4
+#define UART1_DMA_RXCH DMA2_Stream5
+#define UART1_DMA_TXCH DMA2_Stream7
+
+void uart1_init(void);
+void uart1_dma_send(const INT8U *buf, INT16U len);
+
+#endif // __UART_H

+ 214 - 0
User/Bsp/uart/uart3.c

@@ -0,0 +1,214 @@
+/**
+************************************************************************************************
+* @文件    : uart3.c
+* @作者    : 樊春春
+* @版本    : V1.0
+* @时间    : 2022/05/31 20:15:29
+* @邮箱    : [email protected]
+* @说明    : 485底层驱动
+************************************************************************************************
+**/
+
+#include "uart3.h"
+
+static INT8U uart3_tx_buf[UART3_TX_LEN]  = {0};
+static INT8U uart3_rx_buf[UART3_REC_LEN] = {0};
+
+UartFrame_TypeDef Uart3FrameStruct[UART3_MAX_MSG_NUM];
+
+extern OS_EVENT *uart3_mbox;
+
+void uart3_nvic_config(void)
+{
+    NVIC_InitTypeDef NVIC_InitStructure;
+    // Usart3 NVIC 配置
+    NVIC_InitStructure.NVIC_IRQChannel                   = USART3_IRQn; //串口3中断通道
+    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;           //抢占优先级
+    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;           //子优先级
+    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;      // IRQ通道使能
+    NVIC_Init(&NVIC_InitStructure);
+}
+
+void uart3_dma_init(void)
+{
+    DMA_InitTypeDef DMA_InitStructure;
+    if ((u32)UART3_DMA_TXCH > (u32)DMA2)
+    {
+        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); // DMA2时钟使能
+    }
+    else
+    {
+        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); // DMA1时钟使能
+    }
+    DMA_DeInit(UART3_DMA_TXCH);
+    while (DMA_GetCmdStatus(UART3_DMA_TXCH) != DISABLE)
+    {
+    } //等待DMA可配置
+    /* 配置 DMA Stream */
+    DMA_InitStructure.DMA_Channel            = UART3_DMA;                   //通道选择
+    DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART3->DR;            // DMA外设地址
+    DMA_InitStructure.DMA_Memory0BaseAddr    = (u32)uart3_tx_buf;           // DMA 存储器0地址
+    DMA_InitStructure.DMA_DIR                = DMA_DIR_MemoryToPeripheral;  //存储器到外设模式
+    DMA_InitStructure.DMA_BufferSize         = UART3_TX_LEN;                //数据传输量
+    DMA_InitStructure.DMA_PeripheralInc      = DMA_PeripheralInc_Disable;   //外设非增量模式
+    DMA_InitStructure.DMA_MemoryInc          = DMA_MemoryInc_Enable;        //存储器增量模式
+    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //外设数据长度:8位
+    DMA_InitStructure.DMA_MemoryDataSize     = DMA_MemoryDataSize_Byte;     //存储器数据长度:8位
+    DMA_InitStructure.DMA_Mode               = DMA_Mode_Normal;             // 使用普通模式
+    DMA_InitStructure.DMA_Priority           = DMA_Priority_Medium;         //中等优先级6
+    DMA_InitStructure.DMA_FIFOMode           = DMA_FIFOMode_Disable;
+    DMA_InitStructure.DMA_FIFOThreshold      = DMA_FIFOThreshold_Full;
+    DMA_InitStructure.DMA_MemoryBurst        = DMA_MemoryBurst_Single;     //存储器突发单次传输
+    DMA_InitStructure.DMA_PeripheralBurst    = DMA_PeripheralBurst_Single; //外设突发单次传输
+    DMA_Init(UART3_DMA_TXCH, &DMA_InitStructure);                          //初始化DMA Stream
+
+    if ((u32)UART3_DMA_RXCH > (u32)DMA2) //得到当前stream是属于DMA2还是DMA1
+    {
+        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); // DMA2时钟使能
+    }
+    else
+    {
+        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); // DMA1时钟使能
+    }
+
+    DMA_DeInit(UART3_DMA_RXCH);
+    while (DMA_GetCmdStatus(UART3_DMA_RXCH) != DISABLE)
+    {
+    } //等待DMA可配置
+    /* 配置 DMA Stream */
+    DMA_InitStructure.DMA_Channel            = UART3_DMA;                   //通道选择
+    DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART3->DR;            // DMA外设地址
+    DMA_InitStructure.DMA_Memory0BaseAddr    = (u32)uart3_rx_buf;           // DMA 存储器0地址
+    DMA_InitStructure.DMA_DIR                = DMA_DIR_PeripheralToMemory;  //外设到存储器模式
+    DMA_InitStructure.DMA_BufferSize         = UART3_REC_LEN;               //数据传输量
+    DMA_InitStructure.DMA_PeripheralInc      = DMA_PeripheralInc_Disable;   //外设非增量模式
+    DMA_InitStructure.DMA_MemoryInc          = DMA_MemoryInc_Enable;        //存储器增量模式
+    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //外设数据长度:8位
+    DMA_InitStructure.DMA_MemoryDataSize     = DMA_MemoryDataSize_Byte;     //存储器数据长度:8位
+    DMA_InitStructure.DMA_Mode               = DMA_Mode_Circular;           // 使用循环模式
+    DMA_InitStructure.DMA_Priority           = DMA_Priority_Medium;         //中等优先级
+    DMA_InitStructure.DMA_FIFOMode           = DMA_FIFOMode_Disable;
+    DMA_InitStructure.DMA_FIFOThreshold      = DMA_FIFOThreshold_Full;
+    DMA_InitStructure.DMA_MemoryBurst        = DMA_MemoryBurst_Single;     //存储器突发单次传输
+    DMA_InitStructure.DMA_PeripheralBurst    = DMA_PeripheralBurst_Single; //外设突发单次传输
+    DMA_Init(UART3_DMA_RXCH, &DMA_InitStructure);
+}
+
+void uart3_config(void)
+{
+    USART_InitTypeDef USART_InitStructure;
+    USART_InitStructure.USART_BaudRate            = 9600;                           //波特率设置
+    USART_InitStructure.USART_WordLength          = USART_WordLength_8b;            //字长为8位数据格式
+    USART_InitStructure.USART_StopBits            = USART_StopBits_1;               //一个停止位
+    USART_InitStructure.USART_Parity              = USART_Parity_No;                //无奇偶校验位
+    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件数据流控制
+    USART_InitStructure.USART_Mode                = USART_Mode_Rx | USART_Mode_Tx;  //收发模式
+    USART_Init(USART3, &USART_InitStructure);                                       //初始化串口1
+
+    USART_Cmd(USART3, ENABLE); //使能串口3
+}
+
+void Uart3DMA_Enable(DMA_Stream_TypeDef *DMA_Streamx, u16 count)
+{
+    DMA_Cmd(DMA_Streamx, DISABLE); //关闭DMA传输
+    while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE)
+    {
+    } //确保DMA可以被设置
+
+    DMA_SetCurrDataCounter(DMA_Streamx, count); //数据传输量
+    DMA_Cmd(DMA_Streamx, ENABLE);               //开启DMA传输
+}
+
+void uart3_init(void)
+{
+    uart3_config();
+    uart3_dma_init();
+    uart3_nvic_config();
+    /* USART DMA enable*/
+    USART_DMACmd(USART3, USART_DMAReq_Rx, ENABLE); // 使能串口DMA接收数据
+    Uart3DMA_Enable(UART3_DMA_RXCH, UART3_REC_LEN);
+
+    /*configure DMA interrupt*/
+    USART_ITConfig(USART3, USART_IT_IDLE, ENABLE); //开启空闲中断
+    USART_ITConfig(USART3, USART_IT_TC, ENABLE);
+
+    UART3_RX_ENABLE;
+}
+
+/****************************************************
+ *  函 数 名:Uart3_Send_Data
+ *  函数功能:串口3发送数据
+ *  入口参数:buf 待发送数据  len 数据长度
+ *  说    明:
+ *****************************************************/
+void Uart3_Send_Data(const INT8U *buf, INT16U len)
+{
+    INT8U i;
+
+    UART3_TX_ENABLE;          // 485 发送使能
+    for (i = 0; i < len; i++) //循环发送数据
+    {
+        while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)
+            ;
+        USART_SendData(USART3, buf[i]);
+    }
+    while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)
+        ;
+
+    UART3_RX_ENABLE; // 接收使能
+}
+
+void uart3_dma_send(const INT8U *buf, INT16U len)
+{
+    if (DMA_GetFlagStatus(UART3_DMA_TXCH, DMA_FLAG_TCIF3) != RESET)
+    {
+        DMA_ClearFlag(UART3_DMA_TXCH, DMA_FLAG_TCIF3);
+    }
+
+    UART3_TX_ENABLE; // 485 发送使能
+    if (len > UART3_TX_LEN)
+    {
+        len = UART3_TX_LEN;
+    }
+    memcpy(uart3_tx_buf, buf, len);
+    USART_DMACmd(USART3, USART_DMAReq_Tx, len);
+    Uart3DMA_Enable(UART3_DMA_TXCH, len);
+}
+
+void USART3_IRQHandler(void)
+{
+    static INT8U   u3_index = 0;
+    volatile INT8U clear    = 0;
+    INT8U          rec_cnt  = 0;
+    if (RESET != USART_GetITStatus(USART3, USART_IT_IDLE))
+    {
+        clear = USART3->SR;
+        clear = USART3->DR; // 先读SR, 再读DR, 就是为了消除IDLE中断
+        DMA_Cmd(UART3_DMA_RXCH, DISABLE);
+        DMA_ClearFlag(UART3_DMA_RXCH, DMA_FLAG_TCIF1);
+        rec_cnt = UART3_REC_LEN - DMA_GetCurrDataCounter(UART3_DMA_RXCH);
+
+        memcpy(Uart3FrameStruct[u3_index].buf, uart3_rx_buf, rec_cnt);
+        Uart3FrameStruct[u3_index].len = rec_cnt;
+
+        OSMboxPost(uart3_mbox, &Uart3FrameStruct[u3_index]);
+        if (u3_index < UART3_MAX_MSG_NUM - 1)
+        {
+            u3_index++;
+        }
+        else
+        {
+            u3_index = 0;
+        }
+        DMA_SetCurrDataCounter(UART3_DMA_RXCH, UART3_REC_LEN);
+        DMA_Cmd(UART3_DMA_RXCH, ENABLE);
+    }
+    else if (RESET != USART_GetITStatus(USART3, USART_IT_TC))
+    {
+        USART_ClearITPendingBit(USART3, USART_IT_TC);
+        UART3_RX_ENABLE;
+        Uart3DMA_Enable(UART3_DMA_RXCH, UART3_REC_LEN); // DMA接收使能
+        DMA_Cmd(UART3_DMA_TXCH, DISABLE);               // 关闭发送DMA
+        DMA_SetCurrDataCounter(UART3_DMA_TXCH, 0);      // 清除发送数据长度
+    }
+}

+ 21 - 0
User/Bsp/uart/uart3.h

@@ -0,0 +1,21 @@
+#ifndef __UART3_H
+#define __UART3_H
+
+#include "includes.h"
+#include "interface.h"
+#include "queue.h"
+#include "stm32f4xx_usart.h"
+#include "string.h"
+
+#define UART3_REC_LEN     128
+#define UART3_TX_LEN      128
+#define UART3_MAX_MSG_NUM 10
+
+#define UART3_DMA      DMA_Channel_4
+#define UART3_DMA_RXCH DMA1_Stream1
+#define UART3_DMA_TXCH DMA1_Stream3
+
+void uart3_init(void);
+void uart3_dma_send(const INT8U *buf, INT16U len);
+
+#endif // __UART3_H

+ 0 - 805
User/app/dm9000/dm9000.c

@@ -1,805 +0,0 @@
-/*
-*********************************************************************************************************
-*
-*	模块名称 : DM9000AEP 底层驱动模块(For STM32F4XX, uip)
-*	文件名称 : dm9000_uip.c
-*	版    本 : V1.1
-*	说    明 : 这是硬件底层驱动程序的主文件。每个c文件可以 #include "bsp.h" 来包含所有的外设驱动模块。
-*			   bsp = Borad surport packet 板级支持包
-*	修改记录 :
-*		版本号  日期        作者     说明
-*		V1.0    2013-03-01  armfly   正式发布
-*		V1.1    2013-06-20  armfly   规范注释,添加必要说明
-*
-*	Copyright (C), 2013-2014, 安富莱电子 www.armfly.com
-*
-*********************************************************************************************************
-*/
-
-#include "bsp.h"
-// #include "uip.h"
-#include "dm9000.h"
-
-//#define DM9000A_FLOW_CONTROL
-//#define DM9000A_UPTO_100M
-//#define Fifo_Point_Check
-//#define Point_Error_Reset
-#define Fix_Note_Address
-//#define FifoPointCheck
-
-/* DM9000A 接收函数设置宏 */
-//#define Rx_Int_enable
-#define Max_Int_Count      1
-#define Max_Ethernet_Lenth 1536
-#define Broadcast_Jump
-#define Max_Broadcast_Lenth 500
-
-/* DM9000A 传送函数设置宏 */
-#define Max_Send_Pack 2
-
-#define NET_BASE_ADDR 0x68400000
-#define NET_REG_ADDR  (*((volatile uint16_t *)NET_BASE_ADDR))
-#define NET_REG_DATA  (*((volatile uint16_t *)(NET_BASE_ADDR + 0x00080000)))
-
-#define ETH_ADDR_LEN 6
-
-/* 定义网卡的 MAC 地址 */
-static unsigned char DEF_MAC_ADDR[ETH_ADDR_LEN] = {0x00, 0x60, 0x6e, 0x90, 0x00, 0xae};
-uint8_t              SendPackOk                 = 0;
-
-uint8_t s_FSMC_Init_Ok = 0; /* 用于指示FSMC是否初始化 */
-
-//#define printk(...)
-#define printk printf
-
-void dm9000_debug_test(void);
-
-static void dm9000_CtrlLinesConfig(void);
-static void dm9000_FSMCConfig(void);
-static void dm9000_initnic(void);
-
-/*
-*********************************************************************************************************
-*	函 数 名: dm9000_init
-*	功能说明: uIP 接口函数,初始化网卡.  uIP 接口函数.
-*	形    参: 无
-*	返 回 值: 无
-*********************************************************************************************************
-*/
-void dm9000_init(void)
-{
-    dm9000_CtrlLinesConfig();
-    dm9000_FSMCConfig();
-
-    s_FSMC_Init_Ok = 1;
-
-    dm9000_initnic(); /* 配置DM9000 */
-}
-
-/*
-*********************************************************************************************************
-*	函 数 名: dm9000_CtrlLinesConfig
-*	功能说明: 配置DM9000AE控制口线,FSMC管脚设置为复用功能 (For SMT32F4)
-*	形    参: 无
-*	返 回 值: 无
-*********************************************************************************************************
-*/
-static void dm9000_CtrlLinesConfig(void)
-{
-    GPIO_InitTypeDef GPIO_InitStructure;
-
-    /* 使能FSMC时钟 */
-    RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE);
-
-    /* 使能 GPIO时钟 */
-    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOG, ENABLE);
-
-    /* 设置 PD.00(D2), PD.01(D3), PD.04(NOE), PD.05(NWE), PD.08(D13), PD.09(D14),
-     PD.10(D15), PD.14(D0), PD.15(D1) 为复用推挽输出 */
-
-    GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_FSMC);
-    GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_FSMC);
-    GPIO_PinAFConfig(GPIOD, GPIO_PinSource4, GPIO_AF_FSMC);
-    GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_FSMC);
-    GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_FSMC);
-    GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_FSMC);
-    GPIO_PinAFConfig(GPIOD, GPIO_PinSource10, GPIO_AF_FSMC);
-    GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_FSMC);
-    GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_FSMC);
-
-    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 |
-                                  GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_14 |
-                                  GPIO_Pin_15;
-    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
-    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
-    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
-    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
-    GPIO_Init(GPIOD, &GPIO_InitStructure);
-
-    /* 设置 PE.07(D4), PE.08(D5), PE.09(D6), PE.10(D7), PE.11(D8), PE.12(D9), PE.13(D10),
-     PE.14(D11), PE.15(D12) 为复用推挽输出 */
-
-    GPIO_PinAFConfig(GPIOE, GPIO_PinSource4, GPIO_AF_FSMC);
-    GPIO_PinAFConfig(GPIOE, GPIO_PinSource5, GPIO_AF_FSMC);
-
-    GPIO_PinAFConfig(GPIOE, GPIO_PinSource7, GPIO_AF_FSMC);
-    GPIO_PinAFConfig(GPIOE, GPIO_PinSource8, GPIO_AF_FSMC);
-    GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_FSMC);
-    GPIO_PinAFConfig(GPIOE, GPIO_PinSource10, GPIO_AF_FSMC);
-    GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_FSMC);
-    GPIO_PinAFConfig(GPIOE, GPIO_PinSource12, GPIO_AF_FSMC);
-    GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_FSMC);
-    GPIO_PinAFConfig(GPIOE, GPIO_PinSource14, GPIO_AF_FSMC);
-    GPIO_PinAFConfig(GPIOE, GPIO_PinSource15, GPIO_AF_FSMC);
-
-    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 |
-                                  GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 |
-                                  GPIO_Pin_15;
-    GPIO_Init(GPIOE, &GPIO_InitStructure);
-
-    /* 设置 PD.13(A18 (RS))  为复用推挽输出 */
-    GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_FSMC);
-
-    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
-    GPIO_Init(GPIOD, &GPIO_InitStructure);
-
-    /* 设置 PG10 (CS)) 为复用推挽输出 */
-    GPIO_PinAFConfig(GPIOG, GPIO_PinSource10, GPIO_AF_FSMC);
-
-    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
-    GPIO_Init(GPIOG, &GPIO_InitStructure);
-
-    /* PA15 是DM9000_INT中断输入口(本程序未使用) */
-}
-
-/*
-*********************************************************************************************************
-*	函 数 名: dm9000_FSMCConfig
-*	功能说明: 配置FSMC并口访问时序
-*	形    参: 无
-*	返 回 值: 无
-*********************************************************************************************************
-*/
-static void dm9000_FSMCConfig(void)
-{
-    FSMC_NORSRAMInitTypeDef       FSMC_NORSRAMInitStructure;
-    FSMC_NORSRAMTimingInitTypeDef p;
-
-    /*-- FSMC Configuration ------------------------------------------------------*/
-    /*----------------------- SRAM Bank 3 ----------------------------------------*/
-    /*-- FSMC Configuration ------------------------------------------------------*/
-    p.FSMC_AddressSetupTime      = 6; /* 设置为2会出错; 3正常 */
-    p.FSMC_AddressHoldTime       = 2;
-    p.FSMC_DataSetupTime         = 4; /* 设置为1出错,2正常 */
-    p.FSMC_BusTurnAroundDuration = 1;
-    p.FSMC_CLKDivision           = 0;
-    p.FSMC_DataLatency           = 0;
-    p.FSMC_AccessMode            = FSMC_AccessMode_A;
-
-    FSMC_NORSRAMInitStructure.FSMC_Bank                  = FSMC_Bank1_NORSRAM3;
-    FSMC_NORSRAMInitStructure.FSMC_DataAddressMux        = FSMC_DataAddressMux_Disable;
-    FSMC_NORSRAMInitStructure.FSMC_MemoryType            = FSMC_MemoryType_SRAM; // FSMC_MemoryType_PSRAM;
-    FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth       = FSMC_MemoryDataWidth_16b;
-    FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode       = FSMC_BurstAccessMode_Disable;
-    FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait      = FSMC_AsynchronousWait_Disable;
-    FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity    = FSMC_WaitSignalPolarity_Low;
-    FSMC_NORSRAMInitStructure.FSMC_WrapMode              = FSMC_WrapMode_Disable;
-    FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive      = FSMC_WaitSignalActive_BeforeWaitState;
-    FSMC_NORSRAMInitStructure.FSMC_WriteOperation        = FSMC_WriteOperation_Enable;
-    FSMC_NORSRAMInitStructure.FSMC_WaitSignal            = FSMC_WaitSignal_Disable;
-    FSMC_NORSRAMInitStructure.FSMC_ExtendedMode          = FSMC_ExtendedMode_Disable;
-    FSMC_NORSRAMInitStructure.FSMC_WriteBurst            = FSMC_WriteBurst_Disable;
-    FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
-    FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct     = &p;
-
-    FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
-
-    /*!< Enable FSMC Bank1_SRAM3 Bank */
-    FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM3, ENABLE);
-}
-
-/*
-*********************************************************************************************************
-*	函 数 名: dm9000_ReadReg
-*	功能说明: 读出DM9000指定寄存器的值
-*	形    参: reg 寄存器地址
-*	返 回 值: 寄存器值
-*********************************************************************************************************
-*/
-uint8_t dm9000_ReadReg(uint8_t reg)
-{
-    NET_REG_ADDR = reg;
-    return (NET_REG_DATA);
-}
-
-/*
-*********************************************************************************************************
-*	函 数 名: dm9000_WriteReg
-*	功能说明: 读出DM9000指定寄存器的值
-*	形    参: reg :寄存器地址
-*			 writedata : 写入的数据
-*	返 回 值: 无
-*********************************************************************************************************
-*/
-void dm9000_WriteReg(uint8_t reg, uint8_t writedata)
-{
-    NET_REG_ADDR = reg;
-    NET_REG_DATA = writedata;
-}
-
-/*
-*********************************************************************************************************
-*	函 数 名: dm9000_hash_table
-*	功能说明: 设置 DM9000A MAC 、 广播 、 多播 寄存器
-*	形    参: 无
-*	返 回 值: 无
-*********************************************************************************************************
-*/
-void dm9000_hash_table(void)
-{
-    uint8_t i;
-
-    /* 将MAC地址告诉uip */
-    for (i = 0; i < 6; i++)
-    {
-        uip_ethaddr.addr[i] = DEF_MAC_ADDR[i];
-    }
-
-    /* 设置 网卡 MAC 位置,来自於 MyHardware */
-    for (i = 0; i < 6; i++)
-    {
-        dm9000_WriteReg(DM9000_REG_PAR + i, DEF_MAC_ADDR[i]);
-    }
-
-    /* 清除 网卡多播设置 */
-    for (i = 0; i < 8; i++)
-    {
-        dm9000_WriteReg(DM9000_REG_MAR + i, 0x00);
-    }
-
-    /* 设置 广播包 设置 */
-    dm9000_WriteReg(DM9000_REG_MAR + 7, 0x80);
-}
-
-/*
-*********************************************************************************************************
-*	函 数 名: dm9000_reset
-*	功能说明: 软件复位DM9000AE
-*	形    参: 无
-*	返 回 值: 无
-*********************************************************************************************************
-*/
-void dm9000_reset(void)
-{
-    dm9000_WriteReg(DM9000_REG_NCR, DM9000_REG_RESET); /* 对 DM9000A 进行软件重置 */
-    bsp_DelayUS(10);                                   /* delay 10us */
-    dm9000_WriteReg(DM9000_REG_NCR, DM9000_REG_RESET); /* 对 DM9000A 进行软件重置 */
-    bsp_DelayUS(10);                                   /* delay 10us */
-
-    /* 基本记存器相关设置 */
-    dm9000_WriteReg(DM9000_REG_IMR, DM9000_IMR_OFF);   /* 开启内存自环模式 */
-    dm9000_WriteReg(DM9000_REG_TCR2, DM9000_TCR2_SET); /* 设置 LED 显示模式1:全双工亮,半双工灭 */
-
-    /* 清除多余资讯 */
-    dm9000_WriteReg(DM9000_REG_NSR, 0x2c);
-    dm9000_WriteReg(DM9000_REG_TCR, 0x00);
-    dm9000_WriteReg(DM9000_REG_ISR, 0x3f);
-
-#ifdef DM9000A_FLOW_CONTROL
-    dm9000_WriteReg(DM9000_REG_BPTR, DM9000_BPTR_SET); /* 半双工流控设置 */
-    dm9000_WriteReg(DM9000_REG_FCTR, DM9000_FCTR_SET); /* 全双工流控设置 */
-    dm9000_WriteReg(DM9000_REG_FCR, DM9000_FCR_SET);   /* 开启流控设置 */
-#endif
-
-#ifdef DM9000A_UPTO_100M
-    /* DM9000A无此寄存器 */
-    dm9000_WriteReg(DM9000_REG_OTCR, DM9000_OTCR_SET); /* 工作频率到 100Mhz 设置 */
-#endif
-
-#ifdef Rx_Int_enable
-    dm9000_WriteReg(DM9000_REG_IMR, DM9000_IMR_SET); /* 开启 中断模式 */
-#else
-    dm9000_WriteReg(DM9000_REG_IMR, DM9000_IMR_OFF); /* 关闭 中断模式 */
-#endif
-
-    dm9000_WriteReg(DM9000_REG_RCR, DM9000_RCR_SET); /* 开启 接收工能 */
-
-    SendPackOk = 0;
-}
-
-/*
-*********************************************************************************************************
-*	函 数 名: dm9000_phy_write
-*	功能说明: 软件复位DM9000AE
-*	形    参: phy_reg : PHY寄存器地址
-*			  writedata : 写入的数据
-*	返 回 值: 无
-*********************************************************************************************************
-*/
-void dm9000_phy_write(uint8_t phy_reg, uint16_t writedata)
-{
-    /* 设置写入 PHY 寄存器的位置 */
-    dm9000_WriteReg(DM9000_REG_EPAR, phy_reg | DM9000_PHY);
-
-    /* 设置写入 PHY 寄存器的值 */
-    dm9000_WriteReg(DM9000_REG_EPDRH, (writedata >> 8) & 0xff);
-    dm9000_WriteReg(DM9000_REG_EPDRL, writedata & 0xff);
-
-    dm9000_WriteReg(DM9000_REG_EPCR, 0x0a); /* 将资料写入 PHY 寄存器 */
-    while (dm9000_ReadReg(DM9000_REG_EPCR) & 0x01)
-        ;                                   /* 查寻是否执行结束 */
-    dm9000_WriteReg(DM9000_REG_EPCR, 0x08); /* 清除写入命令 */
-}
-
-/*
-*********************************************************************************************************
-*	函 数 名: dm9000_initnic
-*	功能说明: 配置DM9000AE芯片(初始化)
-*	形    参: 无
-*	返 回 值: 无
-*********************************************************************************************************
-*/
-static void dm9000_initnic(void)
-{
-    dm9000_WriteReg(DM9000_REG_NCR, DM9000_REG_RESET); /* 对 DM9000A 进行软件重置 */
-    bsp_DelayUS(10);                                   /* delay 10us */
-
-    dm9000_hash_table(); /* 设置 DM9000A MAC 及 多播*/
-
-    dm9000_reset(); /* 进行 DM9000A 软件设置 */
-
-    dm9000_WriteReg(DM9000_REG_GPR, DM9000_PHY_OFF); /* 关闭 PHY ,进行 PHY 设置*/
-    dm9000_phy_write(0x00, 0x8000);                  /* 重置 PHY 的寄存器 */
-#ifdef DM9000A_FLOW_CONTROL
-    dm9000_phy_write(0x04, 0x01e1 | 0x0400); /* 设置 自适应模式相容表 */
-#else
-    dm9000_phy_write(0x04, 0x01e1);                  /* 设置 自适应模式相容表 */
-#endif
-    // dm9000_phy_write(0x00, 0x1000);					/* 设置 基本连接模式 */
-    /* 连接模式设置
-      0x0000 : 固定10M半双工
-      0x0100 : 固定10M全双工
-      0x2000 : 固定100M半双工
-      0x2100 : 固定100M全双工
-      0x1000 : 自适应模式
-    */
-    dm9000_phy_write(0x00, 0x1000); /* 设置 基本连接模式 */
-
-    dm9000_WriteReg(DM9000_REG_GPR, DM9000_PHY_ON); /* 结束 PHY 设置, 开启 PHY */
-
-    // dm9000_debug_test();
-}
-
-/*
-*********************************************************************************************************
-*	函 数 名: dm9000_receive_packet
-*	功能说明: 配置DM9000AE芯片(初始化)
-*	形    参: _uip_buf : 接收结果存放的缓冲区指针
-*	返 回 值: > 0 表示接收的数据长度, 0表示没有数据
-*********************************************************************************************************
-*/
-uint16_t dm9000_receive_packet(uint8_t *_uip_buf)
-{
-    uint16_t  ReceiveLength;
-    uint16_t *ReceiveData;
-    uint8_t   rx_int_count = 0;
-    uint8_t   rx_checkbyte;
-    uint16_t  rx_status, rx_length;
-    uint8_t   jump_packet;
-    uint16_t  i;
-    uint16_t  calc_len;
-    uint16_t  calc_MRR;
-
-    do
-    {
-        ReceiveLength = 0; /* 清除接收的长度 */
-        ReceiveData   = (uint16_t *)_uip_buf;
-        jump_packet   = 0;                 /* 清除跳包动作 */
-        dm9000_ReadReg(DM9000_REG_MRCMDX); /* 读取内存数据,地址不增加 */
-        /* 计算内存数据位置 */
-        calc_MRR     = (dm9000_ReadReg(DM9000_REG_MRRH) << 8) + dm9000_ReadReg(DM9000_REG_MRRL);
-        rx_checkbyte = dm9000_ReadReg(DM9000_REG_MRCMDX); /*  */
-
-        if (rx_checkbyte == DM9000_PKT_RDY) /* 取 */
-        {
-            /* 读取封包相关资讯 及 长度 */
-            NET_REG_ADDR = DM9000_REG_MRCMD;
-            rx_status    = NET_REG_DATA;
-            rx_length    = NET_REG_DATA;
-
-            /* 若收到超过系统可承受的封包,此包跳过 */
-            if (rx_length > Max_Ethernet_Lenth)
-                jump_packet = 1;
-
-#ifdef Broadcast_Jump
-            /* 若收到的广播或多播包超过特定长度,此包跳过 */
-            if (rx_status & 0x4000)
-            {
-                if (rx_length > Max_Broadcast_Lenth)
-                    jump_packet = 1;
-            }
-#endif
-            /* 计算下一个包的指针位 , 若接收长度为奇数,需加一对齐偶字节。*/
-            /* 若是超过 0x3fff ,则需回归绕到 0x0c00 起始位置 */
-            calc_MRR += (rx_length + 4);
-            if (rx_length & 0x01)
-                calc_MRR++;
-            if (calc_MRR > 0x3fff)
-                calc_MRR -= 0x3400;
-
-            if (jump_packet == 0x01)
-            {
-                /* 将指针移到下一个包的包头位置 */
-                dm9000_WriteReg(DM9000_REG_MRRH, (calc_MRR >> 8) & 0xff);
-                dm9000_WriteReg(DM9000_REG_MRRL, calc_MRR & 0xff);
-                continue;
-            }
-
-            /* 开始将内存的资料搬到到系统中,每次移动一个 word */
-            calc_len = (rx_length + 1) >> 1;
-            for (i = 0; i < calc_len; i++)
-                ReceiveData[i] = NET_REG_DATA;
-
-            /* 将包长回报给 TCP/IP 上层,并减去最後 4 BYTE 的 CRC-32 检核码 */
-            ReceiveLength = rx_length - 4;
-
-            rx_int_count++; /* 累计收包次数 */
-
-#ifdef FifoPointCheck
-            if (calc_MRR != ((dm9000_ReadReg(DM9000_REG_MRRH) << 8) + dm9000_ReadReg(DM9000_REG_MRRL)))
-            {
-#ifdef Point_Error_Reset
-                dm9000_reset(); /* 若是指针出错,重置 */
-                return ReceiveLength;
-#endif
-                /*若是指针出错,将指针移到下一个包的包头位置  */
-                dm9000_WriteReg(DM9000_REG_MRRH, (calc_MRR >> 8) & 0xff);
-                dm9000_WriteReg(DM9000_REG_MRRL, calc_MRR & 0xff);
-            }
-#endif
-            return ReceiveLength;
-        }
-        else
-        {
-            if (rx_checkbyte == DM9000_PKT_NORDY) /* 未收到包 */
-            {
-                dm9000_WriteReg(DM9000_REG_ISR, 0x3f); /*  */
-            }
-            else
-            {
-                dm9000_reset(); /* 接收指针出错,重置 */
-            }
-            return (0);
-        }
-    } while (rx_int_count < Max_Int_Count); /* 是否超过最多接收封包计数 */
-    return 0;
-}
-
-/*
-*********************************************************************************************************
-*	函 数 名: dm9000_send_packet
-*	功能说明: 发送一包数据
-*	形    参: p_char : 发送数据缓冲区
-*	返 回 值: length : 数据长度
-*********************************************************************************************************
-*/
-void dm9000_send_packet(uint8_t *p_char, uint16_t length)
-{
-    uint16_t      SendLength = length;
-    uint16_t     *SendData   = (uint16_t *)p_char;
-    uint16_t      i;
-    uint16_t      calc_len;
-    __IO uint16_t calc_MWR;
-
-    /* 检查 DM9000A 是否还在传送中!若是等待直到传送结束 */
-    if (SendPackOk == Max_Send_Pack)
-    {
-        while (dm9000_ReadReg(DM9000_REG_TCR) & DM9000_TCR_SET)
-        {
-            bsp_DelayUS(5);
-        }
-        SendPackOk = 0;
-    }
-
-    SendPackOk++; /* 设置传送计数 */
-
-#ifdef FifoPointCheck
-    /* 计算下一个传送的指针位 , 若接收长度为奇数,需加一对齐偶字节。*/
-    /* 若是超过 0x0bff ,则需回归绕到 0x0000 起始位置 */
-    calc_MWR = (dm9000_ReadReg(DM9000_REG_MWRH) << 8) + dm9000_ReadReg(DM9000_REG_MWRL);
-    calc_MWR += SendLength;
-    if (SendLength & 0x01)
-        calc_MWR++;
-    if (calc_MWR > 0x0bff)
-        calc_MWR -= 0x0c00;
-#endif
-
-    dm9000_WriteReg(DM9000_REG_TXPLH, (SendLength >> 8) & 0xff); /* 设置传送封包的长度 */
-    dm9000_WriteReg(DM9000_REG_TXPLL, SendLength & 0xff);
-
-    /* 开始将系统的资料搬到到内存中,每次移动一个 word */
-    NET_REG_ADDR = DM9000_REG_MWCMD;
-    calc_len     = (SendLength + 1) >> 1;
-    for (i = 0; i < calc_len; i++)
-        NET_REG_DATA = SendData[i];
-
-    dm9000_WriteReg(DM9000_REG_TCR, DM9000_TCR_SET); /* 进行传送 */
-
-#ifdef FifoPointCheck
-    if (calc_MWR != ((dm9000_ReadReg(DM9000_REG_MWRH) << 8) + dm9000_ReadReg(DM9000_REG_MWRL)))
-    {
-#ifdef Point_Error_Reset
-        /* 若是指针出错,等待此一封包送完 , 之後进行重置 */
-        while (dm9000_ReadReg(DM9000_REG_TCR) & DM9000_TCR_SET)
-            bsp_DelayUS(5);
-        dm9000_reset();
-        return;
-#endif
-        /*若是指针出错,将指针移到下一个传送包的包头位置  */
-        dm9000_WriteReg(DM9000_REG_MWRH, (calc_MWR >> 8) & 0xff);
-        dm9000_WriteReg(DM9000_REG_MWRL, calc_MWR & 0xff);
-    }
-#endif
-    return;
-}
-
-/*
-*********************************************************************************************************
-*	函 数 名: dm9000_interrupt
-*	功能说明: 中断处理函数 (webserver例程未使用中断)
-*	形    参: 无
-*	返 回 值: 无
-*********************************************************************************************************
-*/
-void dm9000_interrupt(void)
-{
-    uint8_t  save_reg;
-    uint16_t isr_status;
-
-    save_reg = NET_REG_ADDR; /* 暂存所使用的位置 */
-
-    dm9000_WriteReg(DM9000_REG_IMR, DM9000_IMR_OFF); /* 关闭 DM9000A 中断 */
-    isr_status = dm9000_ReadReg(DM9000_REG_ISR);     /* 取得中断产生值 */
-
-    if (isr_status & DM9000_RX_INTR)
-    { /* 检查是否为接收中断 */
-        // dm9000_receive_packet();							/* 执行接收处理程序 */
-    }
-
-    dm9000_WriteReg(DM9000_REG_IMR, DM9000_IMR_SET); /* 开启 DM9000A 中断 */
-    NET_REG_ADDR = save_reg;                         /* 回复所使用的位置 */
-}
-
-/*******************************************************************************
- *	函数名: dm9000_ReadID
- *	参  数: 无
- *	返  回: 无
- *	功  能: 读取芯片ID
- */
-uint32_t dm9000_ReadID(void)
-{
-    uint8_t vid1, vid2, pid1, pid2;
-
-    if (s_FSMC_Init_Ok == 0)
-    {
-        dm9000_CtrlLinesConfig();
-        dm9000_FSMCConfig();
-
-        s_FSMC_Init_Ok = 1;
-    }
-    vid1 = dm9000_ReadReg(DM9000_REG_VID_L) & 0xFF;
-    vid2 = dm9000_ReadReg(DM9000_REG_VID_H) & 0xFF;
-    pid1 = dm9000_ReadReg(DM9000_REG_PID_L) & 0xFF;
-    pid2 = dm9000_ReadReg(DM9000_REG_PID_H) & 0xFF;
-
-    return (vid2 << 24) | (vid1 << 16) | (pid2 << 8) | pid1;
-}
-
-#if 0
-	/*
-	*********************************************************************************************************
-	*	函 数 名: dm9000_debug_test
-	*	功能说明: 测试DM9000AE的函数,用于排错
-	*	形    参: 无
-	*	返 回 值: 无
-	*********************************************************************************************************
-	*/
-	void dm9000_debug_test(void)
-	{
-		uint32_t check_device;
-		uint8_t  check_iomode;
-		uint8_t  check_reg_fail = 0;
-		uint8_t  check_fifo_fail = 0;
-		uint16_t i;
-		uint16_t j;
-
-		dm9000_WriteReg(DM9000_REG_NCR, DM9000_REG_RESET);			/* 对 DM9000A 进行软件重置 */
-		bsp_DelayUS(10);								/* delay 10us */
-		dm9000_WriteReg(DM9000_REG_NCR, DM9000_REG_RESET);			/* 对 DM9000A 进行软件重置 */
-		bsp_DelayUS(10);								/* delay 10us */
-
-		check_device  = dm9000_ReadReg(DM9000_REG_VID_L);
-		check_device |= dm9000_ReadReg(DM9000_REG_VID_H) << 8;
-		check_device |= dm9000_ReadReg(DM9000_REG_PID_L) << 16;
-		check_device |= dm9000_ReadReg(DM9000_REG_PID_H) << 24;
-
-		if(check_device != 0x90000A46)
-		{
-			printk("dm9000_DEBUG ==> DEIVCE NOT FOUND, SYSTEM HOLD !!\n");
-			while(1);
-		}
-		else
-		{
-			printk("dm9000_DEBUG ==> DEIVCE FOUND !!\n");
-		}
-
-		check_iomode = dm9000_ReadReg(DM9000_REG_ISR) >> 6;
-		if(check_iomode != DM9000_WORD_MODE)
-		{
-			printk("dm9000_DEBUG ==> DEIVCE NOT WORD MODE, SYSTEM HOLD !!\n");
-			while(1);
-		}
-		else
-		{
-			printk("dm9000_DEBUG ==> DEIVCE IS WORD MODE !!\n");
-		}
-
-		printk("dm9000_DEBUG ==> REGISTER R/W TEST !!\n");
-		NET_REG_ADDR = DM9000_REG_MAR;
-		for(i = 0; i < 0x0100; i++)
-		{
-			NET_REG_DATA = i;
-			if(i != (NET_REG_DATA & 0xff))
-			{
-				printk("             > error W %02x , R %02x \n", i , NET_REG_DATA);
-				check_reg_fail = 1;
-			}
-		}
-
-		if(check_reg_fail)
-		{
-			printk("dm9000_DEBUG ==> REGISTER R/W FAIL, SYSTEM HOLD !!\n");
-			while(1);
-		}
-
-		printk("dm9000_DEBUG ==> FIFO R/W TEST !!\n");
-		printk("dm9000_DEBUG ==> FIFO WRITE START POINT 0x%02x%02x \n",
-				dm9000_ReadReg(DM9000_REG_MWRH), dm9000_ReadReg(DM9000_REG_MWRL));
-
-		NET_REG_ADDR = DM9000_REG_MWCMD;
-		for(i = 0; i < 0x1000; i++)
-			NET_REG_DATA = ((i & 0xff) * 0x0101);
-
-		printk("dm9000_DEBUG ==> FIFO WRITE END POINT 0x%02x%02x \n",
-				dm9000_ReadReg(DM9000_REG_MWRH), dm9000_ReadReg(DM9000_REG_MWRL));
-
-		if((dm9000_ReadReg(DM9000_REG_MWRH) != 0x20) || (dm9000_ReadReg(DM9000_REG_MWRL) != 0x00))
-		{
-			printk("dm9000_DEBUG ==> FIFO WRITE FAIL, SYSTEM HOLD !!\n");
-			while(1);
-		}
-
-		dm9000_ReadReg(DM9000_REG_MRCMDX);
-		printk("dm9000_DEBUG ==> FIFO READ START POINT 0x%02x%02x \n",
-				dm9000_ReadReg(DM9000_REG_MRRH), dm9000_ReadReg(DM9000_REG_MRRL));
-		dm9000_ReadReg(DM9000_REG_MRCMDX);
-
-		NET_REG_ADDR = DM9000_REG_MRCMD;
-		for(i = 0; i < 0x1000; i++)
-		{
-			j = NET_REG_DATA;
-
-			if(((i & 0xff) * 0x0101) != j)
-			{
-				//printk("             > error W %04x , R %04x \n",
-				//		((i & 0xff) * 0x0101) , j);
-				check_fifo_fail = 1;
-			}
-		}
-
-		printk("dm9000_DEBUG ==> FIFO READ END POINT 0x%02x%02x \n",
-				dm9000_ReadReg(DM9000_REG_MRRH), dm9000_ReadReg(DM9000_REG_MRRL));
-
-		if((dm9000_ReadReg(DM9000_REG_MRRH) != 0x20) || (dm9000_ReadReg(DM9000_REG_MRRL) != 0x00))
-		{
-			printk("dm9000_DEBUG ==> FIFO WRITE FAIL, SYSTEM HOLD !!\n");
-			while(1);
-		}
-
-			if(check_fifo_fail)
-		{
-			printk("dm9000_DEBUG ==> FIFO R/W DATA FAIL, SYSTEM HOLD !!\n");
-			while(1);
-		}
-
-		printk("dm9000_DEBUG ==> PACKET SEND & INT TEST !! \n");
-		dm9000_WriteReg(DM9000_REG_NCR, DM9000_REG_RESET);
-		bsp_DelayUS(10);
-		dm9000_WriteReg(DM9000_REG_NCR, DM9000_REG_RESET);
-		bsp_DelayUS(10);
-
-		dm9000_WriteReg(DM9000_REG_IMR, DM9000_IMR_OFF | DM9000_TX_INTR);
-
-		dm9000_WriteReg(DM9000_REG_TXPLH, 0x01);
-		dm9000_WriteReg(DM9000_REG_TXPLL, 0x00);
-
-		do
-		{
-			dm9000_WriteReg(DM9000_REG_ISR, DM9000_TX_INTR);
-			printk("dm9000_DEBUG ==> INT PIN IS OFF\n");
-
-			NET_REG_ADDR = DM9000_REG_MWCMD;
-			for(i = 0; i < (0x0100 / 2); i++)
-			{
-				if(i < 3)
-					NET_REG_DATA = 0xffff;
-				else
-					NET_REG_DATA = i * 0x0101;
-			}
-
-			printk("dm9000_DEBUG ==> PACKET IS SEND \n");
-			dm9000_WriteReg(DM9000_REG_TCR, DM9000_TCR_SET);
-
-			while(dm9000_ReadReg(DM9000_REG_TCR) & DM9000_TCR_SET) bsp_DelayUS (5);
-			if(dm9000_ReadReg(DM9000_REG_ISR) & DM9000_TX_INTR)
-				printk("dm9000_DEBUG ==> INT PIN IS ACTIVE \n");
-			else
-				printk("dm9000_DEBUG ==> INT PIN IS NOT ACTIVE \n");
-
-			for(i = 0; i < 10; i++)
-				bsp_DelayUS(1000);
-
-		}while(1);
-	}
-
-	/*
-	*********************************************************************************************************
-	*	函 数 名: etherdev_chkmedia
-	*	功能说明: 检测网络连接状态
-	*	形    参: 无
-	*	返 回 值: 无
-	*********************************************************************************************************
-	*/
-	void etherdev_chkmedia(void)
-	{
-	//	uint8_t status;
-
-		while(!(dm9000_ReadReg(DM9000_REG_NSR) & DM9000_PHY))
-		{
-			bsp_DelayUS(2000);
-		}
-	}
-
-
-	/*******************************************************************************
-	*	函数名: etherdev_poll
-	*	参  数: 无
-	*	返  回: 无
-	*	功  能: uIP 接口函数, 采用查询方式接收一个IP包
-	*/
-	/*
-	                              etherdev_poll()
-
-	    This function will read an entire IP packet into the uip_buf.
-	    If it must wait for more than 0.5 seconds, it will return with
-	    the return value 0. Otherwise, when a full packet has been read
-	    into the uip_buf buffer, the length of the packet is returned.
-	*/
-	uint16_t etherdev_poll(void)
-	{
-		uint16_t bytes_read = 0;
-#if 0
-
-		/* tick_count threshold should be 12 for 0.5 sec bail-out
-			One second (24) worked better for me, but socket recycling
-			is then slower. I set UIP_TIME_WAIT_TIMEOUT 60 in uipopt.h
-			to counter this. Retransmission timing etc. is affected also. */
-		while ((!(bytes_read = etherdev_read())) && (timer0_tick() < 12)) continue;
-
-		timer0_reset();
-
-#endif
-		return bytes_read;
-	}
-
-#endif

+ 0 - 243
User/app/dm9161/dm9161.c

@@ -1,243 +0,0 @@
-/*!
-    \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(&ETH_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(&ETH_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);
-    }
-}

+ 0 - 15
User/app/dm9161/dm9161.h

@@ -1,15 +0,0 @@
-#ifndef __DM_9161_H
-#define __DM_9161_H
-#include "fly_param.h"
-#include "includes.h"
-#include "interface.h"
-#include "lwip/netif.h"
-#include "netconf.h"
-
-#define TCP_PORT 8080
-/* Exported constants --------------------------------------------------------*/
-#define DP83848_PHY_ADDRESS ((uint16_t)0x01)
-
-void enet_system_setup(void);
-void dm9161_task(void);
-#endif

+ 804 - 0
User/app/dm9k/dm9k.c

@@ -0,0 +1,804 @@
+/*
+*********************************************************************************************************
+*
+*	模块名称 : DM9000AEP 底层驱动模块(For STM32F4XX, uip)
+*	文件名称 : dm9k_uip.c
+*	版    本 : V1.1
+*	说    明 : 这是硬件底层驱动程序的主文件。每个c文件可以 #include "bsp.h" 来包含所有的外设驱动模块。
+*			   bsp = Borad surport packet 板级支持包
+*	修改记录 :
+*		版本号  日期        作者     说明
+*		V1.0    2013-03-01  armfly   正式发布
+*		V1.1    2013-06-20  armfly   规范注释,添加必要说明
+*
+*	Copyright (C), 2013-2014, 安富莱电子 www.armfly.com
+*
+*********************************************************************************************************
+*/
+
+#include "dm9k.h"
+#include "bsp.h"
+
+//#define DM9000A_FLOW_CONTROL
+//#define DM9000A_UPTO_100M
+//#define Fifo_Point_Check
+//#define Point_Error_Reset
+#define Fix_Note_Address
+//#define FifoPointCheck
+
+/* DM9000A 接收函数设置宏 */
+//#define Rx_Int_enable
+#define Max_Int_Count      1
+#define Max_Ethernet_Lenth 1536
+#define Broadcast_Jump
+#define Max_Broadcast_Lenth 500
+
+/* DM9000A 传送函数设置宏 */
+#define Max_Send_Pack 2
+
+#define NET_BASE_ADDR 0x68400000
+#define NET_REG_ADDR  (*((volatile uint16_t *)NET_BASE_ADDR))
+#define NET_REG_DATA  (*((volatile uint16_t *)(NET_BASE_ADDR + 0x00080000)))
+
+#define ETH_ADDR_LEN 6
+
+/* 定义网卡的 MAC 地址 */
+static unsigned char DEF_MAC_ADDR[ETH_ADDR_LEN] = {0x00, 0x60, 0x6e, 0x90, 0x00, 0xae};
+uint8_t              SendPackOk                 = 0;
+
+uint8_t s_FSMC_Init_Ok = 0; /* 用于指示FSMC是否初始化 */
+
+//#define printk(...)
+#define printk printf
+
+void dm9k_debug_test(void);
+
+static void DM9K_CtrlLinesConfig(void);
+static void DM9K_FSMCConfig(void);
+static void dm9k_initnic(void);
+
+/*
+*********************************************************************************************************
+*	函 数 名: dm9k_init
+*	功能说明: uIP 接口函数,初始化网卡.  uIP 接口函数.
+*	形    参: 无
+*	返 回 值: 无
+*********************************************************************************************************
+*/
+void dm9k_init(void)
+{
+    DM9K_CtrlLinesConfig();
+    DM9K_FSMCConfig();
+
+    s_FSMC_Init_Ok = 1;
+
+    dm9k_initnic(); /* 配置DM9000 */
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: DM9K_CtrlLinesConfig
+*	功能说明: 配置DM9000AE控制口线,FSMC管脚设置为复用功能 (For SMT32F4)
+*	形    参: 无
+*	返 回 值: 无
+*********************************************************************************************************
+*/
+static void DM9K_CtrlLinesConfig(void)
+{
+    GPIO_InitTypeDef GPIO_InitStructure;
+
+    /* 使能FSMC时钟 */
+    RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE);
+
+    /* 使能 GPIO时钟 */
+    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOG, ENABLE);
+
+    /* 设置 PD.00(D2), PD.01(D3), PD.04(NOE), PD.05(NWE), PD.08(D13), PD.09(D14),
+     PD.10(D15), PD.14(D0), PD.15(D1) 为复用推挽输出 */
+
+    GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_FSMC);
+    GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_FSMC);
+    GPIO_PinAFConfig(GPIOD, GPIO_PinSource4, GPIO_AF_FSMC);
+    GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_FSMC);
+    GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_FSMC);
+    GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_FSMC);
+    GPIO_PinAFConfig(GPIOD, GPIO_PinSource10, GPIO_AF_FSMC);
+    GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_FSMC);
+    GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_FSMC);
+
+    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 |
+                                  GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_14 |
+                                  GPIO_Pin_15;
+    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
+    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
+    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
+    GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
+    GPIO_Init(GPIOD, &GPIO_InitStructure);
+
+    /* 设置 PE.07(D4), PE.08(D5), PE.09(D6), PE.10(D7), PE.11(D8), PE.12(D9), PE.13(D10),
+     PE.14(D11), PE.15(D12) 为复用推挽输出 */
+
+    GPIO_PinAFConfig(GPIOE, GPIO_PinSource4, GPIO_AF_FSMC);
+    GPIO_PinAFConfig(GPIOE, GPIO_PinSource5, GPIO_AF_FSMC);
+
+    GPIO_PinAFConfig(GPIOE, GPIO_PinSource7, GPIO_AF_FSMC);
+    GPIO_PinAFConfig(GPIOE, GPIO_PinSource8, GPIO_AF_FSMC);
+    GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_FSMC);
+    GPIO_PinAFConfig(GPIOE, GPIO_PinSource10, GPIO_AF_FSMC);
+    GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_FSMC);
+    GPIO_PinAFConfig(GPIOE, GPIO_PinSource12, GPIO_AF_FSMC);
+    GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_FSMC);
+    GPIO_PinAFConfig(GPIOE, GPIO_PinSource14, GPIO_AF_FSMC);
+    GPIO_PinAFConfig(GPIOE, GPIO_PinSource15, GPIO_AF_FSMC);
+
+    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 |
+                                  GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 |
+                                  GPIO_Pin_15;
+    GPIO_Init(GPIOE, &GPIO_InitStructure);
+
+    /* 设置 PD.13(A18 (RS))  为复用推挽输出 */
+    GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_FSMC);
+
+    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
+    GPIO_Init(GPIOD, &GPIO_InitStructure);
+
+    /* 设置 PG10 (CS)) 为复用推挽输出 */
+    GPIO_PinAFConfig(GPIOG, GPIO_PinSource10, GPIO_AF_FSMC);
+
+    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
+    GPIO_Init(GPIOG, &GPIO_InitStructure);
+
+    /* PA15 是DM9000_INT中断输入口(本程序未使用) */
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: DM9K_FSMCConfig
+*	功能说明: 配置FSMC并口访问时序
+*	形    参: 无
+*	返 回 值: 无
+*********************************************************************************************************
+*/
+static void DM9K_FSMCConfig(void)
+{
+    FSMC_NORSRAMInitTypeDef       FSMC_NORSRAMInitStructure;
+    FSMC_NORSRAMTimingInitTypeDef p;
+
+    /*-- FSMC Configuration ------------------------------------------------------*/
+    /*----------------------- SRAM Bank 3 ----------------------------------------*/
+    /*-- FSMC Configuration ------------------------------------------------------*/
+    p.FSMC_AddressSetupTime      = 6; /* 设置为2会出错; 3正常 */
+    p.FSMC_AddressHoldTime       = 2;
+    p.FSMC_DataSetupTime         = 4; /* 设置为1出错,2正常 */
+    p.FSMC_BusTurnAroundDuration = 1;
+    p.FSMC_CLKDivision           = 0;
+    p.FSMC_DataLatency           = 0;
+    p.FSMC_AccessMode            = FSMC_AccessMode_A;
+
+    FSMC_NORSRAMInitStructure.FSMC_Bank                  = FSMC_Bank1_NORSRAM3;
+    FSMC_NORSRAMInitStructure.FSMC_DataAddressMux        = FSMC_DataAddressMux_Disable;
+    FSMC_NORSRAMInitStructure.FSMC_MemoryType            = FSMC_MemoryType_SRAM; // FSMC_MemoryType_PSRAM;
+    FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth       = FSMC_MemoryDataWidth_16b;
+    FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode       = FSMC_BurstAccessMode_Disable;
+    FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait      = FSMC_AsynchronousWait_Disable;
+    FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity    = FSMC_WaitSignalPolarity_Low;
+    FSMC_NORSRAMInitStructure.FSMC_WrapMode              = FSMC_WrapMode_Disable;
+    FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive      = FSMC_WaitSignalActive_BeforeWaitState;
+    FSMC_NORSRAMInitStructure.FSMC_WriteOperation        = FSMC_WriteOperation_Enable;
+    FSMC_NORSRAMInitStructure.FSMC_WaitSignal            = FSMC_WaitSignal_Disable;
+    FSMC_NORSRAMInitStructure.FSMC_ExtendedMode          = FSMC_ExtendedMode_Disable;
+    FSMC_NORSRAMInitStructure.FSMC_WriteBurst            = FSMC_WriteBurst_Disable;
+    FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
+    FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct     = &p;
+
+    FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
+
+    /*!< Enable FSMC Bank1_SRAM3 Bank */
+    FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM3, ENABLE);
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: dm9k_ReadReg
+*	功能说明: 读出DM9000指定寄存器的值
+*	形    参: reg 寄存器地址
+*	返 回 值: 寄存器值
+*********************************************************************************************************
+*/
+uint8_t dm9k_ReadReg(uint8_t reg)
+{
+    NET_REG_ADDR = reg;
+    return (NET_REG_DATA);
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: dm9k_WriteReg
+*	功能说明: 读出DM9000指定寄存器的值
+*	形    参: reg :寄存器地址
+*			 writedata : 写入的数据
+*	返 回 值: 无
+*********************************************************************************************************
+*/
+void dm9k_WriteReg(uint8_t reg, uint8_t writedata)
+{
+    NET_REG_ADDR = reg;
+    NET_REG_DATA = writedata;
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: dm9k_hash_table
+*	功能说明: 设置 DM9000A MAC 、 广播 、 多播 寄存器
+*	形    参: 无
+*	返 回 值: 无
+*********************************************************************************************************
+*/
+void dm9k_hash_table(void)
+{
+    uint8_t i;
+
+    /* 将MAC地址告诉uip */
+    for (i = 0; i < 6; i++)
+    {
+        uip_ethaddr.addr[i] = DEF_MAC_ADDR[i];
+    }
+
+    /* 设置 网卡 MAC 位置,来自於 MyHardware */
+    for (i = 0; i < 6; i++)
+    {
+        dm9k_WriteReg(DM9000_REG_PAR + i, DEF_MAC_ADDR[i]);
+    }
+
+    /* 清除 网卡多播设置 */
+    for (i = 0; i < 8; i++)
+    {
+        dm9k_WriteReg(DM9000_REG_MAR + i, 0x00);
+    }
+
+    /* 设置 广播包 设置 */
+    dm9k_WriteReg(DM9000_REG_MAR + 7, 0x80);
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: dm9k_reset
+*	功能说明: 软件复位DM9000AE
+*	形    参: 无
+*	返 回 值: 无
+*********************************************************************************************************
+*/
+void dm9k_reset(void)
+{
+    dm9k_WriteReg(DM9000_REG_NCR, DM9000_REG_RESET); /* 对 DM9000A 进行软件重置 */
+    bsp_DelayUS(10);                                 /* delay 10us */
+    dm9k_WriteReg(DM9000_REG_NCR, DM9000_REG_RESET); /* 对 DM9000A 进行软件重置 */
+    bsp_DelayUS(10);                                 /* delay 10us */
+
+    /* 基本记存器相关设置 */
+    dm9k_WriteReg(DM9000_REG_IMR, DM9000_IMR_OFF);   /* 开启内存自环模式 */
+    dm9k_WriteReg(DM9000_REG_TCR2, DM9000_TCR2_SET); /* 设置 LED 显示模式1:全双工亮,半双工灭 */
+
+    /* 清除多余资讯 */
+    dm9k_WriteReg(DM9000_REG_NSR, 0x2c);
+    dm9k_WriteReg(DM9000_REG_TCR, 0x00);
+    dm9k_WriteReg(DM9000_REG_ISR, 0x3f);
+
+#ifdef DM9000A_FLOW_CONTROL
+    dm9k_WriteReg(DM9000_REG_BPTR, DM9000_BPTR_SET); /* 半双工流控设置 */
+    dm9k_WriteReg(DM9000_REG_FCTR, DM9000_FCTR_SET); /* 全双工流控设置 */
+    dm9k_WriteReg(DM9000_REG_FCR, DM9000_FCR_SET);   /* 开启流控设置 */
+#endif
+
+#ifdef DM9000A_UPTO_100M
+    /* DM9000A无此寄存器 */
+    dm9k_WriteReg(DM9000_REG_OTCR, DM9000_OTCR_SET); /* 工作频率到 100Mhz 设置 */
+#endif
+
+#ifdef Rx_Int_enable
+    dm9k_WriteReg(DM9000_REG_IMR, DM9000_IMR_SET); /* 开启 中断模式 */
+#else
+    dm9k_WriteReg(DM9000_REG_IMR, DM9000_IMR_OFF); /* 关闭 中断模式 */
+#endif
+
+    dm9k_WriteReg(DM9000_REG_RCR, DM9000_RCR_SET); /* 开启 接收工能 */
+
+    SendPackOk = 0;
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: dm9k_phy_write
+*	功能说明: 软件复位DM9000AE
+*	形    参: phy_reg : PHY寄存器地址
+*			  writedata : 写入的数据
+*	返 回 值: 无
+*********************************************************************************************************
+*/
+void dm9k_phy_write(uint8_t phy_reg, uint16_t writedata)
+{
+    /* 设置写入 PHY 寄存器的位置 */
+    dm9k_WriteReg(DM9000_REG_EPAR, phy_reg | DM9000_PHY);
+
+    /* 设置写入 PHY 寄存器的值 */
+    dm9k_WriteReg(DM9000_REG_EPDRH, (writedata >> 8) & 0xff);
+    dm9k_WriteReg(DM9000_REG_EPDRL, writedata & 0xff);
+
+    dm9k_WriteReg(DM9000_REG_EPCR, 0x0a); /* 将资料写入 PHY 寄存器 */
+    while (dm9k_ReadReg(DM9000_REG_EPCR) & 0x01)
+        ;                                 /* 查寻是否执行结束 */
+    dm9k_WriteReg(DM9000_REG_EPCR, 0x08); /* 清除写入命令 */
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: dm9k_initnic
+*	功能说明: 配置DM9000AE芯片(初始化)
+*	形    参: 无
+*	返 回 值: 无
+*********************************************************************************************************
+*/
+static void dm9k_initnic(void)
+{
+    dm9k_WriteReg(DM9000_REG_NCR, DM9000_REG_RESET); /* 对 DM9000A 进行软件重置 */
+    bsp_DelayUS(10);                                 /* delay 10us */
+
+    dm9k_hash_table(); /* 设置 DM9000A MAC 及 多播*/
+
+    dm9k_reset(); /* 进行 DM9000A 软件设置 */
+
+    dm9k_WriteReg(DM9000_REG_GPR, DM9000_PHY_OFF); /* 关闭 PHY ,进行 PHY 设置*/
+    dm9k_phy_write(0x00, 0x8000);                  /* 重置 PHY 的寄存器 */
+#ifdef DM9000A_FLOW_CONTROL
+    dm9k_phy_write(0x04, 0x01e1 | 0x0400); /* 设置 自适应模式相容表 */
+#else
+    dm9k_phy_write(0x04, 0x01e1);                  /* 设置 自适应模式相容表 */
+#endif
+    // dm9k_phy_write(0x00, 0x1000);					/* 设置 基本连接模式 */
+    /* 连接模式设置
+      0x0000 : 固定10M半双工
+      0x0100 : 固定10M全双工
+      0x2000 : 固定100M半双工
+      0x2100 : 固定100M全双工
+      0x1000 : 自适应模式
+    */
+    dm9k_phy_write(0x00, 0x1000); /* 设置 基本连接模式 */
+
+    dm9k_WriteReg(DM9000_REG_GPR, DM9000_PHY_ON); /* 结束 PHY 设置, 开启 PHY */
+
+    // dm9k_debug_test();
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: dm9k_receive_packet
+*	功能说明: 配置DM9000AE芯片(初始化)
+*	形    参: _uip_buf : 接收结果存放的缓冲区指针
+*	返 回 值: > 0 表示接收的数据长度, 0表示没有数据
+*********************************************************************************************************
+*/
+uint16_t dm9k_receive_packet(uint8_t *_uip_buf)
+{
+    uint16_t  ReceiveLength;
+    uint16_t *ReceiveData;
+    uint8_t   rx_int_count = 0;
+    uint8_t   rx_checkbyte;
+    uint16_t  rx_status, rx_length;
+    uint8_t   jump_packet;
+    uint16_t  i;
+    uint16_t  calc_len;
+    uint16_t  calc_MRR;
+
+    do
+    {
+        ReceiveLength = 0; /* 清除接收的长度 */
+        ReceiveData   = (uint16_t *)_uip_buf;
+        jump_packet   = 0;               /* 清除跳包动作 */
+        dm9k_ReadReg(DM9000_REG_MRCMDX); /* 读取内存数据,地址不增加 */
+        /* 计算内存数据位置 */
+        calc_MRR     = (dm9k_ReadReg(DM9000_REG_MRRH) << 8) + dm9k_ReadReg(DM9000_REG_MRRL);
+        rx_checkbyte = dm9k_ReadReg(DM9000_REG_MRCMDX); /*  */
+
+        if (rx_checkbyte == DM9000_PKT_RDY) /* 取 */
+        {
+            /* 读取封包相关资讯 及 长度 */
+            NET_REG_ADDR = DM9000_REG_MRCMD;
+            rx_status    = NET_REG_DATA;
+            rx_length    = NET_REG_DATA;
+
+            /* 若收到超过系统可承受的封包,此包跳过 */
+            if (rx_length > Max_Ethernet_Lenth)
+                jump_packet = 1;
+
+#ifdef Broadcast_Jump
+            /* 若收到的广播或多播包超过特定长度,此包跳过 */
+            if (rx_status & 0x4000)
+            {
+                if (rx_length > Max_Broadcast_Lenth)
+                    jump_packet = 1;
+            }
+#endif
+            /* 计算下一个包的指针位 , 若接收长度为奇数,需加一对齐偶字节。*/
+            /* 若是超过 0x3fff ,则需回归绕到 0x0c00 起始位置 */
+            calc_MRR += (rx_length + 4);
+            if (rx_length & 0x01)
+                calc_MRR++;
+            if (calc_MRR > 0x3fff)
+                calc_MRR -= 0x3400;
+
+            if (jump_packet == 0x01)
+            {
+                /* 将指针移到下一个包的包头位置 */
+                dm9k_WriteReg(DM9000_REG_MRRH, (calc_MRR >> 8) & 0xff);
+                dm9k_WriteReg(DM9000_REG_MRRL, calc_MRR & 0xff);
+                continue;
+            }
+
+            /* 开始将内存的资料搬到到系统中,每次移动一个 word */
+            calc_len = (rx_length + 1) >> 1;
+            for (i = 0; i < calc_len; i++)
+                ReceiveData[i] = NET_REG_DATA;
+
+            /* 将包长回报给 TCP/IP 上层,并减去最後 4 BYTE 的 CRC-32 检核码 */
+            ReceiveLength = rx_length - 4;
+
+            rx_int_count++; /* 累计收包次数 */
+
+#ifdef FifoPointCheck
+            if (calc_MRR != ((dm9k_ReadReg(DM9000_REG_MRRH) << 8) + dm9k_ReadReg(DM9000_REG_MRRL)))
+            {
+#ifdef Point_Error_Reset
+                dm9k_reset(); /* 若是指针出错,重置 */
+                return ReceiveLength;
+#endif
+                /*若是指针出错,将指针移到下一个包的包头位置  */
+                dm9k_WriteReg(DM9000_REG_MRRH, (calc_MRR >> 8) & 0xff);
+                dm9k_WriteReg(DM9000_REG_MRRL, calc_MRR & 0xff);
+            }
+#endif
+            return ReceiveLength;
+        }
+        else
+        {
+            if (rx_checkbyte == DM9000_PKT_NORDY) /* 未收到包 */
+            {
+                dm9k_WriteReg(DM9000_REG_ISR, 0x3f); /*  */
+            }
+            else
+            {
+                dm9k_reset(); /* 接收指针出错,重置 */
+            }
+            return (0);
+        }
+    } while (rx_int_count < Max_Int_Count); /* 是否超过最多接收封包计数 */
+    return 0;
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: dm9k_send_packet
+*	功能说明: 发送一包数据
+*	形    参: p_char : 发送数据缓冲区
+*	返 回 值: length : 数据长度
+*********************************************************************************************************
+*/
+void dm9k_send_packet(uint8_t *p_char, uint16_t length)
+{
+    uint16_t      SendLength = length;
+    uint16_t     *SendData   = (uint16_t *)p_char;
+    uint16_t      i;
+    uint16_t      calc_len;
+    __IO uint16_t calc_MWR;
+
+    /* 检查 DM9000A 是否还在传送中!若是等待直到传送结束 */
+    if (SendPackOk == Max_Send_Pack)
+    {
+        while (dm9k_ReadReg(DM9000_REG_TCR) & DM9000_TCR_SET)
+        {
+            bsp_DelayUS(5);
+        }
+        SendPackOk = 0;
+    }
+
+    SendPackOk++; /* 设置传送计数 */
+
+#ifdef FifoPointCheck
+    /* 计算下一个传送的指针位 , 若接收长度为奇数,需加一对齐偶字节。*/
+    /* 若是超过 0x0bff ,则需回归绕到 0x0000 起始位置 */
+    calc_MWR = (dm9k_ReadReg(DM9000_REG_MWRH) << 8) + dm9k_ReadReg(DM9000_REG_MWRL);
+    calc_MWR += SendLength;
+    if (SendLength & 0x01)
+        calc_MWR++;
+    if (calc_MWR > 0x0bff)
+        calc_MWR -= 0x0c00;
+#endif
+
+    dm9k_WriteReg(DM9000_REG_TXPLH, (SendLength >> 8) & 0xff); /* 设置传送封包的长度 */
+    dm9k_WriteReg(DM9000_REG_TXPLL, SendLength & 0xff);
+
+    /* 开始将系统的资料搬到到内存中,每次移动一个 word */
+    NET_REG_ADDR = DM9000_REG_MWCMD;
+    calc_len     = (SendLength + 1) >> 1;
+    for (i = 0; i < calc_len; i++)
+        NET_REG_DATA = SendData[i];
+
+    dm9k_WriteReg(DM9000_REG_TCR, DM9000_TCR_SET); /* 进行传送 */
+
+#ifdef FifoPointCheck
+    if (calc_MWR != ((dm9k_ReadReg(DM9000_REG_MWRH) << 8) + dm9k_ReadReg(DM9000_REG_MWRL)))
+    {
+#ifdef Point_Error_Reset
+        /* 若是指针出错,等待此一封包送完 , 之後进行重置 */
+        while (dm9k_ReadReg(DM9000_REG_TCR) & DM9000_TCR_SET)
+            bsp_DelayUS(5);
+        dm9k_reset();
+        return;
+#endif
+        /*若是指针出错,将指针移到下一个传送包的包头位置  */
+        dm9k_WriteReg(DM9000_REG_MWRH, (calc_MWR >> 8) & 0xff);
+        dm9k_WriteReg(DM9000_REG_MWRL, calc_MWR & 0xff);
+    }
+#endif
+    return;
+}
+
+/*
+*********************************************************************************************************
+*	函 数 名: dm9k_interrupt
+*	功能说明: 中断处理函数 (webserver例程未使用中断)
+*	形    参: 无
+*	返 回 值: 无
+*********************************************************************************************************
+*/
+void dm9k_interrupt(void)
+{
+    uint8_t  save_reg;
+    uint16_t isr_status;
+
+    save_reg = NET_REG_ADDR; /* 暂存所使用的位置 */
+
+    dm9k_WriteReg(DM9000_REG_IMR, DM9000_IMR_OFF); /* 关闭 DM9000A 中断 */
+    isr_status = dm9k_ReadReg(DM9000_REG_ISR);     /* 取得中断产生值 */
+
+    if (isr_status & DM9000_RX_INTR)
+    { /* 检查是否为接收中断 */
+        // dm9k_receive_packet();							/* 执行接收处理程序 */
+    }
+
+    dm9k_WriteReg(DM9000_REG_IMR, DM9000_IMR_SET); /* 开启 DM9000A 中断 */
+    NET_REG_ADDR = save_reg;                       /* 回复所使用的位置 */
+}
+
+/*******************************************************************************
+ *	函数名: dm9k_ReadID
+ *	参  数: 无
+ *	返  回: 无
+ *	功  能: 读取芯片ID
+ */
+uint32_t dm9k_ReadID(void)
+{
+    uint8_t vid1, vid2, pid1, pid2;
+
+    if (s_FSMC_Init_Ok == 0)
+    {
+        DM9K_CtrlLinesConfig();
+        DM9K_FSMCConfig();
+
+        s_FSMC_Init_Ok = 1;
+    }
+    vid1 = dm9k_ReadReg(DM9000_REG_VID_L) & 0xFF;
+    vid2 = dm9k_ReadReg(DM9000_REG_VID_H) & 0xFF;
+    pid1 = dm9k_ReadReg(DM9000_REG_PID_L) & 0xFF;
+    pid2 = dm9k_ReadReg(DM9000_REG_PID_H) & 0xFF;
+
+    return (vid2 << 24) | (vid1 << 16) | (pid2 << 8) | pid1;
+}
+
+#if 0
+	/*
+	*********************************************************************************************************
+	*	函 数 名: dm9k_debug_test
+	*	功能说明: 测试DM9000AE的函数,用于排错
+	*	形    参: 无
+	*	返 回 值: 无
+	*********************************************************************************************************
+	*/
+	void dm9k_debug_test(void)
+	{
+		uint32_t check_device;
+		uint8_t  check_iomode;
+		uint8_t  check_reg_fail = 0;
+		uint8_t  check_fifo_fail = 0;
+		uint16_t i;
+		uint16_t j;
+
+		dm9k_WriteReg(DM9000_REG_NCR, DM9000_REG_RESET);			/* 对 DM9000A 进行软件重置 */
+		bsp_DelayUS(10);								/* delay 10us */
+		dm9k_WriteReg(DM9000_REG_NCR, DM9000_REG_RESET);			/* 对 DM9000A 进行软件重置 */
+		bsp_DelayUS(10);								/* delay 10us */
+
+		check_device  = dm9k_ReadReg(DM9000_REG_VID_L);
+		check_device |= dm9k_ReadReg(DM9000_REG_VID_H) << 8;
+		check_device |= dm9k_ReadReg(DM9000_REG_PID_L) << 16;
+		check_device |= dm9k_ReadReg(DM9000_REG_PID_H) << 24;
+
+		if(check_device != 0x90000A46)
+		{
+			printk("DM9K_DEBUG ==> DEIVCE NOT FOUND, SYSTEM HOLD !!\n");
+			while(1);
+		}
+		else
+		{
+			printk("DM9K_DEBUG ==> DEIVCE FOUND !!\n");
+		}
+
+		check_iomode = dm9k_ReadReg(DM9000_REG_ISR) >> 6;
+		if(check_iomode != DM9000_WORD_MODE)
+		{
+			printk("DM9K_DEBUG ==> DEIVCE NOT WORD MODE, SYSTEM HOLD !!\n");
+			while(1);
+		}
+		else
+		{
+			printk("DM9K_DEBUG ==> DEIVCE IS WORD MODE !!\n");
+		}
+
+		printk("DM9K_DEBUG ==> REGISTER R/W TEST !!\n");
+		NET_REG_ADDR = DM9000_REG_MAR;
+		for(i = 0; i < 0x0100; i++)
+		{
+			NET_REG_DATA = i;
+			if(i != (NET_REG_DATA & 0xff))
+			{
+				printk("             > error W %02x , R %02x \n", i , NET_REG_DATA);
+				check_reg_fail = 1;
+			}
+		}
+
+		if(check_reg_fail)
+		{
+			printk("DM9K_DEBUG ==> REGISTER R/W FAIL, SYSTEM HOLD !!\n");
+			while(1);
+		}
+
+		printk("DM9K_DEBUG ==> FIFO R/W TEST !!\n");
+		printk("DM9K_DEBUG ==> FIFO WRITE START POINT 0x%02x%02x \n",
+				dm9k_ReadReg(DM9000_REG_MWRH), dm9k_ReadReg(DM9000_REG_MWRL));
+
+		NET_REG_ADDR = DM9000_REG_MWCMD;
+		for(i = 0; i < 0x1000; i++)
+			NET_REG_DATA = ((i & 0xff) * 0x0101);
+
+		printk("DM9K_DEBUG ==> FIFO WRITE END POINT 0x%02x%02x \n",
+				dm9k_ReadReg(DM9000_REG_MWRH), dm9k_ReadReg(DM9000_REG_MWRL));
+
+		if((dm9k_ReadReg(DM9000_REG_MWRH) != 0x20) || (dm9k_ReadReg(DM9000_REG_MWRL) != 0x00))
+		{
+			printk("DM9K_DEBUG ==> FIFO WRITE FAIL, SYSTEM HOLD !!\n");
+			while(1);
+		}
+
+		dm9k_ReadReg(DM9000_REG_MRCMDX);
+		printk("DM9K_DEBUG ==> FIFO READ START POINT 0x%02x%02x \n",
+				dm9k_ReadReg(DM9000_REG_MRRH), dm9k_ReadReg(DM9000_REG_MRRL));
+		dm9k_ReadReg(DM9000_REG_MRCMDX);
+
+		NET_REG_ADDR = DM9000_REG_MRCMD;
+		for(i = 0; i < 0x1000; i++)
+		{
+			j = NET_REG_DATA;
+
+			if(((i & 0xff) * 0x0101) != j)
+			{
+				//printk("             > error W %04x , R %04x \n",
+				//		((i & 0xff) * 0x0101) , j);
+				check_fifo_fail = 1;
+			}
+		}
+
+		printk("DM9K_DEBUG ==> FIFO READ END POINT 0x%02x%02x \n",
+				dm9k_ReadReg(DM9000_REG_MRRH), dm9k_ReadReg(DM9000_REG_MRRL));
+
+		if((dm9k_ReadReg(DM9000_REG_MRRH) != 0x20) || (dm9k_ReadReg(DM9000_REG_MRRL) != 0x00))
+		{
+			printk("DM9K_DEBUG ==> FIFO WRITE FAIL, SYSTEM HOLD !!\n");
+			while(1);
+		}
+
+			if(check_fifo_fail)
+		{
+			printk("DM9K_DEBUG ==> FIFO R/W DATA FAIL, SYSTEM HOLD !!\n");
+			while(1);
+		}
+
+		printk("DM9K_DEBUG ==> PACKET SEND & INT TEST !! \n");
+		dm9k_WriteReg(DM9000_REG_NCR, DM9000_REG_RESET);
+		bsp_DelayUS(10);
+		dm9k_WriteReg(DM9000_REG_NCR, DM9000_REG_RESET);
+		bsp_DelayUS(10);
+
+		dm9k_WriteReg(DM9000_REG_IMR, DM9000_IMR_OFF | DM9000_TX_INTR);
+
+		dm9k_WriteReg(DM9000_REG_TXPLH, 0x01);
+		dm9k_WriteReg(DM9000_REG_TXPLL, 0x00);
+
+		do
+		{
+			dm9k_WriteReg(DM9000_REG_ISR, DM9000_TX_INTR);
+			printk("DM9K_DEBUG ==> INT PIN IS OFF\n");
+
+			NET_REG_ADDR = DM9000_REG_MWCMD;
+			for(i = 0; i < (0x0100 / 2); i++)
+			{
+				if(i < 3)
+					NET_REG_DATA = 0xffff;
+				else
+					NET_REG_DATA = i * 0x0101;
+			}
+
+			printk("DM9K_DEBUG ==> PACKET IS SEND \n");
+			dm9k_WriteReg(DM9000_REG_TCR, DM9000_TCR_SET);
+
+			while(dm9k_ReadReg(DM9000_REG_TCR) & DM9000_TCR_SET) bsp_DelayUS (5);
+			if(dm9k_ReadReg(DM9000_REG_ISR) & DM9000_TX_INTR)
+				printk("DM9K_DEBUG ==> INT PIN IS ACTIVE \n");
+			else
+				printk("DM9K_DEBUG ==> INT PIN IS NOT ACTIVE \n");
+
+			for(i = 0; i < 10; i++)
+				bsp_DelayUS(1000);
+
+		}while(1);
+	}
+
+	/*
+	*********************************************************************************************************
+	*	函 数 名: etherdev_chkmedia
+	*	功能说明: 检测网络连接状态
+	*	形    参: 无
+	*	返 回 值: 无
+	*********************************************************************************************************
+	*/
+	void etherdev_chkmedia(void)
+	{
+	//	uint8_t status;
+
+		while(!(dm9k_ReadReg(DM9000_REG_NSR) & DM9000_PHY))
+		{
+			bsp_DelayUS(2000);
+		}
+	}
+
+
+	/*******************************************************************************
+	*	函数名: etherdev_poll
+	*	参  数: 无
+	*	返  回: 无
+	*	功  能: uIP 接口函数, 采用查询方式接收一个IP包
+	*/
+	/*
+	                              etherdev_poll()
+
+	    This function will read an entire IP packet into the uip_buf.
+	    If it must wait for more than 0.5 seconds, it will return with
+	    the return value 0. Otherwise, when a full packet has been read
+	    into the uip_buf buffer, the length of the packet is returned.
+	*/
+	uint16_t etherdev_poll(void)
+	{
+		uint16_t bytes_read = 0;
+#if 0
+
+		/* tick_count threshold should be 12 for 0.5 sec bail-out
+			One second (24) worked better for me, but socket recycling
+			is then slower. I set UIP_TIME_WAIT_TIMEOUT 60 in uipopt.h
+			to counter this. Retransmission timing etc. is affected also. */
+		while ((!(bytes_read = etherdev_read())) && (timer0_tick() < 12)) continue;
+
+		timer0_reset();
+
+#endif
+		return bytes_read;
+	}
+
+#endif

+ 9 - 6
User/app/dm9000/dm9000.h → User/app/dm9k/dm9k.h

@@ -10,11 +10,14 @@
  *
  */
 
-#ifndef _DM9K_UIP_H_
-#define _DM9K_UIP_H_
+#ifndef _DM9K_H_
+#define _DM9K_H_
 
+#include "includes.h"
 #include <inttypes.h>
 
+#define TCP_PORT 8080
+
 /* DM9000 REGISTER LIST */
 #define DM9000_REG_NCR    0x00
 #define DM9000_REG_NSR    0x01
@@ -92,10 +95,10 @@
 #define DM9000_IMR_OFF    0x80 /* 设定 关闭中断使能 条件设置 */
 
 /* EXPORTED SUBPROGRAM SPECIFICATIONS */
-void     dm9000_init(void);
-void     dm9000_send_packet(uint8_t *p_char, uint16_t length);
-uint16_t dm9000_receive_packet(uint8_t *_uip_buf);
+void     dm9k_init(void);
+void     dm9k_send_packet(uint8_t *p_char, uint16_t length);
+uint16_t dm9k_receive_packet(uint8_t *_uip_buf);
 
-uint32_t dm9000_ReadID(void);
+uint32_t dm9k_ReadID(void);
 
 #endif

+ 111 - 123
User/app/fly_uart/fly_uart.c → User/app/modbus/modbus.c

@@ -1,21 +1,28 @@
-/*-------------------------------------------------------------------------------------------------
-** module: samkoon communication
-** com   : 485\Modbus Rtu
-** data  : 2022-4-15
-** by    : xie
--------------------------------------------------------------------------------------------------*/
-#include "fly_uart.h"
+/**
+************************************************************************************************
+* @文件    : fly_modbus.c
+* @作者    : 樊春春
+* @版本    : V1.0
+* @时间    : 2022/05/30 22:46:49
+* @邮箱    : [email protected]
+* @说明    :
+************************************************************************************************
+**/
+
+#include "modbus.h"
 
 INT16U misc_info_buf[BUF_LEN_128] = {0};
 INT16U cell_temp_buf[BUF_LEN_512] = {0};
 INT16U samkoon_txbuf[BUF_LEN_128] = {0};
 
-/*---------------------------------------
-** func :Count_CRC
-** brief:CRC cal
-** para :addr: 数据指针   num: 数量
----------------------------------------*/
-unsigned short Count_CRC(const INT8U *addr, int num)
+/****************************************************
+ * @函数    : crc_count
+ * @作者    : 樊春春
+ * @功能    : 计算CRC数据
+ * @入参    : addr: 数据指针   num: 数量
+ * @说明    : 无
+ *****************************************************/
+unsigned short crc_count(const INT8U *addr, int num)
 {
     unsigned short crc = 0xFFFF;
     int            i;
@@ -30,6 +37,61 @@ unsigned short Count_CRC(const INT8U *addr, int num)
     return crc;
 }
 
+/****************************************************
+ * @函数    : crc_write
+ * @作者    : 樊春春
+ * @功能    : CRC写入
+ * @入参    : p: 数据指针   data: crc值   count: 写入位置
+ * @说明    : 无
+ *****************************************************/
+void crc_write(INT8U *p, INT16U data, INT8U count)
+{
+    p    = p + (2 * count + 3);
+    *p++ = (INT8U)data;
+    *p++ = (INT8U)(data >> 8);
+}
+
+/****************************************************
+ * @函数    : little2big
+ * @作者    : 樊春春
+ * @功能    : 小端转大端
+ * @入参    : buf: 数据指针   count: 个数
+ * @说明    : 无
+ *****************************************************/
+void little2big(INT16U *buf, INT16U count)
+{
+    INT8U  i      = 0;
+    INT8U *p      = (INT8U *)buf;
+    INT8U *p_next = (INT8U *)buf + 1;
+    char   tmp;
+    for (i = 0; i < 2 * count; i++)
+    {
+        tmp     = *p;
+        *p      = *p_next;
+        *p_next = tmp;
+        p       = p + 2;
+        p_next  = p_next + 2;
+    }
+}
+
+/****************************************************
+ * @函数    : modbus_head
+ * @作者    : 樊春春
+ * @功能    : Mosbus头拷贝
+ * @入参    : des_data: 目标数据指针   src_data: 源数据指针
+ * @说明    : 无
+ *****************************************************/
+void modbus_head(INT8U *des_data, INT8U *src_data)
+{
+    INT8U i = 0;
+    for (i = 0; i < 2; i++)
+    {
+        *des_data++ = *src_data++;
+    }
+    src_data  = src_data + 3;
+    *des_data = (*src_data) * 2;
+}
+
 /*--------------------------------------------
 ** func :ImportantData_TxGet
 ** brief:copy send data;
@@ -163,53 +225,7 @@ void Rtu_TxData_Get(INT16U pos,
         *des_data++ = *src_data++;
     }
 }
-/*---------------------------------------
-** func :Crc_Write()
-** brief: none
-** para :
----------------------------------------*/
-void Crc_Write(INT8U *p, INT16U data, INT8U count)
-{
-    p    = p + (2 * count + 3);
-    *p++ = (INT8U)data;
-    *p++ = (INT8U)(data >> 8);
-}
-/*---------------------------------------
-** func :Small2Big()
-** brief: none
-** para :
----------------------------------------*/
-void Small2Big(INT16U *buf, INT16U count)
-{
-    INT8U  i      = 0;
-    INT8U *p      = (INT8U *)buf;
-    INT8U *p_next = (INT8U *)buf + 1;
-    char   tmp;
-    for (i = 0; i < 2 * count; i++)
-    {
-        tmp     = *p;
-        *p      = *p_next;
-        *p_next = tmp;
-        p       = p + 2;
-        p_next  = p_next + 2;
-    }
-}
 
-/*---------------------------------------
-** func :Modbus_Head_Copy
-** brief: none
-** para :
----------------------------------------*/
-void Modbus_Head_Copy(INT8U *des_data, INT8U *src_data)
-{
-    INT8U i = 0;
-    for (i = 0; i < 2; i++)
-    {
-        *des_data++ = *src_data++; // addr fun_code
-    }
-    src_data  = src_data + 3;
-    *des_data = (*src_data) * 2;
-}
 /*----------------------------------------------
 ** @func  : samkoon_com_work
 ** @brief : BMS with samkoon communication
@@ -237,56 +253,56 @@ void samkoon_com_work(INT8U *rec_data, void (*tx_p)(const INT8U *buf, INT16U len
     case FUNC_03:
         if ((register_addr_hmi >= MODBUS_ADDR_RACK_REMOTE) && (register_addr_hmi < MODBUS_ADDR_RACK_REMOTE_END))
         {
-            Modbus_Head_Copy((INT8U *)samkoon_txbuf, rec_data);
+            modbus_head((INT8U *)samkoon_txbuf, rec_data);
             // Important_Bin_ReadOut(misc_info_buf);
-            Small2Big(misc_info_buf, register_count);
+            little2big(misc_info_buf, register_count);
             Rtu_TxData_Get(register_addr_hmi, register_count, 0xc9, 3, (INT8U *)samkoon_txbuf, (INT8U *)misc_info_buf);
-            tmp_crc = Count_CRC((INT8U *)samkoon_txbuf, 2 * register_count + 3);
-            Crc_Write((INT8U *)samkoon_txbuf, tmp_crc, register_count);
+            tmp_crc = crc_count((INT8U *)samkoon_txbuf, 2 * register_count + 3);
+            crc_write((INT8U *)samkoon_txbuf, tmp_crc, register_count);
             //				samkoon_txbuf[register_count + 2] = tmp_crc;
             tx_p((INT8U *)samkoon_txbuf, (2 * register_count + 5));
         }
         else if ((register_addr_hmi >= MODBUS_ADDR_SOC) && (register_addr_hmi < MODBUS_RACK_END))
         {
-            Modbus_Head_Copy((INT8U *)samkoon_txbuf, rec_data);
+            modbus_head((INT8U *)samkoon_txbuf, rec_data);
             // ImportantData_Readout(misc_info_buf);
-            Small2Big(misc_info_buf, register_count);
+            little2big(misc_info_buf, register_count);
             Rtu_TxData_Get(register_addr_hmi, register_count, 0x4101, 3, (INT8U *)samkoon_txbuf, (INT8U *)misc_info_buf);
-            tmp_crc = Count_CRC((INT8U *)samkoon_txbuf, 2 * register_count + 3);
-            Crc_Write((INT8U *)samkoon_txbuf, tmp_crc, register_count);
+            tmp_crc = crc_count((INT8U *)samkoon_txbuf, 2 * register_count + 3);
+            crc_write((INT8U *)samkoon_txbuf, tmp_crc, register_count);
             //			   	samkoon_txbuf[register_count + 2] = tmp_crc;
             tx_p((INT8U *)samkoon_txbuf, (2 * register_count + 5));
         }
         else if ((register_addr_hmi >= MODBUS_ADDR_CELL) && (register_addr_hmi < MODBUS_CELL_END))
         {
-            Modbus_Head_Copy((INT8U *)samkoon_txbuf, rec_data);
+            modbus_head((INT8U *)samkoon_txbuf, rec_data);
             Cell_Info_ReadOut(cell_temp_buf); // read all cell data
-            Small2Big(cell_temp_buf, register_count);
+            little2big(cell_temp_buf, register_count);
             Rtu_TxData_Get(register_addr_hmi, register_count, 0x4301, 3, (INT8U *)samkoon_txbuf, (INT8U *)cell_temp_buf);
-            tmp_crc = Count_CRC((INT8U *)samkoon_txbuf, 2 * register_count + 3);
-            Crc_Write((INT8U *)samkoon_txbuf, tmp_crc, register_count);
+            tmp_crc = crc_count((INT8U *)samkoon_txbuf, 2 * register_count + 3);
+            crc_write((INT8U *)samkoon_txbuf, tmp_crc, register_count);
             //			     samkoon_txbuf[register_count + 2] = tmp_crc;
             tx_p((INT8U *)samkoon_txbuf, (2 * register_count + 5));
         }
         else if ((register_addr_hmi >= MODBUS_ADDR_TEMP) && (register_addr_hmi < MODBUS_TEMP_END))
         {
-            Modbus_Head_Copy((INT8U *)samkoon_txbuf, rec_data);
+            modbus_head((INT8U *)samkoon_txbuf, rec_data);
             Temperature_ReadOut(cell_temp_buf); // read all temp data
-            Small2Big(cell_temp_buf, register_count);
+            little2big(cell_temp_buf, register_count);
             Rtu_TxData_Get(register_addr_hmi, register_count, 0x55c1, 3, (INT8U *)samkoon_txbuf, (INT8U *)cell_temp_buf);
-            tmp_crc = Count_CRC((INT8U *)samkoon_txbuf, 2 * register_count + 3);
-            Crc_Write((INT8U *)samkoon_txbuf, tmp_crc, register_count);
+            tmp_crc = crc_count((INT8U *)samkoon_txbuf, 2 * register_count + 3);
+            crc_write((INT8U *)samkoon_txbuf, tmp_crc, register_count);
             //			     samkoon_txbuf[register_count + 2] = tmp_crc;
             tx_p((INT8U *)samkoon_txbuf, (2 * register_count + 5));
         }
         else if ((register_addr_hmi >= MODBUS_ADDR_READ_KA) && (register_addr_hmi < MODBUS_KA_END))
         {
-            Modbus_Head_Copy((INT8U *)samkoon_txbuf, rec_data);
+            modbus_head((INT8U *)samkoon_txbuf, rec_data);
             // Relay_Bin_Readout(misc_info_buf);
-            Small2Big(misc_info_buf, register_count);
+            little2big(misc_info_buf, register_count);
             Rtu_TxData_Get(register_addr_hmi, register_count, 0x1001, 3, (INT8U *)samkoon_txbuf, (INT8U *)misc_info_buf);
-            tmp_crc = Count_CRC((INT8U *)samkoon_txbuf, 2 * register_count + 3);
-            Crc_Write((INT8U *)samkoon_txbuf, tmp_crc, register_count);
+            tmp_crc = crc_count((INT8U *)samkoon_txbuf, 2 * register_count + 3);
+            crc_write((INT8U *)samkoon_txbuf, tmp_crc, register_count);
             //			   	 samkoon_txbuf[register_count + 2] = tmp_crc;
             tx_p((INT8U *)samkoon_txbuf, (2 * register_count + 5));
         }
@@ -301,34 +317,7 @@ void samkoon_com_work(INT8U *rec_data, void (*tx_p)(const INT8U *buf, INT16U len
     }
 }
 
-// void fly_uart1_task(void)
-// {
-//     INT8U err = 0;
-//     //    INT8U test_buf[20] = {0};
-//     UartFrame_TypeDef *msg;
-//     INT16U             crc_check       = 0;
-//     INT16U             rcv_crc         = 0;
-//     txfun_p            Modbus_RTU_Send = Uart1_dma_Send_Data;
-//     while (1)
-//     {
-//         OSTimeDly(200);
-//         iwdg_feed(UART1_DOG);
-//         msg = (UartFrame_TypeDef *)OSMboxPend(uart1_mbox, 50, &err);
-//         if ((err == OS_ERR_NONE) && (msg->len >= 2))
-//         {
-//             crc_check = Count_CRC(msg->buf, 6);
-//             rcv_crc   = ((msg->buf[7] << 8) | msg->buf[6]);
-//             if (crc_check != rcv_crc)
-//             {
-//                 continue;
-//             }
-
-//             samkoon_com_work(&(msg->buf[0]), Modbus_RTU_Send);
-//         }
-//     }
-// }
-
-void fly_uart1_task(void)
+void modbus_slave_task(void)
 {
     INT8U              err          = 0;
     INT8U              test_buf[20] = {0};
@@ -336,12 +325,12 @@ void fly_uart1_task(void)
     while (1)
     {
         OSTimeDly(200);
-        iwdg_feed(UART1_DOG);
-        msg         = (UartFrame_TypeDef *)OSMboxPend(uart1_mbox, 50, &err);
+        iwdg_feed(UART3_DOG);
+        msg         = (UartFrame_TypeDef *)OSMboxPend(uart3_mbox, 50, &err);
         test_buf[0] = 0x01;
         test_buf[1] = 0xBB;
         test_buf[2] = 0xAA;
-        Uart1_dma_Send_Data(test_buf, 3);
+        uart3_dma_send(test_buf, 3);
         if ((err == OS_ERR_NONE) && (msg->len >= 2))
         {
             if ((msg->buf[0] == 0x00) && (msg->buf[1] == 0xAA) && (msg->buf[2] == 0xBB))
@@ -349,13 +338,13 @@ void fly_uart1_task(void)
                 test_buf[0] = 0x00;
                 test_buf[1] = 0xBB;
                 test_buf[2] = 0xAA;
-                Uart1_dma_Send_Data(test_buf, 3);
+                uart3_dma_send(test_buf, 3);
             }
         }
     }
 }
 
-void fly_uart3_task(void)
+void modbus_master_task(void)
 {
     INT8U              err          = 0;
     INT8U              test_buf[20] = {0};
@@ -363,21 +352,20 @@ void fly_uart3_task(void)
     while (1)
     {
         OSTimeDly(200);
-        iwdg_feed(UART3_DOG);
-        msg = (UartFrame_TypeDef *)OSMboxPend(uart3_mbox, 50, &err);
+        iwdg_feed(UART1_DOG);
+        msg = (UartFrame_TypeDef *)OSMboxPend(uart1_mbox, 50, &err);
         // test_buf[0] = 0x01;
-        // test_buf[1] = 0xBB;
-        // test_buf[2] = 0xAA;
-        // Uart3_dma_Send_Data(test_buf, 3);
+        // test_buf[1] = 0x00;
+        // test_buf[2] = 0x00;
+        // uart1_dma_send(test_buf, 3);
         if ((err == OS_ERR_NONE) && (msg->len >= 2))
         {
-            if ((msg->buf[0] == 0x00) && (msg->buf[1] == 0xAA) && (msg->buf[2] == 0xBB))
+            if ((msg->buf[0] == 0x00) && (msg->buf[1] == 0x00) && (msg->buf[2] == 0x00))
             {
-                test_buf[0] = 0x00;
-                test_buf[1] = 0xBB;
-                test_buf[2] = 0xAA;
-                // Uart3_Send_Data(test_buf, 3);
-                Uart3_dma_Send_Data(test_buf, 3);
+                // test_buf[0] = 0x00;
+                // test_buf[1] = 0xBB;
+                // test_buf[2] = 0xAA;
+                // uart1_dma_send(test_buf, 3);
             }
         }
     }

+ 6 - 5
User/app/fly_uart/fly_uart.h → User/app/modbus/modbus.h

@@ -1,12 +1,13 @@
-#ifndef __BMS_UART_H
-#define __BMS_UART_H
+#ifndef __MODBUS_H
+#define __MODBUS_H
 
 // #include "bms_config.h"
 #include "fly_param.h"
 // #include "bmu_param.h"
 #include "includes.h"
 #include "iwdg.h"
-#include "uart.h"
+#include "uart1.h"
+#include "uart3.h"
 
 typedef unsigned char  MODBUS_INT8U;
 typedef signed char    MODBUS_INT8S;
@@ -63,7 +64,7 @@ typedef void (*txfun_p)(const INT8U *buf, INT16U len);
 #define MODBUS_KA_END               0x1009
 #define MODBUS_ADDR_RACK_REMOTE_END 0xE7
 
-void fly_uart1_task(void);
-void fly_uart3_task(void);
+void modbus_master_task(void);
+void modbus_slave_task(void);
 
 #endif

+ 68 - 0
User/app/net/net.c

@@ -0,0 +1,68 @@
+#include "net.h"
+
+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 net_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);
+    }
+}

+ 14 - 0
User/app/net/net.h

@@ -0,0 +1,14 @@
+#ifndef __NET_H
+#define __NET_H
+#include "fly_param.h"
+#include "includes.h"
+#include "interface.h"
+#include "lwip/api.h"
+#include "lwip/netif.h"
+#include "lwip/sockets.h"
+#include "netconf.h"
+
+#define TCP_PORT 8080
+
+void net_task(void);
+#endif

+ 6 - 0
User/app/queue/queue.h

@@ -10,6 +10,12 @@ typedef struct
     INT32U can_id;
 } CanData_TypeDef;
 
+typedef struct
+{
+    INT8U buf[128];
+    INT8U len;
+} UartFrame_TypeDef;
+
 typedef struct
 {
     CanData_TypeDef CanBuf[MAX_QSIZE];

+ 1 - 1
User/lwipopts.h → User/config/lwip/lwipopts.h

@@ -114,7 +114,7 @@
 
 #define LWIP_SO_RCVTIMEO 1 /* set to 1 to enable receive timeout for sockets/netconns and \
                               SO_RCVTIMEO processing */
-
+#define LWIP_NETIF_LINK_CALLBACK 1
 /* Lwip debug options */
 #define LWIP_DEBUG 0
 

+ 63 - 27
User/netconf.c → User/config/lwip/netconf.c

@@ -9,13 +9,14 @@
     2016-08-15, V1.0.0, firmware for GD32F4xx
 */
 
+#include "netconf.h"
+#include "ethernetif.h"
+#include "lwip/dhcp.h"
 #include "lwip/mem.h"
 #include "lwip/memp.h"
-#include "lwip/dhcp.h"
-#include "ethernetif.h"
-#include "main.h"
-#include "netconf.h"
+#include "lwip/netif.h"
 #include "lwip/tcpip.h"
+#include "main.h"
 #include <stdio.h>
 
 #define MAX_DHCP_TRIES 4
@@ -30,11 +31,34 @@ typedef enum
 
 #ifdef USE_DHCP
 dhcp_state_enum dhcp_state = DHCP_START;
-ip4_addr_t ip_address = {0};
+ip4_addr_t      ip_address = {0};
 #endif /* USE_DHCP */
 
-struct netif xnetif;
+struct netif gnetif;
+/**
+ * @brief  Link callback function, this function is called on change of link status.
+ * @param  The network interface
+ * @retval None
+ */
+void ETH_link_callback(struct netif *netif)
+{
+    if (netif_is_link_up(netif))
+    {
+        /* When the netif is fully configured this function must be called.*/
+        netif_set_up(&gnetif);
+    }
+    else
+    {
+        ETH_Stop();
+#ifdef USE_DHCP
+        DHCP_state = DHCP_LINK_DOWN;
+        dhcp_stop(netif);
+#endif /* USE_DHCP */
 
+        /*  When the netif link is down this function must be called.*/
+        netif_set_down(&gnetif);
+    }
+}
 /*!
     \brief      initializes the LwIP stack
     \param[in]  none
@@ -52,9 +76,9 @@ void lwip_stack_init(void)
 
     /* IP address setting */
 #ifdef USE_DHCP
-    ipaddr.addr = 0;
+    ipaddr.addr  = 0;
     netmask.addr = 0;
-    gw.addr = 0;
+    gw.addr      = 0;
 #else
     IP4_ADDR(&ipaddr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
     IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
@@ -62,25 +86,37 @@ void lwip_stack_init(void)
 
 #endif /* USE_DHCP */
 
-    /* - netif_add(struct netif *netif, struct ip_addr *ipaddr,
-              struct ip_addr *netmask, struct ip_addr *gw,
-              void *state, err_t (* init)(struct netif *netif),
-              err_t (* input)(struct pbuf *p, struct netif *netif))
+    /* - netif_add(struct netif *netif,
+                  struct ip_addr *ipaddr,
+                  struct ip_addr *netmask,
+                  struct ip_addr *gw,
+                  void *state,
+                  err_t (* init)(struct netif *netif),
+                  err_t (* input)(struct pbuf *p,
+                  struct netif *netif))
 
-     Adds your network interface to the netif_list. Allocate a struct
-    netif and pass a pointer to this structure as the first argument.
-    Give pointers to cleared ip_addr structures when using DHCP,
-    or fill them with sane numbers otherwise. The state pointer may be NULL.
+      将您的网络接口添加到 netif_list。
+      分配结构netif,并将指向该结构的指针作为第一个参数传递。
+      使用DHCP时,请提供指向已清除的ip_addr结构的指针,否则,请使用健全的数字填充它们。
+      状态指针可以为NULL。
 
-    The init function pointer must point to a initialization function for
-    your ethernet netif interface. The following code illustrates it's use. */
-    netif_add(&xnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
+      初始化函数指针必须指向您的以太网netif接口的初始化函数。 以下代码说明了它的用法。*/
+    netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
 
-    /* registers the default network interface */
-    netif_set_default(&xnetif);
+    /* 注册默认网络接口.*/
+    netif_set_default(&gnetif);
 
-    /* when the netif is fully configured this function must be called */
-    netif_set_up(&xnetif);
+    if (EthStatus == (ETH_INIT_FLAG | ETH_LINK_FLAG))
+    {
+        gnetif.flags |= NETIF_FLAG_LINK_UP;
+        /* 配置完成网卡后启动网卡*/
+        netif_set_up(&gnetif);
+    }
+    else
+    {
+        netif_set_down(&gnetif);
+    }
+    netif_set_link_callback(&gnetif, ETH_link_callback);
 }
 
 #ifdef USE_DHCP
@@ -92,9 +128,9 @@ void lwip_stack_init(void)
 */
 void dhcp_task(void *pvParameters)
 {
-    ip4_addr_t ipaddr;
-    ip4_addr_t netmask;
-    ip4_addr_t gw;
+    ip4_addr_t   ipaddr;
+    ip4_addr_t   netmask;
+    ip4_addr_t   gw;
     struct dhcp *dhcp_client;
 
     dhcp_client = netif_dhcp_data(&xnetif);
@@ -106,7 +142,7 @@ void dhcp_task(void *pvParameters)
         case DHCP_START:
             dhcp_start(&xnetif);
             ip_address.addr = 0;
-            dhcp_state = DHCP_WAIT_ADDRESS;
+            dhcp_state      = DHCP_WAIT_ADDRESS;
             break;
 
         case DHCP_WAIT_ADDRESS:

+ 1 - 1
User/netconf.h → User/config/lwip/netconf.h

@@ -40,7 +40,7 @@ OF SUCH DAMAGE.
 
 //#define USE_DHCP       1 /* enable DHCP, if disabled static address is used */
 
-#define USE_ENET_INTERRUPT
+// #define USE_ETH_INTERRUPT
 //#define TIMEOUT_CHECK_USE_LWIP
 /* MAC address: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */
 #define MAC_ADDR0 2

+ 0 - 0
User/UCOS-CONFIG/app_hooks.c → User/config/ucos/app_hooks.c


+ 0 - 0
User/UCOS-CONFIG/cpu_cfg.h → User/config/ucos/cpu_cfg.h


+ 0 - 0
User/UCOS-CONFIG/includes.h → User/config/ucos/includes.h


+ 0 - 0
User/UCOS-CONFIG/lib_cfg.h → User/config/ucos/lib_cfg.h


+ 0 - 0
User/UCOS-CONFIG/os_cfg.h → User/config/ucos/os_cfg.h


+ 41 - 4
User/main.c

@@ -104,7 +104,7 @@ void init_task(void *pvParameters)
                     (void *)0,
                     (INT16U)OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR | OS_TASK_OPT_SAVE_FP);
 
-    OSTaskCreateExt((void (*)(void *))fly_uart1_task,
+    OSTaskCreateExt((void (*)(void *))modbus_master_task,
                     (void *)0,
                     (OS_STK *)&uart1_task_stk[UART1_TASK_STK_SIZE - 1],
                     (INT8U)UART1_TASK_PRIO,
@@ -114,7 +114,8 @@ void init_task(void *pvParameters)
                     (void *)0,
                     (INT16U)OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR | OS_TASK_OPT_SAVE_FP);
 
-    OSTaskCreateExt((void (*)(void *))fly_uart3_task,
+    /* 485 modbus´Ó»úģʽ */
+    OSTaskCreateExt((void (*)(void *))modbus_slave_task,
                     (void *)0,
                     (OS_STK *)&uart3_task_stk[UART3_TASK_STK_SIZE - 1],
                     (INT8U)UART3_TASK_PRIO,
@@ -124,7 +125,7 @@ void init_task(void *pvParameters)
                     (void *)0,
                     (INT16U)OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR | OS_TASK_OPT_SAVE_FP);
 
-    OSTaskCreateExt((void (*)(void *))dm9161_task,
+    OSTaskCreateExt((void (*)(void *))net_task,
                     (void *)0,
                     (OS_STK *)&net_task_stk[NET_TASK_STK_SIZE - 1],
                     (INT8U)NET_PRIO,
@@ -137,11 +138,47 @@ void init_task(void *pvParameters)
 
 void misc_task(void)
 {
+    INT8U  cnt      = 0;
+    INT16U save_cnt = 0;
     while (1)
     {
         iwdg_feed(MISC_DOG);
-        // LED_RUN_TOGGLE;
         OSTimeDly(500);
         LED3_RUN_TOGGLE;
+        //     if ((cnt % 10 == 0) && (cnt != 0))
+        //     {
+        //         continue;
+        //     }
+        //     if ((cnt % 25 == 0) && (cnt != 0))
+        //     {
+        //         LED3_RUN_TOGGLE;
+        //     }
+
+        //     if ((cnt % 15 == 0) && (cnt != 0))
+        //     {
+        //         continue;
+        //     }
+        //     if (cnt == 10)
+        //     {
+        //         continue;
+        //     }
+        //     if (cnt >= 50)
+        //     {
+        //         cnt = 0;
+        //     }
+        //     else
+        //     {
+        //         cnt++;
+        //     }
+
+        //     if (save_cnt >= 1500)
+        //     {
+        //         printf(1111);
+        //         save_cnt = 0;
+        //     }
+        //     else
+        //     {
+        //         save_cnt++;
+        //     }
     };
 }

+ 3 - 2
User/main.h

@@ -1,12 +1,13 @@
 #ifndef __MAIN_H
 #define __MAIN_H
 
-#include "dm9161.h"
 #include "fly_param.h"
-#include "fly_uart.h"
 #include "includes.h"
 #include "interface.h"
 #include "led.h"
+#include "modbus.h"
+#include "net.h"
+#include "stm32f4x7_phy.h"
 #include "stm32f4xx.h"
 #include <stdio.h>
 

+ 4 - 3
User/stm32f4x7_eth_conf.h

@@ -43,9 +43,10 @@ extern "C"
 /* Uncomment the line below when using time stamping and/or IPv4 checksum offload */
 #define USE_ENHANCED_DMA_DESCRIPTORS
 
-    /* Uncomment the line below if you want to use user defined Delay function
-       (for precise timing), otherwise default _eth_delay_ function defined within
-       the Ethernet driver is used (less precise timing) */
+/* Uncomment the line below if you want to use user defined Delay function
+   (for precise timing), otherwise default _eth_delay_ function defined within
+   the Ethernet driver is used (less precise timing) */
+#define USE_Delay
 
 #ifdef USE_Delay
     //  #include "main.h"              /* Header file where the Delay function prototype is exported */

+ 2 - 2
User/stm32f4xx_it.c

@@ -169,7 +169,7 @@ void DebugMon_Handler(void)
  * @}
  */
 
-#ifdef USE_ENET_INTERRUPT
+#ifdef USE_ETH_INTERRUPT
 /*!
     \brief      this function handles ethernet interrupt request
     \param[in]  none
@@ -193,6 +193,6 @@ void ETH_IRQHandler(void)
 
     OSIntExit();
 }
-#endif /* USE_ENET_INTERRUPT */
+#endif /* USE_ETH_INTERRUPT */
 
 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

+ 6 - 5
platformio.ini

@@ -16,6 +16,7 @@ build_flags =
   -IUser
   ; -IUser/bsp
   -IUser/bsp/can
+  -IUser/bsp/eth
   -IUser/bsp/interface
   -IUser/bsp/iwdg
   -IUser/bsp/timer
@@ -23,9 +24,10 @@ build_flags =
   -IUser/app
   -IUser/app/queue
   -IUser/app/led
-  -IUser/app/dm9161
-  -IUser/app/fly_uart
-  -IUser/UCOS-CONFIG
+  -IUser/app/modbus
+  -IUser/app/net
+  -IUser/config/lwip
+  -IUser/config/ucos
   -ILibraries/CMSIS/Include
   -ILibraries/CMSIS/Device/ST/STM32F4xx/Include
   -ILibraries/STM32F4xx_StdPeriph_Driver/inc
@@ -54,7 +56,7 @@ build_unflags =
 
 src_filter = +<User/> 
             +<Project/GCC/> 
-            -<User/app/dm9000>
+            -<User/app/dm9k>
             +<Libraries/STM32F4xx_StdPeriph_Driver/src/>
             -<Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_qspi.c>
             -<Libraries/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmpi2c.c>
@@ -84,7 +86,6 @@ src_filter = +<User/>
             -<MiddleWare/uCOS_II/uCOS-II/Ports/ARM-Cortex-M3>
             -<MiddleWare/uCOS_II/uCOS-II/Ports/ARM-Cortex-M4/Generic/IAR>
             -<MiddleWare/uCOS_II/uCOS-II/Ports/ARM-Cortex-M4/Generic/RealView>
-            
 debug_tool = stlink
 upload_protocol = stlink
 ; upload_protocol = custom