rp-sys 0.28.1

FFI bindings to redpitaya API
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
use libc::{c_char, c_int, c_uint};
use std::sync::Mutex;

macro_rules! return_cstring {
    ($e:expr) => (
        {
            let s = std::ffi::CString::new($e)
                .unwrap();

            let p = s.as_ptr();
            std::mem::forget(s);

            p
        }
    );
}

pub const ADC_BUFFER_SIZE: u32 = 16384;

pub const RP_OK: u32 = 0;
pub const RP_EOED: u32 = 1;
pub const RP_EOMD: u32 = 2;
pub const RP_ECMD: u32 = 3;
pub const RP_EMMD: u32 = 4;
pub const RP_EUMD: u32 = 5;
pub const RP_EOOR: u32 = 6;
pub const RP_ELID: u32 = 7;
pub const RP_EMRO: u32 = 8;
pub const RP_EWIP: u32 = 9;
pub const RP_EPN: u32 = 10;
pub const RP_UIA: u32 = 11;
pub const RP_FCA: u32 = 12;
pub const RP_RCA: u32 = 13;
pub const RP_BTS: u32 = 14;
pub const RP_EIPV: u32 = 15;
pub const RP_EUF: u32 = 16;
pub const RP_ENN: u32 = 17;
pub const RP_EFOB: u32 = 18;
pub const RP_EFCB: u32 = 19;
pub const RP_EABA: u32 = 20;
pub const RP_EFRB: u32 = 21;
pub const RP_EFWB: u32 = 22;
pub const RP_EMNC: u32 = 23;
pub const RP_NOTS: u32 = 24;

pub const SPECTR_OUT_SIG_LEN: u32 = 2048;

const ADC_SAMPLE_PERIOD: u32 = 8;
const ANALOG_IN_MAX_VAL: f32 = 7.;
const ANALOG_IN_MIN_VAL: f32 = 0.;
const ANALOG_IN_MAX_VAL_INTEGER: u32 = 0xFFF;
const ANALOG_OUT_MAX_VAL: f32 = 1.8;
const ANALOG_OUT_MIN_VAL: f32 = 0.;
const ANALOG_OUT_MAX_VAL_INTEGER: u32 = 156;

struct AcqState {
    decimation: rp_acq_decimation_t,
    averaging: bool,
    gain: Vec<rp_pinState_t>,
    trigger_src: rp_acq_trig_src_t,
    trigger_delay: i64,
    trigger_hyst: f32,
    trigger_level: Vec<f32>,
    arm_keep: bool,
}

impl Default for AcqState
{
    fn default() -> Self
    {
        Self {
            decimation: rp_acq_decimation_t::RP_DEC_1, // @FIXME
            averaging: false,
            gain: vec![rp_pinState_t::RP_LOW; 2],
            trigger_src: rp_acq_trig_src_t::RP_TRIG_SRC_DISABLED,
            trigger_delay: 16381,
            trigger_hyst: 1., // @FIXME
            trigger_level: vec![1.; 3], // @FIXME
            arm_keep: false, // @FIXME
        }
    }
}

struct GpioState {
    p_direction: u32,
    p_state: u32,
    n_direction: u32,
    n_state: u32,
}

impl Default for GpioState
{
    fn default() -> Self
    {
        Self {
            p_direction: 0,
            p_state: 0,
            n_direction: 0,
            n_state: 0,
        }
    }
}

struct GenState {
    offsets: Vec<f32>,
    out_enable: Vec<bool>,
    phases: Vec<f32>,
    waveforms: Vec<rp_waveform_t>,
    modes: Vec<rp_gen_mode_t>,
    trigger_src: Vec<rp_trig_src_t>,
    amp: Vec<f32>,
    burst_count: Vec<i32>,
    burst_period: Vec<u32>,
    burst_repetitions: Vec<i32>,
    duty_cycle: Vec<f32>,
    freq: Vec<f32>,
}

impl Default for GenState
{
    fn default() -> Self
    {
        Self {
            offsets: vec![0.; 2],
            out_enable: vec![false; 2],
            phases: vec![0.; 2],
            waveforms: vec![rp_waveform_t::RP_WAVEFORM_SINE; 2],
            modes: vec![rp_gen_mode_t::RP_GEN_MODE_BURST; 2],
            trigger_src: vec![rp_trig_src_t::RP_GEN_TRIG_SRC_EXT_NE; 2],
            amp: vec![0.; 2],
            burst_count: vec![0; 2],
            burst_period: vec![0; 2],
            burst_repetitions: vec![0; 2],
            duty_cycle: vec![0.; 2],
            freq: vec![0.; 2],
        }
    }
}

struct PinState {
    directions: Vec<rp_pinDirection_t>,
    states: Vec<rp_pinState_t>,
}

impl Default for PinState
{
    fn default() -> Self
    {
        Self {
            directions: vec![rp_pinDirection_t::RP_IN; 24],
            states: vec![rp_pinState_t::RP_LOW; 24],
        }
    }
}

struct State {
    acq: AcqState,
    apin: Vec<u32>,
    calib: rp_calib_params_t,
    gen: GenState,
    gpio: GpioState,
    led_state: u32,
    pin: PinState,
}

impl Default for State {
    fn default() -> Self
    {
        Self {
            acq: Default::default(),
            apin: vec![0; 8],
            calib: Default::default(),
            gen: Default::default(),
            gpio: Default::default(),
            led_state: 0,
            pin: Default::default(),
        }
    }
}

lazy_static::lazy_static! {
    static ref STATE: Mutex<State> = Mutex::new(Default::default());
}

macro_rules! state {
    () => {
        STATE.lock().unwrap()
    }
}

macro_rules! ok {
    () => {
        RP_OK as c_int
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct rp_calib_params_t {
    pub fe_ch1_fs_g_hi: u32,
    pub fe_ch2_fs_g_hi: u32,
    pub fe_ch1_fs_g_lo: u32,
    pub fe_ch2_fs_g_lo: u32,
    pub fe_ch1_lo_offs: i32,
    pub fe_ch2_lo_offs: i32,
    pub be_ch1_fs: u32,
    pub be_ch2_fs: u32,
    pub be_ch1_dc_offs: i32,
    pub be_ch2_dc_offs: i32,
    pub magic: u32,
    pub fe_ch1_hi_offs: i32,
    pub fe_ch2_hi_offs: i32,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum rp_acq_decimation_t {
    RP_DEC_1,
    RP_DEC_8,
    RP_DEC_64,
    RP_DEC_1024,
    RP_DEC_8192,
    RP_DEC_65536,
}

impl std::convert::Into<rp_acq_sampling_rate_t> for rp_acq_decimation_t {
    fn into(self) -> rp_acq_sampling_rate_t {
        use rp_acq_sampling_rate_t::*;

        match self {
            Self::RP_DEC_1 => RP_SMP_125M,
            Self::RP_DEC_8 => RP_SMP_15_625M,
            Self::RP_DEC_64 => RP_SMP_1_953M,
            Self::RP_DEC_1024 => RP_SMP_122_070K,
            Self::RP_DEC_8192 => RP_SMP_15_258K,
            Self::RP_DEC_65536 => RP_SMP_1_907K,
        }
    }
}

impl std::convert::From<rp_acq_sampling_rate_t> for rp_acq_decimation_t {
    fn from(sampling_rate: rp_acq_sampling_rate_t) -> Self {
        use rp_acq_sampling_rate_t::*;

        match sampling_rate {
            RP_SMP_125M => Self::RP_DEC_1,
            RP_SMP_15_625M => Self::RP_DEC_8,
            RP_SMP_1_953M => Self::RP_DEC_64,
            RP_SMP_122_070K => Self::RP_DEC_1024,
            RP_SMP_15_258K => Self::RP_DEC_8192,
            RP_SMP_1_907K => Self::RP_DEC_65536,
        }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum rp_acq_sampling_rate_t {
    RP_SMP_125M,
    RP_SMP_15_625M,
    RP_SMP_1_953M,
    RP_SMP_122_070K,
    RP_SMP_15_258K,
    RP_SMP_1_907K,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum rp_acq_trig_src_t {
    RP_TRIG_SRC_DISABLED,
    RP_TRIG_SRC_NOW,
    RP_TRIG_SRC_CHA_PE,
    RP_TRIG_SRC_CHA_NE,
    RP_TRIG_SRC_CHB_PE,
    RP_TRIG_SRC_CHB_NE,
    RP_TRIG_SRC_EXT_PE,
    RP_TRIG_SRC_EXT_NE,
    RP_TRIG_SRC_AWG_PE,
    RP_TRIG_SRC_AWG_NE,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum rp_acq_trig_state_t {
    RP_TRIG_STATE_TRIGGERED,
    RP_TRIG_STATE_WAITING,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Hash)]
pub enum rp_apin_t {
    RP_AOUT0,
    RP_AOUT1,
    RP_AOUT2,
    RP_AOUT3,
    RP_AIN0,
    RP_AIN1,
    RP_AIN2,
    RP_AIN3,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum rp_channel_t {
    RP_CH_1,
    RP_CH_2,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum rp_channel_trigger_t {
    RP_T_CH_1,
    RP_T_CH_2,
    RP_T_CH_EXT,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum rp_dpin_t {
    RP_LED0,
    RP_LED1,
    RP_LED2,
    RP_LED3,
    RP_LED4,
    RP_LED5,
    RP_LED6,
    RP_LED7,
    RP_DIO0_P,
    RP_DIO1_P,
    RP_DIO2_P,
    RP_DIO3_P,
    RP_DIO4_P,
    RP_DIO5_P,
    RP_DIO6_P,
    RP_DIO7_P,
    RP_DIO0_N,
    RP_DIO1_N,
    RP_DIO2_N,
    RP_DIO3_N,
    RP_DIO4_N,
    RP_DIO5_N,
    RP_DIO6_N,
    RP_DIO7_N,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum rp_gen_mode_t {
    RP_GEN_MODE_CONTINUOUS,
    RP_GEN_MODE_BURST,
    RP_GEN_MODE_STREAM,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum rp_pinDirection_t {
    RP_IN,
    RP_OUT,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum rp_pinState_t {
    RP_LOW,
    RP_HIGH,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum rp_trig_src_t {
    RP_GEN_TRIG_SRC_INTERNAL,
    RP_GEN_TRIG_SRC_EXT_PE,
    RP_GEN_TRIG_SRC_EXT_NE,
    RP_GEN_TRIG_GATED_BURST,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum rp_waveform_t {
    RP_WAVEFORM_SINE,
    RP_WAVEFORM_SQUARE,
    RP_WAVEFORM_TRIANGLE,
    RP_WAVEFORM_RAMP_UP,
    RP_WAVEFORM_RAMP_DOWN,
    RP_WAVEFORM_DC,
    RP_WAVEFORM_PWM,
    RP_WAVEFORM_ARBITRARY,
}

pub unsafe fn rp_AIpinGetValue(pin: c_uint, value: *mut f32) -> c_int
{
    let mut value_raw = 0;
    let result = rp_AIpinGetValueRaw(pin, &mut value_raw);

    *value = (value_raw as f32 / ANALOG_IN_MAX_VAL_INTEGER as f32)
        * (ANALOG_IN_MAX_VAL - ANALOG_IN_MIN_VAL)
        + ANALOG_IN_MIN_VAL;

    result
}

pub unsafe fn rp_AIpinGetValueRaw(pin: c_uint, value: *mut u32) -> c_int
{
    if pin >= 8 {
        return RP_EPN as c_int;
    }

    *value = state!().apin[pin as usize];

    ok!()
}

pub unsafe fn rp_AOpinGetRange(pin: c_uint, min_val: *mut f32, max_val: *mut f32) -> c_int
{
    *min_val = ANALOG_OUT_MIN_VAL;
    *max_val = ANALOG_OUT_MAX_VAL;

    ok!()
}

pub unsafe fn rp_AOpinGetValue(pin: c_uint, value: *mut f32) -> c_int
{
    let mut value_raw = 0;
    let result = rp_AOpinGetValueRaw(pin, &mut value_raw);

    *value = (value_raw as f32 / ANALOG_OUT_MAX_VAL_INTEGER as f32)
        * (ANALOG_OUT_MAX_VAL - ANALOG_OUT_MIN_VAL)
        + ANALOG_OUT_MIN_VAL;

    result
}

pub unsafe fn rp_AOpinGetValueRaw(pin: c_uint, value: *mut u32) -> c_int
{
    if pin >= 8 {
        return RP_EPN as c_int;
    }

    *value = state!().apin[pin as usize];

    ok!()
}

pub unsafe fn rp_AOpinReset() -> c_int
{
    state!().apin = vec![0; 8];

    ok!()
}

pub unsafe fn rp_AOpinSetValue(pin: c_uint, value: f32) -> c_int
{
    let value_raw = (value - ANALOG_OUT_MIN_VAL) / (ANALOG_OUT_MAX_VAL - ANALOG_OUT_MIN_VAL) * ANALOG_OUT_MAX_VAL_INTEGER as f32;

    rp_AOpinSetValueRaw(pin, value_raw as u32)
}

pub unsafe fn rp_AOpinSetValueRaw(pin: c_uint, value: u32) -> c_int
{
    if pin >= 8 {
        return RP_EPN as c_int;
    }

    if value > ANALOG_OUT_MAX_VAL_INTEGER {
        return RP_EOOR as c_int;
    }

    state!().apin[pin as usize] = value;

    ok!()
}

pub unsafe fn rp_AcqGetAveraging(enabled: *mut bool) -> c_int
{
    *enabled = state!().acq.averaging;

    ok!()
}

pub unsafe fn rp_AcqGetBufSize(size: *mut u32) -> c_int
{
    *size = ADC_BUFFER_SIZE;

    ok!()
}

pub unsafe fn rp_AcqGetDataPosRaw(channel: rp_channel_t, start_pos: u32, end_pos: u32, buffer: *mut i16, buffer_size: *mut u32) -> c_int
{
    ok!()
}

pub unsafe fn rp_AcqGetDataPosV(channel: rp_channel_t, start_pos: u32, end_pos: u32, buffer: *mut f32, buffer_size: *mut u32) -> c_int
{
    ok!()
}

pub unsafe fn rp_AcqGetDataRaw(channel: rp_channel_t, pos: u32, size: *mut u32, buffer: *mut i16) -> c_int
{
    ok!()
}

pub unsafe fn rp_AcqGetDataRawV2(pos: u32, size: *mut u32, buffer: *mut u16, buffer2: *mut u16) -> c_int
{
    ok!()
}

pub unsafe fn rp_AcqGetDataV(channel: rp_channel_t, pos: u32, size: *mut u32, buffer: *mut f32) -> c_int
{
    ok!()
}

pub unsafe fn rp_AcqGetDataV2(pos: u32, size: *mut u32, buffer1: *mut f32, buffer2: *mut f32) -> c_int
{
    ok!()
}

pub unsafe fn rp_AcqGetDecimation(decimation: *mut rp_acq_decimation_t) -> c_int
{
    *decimation = state!().acq.decimation;

    ok!()
}

pub unsafe fn rp_AcqSetDecimationFactor(decimation: u32) -> c_int
{
    state!().acq.decimation = decimation.into();

    ok!()
}

pub unsafe fn rp_AcqGetDecimationFactor(decimation: *mut u32) -> c_int
{
    *decimation = state!().acq.decimation.into();

    ok!()
}

pub unsafe fn rp_AcqGetGain(channel: rp_channel_t, state: *mut rp_pinState_t) -> c_int
{
    *state = state!().acq.gain[channel as usize];

    ok!()
}

pub unsafe fn rp_AcqGetGainV(channel: rp_channel_t, voltage: *mut f32) -> c_int
{
    let mut state = rp_pinState_t::RP_LOW;

    rp_AcqGetGain(channel, &mut state);

    if state == rp_pinState_t::RP_LOW {
        *voltage = 1.0;
    }
    else {
        *voltage = 20.0;
    }

    ok!()
}

pub unsafe fn rp_AcqGetLatestDataRaw(channel: rp_channel_t, size: *mut u32, buffer: *mut i16) -> c_int
{
    ok!()
}

pub unsafe fn rp_AcqGetLatestDataV(channel: rp_channel_t, size: *mut u32, buffer: *mut f32) -> c_int
{
    ok!()
}

pub unsafe fn rp_AcqGetNormalizedDataPos(pos: u32) -> u32
{
    pos % ADC_BUFFER_SIZE
}

pub unsafe fn rp_AcqGetOldestDataRaw(channel: rp_channel_t, size: *mut u32, buffer: *mut i16) -> c_int
{
    ok!()
}

pub unsafe fn rp_AcqGetOldestDataV(channel: rp_channel_t, size: *mut u32, buffer: *mut f32) -> c_int
{
    ok!()
}

pub unsafe fn rp_AcqGetPreTriggerCounter(value: *mut u32) -> c_int
{
    *value = 10;

    ok!()
}

pub unsafe fn rp_AcqGetSamplingRate(sampling_rate: *mut rp_acq_sampling_rate_t) -> c_int
{
    *sampling_rate = state!().acq.decimation.into();

    ok!()
}

pub unsafe fn rp_AcqGetSamplingRateHz(sampling_rate: *mut f32) -> c_int
{
    let max_rate: f32 = 125_000_000.;
    let decimation: u32 = state!().acq.decimation.into();

    *sampling_rate = max_rate / decimation as f32;

    0
}

pub unsafe fn rp_AcqGetTriggerDelay(decimated_data_num: *mut i32) -> c_int
{
    let delay = state!().acq.trigger_delay;
    let decimation: u32 = state!().acq.decimation.into();

    *decimated_data_num = (delay / ADC_SAMPLE_PERIOD as i64 / decimation as i64) as i32;

    ok!()
}

pub unsafe fn rp_AcqGetTriggerDelayNs(time_ns: *mut i64) -> c_int
{
    *time_ns = state!().acq.trigger_delay;

    ok!()
}

pub unsafe fn rp_AcqGetTriggerHyst(voltage: *mut f32) -> c_int
{
    *voltage = state!().acq.trigger_hyst;

    ok!()
}

pub unsafe fn rp_AcqGetTriggerLevel(channel: rp_channel_trigger_t, voltage: *mut f32) -> c_int
{
    *voltage = state!().acq.trigger_level[channel as usize];

    ok!()
}

pub unsafe fn rp_AcqGetTriggerSrc(source: *mut rp_acq_trig_src_t) -> c_int
{
    *source = state!().acq.trigger_src;

    ok!()
}

pub unsafe fn rp_AcqGetTriggerState(state: *mut rp_acq_trig_state_t) -> c_int
{
    *state = rp_acq_trig_state_t::RP_TRIG_STATE_TRIGGERED;

    ok!()
}

pub unsafe fn rp_AcqGetWritePointer(pos: *mut u32) -> c_int
{
    *pos = 0;

    ok!()
}

pub unsafe fn rp_AcqGetWritePointerAtTrig(pos: *mut u32) -> c_int
{
    *pos = 0;

    ok!()
}

pub unsafe fn rp_AcqReset() -> c_int
{
    state!().acq = Default::default();

    ok!()
}

pub unsafe fn rp_AcqSetArmKeep(enable: bool) -> c_int
{
    state!().acq.arm_keep = enable;

    ok!()
}

pub unsafe fn rp_AcqGetArmKeep(state: &mut bool) -> c_int
{
    *state = state!().acq.arm_keep;

    ok!()
}

pub unsafe fn rp_AcqSetAveraging(enabled: bool) -> c_int
{
    state!().acq.averaging = enabled;

    ok!()
}

pub unsafe fn rp_AcqGetBufferFillState(state: &mut bool) -> c_int
{
    *state = true;

    ok!()
}

pub unsafe fn rp_AcqSetDecimation(decimation: rp_acq_decimation_t) -> c_int
{
    state!().acq.decimation = decimation;

    ok!()
}

pub unsafe fn rp_AcqSetGain(channel: rp_channel_t, state: rp_pinState_t) -> c_int
{
    state!().acq.gain[channel as usize] = state;

    ok!()
}

pub unsafe fn rp_AcqSetSamplingRate(sampling_rate: rp_acq_sampling_rate_t) -> c_int
{
    state!().acq.decimation = sampling_rate.into();

    ok!()
}

pub unsafe fn rp_AcqSetTriggerDelay(decimated_data_num: i32) -> c_int
{
    let decimation: u32 = state!().acq.decimation.into();

    state!().acq.trigger_delay = (decimated_data_num as u32 * ADC_SAMPLE_PERIOD * decimation) as i64;

    ok!()
}

pub unsafe fn rp_AcqSetTriggerDelayNs(time_ns: i64) -> c_int
{
    state!().acq.trigger_delay = time_ns;

    ok!()
}

pub unsafe fn rp_AcqSetTriggerHyst(voltage: f32) -> c_int
{
    state!().acq.trigger_hyst = voltage;

    ok!()
}

pub unsafe fn rp_AcqSetTriggerLevel(channel: rp_channel_trigger_t, voltage: f32) -> c_int
{
    state!().acq.trigger_level[channel as usize] = voltage;

    ok!()
}

pub unsafe fn rp_AcqSetTriggerSrc(source: rp_acq_trig_src_t) -> c_int
{
    state!().acq.trigger_src = source;

    ok!()
}

pub unsafe fn rp_AcqStart() -> c_int
{
    ok!()
}

pub unsafe fn rp_AcqStop() -> c_int
{
    ok!()
}

pub unsafe fn rp_ApinGetRange(pin: rp_apin_t, min_val: *mut f32, max_val: *mut f32) -> c_int
{
    if pin <= rp_apin_t::RP_AOUT3 {
        *min_val = ANALOG_OUT_MIN_VAL;
        *max_val = ANALOG_OUT_MAX_VAL;
    } else {
        *min_val = ANALOG_IN_MIN_VAL;
        *max_val = ANALOG_IN_MAX_VAL;
    }

    ok!()
}

pub unsafe fn rp_ApinGetValue(pin: rp_apin_t, value: *mut f32) -> c_int
{
    if pin <= rp_apin_t::RP_AOUT3 {
        rp_AOpinGetValue(pin as c_uint, value)
    } else {
        rp_AIpinGetValue(pin as c_uint, value)
    }
}

pub unsafe fn rp_ApinGetValueRaw(pin: rp_apin_t, value: *mut u32) -> c_int
{
    if pin <= rp_apin_t::RP_AOUT3 {
        rp_AOpinGetValueRaw(pin as c_uint, value)
    } else {
        rp_AIpinGetValueRaw(pin as c_uint, value)
    }
}

pub unsafe fn rp_ApinReset() -> c_int
{
    state!().apin = vec![0; 8];

    ok!()
}

pub unsafe fn rp_ApinSetValue(pin: rp_apin_t, value: f32) -> c_int
{
    if pin <= rp_apin_t::RP_AOUT3 {
        rp_AOpinSetValue(pin as c_uint, value)
    } else {
        RP_EPN as c_int
    }
}

pub unsafe fn rp_ApinSetValueRaw(pin: rp_apin_t, value: u32) -> c_int
{
    if pin <= rp_apin_t::RP_AOUT3 {
        rp_AOpinSetValueRaw(pin as c_uint, value)
    } else {
        RP_EPN as c_int
    }
}

pub unsafe fn rp_CalibInit() -> c_int
{
    ok!()
}

pub unsafe fn rp_CalibrateBackEnd(channel: rp_channel_t, out_params: *mut rp_calib_params_t) -> c_int
{
    *out_params = state!().calib;

    ok!()
}

pub unsafe fn rp_CalibrateBackEndOffset(channel: rp_channel_t) -> c_int
{
    ok!()
}

pub unsafe fn rp_CalibrateBackEndScale(channel: rp_channel_t) -> c_int
{
    ok!()
}

pub unsafe fn rp_CalibrateFrontEndOffset(channel: rp_channel_t, gain: rp_pinState_t, out_params: *mut rp_calib_params_t) -> c_int
{
    ok!()
}

pub unsafe fn rp_CalibrateFrontEndScaleHV(channel: rp_channel_t, referentialVoltage: f32, out_params: *mut rp_calib_params_t) -> c_int
{
    ok!()
}

pub unsafe fn rp_CalibrateFrontEndScaleLV(channel: rp_channel_t, referentialVoltage: f32, out_params: *mut rp_calib_params_t) -> c_int
{
    ok!()
}

pub unsafe fn rp_CalibrationReset() -> c_int
{
    state!().calib = Default::default();

    ok!()
}

pub unsafe fn rp_CalibrationSetCachedParams() -> c_int
{
    ok!()
}

pub unsafe fn rp_CalibrationWriteParams(calib_params: rp_calib_params_t) -> c_int
{
    state!().calib = calib_params;

    ok!()
}

pub unsafe fn rp_CmnCnvCntToV(field_len: u32, cnts: u32, adc_max_v: f32, calibScale: u32, calib_dc_off: c_int, user_dc_off: f32) -> f32
{
    cnts as f32
}

pub unsafe fn rp_DpinGetDirection(pin: rp_dpin_t, direction: *mut rp_pinDirection_t) -> c_int
{
    *direction = state!().pin.directions[pin as usize];

    ok!()
}

pub unsafe fn rp_DpinGetState(pin: rp_dpin_t, state: *mut rp_pinState_t) -> c_int
{
    *state = state!().pin.states[pin as usize];

    ok!()
}

pub unsafe fn rp_DpinReset() -> c_int
{
    state!().pin = Default::default();

    ok!()
}

pub unsafe fn rp_DpinSetDirection(pin: rp_dpin_t, direction: rp_pinDirection_t) -> c_int
{
    state!().pin.directions[pin as usize] = direction;

    ok!()
}

pub unsafe fn rp_DpinSetState(pin: rp_dpin_t, state: rp_pinState_t) -> c_int
{
    state!().pin.states[pin as usize] = state;

    ok!()
}

pub unsafe fn rp_EnableDigitalLoop(_enable: bool) -> c_int
{
    ok!()
}

pub unsafe fn rp_GPIOnGetDirection(direction: *mut u32) -> c_int
{
    *direction = state!().gpio.p_direction;

    ok!()
}

pub unsafe fn rp_GPIOnGetState(state: *mut u32) -> c_int
{
    *state = state!().gpio.p_state;

    ok!()
}

pub unsafe fn rp_GPIOnSetDirection(direction: u32) -> c_int
{
    state!().gpio.p_direction = direction;

    ok!()
}

pub unsafe fn rp_GPIOnSetState(state: u32) -> c_int
{
    state!().gpio.p_state = state;

    ok!()
}

pub unsafe fn rp_GPIOpGetDirection(direction: *mut u32) -> c_int
{
    *direction = state!().gpio.n_direction;

    ok!()
}

pub unsafe fn rp_GPIOpGetState(state: *mut u32) -> c_int
{
    *state = state!().gpio.n_state;

    ok!()
}

pub unsafe fn rp_GPIOpSetDirection(direction: u32) -> c_int
{
    state!().gpio.n_direction = direction;

    ok!()
}

pub unsafe fn rp_GPIOpSetState(state: u32) -> c_int
{
    state!().gpio.n_state = state;

    ok!()
}

pub unsafe fn rp_GenAmp(channel: rp_channel_t, amplitude: f32) -> c_int
{
    state!().gen.amp[channel as usize] = amplitude;

    ok!()
}

pub unsafe fn rp_GenArbWaveform(channel: rp_channel_t, waveform: *mut f32, length: u32) -> c_int
{
    todo!()
}

pub unsafe fn rp_GenBurstCount(channel: rp_channel_t, num: c_int) -> c_int
{
    state!().gen.burst_count[channel as usize] = num;

    ok!()
}

pub unsafe fn rp_GenBurstPeriod(channel: rp_channel_t, period: u32) -> c_int
{
    state!().gen.burst_period[channel as usize] = period;

    ok!()
}

pub unsafe fn rp_GenBurstRepetitions(channel: rp_channel_t, repetitions: c_int) -> c_int
{
    state!().gen.burst_repetitions[channel as usize] = repetitions;

    ok!()
}

pub unsafe fn rp_GenDutyCycle(channel: rp_channel_t, ratio: f32) -> c_int
{
    state!().gen.duty_cycle[channel as usize] = ratio;

    ok!()
}

pub unsafe fn rp_GenFreq(channel: rp_channel_t, frequency: f32) -> c_int
{
    state!().gen.freq[channel as usize] = frequency;

    ok!()
}

pub unsafe fn rp_GenGetAmp(channel: rp_channel_t, amplitude: *mut f32) -> c_int
{
    *amplitude = state!().gen.amp[channel as usize];

    ok!()
}

pub unsafe fn rp_GenGetArbWaveform(channel: rp_channel_t, waveform: *mut f32, length: *mut u32) -> c_int
{
    todo!()
}

pub unsafe fn rp_GenGetBurstCount(channel: rp_channel_t, num: *mut c_int) -> c_int
{
    *num = state!().gen.burst_count[channel as usize];

    ok!()
}

pub unsafe fn rp_GenGetBurstPeriod(channel: rp_channel_t, period: *mut u32) -> c_int
{
    *period = state!().gen.burst_period[channel as usize];

    ok!()
}

pub unsafe fn rp_GenGetBurstRepetitions(channel: rp_channel_t, repetitions: *mut c_int) -> c_int
{
    *repetitions = state!().gen.burst_repetitions[channel as usize];

    ok!()
}

pub unsafe fn rp_GenGetDutyCycle(channel: rp_channel_t, ratio: *mut f32) -> c_int
{
    *ratio = state!().gen.duty_cycle[channel as usize];

    ok!()
}

pub unsafe fn rp_GenGetFreq(channel: rp_channel_t, frequency: *mut f32) -> c_int
{
    *frequency = state!().gen.freq[channel as usize];

    ok!()
}

pub unsafe fn rp_GenGetMode(channel: rp_channel_t, mode: *mut rp_gen_mode_t) -> c_int
{
    *mode = state!().gen.modes[channel as usize];

    ok!()
}

pub unsafe fn rp_GenGetOffset(channel: rp_channel_t, offset: *mut f32) -> c_int
{
    *offset = state!().gen.offsets[channel as usize];

    ok!()
}

pub unsafe fn rp_GenGetPhase(channel: rp_channel_t, phase: *mut f32) -> c_int
{
    *phase = state!().gen.phases[channel as usize];

    ok!()
}

pub unsafe fn rp_GenGetTriggerSource(channel: rp_channel_t, src: *mut rp_trig_src_t) -> c_int
{
    *src = state!().gen.trigger_src[channel as usize];

    ok!()
}

pub unsafe fn rp_GenGetWaveform(channel: rp_channel_t, type_: *mut rp_waveform_t) -> c_int
{
    *type_ = state!().gen.waveforms[channel as usize];

    ok!()
}

pub unsafe fn rp_GenMode(channel: rp_channel_t, mode: rp_gen_mode_t) -> c_int
{
    state!().gen.modes[channel as usize] = mode;

    ok!()
}

pub unsafe fn rp_GenOffset(channel: rp_channel_t, offset: f32) -> c_int
{
    state!().gen.offsets[channel as usize] = offset;

    ok!()
}

pub unsafe fn rp_GenOutDisable(channel: rp_channel_t) -> c_int
{
    state!().gen.out_enable[channel as usize] = false;

    ok!()
}

pub unsafe fn rp_GenOutEnable(channel: rp_channel_t) -> c_int
{
    state!().gen.out_enable[channel as usize] = true;

    ok!()
}

pub unsafe fn rp_GenOutIsEnabled(channel: rp_channel_t, value: *mut bool) -> c_int
{
    *value = state!().gen.out_enable[channel as usize];

    ok!()
}

pub unsafe fn rp_GenPhase(channel: rp_channel_t, phase: f32) -> c_int
{
    state!().gen.phases[channel as usize] = phase;

    ok!()
}

pub unsafe fn rp_GenReset() -> c_int
{
    state!().gen = Default::default();

    ok!()
}

pub unsafe fn rp_GenTrigger(channel: u32) -> c_int
{
    ok!()
}

pub unsafe fn rp_GenTriggerSource(channel: rp_channel_t, src: rp_trig_src_t) -> c_int
{
    state!().gen.trigger_src[channel as usize] = src;

    ok!()
}

pub unsafe fn rp_SetEnableTempProtection(channel: rp_channel_t, enable: bool) -> c_int
{
    RP_NOTS as c_int
}

pub unsafe fn rp_GetEnableTempProtection(channel: rp_channel_t, enable: &mut bool) -> c_int
{
    RP_NOTS as c_int
}

pub unsafe fn rp_SetLatchTempAlarm(channel: rp_channel_t, status: bool) -> c_int
{
    RP_NOTS as c_int
}

pub unsafe fn rp_GetLatchTempAlarm(channel: rp_channel_t, status: &mut bool) -> c_int
{
    RP_NOTS as c_int
}

pub unsafe fn rp_GetRuntimeTempAlarm(channel: rp_channel_t, status: &mut bool) -> c_int
{
    RP_NOTS as c_int
}

pub unsafe fn rp_GetPllControlEnable(enable: &mut bool) -> c_int
{
    RP_NOTS as c_int
}

pub unsafe fn rp_SetPllControlEnable(enable: bool) -> c_int
{
    RP_NOTS as c_int
}

pub unsafe fn rp_GetPllControlLocked(status: &mut bool) -> c_int
{
    RP_NOTS as c_int
}

pub unsafe fn rp_GenWaveform(channel: rp_channel_t, type_: rp_waveform_t) -> c_int
{
    state!().gen.waveforms[channel as usize] = type_;

    ok!()
}

pub unsafe fn rp_GetCalibrationSettings() -> rp_calib_params_t
{
    state!().calib
}

pub unsafe fn rp_GetError(errorCode: c_int) -> *const c_char
{
    let error = match errorCode as u32 {
        RP_OK => "OK",
        RP_EOED => "Failed to Open EEPROM Device.",
        RP_EOMD => "Failed to open memory device.",
        RP_ECMD => "Failed to close memory device.",
        RP_EMMD => "Failed to map memory device.",
        RP_EUMD => "Failed to unmap memory device.",
        RP_EOOR => "Value out of range.",
        RP_ELID => "LED input direction is not valid.",
        RP_EMRO => "Modifying read only filed is not allowed.",
        RP_EWIP => "Writing to input pin is not valid.",
        RP_EPN => "Invalid Pin number.",
        RP_UIA => "Uninitialized Input Argument.",
        RP_FCA => "Failed to Find Calibration Parameters.",
        RP_RCA => "Failed to Read Calibration Parameters.",
        RP_BTS => "Buffer too small",
        RP_EIPV => "Invalid parameter value",
        RP_EUF => "Unsupported Feature",
        RP_ENN => "Data not normalized",
        RP_EFOB => "Failed to open bus",
        RP_EFCB => "Failed to close bus",
        RP_EABA => "Failed to acquire bus access",
        RP_EFRB => "Failed to read from the bus",
        RP_EFWB => "Failed to write to the bus",
        _ => "Unknown error",
    };

    return_cstring!(error)
}

pub unsafe fn rp_GetVersion() -> *const c_char
{
    return_cstring!("0.00-0000 (unknown)")
}

pub unsafe fn rp_IdGetDNA(dna: *mut u64) -> c_int
{
    *dna = 1;

    ok!()
}

pub unsafe fn rp_IdGetID(id: *mut u32) -> c_int
{
    *id = 2;

    ok!()
}

pub unsafe fn rp_Init() -> c_int
{
    ok!()
}

pub unsafe fn rp_InitReset(reset: bool) -> c_int
{
    ok!()
}

pub unsafe fn rp_LEDGetState(state: *mut u32) -> c_int
{
    *state = state!().led_state;

    ok!()
}

pub unsafe fn rp_LEDSetState(state: u32) -> c_int
{
    state!().led_state = state;

    ok!()
}

pub unsafe fn rp_Release() -> c_int
{
    ok!()
}

pub unsafe fn rp_Reset() -> c_int
{
    *state!() = Default::default();

    ok!()
}