This commit is contained in:
2026-03-14 08:47:57 +08:00
parent 31191a4156
commit f940356c75
49 changed files with 27459 additions and 74 deletions
+151
View File
@@ -0,0 +1,151 @@
/*
FreeRTOS V202112.00
All rights reserved
VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
This file is part of the FreeRTOS distribution.
FreeRTOS is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License (version 2) as published by the
Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
***************************************************************************
>>! NOTE: The modification to the GPL is included to allow you to !<<
>>! distribute a combined work that includes FreeRTOS without being !<<
>>! obliged to provide the source code for proprietary components !<<
>>! outside of the FreeRTOS kernel. !<<
***************************************************************************
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. Full license text is available on the following
link: http://www.freertos.org/a00114.html
***************************************************************************
* *
* FreeRTOS provides completely free yet professionally developed, *
* robust, strictly quality controlled, supported, and cross *
* platform software that is more than just the market leader, it *
* is the industry's de facto standard. *
* *
* Help yourself get started quickly while simultaneously helping *
* to support the FreeRTOS project by purchasing a FreeRTOS *
* tutorial book, reference manual, or both: *
* http://www.FreeRTOS.org/Documentation *
* *
***************************************************************************
http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
the FAQ page "My application does not run, what could be wrong?". Have you
defined configASSERT()?
http://www.FreeRTOS.org/support - In return for receiving this top quality
embedded software for free we request you assist our global community by
participating in the support forum.
http://www.FreeRTOS.org/training - Investing in training allows your team to
be as productive as possible as early as possible. Now you can receive
FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
Ltd, and the world's leading authority on the world's leading RTOS.
http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
including FreeRTOS+Trace - an indispensable productivity tool, a DOS
compatible FAT file system, and our tiny thread aware UDP/IP stack.
http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
licenses offer ticketed support, indemnification and commercial middleware.
http://www.SafeRTOS.com - High Integrity Systems also provide a safety
engineered and independently SIL3 certified version for use in safety and
mission critical applications that require provable dependability.
1 tab == 4 spaces!
*/
#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H
#include "debug.h"
/*-----------------------------------------------------------
* Application specific definitions.
*
* These definitions should be adjusted for your particular hardware and
* application requirements.
*
* THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
* FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
*
* See http://www.freertos.org/a00110.html.
*----------------------------------------------------------*/
/* See https://www.freertos.org/Using-FreeRTOS-on-RISC-V.html */
/* don't have MTIME */
#define configMTIME_BASE_ADDRESS ( 0 )
#define configMTIMECMP_BASE_ADDRESS ( 0 )
#define configUSE_PREEMPTION 1
#define configUSE_IDLE_HOOK 0
#define configUSE_TICK_HOOK 0
#define configCPU_CLOCK_HZ SystemCoreClock
#define configTICK_RATE_HZ ( ( TickType_t ) 500 )
#define configMAX_PRIORITIES ( 15 )
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 256 ) /* Can be as low as 60 but some of the demo tasks that use this constant require it to be higher. */
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 12 * 1024 ) )
#define configMAX_TASK_NAME_LEN ( 16 )
#define configUSE_TRACE_FACILITY 0
#define configUSE_16_BIT_TICKS 0
#define configIDLE_SHOULD_YIELD 0
#define configUSE_MUTEXES 1
#define configQUEUE_REGISTRY_SIZE 8
#define configCHECK_FOR_STACK_OVERFLOW 0
#define configUSE_RECURSIVE_MUTEXES 1
#define configUSE_MALLOC_FAILED_HOOK 0
#define configUSE_APPLICATION_TASK_TAG 0
#define configUSE_COUNTING_SEMAPHORES 1
#define configGENERATE_RUN_TIME_STATS 0
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 0
/* Co-routine definitions. */
#define configUSE_CO_ROUTINES 0
#define configMAX_CO_ROUTINE_PRIORITIES ( 2 )
/* Software timer definitions. */
#define configUSE_TIMERS 1
#define configTIMER_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
#define configTIMER_QUEUE_LENGTH 4
#define configTIMER_TASK_STACK_DEPTH ( configMINIMAL_STACK_SIZE )
/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function. */
#define INCLUDE_vTaskPrioritySet 1
#define INCLUDE_uxTaskPriorityGet 1
#define INCLUDE_vTaskDelete 1
#define INCLUDE_vTaskCleanUpResources 1
#define INCLUDE_vTaskSuspend 1
#define INCLUDE_vTaskDelayUntil 1
#define INCLUDE_vTaskDelay 1
#define INCLUDE_eTaskGetState 1
#define INCLUDE_xTimerPendFunctionCall 1
#define INCLUDE_xTaskAbortDelay 1
#define INCLUDE_xTaskGetHandle 1
#define INCLUDE_xSemaphoreGetMutexHolder 1
/* Normal assert() semantics without relying on the provision of an assert.h
header file. */
#define configASSERT( x ) if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); printf("err at line %d of file \"%s\". \r\n ",__LINE__,__FILE__); while(1); }
/* Map to the platform printf function. */
#define configPRINT_STRING( pcString ) printf( pcString )
#endif /* FREERTOS_CONFIG_H */
+15 -2
View File
@@ -11,6 +11,8 @@
*******************************************************************************/
#include "eth_driver.h"
#include "ch32v30x_it.h"
#include "FreeRTOS.h"
#include "task.h"
void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
@@ -89,8 +91,19 @@ void ETH_IRQHandler(void)
*/
void TIM2_IRQHandler(void)
{
sys_tick_ms += WCHNETTIMERPERIOD; /* 累加实际间隔毫秒数 */
WCHNET_TimeIsr(WCHNETTIMERPERIOD);
static uint8_t wchnet_divider = 0;
/* 2ms tick for FreeRTOS (configTICK_RATE_HZ = 500) */
sys_tick_ms += 2;
xPortSysTickHandler();
/* Software divider: call WCHNET_TimeIsr every 5 ticks = 10ms */
if (++wchnet_divider >= 5)
{
wchnet_divider = 0;
WCHNET_TimeIsr(WCHNETTIMERPERIOD);
}
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
+120 -65
View File
@@ -6,6 +6,10 @@
#include "qdx_preprocess.h"
#include "qdx_tcp_logic.h"
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
#define MAX_TCP_PAYLOAD_SIZE 10240
uint8_t g_TxNetBuffer_A_Mem[MAX_TCP_PAYLOAD_SIZE];
uint8_t g_TxNetBuffer_B_Mem[MAX_TCP_PAYLOAD_SIZE];
@@ -27,15 +31,45 @@ TcpTxBuffer_t g_TxNetBuffer_B = {
void OnConfigUpdate(const ConfigCommon_t *common, const Config2D_t *cfg2d, const Config1D_t *cfg1d)
{
// Update preprocessing thresholds
// Preprocess_Settings_Change(...); // Assuming this needs to be implemented or already exists
Preprocess_Settings_Change(cfg2d, cfg1d, common);
}
void OnDetectionResult(uint8_t frameType, uint8_t isOK)
/* NG output GPIO: PA8 push-pull, active-high when NG detected */
#define NG_GPIO_PORT GPIOA
#define NG_GPIO_PIN GPIO_Pin_8
#define NG_GPIO_CLK RCC_APB2Periph_GPIOA
#define NG_PULSE_MS 200 /* default NG pulse width */
static TimerHandle_t g_ng_timer;
static void ng_timer_callback(TimerHandle_t xTimer)
{
// If not OK, trigger GPIO NG cylinder
if (!isOK) {
// trigger GPIO action
(void)xTimer;
GPIO_ResetBits(NG_GPIO_PORT, NG_GPIO_PIN);
}
static void NG_GPIO_Init(void)
{
GPIO_InitTypeDef gpio = {0};
RCC_APB2PeriphClockCmd(NG_GPIO_CLK, ENABLE);
gpio.GPIO_Pin = NG_GPIO_PIN;
gpio.GPIO_Mode = GPIO_Mode_Out_PP;
gpio.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(NG_GPIO_PORT, &gpio);
GPIO_ResetBits(NG_GPIO_PORT, NG_GPIO_PIN);
g_ng_timer = xTimerCreate("ng", pdMS_TO_TICKS(NG_PULSE_MS),
pdFALSE, NULL, ng_timer_callback);
}
void OnDetectionResult(uint32_t frameNumber, uint8_t resultStatus)
{
(void)frameNumber;
/* resultStatus: 0 = NG, 1 = OK */
if (resultStatus == 0) {
GPIO_SetBits(NG_GPIO_PORT, NG_GPIO_PIN);
if (g_ng_timer)
xTimerStart(g_ng_timer, 0);
}
}
@@ -50,7 +84,6 @@ u16 desport = 5512;
u16 srcport = 5511;
u8 SocketId;
u8 socket[WCHNET_MAX_SOCKET_NUM];
u8 SocketRecvBuf[WCHNET_MAX_SOCKET_NUM][RECE_BUF_LEN];
void mStopIfError(u8 iError)
@@ -63,7 +96,8 @@ void TIM2_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure = {0};
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = WCHNETTIMERPERIOD * 1000 - 1;
/* 2ms period: matches configTICK_RATE_HZ = 500 */
TIM_TimeBaseStructure.TIM_Period = 2000 - 1;
TIM_TimeBaseStructure.TIM_Prescaler = SystemCoreClock / 1000000 - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
@@ -78,39 +112,34 @@ void TIM2_Init(void)
/* qdx_port.c notification hooks */
extern void qdx_port_sock_recv_notify(uint8_t sockid);
extern void qdx_port_sock_connect_notify(uint8_t sockid);
extern void qdx_port_sock_disconnect_notify(uint8_t sockid);
extern void qdx_port_init(void);
void WCHNET_HandleSockInt(u8 socketid, u8 intstat)
{
u8 i;
if (intstat & SINT_STAT_RECV)
{
/* DataLoopback removed, handled in qdx_tcp_logic */
qdx_port_sock_recv_notify(socketid);
}
if (intstat & SINT_STAT_CONNECT)
{
#if KEEPALIVE_ENABLE
WCHNET_SocketSetKeepLive(socketid, ENABLE);
#endif
WCHNET_ModifyRecvBuf(socketid, (u32)SocketRecvBuf[socketid], RECE_BUF_LEN);
for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) {
if (socket[i] == 0xff) { socket[i] = socketid; break; }
}
printf("TCP Connect Success, socket id: %d\r\n", socketid);
qdx_port_sock_connect_notify(socketid);
printf("TCP Connected, socket %d\r\n", socketid);
}
if (intstat & SINT_STAT_DISCONNECT)
{
for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) {
if (socket[i] == socketid) { socket[i] = 0xff; break; }
}
printf("TCP Disconnect\r\n");
qdx_port_sock_disconnect_notify(socketid);
printf("TCP Disconnected, socket %d\r\n", socketid);
}
if (intstat & SINT_STAT_TIM_OUT)
{
for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) {
if (socket[i] == socketid) { socket[i] = 0xff; break; }
}
printf("TCP Timeout\r\n");
TcpLogic_Init(MACAddr);
qdx_port_sock_disconnect_notify(socketid);
printf("TCP Timeout, socket %d\r\n", socketid);
}
}
@@ -134,6 +163,62 @@ void WCHNET_HandleGlobalInt(void)
}
}
/* ============================================================
* RTOS Task: WCHNET protocol stack driver (highest priority)
* ============================================================ */
static void task_wchnet_entry(void *pvParameters)
{
(void)pvParameters;
while (1)
{
WCHNET_MainTask();
if (WCHNET_QueryGlobalInt())
WCHNET_HandleGlobalInt();
vTaskDelay(pdMS_TO_TICKS(5));
}
}
/* ============================================================
* RTOS Task: Business logic (DVP + preprocess + send)
* ============================================================ */
static void task_business_entry(void *pvParameters)
{
(void)pvParameters;
static uint8_t use_buffer_A = 1;
while (1)
{
DVP_Task();
if (Frame_Ready_Flag)
{
Frame_Ready_Flag = 0;
RawImageBuffer_t raw_img;
raw_img.pData = (uint16_t *)FrameBuffer;
raw_img.Width = SENSOR_WIDTH;
raw_img.Height = SENSOR_HEIGHT;
raw_img.FrameNumber = Ready_Frame_Count;
if (Preprocess_CheckInternalTrigger2D(&raw_img) == 1)
{
PreprocessResult_t meta;
TcpTxBuffer_t *tx_buf = use_buffer_A ? &g_TxNetBuffer_A : &g_TxNetBuffer_B;
use_buffer_A = !use_buffer_A;
if (Preprocess_Execute(&raw_img, tx_buf, &meta) == 0)
{
TcpLogic_BuildAndSendTemperatureFrame(tx_buf, &meta, 1, 1);
}
}
}
else
{
vTaskDelay(pdMS_TO_TICKS(2));
}
}
}
int main(void)
{
u8 i;
@@ -151,6 +236,7 @@ int main(void)
DVP_Init();
TIM2_Init();
NG_GPIO_Init();
i = ETH_LibInit(IPAddr, GWIPAddr, IPMask, MACAddr);
mStopIfError(i);
@@ -163,49 +249,18 @@ int main(void)
}
#endif
memset(socket, 0xff, WCHNET_MAX_SOCKET_NUM);
qdx_port_init();
Preprocess_Init(SENSOR_WIDTH, SENSOR_HEIGHT);
TcpLogic_Init(MACAddr, NULL);
TcpLogic_RegisterConfigCallback(OnConfigUpdate);
TcpLogic_RegisterDetectionCallback(OnDetectionResult);
TcpLogic_Start();
xTaskCreate(task_wchnet_entry, "wchnet", 1024, NULL, 6, NULL);
xTaskCreate(task_business_entry, "business", 1024, NULL, 5, NULL);
vTaskStartScheduler();
while (1)
{
DVP_Task();
if (Frame_Ready_Flag)
{
Frame_Ready_Flag = 0;
RawImageBuffer_t raw_img;
raw_img.pData = (uint16_t *)FrameBuffer;
raw_img.Width = SENSOR_WIDTH;
raw_img.Height = SENSOR_HEIGHT;
raw_img.FrameNumber = Ready_Frame_Count;
if (Preprocess_CheckInternalTrigger2D(&raw_img) == 1)
{
PreprocessResult_t meta;
// Toggle between A and B
static uint8_t use_buffer_A = 1;
TcpTxBuffer_t *tx_buf = use_buffer_A ? &g_TxNetBuffer_A : &g_TxNetBuffer_B;
use_buffer_A = !use_buffer_A;
if (Preprocess_Execute(&raw_img, tx_buf, &meta) == 0)
{
TcpLogic_BuildAndSendTemperatureFrame(tx_buf, &meta, 1, 1);
}
}
}
WCHNET_MainTask();
if (WCHNET_QueryGlobalInt())
WCHNET_HandleGlobalInt();
}
/* Should never reach here */
while (1) {}
}
+1 -1
View File
@@ -24,7 +24,7 @@ extern "C" {
#define WCHNET_NUM_UDP 0 /* The number of UDP connections */
#define WCHNET_NUM_TCP 1 /* Number of TCP connections */
#define WCHNET_NUM_TCP 2 /* Number of TCP connections (control + data stream) */
#define WCHNET_NUM_TCP_LISTEN 0 /* Number of TCP listening */