Sample program for the USB Host lib with HID

Dependencies:   USBHost_DISCO-F746NG mbed

Committer:
DieterGraef
Date:
Fri Jun 17 09:01:37 2016 +0000
Revision:
2:ca1b5b911ba8
Parent:
0:af2040964256
Demo program now uses USB Stick on high speed and HID on fast speed interface. Move your mouse!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DieterGraef 0:af2040964256 1 /* mbed Microcontroller Library
DieterGraef 0:af2040964256 2 * Copyright (c) 2006-2012 ARM Limited
DieterGraef 0:af2040964256 3 *
DieterGraef 0:af2040964256 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
DieterGraef 0:af2040964256 5 * of this software and associated documentation files (the "Software"), to deal
DieterGraef 0:af2040964256 6 * in the Software without restriction, including without limitation the rights
DieterGraef 0:af2040964256 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
DieterGraef 0:af2040964256 8 * copies of the Software, and to permit persons to whom the Software is
DieterGraef 0:af2040964256 9 * furnished to do so, subject to the following conditions:
DieterGraef 0:af2040964256 10 *
DieterGraef 0:af2040964256 11 * The above copyright notice and this permission notice shall be included in
DieterGraef 0:af2040964256 12 * all copies or substantial portions of the Software.
DieterGraef 0:af2040964256 13 *
DieterGraef 0:af2040964256 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
DieterGraef 0:af2040964256 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
DieterGraef 0:af2040964256 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
DieterGraef 0:af2040964256 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
DieterGraef 0:af2040964256 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
DieterGraef 0:af2040964256 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
DieterGraef 0:af2040964256 20 * SOFTWARE.
DieterGraef 0:af2040964256 21 */
DieterGraef 0:af2040964256 22 #include "Thread.h"
DieterGraef 0:af2040964256 23
DieterGraef 0:af2040964256 24 #include "mbed_error.h"
DieterGraef 0:af2040964256 25 #include "rtos_idle.h"
DieterGraef 0:af2040964256 26
DieterGraef 0:af2040964256 27 // rt_tid2ptcb is an internal function which we exposed to get TCB for thread id
DieterGraef 0:af2040964256 28 #undef NULL //Workaround for conflicting macros in rt_TypeDef.h and stdio.h
DieterGraef 0:af2040964256 29 #include "rt_TypeDef.h"
DieterGraef 0:af2040964256 30
DieterGraef 0:af2040964256 31 extern "C" P_TCB rt_tid2ptcb(osThreadId thread_id);
DieterGraef 0:af2040964256 32
DieterGraef 0:af2040964256 33 namespace rtos {
DieterGraef 0:af2040964256 34
DieterGraef 0:af2040964256 35 Thread::Thread(void (*task)(void const *argument), void *argument,
DieterGraef 0:af2040964256 36 osPriority priority, uint32_t stack_size, unsigned char *stack_pointer) {
DieterGraef 0:af2040964256 37 #if defined(__MBED_CMSIS_RTOS_CA9) || defined(__MBED_CMSIS_RTOS_CM)
DieterGraef 0:af2040964256 38 _thread_def.pthread = task;
DieterGraef 0:af2040964256 39 _thread_def.tpriority = priority;
DieterGraef 0:af2040964256 40 _thread_def.stacksize = stack_size;
DieterGraef 0:af2040964256 41 if (stack_pointer != NULL) {
DieterGraef 0:af2040964256 42 _thread_def.stack_pointer = (uint32_t*)stack_pointer;
DieterGraef 0:af2040964256 43 _dynamic_stack = false;
DieterGraef 0:af2040964256 44 } else {
DieterGraef 0:af2040964256 45 _thread_def.stack_pointer = new uint32_t[stack_size/sizeof(uint32_t)];
DieterGraef 0:af2040964256 46 if (_thread_def.stack_pointer == NULL)
DieterGraef 0:af2040964256 47 error("Error allocating the stack memory\n");
DieterGraef 0:af2040964256 48 _dynamic_stack = true;
DieterGraef 0:af2040964256 49 }
DieterGraef 0:af2040964256 50
DieterGraef 0:af2040964256 51 //Fill the stack with a magic word for maximum usage checking
DieterGraef 0:af2040964256 52 for (uint32_t i = 0; i < (stack_size / sizeof(uint32_t)); i++) {
DieterGraef 0:af2040964256 53 _thread_def.stack_pointer[i] = 0xE25A2EA5;
DieterGraef 0:af2040964256 54 }
DieterGraef 0:af2040964256 55 #endif
DieterGraef 0:af2040964256 56 _tid = osThreadCreate(&_thread_def, argument);
DieterGraef 0:af2040964256 57 }
DieterGraef 0:af2040964256 58
DieterGraef 0:af2040964256 59 osStatus Thread::terminate() {
DieterGraef 0:af2040964256 60 return osThreadTerminate(_tid);
DieterGraef 0:af2040964256 61 }
DieterGraef 0:af2040964256 62
DieterGraef 0:af2040964256 63 osStatus Thread::set_priority(osPriority priority) {
DieterGraef 0:af2040964256 64 return osThreadSetPriority(_tid, priority);
DieterGraef 0:af2040964256 65 }
DieterGraef 0:af2040964256 66
DieterGraef 0:af2040964256 67 osPriority Thread::get_priority() {
DieterGraef 0:af2040964256 68 return osThreadGetPriority(_tid);
DieterGraef 0:af2040964256 69 }
DieterGraef 0:af2040964256 70
DieterGraef 0:af2040964256 71 int32_t Thread::signal_set(int32_t signals) {
DieterGraef 0:af2040964256 72 return osSignalSet(_tid, signals);
DieterGraef 0:af2040964256 73 }
DieterGraef 0:af2040964256 74
DieterGraef 0:af2040964256 75 int32_t Thread::signal_clr(int32_t signals) {
DieterGraef 0:af2040964256 76 return osSignalClear(_tid, signals);
DieterGraef 0:af2040964256 77 }
DieterGraef 0:af2040964256 78
DieterGraef 0:af2040964256 79 Thread::State Thread::get_state() {
DieterGraef 0:af2040964256 80 #if !defined(__MBED_CMSIS_RTOS_CA9) && !defined(__MBED_CMSIS_RTOS_CM)
DieterGraef 0:af2040964256 81 #ifdef CMSIS_OS_RTX
DieterGraef 0:af2040964256 82 return ((State)_thread_def.tcb.state);
DieterGraef 0:af2040964256 83 #endif
DieterGraef 0:af2040964256 84 #else
DieterGraef 0:af2040964256 85 uint8_t status;
DieterGraef 0:af2040964256 86 status = osThreadGetState(_tid);
DieterGraef 0:af2040964256 87 return ((State)status);
DieterGraef 0:af2040964256 88 #endif
DieterGraef 0:af2040964256 89 }
DieterGraef 0:af2040964256 90
DieterGraef 0:af2040964256 91 uint32_t Thread::stack_size() {
DieterGraef 0:af2040964256 92 #ifndef __MBED_CMSIS_RTOS_CA9
DieterGraef 0:af2040964256 93 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
DieterGraef 0:af2040964256 94 return _thread_def.tcb.priv_stack;
DieterGraef 0:af2040964256 95 #else
DieterGraef 0:af2040964256 96 P_TCB tcb = rt_tid2ptcb(_tid);
DieterGraef 0:af2040964256 97 return tcb->priv_stack;
DieterGraef 0:af2040964256 98 #endif
DieterGraef 0:af2040964256 99 #else
DieterGraef 0:af2040964256 100 return 0;
DieterGraef 0:af2040964256 101 #endif
DieterGraef 0:af2040964256 102 }
DieterGraef 0:af2040964256 103
DieterGraef 0:af2040964256 104 uint32_t Thread::free_stack() {
DieterGraef 0:af2040964256 105 #ifndef __MBED_CMSIS_RTOS_CA9
DieterGraef 0:af2040964256 106 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
DieterGraef 0:af2040964256 107 uint32_t bottom = (uint32_t)_thread_def.tcb.stack;
DieterGraef 0:af2040964256 108 return _thread_def.tcb.tsk_stack - bottom;
DieterGraef 0:af2040964256 109 #else
DieterGraef 0:af2040964256 110 P_TCB tcb = rt_tid2ptcb(_tid);
DieterGraef 0:af2040964256 111 uint32_t bottom = (uint32_t)tcb->stack;
DieterGraef 0:af2040964256 112 return tcb->tsk_stack - bottom;
DieterGraef 0:af2040964256 113 #endif
DieterGraef 0:af2040964256 114 #else
DieterGraef 0:af2040964256 115 return 0;
DieterGraef 0:af2040964256 116 #endif
DieterGraef 0:af2040964256 117 }
DieterGraef 0:af2040964256 118
DieterGraef 0:af2040964256 119 uint32_t Thread::used_stack() {
DieterGraef 0:af2040964256 120 #ifndef __MBED_CMSIS_RTOS_CA9
DieterGraef 0:af2040964256 121 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
DieterGraef 0:af2040964256 122 uint32_t top = (uint32_t)_thread_def.tcb.stack + _thread_def.tcb.priv_stack;
DieterGraef 0:af2040964256 123 return top - _thread_def.tcb.tsk_stack;
DieterGraef 0:af2040964256 124 #else
DieterGraef 0:af2040964256 125 P_TCB tcb = rt_tid2ptcb(_tid);
DieterGraef 0:af2040964256 126 uint32_t top = (uint32_t)tcb->stack + tcb->priv_stack;
DieterGraef 0:af2040964256 127 return top - tcb->tsk_stack;
DieterGraef 0:af2040964256 128 #endif
DieterGraef 0:af2040964256 129 #else
DieterGraef 0:af2040964256 130 return 0;
DieterGraef 0:af2040964256 131 #endif
DieterGraef 0:af2040964256 132 }
DieterGraef 0:af2040964256 133
DieterGraef 0:af2040964256 134 uint32_t Thread::max_stack() {
DieterGraef 0:af2040964256 135 #ifndef __MBED_CMSIS_RTOS_CA9
DieterGraef 0:af2040964256 136 #if defined(CMSIS_OS_RTX) && !defined(__MBED_CMSIS_RTOS_CM)
DieterGraef 0:af2040964256 137 uint32_t high_mark = 0;
DieterGraef 0:af2040964256 138 while (_thread_def.tcb.stack[high_mark] == 0xE25A2EA5)
DieterGraef 0:af2040964256 139 high_mark++;
DieterGraef 0:af2040964256 140 return _thread_def.tcb.priv_stack - (high_mark * 4);
DieterGraef 0:af2040964256 141 #else
DieterGraef 0:af2040964256 142 P_TCB tcb = rt_tid2ptcb(_tid);
DieterGraef 0:af2040964256 143 uint32_t high_mark = 0;
DieterGraef 0:af2040964256 144 while (tcb->stack[high_mark] == 0xE25A2EA5)
DieterGraef 0:af2040964256 145 high_mark++;
DieterGraef 0:af2040964256 146 return tcb->priv_stack - (high_mark * 4);
DieterGraef 0:af2040964256 147 #endif
DieterGraef 0:af2040964256 148 #else
DieterGraef 0:af2040964256 149 return 0;
DieterGraef 0:af2040964256 150 #endif
DieterGraef 0:af2040964256 151 }
DieterGraef 0:af2040964256 152
DieterGraef 0:af2040964256 153 osEvent Thread::signal_wait(int32_t signals, uint32_t millisec) {
DieterGraef 0:af2040964256 154 return osSignalWait(signals, millisec);
DieterGraef 0:af2040964256 155 }
DieterGraef 0:af2040964256 156
DieterGraef 0:af2040964256 157 osStatus Thread::wait(uint32_t millisec) {
DieterGraef 0:af2040964256 158 return osDelay(millisec);
DieterGraef 0:af2040964256 159 }
DieterGraef 0:af2040964256 160
DieterGraef 0:af2040964256 161 osStatus Thread::yield() {
DieterGraef 0:af2040964256 162 return osThreadYield();
DieterGraef 0:af2040964256 163 }
DieterGraef 0:af2040964256 164
DieterGraef 0:af2040964256 165 osThreadId Thread::gettid() {
DieterGraef 0:af2040964256 166 return osThreadGetId();
DieterGraef 0:af2040964256 167 }
DieterGraef 0:af2040964256 168
DieterGraef 0:af2040964256 169 void Thread::attach_idle_hook(void (*fptr)(void)) {
DieterGraef 0:af2040964256 170 rtos_attach_idle_hook(fptr);
DieterGraef 0:af2040964256 171 }
DieterGraef 0:af2040964256 172
DieterGraef 0:af2040964256 173 Thread::~Thread() {
DieterGraef 0:af2040964256 174 terminate();
DieterGraef 0:af2040964256 175 #ifdef __MBED_CMSIS_RTOS_CM
DieterGraef 0:af2040964256 176 if (_dynamic_stack) {
DieterGraef 0:af2040964256 177 delete[] (_thread_def.stack_pointer);
DieterGraef 0:af2040964256 178 }
DieterGraef 0:af2040964256 179 #endif
DieterGraef 0:af2040964256 180 }
DieterGraef 0:af2040964256 181
DieterGraef 0:af2040964256 182 }