Repostiory containing DAPLink source code with Reset Pin workaround for HANI_IOT board.

Upstream: https://github.com/ARMmbed/DAPLink

Committer:
Pawel Zarembski
Date:
Tue Apr 07 12:55:42 2020 +0200
Revision:
0:01f31e923fe2
hani: DAPLink with reset workaround

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pawel Zarembski 0:01f31e923fe2 1 /**
Pawel Zarembski 0:01f31e923fe2 2 * @file usbd_user_hid.c
Pawel Zarembski 0:01f31e923fe2 3 * @brief HID driver for CMSIS-DAP packet processing
Pawel Zarembski 0:01f31e923fe2 4 *
Pawel Zarembski 0:01f31e923fe2 5 * DAPLink Interface Firmware
Pawel Zarembski 0:01f31e923fe2 6 * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
Pawel Zarembski 0:01f31e923fe2 7 * SPDX-License-Identifier: Apache-2.0
Pawel Zarembski 0:01f31e923fe2 8 *
Pawel Zarembski 0:01f31e923fe2 9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Pawel Zarembski 0:01f31e923fe2 10 * not use this file except in compliance with the License.
Pawel Zarembski 0:01f31e923fe2 11 * You may obtain a copy of the License at
Pawel Zarembski 0:01f31e923fe2 12 *
Pawel Zarembski 0:01f31e923fe2 13 * http://www.apache.org/licenses/LICENSE-2.0
Pawel Zarembski 0:01f31e923fe2 14 *
Pawel Zarembski 0:01f31e923fe2 15 * Unless required by applicable law or agreed to in writing, software
Pawel Zarembski 0:01f31e923fe2 16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Pawel Zarembski 0:01f31e923fe2 17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Pawel Zarembski 0:01f31e923fe2 18 * See the License for the specific language governing permissions and
Pawel Zarembski 0:01f31e923fe2 19 * limitations under the License.
Pawel Zarembski 0:01f31e923fe2 20 */
Pawel Zarembski 0:01f31e923fe2 21
Pawel Zarembski 0:01f31e923fe2 22 #include <string.h>
Pawel Zarembski 0:01f31e923fe2 23 #include "rl_usb.h"
Pawel Zarembski 0:01f31e923fe2 24 #include "usb.h"
Pawel Zarembski 0:01f31e923fe2 25 #define __NO_USB_LIB_C
Pawel Zarembski 0:01f31e923fe2 26 #include "usb_config.c"
Pawel Zarembski 0:01f31e923fe2 27 #include "DAP_config.h"
Pawel Zarembski 0:01f31e923fe2 28 #include "DAP.h"
Pawel Zarembski 0:01f31e923fe2 29 #include "util.h"
Pawel Zarembski 0:01f31e923fe2 30 #include "DAP_queue.h"
Pawel Zarembski 0:01f31e923fe2 31 #include "main.h"
Pawel Zarembski 0:01f31e923fe2 32
Pawel Zarembski 0:01f31e923fe2 33
Pawel Zarembski 0:01f31e923fe2 34 #if (USBD_HID_OUTREPORT_MAX_SZ > DAP_PACKET_SIZE)
Pawel Zarembski 0:01f31e923fe2 35 #error "USB HID Output Report Size must be less than DAP Packet Size"
Pawel Zarembski 0:01f31e923fe2 36 #endif
Pawel Zarembski 0:01f31e923fe2 37 #if (USBD_HID_INREPORT_MAX_SZ > DAP_PACKET_SIZE)
Pawel Zarembski 0:01f31e923fe2 38 #error "USB HID Input Report Size must be less than DAP Packet Size"
Pawel Zarembski 0:01f31e923fe2 39 #endif
Pawel Zarembski 0:01f31e923fe2 40
Pawel Zarembski 0:01f31e923fe2 41 static volatile uint8_t USB_ResponseIdle;
Pawel Zarembski 0:01f31e923fe2 42 static DAP_queue DAP_Cmd_queue;
Pawel Zarembski 0:01f31e923fe2 43
Pawel Zarembski 0:01f31e923fe2 44 void hid_send_packet()
Pawel Zarembski 0:01f31e923fe2 45 {
Pawel Zarembski 0:01f31e923fe2 46 uint8_t * sbuf;
Pawel Zarembski 0:01f31e923fe2 47 int slen;
Pawel Zarembski 0:01f31e923fe2 48 if (DAP_queue_get_send_buf(&DAP_Cmd_queue, &sbuf, &slen)) {
Pawel Zarembski 0:01f31e923fe2 49 if (slen > USBD_HID_OUTREPORT_MAX_SZ){
Pawel Zarembski 0:01f31e923fe2 50 util_assert(0);
Pawel Zarembski 0:01f31e923fe2 51 }else {
Pawel Zarembski 0:01f31e923fe2 52 usbd_hid_get_report_trigger(0, sbuf, USBD_HID_OUTREPORT_MAX_SZ);
Pawel Zarembski 0:01f31e923fe2 53 }
Pawel Zarembski 0:01f31e923fe2 54 }
Pawel Zarembski 0:01f31e923fe2 55 }
Pawel Zarembski 0:01f31e923fe2 56
Pawel Zarembski 0:01f31e923fe2 57 // USB HID Callback: when system initializes
Pawel Zarembski 0:01f31e923fe2 58 void usbd_hid_init(void)
Pawel Zarembski 0:01f31e923fe2 59 {
Pawel Zarembski 0:01f31e923fe2 60 USB_ResponseIdle = 1;
Pawel Zarembski 0:01f31e923fe2 61 DAP_queue_init(&DAP_Cmd_queue);
Pawel Zarembski 0:01f31e923fe2 62 }
Pawel Zarembski 0:01f31e923fe2 63
Pawel Zarembski 0:01f31e923fe2 64 // USB HID Callback: when data needs to be prepared for the host
Pawel Zarembski 0:01f31e923fe2 65 int usbd_hid_get_report(U8 rtype, U8 rid, U8 *buf, U8 req)
Pawel Zarembski 0:01f31e923fe2 66 {
Pawel Zarembski 0:01f31e923fe2 67 uint8_t * sbuf;
Pawel Zarembski 0:01f31e923fe2 68 int slen;
Pawel Zarembski 0:01f31e923fe2 69 switch (rtype) {
Pawel Zarembski 0:01f31e923fe2 70 case HID_REPORT_INPUT:
Pawel Zarembski 0:01f31e923fe2 71 switch (req) {
Pawel Zarembski 0:01f31e923fe2 72 case USBD_HID_REQ_PERIOD_UPDATE:
Pawel Zarembski 0:01f31e923fe2 73 break;
Pawel Zarembski 0:01f31e923fe2 74
Pawel Zarembski 0:01f31e923fe2 75 case USBD_HID_REQ_EP_CTRL:
Pawel Zarembski 0:01f31e923fe2 76 case USBD_HID_REQ_EP_INT:
Pawel Zarembski 0:01f31e923fe2 77 if (DAP_queue_get_send_buf(&DAP_Cmd_queue, &sbuf, &slen)) {
Pawel Zarembski 0:01f31e923fe2 78 if (slen > USBD_HID_OUTREPORT_MAX_SZ){
Pawel Zarembski 0:01f31e923fe2 79 util_assert(0);
Pawel Zarembski 0:01f31e923fe2 80 }else {
Pawel Zarembski 0:01f31e923fe2 81 memcpy(buf, sbuf, slen);
Pawel Zarembski 0:01f31e923fe2 82 return (USBD_HID_OUTREPORT_MAX_SZ);
Pawel Zarembski 0:01f31e923fe2 83 }
Pawel Zarembski 0:01f31e923fe2 84 } else if (req == USBD_HID_REQ_EP_INT) {
Pawel Zarembski 0:01f31e923fe2 85 USB_ResponseIdle = 1;
Pawel Zarembski 0:01f31e923fe2 86 }
Pawel Zarembski 0:01f31e923fe2 87 break;
Pawel Zarembski 0:01f31e923fe2 88 }
Pawel Zarembski 0:01f31e923fe2 89
Pawel Zarembski 0:01f31e923fe2 90 break;
Pawel Zarembski 0:01f31e923fe2 91
Pawel Zarembski 0:01f31e923fe2 92 case HID_REPORT_FEATURE:
Pawel Zarembski 0:01f31e923fe2 93 break;
Pawel Zarembski 0:01f31e923fe2 94 }
Pawel Zarembski 0:01f31e923fe2 95
Pawel Zarembski 0:01f31e923fe2 96 return (0);
Pawel Zarembski 0:01f31e923fe2 97 }
Pawel Zarembski 0:01f31e923fe2 98
Pawel Zarembski 0:01f31e923fe2 99 // USB HID override function return 1 if the activity is trivial or response is null
Pawel Zarembski 0:01f31e923fe2 100 __attribute__((weak))
Pawel Zarembski 0:01f31e923fe2 101 uint8_t usbd_hid_no_activity(U8 *buf)
Pawel Zarembski 0:01f31e923fe2 102 {
Pawel Zarembski 0:01f31e923fe2 103 return 0;
Pawel Zarembski 0:01f31e923fe2 104 }
Pawel Zarembski 0:01f31e923fe2 105
Pawel Zarembski 0:01f31e923fe2 106 // USB HID Callback: when data is received from the host
Pawel Zarembski 0:01f31e923fe2 107 void usbd_hid_set_report(U8 rtype, U8 rid, U8 *buf, int len, U8 req)
Pawel Zarembski 0:01f31e923fe2 108 {
Pawel Zarembski 0:01f31e923fe2 109 uint8_t * rbuf;
Pawel Zarembski 0:01f31e923fe2 110 main_led_state_t led_next_state = MAIN_LED_FLASH;
Pawel Zarembski 0:01f31e923fe2 111 switch (rtype) {
Pawel Zarembski 0:01f31e923fe2 112 case HID_REPORT_OUTPUT:
Pawel Zarembski 0:01f31e923fe2 113 if (len == 0) {
Pawel Zarembski 0:01f31e923fe2 114 break;
Pawel Zarembski 0:01f31e923fe2 115 }
Pawel Zarembski 0:01f31e923fe2 116
Pawel Zarembski 0:01f31e923fe2 117 if (buf[0] == ID_DAP_TransferAbort) {
Pawel Zarembski 0:01f31e923fe2 118 DAP_TransferAbort = 1;
Pawel Zarembski 0:01f31e923fe2 119 break;
Pawel Zarembski 0:01f31e923fe2 120 }
Pawel Zarembski 0:01f31e923fe2 121
Pawel Zarembski 0:01f31e923fe2 122 // execute and store to DAP_queue
Pawel Zarembski 0:01f31e923fe2 123 if (DAP_queue_execute_buf(&DAP_Cmd_queue, buf, len, &rbuf)) {
Pawel Zarembski 0:01f31e923fe2 124 if(usbd_hid_no_activity(rbuf) == 1){
Pawel Zarembski 0:01f31e923fe2 125 //revert HID LED to default if the response is null
Pawel Zarembski 0:01f31e923fe2 126 led_next_state = MAIN_LED_DEF;
Pawel Zarembski 0:01f31e923fe2 127 }
Pawel Zarembski 0:01f31e923fe2 128 if (USB_ResponseIdle) {
Pawel Zarembski 0:01f31e923fe2 129 hid_send_packet();
Pawel Zarembski 0:01f31e923fe2 130 USB_ResponseIdle = 0;
Pawel Zarembski 0:01f31e923fe2 131 }
Pawel Zarembski 0:01f31e923fe2 132 } else {
Pawel Zarembski 0:01f31e923fe2 133 util_assert(0);
Pawel Zarembski 0:01f31e923fe2 134 }
Pawel Zarembski 0:01f31e923fe2 135
Pawel Zarembski 0:01f31e923fe2 136 main_blink_hid_led(led_next_state);
Pawel Zarembski 0:01f31e923fe2 137
Pawel Zarembski 0:01f31e923fe2 138 break;
Pawel Zarembski 0:01f31e923fe2 139
Pawel Zarembski 0:01f31e923fe2 140 case HID_REPORT_FEATURE:
Pawel Zarembski 0:01f31e923fe2 141 break;
Pawel Zarembski 0:01f31e923fe2 142 }
Pawel Zarembski 0:01f31e923fe2 143 }
Pawel Zarembski 0:01f31e923fe2 144
Pawel Zarembski 0:01f31e923fe2 145