freescale

来自「Freescale 系列单片机常用模块与综合系统设计」· 代码 · 共 542 行 · 第 1/2 页

TXT
542
字号
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_TXFULL - It was not possible to send
**                           requested number of bytes
** ===================================================================
*/
byte Uart_SendBlock(Uart_TComData * Ptr, word Size, word *Snd)
{
  word count;                          /* Number of sent chars */
  byte result = ERR_OK;                /* Last error */

  count = 0;
  while(count < Size) {
    result = Uart_SendChar(*Ptr++);    /* Send data */
    if(result == ERR_OK) {             /* Has data been successfully sent ?*/
      count++;                         /* If yes, increase the count of sent data*/
    } else {
      *Snd = count;                    /* Return the number of sent chars */
      return result;                   /* Return the last error code*/
    }
  }
  *Snd = count;                        /* Return the number of sent chars */
  return result;                       /* Return the last error code*/
}

/*
** ===================================================================
**     Method      :  Uart_ClearRxBuf (bean AsynchroSerial)
**
**     Description :
**         Clears the receive buffer.
**         This method is available only if non-zero length of the
**         input buffer is defined and the receiver property is
**         enabled.
**     Parameters  : None
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte Uart_ClearRxBuf(void)
{
  EnterCritical();                     /* Save the PS register */
  Uart_InpLen = 0;                     /* Set number of chars in the transmit buffer to 0 */
  InpIndxR = InpIndxW = 0;             /* Reset indices */
  SerFlag &= ~ (CHAR_IN_RX | FULL_RX); /* Clear the flags indicating a char in buffer */
  ExitCritical();                      /* Restore the PS register */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  Uart_ClearTxBuf (bean AsynchroSerial)
**
**     Description :
**         Clears the transmit buffer.
**         This method is available only if non-zero length of the
**         output buffer is defined and the receiver property is
**         enabled.
**     Parameters  : None
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte Uart_ClearTxBuf(void)
{
  EnterCritical();                     /* Save the PS register */
  Uart_OutLen = 0;                     /* Set number of chars in the receive buffer to 0 */
  OutIndxR = OutIndxW = 0;             /* Reset indices */
  ExitCritical();                      /* Restore the PS register */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  Uart_GetCharsInRxBuf (bean AsynchroSerial)
**
**     Description :
**         Returns the number of characters in the input buffer.
**         This method is available only if the receiver property is
**         enabled.
**     Parameters  : None
**     Returns     :
**         ---             - The number of characters in the input
**                           buffer.
** ===================================================================
*/
/*
word Uart_GetCharsInRxBuf(void)

**      This method is implemented as a macro. See header module. **
*/

/*
** ===================================================================
**     Method      :  Uart_GetCharsInTxBuf (bean AsynchroSerial)
**
**     Description :
**         Returns the number of characters in the output buffer.
**         This method is available only if the transmitter property
**         is enabled.
**     Parameters  : None
**     Returns     :
**         ---             - The number of characters in the output
**                           buffer.
** ===================================================================
*/
/*
word Uart_GetCharsInTxBuf(void)

**      This method is implemented as a macro. See header module. **
*/

/*
** ===================================================================
**     Method      :  Uart_InterruptRx (bean AsynchroSerial)
**
**     Description :
**         The method services the receive interrupt of the selected 
**         peripheral(s) and eventually invokes the bean's event(s).
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
#define ON_ERROR      1
#define ON_FULL_RX    2
#define ON_RX_CHAR    4
#define ON_IDLE_CHAR  8
ISR(Uart_InterruptRx)
{
  byte StatReg = SCI1S1;               /* Temporary variable for status flags */
  Uart_TComData Data;                  /* Temporary variable for data */
  byte OnFlags = 0;                    /* Temporary variable for flags */

  Data = SCI1D;                        /* Read data from the receiver */
  if(Uart_InpLen < Uart_INP_BUF_SIZE) { /* Is number of bytes in the receive buffer lower than size of buffer? */
    Uart_InpLen++;                     /* Increse number of chars in the receive buffer */
    InpBuffer[InpIndxW] = Data;        /* Save received char to the receive buffer */
    InpIndxW = (byte)((InpIndxW+1) & (Uart_INP_BUF_SIZE-1)); /* Update index */
    OnFlags |= ON_RX_CHAR;             /* Set flag "OnRXChar" */
    if(Uart_InpLen== Uart_INP_BUF_SIZE) { /* Is number of bytes in the receive buffer equal as a size of buffer? */
      OnFlags |= ON_FULL_RX;           /* If yes then set flag "OnFullRxBuff" */
    }
  } else {
    SerFlag |= FULL_RX;                /* If yes then set flag buffer overflow */
    OnFlags |= ON_ERROR;               /* Set flag "OnError" */
  }
  if(OnFlags & ON_ERROR) {             /* Is OnError flag set? */
    Uart_OnError();                    /* If yes then invoke user event */
  }
  else {
    if(OnFlags & ON_RX_CHAR) {         /* Is OnRxChar flag set? */
      Uart_OnRxChar();                 /* If yes then invoke user event */
    }
    if(OnFlags & ON_FULL_RX) {         /* Is OnFullRxBuf flag set? */
      Uart_OnFullRxBuf();              /* If yes then invoke user event */
    }
  }
}

/*
** ===================================================================
**     Method      :  Uart_InterruptTx (bean AsynchroSerial)
**
**     Description :
**         The method services the receive interrupt of the selected 
**         peripheral(s) and eventually invokes the bean's event(s).
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
#define ON_FREE_TX  1
#define ON_TX_CHAR  2
ISR(Uart_InterruptTx)
{
  byte OnFlags = 0;                    /* Temporary variable for flags */

  if(SerFlag & RUNINT_FROM_TX) {       /* Is flag "running int from TX" set? */
    OnFlags |= ON_TX_CHAR;             /* Set flag "OnTxChar" */
  }
  SerFlag &= ~RUNINT_FROM_TX;          /* Reset flag "running int from TX" */
  if(Uart_OutLen) {                    /* Is number of bytes in the transmit buffer greater than 0? */
    Uart_OutLen--;                     /* Decrease number of chars in the transmit buffer */
    SerFlag |= RUNINT_FROM_TX;         /* Set flag "running int from TX"? */
    (void)SCI1S1;                      /* Reset interrupt request flag */
    SCI1D = OutBuffer[OutIndxR];       /* Store char to transmitter register */
    OutIndxR = (byte)((OutIndxR+1) & (Uart_OUT_BUF_SIZE-1)); /* Update index */
  }
  else {
    OnFlags |= ON_FREE_TX;             /* Set flag "OnFreeTxBuf" */
    SCI1C2_TIE = 0;                    /* Disable transmit interrupt */
  }
  if(OnFlags & ON_TX_CHAR) {           /* Is flag "OnTxChar" set? */
    Uart_OnTxChar();                   /* If yes then invoke user event */
  }
  if(OnFlags & ON_FREE_TX) {           /* Is flag "OnFreeTxBuf" set? */
    Uart_OnFreeTxBuf();                /* If yes then invoke user event */
  }
}

/*
** ===================================================================
**     Method      :  Uart_InterruptError (bean AsynchroSerial)
**
**     Description :
**         The method services the error interrupt of the selected 
**         peripheral(s) and eventually invokes the bean's event(s).
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
ISR(Uart_InterruptError)
{
  byte StatReg = getReg(SCI1S1);

  (void)SCI1D;                         /* Dummy read of data register - clear error bits */
  if(StatReg & (SCI1S1_OR_MASK|SCI1S1_NF_MASK|SCI1S1_FE_MASK|SCI1S1_PF_MASK)) { /* Is an error detected? */
    SerFlag |= COMMON_ERR;             /* If yes then set an internal flag */
    Uart_OnError();                    /* Invoke user event */
  }
}

/*
** ===================================================================
**     Method      :  Uart_Init (bean AsynchroSerial)
**
**     Description :
**         Initializes the associated peripheral(s) and the bean internal 
**         variables. The method is called automatically as a part of the 
**         application initialization code.
**         This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
void Uart_Init(void)
{
  SerFlag = 0;                         /* Reset flags */
  Uart_InpLen = 0;                     /* No char in the receive buffer */
  InpIndxR = InpIndxW = 0;             /* Reset indices */
  Uart_OutLen = 0;                     /* No char in the transmit buffer */
  OutIndxR = OutIndxW = 0;             /* Reset indices */
  /* SCI1C1: LOOPS=0,SCISWAI=0,RSRC=0,M=0,WAKE=0,ILT=0,PE=0,PT=0 */
  setReg8(SCI1C1, 0x00);               /* Configure the SCI */ 
  /* SCI1C3: R8=0,T8=0,TXDIR=0,TXINV=0,ORIE=0,NEIE=0,FEIE=0,PEIE=0 */
  setReg8(SCI1C3, 0x00);               /* Disable error interrupts */ 
  /* SCI1S2: LBKDIF=0,RXEDGIF=0,??=0,RXINV=0,RWUID=0,BRK13=0,LBKDE=0,RAF=0 */
  setReg8(SCI1S2, 0x00);                
  /* SCI1C2: TIE=0,TCIE=0,RIE=0,ILIE=0,TE=0,RE=0,RWU=0,SBK=0 */
  setReg8(SCI1C2, 0x00);               /* Disable all interrupts */ 
  SCI1BDH = 0x00;                      /* Set high divisor register (enable device) */
  SCI1BDL = 0x4E;                      /* Set low divisor register (enable device) */
      /* SCI1C3: ORIE=1,NEIE=1,FEIE=1,PEIE=1 */
  SCI1C3 |= 0x0F;                      /* Enable error interrupts */
  SCI1C2 |= ( SCI1C2_TE_MASK | SCI1C2_RE_MASK | SCI1C2_RIE_MASK); /*  Enable transmitter, Enable receiver, Enable receiver interrupt */
}



/* END Uart. */


/*
** ###################################################################
**
**     This file was created by UNIS Processor Expert 3.03 [04.07]
**     for the Freescale HCS08 series of microcontrollers.
**
** ###################################################################
*/

⌨️ 快捷键说明

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