I2CTarget.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023, 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  * To use the I2CTarget driver, ensure that the correct driver library for your
38  * device is linked in and include this header file as follows:
39  * @code
40  * #include <ti/drivers/I2CTarget.h>
41  * @endcode
42  *
43  * # Overview
44  *
45  * The I2CTarget driver operates the I2C peripheral as a target on an I2C bus.
46  * The I2CTarget driver is event driven. When an @ref I2CTarget_Event occurs,
47  * the I2CTarget driver will call a user-specified callback function
48  * (from HWI context). It is the user-specified callback's responsibility to
49  * process the @ref I2CTarget_Event events, and to manipulate and interperet
50  * the data.
51  *
52  * <hr>
53  * @anchor ti_drivers_I2CTARGET_Usage
54  * # Usage
55  *
56  * This documentation provides a basic @ref ti_drivers_I2CTARGET_Synopsis
57  * "usage summary" and a set of @ref ti_drivers_I2CTARGET_Examples "examples"
58  * in the form of commented code fragments. Detailed descriptions of the
59  * APIs are provided in subsequent sections.
60  *
61  * @anchor ti_drivers_I2CTARGET_Synopsis
62  * ## Synopsis
63  * @anchor ti_drivers_I2CTARGET_Synopsis_Code
64  * @code
65  * // Import the I2CTarget driver definitions
66  * #include <ti/drivers/I2CTarget.h>
67  *
68  * // Define own I2C bus address
69  * #define OWN_ADDRESS 42
70  *
71  * // Initialize I2CTarget driver
72  * I2CTarget_init();
73  *
74  * // Initialize I2CTarget parameters
75  * I2CTarget_Params params;
76  * I2CTarget_Params_init(&params);
77  * params.eventCallbackFxn = myEventCallbackFunction;
78  * params.targetAddress = OWN_ADDRESS;
79  *
80  * // Open the I2CTarget driver
81  * I2CTarget_Handle i2c;
82  * i2c = I2CTarget_open(CONFIG_I2CTARGET_0, &params);
83  * if (i2C == NULL) {
84  * // Error opening I2CTarget
85  * while (1) {}
86  * }
87  *
88  * // Start I2C Target operation. The driver will send
89  * // events to the user-specified event callback function
90  * I2CTarget_start(i2c);
91  *
92  * // Transmitting and receiving bytes is handled by the
93  * // user-specified callback function. It is up to the user
94  * // to implement the appropriate state machine for its I2C
95  * // protocol.
96  *
97  * // Stop operation. The I2CTarget driver will no longer trigger
98  * // events and the device can enter low-power modes.
99  * I2CTarget_stop(i2c);
100  *
101  * // Close the I2CTarget driver
102  * I2CTarget_close(i2c);
103  * @endcode
104  *
105  * <hr>
106  * @anchor ti_drivers_I2CTARGET_Examples
107  * # Examples
108  * @anchor ti_drivers_I2CTARGET_Example_open
109  * ## Opening the I2CTarget Driver
110  *
111  * Opening a I2CTarget requires four steps:
112  * 1. Create and initialize a @ref I2CTarget_Params structure.
113  * 2. Fill in the desired parameters.
114  * 3. Call @ref I2CTarget_open(), passing the index of the I2C in the
115  * @ref I2CTarget_config structure, and the address of the @ref I2CTarget_Params
116  * structure. The I2C peripheral instance is specified by the index in
117  * the @ref I2CTarget_config structure.
118  * 4. Check that the I2CTarget handle returned by @ref I2CTarget_open() is non-NULL,
119  * and save it. The handle will be used to start and stop I2C Target operation
120  * of the I2C instance you just opened.
121  *
122  * @code
123  * I2CTarget_Handle i2c;
124  * I2CTarget_Params params;
125  * params.eventCallbackFxn = myEventCallbackFunction;
126  * params.targetAddress = 42;
127  *
128  * i2c = I2CTarget_open(CONFIG_I2CTARGET_0, NULL);
129  *
130  * if (i2c == NULL) {
131  * // Error opening I2CTarget
132  * while (1) {}
133  * }
134  * @endcode
135  *
136  * Only one I2CTarget index can be used at a time; calling @ref I2CTarget_open() a
137  * second time with the same index previosly passed to @ref I2CTarget_open() will
138  * result in an error. You can, though, re-use the index if the instance is closed
139  * via @ref I2CTarget_close().
140  * In the previous example code, CONFIG_I2CTARGET_0 is passed to @ref I2CTarget_open().
141  * This macro is defined in the example's ti_drivers_config.h file.
142  *
143  * @anchor ti_drivers_I2CTARGET_Example_start_stop_operation
144  * ## Start/Stop I2C Operation #
145  * The I2CTarget driver will start operation when calling @ref I2CTarget_start().
146  * It is the application's responsibility to process all incoming
147  * @ref I2CTarget_Event events in the @ref I2CTarget_Params.eventCallbackFxn and
148  * implement the necessary state machine for the I2C protocol used.
149  *
150  * @code
151  * I2CTarget_start(i2cTargetHandle);
152  * @endcode
153  *
154  * The I2CTarget driver operation is stopped by calling @ref I2CTarget_stop().
155  *
156  * @code
157  * I2CTarget_stop(i2cTargetHandle);
158  * @endcode
159  *
160  * @anchor ti_drivers_I2CTARGET_Example_receive_byte
161  * ## Transferring data
162  * This example shows a simplified example on how to transmit and receive data.
163  *
164  * @code
165  * // No command input
166  * #define NO_COMMAND 0x00
167  * // Define command to reset value of txData
168  * #define CMD_RESET_TXDATA 0x55
169  *
170  * // Variable storing data to transmit
171  * volatile uint8_t txData = 0;
172  *
173  * // Variable storing command received from an I2C controller
174  * volatile uint8_t command = 0;
175  *
176  * // User callback function
177  * int_fast16_t targetCallbackFxn(I2CTarget_Handle handle, I2CTarget_Event event, uint8_t *val) {
178  * if (event == I2CTarget_Event_WRITE_REQUESTED)
179  * {
180  * // Early event (before data is received) that the controller wants
181  * // to write a byte to us (target-receiver).
182  * // This event may be used to set up receive buffers, or other
183  * // preparations.
184  * }
185  *
186  * if (event == I2CTarget_Event_WRITE_RECEIVED) {
187  * // Controller has written a byte to the target (target-receiver)
188  * command = *data;
189  *
190  * // Tell driver data has been read
191  * return I2CTarget_STATUS_SUCCESS;
192  * }
193  *
194  * if (event == I2CTarget_Event_READ_REQUESTED || event == I2CTarget_Event_READ_PROCESSED) {
195  * // Controller wants to read from target (target-transmitter)
196  * *data = txData++;
197  *
198  * // Tell driver that data has been provided.
199  * return I2CTarget_STATUS_SUCCESS;
200  *
201  * // If the user callback does not have data, return
202  * // I2CTarget_STATUS_ERROR and the I2CTarget driver will
203  * // NACK the request (if supported by the device-specific driver implementation).
204  * }
205  *
206  * if (event == I2CTarget_Event_STOP) {
207  * // Stop condition received. Here we could, for example, reset
208  * // our I2C protocol state machine.
209  * }
210  *
211  * // Return success by default
212  * return I2CTarget_STATUS_SUCCESS;
213  * }
214  *
215  * // Main thread
216  * void *mainThread(void *arg0) {
217  * // Call driver init functions
218  * I2CTarget_init();
219  *
220  * // Open I2CTarget driver
221  * I2CTarget_Params params;
222  * I2CTarget_Params_init(&params);
223  * params.eventCallbackFxn = targetCallbackFxn;
224  * params.targetAddress = 42;
225  * I2CTarget_Handle handle = I2CTarget_open(CONFIG_I2CTARGET_0, &params);
226  * if (handle == NULL) {
227  * // Error
228  * while (1) {}
229  * }
230  *
231  * // Start operation
232  * I2CTarget_start(handle);
233  *
234  * // Data transfers happens in targetCallbackFxn
235  * while (1) {
236  * if (command == CMD_RESET_TXDATA) {
237  * command = NO_COMMAND;
238  * txData = 0;
239  * }
240  * }
241  * }
242  * @endcode
243  *
244  * <hr>
245  * @anchor ti_drivers_I2CTarget_Configuration
246  * # Configuration
247  *
248  * Refer to the @ref driver_configuration "Driver's Configuration" section
249  * for driver configuration information.
250  * <hr>
251  *
252  * ============================================================================
253  */
254 
255 #ifndef ti_drivers_I2CTARGET__include
256 #define ti_drivers_I2CTARGET__include
257 
258 #ifdef __cplusplus
259 extern "C" {
260 #endif
261 
263 #include <stdint.h>
264 
265 #include <ti/drivers/dpl/HwiP.h>
274 #define I2CTarget_STATUS_SUCCESS (0)
275 
281 #define I2CTarget_STATUS_ERROR (-1)
282 
288 
293 typedef enum
294 {
300 
306 
313 
322 
328 
333 typedef enum
334 {
335  I2CTarget_State_STOPPED, /* I2CTarget driver is not started. */
336  I2CTarget_State_IDLE, /* I2CTarget driver is started, no ongoing transactions */
337  I2CTarget_State_RECEIVE, /* I2CTarget driver is receiving data */
338  I2CTarget_State_TRANSMIT /* I2CTarget driver is transmitting */
339 } I2CTarget_State;
356 typedef int_fast16_t (*I2CTarget_EventCallbackFxn)(I2CTarget_Handle handle, I2CTarget_Event event, uint8_t *val);
357 
366 typedef struct
367 {
371  uint_least16_t targetAddress;
373  void *custom;
375 
377 #define I2CTARGET_BASE_OBJECT \
378  /* I2CTarget control variables */ \
379  I2CTarget_EventCallbackFxn eventCallbackFxn; /* Callback function pointer */ \
380  uint_least16_t targetAddress; /* Device's own address on the I2C bus */ \
381  I2CTarget_State currentState; /* Internal state machine */ \
382  \
383  /* I2CTarget RTOS objects */ \
384  HwiP_Struct hwi; /* Hwi object handle */ \
385  SemaphoreP_Struct mutex; /* Grants exclusive access to I2C module */ \
386  \
387  bool isOpen; /* Flag to show module is open */ \
388 
395 typedef struct
396 {
397  I2CTARGET_BASE_OBJECT
398 } I2CTarget_Object;
402 #define I2CTARGET_BASE_HWATTRS \
403  \
404  uint32_t baseAddr; \
405  \
406  uint32_t intNum; \
407  \
408  uint32_t intPriority;
409 
415 typedef struct
416 {
417  I2CTARGET_BASE_HWATTRS
418 } I2CTarget_HWAttrs;
432 typedef struct I2CTarget_Config_
433 {
435  void *object;
436 
438  void const *hwAttrs;
440 
441 extern const I2CTarget_Config I2CTarget_config[];
442 extern const uint_least8_t I2CTarget_count;
443 
452 extern void I2CTarget_init(void);
453 
465 
488 extern I2CTarget_Handle I2CTarget_open(uint_least8_t index, I2CTarget_Params *params);
489 
497 extern void I2CTarget_start(I2CTarget_Handle handle);
498 
506 extern void I2CTarget_stop(I2CTarget_Handle handle);
507 
518 extern void I2CTarget_close(I2CTarget_Handle handle);
519 
520 #ifdef __cplusplus
521 }
522 #endif
523 
524 #endif /* ti_drivers_I2CTARGET__include */
Definition: I2CTarget.h:312
ADC_Params params
Definition: Driver_Init.h:11
Definition: I2CTarget.h:321
void I2CTarget_Params_init(I2CTarget_Params *params)
Function to initialize the I2CTarget_Params struct to its defaults.
void I2CTarget_stop(I2CTarget_Handle handle)
Stop I2CTarget driver from listening on I2C bus.
Definition: I2CTarget.h:305
Definition: I2CTarget.h:326
void I2CTarget_start(I2CTarget_Handle handle)
Start I2CTarget driver listening on I2C bus.
void I2CTarget_close(I2CTarget_Handle handle)
I2CTarget_EventCallbackFxn eventCallbackFxn
Definition: I2CTarget.h:369
void * custom
Definition: I2CTarget.h:373
I2CTarget Global configuration.
Definition: I2CTarget.h:432
Semaphore module for the RTOS Porting Interface.
struct I2CTarget_Config_ * I2CTarget_Handle
A handle that is returned from a I2CTarget_open() call.
Definition: I2CTarget.h:287
I2CTarget_Event
I2CTarget events that the application&#39;s I2CTarget_EventCallbackFxn must support.
Definition: I2CTarget.h:293
int_fast16_t(* I2CTarget_EventCallbackFxn)(I2CTarget_Handle handle, I2CTarget_Event event, uint8_t *val)
I2CTarget event callback function.
Definition: I2CTarget.h:356
const uint_least8_t I2CTarget_count
const I2CTarget_Config I2CTarget_config[]
I2CTarget Parameters.
Definition: I2CTarget.h:366
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...
struct I2CTarget_Config_ I2CTarget_Config
I2CTarget Global configuration.
void * object
Definition: I2CTarget.h:435
uint_least16_t targetAddress
Definition: I2CTarget.h:371
void const * hwAttrs
Definition: I2CTarget.h:438
void I2CTarget_init(void)
Function to initializes the I2CTarget module.
Hardware Interrupt module for the RTOS Porting Interface.
Definition: I2CTarget.h:299
© Copyright 1995-2024, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale