[DemoCode下载] 用NUC开发键盘鼠标示例

[复制链接]
1948|4
 楼主| huahuagg 发表于 2021-7-10 18:14 | 显示全部楼层 |阅读模式
EC_NUC122_USBD_HID_KB_MS_MMKey_LEDCtrl_V1.00.zip (3.42 MB, 下载次数: 10)
 楼主| huahuagg 发表于 2021-7-10 18:14 | 显示全部楼层
  1. /******************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * @brief
  4. *           This sample code supports to use GPIO to simulate key input.
  5. * @note
  6. * Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
  7. ******************************************************************************/
  8. #include <stdio.h>
  9. #include "NUC122.h"
  10. #include "hid.h"
  11. #include "string.h"
  12. #include "usbd_user.h"

  13. /*--------------------------------------------------------------------------*/
  14. extern uint8_t volatile g_u8Suspend;
  15. uint8_t volatile g_u8EP2Ready = 0;
  16. uint8_t volatile g_u8EP3Ready = 0;

  17. void SYS_Init(void)
  18. {
  19.     /*---------------------------------------------------------------------------------------------------------*/
  20.     /* Init System Clock                                                                                       */
  21.     /*---------------------------------------------------------------------------------------------------------*/

  22.     /* Enable Internal RC 22.1184MHz clock */
  23.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  24.     /* Waiting for Internal RC clock ready */
  25.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  26.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  27.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  28.     /* Enable external XTAL 12MHz clock */
  29.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  30.     /* Waiting for external XTAL clock ready */
  31.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  32.     /* Set core clock */
  33.     CLK_SetCoreClock(48000000);

  34.     /* Enable module clock */
  35.     CLK_EnableModuleClock(UART0_MODULE);
  36.     CLK_EnableModuleClock(USBD_MODULE);

  37.     /* Select module clock source */
  38.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));
  39.     CLK_SetModuleClock(USBD_MODULE, 0, CLK_CLKDIV_USB(2));


  40.     /*---------------------------------------------------------------------------------------------------------*/
  41.     /* Init I/O Multi-function                                                                                 */
  42.     /*---------------------------------------------------------------------------------------------------------*/

  43.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  44.     SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk);
  45.     SYS->GPB_MFP |= (SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD);
  46. }


  47. void UART0_Init(void)
  48. {
  49.     /*---------------------------------------------------------------------------------------------------------*/
  50.     /* Init UART                                                                                               */
  51.     /*---------------------------------------------------------------------------------------------------------*/
  52.     /* Reset IP */
  53.     SYS->IPRSTC2 |=  SYS_IPRSTC2_UART0_RST_Msk;
  54.     SYS->IPRSTC2 &= ~SYS_IPRSTC2_UART0_RST_Msk;

  55.     /* Configure UART0 and set UART0 Baudrate */
  56.     UART0->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HXT, 115200);
  57.     UART0->LCR = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1;
  58. }

  59. void PowerDown()
  60. {
  61.     printf("Enter power down ...\n");

  62.     SYS_UnlockReg();

  63.     /* Wakeup Enable */
  64.     USBD->INTEN |= USBD_INTEN_WAKEUP_EN_Msk;

  65.     /* To check if all the debug messages are finished */
  66.     UART_WAIT_TX_EMPTY(UART0);

  67.     CLK_PowerDown();

  68.     if(g_usbd_RemoteWakeupEn && g_u8Suspend)
  69.     {
  70.         /* Enable PHY before sending Resume('K') state */
  71.         USBD->ATTR |= USBD_ATTR_PHY_EN_Msk;

  72.         /* Keep remote wakeup for 1 ms */
  73.         USBD->ATTR |= USBD_ATTR_RWAKEUP_Msk;
  74.         CLK_SysTickDelay(1000); /* Delay 1ms */
  75.         USBD->ATTR ^= USBD_ATTR_RWAKEUP_Msk;
  76.         printf("Remote Wakeup!!\n");
  77.     }

  78.     printf("device wakeup!\n");

  79. }
  80. void GPCD_IRQHandler(void)
  81. {
  82.     /* To check if PD1 or PD2 interrupt occurred */
  83.     if(GPIO_GET_INT_FLAG(PD, BIT1))
  84.     {
  85.         GPIO_CLR_INT_FLAG(PD, BIT1);
  86.         printf("PD.1 INT occurred.\n");
  87.     }
  88.     else if(GPIO_GET_INT_FLAG(PD, BIT2))
  89.     {
  90.         GPIO_CLR_INT_FLAG(PD, BIT2);
  91.         printf("PD.2 INT occurred.\n");
  92.     }
  93.     else
  94.     {
  95.         /* Un-expected interrupt. Just clear all PA and PB interrupts */
  96.         PC->ISRC = PC->ISRC;
  97.         PD->ISRC = PD->ISRC;
  98.         printf("Un-expected interrupts.\n");
  99.     }
  100. }

  101. /*---------------------------------------------------------------------------------------------------------*/
  102. /*  Main Function                                                                                          */
  103. /*---------------------------------------------------------------------------------------------------------*/
  104. int32_t main(void)
  105. {
  106.     /* Unlock protected registers */
  107.     SYS_UnlockReg();

  108.     SYS_Init();
  109.     UART0_Init();

  110.     printf("\n");
  111.     printf("+-------------------------------------------------------+\n");
  112.     printf("|              NuMicro USB HID Sample Code              |\n");
  113.     printf("+-------------------------------------------------------+\n");
  114.     printf("Use GPD1 to move mouse pointer right when GPD1 = 0\n");
  115.     printf("Use GPD2 to report a key 'a' when GPD2 = 0\n");
  116.     printf("Use GPD3 to report key - Volume + when GPD3 = 0\n");
  117.     printf("Use GPD4 to report key - Volume - when GPD4 = 0\n");

  118.     /* Set GPD5 output */
  119.     GPIO_SetMode(PD, BIT5, GPIO_PMD_OUTPUT);

  120.     /* Enable PD Interrupt for Remote Wakeup */
  121.     GPIO_EnableInt(PD, 1, GPIO_INT_FALLING);
  122.     GPIO_EnableInt(PD, 2, GPIO_INT_FALLING);
  123.     NVIC_EnableIRQ(GPCD_IRQn);

  124.     USBD_User_Open(&gsUserInfo, HID_ClassRequest, NULL);

  125.     /* Endpoint configuration */
  126.     HID_Init();

  127.     USBD_Start();

  128.     NVIC_EnableIRQ(USBD_IRQn);

  129.     /* start to IN data */
  130.     g_u8EP2Ready = 1;
  131.     g_u8EP3Ready = 1;

  132.     while(1)
  133.     {
  134.         HID_UpdateKbData();

  135.         HID_UpdateMsData();

  136.         /* Enter power down when USB suspend */
  137.         if(g_u8Suspend)
  138.             PowerDown();

  139.         if(memcmp(SetReport, PreSetReport, 2))
  140.         {
  141.             if(SetReport[1] &  0x2)
  142.                 PD5 = 1;    /* Turn On CapsLock LED */
  143.             else
  144.                 PD5 = 0;    /* Turn Off CapsLock LED */
  145.             memcpy(PreSetReport, SetReport, 2);
  146.         }
  147.     }
  148. }



  149. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/

 楼主| huahuagg 发表于 2021-7-10 21:57 | 显示全部楼层
学习一下,以后做个高级的键盘。
fuqinyyy 发表于 2021-7-11 09:02 来自手机 | 显示全部楼层
可以做个键盘模拟游戏
twjiang 发表于 2021-7-12 10:01 | 显示全部楼层
想开发  USB-UVC 的摄像头 streaming 吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

159

主题

1430

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部