Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

709 Zeilen
16KB

  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. /// JB
  43. FlightSimEvent * FlightSimEvent::first = NULL;
  44. FlightSimEvent * FlightSimEvent::last = NULL;
  45. FlightSimData * FlightSimData::first = NULL;
  46. FlightSimData * FlightSimData::last = NULL;
  47. /// JB End
  48. uint8_t FlightSimClass::enabled = 0;
  49. uint8_t FlightSimClass::request_id_messages = 0;
  50. unsigned long FlightSimClass::frameCount = 0;
  51. elapsedMillis FlightSimClass::enableTimeout;
  52. static unsigned int unassigned_id = 1; // TODO: move into FlightSimClass
  53. FlightSimCommand::FlightSimCommand()
  54. {
  55. id = unassigned_id++;
  56. if (!first) {
  57. first = this;
  58. } else {
  59. last->next = this;
  60. }
  61. last = this;
  62. name = NULL;
  63. next = NULL;
  64. FlightSimClass::request_id_messages = 1;
  65. }
  66. void FlightSimCommand::identify(void)
  67. {
  68. uint8_t len, buf[6];
  69. if (!FlightSim.enabled || !name) return;
  70. len = strlen((const char *)name);
  71. buf[0] = len + 6;
  72. buf[1] = 1;
  73. buf[2] = id;
  74. buf[3] = id >> 8;
  75. buf[4] = 0;
  76. buf[5] = 0;
  77. FlightSimClass::xmit(buf, 6, name, len);
  78. }
  79. void FlightSimCommand::sendcmd(uint8_t n)
  80. {
  81. uint8_t buf[4];
  82. if (!FlightSim.enabled || !name) return;
  83. buf[0] = 4;
  84. buf[1] = n;
  85. buf[2] = id;
  86. buf[3] = id >> 8;
  87. FlightSimClass::xmit(buf, 4, NULL, 0);
  88. }
  89. /// JB
  90. FlightSimEvent::FlightSimEvent()
  91. {
  92. id = unassigned_id++;
  93. if (!first) {
  94. first = this;
  95. } else {
  96. last->next = this;
  97. }
  98. last = this;
  99. name = NULL;
  100. next = NULL;
  101. occur_callback = NULL;
  102. occurredFlag = 0;
  103. callbackInfo = NULL;
  104. hasCallbackInfo = 0;
  105. value = 0;
  106. FlightSimClass::request_id_messages = 1;
  107. }
  108. void FlightSimEvent::identify(void)
  109. {
  110. uint8_t len, buf[6];
  111. if (!FlightSim.enabled || !name) return;
  112. len = strlen((const char *)name);
  113. buf[0] = len + 6;
  114. buf[1] = 1;
  115. buf[2] = id;
  116. buf[3] = id >> 8;
  117. buf[4] = 3;
  118. buf[5] = 0;
  119. FlightSimClass::xmit(buf, 6, name, len);
  120. }
  121. void FlightSimEvent::send(unsigned int data, unsigned int flags)
  122. {
  123. uint8_t buf[4];
  124. uint32_t txData[2];
  125. if (!FlightSim.enabled || !name) return;
  126. buf[0] = 12;
  127. buf[1] = 7;
  128. buf[2] = id;
  129. buf[3] = id >> 8;
  130. value = data;
  131. txData[0] = data;
  132. txData[1] = flags;
  133. FlightSimClass::xmit(buf, 4, (uint8_t *)&txData, 8);
  134. }
  135. void FlightSimEvent::update(long val)
  136. {
  137. value = (unsigned int) val;
  138. occurredFlag = true;
  139. if (occur_callback) {
  140. if (!hasCallbackInfo) {
  141. (*occur_callback)(val);
  142. } else {
  143. (*(void(*)(long,void*))occur_callback)(val,callbackInfo);
  144. }
  145. }
  146. }
  147. FlightSimEvent * FlightSimEvent::find(unsigned int n)
  148. {
  149. for (FlightSimEvent *p = first; p; p = p->next) {
  150. if (p->id == n) return p;
  151. }
  152. return NULL;
  153. }
  154. FlightSimData::FlightSimData()
  155. {
  156. id = unassigned_id++;
  157. if (!first) {
  158. first = this;
  159. } else {
  160. last->next = this;
  161. }
  162. last = this;
  163. name = NULL;
  164. next = NULL;
  165. valueLen = 0;
  166. hasCallbackInfo = 0;
  167. callbackWithObject = 0;
  168. callbackInfo = NULL;
  169. change_callback = NULL;
  170. FlightSimClass::request_id_messages = 1;
  171. }
  172. void FlightSimData::identify(void)
  173. {
  174. uint8_t len, buf[6];
  175. if (!FlightSim.enabled || !name) return;
  176. len = strlen((const char *)name);
  177. buf[0] = len + 6;
  178. buf[1] = 1;
  179. buf[2] = id;
  180. buf[3] = id >> 8;
  181. buf[4] = 4;
  182. buf[5] = 0;
  183. FlightSimClass::xmit(buf, 6, name, len);
  184. }
  185. void FlightSimData::update(char *val, size_t len)
  186. {
  187. valueLen = len;
  188. memcpy(value, val, len);
  189. if (len<FLIGHTSIM_DATA_MAXLEN) {
  190. memset(value+len,0,FLIGHTSIM_DATA_MAXLEN-len);
  191. }
  192. if (change_callback) {
  193. if (!callbackWithObject) {
  194. if (!hasCallbackInfo) {
  195. (*change_callback)(value);
  196. } else {
  197. (*(void(*)(char*,void*))change_callback)(value,callbackInfo);
  198. }
  199. } else {
  200. if (!hasCallbackInfo) {
  201. (*(void(*)(FlightSimData*))change_callback)(this);
  202. } else {
  203. (*(void(*)(FlightSimData*,void*))change_callback)(this,callbackInfo);
  204. }
  205. }
  206. }
  207. }
  208. FlightSimData * FlightSimData::find(unsigned int n)
  209. {
  210. for (FlightSimData *p = first; p; p = p->next) {
  211. if (p->id == n) return p;
  212. }
  213. return NULL;
  214. }
  215. /// JB End
  216. FlightSimInteger::FlightSimInteger()
  217. {
  218. id = unassigned_id++;
  219. if (!first) {
  220. first = this;
  221. } else {
  222. last->next = this;
  223. }
  224. last = this;
  225. name = NULL;
  226. next = NULL;
  227. value = 0;
  228. change_callback = NULL;
  229. callbackInfo = NULL;
  230. hasCallbackInfo = false;
  231. FlightSimClass::request_id_messages = 1;
  232. }
  233. void FlightSimInteger::identify(void)
  234. {
  235. uint8_t len, buf[6];
  236. if (!FlightSim.enabled || !name) return;
  237. len = strlen((const char *)name);
  238. buf[0] = len + 6;
  239. buf[1] = 1;
  240. buf[2] = id;
  241. buf[3] = id >> 8;
  242. buf[4] = 1;
  243. buf[5] = 0;
  244. FlightSimClass::xmit(buf, 6, name, len);
  245. }
  246. void FlightSimInteger::write(long val)
  247. {
  248. uint8_t buf[6];
  249. value = val;
  250. if (!FlightSim.enabled || !name) return; // TODO: mark as dirty
  251. buf[0] = 10;
  252. buf[1] = 2;
  253. buf[2] = id;
  254. buf[3] = id >> 8;
  255. buf[4] = 1;
  256. buf[5] = 0;
  257. FlightSimClass::xmit(buf, 6, (uint8_t *)&value, 4);
  258. }
  259. void FlightSimInteger::update(long val)
  260. {
  261. value = val;
  262. if (change_callback) {
  263. if (!hasCallbackInfo) {
  264. (*change_callback)(val);
  265. } else {
  266. (*(void(*)(long,void*))change_callback)(val,callbackInfo);
  267. }
  268. }
  269. }
  270. FlightSimInteger * FlightSimInteger::find(unsigned int n)
  271. {
  272. for (FlightSimInteger *p = first; p; p = p->next) {
  273. if (p->id == n) return p;
  274. }
  275. return NULL;
  276. }
  277. FlightSimFloat::FlightSimFloat()
  278. {
  279. id = unassigned_id++;
  280. if (!first) {
  281. first = this;
  282. } else {
  283. last->next = this;
  284. }
  285. last = this;
  286. name = NULL;
  287. next = NULL;
  288. value = 0;
  289. change_callback = NULL;
  290. hasCallbackInfo = false;
  291. callbackInfo = NULL;
  292. FlightSimClass::request_id_messages = 1;
  293. }
  294. void FlightSimFloat::identify(void)
  295. {
  296. uint8_t len, buf[6];
  297. if (!FlightSim.enabled || !name) return;
  298. len = strlen((const char *)name);
  299. buf[0] = len + 6;
  300. buf[1] = 1;
  301. buf[2] = id;
  302. buf[3] = id >> 8;
  303. buf[4] = 2;
  304. buf[5] = 0;
  305. FlightSimClass::xmit(buf, 6, name, len);
  306. }
  307. void FlightSimFloat::write(float val)
  308. {
  309. uint8_t buf[6];
  310. value = val;
  311. if (!FlightSim.enabled || !name) return; // TODO: mark as dirty
  312. buf[0] = 10;
  313. buf[1] = 2;
  314. buf[2] = id;
  315. buf[3] = id >> 8;
  316. buf[4] = 2;
  317. buf[5] = 0;
  318. FlightSimClass::xmit(buf, 6, (uint8_t *)&value, 4);
  319. }
  320. void FlightSimFloat::update(float val)
  321. {
  322. value = val;
  323. if (change_callback) { // add: JB
  324. if (!hasCallbackInfo) {
  325. (*change_callback)(val);
  326. } else {
  327. (*(void(*)(float,void*))change_callback)(val,callbackInfo);
  328. }
  329. }
  330. }
  331. FlightSimFloat * FlightSimFloat::find(unsigned int n)
  332. {
  333. for (FlightSimFloat *p = first; p; p = p->next) {
  334. if (p->id == n) return p;
  335. }
  336. return NULL;
  337. }
  338. FlightSimClass::FlightSimClass()
  339. {
  340. }
  341. void FlightSimClass::update(void)
  342. {
  343. uint8_t len, maxlen, type, *p, *end;
  344. union {
  345. uint8_t b[4];
  346. long l;
  347. float f;
  348. } data;
  349. usb_packet_t *rx_packet;
  350. uint16_t id;
  351. while (1) {
  352. if (!usb_configuration) break;
  353. rx_packet = usb_rx(FLIGHTSIM_RX_ENDPOINT);
  354. if (!rx_packet) break;
  355. p = rx_packet->buf;
  356. end = p + 64;
  357. maxlen = 64;
  358. do {
  359. len = p[0];
  360. if (len < 2 || len > maxlen) break;
  361. switch (p[1]) {
  362. case 0x02: // write data
  363. if (len < 10) break;
  364. id = p[2] | (p[3] << 8);
  365. type = p[4];
  366. if (type == 1) {
  367. FlightSimInteger *item = FlightSimInteger::find(id);
  368. if (!item) break;
  369. #ifdef KINETISK
  370. data.l = *(long *)(p + 6);
  371. #else
  372. data.b[0] = p[6];
  373. data.b[1] = p[7];
  374. data.b[2] = p[8];
  375. data.b[3] = p[9];
  376. #endif
  377. item->update(data.l);
  378. } else if (type == 2) {
  379. FlightSimFloat *item = FlightSimFloat::find(id);
  380. if (!item) break;
  381. data.b[0] = p[6];
  382. data.b[1] = p[7];
  383. data.b[2] = p[8];
  384. data.b[3] = p[9];
  385. item->update(data.f);
  386. /// JB
  387. } else if (type == 3) {
  388. FlightSimEvent *item = FlightSimEvent::find(id);
  389. if (!item) break;
  390. #ifdef KINETISK
  391. data.l = *(long *)(p + 6);
  392. #else
  393. data.b[0] = p[6];
  394. data.b[1] = p[7];
  395. data.b[2] = p[8];
  396. data.b[3] = p[9];
  397. #endif
  398. item->update(data.f);
  399. } else if (type == 4) {
  400. FlightSimData *item = FlightSimData::find(id);
  401. if (!item) break;
  402. item->update(((char*)p)+6,len-6);
  403. /// JB End
  404. }
  405. break;
  406. case 0x03: // enable/disable
  407. if (len < 4) break;
  408. switch (p[2]) {
  409. case 1:
  410. request_id_messages = 1;
  411. /* no break */
  412. case 2:
  413. enable();
  414. frameCount++;
  415. break;
  416. case 3:
  417. disable();
  418. }
  419. }
  420. p += len;
  421. maxlen -= len;
  422. } while (p < end);
  423. usb_free(rx_packet);
  424. }
  425. if (enabled && request_id_messages) {
  426. request_id_messages = 0;
  427. for (FlightSimCommand *p = FlightSimCommand::first; p; p = p->next) {
  428. p->identify();
  429. }
  430. /// JB
  431. for (FlightSimEvent *p = FlightSimEvent::first; p; p = p->next) {
  432. p->identify();
  433. }
  434. for (FlightSimData *p = FlightSimData::first; p; p=p->next) {
  435. p->identify();
  436. }
  437. /// JB End
  438. for (FlightSimInteger *p = FlightSimInteger::first; p; p = p->next) {
  439. p->identify();
  440. // TODO: send any dirty data
  441. }
  442. for (FlightSimFloat *p = FlightSimFloat::first; p; p = p->next) {
  443. p->identify();
  444. // TODO: send any dirty data
  445. }
  446. }
  447. }
  448. bool FlightSimClass::isEnabled(void)
  449. {
  450. if (!usb_configuration) return false;
  451. if (!enabled) return false;
  452. if (enableTimeout > 1500) return false;
  453. return true;
  454. }
  455. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  456. #define TX_PACKET_LIMIT 8
  457. static usb_packet_t *tx_packet=NULL;
  458. static volatile uint8_t tx_noautoflush=0;
  459. void FlightSimClass::xmit(const void *p1, uint8_t n1, const void *p2, uint8_t n2)
  460. {
  461. uint16_t total;
  462. total = n1 + n2;
  463. if (total > FLIGHTSIM_TX_SIZE) {
  464. xmit_big_packet(p1, n1, p2, n2);
  465. return;
  466. }
  467. if (!enabled || !usb_configuration) return;
  468. tx_noautoflush = 1;
  469. if (tx_packet) {
  470. if (total <= FLIGHTSIM_TX_SIZE - tx_packet->index) goto send;
  471. for (int i = tx_packet->index; i < FLIGHTSIM_TX_SIZE; i++) {
  472. tx_packet->buf[i] = 0;
  473. }
  474. tx_packet->len = FLIGHTSIM_TX_SIZE;
  475. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  476. tx_packet = NULL;
  477. }
  478. while (1) {
  479. if (usb_tx_packet_count(FLIGHTSIM_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  480. tx_packet = usb_malloc();
  481. if (tx_packet) break;
  482. }
  483. if (!enabled || !usb_configuration) {
  484. tx_noautoflush = 0;
  485. return;
  486. }
  487. tx_noautoflush = 0;
  488. yield();
  489. tx_noautoflush = 1;
  490. }
  491. send:
  492. memcpy(tx_packet->buf + tx_packet->index, p1, n1);
  493. tx_packet->index += n1;
  494. if (n2 > 0) {
  495. memcpy(tx_packet->buf + tx_packet->index, p2, n2);
  496. tx_packet->index += n2;
  497. }
  498. if (tx_packet->index >= FLIGHTSIM_TX_SIZE) {
  499. tx_packet->len = FLIGHTSIM_TX_SIZE;
  500. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  501. tx_packet = NULL;
  502. }
  503. tx_noautoflush = 0;
  504. }
  505. void FlightSimClass::xmit_big_packet(const void *p1, uint8_t n1, const void *p2, uint8_t n2)
  506. {
  507. if (!enabled || !usb_configuration) return;
  508. uint16_t remaining = n1 + n2;
  509. if (remaining > 255) return;
  510. bool part2 = false;
  511. uint8_t remainingPart1 = n1;
  512. const uint8_t *dataPtr = (const uint8_t*)p1;
  513. bool writeFragmentHeader = false;
  514. uint8_t fragmentCounter = 1;
  515. tx_noautoflush =1; // don't mess with my data, I'm working on it!
  516. if (tx_packet) {
  517. // If we have a current packet, fill it with whatever fits
  518. uint8_t partLen = FLIGHTSIM_TX_SIZE - tx_packet->index;
  519. if (partLen > n1) partLen=n1;
  520. // copy first part, containing total packet length
  521. memcpy(tx_packet->buf + tx_packet->index, dataPtr, partLen);
  522. remainingPart1 -= partLen;
  523. tx_packet->index += partLen;
  524. if (remainingPart1) {
  525. // there still is data from the first part that
  526. // will go to the next packet. The boolean variable
  527. // part2 remains false
  528. remaining = remainingPart1+n2;
  529. dataPtr += partLen;
  530. } else {
  531. // maybe we have space for some data from the second part
  532. part2=true;
  533. partLen = FLIGHTSIM_TX_SIZE - tx_packet->index;
  534. // there is no need here to check whether partLen is
  535. // bigger than n2. It's not. If it were, all the data
  536. // would have fit in a single packet and xmit_big_packet
  537. // would never have been called...
  538. remaining = n2;
  539. if (partLen) {
  540. memcpy(tx_packet->buf + tx_packet->index, p2, partLen);
  541. remaining -= partLen;
  542. tx_packet->index += partLen;
  543. }
  544. dataPtr = (const uint8_t*)p2 + partLen;
  545. }
  546. // Packet padding should not be necessary, as xmit_big_packet
  547. // will only be called for data that doesn't fit in a single
  548. // packet. So, the previous code should always fill up the
  549. // first packet. Right?
  550. for (int i = tx_packet->index; i < FLIGHTSIM_TX_SIZE; i++) {
  551. tx_packet->buf[i] = 0;
  552. }
  553. // queue first packet for sending
  554. tx_packet->len = FLIGHTSIM_TX_SIZE;
  555. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  556. tx_packet = NULL;
  557. writeFragmentHeader = true;
  558. } else {
  559. remaining = n1+n2;
  560. }
  561. while (remaining >0) {
  562. while (1) {
  563. // get memory for next packet
  564. if (usb_tx_packet_count(FLIGHTSIM_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  565. tx_packet = usb_malloc();
  566. if (tx_packet) {
  567. break;
  568. }
  569. }
  570. if (!enabled || !usb_configuration) {
  571. // teensy disconnected
  572. tx_noautoflush = 0;
  573. return;
  574. }
  575. tx_noautoflush = 0; // you can pick up my data, if you like
  576. yield(); // do other things and wait for memory to become free
  577. tx_noautoflush = 1; // wait, I'm working on the packet data
  578. }
  579. if (writeFragmentHeader) {
  580. tx_packet->buf[0]=(remaining+3 <= FLIGHTSIM_TX_SIZE) ? (byte) remaining+3 : FLIGHTSIM_TX_SIZE;
  581. tx_packet->buf[1]=0xff;
  582. tx_packet->buf[2]=fragmentCounter++;
  583. tx_packet->index=3;
  584. }
  585. if (!part2) {
  586. // we still need to send the first part
  587. uint8_t partLen = FLIGHTSIM_TX_SIZE - tx_packet->index;
  588. if (partLen > remainingPart1)
  589. partLen=remainingPart1;
  590. memcpy(tx_packet->buf + tx_packet->index, dataPtr, partLen);
  591. dataPtr += partLen;
  592. remainingPart1 -= partLen;
  593. tx_packet->index += partLen;
  594. remaining -= partLen;
  595. if (!remainingPart1) {
  596. part2=true;
  597. dataPtr = (const uint8_t*)p2;
  598. }
  599. }
  600. if (part2) {
  601. uint8_t partLen = FLIGHTSIM_TX_SIZE - tx_packet->index;
  602. if (partLen) {
  603. if (partLen > remaining)
  604. partLen=remaining;
  605. memcpy(tx_packet->buf + tx_packet->index, dataPtr, partLen);
  606. remaining -= partLen;
  607. tx_packet->index += partLen;
  608. dataPtr += partLen;
  609. }
  610. }
  611. writeFragmentHeader = true;
  612. if (tx_packet->index >= FLIGHTSIM_TX_SIZE) {
  613. // queue packet for sending
  614. tx_packet->len = FLIGHTSIM_TX_SIZE;
  615. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  616. tx_packet = NULL;
  617. }
  618. }
  619. tx_noautoflush = 0; // data is ready to be transmitted on start of USB token
  620. }
  621. extern "C" {
  622. // This gets called from usb_isr when a USB start token arrives.
  623. // If we have a packet to transmit AND transmission isn't disabled
  624. // by tx_noautoflush, we fill it up with zeros and send it out
  625. // to USB
  626. void usb_flightsim_flush_callback(void)
  627. {
  628. if (tx_noautoflush || !tx_packet || tx_packet->index == 0) return;
  629. for (int i=tx_packet->index; i < FLIGHTSIM_TX_SIZE; i++) {
  630. tx_packet->buf[i] = 0;
  631. }
  632. tx_packet->len = FLIGHTSIM_TX_SIZE;
  633. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  634. tx_packet = NULL;
  635. }
  636. }
  637. #endif // F_CPU
  638. #endif // FLIGHTSIM_INTERFACE