Bmag incl gps rettelse

Dependencies:   mbed WDT MODSERIAL BME280

Committer:
gert_lauritsen
Date:
Thu Jun 20 07:13:18 2019 +0000
Revision:
56:df9052e3808c
change date stamp from gps, reads serialnumb from file.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gert_lauritsen 56:df9052e3808c 1 /* mbed USBHost Library
gert_lauritsen 56:df9052e3808c 2 * Copyright (c) 2006-2013 ARM Limited
gert_lauritsen 56:df9052e3808c 3 *
gert_lauritsen 56:df9052e3808c 4 * Licensed under the Apache License, Version 2.0 (the "License");
gert_lauritsen 56:df9052e3808c 5 * you may not use this file except in compliance with the License.
gert_lauritsen 56:df9052e3808c 6 * You may obtain a copy of the License at
gert_lauritsen 56:df9052e3808c 7 *
gert_lauritsen 56:df9052e3808c 8 * http://www.apache.org/licenses/LICENSE-2.0
gert_lauritsen 56:df9052e3808c 9 *
gert_lauritsen 56:df9052e3808c 10 * Unless required by applicable law or agreed to in writing, software
gert_lauritsen 56:df9052e3808c 11 * distributed under the License is distributed on an "AS IS" BASIS,
gert_lauritsen 56:df9052e3808c 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
gert_lauritsen 56:df9052e3808c 13 * See the License for the specific language governing permissions and
gert_lauritsen 56:df9052e3808c 14 * limitations under the License.
gert_lauritsen 56:df9052e3808c 15 */
gert_lauritsen 56:df9052e3808c 16
gert_lauritsen 56:df9052e3808c 17 #include "USBHostHub.h"
gert_lauritsen 56:df9052e3808c 18
gert_lauritsen 56:df9052e3808c 19 #if MAX_HUB_NB
gert_lauritsen 56:df9052e3808c 20
gert_lauritsen 56:df9052e3808c 21 #include "USBHost.h"
gert_lauritsen 56:df9052e3808c 22 #include "dbg.h"
gert_lauritsen 56:df9052e3808c 23
gert_lauritsen 56:df9052e3808c 24 #define GET_STATUS 0x00
gert_lauritsen 56:df9052e3808c 25 #define CLEAR_FEATURE 0x01
gert_lauritsen 56:df9052e3808c 26 #define GET_STATE 0x02
gert_lauritsen 56:df9052e3808c 27 #define SET_FEATURE 0x03
gert_lauritsen 56:df9052e3808c 28 #define GET_DESCRIPTOR 0x06
gert_lauritsen 56:df9052e3808c 29
gert_lauritsen 56:df9052e3808c 30 #define PORT_CONNECTION_FEATURE (0x00)
gert_lauritsen 56:df9052e3808c 31 #define PORT_ENABLE_FEATURE (0x01)
gert_lauritsen 56:df9052e3808c 32 #define PORT_RESET_FEATURE (0x04)
gert_lauritsen 56:df9052e3808c 33 #define PORT_POWER_FEATURE (0x08)
gert_lauritsen 56:df9052e3808c 34
gert_lauritsen 56:df9052e3808c 35 #define C_PORT_CONNECTION_FEATURE (16)
gert_lauritsen 56:df9052e3808c 36 #define C_PORT_ENABLE_FEATURE (17)
gert_lauritsen 56:df9052e3808c 37 #define C_PORT_RESET_FEATURE (20)
gert_lauritsen 56:df9052e3808c 38
gert_lauritsen 56:df9052e3808c 39 #define PORT_CONNECTION (1 << 0)
gert_lauritsen 56:df9052e3808c 40 #define PORT_ENABLE (1 << 1)
gert_lauritsen 56:df9052e3808c 41 #define PORT_SUSPEND (1 << 2)
gert_lauritsen 56:df9052e3808c 42 #define PORT_OVER_CURRENT (1 << 3)
gert_lauritsen 56:df9052e3808c 43 #define PORT_RESET (1 << 4)
gert_lauritsen 56:df9052e3808c 44 #define PORT_POWER (1 << 8)
gert_lauritsen 56:df9052e3808c 45 #define PORT_LOW_SPEED (1 << 9)
gert_lauritsen 56:df9052e3808c 46
gert_lauritsen 56:df9052e3808c 47 #define C_PORT_CONNECTION (1 << 16)
gert_lauritsen 56:df9052e3808c 48 #define C_PORT_ENABLE (1 << 17)
gert_lauritsen 56:df9052e3808c 49 #define C_PORT_SUSPEND (1 << 18)
gert_lauritsen 56:df9052e3808c 50 #define C_PORT_OVER_CURRENT (1 << 19)
gert_lauritsen 56:df9052e3808c 51 #define C_PORT_RESET (1 << 20)
gert_lauritsen 56:df9052e3808c 52
gert_lauritsen 56:df9052e3808c 53 USBHostHub::USBHostHub() {
gert_lauritsen 56:df9052e3808c 54 host = NULL;
gert_lauritsen 56:df9052e3808c 55 init();
gert_lauritsen 56:df9052e3808c 56 }
gert_lauritsen 56:df9052e3808c 57
gert_lauritsen 56:df9052e3808c 58 void USBHostHub::init() {
gert_lauritsen 56:df9052e3808c 59 dev_connected = false;
gert_lauritsen 56:df9052e3808c 60 dev = NULL;
gert_lauritsen 56:df9052e3808c 61 int_in = NULL;
gert_lauritsen 56:df9052e3808c 62 dev_connected = false;
gert_lauritsen 56:df9052e3808c 63 hub_intf = -1;
gert_lauritsen 56:df9052e3808c 64 hub_device_found = false;
gert_lauritsen 56:df9052e3808c 65 nb_port = 0;
gert_lauritsen 56:df9052e3808c 66 hub_characteristics = 0;
gert_lauritsen 56:df9052e3808c 67
gert_lauritsen 56:df9052e3808c 68 for (int i = 0; i < MAX_HUB_PORT; i++) {
gert_lauritsen 56:df9052e3808c 69 device_children[i] = NULL;
gert_lauritsen 56:df9052e3808c 70 }
gert_lauritsen 56:df9052e3808c 71 }
gert_lauritsen 56:df9052e3808c 72
gert_lauritsen 56:df9052e3808c 73 void USBHostHub::setHost(USBHost * host_) {
gert_lauritsen 56:df9052e3808c 74 host = host_;
gert_lauritsen 56:df9052e3808c 75 }
gert_lauritsen 56:df9052e3808c 76
gert_lauritsen 56:df9052e3808c 77 bool USBHostHub::connected()
gert_lauritsen 56:df9052e3808c 78 {
gert_lauritsen 56:df9052e3808c 79 return dev_connected;
gert_lauritsen 56:df9052e3808c 80 }
gert_lauritsen 56:df9052e3808c 81
gert_lauritsen 56:df9052e3808c 82 bool USBHostHub::connect(USBDeviceConnected * dev)
gert_lauritsen 56:df9052e3808c 83 {
gert_lauritsen 56:df9052e3808c 84 if (dev_connected) {
gert_lauritsen 56:df9052e3808c 85 return true;
gert_lauritsen 56:df9052e3808c 86 }
gert_lauritsen 56:df9052e3808c 87
gert_lauritsen 56:df9052e3808c 88 if(host->enumerate(dev, this)) {
gert_lauritsen 56:df9052e3808c 89 init();
gert_lauritsen 56:df9052e3808c 90 return false;
gert_lauritsen 56:df9052e3808c 91 }
gert_lauritsen 56:df9052e3808c 92
gert_lauritsen 56:df9052e3808c 93 if (hub_device_found) {
gert_lauritsen 56:df9052e3808c 94 this->dev = dev;
gert_lauritsen 56:df9052e3808c 95
gert_lauritsen 56:df9052e3808c 96 int_in = dev->getEndpoint(hub_intf, INTERRUPT_ENDPOINT, IN);
gert_lauritsen 56:df9052e3808c 97
gert_lauritsen 56:df9052e3808c 98 if (!int_in) {
gert_lauritsen 56:df9052e3808c 99 init();
gert_lauritsen 56:df9052e3808c 100 return false;
gert_lauritsen 56:df9052e3808c 101 }
gert_lauritsen 56:df9052e3808c 102
gert_lauritsen 56:df9052e3808c 103 USB_INFO("New HUB: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, hub_intf);
gert_lauritsen 56:df9052e3808c 104 dev->setName("Hub", hub_intf);
gert_lauritsen 56:df9052e3808c 105 host->registerDriver(dev, hub_intf, this, &USBHostHub::disconnect);
gert_lauritsen 56:df9052e3808c 106
gert_lauritsen 56:df9052e3808c 107 int_in->attach(this, &USBHostHub::rxHandler);
gert_lauritsen 56:df9052e3808c 108
gert_lauritsen 56:df9052e3808c 109 // get HUB descriptor
gert_lauritsen 56:df9052e3808c 110 host->controlRead( dev,
gert_lauritsen 56:df9052e3808c 111 USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS,
gert_lauritsen 56:df9052e3808c 112 GET_DESCRIPTOR,
gert_lauritsen 56:df9052e3808c 113 0x29 << 8, 0, buf, sizeof(HubDescriptor));
gert_lauritsen 56:df9052e3808c 114 nb_port = buf[2];
gert_lauritsen 56:df9052e3808c 115 hub_characteristics = buf[3];
gert_lauritsen 56:df9052e3808c 116
gert_lauritsen 56:df9052e3808c 117 USB_DBG("Hub has %d port", nb_port);
gert_lauritsen 56:df9052e3808c 118
gert_lauritsen 56:df9052e3808c 119 for (uint8_t j = 1; j <= nb_port; j++) {
gert_lauritsen 56:df9052e3808c 120 setPortFeature(PORT_POWER_FEATURE, j);
gert_lauritsen 56:df9052e3808c 121 }
gert_lauritsen 56:df9052e3808c 122 wait_ms(buf[5]*2);
gert_lauritsen 56:df9052e3808c 123
gert_lauritsen 56:df9052e3808c 124 host->interruptRead(dev, int_in, buf, 1, false);
gert_lauritsen 56:df9052e3808c 125 dev_connected = true;
gert_lauritsen 56:df9052e3808c 126 return true;
gert_lauritsen 56:df9052e3808c 127 }
gert_lauritsen 56:df9052e3808c 128
gert_lauritsen 56:df9052e3808c 129 return false;
gert_lauritsen 56:df9052e3808c 130 }
gert_lauritsen 56:df9052e3808c 131
gert_lauritsen 56:df9052e3808c 132 void USBHostHub::disconnect() {
gert_lauritsen 56:df9052e3808c 133 init();
gert_lauritsen 56:df9052e3808c 134 }
gert_lauritsen 56:df9052e3808c 135
gert_lauritsen 56:df9052e3808c 136 /*virtual*/ void USBHostHub::setVidPid(uint16_t vid, uint16_t pid)
gert_lauritsen 56:df9052e3808c 137 {
gert_lauritsen 56:df9052e3808c 138 // we don't check VID/PID for MSD driver
gert_lauritsen 56:df9052e3808c 139 }
gert_lauritsen 56:df9052e3808c 140
gert_lauritsen 56:df9052e3808c 141 /*virtual*/ bool USBHostHub::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed
gert_lauritsen 56:df9052e3808c 142 {
gert_lauritsen 56:df9052e3808c 143 if ((hub_intf == -1) &&
gert_lauritsen 56:df9052e3808c 144 (intf_class == HUB_CLASS) &&
gert_lauritsen 56:df9052e3808c 145 (intf_subclass == 0) &&
gert_lauritsen 56:df9052e3808c 146 (intf_protocol == 0)) {
gert_lauritsen 56:df9052e3808c 147 hub_intf = intf_nb;
gert_lauritsen 56:df9052e3808c 148 return true;
gert_lauritsen 56:df9052e3808c 149 }
gert_lauritsen 56:df9052e3808c 150 return false;
gert_lauritsen 56:df9052e3808c 151 }
gert_lauritsen 56:df9052e3808c 152
gert_lauritsen 56:df9052e3808c 153 /*virtual*/ bool USBHostHub::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
gert_lauritsen 56:df9052e3808c 154 {
gert_lauritsen 56:df9052e3808c 155 if (intf_nb == hub_intf) {
gert_lauritsen 56:df9052e3808c 156 if ((type == INTERRUPT_ENDPOINT) && (dir == IN)) {
gert_lauritsen 56:df9052e3808c 157 hub_device_found = true;
gert_lauritsen 56:df9052e3808c 158 return true;
gert_lauritsen 56:df9052e3808c 159 }
gert_lauritsen 56:df9052e3808c 160 }
gert_lauritsen 56:df9052e3808c 161 return false;
gert_lauritsen 56:df9052e3808c 162 }
gert_lauritsen 56:df9052e3808c 163
gert_lauritsen 56:df9052e3808c 164 void USBHostHub::deviceConnected(USBDeviceConnected * dev) {
gert_lauritsen 56:df9052e3808c 165 device_children[dev->getPort() - 1] = dev;
gert_lauritsen 56:df9052e3808c 166 }
gert_lauritsen 56:df9052e3808c 167
gert_lauritsen 56:df9052e3808c 168 void USBHostHub::deviceDisconnected(USBDeviceConnected * dev) {
gert_lauritsen 56:df9052e3808c 169 device_children[dev->getPort() - 1] = NULL;
gert_lauritsen 56:df9052e3808c 170 }
gert_lauritsen 56:df9052e3808c 171
gert_lauritsen 56:df9052e3808c 172 void USBHostHub::hubDisconnected() {
gert_lauritsen 56:df9052e3808c 173 for (uint8_t i = 0; i < MAX_HUB_PORT; i++) {
gert_lauritsen 56:df9052e3808c 174 if (device_children[i] != NULL) {
gert_lauritsen 56:df9052e3808c 175 host->freeDevice(device_children[i]);
gert_lauritsen 56:df9052e3808c 176 }
gert_lauritsen 56:df9052e3808c 177 }
gert_lauritsen 56:df9052e3808c 178 }
gert_lauritsen 56:df9052e3808c 179
gert_lauritsen 56:df9052e3808c 180 void USBHostHub::rxHandler() {
gert_lauritsen 56:df9052e3808c 181 uint32_t status;
gert_lauritsen 56:df9052e3808c 182 if (int_in) {
gert_lauritsen 56:df9052e3808c 183 if (int_in->getState() == USB_TYPE_IDLE) {
gert_lauritsen 56:df9052e3808c 184 for (int port = 1; port <= nb_port; port++) {
gert_lauritsen 56:df9052e3808c 185 status = getPortStatus(port);
gert_lauritsen 56:df9052e3808c 186 USB_DBG("[hub handler hub: %d] status port %d [hub: %p]: 0x%X", dev->getHub(), port, dev, status);
gert_lauritsen 56:df9052e3808c 187
gert_lauritsen 56:df9052e3808c 188 // if connection status has changed
gert_lauritsen 56:df9052e3808c 189 if (status & C_PORT_CONNECTION) {
gert_lauritsen 56:df9052e3808c 190 if (status & PORT_CONNECTION) {
gert_lauritsen 56:df9052e3808c 191 USB_DBG("[hub handler hub: %d - port: %d] new device connected", dev->getHub(), port);
gert_lauritsen 56:df9052e3808c 192 host->deviceConnected(dev->getHub() + 1, port, status & PORT_LOW_SPEED, this);
gert_lauritsen 56:df9052e3808c 193 } else {
gert_lauritsen 56:df9052e3808c 194 USB_DBG("[hub handler hub: %d - port: %d] device disconnected", dev->getHub(), port);
gert_lauritsen 56:df9052e3808c 195 host->deviceDisconnected(dev->getHub() + 1, port, this, 0);
gert_lauritsen 56:df9052e3808c 196 }
gert_lauritsen 56:df9052e3808c 197
gert_lauritsen 56:df9052e3808c 198 clearPortFeature(C_PORT_CONNECTION_FEATURE, port);
gert_lauritsen 56:df9052e3808c 199 }
gert_lauritsen 56:df9052e3808c 200
gert_lauritsen 56:df9052e3808c 201 if (status & C_PORT_RESET) {
gert_lauritsen 56:df9052e3808c 202 clearPortFeature(C_PORT_RESET_FEATURE, port);
gert_lauritsen 56:df9052e3808c 203 }
gert_lauritsen 56:df9052e3808c 204
gert_lauritsen 56:df9052e3808c 205 if (status & C_PORT_ENABLE) {
gert_lauritsen 56:df9052e3808c 206 clearPortFeature(C_PORT_ENABLE_FEATURE, port);
gert_lauritsen 56:df9052e3808c 207 }
gert_lauritsen 56:df9052e3808c 208
gert_lauritsen 56:df9052e3808c 209 if ((status & PORT_OVER_CURRENT)) {
gert_lauritsen 56:df9052e3808c 210 USB_ERR("OVER CURRENT DETECTED\r\n");
gert_lauritsen 56:df9052e3808c 211 clearPortFeature(PORT_OVER_CURRENT, port);
gert_lauritsen 56:df9052e3808c 212 host->deviceDisconnected(dev->getHub() + 1, port, this, 0);
gert_lauritsen 56:df9052e3808c 213 }
gert_lauritsen 56:df9052e3808c 214 }
gert_lauritsen 56:df9052e3808c 215 }
gert_lauritsen 56:df9052e3808c 216 host->interruptRead(dev, int_in, buf, 1, false);
gert_lauritsen 56:df9052e3808c 217 }
gert_lauritsen 56:df9052e3808c 218 }
gert_lauritsen 56:df9052e3808c 219
gert_lauritsen 56:df9052e3808c 220 void USBHostHub::portReset(uint8_t port) {
gert_lauritsen 56:df9052e3808c 221 // reset port
gert_lauritsen 56:df9052e3808c 222 uint32_t status;
gert_lauritsen 56:df9052e3808c 223 USB_DBG("reset port %d on hub: %p [this: %p]", port, dev, this)
gert_lauritsen 56:df9052e3808c 224 setPortFeature(PORT_RESET_FEATURE, port);
gert_lauritsen 56:df9052e3808c 225 #if defined(TARGET_RZ_A1H)
gert_lauritsen 56:df9052e3808c 226 Thread::wait(50); // Reset release waiting for Hi-Speed check.
gert_lauritsen 56:df9052e3808c 227 #endif
gert_lauritsen 56:df9052e3808c 228 while(1) {
gert_lauritsen 56:df9052e3808c 229 status = getPortStatus(port);
gert_lauritsen 56:df9052e3808c 230 if (status & (PORT_ENABLE | PORT_RESET))
gert_lauritsen 56:df9052e3808c 231 break;
gert_lauritsen 56:df9052e3808c 232 if (status & PORT_OVER_CURRENT) {
gert_lauritsen 56:df9052e3808c 233 USB_ERR("OVER CURRENT DETECTED\r\n");
gert_lauritsen 56:df9052e3808c 234 clearPortFeature(PORT_OVER_CURRENT, port);
gert_lauritsen 56:df9052e3808c 235 host->deviceDisconnected(dev->getHub() + 1, port, this, 0);
gert_lauritsen 56:df9052e3808c 236 break;
gert_lauritsen 56:df9052e3808c 237 }
gert_lauritsen 56:df9052e3808c 238 Thread::wait(10);
gert_lauritsen 56:df9052e3808c 239 }
gert_lauritsen 56:df9052e3808c 240 }
gert_lauritsen 56:df9052e3808c 241
gert_lauritsen 56:df9052e3808c 242 void USBHostHub::setPortFeature(uint32_t feature, uint8_t port) {
gert_lauritsen 56:df9052e3808c 243 host->controlWrite( dev,
gert_lauritsen 56:df9052e3808c 244 USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT,
gert_lauritsen 56:df9052e3808c 245 SET_FEATURE,
gert_lauritsen 56:df9052e3808c 246 feature,
gert_lauritsen 56:df9052e3808c 247 port,
gert_lauritsen 56:df9052e3808c 248 NULL,
gert_lauritsen 56:df9052e3808c 249 0);
gert_lauritsen 56:df9052e3808c 250 }
gert_lauritsen 56:df9052e3808c 251
gert_lauritsen 56:df9052e3808c 252 void USBHostHub::clearPortFeature(uint32_t feature, uint8_t port) {
gert_lauritsen 56:df9052e3808c 253 host->controlWrite( dev,
gert_lauritsen 56:df9052e3808c 254 USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT,
gert_lauritsen 56:df9052e3808c 255 CLEAR_FEATURE,
gert_lauritsen 56:df9052e3808c 256 feature,
gert_lauritsen 56:df9052e3808c 257 port,
gert_lauritsen 56:df9052e3808c 258 NULL,
gert_lauritsen 56:df9052e3808c 259 0);
gert_lauritsen 56:df9052e3808c 260 }
gert_lauritsen 56:df9052e3808c 261
gert_lauritsen 56:df9052e3808c 262 uint32_t USBHostHub::getPortStatus(uint8_t port) {
gert_lauritsen 56:df9052e3808c 263 uint32_t st;
gert_lauritsen 56:df9052e3808c 264 host->controlRead( dev,
gert_lauritsen 56:df9052e3808c 265 USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT,
gert_lauritsen 56:df9052e3808c 266 GET_STATUS,
gert_lauritsen 56:df9052e3808c 267 0,
gert_lauritsen 56:df9052e3808c 268 port,
gert_lauritsen 56:df9052e3808c 269 (uint8_t *)&st,
gert_lauritsen 56:df9052e3808c 270 4);
gert_lauritsen 56:df9052e3808c 271 return st;
gert_lauritsen 56:df9052e3808c 272 }
gert_lauritsen 56:df9052e3808c 273
gert_lauritsen 56:df9052e3808c 274 #endif