Browse Source

change adc_cantool for Cherry.

yuchuli 2 months ago
parent
commit
165a7c8cbb

+ 1 - 0
src/driver/driver_can_nvidia_agx/basecan.h

@@ -14,6 +14,7 @@ class basecan_msg
     bool isRemote;
     unsigned char nLen;
     unsigned char data[64];
+    double frecvtime;
 };
 
 class basecan : public QThread

+ 14 - 1
src/driver/driver_can_nvidia_agx/canctrl.cpp

@@ -226,7 +226,20 @@ void canctrl::sharecanmsg(void *xpa, basecan_msg * pxmsg,int ncount,int nch)
         if(pxmsg[i].nLen>0) praw->set_data(pxmsg[i].data,pxmsg[i].nLen);
         praw->set_bext(pxmsg[i].isExtern);
         praw->set_bremote(pxmsg[i].isRemote);
-        praw->set_rectime(QDateTime::currentMSecsSinceEpoch());
+        if(pxmsg[i].frecvtime<0.001){
+            int64_t timenow = std::chrono::system_clock::now().time_since_epoch().count();
+            int64_t timesec = timenow/1e9;
+            int64_t timenano = timenow - timesec * 1e9;
+            double frecvtime = timenano;
+            frecvtime = frecvtime*1e-9;
+            frecvtime = frecvtime + timesec;
+            praw->set_rectime(frecvtime);
+ //       praw->set_rectime(QDateTime::currentMSecsSinceEpoch());
+        }
+        else
+        {
+            praw->set_rectime(pxmsg[i].frecvtime);
+        }
         praw->set_len(pxmsg[i].nLen);
 
     }

+ 49 - 9
src/driver/driver_can_nvidia_agx/nvcan.cpp

@@ -80,13 +80,15 @@ void nvcan::run()
     struct sockaddr_can addr;
     char ctrlmsg[CMSG_SPACE(sizeof(struct timeval) + 3*sizeof(struct timespec) + sizeof(__u32))];
     struct iovec iov;
-    struct msghdr msg;
+    struct msghdr xmsghdr;
     struct canfd_frame frame;
     int nbytes, i, maxdlen;
     struct ifreq ifr;
     struct timeval tv, last_tv;
     struct timeval timeout_config = { 0, 0 }, *timeout_current = 0;
 
+    struct cmsghdr *cmsg;
+
     mfault->SetFaultState(0,0,"Initializing.");
     
     for(i=0;i<currmax;i++)
@@ -113,6 +115,13 @@ void nvcan::run()
         //CANFD Support
         setsockopt(s[i], SOL_CAN_RAW, CAN_RAW_FD_FRAMES, &canfd_on, sizeof(canfd_on));
 
+        const int timestamp_on = 1;
+
+        if (setsockopt(s[i], SOL_SOCKET, SO_TIMESTAMP,
+                   &timestamp_on, sizeof(timestamp_on)) < 0) {
+            perror("setsockopt SO_TIMESTAMP");
+        }
+
         if (bind(s[i], (struct sockaddr *)&addr, sizeof(addr)) < 0) {
             mfault->SetFaultState(2,3,"bind error.");
             emit SIG_CANOPENSTATE(false,-3,"bind error");
@@ -126,10 +135,10 @@ void nvcan::run()
     emit SIG_CANOPENSTATE(true,0,"open can card successfully");
 
     iov.iov_base = &frame;
-    msg.msg_name = &addr;
-    msg.msg_iov = &iov;
-    msg.msg_iovlen = 1;
-    msg.msg_control = &ctrlmsg;
+    xmsghdr.msg_name = &addr;
+    xmsghdr.msg_iov = &iov;
+    xmsghdr.msg_iovlen = 1;
+    xmsghdr.msg_control = &ctrlmsg;
 
     qint64 nLastRecv = QDateTime::currentMSecsSinceEpoch();
     int nRecvState = 0; // 0 Have Data  1 No Data;
@@ -165,11 +174,11 @@ void nvcan::run()
                 nLastRecv = QDateTime::currentMSecsSinceEpoch();
                 /* these settings may be modified by recvmsg() */
                 iov.iov_len = sizeof(frame);
-                msg.msg_namelen = sizeof(addr);
-                msg.msg_controllen = sizeof(ctrlmsg);
-                msg.msg_flags = 0;
+                xmsghdr.msg_namelen = sizeof(addr);
+                xmsghdr.msg_controllen = sizeof(ctrlmsg);
+                xmsghdr.msg_flags = 0;
 
-                nbytes = recvmsg(s[i], &msg, 0);
+                nbytes = recvmsg(s[i], &xmsghdr, 0);
 
                 if (nbytes < 0) {
  //                   if ((errno == ENETDOWN) && !down_causes_exit) {
@@ -207,6 +216,37 @@ void nvcan::run()
                 msg.nLen = frame.len;
 //                if(msg.id == 0x1c2)qDebug("id = %08x",msg.id);
                 if((frame.len<=64)&&(frame.len>0))memcpy(msg.data,frame.data,frame.len);
+
+                msg.frecvtime = 0.0;
+                for (cmsg = CMSG_FIRSTHDR(&xmsghdr);
+                     cmsg && (cmsg->cmsg_level == SOL_SOCKET);
+                     cmsg = CMSG_NXTHDR(&xmsghdr,cmsg)) {
+                    if (cmsg->cmsg_type == SO_TIMESTAMP) {
+                        memcpy(&tv, CMSG_DATA(cmsg), sizeof(tv));
+                    } else if (cmsg->cmsg_type == SO_TIMESTAMPING) {
+
+                        struct timespec *stamp = (struct timespec *)CMSG_DATA(cmsg);
+
+                        /*
+                         * stamp[0] is the software timestamp
+                         * stamp[1] is deprecated
+                         * stamp[2] is the raw hardware timestamp
+                         * See chapter 2.1.2 Receive timestamps in
+                         * linux/Documentation/networking/timestamping.txt
+                         */
+                        tv.tv_sec = stamp[2].tv_sec;
+                        tv.tv_usec = stamp[2].tv_nsec/1000;
+
+                        msg.frecvtime = tv.tv_usec;
+                        msg.frecvtime = tv.tv_usec/((double)1e6);
+                        msg.frecvtime = tv.tv_sec + msg.frecvtime;
+                        std::cout<<" set frecvtime."<<std::endl;
+
+                    } else if (cmsg->cmsg_type == SO_RXQ_OVFL)
+                        std::cout<<" type is SO_RXQ_OVFL"<<std::endl;
+ //                       memcpy(&dropcnt[i], CMSG_DATA(cmsg), sizeof(__u32));
+                }
+
                 if(mMsgRecvBuf[i].size()<BUF_SIZE)
                 {
                     mMsgRecvBuf[i].push_back(msg);

+ 80 - 0
src/tool/adc_cantool/window/RawTxWindow/RawTxWindow.cpp

@@ -1038,11 +1038,91 @@ void RawTxWindow::fileSend(bool enable)
 }
 
 #ifdef CHECK_FOR_CHERRY
+
+unsigned char CRCCheck_SAEJ1850(unsigned char msg[], int len, unsigned char idleCrc)
+{
+    int i = 0;
+    int j = 0;
+    unsigned char crc8;
+    unsigned char poly = 0x1D;
+    crc8 = idleCrc;
+    for (i = 0; i<len; i++) {
+        crc8 ^= msg[i];
+        for (j = 0; j<8; j++) {
+            if (crc8 & 0x80) {
+                crc8 = (crc8 << 1) ^ poly;
+            }
+            else {
+                crc8 <<= 1;
+            }
+        }
+    }
+    //   crc8 ^= 0xFF;
+    crc8 ^= 0x00;//crc8 ^= 0xFF;
+    return crc8;
+}
+
+void fillcrc(unsigned short dataid,unsigned char * pdata){
+    unsigned char xdata[9];
+    memcpy(xdata,&dataid,2);
+    memcpy(xdata+2,pdata+1,7);
+    unsigned char xcrc = CRCCheck_SAEJ1850(xdata,9,0x0);
+    pdata[0] = xcrc;
+}
 void RawTxWindow::setcheck(CanMessage &xmsg)
 {
+    static int eps1count = 0;
+    static int oneboxcount = 0;
+    static int vcucount = 0;
+    if(xmsg.getId() == 0x195)
+    {
+        unsigned char pdata[24];
+        unsigned int i;
+        for(i=0;i<24;i++)pdata[i] = xmsg.getByte(i);
+
+        pdata[1] = eps1count;
+        pdata[9] = eps1count;
+
+
+        fillcrc(0x0004,pdata);
+        fillcrc(0x0005,pdata+8);
+        for(i=0;i<24;i++)xmsg.setDataAt(i,pdata[i]);
+
+        eps1count++;
+        if(eps1count > 14)eps1count = 0;
+    }
+
     if(xmsg.getId() == 0x159)
     {
+        unsigned char pdata[24];
+        unsigned int i;
+        for(i=0;i<24;i++)pdata[i] = xmsg.getByte(i);
+
+        pdata[1] = oneboxcount;
+
+        fillcrc(0x0008,pdata);
+        for(i=0;i<24;i++)xmsg.setDataAt(i,pdata[i]);
+
+        oneboxcount++;
+        if(oneboxcount > 14)oneboxcount = 0;
+    }
+
+    if(xmsg.getId() == 0x167)
+    {
+        unsigned char pdata[24];
+        unsigned int i;
+        for(i=0;i<24;i++)pdata[i] = xmsg.getByte(i);
+
+
+        pdata[1] = vcucount;
+        pdata[9] = vcucount;
+
+        fillcrc(0x000B,pdata);
+        fillcrc(0x000C,pdata+8);
+        for(i=0;i<24;i++)xmsg.setDataAt(i,pdata[i]);
 
+        vcucount++;
+        if(vcucount > 14)vcucount = 0;
     }
 }
 #endif