Running multiple threads on mbed using RTOS

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player_appbd

Committer:
wschon
Date:
Sun Feb 28 22:40:13 2016 +0000
Revision:
1:2129bb91c172
Added USB libraries;

Who changed what in which revision?

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