/**************************************************************************//**
* @file main.c
* @version V3.00
* $Revision: 4 $
* $Date: 16/06/21 7:36p $
* @brief
* Show how to set I2C use Multi bytes API Read and Write data to Slave.
* Needs to work with I2C_Slave sample code.
* @note
* Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
*
******************************************************************************/
#include <stdio.h>
#include "NUC123.h"
#define PLLCTL_SETTING CLK_PLLCTL_72MHz_HXT
#define PLL_CLOCK 72000000
/*---------------------------------------------------------------------------------------------------------*/
/* Global variables */
/*---------------------------------------------------------------------------------------------------------*/
volatile uint8_t g_u8DeviceAddr;
/*---------------------------------------------------------------------------------------------------------*/
/* System initial */
/*---------------------------------------------------------------------------------------------------------*/
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable XT1_OUT (PF0) and XT1_IN (PF1) */
SYS->GPF_MFP |= SYS_GPF_MFP_PF0_XT1_OUT | SYS_GPF_MFP_PF1_XT1_IN;
/* Enable Internal RC 22.1184MHz clock */
CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);
/* Waiting for Internal RC clock ready */
CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);
/* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));
/* Enable external XTAL 12MHz clock */
CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);
/* Waiting for external XTAL clock ready */
CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);
/* Set core clock as PLL_CLOCK from PLL */
CLK_SetCoreClock(PLL_CLOCK);
/* Enable UART module clock */
CLK_EnableModuleClock(UART0_MODULE);
/* Enable I2C0, I2C1 module clock */
CLK_EnableModuleClock(I2C0_MODULE);
/* Select UART module clock source */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set GPB multi-function pins for UART0 RXD and TXD */
SYS->GPB_MFP = SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD;
/* Set GPF multi-function pins for I2C0 SDA and SCL */
SYS->GPF_MFP |= (SYS_GPF_MFP_PF2_I2C0_SDA | SYS_GPF_MFP_PF3_I2C0_SCL);
SYS->ALT_MFP1 &= ~(SYS_ALT_MFP1_PF2_Msk | SYS_ALT_MFP1_PF3_Msk);
SYS->ALT_MFP1 |= (SYS_ALT_MFP1_PF2_I2C0_SDA | SYS_ALT_MFP1_PF3_I2C0_SCL);
}
void UART0_Init()
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
/* Reset IP */
SYS_ResetModule(UART0_RST);
/* Configure UART0 and set UART0 Baudrate */
UART_Open(UART0, 115200);
}
void I2C0_Init(void)
{
/* Open I2C0 module and set bus clock */
I2C_Open(I2C0, 100000);
/* Get I2C0 Bus Clock */
printf("I2C0 clock %d Hz\n", I2C_GetBusClockFreq(I2C0));
/* Set I2C0 4 Slave Addresses */
I2C_SetSlaveAddr(I2C0, 0, 0x15, 0); /* Slave Address : 0x15 */
I2C_SetSlaveAddr(I2C0, 1, 0x35, 0); /* Slave Address : 0x35 */
I2C_SetSlaveAddr(I2C0, 2, 0x55, 0); /* Slave Address : 0x55 */
I2C_SetSlaveAddr(I2C0, 3, 0x75, 0); /* Slave Address : 0x75 */
/* Set I2C0 4 Slave Addresses Mask */
I2C_SetSlaveAddrMask(I2C0, 0, 0x01);
I2C_SetSlaveAddrMask(I2C0, 1, 0x04);
I2C_SetSlaveAddrMask(I2C0, 2, 0x01);
I2C_SetSlaveAddrMask(I2C0, 3, 0x04);
}
void I2C0_Close(void)
{
/* Disable I2C0 interrupt and clear corresponding NVIC bit */
I2C_DisableInt(I2C0);
NVIC_DisableIRQ(I2C0_IRQn);
/* Disable I2C0 and close I2C0 clock */
I2C_Close(I2C0);
CLK_DisableModuleClock(I2C0_MODULE);
}
/*---------------------------------------------------------------------------------------------------------*/
/* Main Function */
/*---------------------------------------------------------------------------------------------------------*/
int32_t main(void)
{
uint32_t i;
uint8_t txbuf[256] = {0}, rDataBuf[256] = {0};
/* Unlock protected registers */
SYS_UnlockReg();
/* Init System, IP clock and multi-function I/O */
SYS_Init();
/* Init UART0 for printf */
UART0_Init();
/* Lock protected registers */
SYS_LockReg();
/*
This sample code sets I2C bus clock to 100kHz. Then, Master accesses Slave with Byte Write
and Byte Read operations, and check if the read data is equal to the programmed data.
*/
printf("+--------------------------------------------------------+\n");
printf("| I2C Driver Sample Code for Multi Bytes Read/Write Test |\n");
printf("| Needs to work with I2C_Slave sample code |\n");
printf("| |\n");
printf("| I2C Master (I2C0) <---> I2C Slave (I2C0) |\n");
printf("| !! This sample code requires two borads to test !! |\n");
printf("+--------------------------------------------------------+\n");
printf("\n");
printf("Configure I2C0 as Master\n");
printf("The I/O connection to I2C0\n");
printf("I2C0_SDA(PF.2), I2C0_SCL(PF.3)\n\n");
/* Init I2C0 */
I2C0_Init();
/* Slave address */
g_u8DeviceAddr = 0x15;
/* Prepare data for transmission */
for(i = 0; i<256; i++)
{
txbuf[i] = (uint8_t) i+3;
}
for(i=0; i<256; i+=32)
{
/* Write 32 bytes data to Slave */
while(I2C_WriteMultiBytesTwoRegs(I2C0, g_u8DeviceAddr, i, &txbuf[i], 32) < 32);
}
printf("Multi bytes Write access Pass.....\n");
printf("\n");
/* Use Multi Bytes Read from Slave (Two Registers) */
while(I2C_ReadMultiBytesTwoRegs(I2C0, g_u8DeviceAddr, 0x0000, rDataBuf, 256) < 256);
/* Compare TX data and RX data */
for(i = 0; i<256; i++)
{
if(txbuf[i] != rDataBuf[i])
printf("Data compare fail... R[%d] Data: 0x%X\n", i, rDataBuf[i]);
}
printf("Multi bytes Read access Pass.....\n");
while(1);
}