/********************************** (C) COPYRIGHT ******************************* * File Name : main.c * Author : WCH * Version : V1.0.0 * Date : 2020/02/19 * Description : Main program body. *******************************************************************************/ #include "delay.h" #include "sys.h" #include "usart.h" #include "string.h" #include "stdio.h" #include "CH392INC.h" #include "CH392.H" #define Query392Interrupt() PAin(0) /*CH392_OP_INTERFACE_MODE可以为1-2 1:串口连接方式 2:spi口连接方式 */ #define CH392_OP_INTERFACE_MODE 1 #if (CH392_OP_INTERFACE_MODE == 1) /* SEL = 0, TX = 1*/ #include "CH392UART.C" #elif (CH392_OP_INTERFACE_MODE == 2) /* SEL = 0, TX = 1*/ #include "CH392SPI_HW.C" #else #error "Please Select Correct Communication Interface " #endif /* 包含命令文件 */ #include "CH392CMD.C" /* 常用变量定义 */ u8 MyBuffer[4096]; /* 数据缓冲区 */ struct _SOCK_INF SockInf; /* 保存Socket信息 */ struct _CH392_SYS CH392Inf; /* 保存CH392信息 */ /* CH392相关定义 */ const u8 CH392IPAddr[4] = {192,168,1,10}; /* CH392IP地址 */ const u8 CH392GWIPAddr[4] = {192,168,1,1}; /* CH392网关 */ const u8 CH392IPMask[4] = {255,255,255,0}; /* CH392子网掩码 */ /* socket 相关定义*/ const u8 Socket0DesIP[4] = {255,255,255,255}; /* Socket 0目的IP地址 */ const u16 Socket0SourPort = 5000; /* Socket 0源端口 */ const u16 Socket0DesPort = 1000; /* Socket 0目的端口 */ /********************************************************************************** * Function Name : mStopIfError * Description : 调试使用,显示错误代码,并停机 * Input : iError * Output : None * Return : None **********************************************************************************/ void mStopIfError(u8 iError) { if (iError == CMD_ERR_SUCCESS) return; printf("Error: %02X\r\n", (u16)iError); while ( 1 ) { delay_ms(200); delay_ms(200); } } /********************************************************************************** * Function Name : InitCH392InfParam * Description : 初始化CH392Inf参数 * Input : None * Output : None * Return : None **********************************************************************************/ void InitCH392InfParam(void) { memset(&CH392Inf,0,sizeof(CH392Inf)); /* 将CH392Inf全部清零*/ memcpy(CH392Inf.IPAddr,CH392IPAddr,sizeof(CH392IPAddr)); /* 将IP地址写入CH392Inf中 */ memcpy(CH392Inf.GWIPAddr,CH392GWIPAddr,sizeof(CH392GWIPAddr)); /* 将网关IP地址写入CH392Inf中 */ memcpy(CH392Inf.MASKAddr,CH392IPMask,sizeof(CH392IPMask)); /* 将子网掩码写入CH392Inf中 */ } /********************************************************************************** * Function Name : InitSocketParam * Description : 初始化socket * Input : None * Output : None * Return : None **********************************************************************************/ void InitSocketParam(void) { memset(&SockInf,0,sizeof(SockInf)); /* 将SockInf[0]全部清零*/ memcpy(SockInf.IPAddr,Socket0DesIP,sizeof(Socket0DesIP)); /* 将目的IP地址写入 */ SockInf.DesPort = Socket0DesPort; /* 目的端口 */ SockInf.SourPort = Socket0SourPort; /* 源端口 */ SockInf.ProtoType = PROTO_TYPE_UDP; /* TCP模式 */ } /********************************************************************************** * Function Name : CH392SocketInitOpen * Description : 配置CH392 socket 参数,初始化并打开socket * Input : None * Output : None * Return : None **********************************************************************************/ void CH392SocketInitOpen(void) { u8 i; CH392SetSocketDesIP(0,SockInf.IPAddr); /* 设置socket 0目标IP地址 */ CH392SetSocketProtType(0,SockInf.ProtoType); /* 设置socket 0协议类型 */ CH392SetSocketDesPort(0,SockInf.DesPort); /* 设置socket 0目的端口 */ CH392SetSocketSourPort(0,SockInf.SourPort); /* 设置socket 0源端口 */ i = CH392OpenSocket(0); /* 打开socket 0 */ mStopIfError(i); /* 检查是否成功 */ } /********************************************************************************** * Function Name : CH392SocketInterrupt * Description : CH392 socket 中断,在全局中断中被调用 * Input : sockindex * Output : None * Return : None **********************************************************************************/ void CH392SocketInterrupt(u8 sockindex) { u8 sock_int_socket; u16 len; u16 i; sock_int_socket = CH392GetSocketInt(sockindex); /* 获取socket 的中断状态 */ if(sock_int_socket & SINT_STAT_SENBUF_FREE) /* 发送缓冲区空闲,可以继续写入要发送的数据 */ { } if(sock_int_socket & SINT_STAT_SEND_OK) /* 发送完成中断 */ { } if(sock_int_socket & SINT_STAT_RECV) /* 接收中断 */ { len = CH392GetRecvLength(sockindex); /* 获取当前缓冲区内数据长度 */ printf("receive len = %d\r\n",len); CH392GetRecvData(sockindex,len,MyBuffer); for(i=0;i