ST/USBHOST forked to add another HID handler for raw keyboard data to get more detail not available with current handlers (all pressed keys, all releases, and periodic updates)

Dependents:   C64-stm429_discovery

Committer:
davervw
Date:
Mon Apr 13 05:25:10 2020 +0000
Revision:
7:9dc1cb9d5e12
Parent:
1:ab240722d7ef
Added handler to USBHostHID/USBHostKeyboard.cpp:;    void (*onKeyData)(uint8_t len, uint8_t* data);; so can get raw keyboard data for all keys simultaneously pressed, and all releases and periodic data

Who changed what in which revision?

UserRevisionLine numberNew contents of line
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 1 /* mbed USBHost Library
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 2 * Copyright (c) 2006-2013 ARM Limited
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 3 *
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 4 * Licensed under the Apache License, Version 2.0 (the "License");
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 5 * you may not use this file except in compliance with the License.
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 6 * You may obtain a copy of the License at
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 7 *
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 8 * http://www.apache.org/licenses/LICENSE-2.0
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 9 *
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 10 * Unless required by applicable law or agreed to in writing, software
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 11 * distributed under the License is distributed on an "AS IS" BASIS,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 13 * See the License for the specific language governing permissions and
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 14 * limitations under the License.
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 15 */
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 16
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 17
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 18 #include "dbg.h"
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 19 #include "USBEndpoint.h"
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 20 #if !defined(USBHOST_OTHER)
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 21 void USBEndpoint::init(HCED * hced_, ENDPOINT_TYPE type_, ENDPOINT_DIRECTION dir_, uint32_t size, uint8_t ep_number, HCTD* td_list_[2])
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 22 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 23 hced = hced_;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 24 type = type_;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 25 dir = dir_;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 26 setup = (type == CONTROL_ENDPOINT) ? true : false;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 27
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 28 //TDs have been allocated by the host
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 29 memcpy((HCTD**)td_list, td_list_, sizeof(HCTD*)*2); //TODO: Maybe should add a param for td_list size... at least a define
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 30 memset(td_list_[0], 0, sizeof(HCTD));
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 31 memset(td_list_[1], 0, sizeof(HCTD));
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 32
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 33 td_list[0]->ep = this;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 34 td_list[1]->ep = this;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 35
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 36 hced->control = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 37 //Empty queue
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 38 hced->tailTD = td_list[0];
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 39 hced->headTD = td_list[0];
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 40 hced->nextED = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 41
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 42 address = (ep_number & 0x7F) | ((dir - 1) << 7);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 43
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 44 hced->control = ((ep_number & 0x7F) << 7) // Endpoint address
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 45 | (type != CONTROL_ENDPOINT ? ( dir << 11) : 0 ) // direction : Out = 1, 2 = In
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 46 | ((size & 0x3ff) << 16); // MaxPkt Size
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 47
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 48 transfer_len = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 49 transferred = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 50 buf_start = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 51 nextEp = NULL;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 52
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 53 td_current = td_list[0];
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 54 td_next = td_list[1];
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 55
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 56 intf_nb = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 57
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 58 state = USB_TYPE_IDLE;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 59 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 60
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 61 void USBEndpoint::setSize(uint32_t size)
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 62 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 63 hced->control &= ~(0x3ff << 16);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 64 hced->control |= (size << 16);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 65 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 66
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 67
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 68 void USBEndpoint::setDeviceAddress(uint8_t addr)
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 69 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 70 hced->control &= ~(0x7f);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 71 hced->control |= (addr & 0x7F);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 72 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 73
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 74 void USBEndpoint::setSpeed(uint8_t speed)
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 75 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 76 hced->control &= ~(1 << 13);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 77 hced->control |= (speed << 13);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 78 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 79 #endif
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 80
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 81 //Only for control Eps
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 82 void USBEndpoint::setNextToken(uint32_t token)
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 83 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 84 switch (token) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 85 case TD_SETUP:
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 86 dir = OUT;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 87 setup = true;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 88 break;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 89 case TD_IN:
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 90 dir = IN;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 91 setup = false;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 92 break;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 93 case TD_OUT:
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 94 dir = OUT;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 95 setup = false;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 96 break;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 97 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 98 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 99 struct {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 100 USB_TYPE type;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 101 const char * str;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 102 } static type_string[] = {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 103 /*0*/ {USB_TYPE_OK, "USB_TYPE_OK"},
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 104 {USB_TYPE_CRC_ERROR, "USB_TYPE_CRC_ERROR"},
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 105 {USB_TYPE_BIT_STUFFING_ERROR, "USB_TYPE_BIT_STUFFING_ERROR"},
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 106 {USB_TYPE_DATA_TOGGLE_MISMATCH_ERROR, "USB_TYPE_DATA_TOGGLE_MISMATCH_ERROR"},
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 107 {USB_TYPE_STALL_ERROR, "USB_TYPE_STALL_ERROR"},
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 108 /*5*/ {USB_TYPE_DEVICE_NOT_RESPONDING_ERROR, "USB_TYPE_DEVICE_NOT_RESPONDING_ERROR"},
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 109 {USB_TYPE_PID_CHECK_FAILURE_ERROR, "USB_TYPE_PID_CHECK_FAILURE_ERROR"},
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 110 {USB_TYPE_UNEXPECTED_PID_ERROR, "USB_TYPE_UNEXPECTED_PID_ERROR"},
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 111 {USB_TYPE_DATA_OVERRUN_ERROR, "USB_TYPE_DATA_OVERRUN_ERROR"},
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 112 {USB_TYPE_DATA_UNDERRUN_ERROR, "USB_TYPE_DATA_UNDERRUN_ERROR"},
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 113 /*10*/ {USB_TYPE_ERROR, "USB_TYPE_ERROR"},
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 114 {USB_TYPE_ERROR, "USB_TYPE_ERROR"},
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 115 {USB_TYPE_BUFFER_OVERRUN_ERROR, "USB_TYPE_BUFFER_OVERRUN_ERROR"},
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 116 {USB_TYPE_BUFFER_UNDERRUN_ERROR, "USB_TYPE_BUFFER_UNDERRUN_ERROR"},
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 117 {USB_TYPE_DISCONNECTED, "USB_TYPE_DISCONNECTED"},
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 118 /*15*/ {USB_TYPE_FREE, "USB_TYPE_FREE"},
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 119 {USB_TYPE_IDLE, "USB_TYPE_IDLE"},
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 120 {USB_TYPE_PROCESSING, "USB_TYPE_PROCESSING"},
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 121 {USB_TYPE_ERROR, "USB_TYPE_ERROR"}
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 122 };
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 123 const char * USBEndpoint::getStateString() {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 124 return type_string[state].str;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 125 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 126
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 127 #if !defined(USBHOST_OTHER)
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 128 void USBEndpoint::setState(uint8_t st) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 129 if (st > 18)
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 130 return;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 131 state = type_string[st].type;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 132 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 133
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 134 USB_TYPE USBEndpoint::queueTransfer()
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 135 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 136 transfer_len = (uint32_t)td_current->bufEnd - (uint32_t)td_current->currBufPtr + 1;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 137 transferred = transfer_len;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 138 buf_start = (uint8_t *)td_current->currBufPtr;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 139
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 140 //Now add this free TD at this end of the queue
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 141 state = USB_TYPE_PROCESSING;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 142 td_current->nextTD = (hcTd*)td_next;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 143 hced->tailTD = td_next;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 144 return USB_TYPE_PROCESSING;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 145 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 146
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 147 void USBEndpoint::unqueueTransfer(volatile HCTD * td)
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 148 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 149 td->control=0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 150 td->currBufPtr=0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 151 td->bufEnd=0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 152 td->nextTD=0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 153 hced->headTD = (HCTD *)((uint32_t)hced->tailTD | ((uint32_t)hced->headTD & 0x2)); //Carry bit
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 154 td_current = td_next;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 155 td_next = td;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 156 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 157
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 158 void USBEndpoint::queueEndpoint(USBEndpoint * ed)
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 159 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 160 nextEp = ed;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 161 hced->nextED = (ed == NULL) ? 0 : (hcEd*)(ed->getHCED());
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 162 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 163 #endif