Skip to content

Commit

Permalink
fix(Examples): Update all I2C examples to be consistent with each other
Browse files Browse the repository at this point in the history
  • Loading branch information
sihyung-maxim committed Aug 1, 2023
1 parent 3bde325 commit 9d8503c
Show file tree
Hide file tree
Showing 24 changed files with 715 additions and 346 deletions.
7 changes: 4 additions & 3 deletions Examples/MAX32572/I2C/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,25 @@ 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.
You will need to connect P2.18 to P0.6 (SCL) and
P2.19 to P0.7 (SDA).
-->I2C Master Initialization Complete
-->I2C Slave Initialization Complete
-->Writing data to slave, and reading the data back
-->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
108 changes: 60 additions & 48 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,27 @@ 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;
Stxdata[i] = Srxdata[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 +149,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->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 +168,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 +187,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 +198,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 +218,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);
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 +253,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 +277,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;
}
10 changes: 7 additions & 3 deletions Examples/MAX32650/I2C/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ 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
-->Result:
Expand Down
Loading

0 comments on commit 9d8503c

Please sign in to comment.