Teensy 4.1 core updated for C++20
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.

713 Zeilen
16KB

  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_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. #ifdef KINETISK
  382. data.f = *(float *)(p + 6);
  383. #else
  384. data.b[0] = p[6];
  385. data.b[1] = p[7];
  386. data.b[2] = p[8];
  387. data.b[3] = p[9];
  388. #endif
  389. item->update(data.f);
  390. /// JB
  391. } else if (type == 3) {
  392. FlightSimEvent *item = FlightSimEvent::find(id);
  393. if (!item) break;
  394. #ifdef KINETISK
  395. data.l = *(long *)(p + 6);
  396. #else
  397. data.b[0] = p[6];
  398. data.b[1] = p[7];
  399. data.b[2] = p[8];
  400. data.b[3] = p[9];
  401. #endif
  402. item->update(data.f);
  403. } else if (type == 4) {
  404. FlightSimData *item = FlightSimData::find(id);
  405. if (!item) break;
  406. item->update(((char*)p)+6,len-6);
  407. /// JB End
  408. }
  409. break;
  410. case 0x03: // enable/disable
  411. if (len < 4) break;
  412. switch (p[2]) {
  413. case 1:
  414. request_id_messages = 1;
  415. /* no break */
  416. case 2:
  417. enable();
  418. frameCount++;
  419. break;
  420. case 3:
  421. disable();
  422. }
  423. }
  424. p += len;
  425. maxlen -= len;
  426. } while (p < end);
  427. usb_free(rx_packet);
  428. }
  429. if (enabled && request_id_messages) {
  430. request_id_messages = 0;
  431. for (FlightSimCommand *p = FlightSimCommand::first; p; p = p->next) {
  432. p->identify();
  433. }
  434. /// JB
  435. for (FlightSimEvent *p = FlightSimEvent::first; p; p = p->next) {
  436. p->identify();
  437. }
  438. for (FlightSimData *p = FlightSimData::first; p; p=p->next) {
  439. p->identify();
  440. }
  441. /// JB End
  442. for (FlightSimInteger *p = FlightSimInteger::first; p; p = p->next) {
  443. p->identify();
  444. // TODO: send any dirty data
  445. }
  446. for (FlightSimFloat *p = FlightSimFloat::first; p; p = p->next) {
  447. p->identify();
  448. // TODO: send any dirty data
  449. }
  450. }
  451. }
  452. bool FlightSimClass::isEnabled(void)
  453. {
  454. if (!usb_configuration) return false;
  455. if (!enabled) return false;
  456. if (enableTimeout > 1500) return false;
  457. return true;
  458. }
  459. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  460. #define TX_PACKET_LIMIT 8
  461. static usb_packet_t *tx_packet=NULL;
  462. static volatile uint8_t tx_noautoflush=0;
  463. void FlightSimClass::xmit(const void *p1, uint8_t n1, const void *p2, uint8_t n2)
  464. {
  465. uint16_t total;
  466. total = n1 + n2;
  467. if (total > FLIGHTSIM_TX_SIZE) {
  468. xmit_big_packet(p1, n1, p2, n2);
  469. return;
  470. }
  471. if (!enabled || !usb_configuration) return;
  472. tx_noautoflush = 1;
  473. if (tx_packet) {
  474. if (total <= FLIGHTSIM_TX_SIZE - tx_packet->index) goto send;
  475. for (int i = tx_packet->index; i < FLIGHTSIM_TX_SIZE; i++) {
  476. tx_packet->buf[i] = 0;
  477. }
  478. tx_packet->len = FLIGHTSIM_TX_SIZE;
  479. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  480. tx_packet = NULL;
  481. }
  482. while (1) {
  483. if (usb_tx_packet_count(FLIGHTSIM_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  484. tx_packet = usb_malloc();
  485. if (tx_packet) break;
  486. }
  487. if (!enabled || !usb_configuration) {
  488. tx_noautoflush = 0;
  489. return;
  490. }
  491. tx_noautoflush = 0;
  492. yield();
  493. tx_noautoflush = 1;
  494. }
  495. send:
  496. memcpy(tx_packet->buf + tx_packet->index, p1, n1);
  497. tx_packet->index += n1;
  498. if (n2 > 0) {
  499. memcpy(tx_packet->buf + tx_packet->index, p2, n2);
  500. tx_packet->index += n2;
  501. }
  502. if (tx_packet->index >= FLIGHTSIM_TX_SIZE) {
  503. tx_packet->len = FLIGHTSIM_TX_SIZE;
  504. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  505. tx_packet = NULL;
  506. }
  507. tx_noautoflush = 0;
  508. }
  509. void FlightSimClass::xmit_big_packet(const void *p1, uint8_t n1, const void *p2, uint8_t n2)
  510. {
  511. if (!enabled || !usb_configuration) return;
  512. uint16_t remaining = n1 + n2;
  513. if (remaining > 255) return;
  514. bool part2 = false;
  515. uint8_t remainingPart1 = n1;
  516. const uint8_t *dataPtr = (const uint8_t*)p1;
  517. bool writeFragmentHeader = false;
  518. uint8_t fragmentCounter = 1;
  519. tx_noautoflush =1; // don't mess with my data, I'm working on it!
  520. if (tx_packet) {
  521. // If we have a current packet, fill it with whatever fits
  522. uint8_t partLen = FLIGHTSIM_TX_SIZE - tx_packet->index;
  523. if (partLen > n1) partLen=n1;
  524. // copy first part, containing total packet length
  525. memcpy(tx_packet->buf + tx_packet->index, dataPtr, partLen);
  526. remainingPart1 -= partLen;
  527. tx_packet->index += partLen;
  528. if (remainingPart1) {
  529. // there still is data from the first part that
  530. // will go to the next packet. The boolean variable
  531. // part2 remains false
  532. remaining = remainingPart1+n2;
  533. dataPtr += partLen;
  534. } else {
  535. // maybe we have space for some data from the second part
  536. part2=true;
  537. partLen = FLIGHTSIM_TX_SIZE - tx_packet->index;
  538. // there is no need here to check whether partLen is
  539. // bigger than n2. It's not. If it were, all the data
  540. // would have fit in a single packet and xmit_big_packet
  541. // would never have been called...
  542. remaining = n2;
  543. if (partLen) {
  544. memcpy(tx_packet->buf + tx_packet->index, p2, partLen);
  545. remaining -= partLen;
  546. tx_packet->index += partLen;
  547. }
  548. dataPtr = (const uint8_t*)p2 + partLen;
  549. }
  550. // Packet padding should not be necessary, as xmit_big_packet
  551. // will only be called for data that doesn't fit in a single
  552. // packet. So, the previous code should always fill up the
  553. // first packet. Right?
  554. for (int i = tx_packet->index; i < FLIGHTSIM_TX_SIZE; i++) {
  555. tx_packet->buf[i] = 0;
  556. }
  557. // queue first packet for sending
  558. tx_packet->len = FLIGHTSIM_TX_SIZE;
  559. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  560. tx_packet = NULL;
  561. writeFragmentHeader = true;
  562. } else {
  563. remaining = n1+n2;
  564. }
  565. while (remaining >0) {
  566. while (1) {
  567. // get memory for next packet
  568. if (usb_tx_packet_count(FLIGHTSIM_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  569. tx_packet = usb_malloc();
  570. if (tx_packet) {
  571. break;
  572. }
  573. }
  574. if (!enabled || !usb_configuration) {
  575. // teensy disconnected
  576. tx_noautoflush = 0;
  577. return;
  578. }
  579. tx_noautoflush = 0; // you can pick up my data, if you like
  580. yield(); // do other things and wait for memory to become free
  581. tx_noautoflush = 1; // wait, I'm working on the packet data
  582. }
  583. if (writeFragmentHeader) {
  584. tx_packet->buf[0]=(remaining+3 <= FLIGHTSIM_TX_SIZE) ? (byte) remaining+3 : FLIGHTSIM_TX_SIZE;
  585. tx_packet->buf[1]=0xff;
  586. tx_packet->buf[2]=fragmentCounter++;
  587. tx_packet->index=3;
  588. }
  589. if (!part2) {
  590. // we still need to send the first part
  591. uint8_t partLen = FLIGHTSIM_TX_SIZE - tx_packet->index;
  592. if (partLen > remainingPart1)
  593. partLen=remainingPart1;
  594. memcpy(tx_packet->buf + tx_packet->index, dataPtr, partLen);
  595. dataPtr += partLen;
  596. remainingPart1 -= partLen;
  597. tx_packet->index += partLen;
  598. remaining -= partLen;
  599. if (!remainingPart1) {
  600. part2=true;
  601. dataPtr = (const uint8_t*)p2;
  602. }
  603. }
  604. if (part2) {
  605. uint8_t partLen = FLIGHTSIM_TX_SIZE - tx_packet->index;
  606. if (partLen) {
  607. if (partLen > remaining)
  608. partLen=remaining;
  609. memcpy(tx_packet->buf + tx_packet->index, dataPtr, partLen);
  610. remaining -= partLen;
  611. tx_packet->index += partLen;
  612. dataPtr += partLen;
  613. }
  614. }
  615. writeFragmentHeader = true;
  616. if (tx_packet->index >= FLIGHTSIM_TX_SIZE) {
  617. // queue packet for sending
  618. tx_packet->len = FLIGHTSIM_TX_SIZE;
  619. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  620. tx_packet = NULL;
  621. }
  622. }
  623. tx_noautoflush = 0; // data is ready to be transmitted on start of USB token
  624. }
  625. extern "C" {
  626. // This gets called from usb_isr when a USB start token arrives.
  627. // If we have a packet to transmit AND transmission isn't disabled
  628. // by tx_noautoflush, we fill it up with zeros and send it out
  629. // to USB
  630. void usb_flightsim_flush_callback(void)
  631. {
  632. if (tx_noautoflush || !tx_packet || tx_packet->index == 0) return;
  633. for (int i=tx_packet->index; i < FLIGHTSIM_TX_SIZE; i++) {
  634. tx_packet->buf[i] = 0;
  635. }
  636. tx_packet->len = FLIGHTSIM_TX_SIZE;
  637. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  638. tx_packet = NULL;
  639. }
  640. }
  641. #endif // F_CPU
  642. #endif // FLIGHTSIM_INTERFACE