/******************************************************************************
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=247401]@brief[/url] NANO100 series USBD driver source file
* [url=home.php?mod=space&uid=895143]@version[/url] 2.0.0
* [url=home.php?mod=space&uid=212281]@date[/url] 22, March, 2013
*
* @note
* Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
******************************************************************************/
#include <stdio.h>
#include "NUC230_240.h"
#include "hid_transfer.h"
#define I2C_EEPROM I2C1
#define I2C_EEPROM_IRQn I2C1_IRQn
void EnableCLKO(uint32_t u32ClkSrc, uint32_t u32ClkDiv)
{
/* CLKO = clock source / 2^(u32ClkDiv + 1) */
CLK->FRQDIV = CLK_FRQDIV_DIVIDER_EN_Msk | u32ClkDiv;
/* Enable CLKO clock source */
CLK->APBCLK |= CLK_APBCLK_FDIV_EN_Msk;
/* Select CLKO clock source */
CLK->CLKSEL2 = (CLK->CLKSEL2 & (~CLK_CLKSEL2_FRQDIV_S_Msk)) | u32ClkSrc;
}
/*--------------------------------------------------------------------------*/
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* 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 Flash Access Delay */
FMC->FATCON |= FMC_FATCON_FOMSEL1_Msk | FMC_FATCON_FOMSEL0_Msk;
/* Set core clock */
CLK_SetCoreClock(72000000);
/* Enable module clock */
CLK_EnableModuleClock(UART0_MODULE);
CLK_EnableModuleClock(USBD_MODULE);
/* Select module clock source */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));
CLK_SetModuleClock(USBD_MODULE, 0, CLK_CLKDIV_USB(3));
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set GPB multi-function pins for UART0 RXD and TXD, and Clock Output */
SYS->GPB_MFP |= (SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD | SYS_GPB_MFP_PB8_CLKO);
SYS->ALT_MFP |= SYS_ALT_MFP_PB8_CLKO;
/* Enable CLKO(PB8) for monitor HCLK. CLKO = HCLK/8 Hz*/
EnableCLKO((2 << CLK_CLKSEL2_FRQDIV_S_Pos), 2);
}
void UART0_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
/* Reset IP */
SYS->IPRSTC2 |= SYS_IPRSTC2_UART0_RST_Msk;
SYS->IPRSTC2 &= ~SYS_IPRSTC2_UART0_RST_Msk;
/* Configure UART0 and set UART0 Baudrate */
UART0->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HXT, 115200);
UART0->LCR = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1;
}
extern uint8_t volatile g_u8EP3Ready;
typedef __packed struct {
uint8_t u8PID;
uint8_t u8Command;
uint8_t u8Size;
uint8_t u8Buffer[61];
} CMD_T;
CMD_T gCmd;
#define I2C_SET_ADDRESS 0x01
#define I2C_WRITE 0x02
#define I2C_READ 0x03
uint8_t g_u8DeviceAddr;
uint8_t g_u8RxData;
uint8_t g_u8DataLen;
volatile uint8_t g_u8EndFlag = 0;
typedef void (*I2C_FUNC)(uint32_t u32Status);
static I2C_FUNC s_I2CHandlerFn = NULL;
/*---------------------------------------------------------------------------------------------------------*/
/* I2C IRQ Handler */
/*---------------------------------------------------------------------------------------------------------*/
void I2C1_IRQHandler(void)
{
uint32_t u32Status;
u32Status = I2C_EEPROM->I2CSTATUS;
if (I2C_EEPROM->I2CTOC & I2C_I2CTOC_TIF_Msk)
{
/* Clear I2C Timeout Flag */
I2C_EEPROM->I2CTOC |= I2C_I2CTOC_TIF_Msk;
}
else
{
if (s_I2CHandlerFn != NULL)
s_I2CHandlerFn(u32Status);
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* I2C Initial Function */
/*---------------------------------------------------------------------------------------------------------*/
__INLINE void I2C_PIN_Init(void)
{
/* Set GPA10,11 multi-function pins for I2C1 SDA and SCL */
SYS->GPA_MFP |= SYS_GPA_MFP_PA10_I2C1_SDA|SYS_GPA_MFP_PA11_I2C1_SCL;
/* Enable I2C1 clock */
CLK->APBCLK |= CLK_APBCLK_I2C1_EN_Msk;
/* Reset I2C1 */
SYS->IPRSTC2 |= SYS_IPRSTC2_I2C1_RST_Msk;
SYS->IPRSTC2 &= ~SYS_IPRSTC2_I2C1_RST_Msk;
}
void I2C_EEPROM_Init(void)
{
I2C_PIN_Init();
/* Enable I2C Controller */
//I2C_EEPROM->I2CON |= I2C_I2CON_ENS1_Msk;
/* I2C clock divider, I2C Bus Clock = PCLK / (4*120) = 100kHz */
//I2C_EEPROM->I2CLK = I2C_I2CLK_DIV4(u8Divider);
I2C_Open(I2C_EEPROM, 100000);
/* Enable I2C interrupt and set corresponding NVIC bit */
I2C_EEPROM->I2CON |= I2C_I2CON_EI_Msk;
NVIC_EnableIRQ(I2C_EEPROM_IRQn);
}
/*---------------------------------------------------------------------------------------------------------*/
/* I2C Rx Callback Function */
/*---------------------------------------------------------------------------------------------------------*/
void I2C_MasterRx(uint32_t u32Status)
{
#if 0
if (u32Status == 0x08) /* START has been transmitted and prepare SLA+W */
{
I2C_EEPROM->I2CDAT = g_u8DeviceAddr << 1; /* Write SLA+W to Register I2CDAT */
I2C_SET_CONTROL_REG(I2C_EEPROM, I2C_I2CON_SI);
}
else if (u32Status == 0x18) /* SLA+W has been transmitted and ACK has been received */
{
I2C_EEPROM->I2CDAT = gCmd.u8Buffer[g_u8DataLen++];
I2C_SET_CONTROL_REG(I2C_EEPROM, I2C_I2CON_SI);
}
else if (u32Status == 0x20) /* SLA+W has been transmitted and NACK has been received */
{
I2C_SET_CONTROL_REG(I2C_EEPROM, I2C_I2CON_STA_STO_SI);
}
else if (u32Status == 0x28) /* DATA has been transmitted and ACK has been received */
{
if (g_u8DataLen != 2)
{
I2C_EEPROM->I2CDAT = gCmd.u8Buffer[g_u8DataLen++];
I2C_SET_CONTROL_REG(I2C_EEPROM, I2C_I2CON_SI);
}
else
{
I2C_SET_CONTROL_REG(I2C_EEPROM, I2C_I2CON_STA_SI);
}
}
#endif
if (u32Status == 0x08) /* Repeat START has been transmitted and prepare SLA+R */
{
I2C_EEPROM->I2CDAT = ((g_u8DeviceAddr << 1) | 0x01); /* Write SLA+R to Register I2CDAT */
I2C_SET_CONTROL_REG(I2C_EEPROM, I2C_I2CON_SI);
}
else if (u32Status == 0x40) /* SLA+R has been transmitted and ACK has been received */
{
I2C_SET_CONTROL_REG(I2C_EEPROM, I2C_I2CON_SI|I2C_I2CON_SI_AA);
}
else if (u32Status == 0x50) /* DATA has been received and NACK has been returned */
{
if(g_u8DataLen!= gCmd.u8Size-1)
{
gCmd.u8Buffer[g_u8DataLen++]= I2C_EEPROM->I2CDAT;
I2C_SET_CONTROL_REG(I2C_EEPROM, I2C_I2CON_SI|I2C_I2CON_SI_AA);
}else{
gCmd.u8Buffer[g_u8DataLen++]= I2C_EEPROM->I2CDAT;
I2C_SET_CONTROL_REG(I2C_EEPROM, I2C_I2CON_SI);
}
}
else if (u32Status == 0x58) /* DATA has been received and NACK has been returned */
{
g_u8RxData = I2C_EEPROM->I2CDAT;
I2C_SET_CONTROL_REG(I2C_EEPROM, I2C_I2CON_STO_SI);
g_u8EndFlag = 1;
}
else
{
/* TO DO */
printf("Status 0x%x is NOT processed\n", u32Status);
while(1);
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* I2C Tx Callback Function */
/*---------------------------------------------------------------------------------------------------------*/
void I2C_MasterTx(uint32_t u32Status)
{
if (u32Status == 0x08) /* START has been transmitted */
{
I2C_EEPROM->I2CDAT = g_u8DeviceAddr << 1; /* Write SLA+W to Register I2CDAT */
I2C_SET_CONTROL_REG(I2C_EEPROM, I2C_I2CON_SI);
}
else if (u32Status == 0x18) /* SLA+W has been transmitted and ACK has been received */
{
I2C_EEPROM->I2CDAT = gCmd.u8Buffer[g_u8DataLen++];
I2C_SET_CONTROL_REG(I2C_EEPROM, I2C_I2CON_SI);
}
else if (u32Status == 0x20) /* SLA+W has been transmitted and NACK has been received */
{
I2C_SET_CONTROL_REG(I2C_EEPROM, I2C_I2CON_STA_STO_SI);
}
else if (u32Status == 0x28) /* DATA has been transmitted and ACK has been received */
{
if (g_u8DataLen != gCmd.u8Size)
{
I2C_EEPROM->I2CDAT = gCmd.u8Buffer[g_u8DataLen++];
I2C_SET_CONTROL_REG(I2C_EEPROM, I2C_I2CON_SI);
}
else
{
I2C_SET_CONTROL_REG(I2C_EEPROM, I2C_I2CON_STO_SI);
g_u8EndFlag = 1;
}
}
else
{
/* TO DO */
//printf("Status 0x%x is NOT processed\n", u32Status);
while(1);
}
}
void I2C_EEPROM_Write(void)
{
g_u8DataLen = 0;
g_u8EndFlag = 0;
/* I2C function to write data to slave */
s_I2CHandlerFn = (I2C_FUNC)I2C_MasterTx;
/* I2C as master sends START signal */
I2C_SET_CONTROL_REG(I2C_EEPROM, I2C_I2CON_STA);
/* Wait I2C Tx Finish */
while (g_u8EndFlag == 0);
}
/*---------------------------------------------------------------------------------------------------------*/
/* Read I2C EEPROM */
/*---------------------------------------------------------------------------------------------------------*/
uint8_t I2C_EEPROM_Read(void)
{
g_u8DataLen = 0;
g_u8EndFlag = 0;
/* I2C function to write data to slave */
s_I2CHandlerFn = (I2C_FUNC)I2C_MasterRx;
/* I2C as master sends START signal */
I2C_SET_CONTROL_REG(I2C_EEPROM, I2C_I2CON_STA);
/* Wait I2C Tx Finish */
while (g_u8EndFlag == 0);
return g_u8RxData;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Main Function */
/*---------------------------------------------------------------------------------------------------------*/
int32_t main(void)
{
uint8_t *ptrOut, *ptrIn;
/*
This sample code demonstrate how to use HID interface to transfer data
between PC and USB device.
A demo window tool are also included in "WindowsTool" directory with this
sample code. User can use it to test data transfer with this sample code.
*/
/* Unlock write-protected registers */
SYS_UnlockReg();
/* Init system and multi-funcition I/O */
SYS_Init();
/* Init UART for debug message */
UART0_Init();
I2C_EEPROM_Init();
printf("NuMicro USB HID Transfer Sample\n");
/* Open USB controller */
USBD_Open(&gsInfo, HID_ClassRequest, NULL);
/*Init Endpoint configuration for HID */
HID_Init();
/* Start USB device */
USBD_Start();
/* Enable USB device interrupt */
NVIC_EnableIRQ(USBD_IRQn);
while(1)
{
if(g_u8EP3Ready==1)
{
g_u8EP3Ready=0;
/* Interrupt OUT */
ptrOut = (uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP3));
ptrIn = (uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP2));
USBD_MemCopy((uint8_t *)&gCmd, ptrOut, 64);
if(gCmd.u8Command==I2C_SET_ADDRESS)
{
g_u8DeviceAddr=gCmd.u8Size;
printf("i2c address 0x%x\n\r",gCmd.u8Size);
}
if(gCmd.u8Command==I2C_WRITE)
{
printf("i2c write\n\r");
I2C_EEPROM_Write();
}
if(gCmd.u8Command==I2C_READ)
{
printf("i2c read\n\r");
I2C_EEPROM_Read();
}
gCmd.u8PID+=1;
USBD_MemCopy(ptrIn, (void *)&gCmd, 64);
USBD_SET_PAYLOAD_LEN(EP3, EP3_MAX_PKT_SIZE);
USBD_SET_PAYLOAD_LEN(EP2, EP2_MAX_PKT_SIZE);
}
}
}
/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/