MSPM0G1X0X_G3X0X TI-Driver Library  2.01.00.03
I2CTarget.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023-2024, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*!*****************************************************************************
33  * @file I2CTarget.h
34  *
35  * @brief I2CTarget driver interface
36  *
37  * @defgroup I2CTarget Inter-Integrated Circuit Target (I2CTarget)
38  *
39  * To use the I2CTarget driver, ensure that the correct driver library for your
40  * device is linked in and include this header file as follows:
41  * @code
42  * #include <ti/drivers/I2CTarget.h>
43  * @endcode
44  *
45  * @anchor ti_drivers_I2C_Target_Overview
46  * # Overview
47  *
48  * The I2CTarget driver operates the I2C peripheral as a target on an I2C bus.
49  * The I2CTarget driver is event driven. When an @ref I2CTarget_Event occurs,
50  * the I2CTarget driver will call a user-specified callback function
51  * (from HWI context). It is the user-specified callback's responsibility to
52  * process the @ref I2CTarget_Event events, and to manipulate and interperet
53  * the data. The manual ACK has been used. If automatic FIFO reception is desired
54  * disable the manual ACK using DL_I2C_disableTargetACKOverride().
55  *
56  * <hr>
57  * @anchor ti_drivers_I2CTARGET_Usage
58  * # Usage
59  *
60  * This documentation provides a basic @ref ti_drivers_I2CTARGET_Synopsis
61  * "usage summary" and a set of @ref ti_drivers_I2CTARGET_Examples "examples"
62  * in the form of commented code fragments. Detailed descriptions of the
63  * APIs are provided in subsequent sections.
64  *
65  * @anchor ti_drivers_I2CTARGET_Synopsis
66  * ## Synopsis
67  * @anchor ti_drivers_I2CTARGET_Synopsis_Code
68  * @code
69  * // Import the I2CTarget driver definitions
70  * #include <ti/drivers/I2CTarget.h>
71  *
72  * // Define own I2C bus address
73  * #define OWN_ADDRESS 0x48
74  *
75  * // Initialize I2CTarget driver
76  * I2CTarget_init();
77  *
78  * // Initialize I2CTarget parameters
79  * I2CTarget_Params params;
80  * I2CTarget_Params_init(&params);
81  * params.eventCallbackFxn = myEventCallbackFunction;
82  * params.targetAddress = OWN_ADDRESS;
83  *
84  * // Open the I2CTarget driver
85  * I2CTarget_Handle i2c;
86  * i2c = I2CTarget_open(CONFIG_I2C_TARGET_0, &params);
87  * if (i2c == NULL) {
88  * // Error opening I2CTarget
89  * while (1) {}
90  * }
91  *
92  * // Start I2C Target operation. The driver will send
93  * // events to the user-specified event callback function
94  * I2CTarget_start(i2c);
95  *
96  * // Transmitting and receiving bytes is handled by the
97  * // user-specified callback function. It is up to the user
98  * // to implement the appropriate state machine for its I2C
99  * // protocol.
100  *
101  * // Stop operation. The I2CTarget driver will no longer trigger
102  * // events and the device can enter low-power modes.
103  * I2CTarget_stop(i2c);
104  *
105  * // Close the I2CTarget driver
106  * I2CTarget_close(i2c);
107  * @endcode
108  *
109  * <hr>
110  * @anchor ti_drivers_I2CTARGET_Examples
111  * # Examples
112  * @anchor ti_drivers_I2CTARGET_Example_open
113  * ## Opening the I2CTarget Driver
114  *
115  * Opening a I2CTarget requires four steps:
116  * 1. Create and initialize a @ref I2CTarget_Params structure.
117  * 2. Fill in the desired parameters.
118  * 3. Call @ref I2CTarget_open(), passing the index of the I2C in the
119  * I2CTarget_config structure, and the address of the @ref I2CTarget_Params
120  * structure. The I2C peripheral instance is specified by the index in
121  * the I2CTarget_config structure.
122  * 4. Check that the I2CTarget handle returned by @ref I2CTarget_open() is non-NULL,
123  * and save it. The handle will be used to start and stop I2C Target operation
124  * of the I2C instance you just opened.
125  *
126  * @code
127  * I2CTarget_Handle i2c;
128  * I2CTarget_Params params;
129  * params.eventCallbackFxn = myEventCallbackFunction;
130  * params.targetAddress = 0x48;
131  *
132  * i2c = I2CTarget_open(CONFIG_I2C_TARGET_0, NULL);
133  *
134  * if (i2c == NULL) {
135  * // Error opening I2CTarget
136  * while (1) {}
137  * }
138  * @endcode
139  *
140  * Only one I2CTarget index can be used at a time; calling @ref I2CTarget_open() a
141  * second time with the same index previosly passed to @ref I2CTarget_open() will
142  * result in an error. You can, though, re-use the index if the instance is closed
143  * via @ref I2CTarget_close().
144  * In the previous example code, CONFIG_I2C_TARGET_0 is passed to @ref I2CTarget_open().
145  * This macro is defined in the example's ti_drivers_config.h file.
146  *
147  * @anchor ti_drivers_I2CTARGET_Example_start_stop_operation
148  * ## Start/Stop I2C Operation #
149  * The I2CTarget driver will start operation when calling @ref I2CTarget_start().
150  * It is the application's responsibility to process all incoming
151  * @ref I2CTarget_Event events in the @ref I2CTarget_Params.eventCallbackFxn and
152  * implement the necessary state machine for the I2C protocol used.
153  *
154  * @code
155  * I2CTarget_start(i2cTargetHandle);
156  * @endcode
157  *
158  * The I2CTarget driver operation is stopped by calling @ref I2CTarget_stop().
159  *
160  * @code
161  * I2CTarget_stop(i2cTargetHandle);
162  * @endcode
163  *
164  * @anchor ti_drivers_I2CTARGET_Example_receive_byte
165  * ## Transferring data
166  * This example shows a simplified example on how to transmit and receive data.
167  *
168  * @code
169  * // No command input
170  * #define NO_COMMAND 0x00
171  * // Define command to reset value of txData
172  * #define CMD_RESET_TXDATA 0x55
173  *
174  * // Variable storing data to transmit
175  * volatile uint8_t txData = 0;
176  *
177  * // Variable storing command received from an I2C controller
178  * volatile uint8_t command = 0;
179  *
180  * // User callback function
181  * int_fast16_t targetCallbackFxn(I2CTarget_Handle handle, I2CTarget_Event event, uint8_t *val) {
182  * if (event == I2CTarget_Event_WRITE_REQUESTED)
183  * {
184  * // Early event (before data is received) that the controller wants
185  * // to write a byte to us (target-receiver).
186  * // This event may be used to set up receive buffers, or other
187  * // preparations.
188  * }
189  *
190  * if (event == I2CTarget_Event_WRITE_RECEIVED) {
191  * // Controller has written a byte to the target (target-receiver)
192  * command = *data;
193  *
194  * // Tell driver data has been read
195  * return I2CTarget_STATUS_SUCCESS;
196  * }
197  *
198  * if (event == I2CTarget_Event_READ_REQUESTED || event == I2CTarget_Event_READ_PROCESSED) {
199  * // Controller wants to read from target (target-transmitter)
200  * *data = txData++;
201  *
202  * // Tell driver that data has been provided.
203  * return I2CTarget_STATUS_SUCCESS;
204  *
205  * // If the user callback does not have data, return
206  * // I2CTarget_STATUS_ERROR and the I2CTarget driver will
207  * // NACK the request (if supported by the device-specific driver implementation).
208  * }
209  *
210  * if (event == I2CTarget_Event_STOP) {
211  * // Stop condition received. Here we could, for example, reset
212  * // our I2C protocol state machine.
213  * }
214  *
215  * // Return success by default
216  * return I2CTarget_STATUS_SUCCESS;
217  * }
218  *
219  * // Main thread
220  * void *mainThread(void *arg0) {
221  * // Call driver init functions
222  * I2CTarget_init();
223  *
224  * // Open I2CTarget driver
225  * I2CTarget_Params params;
226  * I2CTarget_Params_init(&params);
227  * params.eventCallbackFxn = targetCallbackFxn;
228  * params.targetAddress = 0x48;
229  * I2CTarget_Handle handle = I2CTarget_open(CONFIG_I2C_TARGET_0, &params);
230  * if (handle == NULL) {
231  * // Error
232  * while (1) {}
233  * }
234  *
235  * // Start operation
236  * I2CTarget_start(handle);
237  *
238  * // Data transfers happens in targetCallbackFxn
239  * while (1) {
240  * if (command == CMD_RESET_TXDATA) {
241  * command = NO_COMMAND;
242  * txData = 0;
243  * }
244  * }
245  * }
246  * @endcode
247  *
248  * <hr>
249  * @anchor ti_drivers_I2CTarget_Configuration
250  * # Configuration
251  *
252  * Refer to the driver_configuration "Driver's Configuration" section
253  * for driver configuration information.
254  * <hr>
255  *
256  * ============================================================================
257  */
261 /* clang-format off */
262 
263 #ifndef ti_drivers_I2CTARGET__include
264 #define ti_drivers_I2CTARGET__include
265 
266 #ifdef __cplusplus
267 extern "C" {
268 #endif
269 
271 #include <stdint.h>
272 
273 #include <ti/drivers/dpl/HwiP.h>
274 #include <ti/drivers/dpl/SemaphoreP.h>
275 #include <ti/driverlib/dl_i2c.h>
289 #define I2CTarget_STATUS_SUCCESS (0)
290 
296 #define I2CTarget_STATUS_ERROR (-1)
297 
302 #define I2C_TARGET_OWN_ADDRESS (0x48)
303 
308 
313 typedef enum
314 {
320 
326 
333 
342 
348 
353 typedef enum
354 {
355  I2CTarget_State_STOPPED, /* I2CTarget driver is not started. */
356  I2CTarget_State_IDLE, /* I2CTarget driver is started, no ongoing transactions */
357  I2CTarget_State_RECEIVE, /* I2CTarget driver is receiving data */
358  I2CTarget_State_TRANSMIT /* I2CTarget driver is transmitting */
359 } I2CTarget_State;
376 typedef int_fast16_t (*I2CTarget_EventCallbackFxn)(I2CTarget_Handle handle, I2CTarget_Event event, uint8_t *val);
377 
386 typedef struct
387 {
391  uint_least16_t targetAddress;
393  void *custom;
395 
397 #define I2CTARGET_BASE_OBJECT \
398  /* I2CTarget control variables */ \
399  I2CTarget_EventCallbackFxn eventCallbackFxn; /* Callback function pointer */ \
400  uint_least16_t targetAddress; /* Device's own address on the I2C bus */ \
401  I2CTarget_State currentState; /* Internal state machine */ \
402  \
403  /* I2CTarget RTOS objects */ \
404  HwiP_Struct hwi; /* Hwi object handle */ \
405  SemaphoreP_Struct mutex; /* Grants exclusive access to I2C module */ \
406  \
407  bool isOpen; /* Flag to show module is open */ \
408 
415 typedef struct
416 {
417  I2CTARGET_BASE_OBJECT
418 } I2CTarget_Object;
422 #define I2CTARGET_BASE_HWATTRS \
423  \
424  I2C_Regs *i2c; \
425  \
426  uint32_t intNum; \
427  \
440  uint32_t intPriority; \
441 
442 
448 typedef struct
449 {
450  I2CTARGET_BASE_HWATTRS
451 } I2CTarget_HWAttrs;
465 typedef struct I2CTarget_Config_
466 {
468  void *object;
469 
471  void const *hwAttrs;
473 
475 extern const I2CTarget_Config I2CTarget_config[];
477 extern const uint_least8_t I2CTarget_count;
478 
487 extern void I2CTarget_init(void);
488 
499 extern void I2CTarget_Params_init(I2CTarget_Params *params);
500 
523 extern I2CTarget_Handle I2CTarget_open(uint_least8_t index, I2CTarget_Params *params);
524 
532 extern void I2CTarget_start(I2CTarget_Handle handle);
533 
541 extern void I2CTarget_stop(I2CTarget_Handle handle);
542 
553 extern void I2CTarget_close(I2CTarget_Handle handle);
554 
555 #ifdef __cplusplus
556 }
557 #endif
558 
559 #endif /* ti_drivers_I2CTARGET__include */
560 /* clang-format on */
void I2CTarget_Params_init(I2CTarget_Params *params)
Function to initialize the I2CTarget_Params struct to its defaults.
int_fast16_t(* I2CTarget_EventCallbackFxn)(I2CTarget_Handle handle, I2CTarget_Event event, uint8_t *val)
I2CTarget event callback function.
Definition: I2CTarget.h:376
const uint_least8_t I2CTarget_count
struct I2CTarget_Config_ I2CTarget_Config
I2CTarget Global configuration.
void I2CTarget_init(void)
Function to initializes the I2CTarget module.
void I2CTarget_stop(I2CTarget_Handle handle)
Stop I2CTarget driver from listening on I2C bus.
Definition: I2CTarget.h:341
I2CTarget_Event
I2CTarget events that the application&#39;s I2CTarget_EventCallbackFxn must support.
Definition: I2CTarget.h:313
Definition: I2CTarget.h:332
I2CTarget_EventCallbackFxn eventCallbackFxn
Definition: I2CTarget.h:389
void * custom
Definition: I2CTarget.h:393
I2CTarget Global configuration.
Definition: I2CTarget.h:465
I2CTarget_Handle I2CTarget_open(uint_least8_t index, I2CTarget_Params *params)
Function to initialize a given I2C target peripheral specified by the particular index value...
Definition: I2CTarget.h:319
void I2CTarget_start(I2CTarget_Handle handle)
Start I2CTarget driver listening on I2C bus.
const I2CTarget_Config I2CTarget_config[]
I2CTarget Parameters.
Definition: I2CTarget.h:386
void * object
Definition: I2CTarget.h:468
uint_least16_t targetAddress
Definition: I2CTarget.h:391
struct I2CTarget_Config_ * I2CTarget_Handle
A handle that is returned from a I2CTarget_open() call.
Definition: I2CTarget.h:307
void const * hwAttrs
Definition: I2CTarget.h:471
Definition: I2CTarget.h:346
void I2CTarget_close(I2CTarget_Handle handle)
Definition: I2CTarget.h:325
© Copyright 1995-2024, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale