⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ccp.c

📁 ccp
💻 C
📖 第 1 页 / 共 3 页
字号:

      case CC_GET_DAQ_SIZE: /* Return the size of a DAQ list and clear */
        {
          #define daqList com[2]
          #define daqId   (*(CCP_DWORD*)&com[4])
          #ifdef CCP_DAQ
            ccpStopDaq(daqList); /* Stop this daq list */
            /* Assignment of CAN identifiers not supported */
            if (daqId!=CCP_DTO_ID) {
              ccp.Crm[1] = CRC_CMD_SYNTAX;
            }
            ccp.Crm[3] = ccpClearDaqList(daqList); /* Number of  ODTs */
            ccp.Crm[4] = daqList*CCP_MAX_ODT; /* PID of the first ODT */
            #ifdef CCP_TESTMODE
              if (gDebugLevel) CCPPRINT("%u: GET_DAQ_SIZE daq=%u,id=%lu,size=%u,pid=%u\n",ctr,daqList,daqId,ccp.Crm[3],daqList*CCP_MAX_ODT);
            #endif
          #else
            ccp.Crm[3] = 0;
            ccp.Crm[4] = 0;
            #ifdef CCP_TESTMODE
              if (gDebugLevel) CCPPRINT("%u: GET_DAQ_SIZE daq=%u,size=0\n",ctr,daqList,daqId);
            #endif
          #endif
        }
        break;

      #ifdef CCP_DAQ

      case CC_SET_DAQ_PTR: /* Set DAQ pointer */
        {
          #define comDaq  com[2]
          #define comOdt  com[3]
          #define comIdx  com[4]
          if (comDaq>=CCP_MAX_DAQ||comOdt>=CCP_MAX_ODT||comIdx>7) {
            ccp.Crm[1] = CRC_CMD_SYNTAX;
            ccp.DaqListPtr = 0;
          } else {
            ccp.DaqListPtr = &ccp.DaqList[comDaq].odt[comOdt][comIdx];
          }
          #ifdef CCP_TESTMODE
            if (gDebugLevel) CCPPRINT("%u: SET_DAQ_PTR daq=%u,odt=%u,idx=%u\n",ctr,comDaq,comOdt,comIdx);
          #endif
        }
        break;

      case CC_WRITE_DAQ: /* Write DAQ entry */
        {
          #define writeDaqSize    com[2]
          #define writeDaqAddrExt com[3]
          #define writeDaqAddr    (*(CCP_DWORD*)&com[4])
          if (
            #ifdef CCP_ODT_ENTRY_SIZE
              #ifdef CCP_DOUBLE_FLOAT
                (writeDaqSize!=8) &&
              #endif
              (writeDaqSize!=1&&writeDaqSize!=2&&writeDaqSize!=4)
            #else
              writeDaqSize!=1
            #endif
            || ccp.DaqListPtr==0) {
            ccp.Crm[1] = CRC_CMD_SYNTAX;
          } else {
            #ifdef CCP_DAQ_BASE_ADDR
              ccp.DaqListPtr->ptr = ccpGetDaqPointer(writeDaqAddrExt,writeDaqAddr);
            #else
              ccp.DaqListPtr->ptr = (CCP_DAQBYTEPTR)ccpGetPointer(writeDaqAddrExt,writeDaqAddr);
            #endif
            #ifdef CCP_ODT_ENTRY_SIZE
              ccp.DaqListPtr->siz = writeDaqSize;
            #endif
          }
          #ifdef CCP_TESTMODE
            if (gDebugLevel) CCPPRINT("%u: WRITE_DAQ size=%u,ext=%u,addr=%lu\n",ctr,writeDaqSize,writeDaqAddrExt,writeDaqAddr);
          #endif
        }
        break;

      case CC_START_STOP: /* Cyclic aquisition start/stop */
        {
          #define ssCmd                 com[2]  /* Start or Stop */
          #define ssDaq                 com[3]  /* DAQ list */
          #define ssLast                com[4]  /* Last ODT to send */
          #define ssEventChannel        com[5]  /* Event Channel Number */
          #define ssPrescaler (*(CCP_WORD*)&com[6]) /* Prescaler */

          #ifdef CCP_SEED_KEY
            if (!(ccp.ProtectionStatus&PL_DAQ))
              ccp.Crm[1] = CRC_ACCESS_DENIED;
            else
          #endif
          if (!(ccp.SessionStatus&SS_DAQ)) { /* Not initialized */
           ccp.Crm[1] = CRC_DAQ_INIT_REQUEST;
          }
          else {
            switch (ssCmd) {
              case 0: /* stop */
                ccpStopDaq(ssDaq);
                break;
              case 1: /* start */
                ccpPrepareDaq(ssDaq,ssLast,ssEventChannel,ssPrescaler);
                ccpStartDaq(ssDaq);
                #ifdef CCP_TESTMODE
                  if (gDebugLevel) ccpPrintDaqList(ssDaq);
                #endif
                break;
              case 2: /* prepare */
                ccpPrepareDaq(ssDaq,ssLast,ssEventChannel,ssPrescaler);
                #ifdef CCP_TESTMODE
                  if (gDebugLevel) ccpPrintDaqList(ssDaq);
                #endif
                break;
              default:
                ccp.Crm[1] = CRC_CMD_SYNTAX;
                break;
            }
          }

          #ifdef CCP_TESTMODE
            if (gDebugLevel) CCPPRINT("%u: START_STOP cmd=%u,daq=%u,last=%u,eventChannel=%u,prescaler=%u\n",ctr,ssCmd,ssDaq,ssLast,ssEventChannel,ssPrescaler);
          #endif

        }
        break;

      case CC_START_STOP_ALL: /* Cyclic aquisition start/stop */
        {
          #ifdef CCP_SEED_KEY
            if (!(ccp.ProtectionStatus&PL_DAQ))
              ccp.Crm[1] = CRC_ACCESS_DENIED;
            else
          #endif
          if (!(ccp.SessionStatus&SS_DAQ)) { /* Not initialized */
           ccp.Crm[1] = CRC_DAQ_INIT_REQUEST;
          } else {
            switch (ssCmd) {
              case 0: /* Stop */
                ccpStopAllDaq();
                break;
              case 1: /* Start */
                ccpStartAllPreparedDaq();
                break;
              default:
                ccp.Crm[1] = CRC_CMD_SYNTAX;
                break;
            }
          }
          #ifdef CCP_TESTMODE
            if (gDebugLevel) CCPPRINT("%u: START_STOP_ALL cmd=%u\n",ctr,ssCmd);
          #endif
        }
        break;

      #endif /* CCP_DAQ */

      #ifdef CCP_CHECKSUM

      case CC_BUILD_CHKSUM: /* Build Checksum */
        {
          CCP_DWORD s;

          /* Initialize Responce */
          ccp.Crm[3] = sizeof(CCP_CHECKSUM_TYPE); /* Checksum Size */
          #ifdef CCP_CHECKSUM_CCITT               /* Checksum */
            *(CCP_DWORD*)&ccp.Crm[4] = 0xFFFFFFFF;
          #else
            *(CCP_DWORD*)&ccp.Crm[4] = 0;
          #endif
          ccp.MTA[CCP_INTERNAL_MTA] = ccp.MTA[0];        /* MTA[0] is not affected */
          #ifdef CCP_MOTOROLA
            s = (*(CCP_WORD*)&com[4]) | ((*(CCP_WORD*)&com[2])<<16);
          #else
            s = (*(CCP_WORD*)&com[2]) | ((*(CCP_WORD*)&com[4])<<16);
          #endif
          #ifdef CCP_TESTMODE
            if (gDebugLevel) CCPPRINT("%u: BUILD_CHKSUM size=%u\n",ctr,s);
          #endif
          #ifndef CCP_CPUTYPE_32BIT
            if (s&0xffff0000) ccp.Crm[1] = CRC_OUT_OF_RANGE; /* Range, max. 64K-1 on <32Bit CPUs */
            ccp.CheckSumSize = (CCP_WORD)s;
          #else
            ccp.CheckSumSize = s;
          #endif
          if (ccp.Crm[1]==0) {
            RST_PORT_BIT(1); /* Timingtest */
            return; /* Checksum calculation will be performed by ccpBackground() */
          }

        }
        break;

      #endif /* CCP_CHECKSUM */

      /* Flash Programming Kernel Download */
      #ifdef CCP_BOOTLOADER_DOWNLOAD

      case CC_PROGRAM_PREPARE: /* Prepare flash kernel download */
        {
          #ifdef CCP_TESTMODE
            if (gDebugLevel) CCPPRINT("%u: PROGRAM_PREPARE\n",ctr);
          #endif
          if (!ccpDisableNormalOperation(ccp.MTA[0],*(CCP_WORD*)&com[2])) {
            ccp.Crm[1] = CRC_ACCESS_DENIED;
          }
        }
        break;

      case CC_PROGRAM_START:   /* Start flash kernel */
        {
          #ifdef CCP_TESTMODE
            if (gDebugLevel) CCPPRINT("%u: PROGRAM_START\n",ctr);
          #endif
          #ifdef CCP_SEED_KEY
            if (!(ccp.ProtectionStatus&PL_PGM))
              ccp.Crm[1] = CRC_ACCESS_DENIED;
            else
          #endif
          ((ccpBootLoader_t)ccp.MTA[0])(&ccp,com);
        }
        break;

      #endif /* CCP_BOOTLOADER_DOWNLOAD */
                           
      /* Flash Programming */
      #ifdef CCP_PROGRAM 

      case CC_CLEAR_MEMORY: /* Clear Memory */
        {
          CCP_DWORD s;

          #ifdef CCP_SEED_KEY
            if (!(ccp.ProtectionStatus&PL_PGM))
              ccp.Crm[1] = CRC_ACCESS_DENIED;
            else
          #endif

          #ifdef CCP_BOOTLOADER
            /* Transfer control to the CCP bootloader */
            ccpBootLoaderStartup(&ccp,com); /* Never returns */
          #else
            /* Clear flash sector */
            #ifdef CCP_MOTOROLA
              s = (*(CCP_WORD*)&com[4]) | ((*(CCP_WORD*)&com[2])<<16);
            #else
              s = (*(CCP_WORD*)&com[2]) | ((*(CCP_WORD*)&com[4])<<16);
            #endif
            ccpFlashClear(ccp.MTA[0],s);
          #endif

          #ifdef CCP_TESTMODE
            if (gDebugLevel) CCPPRINT("%u: CLEAR_MEMORY size=%u\n",ctr,s);
          #endif
        }
        break;

      /* Flash Programming */
      #ifndef CCP_BOOTLOADER
      
      case CC_PROGRAM: /* Program */
        {
          CCP_BYTE r;
          #define size com[2]
          #ifdef CCP_SEED_KEY
            if (!(ccp.ProtectionStatus&PL_PGM)) {
              ccp.Crm[1] = CRC_ACCESS_DENIED;
              r = 0;
            }
            else
          #endif
          r = ccpFlashProgramm(&com[3],ccp.MTA[0],size);
          ccp.MTA[0] += size;
          if (r==CCP_WRITE_PENDING) return; /* FLASH write pending */
          #ifdef CCP_TESTMODE
            if (gDebugLevel) {
              CCPPRINT("%u: PROGRAM ",ctr);
              if (size==0) {
                CCPPRINT("EOS\n");
              } else {
                int i;
                for (i=0;i<size;i++) CCPPRINT("%02X ",com[3+i]);
                CCPPRINT("\n");
              }
            }
          #endif
        }
        break;

      case CC_PROGRAM6: /* Program */
        {
          CCP_BYTE r;
          #ifdef CCP_SEED_KEY
            if (!(ccp.ProtectionStatus&PL_PGM)) {
              ccp.Crm[1] = CRC_ACCESS_DENIED;
              r = 0;
            }
            else
          #endif
          r = ccpFlashProgramm(&com[2],ccp.MTA[0],6);
          ccp.MTA[0] += 6;
          if (r==CCP_WRITE_PENDING) return; /* FLASH write pending */
          #ifdef CCP_TESTMODE
            if (gDebugLevel) {
              int i;
              CCPPRINT("%u: PROGRAM6: ",ctr);
              for (i=0;i<6;i++) CCPPRINT("%02X ",com[2+i]);
              CCPPRINT("\n");
            }
          #endif
        }
        break;
        
      #endif /* !CCP_BOOTLOADER */
      #endif /* CCP_PROGRAM */
      
      #ifdef CCP_CALPAGE

      case CC_SET_CAL_PAGE: /* Select Calibration Page */
        {
          ccpSetCalPage((CCP_DWORD)ccp.MTA[0]);
          #ifdef CCP_TESTMODE
            if (gDebugLevel) CCPPRINT("%u: SEL_CAL_PAGE id=%08Xh\n",ctr,(CCP_DWORD)ccp.MTA[0]);
          #endif
        }
        break;

      case CC_GET_CAL_PAGE: /* Get Active Calibration Page */
        {
          ccp.Crm[3] = 0; /* Address Extension */
          *(CCP_DWORD*)&ccp.Crm[4] = ccpGetCalPage(); /* Address */
          #ifdef CCP_TESTMODE
            if (gDebugLevel) CCPPRINT("%u: GET_CAL_PAGE -> id=%08Xh\n",ctr,*(CCP_DWORD*)&ccp.Crm[4]);
          #endif
        }
        break;
                       
      #endif /* CCP_CALPAGE */
      
      #ifdef CCP_SET_SESSION_STATUS

      case CC_SET_S_STATUS: /* Set Session Status */
        {
          /* Set Resume and Store mode in SessionStatus */
          ccp.SessionStatus &= ~(SS_STORE|SS_RESUME);
          ccp.SessionStatus |= (com[2]&(SS_STORE|SS_RESUME));

          /* Save as UserSessionStatus */
          ccp.UserSessionStatus = com[2];

          #ifdef CCP_TESTMODE
            if (gDebugLevel) CCPPRINT("%u: SET_S_STATUS status=%02X\n",ctr,ccp.UserSessionStatus);
          #endif
        }
        break;

      case CC_GET_S_STATUS: /* Get Session Status */
        {
          ccp.Crm[3] = ccp.UserSessionStatus;
          ccp.Crm[4] = 0; /* No additional status */

          #ifdef CCP_TESTMODE
            if (gDebugLevel) CCPPRINT("%u: GET_S_STATUS -> status=%02X\n",ctr,ccp.Crm[3]);
          #endif
        }
        break;
      
      #endif /* CCP_SET_SESSION_STATUS */
      
      case CC_GET_CCP_VERSION: /* Get Version */
        {
          ccp.Crm[3] = CCP_VERSION_MAJOR;
          ccp.Crm[4] = CCP_VERSION_MINOR;
          #ifdef CCP_TESTMODE
            if (gDebugLevel) CCPPRINT("%u: GET_VERSION -> major=%02X minor=%02X\n",ctr,ccp.Crm[3],ccp.Crm[4]);
          #endif
        }
        break;

      default: /* unknown */
        {
          ccp.Crm[1] = CRC_CMD_UNKNOWN;
          #ifdef CCP_TESTMODE
            if (gDebugLevel) CCPPRINT("*** UNKNOWN COMMAND ***\n");
          #endif
          break;
        }

    } /* switch */


    /* Responce */
    /* |||| */
  }

  /* Not connected */
  else {

    RST_PORT_BIT(1); /* Timingtest */

    /* No responce */
    return;
  }

  ccpSendCrm();

  RST_PORT_BIT(1); /* Timingtest */
}


/*--------------------------------------------------------------------------*/
/* Send notification callback */
/* ccpSend must not fail, when called from this function */
/*--------------------------------------------------------------------------*/
CCP_BYTE ccpSendCallBack( void ) {

  SET_PORT_BIT(2); /* Timingtest */

  /* Clear all pending flags, except for CCP_CMD_PENDING */
  ccp.SendStatus &= ~CCP_SEND_PENDING;

  /* Send a CRM message */
  if (ccp.SendStatus&CCP_CRM_REQUEST) {
    ccp.SendStatus &= ~CCP_CRM_REQUEST;
    ccpSendCrm();
    RST_PORT_BIT(2); /* Timingtest */
    return 1;
  }

  /* Send a DAQ message */
  #ifdef CCP_DAQ
    if (ccp.SessionStatus&SS_RUN) {

      /* Send a  DAQ message (DTM) from the queue */
      #ifdef CCP_SEND_QUEUE

        CCP_DISABLE_INTERRUPT;
        if (ccp.Queue.len) {
          ccp.SendStatus |= CCP_DTM_PENDING;
          ccpSend((CCP_BYTEPTR)&ccp.Queue.msg[ccp.Queue.rp]);
          ccp.Queue.rp++;
          if (ccp.Queue.rp>=CCP_SEND_QUEUE_SIZE) ccp.Queue.rp = 0;
          ccp.Queue.len--;
          CCP_ENABLE_INTERRUPT;
          RST_PORT_BIT(2); /* Timingtest */
          return 1;
        }
        CCP_ENABLE_INTERRUPT;

      /* Send a pending DAQ message (DTM) */
      #else

        if (ccp.SendStatus&CCP_DTM_REQUEST) {
          ccp.SendStatus &= ~CCP_DTM_REQUEST;
          ccpSendDtm();
          RST_PORT_BIT(2); /* Timingtest */
          return 1;
        }

      #endif
    }
  #endif

  RST_PORT_BIT(2); /* Timingtest */
  return 0;
}


/*--------------------------------------------------------------------------*/
/* Initialization */
/*--------------------------------------------------------------------------*/

void ccpInit( void ) {

  /* Initialize all CCP variables to zero */
  CCP_BYTEPTR p;
  CCP_BYTEPTR pl;
  p = (CCP_BYTEPTR)&ccp;
  pl = p+sizeof(ccp);
  while (p<pl) *p++ = 0;

}


/*--------------------------------------------------------------------------*/
/* Test */
/* Screen output for test and diagnostics */
/*--------------------------------------------------------------------------*/

#ifdef CCP_TESTMODE

#ifdef CCP_DAQ

/* Print all DAQ lists to screen */
static void ccpPrintDaqList( CCP_BYTE daq )
{

  CCP_BYTE i,j,f;

  if (daq>=CCP_MAX_DAQ) return;
  CCPPRINT("DAQ-List %u:\n",daq);
  CCPPRINT(" event channel=%u,",ccp.DaqList[daq].eventChannel);
  CCPPRINT(" prescaler=%u,",ccp.DaqList[daq].prescaler);
  CCPPRINT(" last=%u,",ccp.DaqList[daq].last);
  CCPPRINT(" flags=%u\n",ccp.DaqList[daq].flags);
  for (j=0;j<CCP_MAX_ODT;j++) {
    for (i=0,f=1;i<=6;i++) {
      if (ccp.DaqList[daq].odt[j][i].ptr) {
        if (f) { CCPPRINT(" PID %u: ",daq*CCP_MAX_ODT+j); f = 0; }
        #ifdef CCP_ODT_ENTRY_SIZE
          CCPPRINT("[%p,%u],",ccp.DaqList[daq].odt[j][i].ptr,ccp.DaqList[daq].odt[j][i].siz);
        #else
          CCPPRINT("[%p],",ccp.DaqList[daq].odt[j][i].ptr);
        #endif
      }
    }
    if (!f) CCPPRINT("\n");
  } /* j */
}

#endif

#endif

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -