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

📄 ext_serial_utils.c

📁 RT9S12 target document
💻 C
📖 第 1 页 / 共 3 页
字号:
         goto EXIT_POINT;

      }

      totalBytesWritten+=bytesWritten;

      /* We must get an ACK back. This is the only place in the code where 'waitForAck' is set to TRUE */
      waitForAck = (boolean_T)true;
      PRINT_DEBUG_MSG_LVL1("ExtMode packet sent, setting waitForAck to TRUE");
      PRINT_DEBUG_MSG_NL1;

   }

   EXIT_POINT:PRINT_DEBUG_MSG_LVL3("OUT, error status: ");
   PRINT_DEBUG_MSG_LVL3_UDec(error);
   PRINT_DEBUG_MSG_NL3;

   return error;

} /* end ExtSetPktWithACK */


/* Function: ExtGetPkt =========================================================
* Abstract:
*  Attempts to get the specified number of bytes from the comm line.  The
*  number of bytes read is returned via the 'nBytesGot' parameter.
*
*  EXT_NO_ERROR is returned on success, EXT_ERROR on failure.
*
* NOTES:
*  o it is not an error for 'nBytesGot' to be returned as 0
*  o this function blocks if no data is available
*/
PRIVATE boolean_T ExtGetPkt(ExtSerialPort *portDev, char *dst, int nBytesToGet, int *returnBytesGot) {

   FIFOBuffer  *buf;
   int         UnusedBytes;
   boolean_T   error=EXT_NO_ERROR;

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

   PRINT_DEBUG_MSG_LVL3("IN");
   PRINT_DEBUG_MSG_NL3;

   buf=GetFIFOPkt();
   while(buf==NULL) {

         PRINT_DEBUG_MSG_LVL1("2");
         PRINT_DEBUG_MSG_NL1;

      error=ExtGetPktBlocking(portDev);
      if(error!=EXT_NO_ERROR) {

         /* never happens, errors caught above  --  fw-07-07 */
         abort_LED(31);    // just in case

         goto EXIT_POINT;

      }

      buf=GetFIFOPkt();

   }

   UnusedBytes=(int_T)(buf->size-buf->offset);
   /* Test packet size. */
   if(nBytesToGet>=UnusedBytes) {

      *returnBytesGot=UnusedBytes;

   }
   else {

      *returnBytesGot=nBytesToGet;

   }

   /* Save packet using char* for proper math. */
   {
      char  *tempPtr=buf->pktBuf;
      tempPtr+=buf->offset;
      (void)memcpy(dst, tempPtr, *returnBytesGot);
   }

   /* Determine if the packet can be discarded. */
   buf->offset+=*returnBytesGot;

   if(buf->offset==buf->size) {

      int   bytesWritten;
      bool  isEmpty=isFIFOFreeEmpty();

      buf->size=0;
      buf->offset=0;

      InsertFIFOFree(RemoveFIFOPkt());

      if(isEmpty) {

         error=ExtSetPkt(portDev, NULL, 0, &bytesWritten, ACK_PACKET);
         if(error!=EXT_NO_ERROR) {

            /* never happens, errors caught above  --  fw-07-07 */
            abort_LED(32);    // just in case

            goto EXIT_POINT;

         }

      }

   }

   EXIT_POINT:PRINT_DEBUG_MSG_LVL3("OUT, error status: ");
   PRINT_DEBUG_MSG_LVL3_UDec(error);
   PRINT_DEBUG_MSG_NL3;

   return error;

} /* end ExtGetPkt */


/* Function: ExtPktPending =====================================================
* Abstract:
*  Returns true, via the 'pending' arg, if data is pending on the comm line.
*  Returns false otherwise.  If data is pending, the packet is read from the
*  comm line.  If that packet is an ACK packet, false is returned.  If the
*  packet is an extmode packet, it is saved and true is returned.
*
*  EXT_NO_ERROR is returned on success, EXT_ERROR on failure.
*/
PRIVATE boolean_T ExtPktPending(ExtSerialPort *portDev, boolean_T *pending, const int nBytesToGet) {

   boolean_T   error=EXT_NO_ERROR;

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

   PRINT_DEBUG_MSG_LVL5("IN");
   PRINT_DEBUG_MSG_NL5;

   *pending=false;

   if(isFIFOPktEmpty()) {

      /*
         * Is there a pkt already waiting?  If so, return true for pending pkt.
         * Otherwise, try to grab a pkt from the comm line (if one exists).
         */
      int32_T numPending = 0;

      PRINT_DEBUG_MSG_LVL5("Checking for pending packets");
      PRINT_DEBUG_MSG_NL5;

      error = ExtSerialPortDataPending(portDev, &numPending);
      if(error!=EXT_NO_ERROR) {

         /* never happens, errors caught above  --  fw-07-07 */
         abort_LED(33);    // just in case

         goto EXIT_POINT;

      }

      /* NOTE: check for '>0' because the host sided version of 'ExtSerialPortDataPending'
       *       returns the actual number of bytes on the target instead of the number
       *       packets (on the target numPending is returned '1' if there's a full packet 
       *       in the ring buffer, '0' if not and '-1' in case of a target comms timeout) 
       * 
       * fw-07-07
       */
      if(numPending > 0) {

         
         PRINT_DEBUG_MSG_LVL1_Raw("<1");

         PRINT_DEBUG_MSG_LVL3("Making blocking call to fetch the data from the comms line...");
         PRINT_DEBUG_MSG_NL3;
         
         error=ExtGetPktBlocking(portDev);
         if(error!=EXT_NO_ERROR) {

            /* never happens, errors caught above  --  fw-07-07 */
            abort_LED(34);    // just in case

            goto EXIT_POINT;

         }

         /*
               * Only if the pkt is saved in the fifo do we return a pkt is
               * pending.  If the acquired pkt was an ACK, it would have been
               * thrown away.
               */
         if(!isFIFOPktEmpty()) {

            PRINT_DEBUG_MSG_LVL3("Pending non-ACK packet detected (in FIFO)...\n\r");
            *pending=true;

         }

      }
      
      /* on the target only... */
      #ifndef MATLAB_MEX_FILE
      else {
        
        if(numPending == -1) {
            
            /* target timeout has been detected... */
            /* incomplete ACK_PACKET (currently only an assumption! -> perform check) packet has been removed */
            PRINT_DEBUG_MSG_LVL1("Target timeout, setting waitForAck = FALSE\n\r");
            waitForAck = false;
        
        }
        
      }
      #endif /* MATLAB_MEX_FILE */

   }
   else {

      PRINT_DEBUG_MSG_LVL3("Pending non-ACK packet detected (in FIFO)...\n\r");
      *pending=true;

   }

EXIT_POINT:

   PRINT_DEBUG_MSG_LVL5("OUT, error status: ");
   PRINT_DEBUG_MSG_LVL5_UDec(error);
   PRINT_DEBUG_MSG_NL5;

   return error;

} /* end ExtPktPending */


/* Function: ExtClearSerialConnection ==========================================
* Abstract:
*  Clear the connection by setting certain global variables to their initial
*  states.  The difference between ExtResetSerialConnection() and
*  ExtClearSerialConnection() is that the reset function frees all allocated
*  memory and nulls out all pointers.  The clear function only initializes
*  some global variables without freeing any memory.  When the connection is
*  being opened or closed, use the reset function.  If the host and target
*  are only disconnecting, use the clear function.
*/
PRIVATE void ExtClearSerialConnection(void) {

   waitForAck=false;

} /* end ExtClearSerialConnection */


/* Function: ExtResetSerialConnection ==========================================
* Abstract:
*  Reset the connection with the target by initializing some global variables
*  and freeing/nulling all allocated memory.
*/
PRIVATE void ExtResetSerialConnection(void) {

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

   PRINT_DEBUG_MSG_LVL3("Freeing memory of FIFOFree queue...");
   PRINT_DEBUG_MSG_NL3;
   #if DEBUG_MSG_LVL > 0
   bufCount=0;
   #endif
   while(!isFIFOFreeEmpty()) {

      free(RemoveFIFOFree());
      #if DEBUG_MSG_LVL > 0
      bufCount++;
      #endif
   }
   PRINT_DEBUG_MSG_LVL3("... ");
   PRINT_DEBUG_MSG_LVL3_UDec((uint16_T)bufCount);
   PRINT_DEBUG_MSG_LVL3_Raw(" buffers representing a total of ");
   PRINT_DEBUG_MSG_LVL3_UDec((uint16_T)(bufCount*(MAX_SERIAL_PKT_SIZE+12)));
   PRINT_DEBUG_MSG_LVL3_Raw(" bytes");
   PRINT_DEBUG_MSG_NL3;

   PRINT_DEBUG_MSG_LVL3("Freeing memory of FIFOPkt queue...");
   PRINT_DEBUG_MSG_NL3;
   #if DEBUG_MSG_LVL > 0
   bufCount=0;
   #endif
   while(!isFIFOPktEmpty()) {

      free(RemoveFIFOPkt());
      #if DEBUG_MSG_LVL > 0
      bufCount++;
      #endif
   }
   PRINT_DEBUG_MSG_LVL3("... ");
   PRINT_DEBUG_MSG_LVL3_UDec((uint16_T)bufCount);
   PRINT_DEBUG_MSG_LVL3_Raw(" buffers representing a total of ");
   PRINT_DEBUG_MSG_LVL3_UDec((uint16_T)(bufCount*(MAX_SERIAL_PKT_SIZE+12)));
   PRINT_DEBUG_MSG_LVL3_Raw(" bytes");
   PRINT_DEBUG_MSG_NL3;

   PRINT_DEBUG_MSG_LVL3("Freeing memory of InBuffer (");
   PRINT_DEBUG_MSG_LVL3_UDec((uint16_T)(MAX_SERIAL_PKT_SIZE));
   PRINT_DEBUG_MSG_LVL3_Raw(" bytes)");
   PRINT_DEBUG_MSG_NL3;
   free(InBuffer->Buffer);
   memset(InBuffer, 0, sizeof(ExtSerialPacket));

   PRINT_DEBUG_MSG_LVL3("Freeing memory of OutBuffer (");
   PRINT_DEBUG_MSG_LVL3_UDec((uint16_T)(MAX_SERIAL_PKT_SIZE));
   PRINT_DEBUG_MSG_LVL3_Raw(" bytes)");
   PRINT_DEBUG_MSG_NL3;
   free(OutBuffer->Buffer);
   memset(OutBuffer, 0, sizeof(ExtSerialPacket));

   PktFIFOHead=PktFIFOTail=NULL;
   FreeFIFOHead=FreeFIFOTail=NULL;

   /* reinitialize 'waitForAck' (to FALSE) */
   ExtClearSerialConnection();

} /* end ExtResetSerialConnection */


/* Function: ExtOpenSerialConnection ===========================================
* Abstract:
*  Open the connection with the target.
*/
PRIVATE ExtSerialPort * ExtOpenSerialConnection(uint16_T port, uint32_T baud) {

   ExtSerialPort  *portDev=NULL;
   uint32_T       maxSize=MAX_SERIAL_PKT_SIZE;    /* removed factor '*2'  --  fw-07-07 */
   boolean_T      error=EXT_NO_ERROR;

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

   portDev=ExtSerialPortCreate();
   if(portDev==NULL) {

      abort_LED(35);

      goto EXIT_POINT;

   }

   ExtResetSerialConnection();

   /*
      * Allocate the buffers for sending and receiving packets big enough to
      * hold the maximum size packet possible for transmission.
      */
   PRINT_DEBUG_MSG_LVL3("Allocating memory for OutBuffer (");
   PRINT_DEBUG_MSG_LVL3_UDec((uint16_T)maxSize);
   PRINT_DEBUG_MSG_LVL3_Raw(" bytes)");
   PRINT_DEBUG_MSG_NL3;
   OutBuffer->Buffer=(char*)malloc((int_T)maxSize);
   if(OutBuffer->Buffer==NULL) {

      abort_LED(36);

      error=EXT_ERROR;
      goto EXIT_POINT;

   }
   OutBuffer->BufferSize=maxSize;

   PRINT_DEBUG_MSG_LVL3("Allocating memory for InBuffer (");
   PRINT_DEBUG_MSG_LVL3_UDec((uint16_T)maxSize);
   PRINT_DEBUG_MSG_LVL3_Raw(" bytes)");
   PRINT_DEBUG_MSG_NL3;
   InBuffer->Buffer=(char*)malloc((int_T)maxSize);
   if(InBuffer->Buffer==NULL) {

      abort_LED(37);

      error=EXT_ERROR;
      goto EXIT_POINT;

   }
   InBuffer->BufferSize=maxSize;

   error=AddToFIFOFree();
   if(error!=EXT_NO_ERROR) {

      abort_LED(38);

      goto EXIT_POINT;

   }

   error=ExtSerialPortConnect(portDev, port, baud);
   if(error!=EXT_NO_ERROR) {

      abort_LED(39);

      goto EXIT_POINT;

   }

   EXIT_POINT:
   if(error!=EXT_NO_ERROR) {

      portDev=NULL;

   }
   return portDev;

} /* end ExtOpenSerialConnection */


/* Function: ExtCloseSerialConnection ==========================================
* Abstract:
*  Close the connection with the target.
*/
PRIVATE boolean_T ExtCloseSerialConnection(ExtSerialPort *portDev) {

   ExtResetSerialConnection();

   return(ExtSerialPortDisconnect(portDev));

} /* end ExtCloseSerialConnection */


/* [EOF] ext_serial_utils.c */

⌨️ 快捷键说明

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