-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnuphasedaq.c
2528 lines (2036 loc) · 70.4 KB
/
nuphasedaq.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "nuphasedaq.h"
#include <linux/spi/spidev.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>
#include <inttypes.h>
#include <errno.h>
#include <endian.h>
#include "bbb_gpio.h"
#define NP_ADDRESS_MAX 128
#define NP_SPI_BYTES NP_WORD_SIZE
#define NP_NUM_MODE 4
#define NP_NUM_REGISTER 128
#define NP_NUM_SURFACE_CHANNELS 6
#define BUF_MASK 0xf
#define MAX_PRETRIGGER 8
#define BOARD_CLOCK_HZ 1500000000/16
// master + slave
#define NBD(d) (d->fd[1] ? 2 : 1)
#define MIN_GOOD_MAX_V 20
#define MAX_MISERY 100
#define SPI_CAST (uintptr_t)
#define NP_DELAY_USECS 0
#define NP_CS_CHANGE 0
#define SPI_CLOCK 20000000
//#define SPI_CLOCK 1000000
#define MAX_XFERS 511
//#define DEBUG_PRINTOUTS 1
//register map
typedef enum
{
REG_FIRMWARE_VER = 0x01,
REG_FIRMWARE_DATE = 0x02,
REG_SCALER_READ = 0x03,
REG_CHIPID_LOW = 0x04,
REG_CHIPID_MID = 0x05,
REG_CHIPID_HI = 0x06,
REG_STATUS = 0x07,
REG_CLEAR_STATUS = 0x09,
REG_EVENT_COUNTER_LOW = 0xa,
REG_EVENT_COUNTER_HIGH = 0xb,
REG_TRIG_COUNTER_LOW = 0xc,
REG_TRIG_COUNTER_HIGH = 0xd,
REG_TRIG_TIME_LOW = 0xe,
REG_TRIG_TIME_HIGH = 0xf,
REG_DEADTIME = 0x10,
REG_TRIG_INFO = 0x11, //bits 23-22 : event buffer ; bit 21: calpulse, bits 19-17: pretrig window, bits16-15: trig type ; bits 14-0: last beam trigger
REG_TRIG_MASKS = 0x12, // bits 22-15 : channel mask ; bits 14-0 : beam mask
REG_BEAM_POWER = 0x14, // add beam to get right register
REG_CHUNK = 0x23, //which 32-bit chunk + i
REG_SYNC = 0x27,
REG_UPDATE_SCALERS = 0x28,
REG_PICK_SCALER = 0x29,
REG_CALPULSE = 0x2a, //cal pulse
REG_LATCHED_PPS_LOW = 0x2c,
REG_LATCHED_PPS_HIGH = 0x2d,
REG_SURFACE_1 = 0x2e,
REG_SURFACE_2 = 0x2f,
REG_CHANNEL_MASK = 0x30,
REG_ATTEN_012 = 0x32,
REG_ATTEN_345 = 0x33,
REG_ATTEN_67 = 0x34,
REG_ATTEN_APPLY = 0x35,
REG_ADC_CLK_RST = 0x37,
REG_ADC_DELAYS = 0x38, //add buffer number to get all
REG_TRIG_DELAY_012 = 0x3d,
REG_TRIG_DELAY_345 = 0x3e,
REG_TRIG_DELAY_67 = 0x3f,
REG_FORCE_TRIG = 0x40,
REG_CHANNEL = 0x41, //select channel to read
REG_MODE = 0x42, //readout mode
REG_RAM_ADDR = 0x45, //ram address
REG_READ = 0x47, //send data to spi miso
REG_SURFACE_SELECT = 0x4a,
REG_EXT_INPUT_CONFIG = 0x4b,
REG_PRETRIGGER = 0x4c,
REG_CLEAR = 0x4d, //clear buffers
REG_BUFFER = 0x4e,
REG_TRIGGER_MASK = 0x50,
REG_TRIG_HOLDOFF = 0x51,
REG_TRIG_ENABLE = 0x52,
REG_TRIGOUT_CONFIG = 0x53,
REG_PHASED_TRIGGER = 0x54,
REG_VERIFICATION_MODE = 0x55,
REG_THRESHOLDS = 0x56, // add the threshold to this to get the right register
REG_HPOL_THRESHOLD = 0x66,
REG_SET_READ_REG = 0x6d,
REG_RESET_COUNTER = 0x7e,
REG_RESET_ALL = 0x7f
} nuphase_register_t;
void easy_break_point()
{
//this is here just for an easy break point
fprintf(stderr,"OOPS\n");
}
//readout modes
typedef enum
{
MODE_REGISTER=0,
MODE_WAVEFORMS=1,
MODE_BEAMS=2,
MODE_POWERSUM=3
} nuphase_readout_mode_t;
struct nuphase_dev
{
const char * device_name[2]; //msater,slave
int fd[2]; //master, slave
int power_gpio; //gpio for enable
int enable_locking;
uint64_t readout_number_offset;
uint64_t event_counter; // should match device...we'll keep this to complain if it doesn't
uint16_t buffer_length;
uint16_t surface_buffer_length;
pthread_mutex_t mut; //mutex for the SPI (not for the gpio though). Only used if enable_locking is true
pthread_mutex_t wait_mut; //mutex for the waiting. Only used if enable_locking is true
uint8_t board_id[2];
uint8_t channel_read_mask[2];// read mask... right now it's always 0xf, but we can make it configurable later
uint8_t surface_channel_read_mask;
volatile int cancel_wait; // needed for signal handlers
struct timespec start_time; //the time of the last clock reset
uint8_t next_read_buffer; //what buffer to read next
uint8_t hardware_next; // what buffer the hardware things we should read next
uint32_t min_threshold;
uint16_t poll_interval;
int spi_clock;
int cs_change;
int delay_us;
int surface_readout;
uint8_t pretrigger;
uint8_t surface_pretrigger;
// store event / header used for calibration here in case we want it later?
nuphase_event_t calib_ev;
nuphase_header_t calib_hd;
//spi buffer
struct spi_ioc_transfer buf[2][MAX_XFERS];
int nused[2];
// device state
int current_buf[2];
int current_mode[2];
bbb_gpio_pin_t * gpio_pin;
// this needs to be cached as it's sent to the board when switching modes
uint64_t surface_event_counter;
//surface things
int surface_throttle;
int n_skipped_in_last_second;
int last_second_skipped;
int nsurface_in_last_second;
int last_surface_second;
int clear_buffer_when_throttled ;
};
//some macros
#define USING(d) if (d->enable_locking) pthread_mutex_lock(&d->mut);
#define DONE(d) if (d->enable_locking) pthread_mutex_unlock(&d->mut);
//Wrappers for io functions to add printouts
static int do_xfer(int fd, int n, struct spi_ioc_transfer * xfer)
{
#ifdef DEBUG_PRINTOUTS
struct timespec start;
struct timespec end;
clock_gettime(CLOCK_REALTIME, &start) ;
#endif
int ret = ioctl(fd,SPI_IOC_MESSAGE(n), xfer);
#ifdef DEBUG_PRINTOUTS
clock_gettime(CLOCK_REALTIME, &end) ;
int i;
printf("START BULK TRANSFER (fd=%d, t = %lu.%lu)\n", fd,start.tv_sec, start.tv_nsec);
for (i = 0; i < n; i++)
{
printf("\tXFR %03d\t",i);
if (xfer[i].tx_buf)
{
uint8_t * tx = (uint8_t*) SPI_CAST xfer[i].tx_buf;
printf("TX [0x%02x 0x%02x 0x%02x 0x%02x]\t", tx[0],tx[1],tx[2],tx[3]);
}
if (xfer[i].rx_buf)
{
uint8_t * rx = (uint8_t*) SPI_CAST xfer[i].rx_buf;
printf("RX [0x%02x 0x%02x 0x%02x 0x%02x]\t", rx[0],rx[1],rx[2],rx[3]);
}
printf("\n");
}
printf("END BULK TRANSFER (fd=%d,t= %lu.%lu)\n", fd,end.tv_sec, end.tv_nsec);
#endif
return ret;
}
static int do_write(int fd, const uint8_t * p)
{
int ret = write(fd,p, NP_SPI_BYTES);
#ifdef DEBUG_PRINTOUTS
printf("WRITE(%d): [0x%02x 0x%02x 0x%02x 0x%02x]\n",fd, p[0],p[1],p[2],p[3]);
#endif
return ret;
}
static int do_read(int fd, uint8_t * p)
{
int ret = read(fd,p, NP_SPI_BYTES);
#ifdef DEBUG_PRINTOUTS
printf("READ(%d): [0x%02x 0x%02x 0x%02x 0x%02x]\n",fd, p[0],p[1],p[2],p[3]);
#endif
return ret;
}
#define N_SCALER_REGISTERS (NP_NUM_SCALERS * (1 + NP_NUM_BEAMS)/2)
#define N_PICK_SCALER (2 + N_SCALER_REGISTERS)
// all possible buffers we might batch
static uint8_t buf_mode[NP_NUM_MODE][NP_SPI_BYTES];
static uint8_t buf_set_read_reg[NP_NUM_REGISTER][NP_SPI_BYTES];
static uint8_t buf_channel[NP_NUM_CHAN][NP_SPI_BYTES];
static uint8_t buf_buffer[NP_NUM_BUFFER][NP_SPI_BYTES];
static uint8_t buf_chunk[NP_NUM_CHUNK][NP_SPI_BYTES];
static uint8_t buf_ram_addr[NP_ADDRESS_MAX][NP_SPI_BYTES];
static uint8_t buf_clear[1 << NP_NUM_BUFFER][NP_SPI_BYTES];
static uint8_t buf_clear_surface[NP_SPI_BYTES] = {REG_CLEAR, 1,0,0}; ;
static uint8_t buf_reset_buf[NP_SPI_BYTES] = {REG_CLEAR,0,1,0};
static uint8_t buf_pick_scaler[N_PICK_SCALER][NP_SPI_BYTES];
//possible states for change_to_surface / change_to_deep
// not all of these are valid, but it's easier to generate all of them
#define N_SURFACE_CHANGE_STATES 32
static uint8_t buf_change_to_surface[NP_SPI_BYTES] = { REG_SURFACE_SELECT,0,0,1};
static uint8_t buf_change_to_deep[NP_SPI_BYTES] = {REG_SURFACE_SELECT,0,0,0};
static uint8_t buf_read[NP_SPI_BYTES] __attribute__((unused))= {REG_READ,0,0,0} ;
static uint8_t buf_update_scalers[NP_SPI_BYTES] = {REG_UPDATE_SCALERS,0,0,1} ;
static uint8_t buf_sync_on[NP_SPI_BYTES] = {REG_SYNC,0,0,1} ;
static uint8_t buf_sync_off[NP_SPI_BYTES] = {REG_SYNC,0,0,0} ;
static uint8_t buf_reset_all[NP_SPI_BYTES] = {REG_RESET_ALL,0,0,1};
static uint8_t buf_reset_almost_all[NP_SPI_BYTES] = {REG_RESET_ALL,0,0,2};
static uint8_t buf_reset_counter[NP_SPI_BYTES] = {REG_RESET_COUNTER,0,0,1};
static uint8_t buf_adc_clk_rst[NP_SPI_BYTES] = {REG_ADC_CLK_RST,0,0,0};
static uint8_t buf_apply_attenuation[NP_SPI_BYTES] = {REG_ATTEN_APPLY,0,0,0};
static void fillBuffers() __attribute__((constructor)); //this will initialize the static buffers.
void fillBuffers()
{
int i;
memset(buf_mode,0,sizeof(buf_mode));
for (i = 0; i < NP_NUM_MODE; i++)
{
buf_mode[i][0] = REG_MODE;
buf_mode[i][3] = i;
}
memset(buf_set_read_reg,0,sizeof(buf_set_read_reg));
for (i = 0; i < NP_NUM_REGISTER; i++)
{
buf_set_read_reg[i][0]= REG_SET_READ_REG;
buf_set_read_reg[i][3] = i;
}
memset(buf_channel,0,sizeof(buf_channel));
for (i = 0; i < NP_NUM_CHAN; i++)
{
buf_channel[i][0] = REG_CHANNEL;
buf_channel[i][3] = 1<<i;
}
memset(buf_buffer,0,sizeof(buf_buffer));
for (i = 0; i < NP_NUM_BUFFER; i++)
{
buf_buffer[i][0] = REG_BUFFER;
buf_buffer[i][3] = i;
}
memset(buf_ram_addr,0,sizeof(buf_ram_addr));
for (i = 0; i < NP_ADDRESS_MAX; i++)
{
buf_ram_addr[i][0] = REG_RAM_ADDR;
buf_ram_addr[i][3] = i;
}
memset(buf_chunk,0,sizeof(buf_chunk));
for (i = 0; i < NP_NUM_CHUNK;i++)
{
buf_chunk[i][0]=REG_CHUNK+i;
}
memset(buf_clear,0,sizeof(buf_clear));
for (i = 0; i < (1 << NP_NUM_BUFFER); i++)
{
buf_clear[i][0] = REG_CLEAR;
buf_clear[i][3] = i;
}
memset(buf_pick_scaler,0,sizeof(buf_pick_scaler));
for (i = 0; i < N_PICK_SCALER; i++)
{
buf_pick_scaler[i][0]=REG_PICK_SCALER;
buf_pick_scaler[i][3]=i;
}
}
static void setup_xfers( nuphase_dev_t *d)
{
int i, b;
USING(d);
for (b = 0; b < NBD(d); b++)
{
for (i = 0; i < MAX_XFERS; i++)
{
d->buf[b][i].len = NP_SPI_BYTES;
d->buf[b][i].cs_change =d->cs_change; //deactivate cs between transfers
d->buf[b][i].delay_usecs = d->delay_us;//?
}
}
DONE(d);
}
static int buffer_send(nuphase_dev_t * d, nuphase_which_board_t which)
{
int wrote;
if (!d->nused[which]) return 0;
wrote = do_xfer(d->fd[which], d->nused[which], d->buf[which]);
if (wrote < d->nused[which] * NP_SPI_BYTES)
{
fprintf(stderr,"IOCTL failed! returned: %d\n",wrote);
return -1;
}
d->nused[which] = 0;
return 0;
}
// this will send if full!
static int buffer_append(nuphase_dev_t * d, nuphase_which_board_t which, const uint8_t * txbuf, const uint8_t * rxbuf)
{
//check if full
if (d->nused[which] >= MAX_XFERS) //greater than just in case, but it already means something went horribly wrong
{
if (buffer_send(d,which))
{
return -1;
}
}
d->buf[which][d->nused[which]].tx_buf = SPI_CAST txbuf;
d->buf[which][d->nused[which]].rx_buf = SPI_CAST rxbuf;
d->nused[which]++;
return 0;
}
static int append_read_register(nuphase_dev_t *d, nuphase_which_board_t which, uint8_t address, uint8_t * result)
{
int ret = 0;
ret += buffer_append(d,which, buf_set_read_reg[address],0);
ret += buffer_append(d,which,0,result);
return ret;
}
/* internal synchronized command if reg_to_read_after is not zero, will read a
* register after (for example to see if something worked) and store the result
* in the appropriate place.
**/
static int synchronized_command(nuphase_dev_t *d, const uint8_t * cmd, uint8_t reg_to_read_after,
uint8_t * result_master, uint8_t * result_slave) {
//just do a normal command
if (NBD(d) < 2)
{
int ret =0;
USING(d);
ret += buffer_append(d,MASTER,cmd,0);
if (reg_to_read_after)
{
ret+=append_read_register(d,MASTER, reg_to_read_after, result_master);
}
ret+= buffer_send(d,MASTER);
DONE(d);
return ret;
}
USING(d);
int ret = 0;
//send sync on to master
ret+=buffer_append(d,MASTER, buf_sync_on,0);
ret+=buffer_send(d, MASTER);
//send command to slave
ret+=buffer_append(d, SLAVE, cmd,0);
ret+=buffer_send(d,SLAVE);
//send command, and then sync off to master
ret+=buffer_append(d,MASTER, cmd,0);
ret+=buffer_append(d,MASTER, buf_sync_off,0);
ret+=buffer_send(d,MASTER);
if (reg_to_read_after)
{
ret+=append_read_register(d, MASTER, reg_to_read_after, result_master);
ret+=append_read_register(d, SLAVE, reg_to_read_after, result_slave);
ret+=buffer_send(d,SLAVE);
ret+=buffer_send(d,MASTER);
}
DONE(d);
return ret;
}
static int mark_buffers_done(nuphase_dev_t * d, nuphase_buffer_mask_t buf)
{
if (NBD(d) < 2) //no slave device, so no sync needed
{
USING(d);
int ret = 0;
uint8_t data_status[4];
ret += buffer_append(d, MASTER, buf_clear[buf],0);
ret+=append_read_register(d,MASTER, REG_CLEAR_STATUS, data_status);
ret+=buffer_send(d,MASTER); //flush so we can clear the buffer immediately
if (data_status[3] & (buf))
{
// fprintf(stderr,"Did not clear buffer mask %x ? (or rate too high? buf mask after clearing: %x))\n", buf, data_status[3] & 0xf) ;
// easy_break_point();
}
DONE(d);
return ret;
}
else
{
uint8_t cleared_master[NP_SPI_BYTES];
uint8_t cleared_slave[NP_SPI_BYTES];
int ret = synchronized_command(d, buf_clear[buf], REG_CLEAR_STATUS, cleared_master, cleared_slave);
// printf("Clearing %d on both\n", buf2clr);
if (!ret)
{
if (cleared_master[3] & ( buf))
{
// fprintf(stderr,"Did not clear buffer mask %x for master ? (or rate too high? buf mask after clearing: %x))\n", buf, cleared_master[3] & 0xf) ;
// easy_break_point();
}
if (cleared_slave[3] & (buf))
{
// fprintf(stderr,"Did not clear buffer %x for master ? (or rate too high? buf mask after clearing: %x))\n", buf, cleared_slave[3] & 0xf) ;
// easy_break_point();
}
// if ((cleared_slave[3] & 0xf) != (cleared_master[3] & 0xf))
// {
// //TODO this might occasionally legimitately happen? maybe?
// fprintf(stderr," master and slave free buffers don't match!: slave: 0x%x, master: 0x%x\n", cleared_slave[3] & 0xf, cleared_master[3] & 0xf);
// easy_break_point();
// }
}
else
{
fprintf(stderr,"Problem clearing stuff :(\n");
return ret;
}
}
return 0;
}
static int loop_over_chunks_half_duplex(nuphase_dev_t * d, nuphase_which_board_t which, uint8_t naddr, uint8_t start_address, uint8_t * result)
{
int iaddr;
int ichunk;
int ret = 0;
for (iaddr = 0; iaddr < naddr; iaddr++)
{
ret += buffer_append(d,which, buf_ram_addr[start_address + iaddr], 0);
if (ret) return ret;
for (ichunk = 0; ichunk < NP_NUM_CHUNK; ichunk++)
{
ret+= buffer_append(d,which, buf_chunk[ichunk], 0);
if (ret) return ret;
ret+= buffer_append(d, which, 0, result + NP_NUM_CHUNK *NP_SPI_BYTES* iaddr + ichunk * NP_SPI_BYTES);
if (ret) return ret;
}
}
return 0;
}
static int __attribute__((unused))
loop_over_chunks_full_duplex(nuphase_dev_t * d, nuphase_which_board_t which, uint8_t naddr, uint8_t start_address, uint8_t * result)
{
int iaddr;
int ichunk;
int ret = 0;
for (iaddr =0; iaddr < naddr; iaddr++)
{
ret += buffer_append(d,which, buf_ram_addr[start_address + iaddr], 0);
if (ret) return ret;
for (ichunk = 0; ichunk < NP_NUM_CHUNK; ichunk++)
{
ret+= buffer_append(d,which, buf_chunk[ichunk], iaddr == 0 && ichunk == 0 ? 0 : result + NP_NUM_CHUNK * NP_SPI_BYTES * iaddr + (ichunk-1) * NP_SPI_BYTES);
if (ret) return ret;
if (iaddr == naddr-1 && ichunk == NP_NUM_CHUNK - 1)
{
ret+= buffer_append(d, which, 0, result + NP_NUM_CHUNK *NP_SPI_BYTES* iaddr + ichunk * NP_SPI_BYTES);
if (ret) return ret;
}
}
}
return 0;
}
int nuphase_read_raw(nuphase_dev_t *d, uint8_t buffer, uint8_t channel, uint8_t start, uint8_t finish, uint8_t * data, nuphase_which_board_t which)
{
uint8_t naddress = finish - start + 1;
int ret = 0;
USING(d); //have to lock for the duration otherwise channel /read mode may be changed form underneath us.
// we don't lock before these because there is no way we sent enough transfers to trigger a read
//
/*
ret += buffer_append(d,which, buf_mode[MODE_WAVEFORMS], 0); if (ret) return 0;
d->current_mode[which] = MODE_WAVEFORMS;
*/
ret += buffer_append(d,which, buf_buffer[buffer], 0); if (ret) return 0;
d->current_buf[which] = buffer;
ret += buffer_append(d,which, buf_channel[channel], 0); if (ret) return 0;
ret += loop_over_chunks_half_duplex(d,which, naddress, start, data);
if(!ret) ret = buffer_send(d,which); //pick up the stragglers.
DONE(d);
return ret;
}
int nuphase_read_register(nuphase_dev_t * d, uint8_t address, uint8_t *result, nuphase_which_board_t which)
{
int ret;
if (address > NP_ADDRESS_MAX) return -1;
USING(d);
ret = append_read_register(d,which, address,result);
ret += buffer_send(d,which);
DONE(d);
if ( result[0] != address)
{
fprintf(stderr,"WARNING: read register mismatch. Expected 0x%x, got 0x%x\n", address, result[0]);
ret++;
}
return ret;
}
int nuphase_sw_trigger(nuphase_dev_t * d )
{
const uint8_t buf[4] = { REG_FORCE_TRIG, 0,0, 1};
int ret = 0;
if (NBD(d) < 2)
{
USING(d);
int wrote;
wrote = do_write(d->fd[0], buf); //always the master
ret = wrote == NP_SPI_BYTES ? 0 : -1;
DONE(d);
}
else
{
ret = synchronized_command(d, buf, 0,0,0);
}
return ret;
}
int nuphase_calpulse(nuphase_dev_t * d, unsigned state)
{
uint8_t buf[4] = { REG_CALPULSE, 0,0, state};
int ret;
USING(d);
ret = do_write(d->fd[0], buf);
ret = do_write(d->fd[1], buf);
DONE(d);
return ret == NP_SPI_BYTES ? 0 : 1 ;
}
static int board_id_counter =1;
nuphase_dev_t * nuphase_open(const char * devicename_master,
const char * devicename_slave,
int gpio_number, int locking)
{
int locked,fd[2];
nuphase_dev_t * dev;
fd[0] = open(devicename_master, O_RDWR);
if (fd[0] < 0)
{
fprintf(stderr,"Could not open %s\n", devicename_master);
return 0;
}
locked = flock(fd[0],LOCK_EX | LOCK_NB);
if (locked < 0)
{
fprintf(stderr,"Could not get exclusive access to %s\n", devicename_master);
close(fd[0]);
return 0;
}
if (devicename_slave)
{
fd[1] = open(devicename_slave, O_RDWR);
if(fd[1] < 0)
{
fprintf(stderr, "Could not open %s\n" ,devicename_slave);
close(fd[0]);
return 0;
}
locked = flock(fd[1], LOCK_EX | LOCK_NB);
if (locked < 0)
{
fprintf(stderr,"Could not get exclusive access to %s\n", devicename_master);
close(fd[0]);
close(fd[1]);
return 0;
}
}
else
{
fd[1] = 0;
}
bbb_gpio_pin_t * gpio_pin = 0;
if (gpio_number)
{
gpio_pin = bbb_gpio_open(gpio_number);
bbb_gpio_set(gpio_pin,0);
}
//make sure sync is off
do_write(fd[0], buf_sync_off);
dev = malloc(sizeof(nuphase_dev_t));
dev->poll_interval = 500;
memset(dev,0,sizeof(*dev));
dev->gpio_pin = gpio_pin;
dev->device_name[0] = devicename_master;
dev->device_name[1] = devicename_slave;
memcpy(dev->fd,fd,sizeof(fd));
dev->spi_clock = SPI_CLOCK;
dev->cancel_wait = 0;
dev->event_counter = 0;
dev->surface_event_counter = 0;
dev->next_read_buffer = 0;
dev->cs_change =NP_CS_CHANGE;
dev->delay_us =NP_DELAY_USECS;
dev->current_buf[0] = -1;
dev->current_buf[1] = -1;
// dev->current_mode[0] = -1;
// dev->current_mode[1] = -1;
dev->min_threshold = 5000;
dev->surface_readout = 1;
//Configure the SPI protocol
uint8_t mode = SPI_MODE_0; //we could change the chip select here too
int ifd = 0;
for (ifd = 0; ifd < NBD(dev); ifd++)
{
ioctl(dev->fd[ifd], SPI_IOC_WR_MODE, &mode);
ioctl(dev->fd[ifd], SPI_IOC_WR_MAX_SPEED_HZ, &dev->spi_clock);
}
// if this is still running in 20 years, someone will have to fix the y2k38 problem
dev->readout_number_offset = ((uint64_t)time(0)) << 32;
dev->buffer_length = 624;
dev->surface_buffer_length = 624;
dev->channel_read_mask[0] = 0xff;
dev->channel_read_mask[1] = fd[1] ? 0xff : 0;
dev->surface_channel_read_mask = 0xfc;
dev->board_id[0] = board_id_counter++;
dev->board_id[1] = fd[1] ? board_id_counter++ : 0;
dev->enable_locking = locking;
memset(dev->nused,0,sizeof(dev->nused));
setup_xfers(dev);
if (locking)
{
pthread_mutex_init(&dev->mut,0);
pthread_mutex_init(&dev->wait_mut,0);
}
//check if this is a master or slave if locking is enabled
uint8_t fwver[4];
nuphase_read_register(dev, REG_FIRMWARE_VER, fwver, MASTER);
if (!fwver[1])
{
fprintf(stderr,"WARNING! The device chosen as master does not identify as master.\n");
}
if (fd[1])
{
nuphase_read_register(dev, REG_FIRMWARE_VER, fwver, SLAVE);
if (fwver[1])
{
fprintf(stderr,"WARNING! The device chosen as slave does not identify as slave.\n");
}
}
if (nuphase_reset(dev, NP_RESET_COUNTERS))
{
fprintf(stderr,"Unable to reset device... ");
nuphase_close(dev);
return 0;
}
dev->pretrigger = 4;
dev->surface_pretrigger = 4;
dev->surface_throttle = 0;
dev->n_skipped_in_last_second = 0;
dev->last_second_skipped = 0;
dev->last_surface_second = 0;
dev->nsurface_in_last_second = 0;
dev->clear_buffer_when_throttled = 0;
return dev;
}
void nuphase_set_board_id(nuphase_dev_t * d, uint8_t id, nuphase_which_board_t which)
{
if (id <= board_id_counter) board_id_counter = id+1;
d->board_id[which] = id;
}
uint8_t nuphase_get_board_id(const nuphase_dev_t * d, nuphase_which_board_t which)
{
return d->board_id[which];
}
void nuphase_set_readout_number_offset(nuphase_dev_t * d, uint64_t offset)
{
d->readout_number_offset = offset;
}
void nuphase_set_surface_buffer_length(nuphase_dev_t * d, uint16_t length)
{
USING(d); //definitely do not want to change this mid readout
d->surface_buffer_length = length;
DONE(d);
}
void nuphase_set_buffer_length(nuphase_dev_t * d, uint16_t length)
{
USING(d); //definitely do not want to change this mid readout
d->buffer_length = length;
DONE(d);
}
uint16_t nuphase_get_bufferlength(const nuphase_dev_t * d)
{
return d->buffer_length;
}
int nuphase_fwinfo(nuphase_dev_t * d, nuphase_fwinfo_t * info, nuphase_which_board_t which)
{
//we need to read 5 registers, so 15 xfers
int ret = 0;
uint8_t version[NP_SPI_BYTES];
uint8_t date[NP_SPI_BYTES];
uint8_t dna_low[NP_SPI_BYTES];
uint8_t dna_mid[NP_SPI_BYTES];
uint8_t dna_hi[NP_SPI_BYTES];
USING(d);
ret+=append_read_register(d, which,REG_FIRMWARE_VER, version);
ret+=append_read_register(d, which,REG_FIRMWARE_DATE, date);
ret+=append_read_register(d, which,REG_CHIPID_LOW, dna_low);
ret+=append_read_register(d, which,REG_CHIPID_MID, dna_mid);
ret+=append_read_register(d, which,REG_CHIPID_HI, dna_hi);
ret+=buffer_send(d, which);
DONE(d);
info->ver.major = version[3] >>4 ;
info->ver.minor = version[3] & 0x0f;
info->ver.master = version[1] & 1;
info->date.day = date[3] & 0xff;
info->date.month = date[2] & 0xf;
info->date.year = (date[2] >> 4) + (date[1] << 4);
//TODO check this logic. not sure endianness is correct
uint64_t dna_low_big = ((uint64_t) dna_low[3]) | ((uint64_t) dna_low[2]) << 8 | ((uint64_t) dna_low[1]) << 16;
uint64_t dna_mid_big = ((uint64_t) dna_mid[3]) | ((uint64_t) dna_mid[2]) << 8 | ((uint64_t) dna_mid[1]) << 16;
uint64_t dna_hi_big = ((uint64_t) dna_hi[3]) | ((uint64_t)dna_hi[2]) << 8 ;
info->dna = (dna_low_big & 0xffffff) | ( (dna_mid_big & 0xffffff) << 24) | ( (dna_hi_big & 0xffff) << 48);
return ret;
}
int nuphase_close(nuphase_dev_t * d)
{
int ret = 0;
nuphase_cancel_wait(d);
int ibd;
USING(d);
//clear the buffers!
for (ibd = 0; ibd < NBD(d); ibd++)
{
ret+= buffer_send(d, ibd);
}
if (d->enable_locking)
{
//this should be allowed?
pthread_mutex_unlock(&d->mut);
ret += 64* pthread_mutex_destroy(&d->mut);
if (pthread_mutex_trylock(&d->wait_mut)) // lock is beind held by a thread
{
nuphase_cancel_wait(d);
pthread_mutex_lock(&d->wait_mut);
}
pthread_mutex_unlock(&d->wait_mut);
ret += 128* pthread_mutex_destroy(&d->wait_mut);
d->enable_locking = 0;
}
if (d->gpio_pin)
{
ret += 256*bbb_gpio_close(d->gpio_pin,0);
}
if (d->fd[1])
{
ret += flock(d->fd[1], LOCK_UN);
ret += 4*close(d->fd[1]);
}
ret += 8*flock(d->fd[0], LOCK_UN);
ret += 16*close(d->fd[0]);
free(d);
return ret;
}
void nuphase_cancel_wait(nuphase_dev_t *d)
{
d->cancel_wait = 1;
}
int nuphase_wait(nuphase_dev_t * d, nuphase_buffer_mask_t * ready_buffers, float timeout, int * surface_wait)
{
//If locking is enabled and a second thread attempts
//to wait for the same device, return EBUSY.
// making nuphase_wait for multiple threads sounds way too hard
if (d->enable_locking)
{
if (pthread_mutex_trylock(&d->wait_mut))
{
return EBUSY;
}
}
/* This was cancelled before (or almost concurrently with when) we started.
/ We'll just clear the cancel and return EAGAIN. I thought long and hard
/ about what to do in the case that nuphase_cancel_wait is called and there
/ is nothing waiting, but I think attempting to detect if nuphase_wait is
/ currently called is fraught with race conditions. Anyway,
/ nuphase_cancel_wait will probably not be called willy-nilly... usually
/ just when you want to exit the program I imagine.
*/
if (d->cancel_wait)
{
d->cancel_wait = 0;
if (d->enable_locking) pthread_mutex_unlock(&d->wait_mut); //unlock the mutex
return EAGAIN;
}
nuphase_buffer_mask_t something = 0;
int something_surface = 0;
struct timespec start;
if (timeout >0) clock_gettime(CLOCK_MONOTONIC, &start);
nuphase_which_board_t which = NBD(d) > 1 ? SLAVE : MASTER;
if (which == MASTER) surface_wait = 0;
float waited = 0;
// keep trying until we either get something, are cancelled, or exceed our timeout (if we have a timeout)
while(!something && !something_surface && (timeout <= 0 || waited < timeout))
{
something = nuphase_check_buffers(d,&d->hardware_next,which, surface_wait ? &something_surface : 0);