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

📄 ext_serial_9s12_port.c

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

   for(i=0; i<size; i++) {

      while((SCISR1&TDRE_mask)==0) {

      };  // wait until the end of a possibly ongoing transmission
      SCIDRL=src[i];                // send character

      PRINT_DEBUG_MSG_LVL4("Sending  0x");
      PRINT_DEBUG_MSG_LVL4_UHex(src[i]);
      PRINT_DEBUG_MSG_NL4;

   }

   *bytesWritten=i;


   /* return error flag */
   PRINT_DEBUG_MSG_LVL5("OUT, error status: ");
   PRINT_DEBUG_MSG_LVL5_UDec(error);
   PRINT_DEBUG_MSG_NL5;

   return EXT_NO_ERROR;

} /* end serial_set_string */

#endif /* TARGET_BOARD != C32-based */



/* Function: SCIx_init ===========================================================
 * Abstract:
 *  Called by ExtInit
 *
 * NOTES:
 *  o This function should not block.
 */
PRIVATE boolean_T SCIx_init(uint16_T port, uint_T baud) {

static boolean_T  firstTime = true;

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

   PRINT_DEBUG_MSG_LVL2("IN, MATLAB communication port: SCI");
   PRINT_DEBUG_MSG_LVL2_UDec(port-1);
   PRINT_DEBUG_MSG_NL2;

   /* initialize reception ring buffer, only ever done once */
   if(firstTime) {
     PRINT_DEBUG_MSG_LVL2("Initializing serial reception buffer 'ExtModeComBuf' (");
     PRINT_DEBUG_MSG_LVL2_UDec((uint16_T)RBUFSIZE);
     PRINT_DEBUG_MSG_LVL2_Raw(" bytes)");
     PRINT_DEBUG_MSG_NL2;
     InitBuffer(ExtModeComBufPtr, RBUFSIZE);
     firstTime = false;
   } else {
     ClearBuffer(ExtModeComBufPtr);
   }

   /* make sure we're using the right port... */
   if(port==1) {

      #if TARGET_BOARD != C32BASED
      SCI1_Init(baud);      /* mc_signal.c */
      #endif

      /* promote SCI1 interrupt REQUESTS to hightest priority */
      HPRIO=0xD4;         /* 0xFFD4 -> SCI1 interrupt = highest priority */

   }
   else {

      SCI0_Init(baud);      /* mc_signal.c */

      /* promote SCI0 interrupt REQUESTS to hightest priority */
      HPRIO=0xD2;         /* 0xFFD2 -> SCI0 interrupt = highest priority */

   }

   PRINT_DEBUG_MSG_LVL2("OUT, error status: EXT_NO_ERROR\n\r");

   /* return error flag */
   return EXT_NO_ERROR;

} /* end SCIx_init */


/* Function: ExtSerialPortPeekSingleByteRB ===========================================
 * Abstract:
 *  Return a copy of a byte on the RB reception buffer without removing it. The read
 *  position is determined by 'myTail'. Setting 'myTail' to '-1' fetches the byte from
 *  the current buffer->tail position; an incremented, possibly wrapped, 'myTail' is returned.
 */
PRIVATE void ExtSerialPortPeekSingleByteRB(unsigned char *myChar, uint16_T *myTail) {

   BufferPeek(ExtModeComBufPtr, *myChar, *myTail);

} /* end ExtSerialPortPeekSingleByteRB */


/* Function: ExtSerialPortMoveRBTail ================================================
 * Abstract:
 *  Set the RB reception ring buffer tail to 'myTail'; used to remove erroneous packets
 *  from the reception buffer
 */
PRIVATE void ExtSerialPortMoveRBTail(int16_T myOffset) {

   BufferMoveTail(ExtModeComBufPtr, myOffset);

} /* end ExtSerialPortMoveRBTail */


/* Function: ExtSerialPortSetRBTail =================================================
 * Abstract:
 *  Set the RB reception ring buffer tail to 'myTail'; used to remove erroneous packets
 *  from the reception buffer
 */
PRIVATE void ExtSerialPortSetRBTail(uint16_T myTail) {

   BufferSetTail(ExtModeComBufPtr, myTail);

} /* end ExtSerialPortSetRBTail */



/***************** VISIBLE FUNCTIONS ******************************************/
/***************** VISIBLE FUNCTIONS ******************************************/
/***************** VISIBLE FUNCTIONS ******************************************/



#if DEBUG_MSG_LVL > 0
/* Function: ExtSerialPortPeekRB ===========================================
 * Abstract:
 *  Displays the last 'n' bytes on the RB reception buffer without removing them...
 */
PUBLIC void ExtSerialPortPeekRB(uint16_T nBytesToDisplay) {

  int i, tempTail;
  unsigned char myChar;
  unsigned int  nBytesAvailable;
  
   
   /* display either the requested number of bytes, or whatever portion thereof is available... */
   nBytesAvailable = Buffercounter(ExtModeComBufPtr);
   nBytesAvailable = (nBytesAvailable > nBytesToDisplay)? nBytesToDisplay:nBytesAvailable;
   
   tempTail = -1;
   for(i=0; i<nBytesAvailable; i++) {
    
      BufferPeek(ExtModeComBufPtr, myChar, tempTail);
      
      PRINT_DEBUG_MSG_LVL1_Raw(" 0x");
      PRINT_DEBUG_MSG_LVL1_UHex((uint16_T)myChar);
      
   }

} /* end ExtSerialPortPeekRB */
#endif


/* Function: ExtSerialPortCreate ===============================================
 * Abstract:
 *  Creates an External Mode Serial Port object.  The External Mode Serial Port
 *  is an abstraction of the physical serial port providing a standard
 *  interface for external mode code.  A pointer to the created object is
 *  returned.
 *
 */
PUBLIC ExtSerialPort * ExtSerialPortCreate(void) {

   static ExtSerialPort serialPort;
   ExtSerialPort        *portDev= &serialPort;

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

   PRINT_DEBUG_MSG_LVL2("IN\n\r");

   /* Determine and save endianess. */
   {
      union Char2Integer_tag {

            int   IntegerMember;
            char  CharMember[sizeof(int)];

      } temp;

      temp.IntegerMember=1;
      if(temp.CharMember[0]!=1) {

         portDev->isLittleEndian=false;

      }
      else {

         portDev->isLittleEndian=true;

      }
   }

   portDev->fConnected=false;
   //PRINT_DEBUG_MSG_LVL1("portDev->fConnected = FALSE\n\r");

   PRINT_DEBUG_MSG_LVL2("OUT\n\r");

   return portDev;

} /* end ExtSerialPortCreate */



/* Function: ExtSerialPortDestroy ===============================================
 * Abstract:
 *  Destroys an External Mode Serial Port object.  (not really required... fw-06-07)
 */
PUBLIC void ExtSerialPortDestroy(ExtSerialPort *portDev) {

   /* no need to do anything... */

} /* end ExtSerialPortDestroy */



/* Function: ExtSerialPortConnect ==============================================
 * Abstract:
 *  Performs a logical connection between the external mode code and the
 *  External Mode Serial Port object and a real connection between the External
 *  Mode Serial Port object and the physical serial port.
 *
 *  EXT_NO_ERROR is returned on success, EXT_ERROR on failure.
 */
PUBLIC boolean_T ExtSerialPortConnect(ExtSerialPort *portDev, uint16_T port, uint32_T baudRate) {

   boolean_T   error=EXT_NO_ERROR;

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

   PRINT_DEBUG_MSG_LVL2("IN\n\r");

   if(portDev->fConnected) {

      abort_LED(3);

      error=EXT_ERROR;
      goto EXIT_POINT;

   }

   portDev->fConnected=true;
   //PRINT_DEBUG_MSG_LVL1("portDev->fConnected = TRUE\n\r");

   error=SCIx_init(port, (uint_T)baudRate);
   if(error!=EXT_NO_ERROR) {

      /* never reached -> SCIx_init always returns EXT_NO_ERROR  --  fw-07-07 */
      portDev->fConnected=false;
      //PRINT_DEBUG_MSG_LVL1("portDev->fConnected = FALSE\n\r");

      abort_LED(4);

      goto EXIT_POINT;

   }

   EXIT_POINT:PRINT_DEBUG_MSG_LVL2("OUT, error status: ");
   PRINT_DEBUG_MSG_LVL2_UDec(error);
   PRINT_DEBUG_MSG_NL2;

   return error;

} /* end ExtSerialPortConnect */



/* Function: ExtSerialPortDisconnect ===========================================
 * Abstract:
 *  Performs a logical disconnection between the external mode code and the
 *  External Mode Serial Port object and a real disconnection between the
 *  External Mode Serial Port object and the physical serial port.
 *
 *  EXT_NO_ERROR is returned on success, EXT_ERROR on failure.
 */
PUBLIC boolean_T ExtSerialPortDisconnect(ExtSerialPort *portDev) {

   boolean_T   error=EXT_NO_ERROR;

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

   PRINT_DEBUG_MSG_LVL2("IN\n\r");

   if(!portDev->fConnected) {

      abort_LED(5);

      error=EXT_ERROR;
      goto EXIT_POINT;

   }

   portDev->fConnected=false;
   //PRINT_DEBUG_MSG_LVL1("portDev->fConnected = FALSE\n\r");

   // no need to close the port on the micro  -  fw-06-07
   //error = serial_uart_close();
   //if (error != EXT_NO_ERROR) goto EXIT_POINT;

   EXIT_POINT:PRINT_DEBUG_MSG_LVL2("OUT, error status: ");
   PRINT_DEBUG_MSG_LVL2_UDec(error);
   PRINT_DEBUG_MSG_NL2;

   return error;

} /* end ExtSerialPortDisconnect */



/* Function: ExtSerialPortSetData ==============================================
 * Abstract:
 *  Sets (sends) the specified number of bytes on the comm line.  The number of
 *  bytes sent is returned via the 'bytesWritten' parameter.
 *
 *  EXT_NO_ERROR is returned on success, EXT_ERROR on failure.
 */
PUBLIC boolean_T ExtSerialPortSetData(ExtSerialPort *portDev, char *data, uint32_T size, uint32_T *bytesWritten) {

   if(!portDev->fConnected) {

      // temporarily disabled to pass this error on to the calling function -- fw-07-07
      //abort_LED(6);

      return EXT_ERROR;

   }

   return serial_set_string(data, size, bytesWritten);

} /* end ExtSerialPortSetData */



/* Function: ExtSerialPortDataPending ==========================================
 * Abstract:
 *  Returns, via the 'bytesPending' arg, the number of bytes pending on the
 *  comm line: returns 'bytesPending = 1' if a full packet is available, 
 *  'bytesPending = 0' if no valid packet is available and '-1' if a target 
 *  timeout condition has been detected.
 *
 *  EXT_NO_ERROR is returned on success, EXT_ERROR on failure.
 */
PUBLIC boolean_T ExtSerialPortDataPending(ExtSerialPort *portDev, int32_T *bytesPending) {

   // local function name... debugging only
   #if DEBUG_MSG_LVL > 0
   const char  *funct_name="ExtSerialPortDataPending";
   #endif
   
   boolean_T      error = EXT_NO_ERROR;
   uint16_T       myTail = -1;      /* always start scanning the RB buffer at the current tail position */
   
   uint16_T       numBytesRB, i;
   unsigned char  char1, *myPtr;
   
   /* packet handling... */
   int            myState = ESP_NoPacket;
   boolean_T      inQuote = false, PacketError = false;
   uint32_T       DataCount = 0;
   uint32_T       mySize;

   if(!portDev->fConnected) {

      // temporarily disabled to pass this error on to the calling function -- fw-07-07
      //abort_LED(7);

      return EXT_ERROR;

   }


   PRINT_DEBUG_MSG_LVL4("IN\n\r");
   

   /* determine number of bytes currently stored in the reception ring buffer */
   numBytesRB = Buffercounter(ExtModeComBufPtr);
   
   /* default assumption: no valid packet available */
   *bytesPending = 0;
   
   /* analyse... */
   if(numBytesRB < MIN_PACKET_SIZE) {
    
    /* not even enough data for the smallest possible packet -> return rightaway */
    
    /* check for timeouts */
    #ifdef USE_TARGET_TIMEOUTS
    
    if(startModel == FALSE) {
    
      /* core timer not running... use calls to this function as a measure for timeout conditions */
      if(ExtSerialPortDataPendingCallsCounter++ >= MAX_NUM_CALLS_TO_PENDING) {

        /* signal the detection of a timeout condition to the calling function */
        *bytesPending = -1;
        
        /* reset counter */
        ExtSerialPortDataPendingCallsCounter = 0;
        
        /* display state of communications variables on port T */
        #if COMMSTATE_ON_PTT == 1
        PTT |=  0x10;       /* indicate the timeout condition */
        #endif
        
      } /* ExtSerialPortDataPendingCallsCounter > MAX_NUM_CALLS_TO_PENDING */
        
    } /* startModel == FALSE */
    else {
    
      /* core timer is running... use ISR driven timeout timer */
      if(commsTimeoutFlag == TRUE) {
      
        #if DEBUG_MSG_LVL > 0

⌨️ 快捷键说明

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