| 1 | /*********************************************************************** |
| 2 | * The langford ADC utility. This utility programs the ADC1210S125 chips |
| 3 | * on board the langford device. |
| 4 | * |
| 5 | * (c) 2013 Per Vices Corporation |
| 6 | * |
| 7 | * See LICENSE.txt for license and copyright information. |
| 8 | ************************************************************************/ |
| 9 | |
| 10 | /*To add a new ioctl function, be sure to add it to the help message and the command handler*/ |
| 11 | |
| 12 | #include <iostream> |
| 13 | #include <string> |
| 14 | #include <fcntl.h> |
| 15 | #include <unistd.h> |
| 16 | #include <sys/ioctl.h> |
| 17 | #include <sys/stat.h> |
| 18 | #include <sys/types.h> |
| 19 | #include <stdlib.h> |
| 20 | #include "langford_ioctl.h" |
| 21 | #include "langford_spi.h" |
| 22 | |
| 23 | |
| 24 | using namespace std; |
| 25 | |
| 26 | |
| 27 | int main(int argc, char **argv) { |
| 28 | int DevFD; |
| 29 | int val; |
| 30 | int nCS, SClk, SDO; |
| 31 | int reg; |
| 32 | int rc = 0; |
| 33 | string CmdStr; |
| 34 | |
| 35 | if (argc != 5) { |
| 36 | cerr << "Usage:" << endl; |
| 37 | cerr << "\t" << argv[0] << " device_name ADC Function Value" << endl; |
| 38 | cerr << "ADC is either A or B" << endl; |
| 39 | cerr << "Pin can be any of:" << endl; |
| 40 | cerr << "\tRaw - Write 3 bytes verbatim" << endl; |
| 41 | cerr << "\tIntRef - Internal reference attenuation in dB (0 to 6), set to -1 to disable internal reference" << endl; |
| 42 | cerr << "\tClkSel - You can enable ADC clock interleaving by setting one ADC to ClkSel = 0 and the other ClkSel = 1" << endl; |
| 43 | return 1; |
| 44 | }; |
| 45 | |
| 46 | //Open character device for ioctl |
| 47 | DevFD = open(argv[1], O_RDWR); |
| 48 | if (!DevFD) { |
| 49 | cerr << "Cannot open " << argv[1] << endl; |
| 50 | return 1; |
| 51 | }; |
| 52 | |
| 53 | //Find which ADC user requested |
| 54 | CmdStr = argv[2]; |
| 55 | if (CmdStr == "A") { |
| 56 | nCS = IOCTL_SET_ADCAnCS; |
| 57 | SClk = IOCTL_SET_ADCASClk; |
| 58 | SDO = IOCTL_SET_ADCASDIO; |
| 59 | } else if (CmdStr == "B") { |
| 60 | nCS = IOCTL_SET_ADCBnCS; |
| 61 | SClk = IOCTL_SET_ADCBSClk; |
| 62 | SDO = IOCTL_SET_ADCBSDIO; |
| 63 | } else { |
| 64 | cerr << "Invalid ADC specified: " << CmdStr << endl; |
| 65 | return 1; |
| 66 | }; |
| 67 | |
| 68 | //Find which function user requested |
| 69 | CmdStr = argv[3]; |
| 70 | //Parse user value |
| 71 | val = strtoul(argv[4], 0, 0); |
| 72 | //Perform basic sanity checking on function and value |
| 73 | if (CmdStr == "Raw") { |
| 74 | reg = val & 0x00ffffff; |
| 75 | } else if (CmdStr == "IntRef") { |
| 76 | if (val == -1) { |
| 77 | cout << "Turning off internal reference" << endl; |
| 78 | reg = 0x000800; |
| 79 | } else if ((val >= 0) && (val <= 6)) { |
| 80 | cout << "Setting internal reference attenuation to " << val << "dB" << endl; |
| 81 | reg = 0x000808 | val; |
| 82 | } else { |
| 83 | cerr << "Invalid attenuation specified: " << val << endl; |
| 84 | return 1; |
| 85 | }; |
| 86 | } else if (CmdStr == "ClkSel") { |
| 87 | if ((val >= 0) && (val <= 1)) { |
| 88 | reg = 0x000601 | (val << 4); |
| 89 | } else { |
| 90 | cerr << "Invalid choice for ClkSel, valid choices are 0 or 1" << endl; |
| 91 | return 1; |
| 92 | }; |
| 93 | } else { |
| 94 | cerr << "Invalid function specified: " << CmdStr << endl; |
| 95 | return 1; |
| 96 | }; |
| 97 | |
| 98 | rc = LangordSpiWrite(DevFD, 3, reg, nCS, SClk, SDO); |
| 99 | cout << "Writing SPI value 0x" << hex << reg << dec << endl; |
| 100 | |
| 101 | close(DevFD); |
| 102 | |
| 103 | cout << "Return code from io_ctl calls: " << rc << endl; |
| 104 | |
| 105 | return rc; |
| 106 | }; |