2 コミット 510d154e36 ... bcedf176a5

作者 SHA1 メッセージ 日付
  wulianwei bcedf176a5 Merge branch 'master' of http://192.168.100.32:3000/wulianwei/NIM_DSP6748 1 ヶ月 前
  wulianwei c98c0ddfab edit: 修复tcp server 返回数据问题 1 ヶ月 前

+ 10 - 3
code/device/console.c

@@ -56,11 +56,18 @@ unsigned char UARTConsoleGetc(void)
     return ((unsigned char) UARTCharGet(UART_CONSOLE_BASE));
 }
 
-void ConsolePutsPure(unsigned char *pHex, unsigned int numBytesToWrite)
+void ConsolePutsHexStr(unsigned char *source, unsigned int numBytesToWrite)
 {
-    while(numBytesToWrite--)
+    unsigned char highByte, lowByte;
+    short i = 0;
+    for (i = 0; i < numBytesToWrite; i++)
     {
-        UARTConsolePutc(*(pHex++));
+        highByte = ((source[i] >> 4)&0x0f) + 0x30;
+        lowByte = (source[i] & 0x0f) + 0x30;
+        highByte = highByte>0x39? highByte+0x07 : highByte;
+        lowByte = lowByte>0x39? lowByte+0x07 : lowByte;
+        UARTConsolePutc(highByte);
+        UARTConsolePutc(lowByte);
     }
 }
 /**

+ 1 - 1
code/device/console.h

@@ -31,7 +31,7 @@ typedef struct uart_recv_st
 
 void ConsoleDeviceInit(void);
 
-void ConsolePutsPure(unsigned char *pHex, unsigned int numBytesToWrite);
+void ConsolePutsHexStr(unsigned char *source, unsigned int numBytesToWrite);
 
 unsigned int ConsolePuts(char *pTxBuffer, int numBytesToWrite);
 

+ 48 - 26
code/device/lwip.c

@@ -16,19 +16,17 @@
 
 #define MAX_SIZE                          4096
 
-#define SEND_SIZE                          5*1024
-
-static unsigned char send_data[SEND_SIZE];
-
 
 static err_t tcp_accept_handle(void *arg, struct tcp_pcb *pcb, err_t err);
 static err_t tcp_recv_handle(void *arg, struct tcp_pcb *pcb, struct pbuf *p,
                              err_t err);
 static void tcp_close_conn(struct tcp_pcb *pcb);
 err_t tcp_send_data(struct tcp_pcb *pcb, struct pbuf *p);
-err_t usb_send_data(struct pbuf *p);
+err_t queue_send_data(struct pbuf *p);
 
 static struct udp_pcb *gui_udp_pcb;
+static struct tcp_pcb *tcp_server_pcb ;
+static struct tcp_pcb *cur_conn = NULL; // 当前连接
 
 void lwip_device_init(void)
 {
@@ -62,27 +60,41 @@ void lwip_device_init(void)
 /**
  * tcp 服务初始化
  */
-void lwip_tcp_init(int port)
+err_t lwip_tcp_init(int port)
 {
-    struct tcp_pcb *pcb;
+    err_t err;
+    //struct tcp_pcb *pcb;
 
-    pcb = tcp_new();
-    tcp_bind(pcb, IP_ADDR_ANY, port);
-    pcb = tcp_listen(pcb);
+    tcp_server_pcb  = tcp_new();
+    if (tcp_server_pcb  == NULL) {
+       ConsolePuts("tcp new 空间不足", -1);
+       return ERR_MEM; // 内存不足
+    }
+    err = tcp_bind(tcp_server_pcb , IP_ADDR_ANY, port);
+    if (err != ERR_OK) {
+       ConsolePuts("tcp 绑定断开失败", -1);
+       return err;
+    }
+    tcp_server_pcb  = tcp_listen(tcp_server_pcb );
+    if(tcp_server_pcb == NULL)
+    {
+        ConsolePuts("tcp 监听失败", -1);
+    }
     /* initialize callback arg and accept callback */
-    tcp_arg(pcb, pcb);
-    tcp_accept(pcb, tcp_accept_handle);
+    tcp_arg(tcp_server_pcb , NULL);
+    tcp_accept(tcp_server_pcb , tcp_accept_handle);
+    return ERR_OK;
 }
 
+
+
 /**
  * 链接监听函数.
  */
 static err_t tcp_accept_handle(void *arg, struct tcp_pcb *pcb, err_t err)
 {
     LWIP_UNUSED_ARG(err);
-
-    /* Decrease the listen backlog counter */
-    tcp_accepted((struct tcp_pcb_listen*)arg);
+    LWIP_UNUSED_ARG(arg);
 
     tcp_setprio(pcb, TCP_PRIO_MAX);
     /* Set up the various callback functions */
@@ -100,6 +112,8 @@ static err_t tcp_accept_handle(void *arg, struct tcp_pcb *pcb, err_t err)
 static err_t tcp_recv_handle(void *arg, struct tcp_pcb *pcb, struct pbuf *p,
                              err_t err)
 {
+    LWIP_UNUSED_ARG(arg);
+    LWIP_UNUSED_ARG(err);
     err_t err_send;
 
     if (p != NULL)
@@ -118,7 +132,9 @@ static err_t tcp_recv_handle(void *arg, struct tcp_pcb *pcb, struct pbuf *p,
         tcp_close_conn(pcb);
         return ERR_OK;
     }
-    err_send = usb_send_data(p);
+    cur_conn = pcb;
+    //tcp_send_data(pcb ,p);
+    err_send = queue_send_data(p);
     return err_send;
 }
 
@@ -158,9 +174,9 @@ err_t tcp_send_data(struct tcp_pcb *pcb, struct pbuf *p)
         len = p->len;
         for (i = 0, j = 0; i < len; i++, j++, cnt++)
         {
+            if(cnt>=MAX_SIZE) break;
             mydata[cnt] = data[j];
         }
-        //ConsolePuts(data, len);
         p = p->next;
 
     }
@@ -177,10 +193,16 @@ err_t tcp_send_data(struct tcp_pcb *pcb, struct pbuf *p)
     return err;
 }
 
+void lwip_tcp_send_gui(unsigned char *data, int len)
+{
+    tcp_write(cur_conn, data, len , TCP_WRITE_FLAG_COPY);
+    tcp_output(cur_conn);
+}
+
 /**
  * 发送数据到USB
  */
-err_t usb_send_data(struct pbuf *p)
+err_t queue_send_data(struct pbuf *p)
 {
     struct pbuf *temp = p;
     unsigned int len = 0;
@@ -188,6 +210,8 @@ err_t usb_send_data(struct pbuf *p)
     {
         char* data = (char*) p->payload;
         len = p->len;
+        ConsolePuts("\r\ntcp rec data:\r\n", -1);
+        ConsolePutsHexStr(data, len);
         enqueueFromEthernet(data, len);
         p = p->next;
     }
@@ -252,13 +276,12 @@ void lwip_udp_send_gui(unsigned char *data, int len)
  */
 void lwip_udp_send_gui_signal(float *data, int flen, char channelFlag)
 {
-    unsigned char head[7]={'T','R',0x1,0x31};
+    unsigned char head[8]={'T','R',1,3,1,1};
     int clen = sizeof(float)*flen;
     int head_len = sizeof(head);
     int total_len = head_len+1+clen;
-    head[4] = total_len;
-    head[5] = total_len>>8;
-    head[6] = 1;
+    head[6] = total_len>>8;
+    head[7] = total_len;
     unsigned char* fdata = malloc(total_len);
     memcpy(fdata,&head,head_len);
     fdata[head_len] = channelFlag;
@@ -277,13 +300,12 @@ void lwip_udp_send_gui_signal(float *data, int flen, char channelFlag)
  */
 void lwip_udp_send_gui_signal2(float *data1,float *data2,float *data3,float *data4, int flen, char channelFlag)
 {
-    unsigned char head[7]={'T','R',0x1,0x31};
+    unsigned char head[8]={'T','R',1,3,1,1};
     int clen = sizeof(float)*flen;
     int head_len = sizeof(head);
     int total_len = head_len+1+clen;
-    head[4] = total_len;
-    head[5] = total_len>>8;
-    head[6] = 1;
+    head[6] = total_len>>8;
+    head[7] = total_len;
     unsigned char* fdata = malloc(total_len);
     memcpy(fdata,&head,head_len);
     fdata[head_len] = channelFlag;

+ 3 - 1
code/device/lwip.h

@@ -14,7 +14,9 @@
 
 void lwip_device_init(void);
 
-void lwip_tcp_init(int port);
+err_t lwip_tcp_init(int port);
+
+void lwip_tcp_send_gui(unsigned char *data, int len);
 
 void lwip_udp_connect(struct udp_pcb *pcb,unsigned int ip,int port);
 

+ 7 - 7
code/hlx/tr_protocol.c

@@ -142,13 +142,13 @@ void ProtocolPackage(trProtocol* protocol) {
 
 // ą¨ÎÄ˝âÎö
 uint8_t ProtocolCheckCRC(uint8_t* message, uint16_t messageLength) {
-    uint16_t crc = CalCRC16(message, messageLength - 2);
-    if ((crc >> 8 & 0xFF) != message[messageLength - 2]) {
-        return 0;
-    }
-    if ((crc & 0xFF) != message[messageLength - 1]) {
-        return 0;
-    }
+//    uint16_t crc = CalCRC16(message, messageLength - 2);
+//    if ((crc >> 8 & 0xFF) != message[messageLength - 2]) {
+//        return 0;
+//    }
+//    if ((crc & 0xFF) != message[messageLength - 1]) {
+//        return 0;
+//    }
     return 1;
 }
 // ťńČĄ°ćąžşĹ

+ 32 - 25
code/hlx/tr_queue_handler.c

@@ -12,10 +12,12 @@
 #include "uartStdio.h"
 #include "usbhspecific.h"
 #include <stdlib.h>
+#include "tr_protocol.h"
 
 extern struct udp_pcb *upcb;
 void handleSign(unsigned char *s, unsigned short sl);
 void handleSign2(unsigned char *s, unsigned short sl);
+void handleGUI(unsigned char *s, unsigned short sl);
 /**
  * 数据转发业务处理,需要进行转发的数据在这个里面处理
  * params   da     目的地址
@@ -53,12 +55,14 @@ void dataNotForwardedHandle(unsigned char *s, unsigned short sl,
     if (sa == F4_ADDRESS)
     {
         // 处理从F4发来的数据
-        handleSign(s + 7, sl - 7);
+        handleSign(s + 8, sl - 8);
 
     }
     else if (sa == GUI_ADDRESS)
     {
         // 处理从gui发来的数据
+        handleGUI(s,sl);
+
     }
     else if (sa == UART_ADDRESS)
     {
@@ -85,12 +89,11 @@ void dataNotForwardedHandle(unsigned char *s, unsigned short sl,
  * 处理采集信号
  */
 
-#define COUNT_SIZE 50
+#define COUNT_SIZE 75
 #define CHANNEL_COUNT 4 //采集通道总数
 void handleSign(unsigned char *s, unsigned short sl)
 {
-    ConsolePuts("aaa1\n", -1);
-    // ConsolePutsPure(s,sl);
+    ConsolePuts("handle signal data from usb\n", -1);
 
     struct Frame
     {
@@ -171,24 +174,7 @@ void handleSign(unsigned char *s, unsigned short sl)
                 fdgui[m*CHANNEL_COUNT+2] = fd3[m];
                 fdgui[m*CHANNEL_COUNT+3] = fd4[m];
             }
-            const int STEP = 100;
-            int pos = 0;
-            int left = 0;
-            do
-            {
-                int left = COUNT_SIZE - pos;
-                if (left <= STEP)
-                {
-                    lwip_udp_send_gui_signal(fdgui + pos*CHANNEL_COUNT, left*CHANNEL_COUNT, fm->sign);
-                }
-                else
-                {
-                    lwip_udp_send_gui_signal(fdgui + pos*CHANNEL_COUNT, STEP*CHANNEL_COUNT, fm->sign);
-                    pos += STEP;
-                }
-            }
-            while (left > STEP);
-
+            lwip_udp_send_gui_signal(fdgui,  COUNT_SIZE*CHANNEL_COUNT, fm->sign);
         }
     }
 
@@ -201,9 +187,7 @@ void handleSign(unsigned char *s, unsigned short sl)
 
 void handleSign2(unsigned char *s, unsigned short sl)
 {
-    ConsolePuts("aaa1\n", -1);
-    // ConsolePutsPure(s,sl);
-
+    ConsolePuts("===handle signal 2 data from usb\n", -1);
     struct Frame
     {
         unsigned char sign;
@@ -302,3 +286,26 @@ void handleSign2(unsigned char *s, unsigned short sl)
 
 }
 
+void handleGUI(unsigned char *s, unsigned short sl)
+{
+    ConsolePuts("\n===handleGUI from gui\n", -1);
+    ConsolePutsHexStr(s, sl);
+    unsigned char check = 0;
+    int a = ProtocolGetOption(s,sl,99,&check);
+    if(a)
+    {
+        unsigned char udpAdd[8] = {0};
+        trProtocol verPro;
+        ProtocolInit(&verPro);
+        ProtocolSetDestAddress(&verPro,3);
+        ProtocolSetSourceAddress(&verPro,1);
+        ProtocolSetType(&verPro,2);
+        //ProtocolSetOption(&verPro,98,sizeof(udpAdd),udpAdd);
+        ProtocolSetOptionChar(&verPro, 100, 1);
+        ProtocolPackage(&verPro);
+        ConsolePuts("\n===handleGUI to gui version\n", -1);
+        ConsolePutsHexStr(verPro.message, verPro.length);
+        lwip_tcp_send_gui(verPro.message,verPro.length);
+    }
+}
+

+ 1 - 1
code/lib/lwip/src/core/ipv4/ip_frag.c

@@ -755,7 +755,7 @@ ip_frag(struct pbuf *p, struct netif *netif, struct ip_addr *dest)
     if (header != NULL) {
       pbuf_chain(header, rambuf);
       netif->output(netif, header, dest);
-      delay_us(100);//保证DMA发送完数据包
+      delay_us(300);//保证DMA发送完数据包
       IPFRAG_STATS_INC(ip_frag.xmit);
       snmp_inc_ipfragcreates();
       pbuf_free(header);

+ 0 - 35
main.c

@@ -11,48 +11,13 @@
 
 void DeviceInit();
 
-char* dd;
 int main()
 {
     DeviceInit();
     ConsolePuts("\r\n ============NIM DSP START===========.\r\n", -1);
     lwip_tcp_init(TCP_SERVER_PORT); //开启本设备TCP服务器
-
-    // 主循环
-    float t = 0;
-#define ALGORITHM_SIZE  1000
     for (;;)
     {
-        if(t>2){
-            t=0;
-        }
-        t += 0.1;
-            // 发送数据
-            float ch1[ALGORITHM_SIZE];
-            float ch2[ALGORITHM_SIZE];
-            int i=0;
-            for(i = 0;i<ALGORITHM_SIZE;i++)
-            {
-                ch1[i] = (t*i);
-            }
-            emg_denoised(ch1, ALGORITHM_SIZE, 1000, ch2);
-
-            // 发送帧尾
-            lwip_udp_send_vofa2(ch1,ch2,sizeof(ch1)/sizeof(float));
-//        ConsoleRecvWait(10);
-//        if (ConsoleRecvFlag() == REV_OK)
-//        {
-//            unsigned char *data = ConsoleRecvData();
-//            lwip_udp_send_gui(sdata, sizeof(sdata));
-//            ConsolePrintf("data %s,len %d,%d",sdata,sizeof(sdata),sizeof(struct pbuf));
-//            ConsoleRecvClear();
-//        }
-            short s1[ALGORITHM_SIZE] = {0};
-            for(i = 0;i<ALGORITHM_SIZE;i++)
-            {
-                s1[i] = i;
-            }
-            //lwip_udp_send_gui((unsigned char*)s1,sizeof(s1));
         HlxMain();
     }
 }

+ 1 - 1
nim_config.h

@@ -36,7 +36,7 @@
 //#define STATIC_IP_ADDRESS               0xC0A801C8  //192.168.1.200
 
 //#define UI_IP_ADDRESS               0xC0A801C9  //192.168.1.201
-#define UI_IP_ADDRESS               0xC0A80164  //192.168.1.101
+#define UI_IP_ADDRESS               0xC0A80169  //192.168.1.101
 
 #define UI_UDP_PORT                 8000