* A generic descriptor for an I2C transfer where every type of parameter of the transfer can be specified.
* This is passed to I2C_queueTransfer.
*/
typedefstruct_I2CgenericTransfer{
unsignedintslaveAddress;//!< Address of the slave where to make the transfer.
I2Cdirectiondirection;//!< Direction of the I2C transfer.
unsignedintwriteSize;//!< Number of bytes to be written to the I2C slave (used for write_i2cDir and writeRead_i2cDir).
unsignedintreadSize;//!< Number of bytes to be read from the I2C slave (used for read_i2cDir and writeRead_i2cDir).
unsignedchar*writeData;//!< Memory location of the data to be written to the I2C slave (used for write_i2cDir and writeRead_i2cDir).
volatileunsignedchar*readData;//!< Memory location to store the data read from the I2C slave (used for read_i2cDir and writeRead_i2cDir).
portTickTypewriteReadDelay;//!< A delay inserted between writing to an I2C slave and reading back from it (used only for writeRead_i2cDir).
I2CtransferStatus*result;//!< The driver will store the result of a transfer in this location at the end of the transfer.
// The following members are not inputs for blocking transfers.
xSemaphoreHandlesemaphore;//!< This will be passed to the callback. If the task is blocking on this semaphore for the transfer to complete, the callback can release it to unblock the task.
void(*callback)(SystemContextcontext,xSemaphoreHandlesem);//!< Name of a function that will be called when the transfer is complete.
// The following members are only used internally by the driver.
portTickTypewriteCompleteTime;//!< Internal use by driver
xTaskHandlecallingTaskHandle;//!< Internal use by driver
}I2CgenericTransfer;
/*!
* A descriptor for an I2C transfer that is passed to I2C_writeRead.
*/
typedefstruct_I2CwriteReadTransfer{
unsignedintslaveAddress;//!< Address of the slave where to make the transfer.
unsignedintwriteSize;//!< Number of bytes to be written to the I2C slave.
unsignedintreadSize;//!< Number of bytes to be read from the I2C slave.
unsignedchar*writeData;//!< Memory location of the data to be written to the I2C slave.
volatileunsignedchar*readData;//!< Memory location to store the data read from the I2C slave.
portTickTypewriteReadDelay;//!< A delay inserted between writing to an I2C slave and reading back from it.
}I2Ctransfer;
/*!
* Initializes the I2C driver.
* @param i2cBusSpeed_Hz Speed of the I2C bus in Hertz. (Minimum: 10000, Maximum 400000)
* @param i2cTransferTimeout Timeout for a byte to be transferred in 1/10th of a tick.
* @note When debugging the code use portMAX_DELAY to make this timeout infinite.
* Otherwise, stepping through code can trigger the I2C timeout.
* If this value is less than 1, the driver uses a timeout of 1.
* @return -3 if creating queues to handle transfers fails,
* -2 if Initializing the TWI peripheral fails,
* -1 if creating the task that consumes I2C transfer requests failed,
* Information used by the driver to automatically receive a fixed number of
* bytes. Note, the number of bytes specified should not include the command.
* itself. Any command not in this list is treated as an invalid command.
*
* This driver expects the following I2C transaction format:\n
* 1) Every request for data from the I2C master is preceded by a command that describes what data is being requested.
* Therefore, an I2C slave never tries to write data on the I2C bus on its own.\n
* 2) Commands may or may not have a response.
*/
typedefstruct_I2Cslave_CommandList{
unsignedcharcommand;//!< Command code (first byte sent by the I2C master).
unsignedintcommandParameterSize;//!< Number of bytes to read after receiving the command. If the command does not have any parameters, this should be 0.
BooleanhasResponse;//!< The user code replies to to this command with a response data packet i.e. I2Cslave_write is called in response to this command.
}I2CslaveCommandList;
/*!
*
* Sets up the I2C slave driver.
*
* @param address Address of the slave on the I2C bus.
* @param commandList Pointer to an array of I2CslaveCommandList structures which the driver
* queries to find out the number of bytes to automatically receive after receiving a certain command.
* @param commandListSize Number of commands in the above list.
* i.e. ((size of commandList array in bytes) / sizeof(I2CslaveCommandList))
*
* @return -5 when no command list is provided or its size is invalid or the address is not a 7-bit number,
* -4 if a commandParameterSize is too large (>=512),
* -3 if command codes in the list are not unique,
* -2 if memory allocation or starting tasks fails,
* -1 if pin configuration fails,
* 0 on success.
*
* @note The command codes in the commandList must be unique.
* In reality, the I2C slave can start receiving data as soon as it is initialized.
* This function simply waits until actual data is available and returns when the i2c master sends a command.
* @param[in,out] data A pointer to the location where the driver should store the received data. This buffer must be at least I2C_SLAVE_RECEIVE_BUFFER_SIZE bytes large.
* @return The number of bytes received as a command,
* if the return value is -1, the driver has not been initialized,
* if the return value is less than the number of bytes expected for the command code, the I2C-master terminated the transfer early.
*/
intI2Cslave_read(unsignedchar*data);
/*!
* @brief Retrieves the current state of the I2C driver.
* @return driver state.
*/
I2CslaveDriverStateI2Cslave_getDriverState(void);
/*!
* @brief Mutes the I2C interface from the higher layers but does not block the
* I2C bus.
*
* This function will cause the I2C driver to accept any amount of data from the
* master and reply with any amount of junk when requested by the master.
* This can be used to prevent the I2C bus from hanging when this subsystem is
* busy doing something that will make it unresponsive on the I2C bus.
*
* @param replyByte A byte that will be repeatedly transmitted when the
* master asks for data.
*
* @see I2Cslave_unMute()
*/
voidI2Cslave_mute(unsignedcharreplyByte);
/*!
* @brief Unmutes the I2C interface. This function should be called after calling
* I2C_Mute when the subsystem has finished doing tasks that made it unavailable