212 lines
5.8 KiB
C
212 lines
5.8 KiB
C
#include "ch32v30x.h"
|
|
#include "string.h"
|
|
#include "eth_driver.h"
|
|
#include "dvp.h"
|
|
#include "qdx_port.h"
|
|
#include "qdx_preprocess.h"
|
|
#include "qdx_tcp_logic.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];
|
|
|
|
TcpTxBuffer_t g_TxNetBuffer_A = {
|
|
.pBuffer = g_TxNetBuffer_A_Mem,
|
|
.TotalCapacity = MAX_TCP_PAYLOAD_SIZE,
|
|
.HeadOffset = 64,
|
|
.ValidPayloadLen = 0
|
|
};
|
|
TcpTxBuffer_t g_TxNetBuffer_B = {
|
|
.pBuffer = g_TxNetBuffer_B_Mem,
|
|
.TotalCapacity = MAX_TCP_PAYLOAD_SIZE,
|
|
.HeadOffset = 64,
|
|
.ValidPayloadLen = 0
|
|
};
|
|
|
|
|
|
|
|
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
|
|
}
|
|
|
|
void OnDetectionResult(uint8_t frameType, uint8_t isOK)
|
|
{
|
|
// If not OK, trigger GPIO NG cylinder
|
|
if (!isOK) {
|
|
// trigger GPIO action
|
|
}
|
|
}
|
|
|
|
#define KEEPALIVE_ENABLE 1
|
|
|
|
u8 MACAddr[6];
|
|
u8 IPAddr[4] = {192, 168, 1, 10};
|
|
u8 GWIPAddr[4] = {192, 168, 1, 1};
|
|
u8 IPMask[4] = {255, 255, 255, 0};
|
|
u8 DESIP[4] = {192, 168, 1, 50};
|
|
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)
|
|
{
|
|
if (iError == WCHNET_ERR_SUCCESS) return;
|
|
printf("Error: %02X\r\n", (u16)iError);
|
|
}
|
|
|
|
void TIM2_Init(void)
|
|
{
|
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure = {0};
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
|
TIM_TimeBaseStructure.TIM_Period = WCHNETTIMERPERIOD * 1000 - 1;
|
|
TIM_TimeBaseStructure.TIM_Prescaler = SystemCoreClock / 1000000 - 1;
|
|
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
|
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
|
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
|
|
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
|
|
TIM_Cmd(TIM2, ENABLE);
|
|
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
|
|
NVIC_EnableIRQ(TIM2_IRQn);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void WCHNET_HandleSockInt(u8 socketid, u8 intstat)
|
|
{
|
|
u8 i;
|
|
|
|
if (intstat & SINT_STAT_RECV)
|
|
{
|
|
/* DataLoopback removed, handled in qdx_tcp_logic */
|
|
}
|
|
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);
|
|
}
|
|
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");
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
|
|
void WCHNET_HandleGlobalInt(void)
|
|
{
|
|
u8 intstat;
|
|
u16 i;
|
|
u8 socketint;
|
|
intstat = WCHNET_GetGlobalInt();
|
|
if (intstat & GINT_STAT_UNREACH) printf("GINT_STAT_UNREACH\r\n");
|
|
if (intstat & GINT_STAT_IP_CONFLI) printf("GINT_STAT_IP_CONFLI\r\n");
|
|
if (intstat & GINT_STAT_PHY_CHANGE) {
|
|
if (WCHNET_GetPHYStatus() & PHY_Linked_Status)
|
|
printf("PHY Link Success\r\n");
|
|
}
|
|
if (intstat & GINT_STAT_SOCKET) {
|
|
for (i = 0; i < WCHNET_MAX_SOCKET_NUM; i++) {
|
|
socketint = WCHNET_GetSocketInt(i);
|
|
if (socketint) WCHNET_HandleSockInt(i, socketint);
|
|
}
|
|
}
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
u8 i;
|
|
SystemCoreClockUpdate();
|
|
Delay_Init();
|
|
USART_Printf_Init(115200);
|
|
printf("TCPClient Test\r\nSystemClk:%d\r\n", SystemCoreClock);
|
|
printf("net version:%x\n", WCHNET_GetVer());
|
|
if (WCHNET_LIB_VER != WCHNET_GetVer()) printf("version error.\n");
|
|
|
|
WCHNET_GetMacAddr(MACAddr);
|
|
printf("mac addr:");
|
|
for (i = 0; i < 6; i++) printf("%x ", MACAddr[i]);
|
|
printf("\n");
|
|
|
|
DVP_Init();
|
|
TIM2_Init();
|
|
|
|
i = ETH_LibInit(IPAddr, GWIPAddr, IPMask, MACAddr);
|
|
mStopIfError(i);
|
|
if (i == WCHNET_ERR_SUCCESS) printf("WCHNET_LibInit Success\r\n");
|
|
|
|
#if KEEPALIVE_ENABLE
|
|
{
|
|
struct _KEEP_CFG cfg = {20000, 15000, 9};
|
|
WCHNET_ConfigKeepLive(&cfg);
|
|
}
|
|
#endif
|
|
|
|
memset(socket, 0xff, WCHNET_MAX_SOCKET_NUM);
|
|
|
|
|
|
Preprocess_Init(SENSOR_WIDTH, SENSOR_HEIGHT);
|
|
TcpLogic_Init(MACAddr, NULL);
|
|
TcpLogic_RegisterConfigCallback(OnConfigUpdate);
|
|
TcpLogic_RegisterDetectionCallback(OnDetectionResult);
|
|
TcpLogic_Start();
|
|
|
|
|
|
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();
|
|
}
|
|
|
|
}
|