JBBoardに接続したモーター2つをRCBControllerでコントロールするテストです。
Dependencies: FatFileSystem TB6612FNG2 mbed
Fork of JBB_BTLE_Test by
usb/Usb_td.cpp@0:1ed23ab1345f, 2012-06-26 (annotated)
- Committer:
- va009039
- Date:
- Tue Jun 26 14:27:45 2012 +0000
- Revision:
- 0:1ed23ab1345f
fix overflow spp_service_buffer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 0:1ed23ab1345f | 1 | #include "mbed.h" |
va009039 | 0:1ed23ab1345f | 2 | #include "Usb_td.h" |
va009039 | 0:1ed23ab1345f | 3 | //#define __DEBUG |
va009039 | 0:1ed23ab1345f | 4 | #include "mydbg.h" |
va009039 | 0:1ed23ab1345f | 5 | |
va009039 | 0:1ed23ab1345f | 6 | #define __TEST |
va009039 | 0:1ed23ab1345f | 7 | |
va009039 | 0:1ed23ab1345f | 8 | #ifdef __TEST |
va009039 | 0:1ed23ab1345f | 9 | #include <queue> |
va009039 | 0:1ed23ab1345f | 10 | #endif |
va009039 | 0:1ed23ab1345f | 11 | |
va009039 | 0:1ed23ab1345f | 12 | template class tdqueue<HCTD*>; |
va009039 | 0:1ed23ab1345f | 13 | template class tdqueue<HCITD*>; |
va009039 | 0:1ed23ab1345f | 14 | |
va009039 | 0:1ed23ab1345f | 15 | HCTD* td_reverse(HCTD* td) |
va009039 | 0:1ed23ab1345f | 16 | { |
va009039 | 0:1ed23ab1345f | 17 | HCTD* result = NULL; |
va009039 | 0:1ed23ab1345f | 18 | HCTD* next; |
va009039 | 0:1ed23ab1345f | 19 | while(td) { |
va009039 | 0:1ed23ab1345f | 20 | next = (HCTD*)td->Next; |
va009039 | 0:1ed23ab1345f | 21 | td->Next = (uint32_t)result; |
va009039 | 0:1ed23ab1345f | 22 | result = td; |
va009039 | 0:1ed23ab1345f | 23 | td = next; |
va009039 | 0:1ed23ab1345f | 24 | } |
va009039 | 0:1ed23ab1345f | 25 | return result; |
va009039 | 0:1ed23ab1345f | 26 | } |
va009039 | 0:1ed23ab1345f | 27 | |
va009039 | 0:1ed23ab1345f | 28 | template <class T> |
va009039 | 0:1ed23ab1345f | 29 | tdqueue<T>::tdqueue():m_head(NULL),m_tail(NULL) |
va009039 | 0:1ed23ab1345f | 30 | { |
va009039 | 0:1ed23ab1345f | 31 | |
va009039 | 0:1ed23ab1345f | 32 | } |
va009039 | 0:1ed23ab1345f | 33 | |
va009039 | 0:1ed23ab1345f | 34 | template <class T> |
va009039 | 0:1ed23ab1345f | 35 | int tdqueue<T>::size() |
va009039 | 0:1ed23ab1345f | 36 | { |
va009039 | 0:1ed23ab1345f | 37 | int n = 0; |
va009039 | 0:1ed23ab1345f | 38 | T td = m_head; |
va009039 | 0:1ed23ab1345f | 39 | while(td) { |
va009039 | 0:1ed23ab1345f | 40 | td = (T)td->Next; |
va009039 | 0:1ed23ab1345f | 41 | n++; |
va009039 | 0:1ed23ab1345f | 42 | } |
va009039 | 0:1ed23ab1345f | 43 | return n; |
va009039 | 0:1ed23ab1345f | 44 | } |
va009039 | 0:1ed23ab1345f | 45 | |
va009039 | 0:1ed23ab1345f | 46 | template <class T> |
va009039 | 0:1ed23ab1345f | 47 | bool tdqueue<T>::empty() |
va009039 | 0:1ed23ab1345f | 48 | { |
va009039 | 0:1ed23ab1345f | 49 | return (m_head == NULL); |
va009039 | 0:1ed23ab1345f | 50 | } |
va009039 | 0:1ed23ab1345f | 51 | |
va009039 | 0:1ed23ab1345f | 52 | template <class T> |
va009039 | 0:1ed23ab1345f | 53 | T tdqueue<T>::front() |
va009039 | 0:1ed23ab1345f | 54 | { |
va009039 | 0:1ed23ab1345f | 55 | return m_head; |
va009039 | 0:1ed23ab1345f | 56 | } |
va009039 | 0:1ed23ab1345f | 57 | |
va009039 | 0:1ed23ab1345f | 58 | template <class T> |
va009039 | 0:1ed23ab1345f | 59 | void tdqueue<T>::pop() |
va009039 | 0:1ed23ab1345f | 60 | { |
va009039 | 0:1ed23ab1345f | 61 | T td = m_head; |
va009039 | 0:1ed23ab1345f | 62 | if (td) { |
va009039 | 0:1ed23ab1345f | 63 | m_head = (T)td->Next; |
va009039 | 0:1ed23ab1345f | 64 | } |
va009039 | 0:1ed23ab1345f | 65 | if (td == m_tail) { |
va009039 | 0:1ed23ab1345f | 66 | m_tail = NULL; |
va009039 | 0:1ed23ab1345f | 67 | } |
va009039 | 0:1ed23ab1345f | 68 | } |
va009039 | 0:1ed23ab1345f | 69 | |
va009039 | 0:1ed23ab1345f | 70 | template <class T> |
va009039 | 0:1ed23ab1345f | 71 | void tdqueue<T>::push(T td) |
va009039 | 0:1ed23ab1345f | 72 | { |
va009039 | 0:1ed23ab1345f | 73 | td->Next = NULL; |
va009039 | 0:1ed23ab1345f | 74 | if (m_tail) { |
va009039 | 0:1ed23ab1345f | 75 | m_tail->Next = (uint32_t)td; |
va009039 | 0:1ed23ab1345f | 76 | } |
va009039 | 0:1ed23ab1345f | 77 | m_tail = td; |
va009039 | 0:1ed23ab1345f | 78 | if (m_head == NULL) { |
va009039 | 0:1ed23ab1345f | 79 | m_head = td; |
va009039 | 0:1ed23ab1345f | 80 | } |
va009039 | 0:1ed23ab1345f | 81 | } |
va009039 | 0:1ed23ab1345f | 82 | |
va009039 | 0:1ed23ab1345f | 83 | #ifdef __TEST |
va009039 | 0:1ed23ab1345f | 84 | static void test_td_list() |
va009039 | 0:1ed23ab1345f | 85 | { |
va009039 | 0:1ed23ab1345f | 86 | tdqueue<HCTD*> list; |
va009039 | 0:1ed23ab1345f | 87 | HCTD* td1; |
va009039 | 0:1ed23ab1345f | 88 | HCTD* td2; |
va009039 | 0:1ed23ab1345f | 89 | HCTD* td3; |
va009039 | 0:1ed23ab1345f | 90 | HCTD* td4; |
va009039 | 0:1ed23ab1345f | 91 | // 0 |
va009039 | 0:1ed23ab1345f | 92 | DBG_ASSERT(list.size() == 0); |
va009039 | 0:1ed23ab1345f | 93 | td1 = list.front(); |
va009039 | 0:1ed23ab1345f | 94 | DBG_ASSERT(td1 == NULL); |
va009039 | 0:1ed23ab1345f | 95 | list.pop(); |
va009039 | 0:1ed23ab1345f | 96 | td1 = list.front(); |
va009039 | 0:1ed23ab1345f | 97 | DBG_ASSERT(td1 == NULL); |
va009039 | 0:1ed23ab1345f | 98 | // 1 |
va009039 | 0:1ed23ab1345f | 99 | td1 = (HCTD*)usb_get_td(1); |
va009039 | 0:1ed23ab1345f | 100 | list.push(td1); |
va009039 | 0:1ed23ab1345f | 101 | DBG_ASSERT(list.size() == 1); |
va009039 | 0:1ed23ab1345f | 102 | td2 = list.front(); |
va009039 | 0:1ed23ab1345f | 103 | DBG_ASSERT(td2 == td1); |
va009039 | 0:1ed23ab1345f | 104 | list.pop(); |
va009039 | 0:1ed23ab1345f | 105 | td2 = list.front(); |
va009039 | 0:1ed23ab1345f | 106 | DBG_ASSERT(td2 == NULL); |
va009039 | 0:1ed23ab1345f | 107 | DBG_ASSERT(list.size() == 0); |
va009039 | 0:1ed23ab1345f | 108 | usb_free_td((byte*)td1); |
va009039 | 0:1ed23ab1345f | 109 | // 2 |
va009039 | 0:1ed23ab1345f | 110 | td1 = (HCTD*)usb_get_td(1); |
va009039 | 0:1ed23ab1345f | 111 | td2 = (HCTD*)usb_get_td(2); |
va009039 | 0:1ed23ab1345f | 112 | list.push(td1); |
va009039 | 0:1ed23ab1345f | 113 | list.push(td2); |
va009039 | 0:1ed23ab1345f | 114 | DBG_ASSERT(list.size() == 2); |
va009039 | 0:1ed23ab1345f | 115 | td3 = list.front(); |
va009039 | 0:1ed23ab1345f | 116 | DBG_ASSERT(td3 == td1); |
va009039 | 0:1ed23ab1345f | 117 | list.pop(); |
va009039 | 0:1ed23ab1345f | 118 | td3 = list.front(); |
va009039 | 0:1ed23ab1345f | 119 | DBG_ASSERT(td3 == td2); |
va009039 | 0:1ed23ab1345f | 120 | list.pop(); |
va009039 | 0:1ed23ab1345f | 121 | td3 = list.front(); |
va009039 | 0:1ed23ab1345f | 122 | DBG_ASSERT(td3 == NULL); |
va009039 | 0:1ed23ab1345f | 123 | usb_free_td((byte*)td1); |
va009039 | 0:1ed23ab1345f | 124 | usb_free_td((byte*)td2); |
va009039 | 0:1ed23ab1345f | 125 | // 3 |
va009039 | 0:1ed23ab1345f | 126 | td1 = (HCTD*)usb_get_td(1); |
va009039 | 0:1ed23ab1345f | 127 | td2 = (HCTD*)usb_get_td(2); |
va009039 | 0:1ed23ab1345f | 128 | td3 = (HCTD*)usb_get_td(3); |
va009039 | 0:1ed23ab1345f | 129 | list.push(td1); |
va009039 | 0:1ed23ab1345f | 130 | list.push(td2); |
va009039 | 0:1ed23ab1345f | 131 | list.push(td3); |
va009039 | 0:1ed23ab1345f | 132 | DBG_ASSERT(list.size() == 3); |
va009039 | 0:1ed23ab1345f | 133 | td4 = list.front(); |
va009039 | 0:1ed23ab1345f | 134 | DBG_ASSERT(td4 == td1); |
va009039 | 0:1ed23ab1345f | 135 | list.pop(); |
va009039 | 0:1ed23ab1345f | 136 | td4 = list.front(); |
va009039 | 0:1ed23ab1345f | 137 | DBG_ASSERT(td4 == td2); |
va009039 | 0:1ed23ab1345f | 138 | list.pop(); |
va009039 | 0:1ed23ab1345f | 139 | td4 = list.front(); |
va009039 | 0:1ed23ab1345f | 140 | DBG_ASSERT(td4 == td3); |
va009039 | 0:1ed23ab1345f | 141 | list.pop(); |
va009039 | 0:1ed23ab1345f | 142 | td4 = list.front(); |
va009039 | 0:1ed23ab1345f | 143 | DBG_ASSERT(td4 == NULL); |
va009039 | 0:1ed23ab1345f | 144 | usb_free_td((byte*)td1); |
va009039 | 0:1ed23ab1345f | 145 | usb_free_td((byte*)td2); |
va009039 | 0:1ed23ab1345f | 146 | usb_free_td((byte*)td3); |
va009039 | 0:1ed23ab1345f | 147 | // n |
va009039 | 0:1ed23ab1345f | 148 | queue<HCTD*> queue; |
va009039 | 0:1ed23ab1345f | 149 | for(int n = 1; n <= 10; n++) { |
va009039 | 0:1ed23ab1345f | 150 | DBG_ASSERT(list.size() == queue.size()); |
va009039 | 0:1ed23ab1345f | 151 | DBG_ASSERT(list.empty() == queue.empty()); |
va009039 | 0:1ed23ab1345f | 152 | if (list.empty() || (rand()%10) > 5) { |
va009039 | 0:1ed23ab1345f | 153 | td1 = (HCTD*)usb_get_td(n); |
va009039 | 0:1ed23ab1345f | 154 | if (td1) { |
va009039 | 0:1ed23ab1345f | 155 | list.push(td1); |
va009039 | 0:1ed23ab1345f | 156 | queue.push(td1); |
va009039 | 0:1ed23ab1345f | 157 | } |
va009039 | 0:1ed23ab1345f | 158 | //DBG("+ %d %p\n", n, td1); |
va009039 | 0:1ed23ab1345f | 159 | } else { |
va009039 | 0:1ed23ab1345f | 160 | td1 = list.front(); |
va009039 | 0:1ed23ab1345f | 161 | td2 = queue.front(); |
va009039 | 0:1ed23ab1345f | 162 | if (td1 != td2) { |
va009039 | 0:1ed23ab1345f | 163 | //DBG("td1=%p td2=%p\n", td1, td2); |
va009039 | 0:1ed23ab1345f | 164 | } |
va009039 | 0:1ed23ab1345f | 165 | DBG_ASSERT(td1 == td2); |
va009039 | 0:1ed23ab1345f | 166 | if (td1) { |
va009039 | 0:1ed23ab1345f | 167 | list.pop(); |
va009039 | 0:1ed23ab1345f | 168 | queue.pop(); |
va009039 | 0:1ed23ab1345f | 169 | usb_free_td((byte*)td1); |
va009039 | 0:1ed23ab1345f | 170 | } |
va009039 | 0:1ed23ab1345f | 171 | //DBG("- %d %p\n", n, td1); |
va009039 | 0:1ed23ab1345f | 172 | } |
va009039 | 0:1ed23ab1345f | 173 | } |
va009039 | 0:1ed23ab1345f | 174 | while(!list.empty()) { |
va009039 | 0:1ed23ab1345f | 175 | td1 = list.front(); |
va009039 | 0:1ed23ab1345f | 176 | list.pop(); |
va009039 | 0:1ed23ab1345f | 177 | usb_free_td((byte*)td1); |
va009039 | 0:1ed23ab1345f | 178 | } |
va009039 | 0:1ed23ab1345f | 179 | //DBG_ASSERT(0); |
va009039 | 0:1ed23ab1345f | 180 | } |
va009039 | 0:1ed23ab1345f | 181 | |
va009039 | 0:1ed23ab1345f | 182 | static void test_td_reverse() |
va009039 | 0:1ed23ab1345f | 183 | { |
va009039 | 0:1ed23ab1345f | 184 | |
va009039 | 0:1ed23ab1345f | 185 | HCTD* td1; |
va009039 | 0:1ed23ab1345f | 186 | HCTD* td2; |
va009039 | 0:1ed23ab1345f | 187 | HCTD* td3; |
va009039 | 0:1ed23ab1345f | 188 | HCTD* td4; |
va009039 | 0:1ed23ab1345f | 189 | // 0 |
va009039 | 0:1ed23ab1345f | 190 | td1 = NULL; |
va009039 | 0:1ed23ab1345f | 191 | td2 = td_reverse(td1); |
va009039 | 0:1ed23ab1345f | 192 | DBG_ASSERT(td2 == NULL); |
va009039 | 0:1ed23ab1345f | 193 | // 1 |
va009039 | 0:1ed23ab1345f | 194 | td1 = (HCTD*)usb_get_td(1); |
va009039 | 0:1ed23ab1345f | 195 | DBG_ASSERT(td1); |
va009039 | 0:1ed23ab1345f | 196 | DBG_ASSERT(td1->Next == NULL); |
va009039 | 0:1ed23ab1345f | 197 | td2 = td_reverse(td1); |
va009039 | 0:1ed23ab1345f | 198 | DBG_ASSERT(td2 == td1); |
va009039 | 0:1ed23ab1345f | 199 | DBG_ASSERT(td2->Next == NULL); |
va009039 | 0:1ed23ab1345f | 200 | usb_free_td((byte*)td1); |
va009039 | 0:1ed23ab1345f | 201 | // 2 |
va009039 | 0:1ed23ab1345f | 202 | td1 = (HCTD*)usb_get_td(1); |
va009039 | 0:1ed23ab1345f | 203 | DBG_ASSERT(td1); |
va009039 | 0:1ed23ab1345f | 204 | td2 = (HCTD*)usb_get_td(2); |
va009039 | 0:1ed23ab1345f | 205 | DBG_ASSERT(td2); |
va009039 | 0:1ed23ab1345f | 206 | td1->Next = (uint32_t)td2; |
va009039 | 0:1ed23ab1345f | 207 | td3 = td_reverse(td1); |
va009039 | 0:1ed23ab1345f | 208 | DBG_ASSERT(td3 == td2); |
va009039 | 0:1ed23ab1345f | 209 | DBG_ASSERT(td3->Next == (uint32_t)td1); |
va009039 | 0:1ed23ab1345f | 210 | DBG_ASSERT(td1->Next == NULL); |
va009039 | 0:1ed23ab1345f | 211 | usb_free_td((byte*)td1); |
va009039 | 0:1ed23ab1345f | 212 | usb_free_td((byte*)td2); |
va009039 | 0:1ed23ab1345f | 213 | // 3 |
va009039 | 0:1ed23ab1345f | 214 | td1 = (HCTD*)usb_get_td(1); |
va009039 | 0:1ed23ab1345f | 215 | td2 = (HCTD*)usb_get_td(2); |
va009039 | 0:1ed23ab1345f | 216 | td3 = (HCTD*)usb_get_td(3); |
va009039 | 0:1ed23ab1345f | 217 | td1->Next = (uint32_t)td2; |
va009039 | 0:1ed23ab1345f | 218 | td2->Next = (uint32_t)td3; |
va009039 | 0:1ed23ab1345f | 219 | td4 = td_reverse(td1); |
va009039 | 0:1ed23ab1345f | 220 | DBG_ASSERT(td4 == td3); |
va009039 | 0:1ed23ab1345f | 221 | DBG_ASSERT(td3->Next == (uint32_t)td2); |
va009039 | 0:1ed23ab1345f | 222 | DBG_ASSERT(td2->Next == (uint32_t)td1); |
va009039 | 0:1ed23ab1345f | 223 | DBG_ASSERT(td1->Next == NULL); |
va009039 | 0:1ed23ab1345f | 224 | usb_free_td((byte*)td1); |
va009039 | 0:1ed23ab1345f | 225 | usb_free_td((byte*)td2); |
va009039 | 0:1ed23ab1345f | 226 | usb_free_td((byte*)td3); |
va009039 | 0:1ed23ab1345f | 227 | } |
va009039 | 0:1ed23ab1345f | 228 | |
va009039 | 0:1ed23ab1345f | 229 | void test_td() |
va009039 | 0:1ed23ab1345f | 230 | { |
va009039 | 0:1ed23ab1345f | 231 | test_td_reverse(); |
va009039 | 0:1ed23ab1345f | 232 | test_td_list(); |
va009039 | 0:1ed23ab1345f | 233 | DBG("Usb_td.cpp TD test done.\n"); |
va009039 | 0:1ed23ab1345f | 234 | } |
va009039 | 0:1ed23ab1345f | 235 | #else //__TEST |
va009039 | 0:1ed23ab1345f | 236 | void test_td() |
va009039 | 0:1ed23ab1345f | 237 | { |
va009039 | 0:1ed23ab1345f | 238 | |
va009039 | 0:1ed23ab1345f | 239 | } |
va009039 | 0:1ed23ab1345f | 240 | #endif //__TEST |