| 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"cxx . . $ `<file>/langford_ioctl.h`/ |
| 21 | #include "langford_spi.h"cxx . . $ `<file>/langford_spi.h`/ |
| 22 | |
| 23 | |
| 24 | using namespace std; |
| 25 | |
| 26 | |
| 27 | int maincxx . . $ main(afcd3168fc1188a4).(int argc, char **argv) { |
| 28 | int DevFD; |
| 29 | int val; |
| 30 | int nCS, SClk, SDO; |
| 31 | int reg; |
| 32 | int rc = 0; |
| 33 | stringcxx . . $ std/string# CmdStrcxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).; |
| 34 | |
| 35 | if (argc != 5) { |
| 36 | cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Usage:" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 37 | cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\t" <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). argv[0] <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " device_name ADC Function Value" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 38 | cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "ADC is either A or B" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 39 | cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Pin can be any of:" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 40 | cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\tRaw - Write 3 bytes verbatim" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 41 | cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\tIntRef - Internal reference attenuation in dB (0 to 6), set to -1 to disable internal reference" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 42 | cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\tClkSel - You can enable ADC clock interleaving by setting one ADC to ClkSel = 0 and the other ClkSel = 1" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 43 | return 1; |
| 44 | }; |
| 45 | |
| 46 | //Open character device for ioctl |
| 47 | DevFD = open(argv[1], O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`!); |
| 48 | if (!DevFD) { |
| 49 | cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Cannot open " <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). argv[1] <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 50 | return 1; |
| 51 | }; |
| 52 | |
| 53 | //Find which ADC user requested |
| 54 | CmdStr =cxx . . $ std/__cxx11/basic_string#`operator=`(978ca1133b5f6e74). argv[2]; |
| 55 | if (CmdStr ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "A") { |
| 56 | nCS = IOCTL_SET_ADCAnCScxx . . $ `langford_ioctl.h:76:9`!; |
| 57 | SClk = IOCTL_SET_ADCASClkcxx . . $ `langford_ioctl.h:72:9`!; |
| 58 | SDO = IOCTL_SET_ADCASDIOcxx . . $ `langford_ioctl.h:74:9`!; |
| 59 | } else if (CmdStr ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "B") { |
| 60 | nCS = IOCTL_SET_ADCBnCScxx . . $ `langford_ioctl.h:86:9`!; |
| 61 | SClk = IOCTL_SET_ADCBSClkcxx . . $ `langford_ioctl.h:82:9`!; |
| 62 | SDO = IOCTL_SET_ADCBSDIOcxx . . $ `langford_ioctl.h:84:9`!; |
| 63 | } else { |
| 64 | cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Invalid ADC specified: " <<cxx . . $ std/`operator<<`(88df5bef833f7f83). CmdStr <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 65 | return 1; |
| 66 | }; |
| 67 | |
| 68 | //Find which function user requested |
| 69 | CmdStr =cxx . . $ std/__cxx11/basic_string#`operator=`(978ca1133b5f6e74). argv[3]; |
| 70 | //Parse user value |
| 71 | val = strtoulcxx . . $ strtoul(335a43d87d12a1b6).(argv[4], 0, 0); |
| 72 | //Perform basic sanity checking on function and value |
| 73 | if (CmdStr ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "Raw") { |
| 74 | reg = val & 0x00ffffff; |
| 75 | } else if (CmdStr ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "IntRef") { |
| 76 | if (val == -1) { |
| 77 | coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Turning off internal reference" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 78 | reg = 0x000800; |
| 79 | } else if ((val >= 0) && (val <= 6)) { |
| 80 | coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Setting internal reference attenuation to " <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). val <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "dB" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 81 | reg = 0x000808 | val; |
| 82 | } else { |
| 83 | cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Invalid attenuation specified: " <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). val <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 84 | return 1; |
| 85 | }; |
| 86 | } else if (CmdStr ==cxx . . $ std/`operator==`(4b17aa87c9f300c6). "ClkSel") { |
| 87 | if ((val >= 0) && (val <= 1)) { |
| 88 | reg = 0x000601 | (val << 4); |
| 89 | } else { |
| 90 | cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Invalid choice for ClkSel, valid choices are 0 or 1" <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 91 | return 1; |
| 92 | }; |
| 93 | } else { |
| 94 | cerrcxx . . $ std/cerr. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Invalid function specified: " <<cxx . . $ std/`operator<<`(88df5bef833f7f83). CmdStr <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 95 | return 1; |
| 96 | }; |
| 97 | |
| 98 | rc = LangordSpiWritecxx . . $ LangordSpiWrite(4ccc28f389ac356e).(DevFD, 3, reg, nCS, SClk, SDO); |
| 99 | coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Writing SPI value 0x" <<cxx . . $ std/basic_ostream#`operator<<`(d90e0e305b0e25c2). hexcxx . . $ std/hex(cf36cc481190cd1f). <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). reg <<cxx . . $ std/basic_ostream#`operator<<`(d90e0e305b0e25c2). deccxx . . $ std/dec(cf36cc481190cd1f). <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 100 | |
| 101 | close(DevFD); |
| 102 | |
| 103 | coutcxx . . $ std/cout. <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "Return code from io_ctl calls: " <<cxx . . $ std/basic_ostream#`operator<<`(23b6575433a77e57). rc <<cxx . . $ std/basic_ostream#`operator<<`(f46f7e25851d787a). endlcxx . . $ std/endl(345bf97a19cc83a9).; |
| 104 | |
| 105 | return rc; |
| 106 | }; |