-
Notifications
You must be signed in to change notification settings - Fork 4
/
oni.c
1331 lines (1060 loc) · 42.2 KB
/
oni.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
// Hint -- "NB:..." indicates why. Other comments indicate what.
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "oni.h"
#include "onidriverloader.h"
// TODO:
// 1. Get rid of oni_fifo_size_t and anything else
// hardcoding fifo width at this top level driver. Or the loaded driver
// could import this number somehow (e.g. via extern variable or context member).
// 2. FMC host rev 1.3 vs 1.4 in devices.
// Device hash table overhead factor
#define ONI_DEVHASHOVERHEAD (10.0)
// NB: 255 in sub-address is invalid in this implementation,
// so we can use this to clear the hash table
#define ONI_DEVIDXNULL 0xFFFFFFFF
// Consistent overhead bytestuffing buffer size
#define ONI_COBSBUFFERSIZE 255
// Frame constants
#define ONI_RFRAMEHEADERSZ sizeof(oni_fifo_time_t) + 2 * sizeof(oni_fifo_dat_t) // [time, dev_idx, data_sz]
#define ONI_WFRAMEHEADERSZ 2 * sizeof(oni_fifo_dat_t) // [dev_idx, data_sz]
// Reference counter
struct ref {
void (*free)(const struct ref *);
int count;
};
// Reference counting buffer
struct oni_buf_impl {
// Raw data buffer
uint8_t *buffer;
uint8_t *read_pos;
uint8_t *end_pos;
// Reference count
struct ref count;
};
// Frame with attached, automatically managed storage
typedef union {
oni_frame_t public;
struct {
oni_frame_t f;
struct oni_buf_impl *buffer;
} private;
} oni_frame_impl_t;
// Acquisition context
struct oni_ctx_impl {
// Hardware translation driver
oni_driver_t driver;
// Device array
oni_size_t num_dev;
oni_device_t *dev_table;
// oni_device_t.idx addressable device hash table
oni_size_t dev_hash_len;
oni_device_t *dev_hash_table;
// Maximum frame size (bytes, includes header)
oni_size_t max_read_frame_size;
oni_size_t max_write_frame_size;
// Block read/write size (bytes, defaults to max_read/write_frame_size)
oni_size_t block_read_size;
oni_size_t block_write_size;
// Current, attached buffers
struct oni_buf_impl *shared_rbuf;
struct oni_buf_impl *shared_wbuf;
// Acquisition state
enum {
CTXNULL = 0,
UNINITIALIZED,
IDLE,
RUNNING
} run_state;
};
// Signal flags
typedef enum {
NULLSIG = (1u << 0),
CONFIGWACK = (1u << 1), // Configuration write-acknowledgment
CONFIGWNACK = (1u << 2), // Configuration no-write-acknowledgment
CONFIGRACK = (1u << 3), // Configuration read-acknowledgment
CONFIGRNACK = (1u << 4), // Configuration no-read-acknowledgment
DEVICETABLEACK = (1u << 5), // Device table start acknowledgment
DEVICEINST = (1u << 6), // Device table instance
} oni_signal_t;
// Static helpers
static inline oni_dev_idx_t _oni_hash32(oni_dev_idx_t x);
static inline int _oni_hash32_find(oni_ctx ctx, oni_dev_idx_t x);
static int _oni_reset_routine(oni_ctx ctx);
static inline int _oni_read(oni_ctx ctx, oni_read_stream_t stream, void *data, size_t size);
static inline int _oni_write(oni_ctx ctx, oni_write_stream_t stream, const char* data, size_t size);
static int _oni_read_signal_packet(oni_ctx ctx, uint8_t *buffer);
static int _oni_read_signal_data(oni_ctx ctx, oni_signal_t *type, void *data, size_t size);
static int _oni_pump_signal_type(oni_ctx ctx, int flags, oni_signal_t *type);
static int _oni_pump_signal_data(oni_ctx ctx, int flags, oni_signal_t *type, void *data, int size);
static int _oni_cobs_unstuff(uint8_t *dst, const uint8_t *src, size_t size);
static inline int _oni_write_config(oni_ctx ctx, oni_config_t reg, oni_reg_val_t value);
static inline int _oni_read_config(oni_ctx, oni_config_t reg, oni_reg_val_t *value);
static int _oni_alloc_write_buffer(oni_ctx ctx, void **data, size_t size);
static int _oni_read_buffer(oni_ctx ctx, void **data, size_t size, int);
static void _oni_dump_buffers(oni_ctx ctx);
static void _oni_destroy_buffer(const struct ref *ref);
static inline void _ref_inc(const struct ref *ref);
static inline void _ref_dec(const struct ref *ref);
oni_ctx oni_create_ctx(const char* drv_name)
{
oni_ctx ctx = calloc(1, sizeof(struct oni_ctx_impl));
if (ctx == NULL) {
errno = EAGAIN;
return NULL;
}
if (oni_create_driver(drv_name, &ctx->driver)) {
errno = EINVAL;
free(ctx);
return NULL;
}
ctx->num_dev = 0;
ctx->dev_hash_table = NULL;
ctx->run_state = UNINITIALIZED;
return ctx;
}
int oni_init_ctx(oni_ctx ctx, int host_idx)
{
assert(ctx != NULL && "Context is NULL.");
assert(ctx->run_state == UNINITIALIZED && "Context is in invalid state.");
if (ctx->run_state != UNINITIALIZED)
return ONI_EINVALSTATE;
int rc = ctx->driver.init(ctx->driver.ctx, host_idx);
if (rc) return rc;
// NB: Trigger reset routine (populates device table and key acquisition
// parameters) Success will set ctx->run_state to IDLE
// Set the reset register
rc = _oni_write_config(ctx, ONI_CONFIG_RESET, 1);
if (rc) return rc;
// Get device table etc
rc = _oni_reset_routine(ctx);
if (rc) return rc;
// Run state is now IDLE
ctx->run_state = IDLE;
return ONI_ESUCCESS;
}
int oni_destroy_ctx(oni_ctx ctx)
{
assert(ctx != NULL && "Context is NULL");
int rc = ctx->driver.destroy_ctx(ctx->driver.ctx);
if (rc) return rc;
if (ctx->dev_hash_table != NULL)
free(ctx->dev_hash_table);
free(ctx);
return ONI_ESUCCESS;
}
int oni_get_opt(const oni_ctx ctx, int ctx_opt, void *value, size_t *option_len)
{
assert(ctx != NULL && "Context is NULL");
switch (ctx_opt) {
case ONI_OPT_DEVICETABLE: {
assert(ctx->run_state > UNINITIALIZED && "Context state must be IDLE or RUNNING.");
if (ctx->run_state < IDLE)
return ONI_EINVALSTATE;
size_t required_bytes = sizeof(oni_device_t) * ctx->num_dev;
if (*option_len < required_bytes)
return ONI_EBUFFERSIZE;
memcpy(value, ctx->dev_table, required_bytes);
*option_len = required_bytes;
break;
}
case ONI_OPT_NUMDEVICES: {
assert(ctx->run_state > UNINITIALIZED && "Context state must be IDLE or RUNNING.");
if (ctx->run_state < IDLE)
return ONI_EINVALSTATE;
size_t required_bytes = sizeof(oni_size_t);
if (*option_len < required_bytes)
return ONI_EBUFFERSIZE;
*(oni_size_t *)value = ctx->num_dev;
*option_len = required_bytes;
break;
}
case ONI_OPT_RUNNING: {
assert(ctx->run_state > UNINITIALIZED && "Context state must be IDLE or RUNNING.");
if (ctx->run_state < IDLE)
return ONI_EINVALSTATE;
if (*option_len < ONI_REGSZ)
return ONI_EBUFFERSIZE;
int rc = _oni_read_config(ctx, ONI_CONFIG_RUNNING, value);
if (rc) return rc;
*option_len = ONI_REGSZ;
break;
}
case ONI_OPT_SYSCLKHZ: {
assert(ctx->run_state > UNINITIALIZED && "Context state must be IDLE or RUNNING.");
if (ctx->run_state < IDLE)
return ONI_EINVALSTATE;
if (*option_len < ONI_REGSZ)
return ONI_EBUFFERSIZE;
int rc = _oni_read_config(ctx, ONI_CONFIG_SYSCLKHZ, value);
if (rc) return rc;
*option_len = ONI_REGSZ;
break;
}
case ONI_OPT_ACQCLKHZ: {
assert(ctx->run_state > UNINITIALIZED && "Context state must be IDLE or RUNNING.");
if (ctx->run_state < IDLE)
return ONI_EINVALSTATE;
if (*option_len < ONI_REGSZ)
return ONI_EBUFFERSIZE;
int rc = _oni_read_config(ctx, ONI_CONFIG_ACQCLKHZ, value);
if (rc) return rc;
*option_len = ONI_REGSZ;
break;
}
case ONI_OPT_HWADDRESS: {
assert(ctx->run_state > UNINITIALIZED && "Context state must be IDLE or RUNNING.");
if (ctx->run_state < IDLE)
return ONI_EINVALSTATE;
if (*option_len < ONI_REGSZ)
return ONI_EBUFFERSIZE;
int rc = _oni_read_config(ctx, ONI_CONFIG_HWADDRESS, value);
if (rc) return rc;
*option_len = ONI_REGSZ;
break;
}
case ONI_OPT_MAXREADFRAMESIZE: {
assert(ctx->run_state > UNINITIALIZED
&& "Context state must be IDLE or RUNNING.");
if (ctx->run_state < IDLE)
return ONI_EINVALSTATE;
size_t required_bytes = sizeof(oni_size_t);
if (*option_len < required_bytes)
return ONI_EBUFFERSIZE;
*(oni_size_t *)value = ctx->max_read_frame_size;
*option_len = required_bytes;
break;
}
case ONI_OPT_MAXWRITEFRAMESIZE: {
assert(ctx->run_state > UNINITIALIZED
&& "Context state must be IDLE or RUNNING.");
if (ctx->run_state < IDLE)
return ONI_EINVALSTATE;
size_t required_bytes = sizeof(oni_size_t);
if (*option_len < required_bytes)
return ONI_EBUFFERSIZE;
*(oni_size_t *)value = ctx->max_write_frame_size;
*option_len = required_bytes;
break;
}
case ONI_OPT_BLOCKREADSIZE: {
assert(ctx->run_state > UNINITIALIZED
&& "Context state must be IDLE or RUNNING.");
if (ctx->run_state < IDLE)
if (*option_len < ONI_REGSZ)
return ONI_EBUFFERSIZE;
*(oni_size_t *)value = ctx->block_read_size;
*option_len = ONI_REGSZ;
break;
}
case ONI_OPT_BLOCKWRITESIZE: {
assert(ctx->run_state > UNINITIALIZED
&& "Context state must be IDLE or RUNNING.");
if (ctx->run_state < IDLE)
if (*option_len < ONI_REGSZ)
return ONI_EBUFFERSIZE;
*(oni_size_t *)value = ctx->block_write_size;
*option_len = ONI_REGSZ;
break;
}
case ONI_OPT_RESET:
case ONI_OPT_RESETACQCOUNTER:
return ONI_EWRITEONLY;
default: {
// Attempt to read to custom (outside ONI spec) configuration
// option
assert(ctx_opt >= ONI_OPT_CUSTOMBEGIN && "Invalid custom configuration register.");
if (ctx_opt < ONI_OPT_CUSTOMBEGIN)
return ONI_EPROTCONFIG;
assert(ctx->run_state > UNINITIALIZED && "Context state must be IDLE or RUNNING.");
if (ctx->run_state < IDLE)
return ONI_EINVALSTATE;
if (*option_len < ONI_REGSZ)
return ONI_EBUFFERSIZE;
int rc = _oni_read_config(ctx,
ONI_CONFIG_CUSTOMBEGIN
+ (ctx_opt - ONI_OPT_CUSTOMBEGIN),
value);
if (rc) return rc;
*option_len = ONI_REGSZ;
break;
}
}
return ONI_ESUCCESS;
}
int oni_set_opt(oni_ctx ctx, int ctx_opt, const void *value, size_t option_len)
{
assert(ctx != NULL && "Context is NULL");
switch (ctx_opt) {
case ONI_OPT_RUNNING: {
assert(ctx->run_state > UNINITIALIZED && "Context state must be IDLE or RUNNING.");
if (ctx->run_state < IDLE)
return ONI_EINVALSTATE;
if (option_len != ONI_REGSZ)
return ONI_EBUFFERSIZE;
int rc = _oni_write_config(
ctx, ONI_CONFIG_RUNNING, *(oni_reg_val_t*)value);
if (rc) return rc;
// Dump buffers
// TODO: Is this always the right thing to do? In the case our RIFFA implementation, yes.
// But other implementations may not clear intermediate FIFOs meaning that the first data
// encountered on restart is not the start of a frame.
_oni_dump_buffers(ctx);
if (*(oni_reg_val_t *)value != 0)
ctx->run_state = RUNNING;
else
ctx->run_state = IDLE;
break;
}
case ONI_OPT_RESET: {
assert(ctx->run_state == IDLE && "Context state must be IDLE.");
if (ctx->run_state != IDLE)
return ONI_EINVALSTATE;
if (option_len != ONI_REGSZ)
return ONI_EBUFFERSIZE;
if (*(oni_reg_val_t *)value != 0) {
int rc = _oni_write_config(
ctx, ONI_CONFIG_RESET, *(oni_reg_val_t*)value);
if (rc) return rc;
// Get device table etc
_oni_reset_routine(ctx);
}
break;
}
case ONI_OPT_RESETACQCOUNTER: {
assert(ctx->run_state > UNINITIALIZED && "Context state must be IDLE or RUNNING.");
if (ctx->run_state < IDLE)
return ONI_EINVALSTATE;
if (option_len != ONI_REGSZ)
return ONI_EBUFFERSIZE;
if (*(oni_reg_val_t *)value != 0) {
int rc = _oni_write_config(
ctx, ONI_CONFIG_RESETACQCOUNTER, *(oni_reg_val_t *)value);
if (rc) return rc;
}
break;
}
case ONI_OPT_HWADDRESS: {
assert(ctx->run_state > UNINITIALIZED && "Context state must be IDLE or RUNNING.");
if (ctx->run_state < IDLE)
return ONI_EINVALSTATE;
if (option_len != ONI_REGSZ)
return ONI_EBUFFERSIZE;
int rc = _oni_write_config(
ctx, ONI_CONFIG_HWADDRESS, *(oni_reg_val_t *)value);
if (rc) return rc;
break;
}
case ONI_OPT_BLOCKREADSIZE: {
// NB: If we are careful, this could be changed during RUNNING
// state. However, I would need to perform runtime size check of
// the block readsize. Also, what if it occurred on a separate
// thread during a call to oni_read_frame?
assert(ctx->run_state == IDLE && "Context state must be IDLE.");
if (ctx->run_state != IDLE)
return ONI_EINVALSTATE;
if (option_len != sizeof(oni_size_t))
return ONI_EBUFFERSIZE;
oni_size_t block_read_size = *(oni_size_t *)value;
// Make sure the block read size is greater than max frame size
if (block_read_size < ctx->max_read_frame_size)
return ONI_EINVALREADSIZE;
// Make sure the block read size is a multiple of the FIFO width
if (block_read_size % sizeof(oni_fifo_dat_t) != 0)
return ONI_EINVALREADSIZE;
ctx->block_read_size = block_read_size;
break;
}
case ONI_OPT_BLOCKWRITESIZE: {
// NB: If we are careful, this could be changed during RUNNING
// state. However, I would need to perform runtime size check of
// the block readsize. Also, what if it occurred on a separate
// thread during a call to oni_read_frame?
assert(ctx->run_state == IDLE && "Context state must be IDLE.");
if (ctx->run_state != IDLE)
return ONI_EINVALSTATE;
if (option_len != sizeof(oni_size_t))
return ONI_EBUFFERSIZE;
oni_size_t block_write_size = *(oni_size_t *)value;
// Make sure the block read size is greater than max frame size
if (block_write_size < ctx->max_write_frame_size)
return ONI_EINVALWRITESIZE;
// Make sure the block read size is a multiple of the FIFO width
if (block_write_size % sizeof(oni_fifo_dat_t) != 0)
return ONI_EINVALWRITESIZE;
ctx->block_write_size = block_write_size;
break;
}
case ONI_OPT_DEVICETABLE:
case ONI_OPT_NUMDEVICES:
case ONI_OPT_SYSCLKHZ:
case ONI_OPT_ACQCLKHZ:
case ONI_OPT_MAXREADFRAMESIZE:
case ONI_OPT_MAXWRITEFRAMESIZE:
return ONI_EREADONLY;
default: {
// Attempt to write to custom (outside ONI spec) configuration
// option
assert(ctx_opt >= ONI_OPT_CUSTOMBEGIN && "Invalid custom configuration register.");
if (ctx_opt < ONI_OPT_CUSTOMBEGIN)
return ONI_EPROTCONFIG;
assert(ctx->run_state > UNINITIALIZED && "Context state must be IDLE or RUNNING.");
if (ctx->run_state < IDLE)
return ONI_EINVALSTATE;
if (option_len != ONI_REGSZ)
return ONI_EBUFFERSIZE;
int rc = _oni_write_config(ctx,
ONI_CONFIG_CUSTOMBEGIN + (ctx_opt - ONI_OPT_CUSTOMBEGIN),
*(oni_reg_val_t *)value);
if (rc) return rc;
break;
}
}
return ctx->driver.set_opt_callback(ctx->driver.ctx, ctx_opt, value, option_len);
}
int oni_get_driver_opt(const oni_ctx ctx, int drv_opt, void* value, size_t *option_len)
{
return ctx->driver.get_opt(ctx->driver.ctx, drv_opt, value, option_len);
}
int oni_set_driver_opt(oni_ctx ctx, int drv_opt, const void* value, size_t option_len)
{
return ctx->driver.set_opt(ctx->driver.ctx, drv_opt, value, option_len);
}
int oni_write_reg(const oni_ctx ctx,
oni_dev_idx_t dev_idx,
oni_reg_addr_t addr,
oni_reg_val_t value)
{
assert(ctx != NULL && "Context is NULL");
assert(ctx->run_state > UNINITIALIZED && "Context must be INITIALIZED.");
// Make sure we are not already in config triggered state
oni_reg_val_t trig = 0;
int rc = _oni_read_config(ctx, ONI_CONFIG_TRIG, &trig);
if (rc) return rc;
if (trig != 0) return ONI_ERETRIG;
// Set config registers and trigger a write
rc = _oni_write_config(ctx, ONI_CONFIG_DEV_IDX, dev_idx);
if (rc) return rc;
rc = _oni_write_config(ctx, ONI_CONFIG_REG_ADDR, addr);
if (rc) return rc;
rc = _oni_write_config(ctx, ONI_CONFIG_REG_VALUE, value);
if (rc) return rc;
oni_reg_val_t rw = 1;
rc = _oni_write_config(ctx, ONI_CONFIG_RW, rw);
if (rc) return rc;
trig = 1;
rc = _oni_write_config(ctx, ONI_CONFIG_TRIG, trig);
if (rc) return rc;
// Wait for response from hardware
oni_signal_t type;
rc = _oni_pump_signal_type(ctx, CONFIGWACK | CONFIGWNACK, &type);
if (rc) return rc;
if (type == CONFIGWNACK) return ONI_EWRITEFAILURE;
return ONI_ESUCCESS;
}
int oni_read_reg(const oni_ctx ctx,
oni_dev_idx_t dev_idx,
oni_reg_addr_t addr,
oni_reg_val_t *value)
{
assert(ctx != NULL && "Context is NULL");
assert(ctx->run_state > UNINITIALIZED && "Context must be INITIALIZED.");
// Make sure we are not already in config triggered state
oni_reg_val_t trig = 0;
int rc = _oni_read_config(ctx, ONI_CONFIG_TRIG, &trig);
if (rc) return rc;
if (trig != 0) return ONI_ERETRIG;
// Set configuration registers and trigger a write
rc = _oni_write_config(ctx, ONI_CONFIG_DEV_IDX, dev_idx);
if (rc) return rc;
rc = _oni_write_config(ctx, ONI_CONFIG_REG_ADDR, addr);
if (rc) return rc;
oni_reg_val_t rw = 0;
rc = _oni_write_config(ctx, ONI_CONFIG_RW, rw);
if (rc) return rc;
trig = 1;
rc = _oni_write_config(ctx, ONI_CONFIG_TRIG, trig);
if (rc) return rc;
// Wait for response from hardware
oni_signal_t type;
rc = _oni_pump_signal_type(ctx, CONFIGRACK | CONFIGRNACK, &type);
if (rc) return rc;
if (type == CONFIGRNACK) return ONI_EREADFAILURE;
rc = _oni_read_config(ctx, ONI_CONFIG_REG_VALUE, value);
if (rc) return rc;
return ONI_ESUCCESS;
}
// NB: Although it seems that with fixed sized reads, we should be able to just
// point the frame header into the shared buffer, the issue is that
// we still need to know what device we are dealing with, which requires that we
// look at the buffer. So there needs to be two _oni_read_buffer's in here no matter
// what, as far as I can tell. But these are basically just function call overhead
// unless there is an allocation event anyway.
int oni_read_frame(const oni_ctx ctx, oni_frame_t **frame)
{
assert(ctx != NULL && "Context is NULL");
// NB: We don't need run_state == RUNNING because this could be changed in
// a different thread
assert(ctx->run_state >= IDLE && "Context is not acquiring.");
// No devices produce data
if (ctx->max_read_frame_size == 0)
return ONI_ENOREADDEV;
// Get the device index and data size from the buffer
// TODO: what is the point of having an oni_fifo_t if we are hard coding the header size anyway?
uint8_t *header = NULL;
int rc = _oni_read_buffer(ctx,
(void **)&header,
ONI_RFRAMEHEADERSZ,
1);
if (rc) return rc;
// Allocate frame and buffer
oni_frame_impl_t *iframe = malloc(sizeof(oni_frame_impl_t));
// Total frame size
int total_size = sizeof(oni_frame_t);
// Copy frame header members (continuous)
// 0. timer (8)
// 1. index (4)
// 2. data_sz (4)
memcpy((void *)&iframe->private.f.time, header, ONI_RFRAMEHEADERSZ);
// Find read size (+ padding)
size_t rsize = iframe->private.f.data_sz;
rsize += rsize % sizeof(oni_fifo_dat_t);
total_size += rsize;
// Read data
rc = _oni_read_buffer(ctx, (void **)&iframe->private.f.data, rsize, 0);
if (rc) return rc;
// Update buffer ref count and provide reference to frame
_ref_inc(&(ctx->shared_rbuf->count));
iframe->private.buffer = ctx->shared_rbuf;
// Public portion of frame
*frame = &iframe->public;
// Size of public portion of frame
return total_size;
}
// NB : Multiframe writes are allowed as long as data_sz is a multiple of
// a single write frame's size.
int oni_create_frame(const oni_ctx ctx,
oni_frame_t **frame,
oni_dev_idx_t dev_idx,
void *data,
size_t data_sz)
{
assert(ctx != NULL && "Context is NULL");
// NB: We don't need run_state == RUNNING because this could be changed in
// a different thread
assert(ctx->run_state >= IDLE && "Context is not acquiring.");
// Get the device hash index
int i = _oni_hash32_find(ctx, dev_idx);
if (i < 0) return ONI_EDEVIDX;
// Check that the devices accepts
if (ctx->dev_hash_table[i].write_size == 0)
return ONI_ENOTWRITEDEV;
// Check that data_sz is a multiple of write_size
if (data_sz % ctx->dev_hash_table[i].write_size != 0)
return ONI_EWRITESIZE;
// Allocate frame and buffer
oni_frame_impl_t *iframe = malloc(sizeof(oni_frame_impl_t));
// Total frame size
int total_size = sizeof(oni_frame_t);
// Pad if needed
size_t asize = data_sz;
asize += asize % sizeof(oni_fifo_dat_t);
total_size += asize;
// Allocate data storage
char *buffer_start = NULL;
int rc = _oni_alloc_write_buffer(ctx, (void **)&buffer_start, ONI_WFRAMEHEADERSZ + asize);
if (rc) return rc;
// Fill out public fields
// NB: https://stackoverflow.com/questions/9691404/how-to-initialize-const-in-a-struct-in-c-with-malloc
*(oni_size_t *)&iframe->private.f.dev_idx = dev_idx;
*(oni_size_t *)&iframe->private.f.data_sz = data_sz;
iframe->private.f.data = buffer_start + ONI_WFRAMEHEADERSZ;
// Copy frame header members into start of continuous buffer, before data
// 0. index (4)
// 2. data_sz (4)
*((oni_fifo_dat_t *)buffer_start + 0) = iframe->private.f.dev_idx;
*((oni_fifo_dat_t *)buffer_start + 1) = iframe->private.f.data_sz >> BYTE_TO_FIFO_SHIFT;
// Copy data into frame
memcpy(iframe->private.f.data, data, data_sz);
// Update buffer ref count and provide reference to frame
_ref_inc(&(ctx->shared_wbuf->count));
iframe->private.buffer = ctx->shared_wbuf;
// Public portion of frame
*frame = &iframe->public;
// Size of public portion of frame
return total_size;
}
int oni_write_frame(const oni_ctx ctx, const oni_frame_t *frame)
{
// Write the frame
oni_frame_impl_t *iframe = (oni_frame_impl_t *)frame;
// Continuous frame starts ONI_WFRAMEHEADERSZ back in shared buffer
size_t wsize = iframe->private.f.data_sz + ONI_WFRAMEHEADERSZ;
int rc = _oni_write(ctx, ONI_WRITE_STREAM_DATA, iframe->private.f.data - ONI_WFRAMEHEADERSZ, wsize);
if (rc != (int)wsize) return ONI_EWRITEFAILURE;
return rc;
}
void oni_destroy_frame(oni_frame_t *frame)
{
if (frame != NULL) {
oni_frame_impl_t* iframe = (oni_frame_impl_t*)frame;
// Decrement buffer reference count
_ref_dec(&(iframe->private.buffer->count));
// Free the container
free(iframe);
}
}
void oni_version(int *major, int *minor, int *patch)
{
*major = ONI_VERSION_MAJOR;
*minor = ONI_VERSION_MINOR;
*patch = ONI_VERSION_PATCH;
}
const char *oni_error_str(int err)
{
assert(err > ONI_MINERRORNUM && "Invalid error number.");
assert(err <= 0 && "Invalid error number.");
switch (err) {
case ONI_ESUCCESS: {
return "Success";
}
case ONI_EPATHINVALID: {
return "Invalid stream path";
}
case ONI_EDEVID: {
return "Invalid device ID";
}
case ONI_EDEVIDX: {
return "Invalid device index";
}
case ONI_EWRITESIZE: {
return "Data size is not an integer multiple of the write size for "
"the designated device";
}
case ONI_EREADFAILURE: {
return "Failure to read from a stream or register";
}
case ONI_EWRITEFAILURE: {
return "Failure to write to a stream or register";
}
case ONI_ENULLCTX: {
return "Attempt to use a NULL context";
}
case ONI_ESEEKFAILURE: {
return "Failure to seek on stream";
}
case ONI_EINVALSTATE: {
return "Invalid operation for the current context run state";
}
case ONI_EINVALOPT: {
return "Invalid context option";
}
case ONI_EINVALARG: {
return "Invalid function arguments";
}
case ONI_ECOBSPACK: {
return "Invalid COBS packet";
}
case ONI_ERETRIG: {
return "Attempt to trigger an already triggered operation";
}
case ONI_EBUFFERSIZE: {
return "Supplied buffer is too small";
}
case ONI_EBADDEVTABLE: {
return "Badly formatted device table supplied by firmware";
}
case ONI_EBADALLOC: {
return "Bad dynamic memory allocation";
}
case ONI_ECLOSEFAIL: {
return "File descriptor close failure (check errno)";
}
case ONI_EREADONLY: {
return "Attempted write to read only object (register, context "
"option, etc)";
}
case ONI_EUNIMPL: {
return "Unimplemented API feature";
}
case ONI_EINVALREADSIZE: {
return "Block read size is smaller than the maximal frame size";
}
case ONI_ENOREADDEV: {
return "Frame read attempted when there are no readable devices in "
"the device table";
}
case ONI_EWRITEONLY: {
return "Attempted to read from a write only object (register, context "
"option, etc)";
}
case ONI_EINIT: {
return "Hardware initialization failed";
}
case ONI_EINVALWRITESIZE: {
return "Write buffer pre-allocation size is smaller than the "
"maximal write frame size";
}
case ONI_ENOTWRITEDEV: {
return "Frame allocation attempted for a non-writable device";
}
case ONI_EDEVIDXREPEAT: {
return "Device table contains repeated device indices";
}
case ONI_EPROTCONFIG : {
return "Attempted to directly read or write a protected "
"configuration option";
}
default:
return "Unknown error";
}
}
static inline oni_dev_idx_t _oni_hash32(oni_dev_idx_t x)
{
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = (x >> 16) ^ x;
return x;
}
static inline int _oni_hash32_find(oni_ctx ctx, oni_dev_idx_t x)
{
int probe;
// TODO: Not sure if this ideal. We are using a hashing function that has no collisions but this
// is not true when doing the modulo here and requires and explicity check == x for that reason.
for (probe = _oni_hash32(x) % ctx->dev_hash_len;
ctx->dev_hash_table[probe].idx != ONI_DEVIDXNULL;
probe = (probe + 1) % ctx->dev_hash_len) {
if (ctx->dev_hash_table[probe].idx == x)
return probe;
}
return -1;
}
static int _oni_reset_routine(oni_ctx ctx)
{
// Get number of devices
oni_signal_t sig_type = NULLSIG;
int rc = _oni_pump_signal_data(
ctx, DEVICETABLEACK, &sig_type, &(ctx->num_dev), sizeof(ctx->num_dev));
if (rc) return rc;
// Hash table size
ctx->dev_hash_len = ctx->num_dev * ONI_DEVHASHOVERHEAD + 1;
// Make space for the device table
oni_device_t *temp = realloc(ctx->dev_table,
ctx->num_dev * sizeof(oni_device_t));
if (temp)
ctx->dev_table = temp;
else
return ONI_EBADALLOC;
// Make space for the device hash table
temp = realloc(ctx->dev_hash_table,
ctx->dev_hash_len * sizeof(oni_device_t));
if (temp)
ctx->dev_hash_table = temp;
else
return ONI_EBADALLOC;
size_t i;
ctx->max_read_frame_size = 0;
ctx->max_write_frame_size = 0;
for (i = 0; i < ctx->num_dev; i++) {
sig_type = NULLSIG;
uint8_t buffer[ONI_COBSBUFFERSIZE];
rc = _oni_read_signal_data(ctx, &sig_type, buffer, ONI_COBSBUFFERSIZE);
if (rc) return rc;
// We should see num_dev device instances appear on the signal stream
if (sig_type != DEVICEINST)
return ONI_EBADDEVTABLE;
// Append the device onto the table
memcpy(ctx->dev_table + i, buffer, sizeof(oni_device_t));
// Check to see if this is the biggest frame in the table
if ((ctx->dev_table + i)->read_size > ctx->max_read_frame_size)
ctx->max_read_frame_size = (ctx->dev_table + i)->read_size;
if ((ctx->dev_table + i)->write_size > ctx->max_write_frame_size)
ctx->max_write_frame_size = (ctx->dev_table + i)->write_size;
}
// Clear the table
for (i = 0; i < ctx->dev_hash_len; i++) {
ctx->dev_hash_table[i].idx = ONI_DEVIDXNULL;
}
// Sort device_table
// Check that dev_idx entries are unique (required for hash)
// Fill the hash_table table
for (i = 0; i < ctx->num_dev; i++) {
// NB: i = ctx->num_dev - 1 will not loop
size_t j;
size_t min_idx = i;
for (j = i + 1; j < ctx->num_dev; j++) {
if (ctx->dev_table[i].idx == ctx->dev_table[j].idx)
return ONI_EDEVIDXREPEAT;
if (ctx->dev_table[j].idx < ctx->dev_table[min_idx].idx)
min_idx = j;
}
// Swap place device i and min_idx
oni_device_t temp = ctx->dev_table[min_idx];
ctx->dev_table[min_idx] = ctx->dev_table[i];
ctx->dev_table[i] = temp;
// Hashing with open addressing
size_t probe;