MSPM0GX51X TI-Driver Library
2.03.00.07
|
Modules | |
I2C status codes | |
I2C driver APIs | |
Common helper functions for I2C driver | |
Data Structures | |
struct | I2C_Transaction |
Defines a transaction to be used with I2C_transfer() or I2C_transferTimeout(). More... | |
struct | I2C_Params |
I2C parameters used with I2C_open(). More... | |
struct | I2C_Config_ |
I2C driver's configuration structure. More... | |
Macros | |
#define | I2C_WAIT_FOREVER (~(0U)) |
Wait forever define used to specify timeouts. | |
Typedefs | |
typedef struct I2C_Config_ * | I2C_Handle |
A handle that is returned from an I2C_open() call. | |
typedef void(* | I2C_CallbackFxn) (I2C_Handle handle, I2C_Transaction *transaction, bool transferStatus) |
The definition of a callback function. More... | |
typedef struct I2C_Config_ | I2C_Config |
I2C driver's configuration structure. More... | |
Enumerations | |
enum | I2C_TransferMode { I2C_MODE_BLOCKING, I2C_MODE_CALLBACK } |
Return behavior of I2C_Transfer() specified in the I2C_Params. More... | |
enum | I2C_BitRate { I2C_100kHz = 0, I2C_400kHz = 1, I2C_1000kHz = 2 } |
Bit rate for an I2C driver instance specified in the I2C_Params. More... | |
Functions | |
void | I2C_cancel (I2C_Handle handle) |
Cancels all I2C transfers. More... | |
void | I2C_close (I2C_Handle handle) |
Function to close an I2C driver instance. More... | |
int_fast16_t | I2C_control (I2C_Handle handle, uint_fast16_t cmd, void *controlArg) |
Function performs implementation specific features on a driver instance. More... | |
void | I2C_init (void) |
Function to initialize the I2C driver. More... | |
I2C_Handle | I2C_open (uint_least8_t index, I2C_Params *params) |
Open an I2C driver instance. More... | |
void | I2C_Params_init (I2C_Params *params) |
Initialize an I2C_Params structure to its default values. More... | |
int_fast16_t | I2C_setClockTimeout (I2C_Handle handle, uint32_t timeout) |
Set the I2C SCL clock timeout. More... | |
bool | I2C_transfer (I2C_Handle handle, I2C_Transaction *transaction) |
Perform an I2C transaction with an I2C target peripheral. More... | |
int_fast16_t | I2C_transferTimeout (I2C_Handle handle, I2C_Transaction *transaction, uint32_t timeout) |
Perform an I2C transaction with an I2C target peripheral. More... | |
Variables | |
const I2C_Config | I2C_config [] |
Instance of I2C driver's configuration structure. | |
const uint_least8_t | I2C_count |
Count of I2C instance. | |
The I2C header file should be included in an application as follows:
The I2C driver is designed to operate as an I2C controller and will not function as an I2C target. Multi-controller arbitration is not supported; therefore, this driver assumes it is the only I2C controller on the bus. This I2C driver's API set provides the ability to transmit and receive data over an I2C bus between the I2C controller and I2C target(s). The application is responsible for manipulating and interpreting the data.
This section provides a basic usage summary and a set of examples in the form of commented code fragments. Detailed descriptions of the I2C APIs and their effect are provided in subsequent sections.
After calling I2C_init(), the application can open an I2C instance by calling I2C_open().The following code example opens an I2C instance with default parameters by passing NULL
for the I2C_Params argument.
This final example shows usage of I2C_MODE_CALLBACK, with queuing of multiple transactions. Because multiple transactions are simultaneously queued, separate I2C_Transaction structures must be used. Each I2C_Transaction will contain a custom application argument of a semaphore handle. The I2C_Transaction.arg will point to the semaphore handle. When the callback function is called, the I2C_Transaction.arg is checked for NULL
. If this value is not NULL
, then it can be assumed the arg
is pointing to a valid semaphore handle. The semaphore handle is then used to call sem_post()
. Hypothetically, this can be used to signal transaction completion to the task(s) that queued the transaction(s).
Snippets of the thread code that initiates the transactions are shown below. Note the use of multiple I2C_Transaction structures. The handle of the semaphore to be posted is specified via i2cTransaction2.arg
. I2C_transfer() is called three times to initiate each transaction. Since callback mode is used, these functions return immediately. After the transactions have been queued, other work can be done. Eventually, sem_wait()
is called causing the thread to block until the transaction completes. When the transaction completes, the application's callback function, callbackFxn
will be called. Once I2C_CallbackFxn posts the semaphore, the thread will be unblocked and can resume execution.
typedef void(* I2C_CallbackFxn) (I2C_Handle handle, I2C_Transaction *transaction, bool transferStatus) |
The definition of a callback function.
When operating in I2C_MODE_CALLBACK, the callback function is called when an I2C_transfer() completes. The application is responsible for declaring an I2C_CallbackFxn function and providing a pointer in I2C_Params.transferCallbackFxn.
[out] | handle | I2C_Handle used with the initial call to I2C_transfer(). |
[out] | transaction | Pointer to the I2C_Transaction structure used with the initial call to I2C_transfer(). This structure contains the custom argument specified by transaction.arg and the transaction status. |
[out] | transferStatus | Boolean indicating if the I2C transaction was successful. If true , the transaction was successful. If false , the transaction failed. |
typedef struct I2C_Config_ I2C_Config |
I2C driver's configuration structure.
enum I2C_TransferMode |
Return behavior of I2C_Transfer() specified in the I2C_Params.
This enumeration defines the return behaviors for a call to I2C_transfer().
Enumerator | |
---|---|
I2C_MODE_BLOCKING | In I2C_MODE_BLOCKING, calls to I2C_transfer() block until the I2C_Transaction completes. Other threads calling I2C_transfer() while a transaction is in progress are also placed into a blocked state. If multiple threads are blocked, the thread with the highest priority will be unblocked first. This implies that arbitration will not be executed in chronological order.
|
I2C_MODE_CALLBACK | In I2C_MODE_CALLBACK, calls to I2C_transfer() return immediately. The application's callback function, I2C_Params.transferCallbackFxn, is called when the transaction is complete. Sequential calls to I2C_transfer() will place I2C_Transaction structures into an internal queue. Queued transactions are automatically started after the previous transaction has completed. This queuing occurs regardless of any error state from previous transactions. The transactions are always executed in chronological order. The I2C_Params.transferCallbackFxn function will be called asynchronously as each transaction is completed. |
enum I2C_BitRate |
Bit rate for an I2C driver instance specified in the I2C_Params.
This enumeration defines the bit rates used with an I2C_transfer().
Enumerator | |
---|---|
I2C_100kHz | I2C Standard-mode. Up to 100 kbit/s. |
I2C_400kHz | I2C Fast-mode. Up to 400 kbit/s. |
I2C_1000kHz | I2C Fast-mode Plus. Up to 1Mbit/s. |
void I2C_cancel | ( | I2C_Handle | handle | ) |
Cancels all I2C transfers.
This function will cancel asynchronous I2C_transfer() operations by generating a STOP condition on the I2C bus.
Calls to I2C_cancel() return immediately; however, the transaction may not yet be canceled.
For I2C_MODE_BLOCKING, the current transaction is canceled.
For I2C_MODE_CALLBACK mode, the in progress transfer, as well as any queued transfers, will be canceled. The individual callback functions for each transfer will be called in chronological order. The callback functions are called in an interrupt context. Additional calls to I2C_transfer() invoked from the callback function of a canceled transaction will always fail. In such cases, the I2C_Transaction.status will indicate I2C_STATUS_INVALID_TRANS.
A canceled transaction may be identified when the I2C_Transaction.status is set to I2C_STATUS_CANCEL.
[in] | handle | An I2C_Handle returned from I2C_open() |
void I2C_close | ( | I2C_Handle | handle | ) |
Function to close an I2C driver instance.
[in] | handle | An I2C_Handle returned from I2C_open(). |
int_fast16_t I2C_control | ( | I2C_Handle | handle, |
uint_fast16_t | cmd, | ||
void * | controlArg | ||
) |
Function performs implementation specific features on a driver instance.
[in] | handle | An I2C_Handle returned from I2C_open(). |
[in] | cmd | A command value defined by the device specific implementation. |
[in] | controlArg | An optional R/W (read/write) argument that is accompanied with cmd |
I2C_STATUS_SUCCESS | The call was successful. |
I2C_STATUS_UNDEFINEDCMD | The cmd value is not supported by the device specific implementation. |
void I2C_init | ( | void | ) |
Function to initialize the I2C driver.
This function must also be called before any other I2C driver APIs.
I2C_Handle I2C_open | ( | uint_least8_t | index, |
I2C_Params * | params | ||
) |
Open an I2C driver instance.
[in] | index | Index in the I2C_Config [] array. |
[in] | params | Pointer to an initialized I2C_Params structure. If NULL, the default I2C_Params values are used. |
NULL
on an error.void I2C_Params_init | ( | I2C_Params * | params | ) |
Initialize an I2C_Params structure to its default values.
[in] | params | A pointer to I2C_Params structure for initialization. |
Defaults values are:
int_fast16_t I2C_setClockTimeout | ( | I2C_Handle | handle, |
uint32_t | timeout | ||
) |
Set the I2C SCL clock timeout.
An I2C target can extend a I2C transaction by periodically pulling the clock low to create a slow bit transfer rate. The application can use this API to program a counter in the I2C module. The count is used to force a timeout if an I2C target holds the clock line low for longer than the timeout
duration. An I2C_STATUS_CLOCK_TIMEOUT status indicates a timeout event occured.
[in] | handle | An I2C_Handle returned from I2C_open() |
[in] | timeout | Timeout in units of I2C clock cycles. Refer to the device specifc reference manual to determine how to calculate the timeout value. |
bool I2C_transfer | ( | I2C_Handle | handle, |
I2C_Transaction * | transaction | ||
) |
Perform an I2C transaction with an I2C target peripheral.
This function will perform an I2C transfer, as specified by an I2C_Transaction structure.
[in] | handle | An I2C_Handle returned from I2C_open() |
[in] | transaction | A pointer to an I2C_Transaction. The application is responsible for allocating and initializing an I2C_Transaction structure prior to passing it to I2C_Transfer(). This structure must persist in memory unmodified until the transfer is complete. |
true
for a successful transfer; false
for an error (for example, an I2C bus fault (NACK)).true
. The I2C_CallbackFxn bool
argument will be true
to indicate success, and false
to indicate an error.int_fast16_t I2C_transferTimeout | ( | I2C_Handle | handle, |
I2C_Transaction * | transaction, | ||
uint32_t | timeout | ||
) |
Perform an I2C transaction with an I2C target peripheral.
This function will perform an I2C transfer, as specified by an I2C_Transaction structure. If the timeout is exceeded, then the I2C transaction is canceled.
[in] | handle | An I2C_Handle returned from I2C_open() |
[in] | transaction | A pointer to an I2C_Transaction. The application is responsible for allocating and initializing an I2C_Transaction structure prior to passing it to I2C_TransferTimeout(). This structure must persist in memory unmodified until the transfer is complete. |
[in] | timeout | The time in system ticks to wait for the transaction to complete. Passing I2C_WAIT_FOREVER into this parameter will cause I2C_transferTimeout() to behave the same as I2C_transfer() but with a more detailed return status. |
I2C_STATUS_SUCCESS
. The I2C_CallbackFxn transferStatus
argument will be true
to indicate success, and false
to indicate an error.