Added TARGET_DISCO_F469NI in USBHOST\USBHost\TARGET_STM\USBHALHost_STM_TARGET.h
Dependents: DISCO-F469NI_USB_Disk STM32F4xx_USB_Memory
Fork of USBHOST by
targets/TARGET_STM/USBEndpoint_STM.cpp@8:3e7a33f81048, 2020-01-04 (annotated)
- Committer:
- kenjiArai
- Date:
- Sat Jan 04 23:30:59 2020 +0000
- Revision:
- 8:3e7a33f81048
updated for STM32F4xx
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 8:3e7a33f81048 | 1 | /* mbed USBHost Library |
kenjiArai | 8:3e7a33f81048 | 2 | * Copyright (c) 2006-2013 ARM Limited |
kenjiArai | 8:3e7a33f81048 | 3 | * |
kenjiArai | 8:3e7a33f81048 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kenjiArai | 8:3e7a33f81048 | 5 | * you may not use this file except in compliance with the License. |
kenjiArai | 8:3e7a33f81048 | 6 | * You may obtain a copy of the License at |
kenjiArai | 8:3e7a33f81048 | 7 | * |
kenjiArai | 8:3e7a33f81048 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kenjiArai | 8:3e7a33f81048 | 9 | * |
kenjiArai | 8:3e7a33f81048 | 10 | * Unless required by applicable law or agreed to in writing, software |
kenjiArai | 8:3e7a33f81048 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
kenjiArai | 8:3e7a33f81048 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kenjiArai | 8:3e7a33f81048 | 13 | * See the License for the specific language governing permissions and |
kenjiArai | 8:3e7a33f81048 | 14 | * limitations under the License. |
kenjiArai | 8:3e7a33f81048 | 15 | */ |
kenjiArai | 8:3e7a33f81048 | 16 | #if defined(TARGET_STM) && defined(USBHOST_OTHER) |
kenjiArai | 8:3e7a33f81048 | 17 | |
kenjiArai | 8:3e7a33f81048 | 18 | #include "dbg.h" |
kenjiArai | 8:3e7a33f81048 | 19 | #include "USBEndpoint.h" |
kenjiArai | 8:3e7a33f81048 | 20 | extern uint32_t HAL_HCD_HC_GetMaxPacket(HCD_HandleTypeDef *hhcd, uint8_t chn_num); |
kenjiArai | 8:3e7a33f81048 | 21 | extern uint32_t HAL_HCD_HC_GetType(HCD_HandleTypeDef *hhcd, uint8_t chn_num); |
kenjiArai | 8:3e7a33f81048 | 22 | extern void HAL_HCD_DisableInt(HCD_HandleTypeDef *hhcd, uint8_t chn_num); |
kenjiArai | 8:3e7a33f81048 | 23 | extern void HAL_HCD_EnableInt(HCD_HandleTypeDef *hhcd, uint8_t chn_num); |
kenjiArai | 8:3e7a33f81048 | 24 | |
kenjiArai | 8:3e7a33f81048 | 25 | |
kenjiArai | 8:3e7a33f81048 | 26 | |
kenjiArai | 8:3e7a33f81048 | 27 | |
kenjiArai | 8:3e7a33f81048 | 28 | void USBEndpoint::init(HCED *hced_, ENDPOINT_TYPE type_, ENDPOINT_DIRECTION dir_, uint32_t size, uint8_t ep_number, HCTD *td_list_[2]) |
kenjiArai | 8:3e7a33f81048 | 29 | { |
kenjiArai | 8:3e7a33f81048 | 30 | HCD_HandleTypeDef *hhcd; |
kenjiArai | 8:3e7a33f81048 | 31 | uint32_t *addr; |
kenjiArai | 8:3e7a33f81048 | 32 | |
kenjiArai | 8:3e7a33f81048 | 33 | hced = hced_; |
kenjiArai | 8:3e7a33f81048 | 34 | type = type_; |
kenjiArai | 8:3e7a33f81048 | 35 | dir = dir_; |
kenjiArai | 8:3e7a33f81048 | 36 | setup = (type == CONTROL_ENDPOINT) ? true : false; |
kenjiArai | 8:3e7a33f81048 | 37 | |
kenjiArai | 8:3e7a33f81048 | 38 | //TDs have been allocated by the host |
kenjiArai | 8:3e7a33f81048 | 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 | 8:3e7a33f81048 | 40 | memset(td_list_[0], 0, sizeof(HCTD)); |
kenjiArai | 8:3e7a33f81048 | 41 | memset(td_list_[1], 0, sizeof(HCTD)); |
kenjiArai | 8:3e7a33f81048 | 42 | |
kenjiArai | 8:3e7a33f81048 | 43 | td_list[0]->ep = this; |
kenjiArai | 8:3e7a33f81048 | 44 | td_list[1]->ep = this; |
kenjiArai | 8:3e7a33f81048 | 45 | |
kenjiArai | 8:3e7a33f81048 | 46 | address = (ep_number & 0x7F) | ((dir - 1) << 7); |
kenjiArai | 8:3e7a33f81048 | 47 | this->size = size; |
kenjiArai | 8:3e7a33f81048 | 48 | this->ep_number = ep_number; |
kenjiArai | 8:3e7a33f81048 | 49 | transfer_len = 0; |
kenjiArai | 8:3e7a33f81048 | 50 | transferred = 0; |
kenjiArai | 8:3e7a33f81048 | 51 | buf_start = 0; |
kenjiArai | 8:3e7a33f81048 | 52 | nextEp = NULL; |
kenjiArai | 8:3e7a33f81048 | 53 | |
kenjiArai | 8:3e7a33f81048 | 54 | td_current = td_list[0]; |
kenjiArai | 8:3e7a33f81048 | 55 | td_next = td_list[1]; |
kenjiArai | 8:3e7a33f81048 | 56 | /* remove potential post pending from previous endpoint */ |
kenjiArai | 8:3e7a33f81048 | 57 | ep_queue.get(0); |
kenjiArai | 8:3e7a33f81048 | 58 | intf_nb = 0; |
kenjiArai | 8:3e7a33f81048 | 59 | hhcd = (HCD_HandleTypeDef *)hced->hhcd; |
kenjiArai | 8:3e7a33f81048 | 60 | addr = &((uint32_t *)hhcd->pData)[hced->ch_num]; |
kenjiArai | 8:3e7a33f81048 | 61 | *addr = 0; |
kenjiArai | 8:3e7a33f81048 | 62 | state = USB_TYPE_IDLE; |
kenjiArai | 8:3e7a33f81048 | 63 | speed = false; |
kenjiArai | 8:3e7a33f81048 | 64 | } |
kenjiArai | 8:3e7a33f81048 | 65 | void USBEndpoint::setSize(uint32_t size) |
kenjiArai | 8:3e7a33f81048 | 66 | { |
kenjiArai | 8:3e7a33f81048 | 67 | this->size = size; |
kenjiArai | 8:3e7a33f81048 | 68 | } |
kenjiArai | 8:3e7a33f81048 | 69 | |
kenjiArai | 8:3e7a33f81048 | 70 | |
kenjiArai | 8:3e7a33f81048 | 71 | void USBEndpoint::setDeviceAddress(uint8_t addr) |
kenjiArai | 8:3e7a33f81048 | 72 | { |
kenjiArai | 8:3e7a33f81048 | 73 | HCD_HandleTypeDef *hhcd; |
kenjiArai | 8:3e7a33f81048 | 74 | uint8_t hcd_speed = HCD_SPEED_FULL; |
kenjiArai | 8:3e7a33f81048 | 75 | /* fix me : small speed device with hub not supported |
kenjiArai | 8:3e7a33f81048 | 76 | if (this->speed) hcd_speed = HCD_SPEED_LOW; */ |
kenjiArai | 8:3e7a33f81048 | 77 | if (this->speed) { |
kenjiArai | 8:3e7a33f81048 | 78 | USB_WARN("small speed device on hub not supported"); |
kenjiArai | 8:3e7a33f81048 | 79 | } |
kenjiArai | 8:3e7a33f81048 | 80 | MBED_ASSERT(HAL_HCD_HC_Init((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num, address, addr, hcd_speed, type, size) != HAL_BUSY); |
kenjiArai | 8:3e7a33f81048 | 81 | this->device_address = addr; |
kenjiArai | 8:3e7a33f81048 | 82 | |
kenjiArai | 8:3e7a33f81048 | 83 | } |
kenjiArai | 8:3e7a33f81048 | 84 | |
kenjiArai | 8:3e7a33f81048 | 85 | void USBEndpoint::setSpeed(uint8_t speed) |
kenjiArai | 8:3e7a33f81048 | 86 | { |
kenjiArai | 8:3e7a33f81048 | 87 | this->speed = speed; |
kenjiArai | 8:3e7a33f81048 | 88 | } |
kenjiArai | 8:3e7a33f81048 | 89 | |
kenjiArai | 8:3e7a33f81048 | 90 | |
kenjiArai | 8:3e7a33f81048 | 91 | |
kenjiArai | 8:3e7a33f81048 | 92 | void USBEndpoint::setState(USB_TYPE st) |
kenjiArai | 8:3e7a33f81048 | 93 | { |
kenjiArai | 8:3e7a33f81048 | 94 | /* modify this state is possible only with a plug */ |
kenjiArai | 8:3e7a33f81048 | 95 | if (state == USB_TYPE_FREE) { |
kenjiArai | 8:3e7a33f81048 | 96 | return; |
kenjiArai | 8:3e7a33f81048 | 97 | } |
kenjiArai | 8:3e7a33f81048 | 98 | |
kenjiArai | 8:3e7a33f81048 | 99 | state = st; |
kenjiArai | 8:3e7a33f81048 | 100 | if (st == USB_TYPE_FREE) { |
kenjiArai | 8:3e7a33f81048 | 101 | HCD_HandleTypeDef *hhcd = (HCD_HandleTypeDef *)hced->hhcd; |
kenjiArai | 8:3e7a33f81048 | 102 | uint32_t *addr = &((uint32_t *)hhcd->pData)[hced->ch_num]; |
kenjiArai | 8:3e7a33f81048 | 103 | if ((*addr) && (type != INTERRUPT_ENDPOINT)) { |
kenjiArai | 8:3e7a33f81048 | 104 | this->ep_queue.put((uint8_t *)1); |
kenjiArai | 8:3e7a33f81048 | 105 | } |
kenjiArai | 8:3e7a33f81048 | 106 | MBED_ASSERT(HAL_HCD_HC_Halt((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num) != HAL_BUSY); |
kenjiArai | 8:3e7a33f81048 | 107 | HAL_HCD_DisableInt((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num); |
kenjiArai | 8:3e7a33f81048 | 108 | *addr = 0; |
kenjiArai | 8:3e7a33f81048 | 109 | |
kenjiArai | 8:3e7a33f81048 | 110 | } |
kenjiArai | 8:3e7a33f81048 | 111 | if (st == USB_TYPE_ERROR) { |
kenjiArai | 8:3e7a33f81048 | 112 | MBED_ASSERT(HAL_HCD_HC_Halt((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num) != HAL_BUSY); |
kenjiArai | 8:3e7a33f81048 | 113 | HAL_HCD_DisableInt((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num); |
kenjiArai | 8:3e7a33f81048 | 114 | |
kenjiArai | 8:3e7a33f81048 | 115 | } |
kenjiArai | 8:3e7a33f81048 | 116 | if (st == USB_TYPE_ERROR) { |
kenjiArai | 8:3e7a33f81048 | 117 | uint8_t hcd_speed = HCD_SPEED_FULL; |
kenjiArai | 8:3e7a33f81048 | 118 | /* small speed device with hub not supported |
kenjiArai | 8:3e7a33f81048 | 119 | if (this->speed) hcd_speed = HCD_SPEED_LOW;*/ |
kenjiArai | 8:3e7a33f81048 | 120 | MBED_ASSERT(HAL_HCD_HC_Init((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num, address, 0, hcd_speed, type, size) != HAL_BUSY); |
kenjiArai | 8:3e7a33f81048 | 121 | } |
kenjiArai | 8:3e7a33f81048 | 122 | } |
kenjiArai | 8:3e7a33f81048 | 123 | |
kenjiArai | 8:3e7a33f81048 | 124 | |
kenjiArai | 8:3e7a33f81048 | 125 | extern uint32_t HAL_HCD_HC_GetMaxPacket(HCD_HandleTypeDef *hhcd, uint8_t chn_num); |
kenjiArai | 8:3e7a33f81048 | 126 | extern uint32_t HAL_HCD_HC_GetType(HCD_HandleTypeDef *hhcd, uint8_t chn_num); |
kenjiArai | 8:3e7a33f81048 | 127 | |
kenjiArai | 8:3e7a33f81048 | 128 | |
kenjiArai | 8:3e7a33f81048 | 129 | USB_TYPE USBEndpoint::queueTransfer() |
kenjiArai | 8:3e7a33f81048 | 130 | { |
kenjiArai | 8:3e7a33f81048 | 131 | HCD_HandleTypeDef *hhcd = (HCD_HandleTypeDef *)hced->hhcd; |
kenjiArai | 8:3e7a33f81048 | 132 | uint32_t *addr = &((uint32_t *)hhcd->pData)[hced->ch_num]; |
kenjiArai | 8:3e7a33f81048 | 133 | uint32_t type = HAL_HCD_HC_GetType(hhcd, hced->ch_num); |
kenjiArai | 8:3e7a33f81048 | 134 | uint32_t max_size = HAL_HCD_HC_GetMaxPacket(hhcd, hced->ch_num); |
kenjiArai | 8:3e7a33f81048 | 135 | /* if a packet is queue on disconnected ; no solution for now */ |
kenjiArai | 8:3e7a33f81048 | 136 | if (state == USB_TYPE_FREE) { |
kenjiArai | 8:3e7a33f81048 | 137 | td_current->state = USB_TYPE_FREE; |
kenjiArai | 8:3e7a33f81048 | 138 | return USB_TYPE_FREE; |
kenjiArai | 8:3e7a33f81048 | 139 | } |
kenjiArai | 8:3e7a33f81048 | 140 | ep_queue.get(0); |
kenjiArai | 8:3e7a33f81048 | 141 | MBED_ASSERT(*addr == 0); |
kenjiArai | 8:3e7a33f81048 | 142 | transfer_len = td_current->size <= max_size ? td_current->size : max_size; |
kenjiArai | 8:3e7a33f81048 | 143 | buf_start = (uint8_t *)td_current->currBufPtr; |
kenjiArai | 8:3e7a33f81048 | 144 | |
kenjiArai | 8:3e7a33f81048 | 145 | //Now add this free TD at this end of the queue |
kenjiArai | 8:3e7a33f81048 | 146 | state = USB_TYPE_PROCESSING; |
kenjiArai | 8:3e7a33f81048 | 147 | /* one request */ |
kenjiArai | 8:3e7a33f81048 | 148 | td_current->nextTD = (hcTd *)0; |
kenjiArai | 8:3e7a33f81048 | 149 | #if defined(MAX_NYET_RETRY) |
kenjiArai | 8:3e7a33f81048 | 150 | td_current->retry = 0; |
kenjiArai | 8:3e7a33f81048 | 151 | #endif |
kenjiArai | 8:3e7a33f81048 | 152 | td_current->setup = setup; |
kenjiArai | 8:3e7a33f81048 | 153 | *addr = (uint32_t)td_current; |
kenjiArai | 8:3e7a33f81048 | 154 | /* dir /setup is inverted for ST */ |
kenjiArai | 8:3e7a33f81048 | 155 | /* token is useful only ctrl endpoint */ |
kenjiArai | 8:3e7a33f81048 | 156 | /* last parameter is ping ? */ |
kenjiArai | 8:3e7a33f81048 | 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 | 8:3e7a33f81048 | 158 | HAL_HCD_EnableInt((HCD_HandleTypeDef *)hced->hhcd, hced->ch_num); |
kenjiArai | 8:3e7a33f81048 | 159 | |
kenjiArai | 8:3e7a33f81048 | 160 | return USB_TYPE_PROCESSING; |
kenjiArai | 8:3e7a33f81048 | 161 | } |
kenjiArai | 8:3e7a33f81048 | 162 | |
kenjiArai | 8:3e7a33f81048 | 163 | void USBEndpoint::unqueueTransfer(volatile HCTD *td) |
kenjiArai | 8:3e7a33f81048 | 164 | { |
kenjiArai | 8:3e7a33f81048 | 165 | if (state == USB_TYPE_FREE) { |
kenjiArai | 8:3e7a33f81048 | 166 | return; |
kenjiArai | 8:3e7a33f81048 | 167 | } |
kenjiArai | 8:3e7a33f81048 | 168 | uint32_t *addr = &((uint32_t *)((HCD_HandleTypeDef *)hced->hhcd)->pData)[hced->ch_num]; |
kenjiArai | 8:3e7a33f81048 | 169 | td->state = 0; |
kenjiArai | 8:3e7a33f81048 | 170 | td->currBufPtr = 0; |
kenjiArai | 8:3e7a33f81048 | 171 | td->size = 0; |
kenjiArai | 8:3e7a33f81048 | 172 | td->nextTD = 0; |
kenjiArai | 8:3e7a33f81048 | 173 | *addr = 0; |
kenjiArai | 8:3e7a33f81048 | 174 | td_current = td_next; |
kenjiArai | 8:3e7a33f81048 | 175 | td_next = td; |
kenjiArai | 8:3e7a33f81048 | 176 | } |
kenjiArai | 8:3e7a33f81048 | 177 | |
kenjiArai | 8:3e7a33f81048 | 178 | void USBEndpoint::queueEndpoint(USBEndpoint *ed) |
kenjiArai | 8:3e7a33f81048 | 179 | { |
kenjiArai | 8:3e7a33f81048 | 180 | nextEp = ed; |
kenjiArai | 8:3e7a33f81048 | 181 | } |
kenjiArai | 8:3e7a33f81048 | 182 | #endif |