serial_lib_cpp.cpp 55 KB

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