[Atmel] 用SAM-BA或JLINK跑ATSAM4E16的程序(21) USB HID 鼠标

[复制链接]
 楼主| ddllxxrr 发表于 2015-12-1 21:02 | 显示全部楼层 |阅读模式
这个程序是用开发板按键来仿真鼠标。

至于USB的全过程请看下参考资料。

USB HID 应用笔记 ,讲得十分简练。

Windows自动识别并自动安装驱动。这一点相当于免驱:





而这里用HID ASF库包括以下几个函数:


bool udi_hid_mouse_btnleft(bool b_state)


bool udi_hid_mouse_btnmiddle(bool b_state)

bool udi_hid_mouse_btnright(bool b_state)

bool udi_hid_mouse_moveScroll(int8_t pos)

bool udi_hid_mouse_moveX(int8_t pos_x)

bool udi_hid_mouse_moveY(int8_t pos_y)


这些都在ui.c中用上了:


  1. #include <asf.h>
  2. #include "ui.h"

  3. #define  MOUSE_MOVE_RANGE  3

  4. /* Wakeup pin is push button 2 (BP3, fast wakeup 10) */
  5. #define  WAKEUP_PMC_FSTT (PUSHBUTTON_2_WKUP_FSTT)
  6. #define  WAKEUP_PIN      (GPIO_PUSH_BUTTON_2)
  7. #define  WAKEUP_PIO      (PIN_PUSHBUTTON_2_PIO)
  8. #define  WAKEUP_PIO_ID   (PIN_PUSHBUTTON_2_ID)
  9. #define  WAKEUP_PIO_MASK (PIN_PUSHBUTTON_2_MASK)
  10. #define  WAKEUP_PIO_ATTR \
  11.         (PIO_INPUT | PIO_PULLUP | PIO_DEBOUNCE | PIO_IT_LOW_LEVEL)

  12. /* Interrupt on "pin change" from push button to do wakeup on USB
  13. * Note:
  14. * This interrupt is enable when the USB host enable remotewakeup feature
  15. * This interrupt wakeup the CPU if this one is in idle mode
  16. */
  17. static void ui_wakeup_handler(uint32_t id, uint32_t mask)
  18. {
  19.         if (WAKEUP_PIO_ID == id && WAKEUP_PIO_MASK == mask) {
  20.                 /* It is a wakeup then send wakeup USB */
  21.                 udc_remotewakeup();
  22.                 LED_On(LED0);
  23.         }
  24. }

  25. void ui_init(void)
  26. {
  27.         /* Set handler for push button */
  28.         pio_handler_set(WAKEUP_PIO, WAKEUP_PIO_ID, WAKEUP_PIO_MASK,
  29.                 WAKEUP_PIO_ATTR, ui_wakeup_handler);
  30.         /* Enable IRQ for button (PIOA) */
  31.         NVIC_EnableIRQ((IRQn_Type) WAKEUP_PIO_ID);
  32.         /* Initialize LEDs */
  33.         LED_Off(LED0);
  34.         LED_Off(LED1);
  35. }

  36. void ui_powerdown(void)
  37. {
  38.         LED_Off(LED0);
  39.         LED_Off(LED1);
  40. }


  41. void ui_wakeup_enable(void)
  42. {
  43.         /* Configure BP3 as PIO input */
  44.         pio_configure_pin(WAKEUP_PIN, WAKEUP_PIO_ATTR);
  45.         /* Enable interrupt for BP3 */
  46.         pio_enable_pin_interrupt(WAKEUP_PIN);
  47.         /* Enable fast wakeup for button pin (WKUP10 for PA20) */
  48.         pmc_set_fast_startup_input(WAKEUP_PMC_FSTT);
  49. }

  50. void ui_wakeup_disable(void)
  51. {
  52.         /* Disable interrupt for button pin */
  53.         pio_disable_pin_interrupt(WAKEUP_PIN);
  54.         /* Disable fast wakeup for button pin (WKUP10 for BP3) */
  55.         pmc_clr_fast_startup_input(WAKEUP_PMC_FSTT);
  56. }

  57. void ui_wakeup(void)
  58. {
  59.         LED_On(LED0);
  60. }

  61. void ui_process(uint16_t framenumber)
  62. {
  63.         static uint8_t cpt_sof = 0;
  64.         static bool btn_left = false, btn_right = false;
  65.         bool btn_pressed;

  66.         if ((framenumber % 1000) == 0) {
  67.                 LED_On(LED1);
  68.         }
  69.         if ((framenumber % 1000) == 500) {
  70.                 LED_Off(LED1);
  71.         }
  72.         /* Scan process running each 2ms */
  73.         cpt_sof++;
  74.         if (cpt_sof < 2) {
  75.                 return;
  76.         }
  77.         cpt_sof = 0;

  78.         /* Uses buttons to move mouse */
  79.         if (!ioport_get_pin_level(GPIO_PUSH_BUTTON_3)) {
  80.                 udi_hid_mouse_moveY(-MOUSE_MOVE_RANGE);
  81.         }
  82.         if (!ioport_get_pin_level(GPIO_PUSH_BUTTON_4)) {
  83.                 udi_hid_mouse_moveY( MOUSE_MOVE_RANGE);
  84.         }
  85.         /* Check button click */
  86.         btn_pressed = !ioport_get_pin_level(GPIO_PUSH_BUTTON_2);
  87.         if (btn_pressed != btn_left) {
  88.                 btn_left = btn_pressed;
  89.                 udi_hid_mouse_btnleft(btn_left);
  90.         }
  91.         btn_pressed = !ioport_get_pin_level(GPIO_PUSH_BUTTON_1);
  92.         if (btn_pressed != btn_right) {
  93.                 btn_right = btn_pressed;
  94.                 udi_hid_mouse_btnright(btn_right);
  95.         }
  96. }

  97. /**
  98. * \defgroup UI User Interface
  99. *
  100. * Human interface on SAM4E-EK:
  101. * - Led 0 (D2) is on when USB is wakeup
  102. * - Led 1 (D3) blinks when USB host has checked and enabled HID Mouse interface
  103. * - Push button 2 (BP3) and push button 1 (BP2) are linked to mouse button
  104. *   left and right
  105. * - Push button 3 (BP4) and push button 4 (BP5) are used to move mouse up
  106. *   and down
  107. * - Only a low level on push button 2 (BP3) will generate a wakeup to USB Host
  108. *   in remote wakeup mode.
  109. *
  110. */



运行结果是:我的板子就四个键,其中一个键管上,按住后光标上移,其中一下向下。一个是左键,一个是右键,都很好用。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2403

主题

6994

帖子

68

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