Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

541 lines
13KB

  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. 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(*)(long,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. #ifdef KINETISK
  249. data.f = *(float *)(p + 6);
  250. #else
  251. data.b[0] = p[6];
  252. data.b[1] = p[7];
  253. data.b[2] = p[8];
  254. data.b[3] = p[9];
  255. #endif
  256. item->update(data.f);
  257. }
  258. break;
  259. case 0x03: // enable/disable
  260. if (len < 4) break;
  261. switch (p[2]) {
  262. case 1:
  263. request_id_messages = 1;
  264. /* no break */
  265. case 2:
  266. enable();
  267. frameCount++;
  268. break;
  269. case 3:
  270. disable();
  271. }
  272. }
  273. p += len;
  274. maxlen -= len;
  275. } while (p < end);
  276. usb_free(rx_packet);
  277. }
  278. if (enabled && request_id_messages) {
  279. request_id_messages = 0;
  280. for (FlightSimCommand *p = FlightSimCommand::first; p; p = p->next) {
  281. p->identify();
  282. }
  283. for (FlightSimInteger *p = FlightSimInteger::first; p; p = p->next) {
  284. p->identify();
  285. // TODO: send any dirty data
  286. }
  287. for (FlightSimFloat *p = FlightSimFloat::first; p; p = p->next) {
  288. p->identify();
  289. // TODO: send any dirty data
  290. }
  291. }
  292. }
  293. bool FlightSimClass::isEnabled(void)
  294. {
  295. if (!usb_configuration) return false;
  296. if (!enabled) return false;
  297. if (enableTimeout > 1500) return false;
  298. return true;
  299. }
  300. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  301. #define TX_PACKET_LIMIT 8
  302. static usb_packet_t *tx_packet=NULL;
  303. static volatile uint8_t tx_noautoflush=0;
  304. void FlightSimClass::xmit(const void *p1, uint8_t n1, const void *p2, uint8_t n2)
  305. {
  306. uint16_t total;
  307. total = n1 + n2;
  308. if (total > FLIGHTSIM_TX_SIZE) {
  309. xmit_big_packet(p1, n1, p2, n2);
  310. return;
  311. }
  312. if (!enabled || !usb_configuration) return;
  313. tx_noautoflush = 1;
  314. if (tx_packet) {
  315. if (total <= FLIGHTSIM_TX_SIZE - tx_packet->index) goto send;
  316. for (int i = tx_packet->index; i < FLIGHTSIM_TX_SIZE; i++) {
  317. tx_packet->buf[i] = 0;
  318. }
  319. tx_packet->len = FLIGHTSIM_TX_SIZE;
  320. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  321. tx_packet = NULL;
  322. }
  323. while (1) {
  324. if (usb_tx_packet_count(FLIGHTSIM_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  325. tx_packet = usb_malloc();
  326. if (tx_packet) break;
  327. }
  328. if (!enabled || !usb_configuration) {
  329. tx_noautoflush = 0;
  330. return;
  331. }
  332. tx_noautoflush = 0;
  333. yield();
  334. tx_noautoflush = 1;
  335. }
  336. send:
  337. memcpy(tx_packet->buf + tx_packet->index, p1, n1);
  338. tx_packet->index += n1;
  339. if (n2 > 0) {
  340. memcpy(tx_packet->buf + tx_packet->index, p2, n2);
  341. tx_packet->index += n2;
  342. }
  343. if (tx_packet->index >= FLIGHTSIM_TX_SIZE) {
  344. tx_packet->len = FLIGHTSIM_TX_SIZE;
  345. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  346. tx_packet = NULL;
  347. }
  348. tx_noautoflush = 0;
  349. }
  350. void FlightSimClass::xmit_big_packet(const void *p1, uint8_t n1, const void *p2, uint8_t n2)
  351. {
  352. if (!enabled || !usb_configuration) return;
  353. uint16_t remaining = n1 + n2;
  354. if (remaining > 255) return;
  355. bool part2 = false;
  356. uint8_t remainingPart1 = n1;
  357. const uint8_t *dataPtr = (const uint8_t*)p1;
  358. bool writeFragmentHeader = 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, containing total packet length
  366. memcpy(tx_packet->buf + tx_packet->index, dataPtr, partLen);
  367. remainingPart1 -= partLen;
  368. tx_packet->index += partLen;
  369. if (remainingPart1) {
  370. // there still is data from the first part that
  371. // will go to the next packet. The boolean variable
  372. // part2 remains false
  373. remaining = remainingPart1+n2;
  374. dataPtr += partLen;
  375. } else {
  376. // maybe we have space for some data from the second part
  377. part2=true;
  378. partLen = FLIGHTSIM_TX_SIZE - tx_packet->index;
  379. // there is no need here to check whether partLen is
  380. // bigger than n2. It's not. If it were, all the data
  381. // would have fit in a single packet and xmit_big_packet
  382. // would never have been called...
  383. remaining = n2;
  384. if (partLen) {
  385. memcpy(tx_packet->buf + tx_packet->index, p2, partLen);
  386. remaining -= partLen;
  387. tx_packet->index += partLen;
  388. }
  389. dataPtr = (const uint8_t*)p2 + partLen;
  390. }
  391. // Packet padding should not be necessary, as xmit_big_packet
  392. // will only be called for data that doesn't fit in a single
  393. // packet. So, the previous code should always fill up the
  394. // first packet. Right?
  395. for (int i = tx_packet->index; i < FLIGHTSIM_TX_SIZE; i++) {
  396. tx_packet->buf[i] = 0;
  397. }
  398. // queue first packet for sending
  399. tx_packet->len = FLIGHTSIM_TX_SIZE;
  400. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  401. tx_packet = NULL;
  402. writeFragmentHeader = true;
  403. } else {
  404. remaining = n1+n2;
  405. }
  406. while (remaining >0) {
  407. while (1) {
  408. // get memory for next packet
  409. if (usb_tx_packet_count(FLIGHTSIM_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  410. tx_packet = usb_malloc();
  411. if (tx_packet) {
  412. break;
  413. }
  414. }
  415. if (!enabled || !usb_configuration) {
  416. // teensy disconnected
  417. tx_noautoflush = 0;
  418. return;
  419. }
  420. tx_noautoflush = 0; // you can pick up my data, if you like
  421. yield(); // do other things and wait for memory to become free
  422. tx_noautoflush = 1; // wait, I'm working on the packet data
  423. }
  424. if (writeFragmentHeader) {
  425. tx_packet->buf[0]=(remaining+3 <= FLIGHTSIM_TX_SIZE) ? (byte) remaining+3 : FLIGHTSIM_TX_SIZE;
  426. tx_packet->buf[1]=0xff;
  427. tx_packet->buf[2]=fragmentCounter++;
  428. tx_packet->index=3;
  429. }
  430. if (!part2) {
  431. // we still need to send the first part
  432. uint8_t partLen = FLIGHTSIM_TX_SIZE - tx_packet->index;
  433. if (partLen > remainingPart1)
  434. partLen=remainingPart1;
  435. memcpy(tx_packet->buf + tx_packet->index, dataPtr, partLen);
  436. dataPtr += partLen;
  437. remainingPart1 -= partLen;
  438. tx_packet->index += partLen;
  439. remaining -= partLen;
  440. if (!remainingPart1) {
  441. part2=true;
  442. dataPtr = (const uint8_t*)p2;
  443. }
  444. }
  445. if (part2) {
  446. uint8_t partLen = FLIGHTSIM_TX_SIZE - tx_packet->index;
  447. if (partLen) {
  448. if (partLen > remaining)
  449. partLen=remaining;
  450. memcpy(tx_packet->buf + tx_packet->index, dataPtr, partLen);
  451. remaining -= partLen;
  452. tx_packet->index += partLen;
  453. dataPtr += partLen;
  454. }
  455. }
  456. writeFragmentHeader = true;
  457. if (tx_packet->index >= FLIGHTSIM_TX_SIZE) {
  458. // queue packet for sending
  459. tx_packet->len = FLIGHTSIM_TX_SIZE;
  460. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  461. tx_packet = NULL;
  462. }
  463. }
  464. tx_noautoflush = 0; // data is ready to be transmitted on start of USB token
  465. }
  466. extern "C" {
  467. // This gets called from usb_isr when a USB start token arrives.
  468. // If we have a packet to transmit AND transmission isn't disabled
  469. // by tx_noautoflush, we fill it up with zeros and send it out
  470. // to USB
  471. void usb_flightsim_flush_callback(void)
  472. {
  473. if (tx_noautoflush || !tx_packet || tx_packet->index == 0) return;
  474. for (int i=tx_packet->index; i < FLIGHTSIM_TX_SIZE; i++) {
  475. tx_packet->buf[i] = 0;
  476. }
  477. tx_packet->len = FLIGHTSIM_TX_SIZE;
  478. usb_tx(FLIGHTSIM_TX_ENDPOINT, tx_packet);
  479. tx_packet = NULL;
  480. }
  481. }
  482. #endif // F_CPU
  483. #endif // FLIGHTSIM_INTERFACE