1 /**
2 * @file     i2c.h
3 * @brief    Inter-integrated circuit (I2C) communications interface driver.
4 */
5 
6 /******************************************************************************
7  *
8  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
9  * Analog Devices, Inc.),
10  * Copyright (C) 2023-2024 Analog Devices, Inc.
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  *     http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  *
24  ******************************************************************************/
25 
26 /* Define to prevent redundant inclusion */
27 #ifndef LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32662_I2C_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32662_I2C_H_
29 
30 #include <stdint.h>
31 #include <stdbool.h>
32 #include "mxc_sys.h"
33 #include "i2c_regs.h"
34 #include "dma_regs.h"
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 /**
41  * @defgroup i2c I2C
42  * @ingroup periphlibs
43  * @{
44  */
45 
46 /***** Definitions *****/
47 #define MXC_I2C_STD_MODE 100000
48 #define MXC_I2C_FAST_SPEED 400000
49 #define MXC_I2C_FASTPLUS_SPEED 1000000
50 #define MXC_I2C_HIGH_SPEED 3400000
51 
52 typedef struct _i2c_req_t mxc_i2c_req_t;
53 /**
54  * @brief   The callback used by the MXC_I2C_ReadByteInteractive() function.
55  *
56  * The callback routine used by the MXC_I2C_ReadByteInteractive() function. This
57  * function allows the application to determine whether the byte received
58  * should be acknowledged or not.
59  *
60  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
61  * @param   byte        The byte received.
62  *
63  * @return  0 if the byte should not be acknowledged (NACK), non-zero to
64  *          acknowledge the byte.
65  */
66 typedef int (*mxc_i2c_getAck_t)(mxc_i2c_regs_t *i2c, unsigned char byte);
67 
68 /**
69  * @brief   The callback routine used by the MXC_I2C_MasterTransactionAsync()
70  *          function to indicate the transaction has completed.
71  *
72  * @param   req         The details of the transaction.
73  * @param   result      0 if all bytes are acknowledged, 1 if any byte
74  *                      transmitted is not acknowledged, negative if error.
75  *                      See \ref MXC_Error_Codes for the list of error codes.
76  */
77 typedef void (*mxc_i2c_complete_cb_t)(mxc_i2c_req_t *req, int result);
78 
79 /**
80  * @brief   The callback routine used by the I2C Read/Write FIFO DMA
81  *          functions to indicate the transaction has completed.
82  *
83  * @param   len         The length of data actually read/written
84  * @param   result      See \ref MXC_Error_Codes for the list of error codes.
85  */
86 typedef void (*mxc_i2c_dma_complete_cb_t)(int len, int result);
87 
88 /**
89  * @brief   The information required to perform a complete I2C transaction as
90  *          the bus master.
91  *
92  * The information required to perform a complete I2C transaction as the bus
93  * master. This structure is used by the MXC_I2C_MasterTransaction() and
94  * MXC_I2C_MasterTransactionAsync() functions.
95  */
96 struct _i2c_req_t {
97     mxc_i2c_regs_t *i2c; ///< Pointer to I2C registers (selects the
98     ///< I2C block used.)
99     unsigned int addr; ///< The 7-bit or 10-bit address of the slave.
100     unsigned char *tx_buf; ///< The buffer containing the bytes to write.
101     unsigned int tx_len; ///< The number of bytes to write. On return
102     ///< from the function, this will be set to
103     ///< the number of bytes actually transmitted.
104     unsigned char *rx_buf; ///< The buffer to read the data into.
105     unsigned int rx_len; ///< The number of bytes to read.  On return
106     ///< from the function, this will be set to
107     ///< the number of bytes actually received.
108     int restart; ///< Controls whether the transaction is
109     ///< terminated with a stop or repeated start
110     ///< condition.  Use 0 for a stop, non-zero
111     ///< for repeated start.
112     mxc_i2c_complete_cb_t callback; ///< The callback used to indicate the
113     ///< transaction is complete or an error has
114     ///< occurred. This field may be set to NULL
115     ///< if no indication is necessary. This
116     ///< field is only used by the
117     ///< MXC_I2C_MasterTransactionAsync() function.
118     ///< MXC_I2C_MasterTransaction() ignores the
119     ///< callback field.
120 };
121 
122 /**
123  * @brief   The list of events reported by the MXC_I2C_SlaveTransaction() and
124  *          MXC_I2C_SlaveTransactionAsync() functions.
125  *
126  * The list of events reported by the MXC_I2C_SlaveTransaction() and
127  * MXC_I2C_SlaveTransactionAsync() functions.  It is up to the calling
128  * application to handle these events.
129  */
130 typedef enum {
131     MXC_I2C_EVT_MASTER_WR, ///< A slave address match occurred with the master
132     ///< requesting a write to the slave.
133     MXC_I2C_EVT_MASTER_RD, ///< A slave address match occurred with the master
134     ///< requesting a read from the slave.
135     MXC_I2C_EVT_RX_THRESH, ///< The receive FIFO contains more bytes than its
136     ///< threshold level.
137     MXC_I2C_EVT_TX_THRESH, ///< The transmit FIFO contains fewer bytes than its
138     ///< threshold level.
139     MXC_I2C_EVT_TRANS_COMP, ///< The transaction has ended.
140     MXC_I2C_EVT_UNDERFLOW, ///< The master has attempted a read when the
141     ///< transmit FIFO was empty.
142     MXC_I2C_EVT_OVERFLOW, ///< The master has written data when the receive
143     ///< FIFO was already full.
144 } mxc_i2c_slave_event_t;
145 
146 /**
147  * @brief   The callback routine used by the MXC_I2C_SlaveTransaction() and
148  *          MXC_I2C_SlaveTransactionAsync functions to handle the various I2C
149  *          slave events.
150  *
151  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
152  * @param   event       The event that occurred to trigger this callback.
153  * @param   data        This field is used to pass Success/Fail for the
154  *                      MXC_I2C_EVT_TRANS_COMP event.
155  *
156  * @return  The return value is only used in the case of an MXC_I2C_EVT_RX_THRESH
157  *          event. In this case, the return specifies if the last byte
158  *          received should be acknowledged or not. Return 0 to acknowledge,
159  *          non-zero to not acknowledge.  The return value is ignored for all
160  *          other event types.
161  */
162 typedef int (*mxc_i2c_slave_handler_t)(mxc_i2c_regs_t *i2c, mxc_i2c_slave_event_t event,
163                                        void *data);
164 
165 /***** Function Prototypes *****/
166 
167 /* ************************************************************************* */
168 /* Control/Configuration functions                                           */
169 /* ************************************************************************* */
170 
171 /**
172  * @brief   Initialize and enable I2C peripheral.
173  *
174  * @note    On default this function enables I2C peripheral clock and i2c gpio pins.
175  *          if you wish to manage clock and gpio related things in upper level instead of here.
176  *          Define MSDK_NO_GPIO_CLK_INIT flag in project.mk file.
177  *          By this flag this function will remove clock and gpio related codes from file.
178  *
179  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
180  * @param   masterMode  Whether to put the device in master or slave mode. Use
181  *                      non-zero
182  *                      for master mode, and zero for slave mode.
183  * @param   slaveAddr   7-bit or 10-bit address to use when in slave mode.
184  *                      This parameter is ignored when masterMode is non-zero.
185  *
186  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
187  */
188 int MXC_I2C_Init(mxc_i2c_regs_t *i2c, int masterMode, unsigned int slaveAddr);
189 
190 /**
191  * @brief   Set slave address for I2C instances acting as slaves on the bus.
192  * @note    Set idx to zero, multiple I2C instances acting as slaves on the
193  *          bus is not yet supported.
194  *
195  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
196  * @param   slaveAddr   7-bit or 10-bit address to use when in slave mode.
197  *                      This parameter is ignored when masterMode is non-zero.
198  * @param   idx         Index of the I2C slave.
199  *
200  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
201  */
202 int MXC_I2C_SetSlaveAddr(mxc_i2c_regs_t *i2c, unsigned int slaveAddr, int idx);
203 
204 /**
205  * @brief   Disable and shutdown I2C peripheral.
206  *
207  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
208  *
209  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
210  */
211 int MXC_I2C_Shutdown(mxc_i2c_regs_t *i2c);
212 
213 /**
214  * @brief   Reset the I2C peripheral.
215  * @note    The peripheral will need to be initialized with MXC_I2C_Init() before use
216  *
217  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
218  *
219  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
220  */
221 int MXC_I2C_Reset(mxc_i2c_regs_t *i2c);
222 
223 /**
224  * @brief   Set the frequency of the I2C interface.
225  *
226  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
227  * @param   hz          The desired frequency in Hertz.
228  *
229  * @return  Negative if error, otherwise actual speed set. See \ref
230  *          MXC_Error_Codes for the list of error return codes.
231  */
232 int MXC_I2C_SetFrequency(mxc_i2c_regs_t *i2c, unsigned int hz);
233 
234 /**
235  * @brief   Get the frequency of the I2C interface.
236  *
237  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
238  *
239  * @return  The I2C bus frequency in Hertz
240  */
241 int MXC_I2C_GetFrequency(mxc_i2c_regs_t *i2c);
242 
243 /**
244  * @brief   Checks if the given I2C bus can be placed in sleep more.
245  *
246  * This functions checks to see if there are any on-going I2C transactions in
247  * progress. If there are transactions in progress, the application should
248  * wait until the I2C bus is free before entering a low-power state.
249  *
250  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
251  *
252  * @return  #E_NO_ERROR if ready, and non-zero if busy or error. See \ref
253  *          MXC_Error_Codes for the list of error return codes.
254  */
255 int MXC_I2C_ReadyForSleep(mxc_i2c_regs_t *i2c);
256 
257 /**
258  * @brief   Enables or disables clock stretching by the slave.
259  *
260  * Enables or disables clock stretching by the slave. This function has no
261  * affect when operating as the master.
262  *
263  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
264  * @param   enable      Enables clock stretching if non-zero, disables if zero.
265  *
266  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
267  */
268 int MXC_I2C_SetClockStretching(mxc_i2c_regs_t *i2c, int enable);
269 
270 /**
271  * @brief   Determines if clock stretching has been enabled.
272  *
273  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
274  *
275  * @return  Zero if clock stretching is disabled, non-zero otherwise
276  */
277 int MXC_I2C_GetClockStretching(mxc_i2c_regs_t *i2c);
278 
279 /**
280  * @brief   Initializes the DMA for I2C DMA transactions.
281  *
282  * This function will release any acquired DMA channels before reacquiring and
283  * reconfiguring selected I2C DMA TX or RX channels.
284  *
285  * @param   i2c         Pointer to I2C registers (selects the I2C block used).
286  * @param   dma         Pointer to DMA registers (selects the DMA block used).
287  * @param   use_dma_tx  If true, acquire and configure DMA TX channel, else release any
288  *                      acquired DMA TX channel.
289  * @param   use_dma_rx  If true, acquire and configure DMA RX channel, else release any
290  *                      acquired DMA RX channel.
291  *
292  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
293  */
294 int MXC_I2C_DMA_Init(mxc_i2c_regs_t *i2c, mxc_dma_regs_t *dma, bool use_dma_tx, bool use_dma_rx);
295 
296 /**
297  * @brief   Retreive the DMA TX Channel associated with I2C instance.
298  *
299  * @param   i2c         Pointer to I2C registers (selects the I2C block used).
300  *
301  * @return  If successful, the DMA TX Channel number is returned. Otherwise, see
302  *          \ref MXC_Error_Codes for a list of return codes.
303  */
304 int MXC_I2C_DMA_GetTXChannel(mxc_i2c_regs_t *i2c);
305 
306 /**
307  * @brief   Retreive the DMA RX Channel associated with I2C instance.
308  *
309  * @param   i2c         Pointer to I2C registers (selects the I2C block used).
310  *
311  * @return  If successful, the DMA RX Channel number is returned. Otherwise, see
312  *          \ref MXC_Error_Codes for a list of return codes.
313  */
314 int MXC_I2C_DMA_GetRXChannel(mxc_i2c_regs_t *i2c);
315 
316 /**
317  * @brief   Sets the I2C instance's DMA TX/RX request select.
318  *
319  * @param   i2c         Pointer to I2C registers (selects the I2C block used).
320  * @param   txData      Pointer to transmit buffer.
321  * @param   rxData      Pointer to receive buffer.
322  *
323  * @return Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
324  */
325 int MXC_I2C_DMA_SetRequestSelect(mxc_i2c_regs_t *i2c, uint8_t *txData, uint8_t *rxData);
326 
327 /* ************************************************************************* */
328 /* Low-level functions                                                       */
329 /* ************************************************************************* */
330 
331 /**
332  * @brief   Generate a start (or repeated start) condition on the I2C bus.
333  *
334  * Generate a start (or repeated start) condition on the I2C bus. This
335  * function may opt to delay the actual generation of the start condition
336  * until data is actually transferred.
337  *
338  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
339  *
340  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
341  */
342 int MXC_I2C_Start(mxc_i2c_regs_t *i2c);
343 
344 /**
345  * @brief   Generate a stop condition on the I2C bus.
346  *
347  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
348  *
349  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
350  */
351 int MXC_I2C_Stop(mxc_i2c_regs_t *i2c);
352 
353 /**
354  * @brief   Write a single byte to the I2C bus.
355  *
356  * Write a single byte to the I2C bus. This function assumes the I2C bus is
357  * already in the proper state (i.e. a start condition has already been
358  * generated and the bus is in the write phase of an I2C transaction). If any
359  * bytes are pending in the FIFO (i.e. in the case of clock stretching), this
360  * function will return E_OVERFLOW.
361  *
362  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
363  * @param   byte        The byte to transmit.
364  *
365  * @return  0 if byte is acknowledged, 1 if not acknowledged, negative if
366  *          error. See \ref MXC_Error_Codes for the list of error return codes.
367  */
368 int MXC_I2C_WriteByte(mxc_i2c_regs_t *i2c, unsigned char byte);
369 
370 /**
371  * @brief   Read a single byte from the I2C bus.
372  *
373  * Read a single byte from the I2C bus. This function assumes the I2C bus is
374  * already in the proper state (i.e. a start condition has already been
375  * generated and the bus is in the read phase of an I2C transaction). If the FIFO
376  * is empty, this function will return E_UNDERFLOW.
377  *
378  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
379  * @param   byte        Pointer to the byte to read into.
380  * @param   ack         Whether or not to acknowledge the byte once received.
381  *
382  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
383  */
384 int MXC_I2C_ReadByte(mxc_i2c_regs_t *i2c, unsigned char *byte, int ack);
385 
386 /**
387  * @brief   Read a single byte from the I2C bus.
388  *
389  * Read a single byte from the I2C bus. After the byte is received, the
390  * provided callback will be used to determine if the byte should be
391  * acknowledged or not before continuing with the rest of the transaction.
392  * This function assumes the I2C bus is already in the proper state (i.e. a
393  * start condition has already been generated and the bus is in the read
394  * phase of an I2C transaction). This function must be called with clock
395  * stretching enabled.
396  *
397  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
398  * @param   byte        Pointer to the byte to read into.
399  * @param   getAck      A function to be called to determine whether or not
400  *                      to acknowledge the byte once received.  A non-zero
401  *                      return value will acknowledge the byte.  If this
402  *                      parameter is set to NULL or its return value is 0,
403  *                      the byte received will not be acknowledged (i.e., it
404  *                      will be NACKed).
405  *
406  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
407  */
408 int MXC_I2C_ReadByteInteractive(mxc_i2c_regs_t *i2c, unsigned char *byte, mxc_i2c_getAck_t getAck);
409 
410 /**
411  * @brief   Write multiple bytes to the I2C bus.
412  *
413  * Write multiple bytes to the I2C bus.  This function assumes the I2C bus is
414  * already in the proper state (i.e. a start condition has already been
415  * generated and the bus is in the write phase of an I2C transaction).
416  *
417  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
418  * @param   bytes       The buffer containing the bytes to transmit.
419  * @param   len         The number of bytes to write. On return from the
420  *                      function, this will be set to the number of bytes
421  *                      actually transmitted.
422  *
423  * @return  0 if all bytes are acknowledged, 1 if any byte transmitted is not
424  *          acknowledged, negative if error. See \ref MXC_Error_Codes for the
425  *          list of error return codes.
426  */
427 int MXC_I2C_Write(mxc_i2c_regs_t *i2c, unsigned char *bytes, unsigned int *len);
428 
429 /**
430  * @brief   Read multiple bytes from the I2C bus.
431  *
432  * Read multiple byte from the I2C bus.  This function assumes the I2C bus is
433  * already in the proper state (i.e. a start condition has already been
434  * generated and the bus is in the read phase of an I2C transaction).
435  *
436  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
437  * @param   bytes       The buffer to read the data into.
438  * @param   len         The number of bytes to read. On return from the
439  *                      function, this will be set to the number of bytes
440  *                      actually received.
441  * @param   ack         Whether or not to acknowledge the last byte once it is
442  *                      received.  All previous bytes will be acknowledged.
443  *
444  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
445  */
446 int MXC_I2C_Read(mxc_i2c_regs_t *i2c, unsigned char *bytes, unsigned int *len, int ack);
447 
448 /**
449  * @brief   Unloads bytes from the receive FIFO.
450  *
451  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
452  * @param   bytes       The buffer to read the data into.
453  * @param   len         The number of bytes to read.
454  *
455  * @return  The number of bytes actually read.
456  */
457 int MXC_I2C_ReadRXFIFO(mxc_i2c_regs_t *i2c, volatile unsigned char *bytes, unsigned int len);
458 
459 /**
460  * @brief   Unloads bytes from the receive FIFO using DMA for longer reads.
461  *
462  * @note    The operation is not complete until the callback has been called
463  *
464  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
465  * @param   bytes       The buffer to read the data into.
466  * @param   len         The number of bytes to read.
467  * @param   callback    The function to call when the read is complete
468  *
469  * @return  See \ref MXC_Error_Codes for a list of return values.
470  */
471 int MXC_I2C_ReadRXFIFODMA(mxc_i2c_regs_t *i2c, unsigned char *bytes, unsigned int len,
472                           mxc_i2c_dma_complete_cb_t callback);
473 
474 /**
475  * @brief   Get the number of bytes currently available in the receive FIFO.
476  *
477  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
478  *
479  * @return  The number of bytes available.
480  */
481 int MXC_I2C_GetRXFIFOAvailable(mxc_i2c_regs_t *i2c);
482 
483 /**
484  * @brief   Loads bytes into the transmit FIFO.
485  *
486  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
487  * @param   bytes       The buffer containing the bytes to write
488  * @param   len         The number of bytes to write.
489  *
490  * @return  The number of bytes actually written.
491  */
492 int MXC_I2C_WriteTXFIFO(mxc_i2c_regs_t *i2c, volatile unsigned char *bytes, unsigned int len);
493 
494 /**
495  * @brief   Loads bytes into the transmit FIFO using DMA for longer writes.
496  *
497  * @note    The operation is not complete until the callback has been called
498  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
499  * @param   bytes       The buffer containing the bytes to write
500  * @param   len         The number of bytes to write.
501  * @param   callback    The function to call when the read is complete
502  *
503  * @return  See \ref MXC_Error_Codes for a list of return values
504  */
505 int MXC_I2C_WriteTXFIFODMA(mxc_i2c_regs_t *i2c, unsigned char *bytes, unsigned int len,
506                            mxc_i2c_dma_complete_cb_t callback);
507 
508 /**
509  * @brief   Get the amount of free space available in the transmit FIFO.
510  *
511  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
512  *
513  * @return  The number of bytes available.
514  */
515 int MXC_I2C_GetTXFIFOAvailable(mxc_i2c_regs_t *i2c);
516 
517 /**
518  * @brief   Removes and discards all bytes currently in the receive FIFO.
519  *
520  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
521  */
522 void MXC_I2C_ClearRXFIFO(mxc_i2c_regs_t *i2c);
523 
524 /**
525  * @brief   Removes and discards all bytes currently in the transmit FIFO.
526  *
527  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
528  */
529 void MXC_I2C_ClearTXFIFO(mxc_i2c_regs_t *i2c);
530 
531 /**
532  * @brief   Get the presently set interrupt flags.
533  *
534  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
535  * @param   flags0      Pointer to variable to store interrupt status for interrupt register intfl0.
536  * @param   flags1      Pointer to variable to store interrupt status for interrupt register intfl1.
537  */
538 int MXC_I2C_GetFlags(mxc_i2c_regs_t *i2c, unsigned int *flags0, unsigned int *flags1);
539 
540 /**
541  * @brief   Clears the Interrupt Flags.
542  *
543  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
544  * @param   flags0      Flags to clear in interrupt register intfl0.
545  * @param   flags1      Flags to clear in interrupt register intfl1.
546  */
547 void MXC_I2C_ClearFlags(mxc_i2c_regs_t *i2c, unsigned int flags0, unsigned int flags1);
548 
549 /**
550  * @brief   Enable Interrupts.
551  *
552  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
553  * @param   flags0      Interrupts to be enabled in int->en0
554  * @param   flags1      Interrupts to be enabled in int->en1
555  */
556 void MXC_I2C_EnableInt(mxc_i2c_regs_t *i2c, unsigned int flags0, unsigned int flags1);
557 
558 /**
559  * @brief   Disable Interrupts.
560  *
561  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
562  * @param   flags0      Interrupts to be disabled in int->en0
563  * @param   flags1      Interrupts to be disabled in int->en1
564  */
565 void MXC_I2C_DisableInt(mxc_i2c_regs_t *i2c, unsigned int flags0, unsigned int flags1);
566 
567 /**
568  * @brief   Enables the slave preload mode
569  *
570  * Use this mode to preload the slave TX FIFO with data that can be sent when
571  * the slave is addressed for a read operation without software intervention.
572  *
573  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
574  */
575 void MXC_I2C_EnablePreload(mxc_i2c_regs_t *i2c);
576 
577 /**
578  * @brief   Disable the slave preload mode
579  *
580  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
581  */
582 void MXC_I2C_DisablePreload(mxc_i2c_regs_t *i2c);
583 
584 /**
585  * @brief   Enables the slave to respond to the general call address
586  *
587  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
588  */
589 void MXC_I2C_EnableGeneralCall(mxc_i2c_regs_t *i2c);
590 
591 /**
592  * @brief   Prevents the slave from responding to the general call address
593  *
594  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
595  */
596 void MXC_I2C_DisableGeneralCall(mxc_i2c_regs_t *i2c);
597 
598 /**
599  * @brief   Set the I2C Timeout
600  *
601  * The I2C timeout determines the amount of time the master will wait while the
602  * slave is stretching the clock, and the amount of time the slave will stretch
603  * the clock while waiting for software to unload the fifo.
604  *
605  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
606  * @param   timeout     Timeout in uS
607  */
608 void MXC_I2C_SetTimeout(mxc_i2c_regs_t *i2c, unsigned int timeout);
609 
610 /**
611  * @brief   Get the current I2C timeout
612  *
613  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
614  *
615  * @return  The current timeout in uS
616  */
617 unsigned int MXC_I2C_GetTimeout(mxc_i2c_regs_t *i2c);
618 
619 /**
620  * @brief   Attempts to recover the I2C bus, ensuring the I2C lines are idle.
621  *
622  * Attempts to recover and reset an I2C bus by sending I2C clocks. During
623  * each clock cycle, the SDA line is cycled to determine if the master has
624  * control of the line. The following steps are performed to create one SCL
625  * clock cycle:
626  *   1. Drive SCL low
627  *   2. Verify SCL is low
628  *   3. Drive SDA low
629  *   4. Verify SDA is low
630  *   5. Release SDA allowing it to return high
631  *   6. Verify SDA is high
632  *   7. Release SCL allowing it to return high.
633  *   8. Verify SCL is high
634  * If any of the steps fail, the bus is considered to still be busy and the
635  * sequence is repeated up to the requested number of times. If all steps
636  * succeed, a final stop condition is generated on the I2C bus.
637  *
638  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
639  * @param   retries     Number of times to attempt the clock cycle sequence.
640  *
641  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
642  */
643 int MXC_I2C_Recover(mxc_i2c_regs_t *i2c, unsigned int retries);
644 
645 /* ************************************************************************* */
646 /* Transaction level functions                                               */
647 /* ************************************************************************* */
648 
649 /**
650  * @brief   Performs a blocking I2C master transaction.
651  *
652  * Performs a blocking I2C master transaction.  These actions will be performed:
653  *   1. If necessary, generate a start condition on the bus.
654  *   2. Send the slave address with the low bit set to 0 (indicating a write).
655  *   3. Transmit req->tx_len bytes of req->tx_buff.
656  *   4. Generate a repeated start condition on the bus.
657  *   5. Send the slave address with the low bit set to 1 (indicating a read).
658  *   6. Receive req->rx_len bytes into req->rx_buf, acknowledging each byte.
659  *   7. Generate a stop (or repeated start) condition on the bus.
660  *   Steps 3-6 will be skipped if req->tx_len and req->rx_len are both 0.
661  *   Steps 2-4 will be skipped if req->tx_len equals 0.
662  *   Steps 4-6 will be skipped if req->rx_len equals 0.
663  *
664  * @param   req         Pointer to details of the transaction
665  *
666  * @return  0 if all bytes are acknowledged, 1 if any byte transmitted is not
667  *          acknowledged, negative if error. See \ref MXC_Error_Codes for the
668  *          list of error return codes.
669  */
670 int MXC_I2C_MasterTransaction(mxc_i2c_req_t *req);
671 
672 /**
673  * @brief   Performs a non-blocking I2C master transaction.
674  *
675  * Performs a non-blocking I2C master transaction.  These actions will be performed:
676  *   1. If necessary, generate a start condition on the bus.
677  *   2. Send the slave address with the low bit set to 0 (indicating a write).
678  *   3. Transmit req->tx_len bytes of req->tx_buff.
679  *   4. Generate a repeated start condition on the bus.
680  *   5. Send the slave address with the low bit set to 1 (indicating a read).
681  *   6. Receive req->rx_len bytes into req->rx_buf, acknowledging each byte.
682  *   7. Generate a stop (or repeated start) condition on the bus.
683  *   8. Execute req->callback to indicate the transaction is complete.
684  *   Steps 3-6 will be skipped if tx_len and rx_len are both 0.
685  *   Steps 2-4 will be skipped if tx_len equals 0.
686  *   Steps 4-6 will be skipped if rx_len equals 0.
687  *
688  * @note    MXC_I2C_AsyncHandler() must be called periodically for this function
689  *          to operate properly. Ideally from the I2C ISR.
690  *
691  * @param   req         Pointer to details of the transaction.  The memory
692  *                      used by this parameter must remain available until
693  *                      the callback is executed.
694  *
695  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
696  */
697 int MXC_I2C_MasterTransactionAsync(mxc_i2c_req_t *req);
698 
699 /**
700  * @brief   Performs a non-blocking I2C Master transaction using DMA for reduced time
701  *          in the ISR. This function initializes the DMA and acquires DMA channels
702  *          if MXC_I2C_DMA_Init(...) was not called earlier.
703  *
704  * It is recommended to initialize the DMA by calling MXC_I2C_DMA_Init(...) function
705  * before calling MXC_I2C_MasterTransactionDMA(...). This provides flexibility in
706  * setting up generic DMA channel vectors during run-time without knowing what DMA
707  * channels will be acquired beforehand.
708  *
709  * Performs a non-blocking I2C transaction.  These actions will be performed:
710  *   1. If necessary, generate a start condition on the bus.
711  *   2. Send the slave address with the low bit set to 0 (indicating a write).
712  *   3. Transmit req->tx_len bytes of req->tx_buff.
713  *   4. Generate a repeated start condition on the bus.
714  *   5. Send the slave address with the low bit set to 1 (indicating a read).
715  *   6. Receive req->rx_len bytes into req->rx_buf, acknowledging each byte.
716  *   7. Generate a stop (or repeated start) condition on the bus.
717  *   8. Execute req->callback to indicate the transaction is complete.
718  *   Steps 3-6 will be skipped if tx_len and rx_len are both 0.
719  *   Steps 2-4 will be skipped if tx_len equals 0.
720  *   Steps 4-6 will be skipped if rx_len equals 0.
721  *
722  * @note    MXC_I2C_AsyncHandler() must be called periodically for this function
723  *          to operate properly. Ideally from the I2C ISR.
724  *
725  * @param   req         Pointer to details of the transaction.  The memory
726  *                      used by this parameter must remain available until
727  *                      the callback is executed.
728  *
729  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
730  */
731 int MXC_I2C_MasterTransactionDMA(mxc_i2c_req_t *req);
732 
733 /**
734  * @brief   Performs a blocking I2C slave transaction.
735  *
736  * Performs a blocking I2C slave transaction. This function will block until a
737  * complete transaction with this slave has been performed. A transaction
738  * begins with the master addressing the slave and ends with a repeated start
739  * condition, a stop condition, or a bus error. The provided callback
740  * function will be called for these events:
741  *      - A slave address match occurs with the master requesting a write to
742  *        the slave.
743  *      - A slave address match occurs with the master requesting a read from
744  *        the slave.
745  *      - The receive FIFO crosses the set threshold (see
746  *        MXC_I2C_SetRXThreshold()). The callback code should unload the receive
747  *        FIFO (see MXC_I2C_ReadFIFO()) to allow the master to send more data.
748  *        The return value of the callback function will determine if the
749  *        last byte received should be acknowledged or not. Return 0 to
750  *        acknowledge, non-zero to not acknowledge.
751  *      - The transmit FIFO crosses the set threshold (see
752  *        MXC_I2C_SetTXThreshold()). If the master is expected to read more
753  *        data from this slave, the callback code should add data to the
754  *        transmit FIFO (see MXC_I2C_WriteFIFO()).
755  *      - The transaction ends.  If the master was writing to the slave, the
756  *        receive FIFO may still contain valid data that needs to be
757  *        retreived (see MXC_I2C_ReadFIFO()).
758  *      - The transmit FIFO underflows because the master requests data when
759  *        the transmit FIFO is empty.
760  *      - The receive FIFO overflows because the master writes data while the
761  *        receive FIFO was full.
762  *
763  * If clock stretching is disabled, careful attention must be paid to the timing
764  * of the callback to avoid losing data on write or unintentionally nacking a read.
765  *
766  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
767  * @param   callback    The function to be called when an I2C event occurs.
768  *
769  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
770  */
771 int MXC_I2C_SlaveTransaction(mxc_i2c_regs_t *i2c, mxc_i2c_slave_handler_t callback);
772 
773 /**
774  * @brief   Performs a non-blocking I2C slave transaction.
775  *
776  * Performs a non-blocking I2C slave transaction. This request will remain active
777  * until a complete transaction with this slave has been performed.  A
778  * transaction begins with the master  begins with the master addressing the
779  * slave and ends with a repeated start condition, a stop condition, or a bus
780  * error. The provided callback function will be called for these events:
781  *      - A slave address match occurs with the master requesting a write to
782  *        the slave.
783  *      - A slave address match occurs with the master requesting a read from
784  *        the slave.
785  *      - The receive FIFO crosses the set threshold (see
786  *        MXC_I2C_SetRXThreshold()). The callback code should unload the receive
787  *        FIFO (see MXC_I2C_ReadFIFO()) to allow the master to send more data.
788  *        The return value of the callback function will determine if the
789  *        last byte received should be acknowledged or not. Return 0 to
790  *        acknowledge, non-zero to not acknowledge.
791  *      - The transmit FIFO crosses the set threshold (see
792  *        MXC_I2C_SetTXThreshold()). If the master is expected to read more
793  *        data from this slave, the callback code should add data to the
794  *        transmit FIFO (see MXC_I2C_WriteFIFO()).
795  *      - The transaction ends.  If the master was writing to the slave, the
796  *        receive FIFO may still contain valid data that needs to be
797  *        retreived (see MXC_I2C_ReadFIFO()).
798  *      - The transmit FIFO underflows because the master requests data when
799  *        the transmit FIFO is empty.
800  *      - The receive FIFO overflows because the master writes data while the
801  *        receive FIFO was full.
802  *
803  * If clock stretching is disabled, careful attention must be paid to the timing
804  * of the callback to avoid losing data on write or unintentionally nacking a read.
805  *
806  * @note    MXC_I2C_AsyncHandler() must be called peridocally for this function
807  *          to operate properly.
808  *
809  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
810  * @param   callback    The function to be called when an I2C event occurs.
811  *
812  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
813  */
814 int MXC_I2C_SlaveTransactionAsync(mxc_i2c_regs_t *i2c, mxc_i2c_slave_handler_t callback);
815 
816 /**
817  * @brief   Set the receive threshold level.
818  *
819  * When operating as a master, the function sets the receive threshold level
820  * for when the master should unload the receive FIFO. Smaller values may
821  * consume more CPU cycles, but decrease the chances of the master delaying
822  * the generation of I2C bus clocks because it has no room in the FIFO to
823  * receive data. Larger values may consume fewer CPU cycles, but risk delays
824  * of the I2C clock. When operating as a slave, this function sets the number
825  * of bytes the slave transaction  functions should receive before issuing a
826  * call to their callback function. Smaller values may consume more CPU
827  * cycles, but reduce the risk of missing data from the master due to the
828  * recieve FIFO being full. Larger values may reduce the number of CPU
829  * cycles, but may cause bytes sent from the master to be missed.
830  *
831  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
832  * @param   numBytes    The threshold level to set. This value must be
833  *                      between 0 and 8 inclusive.
834  *
835  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
836  */
837 int MXC_I2C_SetRXThreshold(mxc_i2c_regs_t *i2c, unsigned int numBytes);
838 
839 /**
840  * @brief   Get the current receive threshold level.
841  *
842  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
843  *
844  * @return  The receive threshold value (in bytes).
845  */
846 int MXC_I2C_GetRXThreshold(mxc_i2c_regs_t *i2c);
847 
848 /**
849  * @brief   Set the transmit threshold level.
850  *
851  * When operating as a master, the function sets the transmit threshold level
852  * for when the master should add additional bytes to the transmit FIFO.
853  * Larger values may consume more CPU cycles, but decrease the chances of the
854  * master delaying the generation of I2C bus clocks because it has no data in
855  * the FIFO to transmit. Smaller values may consume fewer CPU cycles, but
856  * risk delays of the I2C clock. When operating as a slave, this function
857  * sets the number of bytes the slave transaction functions should transmit
858  * before issuing a call to their callback function. Larger values may
859  * consume more CPU cycles, but reduce the risk of not having data ready when
860  * the master requests it.  Smaller values may reduce the number of CPU
861  * cycles, but may cause the master to read from an empty FIFO.  (The master
862  * will read 0xFF in this case.)
863  *
864  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
865  * @param   numBytes    The threshold level to set.  This value must be
866  *                      between 0 and 8 inclusive.
867  *
868  * @return  Success/Fail, see \ref MXC_Error_Codes for a list of return codes.
869  */
870 int MXC_I2C_SetTXThreshold(mxc_i2c_regs_t *i2c, unsigned int numBytes);
871 
872 /**
873  * @brief   Get the current transmit threshold level.
874  *
875  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
876  *
877  * @return  The transmit threshold value (in bytes).
878  */
879 int MXC_I2C_GetTXThreshold(mxc_i2c_regs_t *i2c);
880 
881 /**
882  * @brief   Stop any asynchronous requests in progress.
883  *
884  * Stop any asynchronous requests in progress. Any callbacks associated with
885  * the active transaction will be NOT executed.
886  *
887  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
888  */
889 void MXC_I2C_AsyncStop(mxc_i2c_regs_t *i2c);
890 
891 /**
892  * @brief   Abort any asynchronous requests in progress.
893  *
894  * Abort any asynchronous requests in progress. Any callbacks associated with
895  * the active transaction will be executed to indicate when the transaction
896  * has been terminated.
897  *
898  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
899  */
900 void MXC_I2C_AbortAsync(mxc_i2c_regs_t *i2c);
901 
902 /**
903  * @brief   The processing function for asynchronous transactions.
904  *
905  * When using the asynchronous functions, the application must call this
906  * function periodically. This can be done from within the I2C interrupt
907  * handler or periodically by the application if I2C interrupts are disabled.
908  *
909  * @param   i2c         Pointer to I2C registers (selects the I2C block used.)
910  */
911 void MXC_I2C_AsyncHandler(mxc_i2c_regs_t *i2c);
912 
913 /**
914  * @brief   The processing function for DMA transactions.
915  *
916  * When using the DMA functions, the application must call this
917  * function periodically. This can be done from within the DMA Interrupt Handler.
918  *
919  * @param   ch          DMA channel
920  * @param   error       Error status
921  */
922 void MXC_I2C_DMACallback(int ch, int error);
923 
924 /**@} end of group i2c */
925 
926 #ifdef __cplusplus
927 }
928 #endif
929 
930 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32662_I2C_H_
931