123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- /**
- **************************************************************************
- * @file mouse_class.c
- * @version v2.0.6
- * @date 2021-12-31
- * @brief usb hid mouse class type
- **************************************************************************
- * Copyright notice & Disclaimer
- *
- * The software Board Support Package (BSP) that is made available to
- * download from Artery official website is the copyrighted work of Artery.
- * Artery authorizes customers to use, copy, and distribute the BSP
- * software and its related documentation for the purpose of design and
- * development in conjunction with Artery microcontrollers. Use of the
- * software is governed by this copyright notice and the following disclaimer.
- *
- * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
- * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
- * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
- * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
- * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
- *
- **************************************************************************
- */
- #include "usbd_core.h"
- #include "mouse_class.h"
- #include "mouse_desc.h"
- /** @addtogroup AT32F403A_407_middlewares_usbd_class
- * @{
- */
-
- /** @defgroup USB_mouse_class
- * @brief usb device mouse demo
- * @{
- */
- /** @defgroup USB_mouse_class_private_functions
- * @{
- */
- usb_sts_type class_init_handler(void *udev);
- usb_sts_type class_clear_handler(void *udev);
- usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup);
- usb_sts_type class_ept0_tx_handler(void *udev);
- usb_sts_type class_ept0_rx_handler(void *udev);
- usb_sts_type class_in_handler(void *udev, uint8_t ept_num);
- usb_sts_type class_out_handler(void *udev, uint8_t ept_num);
- usb_sts_type class_sof_handler(void *udev);
- usb_sts_type class_event_handler(void *udev, usbd_event_type event);
- /* hid static variable */
- static uint32_t hid_protocol = 0;
- static uint32_t hid_set_idle = 0;
- static uint32_t alt_setting = 0;
- static uint8_t hid_state;
- __IO uint8_t hid_suspend_flag = 0;
- uint8_t hid_set_report[64];
- /* usb device class handler */
- usbd_class_handler mouse_class_handler =
- {
- class_init_handler,
- class_clear_handler,
- class_setup_handler,
- class_ept0_tx_handler,
- class_ept0_rx_handler,
- class_in_handler,
- class_out_handler,
- class_sof_handler,
- class_event_handler,
- };
- /**
- * @brief initialize usb endpoint
- * @param udev: to the structure of usbd_core_type
- * @retval status of usb_sts_type
- */
- usb_sts_type class_init_handler(void *udev)
- {
- usb_sts_type status = USB_OK;
- usbd_core_type *pudev = (usbd_core_type *)udev;
-
- #ifndef USB_EPT_AUTO_MALLOC_BUFFER
- /* use user define buffer address */
- usbd_ept_buf_custom_define(pudev, USBD_HID_IN_EPT, EPT1_TX_ADDR);
- usbd_ept_buf_custom_define(pudev, USBD_HID_OUT_EPT, EPT1_RX_ADDR);
- #endif
-
- /* open hid in endpoint */
- usbd_ept_open(pudev, USBD_HID_IN_EPT, EPT_INT_TYPE, USBD_IN_MAXPACKET_SIZE);
-
- return status;
- }
- /**
- * @brief clear endpoint or other state
- * @param udev: to the structure of usbd_core_type
- * @retval status of usb_sts_type
- */
- usb_sts_type class_clear_handler(void *udev)
- {
- usb_sts_type status = USB_OK;
- usbd_core_type *pudev = (usbd_core_type *)udev;
-
- /* close hid in endpoint */
- usbd_ept_close(pudev, USBD_HID_IN_EPT);
-
- return status;
- }
- /**
- * @brief usb device class setup request handler
- * @param udev: to the structure of usbd_core_type
- * @param setup: setup packet
- * @retval status of usb_sts_type
- */
- usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup)
- {
- usb_sts_type status = USB_OK;
- usbd_core_type *pudev = (usbd_core_type *)udev;
- uint16_t len;
- uint8_t *buf;
- switch(setup->bmRequestType & USB_REQ_TYPE_RESERVED)
- {
- /* class request */
- case USB_REQ_TYPE_CLASS:
- switch(setup->bRequest)
- {
- case HID_REQ_SET_PROTOCOL:
- hid_protocol = (uint8_t)setup->wValue;
- break;
- case HID_REQ_GET_PROTOCOL:
- usbd_ctrl_send(pudev, (uint8_t *)&hid_protocol, 1);
- break;
- case HID_REQ_SET_IDLE:
- hid_set_idle = (uint8_t)(setup->wValue >> 8);
- break;
- case HID_REQ_GET_IDLE:
- usbd_ctrl_send(pudev, (uint8_t *)&hid_set_idle, 1);
- break;
- case HID_REQ_SET_REPORT:
- hid_state = HID_REQ_SET_REPORT;
- usbd_ctrl_recv(pudev, hid_set_report, setup->wLength);
- break;
- default:
- usbd_ctrl_unsupport(pudev);
- break;
- }
- break;
- /* standard request */
- case USB_REQ_TYPE_STANDARD:
- switch(setup->bRequest)
- {
- case USB_STD_REQ_GET_DESCRIPTOR:
- if(setup->wValue >> 8 == HID_REPORT_DESC)
- {
- len = MIN(USBD_HID_SIZ_REPORT_DESC, setup->wLength);
- buf = (uint8_t *)g_usbd_hid_report;
- }
- else if(setup->wValue >> 8 == HID_DESCRIPTOR_TYPE)
- {
- len = MIN(9, setup->wLength);
- buf = (uint8_t *)g_hid_usb_desc;
- }
- usbd_ctrl_send(pudev, (uint8_t *)buf, len);
- break;
- case USB_STD_REQ_GET_INTERFACE:
- usbd_ctrl_send(pudev, (uint8_t *)&alt_setting, 1);
- break;
- case USB_STD_REQ_SET_INTERFACE:
- alt_setting = setup->wValue;
- break;
- default:
- usbd_ctrl_unsupport(pudev);
- break;
- }
- break;
- default:
- usbd_ctrl_unsupport(pudev);
- break;
- }
- return status;
- }
- /**
- * @brief usb device class endpoint 0 in status stage complete
- * @param udev: to the structure of usbd_core_type
- * @retval status of usb_sts_type
- */
- usb_sts_type class_ept0_tx_handler(void *udev)
- {
- usb_sts_type status = USB_OK;
-
- /* ...user code... */
-
- return status;
- }
- /**
- * @brief usb device class endpoint 0 out status stage complete
- * @param udev: to the structure of usbd_core_type
- * @retval status of usb_sts_type
- */
- usb_sts_type class_ept0_rx_handler(void *udev)
- {
- usb_sts_type status = USB_OK;
- usbd_core_type *pudev = (usbd_core_type *)udev;
- uint32_t recv_len = usbd_get_recv_len(pudev, 0);
- /* ...user code... */
- if( hid_state == HID_REQ_SET_REPORT)
- {
- /* hid buffer process */
- hid_state = 0;
- }
-
- return status;
- }
- /**
- * @brief usb device class transmision complete handler
- * @param udev: to the structure of usbd_core_type
- * @param ept_num: endpoint number
- * @retval status of usb_sts_type
- */
- usb_sts_type class_in_handler(void *udev, uint8_t ept_num)
- {
- usb_sts_type status = USB_OK;
-
- /* ...user code...
- trans next packet data
- */
-
- return status;
- }
- /**
- * @brief usb device class endpoint receive data
- * @param udev: to the structure of usbd_core_type
- * @param ept_num: endpoint number
- * @retval status of usb_sts_type
- */
- usb_sts_type class_out_handler(void *udev, uint8_t ept_num)
- {
- usb_sts_type status = USB_OK;
-
- return status;
- }
- /**
- * @brief usb device class sof handler
- * @param udev: to the structure of usbd_core_type
- * @retval status of usb_sts_type
- */
- usb_sts_type class_sof_handler(void *udev)
- {
- usb_sts_type status = USB_OK;
-
- /* ...user code... */
-
- return status;
- }
- /**
- * @brief usb device class event handler
- * @param udev: to the structure of usbd_core_type
- * @param event: usb device event
- * @retval status of usb_sts_type
- */
- usb_sts_type class_event_handler(void *udev, usbd_event_type event)
- {
- usb_sts_type status = USB_OK;
- switch(event)
- {
- case USBD_RESET_EVENT:
-
- /* ...user code... */
-
- break;
- case USBD_SUSPEND_EVENT:
- hid_suspend_flag = 1;
- /* ...user code... */
-
- break;
- case USBD_WAKEUP_EVENT:
- /* ...user code... */
-
- break;
- default:
- break;
- }
- return status;
- }
- /**
- * @brief usb device class send report
- * @param udev: to the structure of usbd_core_type
- * @param report: report buffer
- * @param len: report length
- * @retval status of usb_sts_type
- */
- usb_sts_type class_send_report(void *udev, uint8_t *report, uint16_t len)
- {
- usb_sts_type status = USB_OK;
- usbd_core_type *pudev = (usbd_core_type *)udev;
- if(usbd_connect_state_get(pudev) == USB_CONN_STATE_CONFIGURED)
- usbd_ept_send(pudev, USBD_HID_IN_EPT, report, len);
-
- return status;
- }
- /**
- * @brief usb device class report function
- * @param udev: to the structure of usbd_core_type
- * @param op: operation
- * @retval none
- */
- void usb_hid_mouse_send(void *udev, uint8_t op)
- {
- static uint8_t mouse_buffer[4] = {0, 0, 0, 0};
- int8_t posx = 0, posy = 0, button = 0;
- switch(op)
- {
- case LEFT_BUTTON:
- button = 0x01;
- break;
-
- case RIGHT_BUTTON:
- button = 0x2;
- break;
-
- case UP_MOVE:
- posy -= MOVE_STEP;
- break;
-
- case DOWN_MOVE:
- posy += MOVE_STEP;
- break;
-
- case LEFT_MOVE:
- posx -= MOVE_STEP;
- break;
-
- case RIGHT_MOVE:
- posx += MOVE_STEP;
- break;
-
- default:
- break;
- }
- mouse_buffer[0] = button;
- mouse_buffer[1] = posx;
- mouse_buffer[2] = posy;
-
- class_send_report(udev, mouse_buffer, 4);
- }
- /**
- * @}
- */
- /**
- * @}
- */
- /**
- * @}
- */
|