Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

10 роки тому
10 роки тому
9 роки тому
9 роки тому
9 роки тому
9 роки тому
9 роки тому
9 роки тому
9 роки тому
9 роки тому
9 роки тому
9 роки тому
9 роки тому
9 роки тому
9 роки тому
9 роки тому
9 роки тому
9 роки тому
9 роки тому
9 роки тому
9 роки тому
9 роки тому
9 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #include "usb_dev.h"
  31. #include "usb_flightsim.h"
  32. #include "core_pins.h" // for yield(), millis()
  33. #include <string.h> // for memcpy()
  34. #ifdef FLIGHTSIM_INTERFACE // defined by usb_dev.h -> usb_desc.h
  35. #if F_CPU >= 20000000
  36. FlightSimCommand * FlightSimCommand::first = NULL;
  37. FlightSimCommand * FlightSimCommand::last = NULL;
  38. FlightSimInteger * FlightSimInteger::first = NULL;
  39. FlightSimInteger * FlightSimInteger::last = NULL;
  40. FlightSimFloat * FlightSimFloat::first = NULL;
  41. FlightSimFloat * FlightSimFloat::last = NULL;
  42. uint8_t FlightSimClass::enabled = 0;
  43. uint8_t FlightSimClass::request_id_messages = 0;
  44. unsigned long FlightSimClass::frameCount = 0;
  45. elapsedMillis FlightSimClass::enableTimeout;
  46. static unsigned int unassigned_id = 1; // TODO: move into FlightSimClass
  47. FlightSimCommand::FlightSimCommand()
  48. {
  49. id = unassigned_id++;
  50. if (!first) {
  51. first = this;
  52. } else {
  53. last->next = this;
  54. }
  55. last = this;
  56. name = NULL;
  57. next = NULL;
  58. FlightSimClass::request_id_messages = 1;
  59. }
  60. void FlightSimCommand::identify(void)
  61. {
  62. uint8_t len, buf[6];
  63. if (!FlightSim.enabled || !name) return;
  64. len = strlen((const char *)name);
  65. buf[0] = len + 6;
  66. buf[1] = 1;
  67. buf[2] = id;
  68. buf[3] = id >> 8;
  69. buf[4] = 0;
  70. buf[5] = 0;
  71. FlightSimClass::xmit(buf, 6, name, len);
  72. }
  73. void FlightSimCommand::sendcmd(uint8_t n)
  74. {
  75. uint8_t buf[4];
  76. if (!FlightSim.enabled || !name) return;
  77. buf[0] = 4;
  78. buf[1] = n;
  79. buf[2] = id;
  80. buf[3] = id >> 8;
  81. FlightSimClass::xmit(buf, 4, NULL, 0);
  82. }
  83. FlightSimInteger::FlightSimInteger()
  84. {
  85. id = unassigned_id++;
  86. if (!first) {
  87. first = this;
  88. } else {
  89. last->next = this;
  90. }
  91. last = this;
  92. name = NULL;
  93. next = NULL;
  94. value = 0;
  95. change_callback = NULL;
  96. callbackInfo = NULL;
  97. hasCallbackInfo = false;
  98. FlightSimClass::request_id_messages = 1;
  99. }
  100. void FlightSimInteger::identify(void)
  101. {
  102. uint8_t len, buf[6];
  103. if (!FlightSim.enabled || !name) return;
  104. len = strlen((const char *)name);
  105. buf[0] = len + 6;
  106. buf[1] = 1;
  107. buf[2] = id;
  108. buf[3] = id >> 8;
  109. buf[4] = 1;
  110. buf[5] = 0;
  111. FlightSimClass::xmit(buf, 6, name, len);
  112. }
  113. void FlightSimInteger::write(long val)
  114. {
  115. uint8_t buf[6];
  116. value = val;
  117. if (!FlightSim.enabled || !name) return; // TODO: mark as dirty
  118. buf[0] = 10;
  119. buf[1] = 2;
  120. buf[2] = id;
  121. buf[3] = id >> 8;
  122. buf[4] = 1;
  123. buf[5] = 0;
  124. FlightSimClass::xmit(buf, 6, (uint8_t *)&value, 4);
  125. }
  126. void FlightSimInteger::update(long val)
  127. {
  128. value = val;
  129. if (change_callback) {
  130. if (!hasCallbackInfo) {
  131. (*change_callback)(val);
  132. } else {
  133. (*(void(*)(long,void*))change_callback)(val,callbackInfo);
  134. }
  135. }
  136. }
  137. FlightSimInteger * FlightSimInteger::find(unsigned int n)
  138. {
  139. for (FlightSimInteger *p = first; p; p = p->next) {
  140. if (p->id == n) return p;
  141. }
  142. return NULL;
  143. }
  144. FlightSimFloat::FlightSimFloat()
  145. {
  146. id = unassigned_id++;
  147. if (!first) {
  148. first = this;
  149. } else {
  150. last->next = this;
  151. }
  152. last = this;
  153. name = NULL;
  154. next = NULL;
  155. value = 0;
  156. change_callback = NULL;
  157. hasCallbackInfo = false;
  158. callbackInfo = NULL;
  159. FlightSimClass::request_id_messages = 1;
  160. }
  161. void FlightSimFloat::identify(void)
  162. {
  163. uint8_t len, buf[6];
  164. if (!FlightSim.enabled || !name) return;
  165. len = strlen((const char *)name);
  166. buf[0] = len + 6;
  167. buf[1] = 1;
  168. buf[2] = id;
  169. buf[3] = id >> 8;
  170. buf[4] = 2;
  171. buf[5] = 0;
  172. FlightSimClass::xmit(buf, 6, name, len);
  173. }
  174. void FlightSimFloat::write(float val)
  175. {
  176. uint8_t buf[6];
  177. value = val;
  178. if (!FlightSim.enabled || !name) return; // TODO: mark as dirty
  179. buf[0] = 10;
  180. buf[1] = 2;
  181. buf[2] = id;
  182. buf[3] = id >> 8;
  183. buf[4] = 2;
  184. buf[5] = 0;
  185. FlightSimClass::xmit(buf, 6, (uint8_t *)&value, 4);
  186. }
  187. void FlightSimFloat::update(float val)
  188. {
  189. value = val;
  190. if (change_callback) { // add: JB
  191. if (!hasCallbackInfo) {
  192. (*change_callback)(val);
  193. } else {
  194. (*(void(*)(float,void*))change_callback)(val,callbackInfo);
  195. }
  196. }
  197. }
  198. FlightSimFloat * FlightSimFloat::find(unsigned int n)
  199. {
  200. for (FlightSimFloat *p = first; p; p = p->next) {
  201. if (p->id == n) return p;
  202. }
  203. return NULL;
  204. }
  205. FlightSimClass::FlightSimClass()
  206. {
  207. }
  208. void FlightSimClass::update(void)
  209. {
  210. uint8_t len, maxlen, type, *p, *end;
  211. union {
  212. uint8_t b[4];
  213. long l;
  214. float f;
  215. } data;
  216. usb_packet_t *rx_packet;
  217. uint16_t id;
  218. while (1) {
  219. if (!usb_configuration) break;
  220. rx_packet = usb_rx(FLIGHTSIM_RX_ENDPOINT);
  221. if (!rx_packet) break;
  222. p = rx_packet->buf;
  223. end = p + 64;
  224. maxlen = 64;
  225. do {
  226. len = p[0];
  227. if (len < 2 || len > maxlen) break;
  228. switch (p[1]) {
  229. case 0x02: // write data
  230. if (len < 10) break;
  231. id = p[2] | (p[3] << 8);
  232. type = p[4];
  233. if (type == 1) {
  234. FlightSimInteger *item = FlightSimInteger::find(id);
  235. if (!item) break;
  236. #ifdef KINETISK
  237. data.l = *(long *)(p + 6);
  238. #else
  239. data.b[0] = p[6];
  240. data.b[1] = p[7];
  241. data.b[2] = p[8];
  242. data.b[3] = p[9];
  243. #endif
  244. item->update(data.l);
  245. } else if (type == 2) {
  246. FlightSimFloat *item = FlightSimFloat::find(id);
  247. if (!item) break;
  248. data.b[0] = p[6];
  249. data.b[1] = p[7];
  250. data.b[2] = p[8];
  251. data.b[3] = p[9];
  252. item->update(data.f);
  253. }
  254. break;
  255. case 0x03: // enable/disable
  256. if (len < 4) break;
  257. switch (p[2]) {
  258. case 1:
  259. request_id_messages = 1;
  260. /* no break */
  261. case 2:
  262. enable();
  263. frameCount++;
  264. break;
  265. case 3:
  266. disable();
  267. }
  268. }
  269. p += len;
  270. maxlen -= len;
  271. } while (p < end);
  272. usb_free(rx_packet);
  273. }
  274. if (enabled && request_id_messages) {
  275. request_id_messages = 0;
  276. for (FlightSimCommand *p = FlightSimCommand::first; p; p = p->next) {
  277. p->identify();
  278. }
  279. for (FlightSimInteger *p = FlightSimInteger::first; p; p = p->next) {
  280. p->identify();
  281. // TODO: send any dirty data
  282. }
  283. for (FlightSimFloat *p = FlightSimFloat::first; p; p = p->next) {
  284. p->identify();
  285. // TODO: send any dirty data
  286. }
  287. }
  288. }
  289. bool FlightSimClass::isEnabled(void)
  290. {
  291. if (!usb_configuration) return false;
  292. if (!enabled) return false;
  293. if (enableTimeout > 1500) return false;
  294. return true;
  295. }
  296. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  297. #define TX_PACKET_LIMIT 8
  298. static usb_packet_t *tx_packet=NULL;
  299. static volatile uint8_t tx_noautoflush=0;
  300. void FlightSimClass::xmit(const void *p1, uint8_t n1, const void *p2, uint8_t n2)
  301. {
  302. uint16_t total;
  303. total = n1 + n2;
  304. if (total > FLIGHTSIM_TX_SIZE) {
  305. xmit_big_packet(p1, n1, p2, n2);
  306. return;
  307. }
  308. if (!enabled || !usb_configuration) return;
  309. tx_noautoflush = 1;
  310. if (tx_packet) {
  311. if (total <= FLIGHTSIM_TX_SIZE - tx_packet->index) goto send;
  312. for (int i = tx_packet->index; i < FLIGHTSIM_TX_SIZE; i++) {
  313. tx_packet->buf[i] = 0;
  314. }
  315. tx_packet->len = FLIGHTSIM_TX_SIZE;
  316. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  317. tx_packet = NULL;
  318. }
  319. while (1) {
  320. if (usb_tx_packet_count(FLIGHTSIM_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  321. tx_packet = usb_malloc();
  322. if (tx_packet) break;
  323. }
  324. if (!enabled || !usb_configuration) {
  325. tx_noautoflush = 0;
  326. return;
  327. }
  328. tx_noautoflush = 0;
  329. yield();
  330. tx_noautoflush = 1;
  331. }
  332. send:
  333. memcpy(tx_packet->buf + tx_packet->index, p1, n1);
  334. tx_packet->index += n1;
  335. if (n2 > 0) {
  336. memcpy(tx_packet->buf + tx_packet->index, p2, n2);
  337. tx_packet->index += n2;
  338. }
  339. if (tx_packet->index >= FLIGHTSIM_TX_SIZE) {
  340. tx_packet->len = FLIGHTSIM_TX_SIZE;
  341. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  342. tx_packet = NULL;
  343. }
  344. tx_noautoflush = 0;
  345. }
  346. void FlightSimClass::xmit_big_packet(const void *p1, uint8_t n1, const void *p2, uint8_t n2)
  347. {
  348. if (!enabled || !usb_configuration) return;
  349. uint16_t remaining = n1 + n2;
  350. if (remaining > 255) return;
  351. bool part2 = false;
  352. uint8_t remainingPart1 = n1;
  353. const uint8_t *dataPtr = (const uint8_t*)p1;
  354. bool writeFragmentHeader = false;
  355. uint8_t fragmentCounter = 1;
  356. tx_noautoflush =1; // don't mess with my data, I'm working on it!
  357. if (tx_packet) {
  358. // If we have a current packet, fill it with whatever fits
  359. uint8_t partLen = FLIGHTSIM_TX_SIZE - tx_packet->index;
  360. if (partLen > n1) partLen=n1;
  361. // copy first part, containing total packet length
  362. memcpy(tx_packet->buf + tx_packet->index, dataPtr, partLen);
  363. remainingPart1 -= partLen;
  364. tx_packet->index += partLen;
  365. if (remainingPart1) {
  366. // there still is data from the first part that
  367. // will go to the next packet. The boolean variable
  368. // part2 remains false
  369. remaining = remainingPart1+n2;
  370. dataPtr += partLen;
  371. } else {
  372. // maybe we have space for some data from the second part
  373. part2=true;
  374. partLen = FLIGHTSIM_TX_SIZE - tx_packet->index;
  375. // there is no need here to check whether partLen is
  376. // bigger than n2. It's not. If it were, all the data
  377. // would have fit in a single packet and xmit_big_packet
  378. // would never have been called...
  379. remaining = n2;
  380. if (partLen) {
  381. memcpy(tx_packet->buf + tx_packet->index, p2, partLen);
  382. remaining -= partLen;
  383. tx_packet->index += partLen;
  384. }
  385. dataPtr = (const uint8_t*)p2 + partLen;
  386. }
  387. // Packet padding should not be necessary, as xmit_big_packet
  388. // will only be called for data that doesn't fit in a single
  389. // packet. So, the previous code should always fill up the
  390. // first packet. Right?
  391. for (int i = tx_packet->index; i < FLIGHTSIM_TX_SIZE; i++) {
  392. tx_packet->buf[i] = 0;
  393. }
  394. // queue first packet for sending
  395. tx_packet->len = FLIGHTSIM_TX_SIZE;
  396. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  397. tx_packet = NULL;
  398. writeFragmentHeader = true;
  399. } else {
  400. remaining = n1+n2;
  401. }
  402. while (remaining >0) {
  403. while (1) {
  404. // get memory for next packet
  405. if (usb_tx_packet_count(FLIGHTSIM_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  406. tx_packet = usb_malloc();
  407. if (tx_packet) {
  408. break;
  409. }
  410. }
  411. if (!enabled || !usb_configuration) {
  412. // teensy disconnected
  413. tx_noautoflush = 0;
  414. return;
  415. }
  416. tx_noautoflush = 0; // you can pick up my data, if you like
  417. yield(); // do other things and wait for memory to become free
  418. tx_noautoflush = 1; // wait, I'm working on the packet data
  419. }
  420. if (writeFragmentHeader) {
  421. tx_packet->buf[0]=(remaining+3 <= FLIGHTSIM_TX_SIZE) ? (byte) remaining+3 : FLIGHTSIM_TX_SIZE;
  422. tx_packet->buf[1]=0xff;
  423. tx_packet->buf[2]=fragmentCounter++;
  424. tx_packet->index=3;
  425. }
  426. if (!part2) {
  427. // we still need to send the first part
  428. uint8_t partLen = FLIGHTSIM_TX_SIZE - tx_packet->index;
  429. if (partLen > remainingPart1)
  430. partLen=remainingPart1;
  431. memcpy(tx_packet->buf + tx_packet->index, dataPtr, partLen);
  432. dataPtr += partLen;
  433. remainingPart1 -= partLen;
  434. tx_packet->index += partLen;
  435. remaining -= partLen;
  436. if (!remainingPart1) {
  437. part2=true;
  438. dataPtr = (const uint8_t*)p2;
  439. }
  440. }
  441. if (part2) {
  442. uint8_t partLen = FLIGHTSIM_TX_SIZE - tx_packet->index;
  443. if (partLen) {
  444. if (partLen > remaining)
  445. partLen=remaining;
  446. memcpy(tx_packet->buf + tx_packet->index, dataPtr, partLen);
  447. remaining -= partLen;
  448. tx_packet->index += partLen;
  449. dataPtr += partLen;
  450. }
  451. }
  452. writeFragmentHeader = true;
  453. if (tx_packet->index >= FLIGHTSIM_TX_SIZE) {
  454. // queue packet for sending
  455. tx_packet->len = FLIGHTSIM_TX_SIZE;
  456. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  457. tx_packet = NULL;
  458. }
  459. }
  460. tx_noautoflush = 0; // data is ready to be transmitted on start of USB token
  461. }
  462. extern "C" {
  463. // This gets called from usb_isr when a USB start token arrives.
  464. // If we have a packet to transmit AND transmission isn't disabled
  465. // by tx_noautoflush, we fill it up with zeros and send it out
  466. // to USB
  467. void usb_flightsim_flush_callback(void)
  468. {
  469. if (tx_noautoflush || !tx_packet || tx_packet->index == 0) return;
  470. for (int i=tx_packet->index; i < FLIGHTSIM_TX_SIZE; i++) {
  471. tx_packet->buf[i] = 0;
  472. }
  473. tx_packet->len = FLIGHTSIM_TX_SIZE;
  474. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  475. tx_packet = NULL;
  476. }
  477. }
  478. #endif // F_CPU
  479. #endif // FLIGHTSIM_INTERFACE