/******************************************************************************
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* @brief
* This sample code supports to use GPIO to simulate key input.
* @note
* Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
******************************************************************************/
#include <stdio.h>
#include "NUC122.h"
#include "hid.h"
#include "string.h"
#include "usbd_user.h"
/*--------------------------------------------------------------------------*/
extern uint8_t volatile g_u8Suspend;
uint8_t volatile g_u8EP2Ready = 0;
uint8_t volatile g_u8EP3Ready = 0;
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 core clock */
CLK_SetCoreClock(48000000);
/* 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(2));
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set GPB multi-function pins for UART0 RXD and TXD */
SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk);
SYS->GPB_MFP |= (SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD);
}
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;
}
void PowerDown()
{
printf("Enter power down ...\n");
SYS_UnlockReg();
/* Wakeup Enable */
USBD->INTEN |= USBD_INTEN_WAKEUP_EN_Msk;
/* To check if all the debug messages are finished */
UART_WAIT_TX_EMPTY(UART0);
CLK_PowerDown();
if(g_usbd_RemoteWakeupEn && g_u8Suspend)
{
/* Enable PHY before sending Resume('K') state */
USBD->ATTR |= USBD_ATTR_PHY_EN_Msk;
/* Keep remote wakeup for 1 ms */
USBD->ATTR |= USBD_ATTR_RWAKEUP_Msk;
CLK_SysTickDelay(1000); /* Delay 1ms */
USBD->ATTR ^= USBD_ATTR_RWAKEUP_Msk;
printf("Remote Wakeup!!\n");
}
printf("device wakeup!\n");
}
void GPCD_IRQHandler(void)
{
/* To check if PD1 or PD2 interrupt occurred */
if(GPIO_GET_INT_FLAG(PD, BIT1))
{
GPIO_CLR_INT_FLAG(PD, BIT1);
printf("PD.1 INT occurred.\n");
}
else if(GPIO_GET_INT_FLAG(PD, BIT2))
{
GPIO_CLR_INT_FLAG(PD, BIT2);
printf("PD.2 INT occurred.\n");
}
else
{
/* Un-expected interrupt. Just clear all PA and PB interrupts */
PC->ISRC = PC->ISRC;
PD->ISRC = PD->ISRC;
printf("Un-expected interrupts.\n");
}
}
/*---------------------------------------------------------------------------------------------------------*/
/* Main Function */
/*---------------------------------------------------------------------------------------------------------*/
int32_t main(void)
{
/* Unlock protected registers */
SYS_UnlockReg();
SYS_Init();
UART0_Init();
printf("\n");
printf("+-------------------------------------------------------+\n");
printf("| NuMicro USB HID Sample Code |\n");
printf("+-------------------------------------------------------+\n");
printf("Use GPD1 to move mouse pointer right when GPD1 = 0\n");
printf("Use GPD2 to report a key 'a' when GPD2 = 0\n");
printf("Use GPD3 to report key - Volume + when GPD3 = 0\n");
printf("Use GPD4 to report key - Volume - when GPD4 = 0\n");
/* Set GPD5 output */
GPIO_SetMode(PD, BIT5, GPIO_PMD_OUTPUT);
/* Enable PD Interrupt for Remote Wakeup */
GPIO_EnableInt(PD, 1, GPIO_INT_FALLING);
GPIO_EnableInt(PD, 2, GPIO_INT_FALLING);
NVIC_EnableIRQ(GPCD_IRQn);
USBD_User_Open(&gsUserInfo, HID_ClassRequest, NULL);
/* Endpoint configuration */
HID_Init();
USBD_Start();
NVIC_EnableIRQ(USBD_IRQn);
/* start to IN data */
g_u8EP2Ready = 1;
g_u8EP3Ready = 1;
while(1)
{
HID_UpdateKbData();
HID_UpdateMsData();
/* Enter power down when USB suspend */
if(g_u8Suspend)
PowerDown();
if(memcmp(SetReport, PreSetReport, 2))
{
if(SetReport[1] & 0x2)
PD5 = 1; /* Turn On CapsLock LED */
else
PD5 = 0; /* Turn Off CapsLock LED */
memcpy(PreSetReport, SetReport, 2);
}
}
}
/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
|