STM32cubeMX将STM32F767+LAN8720+LwIP+FreeRTOS的以太网实现

时间:2024-03-26 07:13:54

通过STM32cubeMX将STM32F767+LAN8720+LwIP+FreeRTOS的以太网实现

本文使用了正点原子的阿波罗开发板,接下来我将粗略的对STM32F767通过STM32cubeMX进行以太网协议配置。

具体步骤

STM32cubeMX将STM32F767+LAN8720+LwIP+FreeRTOS的以太网实现
1.首先使能ETH,模式选择RMII;
STM32cubeMX将STM32F767+LAN8720+LwIP+FreeRTOS的以太网实现
2.使能LWIP和FREERTOS;
STM32cubeMX将STM32F767+LAN8720+LwIP+FreeRTOS的以太网实现
因为阿波罗的开发板PHY芯片的ETH_DXT1和ETH_DXT0用的是PG13,PG14引脚,和默认的不同,所以这里改一下
STM32cubeMX将STM32F767+LAN8720+LwIP+FreeRTOS的以太网实现
3.配置时钟,在这里我放上我自己的时钟配置图,只要不标红就行,我用的是25MHz外部晶振作时钟源 ;
STM32cubeMX将STM32F767+LAN8720+LwIP+FreeRTOS的以太网实现
STM32cubeMX将STM32F767+LAN8720+LwIP+FreeRTOS的以太网实现
4.配置ETH,由于阿波罗开发板的LAN8720芯片的PHYAD0引脚悬空,故PHY Address设置为0;
STM32cubeMX将STM32F767+LAN8720+LwIP+FreeRTOS的以太网实现
5.由于我用的STM32cubeMX的版本是将LAN8742A作为默认芯片,所以我们这里不做修改,只用将内部参数改为LAN8720参数即可;
STM32cubeMX将STM32F767+LAN8720+LwIP+FreeRTOS的以太网实现
6.配置LWIP,本人这里不使用DHCP获取IP地址等信息,并且我使用的是TCP,所以未对UDP使能,设置如图;
STM32cubeMX将STM32F767+LAN8720+LwIP+FreeRTOS的以太网实现
7.配置FREERTOS,我在这里加了两个线程备用

STM32cubeMX将STM32F767+LAN8720+LwIP+FreeRTOS的以太网实现
8.按下生成按钮,生成项目,生成时需要选择编译软件为自己用的软件,本人使用的是V5;

由于阿波罗开发板的PHY芯片的RESET引脚是依靠PCF8574的P7位控制,所以我们要在项目中引入PCF8574的代码,以下代码可以在阿波罗资料盘中找到
STM32cubeMX将STM32F767+LAN8720+LwIP+FreeRTOS的以太网实现

打开main.c里的加上#include “pcf8574.h”,并在初始化部分将PCF8574初始化

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f7xx_hal.h"
#include "cmsis_os.h"
#include "lwip.h"
#include "gpio.h"

/* USER CODE BEGIN Includes */
#include "pcf8574.h"
/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void MX_FREERTOS_Init(void);

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
 
/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  *
  * @retval None
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */
	PCF8574_Init();
  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Call init function for freertos objects (in freertos.c) */
  MX_FREERTOS_Init();

  /* Start scheduler */
  osKernelStart();
  
  /* We should never get here as control is now taken by the scheduler */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

  }
  /* USER CODE END 3 */

}

打开freertos.c里的加上
#include “gpio.h”
#include “sys.h”
#include “pcf8574.h”
#include “tcp_echoserver.h”,并在主线程将PCF8574初的P7管脚使能
PCF8574_WriteBit(ETH_RESET_IO,0);代码如下

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */     
#include "gpio.h"
#include "sys.h"
#include "pcf8574.h"
#include "tcp_echoserver.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
osThreadId defaultTaskHandle;
osThreadId myTaskLEDHandle;
osThreadId myTaskTCPHandle;

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
 extern void tcp_echoserver_init(void);
/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void const * argument);
void LEDTask(void const * argument);
void TCPTask(void const * argument);

extern void MX_LWIP_Init(void);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* Create the thread(s) */
  /* definition and creation of defaultTask */
  osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
  defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);

  /* definition and creation of myTaskLED */
  osThreadDef(myTaskLED, LEDTask, osPriorityIdle, 0, 128);
  myTaskLEDHandle = osThreadCreate(osThread(myTaskLED), NULL);

  /* definition and creation of myTaskTCP */
  osThreadDef(myTaskTCP, TCPTask, osPriorityIdle, 0, 1024);
  myTaskTCPHandle = osThreadCreate(osThread(myTaskTCP), NULL);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */
}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used 
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void const * argument)
{
  /* init code for LWIP */
  MX_LWIP_Init();
  /* USER CODE BEGIN StartDefaultTask */
	PCF8574_WriteBit(ETH_RESET_IO,0);       //Æô¶¯PHYоƬ
 
	Tcp_Server_Init();
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END StartDefaultTask */
}

/* USER CODE BEGIN Header_LEDTask */
/**
* @brief Function implementing the myTaskLED thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_LEDTask */
void LEDTask(void const * argument)
{
  /* USER CODE BEGIN LEDTask */
  /* Infinite loop */
  for(;;)
  {

     osDelay(1); 

  }
  /* USER CODE END LEDTask */
}

/* USER CODE BEGIN Header_TCPTask */
/**
* @brief Function implementing the myTaskTCP thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_TCPTask */
void TCPTask(void const * argument)
{
	
  /* USER CODE BEGIN TCPTask */
  /* Infinite loop */
  for(;;)
  {
	osDelay(1); 
  }
  /* USER CODE END TCPTask */
}

接下来我们的开发板连上电脑,保证电脑IP地址和开发板IP地址在同一网段后按Windows+R后输入cmd回车打开CMD,在界面输入ping xxx.xxx.xxx.xxx(xxx代表开发板IP地址),可以得到
STM32cubeMX将STM32F767+LAN8720+LwIP+FreeRTOS的以太网实现
这样开发板就ping通了,这个时候就可以引入服务器或是客户端的代码,实现对应功能。

相关文章