PageRenderTime 45ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/xgateTests.c

https://gitlab.com/libreems-suite/libreems-firmware
C | 551 lines | 377 code | 26 blank | 148 comment | 129 complexity | 87c8951e1dd99360ee470a175a021596 MD5 | raw file
  1. // Temporary file to make Sean's life easy!
  2. /* FreeEMS - the open source engine management system
  3. *
  4. * Copyright 2011 Sean Keys
  5. *
  6. * This file is part of the FreeEMS project.
  7. *
  8. * FreeEMS software is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * FreeEMS software is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with any FreeEMS software. If not, see http://www.gnu.org/licenses/
  20. *
  21. * We ask that if you make any changes to this file you email them upstream to
  22. * us at admin(at)diyefi(dot)org or, even better, fork the code on github.com!
  23. *
  24. * Thank you for choosing FreeEMS to run your engine!
  25. */
  26. /** @file
  27. *
  28. * @ingroup testing
  29. * @ingroup xgateFiles
  30. *
  31. * @brief XGate Timing and Bit Bang Tests
  32. *
  33. * @author Sean Keys
  34. */
  35. #define XGATETESTS_C
  36. void sleepMicro(unsigned short) FPAGE_FE;
  37. void generateCoreVars(void) LOOKUPF;
  38. /* gcc is one of the few compilers that supports nested functions in C ! */
  39. //static unsigned char waitCycles = 44;
  40. static unsigned short testCH = 0;
  41. static unsigned long delay = 2500;
  42. static unsigned short runtime = 5000; //0x0FFF; //0x0FFE - 40;
  43. static unsigned long delay2 = 10000;
  44. static unsigned long sleep = 10;
  45. static unsigned short numRuns = 0;
  46. static unsigned char once = 1;
  47. static unsigned char debug = 0;
  48. /**
  49. * This test will schedule the first eight channel in sequential fashion. Incrementing after each channel has been
  50. * scheduled both the delay and runtime
  51. */
  52. void incrementalTests() {
  53. if (Clocks.millisToTenths % 1 == 0) {
  54. numRuns++;
  55. unsigned char savedRPage = RPAGE;
  56. RPAGE = RPAGE_TUNE_TWO;
  57. *xgsInStamp = TC0;
  58. *xgsEventsToSch = 1;
  59. if (testCH == 0) {
  60. XGOutputEvents[0].channelID = testCH;
  61. XGOutputEvents[0].delay = delay;
  62. XGOutputEvents[0].runtime = runtime + numRuns;
  63. } else if (testCH == 1) {
  64. XGOutputEvents[0].channelID = testCH;
  65. XGOutputEvents[0].delay = delay;
  66. XGOutputEvents[0].runtime = runtime;
  67. } else if (testCH == 2) {
  68. XGOutputEvents[0].channelID = testCH;
  69. XGOutputEvents[0].delay = delay;
  70. XGOutputEvents[0].runtime = runtime - numRuns;
  71. } else if (testCH == 3) {
  72. XGOutputEvents[0].channelID = testCH;
  73. XGOutputEvents[0].delay = delay;
  74. XGOutputEvents[0].runtime = runtime;
  75. } else if (testCH == 4) {
  76. XGOutputEvents[0].channelID = testCH;
  77. XGOutputEvents[0].delay = delay;
  78. XGOutputEvents[0].runtime = runtime + (numRuns*2);
  79. } else if (testCH == 5) {
  80. XGOutputEvents[0].channelID = testCH;
  81. XGOutputEvents[0].delay = delay;
  82. XGOutputEvents[0].runtime = runtime;
  83. } else if (testCH == 6) {
  84. XGOutputEvents[0].channelID = testCH;
  85. XGOutputEvents[0].delay = delay;
  86. XGOutputEvents[0].runtime = runtime - (numRuns*2);
  87. } else if (testCH == 7) {
  88. XGOutputEvents[0].channelID = testCH;
  89. XGOutputEvents[0].delay = delay;
  90. XGOutputEvents[0].runtime = runtime;
  91. }
  92. XGSCHEDULE();
  93. testCH++;
  94. sleep++;
  95. if (testCH > 7) {
  96. testCH = 0;
  97. }
  98. RPAGE = savedRPage;
  99. }
  100. return;
  101. }
  102. void wiringTest() {
  103. if (Clocks.millisToTenths % 100 == 0) {
  104. unsigned char savedRPage = RPAGE;
  105. RPAGE = RPAGE_TUNE_TWO;
  106. *xgsInStamp = TC0;
  107. *xgsEventsToSch = 1;
  108. XGOutputEvents[0].channelID = testCH;
  109. XGOutputEvents[0].delay = delay;
  110. XGOutputEvents[0].runtime = 0x2FFF;
  111. XGSCHEDULE();
  112. RPAGE = savedRPage;
  113. numRuns++;
  114. if (numRuns == 10) {
  115. ++testCH;
  116. if (testCH > 11) {
  117. testCH = 0;
  118. }
  119. numRuns = 0;
  120. }
  121. }
  122. return;
  123. }
  124. /**
  125. * This test will allow you to measure the latency difference between your first and last event
  126. */
  127. void firstLastDiffTest() {
  128. if (Clocks.millisToTenths % 100 == 0) {
  129. PORTB = 0xFF;
  130. //sleep(1);
  131. PORTB = 0;
  132. unsigned char savedRPage = RPAGE;
  133. RPAGE = RPAGE_TUNE_TWO;
  134. *xgsInStamp = TC0;
  135. *xgsEventsToSch = 2;
  136. XGOutputEvents[0].channelID = 0;
  137. XGOutputEvents[0].delay = 2500;
  138. XGOutputEvents[0].runtime = 2500;
  139. XGOutputEvents[1].channelID = 35;
  140. XGOutputEvents[1].delay = 2500;
  141. XGOutputEvents[1].runtime = 2500;
  142. XGSCHEDULE();
  143. RPAGE = savedRPage;
  144. }
  145. return;
  146. }
  147. /**
  148. * A test.
  149. */
  150. void singleTest() {
  151. if (Clocks.millisToTenths % 100 == 0) {
  152. PORTB = 0xFF;
  153. PORTB = 0xFF;
  154. PORTB = 0xFF;
  155. PORTB = 0xFF;
  156. PORTB = 0xFF;
  157. PORTB = 0xFF;
  158. //sleep(1);
  159. PORTB = 0;
  160. unsigned char savedRPage = RPAGE;
  161. RPAGE = RPAGE_TUNE_TWO;
  162. *xgsInStamp = TC0;
  163. *xgsEventsToSch = 2;
  164. XGOutputEvents[0].channelID = testCH;
  165. XGOutputEvents[0].delay = 2500;
  166. XGOutputEvents[0].runtime = 2500;
  167. XGOutputEvents[1].channelID = testCH + 1;
  168. XGOutputEvents[1].delay = 5000;
  169. XGOutputEvents[1].runtime = 5000;
  170. XGSCHEDULE();
  171. RPAGE = savedRPage;
  172. }
  173. testCH++;
  174. if (testCH > 2) {
  175. testCH = 0;
  176. }
  177. return;
  178. }
  179. /**
  180. * This test will allow you to observe overlaps be varying the delays
  181. */
  182. void delayShiftDiffTest() {
  183. if (Clocks.millisToTenths % 2 == 0) {
  184. unsigned char savedRPage = RPAGE;
  185. RPAGE = RPAGE_TUNE_TWO;
  186. *xgsInStamp = TC0;
  187. *xgsEventsToSch = 1;
  188. if (testCH == 0) {
  189. XGOutputEvents[0].channelID = testCH;
  190. XGOutputEvents[0].delay = delay;
  191. XGOutputEvents[0].runtime = DELAYTESTPW;
  192. } else if (testCH == 1) {
  193. XGOutputEvents[0].channelID = testCH;
  194. XGOutputEvents[0].delay = delay2;
  195. XGOutputEvents[0].runtime = DELAYTESTPW;
  196. } else if (testCH == 2) {
  197. XGOutputEvents[0].channelID = testCH;
  198. XGOutputEvents[0].delay = delay + 1;
  199. XGOutputEvents[0].runtime = DELAYTESTPW;
  200. } else if (testCH == 3) {
  201. XGOutputEvents[0].channelID = testCH;
  202. XGOutputEvents[0].delay = delay2 - 1;
  203. XGOutputEvents[0].runtime = DELAYTESTPW;
  204. } else if (testCH == 4) {
  205. XGOutputEvents[0].channelID = testCH;
  206. XGOutputEvents[0].delay = delay + 2;
  207. XGOutputEvents[0].runtime = DELAYTESTPW;
  208. } else if (testCH == 5) {
  209. XGOutputEvents[0].channelID = testCH;
  210. XGOutputEvents[0].delay = delay2 - 2;
  211. XGOutputEvents[0].runtime = DELAYTESTPW;
  212. } else if (testCH == 6) {
  213. XGOutputEvents[0].channelID = testCH;
  214. XGOutputEvents[0].delay = delay + 3;
  215. XGOutputEvents[0].runtime = DELAYTESTPW;
  216. } else if (testCH == 7) {
  217. XGOutputEvents[0].channelID = testCH;
  218. XGOutputEvents[0].delay = delay2 - 3;
  219. XGOutputEvents[0].runtime = DELAYTESTPW;
  220. }
  221. XGSCHEDULE();
  222. testCH++;
  223. delay++;
  224. delay2--;
  225. sleep++;
  226. if (delay > 10000) {
  227. delay = 200;
  228. }
  229. if (delay2 > 10000) {
  230. delay2 = 10000;
  231. }
  232. if (testCH > 7) {
  233. testCH = 0;
  234. }
  235. RPAGE = savedRPage;
  236. }
  237. }
  238. /**
  239. * This test will allow you to observe how log it takes to schedule 1-6 events
  240. */
  241. void scheduleLatencyTest() {
  242. if (Clocks.millisToTenths % 100 == 0) {
  243. unsigned char savedRPage = RPAGE;
  244. RPAGE = RPAGE_TUNE_TWO;
  245. PORTB = 0xFF;
  246. PORTB = 0xFF;
  247. PORTB = 0xFF;
  248. PORTB = 0xFF;
  249. PORTB = 0;
  250. *xgsInStamp = TC0;
  251. *xgsEventsToSch = 6;
  252. delay = 2500;
  253. runtime = 2500;
  254. XGOutputEvents[0].channelID = testCH;
  255. XGOutputEvents[0].delay = delay;
  256. XGOutputEvents[0].runtime = runtime;
  257. XGOutputEvents[1].channelID = testCH + 1;
  258. XGOutputEvents[1].delay = delay;
  259. XGOutputEvents[1].runtime = runtime;
  260. XGOutputEvents[2].channelID = testCH + 2;
  261. XGOutputEvents[2].delay = delay;
  262. XGOutputEvents[2].runtime = runtime;
  263. XGOutputEvents[3].channelID = testCH + 3;
  264. XGOutputEvents[3].delay = delay;
  265. XGOutputEvents[3].runtime = runtime;
  266. XGOutputEvents[4].channelID = testCH + 4;
  267. XGOutputEvents[4].delay = delay;
  268. XGOutputEvents[4].runtime = runtime;
  269. XGOutputEvents[5].channelID = testCH + 5;
  270. XGOutputEvents[5].delay = delay;
  271. XGOutputEvents[5].runtime = runtime;
  272. XGSCHEDULE();
  273. RPAGE = savedRPage;
  274. }
  275. }
  276. /**
  277. * This test will allow you to observe how log it takes to schedule 1-6 events
  278. */
  279. //void competingInterruptsTest() {
  280. // if (Clocks.millisToTenths % 10 == 0) {
  281. // unsigned int i = 0;
  282. //
  283. // do {
  284. // XGSEM = 0x0101;
  285. // if (i) {
  286. // PORTA = 0xFF;
  287. // }
  288. // i++;
  289. // } while (!(XGSEM & 0x01));
  290. // XGSEM = 0x0100;
  291. //
  292. // PORTB |= 0x80;
  293. // unsigned char savedRPage = RPAGE;
  294. // RPAGE = RPAGE_TUNE_TWO;
  295. // *xgsInStamp = TC0;
  296. // *xgsEventsToSch = 6;
  297. // //*(unsigned short*)(eventFlags1 - 0x8000) = (unsigned short)0;
  298. // //*(unsigned short*)(eventFlags1 - 0x8000) = (unsigned short)0;
  299. // delay++;
  300. // //runtime++;
  301. // XGOutputEvents[0].channelID = testCH;
  302. // XGOutputEvents[0].delay = delay;
  303. // XGOutputEvents[0].runtime = runtime;
  304. // XGOutputEvents[1].channelID = testCH + 1;
  305. // XGOutputEvents[1].delay = delay + 200;
  306. // XGOutputEvents[1].runtime = runtime;
  307. // XGOutputEvents[2].channelID = testCH + 2;
  308. // XGOutputEvents[2].delay = delay + 3 + numRuns;
  309. // XGOutputEvents[2].runtime = runtime;
  310. // XGOutputEvents[3].channelID = testCH + 3;
  311. // XGOutputEvents[3].delay = delay + numRuns;
  312. // XGOutputEvents[3].runtime = runtime + 4;
  313. // XGOutputEvents[4].channelID = testCH + 4;
  314. // XGOutputEvents[4].delay = 0;
  315. // XGOutputEvents[4].runtime = 2500;
  316. // XGOutputEvents[5].channelID = testCH + 35;
  317. // XGOutputEvents[5].delay = delay + 5 + numRuns;
  318. // XGOutputEvents[5].runtime = runtime;
  319. // XGSCHEDULE();
  320. // RPAGE = savedRPage;
  321. // PORTB &= 0x7F;
  322. // }
  323. // if (runtime > 10500) {
  324. // runtime = 250;
  325. // }
  326. // numRuns++;
  327. //}
  328. /**
  329. *
  330. */
  331. void metronomeOverlapTests() {
  332. if (once) {
  333. delay = 45000;
  334. --once;
  335. }
  336. if (Clocks.millisToTenths % 10 == 0) {
  337. unsigned char savedRPage = RPAGE;
  338. RPAGE = RPAGE_TUNE_TWO;
  339. *xgsInStamp = TC0;
  340. *xgsEventsToSch = 1;
  341. if (Clocks.millisToTenths % 100 == 0) {
  342. if(numRuns % 10 == 0) {
  343. PORTB |= (1 << 1);
  344. sleepMicro(20);
  345. PORTB &= ~(1 << 1);
  346. XGOutputEvents[0].channelID = 1;
  347. XGOutputEvents[0].delay = runtime;
  348. XGOutputEvents[0].runtime = runtime;
  349. XGSCHEDULE()
  350. } else {
  351. if(testCH > 1) {
  352. PORTB |= (1 << testCH);
  353. sleepMicro(20);
  354. PORTB &= ~(1 << testCH);
  355. }
  356. if (testCH == 2) {
  357. XGOutputEvents[0].channelID = testCH;
  358. XGOutputEvents[0].delay = delay;
  359. XGOutputEvents[0].runtime = runtime - numRuns;
  360. } else if (testCH == 3) {
  361. XGOutputEvents[0].channelID = testCH;
  362. XGOutputEvents[0].delay = delay;
  363. XGOutputEvents[0].runtime = runtime;
  364. } else if (testCH == 4) {
  365. XGOutputEvents[0].channelID = testCH;
  366. XGOutputEvents[0].delay = delay;
  367. XGOutputEvents[0].runtime = runtime + (numRuns * 2);
  368. } else if (testCH == 5) {
  369. XGOutputEvents[0].channelID = testCH;
  370. XGOutputEvents[0].delay = delay;
  371. XGOutputEvents[0].runtime = runtime;
  372. } else if (testCH == 6) {
  373. XGOutputEvents[0].channelID = testCH;
  374. XGOutputEvents[0].delay = delay;
  375. XGOutputEvents[0].runtime = runtime - (numRuns * 2);
  376. } else if (testCH == 7) {
  377. XGOutputEvents[0].channelID = testCH;
  378. XGOutputEvents[0].delay = delay;
  379. XGOutputEvents[0].runtime = runtime;
  380. }
  381. XGSCHEDULE()
  382. numRuns++;
  383. delay++;
  384. testCH++;
  385. }
  386. }else {
  387. XGOutputEvents[0].channelID = 0;
  388. XGOutputEvents[0].delay = runtime;
  389. XGOutputEvents[0].runtime = runtime;
  390. XGSCHEDULE()
  391. }
  392. if (testCH > 7) {
  393. testCH = 0;
  394. }
  395. RPAGE = savedRPage;
  396. }
  397. return;
  398. }
  399. void metronomeTests() {
  400. if (Clocks.millisToTenths % 100 == 0) {
  401. ++numRuns;
  402. if(numRuns % 10 == 0) {
  403. unsigned char savedRPage = RPAGE;
  404. RPAGE = RPAGE_TUNE_TWO;
  405. *xgsInStamp = TC0;
  406. *xgsEventsToSch = 1;
  407. PORTB |= (1 << testCH);
  408. sleepMicro(20);
  409. PORTB &= ~(1 << testCH);
  410. if (testCH == 0) {
  411. XGOutputEvents[0].channelID = testCH;
  412. XGOutputEvents[0].delay = 0xFFFAUL + debug++;
  413. XGOutputEvents[0].runtime = runtime;
  414. } else if (testCH == 1) {
  415. XGOutputEvents[0].channelID = testCH;
  416. XGOutputEvents[0].delay = 0xFFFFUL + (0xFFFF / 2);
  417. XGOutputEvents[0].runtime = runtime;
  418. } else if (testCH == 2) {
  419. XGOutputEvents[0].channelID = testCH;
  420. XGOutputEvents[0].delay = 0xFFFFUL + (0xFFFF / 3);
  421. XGOutputEvents[0].runtime = runtime;
  422. } else if (testCH == 3) {
  423. XGOutputEvents[0].channelID = testCH;
  424. XGOutputEvents[0].delay = delay * (numRuns / 10);
  425. XGOutputEvents[0].runtime = runtime * (numRuns / 10);
  426. } else if (testCH == 4) {
  427. XGOutputEvents[0].channelID = testCH;
  428. XGOutputEvents[0].delay = delay * (numRuns / 10);
  429. XGOutputEvents[0].runtime = runtime;
  430. } else if (testCH == 5) {
  431. XGOutputEvents[0].channelID = testCH;
  432. XGOutputEvents[0].delay = delay * (numRuns / 10);
  433. XGOutputEvents[0].runtime = runtime;
  434. } else if (testCH == 6) {
  435. XGOutputEvents[0].channelID = testCH;
  436. XGOutputEvents[0].delay = delay * (numRuns / 10);
  437. XGOutputEvents[0].runtime = runtime;
  438. } else if (testCH == 7) {
  439. XGOutputEvents[0].channelID = testCH;
  440. XGOutputEvents[0].delay = delay * (numRuns / 10);
  441. XGOutputEvents[0].runtime = runtime;
  442. }
  443. XGSCHEDULE();
  444. testCH++;
  445. sleep++;
  446. if (testCH > 7) {
  447. testCH = 0;
  448. }
  449. RPAGE = savedRPage;
  450. }
  451. }
  452. return;
  453. }
  454. //void schdulerTests() {
  455. // if (Clocks.millisToTenths % 100 == 0) {
  456. // ++numRuns;
  457. // if(numRuns % 10 == 0) {
  458. //ATOMIC_START();
  459. // unsigned long potentialDelay = 0x0FFF;
  460. // // outputEventInputEventNumbers[numRuns] = mappedEvent;
  461. // outputEventDelayFinalPeriod[testCH] = (unsigned short)potentialDelay;
  462. // outputEventPulseWidthsMath[testCH] = 0x0FFF;
  463. // outputEventExtendNumberOfRepeats[testCH] = 0;
  464. // outputEventDelayTotalPeriod[testCH] = potentialDelay; // No async accesses occur
  465. // unsigned short edgeTimeStamp = TC0; /* Save the edge time stamp */
  466. // LongTime timeStamp;
  467. // /* Install the low word */
  468. // timeStamp.timeShorts[1] = edgeTimeStamp;
  469. // /* Find out what our timer value means and put it in the high word */
  470. //// if(TFLGOF && !(edgeTimeStamp & 0x8000)){ /* see 10.3.5 paragraph 4 of 68hc11 ref manual for details */
  471. //// timeStamp.timeShorts[0] = timerExtensionClock + 1;
  472. //// }else{
  473. //// timeStamp.timeShorts[0] = timerExtensionClock;
  474. //// }
  475. // //unsigned long thisEventTimeStamp = timeStamp.timeLong;
  476. // schedulePortTPin(testCH, timeStamp);
  477. //ATOMIC_END();
  478. // testCH++;
  479. // if(testCH == fixedConfigs1.schedulingSettings.numberOfConfiguredOutputEvents) testCH = 0;
  480. // }
  481. //}
  482. //}
  483. void stagedInjectionTests() {
  484. #define TIME_DIVISOR 50UL //6000 rpm
  485. if (Clocks.millisToTenths % TIME_DIVISOR == 0) {
  486. ATOMIC_START();
  487. *ticksPerDegree = (unsigned short)((ticks_per_degree_multiplier * (1250UL * TIME_DIVISOR) ) / (720UL * ANGLE_FACTOR));
  488. //CoreVars->DRPM = #define TICKS_PER_DEGREE_MULTIPLIER (10 * ANGLE_FACTOR) // FIX <<< shouldn't be done like this.
  489. generateCoreVars();
  490. //CoreVars->RPM = RPM(6000); //these need to match TIME_DIVISOR or your scope results wont
  491. unsigned char outputEventNumber = 0;
  492. outputEventDelayTotalPeriod[outputEventNumber] = 200;
  493. outputEventPulseWidthsMath[outputEventNumber] = runtime + DerivedVars->IDT;
  494. unsigned short edgeTimeStamp = TC0;
  495. LongTime timeStamp;
  496. /* Install the low word */
  497. timeStamp.timeShorts[1] = edgeTimeStamp;
  498. /* Find out what our timer value means and put it in the high word */
  499. if (TFLGOF && !(edgeTimeStamp & 0x8000)) { /* see 10.3.5 paragraph 4 of 68hc11 ref manual for details */
  500. timeStamp.timeShorts[0] = timerExtensionClock + 1;
  501. } else {
  502. timeStamp.timeShorts[0] = timerExtensionClock;
  503. }
  504. schedulePortTPin(outputEventNumber, timeStamp);
  505. /* roll back and fwd */
  506. if(once){
  507. runtime += 30;
  508. }else {
  509. runtime -= 30;
  510. }
  511. if(runtime > 34000) {
  512. once = 0;
  513. }
  514. if(runtime < 5000){
  515. once = 1;
  516. }
  517. ATOMIC_END();
  518. }
  519. }
  520. wiringTest();
  521. //incrementalTests();
  522. //metronomeTests();
  523. //metronomeOverlapTests();
  524. //schdulerTests();
  525. //stagedInjectionTests();