Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of uvchost by
Usb_td.cpp
00001 #include "mbed.h" 00002 #include "Usb_td.h" 00003 #define __DEBUG 00004 #include "mydbg.h" 00005 00006 #define __TEST 00007 00008 #ifdef __TEST 00009 #include <queue> 00010 #endif 00011 00012 template class tdqueue<HCTD*>; 00013 template class tdqueue<HCITD*>; 00014 00015 HCTD* td_reverse(HCTD* td) 00016 { 00017 HCTD* result = NULL; 00018 HCTD* next; 00019 while(td) { 00020 next = (HCTD*)td->Next; 00021 td->Next = (uint32_t)result; 00022 result = td; 00023 td = next; 00024 } 00025 return result; 00026 } 00027 00028 template <class T> 00029 tdqueue<T>::tdqueue():m_head(NULL),m_tail(NULL) 00030 { 00031 00032 } 00033 00034 template <class T> 00035 int tdqueue<T>::size() 00036 { 00037 int n = 0; 00038 T td = m_head; 00039 while(td) { 00040 td = (T)td->Next; 00041 n++; 00042 } 00043 return n; 00044 } 00045 00046 template <class T> 00047 bool tdqueue<T>::empty() 00048 { 00049 return (m_head == NULL); 00050 } 00051 00052 template <class T> 00053 T tdqueue<T>::front() 00054 { 00055 return m_head; 00056 } 00057 00058 template <class T> 00059 void tdqueue<T>::pop() 00060 { 00061 T td = m_head; 00062 if (td) { 00063 m_head = (T)td->Next; 00064 } 00065 if (td == m_tail) { 00066 m_tail = NULL; 00067 } 00068 } 00069 00070 template <class T> 00071 void tdqueue<T>::push(T td) 00072 { 00073 td->Next = NULL; 00074 if (m_tail) { 00075 m_tail->Next = (uint32_t)td; 00076 } 00077 m_tail = td; 00078 if (m_head == NULL) { 00079 m_head = td; 00080 } 00081 } 00082 00083 #ifdef __TEST 00084 static void test_td_list() 00085 { 00086 tdqueue<HCTD*> list; 00087 HCTD* td1; 00088 HCTD* td2; 00089 HCTD* td3; 00090 HCTD* td4; 00091 // 0 00092 DBG_ASSERT(list.size() == 0); 00093 td1 = list.front(); 00094 DBG_ASSERT(td1 == NULL); 00095 list.pop(); 00096 td1 = list.front(); 00097 DBG_ASSERT(td1 == NULL); 00098 // 1 00099 td1 = (HCTD*)usb_get_td(1); 00100 list.push(td1); 00101 DBG_ASSERT(list.size() == 1); 00102 td2 = list.front(); 00103 DBG_ASSERT(td2 == td1); 00104 list.pop(); 00105 td2 = list.front(); 00106 DBG_ASSERT(td2 == NULL); 00107 DBG_ASSERT(list.size() == 0); 00108 usb_free_td((byte*)td1); 00109 // 2 00110 td1 = (HCTD*)usb_get_td(1); 00111 td2 = (HCTD*)usb_get_td(2); 00112 list.push(td1); 00113 list.push(td2); 00114 DBG_ASSERT(list.size() == 2); 00115 td3 = list.front(); 00116 DBG_ASSERT(td3 == td1); 00117 list.pop(); 00118 td3 = list.front(); 00119 DBG_ASSERT(td3 == td2); 00120 list.pop(); 00121 td3 = list.front(); 00122 DBG_ASSERT(td3 == NULL); 00123 usb_free_td((byte*)td1); 00124 usb_free_td((byte*)td2); 00125 // 3 00126 td1 = (HCTD*)usb_get_td(1); 00127 td2 = (HCTD*)usb_get_td(2); 00128 td3 = (HCTD*)usb_get_td(3); 00129 list.push(td1); 00130 list.push(td2); 00131 list.push(td3); 00132 DBG_ASSERT(list.size() == 3); 00133 td4 = list.front(); 00134 DBG_ASSERT(td4 == td1); 00135 list.pop(); 00136 td4 = list.front(); 00137 DBG_ASSERT(td4 == td2); 00138 list.pop(); 00139 td4 = list.front(); 00140 DBG_ASSERT(td4 == td3); 00141 list.pop(); 00142 td4 = list.front(); 00143 DBG_ASSERT(td4 == NULL); 00144 usb_free_td((byte*)td1); 00145 usb_free_td((byte*)td2); 00146 usb_free_td((byte*)td3); 00147 // n 00148 queue<HCTD*> queue; 00149 for(int n = 1; n <= 10; n++) { 00150 DBG_ASSERT(list.size() == queue.size()); 00151 DBG_ASSERT(list.empty() == queue.empty()); 00152 if (list.empty() || (rand()%10) > 5) { 00153 td1 = (HCTD*)usb_get_td(n); 00154 if (td1) { 00155 list.push(td1); 00156 queue.push(td1); 00157 } 00158 //DBG("+ %d %p\n", n, td1); 00159 } else { 00160 td1 = list.front(); 00161 td2 = queue.front(); 00162 if (td1 != td2) { 00163 //DBG("td1=%p td2=%p\n", td1, td2); 00164 } 00165 DBG_ASSERT(td1 == td2); 00166 if (td1) { 00167 list.pop(); 00168 queue.pop(); 00169 usb_free_td((byte*)td1); 00170 } 00171 //DBG("- %d %p\n", n, td1); 00172 } 00173 } 00174 while(!list.empty()) { 00175 td1 = list.front(); 00176 list.pop(); 00177 usb_free_td((byte*)td1); 00178 } 00179 //DBG_ASSERT(0); 00180 } 00181 00182 static void test_td_reverse() 00183 { 00184 00185 HCTD* td1; 00186 HCTD* td2; 00187 HCTD* td3; 00188 HCTD* td4; 00189 // 0 00190 td1 = NULL; 00191 td2 = td_reverse(td1); 00192 DBG_ASSERT(td2 == NULL); 00193 // 1 00194 td1 = (HCTD*)usb_get_td(1); 00195 DBG_ASSERT(td1); 00196 DBG_ASSERT(td1->Next == NULL); 00197 td2 = td_reverse(td1); 00198 DBG_ASSERT(td2 == td1); 00199 DBG_ASSERT(td2->Next == NULL); 00200 usb_free_td((byte*)td1); 00201 // 2 00202 td1 = (HCTD*)usb_get_td(1); 00203 DBG_ASSERT(td1); 00204 td2 = (HCTD*)usb_get_td(2); 00205 DBG_ASSERT(td2); 00206 td1->Next = (uint32_t)td2; 00207 td3 = td_reverse(td1); 00208 DBG_ASSERT(td3 == td2); 00209 DBG_ASSERT(td3->Next == (uint32_t)td1); 00210 DBG_ASSERT(td1->Next == NULL); 00211 usb_free_td((byte*)td1); 00212 usb_free_td((byte*)td2); 00213 // 3 00214 td1 = (HCTD*)usb_get_td(1); 00215 td2 = (HCTD*)usb_get_td(2); 00216 td3 = (HCTD*)usb_get_td(3); 00217 td1->Next = (uint32_t)td2; 00218 td2->Next = (uint32_t)td3; 00219 td4 = td_reverse(td1); 00220 DBG_ASSERT(td4 == td3); 00221 DBG_ASSERT(td3->Next == (uint32_t)td2); 00222 DBG_ASSERT(td2->Next == (uint32_t)td1); 00223 DBG_ASSERT(td1->Next == NULL); 00224 usb_free_td((byte*)td1); 00225 usb_free_td((byte*)td2); 00226 usb_free_td((byte*)td3); 00227 } 00228 00229 void test_td() 00230 { 00231 test_td_reverse(); 00232 test_td_list(); 00233 DBG("Usb_td.cpp TD test done.\n"); 00234 } 00235 #else //__TEST 00236 void test_td() 00237 { 00238 00239 } 00240 #endif //__TEST
Generated on Wed Jul 13 2022 01:34:55 by
1.7.2
