#include <stdio.h>
#include "board.h"
#include "peripherals.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "fsl_debug_console.h"
#include <string.h>
#include "fsl_lpi2c.h"
#define EXAMPLE_I2C_MASTER_BASE (LPI2C1_BASE)
#define EXAMPLE_I2C_MASTER ((LPI2C_Type *)EXAMPLE_I2C_MASTER_BASE)
#define I2C_MASTER_SLAVE_ADDR_7BIT 0x1AU
//
// Global variables
//
lpi2c_master_handle_t g_m_handle;
volatile bool g_MasterCompletionFlag = false;
volatile bool g_MasterNackFlag = false;
static void lpi2c_master_callback(LPI2C_Type *base, lpi2c_master_handle_t *handle, status_t status, void *userData)
{
PRINTF("\r\n*** INT\r\n");
if (status == kStatus_LPI2C_Nak)
{
g_MasterNackFlag = true;
}
else
{
g_MasterCompletionFlag = true;
/* Display failure information when status is not success. */
if (status != kStatus_Success)
{
PRINTF("\r\n*** Error occured during transfer! status = %d\r\n", status);
}
}
}
/*
* @brief Application entry point.
*/
int main(void) {
/* Init board hardware. */
BOARD_ConfigMPU();
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitBootPeripherals();
#ifndef BOARD_INIT_DEBUG_CONSOLE_PERIPHERAL
/* Init FSL debug console. */
BOARD_InitDebugConsole();
#endif
lpi2c_master_transfer_t masterXfer = {0};
status_t reVal = kStatus_Fail;
/* Create the LPI2C handle for the non-blocking transfer */
LPI2C_MasterTransferCreateHandle(EXAMPLE_I2C_MASTER, &g_m_handle, lpi2c_master_callback, NULL);
PRINTF("\r\nSend data to slave...\r\n");
uint8_t deviceAddress = 0x01U;
uint8_t data[1] = {0xAA};
masterXfer.slaveAddress = I2C_MASTER_SLAVE_ADDR_7BIT;
masterXfer.direction = kLPI2C_Write;
masterXfer.subaddress = (uint32_t)deviceAddress;
masterXfer.subaddressSize = 1;
masterXfer.data = data;
masterXfer.dataSize = 1;
masterXfer.flags = kLPI2C_TransferDefaultFlag;
/* Send master non-blocking data to slave */
reVal = LPI2C_MasterTransferNonBlocking(EXAMPLE_I2C_MASTER, &g_m_handle, &masterXfer);
if (reVal != kStatus_Success)
{
PRINTF("\r\nI2C transfer error!\r\n");
return -1;
}
/* Wait for transfer completed. */
while ((!g_MasterCompletionFlag) && (!g_MasterNackFlag))
{
}
if (g_MasterNackFlag)
{
PRINTF("- Master nacked by slave!\r\n");
g_MasterNackFlag = false;
}
g_MasterCompletionFlag = false;
PRINTF("\r\nEnd of LPI2C example.\r\n");
while (1)
{
}
return 0 ;
}