Example #1
0
/*
** ===================================================================
**     Method      :  Accel_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 component 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 Accel_SendBlock(void *Ptr,word Siz,word *Snt)
{
  LDD_TError Error;
  LDD_I2C_TErrorMask ErrorMask;
  word TmpSnt = 0x00U;
  word Tr;

  if (Siz == 0U) {                     /* Test variable Size on zero */
    *Snt = 0U;
    return ERR_OK;                     /* If zero then OK */
  }
  Error = IntI2cLdd1_MasterSendBlock(IntI2cLdd1_DeviceDataPtr, (LDD_TData *)Ptr, (LDD_I2C_TSize)Siz, LDD_I2C_NO_SEND_STOP); /* Send one data byte */
  if (Error == ERR_BUSY) {
    return ERR_BUSOFF;
  }
  for (Tr=0U; Tr<0x2710U; Tr++) {
    IntI2cLdd1_Main(IntI2cLdd1_DeviceDataPtr); /* Call main communication method */
    if (IntI2cLdd1_MasterGetBlockSentStatus(IntI2cLdd1_DeviceDataPtr)) { /* Are all a data transmitted? */
      break;
    }
    if (TmpSnt != (word)IntI2cLdd1_MasterGetSentDataNum(IntI2cLdd1_DeviceDataPtr)) { /* Is a data byte sent? */
      Tr = (word)-1;                   /* Clear trials */
      TmpSnt = (word)IntI2cLdd1_MasterGetSentDataNum(IntI2cLdd1_DeviceDataPtr); /* Set TmpSnt on a new value */
    }
  }
  *Snt = (word)IntI2cLdd1_MasterGetSentDataNum(IntI2cLdd1_DeviceDataPtr); /* Return sent data number */
  (void)IntI2cLdd1_GetError(IntI2cLdd1_DeviceDataPtr, &ErrorMask);
  if (ErrorMask != 0U) {               /* Is any error detected after transfer? */
    if ((ErrorMask & LDD_I2C_MASTER_NACK) != 0U) { /* If yes, decode error and return error code */
      return ERR_BUSY;
    } else if ((ErrorMask & LDD_I2C_ARBIT_LOST) != 0U) {
      return ERR_ARBITR;
    }
  }
  if (Tr == 0x2710U) {                 /* Is Tr == 'Polling trials'? */
    return ERR_BUSOFF;                 /* Return with error */
  }
  return ERR_OK;                       /* Return ERR_OK */
}
Example #2
0
/*
** ===================================================================
**     Method      :  CI2C1_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 CI2C1_GetCharsInTxBuf(void)
{
  return (word)(OutLenM - IntI2cLdd1_MasterGetSentDataNum(IntI2cLdd1_DeviceDataPtr)); /* Return number of chars remaining in the Master Tx buffer */
}