freescale

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

TXT
631
字号
**         When working as a SLAVE, this method reads a block of
**         characters from the input slave buffer.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Ptr             - A pointer to the block space for
**                           received data.
**         Siz             - The size of the block.
**       * Rcv             - Amount of received data. In master mode,
**                           if interrupt support is enabled, the
**                           parameter always returns the same value
**                           as the parameter 'Siz' of this method.
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
**                           ERR_DISABLED -  Device is disabled
**                           ERR_BUSY - The slave device is busy, it
**                           does not respond by an acknowledge (only
**                           in master mode and when interrupt
**                           service is disabled)
**                           ERR_BUSOFF - Clock timeout elapsed or
**                           device cannot receive data
**                           ERR_RXEMPTY - The receive buffer didn't
**                           contain the requested number of data.
**                           Only available data (or no data) has
**                           been returned  (slave mode only).
**                           ERR_OVERRUN - Overrun error was detected
**                           from last character or block receiving
**                           (slave mode only)
**                           ERR_ARBITR - Arbitration lost (only when
**                           interrupt service is disabled and in
**                           master mode)
** ===================================================================
*/
byte I2C_Slave_RecvBlock(void* Ptr,word Siz,word *Rcv)
{
  word count;                          /* Number of sent chars */
  byte errrc = ERR_OK;                 /* Most serious error */

  if (!Siz) {                          /* Test variable Size on zero */
    *Rcv = 0;
    return ERR_OK;                     /* If zero then OK */
  }
  EnterCritical();                     /* Enter the critical section */
  if ((I2C_Slave_SerFlag & (OVERRUN_ERR | FULL_RX)) != 0) { /* Is the overrun occured? */
    errrc = ERR_OVERRUN;               /* If yes then return OVERRUN error */
  }
  I2C_Slave_SerFlag &= ~(OVERRUN_ERR | CHAR_IN_RX | FULL_RX); /* Clear flag "char in RX buffer" */
  *Rcv = 0;                            /* Clear number of received chars */
  for(count=0; count < Siz; count++) {
    if(InpLenS) {                      /* Is number of bytes in the receive buffer lower than size of buffer? */
      InpLenS--;                       /* Decrease number of received chars */
      *((byte *)Ptr + count) = InpBufferS[InpPtrRS++]; /* Read the char */
      if (InpPtrRS >= 8) {             /* Is the index out of the receive buffer? */
        InpPtrRS = 0;                  /* Set index to the first item into the receive buffer */
      }
      (*Rcv)++;                        /* Increment number of sent chars */
    }
    else {
      ExitCritical();                  /* Exit the critical section */
      if(errrc==ERR_OVERRUN) {
        return ERR_OVERRUN;            /* Return with error */
      }
      else {
        return ERR_RXEMPTY;            /* Return with error */
      }
    }
  }
  ExitCritical();                      /* Exit the critical section */
  return errrc;                        /* Return error code */
}

/*
** ===================================================================
**     Method      :  I2C_Slave_ClearTxBuf (component InternalI2C)
**
**     Description :
**         This method clears all characters in internal slave
**         output buffer. This method is not available for the
**         MASTER mode.
**     Parameters  : None
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte I2C_Slave_ClearTxBuf(void)
{
  EnterCritical();                     /* Enter the critical section */
  OutLenS = 0;                         /* Set number of chars in the receive buffer to 0 */
  OutPtrRS = OutPtrWS = 0;             /* Set indexes on the first item in the receive buffer */
  I2C_Slave_SerFlag &= ~(FULL_TX|CHAR_IN_TX);
  ExitCritical();                      /* Exit the critical section */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  I2C_Slave_ClearRxBuf (component InternalI2C)
**
**     Description :
**         This method clears all characters in internal slave input
**         buffer. This method is not available for the MASTER mode.
**     Parameters  : None
**     Returns     :
**         ---             - Error code, possible codes:
**                           ERR_OK - OK
**                           ERR_SPEED - This device does not work in
**                           the active speed mode
** ===================================================================
*/
byte I2C_Slave_ClearRxBuf(void)
{
  EnterCritical();                     /* Enter the critical section */
  InpLenS = 0;                         /* Set number of chars in the transmit buffer to 0 */
  InpPtrRS = InpPtrWS = 0;             /* Set indexes on the first item in the transmit buffer */
  I2C_Slave_SerFlag &= ~(OVERRUN_ERR | CHAR_IN_RX | FULL_RX);
  ExitCritical();                      /* Exit the critical section */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  I2C_Slave_GetCharsInTxBuf (component InternalI2C)
**
**     Description :
**         Returns number of characters in the output buffer. In
**         SLAVE mode returns the number of characters in the
**         internal slave output buffer. In MASTER mode returns
**         number of characters to be sent from the user buffer
**         (passed by SendBlock method).
**         This method is not supported in polling mode.
**     Parameters  : None
**     Returns     :
**         ---             - Number of characters in the output
**                           buffer.
** ===================================================================
*/
word I2C_Slave_GetCharsInTxBuf(void)
{
  return((word)OutLenS);               /* Return number of chars in the transmit buffer */
}

/*
** ===================================================================
**     Method      :  I2C_Slave_GetCharsInRxBuf (component InternalI2C)
**
**     Description :
**         Returns number of characters in the input buffer. In
**         SLAVE mode returns the number of characters in the
**         internal slave input buffer. In MASTER mode returns
**         number of characters to be received into a user buffer
**         (passed by RecvChar or RecvBlock method).
**         This method is not supported in polling mode.
**     Parameters  : None
**     Returns     :
**         ---             - Number of characters in the input
**                           buffer.
** ===================================================================
*/
word I2C_Slave_GetCharsInRxBuf(void)
{
  return((word)InpLenS);               /* Return number of chars in receive buffer */
}

/*
** ===================================================================
**     Method      :  I2C_Slave_Init (component InternalI2C)
**
**     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 I2C_Slave_Init(void)
{
  I2C_Slave_SerFlag = 0;               /* Reset all flags */
  InpLenS = 0;                         /* No char in the receive buffer */
  InpPtrWS = InpPtrRS = 0;             /* Set indexes on the first item in the receive buffer */
  OutLenS = 0;                         /* No char in the transmit buffer */
  OutPtrWS = OutPtrRS = 0;             /* Set indexes on the first item in the transmit buffer */
  /* IICA: AD7=0,AD6=0,AD5=0,AD4=0,AD3=0,AD2=1,AD1=0,??=0 */
  IICA = 0x04;                         /* Define slave address */
  /* IICC2: GCAEN=0,ADEXT=0,??=0,??=0,??=0,AD10=0,AD9=0,AD8=0 */
  IICC2 = 0x00;
  /* IICF: MULT1=1,MULT0=0,ICR5=0,ICR4=0,ICR3=1,ICR2=0,ICR1=0,ICR0=1 */
  setReg8(IICF, 0x89);                  
  IICC1_IICEN = 1;                     /* Enable device */
  /* IICC1: IICEN=1,IICIE=1,MST=0,TX=0,TXAK=0,RSTA=0,??=0,??=0 */
  IICC1 = 0xC0;                        /* Control register settings */
}


/* END I2C_Slave. */

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

⌨️ 快捷键说明

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