due-pp-lib.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  1. //2023.20.03 win port
  2. //2023.10.06 +due_download_prog() file output, !!! +static int min_ticks
  3. //2023.10.07 +int due_download_prog_save_to_file(int fd,due_prog_t *program);
  4. //10.18 +int due_download_prog_save_to_file_command/data
  5. /*
  6. Copyright 2018, Carl Michal
  7. This file is part of due-pp-lib.
  8. due-pp-lib 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 (at
  11. your option) any later version.
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. pulse program assembler for Due pulse programmer.
  21. port C pins available C1-9, C12-19, 21-26 and 28-30 =9+8+6+3 = 26 pins
  22. could use A: 0-4 6-7 8-9 (RX0, TX0), 10-29 ? = 5+2+20 = 29 if give up RX/TX
  23. A.21 and C.30 are likely hard to use - hooked to LEDs, no headers.
  24. (8/9 = Uart, could maybe use, 21 maybe used for usb led?
  25. portB 12-21, 25-27, port D0-10
  26. A.29 and C.26 are same pin
  27. A.28 and C.29 are same pin
  28. stick with port C for now.
  29. so - we have 25 C pins, 28 A pins but 2 that are shared so 26/27 or 25/28 or 24/29
  30. C1-9, 12-19, 21-26 and 28-30. These are:
  31. C.0 = A.0 = D69 CANTX0
  32. C.1 = D33 A.1 = D68 CANRX0
  33. C.2 = D34 A.2 = A7 = D61
  34. C.3 = D35 A.3 = A6 = D60
  35. C.4 = D36 A.4 = A5 = D59
  36. C.5 = D37 A.5 =
  37. C.6 = D38 A.6 = A4 = D58
  38. C.7 = D39 A.7 = D31
  39. C.8 = D40 A.8 = D0 (RX0 no go?)
  40. C.9 = D41 A.9 = D1 (TX0 no go?)
  41. C.10 = A.10 = D19
  42. C.11 = A.11 = D18
  43. C.12 = D51 A.12 = D17
  44. C.13 = D50 A.13 = D16
  45. C.14 = D49 A.14 = D23
  46. C.15 = D48 A.15 = D24
  47. C.16 = D47 A.16 = A0 = D54
  48. C.17 = D46 A.17 = D70
  49. C.18 = D45 A.18 = D71
  50. C.19 = D44 A.19 = D42
  51. C.20 = A.20 = D43
  52. C.21 = D9 A.21 = D73 (not easy)
  53. C.22 = D8 A.22 = A3 = D57
  54. C.23 = D7 A.23 = A2 = D56
  55. C.24 = D6 A.24 = A1 = D55
  56. C.25 = D5 A.25 = D74 (MISO)
  57. C.26 = D4/D87 A.26 = D75 (MOSI)
  58. C.27 = A.27 = D76 (SCK)
  59. C.28 = D3 A.28 = D77/D10
  60. C.29 = D10/D77 A.29 = D87/D4
  61. C.30 = D72 (not easy)
  62. C.31 =
  63. */
  64. #include "due-pp-lib.h"
  65. #include <math.h>
  66. #include <unistd.h>
  67. #include <stdint.h>
  68. #include <stdlib.h>
  69. #include <stdio.h>
  70. #include <stdio.h>
  71. #include <string.h>
  72. #include <sys/file.h>
  73. #include <sys/types.h>
  74. #include <sys/stat.h>
  75. //#include "serialib.h" // Serial library
  76. //#include <fcntl.h>
  77. //#include "fcntl.h"
  78. #include <unistd.h>
  79. //#include <termios.h>
  80. //#include "termios.h"
  81. #include <sys/time.h>
  82. //#include "file.h"
  83. //#include "flock.c"
  84. #define BUFFLEN 80
  85. #define NUM_OPCODES 14
  86. #define START_LOOP 0
  87. #define END_LOOP 1
  88. #define BRANCH 2
  89. #define EXIT 3
  90. #define SUB_START 4
  91. #define SUB_END 5
  92. #define EXT_TRIG 6
  93. #define TRIG_MAX 7
  94. #define WRITE_DACS 8
  95. #define WRITE_ALT 9
  96. #define SWAP_TO_ALT 10
  97. #define SWAP_TO_DEFAULT 11
  98. #define SWAP_TO_DACS 12
  99. #define WRITE_DEFAULT 13
  100. #define CALL_SUB 255
  101. /* Operations for the `flock' call. */
  102. #define LOCK_SH 1 /* Shared lock. */
  103. #define LOCK_EX 2 /* Exclusive lock. */
  104. #define LOCK_UN 8 /* Unlock. */
  105. /* Can be OR'd in to one of the above. */
  106. #define LOCK_NB 4 /* Don't block when locking. */
  107. //
  108. #define O_NOCTTY 0
  109. #define STATE_INITIALIZED 1
  110. #define STATE_FINALIZED 2
  111. #define STATE_EXITED 3
  112. // This is the maximum length of an unrolled loop in the flash program:
  113. #define MAX_QUE_LEN 12000
  114. // branch and exit assigned to 15?
  115. // this isn't a great system since the minimums don't necessarily line up
  116. // with opcodes. Here the NUM_OPCODES element is for an ordinary event,
  117. // and NUM_OPCODES+1 is for CALL_SUB. EXT_TRIG and TRIG_MAX
  118. // have restrictions on both the event themselves and the preceding.
  119. // Some of these are for the events preceding. Take the bigger of the two and
  120. // use for both.
  121. //int min_ticks[NUM_OPCODES+2] = {20,20,20,25,0,25,20,25,25,25,25,25,25,20,10,25};
  122. static int min_ticks[NUM_OPCODES+2] = {20,20,20,25,0,25,20,25,25,25,25,25,25,20,10,25}; //! no static - ld error
  123. void checksum_data(unsigned char *c1, unsigned char *c2, int len, unsigned int *data) {
  124. // calculate checksums for the data. Based on what Bruker does in SBS.
  125. unsigned int i, ch1 = 0, ch2 = 0;
  126. // unsigned char *cdata;
  127. /* d0 d1 d2 d3 d4 d5 ... dn
  128. 1 2 3 4 5 6 ... n
  129. n+1 n n-1 n-2 ... 2
  130. for the first checksum, multiply top row elements by middle row elements. Sum, and keep low 8 bits.
  131. For the second, use the lower row.
  132. */
  133. // cdata = (unsigned char *) data;
  134. /* for (i = 0; i < len*4; i++) {
  135. ch1 += (i+1)*cdata[i];
  136. ch2 += (len*4+1-i)*cdata[i];
  137. } */
  138. for (i = 0; i < len; i++) {
  139. ch1 += (i+1)*data[i];
  140. ch2 += (len+1-i)*data[i];
  141. }
  142. *c1 = ch1 & 0xff;
  143. *c2 = ch2 & 0xff;
  144. }
  145. unsigned int due_shift_bits(unsigned int inputs, unsigned int port){
  146. // look at active port, shift accordingly
  147. unsigned int outputs=0;
  148. switch (port){
  149. case DEFAULT_PORT:
  150. //this assumes port C, missing bits 0, 10, 11, 20, 27, 30, 31
  151. // 25 useful bits.
  152. /*
  153. 0-8 -> 1-9
  154. 9-16 -> 12-19
  155. 17-22 -> 21-26
  156. 23-24 -> 28-29
  157. */
  158. //31-28 27-24 23-20 19-16 15-12 11-8 7-4 3-0
  159. outputs |= ((inputs & 0x01800000) << 5);
  160. outputs |= ((inputs & 0x007e0000) << 4);
  161. outputs |= ((inputs & 0x0001fe00) << 3);
  162. outputs |= ((inputs & 0x000001ff) << 1);
  163. // printf("shifting bits for default port was: 0x%x, now: 0x%x\n",inputs,outputs);
  164. return outputs;
  165. break;
  166. case ALT_PORT:
  167. // this assumes port A, missing bits: 5, 8, 9, 21, 28-31.
  168. //24 useful bits.
  169. /*
  170. 0-4 -> 0-4
  171. 5-6 -> 6-7
  172. 7-17 -> 10-20
  173. 18-23 -> 22-27
  174. */
  175. outputs |= ((inputs & 0x00fc0000) << 4 );
  176. outputs |= ((inputs & 0x0003ff80) << 3 );
  177. outputs |= ((inputs & 0x00000070) << 1 );
  178. outputs |= ((inputs & 0x0000000f));
  179. // printf("shifting bits for alt port was: 0x%x, now: 0x%x\n",inputs,outputs);
  180. return outputs;
  181. break;
  182. case DAC_PORT:
  183. // do nothing
  184. return inputs;
  185. break;
  186. default:
  187. printf("duepp: in shift bits with unknown port to shift for, doing nothing!\n");
  188. printf("This can happen if you have a subroutine that is never called\n");
  189. return inputs;
  190. }
  191. // return inputs;
  192. }
  193. int do_play_queue(due_prog_t *program, uint32_t whats_next){
  194. /* this fills in the jump address for the events that have been queued, these events end
  195. with a function determined by whats_next: either a loop start, a loop end, a bare branch, or
  196. a branch to exit. */
  197. // need to deal with CALL_SUB and SUB_END.
  198. if (program->queued_events > 12000 ){
  199. printf("duepp program->queued_events out of range: %i\n",program->queued_events);
  200. program->error = 1;
  201. return -1;
  202. }
  203. if (program->queued_events > 12000){
  204. printf("duepp got program->queued_events = %i, is > 12000, can't handle\n",program->queued_events);
  205. program->error = 1;
  206. return -1;
  207. }
  208. if (program->queued_events == 0){
  209. if (program->dpos > MAXDATA-2){
  210. printf("duepp: program length overrun\n");
  211. program->error = 1;
  212. return -1;
  213. }
  214. program->data[program->dpos] = (whats_next << 16);
  215. program->dpos += 1;
  216. // should only ever get: START_LOOP, EXIT, SUB_END, SWAP_TO_ALT, SWAP_TO_DEFAULT, CALL_SUB, SWAP_TO_DACS
  217. }
  218. else if (program->queued_events > 0){ // these have direct endings, optimized for speed.
  219. if (whats_next <= BRANCH){
  220. program->data[program->queue_pos] = (whats_next << 16 ) | program->queued_events;
  221. program->queued_events = 0;
  222. }
  223. else if (whats_next < NUM_OPCODES){ // all these do a branch to get to the operation.
  224. if (program->dpos > MAXDATA-2){
  225. printf("duepp: program length overrun\n");
  226. program->error = 1;
  227. return -1;
  228. }
  229. program->data[program->queue_pos] = (BRANCH << 16 ) | program->queued_events;
  230. program->queued_events = 0;
  231. program->data[program->dpos] = (whats_next << 16 ) ;
  232. program->dpos += 1;
  233. }
  234. else if (whats_next == CALL_SUB){// this one's special
  235. program->data[program->queue_pos] = (BRANCH <<16) | program->queued_events;
  236. // call_sub gets three more instructions inserted
  237. // printf("queue for call_sub, queued events: %i\n",program->queued_events);
  238. program->queued_events = 0;
  239. }
  240. else{
  241. printf("duepp: play_queue got unknown what's next\n");
  242. program->error = 1;
  243. return -1;
  244. }
  245. }
  246. return 0;
  247. }
  248. int play_queue(due_prog_t *program, int whats_next){
  249. int rval = 0;
  250. int i,dposi,j,cache_queued_events;
  251. if (program->queued_events <= MAX_QUE_LEN){
  252. return do_play_queue(program, whats_next);
  253. }
  254. // ok, so we have more than the max number of events,
  255. // (it can never be more than double though) Look for an event that's long enough
  256. // to break the queue up.
  257. if (program->dpos > MAXDATA-2){
  258. printf("duepp: not enough room left to split events\n");
  259. program->error = 1;
  260. return -1;
  261. }
  262. for (i=MAX_QUE_LEN-1;i >= program->queued_events-MAX_QUE_LEN-1;i--){
  263. // program->dpos points at the next event, each event has two entries - an output word and a timer delay
  264. dposi = program->dpos-2*program->queued_events+2*i; // points to the output word of the i'th event in the queue.
  265. if ( program->data[dposi+1] >= min_ticks[BRANCH] ) // use a 20 tick minimum for breaking up.
  266. break;
  267. }
  268. // i is the index of the last event in the first queue.
  269. if ( i >= program->queued_events-MAX_QUE_LEN-1 ){
  270. i+=1; // now i is the first event in the second queue.
  271. // slide all events from i to the end down a slot
  272. for (j = program->queued_events; j>=i ; j--){
  273. dposi = program->dpos-2*program->queued_events+2*j;
  274. program->data[dposi+2]=program->data[dposi+1];
  275. program->data[dposi+1]=program->data[dposi];
  276. // that leaves a slot just before the i'th event to stick in a new jump address
  277. }
  278. printf("duepp: breaking %i into two queues of length: %i and %i\n",program->queued_events,i,program->queued_events-i);
  279. cache_queued_events = program->queued_events;
  280. program->queued_events = i;
  281. rval = do_play_queue(program, BRANCH);
  282. if (rval < 0) return rval;
  283. // then do the rest
  284. program->queue_pos = program->dpos-2*cache_queued_events + 2*i;
  285. program->queued_events = cache_queued_events-i;
  286. do_play_queue(program, whats_next);
  287. if (rval < 0) return rval;
  288. program->dpos += 1; // add one for the new jump address
  289. }
  290. else{
  291. printf("duepp: Got %i events, more than %i, and couldn't find a spot to break it up\n",program->queued_events,MAX_QUE_LEN);
  292. program->error = 1;
  293. return -1;
  294. }
  295. return 0;
  296. }
  297. int due_add_event(due_prog_t *program, unsigned int outputs, unsigned int ticks) {
  298. // printf("due_add_event with outputs: 0x%x, ticks: %i\n",outputs,ticks);
  299. if (ticks < min_ticks[NUM_OPCODES]){
  300. printf("duepp: Got due_add_event with %i ticks. Must be at least: %i\n",ticks,min_ticks[NUM_OPCODES]);
  301. program->error = 1;
  302. return -1;
  303. }
  304. if (program->state != STATE_INITIALIZED && program->in_sub == 0){
  305. printf("duepp: to add an event, the program must be in STATE_INITIALIZED or defining a subroutine\n");
  306. program->error = 1;
  307. return -1;
  308. }
  309. if (program->queued_events == 0){
  310. if (program->dpos > MAXDATA-2){
  311. printf("duepp: program length overrun\n");
  312. program->error = 1;
  313. return -1;
  314. }
  315. program->queue_pos = program->dpos;
  316. program->dpos += 1;
  317. }
  318. program->queued_events += 1;
  319. program->events += 1;
  320. if (program->dpos > MAXDATA-3){
  321. printf("duepp: program length overrun\n");
  322. program->error = 1;
  323. return -1;
  324. }
  325. if (program->auto_shift) outputs = due_shift_bits(outputs,program->active_port);
  326. program->data[program->dpos] = outputs;
  327. program->dpos += 1;
  328. program->data[program->dpos] = ticks;
  329. program->dpos += 1;
  330. program->last_ticks = ticks;
  331. // printf("due add_event, putting outputs 0x%x in event: %i\n",outputs,program->dpos);
  332. // printf("due add_event, putting outputs 0x%x in event: %i\n",outputs,program->dpos);
  333. return 0;
  334. } //due_add_event
  335. int due_start_loop(due_prog_t *program, unsigned int loops,unsigned int outputs, unsigned int ticks) {
  336. int rval;
  337. if (program->last_ticks < min_ticks[START_LOOP]){
  338. printf("duepp: Got start_loop with %i ticks in previous event. Must be at least: %i\n",program->last_ticks,min_ticks[START_LOOP]);
  339. program->error = 1;
  340. program->error = 1;
  341. return -1;
  342. }
  343. if (program->state != STATE_INITIALIZED && program->in_sub == 0){
  344. printf("duepp: to start a loop, the program must be in STATE_INITIALIZED, or must be defining a subroutine\n");
  345. program->error = 1;
  346. return -1;
  347. }
  348. // if there are queued events, do them
  349. rval = play_queue(program, START_LOOP);
  350. if (rval < 0 ) return rval;
  351. if (program->dpos > MAXDATA-2){
  352. printf("duepp: program length overrun\n");
  353. program->error = 1;
  354. return -1;
  355. }
  356. program->data[program->dpos] = loops;
  357. program->dpos += 1;
  358. program->loop_level +=1;
  359. return due_add_event(program, outputs, ticks);
  360. }
  361. int due_end_loop(due_prog_t *program, unsigned int outputs, unsigned int ticks) {
  362. int rval;
  363. if (ticks < min_ticks[END_LOOP]){
  364. printf("duepp: Got end_loop with %i ticks. Must be at least: %i\n",ticks,min_ticks[END_LOOP]);
  365. program->error = 1;
  366. return -1;
  367. }
  368. if (program->state != STATE_INITIALIZED && program->in_sub == 0){
  369. printf("duepp: to end a loop, the program must be in STATE_INITIALIZED, or must be defining a subroutine\n");
  370. program->error = 1;
  371. return -1;
  372. }
  373. if (program->loop_level < 1){
  374. printf("duepp: Got end_loop, but no loop to end!\n");
  375. program->error = 1;
  376. return -1;
  377. }
  378. rval = due_add_event(program, outputs, ticks);
  379. if (rval < 0 ) return rval;
  380. rval = play_queue(program, END_LOOP);
  381. program->loop_level -=1;
  382. return rval;
  383. }
  384. int due_init_program(due_prog_t *program,char auto_shift){
  385. int i;
  386. program->auto_shift = auto_shift;
  387. program->active_port = DEFAULT_PORT;
  388. program->dpos = 0;
  389. program->events = 0;
  390. program->queued_events = 0; // how many events to do in the continuous sequence loop.
  391. program->state = STATE_INITIALIZED;
  392. program->in_sub = 0;
  393. program->error = 0;
  394. for (i=0;i<MAXSUB;i++){
  395. program->sub_table[i]=0;
  396. program->sub_entry_port[i] = -1;
  397. }
  398. return 0;
  399. }
  400. int due_exit_program(due_prog_t *program){
  401. int rval;
  402. if (program->last_ticks < min_ticks[EXIT]){
  403. printf("duepp: Got exit_program with %i ticks in previous event. Must be at least: %i\n",program->last_ticks,min_ticks[EXIT]);
  404. program->error = 1;
  405. return -1;
  406. }
  407. if (program->state != STATE_INITIALIZED ){
  408. printf("duepp: got exit_program, but the program must be in STATE_INITIALIZED\n");
  409. program->error = 1;
  410. return -1;
  411. }
  412. if (program->loop_level != 0){
  413. printf("duepp: Loop starts and ends don't match, expect trouble!\n");
  414. program->error = 1;
  415. return -1;
  416. }
  417. rval = play_queue(program, EXIT);
  418. if (rval < 0 ) return rval;
  419. program->state = STATE_EXITED;
  420. return 0;
  421. }
  422. int due_swap_to_alt(due_prog_t *program, unsigned int outputs, unsigned int ticks){
  423. int rval;
  424. if (program->last_ticks < min_ticks[SWAP_TO_ALT]){
  425. printf("duepp: Got swap_to_alt with %i ticks in previous event. Must be at least: %i\n",program->last_ticks,min_ticks[SWAP_TO_ALT]);
  426. program->error = 1;
  427. return -1;
  428. }
  429. if (program->state != STATE_INITIALIZED && program->in_sub == 0){
  430. printf("duepp: to swap_to_alt, the program must be in STATE_INITIALIZED, or must be defining a subroutine\n");
  431. program->error = 1;
  432. return -1;
  433. }
  434. rval = play_queue(program, SWAP_TO_ALT);
  435. if (rval < 0 ) return rval;
  436. if (program->active_port == ALT_PORT){
  437. printf("duepp: WARNING: got swap_to_alt when port was already alt?\n");
  438. }
  439. program->active_port = ALT_PORT;
  440. return due_add_event(program, outputs,ticks);
  441. }
  442. int due_swap_to_default(due_prog_t *program, unsigned int outputs, unsigned int ticks){
  443. int rval;
  444. if (program->last_ticks < min_ticks[SWAP_TO_DEFAULT]){
  445. printf("duepp: Got swap_to_default with %i ticks in previous event. Must be at least: %i\n",program->last_ticks,min_ticks[SWAP_TO_DEFAULT]);
  446. program->error = 1;
  447. return -1;
  448. }
  449. if (program->state != STATE_INITIALIZED && program->in_sub == 0){
  450. printf("duepp: to swap_to_default, the program must be in STATE_INITIALIZED, or must be defining a subroutine\n");
  451. program->error = 1;
  452. return -1;
  453. }
  454. rval = play_queue(program, SWAP_TO_DEFAULT);
  455. if (rval < 0 ) return rval;
  456. if (program->active_port == DEFAULT_PORT){
  457. printf("duepp: WARNING: got swap_to_default when port was already default?\n");
  458. }
  459. program->active_port = DEFAULT_PORT;
  460. return due_add_event(program, outputs,ticks);
  461. }
  462. int due_swap_to_dacs(due_prog_t *program, unsigned int dac0, unsigned int dac1, unsigned int ticks){
  463. int dacword,rval;
  464. if (program->last_ticks < min_ticks[SWAP_TO_DACS]){
  465. printf("duepp: Got swap_to_dacs with %i ticks in previous event. Must be at least: %i\n",program->last_ticks,min_ticks[SWAP_TO_DACS]);
  466. program->error = 1;
  467. return -1;
  468. }
  469. if (program->state != STATE_INITIALIZED && program->in_sub == 0){
  470. printf("duepp: to swap_to_dacs, the program must be in STATE_INITIALIZED, or must be defining a subroutine\n");
  471. program->error = 1;
  472. return -1;
  473. }
  474. dacword = dac0 | dac1<<16 | 1<<28;
  475. rval = play_queue(program, SWAP_TO_DACS);
  476. if (rval < 0 ) return rval;
  477. if (program->active_port == DAC_PORT){
  478. printf("duepp: WARNING: got swap_to_dacs when port was already dacs?\n");
  479. }
  480. program->active_port = DAC_PORT;
  481. return due_add_event(program, dacword,ticks);
  482. }
  483. // There are a few things that need to get filled in.
  484. // In the subroutines themselves, the first word will contain a typical branch to be resolved.
  485. // that needs to get resolved, then copied in to the callers branch spot
  486. // the SUB_END instructions also need to get resolved
  487. // Finally, we need to calculate the data offsets and give them to the caller.
  488. // the sub_table contains the information needed to do the data offsets.
  489. int due_finalize_program(due_prog_t *program){
  490. int inst,elements,i;
  491. // in here we resolve subroutines.
  492. if (program->state != STATE_EXITED || program->in_sub == 1){
  493. printf("duepp: to finalize program, the program must be in STATE_EXITED, and must not be defining a subroutine\n");
  494. program->error = 1;
  495. return -1;
  496. }
  497. if (program->loop_level != 0){
  498. printf("duepp: Loop starts and ends don't match!\n");
  499. program->error = 1;
  500. return -1;
  501. }
  502. if (program->state != STATE_EXITED){
  503. printf("duepp: got finalize_program, but haven't yet exited\n");
  504. program->error = 1;
  505. return -1;
  506. }
  507. if (program->in_sub){
  508. printf("duepp: got finalize_program, but still inside a subroutine!\n");
  509. program->error = 1;
  510. return -1;
  511. }
  512. // now in here, we need to go through and get the subroutine calls
  513. // ready. We find each subroutine call, copy the number of event and
  514. // the what's next code in from the subroutine header (which we can
  515. // locate in the sub_table) we can also calculate the data offset
  516. inst = 0 ;
  517. i=0;
  518. while( i < program->dpos){
  519. inst = program->data[i]>>16;
  520. elements = program->data[i] & 0xffff;
  521. // printf("got inst: %i, with elements: %i at pos: %i\n",inst,elements,i);
  522. if (inst == CALL_SUB ){ // elements is the subroutine_id - which we only need to look up the data address.
  523. // leave program->data[i] alone. run-time resolver will take care of it.
  524. // 2. calculate the address for the jump -into the generic branch
  525. // printf("got sub call at pos %i ",i);
  526. if (program->sub_table[elements] == 0){
  527. printf("duepp: trying to resolve a subroutine, id: %i, but subroutine not found.\n",elements);
  528. program->error = 1;
  529. return -1;
  530. }
  531. program->data[i+1] = ((program->sub_table[elements]+1) - (i+3))*4; // data offset - point it one past the jump header
  532. program->data[i+2] = program->data[program->sub_table[elements]]; // copy the jump header from the start of the subroutine.
  533. // printf(" data offset is %i, target is: %i, current is: %i\n",program->data[i+1],program->sub_table[elements]+1,(i+3));
  534. i+=3;
  535. }
  536. else{
  537. i += 1; // advance past the address
  538. i += 2*elements; // advance past the events
  539. // if (inst == START_LOOP || inst == EXIT || inst == WRITE_DACS || inst == WRITE_ALT || inst == WRITE_DEFAULT ) i += 1; // for start loop, advance past the loop count.
  540. if (inst == START_LOOP || inst == WRITE_DACS || inst == WRITE_ALT || inst == WRITE_DEFAULT ) i += 1; // for start loop, advance past the loop count.
  541. }
  542. }
  543. program->state = STATE_FINALIZED;
  544. return 0;
  545. }
  546. // during pulse prog creation, the caller's three words are:
  547. // CALL_SUB<<16 | subroutine_id
  548. // blank
  549. // blank
  550. // after finalize_program, these are replaced with:
  551. // CALL_SUB <<16
  552. // data offset
  553. // what's_next << 16 | events [ for subroutine_id] - copied
  554. // run time resolver replaces them with:
  555. // address for start_sub code
  556. // data_offset (leaves alone)
  557. // resolves this as any other event.
  558. int due_call_sub(due_prog_t *program,unsigned int subroutine_id, unsigned int outputs,unsigned int ticks){
  559. int rval;
  560. if (ticks < min_ticks[NUM_OPCODES+1]){
  561. printf("duepp: Got call_sub with %i ticks. Must be at least: %i\n",ticks,min_ticks[NUM_OPCODES+1]);
  562. program->error = 1;
  563. return -1;
  564. }
  565. if (program->state != STATE_INITIALIZED && program->in_sub == 0){
  566. printf("duepp: to call a subroutine, the program must be in STATE_INITIALIZED, or must be defining a subroutine\n");
  567. program->error = 1;
  568. return -1;
  569. }
  570. if (subroutine_id >= MAXSUB){
  571. printf("duepp: subroutine id of %i is too big, max is: %i\n",subroutine_id,MAXSUB-1);
  572. return -1;
  573. }
  574. if (program->sub_entry_port[subroutine_id] == -1){
  575. program->sub_entry_port[subroutine_id] = program->active_port;
  576. }
  577. else if (program->sub_entry_port[subroutine_id] != program->active_port){
  578. printf("duepp: WARNING: Got call to subroutine %i with active port %i. Previous call had active port: %i\n",subroutine_id,
  579. program->active_port,program->sub_entry_port[subroutine_id]);
  580. }
  581. rval = due_add_event(program, outputs,ticks);
  582. if (rval < 0) return rval;
  583. rval = play_queue(program, CALL_SUB);
  584. // play_queue will insert
  585. if (program->dpos > MAXDATA-4){
  586. printf("duepp: program length overrun\n");
  587. program->error = 1;
  588. return -1;
  589. }
  590. program->data[program->dpos] = CALL_SUB <<16 | subroutine_id; //
  591. // next is the data offset to the start of the subroutine events
  592. // and after that is the branch address.
  593. // we don't know enough at this point to fill either of those in.
  594. program->dpos += 3;
  595. return rval;
  596. }
  597. // start_sub does not create an event! Call this before created the events in the subroutine.
  598. int due_start_sub(due_prog_t *program, unsigned int subroutine_id){
  599. if (program->state != STATE_EXITED){
  600. printf("duepp: in start_sub. Program state needs to be STATE_EXITED\n");
  601. program->error = 1;
  602. return -1;
  603. }
  604. if (program->in_sub) {
  605. printf("duepp: subroutine can't be defined inside a subroutine\n");
  606. program->error = 1;
  607. return -1;
  608. }
  609. if (program->queued_events != 0){
  610. printf("duepp: got start_sub but had events queued. Shouldn't happen\n");
  611. program->error = 1;
  612. return -1;
  613. }
  614. if (subroutine_id >= MAXSUB){
  615. printf("duepp: subroutine id of %i is too big, max is: %i\n",subroutine_id,MAXSUB-1);
  616. return -1;
  617. }
  618. // record where the subroutine data starts. This actually points to the jump
  619. // address that starts the subroutine data section.
  620. // This address should never be read from this position, it gets copied into the
  621. // the data stream at the caller.
  622. program->sub_table[subroutine_id] = program->dpos;
  623. // Don't want due_add_event to leave space for a jump address
  624. // The in_sub flags tells it not to set program->queue_pos.
  625. program->in_sub = 1;
  626. program->in_sub_num = subroutine_id;
  627. program->active_port = program->sub_entry_port[subroutine_id];
  628. if (program->active_port == -1){
  629. printf("duepp: WARNING: port for entry of subroutine %i is -1, indicating the subroutine was never called\n",subroutine_id);
  630. }
  631. return 0;
  632. }
  633. int due_return_from_sub(due_prog_t *program, unsigned int outputs, unsigned int ticks){
  634. int rval;
  635. if (program->in_sub != 1){
  636. printf("duepp: got return from sub, but wasn't in a subroutine!\n");
  637. program->error = 1;
  638. return -1;
  639. }
  640. if (program->sub_entry_port[program->in_sub_num] != program->active_port){
  641. printf("duepp: WARNING: Subroutine: %i entered with active port: %i. Leaving with active port: %i\n",program->in_sub_num,
  642. program->sub_entry_port[program->in_sub_num],program->active_port);
  643. }
  644. rval = due_add_event(program, outputs,ticks);
  645. if (rval < 0) return rval;
  646. rval = play_queue(program, SUB_END);
  647. program->in_sub = 0;
  648. return rval;
  649. }
  650. int due_wait_for_trigger(due_prog_t *program,unsigned int outputs,unsigned int ticks){
  651. int rval;
  652. // waits for an external trigger. Will wait forever. The requested delay
  653. // starts when the trigger is received. The requested outputs are set
  654. // just before we start waiting
  655. if (program->last_ticks < min_ticks[EXT_TRIG]){
  656. printf("duepp: Got wait_for_trigger with %i ticks in previous event. Must be at least: %i\n",program->last_ticks,min_ticks[EXT_TRIG]);
  657. program->error = 1;
  658. return -1;
  659. }
  660. if (ticks < min_ticks[EXT_TRIG]){
  661. printf("duepp: Got wait_for_trigger with %i ticks. Must be at least: %i\n",ticks,min_ticks[EXT_TRIG]);
  662. program->error = 1;
  663. return -1;
  664. }
  665. if (program->state != STATE_INITIALIZED && program->in_sub == 0){
  666. printf("duepp: to wait for trigger, the program must be in STATE_INITIALIZED, or must be defining a subroutine\n");
  667. program->error = 1;
  668. return -1;
  669. }
  670. rval = due_add_event(program, outputs,ticks);
  671. if (rval < 0) return rval;
  672. return play_queue(program, EXT_TRIG);
  673. }
  674. int due_wait_for_trigger_max(due_prog_t *program,unsigned int outputs,unsigned int ticks){
  675. int rval;
  676. // Waits for an external trigger, but will only wait for a maximum of the
  677. // requested delay time. There's 1 us delay after the trigger is received.
  678. // The requested outpus are set just before we start waiting.
  679. if (program->last_ticks < min_ticks[TRIG_MAX]){
  680. printf("duepp: Got wait_for_trigger_max with %i ticks in previous event. Must be at least: %i\n",program->last_ticks,min_ticks[TRIG_MAX]);
  681. program->error = 1;
  682. return -1;
  683. }
  684. if (ticks < min_ticks[TRIG_MAX]){
  685. printf("duepp: Got wait_for_trigger with %i ticks. Must be at least: %i\n",ticks,min_ticks[TRIG_MAX]);
  686. program->error = 1;
  687. return -1;
  688. }
  689. if (program->state != STATE_INITIALIZED && program->in_sub == 0){
  690. printf("duepp: to wait for trigger max, the program must be in STATE_INITIALIZED, or must be defining a subroutine\n");
  691. program->error = 1;
  692. return -1;
  693. }
  694. rval = due_add_event(program, outputs,ticks);
  695. if (rval < 0) return rval;
  696. return play_queue(program, TRIG_MAX);
  697. }
  698. int due_write_dacs(due_prog_t *program, unsigned int dac0, unsigned int dac1,unsigned int outputs,unsigned int ticks){
  699. int rval;
  700. if (ticks < min_ticks[WRITE_DACS]){
  701. printf("duepp: Got write_dacs with %i ticks. Must be at least: %i\n",ticks,min_ticks[WRITE_DACS]);
  702. program->error = 1;
  703. return -1;
  704. }
  705. if (program->state != STATE_INITIALIZED && program->in_sub == 0){
  706. printf("duepp: to write_dacs, the program must be in STATE_INITIALIZED, or must be defining a subroutine\n");
  707. program->error = 1;
  708. return -1;
  709. }
  710. if (program->active_port == DAC_PORT){
  711. printf("duepp: WARNING: Got write dacs while active port is already dacs!\n");
  712. }
  713. rval = due_add_event(program, outputs,ticks);
  714. if (rval < 0) return rval;
  715. rval =play_queue(program, WRITE_DACS);
  716. if (program->dpos > MAXDATA-2){
  717. printf("duepp: program length overrun\n");
  718. program->error = 1;
  719. return -1;
  720. }
  721. program->data[program->dpos] = dac0 | dac1<<16 | 1<<28;
  722. program->dpos += 1;
  723. return rval;
  724. }
  725. int due_write_alt(due_prog_t *program,unsigned int outputs_alt,unsigned int outputs,unsigned int ticks){
  726. // first arg is the outputs for port A. Second arg is for whatever was last swapped to.
  727. // port A outputs will not be synchronized. Should not be latched.
  728. int rval;
  729. if (ticks < min_ticks[WRITE_ALT]){
  730. printf("duepp: Got write_alt with %i ticks. Must be at least: %i\n",ticks,min_ticks[WRITE_ALT]);
  731. program->error = 1;
  732. return -1;
  733. }
  734. if (program->state != STATE_INITIALIZED && program->in_sub == 0){
  735. printf("duepp: to write_alt, the program must be in STATE_INITIALIZED, or must be defining a subroutine\n");
  736. program->error = 1;
  737. return -1;
  738. }
  739. if (program->active_port == ALT_PORT){
  740. printf("duepp: WARNING: Got write alt while active port is already alt!\n");
  741. }
  742. rval = due_add_event(program, outputs,ticks);
  743. if (rval < 0) return rval;
  744. rval = play_queue(program, WRITE_ALT);
  745. if (program->dpos > MAXDATA-2){
  746. printf("duepp: program length overrun\n");
  747. program->error = 1;
  748. return -1;
  749. }
  750. if (program->auto_shift) outputs_alt = due_shift_bits(outputs_alt,ALT_PORT);
  751. program->data[program->dpos] = outputs_alt;
  752. program->dpos += 1;
  753. return rval;
  754. }
  755. int due_write_default(due_prog_t *program, unsigned int outputs_def,unsigned int outputs,unsigned int ticks){
  756. // first arg is the outputs for port C.
  757. // port C outputs will be synchronized.
  758. int rval;
  759. if (program->last_ticks < min_ticks[WRITE_DEFAULT]){
  760. printf("duepp: Got write_default with %i ticks in previous event. Must be at least: %i\n",program->last_ticks,min_ticks[WRITE_DEFAULT]);
  761. program->error = 1;
  762. return -1;
  763. }
  764. if (program->state != STATE_INITIALIZED && program->in_sub == 0){
  765. printf("duepp: to write_default, the program must be in STATE_INITIALIZED, or must be defining a subroutine\n");
  766. program->error = 1;
  767. return -1;
  768. }
  769. if (program->active_port == DEFAULT_PORT){
  770. printf("duepp: WARNING: Got write default while active port is already default!\n");
  771. }
  772. rval = play_queue(program, WRITE_DEFAULT); // play out previous events
  773. if (rval < 0) return rval;
  774. if (program->dpos > MAXDATA-2){
  775. printf("duepp: program length overrun\n");
  776. program->error = 1;
  777. return -1;
  778. }
  779. if (program->auto_shift) outputs_def = due_shift_bits(outputs_def,DEFAULT_PORT);
  780. program->data[program->dpos] = outputs_def; // stick the new output word in place - it will get latched with the next event.
  781. program->dpos += 1;
  782. return due_add_event(program, outputs,ticks);
  783. }
  784. //this happens on the arduino:
  785. // this is the starting address in flash of our unrolled loops:
  786. #define BASE_ADDR 0x802f8
  787. #define EVSIZE 12
  788. void resolve_jumps(due_prog_t *program){
  789. // program->dpos is how many elements there are in data
  790. int i,inst=0,elements;
  791. uint32_t base_addrs[NUM_OPCODES];
  792. int code_lengths[NUM_OPCODES + 1] = {12000 * EVSIZE, 16 + 12000 * EVSIZE, 20 + 12000 * EVSIZE,
  793. 4, 2, 10, 8, 50, 58, 10,10,6,6,8,10};
  794. base_addrs[0] = BASE_ADDR+code_lengths[0];
  795. for (i=0;i<NUM_OPCODES;i++)
  796. base_addrs[i] = base_addrs[i-1] + code_lengths[i];
  797. for (i=0;i<NUM_OPCODES;i++)
  798. printf("duepp: base addrs: %i 0x%x\n",i,base_addrs[i]);
  799. i=0;
  800. while( i < program->dpos){
  801. inst = program->data[i]>>16;
  802. elements = program->data[i] & 0xffff;
  803. printf("duepp: got inst: %i, with elements: %i\n",inst,elements);
  804. if (inst == CALL_SUB){
  805. // have to do three things in here:
  806. // 1. set program->data[i] to the sub start address
  807. program->data[i] = base_addrs[SUB_START] +1;
  808. // 2. calculate the address for the jump -into the generic branch
  809. inst = program->data[i+2] >> 16;
  810. // i+2 is like a usual one, but don't advance past the elements afterwards.
  811. elements = program->data[i+2] & 0xffff;
  812. program->data[i+2] = base_addrs[inst] - EVSIZE*elements +1;
  813. //3. find the end of the subroutine and stick the SUB_END address in at its end.
  814. // program->data[i+1] holds the offset, in bytes from the start of the subroutine to the the word following the subroutine call args.
  815. // advance past the start address, the data offset, and the jump address.
  816. i+=3;
  817. }
  818. else{
  819. program->data[i] = base_addrs[inst] - EVSIZE* elements + 1; // +1 for bx.
  820. i += 1; // advance past the address
  821. i += 2*elements; // advance past the events
  822. if (inst == START_LOOP || inst == WRITE_DACS || inst == WRITE_ALT ) i += 1; // for start loop or write_DACS, advance past the argument.
  823. if (inst == WRITE_DEFAULT) i += 2;
  824. }
  825. }
  826. }
  827. int due_dump_program(due_prog_t *program){
  828. int i=0, inst, elements,j;
  829. if (program->state != STATE_FINALIZED ){
  830. printf("duepp: WARNING. Got dump_program, but program has not been finalized. Subroutine calls are not complete.\n");
  831. }
  832. printf("\nProgram Dump\n");
  833. while( i < program->dpos){
  834. inst = program->data[i]>>16;
  835. elements = program->data[i] & 0xffff;
  836. switch (inst){
  837. case CALL_SUB:
  838. printf("%i CALL SUB: to sub id: %i, data offset: %i, sub data starts at: %i,",i,elements,program->data[i+1]/4,i+program->data[i+1]/4+3);
  839. inst = program->data[i+2]>>16;
  840. elements = program->data[i+2] & 0xffff;
  841. printf(" subroutine starts with call at header in position: %i with %i events\n",i+program->data[i+1]/4+2,elements);
  842. i+=3;
  843. break;
  844. case START_LOOP:
  845. printf("%i START_LOOP header, %i events\n",i,elements);
  846. for(j=0;j<elements;j++)
  847. printf("%i outputs: 0x%x time: 0x%x\n",i+2*j+1,program->data[i+1+2*j],program->data[i+1+2*j+1]);
  848. printf("%i START_LOOP with %i iterations\n",i,program->data[i+1+2*elements]);
  849. // printf(" start_loop here\n");
  850. i += 1+2*elements +1; // the header, the elements and the loop counter
  851. break;
  852. case END_LOOP:
  853. printf("%i END_LOOP header, %i events\n",i,elements);
  854. for(j=0;j<elements;j++)
  855. printf("%i outputs: 0x%x time: 0x%x\n",i+2*j+1,program->data[i+1+2*j],program->data[i+1+2*j+1]);
  856. i += 1 +2*elements; // the header, the elements
  857. printf("%i END_LOOP\n",i);
  858. // printf(" end_loop here\n");
  859. break;
  860. case BRANCH:
  861. printf("%i BRANCH header, %i events\n",i,elements);
  862. for(j=0;j<elements;j++)
  863. printf("%i outputs: 0x%x time: 0x%x\n",i+2*j+1,program->data[i+1+2*j],program->data[i+1+2*j+1]);
  864. // printf(" branch here\n");
  865. printf("%i BRANCH\n",i);
  866. i += 1 +2*elements; // the header, the elements
  867. break;
  868. case EXIT:
  869. printf("%i EXIT\n",i);
  870. if (elements > 0) printf("ERROR, got elements > 0: %i\n",elements);
  871. i += 1;
  872. break;
  873. case SUB_START:
  874. printf("%i SUB_START\n",i);
  875. if (elements > 0) printf("ERROR, got elements > 0: %i\n",elements);
  876. i += 1;
  877. break;
  878. case SUB_END:
  879. printf("%i SUB_END\n",i);
  880. if (elements > 0) printf("ERROR, got elements > 0: %i\n",elements);
  881. i += 1;
  882. break;
  883. case EXT_TRIG:
  884. printf("%i EXT_TRIG\n",i);
  885. if (elements > 0) printf("ERROR, got elements > 0: %i\n",elements);
  886. i += 1;
  887. break;
  888. case TRIG_MAX:
  889. printf("%i TRIG_MAX\n",i);
  890. if (elements > 0) printf("ERROR, got elements > 0: %i\n",elements);
  891. i += 1;
  892. break;
  893. case WRITE_DACS:
  894. j = program->data[i+1];
  895. printf("%i WRITE_DACS with dac vals: %i and %i for dacs %i and %i\n",i,j & 0xfff, (j>>16) & 0xfff, (j >> 12) & 1, (j>>28) & 1 );
  896. if (elements > 0) printf("ERROR, got elements > 0: %i\n",elements);
  897. i += 2;
  898. break;
  899. case WRITE_ALT:
  900. j = program->data[i+1];
  901. printf("%i WRITE_ALT with value: 0x%x\n",i,j);
  902. if (elements > 0) printf("ERROR, got elements > 0: %i\n",elements);
  903. i += 2;
  904. break;
  905. case SWAP_TO_ALT:
  906. printf("%i SWAP_TO_ALT\n",i);
  907. if (elements > 0) printf("ERROR, got elements > 0: %i\n",elements);
  908. i += 1;
  909. break;
  910. case SWAP_TO_DACS:
  911. printf("%i SWAP_TO_DACS\n",i);
  912. if (elements > 0) printf("ERROR, got elements > 0: %i\n",elements);
  913. i += 1;
  914. break;
  915. case SWAP_TO_DEFAULT:
  916. printf("%i SWAP_TO_DEFAULT\n",i);
  917. if (elements > 0) printf("ERROR, got elements > 0: %i\n",elements);
  918. i += 1;
  919. break;
  920. case WRITE_DEFAULT:
  921. printf("%i WRITE_DEFAULT with value: 0x%x - gets latched in with next event.\n",i,program->data[i+1]);
  922. if (elements > 0) printf("ERROR, got elements > 0: %i\n",elements);
  923. i+=2;
  924. break;
  925. default:
  926. printf("GOT AN UNKNOWN OPCODE %i and position %i, aborting\n",inst,i);
  927. i+=1;
  928. return -1;
  929. break;
  930. }
  931. }
  932. return 0;
  933. }
  934. int my_read(int fd,char *buff,int timeout){
  935. // reads from serial port till it sees a newline or timeout runs out.
  936. // timeout measured in 0.1 s intervals
  937. // buff is assumed to be able to hold 80 characters.
  938. // returns the number of characters found.
  939. int i,pos=0,newbytes;
  940. do{
  941. i=0;
  942. do{
  943. newbytes = read(fd,&buff[pos],1);
  944. if (newbytes == -1){ //probably woken by signal
  945. return -1;
  946. }
  947. if (newbytes == 1){
  948. if (buff[pos] == '\n') {
  949. buff[pos+1] = 0;
  950. return pos+1;
  951. }
  952. pos += 1;
  953. i=0;
  954. }
  955. else
  956. i += 1;
  957. }while ((i<timeout || timeout == 0) && newbytes == 0);
  958. }while ((i < timeout || timeout == 0) && pos < BUFFLEN-1);
  959. buff[pos] = 0;
  960. return pos;
  961. }
  962. int my_read0(int fd,char *buff,int timeout){
  963. // reads from serial port till it sees a newline or timeout runs out.
  964. // timeout measured in 0.1 s intervals
  965. // buff is assumed to be able to hold 80 characters.
  966. // returns the number of characters found.
  967. int i,pos=0,newbytes;
  968. for (i=0;i<timeout;i++){
  969. newbytes=read(fd,&buff[pos],BUFFLEN-pos-1);
  970. pos += newbytes;
  971. buff[pos] = 0;
  972. if (timeout == 0) i = 0; // wait forever.
  973. if (pos > BUFFLEN-2 ) return pos;
  974. if (strstr(buff,"\n") != NULL)
  975. break;
  976. }
  977. // printf("my_read, returning: %s, length: %i",buff,pos);
  978. return pos;
  979. }
  980. void due_close_prog(int fd){
  981. if (fd >=0){
  982. //flock(fd,LOCK_UN);
  983. //close(fd);
  984. }
  985. }
  986. int due_open_prog(char *device){
  987. //struct termios myterm;
  988. int fd0,bytes_read,rval;
  989. char sbuff[BUFFLEN];
  990. // fd0 = open(device, O_RDWR | O_NOCTTY);
  991. if (fd0 < 0){
  992. printf("duepp: can't open port to programmer %s\n",device);
  993. return -1;
  994. }
  995. //rval = flock(fd0,LOCK_EX|LOCK_NB); // exclusive lock, don't block if we can't.
  996. if (rval < 0){
  997. printf("duepp: Couldn't obtain lock on due programmer board\n");
  998. close(fd0);
  999. return -1;
  1000. }
  1001. //tcgetattr(fd0,&myterm);
  1002. //myterm.c_iflag = 0;
  1003. //myterm.c_oflag= CR0; //Carriage return delay mask. Values are CR0, CR1, CR2, or CR3.
  1004. //myterm.c_cflag = CS8 |CLOCAL|CREAD|B38400; // speed doesn't matter for usb
  1005. //myterm.c_lflag=0;
  1006. //myterm.c_cc[VMIN]=0; // non-blocking
  1007. //myterm.c_cc[VTIME]=1; // returns after 0.1s if no characters available
  1008. //tcsetattr(fd0,TCSANOW, &myterm);
  1009. // tcflush(fd0,TCIFLUSH);
  1010. printf("duepp: writing Q: ");
  1011. //write(fd0,"Q",1);
  1012. // bytes_read = my_read(fd0,sbuff,25);
  1013. if (bytes_read > 0 ){
  1014. printf("duepp: Got: %s",sbuff);
  1015. if (strncmp(sbuff,"Due pulse programmer v1",23) == 0)
  1016. return fd0;
  1017. }
  1018. //rval=flock(fd0,LOCK_UN);
  1019. //close(fd0);
  1020. return -1;
  1021. }
  1022. int due_download_prog(int fd,due_prog_t *program){
  1023. char cbyte[3];
  1024. char sbuff[BUFFLEN];
  1025. unsigned char c1,c2;
  1026. int c1d,c2d;
  1027. int i,bytes_read;
  1028. struct timeval start_time,end_time;
  1029. struct timezone tz;
  1030. double d_time;
  1031. if (fd <= 0){
  1032. printf("duepp: due_download: got invalid file descriptor\n");
  1033. return -1;
  1034. }
  1035. if (program->error != 0){
  1036. printf("duepp: pulse program has an error flag set, will not download!\n");
  1037. return -1;
  1038. }
  1039. if (program->state != STATE_FINALIZED){
  1040. printf("duepp: WARNING. Program has not been finalized. If it contains any subroutines, bad things will happen.\n");
  1041. }
  1042. gettimeofday(&start_time,&tz);
  1043. printf("duepp: Sending prog size: %i ",program->dpos);
  1044. fflush(stdout);
  1045. // write(fd,"D",1);
  1046. // send data length, low byte, high byte
  1047. cbyte[0] = 'D';
  1048. cbyte[1] = program->dpos & 0xff;
  1049. cbyte[2] = (program->dpos>>8)&0xff;
  1050. write(fd,cbyte,3);
  1051. bytes_read = my_read(fd,sbuff,5000);
  1052. if (bytes_read > 0 ){
  1053. printf("duepp: Got: %s",sbuff);
  1054. if (strstr(sbuff,"size ok") == NULL){
  1055. printf("duepp: didn't get size ok, aborting\n");
  1056. return -1;
  1057. }
  1058. }
  1059. else{
  1060. printf("duepp: no response to program size\n");
  1061. return -1;
  1062. }
  1063. // then data.
  1064. printf("duepp: Sending program: \n");
  1065. fflush(stdout);
  1066. // first do 512 byte blocks: = 128 4-byte words
  1067. for ( i = 0 ; i+128 < program->dpos ; i += 128){
  1068. // printf("512 byte block at pos: %i ",i);
  1069. write(fd,&(program->data[i]),512);
  1070. bytes_read = my_read(fd,sbuff,100);
  1071. //printf("got: %s",sbuff);
  1072. }
  1073. //write the rest:
  1074. // printf("writing final %i words\n",(program->dpos-i));
  1075. write(fd,&(program->data[i]),(program->dpos-i)*4);
  1076. //write(fd,&data[i],dpos*4);
  1077. bytes_read = my_read(fd,sbuff,10000);
  1078. if (bytes_read > 0){
  1079. printf("duepp: got: %s",sbuff);
  1080. if (strstr(sbuff,"data received") != NULL){ // first two bytes are checksums
  1081. gettimeofday(&end_time,&tz);
  1082. d_time=(end_time.tv_sec-start_time.tv_sec)*1e6
  1083. +(end_time.tv_usec-start_time.tv_usec);
  1084. // fprintf(stderr,"downloaded %i bytes in: %.0f us\n",program->dpos*4,d_time);
  1085. printf("duepp: downloaded %i bytes in: %.0f us\n",program->dpos*4,d_time);
  1086. // check checksums
  1087. checksum_data(&c1,&c2,program->dpos,program->data);
  1088. sscanf(sbuff,"%i %i",&c1d,&c2d);
  1089. if (c1 != c1d || c2 != c2d) {
  1090. printf("duepp: checksums don't match! I calculate: %i %i, received: %i %i\n",c1&0xff,c2&0xff,c1d,c2d);
  1091. return -1;
  1092. }
  1093. // else printf("checksum match: %i %i, %i %i\n",c1&0xff, c2&0xff,c1d,c2d);
  1094. return 0;
  1095. }
  1096. }
  1097. printf("duepp: no response to sent program\n");
  1098. return -1;
  1099. } //due_download_prog
  1100. int due_download_prog_save_to_file(int fd,due_prog_t *program){
  1101. char cbyte[3];
  1102. char sbuff[BUFFLEN];
  1103. unsigned char c1,c2;
  1104. int c1d,c2d;
  1105. int i,bytes_read;
  1106. struct timeval start_time,end_time;
  1107. struct timezone tz;
  1108. double d_time;
  1109. if (fd <= 0){
  1110. printf("duepp: due_download: got invalid file descriptor (<=0) \n");
  1111. return -1;
  1112. }
  1113. if (program->error != 0){
  1114. printf("duepp: pulse program has an error flag set, will not download!\n");
  1115. return -1;
  1116. }
  1117. if (program->state != STATE_FINALIZED){
  1118. printf("duepp: WARNING. Program has not been finalized. If it contains any subroutines, bad things will happen.\n");
  1119. }
  1120. gettimeofday(&start_time,&tz);
  1121. printf("duepp: Sending prog size: %i ",program->dpos);
  1122. fflush(stdout);
  1123. // write(fd,"D",1);
  1124. // send data length, low byte, high byte
  1125. cbyte[0] = 'D';
  1126. cbyte[1] = program->dpos & 0xff;
  1127. cbyte[2] = (program->dpos>>8)&0xff;
  1128. write(fd,cbyte,3);
  1129. bytes_read = my_read(fd,sbuff,5000);
  1130. if (bytes_read > 0 ){
  1131. printf("duepp: Got: %s",sbuff);
  1132. if (strstr(sbuff,"size ok") == NULL){
  1133. printf("duepp: didn't get size ok, aborting\n");
  1134. //return -1;
  1135. }
  1136. }
  1137. else{
  1138. printf("duepp: no response to program size\n");
  1139. //return -1;
  1140. }
  1141. // then data.
  1142. printf("duepp: Sending program: \n");
  1143. fflush(stdout);
  1144. // first do 512 byte blocks: = 128 4-byte words
  1145. for ( i = 0 ; i+128 < program->dpos ; i += 128){
  1146. printf("due_download_prog_save_to_file : 512 byte block at pos: %i ",i);
  1147. write(fd,&(program->data[i]),512);
  1148. //bytes_read = my_read(fd,sbuff,100);
  1149. //printf("got: %s",sbuff);
  1150. }
  1151. //write the rest:
  1152. printf("due_download_prog_save_to_file : writing final %i words\n",(program->dpos-i));
  1153. write(fd,&(program->data[i]),(program->dpos-i)*4);
  1154. //write(fd,&data[i],dpos*4);
  1155. //bytes_read = my_read(fd,sbuff,10000);
  1156. if (bytes_read > 0){
  1157. printf("duepp: got: %s",sbuff);
  1158. if (strstr(sbuff,"data received") != NULL){ // first two bytes are checksums
  1159. gettimeofday(&end_time,&tz);
  1160. d_time=(end_time.tv_sec-start_time.tv_sec)*1e6
  1161. +(end_time.tv_usec-start_time.tv_usec);
  1162. // fprintf(stderr,"downloaded %i bytes in: %.0f us\n",program->dpos*4,d_time);
  1163. printf("duepp: downloaded %i bytes in: %.0f us\n",program->dpos*4,d_time);
  1164. // check checksums
  1165. checksum_data(&c1,&c2,program->dpos,program->data);
  1166. sscanf(sbuff,"%i %i",&c1d,&c2d);
  1167. if (c1 != c1d || c2 != c2d) {
  1168. printf("duepp: checksums don't match! I calculate: %i %i, received: %i %i\n",c1&0xff,c2&0xff,c1d,c2d);
  1169. //return -1;
  1170. }
  1171. // else printf("checksum match: %i %i, %i %i\n",c1&0xff, c2&0xff,c1d,c2d);
  1172. return 0;
  1173. }
  1174. }
  1175. printf("duepp: no response to sent program\n");
  1176. //return -1;
  1177. } //due_download_prog_save_to_file
  1178. int due_download_prog_save_to_file_command(int fd,due_prog_t *program)
  1179. {
  1180. char cbyte[3];
  1181. char sbuff[BUFFLEN];
  1182. unsigned char c1,c2;
  1183. int c1d,c2d;
  1184. int i,bytes_read;
  1185. struct timeval start_time,end_time;
  1186. struct timezone tz;
  1187. double d_time;
  1188. if (fd <= 0){
  1189. printf("duepp: due_download_command: got invalid file descriptor (<=0) \n");
  1190. return -1;
  1191. }
  1192. if (program->error != 0){
  1193. printf("duepp: pulse program has an error flag set, will not download!\n");
  1194. return -1;
  1195. }
  1196. if (program->state != STATE_FINALIZED){
  1197. printf("duepp: WARNING. Program has not been finalized. If it contains any subroutines, bad things will happen.\n");
  1198. }
  1199. gettimeofday(&start_time,&tz);
  1200. printf("duepp: Sending prog size: %i ",program->dpos);
  1201. fflush(stdout);
  1202. // write(fd,"D",1);
  1203. // send data length, low byte, high byte
  1204. cbyte[0] = 'D';
  1205. cbyte[1] = program->dpos & 0xff;
  1206. cbyte[2] = (program->dpos>>8)&0xff;
  1207. write(fd,cbyte,3);
  1208. bytes_read = my_read(fd,sbuff,5000);
  1209. if (bytes_read > 0 ){
  1210. printf("duepp: Got: %s",sbuff);
  1211. if (strstr(sbuff,"size ok") == NULL){
  1212. printf("duepp: didn't get size ok, aborting\n");
  1213. //return -1;
  1214. }
  1215. }
  1216. else{
  1217. printf("duepp: no response to program size\n");
  1218. //return -1;
  1219. }
  1220. } //due_download_prog_save_to_file_command
  1221. int due_download_prog_save_to_file_data(int fd,due_prog_t *program)
  1222. {
  1223. char cbyte[3];
  1224. char sbuff[BUFFLEN];
  1225. unsigned char c1,c2;
  1226. int c1d,c2d;
  1227. int i,bytes_read;
  1228. struct timeval start_time,end_time;
  1229. struct timezone tz;
  1230. double d_time;
  1231. if (fd <= 0){
  1232. printf("duepp: due_download: got invalid file descriptor (<=0) \n");
  1233. return -1;
  1234. }
  1235. if (program->error != 0){
  1236. printf("duepp: pulse program has an error flag set, will not download!\n");
  1237. return -1;
  1238. }
  1239. if (program->state != STATE_FINALIZED){
  1240. printf("duepp: WARNING. Program has not been finalized. If it contains any subroutines, bad things will happen.\n");
  1241. }
  1242. gettimeofday(&start_time,&tz);
  1243. printf("duepp: Sending prog size: %i ",program->dpos);
  1244. fflush(stdout);
  1245. // write(fd,"D",1);
  1246. // // send data length, low byte, high byte
  1247. // cbyte[0] = 'D';
  1248. // cbyte[1] = program->dpos & 0xff;
  1249. // cbyte[2] = (program->dpos>>8)&0xff;
  1250. // write(fd,cbyte,3);
  1251. //
  1252. // bytes_read = my_read(fd,sbuff,5000);
  1253. // if (bytes_read > 0 ){
  1254. // printf("duepp: Got: %s",sbuff);
  1255. // if (strstr(sbuff,"size ok") == NULL){
  1256. // printf("duepp: didn't get size ok, aborting\n");
  1257. // //return -1;
  1258. // }
  1259. // }
  1260. // else{
  1261. //printf("duepp: no response to program size\n");
  1262. //return -1;
  1263. // }
  1264. // then data.
  1265. printf("duepp: Sending program: \n");
  1266. fflush(stdout);
  1267. // first do 512 byte blocks: = 128 4-byte words
  1268. for ( i = 0 ; i+128 < program->dpos ; i += 128){
  1269. printf("due_download_prog_save_to_file : 512 byte block at pos: %i ",i);
  1270. write(fd,&(program->data[i]),512);
  1271. //bytes_read = my_read(fd,sbuff,100);
  1272. //printf("got: %s",sbuff);
  1273. }
  1274. //write the rest:
  1275. printf("due_download_prog_save_to_file : writing final %i words\n",(program->dpos-i));
  1276. write(fd,&(program->data[i]),(program->dpos-i)*4);
  1277. //write(fd,&data[i],dpos*4);
  1278. //bytes_read = my_read(fd,sbuff,10000);
  1279. if (bytes_read > 0){
  1280. printf("duepp: got: %s",sbuff);
  1281. if (strstr(sbuff,"data received") != NULL){ // first two bytes are checksums
  1282. gettimeofday(&end_time,&tz);
  1283. d_time=(end_time.tv_sec-start_time.tv_sec)*1e6
  1284. +(end_time.tv_usec-start_time.tv_usec);
  1285. // fprintf(stderr,"downloaded %i bytes in: %.0f us\n",program->dpos*4,d_time);
  1286. printf("duepp: downloaded %i bytes in: %.0f us\n",program->dpos*4,d_time);
  1287. // check checksums
  1288. checksum_data(&c1,&c2,program->dpos,program->data);
  1289. sscanf(sbuff,"%i %i",&c1d,&c2d);
  1290. if (c1 != c1d || c2 != c2d) {
  1291. printf("duepp: checksums don't match! I calculate: %i %i, received: %i %i\n",c1&0xff,c2&0xff,c1d,c2d);
  1292. //return -1;
  1293. }
  1294. // else printf("checksum match: %i %i, %i %i\n",c1&0xff, c2&0xff,c1d,c2d);
  1295. return 0;
  1296. }
  1297. }
  1298. printf("duepp: no response to sent program\n");
  1299. //return -1;
  1300. } //due_download_prog_save_to_file_data
  1301. int due_run_program(int fd, char start_command){
  1302. // start_command is e, E or R for software start, triggered start and restart (resume when final event is still running).
  1303. int bytes_read;
  1304. char sbuff[BUFFLEN];
  1305. if (fd <= 0){
  1306. printf("duepp: due_run: got invalid file descriptor\n");
  1307. return -1;
  1308. }
  1309. printf("duepp: writing %c: ",start_command);
  1310. write(fd,&start_command,1);
  1311. bytes_read = my_read(fd,sbuff,25);
  1312. if (bytes_read > 0 ){
  1313. printf("duepp: Got: %s",sbuff);
  1314. if (strncmp(sbuff,"Starting",8) == 0 || strncmp(sbuff,"Restarting",10) == 0)
  1315. return 0;
  1316. }
  1317. return -1;
  1318. }
  1319. int due_wait_for_completion(int fd, int timeout){
  1320. char sbuff[BUFFLEN];
  1321. int bytes_read;
  1322. if (fd <= 0){
  1323. printf("duepp: due_wait_for_completion: got invalid file descriptor\n");
  1324. return -1;
  1325. }
  1326. printf("duepp: waiting for completion: \n");
  1327. fflush(stdout);
  1328. bytes_read = my_read(fd,sbuff,timeout);
  1329. if (bytes_read == -1) return -1; // woken by signal.
  1330. if (bytes_read == 0) return 1; // timeout
  1331. printf("duepp: Got: %s\n",sbuff);
  1332. if (strncmp(sbuff,"Final Event started",19)==0) return 0;
  1333. if (strncmp(sbuff,"Was interrupted",15)==0) return 2;
  1334. return -1;
  1335. }
  1336. int due_interrupt_program(int fd){
  1337. // don't read anything. Follow this with due_wait_for_completion.
  1338. printf("duepp: writing K: ");
  1339. write(fd,"K",1);
  1340. return 0;
  1341. }
  1342. int due_get_status(int fd){
  1343. char sbuff[BUFFLEN];
  1344. int bytes_read;
  1345. if (fd <= 0){
  1346. printf("duepp: due_get_status: got invalid file descriptor\n");
  1347. return -1;
  1348. }
  1349. printf("duepp: writing S: \n");
  1350. write(fd,"S",1);
  1351. bytes_read = my_read(fd,sbuff,100);
  1352. if (bytes_read == 0) return -1; //
  1353. printf("duepp: Got: %s\n",sbuff);
  1354. return 0;
  1355. }
  1356. int due_write_dacs_now(int fd, unsigned int dac0,unsigned int dac1){
  1357. unsigned int dval,bytes_read,i;
  1358. char sbuff[BUFFLEN];
  1359. char cbyte[5],*obytes;
  1360. if (fd <= 0){
  1361. printf("duepp: due_write_dacs_now: got invalid file descriptor\n");
  1362. return -1;
  1363. }
  1364. dval = dac0 | dac1<<16 | 1<<28;
  1365. // write(fd,"A",1);
  1366. // write(fd,&dval,4);
  1367. cbyte[0] = 'A';
  1368. obytes = (char *) &dval;
  1369. for (i=0;i<4;i++)
  1370. cbyte[i+1] = obytes[i];
  1371. write(fd,cbyte,5);
  1372. bytes_read = my_read(fd,sbuff,100);
  1373. if (bytes_read >0){
  1374. printf("duepp: Got: %s\n",sbuff);
  1375. if (strncmp(sbuff,"OK",2) == 0) return 0;
  1376. }
  1377. return -1;
  1378. }
  1379. int due_write_alt_now(int fd, unsigned int output){
  1380. unsigned int bytes_read,i;
  1381. char sbuff[BUFFLEN];
  1382. char cbyte[5],*obytes;
  1383. if (fd <= 0){
  1384. printf("duepp: due_write_alt_now: got invalid file descriptor\n");
  1385. return -1;
  1386. }
  1387. // write(fd,"P",1);
  1388. // write(fd,&output,4);
  1389. cbyte[0] = 'P';
  1390. obytes = (char *) &output;
  1391. for (i=0;i<4;i++)
  1392. cbyte[i+1] = obytes[i];
  1393. write(fd,cbyte,5);
  1394. bytes_read = my_read(fd,sbuff,100);
  1395. if (bytes_read >0){
  1396. printf("duepp: Got: %s\n",sbuff);
  1397. if (strncmp(sbuff,"OK",2) == 0) return 0;
  1398. }
  1399. return -1;
  1400. }
  1401. int due_read_analog(int fd, unsigned char pin){
  1402. // pin number should be one of 62, 63, 64, 65 for A8-A11 (D62-D65 or B.17-B.20)
  1403. unsigned int bytes_read;
  1404. char sbuff[BUFFLEN];
  1405. char cbyte[2];
  1406. unsigned int rval=0;
  1407. if (fd <= 0){
  1408. printf("duepp: due_read_analog: got invalid file descriptor\n");
  1409. return -1;
  1410. }
  1411. cbyte[0] = 'a';
  1412. cbyte[1] = pin;
  1413. write(fd,cbyte,2);
  1414. bytes_read = my_read(fd,sbuff,100);
  1415. if (bytes_read >0){
  1416. rval = atoi(sbuff);
  1417. // printf("duepp: Got: %s, converted to int: %i\n",sbuff,rval);
  1418. }
  1419. return rval;
  1420. }