Skip to content

Commit

Permalink
Sync up i2c_reva.c to latest main
Browse files Browse the repository at this point in the history
  • Loading branch information
sihyung-maxim committed Oct 4, 2024
1 parent 3a86bf6 commit 378c06c
Showing 1 changed file with 46 additions and 14 deletions.
60 changes: 46 additions & 14 deletions Libraries/PeriphDrivers/Source/I2C/i2c_reva.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/******************************************************************************
*
* Copyright (C) 2022-2023 Maxim Integrated Products, Inc. All Rights Reserved.
* (now owned by Analog Devices, Inc.),
* Copyright (C) 2023 Analog Devices, Inc. All Rights Reserved. This software
* is proprietary to Analog Devices, Inc. and its licensors.
* Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
* Analog Devices, Inc.),
* Copyright (C) 2023-2024 Analog Devices, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,6 +32,7 @@
#include "i2c_reva.h"
#include "dma.h"
#include "dma_reva.h"
#include "nvic_table.h"

/* **** Variable Declaration **** */
typedef struct {
Expand Down Expand Up @@ -298,8 +298,8 @@ int MXC_I2C_RevA_DMA_Init(mxc_i2c_reva_regs_t *i2c, mxc_dma_reva_regs_t *dma, bo
bool use_dma_rx)
{
int8_t i2cNum;
int8_t rxChannel;
int8_t txChannel;
int8_t rxChannel = -1;
int8_t txChannel = -1;

if (i2c == NULL || dma == NULL) {
return E_NULL_PTR;
Expand Down Expand Up @@ -355,6 +355,15 @@ int MXC_I2C_RevA_DMA_Init(mxc_i2c_reva_regs_t *i2c, mxc_dma_reva_regs_t *dma, bo
MXC_DMA_SetChannelInterruptEn(txChannel, 0, 1);

states[i2cNum].channelTx = txChannel;
#ifdef __arm__
NVIC_EnableIRQ(MXC_DMA_CH_GET_IRQ(txChannel));
#if TARGET_NUM == 32665
MXC_NVIC_SetVector(MXC_DMA_CH_GET_IRQ(txChannel),
MXC_DMA_Get_DMA_Handler((mxc_dma_regs_t *)dma));
#else
MXC_NVIC_SetVector(MXC_DMA_CH_GET_IRQ(txChannel), MXC_DMA_Handler);
#endif
#endif
}

// Set up I2C DMA RX.
Expand Down Expand Up @@ -385,6 +394,15 @@ int MXC_I2C_RevA_DMA_Init(mxc_i2c_reva_regs_t *i2c, mxc_dma_reva_regs_t *dma, bo

MXC_DMA_EnableInt(rxChannel);
MXC_DMA_SetChannelInterruptEn(rxChannel, 0, 1);
#ifdef __arm__
NVIC_EnableIRQ(MXC_DMA_CH_GET_IRQ(rxChannel));
#if TARGET_NUM == 32665
MXC_NVIC_SetVector(MXC_DMA_CH_GET_IRQ(txChannel),
MXC_DMA_Get_DMA_Handler((mxc_dma_regs_t *)dma));
#else
MXC_NVIC_SetVector(MXC_DMA_CH_GET_IRQ(txChannel), MXC_DMA_Handler);
#endif
#endif

states[i2cNum].channelRx = rxChannel;
}
Expand Down Expand Up @@ -1083,8 +1101,8 @@ int MXC_I2C_RevA_MasterTransactionDMA(mxc_i2c_reva_req_t *req, mxc_dma_regs_t *d
MXC_I2C_SetRXThreshold((mxc_i2c_regs_t *)i2c, 1);

states[i2cNum].req = req;
states[i2cNum].writeDone = 0;
states[i2cNum].readDone = 0;
states[i2cNum].writeDone = (req->tx_len == 0);
states[i2cNum].readDone = (req->rx_len == 0);

// If MXC_I2C_DMA_Init(...) was not already called, then configure both DMA TX/RXchannels by default.
if (states[i2cNum].dma_initialized == false) {
Expand Down Expand Up @@ -1123,17 +1141,31 @@ int MXC_I2C_RevA_MasterTransactionDMA(mxc_i2c_reva_req_t *req, mxc_dma_regs_t *d
i2c->rxctrl1 = req->rx_len; // 0 for 256, otherwise number of bytes to read
}

MXC_I2C_Start((mxc_i2c_regs_t *)i2c); // Start or Restart as needed

while (i2c->mstctrl & MXC_F_I2C_REVA_MSTCTRL_RESTART) {}

i2c->fifo = ((req->addr) << 1) | 0x1; // Load the slave address with write bit set

#if TARGET_NUM == 32665
MXC_I2C_ReadRXFIFODMA((mxc_i2c_regs_t *)i2c, req->rx_buf, req->rx_len, NULL, dma);
#else
MXC_I2C_ReadRXFIFODMA((mxc_i2c_regs_t *)i2c, req->rx_buf, req->rx_len, NULL);
#endif

MXC_I2C_Start((mxc_i2c_regs_t *)i2c); // Start or Restart as needed

while (i2c->mstctrl & MXC_F_I2C_REVA_MSTCTRL_RESTART) {}

i2c->fifo = ((req->addr) << 1) | 0x1; // Load the slave address with write bit set
while (!((i2c->intfl0 & MXC_F_I2C_REVA_INTFL0_ADDR_ACK) ||
(i2c->intfl0 & MXC_F_I2C_REVA_INTFL0_ADDR_NACK_ERR))) {
// Wait for an ACK or NACK from the slave
}
if (!(i2c->intfl0 & MXC_F_I2C_REVA_INTFL0_ADDR_ACK)) {
// If we did not get an ACK, then something went wrong.
// Abort the transaction and signal the user's callback
MXC_I2C_RevA_Stop(i2c);
MXC_DMA_Stop(states[i2cNum].channelRx);
if (states[i2cNum].req->callback != NULL) {
states[i2cNum].req->callback(states[i2cNum].req, E_COMM_ERR);
}
return E_COMM_ERR;
}
}
} else {
states[i2cNum].readDone = 1;
Expand Down

0 comments on commit 378c06c

Please sign in to comment.