- /**************************************************************************//**
- * [url=home.php?mod=space&uid=288409]@file[/url] main.c
- * [url=home.php?mod=space&uid=895143]@version[/url] V3.00
- * @brief
- * Show how to use I2C Signle byte API Read and Write data to Slave
- * Needs to work with I2C_Slave sample code.
- * [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2016 Nuvoton Technology Corp. All rights reserved.
- *****************************************************************************/
- #include <stdio.h>
- #include "NuMicro.h"
- #define PLL_CLOCK 192000000
- /*---------------------------------------------------------------------------------------------------------*/
- /* Global variables */
- /*---------------------------------------------------------------------------------------------------------*/
- volatile uint8_t g_u8DeviceAddr;
- void SYS_Init(void)
- {
- /* Set PF multi-function pins for XT1_OUT(PF.2) and XT1_IN(PF.3) */
- SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF2MFP_Msk)) | SYS_GPF_MFPL_PF2MFP_XT1_OUT;
- SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF3MFP_Msk)) | SYS_GPF_MFPL_PF3MFP_XT1_IN;
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init System Clock */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Enable HIRC clock */
- CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
- /* Wait for HIRC clock ready */
- CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
- /* Select HCLK clock source as HIRC and HCLK source divider as 1 */
- CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
- /* Set XT1_OUT(PF.2) and XT1_IN(PF.3) to input mode */
- PF->MODE &= ~(GPIO_MODE_MODE2_Msk | GPIO_MODE_MODE3_Msk);
- /* Enable HXT clock */
- CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);
- /* Wait for HXT clock ready */
- CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);
- /* Set core clock as PLL_CLOCK from PLL */
- CLK_SetCoreClock(PLL_CLOCK);
- /* Enable UART module clock */
- CLK_EnableModuleClock(UART0_MODULE);
- /* Select UART module clock source as HXT `and UART module clock divider as 1 */
- CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HXT, CLK_CLKDIV0_UART0(1));
- /* Enable I2C0 peripheral clock */
- CLK_EnableModuleClock(I2C0_MODULE);
- /* Update System Core Clock */
- /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and CyclesPerUs automatically. */
- SystemCoreClockUpdate();
- /* Set PA multi-function pins for UART0 RXD(PA.0) and TXD(PA.1) */
- SYS->GPA_MFPL = (SYS->GPA_MFPL & (~SYS_GPA_MFPL_PA0MFP_Msk)) | SYS_GPA_MFPL_PA0MFP_UART0_RXD;
- SYS->GPA_MFPL = (SYS->GPA_MFPL & (~SYS_GPA_MFPL_PA1MFP_Msk)) | SYS_GPA_MFPL_PA1MFP_UART0_TXD;
- /* Set I2C0 multi-function pins */
- SYS->GPA_MFPL = (SYS->GPA_MFPL & ~(SYS_GPA_MFPL_PA4MFP_Msk | SYS_GPA_MFPL_PA5MFP_Msk)) |
- (SYS_GPA_MFPL_PA4MFP_I2C0_SDA | SYS_GPA_MFPL_PA5MFP_I2C0_SCL);
- /* I2C pin enable schmitt trigger */
- PA->SMTEN |= GPIO_SMTEN_SMTEN4_Msk | GPIO_SMTEN_SMTEN5_Msk;
- }
- void I2C0_Init(void)
- {
- /* Open I2C module and set bus clock */
- I2C_Open(I2C0, 100000);
- /* Get I2C0 Bus Clock */
- printf("I2C clock %d Hz\n", I2C_GetBusClockFreq(I2C0));
- }
- 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);
- }
- int32_t main(void)
- {
- uint32_t i;
- uint8_t u8data, u8tmp, err;
- /* Unlock protected registers */
- SYS_UnlockReg();
- /* Init System, IP clock and multi-function I/O. */
- SYS_Init();
- /* Lock protected registers */
- SYS_LockReg();
- /* Configure UART0: 115200, 8-bit word, no parity bit, 1 stop bit. */
- UART_Open(UART0, 115200);
- /*
- 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 Single Byte 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");
- /* Init I2C0 */
- I2C0_Init();
- /* Slave Address */
- g_u8DeviceAddr = 0x15;
- err = 0;
- for(i = 0; i < 256; i++)
- {
- u8tmp = (uint8_t)i + 3;
- /* Single Byte Write (Two Registers) */
- while(I2C_WriteByteTwoRegs(I2C0, g_u8DeviceAddr, i, u8tmp));
- /* Single Byte Read (Two Registers) */
- u8data = I2C_ReadByteTwoRegs(I2C0, g_u8DeviceAddr, i);
- if(u8data != u8tmp)
- {
- err = 1;
- printf("%03d: Single byte write data fail, W(0x%X)/R(0x%X) \n", i, u8tmp, u8data);
- }
- }
- printf("\n");
- if(err)
- printf("Single byte Read/Write access Fail.....\n");
- else
- printf("Single byte Read/Write access Pass.....\n");
- while(1);
- }
- /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
|