freescale

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

TXT
631
字号
}

/*
** ===================================================================
**     Method      :  I2C_Slave_SendChar (component InternalI2C)
**
**     Description :
**         When working as a MASTER, this method writes one (7-bit
**         addressing) or two (10-bit addressing) slave address
**         bytes inclusive of R/W bit = 0 to the I2C bus and then
**         writes one character (byte) to the bus. The slave address
**         must be specified before, by the "SelectSlave" or
**         "SelectSlave10" method or in the bean initialization
**         section, "Target slave address init" property. If
**         interrupt service is enabled and the method returns
**         ERR_OK, it doesn't mean that transmission was successful.
**         The state of transmission is obtainable from
**         (OnTransmitData, OnError or OnArbitLost) events. 
**         When working as a SLAVE, this method writes a character
**         to the internal output slave buffer and, after the master
**         starts the communication, to the I2C bus. If no character
**         is ready for a transmission (internal output slave buffer
**         is empty), the Empty character will be sent (see "Empty
**         character" property).
**     Parameters  :
**         NAME            - DESCRIPTION
**         Chr             - Character to send.
**     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 transmit data
**                           ERR_TXFULL - Transmitter is full (slave
**                           mode only)
**                           ERR_ARBITR - Arbitration lost (only when
**                           interrupt service is disabled and in
**                           master mode)
** ===================================================================
*/
byte I2C_Slave_SendChar(byte Chr)
{
  ChrTemp = Chr;                       /* Save character */
  return (I2C_Slave_SendBlock(&ChrTemp, (word)1, (word*)&I2C_Slave_SndRcvTemp)); /* Send character and return */
}

/*
** ===================================================================
**     Method      :  I2C_Slave_RecvChar (component InternalI2C)
**
**     Description :
**         When working as a MASTER, this method writes one (7-bit
**         addressing) or two (10-bit addressing) slave address
**         bytes inclusive of R/W bit = 1 to the I2C bus, then reads
**         one character (byte) from the bus and then sends the stop
**         condition. The slave address must be specified before, by
**         the "SelectSlave" or "SelectSlave10" method or in bean
**         initialization section, property "Target slave address
**         init". If interrupt service is enabled and the method
**         returns ERR_OK, it doesn't mean that transmission was
**         finished successfully. The state of transmission must be
**         tested by means of events (OnReceiveData, OnError or
**         OnArbitLost). In case of successful transmission,
**         received data is ready after OnReceiveData event is
**         called. 
**         When working as a SLAVE, this method reads a character
**         from the input slave buffer.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Chr             - Received character.
**     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 the acknowledge
**                           (only in master mode and when interrupt
**                           service is disabled)
**                           ERR_BUSOFF - Clock timeout elapsed or
**                           device cannot receive data
**                           ERR_RXEMPTY - No data in receiver (slave
**                           mode only)
**                           ERR_OVERRUN - Overrun error was detected
**                           from the last character or block
**                           received (slave mode only)
**                           ERR_ARBITR - Arbitration lost (only when
**                           interrupt service is disabled and in
**                           master mode)
**                           ERR_NOTAVAIL - Method is not available
**                           in current mode - see generated code
**                           comment
** ===================================================================
*/
/*
byte I2C_Slave_RecvChar(byte *Chr)

**  This method is implemented as a macro. See I2C_Slave.h file.  **
*/

/*
** ===================================================================
**     Method      :  I2C_Slave_SendBlock (component InternalI2C)
**
**     Description :
**         When working as a MASTER, this method writes one (7-bit
**         addressing) or two (10-bit addressing) slave address
**         bytes inclusive of R/W bit = 0 to the I2C bus and then
**         writes the block of characters to the bus. The slave
**         address must be specified before, by the "SelectSlave" or
**         "SlaveSelect10" method or in bean initialization section,
**         "Target slave address init" property. If interrupt
**         service is enabled and the method returns ERR_OK, it
**         doesn't mean that transmission was successful. The state
**         of transmission is detectable by means of events
**         (OnTransmitData, OnError or OnArbitLost). Data to be send
**         is not copied to an internal buffer and remains in the
**         original location. Therefore the content of the buffer
**         should not be changed until the transmission is complete.
**         Event OnTransmitData can be used to detect the end of the
**         transmission.
**         When working as a SLAVE, this method writes a block of
**         characters to the internal output slave buffer and then,
**         after the master starts the communication, to the I2C bus.
**         If no character is ready for a transmission (internal
**         output slave buffer is empty), the "Empty character" will
**         be sent (see "Empty character" property). In SLAVE mode
**         the data are copied to an internal buffer, if specified
**         by "Output buffer size" property.
**     Parameters  :
**         NAME            - DESCRIPTION
**       * Ptr             - Pointer to the block of data to send.
**         Siz             - Size of the block.
**       * Snt             - Amount of data sent (moved to a buffer).
**                           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 the acknowledge
**                           (only in master mode and when interrupt
**                           service is disabled)
**                           ERR_BUSOFF - Clock timeout elapsed or
**                           device cannot transmit data
**                           ERR_TXFULL - Transmitter is full. Some
**                           data has not been sent. (slave mode only)
**                           ERR_ARBITR - Arbitration lost (only when
**                           interrupt service is disabled and in
**                           master mode)
** ===================================================================
*/
byte I2C_Slave_SendBlock(void * Ptr,word Siz,word *Snt)
{
  word count;                          /* Number of sent chars */

  if (!Siz) {                          /* Test variable Size on zero */
    *Snt = 0;
    return ERR_OK;                     /* If zero then OK */
  }
  EnterCritical();                     /* Enter the critical section */
  *Snt = 0;                            /* Return number of sent chars */
  for(count=0; count < Siz; count++) {
    if(OutLenS < 8) {                  /* Is number of bytes in the transmit buffer lower than size of buffer? */
      OutLenS++;                       /* Increase number of bytes in the transmit buffer */
      OutBufferS[OutPtrWS++] = *((byte *)Ptr + count); /* Store char to buffer */
      if (OutPtrWS >= 8) {             /* Is the pointer out of the transmit buffer? */
        OutPtrWS = 0;                  /* Set pointer to the first item in the transmit buffer */
      }
      (*Snt)++;                        /* Increment number of sent chars */
    }
    else {
      ExitCritical();                  /* Exit the critical section */
      return ERR_TXFULL;               /* Return with error */
    }
  }
  ExitCritical();                      /* Exit the critical section */
  return ERR_OK;                       /* OK */
}

/*
** ===================================================================
**     Method      :  I2C_Slave_RecvBlock (component InternalI2C)
**
**     Description :
**         When working as a MASTER, this method writes one (7-bit
**         addressing) or two (10-bit addressing) slave address
**         bytes inclusive of R/W bit = 1 to the I2C bus, then reads
**         the block of characters from the bus and then sends the
**         stop condition. The slave address must be specified
**         before, by the "SelectSlave" or "SelectSlave10" method or
**         in bean initialization section, "Target slave address
**         init" property. If interrupt service is enabled and the
**         method returns ERR_OK, it doesn't mean that transmission
**         was finished successfully. The state of transmission must
**         be tested by means of events (OnReceiveData, OnError or
**         OnArbitLost). In case of successful transmission,
**         received data is ready after OnReceiveData event is
**         called. 

⌨️ 快捷键说明

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