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

📄 ext_serial_9s12_port.c

📁 RT9S12 target document
💻 C
📖 第 1 页 / 共 3 页
字号:
        PRINT_DEBUG_MSG_LVL1("Detected a target timeout...\n\r");
        PRINT_DEBUG_MSG_LVL1_Raw("Buffer contents:\n\r[");
        /* nBytesAvailable = FIFOBUFSIZE   ->   display up to FIFOBUFSIZE bytes... */
        ExtSerialPortPeekRB(FIFOBUFSIZE);
        PRINT_DEBUG_MSG_LVL1_Raw(" ]\n\r");
        #endif
        
        if(numBytesRB > 0) {
        
          /* determine number of bytes currently stored in the reception ring buffer */
          numBytesRB = Buffercounter(ExtModeComBufPtr);
          
          /* still not enough data? */
          if(numBytesRB < MIN_PACKET_SIZE) {
         
            PRINT_DEBUG_MSG_LVL1_Raw("Removing incomplete packet.\n\r");
            
            ExtSerialPortMoveRBTail(numBytesRB);
            
            #if DEBUG_MSG_LVL > 0
            PRINT_DEBUG_MSG_LVL1_Raw("New buffer contents:\n\r[");
            /* nBytesAvailable = FIFOBUFSIZE   ->   display up to FIFOBUFSIZE bytes... */
            ExtSerialPortPeekRB(FIFOBUFSIZE);
            PRINT_DEBUG_MSG_LVL1_Raw(" ]\n\r");
            #endif
            
          }
          else {
          
            /* now enough data -> return... */
            goto EXIT_POINT;
            
          }
          
        } /* numBytesRB > 0 */
        
        /* signal the detection of a timeout condition to the calling function */
        *bytesPending = -1;
        
        /* reset timeout flag (defined in 'mc_timer.c') */
        commsTimeoutFlag = FALSE;
        
        /* display state of communications variables on port T */
        #if COMMSTATE_ON_PTT == 1
        PTT |=  0x10;       /* indicate the timeout condition */
        #endif

      } /* commsTimeoutFlag == TRUE */
    
    } /* startModel == TRUE */
    
    #endif /* USE_TARGET_TIMEOUTS */
      
    
    /* ensure that the buffer data starts with a packet_head byte... otherwise discard data up to the
     * next packet head
     */
    if(numBytesRB > 0) {
    
      /* determine number of bytes currently stored in the reception ring buffer */
      numBytesRB = Buffercounter(ExtModeComBufPtr);
      
      /* still not enough data? */
      if(numBytesRB < MIN_PACKET_SIZE) {
        
        #if DEBUG_MSG_LVL > 0
        PRINT_DEBUG_MSG_LVL1("numBytesRB = ");
        PRINT_DEBUG_MSG_LVL1_UHex((uint16_T)numBytesRB);
        PRINT_DEBUG_MSG_LVL1_Raw(" bytes -> {");
        ExtSerialPortPeekRB(FIFOBUFSIZE);
        PRINT_DEBUG_MSG_LVL1_Raw(" }\n\r");
        #endif
        
        /* peek at the next byte on the RB reception buffer */
        ExtSerialPortPeekSingleByteRB(&char1, &myTail);
          
        PRINT_DEBUG_MSG_LVL1("Next RB byte is a 0x");
        PRINT_DEBUG_MSG_LVL1_UHex((uint16_T)char1);
        PRINT_DEBUG_MSG_LVL1_Raw("\n\r");
            
        while((numBytesRB-- > 0) && (char1 != packet_head)) {
        
          /* char1 is not a packet_head byte -> advance tail position by '1' */  
          ExtSerialPortMoveRBTail(1);
            
          /* feedback... */
          PRINT_DEBUG_MSG_LVL1("Received data does not begin with a packet_head -> discarding byte (0x");
          PRINT_DEBUG_MSG_LVL1_UHex((uint16_T)char1);
          PRINT_DEBUG_MSG_LVL1_Raw(")\n\r");

          /* peek at the next byte on the RB reception buffer */
          ExtSerialPortPeekSingleByteRB(&char1, &myTail);
           
        }
        
      } /* numBytesRB < MIN_PACKET_SIZE */
      else {
        
        /* now enough data -> return... */
        goto EXIT_POINT;
          
      }
          
    } /* numBytesRB > 0 */
    
    
    /* not enough data... return */
    goto EXIT_POINT;
    
    
   } /* numBytesRB < MIN_PACKET_SIZE */
   else {
    
    /* enough data in the RB reception buffer -> need to check for valid packet... */
    PRINT_DEBUG_MSG_LVL3("Number of bytes in the RB buffer: ");
    PRINT_DEBUG_MSG_LVL3_UDec(numBytesRB);
    PRINT_DEBUG_MSG_NL3;
    

    #ifdef USE_TARGET_TIMEOUTS
    /* reset timeout counter (only used during system startup) */
    ExtSerialPortDataPendingCallsCounter = 0;
    #endif

    
    /* cycle through all available data... */
    for(i=0; i<numBytesRB; i++) {
    
      /* update number of bytes currently stored in the reception ring buffer */
      numBytesRB = Buffercounter(ExtModeComBufPtr);
   
      /* get a copy of the next character on the ring buffer */
      ExtSerialPortPeekSingleByteRB(&char1, &myTail);
      
      PRINT_DEBUG_MSG_LVL5_Raw("0x");
      PRINT_DEBUG_MSG_LVL5_UHex((uint16_T)char1);
      PRINT_DEBUG_MSG_LVL5_Raw(" (myTail: ");
      PRINT_DEBUG_MSG_LVL5_UDec((uint16_T)myTail);
      PRINT_DEBUG_MSG_LVL5_Raw(" )");
      PRINT_DEBUG_MSG_NL5;
      
      /* =================================================== */
      /* Handle quoting and filtering (does not deal with xon/xoff issues). */
      switch(myState) {

         case ESP_InType:
         case ESP_InSize:
         case ESP_InPayload:
            /* Handle quoted characters in payload. */
            if(inQuote) {

               inQuote  = false;
               char1   ^= mask_character;

            }
            else {

               /*
                    * No characters requiring escaping should be in the input
                    * stream, except for control purposes.
                    */
               switch(char1) {

                  case escape_character:
                     inQuote = true;
                     /* Need to go get next charater at this point. */
                     continue;
                     break;
                     /*
                          * other special characters should only exist
                          * in payload when quoted.
                          */
                  case packet_head:
                     /*
                      * Error - start handling the packet this header
                      * goes with.
                      */
                     #if DEBUG_MSG_LVL > 0
                     PRINT_DEBUG_MSG_LVL1("Error. Detected unexpected packet_head (0x");
                     PRINT_DEBUG_MSG_LVL1_UHex((uint16_T)char1);
                     PRINT_DEBUG_MSG_LVL1_Raw("). Buffer contents:\n\r[");
                     /* nBytesAvailable = FIFOBUFSIZE   ->   display up to FIFOBUFSIZE bytes... */
                     ExtSerialPortPeekRB(FIFOBUFSIZE);
                     PRINT_DEBUG_MSG_LVL1_Raw(" ]\n\r");
                     #endif
                     
                     PRINT_DEBUG_MSG_LVL1("Removing erroneous packet.\n\r");
                     ExtSerialPortSetRBTail(myTail);
                     /* decrement tail by one  */
                     ExtSerialPortMoveRBTail(-1);
                     
                     #if DEBUG_MSG_LVL > 0
                     PRINT_DEBUG_MSG_LVL1_Raw("Buffer contents:\n\r[");
                     /* nBytesAvailable = FIFOBUFSIZE   ->   display up to FIFOBUFSIZE bytes... */
                     ExtSerialPortPeekRB(FIFOBUFSIZE);
                     PRINT_DEBUG_MSG_LVL1_Raw(" ]\n\r");
                     #endif
                     //abort_LED(22); /* test only */
                     
                     goto EXIT_POINT;

                  case packet_tail:

                     /* Error - reset packet handling. */
                     #if DEBUG_MSG_LVL > 0
                     PRINT_DEBUG_MSG_LVL1("Error. Detected unexpected packet_tail (0x");
                     PRINT_DEBUG_MSG_LVL1_UHex((uint16_T)char1);
                     PRINT_DEBUG_MSG_LVL1_Raw("). Buffer contents:\n\r[");
                     /* nBytesAvailable = FIFOBUFSIZE   ->   display up to FIFOBUFSIZE bytes... */
                     ExtSerialPortPeekRB(FIFOBUFSIZE);
                     PRINT_DEBUG_MSG_LVL1_Raw(" ]\n\r");
                     #endif
                     
                     PRINT_DEBUG_MSG_LVL1("Removing erroneous packet.\n\r");
                     /* advances myTail by one (to the end of packet byte, i.e. the second 0x3) */
                     ExtSerialPortMoveRBTail(1);

                     #if DEBUG_MSG_LVL > 0
                     PRINT_DEBUG_MSG_LVL1_Raw("Buffer contents:\n\r[");
                     /* nBytesAvailable = FIFOBUFSIZE   ->   display up to FIFOBUFSIZE bytes... */
                     ExtSerialPortPeekRB(FIFOBUFSIZE);
                     PRINT_DEBUG_MSG_LVL1_Raw(" ]\n\r");
                     #endif
                     
                     /* display state of communications variables on port T */
                     #if COMMSTATE_ON_PTT == 1
                     PTT |=  0x08;    /* indicate the detection of an erroneous packet */
                     #endif
                     //abort_LED(23); /* test only */
                     
                     goto EXIT_POINT;
                     
                  default:
                     break;

               }

            }
            break;
            /* No quoting in non-payload portions. */
         case ESP_NoPacket:
         case ESP_InHead:
         case ESP_InTail:
         case ESP_Complete:
         default:
            break;

      }

      switch(myState) {

         case ESP_NoPacket:
            if(char1==packet_head) {
               myState=ESP_InHead;
            }         
            break;
         case ESP_InHead:
            if(char1==packet_head) {
               myState=ESP_InType;
            }
            else {
               PacketError=true;
            }
            break;
         case ESP_InType:
            DataCount = 0;
            myPtr = (unsigned char *)&mySize;
            myState=ESP_InSize;
            break;
         case ESP_InSize:
            if(DataCount < sizeof(mySize)) {
            
              /* read size  -  comes in the wrong way 'round ... assumes 'little endian' target */
              myPtr[3-DataCount++] = char1;
              
              /* all 4 'size' bytes processed yet? */
              if(DataCount == sizeof(mySize)) {

                 DataCount = 0;
                 
                 if(mySize != 0) {
                    myState = ESP_InPayload;
                 }
                 else {
                    myState = ESP_InTail;
                 }
                 
              }

            }
            else {

               PacketError=true;

            }
            break;
         case ESP_InPayload:
            if(DataCount < mySize) {

               if(++DataCount == mySize) {
    
                  DataCount = 0;
                  myState = ESP_InTail;
               }

            }
            else {

               PacketError=true;

            }
            break;
         case ESP_InTail:
            if(DataCount < 2) {

               if(char1 == packet_tail) {

                  if(++DataCount == 2) {
                      /* found a complete packet on the ring buffer... indicate this to the calling function */
                      *bytesPending = 1;

                      /* display state of communications variables on port T */
                      #if COMMSTATE_ON_PTT == 1
                      PTT &= ~0x10;       /* indicate the no-timeout condition */
                      #endif

                      goto EXIT_POINT;
                  }

               }
               else {

                  PacketError=true;

               }

            }
            else {

               PacketError=true;

            }
            break;
         default:
            /* unknown state - should never happen */
            break;

      }

      if(PacketError) {

         #if DEBUG_MSG_LVL > 0
         PRINT_DEBUG_MSG_LVL1("Packet error. Resetting packet counter...\n\r");
         PRINT_DEBUG_MSG_LVL1("Packet error at byte 0x");
         PRINT_DEBUG_MSG_LVL1_UHex((uint16_T)char1);
         PRINT_DEBUG_MSG_LVL1_Raw(". Buffer contents:\n\r[");
         /* nBytesAvailable = FIFOBUFSIZE   ->   display up to FIFOBUFSIZE bytes... */
         ExtSerialPortPeekRB(FIFOBUFSIZE);
         PRINT_DEBUG_MSG_LVL1_Raw(" ]\n\r");
         #endif
         
         PRINT_DEBUG_MSG_LVL1("Removing erroneous packet.\n\r");
         ExtSerialPortSetRBTail(myTail);
         
         #if DEBUG_MSG_LVL > 0
         PRINT_DEBUG_MSG_LVL1_Raw("). Buffer contents:\n\r[");
         /* nBytesAvailable = FIFOBUFSIZE   ->   display up to FIFOBUFSIZE bytes... */
         ExtSerialPortPeekRB(FIFOBUFSIZE);
         PRINT_DEBUG_MSG_LVL1_Raw(" ]\n\r");
         #endif
         //abort_LED(24); /* test only */
         
         myState = ESP_NoPacket;
         PacketError=false;

      }
      /* =================================================== */
      
      
    } /* for numBytesRB */

    
    /* at this stage it is clear that a full packet has not been received... */
    *bytesPending = 0;
    
   } /* else (enough bytes to start analysing) */
   

EXIT_POINT:

   PRINT_DEBUG_MSG_LVL4("OUT, error status: ");
   PRINT_DEBUG_MSG_LVL4_UDec(error);
   PRINT_DEBUG_MSG_NL4;

   return error;

} /* end ExtSerialPortDataPending */



/* Function: ExtSerialPortGetRawChar ===========================================
 * Abstract:
 *  Attempts to get one byte from the comm line.  The number of bytes read is
 *  returned via the 'bytesRead' parameter.
 *
 *  EXT_NO_ERROR is returned on success, EXT_ERROR on failure.
 */
PUBLIC boolean_T ExtSerialPortGetRawChar(ExtSerialPort *portDev, char *c, uint32_T *bytesRead) {

   // local function name... debugging only
   #if DEBUG_MSG_LVL > 0
   const char  *funct_name="ExtSerialPortGetRawChar";
   #endif

   if(!portDev->fConnected) {

      PRINT_DEBUG_MSG_LVL3("Error... not connected\n\r");

      abort_LED(8);

      return EXT_ERROR;

   }

   return serial_get_string(c, 1, bytesRead);

} /* end ExtSerialPortGetRawChar */


/* [EOF] ext_serial_9S12_port.c */

⌨️ 快捷键说明

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