STM32F7xx Keil5 RTX RL-TCPnet DP83822移植

使用之前RTX工程模板

RTE中RL-TCPnet配置

STM32F7xx Keil5 RTX RL-TCPnet DP83822移植_第1张图片
STM32F7xx Keil5 RTX RL-TCPnet DP83822移植_第2张图片
STM32F7xx Keil5 RTX RL-TCPnet DP83822移植_第3张图片
暂时全部默认配置,DHCP已打开
STM32F7xx Keil5 RTX RL-TCPnet DP83822移植_第4张图片
修改RTE_Device.h ETH配置

STM32F7xx Keil5 RTX RL-TCPnet DP83822移植_第5张图片
修改DP83822驱动

STM32F7xx Keil5 RTX RL-TCPnet DP83822移植_第6张图片
去掉文件只读属性,之后需要修改,添加到工程

STM32F7xx Keil5 RTX RL-TCPnet DP83822移植_第7张图片
修改DP83822 ID

STM32F7xx Keil5 RTX RL-TCPnet DP83822移植_第8张图片
RTE创建tcp server例程,参考该例程,进行修改
STM32F7xx Keil5 RTX RL-TCPnet DP83822移植_第9张图片
STM32F7xx Keil5 RTX RL-TCPnet DP83822移植_第10张图片
STM32F7xx Keil5 RTX RL-TCPnet DP83822移植_第11张图片
修改后的TCP_Socket_server.c

/*------------------------------------------------------------------------------
 * MDK Middleware - Component ::Network:Service
 * Copyright (c) 2004-2019 Arm Limited (or its affiliates). All rights reserved.
 *------------------------------------------------------------------------------
 * Name:    TCP_Socket_Server.c
 * Purpose: TCP Socket Server Code Template
 * Rev.:    V7.0.0
 *----------------------------------------------------------------------------*/
//! [code_TCP_Socket_Server]
#include "rl_net.h"
#include "cmsis_os2.h"
#include "stm32f7xx_hal.h"

#define PORT_NUM 2000
int32_t tcp_sock;

// Notify the user application about TCP socket events.
uint32_t tcp_cb_server (int32_t socket, netTCP_Event event,
                        const NET_ADDR *addr, const uint8_t *buf, uint32_t len) {
 
  switch (event) {
    case netTCP_EventConnect:
      // Connect request received
      /* Example
      if (addr->addr_type == NET_ADDR_IP4) {
        // IPv4 client
        if (addr->addr[0] == 192  &&
            addr->addr[1] == 168  &&
            addr->addr[2] == 0    &&
            addr->addr[3] == 1) {
          // Accept connection from client at 192.168.0.1
          return (1);
        }
      }
      else {
        // IPv6 client
        const uint8_t ip6_addr[NET_ADDR_IP6_LEN] = { 
                         0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                         0x1c, 0x30, 0x6c, 0xff, 0xfe, 0xa2, 0x45, 0x5e };
        if (memcmp (addr->addr, ip6_addr, NET_ADDR_IP6_LEN) == 0) {
          // Accept connection from client at [fe80::1c30:6cff:fea2:455e]
          return (1);
        }
      }
      // Deny connection.
      return (0);
      */
      return (1);
 
    case netTCP_EventEstablished:
      // Connection established
      break;
 
    case netTCP_EventClosed:
      // Connection was properly closed
      break;
 
    case netTCP_EventAborted:
      // Connection is for some reason aborted
      break;
 
    case netTCP_EventACK:
      // Previously sent data acknowledged
      break;
 
    case netTCP_EventData:
      // Data received
      /* Example
      if ((buf[0] == 0x01) && (len == 2)) {
        // Switch LEDs on and off
        // LED_out (buf[1]);
      }
      */
      break;
  }
  return (0);
}
 
// Allocate and initialize the socket.
__NO_RETURN static void tcp_server_task (void *argument)
{
  netStatus ret;
  netTCP_State net_state;
  int32_t iCount;
  uint8_t *sendbuf;
  uint32_t maxlen;
  
  netInitialize();

  tcp_sock = netTCP_GetSocket (tcp_cb_server);
  
  if (tcp_sock > 0)
  {
    ret = netTCP_Listen (tcp_sock, PORT_NUM);
    netTCP_SetOption (tcp_sock, netTCP_OptionKeepAlive, 1);
  }
  
  osDelay(10000);
  
  for (;;)
  {
    net_state = netTCP_GetState(tcp_sock);
    if(net_state == netTCP_StateESTABLISHED)
    {
      iCount = 8;
      do
      {
        if(netTCP_SendReady(tcp_sock) == true)
        {
          maxlen  = netTCP_GetMaxSegmentSize (tcp_sock);
          
          iCount -= maxlen;
          if(iCount < 0)
            maxlen = iCount + maxlen;
          
          sendbuf = netTCP_GetBuffer (maxlen);
          sendbuf[0] = '1';
          sendbuf[1] = '2';
          sendbuf[2] = '3';
          sendbuf[3] = '4';
          sendbuf[4] = '5';
          sendbuf[5] = '6';
          sendbuf[6] = '7';
          sendbuf[7] = '8';
          netTCP_Send (tcp_sock, sendbuf, maxlen);
        }
      } while (iCount > 0);
    }
    
    osDelay(1000);
  }
}

const osThreadAttr_t tcp_server_task_Attr = 
{
  .name = "tcp_server_task",
  .attr_bits = osThreadDetached, 
  .priority = osPriorityNormal2,
  .stack_size = 2048,
};

void tcp_server_task_init(void)
{
  GPIO_InitTypeDef  GPIO_InitStruct = {0};
  // ERST PG9
  __HAL_RCC_GPIOG_CLK_ENABLE();
  // ETH_XTAL_CLK PA8, MCO1, 25MHz
  HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_1);

  GPIO_InitStruct.Pin = GPIO_PIN_9;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
  HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);

  // reset PHY
  HAL_GPIO_WritePin(GPIOG, GPIO_PIN_9, GPIO_PIN_RESET);
  osDelay(10);
  HAL_GPIO_WritePin(GPIOG, GPIO_PIN_9, GPIO_PIN_SET);
  osDelay(200);
  
  osThreadNew(tcp_server_task, NULL, &tcp_server_task_Attr);
}
//! [code_TCP_Socket_Server]

测试

ping

STM32F7xx Keil5 RTX RL-TCPnet DP83822移植_第12张图片

Tcp client

STM32F7xx Keil5 RTX RL-TCPnet DP83822移植_第13张图片

问题

可以PING通,但client无法连接,后来发现是netInitialize以及server相关的测试代码需要放在同一个task里

参考

【STM32F407】第11章 RL-TCPnet V7.X之TCP服务器
https://blog.csdn.net/Simon223/article/details/108494688

你可能感兴趣的:(RL-TCPnet,RTX,stm32,单片机,嵌入式硬件)