Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Examples): Add generic I2C RX/TX DMA vector flexibility to I2C examples for all parts #693

Merged
merged 32 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ee9de3b
feat(Examples): Add I2C DMA to I2C examples for MAX32670 and MAX32675
sihyung-maxim Jul 28, 2023
f68aeea
style(Examples): Fix linter
sihyung-maxim Jul 28, 2023
3569d84
fix(Examples): Revert back to I2C1, not I2C2
sihyung-maxim Jul 28, 2023
ba0e820
Revert back to previous I2C values
sihyung-maxim Jul 28, 2023
164d2fb
feat(PeriphDrivers): Decouple DMA initialization in I2C DMA transaction
sihyung-maxim Jul 31, 2023
8bab86b
Merge branch 'feat/me15_i2c_dma' of github.com:Analog-Devices-MSDK/ms…
sihyung-maxim Jul 31, 2023
5cfd2d2
clang-format bot reformatting.
sihyung-maxim Jul 31, 2023
c7feaa8
feat(PeriphDrivers): Add New DMA functions to all parts
sihyung-maxim Jul 31, 2023
0f3848e
Merge branch 'feat/me15_i2c_dma' of github.com:Analog-Devices-MSDK/ms…
sihyung-maxim Jul 31, 2023
bff6c29
feat(PeriphDrivers): Add New DMA functions to all parts
sihyung-maxim Jul 31, 2023
8896e68
feat(PeriphDrivers): Add I2C shutdown to all parts
sihyung-maxim Jul 31, 2023
70c6399
fix(PeriphDrivers): Update I2C RevB FIFODMA functions
sihyung-maxim Jul 31, 2023
1be3039
clang-format bot reformatting.
sihyung-maxim Aug 1, 2023
3bde325
fix(PeriphDrivers): Add dma_regs.h to i2c.h
sihyung-maxim Aug 1, 2023
9d8503c
fix(Examples): Update all I2C examples to be consistent with each other
sihyung-maxim Aug 1, 2023
93d1d59
clang-format bot reformatting.
sihyung-maxim Aug 1, 2023
4fcd65a
fix(Examples): Fix build errors and printf formatting
sihyung-maxim Aug 1, 2023
9cbeff2
fix(Examples): Fix README printf formatting
sihyung-maxim Aug 1, 2023
afc421c
fix(PeriphDrivers): Fix I2C build warnings
sihyung-maxim Aug 1, 2023
157c926
fix(PeriphDrivers): Remove extra code
sihyung-maxim Aug 1, 2023
06d839f
feat(PeriphDrivers): Add selectable I2C DMA TX or RX function
sihyung-maxim Aug 22, 2023
acdd73f
fix(Examples): Fix build errors with new I2C DMA function
sihyung-maxim Aug 22, 2023
7951ca7
fix(PeriphDrivers): Fix build errors
sihyung-maxim Aug 22, 2023
6d67432
clang-format bot reformatting.
sihyung-maxim Aug 22, 2023
ce64aa7
Merge branch 'main' into feat/me15_i2c_dma
sihyung-maxim Aug 22, 2023
b7ad5f7
fix(PeriphDrivers): Fix typecast error
sihyung-maxim Aug 22, 2023
633dd66
fix(PeriphDrivers): Fix build errors
sihyung-maxim Aug 22, 2023
e64954f
Use == false instead of != true
sihyung-maxim Aug 22, 2023
872c4d1
fix(Examples,PeriphDrivers): PR review
sihyung-maxim Sep 5, 2023
209fb59
fix(PeriphDrivers): Remove excess local variables
sihyung-maxim Sep 5, 2023
ec78ae6
Merge branch 'main' into feat/me15_i2c_dma
sihyung-maxim Sep 5, 2023
9d134e2
Resolve merge conflicts
Jake-Carter Sep 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Examples/MAX32572/I2C/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Universal instructions on building, flashing, and debugging this project can be

## Expected Output
```
******** I2C SLAVE ASYNC TRANSACTION TEST *********
******** I2C Master-Slave Transaction Demo *********

This example uses one I2C peripheral as a master to
read and write to another I2C which acts as a slave.
Expand All @@ -37,9 +37,9 @@ P2.19 to P0.7 (SDA).

-->Result:

-->TxData: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61 62 63
-->TxData: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe

-->RxData: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61 62 63
-->RxData: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe


-->I2C Transaction Successful
Expand Down
112 changes: 60 additions & 52 deletions Examples/MAX32572/I2C/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,32 @@

/**
* @file main.c
* @brief I2C Loopback Example
* @brief I2C Master-Slave Transaction Demo
* @details This example uses the I2C Master to read/write from/to the I2C Slave. For
* this example you must connect P0.6 to P0.14 (SDA) and P0.7 to P0.15 (SCL). The Master
* will use P0.6 and P0.7. The Slave will use P0.14 and P0.15. You must also
* this example you must connect P0.6 to P0.18 (SDA) and P0.7 to P0.19 (SCL). The Master
* will use P0.6 and P0.7. The Slave will use P0.18 and P0.19. You must also
* connect the pull-up jumpers (JP23 and JP24) to the proper I/O voltage.
* Refer to JP27 to determine the I/O voltage.
* @note Other devices on the EvKit will be using the same bus. This example cannot be combined with
* a PMIC or bluetooth example because the I2C Slave uses GPIO pins for those devices.
*/

/***** Includes *****/

#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include <MAX32xxx.h>
#include "mxc_device.h"
#include "mxc_delay.h"
#include "mxc_errors.h"
#include "nvic_table.h"
#include "i2c.h"
#include "dma.h"
#include "led.h"

/***** Definitions *****/
// #define MASTERDMA

#define MASTERDMA //Comment this line out if standard I2C transaction is required

#define I2C_MASTER MXC_I2C1
#define I2C_SLAVE MXC_I2C2
Expand All @@ -60,14 +67,14 @@
#define I2C_SLAVE_ADDR (0x51)
#define I2C_BYTES 255

typedef enum { FAILED, PASSED } test_t;

/***** Globals *****/

static uint8_t Stxdata[I2C_BYTES];
static uint8_t Srxdata[I2C_BYTES];
static uint8_t txdata[I2C_BYTES];
static uint8_t rxdata[I2C_BYTES];
volatile uint8_t DMA_FLAG = 0;
int8_t DMA_TX_CH;
int8_t DMA_RX_CH;
volatile int I2C_FLAG;
volatile int txnum = 0;
volatile int txcnt = 0;
Expand All @@ -79,28 +86,22 @@ volatile int rxnum = 0;
void I2C2_IRQHandler(void)
{
MXC_I2C_AsyncHandler(I2C_SLAVE);
return;
}

void DMA0_IRQHandler(void)
void DMA_TX_IRQHandler(void)
{
MXC_DMA_Handler();
MXC_DMA_ReleaseChannel(0);
DMA_FLAG = 1;
}

void DMA1_IRQHandler(void)
void DMA_RX_IRQHandler(void)
{
MXC_DMA_Handler();
MXC_DMA_ReleaseChannel(1);
DMA_FLAG = 1;
}

//I2C callback function
void I2C_Callback(mxc_i2c_req_t *req, int error)
{
I2C_FLAG = error;
return;
}

int slaveHandler(mxc_i2c_regs_t *i2c, mxc_i2c_slave_event_t event, void *data)
Expand All @@ -113,28 +114,23 @@ int slaveHandler(mxc_i2c_regs_t *i2c, mxc_i2c_slave_event_t event, void *data)
break;

case MXC_I2C_EVT_MASTER_RD:

// Serve as a 16 byte loopback, returning data*2
for (int i = 0; i < I2C_BYTES; i++) {
Stxdata[i] = i;
}

txnum = I2C_BYTES;
txcnt = 0;
i2c->int_fl0 = MXC_F_I2C_INT_FL0_TX_LOCK_OUT | MXC_F_I2C_INT_FL0_ADDR_MATCH;
i2c->intfl0 = MXC_F_I2C_INTFL0_TX_LOCKOUT | MXC_F_I2C_INTFL0_ADDR_MATCH;
break;

case MXC_I2C_EVT_RX_THRESH:
case MXC_I2C_EVT_OVERFLOW:
rxnum += MXC_I2C_ReadRXFIFO(i2c, &Srxdata[rxnum], MXC_I2C_GetRXFIFOAvailable(i2c));
if (rxnum == I2C_BYTES) {
i2c->int_en0 |= MXC_F_I2C_INT_EN0_ADDR_MATCH;
i2c->inten0 |= MXC_F_I2C_INTEN0_ADDR_MATCH;
}

break;

case MXC_I2C_EVT_TX_THRESH:
case MXC_I2C_EVT_UNDERFLOW:

// Write as much data as possible into TX FIFO
// Unless we're at the end of the transaction (only write what's needed)
if (txcnt >= txnum) {
Expand All @@ -149,10 +145,11 @@ int slaveHandler(mxc_i2c_regs_t *i2c, mxc_i2c_slave_event_t event, void *data)
default:
if (*((int *)data) == E_COMM_ERR) {
printf("I2C Slave Error!\n");
printf("i2c->int_fl0 = 0x%08x\n", i2c->int_fl0);
printf("i2c->status = 0x%08x\n", i2c->status);
printf("i2c->intfl0 = 0x%08x\n", i2c->intfl0);
printf("i2c->status = 0x%08x\n", i2c->status);
I2C_Callback(NULL, E_COMM_ERR);
return 1;

} else if (*((int *)data) == E_NO_ERROR) {
rxnum += MXC_I2C_ReadRXFIFO(i2c, &Srxdata[rxnum], MXC_I2C_GetRXFIFOAvailable(i2c));
I2C_Callback(NULL, E_NO_ERROR);
Expand All @@ -167,6 +164,7 @@ int slaveHandler(mxc_i2c_regs_t *i2c, mxc_i2c_slave_event_t event, void *data)
void printData(void)
{
int i;

printf("\n-->TxData: ");

for (i = 0; i < sizeof(txdata); ++i) {
Expand All @@ -185,7 +183,7 @@ void printData(void)
}

//Compare data to see if they are the same
int verifyData()
int verifyData(void)
{
int i, fails = 0;

Expand All @@ -196,16 +194,16 @@ int verifyData()
}

if (fails > 0) {
return FAILED;
} else {
return PASSED;
return E_FAIL;
}

return E_NO_ERROR;
}

// *****************************************************************************
int main()
{
printf("\n******** I2C SLAVE ASYNC TRANSACTION TEST *********\n");
printf("\n******** I2C Master-Slave Transaction Demo *********\n");
printf("\nThis example uses one I2C peripheral as a master to\n");
printf("read and write to another I2C which acts as a slave.\n");

Expand All @@ -216,18 +214,27 @@ int main()

//Setup the I2CM
error = MXC_I2C_Init(I2C_MASTER, 1, 0);

if (error != E_NO_ERROR) {
printf("Failed master\n");
return FAILED;
}

//Setup the I2CM DMA
error = MXC_I2C_DMA_Init(I2C_MASTER, MXC_DMA, true, true);
if (error != E_NO_ERROR) {
printf("Failed DMA master\n");
return error;
} else {
printf("\n-->I2C Master Initialization Complete");
}

//Setup the I2CS
error = MXC_I2C_Init(I2C_SLAVE, 0, I2C_SLAVE_ADDR);

if (error != E_NO_ERROR) {
printf("Failed slave\n");
return FAILED;
} else {
printf("\n-->I2C Slave Initialization Complete");
}

NVIC_SetVector(I2C2_IRQn, I2C2_IRQHandler);
Expand All @@ -242,6 +249,8 @@ int main()
for (i = 0; i < I2C_BYTES; i++) {
txdata[i] = i;
rxdata[i] = 0;
Stxdata[i] = i;
Srxdata[i] = 0;
}

// This will write data to slave
Expand All @@ -264,43 +273,42 @@ int main()
return FAILED;
}

MXC_DMA_ReleaseChannel(0);
MXC_DMA_ReleaseChannel(1);

#ifdef MASTERDMA
NVIC_EnableIRQ(DMA0_IRQn);
NVIC_EnableIRQ(DMA1_IRQn);
__enable_irq();
DMA_TX_CH = MXC_I2C_DMA_GetTXChannel(I2C_MASTER);
DMA_RX_CH = MXC_I2C_DMA_GetRXChannel(I2C_MASTER);

NVIC_EnableIRQ(MXC_DMA_CH_GET_IRQ(DMA_TX_CH));
NVIC_EnableIRQ(MXC_DMA_CH_GET_IRQ(DMA_RX_CH));

MXC_NVIC_SetVector(MXC_DMA_CH_GET_IRQ(DMA_TX_CH), DMA_TX_IRQHandler);
MXC_NVIC_SetVector(MXC_DMA_CH_GET_IRQ(DMA_RX_CH), DMA_RX_IRQHandler);

if ((error = MXC_I2C_MasterTransactionDMA(&reqMaster)) != 0) {
printf("Error writing: %d\n", error);
return FAILED;
return error;
}

while (DMA_FLAG == 0) {}

#else
if ((error = MXC_I2C_MasterTransaction(&reqMaster)) != 0) {
printf("Error writing: %d\n", error);
return FAILED;
return error;
}
#endif

while (I2C_FLAG == 1) {}

#endif

printf("\n-->Result: \n");

printData();

printf("\n");

MXC_I2C_Shutdown(I2C_MASTER);
MXC_I2C_Shutdown(I2C_SLAVE);

if (verifyData()) {
printf("\n-->I2C Transaction Successful\n");
LED_On(0);
return 0;
} else {
printf("\n-->I2C Transaction Failed\n");
return -1;
return E_FAIL;
}

return E_NO_ERROR;
}
9 changes: 6 additions & 3 deletions Examples/MAX32650/I2C/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ Universal instructions on building, flashing, and debugging this project can be

## Expected Output
```
***** I2C Loopback Example *****
This example uses one I2C peripheral as a master
to read and write to another I2C which acts as a slave.
******** I2C Master-Slave Transaction Demo *********

This example uses one I2C peripheral as a master to
read and write to another I2C which acts as a slave.

You will need to connect P2.7 to P2.17 (SDA) and
P2.8 to P2.18 (SCL).

-->I2C Master Initialization Complete
-->I2C Slave Initialization Complete

-->Writing data to slave, and reading the data back

Expand Down
Loading