255 lines
7.5 KiB
C
255 lines
7.5 KiB
C
/* USER CODE BEGIN Header */
|
|
/**
|
|
******************************************************************************
|
|
* File Name : freertos.c
|
|
* Description : Code for freertos applications
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* Copyright (c) 2025 STMicroelectronics.
|
|
* All rights reserved.
|
|
*
|
|
* This software is licensed under terms that can be found in the LICENSE file
|
|
* in the root directory of this software component.
|
|
* @file freertos.c
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
/* USER CODE END Header */
|
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "FreeRTOS.h"
|
|
#include "task.h"
|
|
#include "main.h"
|
|
#include "cmsis_os.h"
|
|
|
|
/* Private includes ----------------------------------------------------------*/
|
|
/* USER CODE BEGIN Includes */
|
|
#include "dac161s997.h"
|
|
#include "ch438q.h"
|
|
#include "tcpserverc.h"
|
|
#include "lan8742.h"
|
|
#include "lwip.h"
|
|
#include "lwip/tcp.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 */
|
|
float current_buff[16] = {0};
|
|
/* USER CODE END Variables */
|
|
osThreadId lwip_taskHandle;
|
|
osThreadId dac_taskHandle;
|
|
osThreadId ch438_taskHandle;
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
/* USER CODE BEGIN FunctionPrototypes */
|
|
extern struct netif gnetif;
|
|
extern ETH_HandleTypeDef heth;
|
|
extern struct tcp_pcb *server_pcb_hart[TCP_MAX];
|
|
/* USER CODE END FunctionPrototypes */
|
|
|
|
void start_lwip_task(void const *argument);
|
|
void start_dac_task(void const *argument);
|
|
void start_ch438_task(void const *argument);
|
|
|
|
extern void MX_LWIP_Init(void);
|
|
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
|
|
|
|
/* GetIdleTaskMemory prototype (linked to static allocation support) */
|
|
void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize);
|
|
|
|
/* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
|
|
static StaticTask_t xIdleTaskTCBBuffer;
|
|
static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];
|
|
|
|
void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize)
|
|
{
|
|
*ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
|
|
*ppxIdleTaskStackBuffer = &xIdleStack[0];
|
|
*pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
|
|
/* place for user code */
|
|
}
|
|
/* USER CODE END GET_IDLE_TASK_MEMORY */
|
|
|
|
/**
|
|
* @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 */
|
|
|
|
/* USER CODE BEGIN RTOS_QUEUES */
|
|
/* add queues, ... */
|
|
/* USER CODE END RTOS_QUEUES */
|
|
|
|
/* Create the thread(s) */
|
|
/* definition and creation of lwip_task */
|
|
osThreadDef(lwip_task, start_lwip_task, osPriorityNormal, 0, 12040);
|
|
lwip_taskHandle = osThreadCreate(osThread(lwip_task), NULL);
|
|
|
|
/* definition and creation of dac_task */
|
|
osThreadDef(dac_task, start_dac_task, osPriorityNormal, 0, 512);
|
|
dac_taskHandle = osThreadCreate(osThread(dac_task), NULL);
|
|
|
|
/* definition and creation of ch438_task */
|
|
osThreadDef(ch438_task, start_ch438_task, osPriorityNormal, 0, 512);
|
|
ch438_taskHandle = osThreadCreate(osThread(ch438_task), NULL);
|
|
|
|
/* USER CODE BEGIN RTOS_THREADS */
|
|
/* add threads, ... */
|
|
/* USER CODE END RTOS_THREADS */
|
|
}
|
|
|
|
/* USER CODE BEGIN Header_start_lwip_task */
|
|
/**
|
|
* @brief Function implementing the lwip_task thread.
|
|
* @param argument: Not used
|
|
* @retval None
|
|
*/
|
|
/* USER CODE END Header_start_lwip_task */
|
|
void start_lwip_task(void const *argument)
|
|
{
|
|
/* init code for LWIP */
|
|
MX_LWIP_Init();
|
|
/* USER CODE BEGIN start_lwip_task */
|
|
/* Infinite loop */
|
|
tcp_echo_init();
|
|
for (;;)
|
|
{
|
|
// osThreadTerminate(NULL);
|
|
// uint32_t phyreg = 0;
|
|
// uint8_t tcp_hart_num = 0;
|
|
// HAL_ETH_ReadPHYRegister(&heth, 0x00, PHY_BSR, &phyreg);
|
|
// if (((phyreg >> 2) & 0x1) != 0x1) // PHY_BSR寄存器的第2位表示PHY是否连接 PHY_LINKED_STATUS
|
|
// {
|
|
// /* When the netif link is down this function must be called */
|
|
|
|
// netif_set_link_down(&gnetif);
|
|
// netif_set_down(&gnetif); // 热插拔下线时调用
|
|
// for (tcp_hart_num = 0; tcp_hart_num < TCP_MAX; tcp_hart_num++)
|
|
// {
|
|
// if (tcp_echo_flags[tcp_hart_num] == 1)
|
|
// {
|
|
// tcp_abort(server_pcb_hart[tcp_hart_num]); // 热插拔下线时调用
|
|
// tcp_echo_flags[tcp_hart_num] = 0;
|
|
// }
|
|
// }
|
|
// }
|
|
// else
|
|
// {
|
|
// /* When the netif is fully configured this function must be called */
|
|
// netif_set_link_up(&gnetif);
|
|
// netif_set_up(&gnetif); // 热插拔上线时调用
|
|
// }
|
|
ch438_interrupt_handler_data_ch438_chip_1();
|
|
ch438_interrupt_handler_data_ch438_chip_2();
|
|
|
|
vTaskDelay(10);
|
|
}
|
|
/* USER CODE END start_lwip_task */
|
|
}
|
|
|
|
/* USER CODE BEGIN Header_start_dac_task */
|
|
/**
|
|
* @brief Function implementing the dac_task thread.
|
|
* @param argument: Not used
|
|
* @retval None
|
|
*/
|
|
/* USER CODE END Header_start_dac_task */
|
|
void start_dac_task(void const *argument)
|
|
{
|
|
/* USER CODE BEGIN start_dac_task */
|
|
/* Infinite loop */
|
|
dac161s997_init();
|
|
for (;;)
|
|
{
|
|
uint8_t ch = 0;
|
|
for (ch = 0; ch < DAC_MAX_NUM; ch++)
|
|
{
|
|
dac161s997_output(ch, current_buff[ch]);
|
|
}
|
|
vTaskDelay(100);
|
|
}
|
|
/* USER CODE END start_dac_task */
|
|
}
|
|
|
|
/* USER CODE BEGIN Header_start_ch438_task */
|
|
/**
|
|
* @brief Function implementing the ch438_task thread.
|
|
* @param argument: Not used
|
|
* @retval None
|
|
*/
|
|
/* USER CODE END Header_start_ch438_task */
|
|
void start_ch438_task(void const *argument)
|
|
{
|
|
/* USER CODE BEGIN start_ch438_task */
|
|
/* Infinite loop */
|
|
ch438_init();
|
|
for (;;)
|
|
{
|
|
// osThreadTerminate(NULL);
|
|
uint32_t phyreg = 0;
|
|
uint8_t tcp_hart_num = 0;
|
|
HAL_ETH_ReadPHYRegister(&heth, 0x00, PHY_BSR, &phyreg);
|
|
if (((phyreg >> 2) & 0x1) != 0x1) // PHY_BSR寄存器的第2位表示PHY是否连接 PHY_LINKED_STATUS
|
|
{
|
|
/* When the netif link is down this function must be called */
|
|
|
|
netif_set_link_down(&gnetif);
|
|
netif_set_down(&gnetif); // 热插拔下线时调用
|
|
for (tcp_hart_num = 0; tcp_hart_num < TCP_MAX; tcp_hart_num++)
|
|
{
|
|
if (tcp_echo_flags[tcp_hart_num] == 1)
|
|
{
|
|
tcp_abort(server_pcb_hart[tcp_hart_num]); // 热插拔下线时调用
|
|
tcp_echo_flags[tcp_hart_num] = 0;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
/* When the netif is fully configured this function must be called */
|
|
netif_set_link_up(&gnetif);
|
|
netif_set_up(&gnetif); // 热插拔上线时调用
|
|
}
|
|
vTaskDelay(500);
|
|
}
|
|
/* USER CODE END start_ch438_task */
|
|
}
|
|
|
|
/* Private application code --------------------------------------------------*/
|
|
/* USER CODE BEGIN Application */
|
|
|
|
/* USER CODE END Application */
|