You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usb_flightsim.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 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_desc.h"
  32. #include "usb_flightsim.h"
  33. #include "core_pins.h" // for yield(), millis()
  34. #include <string.h> // for memcpy()
  35. #ifdef FLIGHTSIM_INTERFACE // defined by usb_dev.h -> usb_desc.h
  36. #if F_CPU >= 20000000
  37. FlightSimCommand * FlightSimCommand::first = NULL;
  38. FlightSimCommand * FlightSimCommand::last = NULL;
  39. FlightSimInteger * FlightSimInteger::first = NULL;
  40. FlightSimInteger * FlightSimInteger::last = NULL;
  41. FlightSimFloat * FlightSimFloat::first = NULL;
  42. FlightSimFloat * FlightSimFloat::last = NULL;
  43. uint8_t FlightSimClass::enabled = 0;
  44. uint8_t FlightSimClass::request_id_messages = 0;
  45. unsigned long FlightSimClass::frameCount = 0;
  46. elapsedMillis FlightSimClass::enableTimeout;
  47. static unsigned int unassigned_id = 1; // TODO: move into FlightSimClass
  48. FlightSimCommand::FlightSimCommand()
  49. {
  50. id = unassigned_id++;
  51. if (!first) {
  52. first = this;
  53. } else {
  54. last->next = this;
  55. }
  56. last = this;
  57. name = NULL;
  58. next = NULL;
  59. FlightSimClass::request_id_messages = 1;
  60. }
  61. void FlightSimCommand::identify(void)
  62. {
  63. uint8_t len, buf[6];
  64. if (!FlightSim.enabled || !name) return;
  65. len = strlen((const char *)name);
  66. buf[0] = len + 6;
  67. buf[1] = 1;
  68. buf[2] = id;
  69. buf[3] = id >> 8;
  70. buf[4] = 0;
  71. buf[5] = 0;
  72. FlightSimClass::xmit(buf, 6, name, len);
  73. }
  74. void FlightSimCommand::sendcmd(uint8_t n)
  75. {
  76. uint8_t buf[4];
  77. if (!FlightSim.enabled || !name) return;
  78. buf[0] = 4;
  79. buf[1] = n;
  80. buf[2] = id;
  81. buf[3] = id >> 8;
  82. FlightSimClass::xmit(buf, 4, NULL, 0);
  83. }
  84. FlightSimInteger::FlightSimInteger()
  85. {
  86. id = unassigned_id++;
  87. if (!first) {
  88. first = this;
  89. } else {
  90. last->next = this;
  91. }
  92. last = this;
  93. name = NULL;
  94. next = NULL;
  95. value = 0;
  96. change_callback = NULL;
  97. callbackInfo = NULL;
  98. hasCallbackInfo = false;
  99. FlightSimClass::request_id_messages = 1;
  100. }
  101. void FlightSimInteger::identify(void)
  102. {
  103. uint8_t len, buf[6];
  104. if (!FlightSim.enabled || !name) return;
  105. len = strlen((const char *)name);
  106. buf[0] = len + 6;
  107. buf[1] = 1;
  108. buf[2] = id;
  109. buf[3] = id >> 8;
  110. buf[4] = 1;
  111. buf[5] = 0;
  112. FlightSimClass::xmit(buf, 6, name, len);
  113. }
  114. void FlightSimInteger::write(long val)
  115. {
  116. uint8_t buf[6];
  117. value = val;
  118. if (!FlightSim.enabled || !name) return; // TODO: mark as dirty
  119. buf[0] = 10;
  120. buf[1] = 2;
  121. buf[2] = id;
  122. buf[3] = id >> 8;
  123. buf[4] = 1;
  124. buf[5] = 0;
  125. FlightSimClass::xmit(buf, 6, (uint8_t *)&value, 4);
  126. }
  127. void FlightSimInteger::update(long val)
  128. {
  129. value = val;
  130. if (change_callback) {
  131. if (!hasCallbackInfo) {
  132. (*change_callback)(val);
  133. } else {
  134. (*(void(*)(long,void*))change_callback)(val,callbackInfo);
  135. }
  136. }
  137. }
  138. FlightSimInteger * FlightSimInteger::find(unsigned int n)
  139. {
  140. for (FlightSimInteger *p = first; p; p = p->next) {
  141. if (p->id == n) return p;
  142. }
  143. return NULL;
  144. }
  145. FlightSimFloat::FlightSimFloat()
  146. {
  147. id = unassigned_id++;
  148. if (!first) {
  149. first = this;
  150. } else {
  151. last->next = this;
  152. }
  153. last = this;
  154. name = NULL;
  155. next = NULL;
  156. value = 0;
  157. change_callback = NULL;
  158. hasCallbackInfo = false;
  159. callbackInfo = NULL;
  160. FlightSimClass::request_id_messages = 1;
  161. }
  162. void FlightSimFloat::identify(void)
  163. {
  164. uint8_t len, buf[6];
  165. if (!FlightSim.enabled || !name) return;
  166. len = strlen((const char *)name);
  167. buf[0] = len + 6;
  168. buf[1] = 1;
  169. buf[2] = id;
  170. buf[3] = id >> 8;
  171. buf[4] = 2;
  172. buf[5] = 0;
  173. FlightSimClass::xmit(buf, 6, name, len);
  174. }
  175. void FlightSimFloat::write(float val)
  176. {
  177. uint8_t buf[6];
  178. value = val;
  179. if (!FlightSim.enabled || !name) return; // TODO: mark as dirty
  180. buf[0] = 10;
  181. buf[1] = 2;
  182. buf[2] = id;
  183. buf[3] = id >> 8;
  184. buf[4] = 2;
  185. buf[5] = 0;
  186. FlightSimClass::xmit(buf, 6, (uint8_t *)&value, 4);
  187. }
  188. void FlightSimFloat::update(float val)
  189. {
  190. value = val;
  191. if (change_callback) { // add: JB
  192. if (!hasCallbackInfo) {
  193. (*change_callback)(val);
  194. } else {
  195. (*(void(*)(long,void*))change_callback)(val,callbackInfo);
  196. }
  197. }
  198. }
  199. FlightSimFloat * FlightSimFloat::find(unsigned int n)
  200. {
  201. for (FlightSimFloat *p = first; p; p = p->next) {
  202. if (p->id == n) return p;
  203. }
  204. return NULL;
  205. }
  206. FlightSimClass::FlightSimClass()
  207. {
  208. }
  209. void FlightSimClass::update(void)
  210. {
  211. uint8_t len, maxlen, type, *p, *end;
  212. union {
  213. uint8_t b[4];
  214. long l;
  215. float f;
  216. } data;
  217. usb_packet_t *rx_packet;
  218. uint16_t id;
  219. while (1) {
  220. if (!usb_configuration) break;
  221. rx_packet = usb_rx(FLIGHTSIM_RX_ENDPOINT);
  222. if (!rx_packet) break;
  223. p = rx_packet->buf;
  224. end = p + 64;
  225. maxlen = 64;
  226. do {
  227. len = p[0];
  228. if (len < 2 || len > maxlen) break;
  229. switch (p[1]) {
  230. case 0x02: // write data
  231. if (len < 10) break;
  232. id = p[2] | (p[3] << 8);
  233. type = p[4];
  234. if (type == 1) {
  235. FlightSimInteger *item = FlightSimInteger::find(id);
  236. if (!item) break;
  237. #ifdef KINETISK
  238. data.l = *(long *)(p + 6);
  239. #else
  240. data.b[0] = p[6];
  241. data.b[1] = p[7];
  242. data.b[2] = p[8];
  243. data.b[3] = p[9];
  244. #endif
  245. item->update(data.l);
  246. } else if (type == 2) {
  247. FlightSimFloat *item = FlightSimFloat::find(id);
  248. if (!item) break;
  249. #ifdef KINETISK
  250. data.f = *(float *)(p + 6);
  251. #else
  252. data.b[0] = p[6];
  253. data.b[1] = p[7];
  254. data.b[2] = p[8];
  255. data.b[3] = p[9];
  256. #endif
  257. item->update(data.f);
  258. }
  259. break;
  260. case 0x03: // enable/disable
  261. if (len < 4) break;
  262. switch (p[2]) {
  263. case 1:
  264. request_id_messages = 1;
  265. /* no break */
  266. case 2:
  267. enable();
  268. frameCount++;
  269. break;
  270. case 3:
  271. disable();
  272. }
  273. }
  274. p += len;
  275. maxlen -= len;
  276. } while (p < end);
  277. usb_free(rx_packet);
  278. }
  279. if (enabled && request_id_messages) {
  280. request_id_messages = 0;
  281. for (FlightSimCommand *p = FlightSimCommand::first; p; p = p->next) {
  282. p->identify();
  283. }
  284. for (FlightSimInteger *p = FlightSimInteger::first; p; p = p->next) {
  285. p->identify();
  286. // TODO: send any dirty data
  287. }
  288. for (FlightSimFloat *p = FlightSimFloat::first; p; p = p->next) {
  289. p->identify();
  290. // TODO: send any dirty data
  291. }
  292. }
  293. }
  294. bool FlightSimClass::isEnabled(void)
  295. {
  296. if (!usb_configuration) return false;
  297. if (!enabled) return false;
  298. if (enableTimeout > 1500) return false;
  299. return true;
  300. }
  301. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  302. #define TX_PACKET_LIMIT 8
  303. static usb_packet_t *tx_packet=NULL;
  304. static volatile uint8_t tx_noautoflush=0;
  305. void FlightSimClass::xmit(const void *p1, uint8_t n1, const void *p2, uint8_t n2)
  306. {
  307. uint8_t total;
  308. total = n1 + n2;
  309. if (total > FLIGHTSIM_TX_SIZE) {
  310. xmit_big_packet(p1, n1, p2, n2);
  311. return;
  312. }
  313. if (!enabled || !usb_configuration) return;
  314. tx_noautoflush = 1;
  315. if (tx_packet) {
  316. if (total <= FLIGHTSIM_TX_SIZE - tx_packet->index) goto send;
  317. for (int i = tx_packet->index; i < FLIGHTSIM_TX_SIZE; i++) {
  318. tx_packet->buf[i] = 0;
  319. }
  320. tx_packet->len = FLIGHTSIM_TX_SIZE;
  321. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  322. tx_packet = NULL;
  323. }
  324. while (1) {
  325. if (usb_tx_packet_count(FLIGHTSIM_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  326. tx_packet = usb_malloc();
  327. if (tx_packet) break;
  328. }
  329. if (!enabled || !usb_configuration) {
  330. tx_noautoflush = 0;
  331. return;
  332. }
  333. tx_noautoflush = 0;
  334. yield();
  335. tx_noautoflush = 1;
  336. }
  337. send:
  338. memcpy(tx_packet->buf + tx_packet->index, p1, n1);
  339. tx_packet->index += n1;
  340. if (n2 > 0) {
  341. memcpy(tx_packet->buf + tx_packet->index, p2, n2);
  342. tx_packet->index += n2;
  343. }
  344. if (tx_packet->index >= FLIGHTSIM_TX_SIZE) {
  345. tx_packet->len = FLIGHTSIM_TX_SIZE;
  346. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  347. tx_packet = NULL;
  348. }
  349. tx_noautoflush = 0;
  350. }
  351. void FlightSimClass::xmit_big_packet(const void *p1, uint16_t n1, const void *p2, uint16_t n2)
  352. {
  353. if (!enabled || !usb_configuration) return;
  354. bool part2 = false;
  355. uint16_t remainingPart1 = n1;
  356. uint16_t remaining;
  357. const void *dataPtr = p1;
  358. bool needAdditionalFragment = false;
  359. uint8_t fragmentCounter = 1;
  360. tx_noautoflush =1; // don't mess with my data, I'm working on it!
  361. if (tx_packet) {
  362. // If we have a current packet, fill it with whatever fits
  363. uint8_t partLen = FLIGHTSIM_TX_SIZE - tx_packet->index;
  364. if (partLen > n1) partLen=n1;
  365. // copy first part
  366. memcpy(tx_packet->buf + tx_packet->index, dataPtr, partLen);
  367. remainingPart1 -= partLen;
  368. tx_packet->index += partLen;
  369. if (remainingPart1) {
  370. remaining = remainingPart1+n2;
  371. dataPtr += partLen;
  372. } else {
  373. // maybe we have space for more
  374. part2=true;
  375. partLen = FLIGHTSIM_TX_SIZE - tx_packet->index;
  376. remaining = n2;
  377. if (partLen) {
  378. memcpy(tx_packet->buf + tx_packet->index, p2, partLen);
  379. remaining -= partLen;
  380. tx_packet->index += partLen;
  381. dataPtr = p2 + partLen;
  382. }
  383. }
  384. // Packet padding should not be necessary, as xmit_big_packet
  385. // will only be called for data that doesn't fit in a single
  386. // packet. So, the previous code should always fill up the
  387. // first packet. Right?
  388. for (int i = tx_packet->index; i < FLIGHTSIM_TX_SIZE; i++) {
  389. tx_packet->buf[i] = 0;
  390. }
  391. tx_packet->len = FLIGHTSIM_TX_SIZE;
  392. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  393. tx_packet = NULL;
  394. needAdditionalFragment = true;
  395. } else {
  396. remaining = n1+n2;
  397. }
  398. while (remaining >0) {
  399. while (1) {
  400. if (usb_tx_packet_count(FLIGHTSIM_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  401. tx_packet = usb_malloc();
  402. if (tx_packet) break;
  403. }
  404. if (!enabled || !usb_configuration) {
  405. tx_noautoflush = 0;
  406. return;
  407. }
  408. tx_noautoflush = 0; // you can pick up my data, if you like
  409. yield();
  410. tx_noautoflush = 1; // wait, I'm working on the packet data
  411. }
  412. if (needAdditionalFragment) {
  413. // fragment header
  414. tx_packet->buf[0]=(remaining+3 <= FLIGHTSIM_TX_SIZE) ? (byte) remaining+3 : FLIGHTSIM_TX_SIZE;
  415. tx_packet->buf[1]=0xff;
  416. tx_packet->buf[2]=fragmentCounter++;
  417. tx_packet->index=3;
  418. }
  419. if (!part2) {
  420. // we still need to send the first part
  421. uint8_t partLen = FLIGHTSIM_TX_SIZE - tx_packet->index;
  422. if (partLen > remainingPart1)
  423. partLen=remainingPart1;
  424. memcpy(tx_packet->buf + tx_packet->index, dataPtr, partLen);
  425. dataPtr += partLen;
  426. remainingPart1 -= partLen;
  427. tx_packet->index += partLen;
  428. remaining -= partLen;
  429. if (!remainingPart1) {
  430. part2=true;
  431. dataPtr = p2;
  432. }
  433. }
  434. if (part2) {
  435. uint8_t partLen = FLIGHTSIM_TX_SIZE - tx_packet->index;
  436. if (partLen) {
  437. if (partLen > remaining)
  438. partLen=remaining;
  439. memcpy(tx_packet->buf + tx_packet->index, dataPtr, partLen);
  440. remaining -= partLen;
  441. tx_packet->index += partLen;
  442. dataPtr += partLen;
  443. }
  444. }
  445. needAdditionalFragment = true;
  446. if (tx_packet->index >= FLIGHTSIM_TX_SIZE) {
  447. tx_packet->len = FLIGHTSIM_TX_SIZE;
  448. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  449. tx_packet = NULL;
  450. }
  451. }
  452. tx_noautoflush = 0; // data is ready to be transmitted on start of USB token
  453. }
  454. extern "C" {
  455. // This gets called when a USB start token arrives.
  456. // If we have a packet to transmit AND transmission isn't disabled
  457. // by tx_noautoflush, we fill it up with zeros and send it out
  458. // to USB
  459. void usb_flightsim_flush_callback(void)
  460. {
  461. if (tx_noautoflush || !tx_packet || tx_packet->index == 0) return;
  462. for (int i=tx_packet->index; i < FLIGHTSIM_TX_SIZE; i++) {
  463. tx_packet->buf[i] = 0;
  464. }
  465. tx_packet->len = FLIGHTSIM_TX_SIZE;
  466. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  467. tx_packet = NULL;
  468. }
  469. }
  470. #endif // F_CPU
  471. #endif // FLIGHTSIM_INTERFACE