only for STM32F769NI-DISCOVERY, porting from mbed OS5 unsupport functions

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?

UserRevisionLine numberNew 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 #if defined(TARGET_STM) && defined(USBHOST_OTHER)
kenjiArai 0:dae1ac0c0a7b 17
kenjiArai 0:dae1ac0c0a7b 18 #include "dbg.h"
kenjiArai 0:dae1ac0c0a7b 19 #include "USBEndpoint.h"
kenjiArai 0:dae1ac0c0a7b 20 extern uint32_t HAL_HCD_HC_GetMaxPacket(HCD_HandleTypeDef *hhcd, uint8_t chn_num);
kenjiArai 0:dae1ac0c0a7b 21 extern uint32_t HAL_HCD_HC_GetType(HCD_HandleTypeDef *hhcd, uint8_t chn_num);
kenjiArai 0:dae1ac0c0a7b 22 extern void HAL_HCD_DisableInt(HCD_HandleTypeDef *hhcd, uint8_t chn_num);
kenjiArai 0:dae1ac0c0a7b 23 extern void HAL_HCD_EnableInt(HCD_HandleTypeDef *hhcd, uint8_t chn_num);
kenjiArai 0:dae1ac0c0a7b 24
kenjiArai 0:dae1ac0c0a7b 25
kenjiArai 0:dae1ac0c0a7b 26
kenjiArai 0:dae1ac0c0a7b 27
kenjiArai 0:dae1ac0c0a7b 28 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 29 {
kenjiArai 0:dae1ac0c0a7b 30 HCD_HandleTypeDef *hhcd;
kenjiArai 0:dae1ac0c0a7b 31 uint32_t *addr;
kenjiArai 0:dae1ac0c0a7b 32
kenjiArai 0:dae1ac0c0a7b 33 hced = hced_;
kenjiArai 0:dae1ac0c0a7b 34 type = type_;
kenjiArai 0:dae1ac0c0a7b 35 dir = dir_;
kenjiArai 0:dae1ac0c0a7b 36 setup = (type == CONTROL_ENDPOINT) ? true : false;
kenjiArai 0:dae1ac0c0a7b 37
kenjiArai 0:dae1ac0c0a7b 38 //TDs have been allocated by the host
kenjiArai 0:dae1ac0c0a7b 39 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 40 memset(td_list_[0], 0, sizeof(HCTD));
kenjiArai 0:dae1ac0c0a7b 41 memset(td_list_[1], 0, sizeof(HCTD));
kenjiArai 0:dae1ac0c0a7b 42
kenjiArai 0:dae1ac0c0a7b 43 td_list[0]->ep = this;
kenjiArai 0:dae1ac0c0a7b 44 td_list[1]->ep = this;
kenjiArai 0:dae1ac0c0a7b 45
kenjiArai 0:dae1ac0c0a7b 46 address = (ep_number & 0x7F) | ((dir - 1) << 7);
kenjiArai 0:dae1ac0c0a7b 47 this->size = size;
kenjiArai 0:dae1ac0c0a7b 48 this->ep_number = ep_number;
kenjiArai 0:dae1ac0c0a7b 49 transfer_len = 0;
kenjiArai 0:dae1ac0c0a7b 50 transferred = 0;
kenjiArai 0:dae1ac0c0a7b 51 buf_start = 0;
kenjiArai 0:dae1ac0c0a7b 52 nextEp = NULL;
kenjiArai 0:dae1ac0c0a7b 53
kenjiArai 0:dae1ac0c0a7b 54 td_current = td_list[0];
kenjiArai 0:dae1ac0c0a7b 55 td_next = td_list[1];
kenjiArai 0:dae1ac0c0a7b 56 /* remove potential post pending from previous endpoint */
kenjiArai 0:dae1ac0c0a7b 57 ep_queue.get(0);
kenjiArai 0:dae1ac0c0a7b 58 intf_nb = 0;
kenjiArai 0:dae1ac0c0a7b 59 hhcd = (HCD_HandleTypeDef *)hced->hhcd;
kenjiArai 0:dae1ac0c0a7b 60 addr = &((uint32_t *)hhcd->pData)[hced->ch_num];
kenjiArai 0:dae1ac0c0a7b 61 *addr = 0;
kenjiArai 0:dae1ac0c0a7b 62 state = USB_TYPE_IDLE;
kenjiArai 0:dae1ac0c0a7b 63 speed = false;
kenjiArai 0:dae1ac0c0a7b 64 }
kenjiArai 0:dae1ac0c0a7b 65 void USBEndpoint::setSize(uint32_t size)
kenjiArai 0:dae1ac0c0a7b 66 {
kenjiArai 0:dae1ac0c0a7b 67 this->size = size;
kenjiArai 0:dae1ac0c0a7b 68 }
kenjiArai 0:dae1ac0c0a7b 69
kenjiArai 0:dae1ac0c0a7b 70
kenjiArai 0:dae1ac0c0a7b 71 void USBEndpoint::setDeviceAddress(uint8_t addr)
kenjiArai 0:dae1ac0c0a7b 72 {
kenjiArai 0:dae1ac0c0a7b 73 HCD_HandleTypeDef *hhcd;
kenjiArai 0:dae1ac0c0a7b 74 uint8_t hcd_speed = HCD_SPEED_FULL;
kenjiArai 0:dae1ac0c0a7b 75 /* fix me : small speed device with hub not supported
kenjiArai 0:dae1ac0c0a7b 76 if (this->speed) hcd_speed = HCD_SPEED_LOW; */
kenjiArai 0:dae1ac0c0a7b 77 if (this->speed) {
kenjiArai 0:dae1ac0c0a7b 78 USB_WARN("small speed device on hub not supported");
kenjiArai 0:dae1ac0c0a7b 79 }
kenjiArai 0:dae1ac0c0a7b 80 MBED_ASSERT(HAL_HCD_HC_Init((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num, address, addr, hcd_speed, type, size) != HAL_BUSY);
kenjiArai 0:dae1ac0c0a7b 81 this->device_address = addr;
kenjiArai 0:dae1ac0c0a7b 82
kenjiArai 0:dae1ac0c0a7b 83 }
kenjiArai 0:dae1ac0c0a7b 84
kenjiArai 0:dae1ac0c0a7b 85 void USBEndpoint::setSpeed(uint8_t speed)
kenjiArai 0:dae1ac0c0a7b 86 {
kenjiArai 0:dae1ac0c0a7b 87 this->speed = speed;
kenjiArai 0:dae1ac0c0a7b 88 }
kenjiArai 0:dae1ac0c0a7b 89
kenjiArai 0:dae1ac0c0a7b 90
kenjiArai 0:dae1ac0c0a7b 91
kenjiArai 0:dae1ac0c0a7b 92 void USBEndpoint::setState(USB_TYPE st)
kenjiArai 0:dae1ac0c0a7b 93 {
kenjiArai 0:dae1ac0c0a7b 94 /* modify this state is possible only with a plug */
kenjiArai 0:dae1ac0c0a7b 95 if (state == USB_TYPE_FREE) {
kenjiArai 0:dae1ac0c0a7b 96 return;
kenjiArai 0:dae1ac0c0a7b 97 }
kenjiArai 0:dae1ac0c0a7b 98
kenjiArai 0:dae1ac0c0a7b 99 state = st;
kenjiArai 0:dae1ac0c0a7b 100 if (st == USB_TYPE_FREE) {
kenjiArai 0:dae1ac0c0a7b 101 HCD_HandleTypeDef *hhcd = (HCD_HandleTypeDef *)hced->hhcd;
kenjiArai 0:dae1ac0c0a7b 102 uint32_t *addr = &((uint32_t *)hhcd->pData)[hced->ch_num];
kenjiArai 0:dae1ac0c0a7b 103 if ((*addr) && (type != INTERRUPT_ENDPOINT)) {
kenjiArai 0:dae1ac0c0a7b 104 this->ep_queue.put((uint8_t *)1);
kenjiArai 0:dae1ac0c0a7b 105 }
kenjiArai 0:dae1ac0c0a7b 106 MBED_ASSERT(HAL_HCD_HC_Halt((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num) != HAL_BUSY);
kenjiArai 0:dae1ac0c0a7b 107 HAL_HCD_DisableInt((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num);
kenjiArai 0:dae1ac0c0a7b 108 *addr = 0;
kenjiArai 0:dae1ac0c0a7b 109
kenjiArai 0:dae1ac0c0a7b 110 }
kenjiArai 0:dae1ac0c0a7b 111 if (st == USB_TYPE_ERROR) {
kenjiArai 0:dae1ac0c0a7b 112 MBED_ASSERT(HAL_HCD_HC_Halt((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num) != HAL_BUSY);
kenjiArai 0:dae1ac0c0a7b 113 HAL_HCD_DisableInt((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num);
kenjiArai 0:dae1ac0c0a7b 114
kenjiArai 0:dae1ac0c0a7b 115 }
kenjiArai 0:dae1ac0c0a7b 116 if (st == USB_TYPE_ERROR) {
kenjiArai 0:dae1ac0c0a7b 117 uint8_t hcd_speed = HCD_SPEED_FULL;
kenjiArai 0:dae1ac0c0a7b 118 /* small speed device with hub not supported
kenjiArai 0:dae1ac0c0a7b 119 if (this->speed) hcd_speed = HCD_SPEED_LOW;*/
kenjiArai 0:dae1ac0c0a7b 120 MBED_ASSERT(HAL_HCD_HC_Init((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num, address, 0, hcd_speed, type, size) != HAL_BUSY);
kenjiArai 0:dae1ac0c0a7b 121 }
kenjiArai 0:dae1ac0c0a7b 122 }
kenjiArai 0:dae1ac0c0a7b 123
kenjiArai 0:dae1ac0c0a7b 124
kenjiArai 0:dae1ac0c0a7b 125 extern uint32_t HAL_HCD_HC_GetMaxPacket(HCD_HandleTypeDef *hhcd, uint8_t chn_num);
kenjiArai 0:dae1ac0c0a7b 126 extern uint32_t HAL_HCD_HC_GetType(HCD_HandleTypeDef *hhcd, uint8_t chn_num);
kenjiArai 0:dae1ac0c0a7b 127
kenjiArai 0:dae1ac0c0a7b 128
kenjiArai 0:dae1ac0c0a7b 129 USB_TYPE USBEndpoint::queueTransfer()
kenjiArai 0:dae1ac0c0a7b 130 {
kenjiArai 0:dae1ac0c0a7b 131 HCD_HandleTypeDef *hhcd = (HCD_HandleTypeDef *)hced->hhcd;
kenjiArai 0:dae1ac0c0a7b 132 uint32_t *addr = &((uint32_t *)hhcd->pData)[hced->ch_num];
kenjiArai 0:dae1ac0c0a7b 133 uint32_t type = HAL_HCD_HC_GetType(hhcd, hced->ch_num);
kenjiArai 0:dae1ac0c0a7b 134 uint32_t max_size = HAL_HCD_HC_GetMaxPacket(hhcd, hced->ch_num);
kenjiArai 0:dae1ac0c0a7b 135 /* if a packet is queue on disconnected ; no solution for now */
kenjiArai 0:dae1ac0c0a7b 136 if (state == USB_TYPE_FREE) {
kenjiArai 0:dae1ac0c0a7b 137 td_current->state = USB_TYPE_FREE;
kenjiArai 0:dae1ac0c0a7b 138 return USB_TYPE_FREE;
kenjiArai 0:dae1ac0c0a7b 139 }
kenjiArai 0:dae1ac0c0a7b 140 ep_queue.get(0);
kenjiArai 0:dae1ac0c0a7b 141 MBED_ASSERT(*addr == 0);
kenjiArai 0:dae1ac0c0a7b 142 transfer_len = td_current->size <= max_size ? td_current->size : max_size;
kenjiArai 0:dae1ac0c0a7b 143 buf_start = (uint8_t *)td_current->currBufPtr;
kenjiArai 0:dae1ac0c0a7b 144
kenjiArai 0:dae1ac0c0a7b 145 //Now add this free TD at this end of the queue
kenjiArai 0:dae1ac0c0a7b 146 state = USB_TYPE_PROCESSING;
kenjiArai 0:dae1ac0c0a7b 147 /* one request */
kenjiArai 0:dae1ac0c0a7b 148 td_current->nextTD = (hcTd *)0;
kenjiArai 0:dae1ac0c0a7b 149 #if defined(MAX_NYET_RETRY)
kenjiArai 0:dae1ac0c0a7b 150 td_current->retry = 0;
kenjiArai 0:dae1ac0c0a7b 151 #endif
kenjiArai 0:dae1ac0c0a7b 152 td_current->setup = setup;
kenjiArai 0:dae1ac0c0a7b 153 *addr = (uint32_t)td_current;
kenjiArai 0:dae1ac0c0a7b 154 /* dir /setup is inverted for ST */
kenjiArai 0:dae1ac0c0a7b 155 /* token is useful only ctrl endpoint */
kenjiArai 0:dae1ac0c0a7b 156 /* last parameter is ping ? */
kenjiArai 0:dae1ac0c0a7b 157 MBED_ASSERT(HAL_HCD_HC_SubmitRequest((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num, dir - 1, type, !setup, (uint8_t *) td_current->currBufPtr, transfer_len, 0) == HAL_OK);
kenjiArai 0:dae1ac0c0a7b 158 HAL_HCD_EnableInt((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num);
kenjiArai 0:dae1ac0c0a7b 159
kenjiArai 0:dae1ac0c0a7b 160 return USB_TYPE_PROCESSING;
kenjiArai 0:dae1ac0c0a7b 161 }
kenjiArai 0:dae1ac0c0a7b 162
kenjiArai 0:dae1ac0c0a7b 163 void USBEndpoint::unqueueTransfer(volatile HCTD *td)
kenjiArai 0:dae1ac0c0a7b 164 {
kenjiArai 0:dae1ac0c0a7b 165 if (state == USB_TYPE_FREE) {
kenjiArai 0:dae1ac0c0a7b 166 return;
kenjiArai 0:dae1ac0c0a7b 167 }
kenjiArai 0:dae1ac0c0a7b 168 uint32_t *addr = &((uint32_t *)((HCD_HandleTypeDef *)hced->hhcd)->pData)[hced->ch_num];
kenjiArai 0:dae1ac0c0a7b 169 td->state = 0;
kenjiArai 0:dae1ac0c0a7b 170 td->currBufPtr = 0;
kenjiArai 0:dae1ac0c0a7b 171 td->size = 0;
kenjiArai 0:dae1ac0c0a7b 172 td->nextTD = 0;
kenjiArai 0:dae1ac0c0a7b 173 *addr = 0;
kenjiArai 0:dae1ac0c0a7b 174 td_current = td_next;
kenjiArai 0:dae1ac0c0a7b 175 td_next = td;
kenjiArai 0:dae1ac0c0a7b 176 }
kenjiArai 0:dae1ac0c0a7b 177
kenjiArai 0:dae1ac0c0a7b 178 void USBEndpoint::queueEndpoint(USBEndpoint *ed)
kenjiArai 0:dae1ac0c0a7b 179 {
kenjiArai 0:dae1ac0c0a7b 180 nextEp = ed;
kenjiArai 0:dae1ac0c0a7b 181 }
kenjiArai 0:dae1ac0c0a7b 182 #endif