Hi,
Unfortunately, I don't have the exact code because I don't have the external ADC. I suggest taking as base the I2C master polling_b2b_transfer.
The i2c_polling_b2b_transfer_master example shows how to use i2c driver as master to do board to board transfer
using polling method:
In this example, one i2c instance as master and another i2c instance on the other board as slave. Master sends a
piece of data to slave, and receive a piece of data from slave. This example checks if the data received from
slave is correct.
You need to consider the following:
1. Connect to P0_11 (I2C0_SDA) and P0_10 (I2C0_SCL) the external ADC device.
2. Check the pull-up resistances.
3. Check the correct address from the external ADC
/*
* Copyright (c) 2016, Freescale Semiconductor, Inc.
* Copyright 2016-2019 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
/* Standard C Included Files */
#include <stdio.h>
#include <string.h>
#include "board.h"
#include "fsl_debug_console.h"
#include "fsl_i2c.h"
#include "pin_mux.h"
/*******************************************************************************
* Definitions
******************************************************************************/
#define EXAMPLE_I2C_MASTER_BASE (I2C0_BASE)
#define I2C_MASTER_CLOCK_FREQUENCY (12000000)
#define WAIT_TIME 10U
#define EXAMPLE_I2C_MASTER ((I2C_Type *)EXAMPLE_I2C_MASTER_BASE)
#define I2C_MASTER_SLAVE_ADDR_7BIT 0x7EU
#define I2C_BAUDRATE 100000U
#define I2C_DATA_LENGTH 33U
/*******************************************************************************
* Prototypes
******************************************************************************/
/*******************************************************************************
* Variables
******************************************************************************/
uint8_t g_master_txBuff[I2C_DATA_LENGTH];
uint8_t g_master_rxBuff[I2C_DATA_LENGTH];
/*******************************************************************************
* Code
******************************************************************************/
/*!
* @brief Main function
*/
int main(void)
{
i2c_master_config_t masterConfig;
status_t reVal = kStatus_Fail;
uint8_t deviceAddress = 0x01U; // Please check the correct address of the external ADC
/* Enable clock of uart0. */
CLOCK_EnableClock(kCLOCK_Uart0);
/* Ser DIV of uart0. */
CLOCK_SetClkDivider(kCLOCK_DivUsartClk, 1U);
/* Enable clock of i2c0. */
CLOCK_EnableClock(kCLOCK_I2c0);
BOARD_InitPins();
BOARD_BootClockIRC12M();
BOARD_InitDebugConsole();
PRINTF("\r\nI2C board2board polling example -- Master transfer.\r\n");
/* Set up i2c master to send data to slave*/
/* First data in txBuff is data length of the transmiting data. */
g_master_txBuff[0] = I2C_DATA_LENGTH - 1U;
for (uint32_t i = 1U; i < I2C_DATA_LENGTH; i++)
{
g_master_txBuff[i] = i - 1;
}
PRINTF("Master will send data :");
for (uint32_t i = 0U; i < I2C_DATA_LENGTH - 1U; i++)
{
if (i % 8 == 0)
{
PRINTF("\r\n");
}
PRINTF("0x%2x ", g_master_txBuff[i + 1]);
}
PRINTF("\r\n\r\n");
/*
* masterConfig.debugEnable = false;
* masterConfig.ignoreAck = false;
* masterConfig.pinConfig = kI2C_2PinOpenDrain;
* masterConfig.baudRate_Bps = 100000U;
* masterConfig.busIdleTimeout_ns = 0;
* masterConfig.pinLowTimeout_ns = 0;
* masterConfig.sdaGlitchFilterWidth_ns = 0;
* masterConfig.sclGlitchFilterWidth_ns = 0;
*/
I2C_MasterGetDefaultConfig(&masterConfig);
/* Change the default baudrate configuration */
masterConfig.baudRate_Bps = I2C_BAUDRATE;
/* Initialize the I2C master peripheral */
I2C_MasterInit(EXAMPLE_I2C_MASTER, &masterConfig, I2C_MASTER_CLOCK_FREQUENCY);
/* Send master blocking data to slave */
if (kStatus_Success == I2C_MasterStart(EXAMPLE_I2C_MASTER, I2C_MASTER_SLAVE_ADDR_7BIT, kI2C_Write))
{
/* subAddress = 0x01, data = g_master_txBuff - write to slave.
start + slaveaddress(w) + subAddress + length of data buffer + data buffer + stop*/
reVal = I2C_MasterWriteBlocking(EXAMPLE_I2C_MASTER, &deviceAddress, 1, kI2C_TransferNoStopFlag);
if (reVal != kStatus_Success)
{
return -1;
}
reVal = I2C_MasterWriteBlocking(EXAMPLE_I2C_MASTER, g_master_txBuff, I2C_DATA_LENGTH, kI2C_TransferDefaultFlag);
if (reVal != kStatus_Success)
{
return -1;
}
reVal = I2C_MasterStop(EXAMPLE_I2C_MASTER);
if (reVal != kStatus_Success)
{
return -1;
}
}
/* Wait until the slave is ready for transmit, wait time depend on user's case.
Slave devices that need some time to process received byte or are not ready yet to
send the next byte, can pull the clock low to signal to the master that it should wait.*/
for (uint32_t i = 0U; i < WAIT_TIME; i++)
{
__NOP();
}
PRINTF("Receive sent data from slave :");
/* Receive blocking data from slave */
/* subAddress = 0x01, data = g_master_rxBuff - read from slave.
start + slaveaddress(w) + subAddress + repeated start + slaveaddress(r) + rx data buffer + stop */
if (kStatus_Success == I2C_MasterStart(EXAMPLE_I2C_MASTER, I2C_MASTER_SLAVE_ADDR_7BIT, kI2C_Write))
{
reVal = I2C_MasterWriteBlocking(EXAMPLE_I2C_MASTER, &deviceAddress, 1, kI2C_TransferNoStopFlag);
if (reVal != kStatus_Success)
{
return -1;
}
reVal = I2C_MasterRepeatedStart(EXAMPLE_I2C_MASTER, I2C_MASTER_SLAVE_ADDR_7BIT, kI2C_Read);
if (reVal != kStatus_Success)
{
return -1;
}
reVal =
I2C_MasterReadBlocking(EXAMPLE_I2C_MASTER, g_master_rxBuff, I2C_DATA_LENGTH - 1, kI2C_TransferDefaultFlag);
if (reVal != kStatus_Success)
{
return -1;
}
reVal = I2C_MasterStop(EXAMPLE_I2C_MASTER);
if (reVal != kStatus_Success)
{
return -1;
}
}
for (uint32_t i = 0U; i < I2C_DATA_LENGTH - 1; i++)
{
if (i % 8 == 0)
{
PRINTF("\r\n");
}
PRINTF("0x%2x ", g_master_rxBuff[i]);
}
PRINTF("\r\n\r\n");
/* Transfer completed. Check the data.*/
for (uint32_t i = 0U; i < I2C_DATA_LENGTH - 1; i++)
{
if (g_master_rxBuff[i] != g_master_txBuff[i + 1])
{
PRINTF("\r\nError occurred in the transfer ! \r\n");
break;
}
}
PRINTF("\r\nEnd of I2C example .\r\n");
while (1)
{
}
}
Regards
Soledad