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