Debian Codegraph @

libs/lmh6521.cpp

raw symbols SCIP
1 /***********************************************************************
2 * This file programs the LMH6521 on board the langford device.
3 *
4 * (c) 2013 Per Vices Corporation
5 * See LICENSE.txt for license and copyright information.
6 **********************************************************************/
7
8 #include <iostream>
9 #include "lmh6521_regs.hpp"
10 #include <stdint.h>
11 #include <stdlib.h>
12 #include <fcntl.h>
13 #include <unistd.h>
14 #include <sys/ioctl.h>
15 #include <sys/stat.h>
16 #include "../langford_ioctl.h"
17
18
19 int WriteReg(std::string FileName, uint32_t reg_val);
20
21
22 int main(int argc, char **argv) {
23 double gain;
24 int channel;
25 int rc;
26 int verbosity;
27
28 //Initialize
29 lmh6521_regs_t lmh6521_regs;
30
31 if ( (argc < 4) || (argc > 5) ){
32 std::cerr << "Usage: " << std::endl;
33 std::cerr << "\t" << argv[0] << " [DEVICE_NAME] [CHANNEL] [GAIN] (OUTPUT)" << std::endl;
34 std::cerr << "[DEVICE_NAME] default is /dev/langford" << std::endl;
35 std::cerr << "[CHANNEL] = {0|1} to program I or Q channel." << std::endl;
36 std::cerr << "[GAIN] (0 <= GAIN <= 31.5), Set desired channel gain (31.5dB range across (-5.5..26)dB )" << std::endl;
37 std::cerr << "[OUTPUT] = {0|1|2} is the (optional) output format (0 = silent (default), 1 = verbose, 2=driver)" << std::endl;
38 return 1;
39 };
40
41 if ( (std::string(argv[2]) == "0") || (std::string(argv[2]) == "1") ){ channel = atof(argv[2]); }
42
43 else {
44 std::cerr << "Invalid Channel Selection: Valid values for [CHANNEL] are: {0|1}" << std::endl;
45 return 1;
46 };
47
48 if ( (atof(argv[3]) >= 0) && (atof(argv[3]) <= 31.5) ) { gain = atof(argv[3]); }
49 else {
50 std::cerr << "Invalid Gain: Valid values for [GAIN] is limited to set [0,31.5]" << std::endl;
51 return 1;
52 };
53
54 if ( (argc == 5) ){
55 if (std::string(argv[4]) == "0") { verbosity = 0; }
56 else if (std::string(argv[4]) == "1") { verbosity = 1; std::cout.precision(10); }
57 else if (std::string(argv[4]) == "2") { verbosity = 2; std::cout.precision(10); }
58 else { std::cerr << "Invalid output selection: 0 for verbose mode, 1 for driver"; return 1;};
59 }
60 else { verbosity = 0; };
61
62
63 gain = lmh6521_regs.set_gain(gain, channel);
64
65 rc = WriteReg(argv[1], lmh6521_regs.get_regs());
66
67 if (verbosity == 1){
68 std::cout << "Programming Channel: " << channel
69 << " => " << gain
70 << std::endl;
71 lmh6521_regs.show_regs();
72 std::cout << "Return code from io_ctl calls: " << rc << std::endl;
73 }
74 else if (verbosity == 2){
75 std::cout << channel
76 << " " << gain
77 << std::endl;
78 };
79
80 return rc;
81 };
82
83
84 int WriteReg(std::string FileName, uint32_t reg_val) {
85 int DevFD;
86 int val;
87 int rc = 0;
88 const unsigned long long val0 = 0, val1 = 1;
89 int i;
90
91 DevFD = open(FileName.c_str(), O_RDWR);
92 if (!DevFD) {
93 std::cerr << "Cannot open " << FileName << std::endl;
94 return 1;
95 };
96
97 rc |= ioctl(DevFD, IOCTL_SET_N7CS, &val0);
98 for (i = 0; i < 16; i++) {
99 rc |= ioctl(DevFD, IOCTL_SET_N7CLK, &val0);
100 val = (reg_val >> (15 - i)) & 0x01;
101 rc |= ioctl(DevFD, IOCTL_SET_N7SDI, &val);
102 rc |= ioctl(DevFD, IOCTL_SET_N7CLK, &val1);
103 };
104 rc |= ioctl(DevFD, IOCTL_SET_N7CLK, &val0);
105 rc |= ioctl(DevFD, IOCTL_SET_N7SDI, &val0);
106 rc |= ioctl(DevFD, IOCTL_SET_N7CS, &val1);
107
108 close(DevFD);
109
110 return rc;
111 };