Arrow / Mbed OS DAPLink Reset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers usbd_user_hid.c Source File

usbd_user_hid.c

Go to the documentation of this file.
00001 /**
00002  * @file    usbd_user_hid.c
00003  * @brief   HID driver for CMSIS-DAP packet processing
00004  *
00005  * DAPLink Interface Firmware
00006  * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
00007  * SPDX-License-Identifier: Apache-2.0
00008  *
00009  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00010  * not use this file except in compliance with the License.
00011  * You may obtain a copy of the License at
00012  *
00013  * http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  * Unless required by applicable law or agreed to in writing, software
00016  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00017  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018  * See the License for the specific language governing permissions and
00019  * limitations under the License.
00020  */
00021 
00022 #include <string.h>
00023 #include "rl_usb.h"
00024 #include "usb.h"
00025 #define __NO_USB_LIB_C
00026 #include "usb_config.c"
00027 #include "DAP_config.h"
00028 #include "DAP.h"
00029 #include "util.h"
00030 #include "DAP_queue.h"
00031 #include "main.h"
00032 
00033 
00034 #if (USBD_HID_OUTREPORT_MAX_SZ > DAP_PACKET_SIZE)
00035 #error "USB HID Output Report Size must be less than DAP Packet Size"
00036 #endif
00037 #if (USBD_HID_INREPORT_MAX_SZ > DAP_PACKET_SIZE)
00038 #error "USB HID Input Report Size must be less than DAP Packet Size"
00039 #endif
00040 
00041 static volatile uint8_t  USB_ResponseIdle;
00042 static DAP_queue DAP_Cmd_queue;
00043 
00044 void hid_send_packet()
00045 {
00046     uint8_t * sbuf;
00047     int slen;
00048     if (DAP_queue_get_send_buf(&DAP_Cmd_queue, &sbuf, &slen)) {
00049         if (slen > USBD_HID_OUTREPORT_MAX_SZ){
00050             util_assert(0);
00051         }else {
00052             usbd_hid_get_report_trigger(0, sbuf, USBD_HID_OUTREPORT_MAX_SZ);
00053         }
00054     }
00055 }
00056 
00057 // USB HID Callback: when system initializes
00058 void usbd_hid_init(void)
00059 {
00060     USB_ResponseIdle = 1;
00061     DAP_queue_init(&DAP_Cmd_queue);
00062 }
00063 
00064 // USB HID Callback: when data needs to be prepared for the host
00065 int usbd_hid_get_report(U8 rtype, U8 rid, U8 *buf, U8 req)
00066 {
00067     uint8_t * sbuf;
00068     int slen;
00069     switch (rtype) {
00070         case HID_REPORT_INPUT:
00071             switch (req) {
00072                 case USBD_HID_REQ_PERIOD_UPDATE:
00073                     break;
00074 
00075                 case USBD_HID_REQ_EP_CTRL:
00076                 case USBD_HID_REQ_EP_INT:
00077                     if (DAP_queue_get_send_buf(&DAP_Cmd_queue, &sbuf, &slen)) {
00078                         if (slen > USBD_HID_OUTREPORT_MAX_SZ){
00079                             util_assert(0);
00080                         }else {
00081                             memcpy(buf, sbuf, slen);
00082                             return (USBD_HID_OUTREPORT_MAX_SZ);
00083                         }
00084                     } else if (req == USBD_HID_REQ_EP_INT) {
00085                         USB_ResponseIdle = 1;
00086                     }
00087                     break;
00088             }
00089 
00090             break;
00091 
00092         case HID_REPORT_FEATURE:
00093             break;
00094     }
00095 
00096     return (0);
00097 }
00098 
00099 // USB HID override function return 1 if the activity is trivial or response is null
00100 __attribute__((weak))
00101 uint8_t usbd_hid_no_activity(U8 *buf)
00102 {
00103     return 0;
00104 }
00105 
00106 // USB HID Callback: when data is received from the host
00107 void usbd_hid_set_report(U8 rtype, U8 rid, U8 *buf, int len, U8 req)
00108 {
00109     uint8_t * rbuf;
00110     main_led_state_t led_next_state = MAIN_LED_FLASH;
00111     switch (rtype) {
00112         case HID_REPORT_OUTPUT:
00113             if (len == 0) {
00114                 break;
00115             }
00116 
00117             if (buf[0] == ID_DAP_TransferAbort) {
00118                 DAP_TransferAbort = 1;
00119                 break;
00120             }
00121 
00122             // execute and store to DAP_queue
00123             if (DAP_queue_execute_buf(&DAP_Cmd_queue, buf, len, &rbuf)) {
00124                 if(usbd_hid_no_activity(rbuf) == 1){
00125                     //revert HID LED to default if the response is null
00126                     led_next_state = MAIN_LED_DEF;
00127                 }
00128                 if (USB_ResponseIdle) {
00129                     hid_send_packet();
00130                     USB_ResponseIdle = 0;
00131                 }
00132             } else {
00133                 util_assert(0);
00134             }
00135 
00136             main_blink_hid_led(led_next_state);
00137 
00138             break;
00139 
00140         case HID_REPORT_FEATURE:
00141             break;
00142     }
00143 }
00144 
00145