[DemoCode下载] USBD_HID_Keyboard,最新例程

[复制链接]
1201|10
 楼主| 天灵灵地灵灵 发表于 2020-7-4 23:04 | 显示全部楼层 |阅读模式
  1. /******************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * @brief
  4. *           Demonstrate how to implement a USB keyboard device.
  5. *           It supports to use GPIO to simulate key input.
  6. * @note
  7. * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  8. ******************************************************************************/
  9. #include <stdio.h>
  10. #include "NUC029xGE.h"
  11. #include "hid_kb.h"

  12. /* If crystal-less is enabled, system won't use any crystal as clock source
  13.    If using crystal-less, system will be 48MHz, otherwise, system is 72MHz
  14. */
  15. #define CRYSTAL_LESS        1
  16. #define HIRC48_AUTO_TRIM    0x412   /* Use USB signal to fine tune HIRC 48MHz */
  17. #define TRIM_INIT           (SYS_BASE+0x118)

  18. /*--------------------------------------------------------------------------*/
  19. uint8_t volatile g_u8EP2Ready = 0;
  20. int IsDebugFifoEmpty(void);


  21. void SYS_Init(void)
  22. {

  23.     /*---------------------------------------------------------------------------------------------------------*/
  24.     /* Init System Clock                                                                                       */
  25.     /*---------------------------------------------------------------------------------------------------------*/

  26.     /* Enable Internal RC 22.1184 MHz clock */
  27.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  28.     /* Waiting for Internal RC clock ready */
  29.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  30.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  31.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  32. #if (!CRYSTAL_LESS)
  33.     /* Enable external XTAL 12 MHz clock */
  34.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  35.     /* Waiting for external XTAL clock ready */
  36.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  37.     /* Set core clock */
  38.     CLK_SetCoreClock(72000000);

  39.     /* Use HIRC as UART clock source */
  40.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HIRC, CLK_CLKDIV0_UART(1));

  41.     /* Use PLL as USB clock source */
  42.     CLK_SetModuleClock(USBD_MODULE, CLK_CLKSEL3_USBDSEL_PLL, CLK_CLKDIV0_USB(3));

  43. #else
  44.     /* Enable Internal RC 48MHz clock */
  45.     CLK_EnableXtalRC(CLK_PWRCTL_HIRC48EN_Msk);

  46.     /* Waiting for Internal RC clock ready */
  47.     CLK_WaitClockReady(CLK_STATUS_HIRC48STB_Msk);

  48.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  49.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC48, CLK_CLKDIV0_HCLK(1));

  50.     /* Use HIRC as UART clock source */
  51.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HIRC, CLK_CLKDIV0_UART(1));

  52.     /* Use HIRC48 as USB clock source */
  53.     CLK_SetModuleClock(USBD_MODULE, CLK_CLKSEL3_USBDSEL_HIRC48, CLK_CLKDIV0_USB(1));
  54. #endif

  55.     /* Enable module clock */
  56.     CLK_EnableModuleClock(UART0_MODULE);
  57.     CLK_EnableModuleClock(USBD_MODULE);


  58.     /*---------------------------------------------------------------------------------------------------------*/
  59.     /* Init I/O Multi-function                                                                                 */
  60.     /*---------------------------------------------------------------------------------------------------------*/

  61.     /* Set PA multi-function pins for UART0 RXD and TXD */
  62.     SYS->GPA_MFPL &= ~(SYS_GPA_MFPL_PA3MFP_Msk | SYS_GPA_MFPL_PA2MFP_Msk);
  63.     SYS->GPA_MFPL |= (SYS_GPA_MFPL_PA3MFP_UART0_RXD | SYS_GPA_MFPL_PA2MFP_UART0_TXD);
  64. }


  65. void UART0_Init(void)
  66. {
  67.     /*---------------------------------------------------------------------------------------------------------*/
  68.     /* Init UART                                                                                               */
  69.     /*---------------------------------------------------------------------------------------------------------*/
  70.     /* Reset IP */
  71.     SYS->IPRST1 |=  SYS_IPRST1_UART0RST_Msk;
  72.     SYS->IPRST1 &= ~SYS_IPRST1_UART0RST_Msk;

  73.     /* Configure UART0 and set UART0 Baudrate */
  74.     UART0->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HIRC, 115200);
  75.     UART0->LINE = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1;
  76. }


  77. void HID_UpdateKbData(void)
  78. {
  79.     int32_t i;
  80.     uint8_t *buf;
  81.     uint32_t key = 0xF;
  82.     static uint32_t preKey;

  83.     if(g_u8EP2Ready)
  84.     {
  85.         buf = (uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP2));

  86.         /* If PB15 = 0, just report it is key 'a' */
  87.         key = (PB->PIN & (1 << 15)) ? 0 : 1;

  88.         if(key == 0)
  89.         {
  90.             for(i = 0; i < 8; i++)
  91.             {
  92.                 buf[i] = 0;
  93.             }

  94.             if(key != preKey)
  95.             {
  96.                 /* Trigger to note key release */
  97.                 USBD_SET_PAYLOAD_LEN(EP2, 8);
  98.             }
  99.         }
  100.         else
  101.         {
  102.             preKey = key;
  103.             buf[2] = 0x04; /* Key A */
  104.             USBD_SET_PAYLOAD_LEN(EP2, 8);
  105.         }
  106.     }
  107. }

  108. void PowerDown()
  109. {
  110.     /* Unlock protected registers */
  111.     SYS_UnlockReg();

  112.     printf("Enter power down ...\n");
  113.     while(!IsDebugFifoEmpty());

  114.     /* Wakeup Enable */
  115.     USBD_ENABLE_INT(USBD_INTEN_WKEN_Msk);

  116.     CLK_PowerDown();

  117.     /* Clear PWR_DOWN_EN if it is not clear by itself */
  118.     if(CLK->PWRCTL & CLK_PWRCTL_PDEN_Msk)
  119.         CLK->PWRCTL ^= CLK_PWRCTL_PDEN_Msk;

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

  121.     /* Lock protected registers */
  122.     SYS_LockReg();
  123. }

  124. /*---------------------------------------------------------------------------------------------------------*/
  125. /*  Main Function                                                                                          */
  126. /*---------------------------------------------------------------------------------------------------------*/
  127. int32_t main(void)
  128. {
  129. #if CRYSTAL_LESS
  130.     uint32_t u32TrimInit;
  131. #endif

  132.     /* Unlock protected registers */
  133.     SYS_UnlockReg();

  134.     SYS_Init();
  135.     UART0_Init();

  136.     printf("\n");
  137.     printf("+--------------------------------------------------------+\n");
  138.     printf("|          NuMicro USB HID Keyboard Sample Code          |\n");
  139.     printf("+--------------------------------------------------------+\n");
  140.     printf("If PB15 = 0, just report it is key 'a'.\n");

  141.     USBD_Open(&gsInfo, HID_ClassRequest, NULL);

  142.     /* Endpoint configuration */
  143.     HID_Init();
  144.     USBD_Start();

  145.     /* Enable USB device interrupt */
  146.     NVIC_EnableIRQ(USBD_IRQn);

  147. #if CRYSTAL_LESS
  148.     /* Backup default trim */
  149.     u32TrimInit = M32(TRIM_INIT);
  150. #endif

  151.     /* Clear SOF */
  152.     USBD->INTSTS = USBD_INTSTS_SOFIF_Msk;

  153.     /* start to IN data */
  154.     g_u8EP2Ready = 1;

  155.     while(1)
  156.     {
  157. #if CRYSTAL_LESS
  158.         /* Start USB trim if it is not enabled. */
  159.         if((SYS->IRCTCTL1 & SYS_IRCTCTL1_FREQSEL_Msk) != 2)
  160.         {
  161.             /* Start USB trim only when SOF */
  162.             if(USBD->INTSTS & USBD_INTSTS_SOFIF_Msk)
  163.             {
  164.                 /* Clear SOF */
  165.                 USBD->INTSTS = USBD_INTSTS_SOFIF_Msk;

  166.                 /* Re-enable crystal-less */
  167.                 SYS->IRCTCTL1 = HIRC48_AUTO_TRIM;
  168.             }
  169.         }

  170.         /* Disable USB Trim when error */
  171.         if(SYS->IRCTISTS & (SYS_IRCTISTS_CLKERRIF1_Msk | SYS_IRCTISTS_TFAILIF1_Msk))
  172.         {
  173.             /* Init TRIM */
  174.             M32(TRIM_INIT) = u32TrimInit;

  175.             /* Disable crystal-less */
  176.             SYS->IRCTCTL1 = 0;

  177.             /* Clear error flags */
  178.             SYS->IRCTISTS = SYS_IRCTISTS_CLKERRIF1_Msk | SYS_IRCTISTS_TFAILIF1_Msk;

  179.             /* Clear SOF */
  180.             USBD->INTSTS = USBD_INTSTS_SOFIF_Msk;
  181.         }
  182. #endif

  183.         /* Enter power down when USB suspend */
  184.         if(g_u8Suspend)
  185.             PowerDown();

  186.         HID_UpdateKbData();
  187.     }
  188. }



  189. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/



 楼主| 天灵灵地灵灵 发表于 2020-7-4 23:05 | 显示全部楼层
  1. /******************************************************************************
  2. * @file     hid_kb.h
  3. * @brief    NUC029xGE series USB HID keyboard header file
  4. *
  5. * @note
  6. * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  7. ******************************************************************************/
  8. #ifndef __USBD_HID_H__
  9. #define __USBD_HID_H__

  10. /* Define the vendor id and product id */
  11. #define USBD_VID        0x0416
  12. #define USBD_PID        0x0123

  13. /*!<Define HID Class Specific Request */
  14. #define GET_REPORT          0x01
  15. #define GET_IDLE            0x02
  16. #define GET_PROTOCOL        0x03
  17. #define SET_REPORT          0x09
  18. #define SET_IDLE            0x0A
  19. #define SET_PROTOCOL        0x0B

  20. /*!<USB HID Interface Class protocol */
  21. #define HID_NONE            0x00
  22. #define HID_KEYBOARD        0x01
  23. #define HID_MOUSE           0x02

  24. /*!<USB HID Class Report Type */
  25. #define HID_RPT_TYPE_INPUT      0x01
  26. #define HID_RPT_TYPE_OUTPUT     0x02
  27. #define HID_RPT_TYPE_FEATURE    0x03

  28. /*-------------------------------------------------------------*/
  29. /* Define EP maximum packet size */
  30. #define EP0_MAX_PKT_SIZE    8
  31. #define EP1_MAX_PKT_SIZE    EP0_MAX_PKT_SIZE
  32. #define EP2_MAX_PKT_SIZE    8

  33. #define SETUP_BUF_BASE  0
  34. #define SETUP_BUF_LEN   8
  35. #define EP0_BUF_BASE    (SETUP_BUF_BASE + SETUP_BUF_LEN)
  36. #define EP0_BUF_LEN     EP0_MAX_PKT_SIZE
  37. #define EP1_BUF_BASE    (SETUP_BUF_BASE + SETUP_BUF_LEN)
  38. #define EP1_BUF_LEN     EP1_MAX_PKT_SIZE
  39. #define EP2_BUF_BASE    (EP1_BUF_BASE + EP1_BUF_LEN)
  40. #define EP2_BUF_LEN     EP2_MAX_PKT_SIZE

  41. /* Define the interrupt In EP number */
  42. #define INT_IN_EP_NUM   0x01

  43. /* Define Descriptor information */
  44. #define HID_DEFAULT_INT_IN_INTERVAL     20
  45. #define USBD_SELF_POWERED               0
  46. #define USBD_REMOTE_WAKEUP              0
  47. #define USBD_MAX_POWER                  50  /* The unit is in 2mA. ex: 50 * 2mA = 100mA */

  48. #define LEN_CONFIG_AND_SUBORDINATE      (LEN_CONFIG+LEN_INTERFACE+LEN_HID+LEN_ENDPOINT)


  49. /*-------------------------------------------------------------*/
  50. extern uint8_t volatile g_u8EP2Ready;
  51. extern uint8_t volatile g_u8Suspend;

  52. /*-------------------------------------------------------------*/
  53. void HID_Init(void);
  54. void HID_ClassRequest(void);
  55. void EP2_Handler(void);

  56. #endif  /* __USBD_HID_H_ */

  57. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
 楼主| 天灵灵地灵灵 发表于 2020-7-4 23:05 | 显示全部楼层
  1. /******************************************************************************
  2. * @file     hid_kb.c
  3. * @brief    NUC029xGE series USBD HID keyboard sample file
  4. *
  5. * @note
  6. * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  7. ******************************************************************************/

  8. /*!<Includes */
  9. #include <string.h>
  10. #include "NUC029xGE.h"
  11. #include "usbd.h"
  12. #include "hid_kb.h"

  13. uint8_t volatile g_u8Suspend = 0;
  14. uint8_t g_u8Idle = 0, g_u8Protocol = 0;

  15. void USBD_IRQHandler(void)
  16. {
  17.     uint32_t u32IntSts = USBD_GET_INT_FLAG();
  18.     uint32_t u32State = USBD_GET_BUS_STATE();

  19. //------------------------------------------------------------------
  20.     if(u32IntSts & USBD_INTSTS_FLDET)
  21.     {
  22.         // Floating detect
  23.         USBD_CLR_INT_FLAG(USBD_INTSTS_FLDET);

  24.         if(USBD_IS_ATTACHED())
  25.         {
  26.             /* USB Plug In */
  27.             USBD_ENABLE_USB();
  28.         }
  29.         else
  30.         {
  31.             /* USB Un-plug */
  32.             USBD_DISABLE_USB();
  33.         }
  34.     }

  35. //------------------------------------------------------------------
  36.     if(u32IntSts & USBD_INTSTS_WAKEUP)
  37.     {
  38.         /* Clear event flag */
  39.         USBD_CLR_INT_FLAG(USBD_INTSTS_WAKEUP);
  40.     }

  41. //------------------------------------------------------------------
  42.     if(u32IntSts & USBD_INTSTS_BUS)
  43.     {
  44.         /* Clear event flag */
  45.         USBD_CLR_INT_FLAG(USBD_INTSTS_BUS);

  46.         if(u32State & USBD_STATE_USBRST)
  47.         {
  48.             /* Bus reset */
  49.             USBD_ENABLE_USB();
  50.             USBD_SwReset();
  51.             g_u8Suspend = 0;
  52.         }
  53.         if(u32State & USBD_STATE_SUSPEND)
  54.         {
  55.             /* Enter power down to wait USB attached */
  56.             g_u8Suspend = 1;

  57.             /* Enable USB but disable PHY */
  58.             USBD_DISABLE_PHY();
  59.         }
  60.         if(u32State & USBD_STATE_RESUME)
  61.         {
  62.             /* Enable USB and enable PHY */
  63.             USBD_ENABLE_USB();
  64.             g_u8Suspend = 0;
  65.         }
  66.     }

  67. //------------------------------------------------------------------
  68.     if(u32IntSts & USBD_INTSTS_USB)
  69.     {
  70.         // USB event
  71.         if(u32IntSts & USBD_INTSTS_SETUP)
  72.         {
  73.             // Setup packet
  74.             /* Clear event flag */
  75.             USBD_CLR_INT_FLAG(USBD_INTSTS_SETUP);

  76.             /* Clear the data IN/OUT ready flag of control end-points */
  77.             USBD_STOP_TRANSACTION(EP0);
  78.             USBD_STOP_TRANSACTION(EP1);

  79.             USBD_ProcessSetupPacket();
  80.         }

  81.         // EP events
  82.         if(u32IntSts & USBD_INTSTS_EP0)
  83.         {
  84.             /* Clear event flag */
  85.             USBD_CLR_INT_FLAG(USBD_INTSTS_EP0);
  86.             // control IN
  87.             USBD_CtrlIn();
  88.         }

  89.         if(u32IntSts & USBD_INTSTS_EP1)
  90.         {
  91.             /* Clear event flag */
  92.             USBD_CLR_INT_FLAG(USBD_INTSTS_EP1);

  93.             // control OUT
  94.             USBD_CtrlOut();
  95.         }

  96.         if(u32IntSts & USBD_INTSTS_EP2)
  97.         {
  98.             /* Clear event flag */
  99.             USBD_CLR_INT_FLAG(USBD_INTSTS_EP2);
  100.             // Interrupt IN
  101.             EP2_Handler();
  102.         }

  103.         if(u32IntSts & USBD_INTSTS_EP3)
  104.         {
  105.             /* Clear event flag */
  106.             USBD_CLR_INT_FLAG(USBD_INTSTS_EP3);
  107.         }

  108.         if(u32IntSts & USBD_INTSTS_EP4)
  109.         {
  110.             /* Clear event flag */
  111.             USBD_CLR_INT_FLAG(USBD_INTSTS_EP4);
  112.         }

  113.         if(u32IntSts & USBD_INTSTS_EP5)
  114.         {
  115.             /* Clear event flag */
  116.             USBD_CLR_INT_FLAG(USBD_INTSTS_EP5);
  117.         }

  118.         if(u32IntSts & USBD_INTSTS_EP6)
  119.         {
  120.             /* Clear event flag */
  121.             USBD_CLR_INT_FLAG(USBD_INTSTS_EP6);
  122.         }

  123.         if(u32IntSts & USBD_INTSTS_EP7)
  124.         {
  125.             /* Clear event flag */
  126.             USBD_CLR_INT_FLAG(USBD_INTSTS_EP7);
  127.         }
  128.     }
  129. }

  130. void EP2_Handler(void)  /* Interrupt IN handler */
  131. {
  132.     g_u8EP2Ready = 1;
  133. }


  134. /*--------------------------------------------------------------------------*/
  135. /**
  136.   * @brief  USBD Endpoint Config.
  137.   * @param  None.
  138.   * @retval None.
  139.   */
  140. void HID_Init(void)
  141. {
  142.     /* Init setup packet buffer */
  143.     /* Buffer range for setup packet -> [0 ~ 0x7] */
  144.     USBD->STBUFSEG = SETUP_BUF_BASE;

  145.     /*****************************************************/
  146.     /* EP0 ==> control IN endpoint, address 0 */
  147.     USBD_CONFIG_EP(EP0, USBD_CFG_CSTALL | USBD_CFG_EPMODE_IN | 0);
  148.     /* Buffer range for EP0 */
  149.     USBD_SET_EP_BUF_ADDR(EP0, EP0_BUF_BASE);

  150.     /* EP1 ==> control OUT endpoint, address 0 */
  151.     USBD_CONFIG_EP(EP1, USBD_CFG_CSTALL | USBD_CFG_EPMODE_OUT | 0);
  152.     /* Buffer range for EP1 */
  153.     USBD_SET_EP_BUF_ADDR(EP1, EP1_BUF_BASE);

  154.     /*****************************************************/
  155.     /* EP2 ==> Interrupt IN endpoint, address 1 */
  156.     USBD_CONFIG_EP(EP2, USBD_CFG_EPMODE_IN | INT_IN_EP_NUM);
  157.     /* Buffer range for EP2 */
  158.     USBD_SET_EP_BUF_ADDR(EP2, EP2_BUF_BASE);

  159. }

  160. void HID_ClassRequest(void)
  161. {
  162.     uint8_t buf[8];

  163.     USBD_GetSetupPacket(buf);

  164.     if(buf[0] & 0x80)    /* request data transfer direction */
  165.     {
  166.         // Device to host
  167.         switch(buf[1])
  168.         {
  169.             case GET_REPORT:
  170. //            {
  171. //                break;
  172. //            }
  173.             case GET_IDLE:
  174.             {
  175.                 USBD_SET_PAYLOAD_LEN(EP1, buf[6]);
  176.                 /* Data stage */
  177.                 USBD_PrepareCtrlIn(&g_u8Idle, buf[6]);
  178.                 /* Status stage */
  179.                 USBD_PrepareCtrlOut(0, 0);
  180.                 break;
  181.             }
  182.             case GET_PROTOCOL:
  183.             {
  184.                 USBD_SET_PAYLOAD_LEN(EP1, buf[6]);
  185.                 /* Data stage */
  186.                 USBD_PrepareCtrlIn(&g_u8Protocol, buf[6]);
  187.                 /* Status stage */
  188.                 USBD_PrepareCtrlOut(0, 0);
  189.                 break;
  190.             }
  191.             default:
  192.             {
  193.                 /* Setup error, stall the device */
  194.                 USBD_SetStall(EP0);
  195.                 USBD_SetStall(EP1);
  196.                 break;
  197.             }
  198.         }
  199.     }
  200.     else
  201.     {
  202.         // Host to device
  203.         switch(buf[1])
  204.         {
  205.             case SET_REPORT:
  206.             {
  207.                 if(buf[3] == 2)
  208.                 {
  209.                     /* Request Type = Output */
  210.                     USBD_SET_DATA1(EP1);
  211.                     USBD_SET_PAYLOAD_LEN(EP1, buf[6]);

  212.                     /* Status stage */
  213.                     USBD_PrepareCtrlIn(0, 0);
  214.                 }
  215.                 break;
  216.             }
  217.             case SET_IDLE:
  218.             {
  219.                 g_u8Idle = buf[3];
  220.                 /* Status stage */
  221.                 USBD_SET_DATA1(EP0);
  222.                 USBD_SET_PAYLOAD_LEN(EP0, 0);
  223.                 break;
  224.             }
  225.             case SET_PROTOCOL:
  226.             {
  227.                 g_u8Protocol = buf[2];
  228.                 /* Status stage */
  229.                 USBD_SET_DATA1(EP0);
  230.                 USBD_SET_PAYLOAD_LEN(EP0, 0);
  231.                 break;
  232.             }
  233.             default:
  234.             {
  235.                 // Stall
  236.                 /* Setup error, stall the device */
  237.                 USBD_SetStall(EP0);
  238.                 USBD_SetStall(EP1);
  239.                 break;
  240.             }
  241.         }
  242.     }
  243. }

  244. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/

 楼主| 天灵灵地灵灵 发表于 2020-7-4 23:06 | 显示全部楼层
  1. /******************************************************************************
  2. * @file     hid_kb.c
  3. * @brief    NUC029xGE series USBD HID keyboard sample file
  4. *
  5. * @note
  6. * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  7. ******************************************************************************/

  8. /*!<Includes */
  9. #include <string.h>
  10. #include "NUC029xGE.h"
  11. #include "usbd.h"
  12. #include "hid_kb.h"

  13. uint8_t volatile g_u8Suspend = 0;
  14. uint8_t g_u8Idle = 0, g_u8Protocol = 0;

  15. void USBD_IRQHandler(void)
  16. {
  17.     uint32_t u32IntSts = USBD_GET_INT_FLAG();
  18.     uint32_t u32State = USBD_GET_BUS_STATE();

  19. //------------------------------------------------------------------
  20.     if(u32IntSts & USBD_INTSTS_FLDET)
  21.     {
  22.         // Floating detect
  23.         USBD_CLR_INT_FLAG(USBD_INTSTS_FLDET);

  24.         if(USBD_IS_ATTACHED())
  25.         {
  26.             /* USB Plug In */
  27.             USBD_ENABLE_USB();
  28.         }
  29.         else
  30.         {
  31.             /* USB Un-plug */
  32.             USBD_DISABLE_USB();
  33.         }
  34.     }

  35. //------------------------------------------------------------------
  36.     if(u32IntSts & USBD_INTSTS_WAKEUP)
  37.     {
  38.         /* Clear event flag */
  39.         USBD_CLR_INT_FLAG(USBD_INTSTS_WAKEUP);
  40.     }

  41. //------------------------------------------------------------------
  42.     if(u32IntSts & USBD_INTSTS_BUS)
  43.     {
  44.         /* Clear event flag */
  45.         USBD_CLR_INT_FLAG(USBD_INTSTS_BUS);

  46.         if(u32State & USBD_STATE_USBRST)
  47.         {
  48.             /* Bus reset */
  49.             USBD_ENABLE_USB();
  50.             USBD_SwReset();
  51.             g_u8Suspend = 0;
  52.         }
  53.         if(u32State & USBD_STATE_SUSPEND)
  54.         {
  55.             /* Enter power down to wait USB attached */
  56.             g_u8Suspend = 1;

  57.             /* Enable USB but disable PHY */
  58.             USBD_DISABLE_PHY();
  59.         }
  60.         if(u32State & USBD_STATE_RESUME)
  61.         {
  62.             /* Enable USB and enable PHY */
  63.             USBD_ENABLE_USB();
  64.             g_u8Suspend = 0;
  65.         }
  66.     }

  67. //------------------------------------------------------------------
  68.     if(u32IntSts & USBD_INTSTS_USB)
  69.     {
  70.         // USB event
  71.         if(u32IntSts & USBD_INTSTS_SETUP)
  72.         {
  73.             // Setup packet
  74.             /* Clear event flag */
  75.             USBD_CLR_INT_FLAG(USBD_INTSTS_SETUP);

  76.             /* Clear the data IN/OUT ready flag of control end-points */
  77.             USBD_STOP_TRANSACTION(EP0);
  78.             USBD_STOP_TRANSACTION(EP1);

  79.             USBD_ProcessSetupPacket();
  80.         }

  81.         // EP events
  82.         if(u32IntSts & USBD_INTSTS_EP0)
  83.         {
  84.             /* Clear event flag */
  85.             USBD_CLR_INT_FLAG(USBD_INTSTS_EP0);
  86.             // control IN
  87.             USBD_CtrlIn();
  88.         }

  89.         if(u32IntSts & USBD_INTSTS_EP1)
  90.         {
  91.             /* Clear event flag */
  92.             USBD_CLR_INT_FLAG(USBD_INTSTS_EP1);

  93.             // control OUT
  94.             USBD_CtrlOut();
  95.         }

  96.         if(u32IntSts & USBD_INTSTS_EP2)
  97.         {
  98.             /* Clear event flag */
  99.             USBD_CLR_INT_FLAG(USBD_INTSTS_EP2);
  100.             // Interrupt IN
  101.             EP2_Handler();
  102.         }

  103.         if(u32IntSts & USBD_INTSTS_EP3)
  104.         {
  105.             /* Clear event flag */
  106.             USBD_CLR_INT_FLAG(USBD_INTSTS_EP3);
  107.         }

  108.         if(u32IntSts & USBD_INTSTS_EP4)
  109.         {
  110.             /* Clear event flag */
  111.             USBD_CLR_INT_FLAG(USBD_INTSTS_EP4);
  112.         }

  113.         if(u32IntSts & USBD_INTSTS_EP5)
  114.         {
  115.             /* Clear event flag */
  116.             USBD_CLR_INT_FLAG(USBD_INTSTS_EP5);
  117.         }

  118.         if(u32IntSts & USBD_INTSTS_EP6)
  119.         {
  120.             /* Clear event flag */
  121.             USBD_CLR_INT_FLAG(USBD_INTSTS_EP6);
  122.         }

  123.         if(u32IntSts & USBD_INTSTS_EP7)
  124.         {
  125.             /* Clear event flag */
  126.             USBD_CLR_INT_FLAG(USBD_INTSTS_EP7);
  127.         }
  128.     }
  129. }

  130. void EP2_Handler(void)  /* Interrupt IN handler */
  131. {
  132.     g_u8EP2Ready = 1;
  133. }


  134. /*--------------------------------------------------------------------------*/
  135. /**
  136.   * @brief  USBD Endpoint Config.
  137.   * @param  None.
  138.   * @retval None.
  139.   */
  140. void HID_Init(void)
  141. {
  142.     /* Init setup packet buffer */
  143.     /* Buffer range for setup packet -> [0 ~ 0x7] */
  144.     USBD->STBUFSEG = SETUP_BUF_BASE;

  145.     /*****************************************************/
  146.     /* EP0 ==> control IN endpoint, address 0 */
  147.     USBD_CONFIG_EP(EP0, USBD_CFG_CSTALL | USBD_CFG_EPMODE_IN | 0);
  148.     /* Buffer range for EP0 */
  149.     USBD_SET_EP_BUF_ADDR(EP0, EP0_BUF_BASE);

  150.     /* EP1 ==> control OUT endpoint, address 0 */
  151.     USBD_CONFIG_EP(EP1, USBD_CFG_CSTALL | USBD_CFG_EPMODE_OUT | 0);
  152.     /* Buffer range for EP1 */
  153.     USBD_SET_EP_BUF_ADDR(EP1, EP1_BUF_BASE);

  154.     /*****************************************************/
  155.     /* EP2 ==> Interrupt IN endpoint, address 1 */
  156.     USBD_CONFIG_EP(EP2, USBD_CFG_EPMODE_IN | INT_IN_EP_NUM);
  157.     /* Buffer range for EP2 */
  158.     USBD_SET_EP_BUF_ADDR(EP2, EP2_BUF_BASE);

  159. }

  160. void HID_ClassRequest(void)
  161. {
  162.     uint8_t buf[8];

  163.     USBD_GetSetupPacket(buf);

  164.     if(buf[0] & 0x80)    /* request data transfer direction */
  165.     {
  166.         // Device to host
  167.         switch(buf[1])
  168.         {
  169.             case GET_REPORT:
  170. //            {
  171. //                break;
  172. //            }
  173.             case GET_IDLE:
  174.             {
  175.                 USBD_SET_PAYLOAD_LEN(EP1, buf[6]);
  176.                 /* Data stage */
  177.                 USBD_PrepareCtrlIn(&g_u8Idle, buf[6]);
  178.                 /* Status stage */
  179.                 USBD_PrepareCtrlOut(0, 0);
  180.                 break;
  181.             }
  182.             case GET_PROTOCOL:
  183.             {
  184.                 USBD_SET_PAYLOAD_LEN(EP1, buf[6]);
  185.                 /* Data stage */
  186.                 USBD_PrepareCtrlIn(&g_u8Protocol, buf[6]);
  187.                 /* Status stage */
  188.                 USBD_PrepareCtrlOut(0, 0);
  189.                 break;
  190.             }
  191.             default:
  192.             {
  193.                 /* Setup error, stall the device */
  194.                 USBD_SetStall(EP0);
  195.                 USBD_SetStall(EP1);
  196.                 break;
  197.             }
  198.         }
  199.     }
  200.     else
  201.     {
  202.         // Host to device
  203.         switch(buf[1])
  204.         {
  205.             case SET_REPORT:
  206.             {
  207.                 if(buf[3] == 2)
  208.                 {
  209.                     /* Request Type = Output */
  210.                     USBD_SET_DATA1(EP1);
  211.                     USBD_SET_PAYLOAD_LEN(EP1, buf[6]);

  212.                     /* Status stage */
  213.                     USBD_PrepareCtrlIn(0, 0);
  214.                 }
  215.                 break;
  216.             }
  217.             case SET_IDLE:
  218.             {
  219.                 g_u8Idle = buf[3];
  220.                 /* Status stage */
  221.                 USBD_SET_DATA1(EP0);
  222.                 USBD_SET_PAYLOAD_LEN(EP0, 0);
  223.                 break;
  224.             }
  225.             case SET_PROTOCOL:
  226.             {
  227.                 g_u8Protocol = buf[2];
  228.                 /* Status stage */
  229.                 USBD_SET_DATA1(EP0);
  230.                 USBD_SET_PAYLOAD_LEN(EP0, 0);
  231.                 break;
  232.             }
  233.             default:
  234.             {
  235.                 // Stall
  236.                 /* Setup error, stall the device */
  237.                 USBD_SetStall(EP0);
  238.                 USBD_SetStall(EP1);
  239.                 break;
  240.             }
  241.         }
  242.     }
  243. }

  244. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/

 楼主| 天灵灵地灵灵 发表于 2020-7-4 23:06 | 显示全部楼层
  1. /******************************************************************************
  2. * @file     descriptors.c
  3. * @brief    NUC029xGE series USBD descriptor
  4. *
  5. * @note
  6. * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  7. ******************************************************************************/
  8. #ifndef __DESCRIPTORS_C__
  9. #define __DESCRIPTORS_C__

  10. /*!<Includes */
  11. #include "NUC029xGE.h"
  12. #include "usbd.h"
  13. #include "hid_kb.h"

  14. /*!<USB HID Report Descriptor */
  15. const uint8_t HID_KeyboardReportDescriptor[] =
  16. {
  17.     0x05, 0x01,     /* Usage Page(Generic Desktop Controls) */
  18.     0x09, 0x06,     /* Usage(Keyboard) */
  19.     0xA1, 0x01,     /* Collection(Application) */
  20.     0x05, 0x07,         /* Usage Page(Keyboard/Keypad) */
  21.     0x19, 0xE0,         /* Usage Minimum(0xE0) */
  22.     0x29, 0xE7,         /* Usage Maximum(0xE7) */
  23.     0x15, 0x00,         /* Logical Minimum(0x0) */
  24.     0x25, 0x01,         /* Logical Maximum(0x1) */
  25.     0x75, 0x01,         /* Report Size(0x1) */
  26.     0x95, 0x08,         /* Report Count(0x8) */
  27.     0x81, 0x02,         /* Input (Data) => Modifier byte */
  28.     0x95, 0x01,         /* Report Count(0x1) */
  29.     0x75, 0x08,         /* Report Size(0x8) */
  30.     0x81, 0x01,         /* Input (Constant) => Reserved byte */
  31.     0x95, 0x05,         /* Report Count(0x5) */
  32.     0x75, 0x01,         /* Report Size(0x1) */
  33.     0x05, 0x08,         /* Usage Page(LEDs) */
  34.     0x19, 0x01,         /* Usage Minimum(0x1) */
  35.     0x29, 0x05,         /* Usage Maximum(0x5) */
  36.     0x91, 0x02,         /* Output (Data) => LED report */
  37.     0x95, 0x01,         /* Report Count(0x1) */
  38.     0x75, 0x03,         /* Report Size(0x3) */
  39.     0x91, 0x01,         /* Output (Constant) => LED report padding */
  40.     0x95, 0x06,         /* Report Count(0x6) */
  41.     0x75, 0x08,         /* Report Size(0x8) */
  42.     0x15, 0x00,         /* Logical Minimum(0x0) */
  43.     0x25, 0x65,         /* Logical Maximum(0x65) */
  44.     0x05, 0x07,         /* Usage Page(Keyboard/Keypad) */
  45.     0x19, 0x00,         /* Usage Minimum(0x0) */
  46.     0x29, 0x65,         /* Usage Maximum(0x65) */
  47.     0x81, 0x00,         /* Input (Data) */
  48.     0xC0,           /* End Collection */
  49. };




  50. /*----------------------------------------------------------------------------*/
  51. /*!<USB Device Descriptor */
  52. const uint8_t gu8DeviceDescriptor[] =
  53. {
  54.     LEN_DEVICE,     /* bLength */
  55.     DESC_DEVICE,    /* bDescriptorType */
  56. #ifdef SUPPORT_LPM
  57.     0x01, 0x02,     /* bcdUSB >= 0x0201 to support LPM */
  58. #else
  59.     0x10, 0x01,     /* bcdUSB */
  60. #endif
  61.     0x00,           /* bDeviceClass */
  62.     0x00,           /* bDeviceSubClass */
  63.     0x00,           /* bDeviceProtocol */
  64.     EP0_MAX_PKT_SIZE,   /* bMaxPacketSize0 */
  65.     /* idVendor */
  66.     USBD_VID & 0x00FF,
  67.     (USBD_VID & 0xFF00) >> 8,
  68.     /* idProduct */
  69.     USBD_PID & 0x00FF,
  70.     (USBD_PID & 0xFF00) >> 8,
  71.     0x00, 0x00,     /* bcdDevice */
  72.     0x01,           /* iManufacture */
  73.     0x02,           /* iProduct */
  74.     0x03,           /* iSerialNumber - no serial */
  75.     0x01            /* bNumConfigurations */
  76. };

  77. /*!<USB Configure Descriptor */
  78. const uint8_t gu8ConfigDescriptor[] =
  79. {
  80.     LEN_CONFIG,     /* bLength */
  81.     DESC_CONFIG,    /* bDescriptorType */
  82.     /* wTotalLength */
  83.     LEN_CONFIG_AND_SUBORDINATE & 0x00FF,
  84.     (LEN_CONFIG_AND_SUBORDINATE & 0xFF00) >> 8,
  85.     0x01,           /* bNumInterfaces */
  86.     0x01,           /* bConfigurationValue */
  87.     0x00,           /* iConfiguration */
  88.     0x80 | (USBD_SELF_POWERED << 6) | (USBD_REMOTE_WAKEUP << 5),/* bmAttributes */
  89.     USBD_MAX_POWER,         /* MaxPower */

  90.     /* I/F descr: HID */
  91.     LEN_INTERFACE,  /* bLength */
  92.     DESC_INTERFACE, /* bDescriptorType */
  93.     0x00,           /* bInterfaceNumber */
  94.     0x00,           /* bAlternateSetting */
  95.     0x01,           /* bNumEndpoints */
  96.     0x03,           /* bInterfaceClass */
  97.     0x01,           /* bInterfaceSubClass */
  98.     HID_KEYBOARD,   /* bInterfaceProtocol */
  99.     0x00,           /* iInterface */

  100.     /* HID Descriptor */
  101.     LEN_HID,        /* Size of this descriptor in UINT8s. */
  102.     DESC_HID,       /* HID descriptor type. */
  103.     0x10, 0x01,     /* HID Class Spec. release number. */
  104.     0x00,           /* H/W target country. */
  105.     0x01,           /* Number of HID class descriptors to follow. */
  106.     DESC_HID_RPT,   /* Descriptor type. */
  107.     /* Total length of report descriptor. */
  108.     sizeof(HID_KeyboardReportDescriptor) & 0x00FF,
  109.     (sizeof(HID_KeyboardReportDescriptor) & 0xFF00) >> 8,

  110.     /* EP Descriptor: interrupt in. */
  111.     LEN_ENDPOINT,   /* bLength */
  112.     DESC_ENDPOINT,  /* bDescriptorType */
  113.     (INT_IN_EP_NUM | EP_INPUT), /* bEndpointAddress */
  114.     EP_INT,         /* bmAttributes */
  115.     /* wMaxPacketSize */
  116.     EP2_MAX_PKT_SIZE & 0x00FF,
  117.     (EP2_MAX_PKT_SIZE & 0xFF00) >> 8,
  118.     HID_DEFAULT_INT_IN_INTERVAL     /* bInterval */
  119. };

  120. /*!<USB Language String Descriptor */
  121. const uint8_t gu8StringLang[4] =
  122. {
  123.     4,              /* bLength */
  124.     DESC_STRING,    /* bDescriptorType */
  125.     0x09, 0x04
  126. };

  127. /*!<USB Vendor String Descriptor */
  128. const uint8_t gu8VendorStringDesc[] =
  129. {
  130.     16,
  131.     DESC_STRING,
  132.     'N', 0, 'u', 0, 'v', 0, 'o', 0, 't', 0, 'o', 0, 'n', 0
  133. };

  134. /*!<USB Product String Descriptor */
  135. const uint8_t gu8ProductStringDesc[] =
  136. {
  137.     26,
  138.     DESC_STRING,
  139.     'H', 0, 'I', 0, 'D', 0, ' ', 0, 'K', 0, 'e', 0, 'y', 0, 'b', 0, 'o', 0, 'a', 0, 'r', 0, 'd', 0
  140. };


  141. const uint8_t gu8StringSerial[26] =
  142. {
  143.     26,             // bLength
  144.     DESC_STRING,    // bDescriptorType
  145.     'A', 0, '0', 0, '2', 0, '0', 0, '1', 0, '4', 0, '0', 0, '9', 0, '0', 0, '3', 0, '0', 0, '2', 0
  146. };

  147. const uint8_t *gpu8UsbString[4] =
  148. {
  149.     gu8StringLang,
  150.     gu8VendorStringDesc,
  151.     gu8ProductStringDesc,
  152.     gu8StringSerial
  153. };

  154. const uint8_t *gpu8UsbHidReport[3] =
  155. {
  156.     HID_KeyboardReportDescriptor,
  157.     NULL,
  158.     NULL
  159. };

  160. const uint32_t gu32UsbHidReportLen[3] =
  161. {
  162.     sizeof(HID_KeyboardReportDescriptor),
  163.     0,
  164.     0
  165. };

  166. const uint32_t gu32ConfigHidDescIdx[3] =
  167. {
  168.     (LEN_CONFIG + LEN_INTERFACE),
  169.     0,
  170.     0
  171. };

  172. #ifdef SUPPORT_LPM
  173. const uint8_t gu8BosDescriptor[] =
  174. {
  175.     LEN_BOS,        /* bLength */
  176.     DESC_BOS,       /* bDescriptorType */
  177.     /* wTotalLength */
  178.     0x0C & 0x00FF,
  179.     (0x0C & 0xFF00) >> 8,
  180.     0x01,           /* bNumDeviceCaps */

  181.     /* Device Capability */
  182.     LEN_DEVCAP,     /* bLength */
  183.     DESC_DEVCAP,/* bDescriptorType */
  184.     0x02,  /* bDevCapabilityType, 0x02 is USB 2.0 Extension */
  185.     0x06, 0x04, 0x00, 0x00  /* bmAttributes, 32 bits */
  186.                             /* bit 0 : Reserved. Must 0. */
  187.                             /* bit 1 : 1 to support LPM. */
  188.                             /* bit 2 : 1 to support BSL & Alternat HIRD. */
  189.                             /* bit 3 : 1 to recommend Baseline BESL. */
  190.                             /* bit 4 : 1 to recommand Deep BESL. */
  191.                             /* bit 11:8 : Recommend Baseline BESL value. Ignore by bit3 is zero. */
  192.                             /* bit 15:12 : Recommend Deep BESL value. Ignore by bit4 is zero. */
  193.                             /* bit 31:16 : Reserved. Must 0. */
  194. };
  195. #endif

  196. const S_USBD_INFO_T gsInfo =
  197. {
  198.     gu8DeviceDescriptor,
  199.     gu8ConfigDescriptor,
  200.     gpu8UsbString,
  201.     gpu8UsbHidReport,
  202.     gu32UsbHidReportLen,
  203.     gu32ConfigHidDescIdx,
  204. #ifdef SUPPORT_LPM
  205.     gu8BosDescriptor
  206. #else
  207.     NULL
  208. #endif
  209. };


  210. #endif  /* __DESCRIPTORS_C__ */

  211. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
aoyi 发表于 2020-8-3 17:40 | 显示全部楼层
非常感谢楼主分享
drer 发表于 2020-8-3 17:41 | 显示全部楼层
真的是最新的
gwsan 发表于 2020-8-3 17:41 | 显示全部楼层
感谢楼主热心发代码
kxsi 发表于 2020-8-3 17:41 | 显示全部楼层
支持楼主 干货啊
nawu 发表于 2020-8-3 17:41 | 显示全部楼层
路过 学习了
yiy 发表于 2020-8-3 21:12 | 显示全部楼层
还带USB接口,真是帅呆了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

183

主题

3475

帖子

13

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