/* ** =================================================================== ** Method : Accel_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 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 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 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) ** ERR_NOTAVAIL - Method is not available in ** current mode - see the comment in the ** generated code. ** =================================================================== */ byte Accel_RecvBlock(void* Ptr,word Siz,word *Rcv) { LDD_TError Error; LDD_I2C_TErrorMask ErrorMask; LDD_I2C_TBusState BusState; word TmpRcv = 0x00U; word Tr; if (Siz == 0U) { /* Test variable Size on zero */ *Rcv = 0U; return ERR_OK; /* If zero then OK */ } Error = IntI2cLdd1_MasterReceiveBlock(IntI2cLdd1_DeviceDataPtr, (LDD_TData *)Ptr, (LDD_I2C_TSize)Siz, LDD_I2C_SEND_STOP); /* Receive a data block */ if (Error == ERR_BUSY) { return ERR_BUSOFF; } for (Tr=0U; Tr<0x2710U; Tr++) { IntI2cLdd1_Main(IntI2cLdd1_DeviceDataPtr); /* Call main communication method */ if (IntI2cLdd1_MasterGetBlockReceivedStatus(IntI2cLdd1_DeviceDataPtr)) { /* Are all a data received? */ break; } if (TmpRcv != (word)IntI2cLdd1_MasterGetReceivedDataNum(IntI2cLdd1_DeviceDataPtr)) { /* Is a data byte received? */ Tr = (word)-1; /* Clear trials counter */ TmpRcv = (word)IntI2cLdd1_MasterGetReceivedDataNum(IntI2cLdd1_DeviceDataPtr); /* Set TmpRcv on a new value */ } } *Rcv = (word)IntI2cLdd1_MasterGetReceivedDataNum(IntI2cLdd1_DeviceDataPtr); /* Return received 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 */ } for (Tr=0U; Tr<0x2710U; Tr++) { (void)IntI2cLdd1_CheckBus(IntI2cLdd1_DeviceDataPtr, &BusState); /* Get bus state */ if (BusState == LDD_I2C_IDLE) { /* Is stop condition generated after transfer? */ return ERR_OK; /* Return ERR_OK */ } } return ERR_BUSOFF; /* Return error */ }
/* ** =================================================================== ** Method : CI2C1_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 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 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 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) ** ERR_NOTAVAIL - Method is not available in ** current mode - see the comment in the ** generated code. ** =================================================================== */ byte CI2C1_RecvBlock(void* Ptr,word Siz,word *Rcv) { LDD_TError Error; if (Siz == 0U) { /* Test variable Size on zero */ *Rcv = 0U; return ERR_OK; /* If zero then OK */ } EnterCritical(); /* Enter the critical section */ Error = IntI2cLdd1_MasterReceiveBlock(IntI2cLdd1_DeviceDataPtr, (LDD_TData *)Ptr, (LDD_I2C_TSize)Siz, LDD_I2C_SEND_STOP); /* Receive a data block */ if (Error == ERR_BUSY) { ExitCritical(); /* Exit the critical section */ return ERR_BUSOFF; } ExitCritical(); /* Exit the critical section */ *Rcv = Siz; /* Dummy number of really received chars */ return ERR_OK; /* OK */ }