MAX32675 Peripheral Driver API
Peripheral Driver API for the MAX32675
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Modules
i2c.h
1
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_MAX32675_I2C_H_
28#define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32675_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
37extern "C" {
38#endif
39
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
52typedef struct _i2c_req_t mxc_i2c_req_t;
53
67typedef int (*mxc_i2c_getAck_t)(mxc_i2c_regs_t *i2c, unsigned char byte);
68
78typedef void (*mxc_i2c_complete_cb_t)(mxc_i2c_req_t *req, int result);
79
87typedef void (*mxc_i2c_dma_complete_cb_t)(int len, int result);
88
97struct _i2c_req_t {
100 unsigned int addr;
101 unsigned char *tx_buf;
102 unsigned int tx_len;
105 unsigned char *rx_buf;
106 unsigned int rx_len;
121};
122
131typedef enum {
146
164 void *data);
165
166/***** Function Prototypes *****/
167
168/* ************************************************************************* */
169/* Control/Configuration functions */
170/* ************************************************************************* */
171
189int MXC_I2C_Init(mxc_i2c_regs_t *i2c, int masterMode, unsigned int slaveAddr);
190
203int MXC_I2C_SetSlaveAddr(mxc_i2c_regs_t *i2c, unsigned int slaveAddr, int idx);
204
213
223
233int MXC_I2C_SetFrequency(mxc_i2c_regs_t *i2c, unsigned int hz);
234
243
257
270
279
295int MXC_I2C_DMA_Init(mxc_i2c_regs_t *i2c, mxc_dma_regs_t *dma, bool use_dma_tx, bool use_dma_rx);
296
306
316
326int MXC_I2C_DMA_SetRequestSelect(mxc_i2c_regs_t *i2c, uint8_t *txData, uint8_t *rxData);
327
328/* ************************************************************************* */
329/* Low-level functions */
330/* ************************************************************************* */
331
344
353
369int MXC_I2C_WriteByte(mxc_i2c_regs_t *i2c, unsigned char byte);
370
385int MXC_I2C_ReadByte(mxc_i2c_regs_t *i2c, unsigned char *byte, int ack);
386
409int MXC_I2C_ReadByteInteractive(mxc_i2c_regs_t *i2c, unsigned char *byte, mxc_i2c_getAck_t getAck);
410
428int MXC_I2C_Write(mxc_i2c_regs_t *i2c, unsigned char *bytes, unsigned int *len);
429
447int MXC_I2C_Read(mxc_i2c_regs_t *i2c, unsigned char *bytes, unsigned int *len, int ack);
448
458int MXC_I2C_ReadRXFIFO(mxc_i2c_regs_t *i2c, volatile unsigned char *bytes, unsigned int len);
459
472int MXC_I2C_ReadRXFIFODMA(mxc_i2c_regs_t *i2c, unsigned char *bytes, unsigned int len,
474
483
493int MXC_I2C_WriteTXFIFO(mxc_i2c_regs_t *i2c, volatile unsigned char *bytes, unsigned int len);
494
506int MXC_I2C_WriteTXFIFODMA(mxc_i2c_regs_t *i2c, unsigned char *bytes, unsigned int len,
508
517
524
531
541int MXC_I2C_GetFlags(mxc_i2c_regs_t *i2c, unsigned int *flags0, unsigned int *flags1);
542
550void MXC_I2C_ClearFlags(mxc_i2c_regs_t *i2c, unsigned int flags0, unsigned int flags1);
551
559void MXC_I2C_EnableInt(mxc_i2c_regs_t *i2c, unsigned int flags0, unsigned int flags1);
560
568void MXC_I2C_DisableInt(mxc_i2c_regs_t *i2c, unsigned int flags0, unsigned int flags1);
569
579
586
593
600
611void MXC_I2C_SetTimeout(mxc_i2c_regs_t *i2c, unsigned int timeout);
612
621
646int MXC_I2C_Recover(mxc_i2c_regs_t *i2c, unsigned int retries);
647
648/* ************************************************************************* */
649/* Transaction level functions */
650/* ************************************************************************* */
651
673int MXC_I2C_MasterTransaction(mxc_i2c_req_t *req);
674
700int MXC_I2C_MasterTransactionAsync(mxc_i2c_req_t *req);
701
734int MXC_I2C_MasterTransactionDMA(mxc_i2c_req_t *req);
735
775
818
840int MXC_I2C_SetRXThreshold(mxc_i2c_regs_t *i2c, unsigned int numBytes);
841
850
873int MXC_I2C_SetTXThreshold(mxc_i2c_regs_t *i2c, unsigned int numBytes);
874
883
893
904
915
925void MXC_I2C_DMACallback(int ch, int error);
926
929#ifdef __cplusplus
930}
931#endif
932
933#endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32675_I2C_H_
Registers, Bit Masks and Bit Positions for the DMA Peripheral Module.
Definition: i2c_regs.h:76
mxc_i2c_complete_cb_t callback
Definition: i2c.h:113
unsigned char * rx_buf
The buffer to read the data into.
Definition: i2c.h:105
unsigned int rx_len
Definition: i2c.h:106
int restart
Definition: i2c.h:109
unsigned char * tx_buf
The buffer containing the bytes to write.
Definition: i2c.h:101
unsigned int addr
The 7-bit or 10-bit address of the slave.
Definition: i2c.h:100
unsigned int tx_len
Definition: i2c.h:102
mxc_i2c_regs_t * i2c
I2C block used.)
Definition: i2c.h:98
int MXC_I2C_ReadByteInteractive(mxc_i2c_regs_t *i2c, unsigned char *byte, mxc_i2c_getAck_t getAck)
Read a single byte from the I2C bus.
int MXC_I2C_SlaveTransaction(mxc_i2c_regs_t *i2c, mxc_i2c_slave_handler_t callback)
Performs a blocking I2C Slave transaction.
int MXC_I2C_WriteByte(mxc_i2c_regs_t *i2c, unsigned char byte)
Write a single byte to the I2C bus.
int MXC_I2C_SetRXThreshold(mxc_i2c_regs_t *i2c, unsigned int numBytes)
Set the receive threshold level.
int MXC_I2C_GetRXFIFOAvailable(mxc_i2c_regs_t *i2c)
Get the number of bytes currently available in the receive FIFO.
void MXC_I2C_ClearFlags(mxc_i2c_regs_t *i2c, unsigned int flags0, unsigned int flags1)
Clears the Interrupt Flags.
void MXC_I2C_DisablePreload(mxc_i2c_regs_t *i2c)
Disable the slave preload mode.
int MXC_I2C_GetTXFIFOAvailable(mxc_i2c_regs_t *i2c)
Get the amount of free space available in the transmit FIFO.
unsigned int MXC_I2C_GetTimeout(mxc_i2c_regs_t *i2c)
Get the current I2C timeout.
int MXC_I2C_ReadRXFIFODMA(mxc_i2c_regs_t *i2c, unsigned char *bytes, unsigned int len, mxc_i2c_dma_complete_cb_t callback)
Unloads bytes from the receive FIFO using DMA for longer reads.
void MXC_I2C_EnableInt(mxc_i2c_regs_t *i2c, unsigned int flags0, unsigned int flags1)
Enable Interrupts.
int MXC_I2C_SetClockStretching(mxc_i2c_regs_t *i2c, int enable)
Enables or disables clock stretching by the slave.
int MXC_I2C_Stop(mxc_i2c_regs_t *i2c)
Generate a stop condition on the I2C bus.
int MXC_I2C_SetSlaveAddr(mxc_i2c_regs_t *i2c, unsigned int slaveAddr, int idx)
Initialize and enable I2C peripheral.
int MXC_I2C_DMA_GetRXChannel(mxc_i2c_regs_t *i2c)
Retreive the DMA RX Channel associated with I2C instance.
void MXC_I2C_ClearRXFIFO(mxc_i2c_regs_t *i2c)
Removes and discards all bytes currently in the receive FIFO.
void MXC_I2C_EnableGeneralCall(mxc_i2c_regs_t *i2c)
Enables the slave to respond to the general call address.
void(* mxc_i2c_complete_cb_t)(mxc_i2c_req_t *req, int result)
The callback routine used by the MXC_I2C_MasterTransactionAsync() function to indicate the transactio...
Definition: i2c.h:78
void MXC_I2C_ClearTXFIFO(mxc_i2c_regs_t *i2c)
Removes and discards all bytes currently in the transmit FIFO.
int(* mxc_i2c_slave_handler_t)(mxc_i2c_regs_t *i2c, mxc_i2c_slave_event_t event, void *data)
The callback routine used by the MXC_I2C_SlaveTransaction() and MXC_I2C_SlaveTransactionAsync functio...
Definition: i2c.h:163
void MXC_I2C_DisableGeneralCall(mxc_i2c_regs_t *i2c)
Prevents the slave from responding to the general call address.
unsigned int MXC_I2C_GetFrequency(mxc_i2c_regs_t *i2c)
Get the frequency of the I2C interface.
int MXC_I2C_MasterTransaction(mxc_i2c_req_t *req)
Performs a blocking I2C Master transaction.
int MXC_I2C_SlaveTransactionAsync(mxc_i2c_regs_t *i2c, mxc_i2c_slave_handler_t callback)
Performs a non-blocking I2C Slave transaction.
int MXC_I2C_MasterTransactionAsync(mxc_i2c_req_t *req)
Performs a non-blocking I2C Master transaction.
unsigned int MXC_I2C_GetRXThreshold(mxc_i2c_regs_t *i2c)
Get the current receive threshold level.
mxc_i2c_slave_event_t
The list of events reported by the MXC_I2C_SlaveTransaction() and MXC_I2C_SlaveTransactionAsync() fun...
Definition: i2c.h:131
int MXC_I2C_SetFrequency(mxc_i2c_regs_t *i2c, unsigned int hz)
Set the frequency of the I2C interface.
int(* mxc_i2c_getAck_t)(mxc_i2c_regs_t *i2c, unsigned char byte)
The callback used by the MXC_I2C_ReadByteInteractive() function.
Definition: i2c.h:67
int MXC_I2C_ReadByte(mxc_i2c_regs_t *i2c, unsigned char *byte, int ack)
Read a single byte from the I2C bus.
int MXC_I2C_GetClockStretching(mxc_i2c_regs_t *i2c)
Determines if clock stretching has been enabled.
void MXC_I2C_AbortAsync(mxc_i2c_regs_t *i2c)
Abort any asynchronous requests in progress.
unsigned int MXC_I2C_GetTXThreshold(mxc_i2c_regs_t *i2c)
Get the current transmit threshold level.
int MXC_I2C_Shutdown(mxc_i2c_regs_t *i2c)
Disable and shutdown I2C peripheral.
int MXC_I2C_GetFlags(mxc_i2c_regs_t *i2c, unsigned int *flags0, unsigned int *flags1)
Get the presently set interrupt flags.
int MXC_I2C_Write(mxc_i2c_regs_t *i2c, unsigned char *bytes, unsigned int *len)
Write multiple bytes to the I2C bus.
void MXC_I2C_SetTimeout(mxc_i2c_regs_t *i2c, unsigned int timeout)
Set the I2C Timeout.
int MXC_I2C_WriteTXFIFO(mxc_i2c_regs_t *i2c, volatile unsigned char *bytes, unsigned int len)
Loads bytes into the transmit FIFO.
int MXC_I2C_Read(mxc_i2c_regs_t *i2c, unsigned char *bytes, unsigned int *len, int ack)
Read multiple bytes from the I2C bus.
int MXC_I2C_Recover(mxc_i2c_regs_t *i2c, unsigned int retries)
Attempts to recover the I2C bus, ensuring the I2C lines are idle.
int MXC_I2C_MasterTransactionDMA(mxc_i2c_req_t *req)
Performs a non-blocking I2C Master transaction using DMA for reduced time in the ISR....
int MXC_I2C_Start(mxc_i2c_regs_t *i2c)
Generate a start (or repeated start) condition on the I2C bus.
int MXC_I2C_DMA_GetTXChannel(mxc_i2c_regs_t *i2c)
Retreive the DMA TX Channel associated with I2C instance.
int MXC_I2C_SetTXThreshold(mxc_i2c_regs_t *i2c, unsigned int numBytes)
Set the transmit threshold level.
int MXC_I2C_DMA_Init(mxc_i2c_regs_t *i2c, mxc_dma_regs_t *dma, bool use_dma_tx, bool use_dma_rx)
Initializes the DMA for I2C DMA transactions.
int MXC_I2C_ReadyForSleep(mxc_i2c_regs_t *i2c)
Checks if the given I2C bus can be placed in sleep more.
void MXC_I2C_DMACallback(int ch, int error)
The processing function for DMA transactions.
void MXC_I2C_AsyncStop(mxc_i2c_regs_t *i2c)
Stop any asynchronous requests in progress.
void MXC_I2C_DisableInt(mxc_i2c_regs_t *i2c, unsigned int flags0, unsigned int flags1)
Disable Interrupts.
int MXC_I2C_Reset(mxc_i2c_regs_t *i2c)
Reset the I2C peripheral.
void MXC_I2C_EnablePreload(mxc_i2c_regs_t *i2c)
Enables the slave preload mode.
void(* mxc_i2c_dma_complete_cb_t)(int len, int result)
The callback routine used by the I2C Read/Write FIFO DMA functions to indicate the transaction has co...
Definition: i2c.h:87
void MXC_I2C_AsyncHandler(mxc_i2c_regs_t *i2c)
The processing function for asynchronous transactions.
int MXC_I2C_DMA_SetRequestSelect(mxc_i2c_regs_t *i2c, uint8_t *txData, uint8_t *rxData)
Sets the I2C instance's DMA TX/RX request select.
int MXC_I2C_ReadRXFIFO(mxc_i2c_regs_t *i2c, volatile unsigned char *bytes, unsigned int len)
Unloads bytes from the receive FIFO.
int MXC_I2C_WriteTXFIFODMA(mxc_i2c_regs_t *i2c, unsigned char *bytes, unsigned int len, mxc_i2c_dma_complete_cb_t callback)
Loads bytes into the transmit FIFO using DMA for longer writes.
int MXC_I2C_Init(mxc_i2c_regs_t *i2c, int masterMode, unsigned int slaveAddr)
Initialize and enable I2C peripheral.
@ MXC_I2C_EVT_RX_THRESH
threshold level.
Definition: i2c.h:136
@ MXC_I2C_EVT_TX_THRESH
threshold level.
Definition: i2c.h:138
@ MXC_I2C_EVT_TRANS_COMP
The transaction has ended.
Definition: i2c.h:140
@ MXC_I2C_EVT_OVERFLOW
FIFO was already full.
Definition: i2c.h:143
@ MXC_I2C_EVT_UNDERFLOW
transmit FIFO was empty.
Definition: i2c.h:141
@ MXC_I2C_EVT_MASTER_WR
requesting a write to the slave.
Definition: i2c.h:132
@ MXC_I2C_EVT_MASTER_RD
requesting a read from the slave.
Definition: i2c.h:134
The information required to perform a complete I2C transaction as the bus master.
Definition: i2c.h:97
Registers, Bit Masks and Bit Positions for the I2C Peripheral Module.